163dd3a1aSKris Buschelman 2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h> /*I "petscts.h" I*/ 3496e6a7aSJed Brown #include <petscdmshell.h> 41e25c274SJed Brown #include <petscdmda.h> 52d5ee99bSBarry Smith #include <petscviewer.h> 62d5ee99bSBarry Smith #include <petscdraw.h> 7d763cef2SBarry Smith 8d5ba7fb7SMatthew Knepley /* Logging support */ 9d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 10166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 11d405a339SMatthew Knepley 1249354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1349354f04SShri Abhyankar 144a2ae208SSatish Balay #undef __FUNCT__ 15e55864a3SBarry Smith #define __FUNCT__ "TSSetTypeFromOptions_Private" 16bdad233fSMatthew Knepley /* 17bdad233fSMatthew Knepley TSSetTypeFromOptions - Sets the type of ts from user options. 18bdad233fSMatthew Knepley 19bdad233fSMatthew Knepley Collective on TS 20bdad233fSMatthew Knepley 21bdad233fSMatthew Knepley Input Parameter: 22bdad233fSMatthew Knepley . ts - The ts 23bdad233fSMatthew Knepley 24bdad233fSMatthew Knepley Level: intermediate 25bdad233fSMatthew Knepley 26bdad233fSMatthew Knepley .keywords: TS, set, options, database, type 27bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType() 28bdad233fSMatthew Knepley */ 298c34d3f5SBarry Smith static PetscErrorCode TSSetTypeFromOptions_Private(PetscOptions *PetscOptionsObject,TS ts) 30bdad233fSMatthew Knepley { 31ace3abfcSBarry Smith PetscBool opt; 322fc52814SBarry Smith const char *defaultType; 33bdad233fSMatthew Knepley char typeName[256]; 34dfbe8321SBarry Smith PetscErrorCode ierr; 35bdad233fSMatthew Knepley 36bdad233fSMatthew Knepley PetscFunctionBegin; 37bbd56ea5SKarl Rupp if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 38bbd56ea5SKarl Rupp else defaultType = TSEULER; 39bdad233fSMatthew Knepley 40607a6623SBarry Smith if (!TSRegisterAllCalled) {ierr = TSRegisterAll();CHKERRQ(ierr);} 41a264d7a6SBarry Smith ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 42a7cc72afSBarry Smith if (opt) { 43bdad233fSMatthew Knepley ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 44bdad233fSMatthew Knepley } else { 45bdad233fSMatthew Knepley ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 46bdad233fSMatthew Knepley } 47bdad233fSMatthew Knepley PetscFunctionReturn(0); 48bdad233fSMatthew Knepley } 49bdad233fSMatthew Knepley 502d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx { 512d5ee99bSBarry Smith PetscViewer viewer; 524b363babSBarry Smith PetscDrawAxis axis; 532d5ee99bSBarry Smith Vec initialsolution; 542d5ee99bSBarry Smith PetscBool showinitial; 552d5ee99bSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 562d5ee99bSBarry Smith PetscBool showtimestepandtime; 572d5ee99bSBarry Smith int color; 582d5ee99bSBarry Smith }; 592d5ee99bSBarry Smith 60bdad233fSMatthew Knepley #undef __FUNCT__ 61bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions" 62bdad233fSMatthew Knepley /*@ 63bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 64bdad233fSMatthew Knepley 65bdad233fSMatthew Knepley Collective on TS 66bdad233fSMatthew Knepley 67bdad233fSMatthew Knepley Input Parameter: 68bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 69bdad233fSMatthew Knepley 70bdad233fSMatthew Knepley Options Database Keys: 714d91e141SJed Brown + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP 72bdad233fSMatthew Knepley . -ts_max_steps maxsteps - maximum number of time-steps to take 733bca7d26SBarry Smith . -ts_final_time time - maximum time to compute to 74bdad233fSMatthew Knepley . -ts_dt dt - initial time step 75a3cdaa26SBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> whether to stop at the exact given final time and how to compute the solution at that ti,e 76a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 77a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 78a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 79a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 80a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 81bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 82de06c3feSJed Brown . -ts_monitor_lg_timestep - Monitor timestep size graphically 83de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 84de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 85de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 86de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 87de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 88de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 892d5ee99bSBarry Smith . -ts_monitor_draw_solution_phase - Monitor solution graphically with phase diagram 90de06c3feSJed Brown . -ts_monitor_draw_error - Monitor error graphically 9191b97e58SBarry Smith . -ts_monitor_solution_binary <filename> - Save each solution to a binary file 9291b97e58SBarry Smith - -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 9391b97e58SBarry Smith 9491b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 95bdad233fSMatthew Knepley 96bdad233fSMatthew Knepley Level: beginner 97bdad233fSMatthew Knepley 98bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 99bdad233fSMatthew Knepley 100a313700dSBarry Smith .seealso: TSGetType() 101bdad233fSMatthew Knepley @*/ 1027087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 103bdad233fSMatthew Knepley { 104ace3abfcSBarry Smith PetscBool opt,flg; 105dfbe8321SBarry Smith PetscErrorCode ierr; 106649052a6SBarry Smith PetscViewer monviewer; 107eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 108089b2837SJed Brown SNES snes; 1091c3436cfSJed Brown TSAdapt adapt; 11031748224SBarry Smith PetscReal time_step; 11149354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 112d1212d36SBarry Smith char dir[16]; 113bdad233fSMatthew Knepley 114bdad233fSMatthew Knepley PetscFunctionBegin; 1150700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1163194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 117a43b19c4SJed Brown /* Handle TS type options */ 118e55864a3SBarry Smith ierr = TSSetTypeFromOptions_Private(PetscOptionsObject,ts);CHKERRQ(ierr); 119bdad233fSMatthew Knepley 120bdad233fSMatthew Knepley /* Handle generic TS options */ 1210298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1220298fd71SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 1230298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 12431748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 12531748224SBarry Smith if (flg) { 12631748224SBarry Smith ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 12731748224SBarry Smith } 12849354f04SShri Abhyankar ierr = PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);CHKERRQ(ierr); 12949354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 1300298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL);CHKERRQ(ierr); 1310298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 1320298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 1330298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 1340298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 135bdad233fSMatthew Knepley 13656f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 13756f85f32SBarry Smith { 13856f85f32SBarry Smith PetscBool set; 13956f85f32SBarry Smith flg = PETSC_FALSE; 14056f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 14156f85f32SBarry Smith if (set) { 14256f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 14356f85f32SBarry Smith } 14456f85f32SBarry Smith } 14556f85f32SBarry Smith #endif 14656f85f32SBarry Smith 147bdad233fSMatthew Knepley /* Monitor options */ 148a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 149eabae89aSBarry Smith if (flg) { 150ce94432eSBarry Smith ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr); 151649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 152bdad233fSMatthew Knepley } 1535180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1545180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1555180491cSLisandro Dalcin 1564f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 157a7cc72afSBarry Smith if (opt) { 1580b039ecaSBarry Smith TSMonitorLGCtx ctx; 1593923b477SBarry Smith PetscInt howoften = 1; 160a80ad3e0SBarry Smith 1610298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 162ce94432eSBarry Smith ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 1634f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 164b3603a34SBarry Smith } 1654f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 166b3603a34SBarry Smith if (opt) { 1670b039ecaSBarry Smith TSMonitorLGCtx ctx; 1683923b477SBarry Smith PetscInt howoften = 1; 169b3603a34SBarry Smith 1700298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 17122d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1724f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 173bdad233fSMatthew Knepley } 1744f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 175ef20d060SBarry Smith if (opt) { 1760b039ecaSBarry Smith TSMonitorLGCtx ctx; 1773923b477SBarry Smith PetscInt howoften = 1; 178ef20d060SBarry Smith 1790298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 18022d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1814f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 182ef20d060SBarry Smith } 183201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 184201da799SBarry Smith if (opt) { 185201da799SBarry Smith TSMonitorLGCtx ctx; 186201da799SBarry Smith PetscInt howoften = 1; 187201da799SBarry Smith 1880298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 18922d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 190201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 191201da799SBarry Smith } 192201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 193201da799SBarry Smith if (opt) { 194201da799SBarry Smith TSMonitorLGCtx ctx; 195201da799SBarry Smith PetscInt howoften = 1; 196201da799SBarry Smith 1970298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 19822d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 199201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 200201da799SBarry Smith } 2018189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 2028189c53fSBarry Smith if (opt) { 2038189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2048189c53fSBarry Smith PetscInt howoften = 1; 2058189c53fSBarry Smith 2060298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 20722d28d08SBarry Smith ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 2088189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 2098189c53fSBarry Smith } 210ef20d060SBarry Smith opt = PETSC_FALSE; 2110dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 212a7cc72afSBarry Smith if (opt) { 21383a4ac43SBarry Smith TSMonitorDrawCtx ctx; 21483a4ac43SBarry Smith PetscInt howoften = 1; 215a80ad3e0SBarry Smith 2160298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 217ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 21883a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 219bdad233fSMatthew Knepley } 220fb1732b5SBarry Smith opt = PETSC_FALSE; 2212d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 2222d5ee99bSBarry Smith if (opt) { 2232d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2242d5ee99bSBarry Smith PetscReal bounds[4]; 2252d5ee99bSBarry Smith PetscInt n = 4; 2262d5ee99bSBarry Smith PetscDraw draw; 2272d5ee99bSBarry Smith 2282d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 2292d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2302d5ee99bSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr); 2312d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 2322d5ee99bSBarry Smith ierr = PetscDrawClear(draw);CHKERRQ(ierr); 2334b363babSBarry Smith ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr); 2344b363babSBarry Smith ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 235f54adfc0SBarry Smith ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 2364b363babSBarry Smith ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr); 23707995780SPeter Brune /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */ 2382d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2392d5ee99bSBarry Smith } 2402d5ee99bSBarry Smith opt = PETSC_FALSE; 2410dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 2423a471f94SBarry Smith if (opt) { 24383a4ac43SBarry Smith TSMonitorDrawCtx ctx; 24483a4ac43SBarry Smith PetscInt howoften = 1; 2453a471f94SBarry Smith 2460298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 247ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 24883a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2493a471f94SBarry Smith } 2503a471f94SBarry Smith opt = PETSC_FALSE; 25191b97e58SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 252fb1732b5SBarry Smith if (flg) { 253fb1732b5SBarry Smith PetscViewer ctx; 254fb1732b5SBarry Smith if (monfilename[0]) { 255ce94432eSBarry Smith ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 256c2fbc07fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 257fb1732b5SBarry Smith } else { 258ce94432eSBarry Smith ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts)); 2590298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr); 260fb1732b5SBarry Smith } 261fb1732b5SBarry Smith } 262ed81e22dSJed Brown opt = PETSC_FALSE; 26391b97e58SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 264ed81e22dSJed Brown if (flg) { 265ed81e22dSJed Brown const char *ptr,*ptr2; 266ed81e22dSJed Brown char *filetemplate; 267ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 268ed81e22dSJed Brown /* Do some cursory validation of the input. */ 269ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 270ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 271ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 272ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 273ce94432eSBarry Smith if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts"); 274ed81e22dSJed Brown if (ptr2) break; 275ed81e22dSJed Brown } 276ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 277ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 278ed81e22dSJed Brown } 279bdad233fSMatthew Knepley 280d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 281d1212d36SBarry Smith if (flg) { 282d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 283d1212d36SBarry Smith int ray = 0; 284d1212d36SBarry Smith DMDADirection ddir; 285d1212d36SBarry Smith DM da; 286d1212d36SBarry Smith PetscMPIInt rank; 287d1212d36SBarry Smith 288ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 289d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 290d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 291ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 292d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 293d1212d36SBarry Smith 294d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 295b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 296d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 297d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 298ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 299d1212d36SBarry Smith if (!rank) { 300d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 301d1212d36SBarry Smith } 30251b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 303d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 304d1212d36SBarry Smith } 30551b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 30651b4a12fSMatthew G. Knepley if (flg) { 30751b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 30851b4a12fSMatthew G. Knepley int ray = 0; 30951b4a12fSMatthew G. Knepley DMDADirection ddir; 31051b4a12fSMatthew G. Knepley DM da; 31151b4a12fSMatthew G. Knepley PetscInt howoften = 1; 312d1212d36SBarry Smith 31351b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 31451b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 31551b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 31651b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 31751b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 31851b4a12fSMatthew G. Knepley 31951b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 320b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 32151b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 32251b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 32351b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 32451b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 32551b4a12fSMatthew G. Knepley } 326a7a1495cSBarry Smith 3277781c08eSBarry Smith /* 3287781c08eSBarry Smith This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui 3297781c08eSBarry Smith will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin() 3307781c08eSBarry Smith */ 331552698daSJed Brown ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr); 332e55864a3SBarry Smith ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr); 333d763cef2SBarry Smith 334d763cef2SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 335d763cef2SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 336d763cef2SBarry Smith 337d763cef2SBarry Smith /* Handle specific TS options */ 338d763cef2SBarry Smith if (ts->ops->setfromoptions) { 339e55864a3SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 340d763cef2SBarry Smith } 341d763cef2SBarry Smith 342d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 343d763cef2SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 344d763cef2SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 345d763cef2SBarry Smith PetscFunctionReturn(0); 346d763cef2SBarry Smith } 347d763cef2SBarry Smith 348d763cef2SBarry Smith #undef __FUNCT__ 349a7a1495cSBarry Smith #undef __FUNCT__ 350a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian" 351a7a1495cSBarry Smith /*@ 352a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 353a7a1495cSBarry Smith set with TSSetRHSJacobian(). 354a7a1495cSBarry Smith 355a7a1495cSBarry Smith Collective on TS and Vec 356a7a1495cSBarry Smith 357a7a1495cSBarry Smith Input Parameters: 358316643e7SJed Brown + ts - the TS context 359a7a1495cSBarry Smith . t - current timestep 3600910c330SBarry Smith - U - input vector 361a7a1495cSBarry Smith 362a7a1495cSBarry Smith Output Parameters: 363a7a1495cSBarry Smith + A - Jacobian matrix 364a7a1495cSBarry Smith . B - optional preconditioning matrix 365a7a1495cSBarry Smith - flag - flag indicating matrix structure 366a7a1495cSBarry Smith 367a7a1495cSBarry Smith Notes: 368a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 369a7a1495cSBarry Smith is used internally within the nonlinear solvers. 370a7a1495cSBarry Smith 37194b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 372a7a1495cSBarry Smith flag parameter. 373a7a1495cSBarry Smith 374a7a1495cSBarry Smith Level: developer 375a7a1495cSBarry Smith 376a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 377a7a1495cSBarry Smith 37894b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 379a7a1495cSBarry Smith @*/ 380d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 381a7a1495cSBarry Smith { 382dfbe8321SBarry Smith PetscErrorCode ierr; 383270bf2e7SJed Brown PetscObjectState Ustate; 38424989b8cSPeter Brune DM dm; 385942e3340SBarry Smith DMTS tsdm; 38624989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 38724989b8cSPeter Brune void *ctx; 38824989b8cSPeter Brune TSIJacobian ijacobianfunc; 389a7a1495cSBarry Smith 390a7a1495cSBarry Smith PetscFunctionBegin; 3910700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3920910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3930910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 39424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 395942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 39624989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 3970298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 39859e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 3990910c330SBarry Smith if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) { 4000e4ef248SJed Brown PetscFunctionReturn(0); 401f8ede8e7SJed Brown } 402d90be118SSean Farley 403ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 404d90be118SSean Farley 405e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 40694ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 40794ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 40894ab13aaSBarry Smith if (A != B) { 40994ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 41094ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 411e1244c69SJed Brown } 412e1244c69SJed Brown ts->rhsjacobian.shift = 0; 413e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 414e1244c69SJed Brown } 415e1244c69SJed Brown 41624989b8cSPeter Brune if (rhsjacobianfunc) { 41794ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 418a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 419d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 420a7a1495cSBarry Smith PetscStackPop; 42194ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 422a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 42394ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,4); 42494ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,5); 425ef66eb69SBarry Smith } else { 42694ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 42794ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 428ef66eb69SBarry Smith } 4290e4ef248SJed Brown ts->rhsjacobian.time = t; 4300910c330SBarry Smith ts->rhsjacobian.X = U; 43159e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 432a7a1495cSBarry Smith PetscFunctionReturn(0); 433a7a1495cSBarry Smith } 434a7a1495cSBarry Smith 4354a2ae208SSatish Balay #undef __FUNCT__ 4364a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 437316643e7SJed Brown /*@ 438d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 439d763cef2SBarry Smith 440316643e7SJed Brown Collective on TS and Vec 441316643e7SJed Brown 442316643e7SJed Brown Input Parameters: 443316643e7SJed Brown + ts - the TS context 444316643e7SJed Brown . t - current time 4450910c330SBarry Smith - U - state vector 446316643e7SJed Brown 447316643e7SJed Brown Output Parameter: 448316643e7SJed Brown . y - right hand side 449316643e7SJed Brown 450316643e7SJed Brown Note: 451316643e7SJed Brown Most users should not need to explicitly call this routine, as it 452316643e7SJed Brown is used internally within the nonlinear solvers. 453316643e7SJed Brown 454316643e7SJed Brown Level: developer 455316643e7SJed Brown 456316643e7SJed Brown .keywords: TS, compute 457316643e7SJed Brown 458316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 459316643e7SJed Brown @*/ 4600910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 461d763cef2SBarry Smith { 462dfbe8321SBarry Smith PetscErrorCode ierr; 46324989b8cSPeter Brune TSRHSFunction rhsfunction; 46424989b8cSPeter Brune TSIFunction ifunction; 46524989b8cSPeter Brune void *ctx; 46624989b8cSPeter Brune DM dm; 46724989b8cSPeter Brune 468d763cef2SBarry Smith PetscFunctionBegin; 4690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4700910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 4710700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 47224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 47324989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 4740298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 475d763cef2SBarry Smith 476ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 477d763cef2SBarry Smith 4780910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 47924989b8cSPeter Brune if (rhsfunction) { 480d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 4810910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 482d763cef2SBarry Smith PetscStackPop; 483214bc6a2SJed Brown } else { 484214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 485b2cd27e8SJed Brown } 48644a41b28SSean Farley 4870910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 488d763cef2SBarry Smith PetscFunctionReturn(0); 489d763cef2SBarry Smith } 490d763cef2SBarry Smith 4914a2ae208SSatish Balay #undef __FUNCT__ 492ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 493ef20d060SBarry Smith /*@ 494ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 495ef20d060SBarry Smith 496ef20d060SBarry Smith Collective on TS and Vec 497ef20d060SBarry Smith 498ef20d060SBarry Smith Input Parameters: 499ef20d060SBarry Smith + ts - the TS context 500ef20d060SBarry Smith - t - current time 501ef20d060SBarry Smith 502ef20d060SBarry Smith Output Parameter: 5030910c330SBarry Smith . U - the solution 504ef20d060SBarry Smith 505ef20d060SBarry Smith Note: 506ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 507ef20d060SBarry Smith is used internally within the nonlinear solvers. 508ef20d060SBarry Smith 509ef20d060SBarry Smith Level: developer 510ef20d060SBarry Smith 511ef20d060SBarry Smith .keywords: TS, compute 512ef20d060SBarry Smith 513abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 514ef20d060SBarry Smith @*/ 5150910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 516ef20d060SBarry Smith { 517ef20d060SBarry Smith PetscErrorCode ierr; 518ef20d060SBarry Smith TSSolutionFunction solutionfunction; 519ef20d060SBarry Smith void *ctx; 520ef20d060SBarry Smith DM dm; 521ef20d060SBarry Smith 522ef20d060SBarry Smith PetscFunctionBegin; 523ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5240910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 525ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 526ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 527ef20d060SBarry Smith 528ef20d060SBarry Smith if (solutionfunction) { 5299b7cd975SBarry Smith PetscStackPush("TS user solution function"); 5300910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 531ef20d060SBarry Smith PetscStackPop; 532ef20d060SBarry Smith } 533ef20d060SBarry Smith PetscFunctionReturn(0); 534ef20d060SBarry Smith } 5359b7cd975SBarry Smith #undef __FUNCT__ 5369b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction" 5379b7cd975SBarry Smith /*@ 5389b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 5399b7cd975SBarry Smith 5409b7cd975SBarry Smith Collective on TS and Vec 5419b7cd975SBarry Smith 5429b7cd975SBarry Smith Input Parameters: 5439b7cd975SBarry Smith + ts - the TS context 5449b7cd975SBarry Smith - t - current time 5459b7cd975SBarry Smith 5469b7cd975SBarry Smith Output Parameter: 5479b7cd975SBarry Smith . U - the function value 5489b7cd975SBarry Smith 5499b7cd975SBarry Smith Note: 5509b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 5519b7cd975SBarry Smith is used internally within the nonlinear solvers. 5529b7cd975SBarry Smith 5539b7cd975SBarry Smith Level: developer 5549b7cd975SBarry Smith 5559b7cd975SBarry Smith .keywords: TS, compute 5569b7cd975SBarry Smith 5579b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 5589b7cd975SBarry Smith @*/ 5599b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 5609b7cd975SBarry Smith { 5619b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 5629b7cd975SBarry Smith void *ctx; 5639b7cd975SBarry Smith DM dm; 5649b7cd975SBarry Smith 5659b7cd975SBarry Smith PetscFunctionBegin; 5669b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5679b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5689b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 5699b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 5709b7cd975SBarry Smith 5719b7cd975SBarry Smith if (forcing) { 5729b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 5739b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 5749b7cd975SBarry Smith PetscStackPop; 5759b7cd975SBarry Smith } 5769b7cd975SBarry Smith PetscFunctionReturn(0); 5779b7cd975SBarry Smith } 578ef20d060SBarry Smith 579ef20d060SBarry Smith #undef __FUNCT__ 580214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 581214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 582214bc6a2SJed Brown { 5832dd45cf8SJed Brown Vec F; 584214bc6a2SJed Brown PetscErrorCode ierr; 585214bc6a2SJed Brown 586214bc6a2SJed Brown PetscFunctionBegin; 5870298fd71SBarry Smith *Frhs = NULL; 5880298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 589214bc6a2SJed Brown if (!ts->Frhs) { 5902dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 591214bc6a2SJed Brown } 592214bc6a2SJed Brown *Frhs = ts->Frhs; 593214bc6a2SJed Brown PetscFunctionReturn(0); 594214bc6a2SJed Brown } 595214bc6a2SJed Brown 596214bc6a2SJed Brown #undef __FUNCT__ 597214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 598214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 599214bc6a2SJed Brown { 600214bc6a2SJed Brown Mat A,B; 6012dd45cf8SJed Brown PetscErrorCode ierr; 602214bc6a2SJed Brown 603214bc6a2SJed Brown PetscFunctionBegin; 604c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 605c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 6060298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 607214bc6a2SJed Brown if (Arhs) { 608214bc6a2SJed Brown if (!ts->Arhs) { 609214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 610214bc6a2SJed Brown } 611214bc6a2SJed Brown *Arhs = ts->Arhs; 612214bc6a2SJed Brown } 613214bc6a2SJed Brown if (Brhs) { 614214bc6a2SJed Brown if (!ts->Brhs) { 615bdb70873SJed Brown if (A != B) { 616214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 617bdb70873SJed Brown } else { 618bdb70873SJed Brown ts->Brhs = ts->Arhs; 619bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 620bdb70873SJed Brown } 621214bc6a2SJed Brown } 622214bc6a2SJed Brown *Brhs = ts->Brhs; 623214bc6a2SJed Brown } 624214bc6a2SJed Brown PetscFunctionReturn(0); 625214bc6a2SJed Brown } 626214bc6a2SJed Brown 627214bc6a2SJed Brown #undef __FUNCT__ 628316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 629316643e7SJed Brown /*@ 6300910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 631316643e7SJed Brown 632316643e7SJed Brown Collective on TS and Vec 633316643e7SJed Brown 634316643e7SJed Brown Input Parameters: 635316643e7SJed Brown + ts - the TS context 636316643e7SJed Brown . t - current time 6370910c330SBarry Smith . U - state vector 6380910c330SBarry Smith . Udot - time derivative of state vector 639214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 640316643e7SJed Brown 641316643e7SJed Brown Output Parameter: 642316643e7SJed Brown . Y - right hand side 643316643e7SJed Brown 644316643e7SJed Brown Note: 645316643e7SJed Brown Most users should not need to explicitly call this routine, as it 646316643e7SJed Brown is used internally within the nonlinear solvers. 647316643e7SJed Brown 648316643e7SJed Brown If the user did did not write their equations in implicit form, this 649316643e7SJed Brown function recasts them in implicit form. 650316643e7SJed Brown 651316643e7SJed Brown Level: developer 652316643e7SJed Brown 653316643e7SJed Brown .keywords: TS, compute 654316643e7SJed Brown 655316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 656316643e7SJed Brown @*/ 6570910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 658316643e7SJed Brown { 659316643e7SJed Brown PetscErrorCode ierr; 66024989b8cSPeter Brune TSIFunction ifunction; 66124989b8cSPeter Brune TSRHSFunction rhsfunction; 66224989b8cSPeter Brune void *ctx; 66324989b8cSPeter Brune DM dm; 664316643e7SJed Brown 665316643e7SJed Brown PetscFunctionBegin; 6660700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6670910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6680910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 6690700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 670316643e7SJed Brown 67124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 67224989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 6730298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 67424989b8cSPeter Brune 675ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 676d90be118SSean Farley 6770910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 67824989b8cSPeter Brune if (ifunction) { 679316643e7SJed Brown PetscStackPush("TS user implicit function"); 6800910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 681316643e7SJed Brown PetscStackPop; 682214bc6a2SJed Brown } 683214bc6a2SJed Brown if (imex) { 68424989b8cSPeter Brune if (!ifunction) { 6850910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 6862dd45cf8SJed Brown } 68724989b8cSPeter Brune } else if (rhsfunction) { 68824989b8cSPeter Brune if (ifunction) { 689214bc6a2SJed Brown Vec Frhs; 690214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 6910910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 692214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 6932dd45cf8SJed Brown } else { 6940910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 6950910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 696316643e7SJed Brown } 6974a6899ffSJed Brown } 6980910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 699316643e7SJed Brown PetscFunctionReturn(0); 700316643e7SJed Brown } 701316643e7SJed Brown 702316643e7SJed Brown #undef __FUNCT__ 703316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 704316643e7SJed Brown /*@ 705316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 706316643e7SJed Brown 707316643e7SJed Brown Collective on TS and Vec 708316643e7SJed Brown 709316643e7SJed Brown Input 710316643e7SJed Brown Input Parameters: 711316643e7SJed Brown + ts - the TS context 712316643e7SJed Brown . t - current timestep 7130910c330SBarry Smith . U - state vector 7140910c330SBarry Smith . Udot - time derivative of state vector 715214bc6a2SJed Brown . shift - shift to apply, see note below 716214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 717316643e7SJed Brown 718316643e7SJed Brown Output Parameters: 719316643e7SJed Brown + A - Jacobian matrix 720316643e7SJed Brown . B - optional preconditioning matrix 721316643e7SJed Brown - flag - flag indicating matrix structure 722316643e7SJed Brown 723316643e7SJed Brown Notes: 7240910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 725316643e7SJed Brown 7260910c330SBarry Smith dF/dU + shift*dF/dUdot 727316643e7SJed Brown 728316643e7SJed Brown Most users should not need to explicitly call this routine, as it 729316643e7SJed Brown is used internally within the nonlinear solvers. 730316643e7SJed Brown 731316643e7SJed Brown Level: developer 732316643e7SJed Brown 733316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 734316643e7SJed Brown 735316643e7SJed Brown .seealso: TSSetIJacobian() 73663495f91SJed Brown @*/ 737d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 738316643e7SJed Brown { 739316643e7SJed Brown PetscErrorCode ierr; 74024989b8cSPeter Brune TSIJacobian ijacobian; 74124989b8cSPeter Brune TSRHSJacobian rhsjacobian; 74224989b8cSPeter Brune DM dm; 74324989b8cSPeter Brune void *ctx; 744316643e7SJed Brown 745316643e7SJed Brown PetscFunctionBegin; 7460700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7470910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7480910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 749316643e7SJed Brown PetscValidPointer(A,6); 75094ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 751316643e7SJed Brown PetscValidPointer(B,7); 75294ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 75324989b8cSPeter Brune 75424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 75524989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 7560298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 75724989b8cSPeter Brune 758ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 759316643e7SJed Brown 76094ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 76124989b8cSPeter Brune if (ijacobian) { 762316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 763d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 764316643e7SJed Brown PetscStackPop; 765214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 76694ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,4); 76794ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,5); 7684a6899ffSJed Brown } 769214bc6a2SJed Brown if (imex) { 770b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 77194ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 77294ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 77394ab13aaSBarry Smith if (A != B) { 77494ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 77594ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 776214bc6a2SJed Brown } 777214bc6a2SJed Brown } 778214bc6a2SJed Brown } else { 779e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 780e1244c69SJed Brown if (rhsjacobian) { 781bd373395SBarry Smith if (ijacobian) { 782e1244c69SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 783bd373395SBarry Smith } else { 784bd373395SBarry Smith ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr); 785e1244c69SJed Brown } 786d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 787e1244c69SJed Brown } 78894ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 789e1244c69SJed Brown ts->rhsjacobian.scale = -1; 790e1244c69SJed Brown ts->rhsjacobian.shift = shift; 79194ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 79294ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 79394ab13aaSBarry Smith if (A != B) { 79494ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 79594ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 796316643e7SJed Brown } 797e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 798e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 799e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 80094ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 80194ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 80294ab13aaSBarry Smith if (A != B) { 80394ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 80494ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 805e1244c69SJed Brown } 806e1244c69SJed Brown } 80794ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 80894ab13aaSBarry Smith if (A != B) { 80994ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 810214bc6a2SJed Brown } 811316643e7SJed Brown } 812316643e7SJed Brown } 81394ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 814316643e7SJed Brown PetscFunctionReturn(0); 815316643e7SJed Brown } 816316643e7SJed Brown 817316643e7SJed Brown #undef __FUNCT__ 8184a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 819d763cef2SBarry Smith /*@C 820d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 821b5abc632SBarry Smith where U_t = G(t,u). 822d763cef2SBarry Smith 8233f9fe445SBarry Smith Logically Collective on TS 824d763cef2SBarry Smith 825d763cef2SBarry Smith Input Parameters: 826d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 8270298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 828d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 829d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 8300298fd71SBarry Smith function evaluation routine (may be NULL) 831d763cef2SBarry Smith 832d763cef2SBarry Smith Calling sequence of func: 83387828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 834d763cef2SBarry Smith 835d763cef2SBarry Smith + t - current timestep 836d763cef2SBarry Smith . u - input vector 837d763cef2SBarry Smith . F - function vector 838d763cef2SBarry Smith - ctx - [optional] user-defined function context 839d763cef2SBarry Smith 840d763cef2SBarry Smith Level: beginner 841d763cef2SBarry Smith 842d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 843d763cef2SBarry Smith 844ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 845d763cef2SBarry Smith @*/ 846089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 847d763cef2SBarry Smith { 848089b2837SJed Brown PetscErrorCode ierr; 849089b2837SJed Brown SNES snes; 8500298fd71SBarry Smith Vec ralloc = NULL; 85124989b8cSPeter Brune DM dm; 852d763cef2SBarry Smith 853089b2837SJed Brown PetscFunctionBegin; 8540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 855ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 85624989b8cSPeter Brune 85724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 85824989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 859089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 860e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 861e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 862e856ceecSJed Brown r = ralloc; 863e856ceecSJed Brown } 864089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 865e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 866d763cef2SBarry Smith PetscFunctionReturn(0); 867d763cef2SBarry Smith } 868d763cef2SBarry Smith 8694a2ae208SSatish Balay #undef __FUNCT__ 870ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 871ef20d060SBarry Smith /*@C 872abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 873ef20d060SBarry Smith 874ef20d060SBarry Smith Logically Collective on TS 875ef20d060SBarry Smith 876ef20d060SBarry Smith Input Parameters: 877ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 878ef20d060SBarry Smith . f - routine for evaluating the solution 879ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 8800298fd71SBarry Smith function evaluation routine (may be NULL) 881ef20d060SBarry Smith 882ef20d060SBarry Smith Calling sequence of func: 883ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 884ef20d060SBarry Smith 885ef20d060SBarry Smith + t - current timestep 886ef20d060SBarry Smith . u - output vector 887ef20d060SBarry Smith - ctx - [optional] user-defined function context 888ef20d060SBarry Smith 889abd5a294SJed Brown Notes: 890abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 891abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 892abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 893abd5a294SJed Brown 8944f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 895abd5a294SJed Brown 896ef20d060SBarry Smith Level: beginner 897ef20d060SBarry Smith 898ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 899ef20d060SBarry Smith 9009b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 901ef20d060SBarry Smith @*/ 902ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 903ef20d060SBarry Smith { 904ef20d060SBarry Smith PetscErrorCode ierr; 905ef20d060SBarry Smith DM dm; 906ef20d060SBarry Smith 907ef20d060SBarry Smith PetscFunctionBegin; 908ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 909ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 910ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 911ef20d060SBarry Smith PetscFunctionReturn(0); 912ef20d060SBarry Smith } 913ef20d060SBarry Smith 914ef20d060SBarry Smith #undef __FUNCT__ 9159b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction" 9169b7cd975SBarry Smith /*@C 9179b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 9189b7cd975SBarry Smith 9199b7cd975SBarry Smith Logically Collective on TS 9209b7cd975SBarry Smith 9219b7cd975SBarry Smith Input Parameters: 9229b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 9239b7cd975SBarry Smith . f - routine for evaluating the forcing function 9249b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 9250298fd71SBarry Smith function evaluation routine (may be NULL) 9269b7cd975SBarry Smith 9279b7cd975SBarry Smith Calling sequence of func: 9289b7cd975SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 9299b7cd975SBarry Smith 9309b7cd975SBarry Smith + t - current timestep 9319b7cd975SBarry Smith . u - output vector 9329b7cd975SBarry Smith - ctx - [optional] user-defined function context 9339b7cd975SBarry Smith 9349b7cd975SBarry Smith Notes: 9359b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 9369b7cd975SBarry Smith create closed-form solutions with a non-physical forcing term. 9379b7cd975SBarry Smith 9389b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 9399b7cd975SBarry Smith 9409b7cd975SBarry Smith Level: beginner 9419b7cd975SBarry Smith 9429b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 9439b7cd975SBarry Smith 9449b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 9459b7cd975SBarry Smith @*/ 9469b7cd975SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 9479b7cd975SBarry Smith { 9489b7cd975SBarry Smith PetscErrorCode ierr; 9499b7cd975SBarry Smith DM dm; 9509b7cd975SBarry Smith 9519b7cd975SBarry Smith PetscFunctionBegin; 9529b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9539b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 9549b7cd975SBarry Smith ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr); 9559b7cd975SBarry Smith PetscFunctionReturn(0); 9569b7cd975SBarry Smith } 9579b7cd975SBarry Smith 9589b7cd975SBarry Smith #undef __FUNCT__ 9594a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 960d763cef2SBarry Smith /*@C 961d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 962b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 963d763cef2SBarry Smith 9643f9fe445SBarry Smith Logically Collective on TS 965d763cef2SBarry Smith 966d763cef2SBarry Smith Input Parameters: 967d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 968e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 969e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 970d763cef2SBarry Smith . f - the Jacobian evaluation routine 971d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 9720298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 973d763cef2SBarry Smith 974d763cef2SBarry Smith Calling sequence of func: 975ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 976d763cef2SBarry Smith 977d763cef2SBarry Smith + t - current timestep 978d763cef2SBarry Smith . u - input vector 979e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 980e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 981d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 982d763cef2SBarry Smith 983d763cef2SBarry Smith 984d763cef2SBarry Smith Level: beginner 985d763cef2SBarry Smith 986d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 987d763cef2SBarry Smith 988ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 989d763cef2SBarry Smith 990d763cef2SBarry Smith @*/ 991e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 992d763cef2SBarry Smith { 993277b19d0SLisandro Dalcin PetscErrorCode ierr; 994089b2837SJed Brown SNES snes; 99524989b8cSPeter Brune DM dm; 99624989b8cSPeter Brune TSIJacobian ijacobian; 997277b19d0SLisandro Dalcin 998d763cef2SBarry Smith PetscFunctionBegin; 9990700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1000e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1001e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1002e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1003e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1004d763cef2SBarry Smith 100524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 100624989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1007e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1008e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1009e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1010e1244c69SJed Brown } 10110298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1012089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 10135f659677SPeter Brune if (!ijacobian) { 1014e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 10150e4ef248SJed Brown } 1016e5d3d808SBarry Smith if (Amat) { 1017e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 10180e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1019bbd56ea5SKarl Rupp 1020e5d3d808SBarry Smith ts->Arhs = Amat; 10210e4ef248SJed Brown } 1022e5d3d808SBarry Smith if (Pmat) { 1023e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 10240e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1025bbd56ea5SKarl Rupp 1026e5d3d808SBarry Smith ts->Brhs = Pmat; 10270e4ef248SJed Brown } 1028d763cef2SBarry Smith PetscFunctionReturn(0); 1029d763cef2SBarry Smith } 1030d763cef2SBarry Smith 1031316643e7SJed Brown 1032316643e7SJed Brown #undef __FUNCT__ 1033316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 1034316643e7SJed Brown /*@C 1035b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1036316643e7SJed Brown 10373f9fe445SBarry Smith Logically Collective on TS 1038316643e7SJed Brown 1039316643e7SJed Brown Input Parameters: 1040316643e7SJed Brown + ts - the TS context obtained from TSCreate() 10410298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1042316643e7SJed Brown . f - the function evaluation routine 10430298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1044316643e7SJed Brown 1045316643e7SJed Brown Calling sequence of f: 1046316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1047316643e7SJed Brown 1048316643e7SJed Brown + t - time at step/stage being solved 1049316643e7SJed Brown . u - state vector 1050316643e7SJed Brown . u_t - time derivative of state vector 1051316643e7SJed Brown . F - function vector 1052316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1053316643e7SJed Brown 1054316643e7SJed Brown Important: 1055d6cbdb99SBarry Smith The user MUST call either this routine, TSSetRHSFunction(). This routine must be used when not solving an ODE, for example a DAE. 1056316643e7SJed Brown 1057316643e7SJed Brown Level: beginner 1058316643e7SJed Brown 1059316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1060316643e7SJed Brown 1061d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1062316643e7SJed Brown @*/ 1063089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 1064316643e7SJed Brown { 1065089b2837SJed Brown PetscErrorCode ierr; 1066089b2837SJed Brown SNES snes; 10670298fd71SBarry Smith Vec resalloc = NULL; 106824989b8cSPeter Brune DM dm; 1069316643e7SJed Brown 1070316643e7SJed Brown PetscFunctionBegin; 10710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1072ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 107324989b8cSPeter Brune 107424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 107524989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 107624989b8cSPeter Brune 1077089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1078e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 1079e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 1080e856ceecSJed Brown res = resalloc; 1081e856ceecSJed Brown } 1082089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 1083e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 1084089b2837SJed Brown PetscFunctionReturn(0); 1085089b2837SJed Brown } 1086089b2837SJed Brown 1087089b2837SJed Brown #undef __FUNCT__ 1088089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 1089089b2837SJed Brown /*@C 1090089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1091089b2837SJed Brown 1092089b2837SJed Brown Not Collective 1093089b2837SJed Brown 1094089b2837SJed Brown Input Parameter: 1095089b2837SJed Brown . ts - the TS context 1096089b2837SJed Brown 1097089b2837SJed Brown Output Parameter: 10980298fd71SBarry Smith + r - vector to hold residual (or NULL) 10990298fd71SBarry Smith . func - the function to compute residual (or NULL) 11000298fd71SBarry Smith - ctx - the function context (or NULL) 1101089b2837SJed Brown 1102089b2837SJed Brown Level: advanced 1103089b2837SJed Brown 1104089b2837SJed Brown .keywords: TS, nonlinear, get, function 1105089b2837SJed Brown 1106089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1107089b2837SJed Brown @*/ 1108089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1109089b2837SJed Brown { 1110089b2837SJed Brown PetscErrorCode ierr; 1111089b2837SJed Brown SNES snes; 111224989b8cSPeter Brune DM dm; 1113089b2837SJed Brown 1114089b2837SJed Brown PetscFunctionBegin; 1115089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1116089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11170298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 111824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 111924989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1120089b2837SJed Brown PetscFunctionReturn(0); 1121089b2837SJed Brown } 1122089b2837SJed Brown 1123089b2837SJed Brown #undef __FUNCT__ 1124089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 1125089b2837SJed Brown /*@C 1126089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1127089b2837SJed Brown 1128089b2837SJed Brown Not Collective 1129089b2837SJed Brown 1130089b2837SJed Brown Input Parameter: 1131089b2837SJed Brown . ts - the TS context 1132089b2837SJed Brown 1133089b2837SJed Brown Output Parameter: 11340298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 11350298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 11360298fd71SBarry Smith - ctx - the function context (or NULL) 1137089b2837SJed Brown 1138089b2837SJed Brown Level: advanced 1139089b2837SJed Brown 1140089b2837SJed Brown .keywords: TS, nonlinear, get, function 1141089b2837SJed Brown 1142089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 1143089b2837SJed Brown @*/ 1144089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1145089b2837SJed Brown { 1146089b2837SJed Brown PetscErrorCode ierr; 1147089b2837SJed Brown SNES snes; 114824989b8cSPeter Brune DM dm; 1149089b2837SJed Brown 1150089b2837SJed Brown PetscFunctionBegin; 1151089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1152089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11530298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 115424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 115524989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1156316643e7SJed Brown PetscFunctionReturn(0); 1157316643e7SJed Brown } 1158316643e7SJed Brown 1159316643e7SJed Brown #undef __FUNCT__ 1160316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 1161316643e7SJed Brown /*@C 1162a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1163ae8867d6SBarry Smith provided with TSSetIFunction(). 1164316643e7SJed Brown 11653f9fe445SBarry Smith Logically Collective on TS 1166316643e7SJed Brown 1167316643e7SJed Brown Input Parameters: 1168316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1169e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1170e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1171316643e7SJed Brown . f - the Jacobian evaluation routine 11720298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1173316643e7SJed Brown 1174316643e7SJed Brown Calling sequence of f: 1175ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1176316643e7SJed Brown 1177316643e7SJed Brown + t - time at step/stage being solved 11781b4a444bSJed Brown . U - state vector 11791b4a444bSJed Brown . U_t - time derivative of state vector 1180316643e7SJed Brown . a - shift 1181e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1182e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1183316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1184316643e7SJed Brown 1185316643e7SJed Brown Notes: 1186e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1187316643e7SJed Brown 1188a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1189b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1190a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1191a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1192a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1193a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1194a4f0a591SBarry Smith 1195316643e7SJed Brown Level: beginner 1196316643e7SJed Brown 1197316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1198316643e7SJed Brown 1199ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1200316643e7SJed Brown 1201316643e7SJed Brown @*/ 1202e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1203316643e7SJed Brown { 1204316643e7SJed Brown PetscErrorCode ierr; 1205089b2837SJed Brown SNES snes; 120624989b8cSPeter Brune DM dm; 1207316643e7SJed Brown 1208316643e7SJed Brown PetscFunctionBegin; 12090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1210e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1211e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1212e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1213e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 121424989b8cSPeter Brune 121524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 121624989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 121724989b8cSPeter Brune 1218089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1219e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1220316643e7SJed Brown PetscFunctionReturn(0); 1221316643e7SJed Brown } 1222316643e7SJed Brown 12234a2ae208SSatish Balay #undef __FUNCT__ 1224e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse" 1225e1244c69SJed Brown /*@ 1226e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1227e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1228e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1229e1244c69SJed Brown not been changed by the TS. 1230e1244c69SJed Brown 1231e1244c69SJed Brown Logically Collective 1232e1244c69SJed Brown 1233e1244c69SJed Brown Input Arguments: 1234e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1235e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1236e1244c69SJed Brown 1237e1244c69SJed Brown Level: intermediate 1238e1244c69SJed Brown 1239e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1240e1244c69SJed Brown @*/ 1241e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1242e1244c69SJed Brown { 1243e1244c69SJed Brown PetscFunctionBegin; 1244e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1245e1244c69SJed Brown PetscFunctionReturn(0); 1246e1244c69SJed Brown } 1247e1244c69SJed Brown 1248e1244c69SJed Brown #undef __FUNCT__ 124955849f57SBarry Smith #define __FUNCT__ "TSLoad" 125055849f57SBarry Smith /*@C 125155849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 125255849f57SBarry Smith 125355849f57SBarry Smith Collective on PetscViewer 125455849f57SBarry Smith 125555849f57SBarry Smith Input Parameters: 125655849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 125755849f57SBarry Smith some related function before a call to TSLoad(). 125855849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 125955849f57SBarry Smith 126055849f57SBarry Smith Level: intermediate 126155849f57SBarry Smith 126255849f57SBarry Smith Notes: 126355849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 126455849f57SBarry Smith 126555849f57SBarry Smith Notes for advanced users: 126655849f57SBarry Smith Most users should not need to know the details of the binary storage 126755849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 126855849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 126955849f57SBarry Smith format is 127055849f57SBarry Smith .vb 127155849f57SBarry Smith has not yet been determined 127255849f57SBarry Smith .ve 127355849f57SBarry Smith 127455849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 127555849f57SBarry Smith @*/ 1276f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 127755849f57SBarry Smith { 127855849f57SBarry Smith PetscErrorCode ierr; 127955849f57SBarry Smith PetscBool isbinary; 128055849f57SBarry Smith PetscInt classid; 128155849f57SBarry Smith char type[256]; 12822d53ad75SBarry Smith DMTS sdm; 1283ad6bc421SBarry Smith DM dm; 128455849f57SBarry Smith 128555849f57SBarry Smith PetscFunctionBegin; 1286f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 128755849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 128855849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 128955849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 129055849f57SBarry Smith 129155849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 1292ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 129355849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 1294f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1295f2c2a1b9SBarry Smith if (ts->ops->load) { 1296f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1297f2c2a1b9SBarry Smith } 1298ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1299ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1300ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1301f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1302f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 13032d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 13042d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 130555849f57SBarry Smith PetscFunctionReturn(0); 130655849f57SBarry Smith } 130755849f57SBarry Smith 13089804daf3SBarry Smith #include <petscdraw.h> 1309e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1310e04113cfSBarry Smith #include <petscviewersaws.h> 1311f05ece33SBarry Smith #endif 131255849f57SBarry Smith #undef __FUNCT__ 13134a2ae208SSatish Balay #define __FUNCT__ "TSView" 13147e2c5f70SBarry Smith /*@C 1315d763cef2SBarry Smith TSView - Prints the TS data structure. 1316d763cef2SBarry Smith 13174c49b128SBarry Smith Collective on TS 1318d763cef2SBarry Smith 1319d763cef2SBarry Smith Input Parameters: 1320d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1321d763cef2SBarry Smith - viewer - visualization context 1322d763cef2SBarry Smith 1323d763cef2SBarry Smith Options Database Key: 1324d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1325d763cef2SBarry Smith 1326d763cef2SBarry Smith Notes: 1327d763cef2SBarry Smith The available visualization contexts include 1328b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1329b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1330d763cef2SBarry Smith output where only the first processor opens 1331d763cef2SBarry Smith the file. All other processors send their 1332d763cef2SBarry Smith data to the first processor to print. 1333d763cef2SBarry Smith 1334d763cef2SBarry Smith The user can open an alternative visualization context with 1335b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1336d763cef2SBarry Smith 1337d763cef2SBarry Smith Level: beginner 1338d763cef2SBarry Smith 1339d763cef2SBarry Smith .keywords: TS, timestep, view 1340d763cef2SBarry Smith 1341b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1342d763cef2SBarry Smith @*/ 13437087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1344d763cef2SBarry Smith { 1345dfbe8321SBarry Smith PetscErrorCode ierr; 134619fd82e9SBarry Smith TSType type; 13472b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 13482d53ad75SBarry Smith DMTS sdm; 1349e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1350f05ece33SBarry Smith PetscBool isams; 1351f05ece33SBarry Smith #endif 1352d763cef2SBarry Smith 1353d763cef2SBarry Smith PetscFunctionBegin; 13540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13553050cee2SBarry Smith if (!viewer) { 1356ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 13573050cee2SBarry Smith } 13580700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1359c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1360fd16b177SBarry Smith 1361251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1362251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 136355849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 13642b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1365e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1366e04113cfSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&isams);CHKERRQ(ierr); 1367f05ece33SBarry Smith #endif 136832077d6dSBarry Smith if (iascii) { 1369dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 137077431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 13717c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1372d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 13735ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1374c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1375d763cef2SBarry Smith } 13765ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1377193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 13782d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 13792d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1380d52bd9f3SBarry Smith if (ts->ops->view) { 1381d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1382d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1383d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1384d52bd9f3SBarry Smith } 13850f5bd95cSBarry Smith } else if (isstring) { 1386a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1387b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 138855849f57SBarry Smith } else if (isbinary) { 138955849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 139055849f57SBarry Smith MPI_Comm comm; 139155849f57SBarry Smith PetscMPIInt rank; 139255849f57SBarry Smith char type[256]; 139355849f57SBarry Smith 139455849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 139555849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 139655849f57SBarry Smith if (!rank) { 139755849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 139855849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 139955849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 140055849f57SBarry Smith } 140155849f57SBarry Smith if (ts->ops->view) { 140255849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 140355849f57SBarry Smith } 1404f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1405f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 14062d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 14072d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 14082b0a91c0SBarry Smith } else if (isdraw) { 14092b0a91c0SBarry Smith PetscDraw draw; 14102b0a91c0SBarry Smith char str[36]; 141189fd9fafSBarry Smith PetscReal x,y,bottom,h; 14122b0a91c0SBarry Smith 14132b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 14142b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 14152b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 14162b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 14170298fd71SBarry Smith ierr = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 141889fd9fafSBarry Smith bottom = y - h; 14192b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 14202b0a91c0SBarry Smith if (ts->ops->view) { 14212b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 14222b0a91c0SBarry Smith } 14232b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1424e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1425f05ece33SBarry Smith } else if (isams) { 1426d45a07a7SBarry Smith PetscMPIInt rank; 14272657e9d9SBarry Smith const char *name; 14282657e9d9SBarry Smith 14292657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1430d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1431d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1432d45a07a7SBarry Smith char dir[1024]; 1433d45a07a7SBarry Smith 1434e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1435a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 14362657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 14372657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 14382657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1439d763cef2SBarry Smith } 14400acecf5bSBarry Smith if (ts->ops->view) { 14410acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 14420acecf5bSBarry Smith } 1443f05ece33SBarry Smith #endif 1444f05ece33SBarry Smith } 1445f05ece33SBarry Smith 1446b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1447251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1448b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1449d763cef2SBarry Smith PetscFunctionReturn(0); 1450d763cef2SBarry Smith } 1451d763cef2SBarry Smith 1452d763cef2SBarry Smith 14534a2ae208SSatish Balay #undef __FUNCT__ 14544a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1455b07ff414SBarry Smith /*@ 1456d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1457d763cef2SBarry Smith the timesteppers. 1458d763cef2SBarry Smith 14593f9fe445SBarry Smith Logically Collective on TS 1460d763cef2SBarry Smith 1461d763cef2SBarry Smith Input Parameters: 1462d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1463d763cef2SBarry Smith - usrP - optional user context 1464d763cef2SBarry Smith 1465d763cef2SBarry Smith Level: intermediate 1466d763cef2SBarry Smith 1467d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1468d763cef2SBarry Smith 1469d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1470d763cef2SBarry Smith @*/ 14717087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1472d763cef2SBarry Smith { 1473d763cef2SBarry Smith PetscFunctionBegin; 14740700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1475d763cef2SBarry Smith ts->user = usrP; 1476d763cef2SBarry Smith PetscFunctionReturn(0); 1477d763cef2SBarry Smith } 1478d763cef2SBarry Smith 14794a2ae208SSatish Balay #undef __FUNCT__ 14804a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1481b07ff414SBarry Smith /*@ 1482d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1483d763cef2SBarry Smith timestepper. 1484d763cef2SBarry Smith 1485d763cef2SBarry Smith Not Collective 1486d763cef2SBarry Smith 1487d763cef2SBarry Smith Input Parameter: 1488d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1489d763cef2SBarry Smith 1490d763cef2SBarry Smith Output Parameter: 1491d763cef2SBarry Smith . usrP - user context 1492d763cef2SBarry Smith 1493d763cef2SBarry Smith Level: intermediate 1494d763cef2SBarry Smith 1495d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1496d763cef2SBarry Smith 1497d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1498d763cef2SBarry Smith @*/ 1499e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1500d763cef2SBarry Smith { 1501d763cef2SBarry Smith PetscFunctionBegin; 15020700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1503e71120c6SJed Brown *(void**)usrP = ts->user; 1504d763cef2SBarry Smith PetscFunctionReturn(0); 1505d763cef2SBarry Smith } 1506d763cef2SBarry Smith 15074a2ae208SSatish Balay #undef __FUNCT__ 15084a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1509d763cef2SBarry Smith /*@ 1510b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1511d763cef2SBarry Smith 1512d763cef2SBarry Smith Not Collective 1513d763cef2SBarry Smith 1514d763cef2SBarry Smith Input Parameter: 1515d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1516d763cef2SBarry Smith 1517d763cef2SBarry Smith Output Parameter: 1518b8123daeSJed Brown . iter - number of steps completed so far 1519d763cef2SBarry Smith 1520d763cef2SBarry Smith Level: intermediate 1521d763cef2SBarry Smith 1522d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 15239be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 1524d763cef2SBarry Smith @*/ 15257087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 1526d763cef2SBarry Smith { 1527d763cef2SBarry Smith PetscFunctionBegin; 15280700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15294482741eSBarry Smith PetscValidIntPointer(iter,2); 1530d763cef2SBarry Smith *iter = ts->steps; 1531d763cef2SBarry Smith PetscFunctionReturn(0); 1532d763cef2SBarry Smith } 1533d763cef2SBarry Smith 15344a2ae208SSatish Balay #undef __FUNCT__ 15354a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1536d763cef2SBarry Smith /*@ 1537d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1538d763cef2SBarry Smith as well as the initial time. 1539d763cef2SBarry Smith 15403f9fe445SBarry Smith Logically Collective on TS 1541d763cef2SBarry Smith 1542d763cef2SBarry Smith Input Parameters: 1543d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1544d763cef2SBarry Smith . initial_time - the initial time 1545d763cef2SBarry Smith - time_step - the size of the timestep 1546d763cef2SBarry Smith 1547d763cef2SBarry Smith Level: intermediate 1548d763cef2SBarry Smith 1549d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1550d763cef2SBarry Smith 1551d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1552d763cef2SBarry Smith @*/ 15537087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1554d763cef2SBarry Smith { 1555e144a568SJed Brown PetscErrorCode ierr; 1556e144a568SJed Brown 1557d763cef2SBarry Smith PetscFunctionBegin; 15580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1559e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1560d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1561d763cef2SBarry Smith PetscFunctionReturn(0); 1562d763cef2SBarry Smith } 1563d763cef2SBarry Smith 15644a2ae208SSatish Balay #undef __FUNCT__ 15654a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1566d763cef2SBarry Smith /*@ 1567d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1568d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1569d763cef2SBarry Smith 15703f9fe445SBarry Smith Logically Collective on TS 1571d763cef2SBarry Smith 1572d763cef2SBarry Smith Input Parameters: 1573d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1574d763cef2SBarry Smith - time_step - the size of the timestep 1575d763cef2SBarry Smith 1576d763cef2SBarry Smith Level: intermediate 1577d763cef2SBarry Smith 1578d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1579d763cef2SBarry Smith 1580d763cef2SBarry Smith .keywords: TS, set, timestep 1581d763cef2SBarry Smith @*/ 15827087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1583d763cef2SBarry Smith { 1584d763cef2SBarry Smith PetscFunctionBegin; 15850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1586c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1587d763cef2SBarry Smith ts->time_step = time_step; 158831748224SBarry Smith ts->time_step_orig = time_step; 1589d763cef2SBarry Smith PetscFunctionReturn(0); 1590d763cef2SBarry Smith } 1591d763cef2SBarry Smith 15924a2ae208SSatish Balay #undef __FUNCT__ 1593a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1594a43b19c4SJed Brown /*@ 159549354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 159649354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 159749354f04SShri Abhyankar or just return at the final time TS computed. 1598a43b19c4SJed Brown 1599a43b19c4SJed Brown Logically Collective on TS 1600a43b19c4SJed Brown 1601a43b19c4SJed Brown Input Parameter: 1602a43b19c4SJed Brown + ts - the time-step context 160349354f04SShri Abhyankar - eftopt - exact final time option 1604a43b19c4SJed Brown 1605a43b19c4SJed Brown Level: beginner 1606a43b19c4SJed Brown 1607a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 1608a43b19c4SJed Brown @*/ 160949354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 1610a43b19c4SJed Brown { 1611a43b19c4SJed Brown PetscFunctionBegin; 1612a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 161349354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 161449354f04SShri Abhyankar ts->exact_final_time = eftopt; 1615a43b19c4SJed Brown PetscFunctionReturn(0); 1616a43b19c4SJed Brown } 1617a43b19c4SJed Brown 1618a43b19c4SJed Brown #undef __FUNCT__ 16194a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1620d763cef2SBarry Smith /*@ 1621d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1622d763cef2SBarry Smith 1623d763cef2SBarry Smith Not Collective 1624d763cef2SBarry Smith 1625d763cef2SBarry Smith Input Parameter: 1626d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1627d763cef2SBarry Smith 1628d763cef2SBarry Smith Output Parameter: 1629d763cef2SBarry Smith . dt - the current timestep size 1630d763cef2SBarry Smith 1631d763cef2SBarry Smith Level: intermediate 1632d763cef2SBarry Smith 1633d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1634d763cef2SBarry Smith 1635d763cef2SBarry Smith .keywords: TS, get, timestep 1636d763cef2SBarry Smith @*/ 16377087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 1638d763cef2SBarry Smith { 1639d763cef2SBarry Smith PetscFunctionBegin; 16400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1641f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1642d763cef2SBarry Smith *dt = ts->time_step; 1643d763cef2SBarry Smith PetscFunctionReturn(0); 1644d763cef2SBarry Smith } 1645d763cef2SBarry Smith 16464a2ae208SSatish Balay #undef __FUNCT__ 16474a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1648d8e5e3e6SSatish Balay /*@ 1649d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1650d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1651d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1652d763cef2SBarry Smith the solution at the next timestep has been calculated. 1653d763cef2SBarry Smith 1654d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1655d763cef2SBarry Smith 1656d763cef2SBarry Smith Input Parameter: 1657d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1658d763cef2SBarry Smith 1659d763cef2SBarry Smith Output Parameter: 1660d763cef2SBarry Smith . v - the vector containing the solution 1661d763cef2SBarry Smith 1662d763cef2SBarry Smith Level: intermediate 1663d763cef2SBarry Smith 1664d763cef2SBarry Smith .seealso: TSGetTimeStep() 1665d763cef2SBarry Smith 1666d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1667d763cef2SBarry Smith @*/ 16687087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1669d763cef2SBarry Smith { 1670d763cef2SBarry Smith PetscFunctionBegin; 16710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 16724482741eSBarry Smith PetscValidPointer(v,2); 16738737fe31SLisandro Dalcin *v = ts->vec_sol; 1674d763cef2SBarry Smith PetscFunctionReturn(0); 1675d763cef2SBarry Smith } 1676d763cef2SBarry Smith 1677bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 16784a2ae208SSatish Balay #undef __FUNCT__ 1679bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1680d8e5e3e6SSatish Balay /*@ 1681bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1682d763cef2SBarry Smith 1683bdad233fSMatthew Knepley Not collective 1684d763cef2SBarry Smith 1685bdad233fSMatthew Knepley Input Parameters: 1686bdad233fSMatthew Knepley + ts - The TS 1687bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1688d763cef2SBarry Smith .vb 16890910c330SBarry Smith U_t - A U = 0 (linear) 16900910c330SBarry Smith U_t - A(t) U = 0 (linear) 16910910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 1692d763cef2SBarry Smith .ve 1693d763cef2SBarry Smith 1694d763cef2SBarry Smith Level: beginner 1695d763cef2SBarry Smith 1696bdad233fSMatthew Knepley .keywords: TS, problem type 1697bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1698d763cef2SBarry Smith @*/ 16997087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1700a7cc72afSBarry Smith { 17019e2a6581SJed Brown PetscErrorCode ierr; 17029e2a6581SJed Brown 1703d763cef2SBarry Smith PetscFunctionBegin; 17040700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1705bdad233fSMatthew Knepley ts->problem_type = type; 17069e2a6581SJed Brown if (type == TS_LINEAR) { 17079e2a6581SJed Brown SNES snes; 17089e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 17099e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 17109e2a6581SJed Brown } 1711d763cef2SBarry Smith PetscFunctionReturn(0); 1712d763cef2SBarry Smith } 1713d763cef2SBarry Smith 1714bdad233fSMatthew Knepley #undef __FUNCT__ 1715bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1716bdad233fSMatthew Knepley /*@C 1717bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1718bdad233fSMatthew Knepley 1719bdad233fSMatthew Knepley Not collective 1720bdad233fSMatthew Knepley 1721bdad233fSMatthew Knepley Input Parameter: 1722bdad233fSMatthew Knepley . ts - The TS 1723bdad233fSMatthew Knepley 1724bdad233fSMatthew Knepley Output Parameter: 1725bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1726bdad233fSMatthew Knepley .vb 1727089b2837SJed Brown M U_t = A U 1728089b2837SJed Brown M(t) U_t = A(t) U 1729b5abc632SBarry Smith F(t,U,U_t) 1730bdad233fSMatthew Knepley .ve 1731bdad233fSMatthew Knepley 1732bdad233fSMatthew Knepley Level: beginner 1733bdad233fSMatthew Knepley 1734bdad233fSMatthew Knepley .keywords: TS, problem type 1735bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1736bdad233fSMatthew Knepley @*/ 17377087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1738a7cc72afSBarry Smith { 1739bdad233fSMatthew Knepley PetscFunctionBegin; 17400700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 17414482741eSBarry Smith PetscValidIntPointer(type,2); 1742bdad233fSMatthew Knepley *type = ts->problem_type; 1743bdad233fSMatthew Knepley PetscFunctionReturn(0); 1744bdad233fSMatthew Knepley } 1745d763cef2SBarry Smith 17464a2ae208SSatish Balay #undef __FUNCT__ 17474a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1748d763cef2SBarry Smith /*@ 1749d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1750d763cef2SBarry Smith of a timestepper. 1751d763cef2SBarry Smith 1752d763cef2SBarry Smith Collective on TS 1753d763cef2SBarry Smith 1754d763cef2SBarry Smith Input Parameter: 1755d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1756d763cef2SBarry Smith 1757d763cef2SBarry Smith Notes: 1758d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1759d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1760d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1761d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1762d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1763d763cef2SBarry Smith 1764d763cef2SBarry Smith Level: advanced 1765d763cef2SBarry Smith 1766d763cef2SBarry Smith .keywords: TS, timestep, setup 1767d763cef2SBarry Smith 1768d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1769d763cef2SBarry Smith @*/ 17707087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1771d763cef2SBarry Smith { 1772dfbe8321SBarry Smith PetscErrorCode ierr; 17736c6b9e74SPeter Brune DM dm; 17746c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 1775d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 17766c6b9e74SPeter Brune TSIJacobian ijac; 17776c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1778d763cef2SBarry Smith 1779d763cef2SBarry Smith PetscFunctionBegin; 17800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1781277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1782277b19d0SLisandro Dalcin 17837adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 17849596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1785d763cef2SBarry Smith } 1786277b19d0SLisandro Dalcin 1787277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1788277b19d0SLisandro Dalcin 1789552698daSJed Brown ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 17901c3436cfSJed Brown 1791e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 1792e1244c69SJed Brown Mat Amat,Pmat; 1793e1244c69SJed Brown SNES snes; 1794e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1795e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 1796e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 1797e1244c69SJed Brown * have displaced the RHS matrix */ 1798e1244c69SJed Brown if (Amat == ts->Arhs) { 1799e1244c69SJed Brown ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 1800e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 1801e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 1802e1244c69SJed Brown } 1803e1244c69SJed Brown if (Pmat == ts->Brhs) { 1804e1244c69SJed Brown ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 1805e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 1806e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 1807e1244c69SJed Brown } 1808e1244c69SJed Brown } 1809e1244c69SJed Brown 1810277b19d0SLisandro Dalcin if (ts->ops->setup) { 1811000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1812277b19d0SLisandro Dalcin } 1813277b19d0SLisandro Dalcin 18146c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 18156c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 18166c6b9e74SPeter Brune */ 18176c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 18180298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 18196c6b9e74SPeter Brune if (!func) { 18206c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 18216c6b9e74SPeter Brune } 18226c6b9e74SPeter Brune /* if the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it. 18236c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 18246c6b9e74SPeter Brune */ 18250298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 18260298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 18270298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 18286c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 18296c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 18306c6b9e74SPeter Brune } 1831277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1832277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1833277b19d0SLisandro Dalcin } 1834277b19d0SLisandro Dalcin 1835277b19d0SLisandro Dalcin #undef __FUNCT__ 1836277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1837277b19d0SLisandro Dalcin /*@ 1838277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1839277b19d0SLisandro Dalcin 1840277b19d0SLisandro Dalcin Collective on TS 1841277b19d0SLisandro Dalcin 1842277b19d0SLisandro Dalcin Input Parameter: 1843277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1844277b19d0SLisandro Dalcin 1845277b19d0SLisandro Dalcin Level: beginner 1846277b19d0SLisandro Dalcin 1847277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1848277b19d0SLisandro Dalcin 1849277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1850277b19d0SLisandro Dalcin @*/ 1851277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1852277b19d0SLisandro Dalcin { 1853277b19d0SLisandro Dalcin PetscErrorCode ierr; 1854277b19d0SLisandro Dalcin 1855277b19d0SLisandro Dalcin PetscFunctionBegin; 1856277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1857277b19d0SLisandro Dalcin if (ts->ops->reset) { 1858277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1859277b19d0SLisandro Dalcin } 1860277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 1861bbd56ea5SKarl Rupp 18624e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 18634e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1864214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 18656bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1866e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 1867e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 186838637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 1869bbd56ea5SKarl Rupp 1870277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1871d763cef2SBarry Smith PetscFunctionReturn(0); 1872d763cef2SBarry Smith } 1873d763cef2SBarry Smith 18744a2ae208SSatish Balay #undef __FUNCT__ 18754a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1876d8e5e3e6SSatish Balay /*@ 1877d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1878d763cef2SBarry Smith with TSCreate(). 1879d763cef2SBarry Smith 1880d763cef2SBarry Smith Collective on TS 1881d763cef2SBarry Smith 1882d763cef2SBarry Smith Input Parameter: 1883d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1884d763cef2SBarry Smith 1885d763cef2SBarry Smith Level: beginner 1886d763cef2SBarry Smith 1887d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1888d763cef2SBarry Smith 1889d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1890d763cef2SBarry Smith @*/ 18916bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1892d763cef2SBarry Smith { 18936849ba73SBarry Smith PetscErrorCode ierr; 1894d763cef2SBarry Smith 1895d763cef2SBarry Smith PetscFunctionBegin; 18966bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 18976bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 18986bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1899d763cef2SBarry Smith 19006bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1901277b19d0SLisandro Dalcin 1902e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 1903e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 19046bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 19056d4c513bSLisandro Dalcin 190684df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 1907aeb4809dSShri Abhyankar if ((*ts)->event) { 1908aeb4809dSShri Abhyankar ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr); 1909aeb4809dSShri Abhyankar } 19106bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 19116bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 19126bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 19136d4c513bSLisandro Dalcin 1914a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1915d763cef2SBarry Smith PetscFunctionReturn(0); 1916d763cef2SBarry Smith } 1917d763cef2SBarry Smith 19184a2ae208SSatish Balay #undef __FUNCT__ 19194a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1920d8e5e3e6SSatish Balay /*@ 1921d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1922d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1923d763cef2SBarry Smith 1924d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1925d763cef2SBarry Smith 1926d763cef2SBarry Smith Input Parameter: 1927d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1928d763cef2SBarry Smith 1929d763cef2SBarry Smith Output Parameter: 1930d763cef2SBarry Smith . snes - the nonlinear solver context 1931d763cef2SBarry Smith 1932d763cef2SBarry Smith Notes: 1933d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1934d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 193594b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1936d763cef2SBarry Smith 1937d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 19380298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 1939d763cef2SBarry Smith 1940d763cef2SBarry Smith Level: beginner 1941d763cef2SBarry Smith 1942d763cef2SBarry Smith .keywords: timestep, get, SNES 1943d763cef2SBarry Smith @*/ 19447087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1945d763cef2SBarry Smith { 1946d372ba47SLisandro Dalcin PetscErrorCode ierr; 1947d372ba47SLisandro Dalcin 1948d763cef2SBarry Smith PetscFunctionBegin; 19490700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 19504482741eSBarry Smith PetscValidPointer(snes,2); 1951d372ba47SLisandro Dalcin if (!ts->snes) { 1952ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 19530298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 19543bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 1955d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 1956496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 19579e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 19589e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 19599e2a6581SJed Brown } 1960d372ba47SLisandro Dalcin } 1961d763cef2SBarry Smith *snes = ts->snes; 1962d763cef2SBarry Smith PetscFunctionReturn(0); 1963d763cef2SBarry Smith } 1964d763cef2SBarry Smith 19654a2ae208SSatish Balay #undef __FUNCT__ 1966deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES" 1967deb2cd25SJed Brown /*@ 1968deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 1969deb2cd25SJed Brown 1970deb2cd25SJed Brown Collective 1971deb2cd25SJed Brown 1972deb2cd25SJed Brown Input Parameter: 1973deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 1974deb2cd25SJed Brown - snes - the nonlinear solver context 1975deb2cd25SJed Brown 1976deb2cd25SJed Brown Notes: 1977deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 1978deb2cd25SJed Brown 1979deb2cd25SJed Brown Level: developer 1980deb2cd25SJed Brown 1981deb2cd25SJed Brown .keywords: timestep, set, SNES 1982deb2cd25SJed Brown @*/ 1983deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 1984deb2cd25SJed Brown { 1985deb2cd25SJed Brown PetscErrorCode ierr; 1986d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 1987deb2cd25SJed Brown 1988deb2cd25SJed Brown PetscFunctionBegin; 1989deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1990deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 1991deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 1992deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 1993bbd56ea5SKarl Rupp 1994deb2cd25SJed Brown ts->snes = snes; 1995bbd56ea5SKarl Rupp 19960298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 19970298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 1998740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 19990298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2000740132f1SEmil Constantinescu } 2001deb2cd25SJed Brown PetscFunctionReturn(0); 2002deb2cd25SJed Brown } 2003deb2cd25SJed Brown 2004deb2cd25SJed Brown #undef __FUNCT__ 200594b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 2006d8e5e3e6SSatish Balay /*@ 200794b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2008d763cef2SBarry Smith a TS (timestepper) context. 2009d763cef2SBarry Smith 201094b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2011d763cef2SBarry Smith 2012d763cef2SBarry Smith Input Parameter: 2013d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2014d763cef2SBarry Smith 2015d763cef2SBarry Smith Output Parameter: 201694b7f48cSBarry Smith . ksp - the nonlinear solver context 2017d763cef2SBarry Smith 2018d763cef2SBarry Smith Notes: 201994b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2020d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2021d763cef2SBarry Smith KSP and PC contexts as well. 2022d763cef2SBarry Smith 202394b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 20240298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2025d763cef2SBarry Smith 2026d763cef2SBarry Smith Level: beginner 2027d763cef2SBarry Smith 202894b7f48cSBarry Smith .keywords: timestep, get, KSP 2029d763cef2SBarry Smith @*/ 20307087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2031d763cef2SBarry Smith { 2032d372ba47SLisandro Dalcin PetscErrorCode ierr; 2033089b2837SJed Brown SNES snes; 2034d372ba47SLisandro Dalcin 2035d763cef2SBarry Smith PetscFunctionBegin; 20360700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20374482741eSBarry Smith PetscValidPointer(ksp,2); 203817186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2039e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2040089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2041089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2042d763cef2SBarry Smith PetscFunctionReturn(0); 2043d763cef2SBarry Smith } 2044d763cef2SBarry Smith 2045d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2046d763cef2SBarry Smith 20474a2ae208SSatish Balay #undef __FUNCT__ 2048adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 2049adb62b0dSMatthew Knepley /*@ 2050adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2051adb62b0dSMatthew Knepley maximum time for iteration. 2052adb62b0dSMatthew Knepley 20533f9fe445SBarry Smith Not Collective 2054adb62b0dSMatthew Knepley 2055adb62b0dSMatthew Knepley Input Parameters: 2056adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 20570298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 20580298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2059adb62b0dSMatthew Knepley 2060adb62b0dSMatthew Knepley Level: intermediate 2061adb62b0dSMatthew Knepley 2062adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2063adb62b0dSMatthew Knepley @*/ 20647087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2065adb62b0dSMatthew Knepley { 2066adb62b0dSMatthew Knepley PetscFunctionBegin; 20670700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2068abc0a331SBarry Smith if (maxsteps) { 20694482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2070adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2071adb62b0dSMatthew Knepley } 2072abc0a331SBarry Smith if (maxtime) { 20734482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2074adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2075adb62b0dSMatthew Knepley } 2076adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2077adb62b0dSMatthew Knepley } 2078adb62b0dSMatthew Knepley 2079adb62b0dSMatthew Knepley #undef __FUNCT__ 20804a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 2081d763cef2SBarry Smith /*@ 2082d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2083d763cef2SBarry Smith maximum time for iteration. 2084d763cef2SBarry Smith 20853f9fe445SBarry Smith Logically Collective on TS 2086d763cef2SBarry Smith 2087d763cef2SBarry Smith Input Parameters: 2088d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2089d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2090d763cef2SBarry Smith - maxtime - final time to iterate to 2091d763cef2SBarry Smith 2092d763cef2SBarry Smith Options Database Keys: 2093d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 20943bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2095d763cef2SBarry Smith 2096d763cef2SBarry Smith Notes: 2097d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2098d763cef2SBarry Smith 2099d763cef2SBarry Smith Level: intermediate 2100d763cef2SBarry Smith 2101d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2102a43b19c4SJed Brown 2103a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2104d763cef2SBarry Smith @*/ 21057087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2106d763cef2SBarry Smith { 2107d763cef2SBarry Smith PetscFunctionBegin; 21080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2109c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2110c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 211139b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 211239b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2113d763cef2SBarry Smith PetscFunctionReturn(0); 2114d763cef2SBarry Smith } 2115d763cef2SBarry Smith 21164a2ae208SSatish Balay #undef __FUNCT__ 21174a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 2118d763cef2SBarry Smith /*@ 2119d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2120d763cef2SBarry Smith for use by the TS routines. 2121d763cef2SBarry Smith 21223f9fe445SBarry Smith Logically Collective on TS and Vec 2123d763cef2SBarry Smith 2124d763cef2SBarry Smith Input Parameters: 2125d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 21260910c330SBarry Smith - u - the solution vector 2127d763cef2SBarry Smith 2128d763cef2SBarry Smith Level: beginner 2129d763cef2SBarry Smith 2130d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2131d763cef2SBarry Smith @*/ 21320910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2133d763cef2SBarry Smith { 21348737fe31SLisandro Dalcin PetscErrorCode ierr; 2135496e6a7aSJed Brown DM dm; 21368737fe31SLisandro Dalcin 2137d763cef2SBarry Smith PetscFunctionBegin; 21380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 21390910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 21400910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 21416bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2142bbd56ea5SKarl Rupp 21430910c330SBarry Smith ts->vec_sol = u; 2144bbd56ea5SKarl Rupp 2145496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 21460910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2147d763cef2SBarry Smith PetscFunctionReturn(0); 2148d763cef2SBarry Smith } 2149d763cef2SBarry Smith 2150e74ef692SMatthew Knepley #undef __FUNCT__ 2151e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 2152ac226902SBarry Smith /*@C 2153000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 21543f2090d5SJed Brown called once at the beginning of each time step. 2155000e7ae3SMatthew Knepley 21563f9fe445SBarry Smith Logically Collective on TS 2157000e7ae3SMatthew Knepley 2158000e7ae3SMatthew Knepley Input Parameters: 2159000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2160000e7ae3SMatthew Knepley - func - The function 2161000e7ae3SMatthew Knepley 2162000e7ae3SMatthew Knepley Calling sequence of func: 2163000e7ae3SMatthew Knepley . func (TS ts); 2164000e7ae3SMatthew Knepley 2165000e7ae3SMatthew Knepley Level: intermediate 2166000e7ae3SMatthew Knepley 2167b8123daeSJed Brown Note: 2168b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 2169b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 2170b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 2171b8123daeSJed Brown 2172000e7ae3SMatthew Knepley .keywords: TS, timestep 21739be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 2174000e7ae3SMatthew Knepley @*/ 21757087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 2176000e7ae3SMatthew Knepley { 2177000e7ae3SMatthew Knepley PetscFunctionBegin; 21780700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2179ae60f76fSBarry Smith ts->prestep = func; 2180000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2181000e7ae3SMatthew Knepley } 2182000e7ae3SMatthew Knepley 2183e74ef692SMatthew Knepley #undef __FUNCT__ 21843f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 218509ee8438SJed Brown /*@ 21863f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 21873f2090d5SJed Brown 21883f2090d5SJed Brown Collective on TS 21893f2090d5SJed Brown 21903f2090d5SJed Brown Input Parameters: 21913f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 21923f2090d5SJed Brown 21933f2090d5SJed Brown Notes: 21943f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 21953f2090d5SJed Brown so most users would not generally call this routine themselves. 21963f2090d5SJed Brown 21973f2090d5SJed Brown Level: developer 21983f2090d5SJed Brown 21993f2090d5SJed Brown .keywords: TS, timestep 22009be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 22013f2090d5SJed Brown @*/ 22027087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 22033f2090d5SJed Brown { 22043f2090d5SJed Brown PetscErrorCode ierr; 22053f2090d5SJed Brown 22063f2090d5SJed Brown PetscFunctionBegin; 22070700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2208ae60f76fSBarry Smith if (ts->prestep) { 2209ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 2210312ce896SJed Brown } 22113f2090d5SJed Brown PetscFunctionReturn(0); 22123f2090d5SJed Brown } 22133f2090d5SJed Brown 22143f2090d5SJed Brown #undef __FUNCT__ 2215b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 2216b8123daeSJed Brown /*@C 2217b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 2218b8123daeSJed Brown called once at the beginning of each stage. 2219b8123daeSJed Brown 2220b8123daeSJed Brown Logically Collective on TS 2221b8123daeSJed Brown 2222b8123daeSJed Brown Input Parameters: 2223b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 2224b8123daeSJed Brown - func - The function 2225b8123daeSJed Brown 2226b8123daeSJed Brown Calling sequence of func: 2227b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 2228b8123daeSJed Brown 2229b8123daeSJed Brown Level: intermediate 2230b8123daeSJed Brown 2231b8123daeSJed Brown Note: 2232b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 2233b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 2234b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 2235b8123daeSJed Brown 2236b8123daeSJed Brown .keywords: TS, timestep 22379be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 2238b8123daeSJed Brown @*/ 2239b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 2240b8123daeSJed Brown { 2241b8123daeSJed Brown PetscFunctionBegin; 2242b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2243ae60f76fSBarry Smith ts->prestage = func; 2244b8123daeSJed Brown PetscFunctionReturn(0); 2245b8123daeSJed Brown } 2246b8123daeSJed Brown 2247b8123daeSJed Brown #undef __FUNCT__ 22489be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage" 22499be3e283SDebojyoti Ghosh /*@C 22509be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 22519be3e283SDebojyoti Ghosh called once at the end of each stage. 22529be3e283SDebojyoti Ghosh 22539be3e283SDebojyoti Ghosh Logically Collective on TS 22549be3e283SDebojyoti Ghosh 22559be3e283SDebojyoti Ghosh Input Parameters: 22569be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 22579be3e283SDebojyoti Ghosh - func - The function 22589be3e283SDebojyoti Ghosh 22599be3e283SDebojyoti Ghosh Calling sequence of func: 22609be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 22619be3e283SDebojyoti Ghosh 22629be3e283SDebojyoti Ghosh Level: intermediate 22639be3e283SDebojyoti Ghosh 22649be3e283SDebojyoti Ghosh Note: 22659be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 22669be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 22679be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 22689be3e283SDebojyoti Ghosh 22699be3e283SDebojyoti Ghosh .keywords: TS, timestep 22709be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 22719be3e283SDebojyoti Ghosh @*/ 22729be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 22739be3e283SDebojyoti Ghosh { 22749be3e283SDebojyoti Ghosh PetscFunctionBegin; 22759be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 22769be3e283SDebojyoti Ghosh ts->poststage = func; 22779be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 22789be3e283SDebojyoti Ghosh } 22799be3e283SDebojyoti Ghosh 22809be3e283SDebojyoti Ghosh #undef __FUNCT__ 2281b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 2282b8123daeSJed Brown /*@ 2283b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 2284b8123daeSJed Brown 2285b8123daeSJed Brown Collective on TS 2286b8123daeSJed Brown 2287b8123daeSJed Brown Input Parameters: 2288b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 22899be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 2290b8123daeSJed Brown 2291b8123daeSJed Brown Notes: 2292b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 2293b8123daeSJed Brown most users would not generally call this routine themselves. 2294b8123daeSJed Brown 2295b8123daeSJed Brown Level: developer 2296b8123daeSJed Brown 2297b8123daeSJed Brown .keywords: TS, timestep 22989be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 2299b8123daeSJed Brown @*/ 2300b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 2301b8123daeSJed Brown { 2302b8123daeSJed Brown PetscErrorCode ierr; 2303b8123daeSJed Brown 2304b8123daeSJed Brown PetscFunctionBegin; 2305b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2306ae60f76fSBarry Smith if (ts->prestage) { 2307ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 2308b8123daeSJed Brown } 2309b8123daeSJed Brown PetscFunctionReturn(0); 2310b8123daeSJed Brown } 2311b8123daeSJed Brown 2312b8123daeSJed Brown #undef __FUNCT__ 23139be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage" 23149be3e283SDebojyoti Ghosh /*@ 23159be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 23169be3e283SDebojyoti Ghosh 23179be3e283SDebojyoti Ghosh Collective on TS 23189be3e283SDebojyoti Ghosh 23199be3e283SDebojyoti Ghosh Input Parameters: 23209be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 23219be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 23229be3e283SDebojyoti Ghosh stageindex - Stage number 23239be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 23249be3e283SDebojyoti Ghosh of stages) with the stage solutions 23259be3e283SDebojyoti Ghosh 23269be3e283SDebojyoti Ghosh Notes: 23279be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 23289be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 23299be3e283SDebojyoti Ghosh 23309be3e283SDebojyoti Ghosh Level: developer 23319be3e283SDebojyoti Ghosh 23329be3e283SDebojyoti Ghosh .keywords: TS, timestep 23339be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 23349be3e283SDebojyoti Ghosh @*/ 23359be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 23369be3e283SDebojyoti Ghosh { 23379be3e283SDebojyoti Ghosh PetscErrorCode ierr; 23389be3e283SDebojyoti Ghosh 23399be3e283SDebojyoti Ghosh PetscFunctionBegin; 23409be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 23414beae5d8SLisandro Dalcin if (ts->poststage) { 23429be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 23439be3e283SDebojyoti Ghosh } 23449be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 23459be3e283SDebojyoti Ghosh } 23469be3e283SDebojyoti Ghosh 23479be3e283SDebojyoti Ghosh #undef __FUNCT__ 2348e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 2349ac226902SBarry Smith /*@C 2350000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 23513f2090d5SJed Brown called once at the end of each time step. 2352000e7ae3SMatthew Knepley 23533f9fe445SBarry Smith Logically Collective on TS 2354000e7ae3SMatthew Knepley 2355000e7ae3SMatthew Knepley Input Parameters: 2356000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2357000e7ae3SMatthew Knepley - func - The function 2358000e7ae3SMatthew Knepley 2359000e7ae3SMatthew Knepley Calling sequence of func: 2360b8123daeSJed Brown $ func (TS ts); 2361000e7ae3SMatthew Knepley 2362000e7ae3SMatthew Knepley Level: intermediate 2363000e7ae3SMatthew Knepley 2364000e7ae3SMatthew Knepley .keywords: TS, timestep 2365b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 2366000e7ae3SMatthew Knepley @*/ 23677087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 2368000e7ae3SMatthew Knepley { 2369000e7ae3SMatthew Knepley PetscFunctionBegin; 23700700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2371ae60f76fSBarry Smith ts->poststep = func; 2372000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2373000e7ae3SMatthew Knepley } 2374000e7ae3SMatthew Knepley 2375e74ef692SMatthew Knepley #undef __FUNCT__ 23763f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 237709ee8438SJed Brown /*@ 23783f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 23793f2090d5SJed Brown 23803f2090d5SJed Brown Collective on TS 23813f2090d5SJed Brown 23823f2090d5SJed Brown Input Parameters: 23833f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 23843f2090d5SJed Brown 23853f2090d5SJed Brown Notes: 23863f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 23873f2090d5SJed Brown so most users would not generally call this routine themselves. 23883f2090d5SJed Brown 23893f2090d5SJed Brown Level: developer 23903f2090d5SJed Brown 23913f2090d5SJed Brown .keywords: TS, timestep 23923f2090d5SJed Brown @*/ 23937087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 23943f2090d5SJed Brown { 23953f2090d5SJed Brown PetscErrorCode ierr; 23963f2090d5SJed Brown 23973f2090d5SJed Brown PetscFunctionBegin; 23980700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2399ae60f76fSBarry Smith if (ts->poststep) { 2400ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 240172ac3e02SJed Brown } 24023f2090d5SJed Brown PetscFunctionReturn(0); 24033f2090d5SJed Brown } 24043f2090d5SJed Brown 2405d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 2406d763cef2SBarry Smith 24074a2ae208SSatish Balay #undef __FUNCT__ 2408a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 2409d763cef2SBarry Smith /*@C 2410a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 2411d763cef2SBarry Smith timestep to display the iteration's progress. 2412d763cef2SBarry Smith 24133f9fe445SBarry Smith Logically Collective on TS 2414d763cef2SBarry Smith 2415d763cef2SBarry Smith Input Parameters: 2416d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2417e213d8f1SJed Brown . monitor - monitoring routine 2418329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 24190298fd71SBarry Smith monitor routine (use NULL if no context is desired) 2420b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 24210298fd71SBarry Smith (may be NULL) 2422d763cef2SBarry Smith 2423e213d8f1SJed Brown Calling sequence of monitor: 24240910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 2425d763cef2SBarry Smith 2426d763cef2SBarry Smith + ts - the TS context 242788c05cc5SBarry Smith . 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 242888c05cc5SBarry Smith been interpolated to) 24291f06c33eSBarry Smith . time - current time 24300910c330SBarry Smith . u - current iterate 2431d763cef2SBarry Smith - mctx - [optional] monitoring context 2432d763cef2SBarry Smith 2433d763cef2SBarry Smith Notes: 2434d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 2435d763cef2SBarry Smith already has been loaded. 2436d763cef2SBarry Smith 2437025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 2438025f1a04SBarry Smith 2439d763cef2SBarry Smith Level: intermediate 2440d763cef2SBarry Smith 2441d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2442d763cef2SBarry Smith 2443a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 2444d763cef2SBarry Smith @*/ 2445c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 2446d763cef2SBarry Smith { 2447d763cef2SBarry Smith PetscFunctionBegin; 24480700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 244917186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2450d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 24518704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 2452d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 2453d763cef2SBarry Smith PetscFunctionReturn(0); 2454d763cef2SBarry Smith } 2455d763cef2SBarry Smith 24564a2ae208SSatish Balay #undef __FUNCT__ 2457a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 2458d763cef2SBarry Smith /*@C 2459a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 2460d763cef2SBarry Smith 24613f9fe445SBarry Smith Logically Collective on TS 2462d763cef2SBarry Smith 2463d763cef2SBarry Smith Input Parameters: 2464d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2465d763cef2SBarry Smith 2466d763cef2SBarry Smith Notes: 2467d763cef2SBarry Smith There is no way to remove a single, specific monitor. 2468d763cef2SBarry Smith 2469d763cef2SBarry Smith Level: intermediate 2470d763cef2SBarry Smith 2471d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2472d763cef2SBarry Smith 2473a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 2474d763cef2SBarry Smith @*/ 24757087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 2476d763cef2SBarry Smith { 2477d952e501SBarry Smith PetscErrorCode ierr; 2478d952e501SBarry Smith PetscInt i; 2479d952e501SBarry Smith 2480d763cef2SBarry Smith PetscFunctionBegin; 24810700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2482d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 24838704b422SBarry Smith if (ts->monitordestroy[i]) { 24848704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 2485d952e501SBarry Smith } 2486d952e501SBarry Smith } 2487d763cef2SBarry Smith ts->numbermonitors = 0; 2488d763cef2SBarry Smith PetscFunctionReturn(0); 2489d763cef2SBarry Smith } 2490d763cef2SBarry Smith 24914a2ae208SSatish Balay #undef __FUNCT__ 2492a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 2493d8e5e3e6SSatish Balay /*@ 2494a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 24955516499fSSatish Balay 24965516499fSSatish Balay Level: intermediate 249741251cbbSSatish Balay 24985516499fSSatish Balay .keywords: TS, set, monitor 24995516499fSSatish Balay 250041251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 250141251cbbSSatish Balay @*/ 2502649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 2503d763cef2SBarry Smith { 2504dfbe8321SBarry Smith PetscErrorCode ierr; 2505ce94432eSBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts)); 2506d132466eSBarry Smith 2507d763cef2SBarry Smith PetscFunctionBegin; 2508649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2509971832b6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr); 2510649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2511d763cef2SBarry Smith PetscFunctionReturn(0); 2512d763cef2SBarry Smith } 2513d763cef2SBarry Smith 25144a2ae208SSatish Balay #undef __FUNCT__ 2515cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 2516cd652676SJed Brown /*@ 2517cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 2518cd652676SJed Brown 2519cd652676SJed Brown Logically Collective on TS 2520cd652676SJed Brown 2521cd652676SJed Brown Input Argument: 2522cd652676SJed Brown . ts - time stepping context 2523cd652676SJed Brown 2524cd652676SJed Brown Output Argument: 2525cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 2526cd652676SJed Brown 2527cd652676SJed Brown Level: intermediate 2528cd652676SJed Brown 2529cd652676SJed Brown .keywords: TS, set 2530cd652676SJed Brown 2531cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 2532cd652676SJed Brown @*/ 2533cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 2534cd652676SJed Brown { 2535cd652676SJed Brown PetscFunctionBegin; 2536cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2537cd652676SJed Brown ts->retain_stages = flg; 2538cd652676SJed Brown PetscFunctionReturn(0); 2539cd652676SJed Brown } 2540cd652676SJed Brown 2541cd652676SJed Brown #undef __FUNCT__ 2542cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 2543cd652676SJed Brown /*@ 2544cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 2545cd652676SJed Brown 2546cd652676SJed Brown Collective on TS 2547cd652676SJed Brown 2548cd652676SJed Brown Input Argument: 2549cd652676SJed Brown + ts - time stepping context 2550cd652676SJed Brown - t - time to interpolate to 2551cd652676SJed Brown 2552cd652676SJed Brown Output Argument: 25530910c330SBarry Smith . U - state at given time 2554cd652676SJed Brown 2555cd652676SJed Brown Notes: 2556cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 2557cd652676SJed Brown 2558cd652676SJed Brown Level: intermediate 2559cd652676SJed Brown 2560cd652676SJed Brown Developer Notes: 2561cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 2562cd652676SJed Brown 2563cd652676SJed Brown .keywords: TS, set 2564cd652676SJed Brown 2565cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 2566cd652676SJed Brown @*/ 25670910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 2568cd652676SJed Brown { 2569cd652676SJed Brown PetscErrorCode ierr; 2570cd652676SJed Brown 2571cd652676SJed Brown PetscFunctionBegin; 2572cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2573b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 25746712e2f1SBarry Smith if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Requested time %g not in last time steps [%g,%g]",t,(double)(ts->ptime-ts->time_step_prev),(double)ts->ptime); 2575ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 25760910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 2577cd652676SJed Brown PetscFunctionReturn(0); 2578cd652676SJed Brown } 2579cd652676SJed Brown 2580cd652676SJed Brown #undef __FUNCT__ 25814a2ae208SSatish Balay #define __FUNCT__ "TSStep" 2582d763cef2SBarry Smith /*@ 25836d9e5789SSean Farley TSStep - Steps one time step 2584d763cef2SBarry Smith 2585d763cef2SBarry Smith Collective on TS 2586d763cef2SBarry Smith 2587d763cef2SBarry Smith Input Parameter: 2588d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2589d763cef2SBarry Smith 25906d9e5789SSean Farley Level: intermediate 2591d763cef2SBarry Smith 2592b8123daeSJed Brown Notes: 2593b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 2594b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 2595b8123daeSJed Brown 259625cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 259725cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 259825cb2221SBarry Smith 2599d763cef2SBarry Smith .keywords: TS, timestep, solve 2600d763cef2SBarry Smith 26019be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 2602d763cef2SBarry Smith @*/ 2603193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 2604d763cef2SBarry Smith { 26052fb8446cSMatthew G. Knepley DM dm; 2606dfbe8321SBarry Smith PetscErrorCode ierr; 2607fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 2608d763cef2SBarry Smith 2609d763cef2SBarry Smith PetscFunctionBegin; 26100700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2611fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 2612fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 2613fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 2614fffbeea8SBarry Smith " type = {Preprint},\n" 2615fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 2616fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 2617fffbeea8SBarry Smith " year = {2014}\n}\n",&cite); 2618fffbeea8SBarry Smith 26192fb8446cSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 2620d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 2621d405a339SMatthew Knepley 2622362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 262324655328SShri ts->ptime_prev = ts->ptime; 2624cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 26254e9ba7eeSMatthew G. Knepley ierr = VecViewFromOptions(ts->vec_sol, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr); 2626bbd56ea5SKarl Rupp 2627e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 2628d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2629193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 2630d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2631bbd56ea5SKarl Rupp 263224655328SShri ts->time_step_prev = ts->ptime - ts->ptime_prev; 2633cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 2634362cd11cSLisandro Dalcin 2635362cd11cSLisandro Dalcin if (ts->reason < 0) { 2636cef5090cSJed Brown if (ts->errorifstepfailed) { 2637cef5090cSJed Brown if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) { 2638ce94432eSBarry Smith SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]); 26392a3a10ceSLisandro Dalcin } else if (ts->reason == TS_DIVERGED_STEP_REJECTED) { 26402a3a10ceSLisandro Dalcin SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_reject or make negative to attempt recovery",TSConvergedReasons[ts->reason]); 2641ce94432eSBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 2642cef5090cSJed Brown } 2643362cd11cSLisandro Dalcin } else if (!ts->reason) { 2644db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 2645db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 2646362cd11cSLisandro Dalcin } 2647d763cef2SBarry Smith PetscFunctionReturn(0); 2648d763cef2SBarry Smith } 2649d763cef2SBarry Smith 26504a2ae208SSatish Balay #undef __FUNCT__ 265105175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 265205175c85SJed Brown /*@ 265305175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 265405175c85SJed Brown 26551c3436cfSJed Brown Collective on TS 265605175c85SJed Brown 265705175c85SJed Brown Input Arguments: 26581c3436cfSJed Brown + ts - time stepping context 26591c3436cfSJed Brown . order - desired order of accuracy 26600298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 266105175c85SJed Brown 266205175c85SJed Brown Output Arguments: 26630910c330SBarry Smith . U - state at the end of the current step 266405175c85SJed Brown 266505175c85SJed Brown Level: advanced 266605175c85SJed Brown 2667108c343cSJed Brown Notes: 2668108c343cSJed Brown This function cannot be called until all stages have been evaluated. 2669108c343cSJed Brown 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. 2670108c343cSJed Brown 26711c3436cfSJed Brown .seealso: TSStep(), TSAdapt 267205175c85SJed Brown @*/ 26730910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 267405175c85SJed Brown { 267505175c85SJed Brown PetscErrorCode ierr; 267605175c85SJed Brown 267705175c85SJed Brown PetscFunctionBegin; 267805175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 267905175c85SJed Brown PetscValidType(ts,1); 26800910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 2681ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 26820910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 268305175c85SJed Brown PetscFunctionReturn(0); 268405175c85SJed Brown } 268505175c85SJed Brown 268605175c85SJed Brown #undef __FUNCT__ 26876a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 26886a4d4014SLisandro Dalcin /*@ 26896a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 26906a4d4014SLisandro Dalcin 26916a4d4014SLisandro Dalcin Collective on TS 26926a4d4014SLisandro Dalcin 26936a4d4014SLisandro Dalcin Input Parameter: 26946a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 2695cc708dedSBarry Smith - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 26965a3a76d0SJed Brown 26976a4d4014SLisandro Dalcin Level: beginner 26986a4d4014SLisandro Dalcin 26995a3a76d0SJed Brown Notes: 27005a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 27015a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 27025a3a76d0SJed Brown stepped over the final time. 27035a3a76d0SJed Brown 27046a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 27056a4d4014SLisandro Dalcin 27066a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 27076a4d4014SLisandro Dalcin @*/ 2708cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 27096a4d4014SLisandro Dalcin { 2710b06615a5SLisandro Dalcin Vec solution; 27116a4d4014SLisandro Dalcin PetscErrorCode ierr; 2712f22f69f0SBarry Smith 27136a4d4014SLisandro Dalcin PetscFunctionBegin; 27140700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2715f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 271649354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE) { /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */ 2717b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 27180910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 2719b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 2720b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 2721b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 27225a3a76d0SJed Brown } 27230910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 2724bbd56ea5SKarl Rupp } else if (u) { 27250910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 27265a3a76d0SJed Brown } 2727b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 27286a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 2729193ac0bcSJed Brown ts->steps = 0; 27305ef26d82SJed Brown ts->ksp_its = 0; 27315ef26d82SJed Brown ts->snes_its = 0; 2732c610991cSLisandro Dalcin ts->num_snes_failures = 0; 2733c610991cSLisandro Dalcin ts->reject = 0; 2734193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 2735193ac0bcSJed Brown 2736ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 2737f05ece33SBarry Smith 2738193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 2739193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 27400910c330SBarry Smith ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 2741cc708dedSBarry Smith ts->solvetime = ts->ptime; 2742193ac0bcSJed Brown } else { 27436a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 2744db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 2745db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 2746e1a7a14fSJed Brown while (!ts->reason) { 2747b06615a5SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2748193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 2749aeb4809dSShri Abhyankar if (ts->event) { 2750aeb4809dSShri Abhyankar ierr = TSEventMonitor(ts);CHKERRQ(ierr); 27511eda64f1SShri Abhyankar if (ts->event->status != TSEVENT_PROCESSING) { 27521eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 27531eda64f1SShri Abhyankar } 27541eda64f1SShri Abhyankar } else { 27551eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 2756aeb4809dSShri Abhyankar } 2757193ac0bcSJed Brown } 275849354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 27590910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 2760cc708dedSBarry Smith ts->solvetime = ts->max_time; 2761b06615a5SLisandro Dalcin solution = u; 27620574a7fbSJed Brown } else { 2763ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 2764cc708dedSBarry Smith ts->solvetime = ts->ptime; 2765b06615a5SLisandro Dalcin solution = ts->vec_sol; 27660574a7fbSJed Brown } 2767b06615a5SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr); 27684e9ba7eeSMatthew G. Knepley ierr = VecViewFromOptions(u, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr); 2769193ac0bcSJed Brown } 2770ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 277156f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 27726a4d4014SLisandro Dalcin PetscFunctionReturn(0); 27736a4d4014SLisandro Dalcin } 27746a4d4014SLisandro Dalcin 27756a4d4014SLisandro Dalcin #undef __FUNCT__ 2776*e5e524a1SHong Zhang #define __FUNCT__ "TSSolveADJ" 2777*e5e524a1SHong Zhang /*@ 2778*e5e524a1SHong Zhang TSSolveADJ - Steps the requested number of timesteps backward. 2779*e5e524a1SHong Zhang 2780*e5e524a1SHong Zhang Collective on TS 2781*e5e524a1SHong Zhang 2782*e5e524a1SHong Zhang Input Parameter: 2783*e5e524a1SHong Zhang + ts - the TS context obtained from TSCreate() 2784*e5e524a1SHong Zhang - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 2785*e5e524a1SHong Zhang 2786*e5e524a1SHong Zhang Level: beginner 2787*e5e524a1SHong Zhang 2788*e5e524a1SHong Zhang .keywords: TS, timestep, solve 2789*e5e524a1SHong Zhang 2790*e5e524a1SHong Zhang .seealso: TSCreate(), TSSetSolution(), TSStep() 2791*e5e524a1SHong Zhang @*/ 2792*e5e524a1SHong Zhang PetscErrorCode TSSolveADJ(TS tsadj,Vec lambda) 2793*e5e524a1SHong Zhang { 2794*e5e524a1SHong Zhang PetscInt i; 2795*e5e524a1SHong Zhang PetscErrorCode ierr; 2796*e5e524a1SHong Zhang 2797*e5e524a1SHong Zhang PetscFunctionBegin; 2798*e5e524a1SHong Zhang PetscValidHeaderSpecific(tsadj,TS_CLASSID,1); 2799*e5e524a1SHong Zhang if (lambda) PetscValidHeaderSpecific(lambda,VEC_CLASSID,2); 2800*e5e524a1SHong Zhang 2801*e5e524a1SHong Zhang ierr = TSSetSolution(tsadj,lambda);CHKERRQ(ierr); 2802*e5e524a1SHong Zhang ierr = TSSetUp(tsadj);CHKERRQ(ierr); 2803*e5e524a1SHong Zhang 2804*e5e524a1SHong Zhang /* reset time step and iteration counters */ 2805*e5e524a1SHong Zhang tsadj->steps = 0; 2806*e5e524a1SHong Zhang tsadj->ksp_its = 0; 2807*e5e524a1SHong Zhang tsadj->snes_its = 0; 2808*e5e524a1SHong Zhang tsadj->num_snes_failures = 0; 2809*e5e524a1SHong Zhang tsadj->reject = 0; 2810*e5e524a1SHong Zhang tsadj->reason = TS_CONVERGED_ITERATING; 2811*e5e524a1SHong Zhang 2812*e5e524a1SHong Zhang // to do: if tsadj->max_steps is not set, complain somewhere. 2813*e5e524a1SHong Zhang 2814*e5e524a1SHong Zhang for (i=tsadj->max_steps; i>0; i--) { 2815*e5e524a1SHong Zhang ierr = TSMonitor(tsadj,i,tsadj->ptime,lambda);CHKERRQ(ierr); 2816*e5e524a1SHong Zhang ierr = TSStep(tsadj);CHKERRQ(ierr); 2817*e5e524a1SHong Zhang } 2818*e5e524a1SHong Zhang 2819*e5e524a1SHong Zhang if (lambda) {ierr = VecCopy(tsadj->vec_sol,lambda);CHKERRQ(ierr);} 2820*e5e524a1SHong Zhang tsadj->solvetime = tsadj->ptime; 2821*e5e524a1SHong Zhang 2822*e5e524a1SHong Zhang //ierr = TSMonitor(tsadj,tsadj->steps,tsadj->ptime,lambda);CHKERRQ(ierr); 2823*e5e524a1SHong Zhang //ierr = VecViewFromOptions(u, ((PetscObject) tsadj)->prefix, "-ts_view_solution");CHKERRQ(ierr); 2824*e5e524a1SHong Zhang //ierr = TSViewFromOptions(tsadj,NULL,"-ts_view");CHKERRQ(ierr); 2825*e5e524a1SHong Zhang ierr = PetscObjectSAWsBlock((PetscObject)tsadj);CHKERRQ(ierr); 2826*e5e524a1SHong Zhang 2827*e5e524a1SHong Zhang PetscFunctionReturn(0); 2828*e5e524a1SHong Zhang } 2829*e5e524a1SHong Zhang 2830*e5e524a1SHong Zhang #undef __FUNCT__ 28314a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 2832228d79bcSJed Brown /*@ 2833228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 2834228d79bcSJed Brown 2835228d79bcSJed Brown Collective on TS 2836228d79bcSJed Brown 2837228d79bcSJed Brown Input Parameters: 2838228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 2839228d79bcSJed Brown . step - step number that has just completed 2840228d79bcSJed Brown . ptime - model time of the state 28410910c330SBarry Smith - u - state at the current model time 2842228d79bcSJed Brown 2843228d79bcSJed Brown Notes: 2844228d79bcSJed Brown TSMonitor() is typically used within the time stepping implementations. 2845228d79bcSJed Brown Users might call this function when using the TSStep() interface instead of TSSolve(). 2846228d79bcSJed Brown 2847228d79bcSJed Brown Level: advanced 2848228d79bcSJed Brown 2849228d79bcSJed Brown .keywords: TS, timestep 2850228d79bcSJed Brown @*/ 28510910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 2852d763cef2SBarry Smith { 28536849ba73SBarry Smith PetscErrorCode ierr; 2854a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 2855d763cef2SBarry Smith 2856d763cef2SBarry Smith PetscFunctionBegin; 2857b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2858b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 28595edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 2860d763cef2SBarry Smith for (i=0; i<n; i++) { 28610910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 2862d763cef2SBarry Smith } 28635edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 2864d763cef2SBarry Smith PetscFunctionReturn(0); 2865d763cef2SBarry Smith } 2866d763cef2SBarry Smith 2867d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 28684a2ae208SSatish Balay #undef __FUNCT__ 2869a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 2870d763cef2SBarry Smith /*@C 2871a9f9c1f6SBarry Smith TSMonitorLGCtxCreate - Creates a line graph context for use with 2872a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 2873d763cef2SBarry Smith 2874d763cef2SBarry Smith Collective on TS 2875d763cef2SBarry Smith 2876d763cef2SBarry Smith Input Parameters: 2877d763cef2SBarry Smith + host - the X display to open, or null for the local machine 2878d763cef2SBarry Smith . label - the title to put in the title bar 28797c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 2880a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 2881a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 2882d763cef2SBarry Smith 2883d763cef2SBarry Smith Output Parameter: 28840b039ecaSBarry Smith . ctx - the context 2885d763cef2SBarry Smith 2886d763cef2SBarry Smith Options Database Key: 28874f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 28884f09c107SBarry Smith . -ts_monitor_lg_solution - 288999fdda47SBarry Smith . -ts_monitor_lg_error - 289099fdda47SBarry Smith . -ts_monitor_lg_ksp_iterations - 289199fdda47SBarry Smith . -ts_monitor_lg_snes_iterations - 289299fdda47SBarry Smith - -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true 2893d763cef2SBarry Smith 2894d763cef2SBarry Smith Notes: 2895a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 2896d763cef2SBarry Smith 2897d763cef2SBarry Smith Level: intermediate 2898d763cef2SBarry Smith 28997c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 2900d763cef2SBarry Smith 29014f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 29027c922b88SBarry Smith 2903d763cef2SBarry Smith @*/ 2904a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 2905d763cef2SBarry Smith { 2906b0a32e0cSBarry Smith PetscDraw win; 2907dfbe8321SBarry Smith PetscErrorCode ierr; 2908d763cef2SBarry Smith 2909d763cef2SBarry Smith PetscFunctionBegin; 2910b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 2911a80ad3e0SBarry Smith ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr); 29123fbbecb0SBarry Smith ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr); 29130b039ecaSBarry Smith ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr); 29143bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr); 2915287de1a7SBarry Smith ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr); 2916287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 2917a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 2918d763cef2SBarry Smith PetscFunctionReturn(0); 2919d763cef2SBarry Smith } 2920d763cef2SBarry Smith 29214a2ae208SSatish Balay #undef __FUNCT__ 29224f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 2923b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 2924d763cef2SBarry Smith { 29250b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 2926c32365f1SBarry Smith PetscReal x = ptime,y; 2927dfbe8321SBarry Smith PetscErrorCode ierr; 2928d763cef2SBarry Smith 2929d763cef2SBarry Smith PetscFunctionBegin; 2930b06615a5SLisandro Dalcin if (!step) { 2931a9f9c1f6SBarry Smith PetscDrawAxis axis; 2932a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 2933a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 2934a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 2935287de1a7SBarry Smith ierr = PetscDrawLGIndicateDataPoints(ctx->lg,PETSC_TRUE);CHKERRQ(ierr); 2936a9f9c1f6SBarry Smith } 2937c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 29380b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 2939b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 29400b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 29413923b477SBarry Smith } 2942d763cef2SBarry Smith PetscFunctionReturn(0); 2943d763cef2SBarry Smith } 2944d763cef2SBarry Smith 29454a2ae208SSatish Balay #undef __FUNCT__ 2946a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 2947d763cef2SBarry Smith /*@C 2948a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 2949a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 2950d763cef2SBarry Smith 29510b039ecaSBarry Smith Collective on TSMonitorLGCtx 2952d763cef2SBarry Smith 2953d763cef2SBarry Smith Input Parameter: 29540b039ecaSBarry Smith . ctx - the monitor context 2955d763cef2SBarry Smith 2956d763cef2SBarry Smith Level: intermediate 2957d763cef2SBarry Smith 2958d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 2959d763cef2SBarry Smith 29604f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 2961d763cef2SBarry Smith @*/ 2962a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 2963d763cef2SBarry Smith { 2964b0a32e0cSBarry Smith PetscDraw draw; 2965dfbe8321SBarry Smith PetscErrorCode ierr; 2966d763cef2SBarry Smith 2967d763cef2SBarry Smith PetscFunctionBegin; 29680b039ecaSBarry Smith ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr); 29696bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 29700b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 29710b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 2972d763cef2SBarry Smith PetscFunctionReturn(0); 2973d763cef2SBarry Smith } 2974d763cef2SBarry Smith 29754a2ae208SSatish Balay #undef __FUNCT__ 29764a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 2977d763cef2SBarry Smith /*@ 2978b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 2979d763cef2SBarry Smith 2980d763cef2SBarry Smith Not Collective 2981d763cef2SBarry Smith 2982d763cef2SBarry Smith Input Parameter: 2983d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2984d763cef2SBarry Smith 2985d763cef2SBarry Smith Output Parameter: 2986d763cef2SBarry Smith . t - the current time 2987d763cef2SBarry Smith 2988d763cef2SBarry Smith Level: beginner 2989d763cef2SBarry Smith 2990b8123daeSJed Brown Note: 2991b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 29929be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 2993b8123daeSJed Brown 2994d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2995d763cef2SBarry Smith 2996d763cef2SBarry Smith .keywords: TS, get, time 2997d763cef2SBarry Smith @*/ 29987087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 2999d763cef2SBarry Smith { 3000d763cef2SBarry Smith PetscFunctionBegin; 30010700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3002f7cf8827SBarry Smith PetscValidRealPointer(t,2); 3003d763cef2SBarry Smith *t = ts->ptime; 3004d763cef2SBarry Smith PetscFunctionReturn(0); 3005d763cef2SBarry Smith } 3006d763cef2SBarry Smith 30074a2ae208SSatish Balay #undef __FUNCT__ 3008*e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime" 3009*e5e524a1SHong Zhang /*@ 3010*e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 3011*e5e524a1SHong Zhang 3012*e5e524a1SHong Zhang Not Collective 3013*e5e524a1SHong Zhang 3014*e5e524a1SHong Zhang Input Parameter: 3015*e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 3016*e5e524a1SHong Zhang 3017*e5e524a1SHong Zhang Output Parameter: 3018*e5e524a1SHong Zhang . t - the previous time 3019*e5e524a1SHong Zhang 3020*e5e524a1SHong Zhang Level: beginner 3021*e5e524a1SHong Zhang 3022*e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3023*e5e524a1SHong Zhang 3024*e5e524a1SHong Zhang .keywords: TS, get, time 3025*e5e524a1SHong Zhang @*/ 3026*e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 3027*e5e524a1SHong Zhang { 3028*e5e524a1SHong Zhang PetscFunctionBegin; 3029*e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3030*e5e524a1SHong Zhang PetscValidRealPointer(t,2); 3031*e5e524a1SHong Zhang *t = ts->ptime_prev; 3032*e5e524a1SHong Zhang PetscFunctionReturn(0); 3033*e5e524a1SHong Zhang } 3034*e5e524a1SHong Zhang 3035*e5e524a1SHong Zhang #undef __FUNCT__ 30366a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 30376a4d4014SLisandro Dalcin /*@ 30386a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 30396a4d4014SLisandro Dalcin 30403f9fe445SBarry Smith Logically Collective on TS 30416a4d4014SLisandro Dalcin 30426a4d4014SLisandro Dalcin Input Parameters: 30436a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 30446a4d4014SLisandro Dalcin - time - the time 30456a4d4014SLisandro Dalcin 30466a4d4014SLisandro Dalcin Level: intermediate 30476a4d4014SLisandro Dalcin 30486a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 30496a4d4014SLisandro Dalcin 30506a4d4014SLisandro Dalcin .keywords: TS, set, time 30516a4d4014SLisandro Dalcin @*/ 30527087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 30536a4d4014SLisandro Dalcin { 30546a4d4014SLisandro Dalcin PetscFunctionBegin; 30550700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3056c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 30576a4d4014SLisandro Dalcin ts->ptime = t; 30586a4d4014SLisandro Dalcin PetscFunctionReturn(0); 30596a4d4014SLisandro Dalcin } 30606a4d4014SLisandro Dalcin 30616a4d4014SLisandro Dalcin #undef __FUNCT__ 30624a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 3063d763cef2SBarry Smith /*@C 3064d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 3065d763cef2SBarry Smith TS options in the database. 3066d763cef2SBarry Smith 30673f9fe445SBarry Smith Logically Collective on TS 3068d763cef2SBarry Smith 3069d763cef2SBarry Smith Input Parameter: 3070d763cef2SBarry Smith + ts - The TS context 3071d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3072d763cef2SBarry Smith 3073d763cef2SBarry Smith Notes: 3074d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3075d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3076d763cef2SBarry Smith hyphen. 3077d763cef2SBarry Smith 3078d763cef2SBarry Smith Level: advanced 3079d763cef2SBarry Smith 3080d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 3081d763cef2SBarry Smith 3082d763cef2SBarry Smith .seealso: TSSetFromOptions() 3083d763cef2SBarry Smith 3084d763cef2SBarry Smith @*/ 30857087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 3086d763cef2SBarry Smith { 3087dfbe8321SBarry Smith PetscErrorCode ierr; 3088089b2837SJed Brown SNES snes; 3089d763cef2SBarry Smith 3090d763cef2SBarry Smith PetscFunctionBegin; 30910700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3092d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3093089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3094089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3095d763cef2SBarry Smith PetscFunctionReturn(0); 3096d763cef2SBarry Smith } 3097d763cef2SBarry Smith 3098d763cef2SBarry Smith 30994a2ae208SSatish Balay #undef __FUNCT__ 31004a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 3101d763cef2SBarry Smith /*@C 3102d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 3103d763cef2SBarry Smith TS options in the database. 3104d763cef2SBarry Smith 31053f9fe445SBarry Smith Logically Collective on TS 3106d763cef2SBarry Smith 3107d763cef2SBarry Smith Input Parameter: 3108d763cef2SBarry Smith + ts - The TS context 3109d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3110d763cef2SBarry Smith 3111d763cef2SBarry Smith Notes: 3112d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3113d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3114d763cef2SBarry Smith hyphen. 3115d763cef2SBarry Smith 3116d763cef2SBarry Smith Level: advanced 3117d763cef2SBarry Smith 3118d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 3119d763cef2SBarry Smith 3120d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 3121d763cef2SBarry Smith 3122d763cef2SBarry Smith @*/ 31237087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 3124d763cef2SBarry Smith { 3125dfbe8321SBarry Smith PetscErrorCode ierr; 3126089b2837SJed Brown SNES snes; 3127d763cef2SBarry Smith 3128d763cef2SBarry Smith PetscFunctionBegin; 31290700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3130d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3131089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3132089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3133d763cef2SBarry Smith PetscFunctionReturn(0); 3134d763cef2SBarry Smith } 3135d763cef2SBarry Smith 31364a2ae208SSatish Balay #undef __FUNCT__ 31374a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 3138d763cef2SBarry Smith /*@C 3139d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 3140d763cef2SBarry Smith TS options in the database. 3141d763cef2SBarry Smith 3142d763cef2SBarry Smith Not Collective 3143d763cef2SBarry Smith 3144d763cef2SBarry Smith Input Parameter: 3145d763cef2SBarry Smith . ts - The TS context 3146d763cef2SBarry Smith 3147d763cef2SBarry Smith Output Parameter: 3148d763cef2SBarry Smith . prefix - A pointer to the prefix string used 3149d763cef2SBarry Smith 3150d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 3151d763cef2SBarry Smith sufficient length to hold the prefix. 3152d763cef2SBarry Smith 3153d763cef2SBarry Smith Level: intermediate 3154d763cef2SBarry Smith 3155d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 3156d763cef2SBarry Smith 3157d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 3158d763cef2SBarry Smith @*/ 31597087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 3160d763cef2SBarry Smith { 3161dfbe8321SBarry Smith PetscErrorCode ierr; 3162d763cef2SBarry Smith 3163d763cef2SBarry Smith PetscFunctionBegin; 31640700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31654482741eSBarry Smith PetscValidPointer(prefix,2); 3166d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3167d763cef2SBarry Smith PetscFunctionReturn(0); 3168d763cef2SBarry Smith } 3169d763cef2SBarry Smith 31704a2ae208SSatish Balay #undef __FUNCT__ 31714a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 3172d763cef2SBarry Smith /*@C 3173d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 3174d763cef2SBarry Smith 3175d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 3176d763cef2SBarry Smith 3177d763cef2SBarry Smith Input Parameter: 3178d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 3179d763cef2SBarry Smith 3180d763cef2SBarry Smith Output Parameters: 3181e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 3182e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 3183e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 3184e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 3185d763cef2SBarry Smith 31860298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 3187d763cef2SBarry Smith 3188d763cef2SBarry Smith Level: intermediate 3189d763cef2SBarry Smith 319026d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 3191d763cef2SBarry Smith 3192d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 3193d763cef2SBarry Smith @*/ 3194e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 3195d763cef2SBarry Smith { 3196089b2837SJed Brown PetscErrorCode ierr; 3197089b2837SJed Brown SNES snes; 319824989b8cSPeter Brune DM dm; 3199089b2837SJed Brown 3200d763cef2SBarry Smith PetscFunctionBegin; 3201089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3202e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 320324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 320424989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 3205d763cef2SBarry Smith PetscFunctionReturn(0); 3206d763cef2SBarry Smith } 3207d763cef2SBarry Smith 32081713a123SBarry Smith #undef __FUNCT__ 32092eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 32102eca1d9cSJed Brown /*@C 32112eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 32122eca1d9cSJed Brown 32132eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 32142eca1d9cSJed Brown 32152eca1d9cSJed Brown Input Parameter: 32162eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 32172eca1d9cSJed Brown 32182eca1d9cSJed Brown Output Parameters: 3219e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 3220e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 32212eca1d9cSJed Brown . f - The function to compute the matrices 32222eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 32232eca1d9cSJed Brown 32240298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 32252eca1d9cSJed Brown 32262eca1d9cSJed Brown Level: advanced 32272eca1d9cSJed Brown 32282eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 32292eca1d9cSJed Brown 32302eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 32312eca1d9cSJed Brown @*/ 3232e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 32332eca1d9cSJed Brown { 3234089b2837SJed Brown PetscErrorCode ierr; 3235089b2837SJed Brown SNES snes; 323624989b8cSPeter Brune DM dm; 32370910c330SBarry Smith 32382eca1d9cSJed Brown PetscFunctionBegin; 3239089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3240f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 3241e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 324224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 324324989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 32442eca1d9cSJed Brown PetscFunctionReturn(0); 32452eca1d9cSJed Brown } 32462eca1d9cSJed Brown 32476083293cSBarry Smith 32482eca1d9cSJed Brown #undef __FUNCT__ 324983a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 32501713a123SBarry Smith /*@C 325183a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 32521713a123SBarry Smith VecView() for the solution at each timestep 32531713a123SBarry Smith 32541713a123SBarry Smith Collective on TS 32551713a123SBarry Smith 32561713a123SBarry Smith Input Parameters: 32571713a123SBarry Smith + ts - the TS context 32581713a123SBarry Smith . step - current time-step 3259142b95e3SSatish Balay . ptime - current time 32600298fd71SBarry Smith - dummy - either a viewer or NULL 32611713a123SBarry Smith 326299fdda47SBarry Smith Options Database: 326399fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 326499fdda47SBarry Smith 326599fdda47SBarry Smith Notes: the initial solution and current solution are not displayed with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 326699fdda47SBarry Smith will look bad 326799fdda47SBarry Smith 32681713a123SBarry Smith Level: intermediate 32691713a123SBarry Smith 32701713a123SBarry Smith .keywords: TS, vector, monitor, view 32711713a123SBarry Smith 3272a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 32731713a123SBarry Smith @*/ 32740910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 32751713a123SBarry Smith { 3276dfbe8321SBarry Smith PetscErrorCode ierr; 327783a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 3278473a3ab2SBarry Smith PetscDraw draw; 32791713a123SBarry Smith 32801713a123SBarry Smith PetscFunctionBegin; 32816083293cSBarry Smith if (!step && ictx->showinitial) { 32826083293cSBarry Smith if (!ictx->initialsolution) { 32830910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 32841713a123SBarry Smith } 32850910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 32866083293cSBarry Smith } 3287b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 32880dcf80beSBarry Smith 32896083293cSBarry Smith if (ictx->showinitial) { 32906083293cSBarry Smith PetscReal pause; 32916083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 32926083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 32936083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 32946083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 32956083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 32966083293cSBarry Smith } 32970910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 3298473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 3299473a3ab2SBarry Smith PetscReal xl,yl,xr,yr,tw,w,h; 3300473a3ab2SBarry Smith char time[32]; 3301473a3ab2SBarry Smith size_t len; 3302473a3ab2SBarry Smith 3303473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 3304473a3ab2SBarry Smith ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr); 3305473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 3306473a3ab2SBarry Smith ierr = PetscStrlen(time,&len);CHKERRQ(ierr); 33070298fd71SBarry Smith ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr); 3308473a3ab2SBarry Smith w = xl + .5*(xr - xl) - .5*len*tw; 3309473a3ab2SBarry Smith h = yl + .95*(yr - yl); 3310473a3ab2SBarry Smith ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 3311473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 3312473a3ab2SBarry Smith } 3313473a3ab2SBarry Smith 33146083293cSBarry Smith if (ictx->showinitial) { 33156083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 33166083293cSBarry Smith } 33171713a123SBarry Smith PetscFunctionReturn(0); 33181713a123SBarry Smith } 33191713a123SBarry Smith 33202d5ee99bSBarry Smith #undef __FUNCT__ 33212d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase" 33222d5ee99bSBarry Smith /*@C 33232d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 33242d5ee99bSBarry Smith 33252d5ee99bSBarry Smith Collective on TS 33262d5ee99bSBarry Smith 33272d5ee99bSBarry Smith Input Parameters: 33282d5ee99bSBarry Smith + ts - the TS context 33292d5ee99bSBarry Smith . step - current time-step 33302d5ee99bSBarry Smith . ptime - current time 33312d5ee99bSBarry Smith - dummy - either a viewer or NULL 33322d5ee99bSBarry Smith 33332d5ee99bSBarry Smith Level: intermediate 33342d5ee99bSBarry Smith 33352d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 33362d5ee99bSBarry Smith 33372d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 33382d5ee99bSBarry Smith @*/ 33392d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 33402d5ee99bSBarry Smith { 33412d5ee99bSBarry Smith PetscErrorCode ierr; 33422d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 33432d5ee99bSBarry Smith PetscDraw draw; 33442d5ee99bSBarry Smith MPI_Comm comm; 33452d5ee99bSBarry Smith PetscInt n; 33462d5ee99bSBarry Smith PetscMPIInt size; 33472d5ee99bSBarry Smith PetscReal xl,yl,xr,yr,tw,w,h; 33482d5ee99bSBarry Smith char time[32]; 33492d5ee99bSBarry Smith size_t len; 33502d5ee99bSBarry Smith const PetscScalar *U; 33512d5ee99bSBarry Smith 33522d5ee99bSBarry Smith PetscFunctionBegin; 33532d5ee99bSBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 33542d5ee99bSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 33552d5ee99bSBarry Smith if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs"); 33562d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 33572d5ee99bSBarry Smith if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 33582d5ee99bSBarry Smith 33592d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 33602d5ee99bSBarry Smith 33612d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 33624b363babSBarry Smith ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 3363ba5783adSMatthew G Knepley if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) { 3364a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 3365a4494fc1SBarry Smith PetscFunctionReturn(0); 3366a4494fc1SBarry Smith } 33672d5ee99bSBarry Smith if (!step) ictx->color++; 33682d5ee99bSBarry Smith ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr); 33692d5ee99bSBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 33702d5ee99bSBarry Smith 33712d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 33724b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 33732d5ee99bSBarry Smith ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr); 33742d5ee99bSBarry Smith ierr = PetscStrlen(time,&len);CHKERRQ(ierr); 33752d5ee99bSBarry Smith ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr); 33762d5ee99bSBarry Smith w = xl + .5*(xr - xl) - .5*len*tw; 33772d5ee99bSBarry Smith h = yl + .95*(yr - yl); 33782d5ee99bSBarry Smith ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 33792d5ee99bSBarry Smith } 33802d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 33812d5ee99bSBarry Smith PetscFunctionReturn(0); 33822d5ee99bSBarry Smith } 33832d5ee99bSBarry Smith 33841713a123SBarry Smith 33856c699258SBarry Smith #undef __FUNCT__ 338683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 33876083293cSBarry Smith /*@C 338883a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 33896083293cSBarry Smith 33906083293cSBarry Smith Collective on TS 33916083293cSBarry Smith 33926083293cSBarry Smith Input Parameters: 33936083293cSBarry Smith . ctx - the monitor context 33946083293cSBarry Smith 33956083293cSBarry Smith Level: intermediate 33966083293cSBarry Smith 33976083293cSBarry Smith .keywords: TS, vector, monitor, view 33986083293cSBarry Smith 339983a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 34006083293cSBarry Smith @*/ 340183a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 34026083293cSBarry Smith { 34036083293cSBarry Smith PetscErrorCode ierr; 34046083293cSBarry Smith 34056083293cSBarry Smith PetscFunctionBegin; 34064b363babSBarry Smith ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr); 340783a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 340883a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 340983a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 34106083293cSBarry Smith PetscFunctionReturn(0); 34116083293cSBarry Smith } 34126083293cSBarry Smith 34136083293cSBarry Smith #undef __FUNCT__ 341483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 34156083293cSBarry Smith /*@C 341683a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 34176083293cSBarry Smith 34186083293cSBarry Smith Collective on TS 34196083293cSBarry Smith 34206083293cSBarry Smith Input Parameter: 34216083293cSBarry Smith . ts - time-step context 34226083293cSBarry Smith 34236083293cSBarry Smith Output Patameter: 34246083293cSBarry Smith . ctx - the monitor context 34256083293cSBarry Smith 342699fdda47SBarry Smith Options Database: 342799fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 342899fdda47SBarry Smith 34296083293cSBarry Smith Level: intermediate 34306083293cSBarry Smith 34316083293cSBarry Smith .keywords: TS, vector, monitor, view 34326083293cSBarry Smith 343383a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 34346083293cSBarry Smith @*/ 343583a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 34366083293cSBarry Smith { 34376083293cSBarry Smith PetscErrorCode ierr; 34386083293cSBarry Smith 34396083293cSBarry Smith PetscFunctionBegin; 3440b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 344183a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 3442e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 3443bbd56ea5SKarl Rupp 344483a4ac43SBarry Smith (*ctx)->howoften = howoften; 3445473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 34460298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 3447473a3ab2SBarry Smith 3448473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 34490298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 34502d5ee99bSBarry Smith (*ctx)->color = PETSC_DRAW_WHITE; 34516083293cSBarry Smith PetscFunctionReturn(0); 34526083293cSBarry Smith } 34536083293cSBarry Smith 34546083293cSBarry Smith #undef __FUNCT__ 345583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 34563a471f94SBarry Smith /*@C 345783a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 34583a471f94SBarry Smith VecView() for the error at each timestep 34593a471f94SBarry Smith 34603a471f94SBarry Smith Collective on TS 34613a471f94SBarry Smith 34623a471f94SBarry Smith Input Parameters: 34633a471f94SBarry Smith + ts - the TS context 34643a471f94SBarry Smith . step - current time-step 34653a471f94SBarry Smith . ptime - current time 34660298fd71SBarry Smith - dummy - either a viewer or NULL 34673a471f94SBarry Smith 34683a471f94SBarry Smith Level: intermediate 34693a471f94SBarry Smith 34703a471f94SBarry Smith .keywords: TS, vector, monitor, view 34713a471f94SBarry Smith 34723a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 34733a471f94SBarry Smith @*/ 34740910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 34753a471f94SBarry Smith { 34763a471f94SBarry Smith PetscErrorCode ierr; 347783a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 347883a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 34793a471f94SBarry Smith Vec work; 34803a471f94SBarry Smith 34813a471f94SBarry Smith PetscFunctionBegin; 3482b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 34830910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 34843a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 34850910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 34863a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 34873a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 34883a471f94SBarry Smith PetscFunctionReturn(0); 34893a471f94SBarry Smith } 34903a471f94SBarry Smith 34912a34c10cSBarry Smith #include <petsc-private/dmimpl.h> 34923a471f94SBarry Smith #undef __FUNCT__ 34936c699258SBarry Smith #define __FUNCT__ "TSSetDM" 34946c699258SBarry Smith /*@ 34956c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 34966c699258SBarry Smith 34973f9fe445SBarry Smith Logically Collective on TS and DM 34986c699258SBarry Smith 34996c699258SBarry Smith Input Parameters: 35006c699258SBarry Smith + ts - the preconditioner context 35016c699258SBarry Smith - dm - the dm 35026c699258SBarry Smith 35036c699258SBarry Smith Level: intermediate 35046c699258SBarry Smith 35056c699258SBarry Smith 35066c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 35076c699258SBarry Smith @*/ 35087087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 35096c699258SBarry Smith { 35106c699258SBarry Smith PetscErrorCode ierr; 3511089b2837SJed Brown SNES snes; 3512942e3340SBarry Smith DMTS tsdm; 35136c699258SBarry Smith 35146c699258SBarry Smith PetscFunctionBegin; 35150700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 351670663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 3517942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 35182a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 3519942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 3520942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 352124989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 352224989b8cSPeter Brune tsdm->originaldm = dm; 352324989b8cSPeter Brune } 352424989b8cSPeter Brune } 35256bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 352624989b8cSPeter Brune } 35276c699258SBarry Smith ts->dm = dm; 3528bbd56ea5SKarl Rupp 3529089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3530089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 35316c699258SBarry Smith PetscFunctionReturn(0); 35326c699258SBarry Smith } 35336c699258SBarry Smith 35346c699258SBarry Smith #undef __FUNCT__ 35356c699258SBarry Smith #define __FUNCT__ "TSGetDM" 35366c699258SBarry Smith /*@ 35376c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 35386c699258SBarry Smith 35393f9fe445SBarry Smith Not Collective 35406c699258SBarry Smith 35416c699258SBarry Smith Input Parameter: 35426c699258SBarry Smith . ts - the preconditioner context 35436c699258SBarry Smith 35446c699258SBarry Smith Output Parameter: 35456c699258SBarry Smith . dm - the dm 35466c699258SBarry Smith 35476c699258SBarry Smith Level: intermediate 35486c699258SBarry Smith 35496c699258SBarry Smith 35506c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 35516c699258SBarry Smith @*/ 35527087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 35536c699258SBarry Smith { 3554496e6a7aSJed Brown PetscErrorCode ierr; 3555496e6a7aSJed Brown 35566c699258SBarry Smith PetscFunctionBegin; 35570700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3558496e6a7aSJed Brown if (!ts->dm) { 3559ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 3560496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 3561496e6a7aSJed Brown } 35626c699258SBarry Smith *dm = ts->dm; 35636c699258SBarry Smith PetscFunctionReturn(0); 35646c699258SBarry Smith } 35651713a123SBarry Smith 35660f5c6efeSJed Brown #undef __FUNCT__ 35670f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 35680f5c6efeSJed Brown /*@ 35690f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 35700f5c6efeSJed Brown 35713f9fe445SBarry Smith Logically Collective on SNES 35720f5c6efeSJed Brown 35730f5c6efeSJed Brown Input Parameter: 3574d42a1c89SJed Brown + snes - nonlinear solver 35750910c330SBarry Smith . U - the current state at which to evaluate the residual 3576d42a1c89SJed Brown - ctx - user context, must be a TS 35770f5c6efeSJed Brown 35780f5c6efeSJed Brown Output Parameter: 35790f5c6efeSJed Brown . F - the nonlinear residual 35800f5c6efeSJed Brown 35810f5c6efeSJed Brown Notes: 35820f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 35830f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 35840f5c6efeSJed Brown 35850f5c6efeSJed Brown Level: advanced 35860f5c6efeSJed Brown 35870f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 35880f5c6efeSJed Brown @*/ 35890910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 35900f5c6efeSJed Brown { 35910f5c6efeSJed Brown TS ts = (TS)ctx; 35920f5c6efeSJed Brown PetscErrorCode ierr; 35930f5c6efeSJed Brown 35940f5c6efeSJed Brown PetscFunctionBegin; 35950f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 35960910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 35970f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 35980f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 35990910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 36000f5c6efeSJed Brown PetscFunctionReturn(0); 36010f5c6efeSJed Brown } 36020f5c6efeSJed Brown 36030f5c6efeSJed Brown #undef __FUNCT__ 36040f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 36050f5c6efeSJed Brown /*@ 36060f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 36070f5c6efeSJed Brown 36080f5c6efeSJed Brown Collective on SNES 36090f5c6efeSJed Brown 36100f5c6efeSJed Brown Input Parameter: 36110f5c6efeSJed Brown + snes - nonlinear solver 36120910c330SBarry Smith . U - the current state at which to evaluate the residual 36130f5c6efeSJed Brown - ctx - user context, must be a TS 36140f5c6efeSJed Brown 36150f5c6efeSJed Brown Output Parameter: 36160f5c6efeSJed Brown + A - the Jacobian 36170f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 36180f5c6efeSJed Brown - flag - indicates any structure change in the matrix 36190f5c6efeSJed Brown 36200f5c6efeSJed Brown Notes: 36210f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 36220f5c6efeSJed Brown 36230f5c6efeSJed Brown Level: developer 36240f5c6efeSJed Brown 36250f5c6efeSJed Brown .seealso: SNESSetJacobian() 36260f5c6efeSJed Brown @*/ 3627d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 36280f5c6efeSJed Brown { 36290f5c6efeSJed Brown TS ts = (TS)ctx; 36300f5c6efeSJed Brown PetscErrorCode ierr; 36310f5c6efeSJed Brown 36320f5c6efeSJed Brown PetscFunctionBegin; 36330f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 36340910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 36350f5c6efeSJed Brown PetscValidPointer(A,3); 363694ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 36370f5c6efeSJed Brown PetscValidPointer(B,4); 363894ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 36390f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 3640d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 36410f5c6efeSJed Brown PetscFunctionReturn(0); 36420f5c6efeSJed Brown } 3643325fc9f4SBarry Smith 36440e4ef248SJed Brown #undef __FUNCT__ 36450e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 36460e4ef248SJed Brown /*@C 36470e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 36480e4ef248SJed Brown 36490e4ef248SJed Brown Collective on TS 36500e4ef248SJed Brown 36510e4ef248SJed Brown Input Arguments: 36520e4ef248SJed Brown + ts - time stepping context 36530e4ef248SJed Brown . t - time at which to evaluate 36540910c330SBarry Smith . U - state at which to evaluate 36550e4ef248SJed Brown - ctx - context 36560e4ef248SJed Brown 36570e4ef248SJed Brown Output Arguments: 36580e4ef248SJed Brown . F - right hand side 36590e4ef248SJed Brown 36600e4ef248SJed Brown Level: intermediate 36610e4ef248SJed Brown 36620e4ef248SJed Brown Notes: 36630e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 36640e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 36650e4ef248SJed Brown 36660e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 36670e4ef248SJed Brown @*/ 36680910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 36690e4ef248SJed Brown { 36700e4ef248SJed Brown PetscErrorCode ierr; 36710e4ef248SJed Brown Mat Arhs,Brhs; 36720e4ef248SJed Brown 36730e4ef248SJed Brown PetscFunctionBegin; 36740e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 3675d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 36760910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 36770e4ef248SJed Brown PetscFunctionReturn(0); 36780e4ef248SJed Brown } 36790e4ef248SJed Brown 36800e4ef248SJed Brown #undef __FUNCT__ 36810e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 36820e4ef248SJed Brown /*@C 36830e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 36840e4ef248SJed Brown 36850e4ef248SJed Brown Collective on TS 36860e4ef248SJed Brown 36870e4ef248SJed Brown Input Arguments: 36880e4ef248SJed Brown + ts - time stepping context 36890e4ef248SJed Brown . t - time at which to evaluate 36900910c330SBarry Smith . U - state at which to evaluate 36910e4ef248SJed Brown - ctx - context 36920e4ef248SJed Brown 36930e4ef248SJed Brown Output Arguments: 36940e4ef248SJed Brown + A - pointer to operator 36950e4ef248SJed Brown . B - pointer to preconditioning matrix 36960e4ef248SJed Brown - flg - matrix structure flag 36970e4ef248SJed Brown 36980e4ef248SJed Brown Level: intermediate 36990e4ef248SJed Brown 37000e4ef248SJed Brown Notes: 37010e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 37020e4ef248SJed Brown 37030e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 37040e4ef248SJed Brown @*/ 3705d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 37060e4ef248SJed Brown { 37070e4ef248SJed Brown PetscFunctionBegin; 37080e4ef248SJed Brown PetscFunctionReturn(0); 37090e4ef248SJed Brown } 37100e4ef248SJed Brown 37110026cea9SSean Farley #undef __FUNCT__ 37120026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 37130026cea9SSean Farley /*@C 37140026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 37150026cea9SSean Farley 37160026cea9SSean Farley Collective on TS 37170026cea9SSean Farley 37180026cea9SSean Farley Input Arguments: 37190026cea9SSean Farley + ts - time stepping context 37200026cea9SSean Farley . t - time at which to evaluate 37210910c330SBarry Smith . U - state at which to evaluate 37220910c330SBarry Smith . Udot - time derivative of state vector 37230026cea9SSean Farley - ctx - context 37240026cea9SSean Farley 37250026cea9SSean Farley Output Arguments: 37260026cea9SSean Farley . F - left hand side 37270026cea9SSean Farley 37280026cea9SSean Farley Level: intermediate 37290026cea9SSean Farley 37300026cea9SSean Farley Notes: 37310910c330SBarry Smith 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 37320026cea9SSean Farley user is required to write their own TSComputeIFunction. 37330026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 37340026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 37350026cea9SSean Farley 37360026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 37370026cea9SSean Farley @*/ 37380910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 37390026cea9SSean Farley { 37400026cea9SSean Farley PetscErrorCode ierr; 37410026cea9SSean Farley Mat A,B; 37420026cea9SSean Farley 37430026cea9SSean Farley PetscFunctionBegin; 37440298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 3745d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 37460910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 37470026cea9SSean Farley PetscFunctionReturn(0); 37480026cea9SSean Farley } 37490026cea9SSean Farley 37500026cea9SSean Farley #undef __FUNCT__ 37510026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 37520026cea9SSean Farley /*@C 3753b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 37540026cea9SSean Farley 37550026cea9SSean Farley Collective on TS 37560026cea9SSean Farley 37570026cea9SSean Farley Input Arguments: 37580026cea9SSean Farley + ts - time stepping context 37590026cea9SSean Farley . t - time at which to evaluate 37600910c330SBarry Smith . U - state at which to evaluate 37610910c330SBarry Smith . Udot - time derivative of state vector 37620026cea9SSean Farley . shift - shift to apply 37630026cea9SSean Farley - ctx - context 37640026cea9SSean Farley 37650026cea9SSean Farley Output Arguments: 37660026cea9SSean Farley + A - pointer to operator 37670026cea9SSean Farley . B - pointer to preconditioning matrix 37680026cea9SSean Farley - flg - matrix structure flag 37690026cea9SSean Farley 3770b41af12eSJed Brown Level: advanced 37710026cea9SSean Farley 37720026cea9SSean Farley Notes: 37730026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 37740026cea9SSean Farley 3775b41af12eSJed Brown It is only appropriate for problems of the form 3776b41af12eSJed Brown 3777b41af12eSJed Brown $ M Udot = F(U,t) 3778b41af12eSJed Brown 3779b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 3780b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 3781b41af12eSJed Brown an implicit operator of the form 3782b41af12eSJed Brown 3783b41af12eSJed Brown $ shift*M + J 3784b41af12eSJed Brown 3785b41af12eSJed Brown where J is the Jacobian of -F(U). Support may be added in a future version of PETSc, but for now, the user must store 3786b41af12eSJed Brown a copy of M or reassemble it when requested. 3787b41af12eSJed Brown 37880026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 37890026cea9SSean Farley @*/ 3790d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 37910026cea9SSean Farley { 3792b41af12eSJed Brown PetscErrorCode ierr; 3793b41af12eSJed Brown 37940026cea9SSean Farley PetscFunctionBegin; 379594ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 3796b41af12eSJed Brown ts->ijacobian.shift = shift; 37970026cea9SSean Farley PetscFunctionReturn(0); 37980026cea9SSean Farley } 3799b41af12eSJed Brown 3800e817cc15SEmil Constantinescu #undef __FUNCT__ 3801e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType" 3802e817cc15SEmil Constantinescu /*@ 3803e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 3804e817cc15SEmil Constantinescu 3805e817cc15SEmil Constantinescu Not Collective 3806e817cc15SEmil Constantinescu 3807e817cc15SEmil Constantinescu Input Parameter: 3808e817cc15SEmil Constantinescu . ts - the TS context 3809e817cc15SEmil Constantinescu 3810e817cc15SEmil Constantinescu Output Parameter: 38114e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 3812e817cc15SEmil Constantinescu 3813e817cc15SEmil Constantinescu Level: beginner 3814e817cc15SEmil Constantinescu 3815e817cc15SEmil Constantinescu .keywords: TS, equation type 3816e817cc15SEmil Constantinescu 3817e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 3818e817cc15SEmil Constantinescu @*/ 3819e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 3820e817cc15SEmil Constantinescu { 3821e817cc15SEmil Constantinescu PetscFunctionBegin; 3822e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3823e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 3824e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 3825e817cc15SEmil Constantinescu PetscFunctionReturn(0); 3826e817cc15SEmil Constantinescu } 3827e817cc15SEmil Constantinescu 3828e817cc15SEmil Constantinescu #undef __FUNCT__ 3829e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType" 3830e817cc15SEmil Constantinescu /*@ 3831e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 3832e817cc15SEmil Constantinescu 3833e817cc15SEmil Constantinescu Not Collective 3834e817cc15SEmil Constantinescu 3835e817cc15SEmil Constantinescu Input Parameter: 3836e817cc15SEmil Constantinescu + ts - the TS context 38374e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 3838e817cc15SEmil Constantinescu 3839e817cc15SEmil Constantinescu Level: advanced 3840e817cc15SEmil Constantinescu 3841e817cc15SEmil Constantinescu .keywords: TS, equation type 3842e817cc15SEmil Constantinescu 3843e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 3844e817cc15SEmil Constantinescu @*/ 3845e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 3846e817cc15SEmil Constantinescu { 3847e817cc15SEmil Constantinescu PetscFunctionBegin; 3848e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3849e817cc15SEmil Constantinescu ts->equation_type = equation_type; 3850e817cc15SEmil Constantinescu PetscFunctionReturn(0); 3851e817cc15SEmil Constantinescu } 38520026cea9SSean Farley 38534af1b03aSJed Brown #undef __FUNCT__ 38544af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 38554af1b03aSJed Brown /*@ 38564af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 38574af1b03aSJed Brown 38584af1b03aSJed Brown Not Collective 38594af1b03aSJed Brown 38604af1b03aSJed Brown Input Parameter: 38614af1b03aSJed Brown . ts - the TS context 38624af1b03aSJed Brown 38634af1b03aSJed Brown Output Parameter: 38644af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 38654af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 38664af1b03aSJed Brown 3867487e0bb9SJed Brown Level: beginner 38684af1b03aSJed Brown 3869cd652676SJed Brown Notes: 3870cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 38714af1b03aSJed Brown 38724af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 38734af1b03aSJed Brown 38744af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 38754af1b03aSJed Brown @*/ 38764af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 38774af1b03aSJed Brown { 38784af1b03aSJed Brown PetscFunctionBegin; 38794af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 38804af1b03aSJed Brown PetscValidPointer(reason,2); 38814af1b03aSJed Brown *reason = ts->reason; 38824af1b03aSJed Brown PetscFunctionReturn(0); 38834af1b03aSJed Brown } 38844af1b03aSJed Brown 3885fb1732b5SBarry Smith #undef __FUNCT__ 3886d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 3887d6ad946cSShri Abhyankar /*@ 3888d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 3889d6ad946cSShri Abhyankar 3890d6ad946cSShri Abhyankar Not Collective 3891d6ad946cSShri Abhyankar 3892d6ad946cSShri Abhyankar Input Parameter: 3893d6ad946cSShri Abhyankar + ts - the TS context 3894d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 3895d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 3896d6ad946cSShri Abhyankar 3897f5abba47SShri Abhyankar Level: advanced 3898d6ad946cSShri Abhyankar 3899d6ad946cSShri Abhyankar Notes: 3900d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 3901d6ad946cSShri Abhyankar 3902d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 3903d6ad946cSShri Abhyankar 3904d6ad946cSShri Abhyankar .seealso: TSConvergedReason 3905d6ad946cSShri Abhyankar @*/ 3906d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 3907d6ad946cSShri Abhyankar { 3908d6ad946cSShri Abhyankar PetscFunctionBegin; 3909d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3910d6ad946cSShri Abhyankar ts->reason = reason; 3911d6ad946cSShri Abhyankar PetscFunctionReturn(0); 3912d6ad946cSShri Abhyankar } 3913d6ad946cSShri Abhyankar 3914d6ad946cSShri Abhyankar #undef __FUNCT__ 3915cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 3916cc708dedSBarry Smith /*@ 3917cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 3918cc708dedSBarry Smith 3919cc708dedSBarry Smith Not Collective 3920cc708dedSBarry Smith 3921cc708dedSBarry Smith Input Parameter: 3922cc708dedSBarry Smith . ts - the TS context 3923cc708dedSBarry Smith 3924cc708dedSBarry Smith Output Parameter: 3925cc708dedSBarry Smith . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 3926cc708dedSBarry Smith 3927487e0bb9SJed Brown Level: beginner 3928cc708dedSBarry Smith 3929cc708dedSBarry Smith Notes: 3930cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 3931cc708dedSBarry Smith 3932cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 3933cc708dedSBarry Smith 3934cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 3935cc708dedSBarry Smith @*/ 3936cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 3937cc708dedSBarry Smith { 3938cc708dedSBarry Smith PetscFunctionBegin; 3939cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3940cc708dedSBarry Smith PetscValidPointer(ftime,2); 3941cc708dedSBarry Smith *ftime = ts->solvetime; 3942cc708dedSBarry Smith PetscFunctionReturn(0); 3943cc708dedSBarry Smith } 3944cc708dedSBarry Smith 3945cc708dedSBarry Smith #undef __FUNCT__ 39465ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 39479f67acb7SJed Brown /*@ 39485ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 39499f67acb7SJed Brown used by the time integrator. 39509f67acb7SJed Brown 39519f67acb7SJed Brown Not Collective 39529f67acb7SJed Brown 39539f67acb7SJed Brown Input Parameter: 39549f67acb7SJed Brown . ts - TS context 39559f67acb7SJed Brown 39569f67acb7SJed Brown Output Parameter: 39579f67acb7SJed Brown . nits - number of nonlinear iterations 39589f67acb7SJed Brown 39599f67acb7SJed Brown Notes: 39609f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 39619f67acb7SJed Brown 39629f67acb7SJed Brown Level: intermediate 39639f67acb7SJed Brown 39649f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 39659f67acb7SJed Brown 39665ef26d82SJed Brown .seealso: TSGetKSPIterations() 39679f67acb7SJed Brown @*/ 39685ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 39699f67acb7SJed Brown { 39709f67acb7SJed Brown PetscFunctionBegin; 39719f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 39729f67acb7SJed Brown PetscValidIntPointer(nits,2); 39735ef26d82SJed Brown *nits = ts->snes_its; 39749f67acb7SJed Brown PetscFunctionReturn(0); 39759f67acb7SJed Brown } 39769f67acb7SJed Brown 39779f67acb7SJed Brown #undef __FUNCT__ 39785ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 39799f67acb7SJed Brown /*@ 39805ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 39819f67acb7SJed Brown used by the time integrator. 39829f67acb7SJed Brown 39839f67acb7SJed Brown Not Collective 39849f67acb7SJed Brown 39859f67acb7SJed Brown Input Parameter: 39869f67acb7SJed Brown . ts - TS context 39879f67acb7SJed Brown 39889f67acb7SJed Brown Output Parameter: 39899f67acb7SJed Brown . lits - number of linear iterations 39909f67acb7SJed Brown 39919f67acb7SJed Brown Notes: 39929f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 39939f67acb7SJed Brown 39949f67acb7SJed Brown Level: intermediate 39959f67acb7SJed Brown 39969f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 39979f67acb7SJed Brown 39985ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 39999f67acb7SJed Brown @*/ 40005ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 40019f67acb7SJed Brown { 40029f67acb7SJed Brown PetscFunctionBegin; 40039f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 40049f67acb7SJed Brown PetscValidIntPointer(lits,2); 40055ef26d82SJed Brown *lits = ts->ksp_its; 40069f67acb7SJed Brown PetscFunctionReturn(0); 40079f67acb7SJed Brown } 40089f67acb7SJed Brown 40099f67acb7SJed Brown #undef __FUNCT__ 4010cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 4011cef5090cSJed Brown /*@ 4012cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4013cef5090cSJed Brown 4014cef5090cSJed Brown Not Collective 4015cef5090cSJed Brown 4016cef5090cSJed Brown Input Parameter: 4017cef5090cSJed Brown . ts - TS context 4018cef5090cSJed Brown 4019cef5090cSJed Brown Output Parameter: 4020cef5090cSJed Brown . rejects - number of steps rejected 4021cef5090cSJed Brown 4022cef5090cSJed Brown Notes: 4023cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4024cef5090cSJed Brown 4025cef5090cSJed Brown Level: intermediate 4026cef5090cSJed Brown 4027cef5090cSJed Brown .keywords: TS, get, number 4028cef5090cSJed Brown 40295ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 4030cef5090cSJed Brown @*/ 4031cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 4032cef5090cSJed Brown { 4033cef5090cSJed Brown PetscFunctionBegin; 4034cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4035cef5090cSJed Brown PetscValidIntPointer(rejects,2); 4036cef5090cSJed Brown *rejects = ts->reject; 4037cef5090cSJed Brown PetscFunctionReturn(0); 4038cef5090cSJed Brown } 4039cef5090cSJed Brown 4040cef5090cSJed Brown #undef __FUNCT__ 4041cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 4042cef5090cSJed Brown /*@ 4043cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 4044cef5090cSJed Brown 4045cef5090cSJed Brown Not Collective 4046cef5090cSJed Brown 4047cef5090cSJed Brown Input Parameter: 4048cef5090cSJed Brown . ts - TS context 4049cef5090cSJed Brown 4050cef5090cSJed Brown Output Parameter: 4051cef5090cSJed Brown . fails - number of failed nonlinear solves 4052cef5090cSJed Brown 4053cef5090cSJed Brown Notes: 4054cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4055cef5090cSJed Brown 4056cef5090cSJed Brown Level: intermediate 4057cef5090cSJed Brown 4058cef5090cSJed Brown .keywords: TS, get, number 4059cef5090cSJed Brown 40605ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 4061cef5090cSJed Brown @*/ 4062cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 4063cef5090cSJed Brown { 4064cef5090cSJed Brown PetscFunctionBegin; 4065cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4066cef5090cSJed Brown PetscValidIntPointer(fails,2); 4067cef5090cSJed Brown *fails = ts->num_snes_failures; 4068cef5090cSJed Brown PetscFunctionReturn(0); 4069cef5090cSJed Brown } 4070cef5090cSJed Brown 4071cef5090cSJed Brown #undef __FUNCT__ 4072cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 4073cef5090cSJed Brown /*@ 4074cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 4075cef5090cSJed Brown 4076cef5090cSJed Brown Not Collective 4077cef5090cSJed Brown 4078cef5090cSJed Brown Input Parameter: 4079cef5090cSJed Brown + ts - TS context 4080cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 4081cef5090cSJed Brown 4082cef5090cSJed Brown Notes: 4083cef5090cSJed Brown The counter is reset to zero for each step 4084cef5090cSJed Brown 4085cef5090cSJed Brown Options Database Key: 4086cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4087cef5090cSJed Brown 4088cef5090cSJed Brown Level: intermediate 4089cef5090cSJed Brown 4090cef5090cSJed Brown .keywords: TS, set, maximum, number 4091cef5090cSJed Brown 40925ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4093cef5090cSJed Brown @*/ 4094cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 4095cef5090cSJed Brown { 4096cef5090cSJed Brown PetscFunctionBegin; 4097cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4098cef5090cSJed Brown ts->max_reject = rejects; 4099cef5090cSJed Brown PetscFunctionReturn(0); 4100cef5090cSJed Brown } 4101cef5090cSJed Brown 4102cef5090cSJed Brown #undef __FUNCT__ 4103cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 4104cef5090cSJed Brown /*@ 4105cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 4106cef5090cSJed Brown 4107cef5090cSJed Brown Not Collective 4108cef5090cSJed Brown 4109cef5090cSJed Brown Input Parameter: 4110cef5090cSJed Brown + ts - TS context 4111cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 4112cef5090cSJed Brown 4113cef5090cSJed Brown Notes: 4114cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 4115cef5090cSJed Brown 4116cef5090cSJed Brown Options Database Key: 4117cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4118cef5090cSJed Brown 4119cef5090cSJed Brown Level: intermediate 4120cef5090cSJed Brown 4121cef5090cSJed Brown .keywords: TS, set, maximum, number 4122cef5090cSJed Brown 41235ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 4124cef5090cSJed Brown @*/ 4125cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 4126cef5090cSJed Brown { 4127cef5090cSJed Brown PetscFunctionBegin; 4128cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4129cef5090cSJed Brown ts->max_snes_failures = fails; 4130cef5090cSJed Brown PetscFunctionReturn(0); 4131cef5090cSJed Brown } 4132cef5090cSJed Brown 4133cef5090cSJed Brown #undef __FUNCT__ 41344e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails" 4135cef5090cSJed Brown /*@ 4136cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 4137cef5090cSJed Brown 4138cef5090cSJed Brown Not Collective 4139cef5090cSJed Brown 4140cef5090cSJed Brown Input Parameter: 4141cef5090cSJed Brown + ts - TS context 4142cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 4143cef5090cSJed Brown 4144cef5090cSJed Brown Options Database Key: 4145cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 4146cef5090cSJed Brown 4147cef5090cSJed Brown Level: intermediate 4148cef5090cSJed Brown 4149cef5090cSJed Brown .keywords: TS, set, error 4150cef5090cSJed Brown 41515ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4152cef5090cSJed Brown @*/ 4153cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 4154cef5090cSJed Brown { 4155cef5090cSJed Brown PetscFunctionBegin; 4156cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4157cef5090cSJed Brown ts->errorifstepfailed = err; 4158cef5090cSJed Brown PetscFunctionReturn(0); 4159cef5090cSJed Brown } 4160cef5090cSJed Brown 4161cef5090cSJed Brown #undef __FUNCT__ 4162fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 4163fb1732b5SBarry Smith /*@C 4164fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 4165fb1732b5SBarry Smith 4166fb1732b5SBarry Smith Collective on TS 4167fb1732b5SBarry Smith 4168fb1732b5SBarry Smith Input Parameters: 4169fb1732b5SBarry Smith + ts - the TS context 4170fb1732b5SBarry Smith . step - current time-step 4171fb1732b5SBarry Smith . ptime - current time 41720910c330SBarry Smith . u - current state 4173fb1732b5SBarry Smith - viewer - binary viewer 4174fb1732b5SBarry Smith 4175fb1732b5SBarry Smith Level: intermediate 4176fb1732b5SBarry Smith 4177fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 4178fb1732b5SBarry Smith 4179fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4180fb1732b5SBarry Smith @*/ 41810910c330SBarry Smith PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 4182fb1732b5SBarry Smith { 4183fb1732b5SBarry Smith PetscErrorCode ierr; 4184ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 4185fb1732b5SBarry Smith 4186fb1732b5SBarry Smith PetscFunctionBegin; 41870910c330SBarry Smith ierr = VecView(u,v);CHKERRQ(ierr); 4188ed81e22dSJed Brown PetscFunctionReturn(0); 4189ed81e22dSJed Brown } 4190ed81e22dSJed Brown 4191ed81e22dSJed Brown #undef __FUNCT__ 4192ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 4193ed81e22dSJed Brown /*@C 4194ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 4195ed81e22dSJed Brown 4196ed81e22dSJed Brown Collective on TS 4197ed81e22dSJed Brown 4198ed81e22dSJed Brown Input Parameters: 4199ed81e22dSJed Brown + ts - the TS context 4200ed81e22dSJed Brown . step - current time-step 4201ed81e22dSJed Brown . ptime - current time 42020910c330SBarry Smith . u - current state 4203ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 4204ed81e22dSJed Brown 4205ed81e22dSJed Brown Level: intermediate 4206ed81e22dSJed Brown 4207ed81e22dSJed Brown Notes: 4208ed81e22dSJed Brown 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. 4209ed81e22dSJed Brown These are named according to the file name template. 4210ed81e22dSJed Brown 4211ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 4212ed81e22dSJed Brown 4213ed81e22dSJed Brown .keywords: TS, vector, monitor, view 4214ed81e22dSJed Brown 4215ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4216ed81e22dSJed Brown @*/ 42170910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 4218ed81e22dSJed Brown { 4219ed81e22dSJed Brown PetscErrorCode ierr; 4220ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 4221ed81e22dSJed Brown PetscViewer viewer; 4222ed81e22dSJed Brown 4223ed81e22dSJed Brown PetscFunctionBegin; 42248caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 4225ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 42260910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 4227ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 4228ed81e22dSJed Brown PetscFunctionReturn(0); 4229ed81e22dSJed Brown } 4230ed81e22dSJed Brown 4231ed81e22dSJed Brown #undef __FUNCT__ 4232ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 4233ed81e22dSJed Brown /*@C 4234ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 4235ed81e22dSJed Brown 4236ed81e22dSJed Brown Collective on TS 4237ed81e22dSJed Brown 4238ed81e22dSJed Brown Input Parameters: 4239ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 4240ed81e22dSJed Brown 4241ed81e22dSJed Brown Level: intermediate 4242ed81e22dSJed Brown 4243ed81e22dSJed Brown Note: 4244ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 4245ed81e22dSJed Brown 4246ed81e22dSJed Brown .keywords: TS, vector, monitor, view 4247ed81e22dSJed Brown 4248ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 4249ed81e22dSJed Brown @*/ 4250ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 4251ed81e22dSJed Brown { 4252ed81e22dSJed Brown PetscErrorCode ierr; 4253ed81e22dSJed Brown 4254ed81e22dSJed Brown PetscFunctionBegin; 4255ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 4256fb1732b5SBarry Smith PetscFunctionReturn(0); 4257fb1732b5SBarry Smith } 4258fb1732b5SBarry Smith 425984df9cb4SJed Brown #undef __FUNCT__ 4260552698daSJed Brown #define __FUNCT__ "TSGetAdapt" 426184df9cb4SJed Brown /*@ 4262552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 426384df9cb4SJed Brown 4264ed81e22dSJed Brown Collective on TS if controller has not been created yet 426584df9cb4SJed Brown 426684df9cb4SJed Brown Input Arguments: 4267ed81e22dSJed Brown . ts - time stepping context 426884df9cb4SJed Brown 426984df9cb4SJed Brown Output Arguments: 4270ed81e22dSJed Brown . adapt - adaptive controller 427184df9cb4SJed Brown 427284df9cb4SJed Brown Level: intermediate 427384df9cb4SJed Brown 4274ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 427584df9cb4SJed Brown @*/ 4276552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 427784df9cb4SJed Brown { 427884df9cb4SJed Brown PetscErrorCode ierr; 427984df9cb4SJed Brown 428084df9cb4SJed Brown PetscFunctionBegin; 428184df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 428284df9cb4SJed Brown PetscValidPointer(adapt,2); 428384df9cb4SJed Brown if (!ts->adapt) { 4284ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 42853bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 42861c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 428784df9cb4SJed Brown } 428884df9cb4SJed Brown *adapt = ts->adapt; 428984df9cb4SJed Brown PetscFunctionReturn(0); 429084df9cb4SJed Brown } 4291d6ebe24aSShri Abhyankar 4292d6ebe24aSShri Abhyankar #undef __FUNCT__ 42931c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 42941c3436cfSJed Brown /*@ 42951c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 42961c3436cfSJed Brown 42971c3436cfSJed Brown Logically Collective 42981c3436cfSJed Brown 42991c3436cfSJed Brown Input Arguments: 43001c3436cfSJed Brown + ts - time integration context 43011c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 43020298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 43031c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 43040298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 43051c3436cfSJed Brown 4306a3cdaa26SBarry Smith Options Database keys: 4307a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 4308a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 4309a3cdaa26SBarry Smith 43101c3436cfSJed Brown Level: beginner 43111c3436cfSJed Brown 4312c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 43131c3436cfSJed Brown @*/ 43141c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 43151c3436cfSJed Brown { 43161c3436cfSJed Brown PetscErrorCode ierr; 43171c3436cfSJed Brown 43181c3436cfSJed Brown PetscFunctionBegin; 4319c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 43201c3436cfSJed Brown if (vatol) { 43211c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 43221c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 4323bbd56ea5SKarl Rupp 43241c3436cfSJed Brown ts->vatol = vatol; 43251c3436cfSJed Brown } 4326c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 43271c3436cfSJed Brown if (vrtol) { 43281c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 43291c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 4330bbd56ea5SKarl Rupp 43311c3436cfSJed Brown ts->vrtol = vrtol; 43321c3436cfSJed Brown } 43331c3436cfSJed Brown PetscFunctionReturn(0); 43341c3436cfSJed Brown } 43351c3436cfSJed Brown 43361c3436cfSJed Brown #undef __FUNCT__ 4337c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 4338c5033834SJed Brown /*@ 4339c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 4340c5033834SJed Brown 4341c5033834SJed Brown Logically Collective 4342c5033834SJed Brown 4343c5033834SJed Brown Input Arguments: 4344c5033834SJed Brown . ts - time integration context 4345c5033834SJed Brown 4346c5033834SJed Brown Output Arguments: 43470298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 43480298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 43490298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 43500298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 4351c5033834SJed Brown 4352c5033834SJed Brown Level: beginner 4353c5033834SJed Brown 4354c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 4355c5033834SJed Brown @*/ 4356c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 4357c5033834SJed Brown { 4358c5033834SJed Brown PetscFunctionBegin; 4359c5033834SJed Brown if (atol) *atol = ts->atol; 4360c5033834SJed Brown if (vatol) *vatol = ts->vatol; 4361c5033834SJed Brown if (rtol) *rtol = ts->rtol; 4362c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 4363c5033834SJed Brown PetscFunctionReturn(0); 4364c5033834SJed Brown } 4365c5033834SJed Brown 4366c5033834SJed Brown #undef __FUNCT__ 43671c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS" 43681c3436cfSJed Brown /*@ 43691c3436cfSJed Brown TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 43701c3436cfSJed Brown 43711c3436cfSJed Brown Collective on TS 43721c3436cfSJed Brown 43731c3436cfSJed Brown Input Arguments: 43741c3436cfSJed Brown + ts - time stepping context 43751c3436cfSJed Brown - Y - state vector to be compared to ts->vec_sol 43761c3436cfSJed Brown 43771c3436cfSJed Brown Output Arguments: 43781c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 43791c3436cfSJed Brown 43801c3436cfSJed Brown Level: developer 43811c3436cfSJed Brown 43821c3436cfSJed Brown .seealso: TSSetTolerances() 43831c3436cfSJed Brown @*/ 43841c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 43851c3436cfSJed Brown { 43861c3436cfSJed Brown PetscErrorCode ierr; 43871c3436cfSJed Brown PetscInt i,n,N; 43880910c330SBarry Smith const PetscScalar *u,*y; 43890910c330SBarry Smith Vec U; 43901c3436cfSJed Brown PetscReal sum,gsum; 43911c3436cfSJed Brown 43921c3436cfSJed Brown PetscFunctionBegin; 43931c3436cfSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 43941c3436cfSJed Brown PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 43951c3436cfSJed Brown PetscValidPointer(norm,3); 43960910c330SBarry Smith U = ts->vec_sol; 43970910c330SBarry Smith PetscCheckSameTypeAndComm(U,1,Y,2); 4398ce94432eSBarry Smith if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 43991c3436cfSJed Brown 44000910c330SBarry Smith ierr = VecGetSize(U,&N);CHKERRQ(ierr); 44010910c330SBarry Smith ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 44020910c330SBarry Smith ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 44031c3436cfSJed Brown ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 44041c3436cfSJed Brown sum = 0.; 4405ceefc113SJed Brown if (ts->vatol && ts->vrtol) { 4406ceefc113SJed Brown const PetscScalar *atol,*rtol; 4407ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4408ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4409ceefc113SJed Brown for (i=0; i<n; i++) { 44100910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 44110910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4412ceefc113SJed Brown } 4413ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4414ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4415ceefc113SJed Brown } else if (ts->vatol) { /* vector atol, scalar rtol */ 4416ceefc113SJed Brown const PetscScalar *atol; 4417ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4418ceefc113SJed Brown for (i=0; i<n; i++) { 44190910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 44200910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4421ceefc113SJed Brown } 4422ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4423ceefc113SJed Brown } else if (ts->vrtol) { /* scalar atol, vector rtol */ 4424ceefc113SJed Brown const PetscScalar *rtol; 4425ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4426ceefc113SJed Brown for (i=0; i<n; i++) { 44270910c330SBarry Smith PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 44280910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4429ceefc113SJed Brown } 4430ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4431ceefc113SJed Brown } else { /* scalar atol, scalar rtol */ 44321c3436cfSJed Brown for (i=0; i<n; i++) { 44330910c330SBarry Smith PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 44340910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 44351c3436cfSJed Brown } 4436ceefc113SJed Brown } 44370910c330SBarry Smith ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 44381c3436cfSJed Brown ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 44391c3436cfSJed Brown 4440ce94432eSBarry Smith ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 44411c3436cfSJed Brown *norm = PetscSqrtReal(gsum / N); 4442620fc8aaSSatish Balay if (PetscIsInfOrNanReal(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 44431c3436cfSJed Brown PetscFunctionReturn(0); 44441c3436cfSJed Brown } 44451c3436cfSJed Brown 44461c3436cfSJed Brown #undef __FUNCT__ 44478d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 44488d59e960SJed Brown /*@ 44498d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 44508d59e960SJed Brown 44518d59e960SJed Brown Logically Collective on TS 44528d59e960SJed Brown 44538d59e960SJed Brown Input Arguments: 44548d59e960SJed Brown + ts - time stepping context 44558d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 44568d59e960SJed Brown 44578d59e960SJed Brown Note: 44588d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 44598d59e960SJed Brown 44608d59e960SJed Brown Level: intermediate 44618d59e960SJed Brown 44628d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 44638d59e960SJed Brown @*/ 44648d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 44658d59e960SJed Brown { 44668d59e960SJed Brown PetscFunctionBegin; 44678d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 44688d59e960SJed Brown ts->cfltime_local = cfltime; 44698d59e960SJed Brown ts->cfltime = -1.; 44708d59e960SJed Brown PetscFunctionReturn(0); 44718d59e960SJed Brown } 44728d59e960SJed Brown 44738d59e960SJed Brown #undef __FUNCT__ 44748d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 44758d59e960SJed Brown /*@ 44768d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 44778d59e960SJed Brown 44788d59e960SJed Brown Collective on TS 44798d59e960SJed Brown 44808d59e960SJed Brown Input Arguments: 44818d59e960SJed Brown . ts - time stepping context 44828d59e960SJed Brown 44838d59e960SJed Brown Output Arguments: 44848d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 44858d59e960SJed Brown 44868d59e960SJed Brown Level: advanced 44878d59e960SJed Brown 44888d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 44898d59e960SJed Brown @*/ 44908d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 44918d59e960SJed Brown { 44928d59e960SJed Brown PetscErrorCode ierr; 44938d59e960SJed Brown 44948d59e960SJed Brown PetscFunctionBegin; 44958d59e960SJed Brown if (ts->cfltime < 0) { 4496ce94432eSBarry Smith ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 44978d59e960SJed Brown } 44988d59e960SJed Brown *cfltime = ts->cfltime; 44998d59e960SJed Brown PetscFunctionReturn(0); 45008d59e960SJed Brown } 45018d59e960SJed Brown 45028d59e960SJed Brown #undef __FUNCT__ 4503d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 4504d6ebe24aSShri Abhyankar /*@ 4505d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 4506d6ebe24aSShri Abhyankar 4507d6ebe24aSShri Abhyankar Input Parameters: 4508d6ebe24aSShri Abhyankar . ts - the TS context. 4509d6ebe24aSShri Abhyankar . xl - lower bound. 4510d6ebe24aSShri Abhyankar . xu - upper bound. 4511d6ebe24aSShri Abhyankar 4512d6ebe24aSShri Abhyankar Notes: 4513d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 4514e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 4515d6ebe24aSShri Abhyankar 45162bd2b0e6SSatish Balay Level: advanced 45172bd2b0e6SSatish Balay 4518d6ebe24aSShri Abhyankar @*/ 4519d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 4520d6ebe24aSShri Abhyankar { 4521d6ebe24aSShri Abhyankar PetscErrorCode ierr; 4522d6ebe24aSShri Abhyankar SNES snes; 4523d6ebe24aSShri Abhyankar 4524d6ebe24aSShri Abhyankar PetscFunctionBegin; 4525d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4526d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 4527d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 4528d6ebe24aSShri Abhyankar } 4529d6ebe24aSShri Abhyankar 4530325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 4531c6db04a5SJed Brown #include <mex.h> 4532325fc9f4SBarry Smith 4533325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 4534325fc9f4SBarry Smith 4535325fc9f4SBarry Smith #undef __FUNCT__ 4536325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 4537325fc9f4SBarry Smith /* 4538325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 4539325fc9f4SBarry Smith TSSetFunctionMatlab(). 4540325fc9f4SBarry Smith 4541325fc9f4SBarry Smith Collective on TS 4542325fc9f4SBarry Smith 4543325fc9f4SBarry Smith Input Parameters: 4544325fc9f4SBarry Smith + snes - the TS context 45450910c330SBarry Smith - u - input vector 4546325fc9f4SBarry Smith 4547325fc9f4SBarry Smith Output Parameter: 4548325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 4549325fc9f4SBarry Smith 4550325fc9f4SBarry Smith Notes: 4551325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 4552325fc9f4SBarry Smith implementations, so most users would not generally call this routine 4553325fc9f4SBarry Smith themselves. 4554325fc9f4SBarry Smith 4555325fc9f4SBarry Smith Level: developer 4556325fc9f4SBarry Smith 4557325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 4558325fc9f4SBarry Smith 4559325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 4560325fc9f4SBarry Smith */ 45610910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 4562325fc9f4SBarry Smith { 4563325fc9f4SBarry Smith PetscErrorCode ierr; 4564325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 4565325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 4566325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 4567325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 4568325fc9f4SBarry Smith 4569325fc9f4SBarry Smith PetscFunctionBegin; 4570325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 45710910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 45720910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 4573325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 45740910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 4575325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 4576325fc9f4SBarry Smith 4577325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 45780910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 45790910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 45800910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 4581bbd56ea5SKarl Rupp 4582325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 4583325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 4584325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 4585325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 4586325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 4587325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 4588325fc9f4SBarry Smith prhs[6] = sctx->ctx; 4589325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 4590325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4591325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 4592325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 4593325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 4594325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 4595325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 4596325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 4597325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 4598325fc9f4SBarry Smith PetscFunctionReturn(0); 4599325fc9f4SBarry Smith } 4600325fc9f4SBarry Smith 4601325fc9f4SBarry Smith 4602325fc9f4SBarry Smith #undef __FUNCT__ 4603325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 4604325fc9f4SBarry Smith /* 4605325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 4606325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 4607e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 4608325fc9f4SBarry Smith 4609325fc9f4SBarry Smith Logically Collective on TS 4610325fc9f4SBarry Smith 4611325fc9f4SBarry Smith Input Parameters: 4612325fc9f4SBarry Smith + ts - the TS context 4613325fc9f4SBarry Smith - func - function evaluation routine 4614325fc9f4SBarry Smith 4615325fc9f4SBarry Smith Calling sequence of func: 46160910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 4617325fc9f4SBarry Smith 4618325fc9f4SBarry Smith Level: beginner 4619325fc9f4SBarry Smith 4620325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 4621325fc9f4SBarry Smith 4622325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4623325fc9f4SBarry Smith */ 4624cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 4625325fc9f4SBarry Smith { 4626325fc9f4SBarry Smith PetscErrorCode ierr; 4627325fc9f4SBarry Smith TSMatlabContext *sctx; 4628325fc9f4SBarry Smith 4629325fc9f4SBarry Smith PetscFunctionBegin; 4630325fc9f4SBarry Smith /* currently sctx is memory bleed */ 4631325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4632325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4633325fc9f4SBarry Smith /* 4634325fc9f4SBarry Smith This should work, but it doesn't 4635325fc9f4SBarry Smith sctx->ctx = ctx; 4636325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4637325fc9f4SBarry Smith */ 4638325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4639bbd56ea5SKarl Rupp 46400298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 4641325fc9f4SBarry Smith PetscFunctionReturn(0); 4642325fc9f4SBarry Smith } 4643325fc9f4SBarry Smith 4644325fc9f4SBarry Smith #undef __FUNCT__ 4645325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 4646325fc9f4SBarry Smith /* 4647325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 4648325fc9f4SBarry Smith TSSetJacobianMatlab(). 4649325fc9f4SBarry Smith 4650325fc9f4SBarry Smith Collective on TS 4651325fc9f4SBarry Smith 4652325fc9f4SBarry Smith Input Parameters: 4653cdcf91faSSean Farley + ts - the TS context 46540910c330SBarry Smith . u - input vector 4655325fc9f4SBarry Smith . A, B - the matrices 4656325fc9f4SBarry Smith - ctx - user context 4657325fc9f4SBarry Smith 4658325fc9f4SBarry Smith Level: developer 4659325fc9f4SBarry Smith 4660325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 4661325fc9f4SBarry Smith 4662325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 4663325fc9f4SBarry Smith @*/ 4664f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 4665325fc9f4SBarry Smith { 4666325fc9f4SBarry Smith PetscErrorCode ierr; 4667325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 4668325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 4669325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 4670325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 4671325fc9f4SBarry Smith 4672325fc9f4SBarry Smith PetscFunctionBegin; 4673cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 46740910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 4675325fc9f4SBarry Smith 46760910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 4677325fc9f4SBarry Smith 4678cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 46790910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 46800910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 46810910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 46820910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 4683bbd56ea5SKarl Rupp 4684325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 4685325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 4686325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 4687325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 4688325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 4689325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 4690325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 4691325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 4692325fc9f4SBarry Smith prhs[8] = sctx->ctx; 4693325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 4694325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4695325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 4696325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 4697325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 4698325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 4699325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 4700325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 4701325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 4702325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 4703325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 4704325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 4705325fc9f4SBarry Smith PetscFunctionReturn(0); 4706325fc9f4SBarry Smith } 4707325fc9f4SBarry Smith 4708325fc9f4SBarry Smith 4709325fc9f4SBarry Smith #undef __FUNCT__ 4710325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 4711325fc9f4SBarry Smith /* 4712325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 4713e3c5b3baSBarry Smith 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 4714325fc9f4SBarry Smith 4715325fc9f4SBarry Smith Logically Collective on TS 4716325fc9f4SBarry Smith 4717325fc9f4SBarry Smith Input Parameters: 4718cdcf91faSSean Farley + ts - the TS context 4719325fc9f4SBarry Smith . A,B - Jacobian matrices 4720325fc9f4SBarry Smith . func - function evaluation routine 4721325fc9f4SBarry Smith - ctx - user context 4722325fc9f4SBarry Smith 4723325fc9f4SBarry Smith Calling sequence of func: 47240910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 4725325fc9f4SBarry Smith 4726325fc9f4SBarry Smith 4727325fc9f4SBarry Smith Level: developer 4728325fc9f4SBarry Smith 4729325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 4730325fc9f4SBarry Smith 4731325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4732325fc9f4SBarry Smith */ 4733cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 4734325fc9f4SBarry Smith { 4735325fc9f4SBarry Smith PetscErrorCode ierr; 4736325fc9f4SBarry Smith TSMatlabContext *sctx; 4737325fc9f4SBarry Smith 4738325fc9f4SBarry Smith PetscFunctionBegin; 4739325fc9f4SBarry Smith /* currently sctx is memory bleed */ 4740325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4741325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4742325fc9f4SBarry Smith /* 4743325fc9f4SBarry Smith This should work, but it doesn't 4744325fc9f4SBarry Smith sctx->ctx = ctx; 4745325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4746325fc9f4SBarry Smith */ 4747325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4748bbd56ea5SKarl Rupp 4749cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 4750325fc9f4SBarry Smith PetscFunctionReturn(0); 4751325fc9f4SBarry Smith } 4752325fc9f4SBarry Smith 4753b5b1a830SBarry Smith #undef __FUNCT__ 4754b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 4755b5b1a830SBarry Smith /* 4756b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 4757b5b1a830SBarry Smith 4758b5b1a830SBarry Smith Collective on TS 4759b5b1a830SBarry Smith 4760b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 4761b5b1a830SBarry Smith @*/ 47620910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 4763b5b1a830SBarry Smith { 4764b5b1a830SBarry Smith PetscErrorCode ierr; 4765b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 4766a530c242SBarry Smith int nlhs = 1,nrhs = 6; 4767b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 4768b5b1a830SBarry Smith long long int lx = 0,ls = 0; 4769b5b1a830SBarry Smith 4770b5b1a830SBarry Smith PetscFunctionBegin; 4771cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 47720910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4773b5b1a830SBarry Smith 4774cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 47750910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 4776bbd56ea5SKarl Rupp 4777b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 4778b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 4779b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 4780b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 4781b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 4782b5b1a830SBarry Smith prhs[5] = sctx->ctx; 4783b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 4784b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4785b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 4786b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 4787b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 4788b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 4789b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 4790b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 4791b5b1a830SBarry Smith PetscFunctionReturn(0); 4792b5b1a830SBarry Smith } 4793b5b1a830SBarry Smith 4794b5b1a830SBarry Smith 4795b5b1a830SBarry Smith #undef __FUNCT__ 4796b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 4797b5b1a830SBarry Smith /* 4798b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 4799b5b1a830SBarry Smith 4800b5b1a830SBarry Smith Level: developer 4801b5b1a830SBarry Smith 4802b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 4803b5b1a830SBarry Smith 4804b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4805b5b1a830SBarry Smith */ 4806cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 4807b5b1a830SBarry Smith { 4808b5b1a830SBarry Smith PetscErrorCode ierr; 4809b5b1a830SBarry Smith TSMatlabContext *sctx; 4810b5b1a830SBarry Smith 4811b5b1a830SBarry Smith PetscFunctionBegin; 4812b5b1a830SBarry Smith /* currently sctx is memory bleed */ 4813b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4814b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4815b5b1a830SBarry Smith /* 4816b5b1a830SBarry Smith This should work, but it doesn't 4817b5b1a830SBarry Smith sctx->ctx = ctx; 4818b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4819b5b1a830SBarry Smith */ 4820b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4821bbd56ea5SKarl Rupp 48220298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 4823b5b1a830SBarry Smith PetscFunctionReturn(0); 4824b5b1a830SBarry Smith } 4825325fc9f4SBarry Smith #endif 4826b3603a34SBarry Smith 48270b039ecaSBarry Smith 48280b039ecaSBarry Smith 4829b3603a34SBarry Smith #undef __FUNCT__ 48304f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 4831b3603a34SBarry Smith /*@C 48324f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 4833b3603a34SBarry Smith in a time based line graph 4834b3603a34SBarry Smith 4835b3603a34SBarry Smith Collective on TS 4836b3603a34SBarry Smith 4837b3603a34SBarry Smith Input Parameters: 4838b3603a34SBarry Smith + ts - the TS context 4839b3603a34SBarry Smith . step - current time-step 4840b3603a34SBarry Smith . ptime - current time 4841b3603a34SBarry Smith - lg - a line graph object 4842b3603a34SBarry Smith 4843b3603a34SBarry Smith Level: intermediate 4844b3603a34SBarry Smith 48450b039ecaSBarry Smith Notes: each process in a parallel run displays its component solutions in a separate window 4846b3603a34SBarry Smith 4847b3603a34SBarry Smith .keywords: TS, vector, monitor, view 4848b3603a34SBarry Smith 4849b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4850b3603a34SBarry Smith @*/ 48510910c330SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4852b3603a34SBarry Smith { 4853b3603a34SBarry Smith PetscErrorCode ierr; 48540b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4855b3603a34SBarry Smith const PetscScalar *yy; 485658ff32f7SBarry Smith PetscInt dim; 4857b3603a34SBarry Smith 4858b3603a34SBarry Smith PetscFunctionBegin; 485958ff32f7SBarry Smith if (!step) { 4860a9f9c1f6SBarry Smith PetscDrawAxis axis; 4861a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4862a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 48630910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 48640b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 48650b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 486658ff32f7SBarry Smith } 48670910c330SBarry Smith ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr); 4868e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 4869e3efe391SJed Brown { 4870e3efe391SJed Brown PetscReal *yreal; 4871e3efe391SJed Brown PetscInt i,n; 48720910c330SBarry Smith ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr); 4873785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 4874e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 48750b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4876e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4877e3efe391SJed Brown } 4878e3efe391SJed Brown #else 48790b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4880e3efe391SJed Brown #endif 48810910c330SBarry Smith ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr); 4882b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 48830b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 48843923b477SBarry Smith } 4885b3603a34SBarry Smith PetscFunctionReturn(0); 4886b3603a34SBarry Smith } 4887b3603a34SBarry Smith 4888b3603a34SBarry Smith #undef __FUNCT__ 48894f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 4890ef20d060SBarry Smith /*@C 48914f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 4892ef20d060SBarry Smith in a time based line graph 4893ef20d060SBarry Smith 4894ef20d060SBarry Smith Collective on TS 4895ef20d060SBarry Smith 4896ef20d060SBarry Smith Input Parameters: 4897ef20d060SBarry Smith + ts - the TS context 4898ef20d060SBarry Smith . step - current time-step 4899ef20d060SBarry Smith . ptime - current time 4900ef20d060SBarry Smith - lg - a line graph object 4901ef20d060SBarry Smith 4902ef20d060SBarry Smith Level: intermediate 4903ef20d060SBarry Smith 4904abd5a294SJed Brown Notes: 4905abd5a294SJed Brown Only for sequential solves. 4906abd5a294SJed Brown 4907abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 4908abd5a294SJed Brown 4909abd5a294SJed Brown Options Database Keys: 49104f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 4911ef20d060SBarry Smith 4912ef20d060SBarry Smith .keywords: TS, vector, monitor, view 4913ef20d060SBarry Smith 4914abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 4915ef20d060SBarry Smith @*/ 49160910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4917ef20d060SBarry Smith { 4918ef20d060SBarry Smith PetscErrorCode ierr; 49190b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4920ef20d060SBarry Smith const PetscScalar *yy; 4921ef20d060SBarry Smith Vec y; 4922a9f9c1f6SBarry Smith PetscInt dim; 4923ef20d060SBarry Smith 4924ef20d060SBarry Smith PetscFunctionBegin; 4925a9f9c1f6SBarry Smith if (!step) { 4926a9f9c1f6SBarry Smith PetscDrawAxis axis; 4927a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 49281ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 49290910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 4930a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 4931a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4932a9f9c1f6SBarry Smith } 49330910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 4934ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 49350910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 4936ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 4937e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 4938e3efe391SJed Brown { 4939e3efe391SJed Brown PetscReal *yreal; 4940e3efe391SJed Brown PetscInt i,n; 4941e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 4942785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 4943e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 49440b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4945e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4946e3efe391SJed Brown } 4947e3efe391SJed Brown #else 49480b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4949e3efe391SJed Brown #endif 4950ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 4951ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 4952b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 49530b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 49543923b477SBarry Smith } 4955ef20d060SBarry Smith PetscFunctionReturn(0); 4956ef20d060SBarry Smith } 4957ef20d060SBarry Smith 4958201da799SBarry Smith #undef __FUNCT__ 4959201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 4960201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4961201da799SBarry Smith { 4962201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4963201da799SBarry Smith PetscReal x = ptime,y; 4964201da799SBarry Smith PetscErrorCode ierr; 4965201da799SBarry Smith PetscInt its; 4966201da799SBarry Smith 4967201da799SBarry Smith PetscFunctionBegin; 4968201da799SBarry Smith if (!n) { 4969201da799SBarry Smith PetscDrawAxis axis; 4970bbd56ea5SKarl Rupp 4971201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4972201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 4973201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4974bbd56ea5SKarl Rupp 4975201da799SBarry Smith ctx->snes_its = 0; 4976201da799SBarry Smith } 4977201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 4978201da799SBarry Smith y = its - ctx->snes_its; 4979201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 49803fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 4981201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4982201da799SBarry Smith } 4983201da799SBarry Smith ctx->snes_its = its; 4984201da799SBarry Smith PetscFunctionReturn(0); 4985201da799SBarry Smith } 4986201da799SBarry Smith 4987201da799SBarry Smith #undef __FUNCT__ 4988201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 4989201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4990201da799SBarry Smith { 4991201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4992201da799SBarry Smith PetscReal x = ptime,y; 4993201da799SBarry Smith PetscErrorCode ierr; 4994201da799SBarry Smith PetscInt its; 4995201da799SBarry Smith 4996201da799SBarry Smith PetscFunctionBegin; 4997201da799SBarry Smith if (!n) { 4998201da799SBarry Smith PetscDrawAxis axis; 4999bbd56ea5SKarl Rupp 5000201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5001201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 5002201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 5003bbd56ea5SKarl Rupp 5004201da799SBarry Smith ctx->ksp_its = 0; 5005201da799SBarry Smith } 5006201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 5007201da799SBarry Smith y = its - ctx->ksp_its; 5008201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 500999fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 5010201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 5011201da799SBarry Smith } 5012201da799SBarry Smith ctx->ksp_its = its; 5013201da799SBarry Smith PetscFunctionReturn(0); 5014201da799SBarry Smith } 5015f9c1d6abSBarry Smith 5016f9c1d6abSBarry Smith #undef __FUNCT__ 5017f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 5018f9c1d6abSBarry Smith /*@ 5019f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 5020f9c1d6abSBarry Smith 5021f9c1d6abSBarry Smith Collective on TS and Vec 5022f9c1d6abSBarry Smith 5023f9c1d6abSBarry Smith Input Parameters: 5024f9c1d6abSBarry Smith + ts - the TS context 5025f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 5026f9c1d6abSBarry Smith 5027f9c1d6abSBarry Smith Output Parameters: 5028f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 5029f9c1d6abSBarry Smith 5030f9c1d6abSBarry Smith Level: developer 5031f9c1d6abSBarry Smith 5032f9c1d6abSBarry Smith .keywords: TS, compute 5033f9c1d6abSBarry Smith 5034f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 5035f9c1d6abSBarry Smith @*/ 5036f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 5037f9c1d6abSBarry Smith { 5038f9c1d6abSBarry Smith PetscErrorCode ierr; 5039f9c1d6abSBarry Smith 5040f9c1d6abSBarry Smith PetscFunctionBegin; 5041f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5042ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 5043f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 5044f9c1d6abSBarry Smith PetscFunctionReturn(0); 5045f9c1d6abSBarry Smith } 504624655328SShri 504724655328SShri #undef __FUNCT__ 504824655328SShri #define __FUNCT__ "TSRollBack" 504924655328SShri /*@ 505024655328SShri TSRollBack - Rolls back one time step 505124655328SShri 505224655328SShri Collective on TS 505324655328SShri 505424655328SShri Input Parameter: 505524655328SShri . ts - the TS context obtained from TSCreate() 505624655328SShri 505724655328SShri Level: advanced 505824655328SShri 505924655328SShri .keywords: TS, timestep, rollback 506024655328SShri 506124655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 506224655328SShri @*/ 506324655328SShri PetscErrorCode TSRollBack(TS ts) 506424655328SShri { 506524655328SShri PetscErrorCode ierr; 506624655328SShri 506724655328SShri PetscFunctionBegin; 506824655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 506924655328SShri 507024655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 507124655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 507224655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 507324655328SShri ts->ptime = ts->ptime_prev; 507424655328SShri PetscFunctionReturn(0); 507524655328SShri } 5076aeb4809dSShri Abhyankar 5077ff22ae23SHong Zhang #undef __FUNCT__ 5078ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages" 5079ff22ae23SHong Zhang /*@ 5080ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 5081ff22ae23SHong Zhang 5082ff22ae23SHong Zhang Input Parameter: 5083ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 5084ff22ae23SHong Zhang 5085ff22ae23SHong Zhang Level: advanced 5086ff22ae23SHong Zhang 5087ff22ae23SHong Zhang .keywords: TS, getstages 5088ff22ae23SHong Zhang 5089ff22ae23SHong Zhang .seealso: TSCreate() 5090ff22ae23SHong Zhang @*/ 5091ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns, Vec **Y) 5092ff22ae23SHong Zhang { 5093ff22ae23SHong Zhang PetscErrorCode ierr; 5094ff22ae23SHong Zhang 5095ff22ae23SHong Zhang PetscFunctionBegin; 5096ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 5097ff22ae23SHong Zhang PetscValidPointer(ns,2); 5098ff22ae23SHong Zhang 5099ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 5100ff22ae23SHong Zhang else { 5101ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 5102ff22ae23SHong Zhang } 5103ff22ae23SHong Zhang PetscFunctionReturn(0); 5104ff22ae23SHong Zhang } 5105ff22ae23SHong Zhang 5106ff22ae23SHong Zhang 5107ff22ae23SHong Zhang 5108