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 = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 2754 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 2755 ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 2756 PetscFunctionReturn(0); 2757 } 2758 2759 struct _n_TSMonitorDrawCtx { 2760 PetscViewer viewer; 2761 Vec initialsolution; 2762 PetscBool showinitial; 2763 PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 2764 }; 2765 2766 #undef __FUNCT__ 2767 #define __FUNCT__ "TSMonitorDrawSolution" 2768 /*@C 2769 TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 2770 VecView() for the solution at each timestep 2771 2772 Collective on TS 2773 2774 Input Parameters: 2775 + ts - the TS context 2776 . step - current time-step 2777 . ptime - current time 2778 - dummy - either a viewer or PETSC_NULL 2779 2780 Options Database: 2781 . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 2782 2783 Notes: the initial solution and current solution are not displayed with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 2784 will look bad 2785 2786 Level: intermediate 2787 2788 .keywords: TS, vector, monitor, view 2789 2790 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 2791 @*/ 2792 PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 2793 { 2794 PetscErrorCode ierr; 2795 TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 2796 2797 PetscFunctionBegin; 2798 if (!step && ictx->showinitial) { 2799 if (!ictx->initialsolution) { 2800 ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 2801 } 2802 ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 2803 } 2804 if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0); 2805 2806 if (ictx->showinitial) { 2807 PetscReal pause; 2808 ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 2809 ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 2810 ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 2811 ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 2812 ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 2813 } 2814 ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 2815 if (ictx->showinitial) { 2816 ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 2817 } 2818 PetscFunctionReturn(0); 2819 } 2820 2821 2822 #undef __FUNCT__ 2823 #define __FUNCT__ "TSMonitorDrawCtxDestroy" 2824 /*@C 2825 TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 2826 2827 Collective on TS 2828 2829 Input Parameters: 2830 . ctx - the monitor context 2831 2832 Level: intermediate 2833 2834 .keywords: TS, vector, monitor, view 2835 2836 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 2837 @*/ 2838 PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 2839 { 2840 PetscErrorCode ierr; 2841 2842 PetscFunctionBegin; 2843 ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 2844 ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 2845 ierr = PetscFree(*ictx);CHKERRQ(ierr); 2846 PetscFunctionReturn(0); 2847 } 2848 2849 #undef __FUNCT__ 2850 #define __FUNCT__ "TSMonitorDrawCtxCreate" 2851 /*@C 2852 TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 2853 2854 Collective on TS 2855 2856 Input Parameter: 2857 . ts - time-step context 2858 2859 Output Patameter: 2860 . ctx - the monitor context 2861 2862 Options Database: 2863 . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 2864 2865 Level: intermediate 2866 2867 .keywords: TS, vector, monitor, view 2868 2869 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 2870 @*/ 2871 PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 2872 { 2873 PetscErrorCode ierr; 2874 2875 PetscFunctionBegin; 2876 ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr); 2877 ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 2878 (*ctx)->showinitial = PETSC_FALSE; 2879 (*ctx)->howoften = howoften; 2880 ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,PETSC_NULL);CHKERRQ(ierr); 2881 PetscFunctionReturn(0); 2882 } 2883 2884 #undef __FUNCT__ 2885 #define __FUNCT__ "TSMonitorDrawError" 2886 /*@C 2887 TSMonitorDrawError - Monitors progress of the TS solvers by calling 2888 VecView() for the error at each timestep 2889 2890 Collective on TS 2891 2892 Input Parameters: 2893 + ts - the TS context 2894 . step - current time-step 2895 . ptime - current time 2896 - dummy - either a viewer or PETSC_NULL 2897 2898 Level: intermediate 2899 2900 .keywords: TS, vector, monitor, view 2901 2902 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 2903 @*/ 2904 PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 2905 { 2906 PetscErrorCode ierr; 2907 TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 2908 PetscViewer viewer = ctx->viewer; 2909 Vec work; 2910 2911 PetscFunctionBegin; 2912 if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0); 2913 ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 2914 ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 2915 ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 2916 ierr = VecView(work,viewer);CHKERRQ(ierr); 2917 ierr = VecDestroy(&work);CHKERRQ(ierr); 2918 PetscFunctionReturn(0); 2919 } 2920 2921 #undef __FUNCT__ 2922 #define __FUNCT__ "TSSetDM" 2923 /*@ 2924 TSSetDM - Sets the DM that may be used by some preconditioners 2925 2926 Logically Collective on TS and DM 2927 2928 Input Parameters: 2929 + ts - the preconditioner context 2930 - dm - the dm 2931 2932 Level: intermediate 2933 2934 2935 .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 2936 @*/ 2937 PetscErrorCode TSSetDM(TS ts,DM dm) 2938 { 2939 PetscErrorCode ierr; 2940 SNES snes; 2941 TSDM tsdm; 2942 2943 PetscFunctionBegin; 2944 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2945 ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 2946 if (ts->dm) { /* Move the TSDM context over to the new DM unless the new DM already has one */ 2947 PetscContainer oldcontainer,container; 2948 ierr = PetscObjectQuery((PetscObject)ts->dm,"TSDM",(PetscObject*)&oldcontainer);CHKERRQ(ierr); 2949 ierr = PetscObjectQuery((PetscObject)dm,"TSDM",(PetscObject*)&container);CHKERRQ(ierr); 2950 if (oldcontainer && !container) { 2951 ierr = DMTSCopyContext(ts->dm,dm);CHKERRQ(ierr); 2952 ierr = DMTSGetContext(ts->dm,&tsdm);CHKERRQ(ierr); 2953 if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 2954 tsdm->originaldm = dm; 2955 } 2956 } 2957 ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 2958 } 2959 ts->dm = dm; 2960 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2961 ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 2962 PetscFunctionReturn(0); 2963 } 2964 2965 #undef __FUNCT__ 2966 #define __FUNCT__ "TSGetDM" 2967 /*@ 2968 TSGetDM - Gets the DM that may be used by some preconditioners 2969 2970 Not Collective 2971 2972 Input Parameter: 2973 . ts - the preconditioner context 2974 2975 Output Parameter: 2976 . dm - the dm 2977 2978 Level: intermediate 2979 2980 2981 .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 2982 @*/ 2983 PetscErrorCode TSGetDM(TS ts,DM *dm) 2984 { 2985 PetscErrorCode ierr; 2986 2987 PetscFunctionBegin; 2988 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2989 if (!ts->dm) { 2990 ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr); 2991 if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 2992 } 2993 *dm = ts->dm; 2994 PetscFunctionReturn(0); 2995 } 2996 2997 #undef __FUNCT__ 2998 #define __FUNCT__ "SNESTSFormFunction" 2999 /*@ 3000 SNESTSFormFunction - Function to evaluate nonlinear residual 3001 3002 Logically Collective on SNES 3003 3004 Input Parameter: 3005 + snes - nonlinear solver 3006 . U - the current state at which to evaluate the residual 3007 - ctx - user context, must be a TS 3008 3009 Output Parameter: 3010 . F - the nonlinear residual 3011 3012 Notes: 3013 This function is not normally called by users and is automatically registered with the SNES used by TS. 3014 It is most frequently passed to MatFDColoringSetFunction(). 3015 3016 Level: advanced 3017 3018 .seealso: SNESSetFunction(), MatFDColoringSetFunction() 3019 @*/ 3020 PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 3021 { 3022 TS ts = (TS)ctx; 3023 PetscErrorCode ierr; 3024 3025 PetscFunctionBegin; 3026 PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3027 PetscValidHeaderSpecific(U,VEC_CLASSID,2); 3028 PetscValidHeaderSpecific(F,VEC_CLASSID,3); 3029 PetscValidHeaderSpecific(ts,TS_CLASSID,4); 3030 ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 3031 PetscFunctionReturn(0); 3032 } 3033 3034 #undef __FUNCT__ 3035 #define __FUNCT__ "SNESTSFormJacobian" 3036 /*@ 3037 SNESTSFormJacobian - Function to evaluate the Jacobian 3038 3039 Collective on SNES 3040 3041 Input Parameter: 3042 + snes - nonlinear solver 3043 . U - the current state at which to evaluate the residual 3044 - ctx - user context, must be a TS 3045 3046 Output Parameter: 3047 + A - the Jacobian 3048 . B - the preconditioning matrix (may be the same as A) 3049 - flag - indicates any structure change in the matrix 3050 3051 Notes: 3052 This function is not normally called by users and is automatically registered with the SNES used by TS. 3053 3054 Level: developer 3055 3056 .seealso: SNESSetJacobian() 3057 @*/ 3058 PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx) 3059 { 3060 TS ts = (TS)ctx; 3061 PetscErrorCode ierr; 3062 3063 PetscFunctionBegin; 3064 PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3065 PetscValidHeaderSpecific(U,VEC_CLASSID,2); 3066 PetscValidPointer(A,3); 3067 PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 3068 PetscValidPointer(B,4); 3069 PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 3070 PetscValidPointer(flag,5); 3071 PetscValidHeaderSpecific(ts,TS_CLASSID,6); 3072 ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr); 3073 PetscFunctionReturn(0); 3074 } 3075 3076 #undef __FUNCT__ 3077 #define __FUNCT__ "TSComputeRHSFunctionLinear" 3078 /*@C 3079 TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 3080 3081 Collective on TS 3082 3083 Input Arguments: 3084 + ts - time stepping context 3085 . t - time at which to evaluate 3086 . U - state at which to evaluate 3087 - ctx - context 3088 3089 Output Arguments: 3090 . F - right hand side 3091 3092 Level: intermediate 3093 3094 Notes: 3095 This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 3096 The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 3097 3098 .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 3099 @*/ 3100 PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 3101 { 3102 PetscErrorCode ierr; 3103 Mat Arhs,Brhs; 3104 MatStructure flg2; 3105 3106 PetscFunctionBegin; 3107 ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 3108 ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 3109 ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 3110 PetscFunctionReturn(0); 3111 } 3112 3113 #undef __FUNCT__ 3114 #define __FUNCT__ "TSComputeRHSJacobianConstant" 3115 /*@C 3116 TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 3117 3118 Collective on TS 3119 3120 Input Arguments: 3121 + ts - time stepping context 3122 . t - time at which to evaluate 3123 . U - state at which to evaluate 3124 - ctx - context 3125 3126 Output Arguments: 3127 + A - pointer to operator 3128 . B - pointer to preconditioning matrix 3129 - flg - matrix structure flag 3130 3131 Level: intermediate 3132 3133 Notes: 3134 This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 3135 3136 .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 3137 @*/ 3138 PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx) 3139 { 3140 PetscFunctionBegin; 3141 *flg = SAME_PRECONDITIONER; 3142 PetscFunctionReturn(0); 3143 } 3144 3145 #undef __FUNCT__ 3146 #define __FUNCT__ "TSComputeIFunctionLinear" 3147 /*@C 3148 TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 3149 3150 Collective on TS 3151 3152 Input Arguments: 3153 + ts - time stepping context 3154 . t - time at which to evaluate 3155 . U - state at which to evaluate 3156 . Udot - time derivative of state vector 3157 - ctx - context 3158 3159 Output Arguments: 3160 . F - left hand side 3161 3162 Level: intermediate 3163 3164 Notes: 3165 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 3166 user is required to write their own TSComputeIFunction. 3167 This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 3168 The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 3169 3170 .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 3171 @*/ 3172 PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 3173 { 3174 PetscErrorCode ierr; 3175 Mat A,B; 3176 MatStructure flg2; 3177 3178 PetscFunctionBegin; 3179 ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 3180 ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr); 3181 ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 3182 PetscFunctionReturn(0); 3183 } 3184 3185 #undef __FUNCT__ 3186 #define __FUNCT__ "TSComputeIJacobianConstant" 3187 /*@C 3188 TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent. 3189 3190 Collective on TS 3191 3192 Input Arguments: 3193 + ts - time stepping context 3194 . t - time at which to evaluate 3195 . U - state at which to evaluate 3196 . Udot - time derivative of state vector 3197 . shift - shift to apply 3198 - ctx - context 3199 3200 Output Arguments: 3201 + A - pointer to operator 3202 . B - pointer to preconditioning matrix 3203 - flg - matrix structure flag 3204 3205 Level: intermediate 3206 3207 Notes: 3208 This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 3209 3210 .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 3211 @*/ 3212 PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx) 3213 { 3214 PetscFunctionBegin; 3215 *flg = SAME_PRECONDITIONER; 3216 PetscFunctionReturn(0); 3217 } 3218 3219 #undef __FUNCT__ 3220 #define __FUNCT__ "TSGetConvergedReason" 3221 /*@ 3222 TSGetConvergedReason - Gets the reason the TS iteration was stopped. 3223 3224 Not Collective 3225 3226 Input Parameter: 3227 . ts - the TS context 3228 3229 Output Parameter: 3230 . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 3231 manual pages for the individual convergence tests for complete lists 3232 3233 Level: beginner 3234 3235 Notes: 3236 Can only be called after the call to TSSolve() is complete. 3237 3238 .keywords: TS, nonlinear, set, convergence, test 3239 3240 .seealso: TSSetConvergenceTest(), TSConvergedReason 3241 @*/ 3242 PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 3243 { 3244 PetscFunctionBegin; 3245 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3246 PetscValidPointer(reason,2); 3247 *reason = ts->reason; 3248 PetscFunctionReturn(0); 3249 } 3250 3251 #undef __FUNCT__ 3252 #define __FUNCT__ "TSGetSolveTime" 3253 /*@ 3254 TSGetSolveTime - Gets the time after a call to TSSolve() 3255 3256 Not Collective 3257 3258 Input Parameter: 3259 . ts - the TS context 3260 3261 Output Parameter: 3262 . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 3263 3264 Level: beginner 3265 3266 Notes: 3267 Can only be called after the call to TSSolve() is complete. 3268 3269 .keywords: TS, nonlinear, set, convergence, test 3270 3271 .seealso: TSSetConvergenceTest(), TSConvergedReason 3272 @*/ 3273 PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 3274 { 3275 PetscFunctionBegin; 3276 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3277 PetscValidPointer(ftime,2); 3278 *ftime = ts->solvetime; 3279 PetscFunctionReturn(0); 3280 } 3281 3282 #undef __FUNCT__ 3283 #define __FUNCT__ "TSGetSNESIterations" 3284 /*@ 3285 TSGetSNESIterations - Gets the total number of nonlinear iterations 3286 used by the time integrator. 3287 3288 Not Collective 3289 3290 Input Parameter: 3291 . ts - TS context 3292 3293 Output Parameter: 3294 . nits - number of nonlinear iterations 3295 3296 Notes: 3297 This counter is reset to zero for each successive call to TSSolve(). 3298 3299 Level: intermediate 3300 3301 .keywords: TS, get, number, nonlinear, iterations 3302 3303 .seealso: TSGetKSPIterations() 3304 @*/ 3305 PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 3306 { 3307 PetscFunctionBegin; 3308 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3309 PetscValidIntPointer(nits,2); 3310 *nits = ts->snes_its; 3311 PetscFunctionReturn(0); 3312 } 3313 3314 #undef __FUNCT__ 3315 #define __FUNCT__ "TSGetKSPIterations" 3316 /*@ 3317 TSGetKSPIterations - Gets the total number of linear iterations 3318 used by the time integrator. 3319 3320 Not Collective 3321 3322 Input Parameter: 3323 . ts - TS context 3324 3325 Output Parameter: 3326 . lits - number of linear iterations 3327 3328 Notes: 3329 This counter is reset to zero for each successive call to TSSolve(). 3330 3331 Level: intermediate 3332 3333 .keywords: TS, get, number, linear, iterations 3334 3335 .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 3336 @*/ 3337 PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 3338 { 3339 PetscFunctionBegin; 3340 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3341 PetscValidIntPointer(lits,2); 3342 *lits = ts->ksp_its; 3343 PetscFunctionReturn(0); 3344 } 3345 3346 #undef __FUNCT__ 3347 #define __FUNCT__ "TSGetStepRejections" 3348 /*@ 3349 TSGetStepRejections - Gets the total number of rejected steps. 3350 3351 Not Collective 3352 3353 Input Parameter: 3354 . ts - TS context 3355 3356 Output Parameter: 3357 . rejects - number of steps rejected 3358 3359 Notes: 3360 This counter is reset to zero for each successive call to TSSolve(). 3361 3362 Level: intermediate 3363 3364 .keywords: TS, get, number 3365 3366 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 3367 @*/ 3368 PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 3369 { 3370 PetscFunctionBegin; 3371 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3372 PetscValidIntPointer(rejects,2); 3373 *rejects = ts->reject; 3374 PetscFunctionReturn(0); 3375 } 3376 3377 #undef __FUNCT__ 3378 #define __FUNCT__ "TSGetSNESFailures" 3379 /*@ 3380 TSGetSNESFailures - Gets the total number of failed SNES solves 3381 3382 Not Collective 3383 3384 Input Parameter: 3385 . ts - TS context 3386 3387 Output Parameter: 3388 . fails - number of failed nonlinear solves 3389 3390 Notes: 3391 This counter is reset to zero for each successive call to TSSolve(). 3392 3393 Level: intermediate 3394 3395 .keywords: TS, get, number 3396 3397 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 3398 @*/ 3399 PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 3400 { 3401 PetscFunctionBegin; 3402 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3403 PetscValidIntPointer(fails,2); 3404 *fails = ts->num_snes_failures; 3405 PetscFunctionReturn(0); 3406 } 3407 3408 #undef __FUNCT__ 3409 #define __FUNCT__ "TSSetMaxStepRejections" 3410 /*@ 3411 TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 3412 3413 Not Collective 3414 3415 Input Parameter: 3416 + ts - TS context 3417 - rejects - maximum number of rejected steps, pass -1 for unlimited 3418 3419 Notes: 3420 The counter is reset to zero for each step 3421 3422 Options Database Key: 3423 . -ts_max_reject - Maximum number of step rejections before a step fails 3424 3425 Level: intermediate 3426 3427 .keywords: TS, set, maximum, number 3428 3429 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3430 @*/ 3431 PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 3432 { 3433 PetscFunctionBegin; 3434 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3435 ts->max_reject = rejects; 3436 PetscFunctionReturn(0); 3437 } 3438 3439 #undef __FUNCT__ 3440 #define __FUNCT__ "TSSetMaxSNESFailures" 3441 /*@ 3442 TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 3443 3444 Not Collective 3445 3446 Input Parameter: 3447 + ts - TS context 3448 - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 3449 3450 Notes: 3451 The counter is reset to zero for each successive call to TSSolve(). 3452 3453 Options Database Key: 3454 . -ts_max_snes_failures - Maximum number of nonlinear solve failures 3455 3456 Level: intermediate 3457 3458 .keywords: TS, set, maximum, number 3459 3460 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 3461 @*/ 3462 PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 3463 { 3464 PetscFunctionBegin; 3465 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3466 ts->max_snes_failures = fails; 3467 PetscFunctionReturn(0); 3468 } 3469 3470 #undef __FUNCT__ 3471 #define __FUNCT__ "TSSetErrorIfStepFails()" 3472 /*@ 3473 TSSetErrorIfStepFails - Error if no step succeeds 3474 3475 Not Collective 3476 3477 Input Parameter: 3478 + ts - TS context 3479 - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 3480 3481 Options Database Key: 3482 . -ts_error_if_step_fails - Error if no step succeeds 3483 3484 Level: intermediate 3485 3486 .keywords: TS, set, error 3487 3488 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3489 @*/ 3490 PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 3491 { 3492 PetscFunctionBegin; 3493 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3494 ts->errorifstepfailed = err; 3495 PetscFunctionReturn(0); 3496 } 3497 3498 #undef __FUNCT__ 3499 #define __FUNCT__ "TSMonitorSolutionBinary" 3500 /*@C 3501 TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 3502 3503 Collective on TS 3504 3505 Input Parameters: 3506 + ts - the TS context 3507 . step - current time-step 3508 . ptime - current time 3509 . u - current state 3510 - viewer - binary viewer 3511 3512 Level: intermediate 3513 3514 .keywords: TS, vector, monitor, view 3515 3516 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3517 @*/ 3518 PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 3519 { 3520 PetscErrorCode ierr; 3521 PetscViewer v = (PetscViewer)viewer; 3522 3523 PetscFunctionBegin; 3524 ierr = VecView(u,v);CHKERRQ(ierr); 3525 PetscFunctionReturn(0); 3526 } 3527 3528 #undef __FUNCT__ 3529 #define __FUNCT__ "TSMonitorSolutionVTK" 3530 /*@C 3531 TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 3532 3533 Collective on TS 3534 3535 Input Parameters: 3536 + ts - the TS context 3537 . step - current time-step 3538 . ptime - current time 3539 . u - current state 3540 - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3541 3542 Level: intermediate 3543 3544 Notes: 3545 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. 3546 These are named according to the file name template. 3547 3548 This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 3549 3550 .keywords: TS, vector, monitor, view 3551 3552 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3553 @*/ 3554 PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 3555 { 3556 PetscErrorCode ierr; 3557 char filename[PETSC_MAX_PATH_LEN]; 3558 PetscViewer viewer; 3559 3560 PetscFunctionBegin; 3561 ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 3562 ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 3563 ierr = VecView(u,viewer);CHKERRQ(ierr); 3564 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 3565 PetscFunctionReturn(0); 3566 } 3567 3568 #undef __FUNCT__ 3569 #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 3570 /*@C 3571 TSMonitorSolutionVTKDestroy - Destroy context for monitoring 3572 3573 Collective on TS 3574 3575 Input Parameters: 3576 . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3577 3578 Level: intermediate 3579 3580 Note: 3581 This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 3582 3583 .keywords: TS, vector, monitor, view 3584 3585 .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 3586 @*/ 3587 PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 3588 { 3589 PetscErrorCode ierr; 3590 3591 PetscFunctionBegin; 3592 ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 3593 PetscFunctionReturn(0); 3594 } 3595 3596 #undef __FUNCT__ 3597 #define __FUNCT__ "TSGetAdapt" 3598 /*@ 3599 TSGetAdapt - Get the adaptive controller context for the current method 3600 3601 Collective on TS if controller has not been created yet 3602 3603 Input Arguments: 3604 . ts - time stepping context 3605 3606 Output Arguments: 3607 . adapt - adaptive controller 3608 3609 Level: intermediate 3610 3611 .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 3612 @*/ 3613 PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 3614 { 3615 PetscErrorCode ierr; 3616 3617 PetscFunctionBegin; 3618 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3619 PetscValidPointer(adapt,2); 3620 if (!ts->adapt) { 3621 ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr); 3622 ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr); 3623 ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 3624 } 3625 *adapt = ts->adapt; 3626 PetscFunctionReturn(0); 3627 } 3628 3629 #undef __FUNCT__ 3630 #define __FUNCT__ "TSSetTolerances" 3631 /*@ 3632 TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 3633 3634 Logically Collective 3635 3636 Input Arguments: 3637 + ts - time integration context 3638 . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 3639 . vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present 3640 . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 3641 - vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present 3642 3643 Level: beginner 3644 3645 .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 3646 @*/ 3647 PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 3648 { 3649 PetscErrorCode ierr; 3650 3651 PetscFunctionBegin; 3652 if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 3653 if (vatol) { 3654 ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 3655 ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 3656 ts->vatol = vatol; 3657 } 3658 if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 3659 if (vrtol) { 3660 ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 3661 ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 3662 ts->vrtol = vrtol; 3663 } 3664 PetscFunctionReturn(0); 3665 } 3666 3667 #undef __FUNCT__ 3668 #define __FUNCT__ "TSGetTolerances" 3669 /*@ 3670 TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 3671 3672 Logically Collective 3673 3674 Input Arguments: 3675 . ts - time integration context 3676 3677 Output Arguments: 3678 + atol - scalar absolute tolerances, PETSC_NULL to ignore 3679 . vatol - vector of absolute tolerances, PETSC_NULL to ignore 3680 . rtol - scalar relative tolerances, PETSC_NULL to ignore 3681 - vrtol - vector of relative tolerances, PETSC_NULL to ignore 3682 3683 Level: beginner 3684 3685 .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 3686 @*/ 3687 PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 3688 { 3689 PetscFunctionBegin; 3690 if (atol) *atol = ts->atol; 3691 if (vatol) *vatol = ts->vatol; 3692 if (rtol) *rtol = ts->rtol; 3693 if (vrtol) *vrtol = ts->vrtol; 3694 PetscFunctionReturn(0); 3695 } 3696 3697 #undef __FUNCT__ 3698 #define __FUNCT__ "TSErrorNormWRMS" 3699 /*@ 3700 TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 3701 3702 Collective on TS 3703 3704 Input Arguments: 3705 + ts - time stepping context 3706 - Y - state vector to be compared to ts->vec_sol 3707 3708 Output Arguments: 3709 . norm - weighted norm, a value of 1.0 is considered small 3710 3711 Level: developer 3712 3713 .seealso: TSSetTolerances() 3714 @*/ 3715 PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 3716 { 3717 PetscErrorCode ierr; 3718 PetscInt i,n,N; 3719 const PetscScalar *u,*y; 3720 Vec U; 3721 PetscReal sum,gsum; 3722 3723 PetscFunctionBegin; 3724 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3725 PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 3726 PetscValidPointer(norm,3); 3727 U = ts->vec_sol; 3728 PetscCheckSameTypeAndComm(U,1,Y,2); 3729 if (U == Y) SETERRQ(((PetscObject)U)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 3730 3731 ierr = VecGetSize(U,&N);CHKERRQ(ierr); 3732 ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 3733 ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 3734 ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 3735 sum = 0.; 3736 if (ts->vatol && ts->vrtol) { 3737 const PetscScalar *atol,*rtol; 3738 ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3739 ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3740 for (i=0; i<n; i++) { 3741 PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 3742 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3743 } 3744 ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3745 ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3746 } else if (ts->vatol) { /* vector atol, scalar rtol */ 3747 const PetscScalar *atol; 3748 ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3749 for (i=0; i<n; i++) { 3750 PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 3751 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3752 } 3753 ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3754 } else if (ts->vrtol) { /* scalar atol, vector rtol */ 3755 const PetscScalar *rtol; 3756 ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3757 for (i=0; i<n; i++) { 3758 PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 3759 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3760 } 3761 ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3762 } else { /* scalar atol, scalar rtol */ 3763 for (i=0; i<n; i++) { 3764 PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 3765 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3766 } 3767 } 3768 ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 3769 ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 3770 3771 ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr); 3772 *norm = PetscSqrtReal(gsum / N); 3773 if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 3774 PetscFunctionReturn(0); 3775 } 3776 3777 #undef __FUNCT__ 3778 #define __FUNCT__ "TSSetCFLTimeLocal" 3779 /*@ 3780 TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 3781 3782 Logically Collective on TS 3783 3784 Input Arguments: 3785 + ts - time stepping context 3786 - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 3787 3788 Note: 3789 After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 3790 3791 Level: intermediate 3792 3793 .seealso: TSGetCFLTime(), TSADAPTCFL 3794 @*/ 3795 PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 3796 { 3797 PetscFunctionBegin; 3798 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3799 ts->cfltime_local = cfltime; 3800 ts->cfltime = -1.; 3801 PetscFunctionReturn(0); 3802 } 3803 3804 #undef __FUNCT__ 3805 #define __FUNCT__ "TSGetCFLTime" 3806 /*@ 3807 TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 3808 3809 Collective on TS 3810 3811 Input Arguments: 3812 . ts - time stepping context 3813 3814 Output Arguments: 3815 . cfltime - maximum stable time step for forward Euler 3816 3817 Level: advanced 3818 3819 .seealso: TSSetCFLTimeLocal() 3820 @*/ 3821 PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 3822 { 3823 PetscErrorCode ierr; 3824 3825 PetscFunctionBegin; 3826 if (ts->cfltime < 0) { 3827 ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr); 3828 } 3829 *cfltime = ts->cfltime; 3830 PetscFunctionReturn(0); 3831 } 3832 3833 #undef __FUNCT__ 3834 #define __FUNCT__ "TSVISetVariableBounds" 3835 /*@ 3836 TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 3837 3838 Input Parameters: 3839 . ts - the TS context. 3840 . xl - lower bound. 3841 . xu - upper bound. 3842 3843 Notes: 3844 If this routine is not called then the lower and upper bounds are set to 3845 SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp(). 3846 3847 Level: advanced 3848 3849 @*/ 3850 PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 3851 { 3852 PetscErrorCode ierr; 3853 SNES snes; 3854 3855 PetscFunctionBegin; 3856 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3857 ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 3858 PetscFunctionReturn(0); 3859 } 3860 3861 #if defined(PETSC_HAVE_MATLAB_ENGINE) 3862 #include <mex.h> 3863 3864 typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 3865 3866 #undef __FUNCT__ 3867 #define __FUNCT__ "TSComputeFunction_Matlab" 3868 /* 3869 TSComputeFunction_Matlab - Calls the function that has been set with 3870 TSSetFunctionMatlab(). 3871 3872 Collective on TS 3873 3874 Input Parameters: 3875 + snes - the TS context 3876 - u - input vector 3877 3878 Output Parameter: 3879 . y - function vector, as set by TSSetFunction() 3880 3881 Notes: 3882 TSComputeFunction() is typically used within nonlinear solvers 3883 implementations, so most users would not generally call this routine 3884 themselves. 3885 3886 Level: developer 3887 3888 .keywords: TS, nonlinear, compute, function 3889 3890 .seealso: TSSetFunction(), TSGetFunction() 3891 */ 3892 PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 3893 { 3894 PetscErrorCode ierr; 3895 TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3896 int nlhs = 1,nrhs = 7; 3897 mxArray *plhs[1],*prhs[7]; 3898 long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 3899 3900 PetscFunctionBegin; 3901 PetscValidHeaderSpecific(snes,TS_CLASSID,1); 3902 PetscValidHeaderSpecific(u,VEC_CLASSID,3); 3903 PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 3904 PetscValidHeaderSpecific(y,VEC_CLASSID,5); 3905 PetscCheckSameComm(snes,1,u,3); 3906 PetscCheckSameComm(snes,1,y,5); 3907 3908 ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3909 ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 3910 ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 3911 ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 3912 prhs[0] = mxCreateDoubleScalar((double)ls); 3913 prhs[1] = mxCreateDoubleScalar(time); 3914 prhs[2] = mxCreateDoubleScalar((double)lx); 3915 prhs[3] = mxCreateDoubleScalar((double)lxdot); 3916 prhs[4] = mxCreateDoubleScalar((double)ly); 3917 prhs[5] = mxCreateString(sctx->funcname); 3918 prhs[6] = sctx->ctx; 3919 ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 3920 ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3921 mxDestroyArray(prhs[0]); 3922 mxDestroyArray(prhs[1]); 3923 mxDestroyArray(prhs[2]); 3924 mxDestroyArray(prhs[3]); 3925 mxDestroyArray(prhs[4]); 3926 mxDestroyArray(prhs[5]); 3927 mxDestroyArray(plhs[0]); 3928 PetscFunctionReturn(0); 3929 } 3930 3931 3932 #undef __FUNCT__ 3933 #define __FUNCT__ "TSSetFunctionMatlab" 3934 /* 3935 TSSetFunctionMatlab - Sets the function evaluation routine and function 3936 vector for use by the TS routines in solving ODEs 3937 equations from MATLAB. Here the function is a string containing the name of a MATLAB function 3938 3939 Logically Collective on TS 3940 3941 Input Parameters: 3942 + ts - the TS context 3943 - func - function evaluation routine 3944 3945 Calling sequence of func: 3946 $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 3947 3948 Level: beginner 3949 3950 .keywords: TS, nonlinear, set, function 3951 3952 .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3953 */ 3954 PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 3955 { 3956 PetscErrorCode ierr; 3957 TSMatlabContext *sctx; 3958 3959 PetscFunctionBegin; 3960 /* currently sctx is memory bleed */ 3961 ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3962 ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3963 /* 3964 This should work, but it doesn't 3965 sctx->ctx = ctx; 3966 mexMakeArrayPersistent(sctx->ctx); 3967 */ 3968 sctx->ctx = mxDuplicateArray(ctx); 3969 ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 3970 PetscFunctionReturn(0); 3971 } 3972 3973 #undef __FUNCT__ 3974 #define __FUNCT__ "TSComputeJacobian_Matlab" 3975 /* 3976 TSComputeJacobian_Matlab - Calls the function that has been set with 3977 TSSetJacobianMatlab(). 3978 3979 Collective on TS 3980 3981 Input Parameters: 3982 + ts - the TS context 3983 . u - input vector 3984 . A, B - the matrices 3985 - ctx - user context 3986 3987 Output Parameter: 3988 . flag - structure of the matrix 3989 3990 Level: developer 3991 3992 .keywords: TS, nonlinear, compute, function 3993 3994 .seealso: TSSetFunction(), TSGetFunction() 3995 @*/ 3996 PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 3997 { 3998 PetscErrorCode ierr; 3999 TSMatlabContext *sctx = (TSMatlabContext *)ctx; 4000 int nlhs = 2,nrhs = 9; 4001 mxArray *plhs[2],*prhs[9]; 4002 long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 4003 4004 PetscFunctionBegin; 4005 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4006 PetscValidHeaderSpecific(u,VEC_CLASSID,3); 4007 4008 /* call Matlab function in ctx with arguments u and y */ 4009 4010 ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 4011 ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 4012 ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 4013 ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 4014 ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 4015 prhs[0] = mxCreateDoubleScalar((double)ls); 4016 prhs[1] = mxCreateDoubleScalar((double)time); 4017 prhs[2] = mxCreateDoubleScalar((double)lx); 4018 prhs[3] = mxCreateDoubleScalar((double)lxdot); 4019 prhs[4] = mxCreateDoubleScalar((double)shift); 4020 prhs[5] = mxCreateDoubleScalar((double)lA); 4021 prhs[6] = mxCreateDoubleScalar((double)lB); 4022 prhs[7] = mxCreateString(sctx->funcname); 4023 prhs[8] = sctx->ctx; 4024 ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 4025 ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4026 *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 4027 mxDestroyArray(prhs[0]); 4028 mxDestroyArray(prhs[1]); 4029 mxDestroyArray(prhs[2]); 4030 mxDestroyArray(prhs[3]); 4031 mxDestroyArray(prhs[4]); 4032 mxDestroyArray(prhs[5]); 4033 mxDestroyArray(prhs[6]); 4034 mxDestroyArray(prhs[7]); 4035 mxDestroyArray(plhs[0]); 4036 mxDestroyArray(plhs[1]); 4037 PetscFunctionReturn(0); 4038 } 4039 4040 4041 #undef __FUNCT__ 4042 #define __FUNCT__ "TSSetJacobianMatlab" 4043 /* 4044 TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 4045 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 4046 4047 Logically Collective on TS 4048 4049 Input Parameters: 4050 + ts - the TS context 4051 . A,B - Jacobian matrices 4052 . func - function evaluation routine 4053 - ctx - user context 4054 4055 Calling sequence of func: 4056 $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 4057 4058 4059 Level: developer 4060 4061 .keywords: TS, nonlinear, set, function 4062 4063 .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4064 */ 4065 PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 4066 { 4067 PetscErrorCode ierr; 4068 TSMatlabContext *sctx; 4069 4070 PetscFunctionBegin; 4071 /* currently sctx is memory bleed */ 4072 ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4073 ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4074 /* 4075 This should work, but it doesn't 4076 sctx->ctx = ctx; 4077 mexMakeArrayPersistent(sctx->ctx); 4078 */ 4079 sctx->ctx = mxDuplicateArray(ctx); 4080 ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 4081 PetscFunctionReturn(0); 4082 } 4083 4084 #undef __FUNCT__ 4085 #define __FUNCT__ "TSMonitor_Matlab" 4086 /* 4087 TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 4088 4089 Collective on TS 4090 4091 .seealso: TSSetFunction(), TSGetFunction() 4092 @*/ 4093 PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 4094 { 4095 PetscErrorCode ierr; 4096 TSMatlabContext *sctx = (TSMatlabContext *)ctx; 4097 int nlhs = 1,nrhs = 6; 4098 mxArray *plhs[1],*prhs[6]; 4099 long long int lx = 0,ls = 0; 4100 4101 PetscFunctionBegin; 4102 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4103 PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4104 4105 ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 4106 ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 4107 prhs[0] = mxCreateDoubleScalar((double)ls); 4108 prhs[1] = mxCreateDoubleScalar((double)it); 4109 prhs[2] = mxCreateDoubleScalar((double)time); 4110 prhs[3] = mxCreateDoubleScalar((double)lx); 4111 prhs[4] = mxCreateString(sctx->funcname); 4112 prhs[5] = sctx->ctx; 4113 ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 4114 ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4115 mxDestroyArray(prhs[0]); 4116 mxDestroyArray(prhs[1]); 4117 mxDestroyArray(prhs[2]); 4118 mxDestroyArray(prhs[3]); 4119 mxDestroyArray(prhs[4]); 4120 mxDestroyArray(plhs[0]); 4121 PetscFunctionReturn(0); 4122 } 4123 4124 4125 #undef __FUNCT__ 4126 #define __FUNCT__ "TSMonitorSetMatlab" 4127 /* 4128 TSMonitorSetMatlab - Sets the monitor function from Matlab 4129 4130 Level: developer 4131 4132 .keywords: TS, nonlinear, set, function 4133 4134 .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4135 */ 4136 PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 4137 { 4138 PetscErrorCode ierr; 4139 TSMatlabContext *sctx; 4140 4141 PetscFunctionBegin; 4142 /* currently sctx is memory bleed */ 4143 ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4144 ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4145 /* 4146 This should work, but it doesn't 4147 sctx->ctx = ctx; 4148 mexMakeArrayPersistent(sctx->ctx); 4149 */ 4150 sctx->ctx = mxDuplicateArray(ctx); 4151 ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 4152 PetscFunctionReturn(0); 4153 } 4154 #endif 4155 4156 4157 4158 #undef __FUNCT__ 4159 #define __FUNCT__ "TSMonitorLGSolution" 4160 /*@C 4161 TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 4162 in a time based line graph 4163 4164 Collective on TS 4165 4166 Input Parameters: 4167 + ts - the TS context 4168 . step - current time-step 4169 . ptime - current time 4170 - lg - a line graph object 4171 4172 Level: intermediate 4173 4174 Notes: each process in a parallel run displays its component solutions in a separate window 4175 4176 .keywords: TS, vector, monitor, view 4177 4178 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4179 @*/ 4180 PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4181 { 4182 PetscErrorCode ierr; 4183 TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4184 const PetscScalar *yy; 4185 PetscInt dim; 4186 4187 PetscFunctionBegin; 4188 if (!step) { 4189 PetscDrawAxis axis; 4190 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4191 ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 4192 ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 4193 ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 4194 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4195 } 4196 ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr); 4197 #if defined(PETSC_USE_COMPLEX) 4198 { 4199 PetscReal *yreal; 4200 PetscInt i,n; 4201 ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr); 4202 ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4203 for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 4204 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4205 ierr = PetscFree(yreal);CHKERRQ(ierr); 4206 } 4207 #else 4208 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4209 #endif 4210 ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr); 4211 if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){ 4212 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4213 } 4214 PetscFunctionReturn(0); 4215 } 4216 4217 #undef __FUNCT__ 4218 #define __FUNCT__ "TSMonitorLGError" 4219 /*@C 4220 TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 4221 in a time based line graph 4222 4223 Collective on TS 4224 4225 Input Parameters: 4226 + ts - the TS context 4227 . step - current time-step 4228 . ptime - current time 4229 - lg - a line graph object 4230 4231 Level: intermediate 4232 4233 Notes: 4234 Only for sequential solves. 4235 4236 The user must provide the solution using TSSetSolutionFunction() to use this monitor. 4237 4238 Options Database Keys: 4239 . -ts_monitor_lg_error - create a graphical monitor of error history 4240 4241 .keywords: TS, vector, monitor, view 4242 4243 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 4244 @*/ 4245 PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4246 { 4247 PetscErrorCode ierr; 4248 TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4249 const PetscScalar *yy; 4250 Vec y; 4251 PetscInt dim; 4252 4253 PetscFunctionBegin; 4254 if (!step) { 4255 PetscDrawAxis axis; 4256 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4257 ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 4258 ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 4259 ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 4260 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4261 } 4262 ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 4263 ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 4264 ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 4265 ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 4266 #if defined(PETSC_USE_COMPLEX) 4267 { 4268 PetscReal *yreal; 4269 PetscInt i,n; 4270 ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 4271 ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4272 for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 4273 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4274 ierr = PetscFree(yreal);CHKERRQ(ierr); 4275 } 4276 #else 4277 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4278 #endif 4279 ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 4280 ierr = VecDestroy(&y);CHKERRQ(ierr); 4281 if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){ 4282 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4283 } 4284 PetscFunctionReturn(0); 4285 } 4286 4287 #undef __FUNCT__ 4288 #define __FUNCT__ "TSMonitorLGSNESIterations" 4289 PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4290 { 4291 TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4292 PetscReal x = ptime,y; 4293 PetscErrorCode ierr; 4294 PetscInt its; 4295 4296 PetscFunctionBegin; 4297 if (!n) { 4298 PetscDrawAxis axis; 4299 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4300 ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 4301 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4302 ctx->snes_its = 0; 4303 } 4304 ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 4305 y = its - ctx->snes_its; 4306 ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4307 if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){ 4308 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4309 } 4310 ctx->snes_its = its; 4311 PetscFunctionReturn(0); 4312 } 4313 4314 #undef __FUNCT__ 4315 #define __FUNCT__ "TSMonitorLGKSPIterations" 4316 PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4317 { 4318 TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4319 PetscReal x = ptime,y; 4320 PetscErrorCode ierr; 4321 PetscInt its; 4322 4323 PetscFunctionBegin; 4324 if (!n) { 4325 PetscDrawAxis axis; 4326 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4327 ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 4328 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4329 ctx->ksp_its = 0; 4330 } 4331 ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 4332 y = its - ctx->ksp_its; 4333 ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4334 if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){ 4335 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4336 } 4337 ctx->ksp_its = its; 4338 PetscFunctionReturn(0); 4339 } 4340 4341 #undef __FUNCT__ 4342 #define __FUNCT__ "TSComputeLinearStability" 4343 /*@ 4344 TSComputeLinearStability - computes the linear stability function at a point 4345 4346 Collective on TS and Vec 4347 4348 Input Parameters: 4349 + ts - the TS context 4350 - xr,xi - real and imaginary part of input arguments 4351 4352 Output Parameters: 4353 . yr,yi - real and imaginary part of function value 4354 4355 Level: developer 4356 4357 .keywords: TS, compute 4358 4359 .seealso: TSSetRHSFunction(), TSComputeIFunction() 4360 @*/ 4361 PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 4362 { 4363 PetscErrorCode ierr; 4364 4365 PetscFunctionBegin; 4366 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4367 if (!ts->ops->linearstability) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 4368 ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 4369 PetscFunctionReturn(0); 4370 } 4371