1 2 #include <petsc-private/tsimpl.h> /*I "petscts.h" I*/ 3 #include <petscdmshell.h> 4 5 /* Logging support */ 6 PetscClassId TS_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 TSDM 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 = DMTSGetContext(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 SNES snes; 1072 1073 PetscFunctionBegin; 1074 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1075 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1076 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 1077 if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 1078 1079 ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 1080 if (classid != TS_FILE_CLASSID) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1081 ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 1082 ierr = TSSetType(ts, type);CHKERRQ(ierr); 1083 if (ts->ops->load) { 1084 ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1085 } 1086 ierr = DMCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr); 1087 ierr = DMLoad(ts->dm,viewer);CHKERRQ(ierr); 1088 ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1089 ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 1090 PetscFunctionReturn(0); 1091 } 1092 1093 #undef __FUNCT__ 1094 #define __FUNCT__ "TSView" 1095 /*@C 1096 TSView - Prints the TS data structure. 1097 1098 Collective on TS 1099 1100 Input Parameters: 1101 + ts - the TS context obtained from TSCreate() 1102 - viewer - visualization context 1103 1104 Options Database Key: 1105 . -ts_view - calls TSView() at end of TSStep() 1106 1107 Notes: 1108 The available visualization contexts include 1109 + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1110 - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1111 output where only the first processor opens 1112 the file. All other processors send their 1113 data to the first processor to print. 1114 1115 The user can open an alternative visualization context with 1116 PetscViewerASCIIOpen() - output to a specified file. 1117 1118 Level: beginner 1119 1120 .keywords: TS, timestep, view 1121 1122 .seealso: PetscViewerASCIIOpen() 1123 @*/ 1124 PetscErrorCode TSView(TS ts,PetscViewer viewer) 1125 { 1126 PetscErrorCode ierr; 1127 TSType type; 1128 PetscBool iascii,isstring,isundials,isbinary; 1129 1130 PetscFunctionBegin; 1131 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1132 if (!viewer) { 1133 ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr); 1134 } 1135 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1136 PetscCheckSameComm(ts,1,viewer,2); 1137 1138 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1139 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 1140 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);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 } 1178 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1179 ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1180 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1181 PetscFunctionReturn(0); 1182 } 1183 1184 1185 #undef __FUNCT__ 1186 #define __FUNCT__ "TSSetApplicationContext" 1187 /*@ 1188 TSSetApplicationContext - Sets an optional user-defined context for 1189 the timesteppers. 1190 1191 Logically Collective on TS 1192 1193 Input Parameters: 1194 + ts - the TS context obtained from TSCreate() 1195 - usrP - optional user context 1196 1197 Level: intermediate 1198 1199 .keywords: TS, timestep, set, application, context 1200 1201 .seealso: TSGetApplicationContext() 1202 @*/ 1203 PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1204 { 1205 PetscFunctionBegin; 1206 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1207 ts->user = usrP; 1208 PetscFunctionReturn(0); 1209 } 1210 1211 #undef __FUNCT__ 1212 #define __FUNCT__ "TSGetApplicationContext" 1213 /*@ 1214 TSGetApplicationContext - Gets the user-defined context for the 1215 timestepper. 1216 1217 Not Collective 1218 1219 Input Parameter: 1220 . ts - the TS context obtained from TSCreate() 1221 1222 Output Parameter: 1223 . usrP - user context 1224 1225 Level: intermediate 1226 1227 .keywords: TS, timestep, get, application, context 1228 1229 .seealso: TSSetApplicationContext() 1230 @*/ 1231 PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1232 { 1233 PetscFunctionBegin; 1234 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1235 *(void**)usrP = ts->user; 1236 PetscFunctionReturn(0); 1237 } 1238 1239 #undef __FUNCT__ 1240 #define __FUNCT__ "TSGetTimeStepNumber" 1241 /*@ 1242 TSGetTimeStepNumber - Gets the number of time steps completed. 1243 1244 Not Collective 1245 1246 Input Parameter: 1247 . ts - the TS context obtained from TSCreate() 1248 1249 Output Parameter: 1250 . iter - number of steps completed so far 1251 1252 Level: intermediate 1253 1254 .keywords: TS, timestep, get, iteration, number 1255 .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep() 1256 @*/ 1257 PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt* iter) 1258 { 1259 PetscFunctionBegin; 1260 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1261 PetscValidIntPointer(iter,2); 1262 *iter = ts->steps; 1263 PetscFunctionReturn(0); 1264 } 1265 1266 #undef __FUNCT__ 1267 #define __FUNCT__ "TSSetInitialTimeStep" 1268 /*@ 1269 TSSetInitialTimeStep - Sets the initial timestep to be used, 1270 as well as the initial time. 1271 1272 Logically Collective on TS 1273 1274 Input Parameters: 1275 + ts - the TS context obtained from TSCreate() 1276 . initial_time - the initial time 1277 - time_step - the size of the timestep 1278 1279 Level: intermediate 1280 1281 .seealso: TSSetTimeStep(), TSGetTimeStep() 1282 1283 .keywords: TS, set, initial, timestep 1284 @*/ 1285 PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1286 { 1287 PetscErrorCode ierr; 1288 1289 PetscFunctionBegin; 1290 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1291 ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1292 ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1293 PetscFunctionReturn(0); 1294 } 1295 1296 #undef __FUNCT__ 1297 #define __FUNCT__ "TSSetTimeStep" 1298 /*@ 1299 TSSetTimeStep - Allows one to reset the timestep at any time, 1300 useful for simple pseudo-timestepping codes. 1301 1302 Logically Collective on TS 1303 1304 Input Parameters: 1305 + ts - the TS context obtained from TSCreate() 1306 - time_step - the size of the timestep 1307 1308 Level: intermediate 1309 1310 .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1311 1312 .keywords: TS, set, timestep 1313 @*/ 1314 PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1315 { 1316 PetscFunctionBegin; 1317 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1318 PetscValidLogicalCollectiveReal(ts,time_step,2); 1319 ts->time_step = time_step; 1320 ts->time_step_orig = time_step; 1321 PetscFunctionReturn(0); 1322 } 1323 1324 #undef __FUNCT__ 1325 #define __FUNCT__ "TSSetExactFinalTime" 1326 /*@ 1327 TSSetExactFinalTime - Determines whether to interpolate solution to the 1328 exact final time requested by the user or just returns it at the final time 1329 it computed. 1330 1331 Logically Collective on TS 1332 1333 Input Parameter: 1334 + ts - the time-step context 1335 - ft - PETSC_TRUE if interpolates, else PETSC_FALSE 1336 1337 Level: beginner 1338 1339 .seealso: TSSetDuration() 1340 @*/ 1341 PetscErrorCode TSSetExactFinalTime(TS ts,PetscBool flg) 1342 { 1343 PetscFunctionBegin; 1344 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1345 PetscValidLogicalCollectiveBool(ts,flg,2); 1346 ts->exact_final_time = flg; 1347 PetscFunctionReturn(0); 1348 } 1349 1350 #undef __FUNCT__ 1351 #define __FUNCT__ "TSGetTimeStep" 1352 /*@ 1353 TSGetTimeStep - Gets the current timestep size. 1354 1355 Not Collective 1356 1357 Input Parameter: 1358 . ts - the TS context obtained from TSCreate() 1359 1360 Output Parameter: 1361 . dt - the current timestep size 1362 1363 Level: intermediate 1364 1365 .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1366 1367 .keywords: TS, get, timestep 1368 @*/ 1369 PetscErrorCode TSGetTimeStep(TS ts,PetscReal* dt) 1370 { 1371 PetscFunctionBegin; 1372 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1373 PetscValidRealPointer(dt,2); 1374 *dt = ts->time_step; 1375 PetscFunctionReturn(0); 1376 } 1377 1378 #undef __FUNCT__ 1379 #define __FUNCT__ "TSGetSolution" 1380 /*@ 1381 TSGetSolution - Returns the solution at the present timestep. It 1382 is valid to call this routine inside the function that you are evaluating 1383 in order to move to the new timestep. This vector not changed until 1384 the solution at the next timestep has been calculated. 1385 1386 Not Collective, but Vec returned is parallel if TS is parallel 1387 1388 Input Parameter: 1389 . ts - the TS context obtained from TSCreate() 1390 1391 Output Parameter: 1392 . v - the vector containing the solution 1393 1394 Level: intermediate 1395 1396 .seealso: TSGetTimeStep() 1397 1398 .keywords: TS, timestep, get, solution 1399 @*/ 1400 PetscErrorCode TSGetSolution(TS ts,Vec *v) 1401 { 1402 PetscFunctionBegin; 1403 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1404 PetscValidPointer(v,2); 1405 *v = ts->vec_sol; 1406 PetscFunctionReturn(0); 1407 } 1408 1409 /* ----- Routines to initialize and destroy a timestepper ---- */ 1410 #undef __FUNCT__ 1411 #define __FUNCT__ "TSSetProblemType" 1412 /*@ 1413 TSSetProblemType - Sets the type of problem to be solved. 1414 1415 Not collective 1416 1417 Input Parameters: 1418 + ts - The TS 1419 - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1420 .vb 1421 U_t - A U = 0 (linear) 1422 U_t - A(t) U = 0 (linear) 1423 F(t,U,U_t) = 0 (nonlinear) 1424 .ve 1425 1426 Level: beginner 1427 1428 .keywords: TS, problem type 1429 .seealso: TSSetUp(), TSProblemType, TS 1430 @*/ 1431 PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1432 { 1433 PetscErrorCode ierr; 1434 1435 PetscFunctionBegin; 1436 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1437 ts->problem_type = type; 1438 if (type == TS_LINEAR) { 1439 SNES snes; 1440 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1441 ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 1442 } 1443 PetscFunctionReturn(0); 1444 } 1445 1446 #undef __FUNCT__ 1447 #define __FUNCT__ "TSGetProblemType" 1448 /*@C 1449 TSGetProblemType - Gets the type of problem to be solved. 1450 1451 Not collective 1452 1453 Input Parameter: 1454 . ts - The TS 1455 1456 Output Parameter: 1457 . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1458 .vb 1459 M U_t = A U 1460 M(t) U_t = A(t) U 1461 F(t,U,U_t) 1462 .ve 1463 1464 Level: beginner 1465 1466 .keywords: TS, problem type 1467 .seealso: TSSetUp(), TSProblemType, TS 1468 @*/ 1469 PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1470 { 1471 PetscFunctionBegin; 1472 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1473 PetscValidIntPointer(type,2); 1474 *type = ts->problem_type; 1475 PetscFunctionReturn(0); 1476 } 1477 1478 #undef __FUNCT__ 1479 #define __FUNCT__ "TSSetUp" 1480 /*@ 1481 TSSetUp - Sets up the internal data structures for the later use 1482 of a timestepper. 1483 1484 Collective on TS 1485 1486 Input Parameter: 1487 . ts - the TS context obtained from TSCreate() 1488 1489 Notes: 1490 For basic use of the TS solvers the user need not explicitly call 1491 TSSetUp(), since these actions will automatically occur during 1492 the call to TSStep(). However, if one wishes to control this 1493 phase separately, TSSetUp() should be called after TSCreate() 1494 and optional routines of the form TSSetXXX(), but before TSStep(). 1495 1496 Level: advanced 1497 1498 .keywords: TS, timestep, setup 1499 1500 .seealso: TSCreate(), TSStep(), TSDestroy() 1501 @*/ 1502 PetscErrorCode TSSetUp(TS ts) 1503 { 1504 PetscErrorCode ierr; 1505 DM dm; 1506 PetscErrorCode (*func)(SNES,Vec,Vec,void*); 1507 PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*); 1508 TSIJacobian ijac; 1509 TSRHSJacobian rhsjac; 1510 1511 PetscFunctionBegin; 1512 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1513 if (ts->setupcalled) PetscFunctionReturn(0); 1514 1515 if (!((PetscObject)ts)->type_name) { 1516 ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1517 } 1518 if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE; 1519 1520 if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1521 1522 ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 1523 1524 if (ts->ops->setup) { 1525 ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1526 } 1527 1528 /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 1529 to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 1530 */ 1531 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1532 ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr); 1533 if (!func) { 1534 ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 1535 } 1536 /* if the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it. 1537 Otherwise, the SNES will use coloring internally to form the Jacobian. 1538 */ 1539 ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr); 1540 ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr); 1541 ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr); 1542 if (!jac && (ijac || rhsjac)) { 1543 ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1544 } 1545 ts->setupcalled = PETSC_TRUE; 1546 PetscFunctionReturn(0); 1547 } 1548 1549 #undef __FUNCT__ 1550 #define __FUNCT__ "TSReset" 1551 /*@ 1552 TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1553 1554 Collective on TS 1555 1556 Input Parameter: 1557 . ts - the TS context obtained from TSCreate() 1558 1559 Level: beginner 1560 1561 .keywords: TS, timestep, reset 1562 1563 .seealso: TSCreate(), TSSetup(), TSDestroy() 1564 @*/ 1565 PetscErrorCode TSReset(TS ts) 1566 { 1567 PetscErrorCode ierr; 1568 1569 PetscFunctionBegin; 1570 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1571 if (ts->ops->reset) { 1572 ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1573 } 1574 if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 1575 ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1576 ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1577 ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 1578 ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1579 ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 1580 ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 1581 ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 1582 ts->setupcalled = PETSC_FALSE; 1583 PetscFunctionReturn(0); 1584 } 1585 1586 #undef __FUNCT__ 1587 #define __FUNCT__ "TSDestroy" 1588 /*@ 1589 TSDestroy - Destroys the timestepper context that was created 1590 with TSCreate(). 1591 1592 Collective on TS 1593 1594 Input Parameter: 1595 . ts - the TS context obtained from TSCreate() 1596 1597 Level: beginner 1598 1599 .keywords: TS, timestepper, destroy 1600 1601 .seealso: TSCreate(), TSSetUp(), TSSolve() 1602 @*/ 1603 PetscErrorCode TSDestroy(TS *ts) 1604 { 1605 PetscErrorCode ierr; 1606 1607 PetscFunctionBegin; 1608 if (!*ts) PetscFunctionReturn(0); 1609 PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 1610 if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1611 1612 ierr = TSReset((*ts));CHKERRQ(ierr); 1613 1614 /* if memory was published with AMS then destroy it */ 1615 ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 1616 if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 1617 1618 ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 1619 ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 1620 ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 1621 ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 1622 1623 ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1624 PetscFunctionReturn(0); 1625 } 1626 1627 #undef __FUNCT__ 1628 #define __FUNCT__ "TSGetSNES" 1629 /*@ 1630 TSGetSNES - Returns the SNES (nonlinear solver) associated with 1631 a TS (timestepper) context. Valid only for nonlinear problems. 1632 1633 Not Collective, but SNES is parallel if TS is parallel 1634 1635 Input Parameter: 1636 . ts - the TS context obtained from TSCreate() 1637 1638 Output Parameter: 1639 . snes - the nonlinear solver context 1640 1641 Notes: 1642 The user can then directly manipulate the SNES context to set various 1643 options, etc. Likewise, the user can then extract and manipulate the 1644 KSP, KSP, and PC contexts as well. 1645 1646 TSGetSNES() does not work for integrators that do not use SNES; in 1647 this case TSGetSNES() returns PETSC_NULL in snes. 1648 1649 Level: beginner 1650 1651 .keywords: timestep, get, SNES 1652 @*/ 1653 PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1654 { 1655 PetscErrorCode ierr; 1656 1657 PetscFunctionBegin; 1658 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1659 PetscValidPointer(snes,2); 1660 if (!ts->snes) { 1661 ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 1662 ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 1663 ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1664 ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 1665 if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 1666 if (ts->problem_type == TS_LINEAR) { 1667 ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 1668 } 1669 } 1670 *snes = ts->snes; 1671 PetscFunctionReturn(0); 1672 } 1673 1674 #undef __FUNCT__ 1675 #define __FUNCT__ "TSGetKSP" 1676 /*@ 1677 TSGetKSP - Returns the KSP (linear solver) associated with 1678 a TS (timestepper) context. 1679 1680 Not Collective, but KSP is parallel if TS is parallel 1681 1682 Input Parameter: 1683 . ts - the TS context obtained from TSCreate() 1684 1685 Output Parameter: 1686 . ksp - the nonlinear solver context 1687 1688 Notes: 1689 The user can then directly manipulate the KSP context to set various 1690 options, etc. Likewise, the user can then extract and manipulate the 1691 KSP and PC contexts as well. 1692 1693 TSGetKSP() does not work for integrators that do not use KSP; 1694 in this case TSGetKSP() returns PETSC_NULL in ksp. 1695 1696 Level: beginner 1697 1698 .keywords: timestep, get, KSP 1699 @*/ 1700 PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1701 { 1702 PetscErrorCode ierr; 1703 SNES snes; 1704 1705 PetscFunctionBegin; 1706 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1707 PetscValidPointer(ksp,2); 1708 if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1709 if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1710 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1711 ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1712 PetscFunctionReturn(0); 1713 } 1714 1715 /* ----------- Routines to set solver parameters ---------- */ 1716 1717 #undef __FUNCT__ 1718 #define __FUNCT__ "TSGetDuration" 1719 /*@ 1720 TSGetDuration - Gets the maximum number of timesteps to use and 1721 maximum time for iteration. 1722 1723 Not Collective 1724 1725 Input Parameters: 1726 + ts - the TS context obtained from TSCreate() 1727 . maxsteps - maximum number of iterations to use, or PETSC_NULL 1728 - maxtime - final time to iterate to, or PETSC_NULL 1729 1730 Level: intermediate 1731 1732 .keywords: TS, timestep, get, maximum, iterations, time 1733 @*/ 1734 PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1735 { 1736 PetscFunctionBegin; 1737 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1738 if (maxsteps) { 1739 PetscValidIntPointer(maxsteps,2); 1740 *maxsteps = ts->max_steps; 1741 } 1742 if (maxtime) { 1743 PetscValidScalarPointer(maxtime,3); 1744 *maxtime = ts->max_time; 1745 } 1746 PetscFunctionReturn(0); 1747 } 1748 1749 #undef __FUNCT__ 1750 #define __FUNCT__ "TSSetDuration" 1751 /*@ 1752 TSSetDuration - Sets the maximum number of timesteps to use and 1753 maximum time for iteration. 1754 1755 Logically Collective on TS 1756 1757 Input Parameters: 1758 + ts - the TS context obtained from TSCreate() 1759 . maxsteps - maximum number of iterations to use 1760 - maxtime - final time to iterate to 1761 1762 Options Database Keys: 1763 . -ts_max_steps <maxsteps> - Sets maxsteps 1764 . -ts_final_time <maxtime> - Sets maxtime 1765 1766 Notes: 1767 The default maximum number of iterations is 5000. Default time is 5.0 1768 1769 Level: intermediate 1770 1771 .keywords: TS, timestep, set, maximum, iterations 1772 1773 .seealso: TSSetExactFinalTime() 1774 @*/ 1775 PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1776 { 1777 PetscFunctionBegin; 1778 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1779 PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1780 PetscValidLogicalCollectiveReal(ts,maxtime,2); 1781 if (maxsteps >= 0) ts->max_steps = maxsteps; 1782 if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 1783 PetscFunctionReturn(0); 1784 } 1785 1786 #undef __FUNCT__ 1787 #define __FUNCT__ "TSSetSolution" 1788 /*@ 1789 TSSetSolution - Sets the initial solution vector 1790 for use by the TS routines. 1791 1792 Logically Collective on TS and Vec 1793 1794 Input Parameters: 1795 + ts - the TS context obtained from TSCreate() 1796 - u - the solution vector 1797 1798 Level: beginner 1799 1800 .keywords: TS, timestep, set, solution, initial conditions 1801 @*/ 1802 PetscErrorCode TSSetSolution(TS ts,Vec u) 1803 { 1804 PetscErrorCode ierr; 1805 DM dm; 1806 1807 PetscFunctionBegin; 1808 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1809 PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1810 ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 1811 ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1812 ts->vec_sol = u; 1813 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1814 ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 1815 PetscFunctionReturn(0); 1816 } 1817 1818 #undef __FUNCT__ 1819 #define __FUNCT__ "TSSetPreStep" 1820 /*@C 1821 TSSetPreStep - Sets the general-purpose function 1822 called once at the beginning of each time step. 1823 1824 Logically Collective on TS 1825 1826 Input Parameters: 1827 + ts - The TS context obtained from TSCreate() 1828 - func - The function 1829 1830 Calling sequence of func: 1831 . func (TS ts); 1832 1833 Level: intermediate 1834 1835 Note: 1836 If a step is rejected, TSStep() will call this routine again before each attempt. 1837 The last completed time step number can be queried using TSGetTimeStepNumber(), the 1838 size of the step being attempted can be obtained using TSGetTimeStep(). 1839 1840 .keywords: TS, timestep 1841 .seealso: TSSetPreStage(), TSSetPostStep(), TSStep() 1842 @*/ 1843 PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1844 { 1845 PetscFunctionBegin; 1846 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1847 ts->ops->prestep = func; 1848 PetscFunctionReturn(0); 1849 } 1850 1851 #undef __FUNCT__ 1852 #define __FUNCT__ "TSPreStep" 1853 /*@ 1854 TSPreStep - Runs the user-defined pre-step function. 1855 1856 Collective on TS 1857 1858 Input Parameters: 1859 . ts - The TS context obtained from TSCreate() 1860 1861 Notes: 1862 TSPreStep() is typically used within time stepping implementations, 1863 so most users would not generally call this routine themselves. 1864 1865 Level: developer 1866 1867 .keywords: TS, timestep 1868 .seealso: TSSetPreStep(), TSPreStage(), TSPostStep() 1869 @*/ 1870 PetscErrorCode TSPreStep(TS ts) 1871 { 1872 PetscErrorCode ierr; 1873 1874 PetscFunctionBegin; 1875 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1876 if (ts->ops->prestep) { 1877 PetscStackPush("TS PreStep function"); 1878 ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 1879 PetscStackPop; 1880 } 1881 PetscFunctionReturn(0); 1882 } 1883 1884 #undef __FUNCT__ 1885 #define __FUNCT__ "TSSetPreStage" 1886 /*@C 1887 TSSetPreStage - Sets the general-purpose function 1888 called once at the beginning of each stage. 1889 1890 Logically Collective on TS 1891 1892 Input Parameters: 1893 + ts - The TS context obtained from TSCreate() 1894 - func - The function 1895 1896 Calling sequence of func: 1897 . PetscErrorCode func(TS ts, PetscReal stagetime); 1898 1899 Level: intermediate 1900 1901 Note: 1902 There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 1903 The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 1904 attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 1905 1906 .keywords: TS, timestep 1907 .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 1908 @*/ 1909 PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 1910 { 1911 PetscFunctionBegin; 1912 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1913 ts->ops->prestage = func; 1914 PetscFunctionReturn(0); 1915 } 1916 1917 #undef __FUNCT__ 1918 #define __FUNCT__ "TSPreStage" 1919 /*@ 1920 TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 1921 1922 Collective on TS 1923 1924 Input Parameters: 1925 . ts - The TS context obtained from TSCreate() 1926 1927 Notes: 1928 TSPreStage() is typically used within time stepping implementations, 1929 most users would not generally call this routine themselves. 1930 1931 Level: developer 1932 1933 .keywords: TS, timestep 1934 .seealso: TSSetPreStep(), TSPreStep(), TSPostStep() 1935 @*/ 1936 PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 1937 { 1938 PetscErrorCode ierr; 1939 1940 PetscFunctionBegin; 1941 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1942 if (ts->ops->prestage) { 1943 PetscStackPush("TS PreStage function"); 1944 ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr); 1945 PetscStackPop; 1946 } 1947 PetscFunctionReturn(0); 1948 } 1949 1950 #undef __FUNCT__ 1951 #define __FUNCT__ "TSSetPostStep" 1952 /*@C 1953 TSSetPostStep - Sets the general-purpose function 1954 called once at the end of each time step. 1955 1956 Logically Collective on TS 1957 1958 Input Parameters: 1959 + ts - The TS context obtained from TSCreate() 1960 - func - The function 1961 1962 Calling sequence of func: 1963 $ func (TS ts); 1964 1965 Level: intermediate 1966 1967 .keywords: TS, timestep 1968 .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 1969 @*/ 1970 PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 1971 { 1972 PetscFunctionBegin; 1973 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1974 ts->ops->poststep = func; 1975 PetscFunctionReturn(0); 1976 } 1977 1978 #undef __FUNCT__ 1979 #define __FUNCT__ "TSPostStep" 1980 /*@ 1981 TSPostStep - Runs the user-defined post-step function. 1982 1983 Collective on TS 1984 1985 Input Parameters: 1986 . ts - The TS context obtained from TSCreate() 1987 1988 Notes: 1989 TSPostStep() is typically used within time stepping implementations, 1990 so most users would not generally call this routine themselves. 1991 1992 Level: developer 1993 1994 .keywords: TS, timestep 1995 @*/ 1996 PetscErrorCode TSPostStep(TS ts) 1997 { 1998 PetscErrorCode ierr; 1999 2000 PetscFunctionBegin; 2001 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2002 if (ts->ops->poststep) { 2003 PetscStackPush("TS PostStep function"); 2004 ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 2005 PetscStackPop; 2006 } 2007 PetscFunctionReturn(0); 2008 } 2009 2010 /* ------------ Routines to set performance monitoring options ----------- */ 2011 2012 #undef __FUNCT__ 2013 #define __FUNCT__ "TSMonitorSet" 2014 /*@C 2015 TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 2016 timestep to display the iteration's progress. 2017 2018 Logically Collective on TS 2019 2020 Input Parameters: 2021 + ts - the TS context obtained from TSCreate() 2022 . monitor - monitoring routine 2023 . mctx - [optional] user-defined context for private data for the 2024 monitor routine (use PETSC_NULL if no context is desired) 2025 - monitordestroy - [optional] routine that frees monitor context 2026 (may be PETSC_NULL) 2027 2028 Calling sequence of monitor: 2029 $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 2030 2031 + ts - the TS context 2032 . 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 2033 been interpolated to) 2034 . time - current time 2035 . u - current iterate 2036 - mctx - [optional] monitoring context 2037 2038 Notes: 2039 This routine adds an additional monitor to the list of monitors that 2040 already has been loaded. 2041 2042 Fortran notes: Only a single monitor function can be set for each TS object 2043 2044 Level: intermediate 2045 2046 .keywords: TS, timestep, set, monitor 2047 2048 .seealso: TSMonitorDefault(), TSMonitorCancel() 2049 @*/ 2050 PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 2051 { 2052 PetscFunctionBegin; 2053 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2054 if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2055 ts->monitor[ts->numbermonitors] = monitor; 2056 ts->monitordestroy[ts->numbermonitors] = mdestroy; 2057 ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 2058 PetscFunctionReturn(0); 2059 } 2060 2061 #undef __FUNCT__ 2062 #define __FUNCT__ "TSMonitorCancel" 2063 /*@C 2064 TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 2065 2066 Logically Collective on TS 2067 2068 Input Parameters: 2069 . ts - the TS context obtained from TSCreate() 2070 2071 Notes: 2072 There is no way to remove a single, specific monitor. 2073 2074 Level: intermediate 2075 2076 .keywords: TS, timestep, set, monitor 2077 2078 .seealso: TSMonitorDefault(), TSMonitorSet() 2079 @*/ 2080 PetscErrorCode TSMonitorCancel(TS ts) 2081 { 2082 PetscErrorCode ierr; 2083 PetscInt i; 2084 2085 PetscFunctionBegin; 2086 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2087 for (i=0; i<ts->numbermonitors; i++) { 2088 if (ts->monitordestroy[i]) { 2089 ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 2090 } 2091 } 2092 ts->numbermonitors = 0; 2093 PetscFunctionReturn(0); 2094 } 2095 2096 #undef __FUNCT__ 2097 #define __FUNCT__ "TSMonitorDefault" 2098 /*@ 2099 TSMonitorDefault - Sets the Default monitor 2100 2101 Level: intermediate 2102 2103 .keywords: TS, set, monitor 2104 2105 .seealso: TSMonitorDefault(), TSMonitorSet() 2106 @*/ 2107 PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 2108 { 2109 PetscErrorCode ierr; 2110 PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm); 2111 2112 PetscFunctionBegin; 2113 ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2114 ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr); 2115 ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2116 PetscFunctionReturn(0); 2117 } 2118 2119 #undef __FUNCT__ 2120 #define __FUNCT__ "TSSetRetainStages" 2121 /*@ 2122 TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 2123 2124 Logically Collective on TS 2125 2126 Input Argument: 2127 . ts - time stepping context 2128 2129 Output Argument: 2130 . flg - PETSC_TRUE or PETSC_FALSE 2131 2132 Level: intermediate 2133 2134 .keywords: TS, set 2135 2136 .seealso: TSInterpolate(), TSSetPostStep() 2137 @*/ 2138 PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 2139 { 2140 PetscFunctionBegin; 2141 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2142 ts->retain_stages = flg; 2143 PetscFunctionReturn(0); 2144 } 2145 2146 #undef __FUNCT__ 2147 #define __FUNCT__ "TSInterpolate" 2148 /*@ 2149 TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 2150 2151 Collective on TS 2152 2153 Input Argument: 2154 + ts - time stepping context 2155 - t - time to interpolate to 2156 2157 Output Argument: 2158 . U - state at given time 2159 2160 Notes: 2161 The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 2162 2163 Level: intermediate 2164 2165 Developer Notes: 2166 TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 2167 2168 .keywords: TS, set 2169 2170 .seealso: TSSetRetainStages(), TSSetPostStep() 2171 @*/ 2172 PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 2173 { 2174 PetscErrorCode ierr; 2175 2176 PetscFunctionBegin; 2177 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2178 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); 2179 if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 2180 ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 2181 PetscFunctionReturn(0); 2182 } 2183 2184 #undef __FUNCT__ 2185 #define __FUNCT__ "TSStep" 2186 /*@ 2187 TSStep - Steps one time step 2188 2189 Collective on TS 2190 2191 Input Parameter: 2192 . ts - the TS context obtained from TSCreate() 2193 2194 Level: intermediate 2195 2196 Notes: 2197 The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 2198 be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 2199 2200 This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 2201 time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 2202 2203 .keywords: TS, timestep, solve 2204 2205 .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 2206 @*/ 2207 PetscErrorCode TSStep(TS ts) 2208 { 2209 PetscReal ptime_prev; 2210 PetscErrorCode ierr; 2211 2212 PetscFunctionBegin; 2213 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2214 ierr = TSSetUp(ts);CHKERRQ(ierr); 2215 2216 ts->reason = TS_CONVERGED_ITERATING; 2217 2218 ptime_prev = ts->ptime; 2219 ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2220 ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 2221 ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2222 ts->time_step_prev = ts->ptime - ptime_prev; 2223 2224 if (ts->reason < 0) { 2225 if (ts->errorifstepfailed) { 2226 if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) { 2227 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]); 2228 } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 2229 } 2230 } else if (!ts->reason) { 2231 if (ts->steps >= ts->max_steps) 2232 ts->reason = TS_CONVERGED_ITS; 2233 else if (ts->ptime >= ts->max_time) 2234 ts->reason = TS_CONVERGED_TIME; 2235 } 2236 2237 PetscFunctionReturn(0); 2238 } 2239 2240 #undef __FUNCT__ 2241 #define __FUNCT__ "TSEvaluateStep" 2242 /*@ 2243 TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 2244 2245 Collective on TS 2246 2247 Input Arguments: 2248 + ts - time stepping context 2249 . order - desired order of accuracy 2250 - done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available) 2251 2252 Output Arguments: 2253 . U - state at the end of the current step 2254 2255 Level: advanced 2256 2257 Notes: 2258 This function cannot be called until all stages have been evaluated. 2259 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. 2260 2261 .seealso: TSStep(), TSAdapt 2262 @*/ 2263 PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 2264 { 2265 PetscErrorCode ierr; 2266 2267 PetscFunctionBegin; 2268 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2269 PetscValidType(ts,1); 2270 PetscValidHeaderSpecific(U,VEC_CLASSID,3); 2271 if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 2272 ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 2273 PetscFunctionReturn(0); 2274 } 2275 2276 #undef __FUNCT__ 2277 #define __FUNCT__ "TSSolve" 2278 /*@ 2279 TSSolve - Steps the requested number of timesteps. 2280 2281 Collective on TS 2282 2283 Input Parameter: 2284 + ts - the TS context obtained from TSCreate() 2285 - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 2286 2287 Level: beginner 2288 2289 Notes: 2290 The final time returned by this function may be different from the time of the internally 2291 held state accessible by TSGetSolution() and TSGetTime() because the method may have 2292 stepped over the final time. 2293 2294 .keywords: TS, timestep, solve 2295 2296 .seealso: TSCreate(), TSSetSolution(), TSStep() 2297 @*/ 2298 PetscErrorCode TSSolve(TS ts,Vec u) 2299 { 2300 PetscBool flg; 2301 char filename[PETSC_MAX_PATH_LEN]; 2302 PetscViewer viewer; 2303 PetscErrorCode ierr; 2304 2305 PetscFunctionBegin; 2306 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2307 if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 2308 if (ts->exact_final_time) { /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */ 2309 if (!ts->vec_sol || u == ts->vec_sol) { 2310 Vec y; 2311 ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 2312 ierr = TSSetSolution(ts,y);CHKERRQ(ierr); 2313 ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */ 2314 } 2315 if (u) { 2316 ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 2317 } 2318 } else { 2319 if (u) { 2320 ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 2321 } 2322 } 2323 ierr = TSSetUp(ts);CHKERRQ(ierr); 2324 /* reset time step and iteration counters */ 2325 ts->steps = 0; 2326 ts->ksp_its = 0; 2327 ts->snes_its = 0; 2328 ts->num_snes_failures = 0; 2329 ts->reject = 0; 2330 ts->reason = TS_CONVERGED_ITERATING; 2331 2332 if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 2333 ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 2334 ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 2335 ts->solvetime = ts->ptime; 2336 } else { 2337 /* steps the requested number of timesteps. */ 2338 ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2339 if (ts->steps >= ts->max_steps) 2340 ts->reason = TS_CONVERGED_ITS; 2341 else if (ts->ptime >= ts->max_time) 2342 ts->reason = TS_CONVERGED_TIME; 2343 while (!ts->reason) { 2344 ierr = TSStep(ts);CHKERRQ(ierr); 2345 ierr = TSPostStep(ts);CHKERRQ(ierr); 2346 ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2347 } 2348 if (ts->exact_final_time && ts->ptime > ts->max_time) { 2349 ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 2350 ts->solvetime = ts->max_time; 2351 } else { 2352 ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 2353 ts->solvetime = ts->ptime; 2354 } 2355 } 2356 ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2357 ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2358 if (flg && !PetscPreLoadingOn) { 2359 ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr); 2360 ierr = TSView(ts,viewer);CHKERRQ(ierr); 2361 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 2362 } 2363 PetscFunctionReturn(0); 2364 } 2365 2366 #undef __FUNCT__ 2367 #define __FUNCT__ "TSMonitor" 2368 /*@ 2369 TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 2370 2371 Collective on TS 2372 2373 Input Parameters: 2374 + ts - time stepping context obtained from TSCreate() 2375 . step - step number that has just completed 2376 . ptime - model time of the state 2377 - u - state at the current model time 2378 2379 Notes: 2380 TSMonitor() is typically used within the time stepping implementations. 2381 Users might call this function when using the TSStep() interface instead of TSSolve(). 2382 2383 Level: advanced 2384 2385 .keywords: TS, timestep 2386 @*/ 2387 PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 2388 { 2389 PetscErrorCode ierr; 2390 PetscInt i,n = ts->numbermonitors; 2391 2392 PetscFunctionBegin; 2393 for (i=0; i<n; i++) { 2394 ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 2395 } 2396 PetscFunctionReturn(0); 2397 } 2398 2399 /* ------------------------------------------------------------------------*/ 2400 struct _n_TSMonitorLGCtx { 2401 PetscDrawLG lg; 2402 PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 2403 PetscInt ksp_its,snes_its; 2404 }; 2405 2406 2407 #undef __FUNCT__ 2408 #define __FUNCT__ "TSMonitorLGCtxCreate" 2409 /*@C 2410 TSMonitorLGCtxCreate - Creates a line graph context for use with 2411 TS to monitor the solution process graphically in various ways 2412 2413 Collective on TS 2414 2415 Input Parameters: 2416 + host - the X display to open, or null for the local machine 2417 . label - the title to put in the title bar 2418 . x, y - the screen coordinates of the upper left coordinate of the window 2419 . m, n - the screen width and height in pixels 2420 - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 2421 2422 Output Parameter: 2423 . ctx - the context 2424 2425 Options Database Key: 2426 + -ts_monitor_lg_timestep - automatically sets line graph monitor 2427 . -ts_monitor_lg_solution - 2428 . -ts_monitor_lg_error - 2429 . -ts_monitor_lg_ksp_iterations - 2430 . -ts_monitor_lg_snes_iterations - 2431 - -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true 2432 2433 Notes: 2434 Use TSMonitorLGCtxDestroy() to destroy. 2435 2436 Level: intermediate 2437 2438 .keywords: TS, monitor, line graph, residual, seealso 2439 2440 .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 2441 2442 @*/ 2443 PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 2444 { 2445 PetscDraw win; 2446 PetscErrorCode ierr; 2447 PetscBool flg = PETSC_TRUE; 2448 2449 PetscFunctionBegin; 2450 ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr); 2451 ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr); 2452 ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr); 2453 ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr); 2454 ierr = PetscOptionsGetBool(PETSC_NULL,"-lg_indicate_data_points",&flg,PETSC_NULL);CHKERRQ(ierr); 2455 if (flg) { 2456 ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr); 2457 } 2458 ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr); 2459 (*ctx)->howoften = howoften; 2460 PetscFunctionReturn(0); 2461 } 2462 2463 #undef __FUNCT__ 2464 #define __FUNCT__ "TSMonitorLGTimeStep" 2465 PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 2466 { 2467 TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 2468 PetscReal x = ptime,y; 2469 PetscErrorCode ierr; 2470 2471 PetscFunctionBegin; 2472 if (!n) { 2473 PetscDrawAxis axis; 2474 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 2475 ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 2476 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 2477 } 2478 ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 2479 ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 2480 if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))){ 2481 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 2482 } 2483 PetscFunctionReturn(0); 2484 } 2485 2486 #undef __FUNCT__ 2487 #define __FUNCT__ "TSMonitorLGCtxDestroy" 2488 /*@C 2489 TSMonitorLGCtxDestroy - Destroys a line graph context that was created 2490 with TSMonitorLGCtxCreate(). 2491 2492 Collective on TSMonitorLGCtx 2493 2494 Input Parameter: 2495 . ctx - the monitor context 2496 2497 Level: intermediate 2498 2499 .keywords: TS, monitor, line graph, destroy 2500 2501 .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 2502 @*/ 2503 PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 2504 { 2505 PetscDraw draw; 2506 PetscErrorCode ierr; 2507 2508 PetscFunctionBegin; 2509 ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr); 2510 ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 2511 ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 2512 ierr = PetscFree(*ctx);CHKERRQ(ierr); 2513 PetscFunctionReturn(0); 2514 } 2515 2516 #undef __FUNCT__ 2517 #define __FUNCT__ "TSGetTime" 2518 /*@ 2519 TSGetTime - Gets the time of the most recently completed step. 2520 2521 Not Collective 2522 2523 Input Parameter: 2524 . ts - the TS context obtained from TSCreate() 2525 2526 Output Parameter: 2527 . t - the current time 2528 2529 Level: beginner 2530 2531 Note: 2532 When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 2533 TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 2534 2535 .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2536 2537 .keywords: TS, get, time 2538 @*/ 2539 PetscErrorCode TSGetTime(TS ts,PetscReal* t) 2540 { 2541 PetscFunctionBegin; 2542 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2543 PetscValidRealPointer(t,2); 2544 *t = ts->ptime; 2545 PetscFunctionReturn(0); 2546 } 2547 2548 #undef __FUNCT__ 2549 #define __FUNCT__ "TSSetTime" 2550 /*@ 2551 TSSetTime - Allows one to reset the time. 2552 2553 Logically Collective on TS 2554 2555 Input Parameters: 2556 + ts - the TS context obtained from TSCreate() 2557 - time - the time 2558 2559 Level: intermediate 2560 2561 .seealso: TSGetTime(), TSSetDuration() 2562 2563 .keywords: TS, set, time 2564 @*/ 2565 PetscErrorCode TSSetTime(TS ts, PetscReal t) 2566 { 2567 PetscFunctionBegin; 2568 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2569 PetscValidLogicalCollectiveReal(ts,t,2); 2570 ts->ptime = t; 2571 PetscFunctionReturn(0); 2572 } 2573 2574 #undef __FUNCT__ 2575 #define __FUNCT__ "TSSetOptionsPrefix" 2576 /*@C 2577 TSSetOptionsPrefix - Sets the prefix used for searching for all 2578 TS options in the database. 2579 2580 Logically Collective on TS 2581 2582 Input Parameter: 2583 + ts - The TS context 2584 - prefix - The prefix to prepend to all option names 2585 2586 Notes: 2587 A hyphen (-) must NOT be given at the beginning of the prefix name. 2588 The first character of all runtime options is AUTOMATICALLY the 2589 hyphen. 2590 2591 Level: advanced 2592 2593 .keywords: TS, set, options, prefix, database 2594 2595 .seealso: TSSetFromOptions() 2596 2597 @*/ 2598 PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 2599 { 2600 PetscErrorCode ierr; 2601 SNES snes; 2602 2603 PetscFunctionBegin; 2604 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2605 ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2606 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2607 ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2608 PetscFunctionReturn(0); 2609 } 2610 2611 2612 #undef __FUNCT__ 2613 #define __FUNCT__ "TSAppendOptionsPrefix" 2614 /*@C 2615 TSAppendOptionsPrefix - Appends to the prefix used for searching for all 2616 TS options in the database. 2617 2618 Logically Collective on TS 2619 2620 Input Parameter: 2621 + ts - The TS context 2622 - prefix - The prefix to prepend to all option names 2623 2624 Notes: 2625 A hyphen (-) must NOT be given at the beginning of the prefix name. 2626 The first character of all runtime options is AUTOMATICALLY the 2627 hyphen. 2628 2629 Level: advanced 2630 2631 .keywords: TS, append, options, prefix, database 2632 2633 .seealso: TSGetOptionsPrefix() 2634 2635 @*/ 2636 PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 2637 { 2638 PetscErrorCode ierr; 2639 SNES snes; 2640 2641 PetscFunctionBegin; 2642 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2643 ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2644 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2645 ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2646 PetscFunctionReturn(0); 2647 } 2648 2649 #undef __FUNCT__ 2650 #define __FUNCT__ "TSGetOptionsPrefix" 2651 /*@C 2652 TSGetOptionsPrefix - Sets the prefix used for searching for all 2653 TS options in the database. 2654 2655 Not Collective 2656 2657 Input Parameter: 2658 . ts - The TS context 2659 2660 Output Parameter: 2661 . prefix - A pointer to the prefix string used 2662 2663 Notes: On the fortran side, the user should pass in a string 'prifix' of 2664 sufficient length to hold the prefix. 2665 2666 Level: intermediate 2667 2668 .keywords: TS, get, options, prefix, database 2669 2670 .seealso: TSAppendOptionsPrefix() 2671 @*/ 2672 PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2673 { 2674 PetscErrorCode ierr; 2675 2676 PetscFunctionBegin; 2677 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2678 PetscValidPointer(prefix,2); 2679 ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2680 PetscFunctionReturn(0); 2681 } 2682 2683 #undef __FUNCT__ 2684 #define __FUNCT__ "TSGetRHSJacobian" 2685 /*@C 2686 TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2687 2688 Not Collective, but parallel objects are returned if TS is parallel 2689 2690 Input Parameter: 2691 . ts - The TS context obtained from TSCreate() 2692 2693 Output Parameters: 2694 + J - The Jacobian J of F, where U_t = G(U,t) 2695 . M - The preconditioner matrix, usually the same as J 2696 . func - Function to compute the Jacobian of the RHS 2697 - ctx - User-defined context for Jacobian evaluation routine 2698 2699 Notes: You can pass in PETSC_NULL for any return argument you do not need. 2700 2701 Level: intermediate 2702 2703 .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2704 2705 .keywords: TS, timestep, get, matrix, Jacobian 2706 @*/ 2707 PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2708 { 2709 PetscErrorCode ierr; 2710 SNES snes; 2711 DM dm; 2712 2713 PetscFunctionBegin; 2714 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2715 ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 2716 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 2717 ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 2718 PetscFunctionReturn(0); 2719 } 2720 2721 #undef __FUNCT__ 2722 #define __FUNCT__ "TSGetIJacobian" 2723 /*@C 2724 TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 2725 2726 Not Collective, but parallel objects are returned if TS is parallel 2727 2728 Input Parameter: 2729 . ts - The TS context obtained from TSCreate() 2730 2731 Output Parameters: 2732 + A - The Jacobian of F(t,U,U_t) 2733 . B - The preconditioner matrix, often the same as A 2734 . f - The function to compute the matrices 2735 - ctx - User-defined context for Jacobian evaluation routine 2736 2737 Notes: You can pass in PETSC_NULL for any return argument you do not need. 2738 2739 Level: advanced 2740 2741 .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2742 2743 .keywords: TS, timestep, get, matrix, Jacobian 2744 @*/ 2745 PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 2746 { 2747 PetscErrorCode ierr; 2748 SNES snes; 2749 DM dm; 2750 2751 PetscFunctionBegin; 2752 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2753 ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 2754 ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 2755 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 2756 ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 2757 PetscFunctionReturn(0); 2758 } 2759 2760 struct _n_TSMonitorDrawCtx { 2761 PetscViewer viewer; 2762 Vec initialsolution; 2763 PetscBool showinitial; 2764 PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 2765 }; 2766 2767 #undef __FUNCT__ 2768 #define __FUNCT__ "TSMonitorDrawSolution" 2769 /*@C 2770 TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 2771 VecView() for the solution at each timestep 2772 2773 Collective on TS 2774 2775 Input Parameters: 2776 + ts - the TS context 2777 . step - current time-step 2778 . ptime - current time 2779 - dummy - either a viewer or PETSC_NULL 2780 2781 Options Database: 2782 . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 2783 2784 Notes: the initial solution and current solution are not displayed with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 2785 will look bad 2786 2787 Level: intermediate 2788 2789 .keywords: TS, vector, monitor, view 2790 2791 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 2792 @*/ 2793 PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 2794 { 2795 PetscErrorCode ierr; 2796 TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 2797 2798 PetscFunctionBegin; 2799 if (!step && ictx->showinitial) { 2800 if (!ictx->initialsolution) { 2801 ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 2802 } 2803 ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 2804 } 2805 if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0); 2806 2807 if (ictx->showinitial) { 2808 PetscReal pause; 2809 ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 2810 ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 2811 ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 2812 ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 2813 ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 2814 } 2815 ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 2816 if (ictx->showinitial) { 2817 ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 2818 } 2819 PetscFunctionReturn(0); 2820 } 2821 2822 2823 #undef __FUNCT__ 2824 #define __FUNCT__ "TSMonitorDrawCtxDestroy" 2825 /*@C 2826 TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 2827 2828 Collective on TS 2829 2830 Input Parameters: 2831 . ctx - the monitor context 2832 2833 Level: intermediate 2834 2835 .keywords: TS, vector, monitor, view 2836 2837 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 2838 @*/ 2839 PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 2840 { 2841 PetscErrorCode ierr; 2842 2843 PetscFunctionBegin; 2844 ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 2845 ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 2846 ierr = PetscFree(*ictx);CHKERRQ(ierr); 2847 PetscFunctionReturn(0); 2848 } 2849 2850 #undef __FUNCT__ 2851 #define __FUNCT__ "TSMonitorDrawCtxCreate" 2852 /*@C 2853 TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 2854 2855 Collective on TS 2856 2857 Input Parameter: 2858 . ts - time-step context 2859 2860 Output Patameter: 2861 . ctx - the monitor context 2862 2863 Options Database: 2864 . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 2865 2866 Level: intermediate 2867 2868 .keywords: TS, vector, monitor, view 2869 2870 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 2871 @*/ 2872 PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 2873 { 2874 PetscErrorCode ierr; 2875 2876 PetscFunctionBegin; 2877 ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr); 2878 ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 2879 (*ctx)->showinitial = PETSC_FALSE; 2880 (*ctx)->howoften = howoften; 2881 ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,PETSC_NULL);CHKERRQ(ierr); 2882 PetscFunctionReturn(0); 2883 } 2884 2885 #undef __FUNCT__ 2886 #define __FUNCT__ "TSMonitorDrawError" 2887 /*@C 2888 TSMonitorDrawError - Monitors progress of the TS solvers by calling 2889 VecView() for the error at each timestep 2890 2891 Collective on TS 2892 2893 Input Parameters: 2894 + ts - the TS context 2895 . step - current time-step 2896 . ptime - current time 2897 - dummy - either a viewer or PETSC_NULL 2898 2899 Level: intermediate 2900 2901 .keywords: TS, vector, monitor, view 2902 2903 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 2904 @*/ 2905 PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 2906 { 2907 PetscErrorCode ierr; 2908 TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 2909 PetscViewer viewer = ctx->viewer; 2910 Vec work; 2911 2912 PetscFunctionBegin; 2913 if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0); 2914 ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 2915 ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 2916 ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 2917 ierr = VecView(work,viewer);CHKERRQ(ierr); 2918 ierr = VecDestroy(&work);CHKERRQ(ierr); 2919 PetscFunctionReturn(0); 2920 } 2921 2922 #undef __FUNCT__ 2923 #define __FUNCT__ "TSSetDM" 2924 /*@ 2925 TSSetDM - Sets the DM that may be used by some preconditioners 2926 2927 Logically Collective on TS and DM 2928 2929 Input Parameters: 2930 + ts - the preconditioner context 2931 - dm - the dm 2932 2933 Level: intermediate 2934 2935 2936 .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 2937 @*/ 2938 PetscErrorCode TSSetDM(TS ts,DM dm) 2939 { 2940 PetscErrorCode ierr; 2941 SNES snes; 2942 TSDM tsdm; 2943 2944 PetscFunctionBegin; 2945 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2946 ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 2947 if (ts->dm) { /* Move the TSDM context over to the new DM unless the new DM already has one */ 2948 PetscContainer oldcontainer,container; 2949 ierr = PetscObjectQuery((PetscObject)ts->dm,"TSDM",(PetscObject*)&oldcontainer);CHKERRQ(ierr); 2950 ierr = PetscObjectQuery((PetscObject)dm,"TSDM",(PetscObject*)&container);CHKERRQ(ierr); 2951 if (oldcontainer && !container) { 2952 ierr = DMTSCopyContext(ts->dm,dm);CHKERRQ(ierr); 2953 ierr = DMTSGetContext(ts->dm,&tsdm);CHKERRQ(ierr); 2954 if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 2955 tsdm->originaldm = dm; 2956 } 2957 } 2958 ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 2959 } 2960 ts->dm = dm; 2961 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2962 ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 2963 PetscFunctionReturn(0); 2964 } 2965 2966 #undef __FUNCT__ 2967 #define __FUNCT__ "TSGetDM" 2968 /*@ 2969 TSGetDM - Gets the DM that may be used by some preconditioners 2970 2971 Not Collective 2972 2973 Input Parameter: 2974 . ts - the preconditioner context 2975 2976 Output Parameter: 2977 . dm - the dm 2978 2979 Level: intermediate 2980 2981 2982 .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 2983 @*/ 2984 PetscErrorCode TSGetDM(TS ts,DM *dm) 2985 { 2986 PetscErrorCode ierr; 2987 2988 PetscFunctionBegin; 2989 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2990 if (!ts->dm) { 2991 ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr); 2992 if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 2993 } 2994 *dm = ts->dm; 2995 PetscFunctionReturn(0); 2996 } 2997 2998 #undef __FUNCT__ 2999 #define __FUNCT__ "SNESTSFormFunction" 3000 /*@ 3001 SNESTSFormFunction - Function to evaluate nonlinear residual 3002 3003 Logically Collective on SNES 3004 3005 Input Parameter: 3006 + snes - nonlinear solver 3007 . U - the current state at which to evaluate the residual 3008 - ctx - user context, must be a TS 3009 3010 Output Parameter: 3011 . F - the nonlinear residual 3012 3013 Notes: 3014 This function is not normally called by users and is automatically registered with the SNES used by TS. 3015 It is most frequently passed to MatFDColoringSetFunction(). 3016 3017 Level: advanced 3018 3019 .seealso: SNESSetFunction(), MatFDColoringSetFunction() 3020 @*/ 3021 PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 3022 { 3023 TS ts = (TS)ctx; 3024 PetscErrorCode ierr; 3025 3026 PetscFunctionBegin; 3027 PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3028 PetscValidHeaderSpecific(U,VEC_CLASSID,2); 3029 PetscValidHeaderSpecific(F,VEC_CLASSID,3); 3030 PetscValidHeaderSpecific(ts,TS_CLASSID,4); 3031 ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 3032 PetscFunctionReturn(0); 3033 } 3034 3035 #undef __FUNCT__ 3036 #define __FUNCT__ "SNESTSFormJacobian" 3037 /*@ 3038 SNESTSFormJacobian - Function to evaluate the Jacobian 3039 3040 Collective on SNES 3041 3042 Input Parameter: 3043 + snes - nonlinear solver 3044 . U - the current state at which to evaluate the residual 3045 - ctx - user context, must be a TS 3046 3047 Output Parameter: 3048 + A - the Jacobian 3049 . B - the preconditioning matrix (may be the same as A) 3050 - flag - indicates any structure change in the matrix 3051 3052 Notes: 3053 This function is not normally called by users and is automatically registered with the SNES used by TS. 3054 3055 Level: developer 3056 3057 .seealso: SNESSetJacobian() 3058 @*/ 3059 PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx) 3060 { 3061 TS ts = (TS)ctx; 3062 PetscErrorCode ierr; 3063 3064 PetscFunctionBegin; 3065 PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3066 PetscValidHeaderSpecific(U,VEC_CLASSID,2); 3067 PetscValidPointer(A,3); 3068 PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 3069 PetscValidPointer(B,4); 3070 PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 3071 PetscValidPointer(flag,5); 3072 PetscValidHeaderSpecific(ts,TS_CLASSID,6); 3073 ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr); 3074 PetscFunctionReturn(0); 3075 } 3076 3077 #undef __FUNCT__ 3078 #define __FUNCT__ "TSComputeRHSFunctionLinear" 3079 /*@C 3080 TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 3081 3082 Collective on TS 3083 3084 Input Arguments: 3085 + ts - time stepping context 3086 . t - time at which to evaluate 3087 . U - state at which to evaluate 3088 - ctx - context 3089 3090 Output Arguments: 3091 . F - right hand side 3092 3093 Level: intermediate 3094 3095 Notes: 3096 This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 3097 The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 3098 3099 .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 3100 @*/ 3101 PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 3102 { 3103 PetscErrorCode ierr; 3104 Mat Arhs,Brhs; 3105 MatStructure flg2; 3106 3107 PetscFunctionBegin; 3108 ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 3109 ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 3110 ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 3111 PetscFunctionReturn(0); 3112 } 3113 3114 #undef __FUNCT__ 3115 #define __FUNCT__ "TSComputeRHSJacobianConstant" 3116 /*@C 3117 TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 3118 3119 Collective on TS 3120 3121 Input Arguments: 3122 + ts - time stepping context 3123 . t - time at which to evaluate 3124 . U - state at which to evaluate 3125 - ctx - context 3126 3127 Output Arguments: 3128 + A - pointer to operator 3129 . B - pointer to preconditioning matrix 3130 - flg - matrix structure flag 3131 3132 Level: intermediate 3133 3134 Notes: 3135 This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 3136 3137 .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 3138 @*/ 3139 PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx) 3140 { 3141 PetscFunctionBegin; 3142 *flg = SAME_PRECONDITIONER; 3143 PetscFunctionReturn(0); 3144 } 3145 3146 #undef __FUNCT__ 3147 #define __FUNCT__ "TSComputeIFunctionLinear" 3148 /*@C 3149 TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 3150 3151 Collective on TS 3152 3153 Input Arguments: 3154 + ts - time stepping context 3155 . t - time at which to evaluate 3156 . U - state at which to evaluate 3157 . Udot - time derivative of state vector 3158 - ctx - context 3159 3160 Output Arguments: 3161 . F - left hand side 3162 3163 Level: intermediate 3164 3165 Notes: 3166 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 3167 user is required to write their own TSComputeIFunction. 3168 This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 3169 The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 3170 3171 .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 3172 @*/ 3173 PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 3174 { 3175 PetscErrorCode ierr; 3176 Mat A,B; 3177 MatStructure flg2; 3178 3179 PetscFunctionBegin; 3180 ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 3181 ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr); 3182 ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 3183 PetscFunctionReturn(0); 3184 } 3185 3186 #undef __FUNCT__ 3187 #define __FUNCT__ "TSComputeIJacobianConstant" 3188 /*@C 3189 TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent. 3190 3191 Collective on TS 3192 3193 Input Arguments: 3194 + ts - time stepping context 3195 . t - time at which to evaluate 3196 . U - state at which to evaluate 3197 . Udot - time derivative of state vector 3198 . shift - shift to apply 3199 - ctx - context 3200 3201 Output Arguments: 3202 + A - pointer to operator 3203 . B - pointer to preconditioning matrix 3204 - flg - matrix structure flag 3205 3206 Level: intermediate 3207 3208 Notes: 3209 This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 3210 3211 .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 3212 @*/ 3213 PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx) 3214 { 3215 PetscFunctionBegin; 3216 *flg = SAME_PRECONDITIONER; 3217 PetscFunctionReturn(0); 3218 } 3219 3220 #undef __FUNCT__ 3221 #define __FUNCT__ "TSGetConvergedReason" 3222 /*@ 3223 TSGetConvergedReason - Gets the reason the TS iteration was stopped. 3224 3225 Not Collective 3226 3227 Input Parameter: 3228 . ts - the TS context 3229 3230 Output Parameter: 3231 . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 3232 manual pages for the individual convergence tests for complete lists 3233 3234 Level: beginner 3235 3236 Notes: 3237 Can only be called after the call to TSSolve() is complete. 3238 3239 .keywords: TS, nonlinear, set, convergence, test 3240 3241 .seealso: TSSetConvergenceTest(), TSConvergedReason 3242 @*/ 3243 PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 3244 { 3245 PetscFunctionBegin; 3246 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3247 PetscValidPointer(reason,2); 3248 *reason = ts->reason; 3249 PetscFunctionReturn(0); 3250 } 3251 3252 #undef __FUNCT__ 3253 #define __FUNCT__ "TSGetSolveTime" 3254 /*@ 3255 TSGetSolveTime - Gets the time after a call to TSSolve() 3256 3257 Not Collective 3258 3259 Input Parameter: 3260 . ts - the TS context 3261 3262 Output Parameter: 3263 . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 3264 3265 Level: beginner 3266 3267 Notes: 3268 Can only be called after the call to TSSolve() is complete. 3269 3270 .keywords: TS, nonlinear, set, convergence, test 3271 3272 .seealso: TSSetConvergenceTest(), TSConvergedReason 3273 @*/ 3274 PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 3275 { 3276 PetscFunctionBegin; 3277 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3278 PetscValidPointer(ftime,2); 3279 *ftime = ts->solvetime; 3280 PetscFunctionReturn(0); 3281 } 3282 3283 #undef __FUNCT__ 3284 #define __FUNCT__ "TSGetSNESIterations" 3285 /*@ 3286 TSGetSNESIterations - Gets the total number of nonlinear iterations 3287 used by the time integrator. 3288 3289 Not Collective 3290 3291 Input Parameter: 3292 . ts - TS context 3293 3294 Output Parameter: 3295 . nits - number of nonlinear iterations 3296 3297 Notes: 3298 This counter is reset to zero for each successive call to TSSolve(). 3299 3300 Level: intermediate 3301 3302 .keywords: TS, get, number, nonlinear, iterations 3303 3304 .seealso: TSGetKSPIterations() 3305 @*/ 3306 PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 3307 { 3308 PetscFunctionBegin; 3309 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3310 PetscValidIntPointer(nits,2); 3311 *nits = ts->snes_its; 3312 PetscFunctionReturn(0); 3313 } 3314 3315 #undef __FUNCT__ 3316 #define __FUNCT__ "TSGetKSPIterations" 3317 /*@ 3318 TSGetKSPIterations - Gets the total number of linear iterations 3319 used by the time integrator. 3320 3321 Not Collective 3322 3323 Input Parameter: 3324 . ts - TS context 3325 3326 Output Parameter: 3327 . lits - number of linear iterations 3328 3329 Notes: 3330 This counter is reset to zero for each successive call to TSSolve(). 3331 3332 Level: intermediate 3333 3334 .keywords: TS, get, number, linear, iterations 3335 3336 .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 3337 @*/ 3338 PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 3339 { 3340 PetscFunctionBegin; 3341 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3342 PetscValidIntPointer(lits,2); 3343 *lits = ts->ksp_its; 3344 PetscFunctionReturn(0); 3345 } 3346 3347 #undef __FUNCT__ 3348 #define __FUNCT__ "TSGetStepRejections" 3349 /*@ 3350 TSGetStepRejections - Gets the total number of rejected steps. 3351 3352 Not Collective 3353 3354 Input Parameter: 3355 . ts - TS context 3356 3357 Output Parameter: 3358 . rejects - number of steps rejected 3359 3360 Notes: 3361 This counter is reset to zero for each successive call to TSSolve(). 3362 3363 Level: intermediate 3364 3365 .keywords: TS, get, number 3366 3367 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 3368 @*/ 3369 PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 3370 { 3371 PetscFunctionBegin; 3372 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3373 PetscValidIntPointer(rejects,2); 3374 *rejects = ts->reject; 3375 PetscFunctionReturn(0); 3376 } 3377 3378 #undef __FUNCT__ 3379 #define __FUNCT__ "TSGetSNESFailures" 3380 /*@ 3381 TSGetSNESFailures - Gets the total number of failed SNES solves 3382 3383 Not Collective 3384 3385 Input Parameter: 3386 . ts - TS context 3387 3388 Output Parameter: 3389 . fails - number of failed nonlinear solves 3390 3391 Notes: 3392 This counter is reset to zero for each successive call to TSSolve(). 3393 3394 Level: intermediate 3395 3396 .keywords: TS, get, number 3397 3398 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 3399 @*/ 3400 PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 3401 { 3402 PetscFunctionBegin; 3403 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3404 PetscValidIntPointer(fails,2); 3405 *fails = ts->num_snes_failures; 3406 PetscFunctionReturn(0); 3407 } 3408 3409 #undef __FUNCT__ 3410 #define __FUNCT__ "TSSetMaxStepRejections" 3411 /*@ 3412 TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 3413 3414 Not Collective 3415 3416 Input Parameter: 3417 + ts - TS context 3418 - rejects - maximum number of rejected steps, pass -1 for unlimited 3419 3420 Notes: 3421 The counter is reset to zero for each step 3422 3423 Options Database Key: 3424 . -ts_max_reject - Maximum number of step rejections before a step fails 3425 3426 Level: intermediate 3427 3428 .keywords: TS, set, maximum, number 3429 3430 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3431 @*/ 3432 PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 3433 { 3434 PetscFunctionBegin; 3435 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3436 ts->max_reject = rejects; 3437 PetscFunctionReturn(0); 3438 } 3439 3440 #undef __FUNCT__ 3441 #define __FUNCT__ "TSSetMaxSNESFailures" 3442 /*@ 3443 TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 3444 3445 Not Collective 3446 3447 Input Parameter: 3448 + ts - TS context 3449 - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 3450 3451 Notes: 3452 The counter is reset to zero for each successive call to TSSolve(). 3453 3454 Options Database Key: 3455 . -ts_max_snes_failures - Maximum number of nonlinear solve failures 3456 3457 Level: intermediate 3458 3459 .keywords: TS, set, maximum, number 3460 3461 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 3462 @*/ 3463 PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 3464 { 3465 PetscFunctionBegin; 3466 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3467 ts->max_snes_failures = fails; 3468 PetscFunctionReturn(0); 3469 } 3470 3471 #undef __FUNCT__ 3472 #define __FUNCT__ "TSSetErrorIfStepFails()" 3473 /*@ 3474 TSSetErrorIfStepFails - Error if no step succeeds 3475 3476 Not Collective 3477 3478 Input Parameter: 3479 + ts - TS context 3480 - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 3481 3482 Options Database Key: 3483 . -ts_error_if_step_fails - Error if no step succeeds 3484 3485 Level: intermediate 3486 3487 .keywords: TS, set, error 3488 3489 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3490 @*/ 3491 PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 3492 { 3493 PetscFunctionBegin; 3494 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3495 ts->errorifstepfailed = err; 3496 PetscFunctionReturn(0); 3497 } 3498 3499 #undef __FUNCT__ 3500 #define __FUNCT__ "TSMonitorSolutionBinary" 3501 /*@C 3502 TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 3503 3504 Collective on TS 3505 3506 Input Parameters: 3507 + ts - the TS context 3508 . step - current time-step 3509 . ptime - current time 3510 . u - current state 3511 - viewer - binary viewer 3512 3513 Level: intermediate 3514 3515 .keywords: TS, vector, monitor, view 3516 3517 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3518 @*/ 3519 PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 3520 { 3521 PetscErrorCode ierr; 3522 PetscViewer v = (PetscViewer)viewer; 3523 3524 PetscFunctionBegin; 3525 ierr = VecView(u,v);CHKERRQ(ierr); 3526 PetscFunctionReturn(0); 3527 } 3528 3529 #undef __FUNCT__ 3530 #define __FUNCT__ "TSMonitorSolutionVTK" 3531 /*@C 3532 TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 3533 3534 Collective on TS 3535 3536 Input Parameters: 3537 + ts - the TS context 3538 . step - current time-step 3539 . ptime - current time 3540 . u - current state 3541 - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3542 3543 Level: intermediate 3544 3545 Notes: 3546 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. 3547 These are named according to the file name template. 3548 3549 This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 3550 3551 .keywords: TS, vector, monitor, view 3552 3553 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3554 @*/ 3555 PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 3556 { 3557 PetscErrorCode ierr; 3558 char filename[PETSC_MAX_PATH_LEN]; 3559 PetscViewer viewer; 3560 3561 PetscFunctionBegin; 3562 ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 3563 ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 3564 ierr = VecView(u,viewer);CHKERRQ(ierr); 3565 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 3566 PetscFunctionReturn(0); 3567 } 3568 3569 #undef __FUNCT__ 3570 #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 3571 /*@C 3572 TSMonitorSolutionVTKDestroy - Destroy context for monitoring 3573 3574 Collective on TS 3575 3576 Input Parameters: 3577 . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3578 3579 Level: intermediate 3580 3581 Note: 3582 This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 3583 3584 .keywords: TS, vector, monitor, view 3585 3586 .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 3587 @*/ 3588 PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 3589 { 3590 PetscErrorCode ierr; 3591 3592 PetscFunctionBegin; 3593 ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 3594 PetscFunctionReturn(0); 3595 } 3596 3597 #undef __FUNCT__ 3598 #define __FUNCT__ "TSGetAdapt" 3599 /*@ 3600 TSGetAdapt - Get the adaptive controller context for the current method 3601 3602 Collective on TS if controller has not been created yet 3603 3604 Input Arguments: 3605 . ts - time stepping context 3606 3607 Output Arguments: 3608 . adapt - adaptive controller 3609 3610 Level: intermediate 3611 3612 .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 3613 @*/ 3614 PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 3615 { 3616 PetscErrorCode ierr; 3617 3618 PetscFunctionBegin; 3619 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3620 PetscValidPointer(adapt,2); 3621 if (!ts->adapt) { 3622 ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr); 3623 ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr); 3624 ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 3625 } 3626 *adapt = ts->adapt; 3627 PetscFunctionReturn(0); 3628 } 3629 3630 #undef __FUNCT__ 3631 #define __FUNCT__ "TSSetTolerances" 3632 /*@ 3633 TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 3634 3635 Logically Collective 3636 3637 Input Arguments: 3638 + ts - time integration context 3639 . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 3640 . vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present 3641 . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 3642 - vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present 3643 3644 Level: beginner 3645 3646 .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 3647 @*/ 3648 PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 3649 { 3650 PetscErrorCode ierr; 3651 3652 PetscFunctionBegin; 3653 if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 3654 if (vatol) { 3655 ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 3656 ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 3657 ts->vatol = vatol; 3658 } 3659 if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 3660 if (vrtol) { 3661 ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 3662 ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 3663 ts->vrtol = vrtol; 3664 } 3665 PetscFunctionReturn(0); 3666 } 3667 3668 #undef __FUNCT__ 3669 #define __FUNCT__ "TSGetTolerances" 3670 /*@ 3671 TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 3672 3673 Logically Collective 3674 3675 Input Arguments: 3676 . ts - time integration context 3677 3678 Output Arguments: 3679 + atol - scalar absolute tolerances, PETSC_NULL to ignore 3680 . vatol - vector of absolute tolerances, PETSC_NULL to ignore 3681 . rtol - scalar relative tolerances, PETSC_NULL to ignore 3682 - vrtol - vector of relative tolerances, PETSC_NULL to ignore 3683 3684 Level: beginner 3685 3686 .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 3687 @*/ 3688 PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 3689 { 3690 PetscFunctionBegin; 3691 if (atol) *atol = ts->atol; 3692 if (vatol) *vatol = ts->vatol; 3693 if (rtol) *rtol = ts->rtol; 3694 if (vrtol) *vrtol = ts->vrtol; 3695 PetscFunctionReturn(0); 3696 } 3697 3698 #undef __FUNCT__ 3699 #define __FUNCT__ "TSErrorNormWRMS" 3700 /*@ 3701 TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 3702 3703 Collective on TS 3704 3705 Input Arguments: 3706 + ts - time stepping context 3707 - Y - state vector to be compared to ts->vec_sol 3708 3709 Output Arguments: 3710 . norm - weighted norm, a value of 1.0 is considered small 3711 3712 Level: developer 3713 3714 .seealso: TSSetTolerances() 3715 @*/ 3716 PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 3717 { 3718 PetscErrorCode ierr; 3719 PetscInt i,n,N; 3720 const PetscScalar *u,*y; 3721 Vec U; 3722 PetscReal sum,gsum; 3723 3724 PetscFunctionBegin; 3725 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3726 PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 3727 PetscValidPointer(norm,3); 3728 U = ts->vec_sol; 3729 PetscCheckSameTypeAndComm(U,1,Y,2); 3730 if (U == Y) SETERRQ(((PetscObject)U)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 3731 3732 ierr = VecGetSize(U,&N);CHKERRQ(ierr); 3733 ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 3734 ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 3735 ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 3736 sum = 0.; 3737 if (ts->vatol && ts->vrtol) { 3738 const PetscScalar *atol,*rtol; 3739 ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3740 ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3741 for (i=0; i<n; i++) { 3742 PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 3743 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3744 } 3745 ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3746 ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3747 } else if (ts->vatol) { /* vector atol, scalar rtol */ 3748 const PetscScalar *atol; 3749 ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3750 for (i=0; i<n; i++) { 3751 PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 3752 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3753 } 3754 ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3755 } else if (ts->vrtol) { /* scalar atol, vector rtol */ 3756 const PetscScalar *rtol; 3757 ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3758 for (i=0; i<n; i++) { 3759 PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 3760 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3761 } 3762 ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3763 } else { /* scalar atol, scalar rtol */ 3764 for (i=0; i<n; i++) { 3765 PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 3766 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3767 } 3768 } 3769 ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 3770 ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 3771 3772 ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr); 3773 *norm = PetscSqrtReal(gsum / N); 3774 if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 3775 PetscFunctionReturn(0); 3776 } 3777 3778 #undef __FUNCT__ 3779 #define __FUNCT__ "TSSetCFLTimeLocal" 3780 /*@ 3781 TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 3782 3783 Logically Collective on TS 3784 3785 Input Arguments: 3786 + ts - time stepping context 3787 - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 3788 3789 Note: 3790 After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 3791 3792 Level: intermediate 3793 3794 .seealso: TSGetCFLTime(), TSADAPTCFL 3795 @*/ 3796 PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 3797 { 3798 PetscFunctionBegin; 3799 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3800 ts->cfltime_local = cfltime; 3801 ts->cfltime = -1.; 3802 PetscFunctionReturn(0); 3803 } 3804 3805 #undef __FUNCT__ 3806 #define __FUNCT__ "TSGetCFLTime" 3807 /*@ 3808 TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 3809 3810 Collective on TS 3811 3812 Input Arguments: 3813 . ts - time stepping context 3814 3815 Output Arguments: 3816 . cfltime - maximum stable time step for forward Euler 3817 3818 Level: advanced 3819 3820 .seealso: TSSetCFLTimeLocal() 3821 @*/ 3822 PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 3823 { 3824 PetscErrorCode ierr; 3825 3826 PetscFunctionBegin; 3827 if (ts->cfltime < 0) { 3828 ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr); 3829 } 3830 *cfltime = ts->cfltime; 3831 PetscFunctionReturn(0); 3832 } 3833 3834 #undef __FUNCT__ 3835 #define __FUNCT__ "TSVISetVariableBounds" 3836 /*@ 3837 TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 3838 3839 Input Parameters: 3840 . ts - the TS context. 3841 . xl - lower bound. 3842 . xu - upper bound. 3843 3844 Notes: 3845 If this routine is not called then the lower and upper bounds are set to 3846 SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp(). 3847 3848 Level: advanced 3849 3850 @*/ 3851 PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 3852 { 3853 PetscErrorCode ierr; 3854 SNES snes; 3855 3856 PetscFunctionBegin; 3857 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3858 ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 3859 PetscFunctionReturn(0); 3860 } 3861 3862 #if defined(PETSC_HAVE_MATLAB_ENGINE) 3863 #include <mex.h> 3864 3865 typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 3866 3867 #undef __FUNCT__ 3868 #define __FUNCT__ "TSComputeFunction_Matlab" 3869 /* 3870 TSComputeFunction_Matlab - Calls the function that has been set with 3871 TSSetFunctionMatlab(). 3872 3873 Collective on TS 3874 3875 Input Parameters: 3876 + snes - the TS context 3877 - u - input vector 3878 3879 Output Parameter: 3880 . y - function vector, as set by TSSetFunction() 3881 3882 Notes: 3883 TSComputeFunction() is typically used within nonlinear solvers 3884 implementations, so most users would not generally call this routine 3885 themselves. 3886 3887 Level: developer 3888 3889 .keywords: TS, nonlinear, compute, function 3890 3891 .seealso: TSSetFunction(), TSGetFunction() 3892 */ 3893 PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 3894 { 3895 PetscErrorCode ierr; 3896 TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3897 int nlhs = 1,nrhs = 7; 3898 mxArray *plhs[1],*prhs[7]; 3899 long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 3900 3901 PetscFunctionBegin; 3902 PetscValidHeaderSpecific(snes,TS_CLASSID,1); 3903 PetscValidHeaderSpecific(u,VEC_CLASSID,3); 3904 PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 3905 PetscValidHeaderSpecific(y,VEC_CLASSID,5); 3906 PetscCheckSameComm(snes,1,u,3); 3907 PetscCheckSameComm(snes,1,y,5); 3908 3909 ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3910 ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 3911 ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 3912 ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 3913 prhs[0] = mxCreateDoubleScalar((double)ls); 3914 prhs[1] = mxCreateDoubleScalar(time); 3915 prhs[2] = mxCreateDoubleScalar((double)lx); 3916 prhs[3] = mxCreateDoubleScalar((double)lxdot); 3917 prhs[4] = mxCreateDoubleScalar((double)ly); 3918 prhs[5] = mxCreateString(sctx->funcname); 3919 prhs[6] = sctx->ctx; 3920 ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 3921 ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3922 mxDestroyArray(prhs[0]); 3923 mxDestroyArray(prhs[1]); 3924 mxDestroyArray(prhs[2]); 3925 mxDestroyArray(prhs[3]); 3926 mxDestroyArray(prhs[4]); 3927 mxDestroyArray(prhs[5]); 3928 mxDestroyArray(plhs[0]); 3929 PetscFunctionReturn(0); 3930 } 3931 3932 3933 #undef __FUNCT__ 3934 #define __FUNCT__ "TSSetFunctionMatlab" 3935 /* 3936 TSSetFunctionMatlab - Sets the function evaluation routine and function 3937 vector for use by the TS routines in solving ODEs 3938 equations from MATLAB. Here the function is a string containing the name of a MATLAB function 3939 3940 Logically Collective on TS 3941 3942 Input Parameters: 3943 + ts - the TS context 3944 - func - function evaluation routine 3945 3946 Calling sequence of func: 3947 $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 3948 3949 Level: beginner 3950 3951 .keywords: TS, nonlinear, set, function 3952 3953 .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3954 */ 3955 PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 3956 { 3957 PetscErrorCode ierr; 3958 TSMatlabContext *sctx; 3959 3960 PetscFunctionBegin; 3961 /* currently sctx is memory bleed */ 3962 ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3963 ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3964 /* 3965 This should work, but it doesn't 3966 sctx->ctx = ctx; 3967 mexMakeArrayPersistent(sctx->ctx); 3968 */ 3969 sctx->ctx = mxDuplicateArray(ctx); 3970 ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 3971 PetscFunctionReturn(0); 3972 } 3973 3974 #undef __FUNCT__ 3975 #define __FUNCT__ "TSComputeJacobian_Matlab" 3976 /* 3977 TSComputeJacobian_Matlab - Calls the function that has been set with 3978 TSSetJacobianMatlab(). 3979 3980 Collective on TS 3981 3982 Input Parameters: 3983 + ts - the TS context 3984 . u - input vector 3985 . A, B - the matrices 3986 - ctx - user context 3987 3988 Output Parameter: 3989 . flag - structure of the matrix 3990 3991 Level: developer 3992 3993 .keywords: TS, nonlinear, compute, function 3994 3995 .seealso: TSSetFunction(), TSGetFunction() 3996 @*/ 3997 PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 3998 { 3999 PetscErrorCode ierr; 4000 TSMatlabContext *sctx = (TSMatlabContext *)ctx; 4001 int nlhs = 2,nrhs = 9; 4002 mxArray *plhs[2],*prhs[9]; 4003 long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 4004 4005 PetscFunctionBegin; 4006 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4007 PetscValidHeaderSpecific(u,VEC_CLASSID,3); 4008 4009 /* call Matlab function in ctx with arguments u and y */ 4010 4011 ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 4012 ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 4013 ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 4014 ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 4015 ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 4016 prhs[0] = mxCreateDoubleScalar((double)ls); 4017 prhs[1] = mxCreateDoubleScalar((double)time); 4018 prhs[2] = mxCreateDoubleScalar((double)lx); 4019 prhs[3] = mxCreateDoubleScalar((double)lxdot); 4020 prhs[4] = mxCreateDoubleScalar((double)shift); 4021 prhs[5] = mxCreateDoubleScalar((double)lA); 4022 prhs[6] = mxCreateDoubleScalar((double)lB); 4023 prhs[7] = mxCreateString(sctx->funcname); 4024 prhs[8] = sctx->ctx; 4025 ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 4026 ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4027 *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 4028 mxDestroyArray(prhs[0]); 4029 mxDestroyArray(prhs[1]); 4030 mxDestroyArray(prhs[2]); 4031 mxDestroyArray(prhs[3]); 4032 mxDestroyArray(prhs[4]); 4033 mxDestroyArray(prhs[5]); 4034 mxDestroyArray(prhs[6]); 4035 mxDestroyArray(prhs[7]); 4036 mxDestroyArray(plhs[0]); 4037 mxDestroyArray(plhs[1]); 4038 PetscFunctionReturn(0); 4039 } 4040 4041 4042 #undef __FUNCT__ 4043 #define __FUNCT__ "TSSetJacobianMatlab" 4044 /* 4045 TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 4046 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 4047 4048 Logically Collective on TS 4049 4050 Input Parameters: 4051 + ts - the TS context 4052 . A,B - Jacobian matrices 4053 . func - function evaluation routine 4054 - ctx - user context 4055 4056 Calling sequence of func: 4057 $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 4058 4059 4060 Level: developer 4061 4062 .keywords: TS, nonlinear, set, function 4063 4064 .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4065 */ 4066 PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 4067 { 4068 PetscErrorCode ierr; 4069 TSMatlabContext *sctx; 4070 4071 PetscFunctionBegin; 4072 /* currently sctx is memory bleed */ 4073 ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4074 ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4075 /* 4076 This should work, but it doesn't 4077 sctx->ctx = ctx; 4078 mexMakeArrayPersistent(sctx->ctx); 4079 */ 4080 sctx->ctx = mxDuplicateArray(ctx); 4081 ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 4082 PetscFunctionReturn(0); 4083 } 4084 4085 #undef __FUNCT__ 4086 #define __FUNCT__ "TSMonitor_Matlab" 4087 /* 4088 TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 4089 4090 Collective on TS 4091 4092 .seealso: TSSetFunction(), TSGetFunction() 4093 @*/ 4094 PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 4095 { 4096 PetscErrorCode ierr; 4097 TSMatlabContext *sctx = (TSMatlabContext *)ctx; 4098 int nlhs = 1,nrhs = 6; 4099 mxArray *plhs[1],*prhs[6]; 4100 long long int lx = 0,ls = 0; 4101 4102 PetscFunctionBegin; 4103 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4104 PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4105 4106 ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 4107 ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 4108 prhs[0] = mxCreateDoubleScalar((double)ls); 4109 prhs[1] = mxCreateDoubleScalar((double)it); 4110 prhs[2] = mxCreateDoubleScalar((double)time); 4111 prhs[3] = mxCreateDoubleScalar((double)lx); 4112 prhs[4] = mxCreateString(sctx->funcname); 4113 prhs[5] = sctx->ctx; 4114 ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 4115 ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4116 mxDestroyArray(prhs[0]); 4117 mxDestroyArray(prhs[1]); 4118 mxDestroyArray(prhs[2]); 4119 mxDestroyArray(prhs[3]); 4120 mxDestroyArray(prhs[4]); 4121 mxDestroyArray(plhs[0]); 4122 PetscFunctionReturn(0); 4123 } 4124 4125 4126 #undef __FUNCT__ 4127 #define __FUNCT__ "TSMonitorSetMatlab" 4128 /* 4129 TSMonitorSetMatlab - Sets the monitor function from Matlab 4130 4131 Level: developer 4132 4133 .keywords: TS, nonlinear, set, function 4134 4135 .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4136 */ 4137 PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 4138 { 4139 PetscErrorCode ierr; 4140 TSMatlabContext *sctx; 4141 4142 PetscFunctionBegin; 4143 /* currently sctx is memory bleed */ 4144 ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4145 ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4146 /* 4147 This should work, but it doesn't 4148 sctx->ctx = ctx; 4149 mexMakeArrayPersistent(sctx->ctx); 4150 */ 4151 sctx->ctx = mxDuplicateArray(ctx); 4152 ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 4153 PetscFunctionReturn(0); 4154 } 4155 #endif 4156 4157 4158 4159 #undef __FUNCT__ 4160 #define __FUNCT__ "TSMonitorLGSolution" 4161 /*@C 4162 TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 4163 in a time based line graph 4164 4165 Collective on TS 4166 4167 Input Parameters: 4168 + ts - the TS context 4169 . step - current time-step 4170 . ptime - current time 4171 - lg - a line graph object 4172 4173 Level: intermediate 4174 4175 Notes: each process in a parallel run displays its component solutions in a separate window 4176 4177 .keywords: TS, vector, monitor, view 4178 4179 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4180 @*/ 4181 PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4182 { 4183 PetscErrorCode ierr; 4184 TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4185 const PetscScalar *yy; 4186 PetscInt dim; 4187 4188 PetscFunctionBegin; 4189 if (!step) { 4190 PetscDrawAxis axis; 4191 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4192 ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 4193 ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 4194 ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 4195 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4196 } 4197 ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr); 4198 #if defined(PETSC_USE_COMPLEX) 4199 { 4200 PetscReal *yreal; 4201 PetscInt i,n; 4202 ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr); 4203 ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4204 for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 4205 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4206 ierr = PetscFree(yreal);CHKERRQ(ierr); 4207 } 4208 #else 4209 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4210 #endif 4211 ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr); 4212 if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){ 4213 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4214 } 4215 PetscFunctionReturn(0); 4216 } 4217 4218 #undef __FUNCT__ 4219 #define __FUNCT__ "TSMonitorLGError" 4220 /*@C 4221 TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 4222 in a time based line graph 4223 4224 Collective on TS 4225 4226 Input Parameters: 4227 + ts - the TS context 4228 . step - current time-step 4229 . ptime - current time 4230 - lg - a line graph object 4231 4232 Level: intermediate 4233 4234 Notes: 4235 Only for sequential solves. 4236 4237 The user must provide the solution using TSSetSolutionFunction() to use this monitor. 4238 4239 Options Database Keys: 4240 . -ts_monitor_lg_error - create a graphical monitor of error history 4241 4242 .keywords: TS, vector, monitor, view 4243 4244 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 4245 @*/ 4246 PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4247 { 4248 PetscErrorCode ierr; 4249 TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4250 const PetscScalar *yy; 4251 Vec y; 4252 PetscInt dim; 4253 4254 PetscFunctionBegin; 4255 if (!step) { 4256 PetscDrawAxis axis; 4257 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4258 ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 4259 ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 4260 ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 4261 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4262 } 4263 ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 4264 ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 4265 ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 4266 ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 4267 #if defined(PETSC_USE_COMPLEX) 4268 { 4269 PetscReal *yreal; 4270 PetscInt i,n; 4271 ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 4272 ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4273 for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 4274 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4275 ierr = PetscFree(yreal);CHKERRQ(ierr); 4276 } 4277 #else 4278 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4279 #endif 4280 ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 4281 ierr = VecDestroy(&y);CHKERRQ(ierr); 4282 if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){ 4283 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4284 } 4285 PetscFunctionReturn(0); 4286 } 4287 4288 #undef __FUNCT__ 4289 #define __FUNCT__ "TSMonitorLGSNESIterations" 4290 PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4291 { 4292 TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4293 PetscReal x = ptime,y; 4294 PetscErrorCode ierr; 4295 PetscInt its; 4296 4297 PetscFunctionBegin; 4298 if (!n) { 4299 PetscDrawAxis axis; 4300 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4301 ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 4302 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4303 ctx->snes_its = 0; 4304 } 4305 ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 4306 y = its - ctx->snes_its; 4307 ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4308 if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){ 4309 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4310 } 4311 ctx->snes_its = its; 4312 PetscFunctionReturn(0); 4313 } 4314 4315 #undef __FUNCT__ 4316 #define __FUNCT__ "TSMonitorLGKSPIterations" 4317 PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4318 { 4319 TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4320 PetscReal x = ptime,y; 4321 PetscErrorCode ierr; 4322 PetscInt its; 4323 4324 PetscFunctionBegin; 4325 if (!n) { 4326 PetscDrawAxis axis; 4327 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4328 ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 4329 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4330 ctx->ksp_its = 0; 4331 } 4332 ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 4333 y = its - ctx->ksp_its; 4334 ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4335 if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){ 4336 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4337 } 4338 ctx->ksp_its = its; 4339 PetscFunctionReturn(0); 4340 } 4341 4342 #undef __FUNCT__ 4343 #define __FUNCT__ "TSComputeLinearStability" 4344 /*@ 4345 TSComputeLinearStability - computes the linear stability function at a point 4346 4347 Collective on TS and Vec 4348 4349 Input Parameters: 4350 + ts - the TS context 4351 - xr,xi - real and imaginary part of input arguments 4352 4353 Output Parameters: 4354 . yr,yi - real and imaginary part of function value 4355 4356 Level: developer 4357 4358 .keywords: TS, compute 4359 4360 .seealso: TSSetRHSFunction(), TSComputeIFunction() 4361 @*/ 4362 PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 4363 { 4364 PetscErrorCode ierr; 4365 4366 PetscFunctionBegin; 4367 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4368 if (!ts->ops->linearstability) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 4369 ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 4370 PetscFunctionReturn(0); 4371 } 4372