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 72d2daff3dSHong Zhang . -ts_checkpoint - checkpoint for adjoint sensitivity analysis 73bdad233fSMatthew Knepley . -ts_max_steps maxsteps - maximum number of time-steps to take 743bca7d26SBarry Smith . -ts_final_time time - maximum time to compute to 75bdad233fSMatthew Knepley . -ts_dt dt - initial time step 76a3cdaa26SBarry 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 77a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 78a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 79a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 80a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 81a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 82bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 83de06c3feSJed Brown . -ts_monitor_lg_timestep - Monitor timestep size graphically 84de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 85de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 86de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 87de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 88de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 89de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 902d5ee99bSBarry Smith . -ts_monitor_draw_solution_phase - Monitor solution graphically with phase diagram 91de06c3feSJed Brown . -ts_monitor_draw_error - Monitor error graphically 9291b97e58SBarry Smith . -ts_monitor_solution_binary <filename> - Save each solution to a binary file 9391b97e58SBarry Smith - -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 9491b97e58SBarry Smith 9591b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 96bdad233fSMatthew Knepley 97bdad233fSMatthew Knepley Level: beginner 98bdad233fSMatthew Knepley 99bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 100bdad233fSMatthew Knepley 101a313700dSBarry Smith .seealso: TSGetType() 102bdad233fSMatthew Knepley @*/ 1037087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 104bdad233fSMatthew Knepley { 105*bc952696SBarry Smith PetscBool opt,flg,tflg; 106dfbe8321SBarry Smith PetscErrorCode ierr; 107649052a6SBarry Smith PetscViewer monviewer; 108eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 109089b2837SJed Brown SNES snes; 1101c3436cfSJed Brown TSAdapt adapt; 11131748224SBarry Smith PetscReal time_step; 11249354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 113d1212d36SBarry Smith char dir[16]; 114bdad233fSMatthew Knepley 115bdad233fSMatthew Knepley PetscFunctionBegin; 1160700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1173194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 118a43b19c4SJed Brown /* Handle TS type options */ 119e55864a3SBarry Smith ierr = TSSetTypeFromOptions_Private(PetscOptionsObject,ts);CHKERRQ(ierr); 120bdad233fSMatthew Knepley 121bdad233fSMatthew Knepley /* Handle generic TS options */ 122*bc952696SBarry Smith if (ts->trajectory) flg = PETSC_TRUE; 123*bc952696SBarry Smith else flg = PETSC_FALSE; 124*bc952696SBarry Smith ierr = PetscOptionsBool("-ts_save_trajectories","Checkpoint for adjoint sensitivity analysis","TSSetSaveTrajectories",tflg,&tflg,NULL);CHKERRQ(ierr); 125*bc952696SBarry Smith if (tflg) {ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);} 1260298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1270298fd71SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 1280298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 12931748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 13031748224SBarry Smith if (flg) { 13131748224SBarry Smith ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 13231748224SBarry Smith } 13349354f04SShri 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); 13449354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 1350298fd71SBarry 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); 1360298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 1370298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 1380298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 1390298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 140bdad233fSMatthew Knepley 14156f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 14256f85f32SBarry Smith { 14356f85f32SBarry Smith PetscBool set; 14456f85f32SBarry Smith flg = PETSC_FALSE; 14556f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 14656f85f32SBarry Smith if (set) { 14756f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 14856f85f32SBarry Smith } 14956f85f32SBarry Smith } 15056f85f32SBarry Smith #endif 15156f85f32SBarry Smith 152bdad233fSMatthew Knepley /* Monitor options */ 153a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 154eabae89aSBarry Smith if (flg) { 155ce94432eSBarry Smith ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr); 156649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 157bdad233fSMatthew Knepley } 1585180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1595180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1605180491cSLisandro Dalcin 1614f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 162a7cc72afSBarry Smith if (opt) { 1630b039ecaSBarry Smith TSMonitorLGCtx ctx; 1643923b477SBarry Smith PetscInt howoften = 1; 165a80ad3e0SBarry Smith 1660298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 167ce94432eSBarry Smith ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 1684f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 169b3603a34SBarry Smith } 1704f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 171b3603a34SBarry Smith if (opt) { 1720b039ecaSBarry Smith TSMonitorLGCtx ctx; 1733923b477SBarry Smith PetscInt howoften = 1; 174b3603a34SBarry Smith 1750298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 17622d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1774f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 178bdad233fSMatthew Knepley } 1794f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 180ef20d060SBarry Smith if (opt) { 1810b039ecaSBarry Smith TSMonitorLGCtx ctx; 1823923b477SBarry Smith PetscInt howoften = 1; 183ef20d060SBarry Smith 1840298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 18522d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1864f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 187ef20d060SBarry Smith } 188201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 189201da799SBarry Smith if (opt) { 190201da799SBarry Smith TSMonitorLGCtx ctx; 191201da799SBarry Smith PetscInt howoften = 1; 192201da799SBarry Smith 1930298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 19422d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 195201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 196201da799SBarry Smith } 197201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 198201da799SBarry Smith if (opt) { 199201da799SBarry Smith TSMonitorLGCtx ctx; 200201da799SBarry Smith PetscInt howoften = 1; 201201da799SBarry Smith 2020298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 20322d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 204201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 205201da799SBarry Smith } 2068189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 2078189c53fSBarry Smith if (opt) { 2088189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2098189c53fSBarry Smith PetscInt howoften = 1; 2108189c53fSBarry Smith 2110298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 21222d28d08SBarry Smith ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 2138189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 2148189c53fSBarry Smith } 215ef20d060SBarry Smith opt = PETSC_FALSE; 2160dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 217a7cc72afSBarry Smith if (opt) { 21883a4ac43SBarry Smith TSMonitorDrawCtx ctx; 21983a4ac43SBarry Smith PetscInt howoften = 1; 220a80ad3e0SBarry Smith 2210298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 222ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 22383a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 224bdad233fSMatthew Knepley } 225fb1732b5SBarry Smith opt = PETSC_FALSE; 2262d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 2272d5ee99bSBarry Smith if (opt) { 2282d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2292d5ee99bSBarry Smith PetscReal bounds[4]; 2302d5ee99bSBarry Smith PetscInt n = 4; 2312d5ee99bSBarry Smith PetscDraw draw; 2322d5ee99bSBarry Smith 2332d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 2342d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2352d5ee99bSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr); 2362d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 2372d5ee99bSBarry Smith ierr = PetscDrawClear(draw);CHKERRQ(ierr); 2384b363babSBarry Smith ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr); 2394b363babSBarry Smith ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 240f54adfc0SBarry Smith ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 2414b363babSBarry Smith ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr); 24207995780SPeter Brune /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */ 2432d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2442d5ee99bSBarry Smith } 2452d5ee99bSBarry Smith opt = PETSC_FALSE; 2460dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 2473a471f94SBarry Smith if (opt) { 24883a4ac43SBarry Smith TSMonitorDrawCtx ctx; 24983a4ac43SBarry Smith PetscInt howoften = 1; 2503a471f94SBarry Smith 2510298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 252ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 25383a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2543a471f94SBarry Smith } 2553a471f94SBarry Smith opt = PETSC_FALSE; 25691b97e58SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 257fb1732b5SBarry Smith if (flg) { 258fb1732b5SBarry Smith PetscViewer ctx; 259fb1732b5SBarry Smith if (monfilename[0]) { 260ce94432eSBarry Smith ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 261c2fbc07fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 262fb1732b5SBarry Smith } else { 263ce94432eSBarry Smith ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts)); 2640298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr); 265fb1732b5SBarry Smith } 266fb1732b5SBarry Smith } 267ed81e22dSJed Brown opt = PETSC_FALSE; 26891b97e58SBarry 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); 269ed81e22dSJed Brown if (flg) { 270ed81e22dSJed Brown const char *ptr,*ptr2; 271ed81e22dSJed Brown char *filetemplate; 272ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 273ed81e22dSJed Brown /* Do some cursory validation of the input. */ 274ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 275ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 276ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 277ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 278ce94432eSBarry 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"); 279ed81e22dSJed Brown if (ptr2) break; 280ed81e22dSJed Brown } 281ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 282ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 283ed81e22dSJed Brown } 284bdad233fSMatthew Knepley 285d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 286d1212d36SBarry Smith if (flg) { 287d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 288d1212d36SBarry Smith int ray = 0; 289d1212d36SBarry Smith DMDADirection ddir; 290d1212d36SBarry Smith DM da; 291d1212d36SBarry Smith PetscMPIInt rank; 292d1212d36SBarry Smith 293ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 294d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 295d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 296ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 297d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 298d1212d36SBarry Smith 299d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 300b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 301d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 302d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 303ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 304d1212d36SBarry Smith if (!rank) { 305d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 306d1212d36SBarry Smith } 30751b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 308d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 309d1212d36SBarry Smith } 31051b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 31151b4a12fSMatthew G. Knepley if (flg) { 31251b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 31351b4a12fSMatthew G. Knepley int ray = 0; 31451b4a12fSMatthew G. Knepley DMDADirection ddir; 31551b4a12fSMatthew G. Knepley DM da; 31651b4a12fSMatthew G. Knepley PetscInt howoften = 1; 317d1212d36SBarry Smith 31851b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 31951b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 32051b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 32151b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 32251b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 32351b4a12fSMatthew G. Knepley 32451b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 325b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 32651b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 32751b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 32851b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 32951b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 33051b4a12fSMatthew G. Knepley } 331a7a1495cSBarry Smith 3327781c08eSBarry Smith /* 3337781c08eSBarry Smith This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui 3347781c08eSBarry Smith will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin() 3357781c08eSBarry Smith */ 336552698daSJed Brown ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr); 337e55864a3SBarry Smith ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr); 338d763cef2SBarry Smith 339d763cef2SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 340d763cef2SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 341d763cef2SBarry Smith 342*bc952696SBarry Smith if (ts->trajectory) { 343*bc952696SBarry Smith ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr); 344*bc952696SBarry Smith } 345*bc952696SBarry Smith 346d763cef2SBarry Smith /* Handle specific TS options */ 347d763cef2SBarry Smith if (ts->ops->setfromoptions) { 348e55864a3SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 349d763cef2SBarry Smith } 350d763cef2SBarry Smith 351d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 352d763cef2SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 353d763cef2SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 354d763cef2SBarry Smith PetscFunctionReturn(0); 355d763cef2SBarry Smith } 356d763cef2SBarry Smith 357d763cef2SBarry Smith #undef __FUNCT__ 358*bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory" 359d2daff3dSHong Zhang /*@ 360*bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 361d2daff3dSHong Zhang 362d2daff3dSHong Zhang Collective on TS 363d2daff3dSHong Zhang 364d2daff3dSHong Zhang Input Parameters: 365*bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 366*bc952696SBarry Smith 367d2daff3dSHong Zhang 368d2daff3dSHong Zhang Level: intermediate 369d2daff3dSHong Zhang 370*bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve() 371d2daff3dSHong Zhang 372*bc952696SBarry Smith .keywords: TS, set, checkpoint, 373d2daff3dSHong Zhang @*/ 374*bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 375d2daff3dSHong Zhang { 376*bc952696SBarry Smith PetscErrorCode ierr; 377*bc952696SBarry Smith 378d2daff3dSHong Zhang PetscFunctionBegin; 379d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 380*bc952696SBarry Smith if (!ts->trajectory) { 381*bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 382*bc952696SBarry Smith /* should it set a default trajectory? */ 383*bc952696SBarry Smith } 384d2daff3dSHong Zhang PetscFunctionReturn(0); 385d2daff3dSHong Zhang } 386d2daff3dSHong Zhang 387d2daff3dSHong Zhang #undef __FUNCT__ 388a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian" 389a7a1495cSBarry Smith /*@ 390a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 391a7a1495cSBarry Smith set with TSSetRHSJacobian(). 392a7a1495cSBarry Smith 393a7a1495cSBarry Smith Collective on TS and Vec 394a7a1495cSBarry Smith 395a7a1495cSBarry Smith Input Parameters: 396316643e7SJed Brown + ts - the TS context 397a7a1495cSBarry Smith . t - current timestep 3980910c330SBarry Smith - U - input vector 399a7a1495cSBarry Smith 400a7a1495cSBarry Smith Output Parameters: 401a7a1495cSBarry Smith + A - Jacobian matrix 402a7a1495cSBarry Smith . B - optional preconditioning matrix 403a7a1495cSBarry Smith - flag - flag indicating matrix structure 404a7a1495cSBarry Smith 405a7a1495cSBarry Smith Notes: 406a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 407a7a1495cSBarry Smith is used internally within the nonlinear solvers. 408a7a1495cSBarry Smith 40994b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 410a7a1495cSBarry Smith flag parameter. 411a7a1495cSBarry Smith 412a7a1495cSBarry Smith Level: developer 413a7a1495cSBarry Smith 414a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 415a7a1495cSBarry Smith 41694b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 417a7a1495cSBarry Smith @*/ 418d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 419a7a1495cSBarry Smith { 420dfbe8321SBarry Smith PetscErrorCode ierr; 421270bf2e7SJed Brown PetscObjectState Ustate; 42224989b8cSPeter Brune DM dm; 423942e3340SBarry Smith DMTS tsdm; 42424989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 42524989b8cSPeter Brune void *ctx; 42624989b8cSPeter Brune TSIJacobian ijacobianfunc; 427a7a1495cSBarry Smith 428a7a1495cSBarry Smith PetscFunctionBegin; 4290700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4300910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 4310910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 43224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 433942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 43424989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 4350298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 43659e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 4370910c330SBarry Smith if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) { 4380e4ef248SJed Brown PetscFunctionReturn(0); 439f8ede8e7SJed Brown } 440d90be118SSean Farley 441ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 442d90be118SSean Farley 443e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 44494ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 44594ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 44694ab13aaSBarry Smith if (A != B) { 44794ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 44894ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 449e1244c69SJed Brown } 450e1244c69SJed Brown ts->rhsjacobian.shift = 0; 451e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 452e1244c69SJed Brown } 453e1244c69SJed Brown 45424989b8cSPeter Brune if (rhsjacobianfunc) { 45594ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 456a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 457d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 458a7a1495cSBarry Smith PetscStackPop; 45994ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 460a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 46194ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,4); 46294ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,5); 463ef66eb69SBarry Smith } else { 46494ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 46594ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 466ef66eb69SBarry Smith } 4670e4ef248SJed Brown ts->rhsjacobian.time = t; 4680910c330SBarry Smith ts->rhsjacobian.X = U; 46959e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 470a7a1495cSBarry Smith PetscFunctionReturn(0); 471a7a1495cSBarry Smith } 472a7a1495cSBarry Smith 4734a2ae208SSatish Balay #undef __FUNCT__ 4744a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 475316643e7SJed Brown /*@ 476d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 477d763cef2SBarry Smith 478316643e7SJed Brown Collective on TS and Vec 479316643e7SJed Brown 480316643e7SJed Brown Input Parameters: 481316643e7SJed Brown + ts - the TS context 482316643e7SJed Brown . t - current time 4830910c330SBarry Smith - U - state vector 484316643e7SJed Brown 485316643e7SJed Brown Output Parameter: 486316643e7SJed Brown . y - right hand side 487316643e7SJed Brown 488316643e7SJed Brown Note: 489316643e7SJed Brown Most users should not need to explicitly call this routine, as it 490316643e7SJed Brown is used internally within the nonlinear solvers. 491316643e7SJed Brown 492316643e7SJed Brown Level: developer 493316643e7SJed Brown 494316643e7SJed Brown .keywords: TS, compute 495316643e7SJed Brown 496316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 497316643e7SJed Brown @*/ 4980910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 499d763cef2SBarry Smith { 500dfbe8321SBarry Smith PetscErrorCode ierr; 50124989b8cSPeter Brune TSRHSFunction rhsfunction; 50224989b8cSPeter Brune TSIFunction ifunction; 50324989b8cSPeter Brune void *ctx; 50424989b8cSPeter Brune DM dm; 50524989b8cSPeter Brune 506d763cef2SBarry Smith PetscFunctionBegin; 5070700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5080910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5090700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 51024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 51124989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 5120298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 513d763cef2SBarry Smith 514ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 515d763cef2SBarry Smith 5160910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 51724989b8cSPeter Brune if (rhsfunction) { 518d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 5190910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 520d763cef2SBarry Smith PetscStackPop; 521214bc6a2SJed Brown } else { 522214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 523b2cd27e8SJed Brown } 52444a41b28SSean Farley 5250910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 526d763cef2SBarry Smith PetscFunctionReturn(0); 527d763cef2SBarry Smith } 528d763cef2SBarry Smith 5294a2ae208SSatish Balay #undef __FUNCT__ 530ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 531ef20d060SBarry Smith /*@ 532ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 533ef20d060SBarry Smith 534ef20d060SBarry Smith Collective on TS and Vec 535ef20d060SBarry Smith 536ef20d060SBarry Smith Input Parameters: 537ef20d060SBarry Smith + ts - the TS context 538ef20d060SBarry Smith - t - current time 539ef20d060SBarry Smith 540ef20d060SBarry Smith Output Parameter: 5410910c330SBarry Smith . U - the solution 542ef20d060SBarry Smith 543ef20d060SBarry Smith Note: 544ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 545ef20d060SBarry Smith is used internally within the nonlinear solvers. 546ef20d060SBarry Smith 547ef20d060SBarry Smith Level: developer 548ef20d060SBarry Smith 549ef20d060SBarry Smith .keywords: TS, compute 550ef20d060SBarry Smith 551abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 552ef20d060SBarry Smith @*/ 5530910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 554ef20d060SBarry Smith { 555ef20d060SBarry Smith PetscErrorCode ierr; 556ef20d060SBarry Smith TSSolutionFunction solutionfunction; 557ef20d060SBarry Smith void *ctx; 558ef20d060SBarry Smith DM dm; 559ef20d060SBarry Smith 560ef20d060SBarry Smith PetscFunctionBegin; 561ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5620910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 563ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 564ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 565ef20d060SBarry Smith 566ef20d060SBarry Smith if (solutionfunction) { 5679b7cd975SBarry Smith PetscStackPush("TS user solution function"); 5680910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 569ef20d060SBarry Smith PetscStackPop; 570ef20d060SBarry Smith } 571ef20d060SBarry Smith PetscFunctionReturn(0); 572ef20d060SBarry Smith } 5739b7cd975SBarry Smith #undef __FUNCT__ 5749b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction" 5759b7cd975SBarry Smith /*@ 5769b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 5779b7cd975SBarry Smith 5789b7cd975SBarry Smith Collective on TS and Vec 5799b7cd975SBarry Smith 5809b7cd975SBarry Smith Input Parameters: 5819b7cd975SBarry Smith + ts - the TS context 5829b7cd975SBarry Smith - t - current time 5839b7cd975SBarry Smith 5849b7cd975SBarry Smith Output Parameter: 5859b7cd975SBarry Smith . U - the function value 5869b7cd975SBarry Smith 5879b7cd975SBarry Smith Note: 5889b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 5899b7cd975SBarry Smith is used internally within the nonlinear solvers. 5909b7cd975SBarry Smith 5919b7cd975SBarry Smith Level: developer 5929b7cd975SBarry Smith 5939b7cd975SBarry Smith .keywords: TS, compute 5949b7cd975SBarry Smith 5959b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 5969b7cd975SBarry Smith @*/ 5979b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 5989b7cd975SBarry Smith { 5999b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 6009b7cd975SBarry Smith void *ctx; 6019b7cd975SBarry Smith DM dm; 6029b7cd975SBarry Smith 6039b7cd975SBarry Smith PetscFunctionBegin; 6049b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6059b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6069b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 6079b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 6089b7cd975SBarry Smith 6099b7cd975SBarry Smith if (forcing) { 6109b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 6119b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 6129b7cd975SBarry Smith PetscStackPop; 6139b7cd975SBarry Smith } 6149b7cd975SBarry Smith PetscFunctionReturn(0); 6159b7cd975SBarry Smith } 616ef20d060SBarry Smith 617ef20d060SBarry Smith #undef __FUNCT__ 618214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 619214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 620214bc6a2SJed Brown { 6212dd45cf8SJed Brown Vec F; 622214bc6a2SJed Brown PetscErrorCode ierr; 623214bc6a2SJed Brown 624214bc6a2SJed Brown PetscFunctionBegin; 6250298fd71SBarry Smith *Frhs = NULL; 6260298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 627214bc6a2SJed Brown if (!ts->Frhs) { 6282dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 629214bc6a2SJed Brown } 630214bc6a2SJed Brown *Frhs = ts->Frhs; 631214bc6a2SJed Brown PetscFunctionReturn(0); 632214bc6a2SJed Brown } 633214bc6a2SJed Brown 634214bc6a2SJed Brown #undef __FUNCT__ 635214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 636214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 637214bc6a2SJed Brown { 638214bc6a2SJed Brown Mat A,B; 6392dd45cf8SJed Brown PetscErrorCode ierr; 640214bc6a2SJed Brown 641214bc6a2SJed Brown PetscFunctionBegin; 642c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 643c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 6440298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 645214bc6a2SJed Brown if (Arhs) { 646214bc6a2SJed Brown if (!ts->Arhs) { 647214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 648214bc6a2SJed Brown } 649214bc6a2SJed Brown *Arhs = ts->Arhs; 650214bc6a2SJed Brown } 651214bc6a2SJed Brown if (Brhs) { 652214bc6a2SJed Brown if (!ts->Brhs) { 653bdb70873SJed Brown if (A != B) { 654214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 655bdb70873SJed Brown } else { 656bdb70873SJed Brown ts->Brhs = ts->Arhs; 657bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 658bdb70873SJed Brown } 659214bc6a2SJed Brown } 660214bc6a2SJed Brown *Brhs = ts->Brhs; 661214bc6a2SJed Brown } 662214bc6a2SJed Brown PetscFunctionReturn(0); 663214bc6a2SJed Brown } 664214bc6a2SJed Brown 665214bc6a2SJed Brown #undef __FUNCT__ 666316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 667316643e7SJed Brown /*@ 6680910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 669316643e7SJed Brown 670316643e7SJed Brown Collective on TS and Vec 671316643e7SJed Brown 672316643e7SJed Brown Input Parameters: 673316643e7SJed Brown + ts - the TS context 674316643e7SJed Brown . t - current time 6750910c330SBarry Smith . U - state vector 6760910c330SBarry Smith . Udot - time derivative of state vector 677214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 678316643e7SJed Brown 679316643e7SJed Brown Output Parameter: 680316643e7SJed Brown . Y - right hand side 681316643e7SJed Brown 682316643e7SJed Brown Note: 683316643e7SJed Brown Most users should not need to explicitly call this routine, as it 684316643e7SJed Brown is used internally within the nonlinear solvers. 685316643e7SJed Brown 686316643e7SJed Brown If the user did did not write their equations in implicit form, this 687316643e7SJed Brown function recasts them in implicit form. 688316643e7SJed Brown 689316643e7SJed Brown Level: developer 690316643e7SJed Brown 691316643e7SJed Brown .keywords: TS, compute 692316643e7SJed Brown 693316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 694316643e7SJed Brown @*/ 6950910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 696316643e7SJed Brown { 697316643e7SJed Brown PetscErrorCode ierr; 69824989b8cSPeter Brune TSIFunction ifunction; 69924989b8cSPeter Brune TSRHSFunction rhsfunction; 70024989b8cSPeter Brune void *ctx; 70124989b8cSPeter Brune DM dm; 702316643e7SJed Brown 703316643e7SJed Brown PetscFunctionBegin; 7040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7050910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7060910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 7070700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 708316643e7SJed Brown 70924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 71024989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 7110298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 71224989b8cSPeter Brune 713ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 714d90be118SSean Farley 7150910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 71624989b8cSPeter Brune if (ifunction) { 717316643e7SJed Brown PetscStackPush("TS user implicit function"); 7180910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 719316643e7SJed Brown PetscStackPop; 720214bc6a2SJed Brown } 721214bc6a2SJed Brown if (imex) { 72224989b8cSPeter Brune if (!ifunction) { 7230910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 7242dd45cf8SJed Brown } 72524989b8cSPeter Brune } else if (rhsfunction) { 72624989b8cSPeter Brune if (ifunction) { 727214bc6a2SJed Brown Vec Frhs; 728214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 7290910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 730214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 7312dd45cf8SJed Brown } else { 7320910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 7330910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 734316643e7SJed Brown } 7354a6899ffSJed Brown } 7360910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 737316643e7SJed Brown PetscFunctionReturn(0); 738316643e7SJed Brown } 739316643e7SJed Brown 740316643e7SJed Brown #undef __FUNCT__ 741316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 742316643e7SJed Brown /*@ 743316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 744316643e7SJed Brown 745316643e7SJed Brown Collective on TS and Vec 746316643e7SJed Brown 747316643e7SJed Brown Input 748316643e7SJed Brown Input Parameters: 749316643e7SJed Brown + ts - the TS context 750316643e7SJed Brown . t - current timestep 7510910c330SBarry Smith . U - state vector 7520910c330SBarry Smith . Udot - time derivative of state vector 753214bc6a2SJed Brown . shift - shift to apply, see note below 754214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 755316643e7SJed Brown 756316643e7SJed Brown Output Parameters: 757316643e7SJed Brown + A - Jacobian matrix 758316643e7SJed Brown . B - optional preconditioning matrix 759316643e7SJed Brown - flag - flag indicating matrix structure 760316643e7SJed Brown 761316643e7SJed Brown Notes: 7620910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 763316643e7SJed Brown 7640910c330SBarry Smith dF/dU + shift*dF/dUdot 765316643e7SJed Brown 766316643e7SJed Brown Most users should not need to explicitly call this routine, as it 767316643e7SJed Brown is used internally within the nonlinear solvers. 768316643e7SJed Brown 769316643e7SJed Brown Level: developer 770316643e7SJed Brown 771316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 772316643e7SJed Brown 773316643e7SJed Brown .seealso: TSSetIJacobian() 77463495f91SJed Brown @*/ 775d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 776316643e7SJed Brown { 777316643e7SJed Brown PetscErrorCode ierr; 77824989b8cSPeter Brune TSIJacobian ijacobian; 77924989b8cSPeter Brune TSRHSJacobian rhsjacobian; 78024989b8cSPeter Brune DM dm; 78124989b8cSPeter Brune void *ctx; 782316643e7SJed Brown 783316643e7SJed Brown PetscFunctionBegin; 7840700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7850910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7860910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 787316643e7SJed Brown PetscValidPointer(A,6); 78894ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 789316643e7SJed Brown PetscValidPointer(B,7); 79094ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 79124989b8cSPeter Brune 79224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 79324989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 7940298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 79524989b8cSPeter Brune 796ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 797316643e7SJed Brown 79894ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 79924989b8cSPeter Brune if (ijacobian) { 800316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 801d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 802316643e7SJed Brown PetscStackPop; 803214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 80494ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,4); 80594ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,5); 8064a6899ffSJed Brown } 807214bc6a2SJed Brown if (imex) { 808b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 80994ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 81094ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 81194ab13aaSBarry Smith if (A != B) { 81294ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 81394ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 814214bc6a2SJed Brown } 815214bc6a2SJed Brown } 816214bc6a2SJed Brown } else { 817e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 818e1244c69SJed Brown if (rhsjacobian) { 819bd373395SBarry Smith if (ijacobian) { 820e1244c69SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 821bd373395SBarry Smith } else { 822bd373395SBarry Smith ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr); 823e1244c69SJed Brown } 824d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 825e1244c69SJed Brown } 82694ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 827e1244c69SJed Brown ts->rhsjacobian.scale = -1; 828e1244c69SJed Brown ts->rhsjacobian.shift = shift; 82994ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 83094ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 83194ab13aaSBarry Smith if (A != B) { 83294ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 83394ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 834316643e7SJed Brown } 835e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 836e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 837e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 83894ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 83994ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 84094ab13aaSBarry Smith if (A != B) { 84194ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 84294ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 843e1244c69SJed Brown } 844e1244c69SJed Brown } 84594ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 84694ab13aaSBarry Smith if (A != B) { 84794ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 848214bc6a2SJed Brown } 849316643e7SJed Brown } 850316643e7SJed Brown } 85194ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 852316643e7SJed Brown PetscFunctionReturn(0); 853316643e7SJed Brown } 854316643e7SJed Brown 855316643e7SJed Brown #undef __FUNCT__ 8564a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 857d763cef2SBarry Smith /*@C 858d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 859b5abc632SBarry Smith where U_t = G(t,u). 860d763cef2SBarry Smith 8613f9fe445SBarry Smith Logically Collective on TS 862d763cef2SBarry Smith 863d763cef2SBarry Smith Input Parameters: 864d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 8650298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 866d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 867d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 8680298fd71SBarry Smith function evaluation routine (may be NULL) 869d763cef2SBarry Smith 870d763cef2SBarry Smith Calling sequence of func: 87187828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 872d763cef2SBarry Smith 873d763cef2SBarry Smith + t - current timestep 874d763cef2SBarry Smith . u - input vector 875d763cef2SBarry Smith . F - function vector 876d763cef2SBarry Smith - ctx - [optional] user-defined function context 877d763cef2SBarry Smith 878d763cef2SBarry Smith Level: beginner 879d763cef2SBarry Smith 880d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 881d763cef2SBarry Smith 882ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 883d763cef2SBarry Smith @*/ 884089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 885d763cef2SBarry Smith { 886089b2837SJed Brown PetscErrorCode ierr; 887089b2837SJed Brown SNES snes; 8880298fd71SBarry Smith Vec ralloc = NULL; 88924989b8cSPeter Brune DM dm; 890d763cef2SBarry Smith 891089b2837SJed Brown PetscFunctionBegin; 8920700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 893ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 89424989b8cSPeter Brune 89524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 89624989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 897089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 898e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 899e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 900e856ceecSJed Brown r = ralloc; 901e856ceecSJed Brown } 902089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 903e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 904d763cef2SBarry Smith PetscFunctionReturn(0); 905d763cef2SBarry Smith } 906d763cef2SBarry Smith 9074a2ae208SSatish Balay #undef __FUNCT__ 908ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 909ef20d060SBarry Smith /*@C 910abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 911ef20d060SBarry Smith 912ef20d060SBarry Smith Logically Collective on TS 913ef20d060SBarry Smith 914ef20d060SBarry Smith Input Parameters: 915ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 916ef20d060SBarry Smith . f - routine for evaluating the solution 917ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 9180298fd71SBarry Smith function evaluation routine (may be NULL) 919ef20d060SBarry Smith 920ef20d060SBarry Smith Calling sequence of func: 921ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 922ef20d060SBarry Smith 923ef20d060SBarry Smith + t - current timestep 924ef20d060SBarry Smith . u - output vector 925ef20d060SBarry Smith - ctx - [optional] user-defined function context 926ef20d060SBarry Smith 927abd5a294SJed Brown Notes: 928abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 929abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 930abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 931abd5a294SJed Brown 9324f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 933abd5a294SJed Brown 934ef20d060SBarry Smith Level: beginner 935ef20d060SBarry Smith 936ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 937ef20d060SBarry Smith 9389b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 939ef20d060SBarry Smith @*/ 940ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 941ef20d060SBarry Smith { 942ef20d060SBarry Smith PetscErrorCode ierr; 943ef20d060SBarry Smith DM dm; 944ef20d060SBarry Smith 945ef20d060SBarry Smith PetscFunctionBegin; 946ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 947ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 948ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 949ef20d060SBarry Smith PetscFunctionReturn(0); 950ef20d060SBarry Smith } 951ef20d060SBarry Smith 952ef20d060SBarry Smith #undef __FUNCT__ 9539b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction" 9549b7cd975SBarry Smith /*@C 9559b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 9569b7cd975SBarry Smith 9579b7cd975SBarry Smith Logically Collective on TS 9589b7cd975SBarry Smith 9599b7cd975SBarry Smith Input Parameters: 9609b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 9619b7cd975SBarry Smith . f - routine for evaluating the forcing function 9629b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 9630298fd71SBarry Smith function evaluation routine (may be NULL) 9649b7cd975SBarry Smith 9659b7cd975SBarry Smith Calling sequence of func: 9669b7cd975SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 9679b7cd975SBarry Smith 9689b7cd975SBarry Smith + t - current timestep 9699b7cd975SBarry Smith . u - output vector 9709b7cd975SBarry Smith - ctx - [optional] user-defined function context 9719b7cd975SBarry Smith 9729b7cd975SBarry Smith Notes: 9739b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 9749b7cd975SBarry Smith create closed-form solutions with a non-physical forcing term. 9759b7cd975SBarry Smith 9769b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 9779b7cd975SBarry Smith 9789b7cd975SBarry Smith Level: beginner 9799b7cd975SBarry Smith 9809b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 9819b7cd975SBarry Smith 9829b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 9839b7cd975SBarry Smith @*/ 9849b7cd975SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 9859b7cd975SBarry Smith { 9869b7cd975SBarry Smith PetscErrorCode ierr; 9879b7cd975SBarry Smith DM dm; 9889b7cd975SBarry Smith 9899b7cd975SBarry Smith PetscFunctionBegin; 9909b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9919b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 9929b7cd975SBarry Smith ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr); 9939b7cd975SBarry Smith PetscFunctionReturn(0); 9949b7cd975SBarry Smith } 9959b7cd975SBarry Smith 9969b7cd975SBarry Smith #undef __FUNCT__ 9974a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 998d763cef2SBarry Smith /*@C 999d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 1000b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1001d763cef2SBarry Smith 10023f9fe445SBarry Smith Logically Collective on TS 1003d763cef2SBarry Smith 1004d763cef2SBarry Smith Input Parameters: 1005d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1006e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1007e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1008d763cef2SBarry Smith . f - the Jacobian evaluation routine 1009d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 10100298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1011d763cef2SBarry Smith 1012d763cef2SBarry Smith Calling sequence of func: 1013ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1014d763cef2SBarry Smith 1015d763cef2SBarry Smith + t - current timestep 1016d763cef2SBarry Smith . u - input vector 1017e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1018e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1019d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1020d763cef2SBarry Smith 1021d763cef2SBarry Smith 1022d763cef2SBarry Smith Level: beginner 1023d763cef2SBarry Smith 1024d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1025d763cef2SBarry Smith 1026ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1027d763cef2SBarry Smith 1028d763cef2SBarry Smith @*/ 1029e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1030d763cef2SBarry Smith { 1031277b19d0SLisandro Dalcin PetscErrorCode ierr; 1032089b2837SJed Brown SNES snes; 103324989b8cSPeter Brune DM dm; 103424989b8cSPeter Brune TSIJacobian ijacobian; 1035277b19d0SLisandro Dalcin 1036d763cef2SBarry Smith PetscFunctionBegin; 10370700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1038e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1039e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1040e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1041e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1042d763cef2SBarry Smith 104324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 104424989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1045e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1046e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1047e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1048e1244c69SJed Brown } 10490298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1050089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 10515f659677SPeter Brune if (!ijacobian) { 1052e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 10530e4ef248SJed Brown } 1054e5d3d808SBarry Smith if (Amat) { 1055e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 10560e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1057bbd56ea5SKarl Rupp 1058e5d3d808SBarry Smith ts->Arhs = Amat; 10590e4ef248SJed Brown } 1060e5d3d808SBarry Smith if (Pmat) { 1061e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 10620e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1063bbd56ea5SKarl Rupp 1064e5d3d808SBarry Smith ts->Brhs = Pmat; 10650e4ef248SJed Brown } 1066d763cef2SBarry Smith PetscFunctionReturn(0); 1067d763cef2SBarry Smith } 1068d763cef2SBarry Smith 1069316643e7SJed Brown 1070316643e7SJed Brown #undef __FUNCT__ 1071316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 1072316643e7SJed Brown /*@C 1073b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1074316643e7SJed Brown 10753f9fe445SBarry Smith Logically Collective on TS 1076316643e7SJed Brown 1077316643e7SJed Brown Input Parameters: 1078316643e7SJed Brown + ts - the TS context obtained from TSCreate() 10790298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1080316643e7SJed Brown . f - the function evaluation routine 10810298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1082316643e7SJed Brown 1083316643e7SJed Brown Calling sequence of f: 1084316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1085316643e7SJed Brown 1086316643e7SJed Brown + t - time at step/stage being solved 1087316643e7SJed Brown . u - state vector 1088316643e7SJed Brown . u_t - time derivative of state vector 1089316643e7SJed Brown . F - function vector 1090316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1091316643e7SJed Brown 1092316643e7SJed Brown Important: 1093d6cbdb99SBarry Smith The user MUST call either this routine, TSSetRHSFunction(). This routine must be used when not solving an ODE, for example a DAE. 1094316643e7SJed Brown 1095316643e7SJed Brown Level: beginner 1096316643e7SJed Brown 1097316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1098316643e7SJed Brown 1099d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1100316643e7SJed Brown @*/ 1101089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 1102316643e7SJed Brown { 1103089b2837SJed Brown PetscErrorCode ierr; 1104089b2837SJed Brown SNES snes; 11050298fd71SBarry Smith Vec resalloc = NULL; 110624989b8cSPeter Brune DM dm; 1107316643e7SJed Brown 1108316643e7SJed Brown PetscFunctionBegin; 11090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1110ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 111124989b8cSPeter Brune 111224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 111324989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 111424989b8cSPeter Brune 1115089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1116e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 1117e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 1118e856ceecSJed Brown res = resalloc; 1119e856ceecSJed Brown } 1120089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 1121e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 1122089b2837SJed Brown PetscFunctionReturn(0); 1123089b2837SJed Brown } 1124089b2837SJed Brown 1125089b2837SJed Brown #undef __FUNCT__ 1126089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 1127089b2837SJed Brown /*@C 1128089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1129089b2837SJed Brown 1130089b2837SJed Brown Not Collective 1131089b2837SJed Brown 1132089b2837SJed Brown Input Parameter: 1133089b2837SJed Brown . ts - the TS context 1134089b2837SJed Brown 1135089b2837SJed Brown Output Parameter: 11360298fd71SBarry Smith + r - vector to hold residual (or NULL) 11370298fd71SBarry Smith . func - the function to compute residual (or NULL) 11380298fd71SBarry Smith - ctx - the function context (or NULL) 1139089b2837SJed Brown 1140089b2837SJed Brown Level: advanced 1141089b2837SJed Brown 1142089b2837SJed Brown .keywords: TS, nonlinear, get, function 1143089b2837SJed Brown 1144089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1145089b2837SJed Brown @*/ 1146089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1147089b2837SJed Brown { 1148089b2837SJed Brown PetscErrorCode ierr; 1149089b2837SJed Brown SNES snes; 115024989b8cSPeter Brune DM dm; 1151089b2837SJed Brown 1152089b2837SJed Brown PetscFunctionBegin; 1153089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1154089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11550298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 115624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 115724989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1158089b2837SJed Brown PetscFunctionReturn(0); 1159089b2837SJed Brown } 1160089b2837SJed Brown 1161089b2837SJed Brown #undef __FUNCT__ 1162089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 1163089b2837SJed Brown /*@C 1164089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1165089b2837SJed Brown 1166089b2837SJed Brown Not Collective 1167089b2837SJed Brown 1168089b2837SJed Brown Input Parameter: 1169089b2837SJed Brown . ts - the TS context 1170089b2837SJed Brown 1171089b2837SJed Brown Output Parameter: 11720298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 11730298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 11740298fd71SBarry Smith - ctx - the function context (or NULL) 1175089b2837SJed Brown 1176089b2837SJed Brown Level: advanced 1177089b2837SJed Brown 1178089b2837SJed Brown .keywords: TS, nonlinear, get, function 1179089b2837SJed Brown 1180089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 1181089b2837SJed Brown @*/ 1182089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1183089b2837SJed Brown { 1184089b2837SJed Brown PetscErrorCode ierr; 1185089b2837SJed Brown SNES snes; 118624989b8cSPeter Brune DM dm; 1187089b2837SJed Brown 1188089b2837SJed Brown PetscFunctionBegin; 1189089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1190089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11910298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 119224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 119324989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1194316643e7SJed Brown PetscFunctionReturn(0); 1195316643e7SJed Brown } 1196316643e7SJed Brown 1197316643e7SJed Brown #undef __FUNCT__ 1198316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 1199316643e7SJed Brown /*@C 1200a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1201ae8867d6SBarry Smith provided with TSSetIFunction(). 1202316643e7SJed Brown 12033f9fe445SBarry Smith Logically Collective on TS 1204316643e7SJed Brown 1205316643e7SJed Brown Input Parameters: 1206316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1207e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1208e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1209316643e7SJed Brown . f - the Jacobian evaluation routine 12100298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1211316643e7SJed Brown 1212316643e7SJed Brown Calling sequence of f: 1213ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1214316643e7SJed Brown 1215316643e7SJed Brown + t - time at step/stage being solved 12161b4a444bSJed Brown . U - state vector 12171b4a444bSJed Brown . U_t - time derivative of state vector 1218316643e7SJed Brown . a - shift 1219e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1220e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1221316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1222316643e7SJed Brown 1223316643e7SJed Brown Notes: 1224e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1225316643e7SJed Brown 1226a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1227b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1228a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1229a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1230a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1231a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1232a4f0a591SBarry Smith 1233316643e7SJed Brown Level: beginner 1234316643e7SJed Brown 1235316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1236316643e7SJed Brown 1237ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1238316643e7SJed Brown 1239316643e7SJed Brown @*/ 1240e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1241316643e7SJed Brown { 1242316643e7SJed Brown PetscErrorCode ierr; 1243089b2837SJed Brown SNES snes; 124424989b8cSPeter Brune DM dm; 1245316643e7SJed Brown 1246316643e7SJed Brown PetscFunctionBegin; 12470700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1248e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1249e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1250e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1251e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 125224989b8cSPeter Brune 125324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 125424989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 125524989b8cSPeter Brune 1256089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1257e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1258316643e7SJed Brown PetscFunctionReturn(0); 1259316643e7SJed Brown } 1260316643e7SJed Brown 12614a2ae208SSatish Balay #undef __FUNCT__ 1262e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse" 1263e1244c69SJed Brown /*@ 1264e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1265e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1266e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1267e1244c69SJed Brown not been changed by the TS. 1268e1244c69SJed Brown 1269e1244c69SJed Brown Logically Collective 1270e1244c69SJed Brown 1271e1244c69SJed Brown Input Arguments: 1272e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1273e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1274e1244c69SJed Brown 1275e1244c69SJed Brown Level: intermediate 1276e1244c69SJed Brown 1277e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1278e1244c69SJed Brown @*/ 1279e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1280e1244c69SJed Brown { 1281e1244c69SJed Brown PetscFunctionBegin; 1282e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1283e1244c69SJed Brown PetscFunctionReturn(0); 1284e1244c69SJed Brown } 1285e1244c69SJed Brown 1286e1244c69SJed Brown #undef __FUNCT__ 128755849f57SBarry Smith #define __FUNCT__ "TSLoad" 128855849f57SBarry Smith /*@C 128955849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 129055849f57SBarry Smith 129155849f57SBarry Smith Collective on PetscViewer 129255849f57SBarry Smith 129355849f57SBarry Smith Input Parameters: 129455849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 129555849f57SBarry Smith some related function before a call to TSLoad(). 129655849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 129755849f57SBarry Smith 129855849f57SBarry Smith Level: intermediate 129955849f57SBarry Smith 130055849f57SBarry Smith Notes: 130155849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 130255849f57SBarry Smith 130355849f57SBarry Smith Notes for advanced users: 130455849f57SBarry Smith Most users should not need to know the details of the binary storage 130555849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 130655849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 130755849f57SBarry Smith format is 130855849f57SBarry Smith .vb 130955849f57SBarry Smith has not yet been determined 131055849f57SBarry Smith .ve 131155849f57SBarry Smith 131255849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 131355849f57SBarry Smith @*/ 1314f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 131555849f57SBarry Smith { 131655849f57SBarry Smith PetscErrorCode ierr; 131755849f57SBarry Smith PetscBool isbinary; 131855849f57SBarry Smith PetscInt classid; 131955849f57SBarry Smith char type[256]; 13202d53ad75SBarry Smith DMTS sdm; 1321ad6bc421SBarry Smith DM dm; 132255849f57SBarry Smith 132355849f57SBarry Smith PetscFunctionBegin; 1324f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 132555849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 132655849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 132755849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 132855849f57SBarry Smith 132955849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 1330ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 133155849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 1332f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1333f2c2a1b9SBarry Smith if (ts->ops->load) { 1334f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1335f2c2a1b9SBarry Smith } 1336ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1337ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1338ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1339f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1340f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 13412d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 13422d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 134355849f57SBarry Smith PetscFunctionReturn(0); 134455849f57SBarry Smith } 134555849f57SBarry Smith 13469804daf3SBarry Smith #include <petscdraw.h> 1347e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1348e04113cfSBarry Smith #include <petscviewersaws.h> 1349f05ece33SBarry Smith #endif 135055849f57SBarry Smith #undef __FUNCT__ 13514a2ae208SSatish Balay #define __FUNCT__ "TSView" 13527e2c5f70SBarry Smith /*@C 1353d763cef2SBarry Smith TSView - Prints the TS data structure. 1354d763cef2SBarry Smith 13554c49b128SBarry Smith Collective on TS 1356d763cef2SBarry Smith 1357d763cef2SBarry Smith Input Parameters: 1358d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1359d763cef2SBarry Smith - viewer - visualization context 1360d763cef2SBarry Smith 1361d763cef2SBarry Smith Options Database Key: 1362d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1363d763cef2SBarry Smith 1364d763cef2SBarry Smith Notes: 1365d763cef2SBarry Smith The available visualization contexts include 1366b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1367b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1368d763cef2SBarry Smith output where only the first processor opens 1369d763cef2SBarry Smith the file. All other processors send their 1370d763cef2SBarry Smith data to the first processor to print. 1371d763cef2SBarry Smith 1372d763cef2SBarry Smith The user can open an alternative visualization context with 1373b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1374d763cef2SBarry Smith 1375d763cef2SBarry Smith Level: beginner 1376d763cef2SBarry Smith 1377d763cef2SBarry Smith .keywords: TS, timestep, view 1378d763cef2SBarry Smith 1379b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1380d763cef2SBarry Smith @*/ 13817087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1382d763cef2SBarry Smith { 1383dfbe8321SBarry Smith PetscErrorCode ierr; 138419fd82e9SBarry Smith TSType type; 13852b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 13862d53ad75SBarry Smith DMTS sdm; 1387e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1388f05ece33SBarry Smith PetscBool isams; 1389f05ece33SBarry Smith #endif 1390d763cef2SBarry Smith 1391d763cef2SBarry Smith PetscFunctionBegin; 13920700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13933050cee2SBarry Smith if (!viewer) { 1394ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 13953050cee2SBarry Smith } 13960700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1397c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1398fd16b177SBarry Smith 1399251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1400251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 140155849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 14022b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1403e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1404e04113cfSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&isams);CHKERRQ(ierr); 1405f05ece33SBarry Smith #endif 140632077d6dSBarry Smith if (iascii) { 1407dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 140877431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 14097c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1410d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 14115ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1412c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1413d763cef2SBarry Smith } 14145ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1415193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 14162d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 14172d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1418d52bd9f3SBarry Smith if (ts->ops->view) { 1419d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1420d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1421d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1422d52bd9f3SBarry Smith } 14230f5bd95cSBarry Smith } else if (isstring) { 1424a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1425b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 142655849f57SBarry Smith } else if (isbinary) { 142755849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 142855849f57SBarry Smith MPI_Comm comm; 142955849f57SBarry Smith PetscMPIInt rank; 143055849f57SBarry Smith char type[256]; 143155849f57SBarry Smith 143255849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 143355849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 143455849f57SBarry Smith if (!rank) { 143555849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 143655849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 143755849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 143855849f57SBarry Smith } 143955849f57SBarry Smith if (ts->ops->view) { 144055849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 144155849f57SBarry Smith } 1442f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1443f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 14442d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 14452d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 14462b0a91c0SBarry Smith } else if (isdraw) { 14472b0a91c0SBarry Smith PetscDraw draw; 14482b0a91c0SBarry Smith char str[36]; 144989fd9fafSBarry Smith PetscReal x,y,bottom,h; 14502b0a91c0SBarry Smith 14512b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 14522b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 14532b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 14542b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 14550298fd71SBarry Smith ierr = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 145689fd9fafSBarry Smith bottom = y - h; 14572b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 14582b0a91c0SBarry Smith if (ts->ops->view) { 14592b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 14602b0a91c0SBarry Smith } 14612b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1462e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1463f05ece33SBarry Smith } else if (isams) { 1464d45a07a7SBarry Smith PetscMPIInt rank; 14652657e9d9SBarry Smith const char *name; 14662657e9d9SBarry Smith 14672657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1468d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1469d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1470d45a07a7SBarry Smith char dir[1024]; 1471d45a07a7SBarry Smith 1472e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1473a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 14742657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 14752657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 14762657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1477d763cef2SBarry Smith } 14780acecf5bSBarry Smith if (ts->ops->view) { 14790acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 14800acecf5bSBarry Smith } 1481f05ece33SBarry Smith #endif 1482f05ece33SBarry Smith } 1483f05ece33SBarry Smith 1484b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1485251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1486b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1487d763cef2SBarry Smith PetscFunctionReturn(0); 1488d763cef2SBarry Smith } 1489d763cef2SBarry Smith 1490d763cef2SBarry Smith 14914a2ae208SSatish Balay #undef __FUNCT__ 14924a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1493b07ff414SBarry Smith /*@ 1494d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1495d763cef2SBarry Smith the timesteppers. 1496d763cef2SBarry Smith 14973f9fe445SBarry Smith Logically Collective on TS 1498d763cef2SBarry Smith 1499d763cef2SBarry Smith Input Parameters: 1500d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1501d763cef2SBarry Smith - usrP - optional user context 1502d763cef2SBarry Smith 1503d763cef2SBarry Smith Level: intermediate 1504d763cef2SBarry Smith 1505d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1506d763cef2SBarry Smith 1507d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1508d763cef2SBarry Smith @*/ 15097087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1510d763cef2SBarry Smith { 1511d763cef2SBarry Smith PetscFunctionBegin; 15120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1513d763cef2SBarry Smith ts->user = usrP; 1514d763cef2SBarry Smith PetscFunctionReturn(0); 1515d763cef2SBarry Smith } 1516d763cef2SBarry Smith 15174a2ae208SSatish Balay #undef __FUNCT__ 15184a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1519b07ff414SBarry Smith /*@ 1520d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1521d763cef2SBarry Smith timestepper. 1522d763cef2SBarry Smith 1523d763cef2SBarry Smith Not Collective 1524d763cef2SBarry Smith 1525d763cef2SBarry Smith Input Parameter: 1526d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1527d763cef2SBarry Smith 1528d763cef2SBarry Smith Output Parameter: 1529d763cef2SBarry Smith . usrP - user context 1530d763cef2SBarry Smith 1531d763cef2SBarry Smith Level: intermediate 1532d763cef2SBarry Smith 1533d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1534d763cef2SBarry Smith 1535d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1536d763cef2SBarry Smith @*/ 1537e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1538d763cef2SBarry Smith { 1539d763cef2SBarry Smith PetscFunctionBegin; 15400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1541e71120c6SJed Brown *(void**)usrP = ts->user; 1542d763cef2SBarry Smith PetscFunctionReturn(0); 1543d763cef2SBarry Smith } 1544d763cef2SBarry Smith 15454a2ae208SSatish Balay #undef __FUNCT__ 15464a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1547d763cef2SBarry Smith /*@ 1548b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1549d763cef2SBarry Smith 1550d763cef2SBarry Smith Not Collective 1551d763cef2SBarry Smith 1552d763cef2SBarry Smith Input Parameter: 1553d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1554d763cef2SBarry Smith 1555d763cef2SBarry Smith Output Parameter: 1556b8123daeSJed Brown . iter - number of steps completed so far 1557d763cef2SBarry Smith 1558d763cef2SBarry Smith Level: intermediate 1559d763cef2SBarry Smith 1560d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 15619be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 1562d763cef2SBarry Smith @*/ 15637087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 1564d763cef2SBarry Smith { 1565d763cef2SBarry Smith PetscFunctionBegin; 15660700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15674482741eSBarry Smith PetscValidIntPointer(iter,2); 1568d763cef2SBarry Smith *iter = ts->steps; 1569d763cef2SBarry Smith PetscFunctionReturn(0); 1570d763cef2SBarry Smith } 1571d763cef2SBarry Smith 15724a2ae208SSatish Balay #undef __FUNCT__ 15734a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1574d763cef2SBarry Smith /*@ 1575d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1576d763cef2SBarry Smith as well as the initial time. 1577d763cef2SBarry Smith 15783f9fe445SBarry Smith Logically Collective on TS 1579d763cef2SBarry Smith 1580d763cef2SBarry Smith Input Parameters: 1581d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1582d763cef2SBarry Smith . initial_time - the initial time 1583d763cef2SBarry Smith - time_step - the size of the timestep 1584d763cef2SBarry Smith 1585d763cef2SBarry Smith Level: intermediate 1586d763cef2SBarry Smith 1587d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1588d763cef2SBarry Smith 1589d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1590d763cef2SBarry Smith @*/ 15917087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1592d763cef2SBarry Smith { 1593e144a568SJed Brown PetscErrorCode ierr; 1594e144a568SJed Brown 1595d763cef2SBarry Smith PetscFunctionBegin; 15960700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1597e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1598d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1599d763cef2SBarry Smith PetscFunctionReturn(0); 1600d763cef2SBarry Smith } 1601d763cef2SBarry Smith 16024a2ae208SSatish Balay #undef __FUNCT__ 16034a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1604d763cef2SBarry Smith /*@ 1605d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1606d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1607d763cef2SBarry Smith 16083f9fe445SBarry Smith Logically Collective on TS 1609d763cef2SBarry Smith 1610d763cef2SBarry Smith Input Parameters: 1611d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1612d763cef2SBarry Smith - time_step - the size of the timestep 1613d763cef2SBarry Smith 1614d763cef2SBarry Smith Level: intermediate 1615d763cef2SBarry Smith 1616d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1617d763cef2SBarry Smith 1618d763cef2SBarry Smith .keywords: TS, set, timestep 1619d763cef2SBarry Smith @*/ 16207087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1621d763cef2SBarry Smith { 1622d763cef2SBarry Smith PetscFunctionBegin; 16230700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1624c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1625d763cef2SBarry Smith ts->time_step = time_step; 162631748224SBarry Smith ts->time_step_orig = time_step; 1627d763cef2SBarry Smith PetscFunctionReturn(0); 1628d763cef2SBarry Smith } 1629d763cef2SBarry Smith 16304a2ae208SSatish Balay #undef __FUNCT__ 1631a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1632a43b19c4SJed Brown /*@ 163349354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 163449354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 163549354f04SShri Abhyankar or just return at the final time TS computed. 1636a43b19c4SJed Brown 1637a43b19c4SJed Brown Logically Collective on TS 1638a43b19c4SJed Brown 1639a43b19c4SJed Brown Input Parameter: 1640a43b19c4SJed Brown + ts - the time-step context 164149354f04SShri Abhyankar - eftopt - exact final time option 1642a43b19c4SJed Brown 1643a43b19c4SJed Brown Level: beginner 1644a43b19c4SJed Brown 1645a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 1646a43b19c4SJed Brown @*/ 164749354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 1648a43b19c4SJed Brown { 1649a43b19c4SJed Brown PetscFunctionBegin; 1650a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 165149354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 165249354f04SShri Abhyankar ts->exact_final_time = eftopt; 1653a43b19c4SJed Brown PetscFunctionReturn(0); 1654a43b19c4SJed Brown } 1655a43b19c4SJed Brown 1656a43b19c4SJed Brown #undef __FUNCT__ 16574a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1658d763cef2SBarry Smith /*@ 1659d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1660d763cef2SBarry Smith 1661d763cef2SBarry Smith Not Collective 1662d763cef2SBarry Smith 1663d763cef2SBarry Smith Input Parameter: 1664d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1665d763cef2SBarry Smith 1666d763cef2SBarry Smith Output Parameter: 1667d763cef2SBarry Smith . dt - the current timestep size 1668d763cef2SBarry Smith 1669d763cef2SBarry Smith Level: intermediate 1670d763cef2SBarry Smith 1671d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1672d763cef2SBarry Smith 1673d763cef2SBarry Smith .keywords: TS, get, timestep 1674d763cef2SBarry Smith @*/ 16757087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 1676d763cef2SBarry Smith { 1677d763cef2SBarry Smith PetscFunctionBegin; 16780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1679f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1680d763cef2SBarry Smith *dt = ts->time_step; 1681d763cef2SBarry Smith PetscFunctionReturn(0); 1682d763cef2SBarry Smith } 1683d763cef2SBarry Smith 16844a2ae208SSatish Balay #undef __FUNCT__ 16854a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1686d8e5e3e6SSatish Balay /*@ 1687d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1688d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1689d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1690d763cef2SBarry Smith the solution at the next timestep has been calculated. 1691d763cef2SBarry Smith 1692d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1693d763cef2SBarry Smith 1694d763cef2SBarry Smith Input Parameter: 1695d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1696d763cef2SBarry Smith 1697d763cef2SBarry Smith Output Parameter: 1698d763cef2SBarry Smith . v - the vector containing the solution 1699d763cef2SBarry Smith 1700d763cef2SBarry Smith Level: intermediate 1701d763cef2SBarry Smith 1702d763cef2SBarry Smith .seealso: TSGetTimeStep() 1703d763cef2SBarry Smith 1704d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1705d763cef2SBarry Smith @*/ 17067087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1707d763cef2SBarry Smith { 1708d763cef2SBarry Smith PetscFunctionBegin; 17090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17104482741eSBarry Smith PetscValidPointer(v,2); 17118737fe31SLisandro Dalcin *v = ts->vec_sol; 1712d763cef2SBarry Smith PetscFunctionReturn(0); 1713d763cef2SBarry Smith } 1714d763cef2SBarry Smith 1715c235aa8dSHong Zhang #undef __FUNCT__ 1716d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointGetSensitivity" 1717c235aa8dSHong Zhang /*@ 1718d4aa0a58SBarry Smith TSAdjointGetSensitivity - Returns the sensitivity in the reverse mode. 1719c235aa8dSHong Zhang 1720c235aa8dSHong Zhang Not Collective, but Vec returned is parallel if TS is parallel 1721c235aa8dSHong Zhang 1722c235aa8dSHong Zhang Input Parameter: 1723c235aa8dSHong Zhang . ts - the TS context obtained from TSCreate() 1724c235aa8dSHong Zhang 1725c235aa8dSHong Zhang Output Parameter: 1726c235aa8dSHong Zhang . v - the vector containing the solution 1727c235aa8dSHong Zhang 1728c235aa8dSHong Zhang Level: intermediate 1729c235aa8dSHong Zhang 1730c235aa8dSHong Zhang .seealso: TSGetTimeStep() 1731c235aa8dSHong Zhang 1732c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity 1733c235aa8dSHong Zhang @*/ 1734d4aa0a58SBarry Smith PetscErrorCode TSAdjointGetSensitivity(TS ts,Vec **v,PetscInt *numberadjs) 1735c235aa8dSHong Zhang { 1736c235aa8dSHong Zhang PetscFunctionBegin; 1737c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1738c235aa8dSHong Zhang PetscValidPointer(v,2); 1739b18ea86cSHong Zhang *v = ts->vecs_sensi; 1740b18ea86cSHong Zhang if(numberadjs) *numberadjs = ts->numberadjs; 1741c235aa8dSHong Zhang PetscFunctionReturn(0); 1742c235aa8dSHong Zhang } 1743c235aa8dSHong Zhang 1744bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 17454a2ae208SSatish Balay #undef __FUNCT__ 1746bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1747d8e5e3e6SSatish Balay /*@ 1748bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1749d763cef2SBarry Smith 1750bdad233fSMatthew Knepley Not collective 1751d763cef2SBarry Smith 1752bdad233fSMatthew Knepley Input Parameters: 1753bdad233fSMatthew Knepley + ts - The TS 1754bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1755d763cef2SBarry Smith .vb 17560910c330SBarry Smith U_t - A U = 0 (linear) 17570910c330SBarry Smith U_t - A(t) U = 0 (linear) 17580910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 1759d763cef2SBarry Smith .ve 1760d763cef2SBarry Smith 1761d763cef2SBarry Smith Level: beginner 1762d763cef2SBarry Smith 1763bdad233fSMatthew Knepley .keywords: TS, problem type 1764bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1765d763cef2SBarry Smith @*/ 17667087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1767a7cc72afSBarry Smith { 17689e2a6581SJed Brown PetscErrorCode ierr; 17699e2a6581SJed Brown 1770d763cef2SBarry Smith PetscFunctionBegin; 17710700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1772bdad233fSMatthew Knepley ts->problem_type = type; 17739e2a6581SJed Brown if (type == TS_LINEAR) { 17749e2a6581SJed Brown SNES snes; 17759e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 17769e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 17779e2a6581SJed Brown } 1778d763cef2SBarry Smith PetscFunctionReturn(0); 1779d763cef2SBarry Smith } 1780d763cef2SBarry Smith 1781bdad233fSMatthew Knepley #undef __FUNCT__ 1782bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1783bdad233fSMatthew Knepley /*@C 1784bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1785bdad233fSMatthew Knepley 1786bdad233fSMatthew Knepley Not collective 1787bdad233fSMatthew Knepley 1788bdad233fSMatthew Knepley Input Parameter: 1789bdad233fSMatthew Knepley . ts - The TS 1790bdad233fSMatthew Knepley 1791bdad233fSMatthew Knepley Output Parameter: 1792bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1793bdad233fSMatthew Knepley .vb 1794089b2837SJed Brown M U_t = A U 1795089b2837SJed Brown M(t) U_t = A(t) U 1796b5abc632SBarry Smith F(t,U,U_t) 1797bdad233fSMatthew Knepley .ve 1798bdad233fSMatthew Knepley 1799bdad233fSMatthew Knepley Level: beginner 1800bdad233fSMatthew Knepley 1801bdad233fSMatthew Knepley .keywords: TS, problem type 1802bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1803bdad233fSMatthew Knepley @*/ 18047087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1805a7cc72afSBarry Smith { 1806bdad233fSMatthew Knepley PetscFunctionBegin; 18070700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 18084482741eSBarry Smith PetscValidIntPointer(type,2); 1809bdad233fSMatthew Knepley *type = ts->problem_type; 1810bdad233fSMatthew Knepley PetscFunctionReturn(0); 1811bdad233fSMatthew Knepley } 1812d763cef2SBarry Smith 18134a2ae208SSatish Balay #undef __FUNCT__ 18144a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1815d763cef2SBarry Smith /*@ 1816d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1817d763cef2SBarry Smith of a timestepper. 1818d763cef2SBarry Smith 1819d763cef2SBarry Smith Collective on TS 1820d763cef2SBarry Smith 1821d763cef2SBarry Smith Input Parameter: 1822d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1823d763cef2SBarry Smith 1824d763cef2SBarry Smith Notes: 1825d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1826d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1827d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1828d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1829d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1830d763cef2SBarry Smith 1831d763cef2SBarry Smith Level: advanced 1832d763cef2SBarry Smith 1833d763cef2SBarry Smith .keywords: TS, timestep, setup 1834d763cef2SBarry Smith 1835d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1836d763cef2SBarry Smith @*/ 18377087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1838d763cef2SBarry Smith { 1839dfbe8321SBarry Smith PetscErrorCode ierr; 18406c6b9e74SPeter Brune DM dm; 18416c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 1842d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 18436c6b9e74SPeter Brune TSIJacobian ijac; 18446c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1845d763cef2SBarry Smith 1846d763cef2SBarry Smith PetscFunctionBegin; 18470700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1848277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1849277b19d0SLisandro Dalcin 18502c18e0fdSBarry Smith ts->total_steps = 0; 18517adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 18529596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1853d763cef2SBarry Smith } 1854277b19d0SLisandro Dalcin 1855277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1856277b19d0SLisandro Dalcin 1857c235aa8dSHong Zhang 1858552698daSJed Brown ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 18591c3436cfSJed Brown 1860e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 1861e1244c69SJed Brown Mat Amat,Pmat; 1862e1244c69SJed Brown SNES snes; 1863e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1864e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 1865e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 1866e1244c69SJed Brown * have displaced the RHS matrix */ 1867e1244c69SJed Brown if (Amat == ts->Arhs) { 1868e1244c69SJed Brown ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 1869e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 1870e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 1871e1244c69SJed Brown } 1872e1244c69SJed Brown if (Pmat == ts->Brhs) { 1873e1244c69SJed Brown ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 1874e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 1875e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 1876e1244c69SJed Brown } 1877e1244c69SJed Brown } 1878277b19d0SLisandro Dalcin if (ts->ops->setup) { 1879000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1880277b19d0SLisandro Dalcin } 1881277b19d0SLisandro Dalcin 18826c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 18836c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 18846c6b9e74SPeter Brune */ 18856c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 18860298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 18876c6b9e74SPeter Brune if (!func) { 18886c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 18896c6b9e74SPeter Brune } 18906c6b9e74SPeter 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. 18916c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 18926c6b9e74SPeter Brune */ 18930298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 18940298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 18950298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 18966c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 18976c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 18986c6b9e74SPeter Brune } 1899277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1900277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1901277b19d0SLisandro Dalcin } 1902277b19d0SLisandro Dalcin 1903277b19d0SLisandro Dalcin #undef __FUNCT__ 1904f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp" 1905f6a906c0SBarry Smith /*@ 1906f6a906c0SBarry Smith TSAdjointSetUp - Sets up the internal data structures for the later use 1907f6a906c0SBarry Smith of an adjoint solver 1908f6a906c0SBarry Smith 1909f6a906c0SBarry Smith Collective on TS 1910f6a906c0SBarry Smith 1911f6a906c0SBarry Smith Input Parameter: 1912f6a906c0SBarry Smith . ts - the TS context obtained from TSCreate() 1913f6a906c0SBarry Smith 1914f6a906c0SBarry Smith Notes: 1915f6a906c0SBarry Smith For basic use of the TS solvers the user need not explicitly call 1916f6a906c0SBarry Smith TSSetUp(), since these actions will automatically occur during 1917f6a906c0SBarry Smith the call to TSStep(). However, if one wishes to control this 1918f6a906c0SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1919f6a906c0SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1920f6a906c0SBarry Smith 1921f6a906c0SBarry Smith Level: advanced 1922f6a906c0SBarry Smith 1923f6a906c0SBarry Smith .keywords: TS, timestep, setup 1924f6a906c0SBarry Smith 1925f6a906c0SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1926f6a906c0SBarry Smith @*/ 1927f6a906c0SBarry Smith PetscErrorCode TSAdjointSetUp(TS ts) 1928f6a906c0SBarry Smith { 1929f6a906c0SBarry Smith PetscErrorCode ierr; 1930f6a906c0SBarry Smith 1931f6a906c0SBarry Smith PetscFunctionBegin; 1932f6a906c0SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1933f6a906c0SBarry Smith if (ts->adjointsetupcalled) PetscFunctionReturn(0); 193408c7845fSBarry Smith if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSAdjointSetSensitivity() first"); 1935f6a906c0SBarry Smith if (ts->ops->setupadj) { 1936f6a906c0SBarry Smith ierr = (*ts->ops->setupadj)(ts);CHKERRQ(ierr); 1937f6a906c0SBarry Smith } 1938f6a906c0SBarry Smith ts->adjointsetupcalled = PETSC_TRUE; 1939f6a906c0SBarry Smith PetscFunctionReturn(0); 1940f6a906c0SBarry Smith } 1941f6a906c0SBarry Smith 1942f6a906c0SBarry Smith #undef __FUNCT__ 1943277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1944277b19d0SLisandro Dalcin /*@ 1945277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1946277b19d0SLisandro Dalcin 1947277b19d0SLisandro Dalcin Collective on TS 1948277b19d0SLisandro Dalcin 1949277b19d0SLisandro Dalcin Input Parameter: 1950277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1951277b19d0SLisandro Dalcin 1952277b19d0SLisandro Dalcin Level: beginner 1953277b19d0SLisandro Dalcin 1954277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1955277b19d0SLisandro Dalcin 1956277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1957277b19d0SLisandro Dalcin @*/ 1958277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1959277b19d0SLisandro Dalcin { 1960277b19d0SLisandro Dalcin PetscErrorCode ierr; 1961277b19d0SLisandro Dalcin 1962277b19d0SLisandro Dalcin PetscFunctionBegin; 1963277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1964b18ea86cSHong Zhang 1965277b19d0SLisandro Dalcin if (ts->ops->reset) { 1966277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1967277b19d0SLisandro Dalcin } 1968277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 1969bbd56ea5SKarl Rupp 19704e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 19714e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1972214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 19736bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1974e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 1975e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 197638637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 1977f8fee759SHong Zhang ts->vecs_sensi = 0; 1978f8fee759SHong Zhang ts->vecs_sensip = 0; 1979ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 198005755b9cSHong Zhang ierr = VecDestroy(&ts->vec_costquad);CHKERRQ(ierr); 198136eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 1982277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1983d763cef2SBarry Smith PetscFunctionReturn(0); 1984d763cef2SBarry Smith } 1985d763cef2SBarry Smith 19864a2ae208SSatish Balay #undef __FUNCT__ 19874a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1988d8e5e3e6SSatish Balay /*@ 1989d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1990d763cef2SBarry Smith with TSCreate(). 1991d763cef2SBarry Smith 1992d763cef2SBarry Smith Collective on TS 1993d763cef2SBarry Smith 1994d763cef2SBarry Smith Input Parameter: 1995d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1996d763cef2SBarry Smith 1997d763cef2SBarry Smith Level: beginner 1998d763cef2SBarry Smith 1999d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2000d763cef2SBarry Smith 2001d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2002d763cef2SBarry Smith @*/ 20036bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2004d763cef2SBarry Smith { 20056849ba73SBarry Smith PetscErrorCode ierr; 2006d763cef2SBarry Smith 2007d763cef2SBarry Smith PetscFunctionBegin; 20086bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 20096bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 20106bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2011d763cef2SBarry Smith 20126bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2013277b19d0SLisandro Dalcin 2014e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2015e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 20166bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 20176d4c513bSLisandro Dalcin 2018*bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2019*bc952696SBarry Smith 202084df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 2021aeb4809dSShri Abhyankar if ((*ts)->event) { 2022aeb4809dSShri Abhyankar ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr); 2023aeb4809dSShri Abhyankar } 20246bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 20256bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 20266bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 20276d4c513bSLisandro Dalcin 2028a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2029d763cef2SBarry Smith PetscFunctionReturn(0); 2030d763cef2SBarry Smith } 2031d763cef2SBarry Smith 20324a2ae208SSatish Balay #undef __FUNCT__ 20334a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 2034d8e5e3e6SSatish Balay /*@ 2035d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2036d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2037d763cef2SBarry Smith 2038d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2039d763cef2SBarry Smith 2040d763cef2SBarry Smith Input Parameter: 2041d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2042d763cef2SBarry Smith 2043d763cef2SBarry Smith Output Parameter: 2044d763cef2SBarry Smith . snes - the nonlinear solver context 2045d763cef2SBarry Smith 2046d763cef2SBarry Smith Notes: 2047d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2048d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 204994b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2050d763cef2SBarry Smith 2051d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 20520298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2053d763cef2SBarry Smith 2054d763cef2SBarry Smith Level: beginner 2055d763cef2SBarry Smith 2056d763cef2SBarry Smith .keywords: timestep, get, SNES 2057d763cef2SBarry Smith @*/ 20587087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2059d763cef2SBarry Smith { 2060d372ba47SLisandro Dalcin PetscErrorCode ierr; 2061d372ba47SLisandro Dalcin 2062d763cef2SBarry Smith PetscFunctionBegin; 20630700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20644482741eSBarry Smith PetscValidPointer(snes,2); 2065d372ba47SLisandro Dalcin if (!ts->snes) { 2066ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 20670298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 20683bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2069d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2070496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 20719e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 20729e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 20739e2a6581SJed Brown } 2074d372ba47SLisandro Dalcin } 2075d763cef2SBarry Smith *snes = ts->snes; 2076d763cef2SBarry Smith PetscFunctionReturn(0); 2077d763cef2SBarry Smith } 2078d763cef2SBarry Smith 20794a2ae208SSatish Balay #undef __FUNCT__ 2080deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES" 2081deb2cd25SJed Brown /*@ 2082deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2083deb2cd25SJed Brown 2084deb2cd25SJed Brown Collective 2085deb2cd25SJed Brown 2086deb2cd25SJed Brown Input Parameter: 2087deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2088deb2cd25SJed Brown - snes - the nonlinear solver context 2089deb2cd25SJed Brown 2090deb2cd25SJed Brown Notes: 2091deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2092deb2cd25SJed Brown 2093deb2cd25SJed Brown Level: developer 2094deb2cd25SJed Brown 2095deb2cd25SJed Brown .keywords: timestep, set, SNES 2096deb2cd25SJed Brown @*/ 2097deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2098deb2cd25SJed Brown { 2099deb2cd25SJed Brown PetscErrorCode ierr; 2100d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2101deb2cd25SJed Brown 2102deb2cd25SJed Brown PetscFunctionBegin; 2103deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2104deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2105deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2106deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2107bbd56ea5SKarl Rupp 2108deb2cd25SJed Brown ts->snes = snes; 2109bbd56ea5SKarl Rupp 21100298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 21110298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2112740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 21130298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2114740132f1SEmil Constantinescu } 2115deb2cd25SJed Brown PetscFunctionReturn(0); 2116deb2cd25SJed Brown } 2117deb2cd25SJed Brown 2118deb2cd25SJed Brown #undef __FUNCT__ 211994b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 2120d8e5e3e6SSatish Balay /*@ 212194b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2122d763cef2SBarry Smith a TS (timestepper) context. 2123d763cef2SBarry Smith 212494b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2125d763cef2SBarry Smith 2126d763cef2SBarry Smith Input Parameter: 2127d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2128d763cef2SBarry Smith 2129d763cef2SBarry Smith Output Parameter: 213094b7f48cSBarry Smith . ksp - the nonlinear solver context 2131d763cef2SBarry Smith 2132d763cef2SBarry Smith Notes: 213394b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2134d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2135d763cef2SBarry Smith KSP and PC contexts as well. 2136d763cef2SBarry Smith 213794b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 21380298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2139d763cef2SBarry Smith 2140d763cef2SBarry Smith Level: beginner 2141d763cef2SBarry Smith 214294b7f48cSBarry Smith .keywords: timestep, get, KSP 2143d763cef2SBarry Smith @*/ 21447087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2145d763cef2SBarry Smith { 2146d372ba47SLisandro Dalcin PetscErrorCode ierr; 2147089b2837SJed Brown SNES snes; 2148d372ba47SLisandro Dalcin 2149d763cef2SBarry Smith PetscFunctionBegin; 21500700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 21514482741eSBarry Smith PetscValidPointer(ksp,2); 215217186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2153e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2154089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2155089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2156d763cef2SBarry Smith PetscFunctionReturn(0); 2157d763cef2SBarry Smith } 2158d763cef2SBarry Smith 2159d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2160d763cef2SBarry Smith 21614a2ae208SSatish Balay #undef __FUNCT__ 2162adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 2163adb62b0dSMatthew Knepley /*@ 2164adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2165adb62b0dSMatthew Knepley maximum time for iteration. 2166adb62b0dSMatthew Knepley 21673f9fe445SBarry Smith Not Collective 2168adb62b0dSMatthew Knepley 2169adb62b0dSMatthew Knepley Input Parameters: 2170adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 21710298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 21720298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2173adb62b0dSMatthew Knepley 2174adb62b0dSMatthew Knepley Level: intermediate 2175adb62b0dSMatthew Knepley 2176adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2177adb62b0dSMatthew Knepley @*/ 21787087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2179adb62b0dSMatthew Knepley { 2180adb62b0dSMatthew Knepley PetscFunctionBegin; 21810700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2182abc0a331SBarry Smith if (maxsteps) { 21834482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2184adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2185adb62b0dSMatthew Knepley } 2186abc0a331SBarry Smith if (maxtime) { 21874482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2188adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2189adb62b0dSMatthew Knepley } 2190adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2191adb62b0dSMatthew Knepley } 2192adb62b0dSMatthew Knepley 2193adb62b0dSMatthew Knepley #undef __FUNCT__ 21944a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 2195d763cef2SBarry Smith /*@ 2196d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2197d763cef2SBarry Smith maximum time for iteration. 2198d763cef2SBarry Smith 21993f9fe445SBarry Smith Logically Collective on TS 2200d763cef2SBarry Smith 2201d763cef2SBarry Smith Input Parameters: 2202d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2203d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2204d763cef2SBarry Smith - maxtime - final time to iterate to 2205d763cef2SBarry Smith 2206d763cef2SBarry Smith Options Database Keys: 2207d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 22083bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2209d763cef2SBarry Smith 2210d763cef2SBarry Smith Notes: 2211d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2212d763cef2SBarry Smith 2213d763cef2SBarry Smith Level: intermediate 2214d763cef2SBarry Smith 2215d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2216a43b19c4SJed Brown 2217a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2218d763cef2SBarry Smith @*/ 22197087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2220d763cef2SBarry Smith { 2221d763cef2SBarry Smith PetscFunctionBegin; 22220700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2223c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2224c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 222539b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 222639b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2227d763cef2SBarry Smith PetscFunctionReturn(0); 2228d763cef2SBarry Smith } 2229d763cef2SBarry Smith 22304a2ae208SSatish Balay #undef __FUNCT__ 22314a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 2232d763cef2SBarry Smith /*@ 2233d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2234d763cef2SBarry Smith for use by the TS routines. 2235d763cef2SBarry Smith 22363f9fe445SBarry Smith Logically Collective on TS and Vec 2237d763cef2SBarry Smith 2238d763cef2SBarry Smith Input Parameters: 2239d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 22400910c330SBarry Smith - u - the solution vector 2241d763cef2SBarry Smith 2242d763cef2SBarry Smith Level: beginner 2243d763cef2SBarry Smith 2244d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2245d763cef2SBarry Smith @*/ 22460910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2247d763cef2SBarry Smith { 22488737fe31SLisandro Dalcin PetscErrorCode ierr; 2249496e6a7aSJed Brown DM dm; 22508737fe31SLisandro Dalcin 2251d763cef2SBarry Smith PetscFunctionBegin; 22520700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22530910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 22540910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 22556bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2256bbd56ea5SKarl Rupp 22570910c330SBarry Smith ts->vec_sol = u; 2258bbd56ea5SKarl Rupp 2259496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 22600910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2261d763cef2SBarry Smith PetscFunctionReturn(0); 2262d763cef2SBarry Smith } 2263d763cef2SBarry Smith 2264e74ef692SMatthew Knepley #undef __FUNCT__ 2265d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetSensitivity" 2266c235aa8dSHong Zhang /*@ 2267d4aa0a58SBarry Smith TSAdjointSetSensitivity - Sets the initial value of sensitivity (w.r.t. initial conditions) 2268c235aa8dSHong Zhang for use by the TS routines. 2269c235aa8dSHong Zhang 2270c235aa8dSHong Zhang Logically Collective on TS and Vec 2271c235aa8dSHong Zhang 2272c235aa8dSHong Zhang Input Parameters: 2273c235aa8dSHong Zhang + ts - the TS context obtained from TSCreate() 2274c235aa8dSHong Zhang - u - the solution vector 2275c235aa8dSHong Zhang 2276c235aa8dSHong Zhang Level: beginner 2277c235aa8dSHong Zhang 2278c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions 2279c235aa8dSHong Zhang @*/ 2280d4aa0a58SBarry Smith PetscErrorCode TSAdjointSetSensitivity(TS ts,Vec *u,PetscInt numberadjs) 2281c235aa8dSHong Zhang { 2282c235aa8dSHong Zhang PetscFunctionBegin; 2283c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2284b18ea86cSHong Zhang PetscValidPointer(u,2); 2285b18ea86cSHong Zhang ts->vecs_sensi = u; 2286d4aa0a58SBarry Smith if(ts->numberadjs && ts->numberadjs!=numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of adjoint variables (3rd parameter) is inconsistent with the one set by TSAdjointSetSensitivityP()"); 2287b18ea86cSHong Zhang ts->numberadjs = numberadjs; 2288c235aa8dSHong Zhang 2289ad8e2604SHong Zhang PetscFunctionReturn(0); 2290ad8e2604SHong Zhang } 2291ad8e2604SHong Zhang 2292ad8e2604SHong Zhang #undef __FUNCT__ 2293d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetSensitivityP" 2294ad8e2604SHong Zhang /*@ 2295d4aa0a58SBarry Smith TSAdjointSetSensitivityP - Sets the initial value of sensitivity (w.r.t. parameters) 2296ad8e2604SHong Zhang for use by the TS routines. 2297ad8e2604SHong Zhang 2298ad8e2604SHong Zhang Logically Collective on TS and Vec 2299ad8e2604SHong Zhang 2300ad8e2604SHong Zhang Input Parameters: 2301ad8e2604SHong Zhang + ts - the TS context obtained from TSCreate() 2302ad8e2604SHong Zhang - u - the solution vector 2303ad8e2604SHong Zhang 2304ad8e2604SHong Zhang Level: beginner 2305ad8e2604SHong Zhang 2306ad8e2604SHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions 2307ad8e2604SHong Zhang @*/ 2308d4aa0a58SBarry Smith PetscErrorCode TSAdjointSetSensitivityP(TS ts,Vec *u,PetscInt numberadjs) 2309ad8e2604SHong Zhang { 2310ad8e2604SHong Zhang PetscFunctionBegin; 2311ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2312ad8e2604SHong Zhang PetscValidPointer(u,2); 2313ad8e2604SHong Zhang ts->vecs_sensip = u; 2314d4aa0a58SBarry Smith if(ts->numberadjs && ts->numberadjs!=numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of adjoint variables (3rd parameter) is inconsistent with the one set by TSAdjointSetSensitivity()"); 2315ad8e2604SHong Zhang ts->numberadjs = numberadjs; 2316ad8e2604SHong Zhang 2317ad8e2604SHong Zhang PetscFunctionReturn(0); 2318ad8e2604SHong Zhang } 2319ad8e2604SHong Zhang 2320ad8e2604SHong Zhang #undef __FUNCT__ 2321d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobianP" 2322ad8e2604SHong Zhang /*@C 2323d4aa0a58SBarry Smith TSAdjointSetRHSJacobianP - Sets the function that computes the Jacobian w.r.t. parameters. 2324ad8e2604SHong Zhang 2325ad8e2604SHong Zhang Logically Collective on TS 2326ad8e2604SHong Zhang 2327ad8e2604SHong Zhang Input Parameters: 2328ad8e2604SHong Zhang + ts - The TS context obtained from TSCreate() 2329ad8e2604SHong Zhang - func - The function 2330ad8e2604SHong Zhang 2331ad8e2604SHong Zhang Calling sequence of func: 233205755b9cSHong Zhang $ func (TS ts,PetscReal t,Vec u,Mat A,void *ctx); 233305755b9cSHong Zhang + t - current timestep 233405755b9cSHong Zhang . u - input vector 233505755b9cSHong Zhang . A - output matrix 233605755b9cSHong Zhang - ctx - [optional] user-defined function context 2337ad8e2604SHong Zhang 2338ad8e2604SHong Zhang Level: intermediate 2339ad8e2604SHong Zhang 2340ad8e2604SHong Zhang .keywords: TS, sensitivity 2341ad8e2604SHong Zhang .seealso: 2342ad8e2604SHong Zhang @*/ 2343d4aa0a58SBarry Smith PetscErrorCode TSAdjointSetRHSJacobianP(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 2344ad8e2604SHong Zhang { 2345ad8e2604SHong Zhang PetscErrorCode ierr; 2346ad8e2604SHong Zhang 2347ad8e2604SHong Zhang PetscFunctionBegin; 2348ad8e2604SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2349ad8e2604SHong Zhang if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 2350ad8e2604SHong Zhang 2351ad8e2604SHong Zhang ts->rhsjacobianp = func; 2352ad8e2604SHong Zhang ts->rhsjacobianpctx = ctx; 2353ad8e2604SHong Zhang if(Amat) { 2354ad8e2604SHong Zhang ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 2355ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2356ad8e2604SHong Zhang 2357ad8e2604SHong Zhang ts->Jacp = Amat; 2358ad8e2604SHong Zhang } 2359ad8e2604SHong Zhang PetscFunctionReturn(0); 2360ad8e2604SHong Zhang } 2361ad8e2604SHong Zhang 2362ad8e2604SHong Zhang #undef __FUNCT__ 2363d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobianP" 2364ad8e2604SHong Zhang /*@ 2365d4aa0a58SBarry Smith TSAdjointComputeRHSJacobianP - Runs the user-defined JacobianP function. 2366ad8e2604SHong Zhang 2367ad8e2604SHong Zhang Collective on TS 2368ad8e2604SHong Zhang 2369ad8e2604SHong Zhang Input Parameters: 2370ad8e2604SHong Zhang . ts - The TS context obtained from TSCreate() 2371ad8e2604SHong Zhang 2372ad8e2604SHong Zhang Level: developer 2373ad8e2604SHong Zhang 2374ad8e2604SHong Zhang .keywords: TS, sensitivity 2375d4aa0a58SBarry Smith .seealso: TSAdjointSetRHSJacobianP() 2376ad8e2604SHong Zhang @*/ 2377d4aa0a58SBarry Smith PetscErrorCode TSAdjointComputeRHSJacobianP(TS ts,PetscReal t,Vec X,Mat Amat) 2378ad8e2604SHong Zhang { 2379ad8e2604SHong Zhang PetscErrorCode ierr; 2380ad8e2604SHong Zhang 2381ad8e2604SHong Zhang PetscFunctionBegin; 2382ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2383ad8e2604SHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 2384ad8e2604SHong Zhang PetscValidPointer(Amat,4); 2385ad8e2604SHong Zhang 2386ad8e2604SHong Zhang PetscStackPush("TS user JacobianP function for sensitivity analysis"); 2387ad8e2604SHong Zhang ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 2388ad8e2604SHong Zhang PetscStackPop; 2389ad8e2604SHong Zhang 2390c235aa8dSHong Zhang PetscFunctionReturn(0); 2391c235aa8dSHong Zhang } 2392c235aa8dSHong Zhang 2393c235aa8dSHong Zhang #undef __FUNCT__ 2394d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetCostIntegrand" 23956fd0887fSHong Zhang /*@C 2396d4aa0a58SBarry Smith TSAdjointSetCostIntegrand - Sets the routine for evaluating the quadrature (or integral) term in a cost function, 23976fd0887fSHong Zhang where Q_t = r(t,u). 23986fd0887fSHong Zhang 23996fd0887fSHong Zhang Logically Collective on TS 24006fd0887fSHong Zhang 24016fd0887fSHong Zhang Input Parameters: 24026fd0887fSHong Zhang + ts - the TS context obtained from TSCreate() 240336eaed60SHong Zhang . q - vector to put the computed quadrature term in the cost function (or NULL to have it created) 24046fd0887fSHong Zhang . fq - routine for evaluating the right-hand-side function 24056fd0887fSHong Zhang - ctx - [optional] user-defined context for private data for the 24066fd0887fSHong Zhang function evaluation routine (may be NULL) 24076fd0887fSHong Zhang 24086fd0887fSHong Zhang Calling sequence of func: 2409a2ae3dd2SHong Zhang $ TSCostIntegrand(TS ts,PetscReal t,Vec u,PetscReal *f,void *ctx); 24106fd0887fSHong Zhang 24116fd0887fSHong Zhang + t - current timestep 24126fd0887fSHong Zhang . u - input vector 241336eaed60SHong Zhang . f - function vector 24146fd0887fSHong Zhang - ctx - [optional] user-defined function context 24156fd0887fSHong Zhang 24166fd0887fSHong Zhang Level: beginner 24176fd0887fSHong Zhang 24186fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 24196fd0887fSHong Zhang 2420d4aa0a58SBarry Smith .seealso: TSAdjointSetRHSJacobianP(),TSAdjointSetSensitivity(),TSAdjointSetSensitivityP() 24216fd0887fSHong Zhang @*/ 2422d4aa0a58SBarry Smith PetscErrorCode TSAdjointSetCostIntegrand(TS ts,PetscInt numberadjs,Vec q,PetscErrorCode (*fq)(TS,PetscReal,Vec,Vec,void*),void *ctx) 24236fd0887fSHong Zhang { 24246fd0887fSHong Zhang PetscErrorCode ierr; 242536eaed60SHong Zhang PetscInt size; 24266fd0887fSHong Zhang 24276fd0887fSHong Zhang PetscFunctionBegin; 24286fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 242936eaed60SHong Zhang if (q) { 243036eaed60SHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,2); 243136eaed60SHong Zhang } else { 2432d4aa0a58SBarry Smith SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"TSAdjointSetCostIntegrand() requires a vector of size numberajds to hold the value of integrals as 3rd input parameter"); 24336fd0887fSHong Zhang } 2434d4aa0a58SBarry Smith if (!ts->numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Call TSAdjointSetSensitivity() or TSAdjointSetSensitivityP() first so that the number of cost functions can be determined."); 2435d4aa0a58SBarry Smith if (ts->numberadjs && ts->numberadjs!=numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSAdjointSetCostIntegrand()) is inconsistent with the one set by TSAdjointSetSensitivity() or TSAdjointSetSensitivityP()"); 243636eaed60SHong Zhang ierr = VecGetSize(q,&size);CHKERRQ(ierr); 2437a2ae3dd2SHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 2438d4aa0a58SBarry Smith if (size!=numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions is inconsistent with the number of integrals (size of the 3rd input vector of TSAdjointSetCostIntegrand())."); 24396fd0887fSHong Zhang 244036eaed60SHong Zhang ierr = PetscObjectReference((PetscObject)q);CHKERRQ(ierr); 244136eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costquad);CHKERRQ(ierr); 244236eaed60SHong Zhang ts->vec_costquad = q; 244336eaed60SHong Zhang 2444a2ae3dd2SHong Zhang ierr = VecDuplicate(ts->vec_costquad,&ts->vec_costintegrand);CHKERRQ(ierr); 2445e4680132SHong Zhang ts->costintegrand = fq; 244605755b9cSHong Zhang ts->costintegrandctx = ctx; 24476fd0887fSHong Zhang 24486fd0887fSHong Zhang PetscFunctionReturn(0); 24496fd0887fSHong Zhang } 24506fd0887fSHong Zhang 24516fd0887fSHong Zhang #undef __FUNCT__ 2452d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointGetCostQuadrature" 245336eaed60SHong Zhang /*@ 2454d4aa0a58SBarry Smith TSAdjointGetCostQuadrature - Returns the values of the quadrature (or integral) terms in a cost function. 245536eaed60SHong Zhang It is valid to call the routine after a backward run. 245636eaed60SHong Zhang 245736eaed60SHong Zhang Not Collective 245836eaed60SHong Zhang 245936eaed60SHong Zhang Input Parameter: 246036eaed60SHong Zhang . ts - the TS context obtained from TSCreate() 246136eaed60SHong Zhang 246236eaed60SHong Zhang Output Parameter: 246336eaed60SHong Zhang . v - the vector containing the solution 246436eaed60SHong Zhang 246536eaed60SHong Zhang Level: intermediate 246636eaed60SHong Zhang 2467d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand() 246836eaed60SHong Zhang 246936eaed60SHong Zhang .keywords: TS, sensitivity analysis 247036eaed60SHong Zhang @*/ 2471d4aa0a58SBarry Smith PetscErrorCode TSAdjointGetCostQuadrature(TS ts,Vec *v) 247236eaed60SHong Zhang { 247336eaed60SHong Zhang PetscFunctionBegin; 247436eaed60SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 247536eaed60SHong Zhang PetscValidPointer(v,2); 247636eaed60SHong Zhang *v = ts->vec_costquad; 247736eaed60SHong Zhang PetscFunctionReturn(0); 247836eaed60SHong Zhang } 247936eaed60SHong Zhang 248036eaed60SHong Zhang #undef __FUNCT__ 2481d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand" 24826fd0887fSHong Zhang /*@ 2483d4aa0a58SBarry Smith TSAdjointComputeCostIntegrand - Evaluates the quadrature function in the cost functions. 24846fd0887fSHong Zhang 24856fd0887fSHong Zhang Input Parameters: 24866fd0887fSHong Zhang + ts - the TS context 24876fd0887fSHong Zhang . t - current time 248805755b9cSHong Zhang - U - state vector 24896fd0887fSHong Zhang 24906fd0887fSHong Zhang Output Parameter: 24916fd0887fSHong Zhang . q - vector of size numberadjs to hold the outputs 24926fd0887fSHong Zhang 24936fd0887fSHong Zhang Note: 24946fd0887fSHong Zhang Most users should not need to explicitly call this routine, as it 24956fd0887fSHong Zhang is used internally within the sensitivity analysis context. 24966fd0887fSHong Zhang 24976fd0887fSHong Zhang Level: developer 24986fd0887fSHong Zhang 24996fd0887fSHong Zhang .keywords: TS, compute 25006fd0887fSHong Zhang 2501d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand() 25026fd0887fSHong Zhang @*/ 2503d4aa0a58SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec U,Vec q) 25046fd0887fSHong Zhang { 25056fd0887fSHong Zhang PetscErrorCode ierr; 25066fd0887fSHong Zhang 25076fd0887fSHong Zhang PetscFunctionBegin; 25086fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25096fd0887fSHong Zhang PetscValidHeaderSpecific(U,VEC_CLASSID,3); 25106fd0887fSHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,4); 25116fd0887fSHong Zhang 25126fd0887fSHong Zhang ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,q,0);CHKERRQ(ierr); 2513e4680132SHong Zhang if (ts->costintegrand) { 2514e4680132SHong Zhang PetscStackPush("TS user integrand in the cost function"); 251505755b9cSHong Zhang ierr = (*ts->costintegrand)(ts,t,U,q,ts->costintegrandctx);CHKERRQ(ierr); 25166fd0887fSHong Zhang PetscStackPop; 25176fd0887fSHong Zhang } else { 25186fd0887fSHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 25196fd0887fSHong Zhang } 25206fd0887fSHong Zhang 25216fd0887fSHong Zhang ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,q,0);CHKERRQ(ierr); 25226fd0887fSHong Zhang PetscFunctionReturn(0); 25236fd0887fSHong Zhang } 25246fd0887fSHong Zhang 25256fd0887fSHong Zhang #undef __FUNCT__ 2526d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetDRDYFunction" 252705755b9cSHong Zhang /*@C 2528d4aa0a58SBarry Smith TSAdjointSetDRDYFunction - Sets the function that computes the gradient of the CostIntegrand function r w.r.t. states y. 252905755b9cSHong Zhang 253005755b9cSHong Zhang Logically Collective on TS 253105755b9cSHong Zhang 253205755b9cSHong Zhang Input Parameters: 253305755b9cSHong Zhang + ts - The TS context obtained from TSCreate() 253405755b9cSHong Zhang - func - The function 253505755b9cSHong Zhang 253605755b9cSHong Zhang Calling sequence of func: 253707658559SBarry Smith . PetscErroCode func(TS ts,PetscReal t,Vec U,Vec *drdy,void *ctx); 253805755b9cSHong Zhang 253905755b9cSHong Zhang Level: intermediate 254005755b9cSHong Zhang 254105755b9cSHong Zhang .keywords: TS, sensitivity 254205755b9cSHong Zhang .seealso: 254305755b9cSHong Zhang @*/ 2544d4aa0a58SBarry Smith PetscErrorCode TSAdjointSetDRDYFunction(TS ts,Vec *drdy,PetscErrorCode (*func)(TS,PetscReal,Vec,Vec*,void*),void *ctx) 254505755b9cSHong Zhang { 254605755b9cSHong Zhang PetscFunctionBegin; 254705755b9cSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 254805755b9cSHong Zhang 254905755b9cSHong Zhang ts->drdyfunction = func; 255005755b9cSHong Zhang ts->drdyfunctionctx = ctx; 255105755b9cSHong Zhang ts->vecs_drdy = drdy; 255205755b9cSHong Zhang PetscFunctionReturn(0); 255305755b9cSHong Zhang } 255405755b9cSHong Zhang 255505755b9cSHong Zhang #undef __FUNCT__ 2556d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction" 255705755b9cSHong Zhang /*@ 2558d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 255905755b9cSHong Zhang 256005755b9cSHong Zhang Collective on TS 256105755b9cSHong Zhang 256205755b9cSHong Zhang Input Parameters: 256305755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 256405755b9cSHong Zhang 256505755b9cSHong Zhang Notes: 2566d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 256705755b9cSHong Zhang so most users would not generally call this routine themselves. 256805755b9cSHong Zhang 256905755b9cSHong Zhang Level: developer 257005755b9cSHong Zhang 257105755b9cSHong Zhang .keywords: TS, sensitivity 2572d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction() 257305755b9cSHong Zhang @*/ 2574d4aa0a58SBarry Smith PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec X,Vec *drdy) 257505755b9cSHong Zhang { 257605755b9cSHong Zhang PetscErrorCode ierr; 257705755b9cSHong Zhang 257805755b9cSHong Zhang PetscFunctionBegin; 257905755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 258005755b9cSHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 258105755b9cSHong Zhang 258205755b9cSHong Zhang PetscStackPush("TS user DRDY function for sensitivity analysis"); 258305755b9cSHong Zhang ierr = (*ts->drdyfunction)(ts,t,X,drdy,ts->drdyfunctionctx); CHKERRQ(ierr); 258405755b9cSHong Zhang PetscStackPop; 258505755b9cSHong Zhang PetscFunctionReturn(0); 258605755b9cSHong Zhang } 258705755b9cSHong Zhang 258805755b9cSHong Zhang #undef __FUNCT__ 2589d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetDRDPFunction" 259005755b9cSHong Zhang /*@C 2591d4aa0a58SBarry Smith TSAdjointSetDRDPFunction - Sets the function that computes the gradient of the CostIntegrand function w.r.t. parameters. 259205755b9cSHong Zhang 259305755b9cSHong Zhang Logically Collective on TS 259405755b9cSHong Zhang 259505755b9cSHong Zhang Input Parameters: 259605755b9cSHong Zhang + ts - The TS context obtained from TSCreate() 259705755b9cSHong Zhang - func - The function 259805755b9cSHong Zhang 259905755b9cSHong Zhang Calling sequence of func: 260007658559SBarry Smith . func(TS ts,PetscReal t,Vec U,Vec *drdy,void *ctx); 260105755b9cSHong Zhang 260205755b9cSHong Zhang Level: intermediate 260305755b9cSHong Zhang 260405755b9cSHong Zhang .keywords: TS, sensitivity 260505755b9cSHong Zhang .seealso: 260605755b9cSHong Zhang @*/ 2607d4aa0a58SBarry Smith PetscErrorCode TSAdjointSetDRDPFunction(TS ts,Vec *drdp,PetscErrorCode (*func)(TS,PetscReal,Vec,Vec*,void*),void *ctx) 260805755b9cSHong Zhang { 260905755b9cSHong Zhang PetscFunctionBegin; 261005755b9cSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 261105755b9cSHong Zhang 261205755b9cSHong Zhang ts->drdpfunction = func; 261305755b9cSHong Zhang ts->drdpfunctionctx = ctx; 261405755b9cSHong Zhang ts->vecs_drdp = drdp; 261505755b9cSHong Zhang 261605755b9cSHong Zhang PetscFunctionReturn(0); 261705755b9cSHong Zhang } 261805755b9cSHong Zhang 261905755b9cSHong Zhang #undef __FUNCT__ 2620d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction" 262105755b9cSHong Zhang /*@ 2622d4aa0a58SBarry Smith TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 262305755b9cSHong Zhang 262405755b9cSHong Zhang Collective on TS 262505755b9cSHong Zhang 262605755b9cSHong Zhang Input Parameters: 262705755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 262805755b9cSHong Zhang 262905755b9cSHong Zhang Notes: 263005755b9cSHong Zhang TSDRDPFunction() is typically used for sensitivity implementation, 263105755b9cSHong Zhang so most users would not generally call this routine themselves. 263205755b9cSHong Zhang 263305755b9cSHong Zhang Level: developer 263405755b9cSHong Zhang 263505755b9cSHong Zhang .keywords: TS, sensitivity 2636d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction() 263705755b9cSHong Zhang @*/ 2638d4aa0a58SBarry Smith PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec X,Vec *drdp) 263905755b9cSHong Zhang { 264005755b9cSHong Zhang PetscErrorCode ierr; 264105755b9cSHong Zhang 264205755b9cSHong Zhang PetscFunctionBegin; 264305755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 264405755b9cSHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 264505755b9cSHong Zhang 264605755b9cSHong Zhang PetscStackPush("TS user DRDP function for sensitivity analysis"); 264705755b9cSHong Zhang ierr = (*ts->drdpfunction)(ts,t,X,drdp,ts->drdpfunctionctx); CHKERRQ(ierr); 264805755b9cSHong Zhang PetscStackPop; 264905755b9cSHong Zhang 265005755b9cSHong Zhang PetscFunctionReturn(0); 265105755b9cSHong Zhang } 265205755b9cSHong Zhang 265305755b9cSHong Zhang #undef __FUNCT__ 2654e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 2655ac226902SBarry Smith /*@C 2656000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 26573f2090d5SJed Brown called once at the beginning of each time step. 2658000e7ae3SMatthew Knepley 26593f9fe445SBarry Smith Logically Collective on TS 2660000e7ae3SMatthew Knepley 2661000e7ae3SMatthew Knepley Input Parameters: 2662000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2663000e7ae3SMatthew Knepley - func - The function 2664000e7ae3SMatthew Knepley 2665000e7ae3SMatthew Knepley Calling sequence of func: 2666000e7ae3SMatthew Knepley . func (TS ts); 2667000e7ae3SMatthew Knepley 2668000e7ae3SMatthew Knepley Level: intermediate 2669000e7ae3SMatthew Knepley 2670b8123daeSJed Brown Note: 2671b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 2672b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 2673b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 2674b8123daeSJed Brown 2675000e7ae3SMatthew Knepley .keywords: TS, timestep 26769be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 2677000e7ae3SMatthew Knepley @*/ 26787087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 2679000e7ae3SMatthew Knepley { 2680000e7ae3SMatthew Knepley PetscFunctionBegin; 26810700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2682ae60f76fSBarry Smith ts->prestep = func; 2683000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2684000e7ae3SMatthew Knepley } 2685000e7ae3SMatthew Knepley 2686e74ef692SMatthew Knepley #undef __FUNCT__ 26873f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 268809ee8438SJed Brown /*@ 26893f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 26903f2090d5SJed Brown 26913f2090d5SJed Brown Collective on TS 26923f2090d5SJed Brown 26933f2090d5SJed Brown Input Parameters: 26943f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 26953f2090d5SJed Brown 26963f2090d5SJed Brown Notes: 26973f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 26983f2090d5SJed Brown so most users would not generally call this routine themselves. 26993f2090d5SJed Brown 27003f2090d5SJed Brown Level: developer 27013f2090d5SJed Brown 27023f2090d5SJed Brown .keywords: TS, timestep 27039be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 27043f2090d5SJed Brown @*/ 27057087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 27063f2090d5SJed Brown { 27073f2090d5SJed Brown PetscErrorCode ierr; 27083f2090d5SJed Brown 27093f2090d5SJed Brown PetscFunctionBegin; 27100700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2711ae60f76fSBarry Smith if (ts->prestep) { 2712ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 2713312ce896SJed Brown } 27143f2090d5SJed Brown PetscFunctionReturn(0); 27153f2090d5SJed Brown } 27163f2090d5SJed Brown 27173f2090d5SJed Brown #undef __FUNCT__ 2718b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 2719b8123daeSJed Brown /*@C 2720b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 2721b8123daeSJed Brown called once at the beginning of each stage. 2722b8123daeSJed Brown 2723b8123daeSJed Brown Logically Collective on TS 2724b8123daeSJed Brown 2725b8123daeSJed Brown Input Parameters: 2726b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 2727b8123daeSJed Brown - func - The function 2728b8123daeSJed Brown 2729b8123daeSJed Brown Calling sequence of func: 2730b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 2731b8123daeSJed Brown 2732b8123daeSJed Brown Level: intermediate 2733b8123daeSJed Brown 2734b8123daeSJed Brown Note: 2735b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 2736b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 2737b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 2738b8123daeSJed Brown 2739b8123daeSJed Brown .keywords: TS, timestep 27409be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 2741b8123daeSJed Brown @*/ 2742b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 2743b8123daeSJed Brown { 2744b8123daeSJed Brown PetscFunctionBegin; 2745b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2746ae60f76fSBarry Smith ts->prestage = func; 2747b8123daeSJed Brown PetscFunctionReturn(0); 2748b8123daeSJed Brown } 2749b8123daeSJed Brown 2750b8123daeSJed Brown #undef __FUNCT__ 27519be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage" 27529be3e283SDebojyoti Ghosh /*@C 27539be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 27549be3e283SDebojyoti Ghosh called once at the end of each stage. 27559be3e283SDebojyoti Ghosh 27569be3e283SDebojyoti Ghosh Logically Collective on TS 27579be3e283SDebojyoti Ghosh 27589be3e283SDebojyoti Ghosh Input Parameters: 27599be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 27609be3e283SDebojyoti Ghosh - func - The function 27619be3e283SDebojyoti Ghosh 27629be3e283SDebojyoti Ghosh Calling sequence of func: 27639be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 27649be3e283SDebojyoti Ghosh 27659be3e283SDebojyoti Ghosh Level: intermediate 27669be3e283SDebojyoti Ghosh 27679be3e283SDebojyoti Ghosh Note: 27689be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 27699be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 27709be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 27719be3e283SDebojyoti Ghosh 27729be3e283SDebojyoti Ghosh .keywords: TS, timestep 27739be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 27749be3e283SDebojyoti Ghosh @*/ 27759be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 27769be3e283SDebojyoti Ghosh { 27779be3e283SDebojyoti Ghosh PetscFunctionBegin; 27789be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 27799be3e283SDebojyoti Ghosh ts->poststage = func; 27809be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 27819be3e283SDebojyoti Ghosh } 27829be3e283SDebojyoti Ghosh 27839be3e283SDebojyoti Ghosh #undef __FUNCT__ 2784b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 2785b8123daeSJed Brown /*@ 2786b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 2787b8123daeSJed Brown 2788b8123daeSJed Brown Collective on TS 2789b8123daeSJed Brown 2790b8123daeSJed Brown Input Parameters: 2791b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 27929be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 2793b8123daeSJed Brown 2794b8123daeSJed Brown Notes: 2795b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 2796b8123daeSJed Brown most users would not generally call this routine themselves. 2797b8123daeSJed Brown 2798b8123daeSJed Brown Level: developer 2799b8123daeSJed Brown 2800b8123daeSJed Brown .keywords: TS, timestep 28019be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 2802b8123daeSJed Brown @*/ 2803b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 2804b8123daeSJed Brown { 2805b8123daeSJed Brown PetscErrorCode ierr; 2806b8123daeSJed Brown 2807b8123daeSJed Brown PetscFunctionBegin; 2808b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2809ae60f76fSBarry Smith if (ts->prestage) { 2810ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 2811b8123daeSJed Brown } 2812b8123daeSJed Brown PetscFunctionReturn(0); 2813b8123daeSJed Brown } 2814b8123daeSJed Brown 2815b8123daeSJed Brown #undef __FUNCT__ 28169be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage" 28179be3e283SDebojyoti Ghosh /*@ 28189be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 28199be3e283SDebojyoti Ghosh 28209be3e283SDebojyoti Ghosh Collective on TS 28219be3e283SDebojyoti Ghosh 28229be3e283SDebojyoti Ghosh Input Parameters: 28239be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 28249be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 28259be3e283SDebojyoti Ghosh stageindex - Stage number 28269be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 28279be3e283SDebojyoti Ghosh of stages) with the stage solutions 28289be3e283SDebojyoti Ghosh 28299be3e283SDebojyoti Ghosh Notes: 28309be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 28319be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 28329be3e283SDebojyoti Ghosh 28339be3e283SDebojyoti Ghosh Level: developer 28349be3e283SDebojyoti Ghosh 28359be3e283SDebojyoti Ghosh .keywords: TS, timestep 28369be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 28379be3e283SDebojyoti Ghosh @*/ 28389be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 28399be3e283SDebojyoti Ghosh { 28409be3e283SDebojyoti Ghosh PetscErrorCode ierr; 28419be3e283SDebojyoti Ghosh 28429be3e283SDebojyoti Ghosh PetscFunctionBegin; 28439be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28444beae5d8SLisandro Dalcin if (ts->poststage) { 28459be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 28469be3e283SDebojyoti Ghosh } 28479be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 28489be3e283SDebojyoti Ghosh } 28499be3e283SDebojyoti Ghosh 28509be3e283SDebojyoti Ghosh #undef __FUNCT__ 2851e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 2852ac226902SBarry Smith /*@C 2853000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 28543f2090d5SJed Brown called once at the end of each time step. 2855000e7ae3SMatthew Knepley 28563f9fe445SBarry Smith Logically Collective on TS 2857000e7ae3SMatthew Knepley 2858000e7ae3SMatthew Knepley Input Parameters: 2859000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2860000e7ae3SMatthew Knepley - func - The function 2861000e7ae3SMatthew Knepley 2862000e7ae3SMatthew Knepley Calling sequence of func: 2863b8123daeSJed Brown $ func (TS ts); 2864000e7ae3SMatthew Knepley 2865000e7ae3SMatthew Knepley Level: intermediate 2866000e7ae3SMatthew Knepley 2867000e7ae3SMatthew Knepley .keywords: TS, timestep 2868b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 2869000e7ae3SMatthew Knepley @*/ 28707087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 2871000e7ae3SMatthew Knepley { 2872000e7ae3SMatthew Knepley PetscFunctionBegin; 28730700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2874ae60f76fSBarry Smith ts->poststep = func; 2875000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2876000e7ae3SMatthew Knepley } 2877000e7ae3SMatthew Knepley 2878e74ef692SMatthew Knepley #undef __FUNCT__ 28793f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 288009ee8438SJed Brown /*@ 28813f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 28823f2090d5SJed Brown 28833f2090d5SJed Brown Collective on TS 28843f2090d5SJed Brown 28853f2090d5SJed Brown Input Parameters: 28863f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 28873f2090d5SJed Brown 28883f2090d5SJed Brown Notes: 28893f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 28903f2090d5SJed Brown so most users would not generally call this routine themselves. 28913f2090d5SJed Brown 28923f2090d5SJed Brown Level: developer 28933f2090d5SJed Brown 28943f2090d5SJed Brown .keywords: TS, timestep 28953f2090d5SJed Brown @*/ 28967087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 28973f2090d5SJed Brown { 28983f2090d5SJed Brown PetscErrorCode ierr; 28993f2090d5SJed Brown 29003f2090d5SJed Brown PetscFunctionBegin; 29010700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2902ae60f76fSBarry Smith if (ts->poststep) { 2903ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 290472ac3e02SJed Brown } 29053f2090d5SJed Brown PetscFunctionReturn(0); 29063f2090d5SJed Brown } 29073f2090d5SJed Brown 2908d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 2909d763cef2SBarry Smith 29104a2ae208SSatish Balay #undef __FUNCT__ 2911a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 2912d763cef2SBarry Smith /*@C 2913a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 2914d763cef2SBarry Smith timestep to display the iteration's progress. 2915d763cef2SBarry Smith 29163f9fe445SBarry Smith Logically Collective on TS 2917d763cef2SBarry Smith 2918d763cef2SBarry Smith Input Parameters: 2919d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2920e213d8f1SJed Brown . monitor - monitoring routine 2921329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 29220298fd71SBarry Smith monitor routine (use NULL if no context is desired) 2923b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 29240298fd71SBarry Smith (may be NULL) 2925d763cef2SBarry Smith 2926e213d8f1SJed Brown Calling sequence of monitor: 29270910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 2928d763cef2SBarry Smith 2929d763cef2SBarry Smith + ts - the TS context 293088c05cc5SBarry 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 293188c05cc5SBarry Smith been interpolated to) 29321f06c33eSBarry Smith . time - current time 29330910c330SBarry Smith . u - current iterate 2934d763cef2SBarry Smith - mctx - [optional] monitoring context 2935d763cef2SBarry Smith 2936d763cef2SBarry Smith Notes: 2937d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 2938d763cef2SBarry Smith already has been loaded. 2939d763cef2SBarry Smith 2940025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 2941025f1a04SBarry Smith 2942d763cef2SBarry Smith Level: intermediate 2943d763cef2SBarry Smith 2944d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2945d763cef2SBarry Smith 2946a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 2947d763cef2SBarry Smith @*/ 2948c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 2949d763cef2SBarry Smith { 2950d763cef2SBarry Smith PetscFunctionBegin; 29510700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 295217186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2953d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 29548704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 2955d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 2956d763cef2SBarry Smith PetscFunctionReturn(0); 2957d763cef2SBarry Smith } 2958d763cef2SBarry Smith 29594a2ae208SSatish Balay #undef __FUNCT__ 2960a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 2961d763cef2SBarry Smith /*@C 2962a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 2963d763cef2SBarry Smith 29643f9fe445SBarry Smith Logically Collective on TS 2965d763cef2SBarry Smith 2966d763cef2SBarry Smith Input Parameters: 2967d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2968d763cef2SBarry Smith 2969d763cef2SBarry Smith Notes: 2970d763cef2SBarry Smith There is no way to remove a single, specific monitor. 2971d763cef2SBarry Smith 2972d763cef2SBarry Smith Level: intermediate 2973d763cef2SBarry Smith 2974d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2975d763cef2SBarry Smith 2976a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 2977d763cef2SBarry Smith @*/ 29787087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 2979d763cef2SBarry Smith { 2980d952e501SBarry Smith PetscErrorCode ierr; 2981d952e501SBarry Smith PetscInt i; 2982d952e501SBarry Smith 2983d763cef2SBarry Smith PetscFunctionBegin; 29840700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2985d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 29868704b422SBarry Smith if (ts->monitordestroy[i]) { 29878704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 2988d952e501SBarry Smith } 2989d952e501SBarry Smith } 2990d763cef2SBarry Smith ts->numbermonitors = 0; 2991d763cef2SBarry Smith PetscFunctionReturn(0); 2992d763cef2SBarry Smith } 2993d763cef2SBarry Smith 29944a2ae208SSatish Balay #undef __FUNCT__ 2995a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 2996d8e5e3e6SSatish Balay /*@ 2997a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 29985516499fSSatish Balay 29995516499fSSatish Balay Level: intermediate 300041251cbbSSatish Balay 30015516499fSSatish Balay .keywords: TS, set, monitor 30025516499fSSatish Balay 300341251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 300441251cbbSSatish Balay @*/ 3005649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 3006d763cef2SBarry Smith { 3007dfbe8321SBarry Smith PetscErrorCode ierr; 3008ce94432eSBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts)); 3009d132466eSBarry Smith 3010d763cef2SBarry Smith PetscFunctionBegin; 3011649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3012971832b6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr); 3013649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3014d763cef2SBarry Smith PetscFunctionReturn(0); 3015d763cef2SBarry Smith } 3016d763cef2SBarry Smith 30174a2ae208SSatish Balay #undef __FUNCT__ 3018cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 3019cd652676SJed Brown /*@ 3020cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 3021cd652676SJed Brown 3022cd652676SJed Brown Logically Collective on TS 3023cd652676SJed Brown 3024cd652676SJed Brown Input Argument: 3025cd652676SJed Brown . ts - time stepping context 3026cd652676SJed Brown 3027cd652676SJed Brown Output Argument: 3028cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 3029cd652676SJed Brown 3030cd652676SJed Brown Level: intermediate 3031cd652676SJed Brown 3032cd652676SJed Brown .keywords: TS, set 3033cd652676SJed Brown 3034cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 3035cd652676SJed Brown @*/ 3036cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 3037cd652676SJed Brown { 3038cd652676SJed Brown PetscFunctionBegin; 3039cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3040cd652676SJed Brown ts->retain_stages = flg; 3041cd652676SJed Brown PetscFunctionReturn(0); 3042cd652676SJed Brown } 3043cd652676SJed Brown 3044cd652676SJed Brown #undef __FUNCT__ 3045cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 3046cd652676SJed Brown /*@ 3047cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3048cd652676SJed Brown 3049cd652676SJed Brown Collective on TS 3050cd652676SJed Brown 3051cd652676SJed Brown Input Argument: 3052cd652676SJed Brown + ts - time stepping context 3053cd652676SJed Brown - t - time to interpolate to 3054cd652676SJed Brown 3055cd652676SJed Brown Output Argument: 30560910c330SBarry Smith . U - state at given time 3057cd652676SJed Brown 3058cd652676SJed Brown Notes: 3059cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 3060cd652676SJed Brown 3061cd652676SJed Brown Level: intermediate 3062cd652676SJed Brown 3063cd652676SJed Brown Developer Notes: 3064cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3065cd652676SJed Brown 3066cd652676SJed Brown .keywords: TS, set 3067cd652676SJed Brown 3068cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 3069cd652676SJed Brown @*/ 30700910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3071cd652676SJed Brown { 3072cd652676SJed Brown PetscErrorCode ierr; 3073cd652676SJed Brown 3074cd652676SJed Brown PetscFunctionBegin; 3075cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3076b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 30776712e2f1SBarry 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); 3078ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 30790910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3080cd652676SJed Brown PetscFunctionReturn(0); 3081cd652676SJed Brown } 3082cd652676SJed Brown 3083cd652676SJed Brown #undef __FUNCT__ 30844a2ae208SSatish Balay #define __FUNCT__ "TSStep" 3085d763cef2SBarry Smith /*@ 30866d9e5789SSean Farley TSStep - Steps one time step 3087d763cef2SBarry Smith 3088d763cef2SBarry Smith Collective on TS 3089d763cef2SBarry Smith 3090d763cef2SBarry Smith Input Parameter: 3091d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3092d763cef2SBarry Smith 30936d9e5789SSean Farley Level: intermediate 3094d763cef2SBarry Smith 3095b8123daeSJed Brown Notes: 3096b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3097b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3098b8123daeSJed Brown 309925cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 310025cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 310125cb2221SBarry Smith 3102d763cef2SBarry Smith .keywords: TS, timestep, solve 3103d763cef2SBarry Smith 31049be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3105d763cef2SBarry Smith @*/ 3106193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3107d763cef2SBarry Smith { 31082fb8446cSMatthew G. Knepley DM dm; 3109dfbe8321SBarry Smith PetscErrorCode ierr; 3110fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3111d763cef2SBarry Smith 3112d763cef2SBarry Smith PetscFunctionBegin; 31130700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3114fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3115fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3116fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3117fffbeea8SBarry Smith " type = {Preprint},\n" 3118fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3119fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3120fffbeea8SBarry Smith " year = {2014}\n}\n",&cite); 3121fffbeea8SBarry Smith 31222fb8446cSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3123d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 3124d405a339SMatthew Knepley 3125362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 312624655328SShri ts->ptime_prev = ts->ptime; 3127cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 31284e9ba7eeSMatthew G. Knepley ierr = VecViewFromOptions(ts->vec_sol, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr); 3129bbd56ea5SKarl Rupp 3130e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3131d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3132193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 313308c7845fSBarry Smith ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 313408c7845fSBarry Smith 313508c7845fSBarry Smith ts->time_step_prev = ts->ptime - ts->ptime_prev; 313608c7845fSBarry Smith ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 313708c7845fSBarry Smith 313808c7845fSBarry Smith if (ts->reason < 0) { 313908c7845fSBarry Smith if (ts->errorifstepfailed) { 314008c7845fSBarry Smith if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) 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]); 314108c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3142d2daff3dSHong Zhang } 314308c7845fSBarry Smith } else if (!ts->reason) { 314408c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 314508c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 314608c7845fSBarry Smith } 31472c18e0fdSBarry Smith ts->total_steps++; 314808c7845fSBarry Smith PetscFunctionReturn(0); 314908c7845fSBarry Smith } 315008c7845fSBarry Smith 315108c7845fSBarry Smith #undef __FUNCT__ 315208c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep" 315308c7845fSBarry Smith /*@ 315408c7845fSBarry Smith TSAdjointStep - Steps one time step 315508c7845fSBarry Smith 315608c7845fSBarry Smith Collective on TS 315708c7845fSBarry Smith 315808c7845fSBarry Smith Input Parameter: 315908c7845fSBarry Smith . ts - the TS context obtained from TSCreate() 316008c7845fSBarry Smith 316108c7845fSBarry Smith Level: intermediate 316208c7845fSBarry Smith 316308c7845fSBarry Smith Notes: 316408c7845fSBarry Smith The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 316508c7845fSBarry Smith be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 316608c7845fSBarry Smith 316708c7845fSBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 316808c7845fSBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 316908c7845fSBarry Smith 317008c7845fSBarry Smith .keywords: TS, timestep, solve 317108c7845fSBarry Smith 317208c7845fSBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 317308c7845fSBarry Smith @*/ 317408c7845fSBarry Smith PetscErrorCode TSAdjointStep(TS ts) 317508c7845fSBarry Smith { 317608c7845fSBarry Smith DM dm; 317708c7845fSBarry Smith PetscErrorCode ierr; 317808c7845fSBarry Smith 317908c7845fSBarry Smith PetscFunctionBegin; 318008c7845fSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 318108c7845fSBarry Smith ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 318208c7845fSBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 318308c7845fSBarry Smith 318408c7845fSBarry Smith ts->reason = TS_CONVERGED_ITERATING; 318508c7845fSBarry Smith ts->ptime_prev = ts->ptime; 318608c7845fSBarry Smith ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 318708c7845fSBarry Smith ierr = VecViewFromOptions(ts->vec_sol, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr); 318808c7845fSBarry Smith 318908c7845fSBarry Smith ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 319008c7845fSBarry Smith if (!ts->ops->stepadj) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed because the adjoint of %s has not been implemented, try other time stepping methods for adjoint sensitivity analysis",((PetscObject)ts)->type_name); 319108c7845fSBarry Smith ierr = (*ts->ops->stepadj)(ts);CHKERRQ(ierr); 3192d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3193bbd56ea5SKarl Rupp 319424655328SShri ts->time_step_prev = ts->ptime - ts->ptime_prev; 3195cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3196362cd11cSLisandro Dalcin 3197362cd11cSLisandro Dalcin if (ts->reason < 0) { 3198cef5090cSJed Brown if (ts->errorifstepfailed) { 3199cef5090cSJed Brown if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) { 3200ce94432eSBarry 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]); 32012a3a10ceSLisandro Dalcin } else if (ts->reason == TS_DIVERGED_STEP_REJECTED) { 32022a3a10ceSLisandro 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]); 3203ce94432eSBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3204cef5090cSJed Brown } 3205362cd11cSLisandro Dalcin } else if (!ts->reason) { 3206db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3207db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3208362cd11cSLisandro Dalcin } 32092c18e0fdSBarry Smith ts->total_steps--; 3210d763cef2SBarry Smith PetscFunctionReturn(0); 3211d763cef2SBarry Smith } 3212d763cef2SBarry Smith 32134a2ae208SSatish Balay #undef __FUNCT__ 321405175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 321505175c85SJed Brown /*@ 321605175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 321705175c85SJed Brown 32181c3436cfSJed Brown Collective on TS 321905175c85SJed Brown 322005175c85SJed Brown Input Arguments: 32211c3436cfSJed Brown + ts - time stepping context 32221c3436cfSJed Brown . order - desired order of accuracy 32230298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 322405175c85SJed Brown 322505175c85SJed Brown Output Arguments: 32260910c330SBarry Smith . U - state at the end of the current step 322705175c85SJed Brown 322805175c85SJed Brown Level: advanced 322905175c85SJed Brown 3230108c343cSJed Brown Notes: 3231108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3232108c343cSJed 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. 3233108c343cSJed Brown 32341c3436cfSJed Brown .seealso: TSStep(), TSAdapt 323505175c85SJed Brown @*/ 32360910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 323705175c85SJed Brown { 323805175c85SJed Brown PetscErrorCode ierr; 323905175c85SJed Brown 324005175c85SJed Brown PetscFunctionBegin; 324105175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 324205175c85SJed Brown PetscValidType(ts,1); 32430910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3244ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 32450910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 324605175c85SJed Brown PetscFunctionReturn(0); 324705175c85SJed Brown } 324805175c85SJed Brown 3249d67e68b7SBarry Smith 3250d67e68b7SBarry Smith #undef __FUNCT__ 32516a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 32526a4d4014SLisandro Dalcin /*@ 32536a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 32546a4d4014SLisandro Dalcin 32556a4d4014SLisandro Dalcin Collective on TS 32566a4d4014SLisandro Dalcin 32576a4d4014SLisandro Dalcin Input Parameter: 32586a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 3259cc708dedSBarry Smith - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 32605a3a76d0SJed Brown 32616a4d4014SLisandro Dalcin Level: beginner 32626a4d4014SLisandro Dalcin 32635a3a76d0SJed Brown Notes: 32645a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 32655a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 32665a3a76d0SJed Brown stepped over the final time. 32675a3a76d0SJed Brown 32686a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 32696a4d4014SLisandro Dalcin 32706a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 32716a4d4014SLisandro Dalcin @*/ 3272cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 32736a4d4014SLisandro Dalcin { 3274b06615a5SLisandro Dalcin Vec solution; 32756a4d4014SLisandro Dalcin PetscErrorCode ierr; 3276f22f69f0SBarry Smith 32776a4d4014SLisandro Dalcin PetscFunctionBegin; 32780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3279f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 3280c88f2d44SBarry Smith 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 */ 3281b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 32820910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 3283b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3284b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3285b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 32865a3a76d0SJed Brown } 32870910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3288bbd56ea5SKarl Rupp } else if (u) { 32890910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 32905a3a76d0SJed Brown } 3291d2daff3dSHong Zhang ierr = TSSetUp(ts);CHKERRQ(ierr); /*compute adj coefficients if the reverse mode is on*/ 32926a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 3293193ac0bcSJed Brown ts->steps = 0; 32945ef26d82SJed Brown ts->ksp_its = 0; 32955ef26d82SJed Brown ts->snes_its = 0; 3296c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3297c610991cSLisandro Dalcin ts->reject = 0; 3298193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3299193ac0bcSJed Brown 3300ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3301f05ece33SBarry Smith 3302193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3303193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 33040910c330SBarry Smith ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 3305cc708dedSBarry Smith ts->solvetime = ts->ptime; 3306193ac0bcSJed Brown } else { 33076a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 3308db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3309c88f2d44SBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3310e1a7a14fSJed Brown while (!ts->reason) { 3311b06615a5SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 3312*bc952696SBarry Smith ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 3313193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 3314aeb4809dSShri Abhyankar if (ts->event) { 3315aeb4809dSShri Abhyankar ierr = TSEventMonitor(ts);CHKERRQ(ierr); 33161eda64f1SShri Abhyankar if (ts->event->status != TSEVENT_PROCESSING) { 33171eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 33181eda64f1SShri Abhyankar } 33191eda64f1SShri Abhyankar } else { 33201eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 3321aeb4809dSShri Abhyankar } 3322193ac0bcSJed Brown } 3323c88f2d44SBarry Smith if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 33240910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 3325cc708dedSBarry Smith ts->solvetime = ts->max_time; 3326b06615a5SLisandro Dalcin solution = u; 33270574a7fbSJed Brown } else { 3328ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3329cc708dedSBarry Smith ts->solvetime = ts->ptime; 3330b06615a5SLisandro Dalcin solution = ts->vec_sol; 33310574a7fbSJed Brown } 3332*bc952696SBarry Smith ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr); 3333b06615a5SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr); 33344e9ba7eeSMatthew G. Knepley ierr = VecViewFromOptions(u, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr); 3335193ac0bcSJed Brown } 3336d2daff3dSHong Zhang 3337ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 333856f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 33396a4d4014SLisandro Dalcin PetscFunctionReturn(0); 33406a4d4014SLisandro Dalcin } 33416a4d4014SLisandro Dalcin 33426a4d4014SLisandro Dalcin #undef __FUNCT__ 3343c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve" 3344c88f2d44SBarry Smith /*@ 3345c88f2d44SBarry Smith TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 3346c88f2d44SBarry Smith 3347c88f2d44SBarry Smith Collective on TS 3348c88f2d44SBarry Smith 3349c88f2d44SBarry Smith Input Parameter: 3350c88f2d44SBarry Smith + ts - the TS context obtained from TSCreate() 3351c88f2d44SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 3352c88f2d44SBarry Smith 3353c88f2d44SBarry Smith Level: intermediate 3354c88f2d44SBarry Smith 3355c88f2d44SBarry Smith Notes: 3356c88f2d44SBarry Smith This must be called after a call to TSSolve() that solves the forward problem 3357c88f2d44SBarry Smith 3358c88f2d44SBarry Smith .keywords: TS, timestep, solve 3359c88f2d44SBarry Smith 3360c88f2d44SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep() 3361c88f2d44SBarry Smith @*/ 3362c88f2d44SBarry Smith PetscErrorCode TSAdjointSolve(TS ts,Vec u) 3363c88f2d44SBarry Smith { 3364c88f2d44SBarry Smith PetscErrorCode ierr; 3365c88f2d44SBarry Smith 3366c88f2d44SBarry Smith PetscFunctionBegin; 3367c88f2d44SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3368c88f2d44SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 3369c88f2d44SBarry Smith if (u) { 3370c88f2d44SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 3371c88f2d44SBarry Smith } 3372c88f2d44SBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3373c88f2d44SBarry Smith /* reset time step and iteration counters */ 3374c88f2d44SBarry Smith ts->steps = 0; 3375c88f2d44SBarry Smith ts->ksp_its = 0; 3376c88f2d44SBarry Smith ts->snes_its = 0; 3377c88f2d44SBarry Smith ts->num_snes_failures = 0; 3378c88f2d44SBarry Smith ts->reject = 0; 3379c88f2d44SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 3380c88f2d44SBarry Smith 3381c88f2d44SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3382c88f2d44SBarry Smith 3383c88f2d44SBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3384c88f2d44SBarry Smith while (!ts->reason) { 3385*bc952696SBarry Smith ierr = TSTrajectoryGet(ts->trajectory,ts,ts->max_steps-ts->steps,ts->ptime);CHKERRQ(ierr); 3386c88f2d44SBarry Smith ierr = TSMonitor(ts,ts->max_steps-ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 338708c7845fSBarry Smith ierr = TSAdjointStep(ts);CHKERRQ(ierr); 3388c88f2d44SBarry Smith if (ts->event) { 3389c88f2d44SBarry Smith ierr = TSEventMonitor(ts);CHKERRQ(ierr); 3390c88f2d44SBarry Smith if (ts->event->status != TSEVENT_PROCESSING) { 3391c88f2d44SBarry Smith ierr = TSPostStep(ts);CHKERRQ(ierr); 3392c88f2d44SBarry Smith } 3393c88f2d44SBarry Smith } else { 3394c88f2d44SBarry Smith ierr = TSPostStep(ts);CHKERRQ(ierr); 3395c88f2d44SBarry Smith } 3396c88f2d44SBarry Smith } 3397c88f2d44SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3398c88f2d44SBarry Smith ts->solvetime = ts->ptime; 3399d67e68b7SBarry Smith ierr = TSMonitor(ts,0,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 3400c88f2d44SBarry Smith ierr = VecViewFromOptions(u, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr); 3401c88f2d44SBarry Smith 3402c88f2d44SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 3403c88f2d44SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 3404c88f2d44SBarry Smith PetscFunctionReturn(0); 3405c88f2d44SBarry Smith } 3406c88f2d44SBarry Smith 3407c88f2d44SBarry Smith #undef __FUNCT__ 34084a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 3409228d79bcSJed Brown /*@ 3410228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 3411228d79bcSJed Brown 3412228d79bcSJed Brown Collective on TS 3413228d79bcSJed Brown 3414228d79bcSJed Brown Input Parameters: 3415228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 3416228d79bcSJed Brown . step - step number that has just completed 3417228d79bcSJed Brown . ptime - model time of the state 34180910c330SBarry Smith - u - state at the current model time 3419228d79bcSJed Brown 3420228d79bcSJed Brown Notes: 3421228d79bcSJed Brown TSMonitor() is typically used within the time stepping implementations. 3422228d79bcSJed Brown Users might call this function when using the TSStep() interface instead of TSSolve(). 3423228d79bcSJed Brown 3424228d79bcSJed Brown Level: advanced 3425228d79bcSJed Brown 3426228d79bcSJed Brown .keywords: TS, timestep 3427228d79bcSJed Brown @*/ 34280910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 3429d763cef2SBarry Smith { 34306849ba73SBarry Smith PetscErrorCode ierr; 3431a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 3432d763cef2SBarry Smith 3433d763cef2SBarry Smith PetscFunctionBegin; 3434b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3435b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 34365edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 3437d763cef2SBarry Smith for (i=0; i<n; i++) { 34380910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 3439d763cef2SBarry Smith } 34405edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 3441d763cef2SBarry Smith PetscFunctionReturn(0); 3442d763cef2SBarry Smith } 3443d763cef2SBarry Smith 3444d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 34454a2ae208SSatish Balay #undef __FUNCT__ 3446a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 3447d763cef2SBarry Smith /*@C 3448a9f9c1f6SBarry Smith TSMonitorLGCtxCreate - Creates a line graph context for use with 3449a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 3450d763cef2SBarry Smith 3451d763cef2SBarry Smith Collective on TS 3452d763cef2SBarry Smith 3453d763cef2SBarry Smith Input Parameters: 3454d763cef2SBarry Smith + host - the X display to open, or null for the local machine 3455d763cef2SBarry Smith . label - the title to put in the title bar 34567c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 3457a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 3458a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 3459d763cef2SBarry Smith 3460d763cef2SBarry Smith Output Parameter: 34610b039ecaSBarry Smith . ctx - the context 3462d763cef2SBarry Smith 3463d763cef2SBarry Smith Options Database Key: 34644f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 34654f09c107SBarry Smith . -ts_monitor_lg_solution - 346699fdda47SBarry Smith . -ts_monitor_lg_error - 346799fdda47SBarry Smith . -ts_monitor_lg_ksp_iterations - 346899fdda47SBarry Smith . -ts_monitor_lg_snes_iterations - 346999fdda47SBarry Smith - -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true 3470d763cef2SBarry Smith 3471d763cef2SBarry Smith Notes: 3472a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 3473d763cef2SBarry Smith 3474d763cef2SBarry Smith Level: intermediate 3475d763cef2SBarry Smith 34767c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 3477d763cef2SBarry Smith 34784f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 34797c922b88SBarry Smith 3480d763cef2SBarry Smith @*/ 3481a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 3482d763cef2SBarry Smith { 3483b0a32e0cSBarry Smith PetscDraw win; 3484dfbe8321SBarry Smith PetscErrorCode ierr; 3485d763cef2SBarry Smith 3486d763cef2SBarry Smith PetscFunctionBegin; 3487b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 3488a80ad3e0SBarry Smith ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr); 34893fbbecb0SBarry Smith ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr); 34900b039ecaSBarry Smith ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr); 34913bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr); 3492287de1a7SBarry Smith ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr); 3493287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 3494a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 3495d763cef2SBarry Smith PetscFunctionReturn(0); 3496d763cef2SBarry Smith } 3497d763cef2SBarry Smith 34984a2ae208SSatish Balay #undef __FUNCT__ 34994f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 3500b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 3501d763cef2SBarry Smith { 35020b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 3503c32365f1SBarry Smith PetscReal x = ptime,y; 3504dfbe8321SBarry Smith PetscErrorCode ierr; 3505d763cef2SBarry Smith 3506d763cef2SBarry Smith PetscFunctionBegin; 3507b06615a5SLisandro Dalcin if (!step) { 3508a9f9c1f6SBarry Smith PetscDrawAxis axis; 3509a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 3510a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 3511a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 3512287de1a7SBarry Smith ierr = PetscDrawLGIndicateDataPoints(ctx->lg,PETSC_TRUE);CHKERRQ(ierr); 3513a9f9c1f6SBarry Smith } 3514c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 35150b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 3516b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 35170b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 35183923b477SBarry Smith } 3519d763cef2SBarry Smith PetscFunctionReturn(0); 3520d763cef2SBarry Smith } 3521d763cef2SBarry Smith 35224a2ae208SSatish Balay #undef __FUNCT__ 3523a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 3524d763cef2SBarry Smith /*@C 3525a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 3526a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 3527d763cef2SBarry Smith 35280b039ecaSBarry Smith Collective on TSMonitorLGCtx 3529d763cef2SBarry Smith 3530d763cef2SBarry Smith Input Parameter: 35310b039ecaSBarry Smith . ctx - the monitor context 3532d763cef2SBarry Smith 3533d763cef2SBarry Smith Level: intermediate 3534d763cef2SBarry Smith 3535d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 3536d763cef2SBarry Smith 35374f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 3538d763cef2SBarry Smith @*/ 3539a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 3540d763cef2SBarry Smith { 3541b0a32e0cSBarry Smith PetscDraw draw; 3542dfbe8321SBarry Smith PetscErrorCode ierr; 3543d763cef2SBarry Smith 3544d763cef2SBarry Smith PetscFunctionBegin; 35450b039ecaSBarry Smith ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr); 35466bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 35470b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 35480b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 3549d763cef2SBarry Smith PetscFunctionReturn(0); 3550d763cef2SBarry Smith } 3551d763cef2SBarry Smith 35524a2ae208SSatish Balay #undef __FUNCT__ 35534a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 3554d763cef2SBarry Smith /*@ 3555b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 3556d763cef2SBarry Smith 3557d763cef2SBarry Smith Not Collective 3558d763cef2SBarry Smith 3559d763cef2SBarry Smith Input Parameter: 3560d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3561d763cef2SBarry Smith 3562d763cef2SBarry Smith Output Parameter: 3563d763cef2SBarry Smith . t - the current time 3564d763cef2SBarry Smith 3565d763cef2SBarry Smith Level: beginner 3566d763cef2SBarry Smith 3567b8123daeSJed Brown Note: 3568b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 35699be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 3570b8123daeSJed Brown 3571d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3572d763cef2SBarry Smith 3573d763cef2SBarry Smith .keywords: TS, get, time 3574d763cef2SBarry Smith @*/ 35757087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 3576d763cef2SBarry Smith { 3577d763cef2SBarry Smith PetscFunctionBegin; 35780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3579f7cf8827SBarry Smith PetscValidRealPointer(t,2); 3580d763cef2SBarry Smith *t = ts->ptime; 3581d763cef2SBarry Smith PetscFunctionReturn(0); 3582d763cef2SBarry Smith } 3583d763cef2SBarry Smith 35844a2ae208SSatish Balay #undef __FUNCT__ 3585e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime" 3586e5e524a1SHong Zhang /*@ 3587e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 3588e5e524a1SHong Zhang 3589e5e524a1SHong Zhang Not Collective 3590e5e524a1SHong Zhang 3591e5e524a1SHong Zhang Input Parameter: 3592e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 3593e5e524a1SHong Zhang 3594e5e524a1SHong Zhang Output Parameter: 3595e5e524a1SHong Zhang . t - the previous time 3596e5e524a1SHong Zhang 3597e5e524a1SHong Zhang Level: beginner 3598e5e524a1SHong Zhang 3599e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3600e5e524a1SHong Zhang 3601e5e524a1SHong Zhang .keywords: TS, get, time 3602e5e524a1SHong Zhang @*/ 3603e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 3604e5e524a1SHong Zhang { 3605e5e524a1SHong Zhang PetscFunctionBegin; 3606e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3607e5e524a1SHong Zhang PetscValidRealPointer(t,2); 3608e5e524a1SHong Zhang *t = ts->ptime_prev; 3609e5e524a1SHong Zhang PetscFunctionReturn(0); 3610e5e524a1SHong Zhang } 3611e5e524a1SHong Zhang 3612e5e524a1SHong Zhang #undef __FUNCT__ 36136a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 36146a4d4014SLisandro Dalcin /*@ 36156a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 36166a4d4014SLisandro Dalcin 36173f9fe445SBarry Smith Logically Collective on TS 36186a4d4014SLisandro Dalcin 36196a4d4014SLisandro Dalcin Input Parameters: 36206a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 36216a4d4014SLisandro Dalcin - time - the time 36226a4d4014SLisandro Dalcin 36236a4d4014SLisandro Dalcin Level: intermediate 36246a4d4014SLisandro Dalcin 36256a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 36266a4d4014SLisandro Dalcin 36276a4d4014SLisandro Dalcin .keywords: TS, set, time 36286a4d4014SLisandro Dalcin @*/ 36297087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 36306a4d4014SLisandro Dalcin { 36316a4d4014SLisandro Dalcin PetscFunctionBegin; 36320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3633c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 36346a4d4014SLisandro Dalcin ts->ptime = t; 36356a4d4014SLisandro Dalcin PetscFunctionReturn(0); 36366a4d4014SLisandro Dalcin } 36376a4d4014SLisandro Dalcin 36386a4d4014SLisandro Dalcin #undef __FUNCT__ 36394a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 3640d763cef2SBarry Smith /*@C 3641d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 3642d763cef2SBarry Smith TS options in the database. 3643d763cef2SBarry Smith 36443f9fe445SBarry Smith Logically Collective on TS 3645d763cef2SBarry Smith 3646d763cef2SBarry Smith Input Parameter: 3647d763cef2SBarry Smith + ts - The TS context 3648d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3649d763cef2SBarry Smith 3650d763cef2SBarry Smith Notes: 3651d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3652d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3653d763cef2SBarry Smith hyphen. 3654d763cef2SBarry Smith 3655d763cef2SBarry Smith Level: advanced 3656d763cef2SBarry Smith 3657d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 3658d763cef2SBarry Smith 3659d763cef2SBarry Smith .seealso: TSSetFromOptions() 3660d763cef2SBarry Smith 3661d763cef2SBarry Smith @*/ 36627087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 3663d763cef2SBarry Smith { 3664dfbe8321SBarry Smith PetscErrorCode ierr; 3665089b2837SJed Brown SNES snes; 3666d763cef2SBarry Smith 3667d763cef2SBarry Smith PetscFunctionBegin; 36680700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3669d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3670089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3671089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3672d763cef2SBarry Smith PetscFunctionReturn(0); 3673d763cef2SBarry Smith } 3674d763cef2SBarry Smith 3675d763cef2SBarry Smith 36764a2ae208SSatish Balay #undef __FUNCT__ 36774a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 3678d763cef2SBarry Smith /*@C 3679d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 3680d763cef2SBarry Smith TS options in the database. 3681d763cef2SBarry Smith 36823f9fe445SBarry Smith Logically Collective on TS 3683d763cef2SBarry Smith 3684d763cef2SBarry Smith Input Parameter: 3685d763cef2SBarry Smith + ts - The TS context 3686d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3687d763cef2SBarry Smith 3688d763cef2SBarry Smith Notes: 3689d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3690d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3691d763cef2SBarry Smith hyphen. 3692d763cef2SBarry Smith 3693d763cef2SBarry Smith Level: advanced 3694d763cef2SBarry Smith 3695d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 3696d763cef2SBarry Smith 3697d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 3698d763cef2SBarry Smith 3699d763cef2SBarry Smith @*/ 37007087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 3701d763cef2SBarry Smith { 3702dfbe8321SBarry Smith PetscErrorCode ierr; 3703089b2837SJed Brown SNES snes; 3704d763cef2SBarry Smith 3705d763cef2SBarry Smith PetscFunctionBegin; 37060700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3707d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3708089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3709089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3710d763cef2SBarry Smith PetscFunctionReturn(0); 3711d763cef2SBarry Smith } 3712d763cef2SBarry Smith 37134a2ae208SSatish Balay #undef __FUNCT__ 37144a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 3715d763cef2SBarry Smith /*@C 3716d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 3717d763cef2SBarry Smith TS options in the database. 3718d763cef2SBarry Smith 3719d763cef2SBarry Smith Not Collective 3720d763cef2SBarry Smith 3721d763cef2SBarry Smith Input Parameter: 3722d763cef2SBarry Smith . ts - The TS context 3723d763cef2SBarry Smith 3724d763cef2SBarry Smith Output Parameter: 3725d763cef2SBarry Smith . prefix - A pointer to the prefix string used 3726d763cef2SBarry Smith 3727d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 3728d763cef2SBarry Smith sufficient length to hold the prefix. 3729d763cef2SBarry Smith 3730d763cef2SBarry Smith Level: intermediate 3731d763cef2SBarry Smith 3732d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 3733d763cef2SBarry Smith 3734d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 3735d763cef2SBarry Smith @*/ 37367087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 3737d763cef2SBarry Smith { 3738dfbe8321SBarry Smith PetscErrorCode ierr; 3739d763cef2SBarry Smith 3740d763cef2SBarry Smith PetscFunctionBegin; 37410700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 37424482741eSBarry Smith PetscValidPointer(prefix,2); 3743d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3744d763cef2SBarry Smith PetscFunctionReturn(0); 3745d763cef2SBarry Smith } 3746d763cef2SBarry Smith 37474a2ae208SSatish Balay #undef __FUNCT__ 37484a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 3749d763cef2SBarry Smith /*@C 3750d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 3751d763cef2SBarry Smith 3752d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 3753d763cef2SBarry Smith 3754d763cef2SBarry Smith Input Parameter: 3755d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 3756d763cef2SBarry Smith 3757d763cef2SBarry Smith Output Parameters: 3758e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 3759e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 3760e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 3761e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 3762d763cef2SBarry Smith 37630298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 3764d763cef2SBarry Smith 3765d763cef2SBarry Smith Level: intermediate 3766d763cef2SBarry Smith 376726d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 3768d763cef2SBarry Smith 3769d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 3770d763cef2SBarry Smith @*/ 3771e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 3772d763cef2SBarry Smith { 3773089b2837SJed Brown PetscErrorCode ierr; 3774089b2837SJed Brown SNES snes; 377524989b8cSPeter Brune DM dm; 3776089b2837SJed Brown 3777d763cef2SBarry Smith PetscFunctionBegin; 3778089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3779e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 378024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 378124989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 3782d763cef2SBarry Smith PetscFunctionReturn(0); 3783d763cef2SBarry Smith } 3784d763cef2SBarry Smith 37851713a123SBarry Smith #undef __FUNCT__ 37862eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 37872eca1d9cSJed Brown /*@C 37882eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 37892eca1d9cSJed Brown 37902eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 37912eca1d9cSJed Brown 37922eca1d9cSJed Brown Input Parameter: 37932eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 37942eca1d9cSJed Brown 37952eca1d9cSJed Brown Output Parameters: 3796e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 3797e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 37982eca1d9cSJed Brown . f - The function to compute the matrices 37992eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 38002eca1d9cSJed Brown 38010298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 38022eca1d9cSJed Brown 38032eca1d9cSJed Brown Level: advanced 38042eca1d9cSJed Brown 38052eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 38062eca1d9cSJed Brown 38072eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 38082eca1d9cSJed Brown @*/ 3809e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 38102eca1d9cSJed Brown { 3811089b2837SJed Brown PetscErrorCode ierr; 3812089b2837SJed Brown SNES snes; 381324989b8cSPeter Brune DM dm; 38140910c330SBarry Smith 38152eca1d9cSJed Brown PetscFunctionBegin; 3816089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3817f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 3818e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 381924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 382024989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 38212eca1d9cSJed Brown PetscFunctionReturn(0); 38222eca1d9cSJed Brown } 38232eca1d9cSJed Brown 38246083293cSBarry Smith 38252eca1d9cSJed Brown #undef __FUNCT__ 382683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 38271713a123SBarry Smith /*@C 382883a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 38291713a123SBarry Smith VecView() for the solution at each timestep 38301713a123SBarry Smith 38311713a123SBarry Smith Collective on TS 38321713a123SBarry Smith 38331713a123SBarry Smith Input Parameters: 38341713a123SBarry Smith + ts - the TS context 38351713a123SBarry Smith . step - current time-step 3836142b95e3SSatish Balay . ptime - current time 38370298fd71SBarry Smith - dummy - either a viewer or NULL 38381713a123SBarry Smith 383999fdda47SBarry Smith Options Database: 384099fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 384199fdda47SBarry Smith 384299fdda47SBarry 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 384399fdda47SBarry Smith will look bad 384499fdda47SBarry Smith 38451713a123SBarry Smith Level: intermediate 38461713a123SBarry Smith 38471713a123SBarry Smith .keywords: TS, vector, monitor, view 38481713a123SBarry Smith 3849a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 38501713a123SBarry Smith @*/ 38510910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 38521713a123SBarry Smith { 3853dfbe8321SBarry Smith PetscErrorCode ierr; 385483a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 3855473a3ab2SBarry Smith PetscDraw draw; 38561713a123SBarry Smith 38571713a123SBarry Smith PetscFunctionBegin; 38586083293cSBarry Smith if (!step && ictx->showinitial) { 38596083293cSBarry Smith if (!ictx->initialsolution) { 38600910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 38611713a123SBarry Smith } 38620910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 38636083293cSBarry Smith } 3864b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 38650dcf80beSBarry Smith 38666083293cSBarry Smith if (ictx->showinitial) { 38676083293cSBarry Smith PetscReal pause; 38686083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 38696083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 38706083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 38716083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 38726083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 38736083293cSBarry Smith } 38740910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 3875473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 3876473a3ab2SBarry Smith PetscReal xl,yl,xr,yr,tw,w,h; 3877473a3ab2SBarry Smith char time[32]; 3878473a3ab2SBarry Smith size_t len; 3879473a3ab2SBarry Smith 3880473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 3881473a3ab2SBarry Smith ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr); 3882473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 3883473a3ab2SBarry Smith ierr = PetscStrlen(time,&len);CHKERRQ(ierr); 38840298fd71SBarry Smith ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr); 3885473a3ab2SBarry Smith w = xl + .5*(xr - xl) - .5*len*tw; 3886473a3ab2SBarry Smith h = yl + .95*(yr - yl); 3887473a3ab2SBarry Smith ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 3888473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 3889473a3ab2SBarry Smith } 3890473a3ab2SBarry Smith 38916083293cSBarry Smith if (ictx->showinitial) { 38926083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 38936083293cSBarry Smith } 38941713a123SBarry Smith PetscFunctionReturn(0); 38951713a123SBarry Smith } 38961713a123SBarry Smith 38972d5ee99bSBarry Smith #undef __FUNCT__ 38982d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase" 38992d5ee99bSBarry Smith /*@C 39002d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 39012d5ee99bSBarry Smith 39022d5ee99bSBarry Smith Collective on TS 39032d5ee99bSBarry Smith 39042d5ee99bSBarry Smith Input Parameters: 39052d5ee99bSBarry Smith + ts - the TS context 39062d5ee99bSBarry Smith . step - current time-step 39072d5ee99bSBarry Smith . ptime - current time 39082d5ee99bSBarry Smith - dummy - either a viewer or NULL 39092d5ee99bSBarry Smith 39102d5ee99bSBarry Smith Level: intermediate 39112d5ee99bSBarry Smith 39122d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 39132d5ee99bSBarry Smith 39142d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 39152d5ee99bSBarry Smith @*/ 39162d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 39172d5ee99bSBarry Smith { 39182d5ee99bSBarry Smith PetscErrorCode ierr; 39192d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 39202d5ee99bSBarry Smith PetscDraw draw; 39212d5ee99bSBarry Smith MPI_Comm comm; 39222d5ee99bSBarry Smith PetscInt n; 39232d5ee99bSBarry Smith PetscMPIInt size; 39242d5ee99bSBarry Smith PetscReal xl,yl,xr,yr,tw,w,h; 39252d5ee99bSBarry Smith char time[32]; 39262d5ee99bSBarry Smith size_t len; 39272d5ee99bSBarry Smith const PetscScalar *U; 39282d5ee99bSBarry Smith 39292d5ee99bSBarry Smith PetscFunctionBegin; 39302d5ee99bSBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 39312d5ee99bSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 39322d5ee99bSBarry Smith if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs"); 39332d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 39342d5ee99bSBarry Smith if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 39352d5ee99bSBarry Smith 39362d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 39372d5ee99bSBarry Smith 39382d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 39394b363babSBarry Smith ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 3940ba5783adSMatthew G Knepley if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) { 3941a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 3942a4494fc1SBarry Smith PetscFunctionReturn(0); 3943a4494fc1SBarry Smith } 39442d5ee99bSBarry Smith if (!step) ictx->color++; 39452d5ee99bSBarry Smith ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr); 39462d5ee99bSBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 39472d5ee99bSBarry Smith 39482d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 39494b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 39502d5ee99bSBarry Smith ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr); 39512d5ee99bSBarry Smith ierr = PetscStrlen(time,&len);CHKERRQ(ierr); 39522d5ee99bSBarry Smith ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr); 39532d5ee99bSBarry Smith w = xl + .5*(xr - xl) - .5*len*tw; 39542d5ee99bSBarry Smith h = yl + .95*(yr - yl); 39552d5ee99bSBarry Smith ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 39562d5ee99bSBarry Smith } 39572d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 39582d5ee99bSBarry Smith PetscFunctionReturn(0); 39592d5ee99bSBarry Smith } 39602d5ee99bSBarry Smith 39611713a123SBarry Smith 39626c699258SBarry Smith #undef __FUNCT__ 396383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 39646083293cSBarry Smith /*@C 396583a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 39666083293cSBarry Smith 39676083293cSBarry Smith Collective on TS 39686083293cSBarry Smith 39696083293cSBarry Smith Input Parameters: 39706083293cSBarry Smith . ctx - the monitor context 39716083293cSBarry Smith 39726083293cSBarry Smith Level: intermediate 39736083293cSBarry Smith 39746083293cSBarry Smith .keywords: TS, vector, monitor, view 39756083293cSBarry Smith 397683a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 39776083293cSBarry Smith @*/ 397883a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 39796083293cSBarry Smith { 39806083293cSBarry Smith PetscErrorCode ierr; 39816083293cSBarry Smith 39826083293cSBarry Smith PetscFunctionBegin; 39834b363babSBarry Smith ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr); 398483a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 398583a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 398683a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 39876083293cSBarry Smith PetscFunctionReturn(0); 39886083293cSBarry Smith } 39896083293cSBarry Smith 39906083293cSBarry Smith #undef __FUNCT__ 399183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 39926083293cSBarry Smith /*@C 399383a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 39946083293cSBarry Smith 39956083293cSBarry Smith Collective on TS 39966083293cSBarry Smith 39976083293cSBarry Smith Input Parameter: 39986083293cSBarry Smith . ts - time-step context 39996083293cSBarry Smith 40006083293cSBarry Smith Output Patameter: 40016083293cSBarry Smith . ctx - the monitor context 40026083293cSBarry Smith 400399fdda47SBarry Smith Options Database: 400499fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 400599fdda47SBarry Smith 40066083293cSBarry Smith Level: intermediate 40076083293cSBarry Smith 40086083293cSBarry Smith .keywords: TS, vector, monitor, view 40096083293cSBarry Smith 401083a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 40116083293cSBarry Smith @*/ 401283a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 40136083293cSBarry Smith { 40146083293cSBarry Smith PetscErrorCode ierr; 40156083293cSBarry Smith 40166083293cSBarry Smith PetscFunctionBegin; 4017b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 401883a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4019e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4020bbd56ea5SKarl Rupp 402183a4ac43SBarry Smith (*ctx)->howoften = howoften; 4022473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 40230298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4024473a3ab2SBarry Smith 4025473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 40260298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 40272d5ee99bSBarry Smith (*ctx)->color = PETSC_DRAW_WHITE; 40286083293cSBarry Smith PetscFunctionReturn(0); 40296083293cSBarry Smith } 40306083293cSBarry Smith 40316083293cSBarry Smith #undef __FUNCT__ 403283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 40333a471f94SBarry Smith /*@C 403483a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 40353a471f94SBarry Smith VecView() for the error at each timestep 40363a471f94SBarry Smith 40373a471f94SBarry Smith Collective on TS 40383a471f94SBarry Smith 40393a471f94SBarry Smith Input Parameters: 40403a471f94SBarry Smith + ts - the TS context 40413a471f94SBarry Smith . step - current time-step 40423a471f94SBarry Smith . ptime - current time 40430298fd71SBarry Smith - dummy - either a viewer or NULL 40443a471f94SBarry Smith 40453a471f94SBarry Smith Level: intermediate 40463a471f94SBarry Smith 40473a471f94SBarry Smith .keywords: TS, vector, monitor, view 40483a471f94SBarry Smith 40493a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 40503a471f94SBarry Smith @*/ 40510910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 40523a471f94SBarry Smith { 40533a471f94SBarry Smith PetscErrorCode ierr; 405483a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 405583a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 40563a471f94SBarry Smith Vec work; 40573a471f94SBarry Smith 40583a471f94SBarry Smith PetscFunctionBegin; 4059b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 40600910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 40613a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 40620910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 40633a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 40643a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 40653a471f94SBarry Smith PetscFunctionReturn(0); 40663a471f94SBarry Smith } 40673a471f94SBarry Smith 40682a34c10cSBarry Smith #include <petsc-private/dmimpl.h> 40693a471f94SBarry Smith #undef __FUNCT__ 40706c699258SBarry Smith #define __FUNCT__ "TSSetDM" 40716c699258SBarry Smith /*@ 40726c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 40736c699258SBarry Smith 40743f9fe445SBarry Smith Logically Collective on TS and DM 40756c699258SBarry Smith 40766c699258SBarry Smith Input Parameters: 40776c699258SBarry Smith + ts - the preconditioner context 40786c699258SBarry Smith - dm - the dm 40796c699258SBarry Smith 40806c699258SBarry Smith Level: intermediate 40816c699258SBarry Smith 40826c699258SBarry Smith 40836c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 40846c699258SBarry Smith @*/ 40857087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 40866c699258SBarry Smith { 40876c699258SBarry Smith PetscErrorCode ierr; 4088089b2837SJed Brown SNES snes; 4089942e3340SBarry Smith DMTS tsdm; 40906c699258SBarry Smith 40916c699258SBarry Smith PetscFunctionBegin; 40920700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 409370663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4094942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 40952a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4096942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4097942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 409824989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 409924989b8cSPeter Brune tsdm->originaldm = dm; 410024989b8cSPeter Brune } 410124989b8cSPeter Brune } 41026bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 410324989b8cSPeter Brune } 41046c699258SBarry Smith ts->dm = dm; 4105bbd56ea5SKarl Rupp 4106089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4107089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 41086c699258SBarry Smith PetscFunctionReturn(0); 41096c699258SBarry Smith } 41106c699258SBarry Smith 41116c699258SBarry Smith #undef __FUNCT__ 41126c699258SBarry Smith #define __FUNCT__ "TSGetDM" 41136c699258SBarry Smith /*@ 41146c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 41156c699258SBarry Smith 41163f9fe445SBarry Smith Not Collective 41176c699258SBarry Smith 41186c699258SBarry Smith Input Parameter: 41196c699258SBarry Smith . ts - the preconditioner context 41206c699258SBarry Smith 41216c699258SBarry Smith Output Parameter: 41226c699258SBarry Smith . dm - the dm 41236c699258SBarry Smith 41246c699258SBarry Smith Level: intermediate 41256c699258SBarry Smith 41266c699258SBarry Smith 41276c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 41286c699258SBarry Smith @*/ 41297087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 41306c699258SBarry Smith { 4131496e6a7aSJed Brown PetscErrorCode ierr; 4132496e6a7aSJed Brown 41336c699258SBarry Smith PetscFunctionBegin; 41340700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4135496e6a7aSJed Brown if (!ts->dm) { 4136ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4137496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4138496e6a7aSJed Brown } 41396c699258SBarry Smith *dm = ts->dm; 41406c699258SBarry Smith PetscFunctionReturn(0); 41416c699258SBarry Smith } 41421713a123SBarry Smith 41430f5c6efeSJed Brown #undef __FUNCT__ 41440f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 41450f5c6efeSJed Brown /*@ 41460f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 41470f5c6efeSJed Brown 41483f9fe445SBarry Smith Logically Collective on SNES 41490f5c6efeSJed Brown 41500f5c6efeSJed Brown Input Parameter: 4151d42a1c89SJed Brown + snes - nonlinear solver 41520910c330SBarry Smith . U - the current state at which to evaluate the residual 4153d42a1c89SJed Brown - ctx - user context, must be a TS 41540f5c6efeSJed Brown 41550f5c6efeSJed Brown Output Parameter: 41560f5c6efeSJed Brown . F - the nonlinear residual 41570f5c6efeSJed Brown 41580f5c6efeSJed Brown Notes: 41590f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 41600f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 41610f5c6efeSJed Brown 41620f5c6efeSJed Brown Level: advanced 41630f5c6efeSJed Brown 41640f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 41650f5c6efeSJed Brown @*/ 41660910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 41670f5c6efeSJed Brown { 41680f5c6efeSJed Brown TS ts = (TS)ctx; 41690f5c6efeSJed Brown PetscErrorCode ierr; 41700f5c6efeSJed Brown 41710f5c6efeSJed Brown PetscFunctionBegin; 41720f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 41730910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 41740f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 41750f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 41760910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 41770f5c6efeSJed Brown PetscFunctionReturn(0); 41780f5c6efeSJed Brown } 41790f5c6efeSJed Brown 41800f5c6efeSJed Brown #undef __FUNCT__ 41810f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 41820f5c6efeSJed Brown /*@ 41830f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 41840f5c6efeSJed Brown 41850f5c6efeSJed Brown Collective on SNES 41860f5c6efeSJed Brown 41870f5c6efeSJed Brown Input Parameter: 41880f5c6efeSJed Brown + snes - nonlinear solver 41890910c330SBarry Smith . U - the current state at which to evaluate the residual 41900f5c6efeSJed Brown - ctx - user context, must be a TS 41910f5c6efeSJed Brown 41920f5c6efeSJed Brown Output Parameter: 41930f5c6efeSJed Brown + A - the Jacobian 41940f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 41950f5c6efeSJed Brown - flag - indicates any structure change in the matrix 41960f5c6efeSJed Brown 41970f5c6efeSJed Brown Notes: 41980f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 41990f5c6efeSJed Brown 42000f5c6efeSJed Brown Level: developer 42010f5c6efeSJed Brown 42020f5c6efeSJed Brown .seealso: SNESSetJacobian() 42030f5c6efeSJed Brown @*/ 4204d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 42050f5c6efeSJed Brown { 42060f5c6efeSJed Brown TS ts = (TS)ctx; 42070f5c6efeSJed Brown PetscErrorCode ierr; 42080f5c6efeSJed Brown 42090f5c6efeSJed Brown PetscFunctionBegin; 42100f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 42110910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 42120f5c6efeSJed Brown PetscValidPointer(A,3); 421394ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 42140f5c6efeSJed Brown PetscValidPointer(B,4); 421594ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 42160f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 4217d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 42180f5c6efeSJed Brown PetscFunctionReturn(0); 42190f5c6efeSJed Brown } 4220325fc9f4SBarry Smith 42210e4ef248SJed Brown #undef __FUNCT__ 42220e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 42230e4ef248SJed Brown /*@C 42240e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 42250e4ef248SJed Brown 42260e4ef248SJed Brown Collective on TS 42270e4ef248SJed Brown 42280e4ef248SJed Brown Input Arguments: 42290e4ef248SJed Brown + ts - time stepping context 42300e4ef248SJed Brown . t - time at which to evaluate 42310910c330SBarry Smith . U - state at which to evaluate 42320e4ef248SJed Brown - ctx - context 42330e4ef248SJed Brown 42340e4ef248SJed Brown Output Arguments: 42350e4ef248SJed Brown . F - right hand side 42360e4ef248SJed Brown 42370e4ef248SJed Brown Level: intermediate 42380e4ef248SJed Brown 42390e4ef248SJed Brown Notes: 42400e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 42410e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 42420e4ef248SJed Brown 42430e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 42440e4ef248SJed Brown @*/ 42450910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 42460e4ef248SJed Brown { 42470e4ef248SJed Brown PetscErrorCode ierr; 42480e4ef248SJed Brown Mat Arhs,Brhs; 42490e4ef248SJed Brown 42500e4ef248SJed Brown PetscFunctionBegin; 42510e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 4252d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 42530910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 42540e4ef248SJed Brown PetscFunctionReturn(0); 42550e4ef248SJed Brown } 42560e4ef248SJed Brown 42570e4ef248SJed Brown #undef __FUNCT__ 42580e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 42590e4ef248SJed Brown /*@C 42600e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 42610e4ef248SJed Brown 42620e4ef248SJed Brown Collective on TS 42630e4ef248SJed Brown 42640e4ef248SJed Brown Input Arguments: 42650e4ef248SJed Brown + ts - time stepping context 42660e4ef248SJed Brown . t - time at which to evaluate 42670910c330SBarry Smith . U - state at which to evaluate 42680e4ef248SJed Brown - ctx - context 42690e4ef248SJed Brown 42700e4ef248SJed Brown Output Arguments: 42710e4ef248SJed Brown + A - pointer to operator 42720e4ef248SJed Brown . B - pointer to preconditioning matrix 42730e4ef248SJed Brown - flg - matrix structure flag 42740e4ef248SJed Brown 42750e4ef248SJed Brown Level: intermediate 42760e4ef248SJed Brown 42770e4ef248SJed Brown Notes: 42780e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 42790e4ef248SJed Brown 42800e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 42810e4ef248SJed Brown @*/ 4282d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 42830e4ef248SJed Brown { 42840e4ef248SJed Brown PetscFunctionBegin; 42850e4ef248SJed Brown PetscFunctionReturn(0); 42860e4ef248SJed Brown } 42870e4ef248SJed Brown 42880026cea9SSean Farley #undef __FUNCT__ 42890026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 42900026cea9SSean Farley /*@C 42910026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 42920026cea9SSean Farley 42930026cea9SSean Farley Collective on TS 42940026cea9SSean Farley 42950026cea9SSean Farley Input Arguments: 42960026cea9SSean Farley + ts - time stepping context 42970026cea9SSean Farley . t - time at which to evaluate 42980910c330SBarry Smith . U - state at which to evaluate 42990910c330SBarry Smith . Udot - time derivative of state vector 43000026cea9SSean Farley - ctx - context 43010026cea9SSean Farley 43020026cea9SSean Farley Output Arguments: 43030026cea9SSean Farley . F - left hand side 43040026cea9SSean Farley 43050026cea9SSean Farley Level: intermediate 43060026cea9SSean Farley 43070026cea9SSean Farley Notes: 43080910c330SBarry 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 43090026cea9SSean Farley user is required to write their own TSComputeIFunction. 43100026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 43110026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 43120026cea9SSean Farley 43130026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 43140026cea9SSean Farley @*/ 43150910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 43160026cea9SSean Farley { 43170026cea9SSean Farley PetscErrorCode ierr; 43180026cea9SSean Farley Mat A,B; 43190026cea9SSean Farley 43200026cea9SSean Farley PetscFunctionBegin; 43210298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 4322d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 43230910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 43240026cea9SSean Farley PetscFunctionReturn(0); 43250026cea9SSean Farley } 43260026cea9SSean Farley 43270026cea9SSean Farley #undef __FUNCT__ 43280026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 43290026cea9SSean Farley /*@C 4330b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 43310026cea9SSean Farley 43320026cea9SSean Farley Collective on TS 43330026cea9SSean Farley 43340026cea9SSean Farley Input Arguments: 43350026cea9SSean Farley + ts - time stepping context 43360026cea9SSean Farley . t - time at which to evaluate 43370910c330SBarry Smith . U - state at which to evaluate 43380910c330SBarry Smith . Udot - time derivative of state vector 43390026cea9SSean Farley . shift - shift to apply 43400026cea9SSean Farley - ctx - context 43410026cea9SSean Farley 43420026cea9SSean Farley Output Arguments: 43430026cea9SSean Farley + A - pointer to operator 43440026cea9SSean Farley . B - pointer to preconditioning matrix 43450026cea9SSean Farley - flg - matrix structure flag 43460026cea9SSean Farley 4347b41af12eSJed Brown Level: advanced 43480026cea9SSean Farley 43490026cea9SSean Farley Notes: 43500026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 43510026cea9SSean Farley 4352b41af12eSJed Brown It is only appropriate for problems of the form 4353b41af12eSJed Brown 4354b41af12eSJed Brown $ M Udot = F(U,t) 4355b41af12eSJed Brown 4356b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4357b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4358b41af12eSJed Brown an implicit operator of the form 4359b41af12eSJed Brown 4360b41af12eSJed Brown $ shift*M + J 4361b41af12eSJed Brown 4362b41af12eSJed 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 4363b41af12eSJed Brown a copy of M or reassemble it when requested. 4364b41af12eSJed Brown 43650026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 43660026cea9SSean Farley @*/ 4367d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 43680026cea9SSean Farley { 4369b41af12eSJed Brown PetscErrorCode ierr; 4370b41af12eSJed Brown 43710026cea9SSean Farley PetscFunctionBegin; 437294ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4373b41af12eSJed Brown ts->ijacobian.shift = shift; 43740026cea9SSean Farley PetscFunctionReturn(0); 43750026cea9SSean Farley } 4376b41af12eSJed Brown 4377e817cc15SEmil Constantinescu #undef __FUNCT__ 4378e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType" 4379e817cc15SEmil Constantinescu /*@ 4380e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 4381e817cc15SEmil Constantinescu 4382e817cc15SEmil Constantinescu Not Collective 4383e817cc15SEmil Constantinescu 4384e817cc15SEmil Constantinescu Input Parameter: 4385e817cc15SEmil Constantinescu . ts - the TS context 4386e817cc15SEmil Constantinescu 4387e817cc15SEmil Constantinescu Output Parameter: 43884e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4389e817cc15SEmil Constantinescu 4390e817cc15SEmil Constantinescu Level: beginner 4391e817cc15SEmil Constantinescu 4392e817cc15SEmil Constantinescu .keywords: TS, equation type 4393e817cc15SEmil Constantinescu 4394e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 4395e817cc15SEmil Constantinescu @*/ 4396e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4397e817cc15SEmil Constantinescu { 4398e817cc15SEmil Constantinescu PetscFunctionBegin; 4399e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4400e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 4401e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 4402e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4403e817cc15SEmil Constantinescu } 4404e817cc15SEmil Constantinescu 4405e817cc15SEmil Constantinescu #undef __FUNCT__ 4406e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType" 4407e817cc15SEmil Constantinescu /*@ 4408e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 4409e817cc15SEmil Constantinescu 4410e817cc15SEmil Constantinescu Not Collective 4411e817cc15SEmil Constantinescu 4412e817cc15SEmil Constantinescu Input Parameter: 4413e817cc15SEmil Constantinescu + ts - the TS context 44144e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4415e817cc15SEmil Constantinescu 4416e817cc15SEmil Constantinescu Level: advanced 4417e817cc15SEmil Constantinescu 4418e817cc15SEmil Constantinescu .keywords: TS, equation type 4419e817cc15SEmil Constantinescu 4420e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 4421e817cc15SEmil Constantinescu @*/ 4422e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4423e817cc15SEmil Constantinescu { 4424e817cc15SEmil Constantinescu PetscFunctionBegin; 4425e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4426e817cc15SEmil Constantinescu ts->equation_type = equation_type; 4427e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4428e817cc15SEmil Constantinescu } 44290026cea9SSean Farley 44304af1b03aSJed Brown #undef __FUNCT__ 44314af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 44324af1b03aSJed Brown /*@ 44334af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 44344af1b03aSJed Brown 44354af1b03aSJed Brown Not Collective 44364af1b03aSJed Brown 44374af1b03aSJed Brown Input Parameter: 44384af1b03aSJed Brown . ts - the TS context 44394af1b03aSJed Brown 44404af1b03aSJed Brown Output Parameter: 44414af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 44424af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 44434af1b03aSJed Brown 4444487e0bb9SJed Brown Level: beginner 44454af1b03aSJed Brown 4446cd652676SJed Brown Notes: 4447cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 44484af1b03aSJed Brown 44494af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 44504af1b03aSJed Brown 44514af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 44524af1b03aSJed Brown @*/ 44534af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 44544af1b03aSJed Brown { 44554af1b03aSJed Brown PetscFunctionBegin; 44564af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 44574af1b03aSJed Brown PetscValidPointer(reason,2); 44584af1b03aSJed Brown *reason = ts->reason; 44594af1b03aSJed Brown PetscFunctionReturn(0); 44604af1b03aSJed Brown } 44614af1b03aSJed Brown 4462fb1732b5SBarry Smith #undef __FUNCT__ 4463d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 4464d6ad946cSShri Abhyankar /*@ 4465d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4466d6ad946cSShri Abhyankar 4467d6ad946cSShri Abhyankar Not Collective 4468d6ad946cSShri Abhyankar 4469d6ad946cSShri Abhyankar Input Parameter: 4470d6ad946cSShri Abhyankar + ts - the TS context 4471d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4472d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4473d6ad946cSShri Abhyankar 4474f5abba47SShri Abhyankar Level: advanced 4475d6ad946cSShri Abhyankar 4476d6ad946cSShri Abhyankar Notes: 4477d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 4478d6ad946cSShri Abhyankar 4479d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 4480d6ad946cSShri Abhyankar 4481d6ad946cSShri Abhyankar .seealso: TSConvergedReason 4482d6ad946cSShri Abhyankar @*/ 4483d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4484d6ad946cSShri Abhyankar { 4485d6ad946cSShri Abhyankar PetscFunctionBegin; 4486d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4487d6ad946cSShri Abhyankar ts->reason = reason; 4488d6ad946cSShri Abhyankar PetscFunctionReturn(0); 4489d6ad946cSShri Abhyankar } 4490d6ad946cSShri Abhyankar 4491d6ad946cSShri Abhyankar #undef __FUNCT__ 4492cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 4493cc708dedSBarry Smith /*@ 4494cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 4495cc708dedSBarry Smith 4496cc708dedSBarry Smith Not Collective 4497cc708dedSBarry Smith 4498cc708dedSBarry Smith Input Parameter: 4499cc708dedSBarry Smith . ts - the TS context 4500cc708dedSBarry Smith 4501cc708dedSBarry Smith Output Parameter: 4502cc708dedSBarry Smith . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 4503cc708dedSBarry Smith 4504487e0bb9SJed Brown Level: beginner 4505cc708dedSBarry Smith 4506cc708dedSBarry Smith Notes: 4507cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 4508cc708dedSBarry Smith 4509cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 4510cc708dedSBarry Smith 4511cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 4512cc708dedSBarry Smith @*/ 4513cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 4514cc708dedSBarry Smith { 4515cc708dedSBarry Smith PetscFunctionBegin; 4516cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4517cc708dedSBarry Smith PetscValidPointer(ftime,2); 4518cc708dedSBarry Smith *ftime = ts->solvetime; 4519cc708dedSBarry Smith PetscFunctionReturn(0); 4520cc708dedSBarry Smith } 4521cc708dedSBarry Smith 4522cc708dedSBarry Smith #undef __FUNCT__ 45232c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps" 45242c18e0fdSBarry Smith /*@ 45252c18e0fdSBarry Smith TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 45262c18e0fdSBarry Smith 45272c18e0fdSBarry Smith Not Collective 45282c18e0fdSBarry Smith 45292c18e0fdSBarry Smith Input Parameter: 45302c18e0fdSBarry Smith . ts - the TS context 45312c18e0fdSBarry Smith 45322c18e0fdSBarry Smith Output Parameter: 45332c18e0fdSBarry Smith . steps - the number of steps 45342c18e0fdSBarry Smith 45352c18e0fdSBarry Smith Level: beginner 45362c18e0fdSBarry Smith 45372c18e0fdSBarry Smith Notes: 45382c18e0fdSBarry Smith Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 45392c18e0fdSBarry Smith 45402c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test 45412c18e0fdSBarry Smith 45422c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 45432c18e0fdSBarry Smith @*/ 45442c18e0fdSBarry Smith PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 45452c18e0fdSBarry Smith { 45462c18e0fdSBarry Smith PetscFunctionBegin; 45472c18e0fdSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45482c18e0fdSBarry Smith PetscValidPointer(steps,2); 45492c18e0fdSBarry Smith *steps = ts->total_steps; 45502c18e0fdSBarry Smith PetscFunctionReturn(0); 45512c18e0fdSBarry Smith } 45522c18e0fdSBarry Smith 45532c18e0fdSBarry Smith #undef __FUNCT__ 45545ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 45559f67acb7SJed Brown /*@ 45565ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 45579f67acb7SJed Brown used by the time integrator. 45589f67acb7SJed Brown 45599f67acb7SJed Brown Not Collective 45609f67acb7SJed Brown 45619f67acb7SJed Brown Input Parameter: 45629f67acb7SJed Brown . ts - TS context 45639f67acb7SJed Brown 45649f67acb7SJed Brown Output Parameter: 45659f67acb7SJed Brown . nits - number of nonlinear iterations 45669f67acb7SJed Brown 45679f67acb7SJed Brown Notes: 45689f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 45699f67acb7SJed Brown 45709f67acb7SJed Brown Level: intermediate 45719f67acb7SJed Brown 45729f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 45739f67acb7SJed Brown 45745ef26d82SJed Brown .seealso: TSGetKSPIterations() 45759f67acb7SJed Brown @*/ 45765ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 45779f67acb7SJed Brown { 45789f67acb7SJed Brown PetscFunctionBegin; 45799f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45809f67acb7SJed Brown PetscValidIntPointer(nits,2); 45815ef26d82SJed Brown *nits = ts->snes_its; 45829f67acb7SJed Brown PetscFunctionReturn(0); 45839f67acb7SJed Brown } 45849f67acb7SJed Brown 45859f67acb7SJed Brown #undef __FUNCT__ 45865ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 45879f67acb7SJed Brown /*@ 45885ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 45899f67acb7SJed Brown used by the time integrator. 45909f67acb7SJed Brown 45919f67acb7SJed Brown Not Collective 45929f67acb7SJed Brown 45939f67acb7SJed Brown Input Parameter: 45949f67acb7SJed Brown . ts - TS context 45959f67acb7SJed Brown 45969f67acb7SJed Brown Output Parameter: 45979f67acb7SJed Brown . lits - number of linear iterations 45989f67acb7SJed Brown 45999f67acb7SJed Brown Notes: 46009f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 46019f67acb7SJed Brown 46029f67acb7SJed Brown Level: intermediate 46039f67acb7SJed Brown 46049f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 46059f67acb7SJed Brown 46065ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 46079f67acb7SJed Brown @*/ 46085ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 46099f67acb7SJed Brown { 46109f67acb7SJed Brown PetscFunctionBegin; 46119f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 46129f67acb7SJed Brown PetscValidIntPointer(lits,2); 46135ef26d82SJed Brown *lits = ts->ksp_its; 46149f67acb7SJed Brown PetscFunctionReturn(0); 46159f67acb7SJed Brown } 46169f67acb7SJed Brown 46179f67acb7SJed Brown #undef __FUNCT__ 4618cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 4619cef5090cSJed Brown /*@ 4620cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4621cef5090cSJed Brown 4622cef5090cSJed Brown Not Collective 4623cef5090cSJed Brown 4624cef5090cSJed Brown Input Parameter: 4625cef5090cSJed Brown . ts - TS context 4626cef5090cSJed Brown 4627cef5090cSJed Brown Output Parameter: 4628cef5090cSJed Brown . rejects - number of steps rejected 4629cef5090cSJed Brown 4630cef5090cSJed Brown Notes: 4631cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4632cef5090cSJed Brown 4633cef5090cSJed Brown Level: intermediate 4634cef5090cSJed Brown 4635cef5090cSJed Brown .keywords: TS, get, number 4636cef5090cSJed Brown 46375ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 4638cef5090cSJed Brown @*/ 4639cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 4640cef5090cSJed Brown { 4641cef5090cSJed Brown PetscFunctionBegin; 4642cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4643cef5090cSJed Brown PetscValidIntPointer(rejects,2); 4644cef5090cSJed Brown *rejects = ts->reject; 4645cef5090cSJed Brown PetscFunctionReturn(0); 4646cef5090cSJed Brown } 4647cef5090cSJed Brown 4648cef5090cSJed Brown #undef __FUNCT__ 4649cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 4650cef5090cSJed Brown /*@ 4651cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 4652cef5090cSJed Brown 4653cef5090cSJed Brown Not Collective 4654cef5090cSJed Brown 4655cef5090cSJed Brown Input Parameter: 4656cef5090cSJed Brown . ts - TS context 4657cef5090cSJed Brown 4658cef5090cSJed Brown Output Parameter: 4659cef5090cSJed Brown . fails - number of failed nonlinear solves 4660cef5090cSJed Brown 4661cef5090cSJed Brown Notes: 4662cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4663cef5090cSJed Brown 4664cef5090cSJed Brown Level: intermediate 4665cef5090cSJed Brown 4666cef5090cSJed Brown .keywords: TS, get, number 4667cef5090cSJed Brown 46685ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 4669cef5090cSJed Brown @*/ 4670cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 4671cef5090cSJed Brown { 4672cef5090cSJed Brown PetscFunctionBegin; 4673cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4674cef5090cSJed Brown PetscValidIntPointer(fails,2); 4675cef5090cSJed Brown *fails = ts->num_snes_failures; 4676cef5090cSJed Brown PetscFunctionReturn(0); 4677cef5090cSJed Brown } 4678cef5090cSJed Brown 4679cef5090cSJed Brown #undef __FUNCT__ 4680cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 4681cef5090cSJed Brown /*@ 4682cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 4683cef5090cSJed Brown 4684cef5090cSJed Brown Not Collective 4685cef5090cSJed Brown 4686cef5090cSJed Brown Input Parameter: 4687cef5090cSJed Brown + ts - TS context 4688cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 4689cef5090cSJed Brown 4690cef5090cSJed Brown Notes: 4691cef5090cSJed Brown The counter is reset to zero for each step 4692cef5090cSJed Brown 4693cef5090cSJed Brown Options Database Key: 4694cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4695cef5090cSJed Brown 4696cef5090cSJed Brown Level: intermediate 4697cef5090cSJed Brown 4698cef5090cSJed Brown .keywords: TS, set, maximum, number 4699cef5090cSJed Brown 47005ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4701cef5090cSJed Brown @*/ 4702cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 4703cef5090cSJed Brown { 4704cef5090cSJed Brown PetscFunctionBegin; 4705cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4706cef5090cSJed Brown ts->max_reject = rejects; 4707cef5090cSJed Brown PetscFunctionReturn(0); 4708cef5090cSJed Brown } 4709cef5090cSJed Brown 4710cef5090cSJed Brown #undef __FUNCT__ 4711cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 4712cef5090cSJed Brown /*@ 4713cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 4714cef5090cSJed Brown 4715cef5090cSJed Brown Not Collective 4716cef5090cSJed Brown 4717cef5090cSJed Brown Input Parameter: 4718cef5090cSJed Brown + ts - TS context 4719cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 4720cef5090cSJed Brown 4721cef5090cSJed Brown Notes: 4722cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 4723cef5090cSJed Brown 4724cef5090cSJed Brown Options Database Key: 4725cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4726cef5090cSJed Brown 4727cef5090cSJed Brown Level: intermediate 4728cef5090cSJed Brown 4729cef5090cSJed Brown .keywords: TS, set, maximum, number 4730cef5090cSJed Brown 47315ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 4732cef5090cSJed Brown @*/ 4733cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 4734cef5090cSJed Brown { 4735cef5090cSJed Brown PetscFunctionBegin; 4736cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4737cef5090cSJed Brown ts->max_snes_failures = fails; 4738cef5090cSJed Brown PetscFunctionReturn(0); 4739cef5090cSJed Brown } 4740cef5090cSJed Brown 4741cef5090cSJed Brown #undef __FUNCT__ 47424e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails" 4743cef5090cSJed Brown /*@ 4744cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 4745cef5090cSJed Brown 4746cef5090cSJed Brown Not Collective 4747cef5090cSJed Brown 4748cef5090cSJed Brown Input Parameter: 4749cef5090cSJed Brown + ts - TS context 4750cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 4751cef5090cSJed Brown 4752cef5090cSJed Brown Options Database Key: 4753cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 4754cef5090cSJed Brown 4755cef5090cSJed Brown Level: intermediate 4756cef5090cSJed Brown 4757cef5090cSJed Brown .keywords: TS, set, error 4758cef5090cSJed Brown 47595ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4760cef5090cSJed Brown @*/ 4761cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 4762cef5090cSJed Brown { 4763cef5090cSJed Brown PetscFunctionBegin; 4764cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4765cef5090cSJed Brown ts->errorifstepfailed = err; 4766cef5090cSJed Brown PetscFunctionReturn(0); 4767cef5090cSJed Brown } 4768cef5090cSJed Brown 4769cef5090cSJed Brown #undef __FUNCT__ 4770fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 4771fb1732b5SBarry Smith /*@C 4772fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 4773fb1732b5SBarry Smith 4774fb1732b5SBarry Smith Collective on TS 4775fb1732b5SBarry Smith 4776fb1732b5SBarry Smith Input Parameters: 4777fb1732b5SBarry Smith + ts - the TS context 4778fb1732b5SBarry Smith . step - current time-step 4779fb1732b5SBarry Smith . ptime - current time 47800910c330SBarry Smith . u - current state 4781fb1732b5SBarry Smith - viewer - binary viewer 4782fb1732b5SBarry Smith 4783fb1732b5SBarry Smith Level: intermediate 4784fb1732b5SBarry Smith 4785fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 4786fb1732b5SBarry Smith 4787fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4788fb1732b5SBarry Smith @*/ 47890910c330SBarry Smith PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 4790fb1732b5SBarry Smith { 4791fb1732b5SBarry Smith PetscErrorCode ierr; 4792ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 4793fb1732b5SBarry Smith 4794fb1732b5SBarry Smith PetscFunctionBegin; 47950910c330SBarry Smith ierr = VecView(u,v);CHKERRQ(ierr); 4796ed81e22dSJed Brown PetscFunctionReturn(0); 4797ed81e22dSJed Brown } 4798ed81e22dSJed Brown 4799ed81e22dSJed Brown #undef __FUNCT__ 4800ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 4801ed81e22dSJed Brown /*@C 4802ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 4803ed81e22dSJed Brown 4804ed81e22dSJed Brown Collective on TS 4805ed81e22dSJed Brown 4806ed81e22dSJed Brown Input Parameters: 4807ed81e22dSJed Brown + ts - the TS context 4808ed81e22dSJed Brown . step - current time-step 4809ed81e22dSJed Brown . ptime - current time 48100910c330SBarry Smith . u - current state 4811ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 4812ed81e22dSJed Brown 4813ed81e22dSJed Brown Level: intermediate 4814ed81e22dSJed Brown 4815ed81e22dSJed Brown Notes: 4816ed81e22dSJed 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. 4817ed81e22dSJed Brown These are named according to the file name template. 4818ed81e22dSJed Brown 4819ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 4820ed81e22dSJed Brown 4821ed81e22dSJed Brown .keywords: TS, vector, monitor, view 4822ed81e22dSJed Brown 4823ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4824ed81e22dSJed Brown @*/ 48250910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 4826ed81e22dSJed Brown { 4827ed81e22dSJed Brown PetscErrorCode ierr; 4828ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 4829ed81e22dSJed Brown PetscViewer viewer; 4830ed81e22dSJed Brown 4831ed81e22dSJed Brown PetscFunctionBegin; 48328caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 4833ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 48340910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 4835ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 4836ed81e22dSJed Brown PetscFunctionReturn(0); 4837ed81e22dSJed Brown } 4838ed81e22dSJed Brown 4839ed81e22dSJed Brown #undef __FUNCT__ 4840ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 4841ed81e22dSJed Brown /*@C 4842ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 4843ed81e22dSJed Brown 4844ed81e22dSJed Brown Collective on TS 4845ed81e22dSJed Brown 4846ed81e22dSJed Brown Input Parameters: 4847ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 4848ed81e22dSJed Brown 4849ed81e22dSJed Brown Level: intermediate 4850ed81e22dSJed Brown 4851ed81e22dSJed Brown Note: 4852ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 4853ed81e22dSJed Brown 4854ed81e22dSJed Brown .keywords: TS, vector, monitor, view 4855ed81e22dSJed Brown 4856ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 4857ed81e22dSJed Brown @*/ 4858ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 4859ed81e22dSJed Brown { 4860ed81e22dSJed Brown PetscErrorCode ierr; 4861ed81e22dSJed Brown 4862ed81e22dSJed Brown PetscFunctionBegin; 4863ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 4864fb1732b5SBarry Smith PetscFunctionReturn(0); 4865fb1732b5SBarry Smith } 4866fb1732b5SBarry Smith 486784df9cb4SJed Brown #undef __FUNCT__ 4868552698daSJed Brown #define __FUNCT__ "TSGetAdapt" 486984df9cb4SJed Brown /*@ 4870552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 487184df9cb4SJed Brown 4872ed81e22dSJed Brown Collective on TS if controller has not been created yet 487384df9cb4SJed Brown 487484df9cb4SJed Brown Input Arguments: 4875ed81e22dSJed Brown . ts - time stepping context 487684df9cb4SJed Brown 487784df9cb4SJed Brown Output Arguments: 4878ed81e22dSJed Brown . adapt - adaptive controller 487984df9cb4SJed Brown 488084df9cb4SJed Brown Level: intermediate 488184df9cb4SJed Brown 4882ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 488384df9cb4SJed Brown @*/ 4884552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 488584df9cb4SJed Brown { 488684df9cb4SJed Brown PetscErrorCode ierr; 488784df9cb4SJed Brown 488884df9cb4SJed Brown PetscFunctionBegin; 488984df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 489084df9cb4SJed Brown PetscValidPointer(adapt,2); 489184df9cb4SJed Brown if (!ts->adapt) { 4892ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 48933bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 48941c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 489584df9cb4SJed Brown } 489684df9cb4SJed Brown *adapt = ts->adapt; 489784df9cb4SJed Brown PetscFunctionReturn(0); 489884df9cb4SJed Brown } 4899d6ebe24aSShri Abhyankar 4900d6ebe24aSShri Abhyankar #undef __FUNCT__ 49011c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 49021c3436cfSJed Brown /*@ 49031c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 49041c3436cfSJed Brown 49051c3436cfSJed Brown Logically Collective 49061c3436cfSJed Brown 49071c3436cfSJed Brown Input Arguments: 49081c3436cfSJed Brown + ts - time integration context 49091c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 49100298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 49111c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 49120298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 49131c3436cfSJed Brown 4914a3cdaa26SBarry Smith Options Database keys: 4915a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 4916a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 4917a3cdaa26SBarry Smith 49181c3436cfSJed Brown Level: beginner 49191c3436cfSJed Brown 4920c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 49211c3436cfSJed Brown @*/ 49221c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 49231c3436cfSJed Brown { 49241c3436cfSJed Brown PetscErrorCode ierr; 49251c3436cfSJed Brown 49261c3436cfSJed Brown PetscFunctionBegin; 4927c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 49281c3436cfSJed Brown if (vatol) { 49291c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 49301c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 4931bbd56ea5SKarl Rupp 49321c3436cfSJed Brown ts->vatol = vatol; 49331c3436cfSJed Brown } 4934c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 49351c3436cfSJed Brown if (vrtol) { 49361c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 49371c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 4938bbd56ea5SKarl Rupp 49391c3436cfSJed Brown ts->vrtol = vrtol; 49401c3436cfSJed Brown } 49411c3436cfSJed Brown PetscFunctionReturn(0); 49421c3436cfSJed Brown } 49431c3436cfSJed Brown 49441c3436cfSJed Brown #undef __FUNCT__ 4945c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 4946c5033834SJed Brown /*@ 4947c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 4948c5033834SJed Brown 4949c5033834SJed Brown Logically Collective 4950c5033834SJed Brown 4951c5033834SJed Brown Input Arguments: 4952c5033834SJed Brown . ts - time integration context 4953c5033834SJed Brown 4954c5033834SJed Brown Output Arguments: 49550298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 49560298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 49570298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 49580298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 4959c5033834SJed Brown 4960c5033834SJed Brown Level: beginner 4961c5033834SJed Brown 4962c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 4963c5033834SJed Brown @*/ 4964c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 4965c5033834SJed Brown { 4966c5033834SJed Brown PetscFunctionBegin; 4967c5033834SJed Brown if (atol) *atol = ts->atol; 4968c5033834SJed Brown if (vatol) *vatol = ts->vatol; 4969c5033834SJed Brown if (rtol) *rtol = ts->rtol; 4970c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 4971c5033834SJed Brown PetscFunctionReturn(0); 4972c5033834SJed Brown } 4973c5033834SJed Brown 4974c5033834SJed Brown #undef __FUNCT__ 49751c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS" 49761c3436cfSJed Brown /*@ 49771c3436cfSJed Brown TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 49781c3436cfSJed Brown 49791c3436cfSJed Brown Collective on TS 49801c3436cfSJed Brown 49811c3436cfSJed Brown Input Arguments: 49821c3436cfSJed Brown + ts - time stepping context 49831c3436cfSJed Brown - Y - state vector to be compared to ts->vec_sol 49841c3436cfSJed Brown 49851c3436cfSJed Brown Output Arguments: 49861c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 49871c3436cfSJed Brown 49881c3436cfSJed Brown Level: developer 49891c3436cfSJed Brown 49901c3436cfSJed Brown .seealso: TSSetTolerances() 49911c3436cfSJed Brown @*/ 49921c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 49931c3436cfSJed Brown { 49941c3436cfSJed Brown PetscErrorCode ierr; 49951c3436cfSJed Brown PetscInt i,n,N; 49960910c330SBarry Smith const PetscScalar *u,*y; 49970910c330SBarry Smith Vec U; 49981c3436cfSJed Brown PetscReal sum,gsum; 49991c3436cfSJed Brown 50001c3436cfSJed Brown PetscFunctionBegin; 50011c3436cfSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 50021c3436cfSJed Brown PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 50031c3436cfSJed Brown PetscValidPointer(norm,3); 50040910c330SBarry Smith U = ts->vec_sol; 50050910c330SBarry Smith PetscCheckSameTypeAndComm(U,1,Y,2); 5006ce94432eSBarry Smith if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 50071c3436cfSJed Brown 50080910c330SBarry Smith ierr = VecGetSize(U,&N);CHKERRQ(ierr); 50090910c330SBarry Smith ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 50100910c330SBarry Smith ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 50111c3436cfSJed Brown ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 50121c3436cfSJed Brown sum = 0.; 5013ceefc113SJed Brown if (ts->vatol && ts->vrtol) { 5014ceefc113SJed Brown const PetscScalar *atol,*rtol; 5015ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 5016ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 5017ceefc113SJed Brown for (i=0; i<n; i++) { 50180910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50190910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 5020ceefc113SJed Brown } 5021ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 5022ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 5023ceefc113SJed Brown } else if (ts->vatol) { /* vector atol, scalar rtol */ 5024ceefc113SJed Brown const PetscScalar *atol; 5025ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 5026ceefc113SJed Brown for (i=0; i<n; i++) { 50270910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50280910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 5029ceefc113SJed Brown } 5030ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 5031ceefc113SJed Brown } else if (ts->vrtol) { /* scalar atol, vector rtol */ 5032ceefc113SJed Brown const PetscScalar *rtol; 5033ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 5034ceefc113SJed Brown for (i=0; i<n; i++) { 50350910c330SBarry Smith PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50360910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 5037ceefc113SJed Brown } 5038ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 5039ceefc113SJed Brown } else { /* scalar atol, scalar rtol */ 50401c3436cfSJed Brown for (i=0; i<n; i++) { 50410910c330SBarry Smith PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50420910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 50431c3436cfSJed Brown } 5044ceefc113SJed Brown } 50450910c330SBarry Smith ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 50461c3436cfSJed Brown ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 50471c3436cfSJed Brown 5048ce94432eSBarry Smith ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 50491c3436cfSJed Brown *norm = PetscSqrtReal(gsum / N); 5050620fc8aaSSatish Balay if (PetscIsInfOrNanReal(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 50511c3436cfSJed Brown PetscFunctionReturn(0); 50521c3436cfSJed Brown } 50531c3436cfSJed Brown 50541c3436cfSJed Brown #undef __FUNCT__ 50558d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 50568d59e960SJed Brown /*@ 50578d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 50588d59e960SJed Brown 50598d59e960SJed Brown Logically Collective on TS 50608d59e960SJed Brown 50618d59e960SJed Brown Input Arguments: 50628d59e960SJed Brown + ts - time stepping context 50638d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 50648d59e960SJed Brown 50658d59e960SJed Brown Note: 50668d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 50678d59e960SJed Brown 50688d59e960SJed Brown Level: intermediate 50698d59e960SJed Brown 50708d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 50718d59e960SJed Brown @*/ 50728d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 50738d59e960SJed Brown { 50748d59e960SJed Brown PetscFunctionBegin; 50758d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 50768d59e960SJed Brown ts->cfltime_local = cfltime; 50778d59e960SJed Brown ts->cfltime = -1.; 50788d59e960SJed Brown PetscFunctionReturn(0); 50798d59e960SJed Brown } 50808d59e960SJed Brown 50818d59e960SJed Brown #undef __FUNCT__ 50828d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 50838d59e960SJed Brown /*@ 50848d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 50858d59e960SJed Brown 50868d59e960SJed Brown Collective on TS 50878d59e960SJed Brown 50888d59e960SJed Brown Input Arguments: 50898d59e960SJed Brown . ts - time stepping context 50908d59e960SJed Brown 50918d59e960SJed Brown Output Arguments: 50928d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 50938d59e960SJed Brown 50948d59e960SJed Brown Level: advanced 50958d59e960SJed Brown 50968d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 50978d59e960SJed Brown @*/ 50988d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 50998d59e960SJed Brown { 51008d59e960SJed Brown PetscErrorCode ierr; 51018d59e960SJed Brown 51028d59e960SJed Brown PetscFunctionBegin; 51038d59e960SJed Brown if (ts->cfltime < 0) { 5104ce94432eSBarry Smith ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 51058d59e960SJed Brown } 51068d59e960SJed Brown *cfltime = ts->cfltime; 51078d59e960SJed Brown PetscFunctionReturn(0); 51088d59e960SJed Brown } 51098d59e960SJed Brown 51108d59e960SJed Brown #undef __FUNCT__ 5111d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 5112d6ebe24aSShri Abhyankar /*@ 5113d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5114d6ebe24aSShri Abhyankar 5115d6ebe24aSShri Abhyankar Input Parameters: 5116d6ebe24aSShri Abhyankar . ts - the TS context. 5117d6ebe24aSShri Abhyankar . xl - lower bound. 5118d6ebe24aSShri Abhyankar . xu - upper bound. 5119d6ebe24aSShri Abhyankar 5120d6ebe24aSShri Abhyankar Notes: 5121d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 5122e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 5123d6ebe24aSShri Abhyankar 51242bd2b0e6SSatish Balay Level: advanced 51252bd2b0e6SSatish Balay 5126d6ebe24aSShri Abhyankar @*/ 5127d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5128d6ebe24aSShri Abhyankar { 5129d6ebe24aSShri Abhyankar PetscErrorCode ierr; 5130d6ebe24aSShri Abhyankar SNES snes; 5131d6ebe24aSShri Abhyankar 5132d6ebe24aSShri Abhyankar PetscFunctionBegin; 5133d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 5134d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 5135d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 5136d6ebe24aSShri Abhyankar } 5137d6ebe24aSShri Abhyankar 5138325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 5139c6db04a5SJed Brown #include <mex.h> 5140325fc9f4SBarry Smith 5141325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 5142325fc9f4SBarry Smith 5143325fc9f4SBarry Smith #undef __FUNCT__ 5144325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 5145325fc9f4SBarry Smith /* 5146325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 5147325fc9f4SBarry Smith TSSetFunctionMatlab(). 5148325fc9f4SBarry Smith 5149325fc9f4SBarry Smith Collective on TS 5150325fc9f4SBarry Smith 5151325fc9f4SBarry Smith Input Parameters: 5152325fc9f4SBarry Smith + snes - the TS context 51530910c330SBarry Smith - u - input vector 5154325fc9f4SBarry Smith 5155325fc9f4SBarry Smith Output Parameter: 5156325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 5157325fc9f4SBarry Smith 5158325fc9f4SBarry Smith Notes: 5159325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 5160325fc9f4SBarry Smith implementations, so most users would not generally call this routine 5161325fc9f4SBarry Smith themselves. 5162325fc9f4SBarry Smith 5163325fc9f4SBarry Smith Level: developer 5164325fc9f4SBarry Smith 5165325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5166325fc9f4SBarry Smith 5167325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5168325fc9f4SBarry Smith */ 51690910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 5170325fc9f4SBarry Smith { 5171325fc9f4SBarry Smith PetscErrorCode ierr; 5172325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5173325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 5174325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 5175325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 5176325fc9f4SBarry Smith 5177325fc9f4SBarry Smith PetscFunctionBegin; 5178325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 51790910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 51800910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 5181325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 51820910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 5183325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 5184325fc9f4SBarry Smith 5185325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 51860910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 51870910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 51880910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 5189bbd56ea5SKarl Rupp 5190325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5191325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 5192325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5193325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5194325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 5195325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 5196325fc9f4SBarry Smith prhs[6] = sctx->ctx; 5197325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 5198325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5199325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5200325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5201325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5202325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5203325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5204325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5205325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5206325fc9f4SBarry Smith PetscFunctionReturn(0); 5207325fc9f4SBarry Smith } 5208325fc9f4SBarry Smith 5209325fc9f4SBarry Smith 5210325fc9f4SBarry Smith #undef __FUNCT__ 5211325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 5212325fc9f4SBarry Smith /* 5213325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 5214325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 5215e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 5216325fc9f4SBarry Smith 5217325fc9f4SBarry Smith Logically Collective on TS 5218325fc9f4SBarry Smith 5219325fc9f4SBarry Smith Input Parameters: 5220325fc9f4SBarry Smith + ts - the TS context 5221325fc9f4SBarry Smith - func - function evaluation routine 5222325fc9f4SBarry Smith 5223325fc9f4SBarry Smith Calling sequence of func: 52240910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 5225325fc9f4SBarry Smith 5226325fc9f4SBarry Smith Level: beginner 5227325fc9f4SBarry Smith 5228325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5229325fc9f4SBarry Smith 5230325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5231325fc9f4SBarry Smith */ 5232cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 5233325fc9f4SBarry Smith { 5234325fc9f4SBarry Smith PetscErrorCode ierr; 5235325fc9f4SBarry Smith TSMatlabContext *sctx; 5236325fc9f4SBarry Smith 5237325fc9f4SBarry Smith PetscFunctionBegin; 5238325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5239325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5240325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5241325fc9f4SBarry Smith /* 5242325fc9f4SBarry Smith This should work, but it doesn't 5243325fc9f4SBarry Smith sctx->ctx = ctx; 5244325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5245325fc9f4SBarry Smith */ 5246325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5247bbd56ea5SKarl Rupp 52480298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 5249325fc9f4SBarry Smith PetscFunctionReturn(0); 5250325fc9f4SBarry Smith } 5251325fc9f4SBarry Smith 5252325fc9f4SBarry Smith #undef __FUNCT__ 5253325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 5254325fc9f4SBarry Smith /* 5255325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 5256325fc9f4SBarry Smith TSSetJacobianMatlab(). 5257325fc9f4SBarry Smith 5258325fc9f4SBarry Smith Collective on TS 5259325fc9f4SBarry Smith 5260325fc9f4SBarry Smith Input Parameters: 5261cdcf91faSSean Farley + ts - the TS context 52620910c330SBarry Smith . u - input vector 5263325fc9f4SBarry Smith . A, B - the matrices 5264325fc9f4SBarry Smith - ctx - user context 5265325fc9f4SBarry Smith 5266325fc9f4SBarry Smith Level: developer 5267325fc9f4SBarry Smith 5268325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5269325fc9f4SBarry Smith 5270325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5271325fc9f4SBarry Smith @*/ 5272f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 5273325fc9f4SBarry Smith { 5274325fc9f4SBarry Smith PetscErrorCode ierr; 5275325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5276325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 5277325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 5278325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 5279325fc9f4SBarry Smith 5280325fc9f4SBarry Smith PetscFunctionBegin; 5281cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 52820910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 5283325fc9f4SBarry Smith 52840910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 5285325fc9f4SBarry Smith 5286cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 52870910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 52880910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 52890910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 52900910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 5291bbd56ea5SKarl Rupp 5292325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5293325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 5294325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5295325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5296325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 5297325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 5298325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 5299325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 5300325fc9f4SBarry Smith prhs[8] = sctx->ctx; 5301325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 5302325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5303325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5304325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5305325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5306325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5307325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5308325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5309325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 5310325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 5311325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5312325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 5313325fc9f4SBarry Smith PetscFunctionReturn(0); 5314325fc9f4SBarry Smith } 5315325fc9f4SBarry Smith 5316325fc9f4SBarry Smith 5317325fc9f4SBarry Smith #undef __FUNCT__ 5318325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 5319325fc9f4SBarry Smith /* 5320325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 5321e3c5b3baSBarry 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 5322325fc9f4SBarry Smith 5323325fc9f4SBarry Smith Logically Collective on TS 5324325fc9f4SBarry Smith 5325325fc9f4SBarry Smith Input Parameters: 5326cdcf91faSSean Farley + ts - the TS context 5327325fc9f4SBarry Smith . A,B - Jacobian matrices 5328325fc9f4SBarry Smith . func - function evaluation routine 5329325fc9f4SBarry Smith - ctx - user context 5330325fc9f4SBarry Smith 5331325fc9f4SBarry Smith Calling sequence of func: 53320910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 5333325fc9f4SBarry Smith 5334325fc9f4SBarry Smith 5335325fc9f4SBarry Smith Level: developer 5336325fc9f4SBarry Smith 5337325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5338325fc9f4SBarry Smith 5339325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5340325fc9f4SBarry Smith */ 5341cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 5342325fc9f4SBarry Smith { 5343325fc9f4SBarry Smith PetscErrorCode ierr; 5344325fc9f4SBarry Smith TSMatlabContext *sctx; 5345325fc9f4SBarry Smith 5346325fc9f4SBarry Smith PetscFunctionBegin; 5347325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5348325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5349325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5350325fc9f4SBarry Smith /* 5351325fc9f4SBarry Smith This should work, but it doesn't 5352325fc9f4SBarry Smith sctx->ctx = ctx; 5353325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5354325fc9f4SBarry Smith */ 5355325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5356bbd56ea5SKarl Rupp 5357cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 5358325fc9f4SBarry Smith PetscFunctionReturn(0); 5359325fc9f4SBarry Smith } 5360325fc9f4SBarry Smith 5361b5b1a830SBarry Smith #undef __FUNCT__ 5362b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 5363b5b1a830SBarry Smith /* 5364b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 5365b5b1a830SBarry Smith 5366b5b1a830SBarry Smith Collective on TS 5367b5b1a830SBarry Smith 5368b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5369b5b1a830SBarry Smith @*/ 53700910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 5371b5b1a830SBarry Smith { 5372b5b1a830SBarry Smith PetscErrorCode ierr; 5373b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5374a530c242SBarry Smith int nlhs = 1,nrhs = 6; 5375b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 5376b5b1a830SBarry Smith long long int lx = 0,ls = 0; 5377b5b1a830SBarry Smith 5378b5b1a830SBarry Smith PetscFunctionBegin; 5379cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53800910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 5381b5b1a830SBarry Smith 5382cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 53830910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 5384bbd56ea5SKarl Rupp 5385b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5386b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 5387b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 5388b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 5389b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 5390b5b1a830SBarry Smith prhs[5] = sctx->ctx; 5391b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 5392b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5393b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 5394b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 5395b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 5396b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 5397b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 5398b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 5399b5b1a830SBarry Smith PetscFunctionReturn(0); 5400b5b1a830SBarry Smith } 5401b5b1a830SBarry Smith 5402b5b1a830SBarry Smith 5403b5b1a830SBarry Smith #undef __FUNCT__ 5404b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 5405b5b1a830SBarry Smith /* 5406b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 5407b5b1a830SBarry Smith 5408b5b1a830SBarry Smith Level: developer 5409b5b1a830SBarry Smith 5410b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 5411b5b1a830SBarry Smith 5412b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5413b5b1a830SBarry Smith */ 5414cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 5415b5b1a830SBarry Smith { 5416b5b1a830SBarry Smith PetscErrorCode ierr; 5417b5b1a830SBarry Smith TSMatlabContext *sctx; 5418b5b1a830SBarry Smith 5419b5b1a830SBarry Smith PetscFunctionBegin; 5420b5b1a830SBarry Smith /* currently sctx is memory bleed */ 5421b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5422b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5423b5b1a830SBarry Smith /* 5424b5b1a830SBarry Smith This should work, but it doesn't 5425b5b1a830SBarry Smith sctx->ctx = ctx; 5426b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5427b5b1a830SBarry Smith */ 5428b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5429bbd56ea5SKarl Rupp 54300298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 5431b5b1a830SBarry Smith PetscFunctionReturn(0); 5432b5b1a830SBarry Smith } 5433325fc9f4SBarry Smith #endif 5434b3603a34SBarry Smith 54350b039ecaSBarry Smith 54360b039ecaSBarry Smith 5437b3603a34SBarry Smith #undef __FUNCT__ 54384f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 5439b3603a34SBarry Smith /*@C 54404f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 5441b3603a34SBarry Smith in a time based line graph 5442b3603a34SBarry Smith 5443b3603a34SBarry Smith Collective on TS 5444b3603a34SBarry Smith 5445b3603a34SBarry Smith Input Parameters: 5446b3603a34SBarry Smith + ts - the TS context 5447b3603a34SBarry Smith . step - current time-step 5448b3603a34SBarry Smith . ptime - current time 5449b3603a34SBarry Smith - lg - a line graph object 5450b3603a34SBarry Smith 5451b3603a34SBarry Smith Level: intermediate 5452b3603a34SBarry Smith 54530b039ecaSBarry Smith Notes: each process in a parallel run displays its component solutions in a separate window 5454b3603a34SBarry Smith 5455b3603a34SBarry Smith .keywords: TS, vector, monitor, view 5456b3603a34SBarry Smith 5457b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5458b3603a34SBarry Smith @*/ 54590910c330SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 5460b3603a34SBarry Smith { 5461b3603a34SBarry Smith PetscErrorCode ierr; 54620b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 5463b3603a34SBarry Smith const PetscScalar *yy; 546458ff32f7SBarry Smith PetscInt dim; 5465b3603a34SBarry Smith 5466b3603a34SBarry Smith PetscFunctionBegin; 546758ff32f7SBarry Smith if (!step) { 5468a9f9c1f6SBarry Smith PetscDrawAxis axis; 5469a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5470a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 54710910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 54720b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 54730b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 547458ff32f7SBarry Smith } 54750910c330SBarry Smith ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr); 5476e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 5477e3efe391SJed Brown { 5478e3efe391SJed Brown PetscReal *yreal; 5479e3efe391SJed Brown PetscInt i,n; 54800910c330SBarry Smith ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr); 5481785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 5482e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 54830b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 5484e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 5485e3efe391SJed Brown } 5486e3efe391SJed Brown #else 54870b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 5488e3efe391SJed Brown #endif 54890910c330SBarry Smith ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr); 5490b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 54910b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 54923923b477SBarry Smith } 5493b3603a34SBarry Smith PetscFunctionReturn(0); 5494b3603a34SBarry Smith } 5495b3603a34SBarry Smith 5496b3603a34SBarry Smith #undef __FUNCT__ 54974f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 5498ef20d060SBarry Smith /*@C 54994f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 5500ef20d060SBarry Smith in a time based line graph 5501ef20d060SBarry Smith 5502ef20d060SBarry Smith Collective on TS 5503ef20d060SBarry Smith 5504ef20d060SBarry Smith Input Parameters: 5505ef20d060SBarry Smith + ts - the TS context 5506ef20d060SBarry Smith . step - current time-step 5507ef20d060SBarry Smith . ptime - current time 5508ef20d060SBarry Smith - lg - a line graph object 5509ef20d060SBarry Smith 5510ef20d060SBarry Smith Level: intermediate 5511ef20d060SBarry Smith 5512abd5a294SJed Brown Notes: 5513abd5a294SJed Brown Only for sequential solves. 5514abd5a294SJed Brown 5515abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 5516abd5a294SJed Brown 5517abd5a294SJed Brown Options Database Keys: 55184f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 5519ef20d060SBarry Smith 5520ef20d060SBarry Smith .keywords: TS, vector, monitor, view 5521ef20d060SBarry Smith 5522abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 5523ef20d060SBarry Smith @*/ 55240910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 5525ef20d060SBarry Smith { 5526ef20d060SBarry Smith PetscErrorCode ierr; 55270b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 5528ef20d060SBarry Smith const PetscScalar *yy; 5529ef20d060SBarry Smith Vec y; 5530a9f9c1f6SBarry Smith PetscInt dim; 5531ef20d060SBarry Smith 5532ef20d060SBarry Smith PetscFunctionBegin; 5533a9f9c1f6SBarry Smith if (!step) { 5534a9f9c1f6SBarry Smith PetscDrawAxis axis; 5535a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 55361ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 55370910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 5538a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 5539a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 5540a9f9c1f6SBarry Smith } 55410910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 5542ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 55430910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 5544ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 5545e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 5546e3efe391SJed Brown { 5547e3efe391SJed Brown PetscReal *yreal; 5548e3efe391SJed Brown PetscInt i,n; 5549e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 5550785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 5551e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 55520b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 5553e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 5554e3efe391SJed Brown } 5555e3efe391SJed Brown #else 55560b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 5557e3efe391SJed Brown #endif 5558ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 5559ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 5560b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 55610b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 55623923b477SBarry Smith } 5563ef20d060SBarry Smith PetscFunctionReturn(0); 5564ef20d060SBarry Smith } 5565ef20d060SBarry Smith 5566201da799SBarry Smith #undef __FUNCT__ 5567201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 5568201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 5569201da799SBarry Smith { 5570201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 5571201da799SBarry Smith PetscReal x = ptime,y; 5572201da799SBarry Smith PetscErrorCode ierr; 5573201da799SBarry Smith PetscInt its; 5574201da799SBarry Smith 5575201da799SBarry Smith PetscFunctionBegin; 5576201da799SBarry Smith if (!n) { 5577201da799SBarry Smith PetscDrawAxis axis; 5578bbd56ea5SKarl Rupp 5579201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5580201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 5581201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 5582bbd56ea5SKarl Rupp 5583201da799SBarry Smith ctx->snes_its = 0; 5584201da799SBarry Smith } 5585201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 5586201da799SBarry Smith y = its - ctx->snes_its; 5587201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 55883fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 5589201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 5590201da799SBarry Smith } 5591201da799SBarry Smith ctx->snes_its = its; 5592201da799SBarry Smith PetscFunctionReturn(0); 5593201da799SBarry Smith } 5594201da799SBarry Smith 5595201da799SBarry Smith #undef __FUNCT__ 5596201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 5597201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 5598201da799SBarry Smith { 5599201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 5600201da799SBarry Smith PetscReal x = ptime,y; 5601201da799SBarry Smith PetscErrorCode ierr; 5602201da799SBarry Smith PetscInt its; 5603201da799SBarry Smith 5604201da799SBarry Smith PetscFunctionBegin; 5605201da799SBarry Smith if (!n) { 5606201da799SBarry Smith PetscDrawAxis axis; 5607bbd56ea5SKarl Rupp 5608201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5609201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 5610201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 5611bbd56ea5SKarl Rupp 5612201da799SBarry Smith ctx->ksp_its = 0; 5613201da799SBarry Smith } 5614201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 5615201da799SBarry Smith y = its - ctx->ksp_its; 5616201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 561799fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 5618201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 5619201da799SBarry Smith } 5620201da799SBarry Smith ctx->ksp_its = its; 5621201da799SBarry Smith PetscFunctionReturn(0); 5622201da799SBarry Smith } 5623f9c1d6abSBarry Smith 5624f9c1d6abSBarry Smith #undef __FUNCT__ 5625f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 5626f9c1d6abSBarry Smith /*@ 5627f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 5628f9c1d6abSBarry Smith 5629f9c1d6abSBarry Smith Collective on TS and Vec 5630f9c1d6abSBarry Smith 5631f9c1d6abSBarry Smith Input Parameters: 5632f9c1d6abSBarry Smith + ts - the TS context 5633f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 5634f9c1d6abSBarry Smith 5635f9c1d6abSBarry Smith Output Parameters: 5636f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 5637f9c1d6abSBarry Smith 5638f9c1d6abSBarry Smith Level: developer 5639f9c1d6abSBarry Smith 5640f9c1d6abSBarry Smith .keywords: TS, compute 5641f9c1d6abSBarry Smith 5642f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 5643f9c1d6abSBarry Smith @*/ 5644f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 5645f9c1d6abSBarry Smith { 5646f9c1d6abSBarry Smith PetscErrorCode ierr; 5647f9c1d6abSBarry Smith 5648f9c1d6abSBarry Smith PetscFunctionBegin; 5649f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5650ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 5651f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 5652f9c1d6abSBarry Smith PetscFunctionReturn(0); 5653f9c1d6abSBarry Smith } 565424655328SShri 565524655328SShri #undef __FUNCT__ 565624655328SShri #define __FUNCT__ "TSRollBack" 565724655328SShri /*@ 565824655328SShri TSRollBack - Rolls back one time step 565924655328SShri 566024655328SShri Collective on TS 566124655328SShri 566224655328SShri Input Parameter: 566324655328SShri . ts - the TS context obtained from TSCreate() 566424655328SShri 566524655328SShri Level: advanced 566624655328SShri 566724655328SShri .keywords: TS, timestep, rollback 566824655328SShri 566924655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 567024655328SShri @*/ 567124655328SShri PetscErrorCode TSRollBack(TS ts) 567224655328SShri { 567324655328SShri PetscErrorCode ierr; 567424655328SShri 567524655328SShri PetscFunctionBegin; 567624655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 567724655328SShri 567824655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 567924655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 568024655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 568124655328SShri ts->ptime = ts->ptime_prev; 568224655328SShri PetscFunctionReturn(0); 568324655328SShri } 5684aeb4809dSShri Abhyankar 5685ff22ae23SHong Zhang #undef __FUNCT__ 5686ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages" 5687ff22ae23SHong Zhang /*@ 5688ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 5689ff22ae23SHong Zhang 5690ff22ae23SHong Zhang Input Parameter: 5691ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 5692ff22ae23SHong Zhang 5693ff22ae23SHong Zhang Level: advanced 5694ff22ae23SHong Zhang 5695ff22ae23SHong Zhang .keywords: TS, getstages 5696ff22ae23SHong Zhang 5697ff22ae23SHong Zhang .seealso: TSCreate() 5698ff22ae23SHong Zhang @*/ 5699ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns, Vec **Y) 5700ff22ae23SHong Zhang { 5701ff22ae23SHong Zhang PetscErrorCode ierr; 5702ff22ae23SHong Zhang 5703ff22ae23SHong Zhang PetscFunctionBegin; 5704ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 5705ff22ae23SHong Zhang PetscValidPointer(ns,2); 5706ff22ae23SHong Zhang 5707ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 5708ff22ae23SHong Zhang else { 5709ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 5710ff22ae23SHong Zhang } 5711ff22ae23SHong Zhang PetscFunctionReturn(0); 5712ff22ae23SHong Zhang } 5713ff22ae23SHong Zhang 5714