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