1 2 #include <petsc-private/tsimpl.h> /*I "petscts.h" I*/ 3 #include <petscdmshell.h> 4 5 /* Logging support */ 6 PetscClassId TS_CLASSID, DMTS_CLASSID; 7 PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 8 9 const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 10 11 #undef __FUNCT__ 12 #define __FUNCT__ "TSSetTypeFromOptions" 13 /* 14 TSSetTypeFromOptions - Sets the type of ts from user options. 15 16 Collective on TS 17 18 Input Parameter: 19 . ts - The ts 20 21 Level: intermediate 22 23 .keywords: TS, set, options, database, type 24 .seealso: TSSetFromOptions(), TSSetType() 25 */ 26 static PetscErrorCode TSSetTypeFromOptions(TS ts) 27 { 28 PetscBool opt; 29 const char *defaultType; 30 char typeName[256]; 31 PetscErrorCode ierr; 32 33 PetscFunctionBegin; 34 if (((PetscObject)ts)->type_name) { 35 defaultType = ((PetscObject)ts)->type_name; 36 } else { 37 defaultType = TSEULER; 38 } 39 40 if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 41 ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 42 if (opt) { 43 ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 44 } else { 45 ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 46 } 47 PetscFunctionReturn(0); 48 } 49 50 #undef __FUNCT__ 51 #define __FUNCT__ "TSSetFromOptions" 52 /*@ 53 TSSetFromOptions - Sets various TS parameters from user options. 54 55 Collective on TS 56 57 Input Parameter: 58 . ts - the TS context obtained from TSCreate() 59 60 Options Database Keys: 61 + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP 62 . -ts_max_steps maxsteps - maximum number of time-steps to take 63 . -ts_final_time time - maximum time to compute to 64 . -ts_dt dt - initial time step 65 . -ts_monitor - print information at each timestep 66 . -ts_monitor_lg_timestep - Monitor timestep size graphically 67 . -ts_monitor_lg_solution - Monitor solution graphically 68 . -ts_monitor_lg_error - Monitor error graphically 69 . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 70 . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 71 . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 72 . -ts_monitor_draw_solution - Monitor solution graphically 73 . -ts_monitor_draw_solution - Monitor solution graphically 74 . -ts_monitor_draw_error - Monitor error graphically 75 . -ts_monitor_draw_solution_binary <filename> - Save each solution to a binary file 76 - -ts_monitor_draw_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 77 78 Level: beginner 79 80 .keywords: TS, timestep, set, options, database 81 82 .seealso: TSGetType() 83 @*/ 84 PetscErrorCode TSSetFromOptions(TS ts) 85 { 86 PetscBool opt,flg; 87 PetscErrorCode ierr; 88 PetscViewer monviewer; 89 char monfilename[PETSC_MAX_PATH_LEN]; 90 SNES snes; 91 TSAdapt adapt; 92 PetscReal time_step; 93 TSExactFinalTimeOption eftopt; 94 char dir[16]; 95 96 PetscFunctionBegin; 97 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 98 ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 99 /* Handle TS type options */ 100 ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr); 101 102 /* Handle generic TS options */ 103 ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr); 104 ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr); 105 ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr); 106 ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 107 if (flg) { 108 ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 109 } 110 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); 111 if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 112 ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,PETSC_NULL);CHKERRQ(ierr); 113 ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,PETSC_NULL);CHKERRQ(ierr); 114 ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr); 115 ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,PETSC_NULL);CHKERRQ(ierr); 116 ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,PETSC_NULL);CHKERRQ(ierr); 117 118 /* Monitor options */ 119 ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 120 if (flg) { 121 ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr); 122 ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 123 } 124 ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 125 if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 126 127 ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 128 if (opt) { 129 TSMonitorLGCtx ctx; 130 PetscInt howoften = 1; 131 132 ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 133 ierr = TSMonitorLGCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 134 ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 135 } 136 ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 137 if (opt) { 138 TSMonitorLGCtx ctx; 139 PetscInt howoften = 1; 140 141 ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 142 ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 143 ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 144 } 145 ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 146 if (opt) { 147 TSMonitorLGCtx ctx; 148 PetscInt howoften = 1; 149 150 ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 151 ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 152 ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 153 } 154 ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 155 if (opt) { 156 TSMonitorLGCtx ctx; 157 PetscInt howoften = 1; 158 159 ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 160 ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 161 ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 162 } 163 ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 164 if (opt) { 165 TSMonitorLGCtx ctx; 166 PetscInt howoften = 1; 167 168 ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 169 ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 170 ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 171 } 172 ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 173 if (opt) { 174 TSMonitorSPEigCtx ctx; 175 PetscInt howoften = 1; 176 177 ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 178 ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 179 ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 180 } 181 opt = PETSC_FALSE; 182 ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 183 if (opt) { 184 TSMonitorDrawCtx ctx; 185 PetscInt howoften = 1; 186 187 ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 188 ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 189 ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 190 } 191 opt = PETSC_FALSE; 192 ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 193 if (opt) { 194 TSMonitorDrawCtx ctx; 195 PetscInt howoften = 1; 196 197 ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 198 ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 199 ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 200 } 201 opt = PETSC_FALSE; 202 ierr = PetscOptionsString("-ts_monitor_draw_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 203 if (flg) { 204 PetscViewer ctx; 205 if (monfilename[0]) { 206 ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 207 } else { 208 ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm); 209 } 210 ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 211 } 212 opt = PETSC_FALSE; 213 ierr = PetscOptionsString("-ts_monitor_draw_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 214 if (flg) { 215 const char *ptr,*ptr2; 216 char *filetemplate; 217 if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_draw_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 218 /* Do some cursory validation of the input. */ 219 ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 220 if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_draw_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 221 for (ptr++ ; ptr && *ptr; ptr++) { 222 ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 223 if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_draw_solution_vtk, should look like filename-%%03D.vts"); 224 if (ptr2) break; 225 } 226 ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 227 ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 228 } 229 230 ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 231 if (flg) { 232 TSMonitorDMDARayCtx *rayctx; 233 int ray = 0; 234 DMDADirection ddir; 235 DM da; 236 PetscMPIInt rank; 237 238 if (dir[1] != '=') SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 239 if (dir[0] == 'x') ddir = DMDA_X; 240 else if (dir[0] == 'y') ddir = DMDA_Y; 241 else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 242 sscanf(dir+2,"%d",&ray); 243 244 ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 245 ierr = PetscNew(TSMonitorDMDARayCtx,&rayctx);CHKERRQ(ierr); 246 ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 247 ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 248 ierr = MPI_Comm_rank(((PetscObject)ts)->comm,&rank);CHKERRQ(ierr); 249 if (!rank) { 250 ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 251 } 252 ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 253 } 254 255 ierr = TSGetTSAdapt(ts,&adapt);CHKERRQ(ierr); 256 ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr); 257 258 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 259 if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 260 261 /* Handle specific TS options */ 262 if (ts->ops->setfromoptions) { 263 ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr); 264 } 265 266 /* process any options handlers added with PetscObjectAddOptionsHandler() */ 267 ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 268 ierr = PetscOptionsEnd();CHKERRQ(ierr); 269 PetscFunctionReturn(0); 270 } 271 272 #undef __FUNCT__ 273 #undef __FUNCT__ 274 #define __FUNCT__ "TSComputeRHSJacobian" 275 /*@ 276 TSComputeRHSJacobian - Computes the Jacobian matrix that has been 277 set with TSSetRHSJacobian(). 278 279 Collective on TS and Vec 280 281 Input Parameters: 282 + ts - the TS context 283 . t - current timestep 284 - U - input vector 285 286 Output Parameters: 287 + A - Jacobian matrix 288 . B - optional preconditioning matrix 289 - flag - flag indicating matrix structure 290 291 Notes: 292 Most users should not need to explicitly call this routine, as it 293 is used internally within the nonlinear solvers. 294 295 See KSPSetOperators() for important information about setting the 296 flag parameter. 297 298 Level: developer 299 300 .keywords: SNES, compute, Jacobian, matrix 301 302 .seealso: TSSetRHSJacobian(), KSPSetOperators() 303 @*/ 304 PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg) 305 { 306 PetscErrorCode ierr; 307 PetscInt Ustate; 308 DM dm; 309 DMTS tsdm; 310 TSRHSJacobian rhsjacobianfunc; 311 void *ctx; 312 TSIJacobian ijacobianfunc; 313 314 PetscFunctionBegin; 315 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 316 PetscValidHeaderSpecific(U,VEC_CLASSID,3); 317 PetscCheckSameComm(ts,1,U,3); 318 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 319 ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 320 ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 321 ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr); 322 ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr); 323 if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) { 324 *flg = ts->rhsjacobian.mstructure; 325 PetscFunctionReturn(0); 326 } 327 328 if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 329 330 if (rhsjacobianfunc) { 331 ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 332 *flg = DIFFERENT_NONZERO_PATTERN; 333 PetscStackPush("TS user Jacobian function"); 334 ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr); 335 PetscStackPop; 336 ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 337 /* make sure user returned a correct Jacobian and preconditioner */ 338 PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 339 PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 340 } else { 341 ierr = MatZeroEntries(*A);CHKERRQ(ierr); 342 if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);} 343 *flg = SAME_NONZERO_PATTERN; 344 } 345 ts->rhsjacobian.time = t; 346 ts->rhsjacobian.X = U; 347 ierr = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 348 ts->rhsjacobian.mstructure = *flg; 349 PetscFunctionReturn(0); 350 } 351 352 #undef __FUNCT__ 353 #define __FUNCT__ "TSComputeRHSFunction" 354 /*@ 355 TSComputeRHSFunction - Evaluates the right-hand-side function. 356 357 Collective on TS and Vec 358 359 Input Parameters: 360 + ts - the TS context 361 . t - current time 362 - U - state vector 363 364 Output Parameter: 365 . y - right hand side 366 367 Note: 368 Most users should not need to explicitly call this routine, as it 369 is used internally within the nonlinear solvers. 370 371 Level: developer 372 373 .keywords: TS, compute 374 375 .seealso: TSSetRHSFunction(), TSComputeIFunction() 376 @*/ 377 PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 378 { 379 PetscErrorCode ierr; 380 TSRHSFunction rhsfunction; 381 TSIFunction ifunction; 382 void *ctx; 383 DM dm; 384 385 PetscFunctionBegin; 386 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 387 PetscValidHeaderSpecific(U,VEC_CLASSID,3); 388 PetscValidHeaderSpecific(y,VEC_CLASSID,4); 389 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 390 ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 391 ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr); 392 393 if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 394 395 ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 396 if (rhsfunction) { 397 PetscStackPush("TS user right-hand-side function"); 398 ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 399 PetscStackPop; 400 } else { 401 ierr = VecZeroEntries(y);CHKERRQ(ierr); 402 } 403 404 ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 405 PetscFunctionReturn(0); 406 } 407 408 #undef __FUNCT__ 409 #define __FUNCT__ "TSComputeSolutionFunction" 410 /*@ 411 TSComputeSolutionFunction - Evaluates the solution function. 412 413 Collective on TS and Vec 414 415 Input Parameters: 416 + ts - the TS context 417 - t - current time 418 419 Output Parameter: 420 . U - the solution 421 422 Note: 423 Most users should not need to explicitly call this routine, as it 424 is used internally within the nonlinear solvers. 425 426 Level: developer 427 428 .keywords: TS, compute 429 430 .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 431 @*/ 432 PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 433 { 434 PetscErrorCode ierr; 435 TSSolutionFunction solutionfunction; 436 void *ctx; 437 DM dm; 438 439 PetscFunctionBegin; 440 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 441 PetscValidHeaderSpecific(U,VEC_CLASSID,3); 442 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 443 ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 444 445 if (solutionfunction) { 446 PetscStackPush("TS user right-hand-side function"); 447 ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 448 PetscStackPop; 449 } 450 PetscFunctionReturn(0); 451 } 452 453 #undef __FUNCT__ 454 #define __FUNCT__ "TSGetRHSVec_Private" 455 static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 456 { 457 Vec F; 458 PetscErrorCode ierr; 459 460 PetscFunctionBegin; 461 *Frhs = PETSC_NULL; 462 ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 463 if (!ts->Frhs) { 464 ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 465 } 466 *Frhs = ts->Frhs; 467 PetscFunctionReturn(0); 468 } 469 470 #undef __FUNCT__ 471 #define __FUNCT__ "TSGetRHSMats_Private" 472 static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 473 { 474 Mat A,B; 475 PetscErrorCode ierr; 476 477 PetscFunctionBegin; 478 ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 479 if (Arhs) { 480 if (!ts->Arhs) { 481 ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 482 } 483 *Arhs = ts->Arhs; 484 } 485 if (Brhs) { 486 if (!ts->Brhs) { 487 ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 488 } 489 *Brhs = ts->Brhs; 490 } 491 PetscFunctionReturn(0); 492 } 493 494 #undef __FUNCT__ 495 #define __FUNCT__ "TSComputeIFunction" 496 /*@ 497 TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 498 499 Collective on TS and Vec 500 501 Input Parameters: 502 + ts - the TS context 503 . t - current time 504 . U - state vector 505 . Udot - time derivative of state vector 506 - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 507 508 Output Parameter: 509 . Y - right hand side 510 511 Note: 512 Most users should not need to explicitly call this routine, as it 513 is used internally within the nonlinear solvers. 514 515 If the user did did not write their equations in implicit form, this 516 function recasts them in implicit form. 517 518 Level: developer 519 520 .keywords: TS, compute 521 522 .seealso: TSSetIFunction(), TSComputeRHSFunction() 523 @*/ 524 PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 525 { 526 PetscErrorCode ierr; 527 TSIFunction ifunction; 528 TSRHSFunction rhsfunction; 529 void *ctx; 530 DM dm; 531 532 PetscFunctionBegin; 533 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 534 PetscValidHeaderSpecific(U,VEC_CLASSID,3); 535 PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 536 PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 537 538 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 539 ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 540 ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr); 541 542 if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 543 544 ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 545 if (ifunction) { 546 PetscStackPush("TS user implicit function"); 547 ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 548 PetscStackPop; 549 } 550 if (imex) { 551 if (!ifunction) { 552 ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 553 } 554 } else if (rhsfunction) { 555 if (ifunction) { 556 Vec Frhs; 557 ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 558 ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 559 ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 560 } else { 561 ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 562 ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 563 } 564 } 565 ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 566 PetscFunctionReturn(0); 567 } 568 569 #undef __FUNCT__ 570 #define __FUNCT__ "TSComputeIJacobian" 571 /*@ 572 TSComputeIJacobian - Evaluates the Jacobian of the DAE 573 574 Collective on TS and Vec 575 576 Input 577 Input Parameters: 578 + ts - the TS context 579 . t - current timestep 580 . U - state vector 581 . Udot - time derivative of state vector 582 . shift - shift to apply, see note below 583 - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 584 585 Output Parameters: 586 + A - Jacobian matrix 587 . B - optional preconditioning matrix 588 - flag - flag indicating matrix structure 589 590 Notes: 591 If F(t,U,Udot)=0 is the DAE, the required Jacobian is 592 593 dF/dU + shift*dF/dUdot 594 595 Most users should not need to explicitly call this routine, as it 596 is used internally within the nonlinear solvers. 597 598 Level: developer 599 600 .keywords: TS, compute, Jacobian, matrix 601 602 .seealso: TSSetIJacobian() 603 @*/ 604 PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex) 605 { 606 PetscInt Ustate, Udotstate; 607 PetscErrorCode ierr; 608 TSIJacobian ijacobian; 609 TSRHSJacobian rhsjacobian; 610 DM dm; 611 void *ctx; 612 613 PetscFunctionBegin; 614 615 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 616 PetscValidHeaderSpecific(U,VEC_CLASSID,3); 617 PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 618 PetscValidPointer(A,6); 619 PetscValidHeaderSpecific(*A,MAT_CLASSID,6); 620 PetscValidPointer(B,7); 621 PetscValidHeaderSpecific(*B,MAT_CLASSID,7); 622 PetscValidPointer(flg,8); 623 624 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 625 ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 626 ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr); 627 628 ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr); 629 ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr); 630 if (ts->ijacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->ijacobian.X == U && ts->ijacobian.Xstate == Ustate && ts->ijacobian.Xdot == Udot && ts->ijacobian.Xdotstate == Udotstate && ts->ijacobian.imex == imex))) { 631 *flg = ts->ijacobian.mstructure; 632 ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 633 PetscFunctionReturn(0); 634 } 635 636 if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 637 638 *flg = SAME_NONZERO_PATTERN; /* In case we're solving a linear problem in which case it wouldn't get initialized below. */ 639 ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 640 if (ijacobian) { 641 *flg = DIFFERENT_NONZERO_PATTERN; 642 PetscStackPush("TS user implicit Jacobian"); 643 ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr); 644 PetscStackPop; 645 /* make sure user returned a correct Jacobian and preconditioner */ 646 PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 647 PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 648 } 649 if (imex) { 650 if (!ijacobian) { /* system was written as Udot = G(t,U) */ 651 ierr = MatZeroEntries(*A);CHKERRQ(ierr); 652 ierr = MatShift(*A,shift);CHKERRQ(ierr); 653 if (*A != *B) { 654 ierr = MatZeroEntries(*B);CHKERRQ(ierr); 655 ierr = MatShift(*B,shift);CHKERRQ(ierr); 656 } 657 *flg = SAME_PRECONDITIONER; 658 } 659 } else { 660 if (!ijacobian) { 661 ierr = TSComputeRHSJacobian(ts,t,U,A,B,flg);CHKERRQ(ierr); 662 ierr = MatScale(*A,-1);CHKERRQ(ierr); 663 ierr = MatShift(*A,shift);CHKERRQ(ierr); 664 if (*A != *B) { 665 ierr = MatScale(*B,-1);CHKERRQ(ierr); 666 ierr = MatShift(*B,shift);CHKERRQ(ierr); 667 } 668 } else if (rhsjacobian) { 669 Mat Arhs,Brhs; 670 MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN; 671 ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 672 ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 673 axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN; 674 ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr); 675 if (*A != *B) { 676 ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr); 677 } 678 *flg = PetscMin(*flg,flg2); 679 } 680 } 681 682 ts->ijacobian.time = t; 683 ts->ijacobian.X = U; 684 ts->ijacobian.Xdot = Udot; 685 ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr); 686 ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr); 687 ts->ijacobian.shift = shift; 688 ts->ijacobian.imex = imex; 689 ts->ijacobian.mstructure = *flg; 690 ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 691 PetscFunctionReturn(0); 692 } 693 694 #undef __FUNCT__ 695 #define __FUNCT__ "TSSetRHSFunction" 696 /*@C 697 TSSetRHSFunction - Sets the routine for evaluating the function, 698 where U_t = G(t,u). 699 700 Logically Collective on TS 701 702 Input Parameters: 703 + ts - the TS context obtained from TSCreate() 704 . r - vector to put the computed right hand side (or PETSC_NULL to have it created) 705 . f - routine for evaluating the right-hand-side function 706 - ctx - [optional] user-defined context for private data for the 707 function evaluation routine (may be PETSC_NULL) 708 709 Calling sequence of func: 710 $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 711 712 + t - current timestep 713 . u - input vector 714 . F - function vector 715 - ctx - [optional] user-defined function context 716 717 Level: beginner 718 719 .keywords: TS, timestep, set, right-hand-side, function 720 721 .seealso: TSSetRHSJacobian(), TSSetIJacobian() 722 @*/ 723 PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 724 { 725 PetscErrorCode ierr; 726 SNES snes; 727 Vec ralloc = PETSC_NULL; 728 DM dm; 729 730 PetscFunctionBegin; 731 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 732 if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 733 734 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 735 ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 736 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 737 if (!r && !ts->dm && ts->vec_sol) { 738 ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 739 r = ralloc; 740 } 741 ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 742 ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 743 PetscFunctionReturn(0); 744 } 745 746 #undef __FUNCT__ 747 #define __FUNCT__ "TSSetSolutionFunction" 748 /*@C 749 TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 750 751 Logically Collective on TS 752 753 Input Parameters: 754 + ts - the TS context obtained from TSCreate() 755 . f - routine for evaluating the solution 756 - ctx - [optional] user-defined context for private data for the 757 function evaluation routine (may be PETSC_NULL) 758 759 Calling sequence of func: 760 $ func (TS ts,PetscReal t,Vec u,void *ctx); 761 762 + t - current timestep 763 . u - output vector 764 - ctx - [optional] user-defined function context 765 766 Notes: 767 This routine is used for testing accuracy of time integration schemes when you already know the solution. 768 If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 769 create closed-form solutions with non-physical forcing terms. 770 771 For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 772 773 Level: beginner 774 775 .keywords: TS, timestep, set, right-hand-side, function 776 777 .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction() 778 @*/ 779 PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 780 { 781 PetscErrorCode ierr; 782 DM dm; 783 784 PetscFunctionBegin; 785 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 786 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 787 ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 788 PetscFunctionReturn(0); 789 } 790 791 #undef __FUNCT__ 792 #define __FUNCT__ "TSSetRHSJacobian" 793 /*@C 794 TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 795 where U_t = G(U,t), as well as the location to store the matrix. 796 797 Logically Collective on TS 798 799 Input Parameters: 800 + ts - the TS context obtained from TSCreate() 801 . A - Jacobian matrix 802 . B - preconditioner matrix (usually same as A) 803 . f - the Jacobian evaluation routine 804 - ctx - [optional] user-defined context for private data for the 805 Jacobian evaluation routine (may be PETSC_NULL) 806 807 Calling sequence of func: 808 $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 809 810 + t - current timestep 811 . u - input vector 812 . A - matrix A, where U_t = A(t)u 813 . B - preconditioner matrix, usually the same as A 814 . flag - flag indicating information about the preconditioner matrix 815 structure (same as flag in KSPSetOperators()) 816 - ctx - [optional] user-defined context for matrix evaluation routine 817 818 Notes: 819 See KSPSetOperators() for important information about setting the flag 820 output parameter in the routine func(). Be sure to read this information! 821 822 The routine func() takes Mat * as the matrix arguments rather than Mat. 823 This allows the matrix evaluation routine to replace A and/or B with a 824 completely new matrix structure (not just different matrix elements) 825 when appropriate, for instance, if the nonzero structure is changing 826 throughout the global iterations. 827 828 Level: beginner 829 830 .keywords: TS, timestep, set, right-hand-side, Jacobian 831 832 .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction() 833 834 @*/ 835 PetscErrorCode TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx) 836 { 837 PetscErrorCode ierr; 838 SNES snes; 839 DM dm; 840 TSIJacobian ijacobian; 841 842 PetscFunctionBegin; 843 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 844 if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 845 if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 846 if (A) PetscCheckSameComm(ts,1,A,2); 847 if (B) PetscCheckSameComm(ts,1,B,3); 848 849 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 850 ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 851 ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr); 852 853 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 854 if (!ijacobian) { 855 ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 856 } 857 if (A) { 858 ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 859 ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 860 ts->Arhs = A; 861 } 862 if (B) { 863 ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 864 ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 865 ts->Brhs = B; 866 } 867 PetscFunctionReturn(0); 868 } 869 870 871 #undef __FUNCT__ 872 #define __FUNCT__ "TSSetIFunction" 873 /*@C 874 TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 875 876 Logically Collective on TS 877 878 Input Parameters: 879 + ts - the TS context obtained from TSCreate() 880 . r - vector to hold the residual (or PETSC_NULL to have it created internally) 881 . f - the function evaluation routine 882 - ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL) 883 884 Calling sequence of f: 885 $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 886 887 + t - time at step/stage being solved 888 . u - state vector 889 . u_t - time derivative of state vector 890 . F - function vector 891 - ctx - [optional] user-defined context for matrix evaluation routine 892 893 Important: 894 The user MUST call either this routine, TSSetRHSFunction(). This routine must be used when not solving an ODE, for example a DAE. 895 896 Level: beginner 897 898 .keywords: TS, timestep, set, DAE, Jacobian 899 900 .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 901 @*/ 902 PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 903 { 904 PetscErrorCode ierr; 905 SNES snes; 906 Vec resalloc = PETSC_NULL; 907 DM dm; 908 909 PetscFunctionBegin; 910 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 911 if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 912 913 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 914 ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 915 916 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 917 if (!res && !ts->dm && ts->vec_sol) { 918 ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 919 res = resalloc; 920 } 921 ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 922 ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 923 924 PetscFunctionReturn(0); 925 } 926 927 #undef __FUNCT__ 928 #define __FUNCT__ "TSGetIFunction" 929 /*@C 930 TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 931 932 Not Collective 933 934 Input Parameter: 935 . ts - the TS context 936 937 Output Parameter: 938 + r - vector to hold residual (or PETSC_NULL) 939 . func - the function to compute residual (or PETSC_NULL) 940 - ctx - the function context (or PETSC_NULL) 941 942 Level: advanced 943 944 .keywords: TS, nonlinear, get, function 945 946 .seealso: TSSetIFunction(), SNESGetFunction() 947 @*/ 948 PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 949 { 950 PetscErrorCode ierr; 951 SNES snes; 952 DM dm; 953 954 PetscFunctionBegin; 955 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 956 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 957 ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 958 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 959 ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 960 PetscFunctionReturn(0); 961 } 962 963 #undef __FUNCT__ 964 #define __FUNCT__ "TSGetRHSFunction" 965 /*@C 966 TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 967 968 Not Collective 969 970 Input Parameter: 971 . ts - the TS context 972 973 Output Parameter: 974 + r - vector to hold computed right hand side (or PETSC_NULL) 975 . func - the function to compute right hand side (or PETSC_NULL) 976 - ctx - the function context (or PETSC_NULL) 977 978 Level: advanced 979 980 .keywords: TS, nonlinear, get, function 981 982 .seealso: TSSetRhsfunction(), SNESGetFunction() 983 @*/ 984 PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 985 { 986 PetscErrorCode ierr; 987 SNES snes; 988 DM dm; 989 990 PetscFunctionBegin; 991 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 992 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 993 ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 994 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 995 ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 996 PetscFunctionReturn(0); 997 } 998 999 #undef __FUNCT__ 1000 #define __FUNCT__ "TSSetIJacobian" 1001 /*@C 1002 TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1003 you provided with TSSetIFunction(). 1004 1005 Logically Collective on TS 1006 1007 Input Parameters: 1008 + ts - the TS context obtained from TSCreate() 1009 . A - Jacobian matrix 1010 . B - preconditioning matrix for A (may be same as A) 1011 . f - the Jacobian evaluation routine 1012 - ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL) 1013 1014 Calling sequence of f: 1015 $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx); 1016 1017 + t - time at step/stage being solved 1018 . U - state vector 1019 . U_t - time derivative of state vector 1020 . a - shift 1021 . A - Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1022 . B - preconditioning matrix for A, may be same as A 1023 . flag - flag indicating information about the preconditioner matrix 1024 structure (same as flag in KSPSetOperators()) 1025 - ctx - [optional] user-defined context for matrix evaluation routine 1026 1027 Notes: 1028 The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve. 1029 1030 The matrix dF/dU + a*dF/dU_t you provide turns out to be 1031 the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1032 The time integrator internally approximates U_t by W+a*U where the positive "shift" 1033 a and vector W depend on the integration method, step size, and past states. For example with 1034 the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1035 W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1036 1037 Level: beginner 1038 1039 .keywords: TS, timestep, DAE, Jacobian 1040 1041 .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian() 1042 1043 @*/ 1044 PetscErrorCode TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx) 1045 { 1046 PetscErrorCode ierr; 1047 SNES snes; 1048 DM dm; 1049 1050 PetscFunctionBegin; 1051 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1052 if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 1053 if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 1054 if (A) PetscCheckSameComm(ts,1,A,2); 1055 if (B) PetscCheckSameComm(ts,1,B,3); 1056 1057 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1058 ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 1059 1060 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1061 ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1062 PetscFunctionReturn(0); 1063 } 1064 1065 #undef __FUNCT__ 1066 #define __FUNCT__ "TSLoad" 1067 /*@C 1068 TSLoad - Loads a KSP that has been stored in binary with KSPView(). 1069 1070 Collective on PetscViewer 1071 1072 Input Parameters: 1073 + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 1074 some related function before a call to TSLoad(). 1075 - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 1076 1077 Level: intermediate 1078 1079 Notes: 1080 The type is determined by the data in the file, any type set into the TS before this call is ignored. 1081 1082 Notes for advanced users: 1083 Most users should not need to know the details of the binary storage 1084 format, since TSLoad() and TSView() completely hide these details. 1085 But for anyone who's interested, the standard binary matrix storage 1086 format is 1087 .vb 1088 has not yet been determined 1089 .ve 1090 1091 .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 1092 @*/ 1093 PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 1094 { 1095 PetscErrorCode ierr; 1096 PetscBool isbinary; 1097 PetscInt classid; 1098 char type[256]; 1099 DMTS sdm; 1100 DM dm; 1101 1102 PetscFunctionBegin; 1103 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1104 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1105 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 1106 if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 1107 1108 ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 1109 if (classid != TS_FILE_CLASSID) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1110 ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 1111 ierr = TSSetType(ts, type);CHKERRQ(ierr); 1112 if (ts->ops->load) { 1113 ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1114 } 1115 ierr = DMCreate(((PetscObject)ts)->comm,&dm);CHKERRQ(ierr); 1116 ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1117 ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1118 ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1119 ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 1120 ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 1121 ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 1122 PetscFunctionReturn(0); 1123 } 1124 1125 #undef __FUNCT__ 1126 #define __FUNCT__ "TSView" 1127 /*@C 1128 TSView - Prints the TS data structure. 1129 1130 Collective on TS 1131 1132 Input Parameters: 1133 + ts - the TS context obtained from TSCreate() 1134 - viewer - visualization context 1135 1136 Options Database Key: 1137 . -ts_view - calls TSView() at end of TSStep() 1138 1139 Notes: 1140 The available visualization contexts include 1141 + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1142 - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1143 output where only the first processor opens 1144 the file. All other processors send their 1145 data to the first processor to print. 1146 1147 The user can open an alternative visualization context with 1148 PetscViewerASCIIOpen() - output to a specified file. 1149 1150 Level: beginner 1151 1152 .keywords: TS, timestep, view 1153 1154 .seealso: PetscViewerASCIIOpen() 1155 @*/ 1156 PetscErrorCode TSView(TS ts,PetscViewer viewer) 1157 { 1158 PetscErrorCode ierr; 1159 TSType type; 1160 PetscBool iascii,isstring,isundials,isbinary,isdraw; 1161 DMTS sdm; 1162 1163 PetscFunctionBegin; 1164 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1165 if (!viewer) { 1166 ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr); 1167 } 1168 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1169 PetscCheckSameComm(ts,1,viewer,2); 1170 1171 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1172 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 1173 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 1174 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1175 if (iascii) { 1176 ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr); 1177 ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 1178 ierr = PetscViewerASCIIPrintf(viewer," maximum time=%G\n",ts->max_time);CHKERRQ(ierr); 1179 if (ts->problem_type == TS_NONLINEAR) { 1180 ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1181 ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1182 } 1183 ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1184 ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 1185 ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 1186 ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1187 if (ts->ops->view) { 1188 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1189 ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1190 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1191 } 1192 } else if (isstring) { 1193 ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1194 ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 1195 } else if (isbinary) { 1196 PetscInt classid = TS_FILE_CLASSID; 1197 MPI_Comm comm; 1198 PetscMPIInt rank; 1199 char type[256]; 1200 1201 ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 1202 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 1203 if (!rank) { 1204 ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 1205 ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 1206 ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 1207 } 1208 if (ts->ops->view) { 1209 ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1210 } 1211 ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1212 ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 1213 ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 1214 ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1215 } else if (isdraw) { 1216 PetscDraw draw; 1217 char str[36]; 1218 PetscReal x,y,bottom,h; 1219 1220 ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 1221 ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 1222 ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 1223 ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 1224 ierr = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,PETSC_NULL,&h);CHKERRQ(ierr); 1225 bottom = y - h; 1226 ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 1227 if (ts->ops->view) { 1228 ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1229 } 1230 ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1231 } 1232 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1233 ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1234 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1235 PetscFunctionReturn(0); 1236 } 1237 1238 1239 #undef __FUNCT__ 1240 #define __FUNCT__ "TSSetApplicationContext" 1241 /*@ 1242 TSSetApplicationContext - Sets an optional user-defined context for 1243 the timesteppers. 1244 1245 Logically Collective on TS 1246 1247 Input Parameters: 1248 + ts - the TS context obtained from TSCreate() 1249 - usrP - optional user context 1250 1251 Level: intermediate 1252 1253 .keywords: TS, timestep, set, application, context 1254 1255 .seealso: TSGetApplicationContext() 1256 @*/ 1257 PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1258 { 1259 PetscFunctionBegin; 1260 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1261 ts->user = usrP; 1262 PetscFunctionReturn(0); 1263 } 1264 1265 #undef __FUNCT__ 1266 #define __FUNCT__ "TSGetApplicationContext" 1267 /*@ 1268 TSGetApplicationContext - Gets the user-defined context for the 1269 timestepper. 1270 1271 Not Collective 1272 1273 Input Parameter: 1274 . ts - the TS context obtained from TSCreate() 1275 1276 Output Parameter: 1277 . usrP - user context 1278 1279 Level: intermediate 1280 1281 .keywords: TS, timestep, get, application, context 1282 1283 .seealso: TSSetApplicationContext() 1284 @*/ 1285 PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1286 { 1287 PetscFunctionBegin; 1288 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1289 *(void**)usrP = ts->user; 1290 PetscFunctionReturn(0); 1291 } 1292 1293 #undef __FUNCT__ 1294 #define __FUNCT__ "TSGetTimeStepNumber" 1295 /*@ 1296 TSGetTimeStepNumber - Gets the number of time steps completed. 1297 1298 Not Collective 1299 1300 Input Parameter: 1301 . ts - the TS context obtained from TSCreate() 1302 1303 Output Parameter: 1304 . iter - number of steps completed so far 1305 1306 Level: intermediate 1307 1308 .keywords: TS, timestep, get, iteration, number 1309 .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep() 1310 @*/ 1311 PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt* iter) 1312 { 1313 PetscFunctionBegin; 1314 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1315 PetscValidIntPointer(iter,2); 1316 *iter = ts->steps; 1317 PetscFunctionReturn(0); 1318 } 1319 1320 #undef __FUNCT__ 1321 #define __FUNCT__ "TSSetInitialTimeStep" 1322 /*@ 1323 TSSetInitialTimeStep - Sets the initial timestep to be used, 1324 as well as the initial time. 1325 1326 Logically Collective on TS 1327 1328 Input Parameters: 1329 + ts - the TS context obtained from TSCreate() 1330 . initial_time - the initial time 1331 - time_step - the size of the timestep 1332 1333 Level: intermediate 1334 1335 .seealso: TSSetTimeStep(), TSGetTimeStep() 1336 1337 .keywords: TS, set, initial, timestep 1338 @*/ 1339 PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1340 { 1341 PetscErrorCode ierr; 1342 1343 PetscFunctionBegin; 1344 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1345 ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1346 ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1347 PetscFunctionReturn(0); 1348 } 1349 1350 #undef __FUNCT__ 1351 #define __FUNCT__ "TSSetTimeStep" 1352 /*@ 1353 TSSetTimeStep - Allows one to reset the timestep at any time, 1354 useful for simple pseudo-timestepping codes. 1355 1356 Logically Collective on TS 1357 1358 Input Parameters: 1359 + ts - the TS context obtained from TSCreate() 1360 - time_step - the size of the timestep 1361 1362 Level: intermediate 1363 1364 .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1365 1366 .keywords: TS, set, timestep 1367 @*/ 1368 PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1369 { 1370 PetscFunctionBegin; 1371 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1372 PetscValidLogicalCollectiveReal(ts,time_step,2); 1373 ts->time_step = time_step; 1374 ts->time_step_orig = time_step; 1375 PetscFunctionReturn(0); 1376 } 1377 1378 #undef __FUNCT__ 1379 #define __FUNCT__ "TSSetExactFinalTime" 1380 /*@ 1381 TSSetExactFinalTime - Determines whether to adapt the final time step to 1382 match the exact final time, interpolate solution to the exact final time, 1383 or just return at the final time TS computed. 1384 1385 Logically Collective on TS 1386 1387 Input Parameter: 1388 + ts - the time-step context 1389 - eftopt - exact final time option 1390 1391 Level: beginner 1392 1393 .seealso: TSExactFinalTimeOption 1394 @*/ 1395 PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 1396 { 1397 PetscFunctionBegin; 1398 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1399 PetscValidLogicalCollectiveEnum(ts,eftopt,2); 1400 ts->exact_final_time = eftopt; 1401 PetscFunctionReturn(0); 1402 } 1403 1404 #undef __FUNCT__ 1405 #define __FUNCT__ "TSGetTimeStep" 1406 /*@ 1407 TSGetTimeStep - Gets the current timestep size. 1408 1409 Not Collective 1410 1411 Input Parameter: 1412 . ts - the TS context obtained from TSCreate() 1413 1414 Output Parameter: 1415 . dt - the current timestep size 1416 1417 Level: intermediate 1418 1419 .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1420 1421 .keywords: TS, get, timestep 1422 @*/ 1423 PetscErrorCode TSGetTimeStep(TS ts,PetscReal* dt) 1424 { 1425 PetscFunctionBegin; 1426 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1427 PetscValidRealPointer(dt,2); 1428 *dt = ts->time_step; 1429 PetscFunctionReturn(0); 1430 } 1431 1432 #undef __FUNCT__ 1433 #define __FUNCT__ "TSGetSolution" 1434 /*@ 1435 TSGetSolution - Returns the solution at the present timestep. It 1436 is valid to call this routine inside the function that you are evaluating 1437 in order to move to the new timestep. This vector not changed until 1438 the solution at the next timestep has been calculated. 1439 1440 Not Collective, but Vec returned is parallel if TS is parallel 1441 1442 Input Parameter: 1443 . ts - the TS context obtained from TSCreate() 1444 1445 Output Parameter: 1446 . v - the vector containing the solution 1447 1448 Level: intermediate 1449 1450 .seealso: TSGetTimeStep() 1451 1452 .keywords: TS, timestep, get, solution 1453 @*/ 1454 PetscErrorCode TSGetSolution(TS ts,Vec *v) 1455 { 1456 PetscFunctionBegin; 1457 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1458 PetscValidPointer(v,2); 1459 *v = ts->vec_sol; 1460 PetscFunctionReturn(0); 1461 } 1462 1463 /* ----- Routines to initialize and destroy a timestepper ---- */ 1464 #undef __FUNCT__ 1465 #define __FUNCT__ "TSSetProblemType" 1466 /*@ 1467 TSSetProblemType - Sets the type of problem to be solved. 1468 1469 Not collective 1470 1471 Input Parameters: 1472 + ts - The TS 1473 - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1474 .vb 1475 U_t - A U = 0 (linear) 1476 U_t - A(t) U = 0 (linear) 1477 F(t,U,U_t) = 0 (nonlinear) 1478 .ve 1479 1480 Level: beginner 1481 1482 .keywords: TS, problem type 1483 .seealso: TSSetUp(), TSProblemType, TS 1484 @*/ 1485 PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1486 { 1487 PetscErrorCode ierr; 1488 1489 PetscFunctionBegin; 1490 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1491 ts->problem_type = type; 1492 if (type == TS_LINEAR) { 1493 SNES snes; 1494 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1495 ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 1496 } 1497 PetscFunctionReturn(0); 1498 } 1499 1500 #undef __FUNCT__ 1501 #define __FUNCT__ "TSGetProblemType" 1502 /*@C 1503 TSGetProblemType - Gets the type of problem to be solved. 1504 1505 Not collective 1506 1507 Input Parameter: 1508 . ts - The TS 1509 1510 Output Parameter: 1511 . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1512 .vb 1513 M U_t = A U 1514 M(t) U_t = A(t) U 1515 F(t,U,U_t) 1516 .ve 1517 1518 Level: beginner 1519 1520 .keywords: TS, problem type 1521 .seealso: TSSetUp(), TSProblemType, TS 1522 @*/ 1523 PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1524 { 1525 PetscFunctionBegin; 1526 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1527 PetscValidIntPointer(type,2); 1528 *type = ts->problem_type; 1529 PetscFunctionReturn(0); 1530 } 1531 1532 #undef __FUNCT__ 1533 #define __FUNCT__ "TSSetUp" 1534 /*@ 1535 TSSetUp - Sets up the internal data structures for the later use 1536 of a timestepper. 1537 1538 Collective on TS 1539 1540 Input Parameter: 1541 . ts - the TS context obtained from TSCreate() 1542 1543 Notes: 1544 For basic use of the TS solvers the user need not explicitly call 1545 TSSetUp(), since these actions will automatically occur during 1546 the call to TSStep(). However, if one wishes to control this 1547 phase separately, TSSetUp() should be called after TSCreate() 1548 and optional routines of the form TSSetXXX(), but before TSStep(). 1549 1550 Level: advanced 1551 1552 .keywords: TS, timestep, setup 1553 1554 .seealso: TSCreate(), TSStep(), TSDestroy() 1555 @*/ 1556 PetscErrorCode TSSetUp(TS ts) 1557 { 1558 PetscErrorCode ierr; 1559 DM dm; 1560 PetscErrorCode (*func)(SNES,Vec,Vec,void*); 1561 PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*); 1562 TSIJacobian ijac; 1563 TSRHSJacobian rhsjac; 1564 1565 PetscFunctionBegin; 1566 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1567 if (ts->setupcalled) PetscFunctionReturn(0); 1568 1569 if (!((PetscObject)ts)->type_name) { 1570 ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1571 } 1572 1573 if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1574 1575 ierr = TSGetTSAdapt(ts,&ts->adapt);CHKERRQ(ierr); 1576 1577 if (ts->ops->setup) { 1578 ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1579 } 1580 1581 /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 1582 to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 1583 */ 1584 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1585 ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr); 1586 if (!func) { 1587 ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 1588 } 1589 /* if the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it. 1590 Otherwise, the SNES will use coloring internally to form the Jacobian. 1591 */ 1592 ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr); 1593 ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr); 1594 ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr); 1595 if (!jac && (ijac || rhsjac)) { 1596 ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1597 } 1598 ts->setupcalled = PETSC_TRUE; 1599 PetscFunctionReturn(0); 1600 } 1601 1602 #undef __FUNCT__ 1603 #define __FUNCT__ "TSReset" 1604 /*@ 1605 TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1606 1607 Collective on TS 1608 1609 Input Parameter: 1610 . ts - the TS context obtained from TSCreate() 1611 1612 Level: beginner 1613 1614 .keywords: TS, timestep, reset 1615 1616 .seealso: TSCreate(), TSSetup(), TSDestroy() 1617 @*/ 1618 PetscErrorCode TSReset(TS ts) 1619 { 1620 PetscErrorCode ierr; 1621 1622 PetscFunctionBegin; 1623 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1624 if (ts->ops->reset) { 1625 ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1626 } 1627 if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 1628 ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1629 ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1630 ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 1631 ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1632 ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 1633 ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 1634 ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 1635 ts->setupcalled = PETSC_FALSE; 1636 PetscFunctionReturn(0); 1637 } 1638 1639 #undef __FUNCT__ 1640 #define __FUNCT__ "TSDestroy" 1641 /*@ 1642 TSDestroy - Destroys the timestepper context that was created 1643 with TSCreate(). 1644 1645 Collective on TS 1646 1647 Input Parameter: 1648 . ts - the TS context obtained from TSCreate() 1649 1650 Level: beginner 1651 1652 .keywords: TS, timestepper, destroy 1653 1654 .seealso: TSCreate(), TSSetUp(), TSSolve() 1655 @*/ 1656 PetscErrorCode TSDestroy(TS *ts) 1657 { 1658 PetscErrorCode ierr; 1659 1660 PetscFunctionBegin; 1661 if (!*ts) PetscFunctionReturn(0); 1662 PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 1663 if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1664 1665 ierr = TSReset((*ts));CHKERRQ(ierr); 1666 1667 /* if memory was published with AMS then destroy it */ 1668 ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 1669 if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 1670 1671 ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 1672 ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 1673 ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 1674 ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 1675 1676 ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1677 PetscFunctionReturn(0); 1678 } 1679 1680 #undef __FUNCT__ 1681 #define __FUNCT__ "TSGetSNES" 1682 /*@ 1683 TSGetSNES - Returns the SNES (nonlinear solver) associated with 1684 a TS (timestepper) context. Valid only for nonlinear problems. 1685 1686 Not Collective, but SNES is parallel if TS is parallel 1687 1688 Input Parameter: 1689 . ts - the TS context obtained from TSCreate() 1690 1691 Output Parameter: 1692 . snes - the nonlinear solver context 1693 1694 Notes: 1695 The user can then directly manipulate the SNES context to set various 1696 options, etc. Likewise, the user can then extract and manipulate the 1697 KSP, KSP, and PC contexts as well. 1698 1699 TSGetSNES() does not work for integrators that do not use SNES; in 1700 this case TSGetSNES() returns PETSC_NULL in snes. 1701 1702 Level: beginner 1703 1704 .keywords: timestep, get, SNES 1705 @*/ 1706 PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1707 { 1708 PetscErrorCode ierr; 1709 1710 PetscFunctionBegin; 1711 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1712 PetscValidPointer(snes,2); 1713 if (!ts->snes) { 1714 ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 1715 ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 1716 ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1717 ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 1718 if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 1719 if (ts->problem_type == TS_LINEAR) { 1720 ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 1721 } 1722 } 1723 *snes = ts->snes; 1724 PetscFunctionReturn(0); 1725 } 1726 1727 #undef __FUNCT__ 1728 #define __FUNCT__ "TSSetSNES" 1729 /*@ 1730 TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 1731 1732 Collective 1733 1734 Input Parameter: 1735 + ts - the TS context obtained from TSCreate() 1736 - snes - the nonlinear solver context 1737 1738 Notes: 1739 Most users should have the TS created by calling TSGetSNES() 1740 1741 Level: developer 1742 1743 .keywords: timestep, set, SNES 1744 @*/ 1745 PetscErrorCode TSSetSNES(TS ts,SNES snes) 1746 { 1747 PetscErrorCode ierr; 1748 PetscErrorCode (*func)(SNES,Vec,Mat *,Mat *,MatStructure *,void *); 1749 1750 PetscFunctionBegin; 1751 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1752 PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 1753 ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 1754 ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 1755 ts->snes = snes; 1756 ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 1757 ierr = SNESGetJacobian(ts->snes,PETSC_NULL,PETSC_NULL,&func,PETSC_NULL);CHKERRQ(ierr); 1758 if (func == SNESTSFormJacobian) { 1759 ierr = SNESSetJacobian(ts->snes,PETSC_NULL,PETSC_NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1760 } 1761 PetscFunctionReturn(0); 1762 } 1763 1764 #undef __FUNCT__ 1765 #define __FUNCT__ "TSGetKSP" 1766 /*@ 1767 TSGetKSP - Returns the KSP (linear solver) associated with 1768 a TS (timestepper) context. 1769 1770 Not Collective, but KSP is parallel if TS is parallel 1771 1772 Input Parameter: 1773 . ts - the TS context obtained from TSCreate() 1774 1775 Output Parameter: 1776 . ksp - the nonlinear solver context 1777 1778 Notes: 1779 The user can then directly manipulate the KSP context to set various 1780 options, etc. Likewise, the user can then extract and manipulate the 1781 KSP and PC contexts as well. 1782 1783 TSGetKSP() does not work for integrators that do not use KSP; 1784 in this case TSGetKSP() returns PETSC_NULL in ksp. 1785 1786 Level: beginner 1787 1788 .keywords: timestep, get, KSP 1789 @*/ 1790 PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1791 { 1792 PetscErrorCode ierr; 1793 SNES snes; 1794 1795 PetscFunctionBegin; 1796 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1797 PetscValidPointer(ksp,2); 1798 if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1799 if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1800 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1801 ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1802 PetscFunctionReturn(0); 1803 } 1804 1805 /* ----------- Routines to set solver parameters ---------- */ 1806 1807 #undef __FUNCT__ 1808 #define __FUNCT__ "TSGetDuration" 1809 /*@ 1810 TSGetDuration - Gets the maximum number of timesteps to use and 1811 maximum time for iteration. 1812 1813 Not Collective 1814 1815 Input Parameters: 1816 + ts - the TS context obtained from TSCreate() 1817 . maxsteps - maximum number of iterations to use, or PETSC_NULL 1818 - maxtime - final time to iterate to, or PETSC_NULL 1819 1820 Level: intermediate 1821 1822 .keywords: TS, timestep, get, maximum, iterations, time 1823 @*/ 1824 PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1825 { 1826 PetscFunctionBegin; 1827 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1828 if (maxsteps) { 1829 PetscValidIntPointer(maxsteps,2); 1830 *maxsteps = ts->max_steps; 1831 } 1832 if (maxtime) { 1833 PetscValidScalarPointer(maxtime,3); 1834 *maxtime = ts->max_time; 1835 } 1836 PetscFunctionReturn(0); 1837 } 1838 1839 #undef __FUNCT__ 1840 #define __FUNCT__ "TSSetDuration" 1841 /*@ 1842 TSSetDuration - Sets the maximum number of timesteps to use and 1843 maximum time for iteration. 1844 1845 Logically Collective on TS 1846 1847 Input Parameters: 1848 + ts - the TS context obtained from TSCreate() 1849 . maxsteps - maximum number of iterations to use 1850 - maxtime - final time to iterate to 1851 1852 Options Database Keys: 1853 . -ts_max_steps <maxsteps> - Sets maxsteps 1854 . -ts_final_time <maxtime> - Sets maxtime 1855 1856 Notes: 1857 The default maximum number of iterations is 5000. Default time is 5.0 1858 1859 Level: intermediate 1860 1861 .keywords: TS, timestep, set, maximum, iterations 1862 1863 .seealso: TSSetExactFinalTime() 1864 @*/ 1865 PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1866 { 1867 PetscFunctionBegin; 1868 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1869 PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1870 PetscValidLogicalCollectiveReal(ts,maxtime,2); 1871 if (maxsteps >= 0) ts->max_steps = maxsteps; 1872 if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 1873 PetscFunctionReturn(0); 1874 } 1875 1876 #undef __FUNCT__ 1877 #define __FUNCT__ "TSSetSolution" 1878 /*@ 1879 TSSetSolution - Sets the initial solution vector 1880 for use by the TS routines. 1881 1882 Logically Collective on TS and Vec 1883 1884 Input Parameters: 1885 + ts - the TS context obtained from TSCreate() 1886 - u - the solution vector 1887 1888 Level: beginner 1889 1890 .keywords: TS, timestep, set, solution, initial conditions 1891 @*/ 1892 PetscErrorCode TSSetSolution(TS ts,Vec u) 1893 { 1894 PetscErrorCode ierr; 1895 DM dm; 1896 1897 PetscFunctionBegin; 1898 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1899 PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1900 ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 1901 ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1902 ts->vec_sol = u; 1903 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1904 ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 1905 PetscFunctionReturn(0); 1906 } 1907 1908 #undef __FUNCT__ 1909 #define __FUNCT__ "TSSetPreStep" 1910 /*@C 1911 TSSetPreStep - Sets the general-purpose function 1912 called once at the beginning of each time step. 1913 1914 Logically Collective on TS 1915 1916 Input Parameters: 1917 + ts - The TS context obtained from TSCreate() 1918 - func - The function 1919 1920 Calling sequence of func: 1921 . func (TS ts); 1922 1923 Level: intermediate 1924 1925 Note: 1926 If a step is rejected, TSStep() will call this routine again before each attempt. 1927 The last completed time step number can be queried using TSGetTimeStepNumber(), the 1928 size of the step being attempted can be obtained using TSGetTimeStep(). 1929 1930 .keywords: TS, timestep 1931 .seealso: TSSetPreStage(), TSSetPostStep(), TSStep() 1932 @*/ 1933 PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1934 { 1935 PetscFunctionBegin; 1936 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1937 ts->ops->prestep = func; 1938 PetscFunctionReturn(0); 1939 } 1940 1941 #undef __FUNCT__ 1942 #define __FUNCT__ "TSPreStep" 1943 /*@ 1944 TSPreStep - Runs the user-defined pre-step function. 1945 1946 Collective on TS 1947 1948 Input Parameters: 1949 . ts - The TS context obtained from TSCreate() 1950 1951 Notes: 1952 TSPreStep() is typically used within time stepping implementations, 1953 so most users would not generally call this routine themselves. 1954 1955 Level: developer 1956 1957 .keywords: TS, timestep 1958 .seealso: TSSetPreStep(), TSPreStage(), TSPostStep() 1959 @*/ 1960 PetscErrorCode TSPreStep(TS ts) 1961 { 1962 PetscErrorCode ierr; 1963 1964 PetscFunctionBegin; 1965 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1966 if (ts->ops->prestep) { 1967 PetscStackPush("TS PreStep function"); 1968 ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 1969 PetscStackPop; 1970 } 1971 PetscFunctionReturn(0); 1972 } 1973 1974 #undef __FUNCT__ 1975 #define __FUNCT__ "TSSetPreStage" 1976 /*@C 1977 TSSetPreStage - Sets the general-purpose function 1978 called once at the beginning of each stage. 1979 1980 Logically Collective on TS 1981 1982 Input Parameters: 1983 + ts - The TS context obtained from TSCreate() 1984 - func - The function 1985 1986 Calling sequence of func: 1987 . PetscErrorCode func(TS ts, PetscReal stagetime); 1988 1989 Level: intermediate 1990 1991 Note: 1992 There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 1993 The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 1994 attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 1995 1996 .keywords: TS, timestep 1997 .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 1998 @*/ 1999 PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 2000 { 2001 PetscFunctionBegin; 2002 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2003 ts->ops->prestage = func; 2004 PetscFunctionReturn(0); 2005 } 2006 2007 #undef __FUNCT__ 2008 #define __FUNCT__ "TSPreStage" 2009 /*@ 2010 TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 2011 2012 Collective on TS 2013 2014 Input Parameters: 2015 . ts - The TS context obtained from TSCreate() 2016 2017 Notes: 2018 TSPreStage() is typically used within time stepping implementations, 2019 most users would not generally call this routine themselves. 2020 2021 Level: developer 2022 2023 .keywords: TS, timestep 2024 .seealso: TSSetPreStep(), TSPreStep(), TSPostStep() 2025 @*/ 2026 PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 2027 { 2028 PetscErrorCode ierr; 2029 2030 PetscFunctionBegin; 2031 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2032 if (ts->ops->prestage) { 2033 PetscStackPush("TS PreStage function"); 2034 ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr); 2035 PetscStackPop; 2036 } 2037 PetscFunctionReturn(0); 2038 } 2039 2040 #undef __FUNCT__ 2041 #define __FUNCT__ "TSSetPostStep" 2042 /*@C 2043 TSSetPostStep - Sets the general-purpose function 2044 called once at the end of each time step. 2045 2046 Logically Collective on TS 2047 2048 Input Parameters: 2049 + ts - The TS context obtained from TSCreate() 2050 - func - The function 2051 2052 Calling sequence of func: 2053 $ func (TS ts); 2054 2055 Level: intermediate 2056 2057 .keywords: TS, timestep 2058 .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 2059 @*/ 2060 PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 2061 { 2062 PetscFunctionBegin; 2063 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2064 ts->ops->poststep = func; 2065 PetscFunctionReturn(0); 2066 } 2067 2068 #undef __FUNCT__ 2069 #define __FUNCT__ "TSPostStep" 2070 /*@ 2071 TSPostStep - Runs the user-defined post-step function. 2072 2073 Collective on TS 2074 2075 Input Parameters: 2076 . ts - The TS context obtained from TSCreate() 2077 2078 Notes: 2079 TSPostStep() is typically used within time stepping implementations, 2080 so most users would not generally call this routine themselves. 2081 2082 Level: developer 2083 2084 .keywords: TS, timestep 2085 @*/ 2086 PetscErrorCode TSPostStep(TS ts) 2087 { 2088 PetscErrorCode ierr; 2089 2090 PetscFunctionBegin; 2091 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2092 if (ts->ops->poststep) { 2093 PetscStackPush("TS PostStep function"); 2094 ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 2095 PetscStackPop; 2096 } 2097 PetscFunctionReturn(0); 2098 } 2099 2100 /* ------------ Routines to set performance monitoring options ----------- */ 2101 2102 #undef __FUNCT__ 2103 #define __FUNCT__ "TSMonitorSet" 2104 /*@C 2105 TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 2106 timestep to display the iteration's progress. 2107 2108 Logically Collective on TS 2109 2110 Input Parameters: 2111 + ts - the TS context obtained from TSCreate() 2112 . monitor - monitoring routine 2113 . mctx - [optional] user-defined context for private data for the 2114 monitor routine (use PETSC_NULL if no context is desired) 2115 - monitordestroy - [optional] routine that frees monitor context 2116 (may be PETSC_NULL) 2117 2118 Calling sequence of monitor: 2119 $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 2120 2121 + ts - the TS context 2122 . 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 2123 been interpolated to) 2124 . time - current time 2125 . u - current iterate 2126 - mctx - [optional] monitoring context 2127 2128 Notes: 2129 This routine adds an additional monitor to the list of monitors that 2130 already has been loaded. 2131 2132 Fortran notes: Only a single monitor function can be set for each TS object 2133 2134 Level: intermediate 2135 2136 .keywords: TS, timestep, set, monitor 2137 2138 .seealso: TSMonitorDefault(), TSMonitorCancel() 2139 @*/ 2140 PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 2141 { 2142 PetscFunctionBegin; 2143 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2144 if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2145 ts->monitor[ts->numbermonitors] = monitor; 2146 ts->monitordestroy[ts->numbermonitors] = mdestroy; 2147 ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 2148 PetscFunctionReturn(0); 2149 } 2150 2151 #undef __FUNCT__ 2152 #define __FUNCT__ "TSMonitorCancel" 2153 /*@C 2154 TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 2155 2156 Logically Collective on TS 2157 2158 Input Parameters: 2159 . ts - the TS context obtained from TSCreate() 2160 2161 Notes: 2162 There is no way to remove a single, specific monitor. 2163 2164 Level: intermediate 2165 2166 .keywords: TS, timestep, set, monitor 2167 2168 .seealso: TSMonitorDefault(), TSMonitorSet() 2169 @*/ 2170 PetscErrorCode TSMonitorCancel(TS ts) 2171 { 2172 PetscErrorCode ierr; 2173 PetscInt i; 2174 2175 PetscFunctionBegin; 2176 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2177 for (i=0; i<ts->numbermonitors; i++) { 2178 if (ts->monitordestroy[i]) { 2179 ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 2180 } 2181 } 2182 ts->numbermonitors = 0; 2183 PetscFunctionReturn(0); 2184 } 2185 2186 #undef __FUNCT__ 2187 #define __FUNCT__ "TSMonitorDefault" 2188 /*@ 2189 TSMonitorDefault - Sets the Default monitor 2190 2191 Level: intermediate 2192 2193 .keywords: TS, set, monitor 2194 2195 .seealso: TSMonitorDefault(), TSMonitorSet() 2196 @*/ 2197 PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 2198 { 2199 PetscErrorCode ierr; 2200 PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm); 2201 2202 PetscFunctionBegin; 2203 ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2204 ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr); 2205 ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2206 PetscFunctionReturn(0); 2207 } 2208 2209 #undef __FUNCT__ 2210 #define __FUNCT__ "TSSetRetainStages" 2211 /*@ 2212 TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 2213 2214 Logically Collective on TS 2215 2216 Input Argument: 2217 . ts - time stepping context 2218 2219 Output Argument: 2220 . flg - PETSC_TRUE or PETSC_FALSE 2221 2222 Level: intermediate 2223 2224 .keywords: TS, set 2225 2226 .seealso: TSInterpolate(), TSSetPostStep() 2227 @*/ 2228 PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 2229 { 2230 PetscFunctionBegin; 2231 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2232 ts->retain_stages = flg; 2233 PetscFunctionReturn(0); 2234 } 2235 2236 #undef __FUNCT__ 2237 #define __FUNCT__ "TSInterpolate" 2238 /*@ 2239 TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 2240 2241 Collective on TS 2242 2243 Input Argument: 2244 + ts - time stepping context 2245 - t - time to interpolate to 2246 2247 Output Argument: 2248 . U - state at given time 2249 2250 Notes: 2251 The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 2252 2253 Level: intermediate 2254 2255 Developer Notes: 2256 TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 2257 2258 .keywords: TS, set 2259 2260 .seealso: TSSetRetainStages(), TSSetPostStep() 2261 @*/ 2262 PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 2263 { 2264 PetscErrorCode ierr; 2265 2266 PetscFunctionBegin; 2267 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2268 if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Requested time %G not in last time steps [%G,%G]",t,ts->ptime-ts->time_step_prev,ts->ptime); 2269 if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 2270 ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 2271 PetscFunctionReturn(0); 2272 } 2273 2274 #undef __FUNCT__ 2275 #define __FUNCT__ "TSStep" 2276 /*@ 2277 TSStep - Steps one time step 2278 2279 Collective on TS 2280 2281 Input Parameter: 2282 . ts - the TS context obtained from TSCreate() 2283 2284 Level: intermediate 2285 2286 Notes: 2287 The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 2288 be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 2289 2290 This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 2291 time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 2292 2293 .keywords: TS, timestep, solve 2294 2295 .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 2296 @*/ 2297 PetscErrorCode TSStep(TS ts) 2298 { 2299 PetscReal ptime_prev; 2300 PetscErrorCode ierr; 2301 2302 PetscFunctionBegin; 2303 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2304 ierr = TSSetUp(ts);CHKERRQ(ierr); 2305 2306 ts->reason = TS_CONVERGED_ITERATING; 2307 2308 ptime_prev = ts->ptime; 2309 ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2310 ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 2311 ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2312 ts->time_step_prev = ts->ptime - ptime_prev; 2313 2314 if (ts->reason < 0) { 2315 if (ts->errorifstepfailed) { 2316 if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) { 2317 SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]); 2318 } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 2319 } 2320 } else if (!ts->reason) { 2321 if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 2322 else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 2323 } 2324 2325 PetscFunctionReturn(0); 2326 } 2327 2328 #undef __FUNCT__ 2329 #define __FUNCT__ "TSEvaluateStep" 2330 /*@ 2331 TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 2332 2333 Collective on TS 2334 2335 Input Arguments: 2336 + ts - time stepping context 2337 . order - desired order of accuracy 2338 - done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available) 2339 2340 Output Arguments: 2341 . U - state at the end of the current step 2342 2343 Level: advanced 2344 2345 Notes: 2346 This function cannot be called until all stages have been evaluated. 2347 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. 2348 2349 .seealso: TSStep(), TSAdapt 2350 @*/ 2351 PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 2352 { 2353 PetscErrorCode ierr; 2354 2355 PetscFunctionBegin; 2356 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2357 PetscValidType(ts,1); 2358 PetscValidHeaderSpecific(U,VEC_CLASSID,3); 2359 if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 2360 ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 2361 PetscFunctionReturn(0); 2362 } 2363 2364 #undef __FUNCT__ 2365 #define __FUNCT__ "TSSolve" 2366 /*@ 2367 TSSolve - Steps the requested number of timesteps. 2368 2369 Collective on TS 2370 2371 Input Parameter: 2372 + ts - the TS context obtained from TSCreate() 2373 - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 2374 2375 Level: beginner 2376 2377 Notes: 2378 The final time returned by this function may be different from the time of the internally 2379 held state accessible by TSGetSolution() and TSGetTime() because the method may have 2380 stepped over the final time. 2381 2382 .keywords: TS, timestep, solve 2383 2384 .seealso: TSCreate(), TSSetSolution(), TSStep() 2385 @*/ 2386 PetscErrorCode TSSolve(TS ts,Vec u) 2387 { 2388 PetscBool flg; 2389 PetscViewer viewer; 2390 PetscErrorCode ierr; 2391 PetscViewerFormat format; 2392 2393 PetscFunctionBegin; 2394 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2395 if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 2396 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 */ 2397 if (!ts->vec_sol || u == ts->vec_sol) { 2398 Vec y; 2399 ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 2400 ierr = TSSetSolution(ts,y);CHKERRQ(ierr); 2401 ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */ 2402 } 2403 if (u) { 2404 ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 2405 } 2406 } else { 2407 if (u) { 2408 ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 2409 } 2410 } 2411 ierr = TSSetUp(ts);CHKERRQ(ierr); 2412 /* reset time step and iteration counters */ 2413 ts->steps = 0; 2414 ts->ksp_its = 0; 2415 ts->snes_its = 0; 2416 ts->num_snes_failures = 0; 2417 ts->reject = 0; 2418 ts->reason = TS_CONVERGED_ITERATING; 2419 2420 if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 2421 ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 2422 ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 2423 ts->solvetime = ts->ptime; 2424 } else { 2425 /* steps the requested number of timesteps. */ 2426 ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2427 if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 2428 else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 2429 while (!ts->reason) { 2430 ierr = TSStep(ts);CHKERRQ(ierr); 2431 ierr = TSPostStep(ts);CHKERRQ(ierr); 2432 ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2433 } 2434 if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 2435 ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 2436 ts->solvetime = ts->max_time; 2437 } else { 2438 if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 2439 ts->solvetime = ts->ptime; 2440 } 2441 } 2442 ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2443 ierr = PetscOptionsGetViewer(((PetscObject)ts)->comm,((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr); 2444 if (flg && !PetscPreLoadingOn) { 2445 ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); 2446 ierr = TSView(ts,viewer);CHKERRQ(ierr); 2447 ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 2448 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 2449 } 2450 PetscFunctionReturn(0); 2451 } 2452 2453 #undef __FUNCT__ 2454 #define __FUNCT__ "TSMonitor" 2455 /*@ 2456 TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 2457 2458 Collective on TS 2459 2460 Input Parameters: 2461 + ts - time stepping context obtained from TSCreate() 2462 . step - step number that has just completed 2463 . ptime - model time of the state 2464 - u - state at the current model time 2465 2466 Notes: 2467 TSMonitor() is typically used within the time stepping implementations. 2468 Users might call this function when using the TSStep() interface instead of TSSolve(). 2469 2470 Level: advanced 2471 2472 .keywords: TS, timestep 2473 @*/ 2474 PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 2475 { 2476 PetscErrorCode ierr; 2477 PetscInt i,n = ts->numbermonitors; 2478 2479 PetscFunctionBegin; 2480 for (i=0; i<n; i++) { 2481 ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 2482 } 2483 PetscFunctionReturn(0); 2484 } 2485 2486 /* ------------------------------------------------------------------------*/ 2487 struct _n_TSMonitorLGCtx { 2488 PetscDrawLG lg; 2489 PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 2490 PetscInt ksp_its,snes_its; 2491 }; 2492 2493 2494 #undef __FUNCT__ 2495 #define __FUNCT__ "TSMonitorLGCtxCreate" 2496 /*@C 2497 TSMonitorLGCtxCreate - Creates a line graph context for use with 2498 TS to monitor the solution process graphically in various ways 2499 2500 Collective on TS 2501 2502 Input Parameters: 2503 + host - the X display to open, or null for the local machine 2504 . label - the title to put in the title bar 2505 . x, y - the screen coordinates of the upper left coordinate of the window 2506 . m, n - the screen width and height in pixels 2507 - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 2508 2509 Output Parameter: 2510 . ctx - the context 2511 2512 Options Database Key: 2513 + -ts_monitor_lg_timestep - automatically sets line graph monitor 2514 . -ts_monitor_lg_solution - 2515 . -ts_monitor_lg_error - 2516 . -ts_monitor_lg_ksp_iterations - 2517 . -ts_monitor_lg_snes_iterations - 2518 - -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true 2519 2520 Notes: 2521 Use TSMonitorLGCtxDestroy() to destroy. 2522 2523 Level: intermediate 2524 2525 .keywords: TS, monitor, line graph, residual, seealso 2526 2527 .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 2528 2529 @*/ 2530 PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 2531 { 2532 PetscDraw win; 2533 PetscErrorCode ierr; 2534 PetscBool flg = PETSC_TRUE; 2535 2536 PetscFunctionBegin; 2537 ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr); 2538 ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr); 2539 ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr); 2540 ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr); 2541 ierr = PetscOptionsGetBool(PETSC_NULL,"-lg_indicate_data_points",&flg,PETSC_NULL);CHKERRQ(ierr); 2542 if (flg) { 2543 ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr); 2544 } 2545 ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr); 2546 (*ctx)->howoften = howoften; 2547 PetscFunctionReturn(0); 2548 } 2549 2550 #undef __FUNCT__ 2551 #define __FUNCT__ "TSMonitorLGTimeStep" 2552 PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 2553 { 2554 TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 2555 PetscReal x = ptime,y; 2556 PetscErrorCode ierr; 2557 2558 PetscFunctionBegin; 2559 if (!n) { 2560 PetscDrawAxis axis; 2561 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 2562 ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 2563 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 2564 } 2565 ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 2566 ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 2567 if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))){ 2568 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 2569 } 2570 PetscFunctionReturn(0); 2571 } 2572 2573 #undef __FUNCT__ 2574 #define __FUNCT__ "TSMonitorLGCtxDestroy" 2575 /*@C 2576 TSMonitorLGCtxDestroy - Destroys a line graph context that was created 2577 with TSMonitorLGCtxCreate(). 2578 2579 Collective on TSMonitorLGCtx 2580 2581 Input Parameter: 2582 . ctx - the monitor context 2583 2584 Level: intermediate 2585 2586 .keywords: TS, monitor, line graph, destroy 2587 2588 .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 2589 @*/ 2590 PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 2591 { 2592 PetscDraw draw; 2593 PetscErrorCode ierr; 2594 2595 PetscFunctionBegin; 2596 ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr); 2597 ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 2598 ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 2599 ierr = PetscFree(*ctx);CHKERRQ(ierr); 2600 PetscFunctionReturn(0); 2601 } 2602 2603 #undef __FUNCT__ 2604 #define __FUNCT__ "TSGetTime" 2605 /*@ 2606 TSGetTime - Gets the time of the most recently completed step. 2607 2608 Not Collective 2609 2610 Input Parameter: 2611 . ts - the TS context obtained from TSCreate() 2612 2613 Output Parameter: 2614 . t - the current time 2615 2616 Level: beginner 2617 2618 Note: 2619 When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 2620 TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 2621 2622 .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2623 2624 .keywords: TS, get, time 2625 @*/ 2626 PetscErrorCode TSGetTime(TS ts,PetscReal* t) 2627 { 2628 PetscFunctionBegin; 2629 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2630 PetscValidRealPointer(t,2); 2631 *t = ts->ptime; 2632 PetscFunctionReturn(0); 2633 } 2634 2635 #undef __FUNCT__ 2636 #define __FUNCT__ "TSSetTime" 2637 /*@ 2638 TSSetTime - Allows one to reset the time. 2639 2640 Logically Collective on TS 2641 2642 Input Parameters: 2643 + ts - the TS context obtained from TSCreate() 2644 - time - the time 2645 2646 Level: intermediate 2647 2648 .seealso: TSGetTime(), TSSetDuration() 2649 2650 .keywords: TS, set, time 2651 @*/ 2652 PetscErrorCode TSSetTime(TS ts, PetscReal t) 2653 { 2654 PetscFunctionBegin; 2655 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2656 PetscValidLogicalCollectiveReal(ts,t,2); 2657 ts->ptime = t; 2658 PetscFunctionReturn(0); 2659 } 2660 2661 #undef __FUNCT__ 2662 #define __FUNCT__ "TSSetOptionsPrefix" 2663 /*@C 2664 TSSetOptionsPrefix - Sets the prefix used for searching for all 2665 TS options in the database. 2666 2667 Logically Collective on TS 2668 2669 Input Parameter: 2670 + ts - The TS context 2671 - prefix - The prefix to prepend to all option names 2672 2673 Notes: 2674 A hyphen (-) must NOT be given at the beginning of the prefix name. 2675 The first character of all runtime options is AUTOMATICALLY the 2676 hyphen. 2677 2678 Level: advanced 2679 2680 .keywords: TS, set, options, prefix, database 2681 2682 .seealso: TSSetFromOptions() 2683 2684 @*/ 2685 PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 2686 { 2687 PetscErrorCode ierr; 2688 SNES snes; 2689 2690 PetscFunctionBegin; 2691 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2692 ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2693 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2694 ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2695 PetscFunctionReturn(0); 2696 } 2697 2698 2699 #undef __FUNCT__ 2700 #define __FUNCT__ "TSAppendOptionsPrefix" 2701 /*@C 2702 TSAppendOptionsPrefix - Appends to the prefix used for searching for all 2703 TS options in the database. 2704 2705 Logically Collective on TS 2706 2707 Input Parameter: 2708 + ts - The TS context 2709 - prefix - The prefix to prepend to all option names 2710 2711 Notes: 2712 A hyphen (-) must NOT be given at the beginning of the prefix name. 2713 The first character of all runtime options is AUTOMATICALLY the 2714 hyphen. 2715 2716 Level: advanced 2717 2718 .keywords: TS, append, options, prefix, database 2719 2720 .seealso: TSGetOptionsPrefix() 2721 2722 @*/ 2723 PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 2724 { 2725 PetscErrorCode ierr; 2726 SNES snes; 2727 2728 PetscFunctionBegin; 2729 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2730 ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2731 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2732 ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2733 PetscFunctionReturn(0); 2734 } 2735 2736 #undef __FUNCT__ 2737 #define __FUNCT__ "TSGetOptionsPrefix" 2738 /*@C 2739 TSGetOptionsPrefix - Sets the prefix used for searching for all 2740 TS options in the database. 2741 2742 Not Collective 2743 2744 Input Parameter: 2745 . ts - The TS context 2746 2747 Output Parameter: 2748 . prefix - A pointer to the prefix string used 2749 2750 Notes: On the fortran side, the user should pass in a string 'prifix' of 2751 sufficient length to hold the prefix. 2752 2753 Level: intermediate 2754 2755 .keywords: TS, get, options, prefix, database 2756 2757 .seealso: TSAppendOptionsPrefix() 2758 @*/ 2759 PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2760 { 2761 PetscErrorCode ierr; 2762 2763 PetscFunctionBegin; 2764 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2765 PetscValidPointer(prefix,2); 2766 ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2767 PetscFunctionReturn(0); 2768 } 2769 2770 #undef __FUNCT__ 2771 #define __FUNCT__ "TSGetRHSJacobian" 2772 /*@C 2773 TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2774 2775 Not Collective, but parallel objects are returned if TS is parallel 2776 2777 Input Parameter: 2778 . ts - The TS context obtained from TSCreate() 2779 2780 Output Parameters: 2781 + J - The Jacobian J of F, where U_t = G(U,t) 2782 . M - The preconditioner matrix, usually the same as J 2783 . func - Function to compute the Jacobian of the RHS 2784 - ctx - User-defined context for Jacobian evaluation routine 2785 2786 Notes: You can pass in PETSC_NULL for any return argument you do not need. 2787 2788 Level: intermediate 2789 2790 .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2791 2792 .keywords: TS, timestep, get, matrix, Jacobian 2793 @*/ 2794 PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2795 { 2796 PetscErrorCode ierr; 2797 SNES snes; 2798 DM dm; 2799 2800 PetscFunctionBegin; 2801 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2802 ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 2803 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 2804 ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 2805 PetscFunctionReturn(0); 2806 } 2807 2808 #undef __FUNCT__ 2809 #define __FUNCT__ "TSGetIJacobian" 2810 /*@C 2811 TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 2812 2813 Not Collective, but parallel objects are returned if TS is parallel 2814 2815 Input Parameter: 2816 . ts - The TS context obtained from TSCreate() 2817 2818 Output Parameters: 2819 + A - The Jacobian of F(t,U,U_t) 2820 . B - The preconditioner matrix, often the same as A 2821 . f - The function to compute the matrices 2822 - ctx - User-defined context for Jacobian evaluation routine 2823 2824 Notes: You can pass in PETSC_NULL for any return argument you do not need. 2825 2826 Level: advanced 2827 2828 .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2829 2830 .keywords: TS, timestep, get, matrix, Jacobian 2831 @*/ 2832 PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 2833 { 2834 PetscErrorCode ierr; 2835 SNES snes; 2836 DM dm; 2837 2838 PetscFunctionBegin; 2839 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2840 ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 2841 ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 2842 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 2843 ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 2844 PetscFunctionReturn(0); 2845 } 2846 2847 struct _n_TSMonitorDrawCtx { 2848 PetscViewer viewer; 2849 Vec initialsolution; 2850 PetscBool showinitial; 2851 PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 2852 }; 2853 2854 #undef __FUNCT__ 2855 #define __FUNCT__ "TSMonitorDrawSolution" 2856 /*@C 2857 TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 2858 VecView() for the solution at each timestep 2859 2860 Collective on TS 2861 2862 Input Parameters: 2863 + ts - the TS context 2864 . step - current time-step 2865 . ptime - current time 2866 - dummy - either a viewer or PETSC_NULL 2867 2868 Options Database: 2869 . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 2870 2871 Notes: the initial solution and current solution are not displayed with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 2872 will look bad 2873 2874 Level: intermediate 2875 2876 .keywords: TS, vector, monitor, view 2877 2878 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 2879 @*/ 2880 PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 2881 { 2882 PetscErrorCode ierr; 2883 TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 2884 2885 PetscFunctionBegin; 2886 if (!step && ictx->showinitial) { 2887 if (!ictx->initialsolution) { 2888 ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 2889 } 2890 ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 2891 } 2892 if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0); 2893 2894 if (ictx->showinitial) { 2895 PetscReal pause; 2896 ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 2897 ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 2898 ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 2899 ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 2900 ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 2901 } 2902 ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 2903 if (ictx->showinitial) { 2904 ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 2905 } 2906 PetscFunctionReturn(0); 2907 } 2908 2909 2910 #undef __FUNCT__ 2911 #define __FUNCT__ "TSMonitorDrawCtxDestroy" 2912 /*@C 2913 TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 2914 2915 Collective on TS 2916 2917 Input Parameters: 2918 . ctx - the monitor context 2919 2920 Level: intermediate 2921 2922 .keywords: TS, vector, monitor, view 2923 2924 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 2925 @*/ 2926 PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 2927 { 2928 PetscErrorCode ierr; 2929 2930 PetscFunctionBegin; 2931 ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 2932 ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 2933 ierr = PetscFree(*ictx);CHKERRQ(ierr); 2934 PetscFunctionReturn(0); 2935 } 2936 2937 #undef __FUNCT__ 2938 #define __FUNCT__ "TSMonitorDrawCtxCreate" 2939 /*@C 2940 TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 2941 2942 Collective on TS 2943 2944 Input Parameter: 2945 . ts - time-step context 2946 2947 Output Patameter: 2948 . ctx - the monitor context 2949 2950 Options Database: 2951 . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 2952 2953 Level: intermediate 2954 2955 .keywords: TS, vector, monitor, view 2956 2957 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 2958 @*/ 2959 PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 2960 { 2961 PetscErrorCode ierr; 2962 2963 PetscFunctionBegin; 2964 ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr); 2965 ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 2966 (*ctx)->showinitial = PETSC_FALSE; 2967 (*ctx)->howoften = howoften; 2968 ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,PETSC_NULL);CHKERRQ(ierr); 2969 PetscFunctionReturn(0); 2970 } 2971 2972 #undef __FUNCT__ 2973 #define __FUNCT__ "TSMonitorDrawError" 2974 /*@C 2975 TSMonitorDrawError - Monitors progress of the TS solvers by calling 2976 VecView() for the error at each timestep 2977 2978 Collective on TS 2979 2980 Input Parameters: 2981 + ts - the TS context 2982 . step - current time-step 2983 . ptime - current time 2984 - dummy - either a viewer or PETSC_NULL 2985 2986 Level: intermediate 2987 2988 .keywords: TS, vector, monitor, view 2989 2990 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 2991 @*/ 2992 PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 2993 { 2994 PetscErrorCode ierr; 2995 TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 2996 PetscViewer viewer = ctx->viewer; 2997 Vec work; 2998 2999 PetscFunctionBegin; 3000 if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0); 3001 ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 3002 ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 3003 ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 3004 ierr = VecView(work,viewer);CHKERRQ(ierr); 3005 ierr = VecDestroy(&work);CHKERRQ(ierr); 3006 PetscFunctionReturn(0); 3007 } 3008 3009 #include <petsc-private/dmimpl.h> 3010 #undef __FUNCT__ 3011 #define __FUNCT__ "TSSetDM" 3012 /*@ 3013 TSSetDM - Sets the DM that may be used by some preconditioners 3014 3015 Logically Collective on TS and DM 3016 3017 Input Parameters: 3018 + ts - the preconditioner context 3019 - dm - the dm 3020 3021 Level: intermediate 3022 3023 3024 .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 3025 @*/ 3026 PetscErrorCode TSSetDM(TS ts,DM dm) 3027 { 3028 PetscErrorCode ierr; 3029 SNES snes; 3030 DMTS tsdm; 3031 3032 PetscFunctionBegin; 3033 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3034 ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 3035 if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 3036 if (ts->dm->dmts && !dm->dmts) { 3037 ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 3038 ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 3039 if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 3040 tsdm->originaldm = dm; 3041 } 3042 } 3043 ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 3044 } 3045 ts->dm = dm; 3046 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3047 ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 3048 PetscFunctionReturn(0); 3049 } 3050 3051 #undef __FUNCT__ 3052 #define __FUNCT__ "TSGetDM" 3053 /*@ 3054 TSGetDM - Gets the DM that may be used by some preconditioners 3055 3056 Not Collective 3057 3058 Input Parameter: 3059 . ts - the preconditioner context 3060 3061 Output Parameter: 3062 . dm - the dm 3063 3064 Level: intermediate 3065 3066 3067 .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 3068 @*/ 3069 PetscErrorCode TSGetDM(TS ts,DM *dm) 3070 { 3071 PetscErrorCode ierr; 3072 3073 PetscFunctionBegin; 3074 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3075 if (!ts->dm) { 3076 ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr); 3077 if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 3078 } 3079 *dm = ts->dm; 3080 PetscFunctionReturn(0); 3081 } 3082 3083 #undef __FUNCT__ 3084 #define __FUNCT__ "SNESTSFormFunction" 3085 /*@ 3086 SNESTSFormFunction - Function to evaluate nonlinear residual 3087 3088 Logically Collective on SNES 3089 3090 Input Parameter: 3091 + snes - nonlinear solver 3092 . U - the current state at which to evaluate the residual 3093 - ctx - user context, must be a TS 3094 3095 Output Parameter: 3096 . F - the nonlinear residual 3097 3098 Notes: 3099 This function is not normally called by users and is automatically registered with the SNES used by TS. 3100 It is most frequently passed to MatFDColoringSetFunction(). 3101 3102 Level: advanced 3103 3104 .seealso: SNESSetFunction(), MatFDColoringSetFunction() 3105 @*/ 3106 PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 3107 { 3108 TS ts = (TS)ctx; 3109 PetscErrorCode ierr; 3110 3111 PetscFunctionBegin; 3112 PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3113 PetscValidHeaderSpecific(U,VEC_CLASSID,2); 3114 PetscValidHeaderSpecific(F,VEC_CLASSID,3); 3115 PetscValidHeaderSpecific(ts,TS_CLASSID,4); 3116 ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 3117 PetscFunctionReturn(0); 3118 } 3119 3120 #undef __FUNCT__ 3121 #define __FUNCT__ "SNESTSFormJacobian" 3122 /*@ 3123 SNESTSFormJacobian - Function to evaluate the Jacobian 3124 3125 Collective on SNES 3126 3127 Input Parameter: 3128 + snes - nonlinear solver 3129 . U - the current state at which to evaluate the residual 3130 - ctx - user context, must be a TS 3131 3132 Output Parameter: 3133 + A - the Jacobian 3134 . B - the preconditioning matrix (may be the same as A) 3135 - flag - indicates any structure change in the matrix 3136 3137 Notes: 3138 This function is not normally called by users and is automatically registered with the SNES used by TS. 3139 3140 Level: developer 3141 3142 .seealso: SNESSetJacobian() 3143 @*/ 3144 PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx) 3145 { 3146 TS ts = (TS)ctx; 3147 PetscErrorCode ierr; 3148 3149 PetscFunctionBegin; 3150 PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3151 PetscValidHeaderSpecific(U,VEC_CLASSID,2); 3152 PetscValidPointer(A,3); 3153 PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 3154 PetscValidPointer(B,4); 3155 PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 3156 PetscValidPointer(flag,5); 3157 PetscValidHeaderSpecific(ts,TS_CLASSID,6); 3158 ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr); 3159 PetscFunctionReturn(0); 3160 } 3161 3162 #undef __FUNCT__ 3163 #define __FUNCT__ "TSComputeRHSFunctionLinear" 3164 /*@C 3165 TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 3166 3167 Collective on TS 3168 3169 Input Arguments: 3170 + ts - time stepping context 3171 . t - time at which to evaluate 3172 . U - state at which to evaluate 3173 - ctx - context 3174 3175 Output Arguments: 3176 . F - right hand side 3177 3178 Level: intermediate 3179 3180 Notes: 3181 This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 3182 The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 3183 3184 .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 3185 @*/ 3186 PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 3187 { 3188 PetscErrorCode ierr; 3189 Mat Arhs,Brhs; 3190 MatStructure flg2; 3191 3192 PetscFunctionBegin; 3193 ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 3194 ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 3195 ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 3196 PetscFunctionReturn(0); 3197 } 3198 3199 #undef __FUNCT__ 3200 #define __FUNCT__ "TSComputeRHSJacobianConstant" 3201 /*@C 3202 TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 3203 3204 Collective on TS 3205 3206 Input Arguments: 3207 + ts - time stepping context 3208 . t - time at which to evaluate 3209 . U - state at which to evaluate 3210 - ctx - context 3211 3212 Output Arguments: 3213 + A - pointer to operator 3214 . B - pointer to preconditioning matrix 3215 - flg - matrix structure flag 3216 3217 Level: intermediate 3218 3219 Notes: 3220 This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 3221 3222 .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 3223 @*/ 3224 PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx) 3225 { 3226 PetscFunctionBegin; 3227 *flg = SAME_PRECONDITIONER; 3228 PetscFunctionReturn(0); 3229 } 3230 3231 #undef __FUNCT__ 3232 #define __FUNCT__ "TSComputeIFunctionLinear" 3233 /*@C 3234 TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 3235 3236 Collective on TS 3237 3238 Input Arguments: 3239 + ts - time stepping context 3240 . t - time at which to evaluate 3241 . U - state at which to evaluate 3242 . Udot - time derivative of state vector 3243 - ctx - context 3244 3245 Output Arguments: 3246 . F - left hand side 3247 3248 Level: intermediate 3249 3250 Notes: 3251 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 3252 user is required to write their own TSComputeIFunction. 3253 This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 3254 The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 3255 3256 .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 3257 @*/ 3258 PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 3259 { 3260 PetscErrorCode ierr; 3261 Mat A,B; 3262 MatStructure flg2; 3263 3264 PetscFunctionBegin; 3265 ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 3266 ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr); 3267 ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 3268 PetscFunctionReturn(0); 3269 } 3270 3271 #undef __FUNCT__ 3272 #define __FUNCT__ "TSComputeIJacobianConstant" 3273 /*@C 3274 TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent. 3275 3276 Collective on TS 3277 3278 Input Arguments: 3279 + ts - time stepping context 3280 . t - time at which to evaluate 3281 . U - state at which to evaluate 3282 . Udot - time derivative of state vector 3283 . shift - shift to apply 3284 - ctx - context 3285 3286 Output Arguments: 3287 + A - pointer to operator 3288 . B - pointer to preconditioning matrix 3289 - flg - matrix structure flag 3290 3291 Level: intermediate 3292 3293 Notes: 3294 This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 3295 3296 .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 3297 @*/ 3298 PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx) 3299 { 3300 PetscFunctionBegin; 3301 *flg = SAME_PRECONDITIONER; 3302 PetscFunctionReturn(0); 3303 } 3304 #undef __FUNCT__ 3305 #define __FUNCT__ "TSGetEquationType" 3306 /*@ 3307 TSGetEquationType - Gets the type of the equation that TS is solving. 3308 3309 Not Collective 3310 3311 Input Parameter: 3312 . ts - the TS context 3313 3314 Output Parameter: 3315 . equation_type - see TSEquatioType 3316 3317 Level: beginner 3318 3319 .keywords: TS, equation type 3320 3321 .seealso: TSSetEquationType(), TSEquationType 3322 @*/ 3323 PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 3324 { 3325 PetscFunctionBegin; 3326 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3327 PetscValidPointer(equation_type,2); 3328 *equation_type = ts->equation_type; 3329 PetscFunctionReturn(0); 3330 } 3331 3332 #undef __FUNCT__ 3333 #define __FUNCT__ "TSSetEquationType" 3334 /*@ 3335 TSSetEquationType - Sets the type of the equation that TS is solving. 3336 3337 Not Collective 3338 3339 Input Parameter: 3340 + ts - the TS context 3341 . equation_type - see TSEquatioType 3342 3343 Level: advanced 3344 3345 .keywords: TS, equation type 3346 3347 .seealso: TSGetEquationType(), TSEquationType 3348 @*/ 3349 PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 3350 { 3351 PetscFunctionBegin; 3352 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3353 ts->equation_type = equation_type; 3354 PetscFunctionReturn(0); 3355 } 3356 3357 #undef __FUNCT__ 3358 #define __FUNCT__ "TSGetConvergedReason" 3359 /*@ 3360 TSGetConvergedReason - Gets the reason the TS iteration was stopped. 3361 3362 Not Collective 3363 3364 Input Parameter: 3365 . ts - the TS context 3366 3367 Output Parameter: 3368 . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 3369 manual pages for the individual convergence tests for complete lists 3370 3371 Level: beginner 3372 3373 Notes: 3374 Can only be called after the call to TSSolve() is complete. 3375 3376 .keywords: TS, nonlinear, set, convergence, test 3377 3378 .seealso: TSSetConvergenceTest(), TSConvergedReason 3379 @*/ 3380 PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 3381 { 3382 PetscFunctionBegin; 3383 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3384 PetscValidPointer(reason,2); 3385 *reason = ts->reason; 3386 PetscFunctionReturn(0); 3387 } 3388 3389 #undef __FUNCT__ 3390 #define __FUNCT__ "TSSetConvergedReason" 3391 /*@ 3392 TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 3393 3394 Not Collective 3395 3396 Input Parameter: 3397 + ts - the TS context 3398 . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 3399 manual pages for the individual convergence tests for complete lists 3400 3401 Level: advanced 3402 3403 Notes: 3404 Can only be called during TSSolve() is active. 3405 3406 .keywords: TS, nonlinear, set, convergence, test 3407 3408 .seealso: TSConvergedReason 3409 @*/ 3410 PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 3411 { 3412 PetscFunctionBegin; 3413 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3414 ts->reason = reason; 3415 PetscFunctionReturn(0); 3416 } 3417 3418 #undef __FUNCT__ 3419 #define __FUNCT__ "TSGetSolveTime" 3420 /*@ 3421 TSGetSolveTime - Gets the time after a call to TSSolve() 3422 3423 Not Collective 3424 3425 Input Parameter: 3426 . ts - the TS context 3427 3428 Output Parameter: 3429 . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 3430 3431 Level: beginner 3432 3433 Notes: 3434 Can only be called after the call to TSSolve() is complete. 3435 3436 .keywords: TS, nonlinear, set, convergence, test 3437 3438 .seealso: TSSetConvergenceTest(), TSConvergedReason 3439 @*/ 3440 PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 3441 { 3442 PetscFunctionBegin; 3443 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3444 PetscValidPointer(ftime,2); 3445 *ftime = ts->solvetime; 3446 PetscFunctionReturn(0); 3447 } 3448 3449 #undef __FUNCT__ 3450 #define __FUNCT__ "TSGetSNESIterations" 3451 /*@ 3452 TSGetSNESIterations - Gets the total number of nonlinear iterations 3453 used by the time integrator. 3454 3455 Not Collective 3456 3457 Input Parameter: 3458 . ts - TS context 3459 3460 Output Parameter: 3461 . nits - number of nonlinear iterations 3462 3463 Notes: 3464 This counter is reset to zero for each successive call to TSSolve(). 3465 3466 Level: intermediate 3467 3468 .keywords: TS, get, number, nonlinear, iterations 3469 3470 .seealso: TSGetKSPIterations() 3471 @*/ 3472 PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 3473 { 3474 PetscFunctionBegin; 3475 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3476 PetscValidIntPointer(nits,2); 3477 *nits = ts->snes_its; 3478 PetscFunctionReturn(0); 3479 } 3480 3481 #undef __FUNCT__ 3482 #define __FUNCT__ "TSGetKSPIterations" 3483 /*@ 3484 TSGetKSPIterations - Gets the total number of linear iterations 3485 used by the time integrator. 3486 3487 Not Collective 3488 3489 Input Parameter: 3490 . ts - TS context 3491 3492 Output Parameter: 3493 . lits - number of linear iterations 3494 3495 Notes: 3496 This counter is reset to zero for each successive call to TSSolve(). 3497 3498 Level: intermediate 3499 3500 .keywords: TS, get, number, linear, iterations 3501 3502 .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 3503 @*/ 3504 PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 3505 { 3506 PetscFunctionBegin; 3507 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3508 PetscValidIntPointer(lits,2); 3509 *lits = ts->ksp_its; 3510 PetscFunctionReturn(0); 3511 } 3512 3513 #undef __FUNCT__ 3514 #define __FUNCT__ "TSGetStepRejections" 3515 /*@ 3516 TSGetStepRejections - Gets the total number of rejected steps. 3517 3518 Not Collective 3519 3520 Input Parameter: 3521 . ts - TS context 3522 3523 Output Parameter: 3524 . rejects - number of steps rejected 3525 3526 Notes: 3527 This counter is reset to zero for each successive call to TSSolve(). 3528 3529 Level: intermediate 3530 3531 .keywords: TS, get, number 3532 3533 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 3534 @*/ 3535 PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 3536 { 3537 PetscFunctionBegin; 3538 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3539 PetscValidIntPointer(rejects,2); 3540 *rejects = ts->reject; 3541 PetscFunctionReturn(0); 3542 } 3543 3544 #undef __FUNCT__ 3545 #define __FUNCT__ "TSGetSNESFailures" 3546 /*@ 3547 TSGetSNESFailures - Gets the total number of failed SNES solves 3548 3549 Not Collective 3550 3551 Input Parameter: 3552 . ts - TS context 3553 3554 Output Parameter: 3555 . fails - number of failed nonlinear solves 3556 3557 Notes: 3558 This counter is reset to zero for each successive call to TSSolve(). 3559 3560 Level: intermediate 3561 3562 .keywords: TS, get, number 3563 3564 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 3565 @*/ 3566 PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 3567 { 3568 PetscFunctionBegin; 3569 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3570 PetscValidIntPointer(fails,2); 3571 *fails = ts->num_snes_failures; 3572 PetscFunctionReturn(0); 3573 } 3574 3575 #undef __FUNCT__ 3576 #define __FUNCT__ "TSSetMaxStepRejections" 3577 /*@ 3578 TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 3579 3580 Not Collective 3581 3582 Input Parameter: 3583 + ts - TS context 3584 - rejects - maximum number of rejected steps, pass -1 for unlimited 3585 3586 Notes: 3587 The counter is reset to zero for each step 3588 3589 Options Database Key: 3590 . -ts_max_reject - Maximum number of step rejections before a step fails 3591 3592 Level: intermediate 3593 3594 .keywords: TS, set, maximum, number 3595 3596 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3597 @*/ 3598 PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 3599 { 3600 PetscFunctionBegin; 3601 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3602 ts->max_reject = rejects; 3603 PetscFunctionReturn(0); 3604 } 3605 3606 #undef __FUNCT__ 3607 #define __FUNCT__ "TSSetMaxSNESFailures" 3608 /*@ 3609 TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 3610 3611 Not Collective 3612 3613 Input Parameter: 3614 + ts - TS context 3615 - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 3616 3617 Notes: 3618 The counter is reset to zero for each successive call to TSSolve(). 3619 3620 Options Database Key: 3621 . -ts_max_snes_failures - Maximum number of nonlinear solve failures 3622 3623 Level: intermediate 3624 3625 .keywords: TS, set, maximum, number 3626 3627 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 3628 @*/ 3629 PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 3630 { 3631 PetscFunctionBegin; 3632 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3633 ts->max_snes_failures = fails; 3634 PetscFunctionReturn(0); 3635 } 3636 3637 #undef __FUNCT__ 3638 #define __FUNCT__ "TSSetErrorIfStepFails()" 3639 /*@ 3640 TSSetErrorIfStepFails - Error if no step succeeds 3641 3642 Not Collective 3643 3644 Input Parameter: 3645 + ts - TS context 3646 - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 3647 3648 Options Database Key: 3649 . -ts_error_if_step_fails - Error if no step succeeds 3650 3651 Level: intermediate 3652 3653 .keywords: TS, set, error 3654 3655 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3656 @*/ 3657 PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 3658 { 3659 PetscFunctionBegin; 3660 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3661 ts->errorifstepfailed = err; 3662 PetscFunctionReturn(0); 3663 } 3664 3665 #undef __FUNCT__ 3666 #define __FUNCT__ "TSMonitorSolutionBinary" 3667 /*@C 3668 TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 3669 3670 Collective on TS 3671 3672 Input Parameters: 3673 + ts - the TS context 3674 . step - current time-step 3675 . ptime - current time 3676 . u - current state 3677 - viewer - binary viewer 3678 3679 Level: intermediate 3680 3681 .keywords: TS, vector, monitor, view 3682 3683 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3684 @*/ 3685 PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 3686 { 3687 PetscErrorCode ierr; 3688 PetscViewer v = (PetscViewer)viewer; 3689 3690 PetscFunctionBegin; 3691 ierr = VecView(u,v);CHKERRQ(ierr); 3692 PetscFunctionReturn(0); 3693 } 3694 3695 #undef __FUNCT__ 3696 #define __FUNCT__ "TSMonitorSolutionVTK" 3697 /*@C 3698 TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 3699 3700 Collective on TS 3701 3702 Input Parameters: 3703 + ts - the TS context 3704 . step - current time-step 3705 . ptime - current time 3706 . u - current state 3707 - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3708 3709 Level: intermediate 3710 3711 Notes: 3712 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. 3713 These are named according to the file name template. 3714 3715 This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 3716 3717 .keywords: TS, vector, monitor, view 3718 3719 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3720 @*/ 3721 PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 3722 { 3723 PetscErrorCode ierr; 3724 char filename[PETSC_MAX_PATH_LEN]; 3725 PetscViewer viewer; 3726 3727 PetscFunctionBegin; 3728 ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 3729 ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 3730 ierr = VecView(u,viewer);CHKERRQ(ierr); 3731 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 3732 PetscFunctionReturn(0); 3733 } 3734 3735 #undef __FUNCT__ 3736 #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 3737 /*@C 3738 TSMonitorSolutionVTKDestroy - Destroy context for monitoring 3739 3740 Collective on TS 3741 3742 Input Parameters: 3743 . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3744 3745 Level: intermediate 3746 3747 Note: 3748 This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 3749 3750 .keywords: TS, vector, monitor, view 3751 3752 .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 3753 @*/ 3754 PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 3755 { 3756 PetscErrorCode ierr; 3757 3758 PetscFunctionBegin; 3759 ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 3760 PetscFunctionReturn(0); 3761 } 3762 3763 #undef __FUNCT__ 3764 #define __FUNCT__ "TSGetTSAdapt" 3765 /*@ 3766 TSGetTSAdapt - Get the adaptive controller context for the current method 3767 3768 Collective on TS if controller has not been created yet 3769 3770 Input Arguments: 3771 . ts - time stepping context 3772 3773 Output Arguments: 3774 . adapt - adaptive controller 3775 3776 Level: intermediate 3777 3778 .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 3779 @*/ 3780 PetscErrorCode TSGetTSAdapt(TS ts,TSAdapt *adapt) 3781 { 3782 PetscErrorCode ierr; 3783 3784 PetscFunctionBegin; 3785 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3786 PetscValidPointer(adapt,2); 3787 if (!ts->adapt) { 3788 ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr); 3789 ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr); 3790 ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 3791 } 3792 *adapt = ts->adapt; 3793 PetscFunctionReturn(0); 3794 } 3795 3796 #undef __FUNCT__ 3797 #define __FUNCT__ "TSSetTolerances" 3798 /*@ 3799 TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 3800 3801 Logically Collective 3802 3803 Input Arguments: 3804 + ts - time integration context 3805 . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 3806 . vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present 3807 . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 3808 - vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present 3809 3810 Level: beginner 3811 3812 .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 3813 @*/ 3814 PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 3815 { 3816 PetscErrorCode ierr; 3817 3818 PetscFunctionBegin; 3819 if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 3820 if (vatol) { 3821 ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 3822 ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 3823 ts->vatol = vatol; 3824 } 3825 if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 3826 if (vrtol) { 3827 ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 3828 ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 3829 ts->vrtol = vrtol; 3830 } 3831 PetscFunctionReturn(0); 3832 } 3833 3834 #undef __FUNCT__ 3835 #define __FUNCT__ "TSGetTolerances" 3836 /*@ 3837 TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 3838 3839 Logically Collective 3840 3841 Input Arguments: 3842 . ts - time integration context 3843 3844 Output Arguments: 3845 + atol - scalar absolute tolerances, PETSC_NULL to ignore 3846 . vatol - vector of absolute tolerances, PETSC_NULL to ignore 3847 . rtol - scalar relative tolerances, PETSC_NULL to ignore 3848 - vrtol - vector of relative tolerances, PETSC_NULL to ignore 3849 3850 Level: beginner 3851 3852 .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 3853 @*/ 3854 PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 3855 { 3856 PetscFunctionBegin; 3857 if (atol) *atol = ts->atol; 3858 if (vatol) *vatol = ts->vatol; 3859 if (rtol) *rtol = ts->rtol; 3860 if (vrtol) *vrtol = ts->vrtol; 3861 PetscFunctionReturn(0); 3862 } 3863 3864 #undef __FUNCT__ 3865 #define __FUNCT__ "TSErrorNormWRMS" 3866 /*@ 3867 TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 3868 3869 Collective on TS 3870 3871 Input Arguments: 3872 + ts - time stepping context 3873 - Y - state vector to be compared to ts->vec_sol 3874 3875 Output Arguments: 3876 . norm - weighted norm, a value of 1.0 is considered small 3877 3878 Level: developer 3879 3880 .seealso: TSSetTolerances() 3881 @*/ 3882 PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 3883 { 3884 PetscErrorCode ierr; 3885 PetscInt i,n,N; 3886 const PetscScalar *u,*y; 3887 Vec U; 3888 PetscReal sum,gsum; 3889 3890 PetscFunctionBegin; 3891 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3892 PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 3893 PetscValidPointer(norm,3); 3894 U = ts->vec_sol; 3895 PetscCheckSameTypeAndComm(U,1,Y,2); 3896 if (U == Y) SETERRQ(((PetscObject)U)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 3897 3898 ierr = VecGetSize(U,&N);CHKERRQ(ierr); 3899 ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 3900 ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 3901 ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 3902 sum = 0.; 3903 if (ts->vatol && ts->vrtol) { 3904 const PetscScalar *atol,*rtol; 3905 ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3906 ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3907 for (i=0; i<n; i++) { 3908 PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 3909 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3910 } 3911 ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3912 ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3913 } else if (ts->vatol) { /* vector atol, scalar rtol */ 3914 const PetscScalar *atol; 3915 ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3916 for (i=0; i<n; i++) { 3917 PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 3918 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3919 } 3920 ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3921 } else if (ts->vrtol) { /* scalar atol, vector rtol */ 3922 const PetscScalar *rtol; 3923 ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3924 for (i=0; i<n; i++) { 3925 PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 3926 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3927 } 3928 ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3929 } else { /* scalar atol, scalar rtol */ 3930 for (i=0; i<n; i++) { 3931 PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 3932 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3933 } 3934 } 3935 ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 3936 ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 3937 3938 ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr); 3939 *norm = PetscSqrtReal(gsum / N); 3940 if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 3941 PetscFunctionReturn(0); 3942 } 3943 3944 #undef __FUNCT__ 3945 #define __FUNCT__ "TSSetCFLTimeLocal" 3946 /*@ 3947 TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 3948 3949 Logically Collective on TS 3950 3951 Input Arguments: 3952 + ts - time stepping context 3953 - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 3954 3955 Note: 3956 After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 3957 3958 Level: intermediate 3959 3960 .seealso: TSGetCFLTime(), TSADAPTCFL 3961 @*/ 3962 PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 3963 { 3964 PetscFunctionBegin; 3965 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3966 ts->cfltime_local = cfltime; 3967 ts->cfltime = -1.; 3968 PetscFunctionReturn(0); 3969 } 3970 3971 #undef __FUNCT__ 3972 #define __FUNCT__ "TSGetCFLTime" 3973 /*@ 3974 TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 3975 3976 Collective on TS 3977 3978 Input Arguments: 3979 . ts - time stepping context 3980 3981 Output Arguments: 3982 . cfltime - maximum stable time step for forward Euler 3983 3984 Level: advanced 3985 3986 .seealso: TSSetCFLTimeLocal() 3987 @*/ 3988 PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 3989 { 3990 PetscErrorCode ierr; 3991 3992 PetscFunctionBegin; 3993 if (ts->cfltime < 0) { 3994 ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr); 3995 } 3996 *cfltime = ts->cfltime; 3997 PetscFunctionReturn(0); 3998 } 3999 4000 #undef __FUNCT__ 4001 #define __FUNCT__ "TSVISetVariableBounds" 4002 /*@ 4003 TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 4004 4005 Input Parameters: 4006 . ts - the TS context. 4007 . xl - lower bound. 4008 . xu - upper bound. 4009 4010 Notes: 4011 If this routine is not called then the lower and upper bounds are set to 4012 SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp(). 4013 4014 Level: advanced 4015 4016 @*/ 4017 PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 4018 { 4019 PetscErrorCode ierr; 4020 SNES snes; 4021 4022 PetscFunctionBegin; 4023 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4024 ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 4025 PetscFunctionReturn(0); 4026 } 4027 4028 #if defined(PETSC_HAVE_MATLAB_ENGINE) 4029 #include <mex.h> 4030 4031 typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 4032 4033 #undef __FUNCT__ 4034 #define __FUNCT__ "TSComputeFunction_Matlab" 4035 /* 4036 TSComputeFunction_Matlab - Calls the function that has been set with 4037 TSSetFunctionMatlab(). 4038 4039 Collective on TS 4040 4041 Input Parameters: 4042 + snes - the TS context 4043 - u - input vector 4044 4045 Output Parameter: 4046 . y - function vector, as set by TSSetFunction() 4047 4048 Notes: 4049 TSComputeFunction() is typically used within nonlinear solvers 4050 implementations, so most users would not generally call this routine 4051 themselves. 4052 4053 Level: developer 4054 4055 .keywords: TS, nonlinear, compute, function 4056 4057 .seealso: TSSetFunction(), TSGetFunction() 4058 */ 4059 PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 4060 { 4061 PetscErrorCode ierr; 4062 TSMatlabContext *sctx = (TSMatlabContext *)ctx; 4063 int nlhs = 1,nrhs = 7; 4064 mxArray *plhs[1],*prhs[7]; 4065 long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 4066 4067 PetscFunctionBegin; 4068 PetscValidHeaderSpecific(snes,TS_CLASSID,1); 4069 PetscValidHeaderSpecific(u,VEC_CLASSID,3); 4070 PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 4071 PetscValidHeaderSpecific(y,VEC_CLASSID,5); 4072 PetscCheckSameComm(snes,1,u,3); 4073 PetscCheckSameComm(snes,1,y,5); 4074 4075 ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 4076 ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 4077 ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 4078 ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 4079 prhs[0] = mxCreateDoubleScalar((double)ls); 4080 prhs[1] = mxCreateDoubleScalar(time); 4081 prhs[2] = mxCreateDoubleScalar((double)lx); 4082 prhs[3] = mxCreateDoubleScalar((double)lxdot); 4083 prhs[4] = mxCreateDoubleScalar((double)ly); 4084 prhs[5] = mxCreateString(sctx->funcname); 4085 prhs[6] = sctx->ctx; 4086 ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 4087 ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4088 mxDestroyArray(prhs[0]); 4089 mxDestroyArray(prhs[1]); 4090 mxDestroyArray(prhs[2]); 4091 mxDestroyArray(prhs[3]); 4092 mxDestroyArray(prhs[4]); 4093 mxDestroyArray(prhs[5]); 4094 mxDestroyArray(plhs[0]); 4095 PetscFunctionReturn(0); 4096 } 4097 4098 4099 #undef __FUNCT__ 4100 #define __FUNCT__ "TSSetFunctionMatlab" 4101 /* 4102 TSSetFunctionMatlab - Sets the function evaluation routine and function 4103 vector for use by the TS routines in solving ODEs 4104 equations from MATLAB. Here the function is a string containing the name of a MATLAB function 4105 4106 Logically Collective on TS 4107 4108 Input Parameters: 4109 + ts - the TS context 4110 - func - function evaluation routine 4111 4112 Calling sequence of func: 4113 $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 4114 4115 Level: beginner 4116 4117 .keywords: TS, nonlinear, set, function 4118 4119 .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4120 */ 4121 PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 4122 { 4123 PetscErrorCode ierr; 4124 TSMatlabContext *sctx; 4125 4126 PetscFunctionBegin; 4127 /* currently sctx is memory bleed */ 4128 ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4129 ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4130 /* 4131 This should work, but it doesn't 4132 sctx->ctx = ctx; 4133 mexMakeArrayPersistent(sctx->ctx); 4134 */ 4135 sctx->ctx = mxDuplicateArray(ctx); 4136 ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 4137 PetscFunctionReturn(0); 4138 } 4139 4140 #undef __FUNCT__ 4141 #define __FUNCT__ "TSComputeJacobian_Matlab" 4142 /* 4143 TSComputeJacobian_Matlab - Calls the function that has been set with 4144 TSSetJacobianMatlab(). 4145 4146 Collective on TS 4147 4148 Input Parameters: 4149 + ts - the TS context 4150 . u - input vector 4151 . A, B - the matrices 4152 - ctx - user context 4153 4154 Output Parameter: 4155 . flag - structure of the matrix 4156 4157 Level: developer 4158 4159 .keywords: TS, nonlinear, compute, function 4160 4161 .seealso: TSSetFunction(), TSGetFunction() 4162 @*/ 4163 PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 4164 { 4165 PetscErrorCode ierr; 4166 TSMatlabContext *sctx = (TSMatlabContext *)ctx; 4167 int nlhs = 2,nrhs = 9; 4168 mxArray *plhs[2],*prhs[9]; 4169 long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 4170 4171 PetscFunctionBegin; 4172 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4173 PetscValidHeaderSpecific(u,VEC_CLASSID,3); 4174 4175 /* call Matlab function in ctx with arguments u and y */ 4176 4177 ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 4178 ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 4179 ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 4180 ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 4181 ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 4182 prhs[0] = mxCreateDoubleScalar((double)ls); 4183 prhs[1] = mxCreateDoubleScalar((double)time); 4184 prhs[2] = mxCreateDoubleScalar((double)lx); 4185 prhs[3] = mxCreateDoubleScalar((double)lxdot); 4186 prhs[4] = mxCreateDoubleScalar((double)shift); 4187 prhs[5] = mxCreateDoubleScalar((double)lA); 4188 prhs[6] = mxCreateDoubleScalar((double)lB); 4189 prhs[7] = mxCreateString(sctx->funcname); 4190 prhs[8] = sctx->ctx; 4191 ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 4192 ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4193 *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 4194 mxDestroyArray(prhs[0]); 4195 mxDestroyArray(prhs[1]); 4196 mxDestroyArray(prhs[2]); 4197 mxDestroyArray(prhs[3]); 4198 mxDestroyArray(prhs[4]); 4199 mxDestroyArray(prhs[5]); 4200 mxDestroyArray(prhs[6]); 4201 mxDestroyArray(prhs[7]); 4202 mxDestroyArray(plhs[0]); 4203 mxDestroyArray(plhs[1]); 4204 PetscFunctionReturn(0); 4205 } 4206 4207 4208 #undef __FUNCT__ 4209 #define __FUNCT__ "TSSetJacobianMatlab" 4210 /* 4211 TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 4212 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 4213 4214 Logically Collective on TS 4215 4216 Input Parameters: 4217 + ts - the TS context 4218 . A,B - Jacobian matrices 4219 . func - function evaluation routine 4220 - ctx - user context 4221 4222 Calling sequence of func: 4223 $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 4224 4225 4226 Level: developer 4227 4228 .keywords: TS, nonlinear, set, function 4229 4230 .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4231 */ 4232 PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 4233 { 4234 PetscErrorCode ierr; 4235 TSMatlabContext *sctx; 4236 4237 PetscFunctionBegin; 4238 /* currently sctx is memory bleed */ 4239 ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4240 ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4241 /* 4242 This should work, but it doesn't 4243 sctx->ctx = ctx; 4244 mexMakeArrayPersistent(sctx->ctx); 4245 */ 4246 sctx->ctx = mxDuplicateArray(ctx); 4247 ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 4248 PetscFunctionReturn(0); 4249 } 4250 4251 #undef __FUNCT__ 4252 #define __FUNCT__ "TSMonitor_Matlab" 4253 /* 4254 TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 4255 4256 Collective on TS 4257 4258 .seealso: TSSetFunction(), TSGetFunction() 4259 @*/ 4260 PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 4261 { 4262 PetscErrorCode ierr; 4263 TSMatlabContext *sctx = (TSMatlabContext *)ctx; 4264 int nlhs = 1,nrhs = 6; 4265 mxArray *plhs[1],*prhs[6]; 4266 long long int lx = 0,ls = 0; 4267 4268 PetscFunctionBegin; 4269 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4270 PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4271 4272 ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 4273 ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 4274 prhs[0] = mxCreateDoubleScalar((double)ls); 4275 prhs[1] = mxCreateDoubleScalar((double)it); 4276 prhs[2] = mxCreateDoubleScalar((double)time); 4277 prhs[3] = mxCreateDoubleScalar((double)lx); 4278 prhs[4] = mxCreateString(sctx->funcname); 4279 prhs[5] = sctx->ctx; 4280 ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 4281 ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4282 mxDestroyArray(prhs[0]); 4283 mxDestroyArray(prhs[1]); 4284 mxDestroyArray(prhs[2]); 4285 mxDestroyArray(prhs[3]); 4286 mxDestroyArray(prhs[4]); 4287 mxDestroyArray(plhs[0]); 4288 PetscFunctionReturn(0); 4289 } 4290 4291 4292 #undef __FUNCT__ 4293 #define __FUNCT__ "TSMonitorSetMatlab" 4294 /* 4295 TSMonitorSetMatlab - Sets the monitor function from Matlab 4296 4297 Level: developer 4298 4299 .keywords: TS, nonlinear, set, function 4300 4301 .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4302 */ 4303 PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 4304 { 4305 PetscErrorCode ierr; 4306 TSMatlabContext *sctx; 4307 4308 PetscFunctionBegin; 4309 /* currently sctx is memory bleed */ 4310 ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4311 ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4312 /* 4313 This should work, but it doesn't 4314 sctx->ctx = ctx; 4315 mexMakeArrayPersistent(sctx->ctx); 4316 */ 4317 sctx->ctx = mxDuplicateArray(ctx); 4318 ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 4319 PetscFunctionReturn(0); 4320 } 4321 #endif 4322 4323 4324 4325 #undef __FUNCT__ 4326 #define __FUNCT__ "TSMonitorLGSolution" 4327 /*@C 4328 TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 4329 in a time based line graph 4330 4331 Collective on TS 4332 4333 Input Parameters: 4334 + ts - the TS context 4335 . step - current time-step 4336 . ptime - current time 4337 - lg - a line graph object 4338 4339 Level: intermediate 4340 4341 Notes: each process in a parallel run displays its component solutions in a separate window 4342 4343 .keywords: TS, vector, monitor, view 4344 4345 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4346 @*/ 4347 PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4348 { 4349 PetscErrorCode ierr; 4350 TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4351 const PetscScalar *yy; 4352 PetscInt dim; 4353 4354 PetscFunctionBegin; 4355 if (!step) { 4356 PetscDrawAxis axis; 4357 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4358 ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 4359 ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 4360 ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 4361 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4362 } 4363 ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr); 4364 #if defined(PETSC_USE_COMPLEX) 4365 { 4366 PetscReal *yreal; 4367 PetscInt i,n; 4368 ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr); 4369 ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4370 for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 4371 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4372 ierr = PetscFree(yreal);CHKERRQ(ierr); 4373 } 4374 #else 4375 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4376 #endif 4377 ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr); 4378 if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){ 4379 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4380 } 4381 PetscFunctionReturn(0); 4382 } 4383 4384 #undef __FUNCT__ 4385 #define __FUNCT__ "TSMonitorLGError" 4386 /*@C 4387 TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 4388 in a time based line graph 4389 4390 Collective on TS 4391 4392 Input Parameters: 4393 + ts - the TS context 4394 . step - current time-step 4395 . ptime - current time 4396 - lg - a line graph object 4397 4398 Level: intermediate 4399 4400 Notes: 4401 Only for sequential solves. 4402 4403 The user must provide the solution using TSSetSolutionFunction() to use this monitor. 4404 4405 Options Database Keys: 4406 . -ts_monitor_lg_error - create a graphical monitor of error history 4407 4408 .keywords: TS, vector, monitor, view 4409 4410 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 4411 @*/ 4412 PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4413 { 4414 PetscErrorCode ierr; 4415 TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4416 const PetscScalar *yy; 4417 Vec y; 4418 PetscInt dim; 4419 4420 PetscFunctionBegin; 4421 if (!step) { 4422 PetscDrawAxis axis; 4423 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4424 ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 4425 ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 4426 ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 4427 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4428 } 4429 ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 4430 ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 4431 ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 4432 ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 4433 #if defined(PETSC_USE_COMPLEX) 4434 { 4435 PetscReal *yreal; 4436 PetscInt i,n; 4437 ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 4438 ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4439 for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 4440 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4441 ierr = PetscFree(yreal);CHKERRQ(ierr); 4442 } 4443 #else 4444 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4445 #endif 4446 ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 4447 ierr = VecDestroy(&y);CHKERRQ(ierr); 4448 if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){ 4449 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4450 } 4451 PetscFunctionReturn(0); 4452 } 4453 4454 #undef __FUNCT__ 4455 #define __FUNCT__ "TSMonitorLGSNESIterations" 4456 PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4457 { 4458 TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4459 PetscReal x = ptime,y; 4460 PetscErrorCode ierr; 4461 PetscInt its; 4462 4463 PetscFunctionBegin; 4464 if (!n) { 4465 PetscDrawAxis axis; 4466 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4467 ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 4468 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4469 ctx->snes_its = 0; 4470 } 4471 ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 4472 y = its - ctx->snes_its; 4473 ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4474 if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){ 4475 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4476 } 4477 ctx->snes_its = its; 4478 PetscFunctionReturn(0); 4479 } 4480 4481 #undef __FUNCT__ 4482 #define __FUNCT__ "TSMonitorLGKSPIterations" 4483 PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4484 { 4485 TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4486 PetscReal x = ptime,y; 4487 PetscErrorCode ierr; 4488 PetscInt its; 4489 4490 PetscFunctionBegin; 4491 if (!n) { 4492 PetscDrawAxis axis; 4493 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4494 ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 4495 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4496 ctx->ksp_its = 0; 4497 } 4498 ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 4499 y = its - ctx->ksp_its; 4500 ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4501 if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){ 4502 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4503 } 4504 ctx->ksp_its = its; 4505 PetscFunctionReturn(0); 4506 } 4507 4508 #undef __FUNCT__ 4509 #define __FUNCT__ "TSComputeLinearStability" 4510 /*@ 4511 TSComputeLinearStability - computes the linear stability function at a point 4512 4513 Collective on TS and Vec 4514 4515 Input Parameters: 4516 + ts - the TS context 4517 - xr,xi - real and imaginary part of input arguments 4518 4519 Output Parameters: 4520 . yr,yi - real and imaginary part of function value 4521 4522 Level: developer 4523 4524 .keywords: TS, compute 4525 4526 .seealso: TSSetRHSFunction(), TSComputeIFunction() 4527 @*/ 4528 PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 4529 { 4530 PetscErrorCode ierr; 4531 4532 PetscFunctionBegin; 4533 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4534 if (!ts->ops->linearstability) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 4535 ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 4536 PetscFunctionReturn(0); 4537 } 4538