163dd3a1aSKris Buschelman 2af0996ceSBarry Smith #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; 108d0ad7a8SHong Zhang PetscLogEvent TS_AdjointStep, 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 142d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx { 152d5ee99bSBarry Smith PetscViewer viewer; 164b363babSBarry Smith PetscDrawAxis axis; 172d5ee99bSBarry Smith Vec initialsolution; 182d5ee99bSBarry Smith PetscBool showinitial; 192d5ee99bSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 202d5ee99bSBarry Smith PetscBool showtimestepandtime; 212d5ee99bSBarry Smith int color; 222d5ee99bSBarry Smith }; 232d5ee99bSBarry Smith 24bdad233fSMatthew Knepley #undef __FUNCT__ 25bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions" 26bdad233fSMatthew Knepley /*@ 27bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 28bdad233fSMatthew Knepley 29bdad233fSMatthew Knepley Collective on TS 30bdad233fSMatthew Knepley 31bdad233fSMatthew Knepley Input Parameter: 32bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 33bdad233fSMatthew Knepley 34bdad233fSMatthew Knepley Options Database Keys: 354d91e141SJed Brown + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP 36ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 373e4cdcaaSBarry Smith . -ts_max_steps <maxsteps> - maximum number of time-steps to take 383e4cdcaaSBarry Smith . -ts_final_time <time> - maximum time to compute to 393e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 40a3cdaa26SBarry 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 41a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 42a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 43a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 44a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 45a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 46ef222394SBarry Smith . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 47847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 48bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 49de06c3feSJed Brown . -ts_monitor_lg_timestep - Monitor timestep size graphically 50de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 51de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 52de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 53de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 54de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 55de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 563e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 573e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 5891b97e58SBarry Smith . -ts_monitor_solution_binary <filename> - Save each solution to a binary file 59b3d3934dSBarry Smith . -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 60110eb670SHong Zhang . -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 61110eb670SHong Zhang . -ts_adjoint_monitor - print information at each adjoint time step 6253ea634cSHong Zhang - -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically 6353ea634cSHong Zhang 6491b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 65bdad233fSMatthew Knepley 66bdad233fSMatthew Knepley Level: beginner 67bdad233fSMatthew Knepley 68bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 69bdad233fSMatthew Knepley 70a313700dSBarry Smith .seealso: TSGetType() 71bdad233fSMatthew Knepley @*/ 727087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 73bdad233fSMatthew Knepley { 74bc952696SBarry Smith PetscBool opt,flg,tflg; 75dfbe8321SBarry Smith PetscErrorCode ierr; 76649052a6SBarry Smith PetscViewer monviewer; 77eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 78089b2837SJed Brown SNES snes; 791c3436cfSJed Brown TSAdapt adapt; 8031748224SBarry Smith PetscReal time_step; 8149354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 82d1212d36SBarry Smith char dir[16]; 836991f827SBarry Smith const char *defaultType; 846991f827SBarry Smith char typeName[256]; 85bdad233fSMatthew Knepley 86bdad233fSMatthew Knepley PetscFunctionBegin; 870700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 883194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 896991f827SBarry Smith if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 906991f827SBarry Smith else defaultType = TSEULER; 916991f827SBarry Smith 926991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 936991f827SBarry Smith ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 946991f827SBarry Smith if (opt) { 956991f827SBarry Smith ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 966991f827SBarry Smith } else { 976991f827SBarry Smith ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 986991f827SBarry Smith } 99bdad233fSMatthew Knepley 100bdad233fSMatthew Knepley /* Handle generic TS options */ 1010298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1020298fd71SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 1030298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 10431748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 10531748224SBarry Smith if (flg) { 10631748224SBarry Smith ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 10731748224SBarry Smith } 10849354f04SShri 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); 10949354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 1100298fd71SBarry 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); 1110298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 1120298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 1130298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 1140298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 115bdad233fSMatthew Knepley 11656f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 11756f85f32SBarry Smith { 11856f85f32SBarry Smith PetscBool set; 11956f85f32SBarry Smith flg = PETSC_FALSE; 12056f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 12156f85f32SBarry Smith if (set) { 12256f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 12356f85f32SBarry Smith } 12456f85f32SBarry Smith } 12556f85f32SBarry Smith #endif 12656f85f32SBarry Smith 127bdad233fSMatthew Knepley /* Monitor options */ 128a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 129eabae89aSBarry Smith if (flg) { 130ce94432eSBarry Smith ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr); 131649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 132bdad233fSMatthew Knepley } 1335180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1345180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1355180491cSLisandro Dalcin 1364f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 137a7cc72afSBarry Smith if (opt) { 1380b039ecaSBarry Smith TSMonitorLGCtx ctx; 1393923b477SBarry Smith PetscInt howoften = 1; 140a80ad3e0SBarry Smith 1410298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 1427f52daa2SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 1434f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 144b3603a34SBarry Smith } 1454f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 146b3603a34SBarry Smith if (opt) { 1470b039ecaSBarry Smith TSMonitorLGCtx ctx; 1483923b477SBarry Smith PetscInt howoften = 1; 149b3603a34SBarry Smith 1500298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 15122d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1524f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 153bdad233fSMatthew Knepley } 1544f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 155ef20d060SBarry Smith if (opt) { 1560b039ecaSBarry Smith TSMonitorLGCtx ctx; 1573923b477SBarry Smith PetscInt howoften = 1; 158ef20d060SBarry Smith 1590298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 16022d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1614f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 162ef20d060SBarry Smith } 163201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 164201da799SBarry Smith if (opt) { 165201da799SBarry Smith TSMonitorLGCtx ctx; 166201da799SBarry Smith PetscInt howoften = 1; 167201da799SBarry Smith 1680298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 1697f52daa2SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 170201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 171201da799SBarry Smith } 172201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 173201da799SBarry Smith if (opt) { 174201da799SBarry Smith TSMonitorLGCtx ctx; 175201da799SBarry Smith PetscInt howoften = 1; 176201da799SBarry Smith 1770298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 1787f52daa2SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 179201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 180201da799SBarry Smith } 1818189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 1828189c53fSBarry Smith if (opt) { 1838189c53fSBarry Smith TSMonitorSPEigCtx ctx; 1848189c53fSBarry Smith PetscInt howoften = 1; 1858189c53fSBarry Smith 1860298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 18722d28d08SBarry Smith ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1888189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 1898189c53fSBarry Smith } 190ef20d060SBarry Smith opt = PETSC_FALSE; 1910dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 192a7cc72afSBarry Smith if (opt) { 19383a4ac43SBarry Smith TSMonitorDrawCtx ctx; 19483a4ac43SBarry Smith PetscInt howoften = 1; 195a80ad3e0SBarry Smith 1960298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 197ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 19883a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 199bdad233fSMatthew Knepley } 200fb1732b5SBarry Smith opt = PETSC_FALSE; 2019110b2e7SHong Zhang ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr); 2029110b2e7SHong Zhang if (opt) { 2039110b2e7SHong Zhang TSMonitorDrawCtx ctx; 2049110b2e7SHong Zhang PetscInt howoften = 1; 2059110b2e7SHong Zhang 2069110b2e7SHong Zhang ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr); 2079110b2e7SHong Zhang ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 2089110b2e7SHong Zhang ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2099110b2e7SHong Zhang } 2109110b2e7SHong Zhang opt = PETSC_FALSE; 2112d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 2122d5ee99bSBarry Smith if (opt) { 2132d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2142d5ee99bSBarry Smith PetscReal bounds[4]; 2152d5ee99bSBarry Smith PetscInt n = 4; 2162d5ee99bSBarry Smith PetscDraw draw; 2172d5ee99bSBarry Smith 2182d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 2192d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2202d5ee99bSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr); 2212d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 2222d5ee99bSBarry Smith ierr = PetscDrawClear(draw);CHKERRQ(ierr); 2234b363babSBarry Smith ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr); 2244b363babSBarry Smith ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 225f54adfc0SBarry Smith ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 2264b363babSBarry Smith ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr); 22707995780SPeter Brune /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */ 2282d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2292d5ee99bSBarry Smith } 2302d5ee99bSBarry Smith opt = PETSC_FALSE; 2310dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 2323a471f94SBarry Smith if (opt) { 23383a4ac43SBarry Smith TSMonitorDrawCtx ctx; 23483a4ac43SBarry Smith PetscInt howoften = 1; 2353a471f94SBarry Smith 2360298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 237ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 23883a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2393a471f94SBarry Smith } 2403a471f94SBarry Smith opt = PETSC_FALSE; 24191b97e58SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 242fb1732b5SBarry Smith if (flg) { 243fb1732b5SBarry Smith PetscViewer ctx; 244fb1732b5SBarry Smith if (monfilename[0]) { 245ce94432eSBarry Smith ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 246c2fbc07fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 247fb1732b5SBarry Smith } else { 248ce94432eSBarry Smith ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts)); 2490298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr); 250fb1732b5SBarry Smith } 251fb1732b5SBarry Smith } 252ed81e22dSJed Brown opt = PETSC_FALSE; 25391b97e58SBarry 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); 254ed81e22dSJed Brown if (flg) { 255ed81e22dSJed Brown const char *ptr,*ptr2; 256ed81e22dSJed Brown char *filetemplate; 257ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 258ed81e22dSJed Brown /* Do some cursory validation of the input. */ 259ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 260ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 261ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 262ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 263ce94432eSBarry 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"); 264ed81e22dSJed Brown if (ptr2) break; 265ed81e22dSJed Brown } 266ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 267ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 268ed81e22dSJed Brown } 269bdad233fSMatthew Knepley 270d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 271d1212d36SBarry Smith if (flg) { 272d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 273d1212d36SBarry Smith int ray = 0; 274d1212d36SBarry Smith DMDADirection ddir; 275d1212d36SBarry Smith DM da; 276d1212d36SBarry Smith PetscMPIInt rank; 277d1212d36SBarry Smith 278ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 279d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 280d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 281ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 282d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 283d1212d36SBarry Smith 284d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 285b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 286d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 287d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 288ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 289d1212d36SBarry Smith if (!rank) { 290d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 291d1212d36SBarry Smith } 29251b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 293d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 294d1212d36SBarry Smith } 29551b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 29651b4a12fSMatthew G. Knepley if (flg) { 29751b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 29851b4a12fSMatthew G. Knepley int ray = 0; 29951b4a12fSMatthew G. Knepley DMDADirection ddir; 30051b4a12fSMatthew G. Knepley DM da; 30151b4a12fSMatthew G. Knepley PetscInt howoften = 1; 302d1212d36SBarry Smith 30351b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 30451b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 30551b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 30651b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 30751b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 3081c3436cfSJed Brown 30951b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 310b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 31151b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 31251b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 31351b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 31451b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 31551b4a12fSMatthew G. Knepley } 316a7a1495cSBarry Smith 317b3d3934dSBarry Smith ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 318b3d3934dSBarry Smith if (opt) { 319b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 320b3d3934dSBarry Smith 321b3d3934dSBarry Smith ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 322b3d3934dSBarry Smith ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 323b3d3934dSBarry Smith } 324b3d3934dSBarry Smith 325847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 326847ff0e1SMatthew G. Knepley ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr); 327847ff0e1SMatthew G. Knepley if (flg) { 328847ff0e1SMatthew G. Knepley DM dm; 329847ff0e1SMatthew G. Knepley DMTS tdm; 330847ff0e1SMatthew G. Knepley 331847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 332847ff0e1SMatthew G. Knepley ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr); 333847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 334847ff0e1SMatthew G. Knepley ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr); 335847ff0e1SMatthew G. Knepley ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 336847ff0e1SMatthew G. Knepley } 337847ff0e1SMatthew G. Knepley 338110eb670SHong Zhang ierr = PetscOptionsString("-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 339110eb670SHong Zhang if (flg) { 340110eb670SHong Zhang ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr); 341110eb670SHong Zhang ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 342110eb670SHong Zhang } 343110eb670SHong Zhang 3447781c08eSBarry Smith /* 3457781c08eSBarry Smith This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui 3467781c08eSBarry Smith will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin() 3477781c08eSBarry Smith */ 348552698daSJed Brown ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr); 349e55864a3SBarry Smith ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr); 350d763cef2SBarry Smith 351d763cef2SBarry Smith /* Handle specific TS options */ 352d763cef2SBarry Smith if (ts->ops->setfromoptions) { 3536991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 354d763cef2SBarry Smith } 355*fbc52257SHong Zhang 35668bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 35768bece0bSHong Zhang if (ts->trajectory) tflg = PETSC_TRUE; 35868bece0bSHong Zhang else tflg = PETSC_FALSE; 35968bece0bSHong Zhang ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 36068bece0bSHong Zhang if (tflg) { 36168bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 36268bece0bSHong Zhang } 36368bece0bSHong Zhang if (ts->adjoint_solve) tflg = PETSC_TRUE; 36468bece0bSHong Zhang else tflg = PETSC_FALSE; 36568bece0bSHong Zhang ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr); 36668bece0bSHong Zhang if (flg) { 36768bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 36868bece0bSHong Zhang ts->adjoint_solve = tflg; 36968bece0bSHong Zhang } 37068bece0bSHong Zhang if (ts->trajectory) { 37168bece0bSHong Zhang ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 37268bece0bSHong Zhang } 373d763cef2SBarry Smith 374d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 375d763cef2SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 376*fbc52257SHong Zhang ierr = PetscOptionsEnd();CHKERRQ(ierr); 377d763cef2SBarry Smith 3786991f827SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3796991f827SBarry Smith if (snes) { 3806991f827SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 38149c41c37SLisandro Dalcin ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 382d763cef2SBarry Smith } 38368bece0bSHong Zhang 384d763cef2SBarry Smith PetscFunctionReturn(0); 385d763cef2SBarry Smith } 386d763cef2SBarry Smith 387d763cef2SBarry Smith #undef __FUNCT__ 388bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory" 389d2daff3dSHong Zhang /*@ 390bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 391d2daff3dSHong Zhang 392d2daff3dSHong Zhang Collective on TS 393d2daff3dSHong Zhang 394d2daff3dSHong Zhang Input Parameters: 395bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 396bc952696SBarry Smith 39768bece0bSHong Zhang Note: This routine should be called after all TS options have been set 398d2daff3dSHong Zhang 399d2daff3dSHong Zhang Level: intermediate 400d2daff3dSHong Zhang 401bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve() 402d2daff3dSHong Zhang 403bc952696SBarry Smith .keywords: TS, set, checkpoint, 404d2daff3dSHong Zhang @*/ 405bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 406d2daff3dSHong Zhang { 407bc952696SBarry Smith PetscErrorCode ierr; 408bc952696SBarry Smith 409d2daff3dSHong Zhang PetscFunctionBegin; 410d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 411bc952696SBarry Smith if (!ts->trajectory) { 412bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 413972caf09SHong Zhang ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 414bc952696SBarry Smith } 415d2daff3dSHong Zhang PetscFunctionReturn(0); 416d2daff3dSHong Zhang } 417d2daff3dSHong Zhang 418a7a1495cSBarry Smith #undef __FUNCT__ 419a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian" 420a7a1495cSBarry Smith /*@ 421a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 422a7a1495cSBarry Smith set with TSSetRHSJacobian(). 423a7a1495cSBarry Smith 424a7a1495cSBarry Smith Collective on TS and Vec 425a7a1495cSBarry Smith 426a7a1495cSBarry Smith Input Parameters: 427316643e7SJed Brown + ts - the TS context 428a7a1495cSBarry Smith . t - current timestep 4290910c330SBarry Smith - U - input vector 430a7a1495cSBarry Smith 431a7a1495cSBarry Smith Output Parameters: 432a7a1495cSBarry Smith + A - Jacobian matrix 433a7a1495cSBarry Smith . B - optional preconditioning matrix 434a7a1495cSBarry Smith - flag - flag indicating matrix structure 435a7a1495cSBarry Smith 436a7a1495cSBarry Smith Notes: 437a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 438a7a1495cSBarry Smith is used internally within the nonlinear solvers. 439a7a1495cSBarry Smith 44094b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 441a7a1495cSBarry Smith flag parameter. 442a7a1495cSBarry Smith 443a7a1495cSBarry Smith Level: developer 444a7a1495cSBarry Smith 445a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 446a7a1495cSBarry Smith 44794b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 448a7a1495cSBarry Smith @*/ 449d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 450a7a1495cSBarry Smith { 451dfbe8321SBarry Smith PetscErrorCode ierr; 452270bf2e7SJed Brown PetscObjectState Ustate; 45324989b8cSPeter Brune DM dm; 454942e3340SBarry Smith DMTS tsdm; 45524989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 45624989b8cSPeter Brune void *ctx; 45724989b8cSPeter Brune TSIJacobian ijacobianfunc; 458b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 459a7a1495cSBarry Smith 460a7a1495cSBarry Smith PetscFunctionBegin; 4610700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4620910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 4630910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 46424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 465942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 46624989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 4670298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 468b2df71adSDebojyoti Ghosh ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 46959e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 470b2df71adSDebojyoti Ghosh if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 4710e4ef248SJed Brown PetscFunctionReturn(0); 472f8ede8e7SJed Brown } 473d90be118SSean Farley 474ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 475d90be118SSean Farley 476e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 47794ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 47894ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 47994ab13aaSBarry Smith if (A != B) { 48094ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 48194ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 482e1244c69SJed Brown } 483e1244c69SJed Brown ts->rhsjacobian.shift = 0; 484e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 485e1244c69SJed Brown } 486e1244c69SJed Brown 48724989b8cSPeter Brune if (rhsjacobianfunc) { 48894ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 489a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 490d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 491a7a1495cSBarry Smith PetscStackPop; 49294ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 493a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 49494ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,4); 49594ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,5); 496ef66eb69SBarry Smith } else { 49794ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 49894ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 499ef66eb69SBarry Smith } 5000e4ef248SJed Brown ts->rhsjacobian.time = t; 5010910c330SBarry Smith ts->rhsjacobian.X = U; 50259e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 503a7a1495cSBarry Smith PetscFunctionReturn(0); 504a7a1495cSBarry Smith } 505a7a1495cSBarry Smith 5064a2ae208SSatish Balay #undef __FUNCT__ 5074a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 508316643e7SJed Brown /*@ 509d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 510d763cef2SBarry Smith 511316643e7SJed Brown Collective on TS and Vec 512316643e7SJed Brown 513316643e7SJed Brown Input Parameters: 514316643e7SJed Brown + ts - the TS context 515316643e7SJed Brown . t - current time 5160910c330SBarry Smith - U - state vector 517316643e7SJed Brown 518316643e7SJed Brown Output Parameter: 519316643e7SJed Brown . y - right hand side 520316643e7SJed Brown 521316643e7SJed Brown Note: 522316643e7SJed Brown Most users should not need to explicitly call this routine, as it 523316643e7SJed Brown is used internally within the nonlinear solvers. 524316643e7SJed Brown 525316643e7SJed Brown Level: developer 526316643e7SJed Brown 527316643e7SJed Brown .keywords: TS, compute 528316643e7SJed Brown 529316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 530316643e7SJed Brown @*/ 5310910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 532d763cef2SBarry Smith { 533dfbe8321SBarry Smith PetscErrorCode ierr; 53424989b8cSPeter Brune TSRHSFunction rhsfunction; 53524989b8cSPeter Brune TSIFunction ifunction; 53624989b8cSPeter Brune void *ctx; 53724989b8cSPeter Brune DM dm; 53824989b8cSPeter Brune 539d763cef2SBarry Smith PetscFunctionBegin; 5400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5410910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5420700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 54324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 54424989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 5450298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 546d763cef2SBarry Smith 547ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 548d763cef2SBarry Smith 5490910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 55024989b8cSPeter Brune if (rhsfunction) { 551d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 5520910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 553d763cef2SBarry Smith PetscStackPop; 554214bc6a2SJed Brown } else { 555214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 556b2cd27e8SJed Brown } 55744a41b28SSean Farley 5580910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 559d763cef2SBarry Smith PetscFunctionReturn(0); 560d763cef2SBarry Smith } 561d763cef2SBarry Smith 5624a2ae208SSatish Balay #undef __FUNCT__ 563ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 564ef20d060SBarry Smith /*@ 565ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 566ef20d060SBarry Smith 567ef20d060SBarry Smith Collective on TS and Vec 568ef20d060SBarry Smith 569ef20d060SBarry Smith Input Parameters: 570ef20d060SBarry Smith + ts - the TS context 571ef20d060SBarry Smith - t - current time 572ef20d060SBarry Smith 573ef20d060SBarry Smith Output Parameter: 5740910c330SBarry Smith . U - the solution 575ef20d060SBarry Smith 576ef20d060SBarry Smith Note: 577ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 578ef20d060SBarry Smith is used internally within the nonlinear solvers. 579ef20d060SBarry Smith 580ef20d060SBarry Smith Level: developer 581ef20d060SBarry Smith 582ef20d060SBarry Smith .keywords: TS, compute 583ef20d060SBarry Smith 584abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 585ef20d060SBarry Smith @*/ 5860910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 587ef20d060SBarry Smith { 588ef20d060SBarry Smith PetscErrorCode ierr; 589ef20d060SBarry Smith TSSolutionFunction solutionfunction; 590ef20d060SBarry Smith void *ctx; 591ef20d060SBarry Smith DM dm; 592ef20d060SBarry Smith 593ef20d060SBarry Smith PetscFunctionBegin; 594ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5950910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 596ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 597ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 598ef20d060SBarry Smith 599ef20d060SBarry Smith if (solutionfunction) { 6009b7cd975SBarry Smith PetscStackPush("TS user solution function"); 6010910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 602ef20d060SBarry Smith PetscStackPop; 603ef20d060SBarry Smith } 604ef20d060SBarry Smith PetscFunctionReturn(0); 605ef20d060SBarry Smith } 6069b7cd975SBarry Smith #undef __FUNCT__ 6079b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction" 6089b7cd975SBarry Smith /*@ 6099b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6109b7cd975SBarry Smith 6119b7cd975SBarry Smith Collective on TS and Vec 6129b7cd975SBarry Smith 6139b7cd975SBarry Smith Input Parameters: 6149b7cd975SBarry Smith + ts - the TS context 6159b7cd975SBarry Smith - t - current time 6169b7cd975SBarry Smith 6179b7cd975SBarry Smith Output Parameter: 6189b7cd975SBarry Smith . U - the function value 6199b7cd975SBarry Smith 6209b7cd975SBarry Smith Note: 6219b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 6229b7cd975SBarry Smith is used internally within the nonlinear solvers. 6239b7cd975SBarry Smith 6249b7cd975SBarry Smith Level: developer 6259b7cd975SBarry Smith 6269b7cd975SBarry Smith .keywords: TS, compute 6279b7cd975SBarry Smith 6289b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 6299b7cd975SBarry Smith @*/ 6309b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 6319b7cd975SBarry Smith { 6329b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 6339b7cd975SBarry Smith void *ctx; 6349b7cd975SBarry Smith DM dm; 6359b7cd975SBarry Smith 6369b7cd975SBarry Smith PetscFunctionBegin; 6379b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6389b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6399b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 6409b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 6419b7cd975SBarry Smith 6429b7cd975SBarry Smith if (forcing) { 6439b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 6449b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 6459b7cd975SBarry Smith PetscStackPop; 6469b7cd975SBarry Smith } 6479b7cd975SBarry Smith PetscFunctionReturn(0); 6489b7cd975SBarry Smith } 649ef20d060SBarry Smith 650ef20d060SBarry Smith #undef __FUNCT__ 651214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 652214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 653214bc6a2SJed Brown { 6542dd45cf8SJed Brown Vec F; 655214bc6a2SJed Brown PetscErrorCode ierr; 656214bc6a2SJed Brown 657214bc6a2SJed Brown PetscFunctionBegin; 6580298fd71SBarry Smith *Frhs = NULL; 6590298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 660214bc6a2SJed Brown if (!ts->Frhs) { 6612dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 662214bc6a2SJed Brown } 663214bc6a2SJed Brown *Frhs = ts->Frhs; 664214bc6a2SJed Brown PetscFunctionReturn(0); 665214bc6a2SJed Brown } 666214bc6a2SJed Brown 667214bc6a2SJed Brown #undef __FUNCT__ 668214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 669214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 670214bc6a2SJed Brown { 671214bc6a2SJed Brown Mat A,B; 6722dd45cf8SJed Brown PetscErrorCode ierr; 673214bc6a2SJed Brown 674214bc6a2SJed Brown PetscFunctionBegin; 675c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 676c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 6770298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 678214bc6a2SJed Brown if (Arhs) { 679214bc6a2SJed Brown if (!ts->Arhs) { 680214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 681214bc6a2SJed Brown } 682214bc6a2SJed Brown *Arhs = ts->Arhs; 683214bc6a2SJed Brown } 684214bc6a2SJed Brown if (Brhs) { 685214bc6a2SJed Brown if (!ts->Brhs) { 686bdb70873SJed Brown if (A != B) { 687214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 688bdb70873SJed Brown } else { 689bdb70873SJed Brown ts->Brhs = ts->Arhs; 690bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 691bdb70873SJed Brown } 692214bc6a2SJed Brown } 693214bc6a2SJed Brown *Brhs = ts->Brhs; 694214bc6a2SJed Brown } 695214bc6a2SJed Brown PetscFunctionReturn(0); 696214bc6a2SJed Brown } 697214bc6a2SJed Brown 698214bc6a2SJed Brown #undef __FUNCT__ 699316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 700316643e7SJed Brown /*@ 7010910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 702316643e7SJed Brown 703316643e7SJed Brown Collective on TS and Vec 704316643e7SJed Brown 705316643e7SJed Brown Input Parameters: 706316643e7SJed Brown + ts - the TS context 707316643e7SJed Brown . t - current time 7080910c330SBarry Smith . U - state vector 7090910c330SBarry Smith . Udot - time derivative of state vector 710214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 711316643e7SJed Brown 712316643e7SJed Brown Output Parameter: 713316643e7SJed Brown . Y - right hand side 714316643e7SJed Brown 715316643e7SJed Brown Note: 716316643e7SJed Brown Most users should not need to explicitly call this routine, as it 717316643e7SJed Brown is used internally within the nonlinear solvers. 718316643e7SJed Brown 719316643e7SJed Brown If the user did did not write their equations in implicit form, this 720316643e7SJed Brown function recasts them in implicit form. 721316643e7SJed Brown 722316643e7SJed Brown Level: developer 723316643e7SJed Brown 724316643e7SJed Brown .keywords: TS, compute 725316643e7SJed Brown 726316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 727316643e7SJed Brown @*/ 7280910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 729316643e7SJed Brown { 730316643e7SJed Brown PetscErrorCode ierr; 73124989b8cSPeter Brune TSIFunction ifunction; 73224989b8cSPeter Brune TSRHSFunction rhsfunction; 73324989b8cSPeter Brune void *ctx; 73424989b8cSPeter Brune DM dm; 735316643e7SJed Brown 736316643e7SJed Brown PetscFunctionBegin; 7370700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7380910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7390910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 7400700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 741316643e7SJed Brown 74224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 74324989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 7440298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 74524989b8cSPeter Brune 746ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 747d90be118SSean Farley 7480910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 74924989b8cSPeter Brune if (ifunction) { 750316643e7SJed Brown PetscStackPush("TS user implicit function"); 7510910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 752316643e7SJed Brown PetscStackPop; 753214bc6a2SJed Brown } 754214bc6a2SJed Brown if (imex) { 75524989b8cSPeter Brune if (!ifunction) { 7560910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 7572dd45cf8SJed Brown } 75824989b8cSPeter Brune } else if (rhsfunction) { 75924989b8cSPeter Brune if (ifunction) { 760214bc6a2SJed Brown Vec Frhs; 761214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 7620910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 763214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 7642dd45cf8SJed Brown } else { 7650910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 7660910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 767316643e7SJed Brown } 7684a6899ffSJed Brown } 7690910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 770316643e7SJed Brown PetscFunctionReturn(0); 771316643e7SJed Brown } 772316643e7SJed Brown 773316643e7SJed Brown #undef __FUNCT__ 774316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 775316643e7SJed Brown /*@ 776316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 777316643e7SJed Brown 778316643e7SJed Brown Collective on TS and Vec 779316643e7SJed Brown 780316643e7SJed Brown Input 781316643e7SJed Brown Input Parameters: 782316643e7SJed Brown + ts - the TS context 783316643e7SJed Brown . t - current timestep 7840910c330SBarry Smith . U - state vector 7850910c330SBarry Smith . Udot - time derivative of state vector 786214bc6a2SJed Brown . shift - shift to apply, see note below 787214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 788316643e7SJed Brown 789316643e7SJed Brown Output Parameters: 790316643e7SJed Brown + A - Jacobian matrix 791316643e7SJed Brown . B - optional preconditioning matrix 792316643e7SJed Brown - flag - flag indicating matrix structure 793316643e7SJed Brown 794316643e7SJed Brown Notes: 7950910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 796316643e7SJed Brown 7970910c330SBarry Smith dF/dU + shift*dF/dUdot 798316643e7SJed Brown 799316643e7SJed Brown Most users should not need to explicitly call this routine, as it 800316643e7SJed Brown is used internally within the nonlinear solvers. 801316643e7SJed Brown 802316643e7SJed Brown Level: developer 803316643e7SJed Brown 804316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 805316643e7SJed Brown 806316643e7SJed Brown .seealso: TSSetIJacobian() 80763495f91SJed Brown @*/ 808d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 809316643e7SJed Brown { 810316643e7SJed Brown PetscErrorCode ierr; 81124989b8cSPeter Brune TSIJacobian ijacobian; 81224989b8cSPeter Brune TSRHSJacobian rhsjacobian; 81324989b8cSPeter Brune DM dm; 81424989b8cSPeter Brune void *ctx; 815316643e7SJed Brown 816316643e7SJed Brown PetscFunctionBegin; 8170700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8180910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8190910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 820316643e7SJed Brown PetscValidPointer(A,6); 82194ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 822316643e7SJed Brown PetscValidPointer(B,7); 82394ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 82424989b8cSPeter Brune 82524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 82624989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 8270298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 82824989b8cSPeter Brune 829ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 830316643e7SJed Brown 83194ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 83224989b8cSPeter Brune if (ijacobian) { 833316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 834d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 835316643e7SJed Brown PetscStackPop; 836214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 83794ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,4); 83894ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,5); 8394a6899ffSJed Brown } 840214bc6a2SJed Brown if (imex) { 841b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 84294ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 84394ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 84494ab13aaSBarry Smith if (A != B) { 84594ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 84694ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 847214bc6a2SJed Brown } 848214bc6a2SJed Brown } 849214bc6a2SJed Brown } else { 850e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 851e1244c69SJed Brown if (rhsjacobian) { 852bd373395SBarry Smith if (ijacobian) { 853214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 854bd373395SBarry Smith } else { 855bd373395SBarry Smith ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr); 856214bc6a2SJed Brown } 857d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 858e1244c69SJed Brown } 85994ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 860e1244c69SJed Brown ts->rhsjacobian.scale = -1; 861e1244c69SJed Brown ts->rhsjacobian.shift = shift; 86294ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 86394ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 86494ab13aaSBarry Smith if (A != B) { 86594ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 86694ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 867316643e7SJed Brown } 868e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 869e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 870e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 87194ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 87294ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 87394ab13aaSBarry Smith if (A != B) { 87494ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 87594ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 876214bc6a2SJed Brown } 877316643e7SJed Brown } 87894ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 87994ab13aaSBarry Smith if (A != B) { 88094ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 881316643e7SJed Brown } 882316643e7SJed Brown } 883316643e7SJed Brown } 88494ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 885316643e7SJed Brown PetscFunctionReturn(0); 886316643e7SJed Brown } 887316643e7SJed Brown 888316643e7SJed Brown #undef __FUNCT__ 8894a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 890d763cef2SBarry Smith /*@C 891d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 892b5abc632SBarry Smith where U_t = G(t,u). 893d763cef2SBarry Smith 8943f9fe445SBarry Smith Logically Collective on TS 895d763cef2SBarry Smith 896d763cef2SBarry Smith Input Parameters: 897d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 8980298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 899d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 900d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 9010298fd71SBarry Smith function evaluation routine (may be NULL) 902d763cef2SBarry Smith 903d763cef2SBarry Smith Calling sequence of func: 90487828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 905d763cef2SBarry Smith 906d763cef2SBarry Smith + t - current timestep 907d763cef2SBarry Smith . u - input vector 908d763cef2SBarry Smith . F - function vector 909d763cef2SBarry Smith - ctx - [optional] user-defined function context 910d763cef2SBarry Smith 911d763cef2SBarry Smith Level: beginner 912d763cef2SBarry Smith 9132bbac0d3SBarry Smith Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 9142bbac0d3SBarry Smith 915d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 916d763cef2SBarry Smith 917ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 918d763cef2SBarry Smith @*/ 919089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 920d763cef2SBarry Smith { 921089b2837SJed Brown PetscErrorCode ierr; 922089b2837SJed Brown SNES snes; 9230298fd71SBarry Smith Vec ralloc = NULL; 92424989b8cSPeter Brune DM dm; 925d763cef2SBarry Smith 926089b2837SJed Brown PetscFunctionBegin; 9270700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 928ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 92924989b8cSPeter Brune 93024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 93124989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 932089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 933e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 934e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 935e856ceecSJed Brown r = ralloc; 936e856ceecSJed Brown } 937089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 938e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 939d763cef2SBarry Smith PetscFunctionReturn(0); 940d763cef2SBarry Smith } 941d763cef2SBarry Smith 9424a2ae208SSatish Balay #undef __FUNCT__ 943ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 944ef20d060SBarry Smith /*@C 945abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 946ef20d060SBarry Smith 947ef20d060SBarry Smith Logically Collective on TS 948ef20d060SBarry Smith 949ef20d060SBarry Smith Input Parameters: 950ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 951ef20d060SBarry Smith . f - routine for evaluating the solution 952ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 9530298fd71SBarry Smith function evaluation routine (may be NULL) 954ef20d060SBarry Smith 955ef20d060SBarry Smith Calling sequence of func: 956ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 957ef20d060SBarry Smith 958ef20d060SBarry Smith + t - current timestep 959ef20d060SBarry Smith . u - output vector 960ef20d060SBarry Smith - ctx - [optional] user-defined function context 961ef20d060SBarry Smith 962abd5a294SJed Brown Notes: 963abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 964abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 965abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 966abd5a294SJed Brown 9674f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 968abd5a294SJed Brown 969ef20d060SBarry Smith Level: beginner 970ef20d060SBarry Smith 971ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 972ef20d060SBarry Smith 9739b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 974ef20d060SBarry Smith @*/ 975ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 976ef20d060SBarry Smith { 977ef20d060SBarry Smith PetscErrorCode ierr; 978ef20d060SBarry Smith DM dm; 979ef20d060SBarry Smith 980ef20d060SBarry Smith PetscFunctionBegin; 981ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 982ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 983ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 984ef20d060SBarry Smith PetscFunctionReturn(0); 985ef20d060SBarry Smith } 986ef20d060SBarry Smith 987ef20d060SBarry Smith #undef __FUNCT__ 9889b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction" 9899b7cd975SBarry Smith /*@C 9909b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 9919b7cd975SBarry Smith 9929b7cd975SBarry Smith Logically Collective on TS 9939b7cd975SBarry Smith 9949b7cd975SBarry Smith Input Parameters: 9959b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 9969b7cd975SBarry Smith . f - routine for evaluating the forcing function 9979b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 9980298fd71SBarry Smith function evaluation routine (may be NULL) 9999b7cd975SBarry Smith 10009b7cd975SBarry Smith Calling sequence of func: 10019b7cd975SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 10029b7cd975SBarry Smith 10039b7cd975SBarry Smith + t - current timestep 10049b7cd975SBarry Smith . u - output vector 10059b7cd975SBarry Smith - ctx - [optional] user-defined function context 10069b7cd975SBarry Smith 10079b7cd975SBarry Smith Notes: 10089b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 10099b7cd975SBarry Smith create closed-form solutions with a non-physical forcing term. 10109b7cd975SBarry Smith 10119b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 10129b7cd975SBarry Smith 10139b7cd975SBarry Smith Level: beginner 10149b7cd975SBarry Smith 10159b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 10169b7cd975SBarry Smith 10179b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 10189b7cd975SBarry Smith @*/ 10199b7cd975SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 10209b7cd975SBarry Smith { 10219b7cd975SBarry Smith PetscErrorCode ierr; 10229b7cd975SBarry Smith DM dm; 10239b7cd975SBarry Smith 10249b7cd975SBarry Smith PetscFunctionBegin; 10259b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10269b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 10279b7cd975SBarry Smith ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr); 10289b7cd975SBarry Smith PetscFunctionReturn(0); 10299b7cd975SBarry Smith } 10309b7cd975SBarry Smith 10319b7cd975SBarry Smith #undef __FUNCT__ 10324a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 1033d763cef2SBarry Smith /*@C 1034f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1035b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1036d763cef2SBarry Smith 10373f9fe445SBarry Smith Logically Collective on TS 1038d763cef2SBarry Smith 1039d763cef2SBarry Smith Input Parameters: 1040d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1041e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1042e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1043d763cef2SBarry Smith . f - the Jacobian evaluation routine 1044d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 10450298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1046d763cef2SBarry Smith 1047f7ab8db6SBarry Smith Calling sequence of f: 1048ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1049d763cef2SBarry Smith 1050d763cef2SBarry Smith + t - current timestep 1051d763cef2SBarry Smith . u - input vector 1052e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1053e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1054d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1055d763cef2SBarry Smith 1056ca5f011dSBarry Smith Notes: The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1057ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1058d763cef2SBarry Smith 1059d763cef2SBarry Smith Level: beginner 1060d763cef2SBarry Smith 1061d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1062d763cef2SBarry Smith 1063ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1064d763cef2SBarry Smith 1065d763cef2SBarry Smith @*/ 1066e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1067d763cef2SBarry Smith { 1068277b19d0SLisandro Dalcin PetscErrorCode ierr; 1069089b2837SJed Brown SNES snes; 107024989b8cSPeter Brune DM dm; 107124989b8cSPeter Brune TSIJacobian ijacobian; 1072277b19d0SLisandro Dalcin 1073d763cef2SBarry Smith PetscFunctionBegin; 10740700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1075e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1076e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1077e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1078e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1079d763cef2SBarry Smith 108024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 108124989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1082e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1083e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1084e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1085e1244c69SJed Brown } 10860298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1087089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 10885f659677SPeter Brune if (!ijacobian) { 1089e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 10900e4ef248SJed Brown } 1091e5d3d808SBarry Smith if (Amat) { 1092e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 10930e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1094bbd56ea5SKarl Rupp 1095e5d3d808SBarry Smith ts->Arhs = Amat; 10960e4ef248SJed Brown } 1097e5d3d808SBarry Smith if (Pmat) { 1098e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 10990e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1100bbd56ea5SKarl Rupp 1101e5d3d808SBarry Smith ts->Brhs = Pmat; 11020e4ef248SJed Brown } 1103d763cef2SBarry Smith PetscFunctionReturn(0); 1104d763cef2SBarry Smith } 1105d763cef2SBarry Smith 1106316643e7SJed Brown 1107316643e7SJed Brown #undef __FUNCT__ 1108316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 1109316643e7SJed Brown /*@C 1110b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1111316643e7SJed Brown 11123f9fe445SBarry Smith Logically Collective on TS 1113316643e7SJed Brown 1114316643e7SJed Brown Input Parameters: 1115316643e7SJed Brown + ts - the TS context obtained from TSCreate() 11160298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1117316643e7SJed Brown . f - the function evaluation routine 11180298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1119316643e7SJed Brown 1120316643e7SJed Brown Calling sequence of f: 1121316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1122316643e7SJed Brown 1123316643e7SJed Brown + t - time at step/stage being solved 1124316643e7SJed Brown . u - state vector 1125316643e7SJed Brown . u_t - time derivative of state vector 1126316643e7SJed Brown . F - function vector 1127316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1128316643e7SJed Brown 1129316643e7SJed Brown Important: 11302bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1131316643e7SJed Brown 1132316643e7SJed Brown Level: beginner 1133316643e7SJed Brown 1134316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1135316643e7SJed Brown 1136d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1137316643e7SJed Brown @*/ 1138089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 1139316643e7SJed Brown { 1140089b2837SJed Brown PetscErrorCode ierr; 1141089b2837SJed Brown SNES snes; 11420298fd71SBarry Smith Vec resalloc = NULL; 114324989b8cSPeter Brune DM dm; 1144316643e7SJed Brown 1145316643e7SJed Brown PetscFunctionBegin; 11460700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1147ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 114824989b8cSPeter Brune 114924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 115024989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 115124989b8cSPeter Brune 1152089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1153e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 1154e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 1155e856ceecSJed Brown res = resalloc; 1156e856ceecSJed Brown } 1157089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 1158e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 1159089b2837SJed Brown PetscFunctionReturn(0); 1160089b2837SJed Brown } 1161089b2837SJed Brown 1162089b2837SJed Brown #undef __FUNCT__ 1163089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 1164089b2837SJed Brown /*@C 1165089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1166089b2837SJed Brown 1167089b2837SJed Brown Not Collective 1168089b2837SJed Brown 1169089b2837SJed Brown Input Parameter: 1170089b2837SJed Brown . ts - the TS context 1171089b2837SJed Brown 1172089b2837SJed Brown Output Parameter: 11730298fd71SBarry Smith + r - vector to hold residual (or NULL) 11740298fd71SBarry Smith . func - the function to compute residual (or NULL) 11750298fd71SBarry Smith - ctx - the function context (or NULL) 1176089b2837SJed Brown 1177089b2837SJed Brown Level: advanced 1178089b2837SJed Brown 1179089b2837SJed Brown .keywords: TS, nonlinear, get, function 1180089b2837SJed Brown 1181089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1182089b2837SJed Brown @*/ 1183089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1184089b2837SJed Brown { 1185089b2837SJed Brown PetscErrorCode ierr; 1186089b2837SJed Brown SNES snes; 118724989b8cSPeter Brune DM dm; 1188089b2837SJed Brown 1189089b2837SJed Brown PetscFunctionBegin; 1190089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1191089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11920298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 119324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 119424989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1195089b2837SJed Brown PetscFunctionReturn(0); 1196089b2837SJed Brown } 1197089b2837SJed Brown 1198089b2837SJed Brown #undef __FUNCT__ 1199089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 1200089b2837SJed Brown /*@C 1201089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1202089b2837SJed Brown 1203089b2837SJed Brown Not Collective 1204089b2837SJed Brown 1205089b2837SJed Brown Input Parameter: 1206089b2837SJed Brown . ts - the TS context 1207089b2837SJed Brown 1208089b2837SJed Brown Output Parameter: 12090298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 12100298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 12110298fd71SBarry Smith - ctx - the function context (or NULL) 1212089b2837SJed Brown 1213089b2837SJed Brown Level: advanced 1214089b2837SJed Brown 1215089b2837SJed Brown .keywords: TS, nonlinear, get, function 1216089b2837SJed Brown 12172bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1218089b2837SJed Brown @*/ 1219089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1220089b2837SJed Brown { 1221089b2837SJed Brown PetscErrorCode ierr; 1222089b2837SJed Brown SNES snes; 122324989b8cSPeter Brune DM dm; 1224089b2837SJed Brown 1225089b2837SJed Brown PetscFunctionBegin; 1226089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1227089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12280298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 122924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 123024989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1231316643e7SJed Brown PetscFunctionReturn(0); 1232316643e7SJed Brown } 1233316643e7SJed Brown 1234316643e7SJed Brown #undef __FUNCT__ 1235316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 1236316643e7SJed Brown /*@C 1237a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1238ae8867d6SBarry Smith provided with TSSetIFunction(). 1239316643e7SJed Brown 12403f9fe445SBarry Smith Logically Collective on TS 1241316643e7SJed Brown 1242316643e7SJed Brown Input Parameters: 1243316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1244e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1245e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1246316643e7SJed Brown . f - the Jacobian evaluation routine 12470298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1248316643e7SJed Brown 1249316643e7SJed Brown Calling sequence of f: 1250ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1251316643e7SJed Brown 1252316643e7SJed Brown + t - time at step/stage being solved 12531b4a444bSJed Brown . U - state vector 12541b4a444bSJed Brown . U_t - time derivative of state vector 1255316643e7SJed Brown . a - shift 1256e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1257e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1258316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1259316643e7SJed Brown 1260316643e7SJed Brown Notes: 1261e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1262316643e7SJed Brown 1263895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1264895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1265895c21f2SBarry Smith 1266a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1267b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1268a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1269a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1270a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1271a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1272a4f0a591SBarry Smith 1273ca5f011dSBarry Smith Notes: The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1274ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1275ca5f011dSBarry Smith 1276316643e7SJed Brown Level: beginner 1277316643e7SJed Brown 1278316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1279316643e7SJed Brown 1280ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1281316643e7SJed Brown 1282316643e7SJed Brown @*/ 1283e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1284316643e7SJed Brown { 1285316643e7SJed Brown PetscErrorCode ierr; 1286089b2837SJed Brown SNES snes; 128724989b8cSPeter Brune DM dm; 1288316643e7SJed Brown 1289316643e7SJed Brown PetscFunctionBegin; 12900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1291e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1292e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1293e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1294e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 129524989b8cSPeter Brune 129624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 129724989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 129824989b8cSPeter Brune 1299089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1300e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1301316643e7SJed Brown PetscFunctionReturn(0); 1302316643e7SJed Brown } 1303316643e7SJed Brown 13044a2ae208SSatish Balay #undef __FUNCT__ 1305e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse" 1306e1244c69SJed Brown /*@ 1307e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1308e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1309e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1310e1244c69SJed Brown not been changed by the TS. 1311e1244c69SJed Brown 1312e1244c69SJed Brown Logically Collective 1313e1244c69SJed Brown 1314e1244c69SJed Brown Input Arguments: 1315e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1316e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1317e1244c69SJed Brown 1318e1244c69SJed Brown Level: intermediate 1319e1244c69SJed Brown 1320e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1321e1244c69SJed Brown @*/ 1322e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1323e1244c69SJed Brown { 1324e1244c69SJed Brown PetscFunctionBegin; 1325e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1326e1244c69SJed Brown PetscFunctionReturn(0); 1327e1244c69SJed Brown } 1328e1244c69SJed Brown 1329e1244c69SJed Brown #undef __FUNCT__ 133055849f57SBarry Smith #define __FUNCT__ "TSLoad" 133155849f57SBarry Smith /*@C 133255849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 133355849f57SBarry Smith 133455849f57SBarry Smith Collective on PetscViewer 133555849f57SBarry Smith 133655849f57SBarry Smith Input Parameters: 133755849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 133855849f57SBarry Smith some related function before a call to TSLoad(). 133955849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 134055849f57SBarry Smith 134155849f57SBarry Smith Level: intermediate 134255849f57SBarry Smith 134355849f57SBarry Smith Notes: 134455849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 134555849f57SBarry Smith 134655849f57SBarry Smith Notes for advanced users: 134755849f57SBarry Smith Most users should not need to know the details of the binary storage 134855849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 134955849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 135055849f57SBarry Smith format is 135155849f57SBarry Smith .vb 135255849f57SBarry Smith has not yet been determined 135355849f57SBarry Smith .ve 135455849f57SBarry Smith 135555849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 135655849f57SBarry Smith @*/ 1357f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 135855849f57SBarry Smith { 135955849f57SBarry Smith PetscErrorCode ierr; 136055849f57SBarry Smith PetscBool isbinary; 136155849f57SBarry Smith PetscInt classid; 136255849f57SBarry Smith char type[256]; 13632d53ad75SBarry Smith DMTS sdm; 1364ad6bc421SBarry Smith DM dm; 136555849f57SBarry Smith 136655849f57SBarry Smith PetscFunctionBegin; 1367f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 136855849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 136955849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 137055849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 137155849f57SBarry Smith 1372060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1373ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1374060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1375f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1376f2c2a1b9SBarry Smith if (ts->ops->load) { 1377f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1378f2c2a1b9SBarry Smith } 1379ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1380ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1381ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1382f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1383f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 13842d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 13852d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 138655849f57SBarry Smith PetscFunctionReturn(0); 138755849f57SBarry Smith } 138855849f57SBarry Smith 13899804daf3SBarry Smith #include <petscdraw.h> 1390e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1391e04113cfSBarry Smith #include <petscviewersaws.h> 1392f05ece33SBarry Smith #endif 139355849f57SBarry Smith #undef __FUNCT__ 13944a2ae208SSatish Balay #define __FUNCT__ "TSView" 13957e2c5f70SBarry Smith /*@C 1396d763cef2SBarry Smith TSView - Prints the TS data structure. 1397d763cef2SBarry Smith 13984c49b128SBarry Smith Collective on TS 1399d763cef2SBarry Smith 1400d763cef2SBarry Smith Input Parameters: 1401d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1402d763cef2SBarry Smith - viewer - visualization context 1403d763cef2SBarry Smith 1404d763cef2SBarry Smith Options Database Key: 1405d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1406d763cef2SBarry Smith 1407d763cef2SBarry Smith Notes: 1408d763cef2SBarry Smith The available visualization contexts include 1409b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1410b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1411d763cef2SBarry Smith output where only the first processor opens 1412d763cef2SBarry Smith the file. All other processors send their 1413d763cef2SBarry Smith data to the first processor to print. 1414d763cef2SBarry Smith 1415d763cef2SBarry Smith The user can open an alternative visualization context with 1416b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1417d763cef2SBarry Smith 1418d763cef2SBarry Smith Level: beginner 1419d763cef2SBarry Smith 1420d763cef2SBarry Smith .keywords: TS, timestep, view 1421d763cef2SBarry Smith 1422b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1423d763cef2SBarry Smith @*/ 14247087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1425d763cef2SBarry Smith { 1426dfbe8321SBarry Smith PetscErrorCode ierr; 142719fd82e9SBarry Smith TSType type; 14282b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 14292d53ad75SBarry Smith DMTS sdm; 1430e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1431536b137fSBarry Smith PetscBool issaws; 1432f05ece33SBarry Smith #endif 1433d763cef2SBarry Smith 1434d763cef2SBarry Smith PetscFunctionBegin; 14350700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14363050cee2SBarry Smith if (!viewer) { 1437ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 14383050cee2SBarry Smith } 14390700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1440c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1441fd16b177SBarry Smith 1442251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1443251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 144455849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 14452b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1446e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1447536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1448f05ece33SBarry Smith #endif 144932077d6dSBarry Smith if (iascii) { 1450dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 145177431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 14527c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1453d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 14545ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1455c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1456d763cef2SBarry Smith } 14575ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1458193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 14592d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 14602d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1461d52bd9f3SBarry Smith if (ts->ops->view) { 1462d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1463d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1464d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1465d52bd9f3SBarry Smith } 14660f5bd95cSBarry Smith } else if (isstring) { 1467a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1468b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 146955849f57SBarry Smith } else if (isbinary) { 147055849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 147155849f57SBarry Smith MPI_Comm comm; 147255849f57SBarry Smith PetscMPIInt rank; 147355849f57SBarry Smith char type[256]; 147455849f57SBarry Smith 147555849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 147655849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 147755849f57SBarry Smith if (!rank) { 147855849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 147955849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 148055849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 148155849f57SBarry Smith } 148255849f57SBarry Smith if (ts->ops->view) { 148355849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 148455849f57SBarry Smith } 1485f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1486f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 14872d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 14882d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 14892b0a91c0SBarry Smith } else if (isdraw) { 14902b0a91c0SBarry Smith PetscDraw draw; 14912b0a91c0SBarry Smith char str[36]; 149289fd9fafSBarry Smith PetscReal x,y,bottom,h; 14932b0a91c0SBarry Smith 14942b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 14952b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 14962b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 14972b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 149851fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 149989fd9fafSBarry Smith bottom = y - h; 15002b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 15012b0a91c0SBarry Smith if (ts->ops->view) { 15022b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 15032b0a91c0SBarry Smith } 15042b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1505e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1506536b137fSBarry Smith } else if (issaws) { 1507d45a07a7SBarry Smith PetscMPIInt rank; 15082657e9d9SBarry Smith const char *name; 15092657e9d9SBarry Smith 15102657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1511d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1512d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1513d45a07a7SBarry Smith char dir[1024]; 1514d45a07a7SBarry Smith 1515e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1516a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 15172657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 15182657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 15192657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1520d763cef2SBarry Smith } 15210acecf5bSBarry Smith if (ts->ops->view) { 15220acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 15230acecf5bSBarry Smith } 1524f05ece33SBarry Smith #endif 1525f05ece33SBarry Smith } 1526f05ece33SBarry Smith 1527b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1528251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1529b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1530d763cef2SBarry Smith PetscFunctionReturn(0); 1531d763cef2SBarry Smith } 1532d763cef2SBarry Smith 1533d763cef2SBarry Smith 15344a2ae208SSatish Balay #undef __FUNCT__ 15354a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1536b07ff414SBarry Smith /*@ 1537d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1538d763cef2SBarry Smith the timesteppers. 1539d763cef2SBarry Smith 15403f9fe445SBarry Smith Logically Collective on TS 1541d763cef2SBarry Smith 1542d763cef2SBarry Smith Input Parameters: 1543d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1544d763cef2SBarry Smith - usrP - optional user context 1545d763cef2SBarry Smith 1546daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 1547daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1548daf670e6SBarry Smith 1549d763cef2SBarry Smith Level: intermediate 1550d763cef2SBarry Smith 1551d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1552d763cef2SBarry Smith 1553d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1554d763cef2SBarry Smith @*/ 15557087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1556d763cef2SBarry Smith { 1557d763cef2SBarry Smith PetscFunctionBegin; 15580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1559d763cef2SBarry Smith ts->user = usrP; 1560d763cef2SBarry Smith PetscFunctionReturn(0); 1561d763cef2SBarry Smith } 1562d763cef2SBarry Smith 15634a2ae208SSatish Balay #undef __FUNCT__ 15644a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1565b07ff414SBarry Smith /*@ 1566d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1567d763cef2SBarry Smith timestepper. 1568d763cef2SBarry Smith 1569d763cef2SBarry Smith Not Collective 1570d763cef2SBarry Smith 1571d763cef2SBarry Smith Input Parameter: 1572d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1573d763cef2SBarry Smith 1574d763cef2SBarry Smith Output Parameter: 1575d763cef2SBarry Smith . usrP - user context 1576d763cef2SBarry Smith 1577daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 1578daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1579daf670e6SBarry Smith 1580d763cef2SBarry Smith Level: intermediate 1581d763cef2SBarry Smith 1582d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1583d763cef2SBarry Smith 1584d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1585d763cef2SBarry Smith @*/ 1586e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1587d763cef2SBarry Smith { 1588d763cef2SBarry Smith PetscFunctionBegin; 15890700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1590e71120c6SJed Brown *(void**)usrP = ts->user; 1591d763cef2SBarry Smith PetscFunctionReturn(0); 1592d763cef2SBarry Smith } 1593d763cef2SBarry Smith 15944a2ae208SSatish Balay #undef __FUNCT__ 15954a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1596d763cef2SBarry Smith /*@ 1597b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1598d763cef2SBarry Smith 1599d763cef2SBarry Smith Not Collective 1600d763cef2SBarry Smith 1601d763cef2SBarry Smith Input Parameter: 1602d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1603d763cef2SBarry Smith 1604d763cef2SBarry Smith Output Parameter: 1605b8123daeSJed Brown . iter - number of steps completed so far 1606d763cef2SBarry Smith 1607d763cef2SBarry Smith Level: intermediate 1608d763cef2SBarry Smith 1609d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 16109be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 1611d763cef2SBarry Smith @*/ 16127087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 1613d763cef2SBarry Smith { 1614d763cef2SBarry Smith PetscFunctionBegin; 16150700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 16164482741eSBarry Smith PetscValidIntPointer(iter,2); 1617d763cef2SBarry Smith *iter = ts->steps; 1618d763cef2SBarry Smith PetscFunctionReturn(0); 1619d763cef2SBarry Smith } 1620d763cef2SBarry Smith 16214a2ae208SSatish Balay #undef __FUNCT__ 16224a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1623d763cef2SBarry Smith /*@ 1624d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1625d763cef2SBarry Smith as well as the initial time. 1626d763cef2SBarry Smith 16273f9fe445SBarry Smith Logically Collective on TS 1628d763cef2SBarry Smith 1629d763cef2SBarry Smith Input Parameters: 1630d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1631d763cef2SBarry Smith . initial_time - the initial time 1632d763cef2SBarry Smith - time_step - the size of the timestep 1633d763cef2SBarry Smith 1634d763cef2SBarry Smith Level: intermediate 1635d763cef2SBarry Smith 1636d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1637d763cef2SBarry Smith 1638d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1639d763cef2SBarry Smith @*/ 16407087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1641d763cef2SBarry Smith { 1642e144a568SJed Brown PetscErrorCode ierr; 1643e144a568SJed Brown 1644d763cef2SBarry Smith PetscFunctionBegin; 16450700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1646e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1647d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1648d763cef2SBarry Smith PetscFunctionReturn(0); 1649d763cef2SBarry Smith } 1650d763cef2SBarry Smith 16514a2ae208SSatish Balay #undef __FUNCT__ 16524a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1653d763cef2SBarry Smith /*@ 1654d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1655d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1656d763cef2SBarry Smith 16573f9fe445SBarry Smith Logically Collective on TS 1658d763cef2SBarry Smith 1659d763cef2SBarry Smith Input Parameters: 1660d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1661d763cef2SBarry Smith - time_step - the size of the timestep 1662d763cef2SBarry Smith 1663d763cef2SBarry Smith Level: intermediate 1664d763cef2SBarry Smith 1665d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1666d763cef2SBarry Smith 1667d763cef2SBarry Smith .keywords: TS, set, timestep 1668d763cef2SBarry Smith @*/ 16697087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1670d763cef2SBarry Smith { 1671d763cef2SBarry Smith PetscFunctionBegin; 16720700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1673c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1674d763cef2SBarry Smith ts->time_step = time_step; 167531748224SBarry Smith ts->time_step_orig = time_step; 1676d763cef2SBarry Smith PetscFunctionReturn(0); 1677d763cef2SBarry Smith } 1678d763cef2SBarry Smith 16794a2ae208SSatish Balay #undef __FUNCT__ 1680a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1681a43b19c4SJed Brown /*@ 168249354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 168349354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 168449354f04SShri Abhyankar or just return at the final time TS computed. 1685a43b19c4SJed Brown 1686a43b19c4SJed Brown Logically Collective on TS 1687a43b19c4SJed Brown 1688a43b19c4SJed Brown Input Parameter: 1689a43b19c4SJed Brown + ts - the time-step context 169049354f04SShri Abhyankar - eftopt - exact final time option 1691a43b19c4SJed Brown 1692a43b19c4SJed Brown Level: beginner 1693a43b19c4SJed Brown 1694a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 1695a43b19c4SJed Brown @*/ 169649354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 1697a43b19c4SJed Brown { 1698a43b19c4SJed Brown PetscFunctionBegin; 1699a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 170049354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 170149354f04SShri Abhyankar ts->exact_final_time = eftopt; 1702a43b19c4SJed Brown PetscFunctionReturn(0); 1703a43b19c4SJed Brown } 1704a43b19c4SJed Brown 1705a43b19c4SJed Brown #undef __FUNCT__ 17064a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1707d763cef2SBarry Smith /*@ 1708d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1709d763cef2SBarry Smith 1710d763cef2SBarry Smith Not Collective 1711d763cef2SBarry Smith 1712d763cef2SBarry Smith Input Parameter: 1713d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1714d763cef2SBarry Smith 1715d763cef2SBarry Smith Output Parameter: 1716d763cef2SBarry Smith . dt - the current timestep size 1717d763cef2SBarry Smith 1718d763cef2SBarry Smith Level: intermediate 1719d763cef2SBarry Smith 1720d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1721d763cef2SBarry Smith 1722d763cef2SBarry Smith .keywords: TS, get, timestep 1723d763cef2SBarry Smith @*/ 17247087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 1725d763cef2SBarry Smith { 1726d763cef2SBarry Smith PetscFunctionBegin; 17270700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1728f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1729d763cef2SBarry Smith *dt = ts->time_step; 1730d763cef2SBarry Smith PetscFunctionReturn(0); 1731d763cef2SBarry Smith } 1732d763cef2SBarry Smith 17334a2ae208SSatish Balay #undef __FUNCT__ 17344a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1735d8e5e3e6SSatish Balay /*@ 1736d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1737d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1738d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1739d763cef2SBarry Smith the solution at the next timestep has been calculated. 1740d763cef2SBarry Smith 1741d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1742d763cef2SBarry Smith 1743d763cef2SBarry Smith Input Parameter: 1744d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1745d763cef2SBarry Smith 1746d763cef2SBarry Smith Output Parameter: 1747d763cef2SBarry Smith . v - the vector containing the solution 1748d763cef2SBarry Smith 1749d763cef2SBarry Smith Level: intermediate 1750d763cef2SBarry Smith 1751d763cef2SBarry Smith .seealso: TSGetTimeStep() 1752d763cef2SBarry Smith 1753d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1754d763cef2SBarry Smith @*/ 17557087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1756d763cef2SBarry Smith { 1757d763cef2SBarry Smith PetscFunctionBegin; 17580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17594482741eSBarry Smith PetscValidPointer(v,2); 17608737fe31SLisandro Dalcin *v = ts->vec_sol; 1761d763cef2SBarry Smith PetscFunctionReturn(0); 1762d763cef2SBarry Smith } 1763d763cef2SBarry Smith 1764c235aa8dSHong Zhang #undef __FUNCT__ 1765dfb21088SHong Zhang #define __FUNCT__ "TSGetCostGradients" 1766c235aa8dSHong Zhang /*@ 1767dfb21088SHong Zhang TSGetCostGradients - Returns the gradients from the TSAdjointSolve() 1768c235aa8dSHong Zhang 1769c235aa8dSHong Zhang Not Collective, but Vec returned is parallel if TS is parallel 1770c235aa8dSHong Zhang 1771c235aa8dSHong Zhang Input Parameter: 1772c235aa8dSHong Zhang . ts - the TS context obtained from TSCreate() 1773c235aa8dSHong Zhang 1774c235aa8dSHong Zhang Output Parameter: 1775abc2977eSBarry Smith + lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 1776abc2977eSBarry Smith - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 1777c235aa8dSHong Zhang 1778c235aa8dSHong Zhang Level: intermediate 1779c235aa8dSHong Zhang 1780c235aa8dSHong Zhang .seealso: TSGetTimeStep() 1781c235aa8dSHong Zhang 1782c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity 1783c235aa8dSHong Zhang @*/ 1784dfb21088SHong Zhang PetscErrorCode TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu) 1785c235aa8dSHong Zhang { 1786c235aa8dSHong Zhang PetscFunctionBegin; 1787c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1788abc2977eSBarry Smith if (numcost) *numcost = ts->numcost; 1789abc2977eSBarry Smith if (lambda) *lambda = ts->vecs_sensi; 1790abc2977eSBarry Smith if (mu) *mu = ts->vecs_sensip; 1791c235aa8dSHong Zhang PetscFunctionReturn(0); 1792c235aa8dSHong Zhang } 1793c235aa8dSHong Zhang 1794bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 17954a2ae208SSatish Balay #undef __FUNCT__ 1796bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1797d8e5e3e6SSatish Balay /*@ 1798bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1799d763cef2SBarry Smith 1800bdad233fSMatthew Knepley Not collective 1801d763cef2SBarry Smith 1802bdad233fSMatthew Knepley Input Parameters: 1803bdad233fSMatthew Knepley + ts - The TS 1804bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1805d763cef2SBarry Smith .vb 18060910c330SBarry Smith U_t - A U = 0 (linear) 18070910c330SBarry Smith U_t - A(t) U = 0 (linear) 18080910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 1809d763cef2SBarry Smith .ve 1810d763cef2SBarry Smith 1811d763cef2SBarry Smith Level: beginner 1812d763cef2SBarry Smith 1813bdad233fSMatthew Knepley .keywords: TS, problem type 1814bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1815d763cef2SBarry Smith @*/ 18167087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1817a7cc72afSBarry Smith { 18189e2a6581SJed Brown PetscErrorCode ierr; 18199e2a6581SJed Brown 1820d763cef2SBarry Smith PetscFunctionBegin; 18210700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1822bdad233fSMatthew Knepley ts->problem_type = type; 18239e2a6581SJed Brown if (type == TS_LINEAR) { 18249e2a6581SJed Brown SNES snes; 18259e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 18269e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 18279e2a6581SJed Brown } 1828d763cef2SBarry Smith PetscFunctionReturn(0); 1829d763cef2SBarry Smith } 1830d763cef2SBarry Smith 1831bdad233fSMatthew Knepley #undef __FUNCT__ 1832bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1833bdad233fSMatthew Knepley /*@C 1834bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1835bdad233fSMatthew Knepley 1836bdad233fSMatthew Knepley Not collective 1837bdad233fSMatthew Knepley 1838bdad233fSMatthew Knepley Input Parameter: 1839bdad233fSMatthew Knepley . ts - The TS 1840bdad233fSMatthew Knepley 1841bdad233fSMatthew Knepley Output Parameter: 1842bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1843bdad233fSMatthew Knepley .vb 1844089b2837SJed Brown M U_t = A U 1845089b2837SJed Brown M(t) U_t = A(t) U 1846b5abc632SBarry Smith F(t,U,U_t) 1847bdad233fSMatthew Knepley .ve 1848bdad233fSMatthew Knepley 1849bdad233fSMatthew Knepley Level: beginner 1850bdad233fSMatthew Knepley 1851bdad233fSMatthew Knepley .keywords: TS, problem type 1852bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1853bdad233fSMatthew Knepley @*/ 18547087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1855a7cc72afSBarry Smith { 1856bdad233fSMatthew Knepley PetscFunctionBegin; 18570700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 18584482741eSBarry Smith PetscValidIntPointer(type,2); 1859bdad233fSMatthew Knepley *type = ts->problem_type; 1860bdad233fSMatthew Knepley PetscFunctionReturn(0); 1861bdad233fSMatthew Knepley } 1862d763cef2SBarry Smith 18634a2ae208SSatish Balay #undef __FUNCT__ 18644a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1865d763cef2SBarry Smith /*@ 1866d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1867d763cef2SBarry Smith of a timestepper. 1868d763cef2SBarry Smith 1869d763cef2SBarry Smith Collective on TS 1870d763cef2SBarry Smith 1871d763cef2SBarry Smith Input Parameter: 1872d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1873d763cef2SBarry Smith 1874d763cef2SBarry Smith Notes: 1875d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1876d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1877d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1878d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1879d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1880d763cef2SBarry Smith 1881d763cef2SBarry Smith Level: advanced 1882d763cef2SBarry Smith 1883d763cef2SBarry Smith .keywords: TS, timestep, setup 1884d763cef2SBarry Smith 1885d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1886d763cef2SBarry Smith @*/ 18877087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1888d763cef2SBarry Smith { 1889dfbe8321SBarry Smith PetscErrorCode ierr; 18906c6b9e74SPeter Brune DM dm; 18916c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 1892d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 18936c6b9e74SPeter Brune TSIJacobian ijac; 18946c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1895d763cef2SBarry Smith 1896d763cef2SBarry Smith PetscFunctionBegin; 18970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1898277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1899277b19d0SLisandro Dalcin 19002c18e0fdSBarry Smith ts->total_steps = 0; 19017adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 19029596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1903d763cef2SBarry Smith } 1904277b19d0SLisandro Dalcin 1905277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1906277b19d0SLisandro Dalcin 19071c3436cfSJed Brown 1908552698daSJed Brown ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 1909277b19d0SLisandro Dalcin 1910e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 1911e1244c69SJed Brown Mat Amat,Pmat; 1912e1244c69SJed Brown SNES snes; 1913e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1914e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 1915e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 1916e1244c69SJed Brown * have displaced the RHS matrix */ 1917e1244c69SJed Brown if (Amat == ts->Arhs) { 1918e1244c69SJed Brown ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 1919e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 1920e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 1921e1244c69SJed Brown } 1922e1244c69SJed Brown if (Pmat == ts->Brhs) { 1923e1244c69SJed Brown ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 1924e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 1925e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 1926e1244c69SJed Brown } 1927e1244c69SJed Brown } 1928277b19d0SLisandro Dalcin if (ts->ops->setup) { 1929000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1930277b19d0SLisandro Dalcin } 1931277b19d0SLisandro Dalcin 19326c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 19336c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 19346c6b9e74SPeter Brune */ 19356c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 19360298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 19376c6b9e74SPeter Brune if (!func) { 19386c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 19396c6b9e74SPeter Brune } 19406c6b9e74SPeter 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. 19416c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 19426c6b9e74SPeter Brune */ 19430298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 19440298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 19450298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 19466c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 19476c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 19486c6b9e74SPeter Brune } 1949277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1950277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1951277b19d0SLisandro Dalcin } 1952277b19d0SLisandro Dalcin 1953277b19d0SLisandro Dalcin #undef __FUNCT__ 1954f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp" 1955f6a906c0SBarry Smith /*@ 1956f6a906c0SBarry Smith TSAdjointSetUp - Sets up the internal data structures for the later use 1957f6a906c0SBarry Smith of an adjoint solver 1958f6a906c0SBarry Smith 1959f6a906c0SBarry Smith Collective on TS 1960f6a906c0SBarry Smith 1961f6a906c0SBarry Smith Input Parameter: 1962f6a906c0SBarry Smith . ts - the TS context obtained from TSCreate() 1963f6a906c0SBarry Smith 1964f6a906c0SBarry Smith Level: advanced 1965f6a906c0SBarry Smith 1966f6a906c0SBarry Smith .keywords: TS, timestep, setup 1967f6a906c0SBarry Smith 1968947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients() 1969f6a906c0SBarry Smith @*/ 1970f6a906c0SBarry Smith PetscErrorCode TSAdjointSetUp(TS ts) 1971f6a906c0SBarry Smith { 1972f6a906c0SBarry Smith PetscErrorCode ierr; 1973f6a906c0SBarry Smith 1974f6a906c0SBarry Smith PetscFunctionBegin; 1975f6a906c0SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1976f6a906c0SBarry Smith if (ts->adjointsetupcalled) PetscFunctionReturn(0); 1977dfb21088SHong Zhang if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first"); 1978d8151eeaSBarry Smith 1979947abb85SHong Zhang if (ts->vec_costintegral) { /* if there is integral in the cost function*/ 1980d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 1981d8151eeaSBarry Smith if (ts->vecs_sensip){ 1982d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 1983d8151eeaSBarry Smith } 1984947abb85SHong Zhang } 1985d8151eeaSBarry Smith 198642f2b339SBarry Smith if (ts->ops->adjointsetup) { 198742f2b339SBarry Smith ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr); 1988f6a906c0SBarry Smith } 1989f6a906c0SBarry Smith ts->adjointsetupcalled = PETSC_TRUE; 1990f6a906c0SBarry Smith PetscFunctionReturn(0); 1991f6a906c0SBarry Smith } 1992f6a906c0SBarry Smith 1993f6a906c0SBarry Smith #undef __FUNCT__ 1994277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1995277b19d0SLisandro Dalcin /*@ 1996277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1997277b19d0SLisandro Dalcin 1998277b19d0SLisandro Dalcin Collective on TS 1999277b19d0SLisandro Dalcin 2000277b19d0SLisandro Dalcin Input Parameter: 2001277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2002277b19d0SLisandro Dalcin 2003277b19d0SLisandro Dalcin Level: beginner 2004277b19d0SLisandro Dalcin 2005277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 2006277b19d0SLisandro Dalcin 2007277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2008277b19d0SLisandro Dalcin @*/ 2009277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2010277b19d0SLisandro Dalcin { 2011277b19d0SLisandro Dalcin PetscErrorCode ierr; 2012277b19d0SLisandro Dalcin 2013277b19d0SLisandro Dalcin PetscFunctionBegin; 2014277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2015b18ea86cSHong Zhang 2016277b19d0SLisandro Dalcin if (ts->ops->reset) { 2017277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2018277b19d0SLisandro Dalcin } 2019277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2020e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2021bbd56ea5SKarl Rupp 20224e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 20234e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2024214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 20256bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2026e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2027e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 202838637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2029bbd56ea5SKarl Rupp 2030947abb85SHong Zhang if (ts->vec_costintegral) { 2031d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2032d8151eeaSBarry Smith if (ts->vecs_drdp){ 2033d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2034d8151eeaSBarry Smith } 2035947abb85SHong Zhang } 2036d8151eeaSBarry Smith ts->vecs_sensi = NULL; 2037d8151eeaSBarry Smith ts->vecs_sensip = NULL; 2038ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 20392c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 204036eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2041277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2042d763cef2SBarry Smith PetscFunctionReturn(0); 2043d763cef2SBarry Smith } 2044d763cef2SBarry Smith 20454a2ae208SSatish Balay #undef __FUNCT__ 20464a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 2047d8e5e3e6SSatish Balay /*@ 2048d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2049d763cef2SBarry Smith with TSCreate(). 2050d763cef2SBarry Smith 2051d763cef2SBarry Smith Collective on TS 2052d763cef2SBarry Smith 2053d763cef2SBarry Smith Input Parameter: 2054d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2055d763cef2SBarry Smith 2056d763cef2SBarry Smith Level: beginner 2057d763cef2SBarry Smith 2058d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2059d763cef2SBarry Smith 2060d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2061d763cef2SBarry Smith @*/ 20626bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2063d763cef2SBarry Smith { 20646849ba73SBarry Smith PetscErrorCode ierr; 2065d763cef2SBarry Smith 2066d763cef2SBarry Smith PetscFunctionBegin; 20676bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 20686bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 20696bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2070d763cef2SBarry Smith 20716bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2072277b19d0SLisandro Dalcin 2073e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2074e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 20756bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 20766d4c513bSLisandro Dalcin 2077bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2078bc952696SBarry Smith 207984df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 2080aeb4809dSShri Abhyankar if ((*ts)->event) { 2081aeb4809dSShri Abhyankar ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr); 2082aeb4809dSShri Abhyankar } 20836bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 20846bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 20856bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 20860dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 20876d4c513bSLisandro Dalcin 2088a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2089d763cef2SBarry Smith PetscFunctionReturn(0); 2090d763cef2SBarry Smith } 2091d763cef2SBarry Smith 20924a2ae208SSatish Balay #undef __FUNCT__ 20934a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 2094d8e5e3e6SSatish Balay /*@ 2095d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2096d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2097d763cef2SBarry Smith 2098d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2099d763cef2SBarry Smith 2100d763cef2SBarry Smith Input Parameter: 2101d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2102d763cef2SBarry Smith 2103d763cef2SBarry Smith Output Parameter: 2104d763cef2SBarry Smith . snes - the nonlinear solver context 2105d763cef2SBarry Smith 2106d763cef2SBarry Smith Notes: 2107d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2108d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 210994b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2110d763cef2SBarry Smith 2111d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 21120298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2113d763cef2SBarry Smith 2114d763cef2SBarry Smith Level: beginner 2115d763cef2SBarry Smith 2116d763cef2SBarry Smith .keywords: timestep, get, SNES 2117d763cef2SBarry Smith @*/ 21187087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2119d763cef2SBarry Smith { 2120d372ba47SLisandro Dalcin PetscErrorCode ierr; 2121d372ba47SLisandro Dalcin 2122d763cef2SBarry Smith PetscFunctionBegin; 21230700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 21244482741eSBarry Smith PetscValidPointer(snes,2); 2125d372ba47SLisandro Dalcin if (!ts->snes) { 2126ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 21270298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 21283bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2129d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2130496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 21319e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 21329e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 21339e2a6581SJed Brown } 2134d372ba47SLisandro Dalcin } 2135d763cef2SBarry Smith *snes = ts->snes; 2136d763cef2SBarry Smith PetscFunctionReturn(0); 2137d763cef2SBarry Smith } 2138d763cef2SBarry Smith 21394a2ae208SSatish Balay #undef __FUNCT__ 2140deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES" 2141deb2cd25SJed Brown /*@ 2142deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2143deb2cd25SJed Brown 2144deb2cd25SJed Brown Collective 2145deb2cd25SJed Brown 2146deb2cd25SJed Brown Input Parameter: 2147deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2148deb2cd25SJed Brown - snes - the nonlinear solver context 2149deb2cd25SJed Brown 2150deb2cd25SJed Brown Notes: 2151deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2152deb2cd25SJed Brown 2153deb2cd25SJed Brown Level: developer 2154deb2cd25SJed Brown 2155deb2cd25SJed Brown .keywords: timestep, set, SNES 2156deb2cd25SJed Brown @*/ 2157deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2158deb2cd25SJed Brown { 2159deb2cd25SJed Brown PetscErrorCode ierr; 2160d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2161deb2cd25SJed Brown 2162deb2cd25SJed Brown PetscFunctionBegin; 2163deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2164deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2165deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2166deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2167bbd56ea5SKarl Rupp 2168deb2cd25SJed Brown ts->snes = snes; 2169bbd56ea5SKarl Rupp 21700298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 21710298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2172740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 21730298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2174740132f1SEmil Constantinescu } 2175deb2cd25SJed Brown PetscFunctionReturn(0); 2176deb2cd25SJed Brown } 2177deb2cd25SJed Brown 2178deb2cd25SJed Brown #undef __FUNCT__ 217994b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 2180d8e5e3e6SSatish Balay /*@ 218194b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2182d763cef2SBarry Smith a TS (timestepper) context. 2183d763cef2SBarry Smith 218494b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2185d763cef2SBarry Smith 2186d763cef2SBarry Smith Input Parameter: 2187d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2188d763cef2SBarry Smith 2189d763cef2SBarry Smith Output Parameter: 219094b7f48cSBarry Smith . ksp - the nonlinear solver context 2191d763cef2SBarry Smith 2192d763cef2SBarry Smith Notes: 219394b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2194d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2195d763cef2SBarry Smith KSP and PC contexts as well. 2196d763cef2SBarry Smith 219794b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 21980298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2199d763cef2SBarry Smith 2200d763cef2SBarry Smith Level: beginner 2201d763cef2SBarry Smith 220294b7f48cSBarry Smith .keywords: timestep, get, KSP 2203d763cef2SBarry Smith @*/ 22047087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2205d763cef2SBarry Smith { 2206d372ba47SLisandro Dalcin PetscErrorCode ierr; 2207089b2837SJed Brown SNES snes; 2208d372ba47SLisandro Dalcin 2209d763cef2SBarry Smith PetscFunctionBegin; 22100700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22114482741eSBarry Smith PetscValidPointer(ksp,2); 221217186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2213e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2214089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2215089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2216d763cef2SBarry Smith PetscFunctionReturn(0); 2217d763cef2SBarry Smith } 2218d763cef2SBarry Smith 2219d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2220d763cef2SBarry Smith 22214a2ae208SSatish Balay #undef __FUNCT__ 2222adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 2223adb62b0dSMatthew Knepley /*@ 2224adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2225adb62b0dSMatthew Knepley maximum time for iteration. 2226adb62b0dSMatthew Knepley 22273f9fe445SBarry Smith Not Collective 2228adb62b0dSMatthew Knepley 2229adb62b0dSMatthew Knepley Input Parameters: 2230adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 22310298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 22320298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2233adb62b0dSMatthew Knepley 2234adb62b0dSMatthew Knepley Level: intermediate 2235adb62b0dSMatthew Knepley 2236adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2237adb62b0dSMatthew Knepley @*/ 22387087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2239adb62b0dSMatthew Knepley { 2240adb62b0dSMatthew Knepley PetscFunctionBegin; 22410700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2242abc0a331SBarry Smith if (maxsteps) { 22434482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2244adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2245adb62b0dSMatthew Knepley } 2246abc0a331SBarry Smith if (maxtime) { 22474482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2248adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2249adb62b0dSMatthew Knepley } 2250adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2251adb62b0dSMatthew Knepley } 2252adb62b0dSMatthew Knepley 2253adb62b0dSMatthew Knepley #undef __FUNCT__ 22544a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 2255d763cef2SBarry Smith /*@ 2256d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2257d763cef2SBarry Smith maximum time for iteration. 2258d763cef2SBarry Smith 22593f9fe445SBarry Smith Logically Collective on TS 2260d763cef2SBarry Smith 2261d763cef2SBarry Smith Input Parameters: 2262d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2263d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2264d763cef2SBarry Smith - maxtime - final time to iterate to 2265d763cef2SBarry Smith 2266d763cef2SBarry Smith Options Database Keys: 2267d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 22683bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2269d763cef2SBarry Smith 2270d763cef2SBarry Smith Notes: 2271d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2272d763cef2SBarry Smith 2273d763cef2SBarry Smith Level: intermediate 2274d763cef2SBarry Smith 2275d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2276a43b19c4SJed Brown 2277a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2278d763cef2SBarry Smith @*/ 22797087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2280d763cef2SBarry Smith { 2281d763cef2SBarry Smith PetscFunctionBegin; 22820700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2283c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2284c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 228539b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 228639b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2287d763cef2SBarry Smith PetscFunctionReturn(0); 2288d763cef2SBarry Smith } 2289d763cef2SBarry Smith 22904a2ae208SSatish Balay #undef __FUNCT__ 22914a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 2292d763cef2SBarry Smith /*@ 2293d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2294d763cef2SBarry Smith for use by the TS routines. 2295d763cef2SBarry Smith 22963f9fe445SBarry Smith Logically Collective on TS and Vec 2297d763cef2SBarry Smith 2298d763cef2SBarry Smith Input Parameters: 2299d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 23000910c330SBarry Smith - u - the solution vector 2301d763cef2SBarry Smith 2302d763cef2SBarry Smith Level: beginner 2303d763cef2SBarry Smith 2304d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2305d763cef2SBarry Smith @*/ 23060910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2307d763cef2SBarry Smith { 23088737fe31SLisandro Dalcin PetscErrorCode ierr; 2309496e6a7aSJed Brown DM dm; 23108737fe31SLisandro Dalcin 2311d763cef2SBarry Smith PetscFunctionBegin; 23120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 23130910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 23140910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 23156bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2316bbd56ea5SKarl Rupp 23170910c330SBarry Smith ts->vec_sol = u; 2318bbd56ea5SKarl Rupp 2319496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 23200910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2321d763cef2SBarry Smith PetscFunctionReturn(0); 2322d763cef2SBarry Smith } 2323d763cef2SBarry Smith 2324e74ef692SMatthew Knepley #undef __FUNCT__ 232573b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps" 232673b18844SBarry Smith /*@ 232773b18844SBarry Smith TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time 232873b18844SBarry Smith 232973b18844SBarry Smith Logically Collective on TS 233073b18844SBarry Smith 233173b18844SBarry Smith Input Parameters: 233273b18844SBarry Smith + ts - the TS context obtained from TSCreate() 233373b18844SBarry Smith . steps - number of steps to use 233473b18844SBarry Smith 233573b18844SBarry Smith Level: intermediate 233673b18844SBarry Smith 233773b18844SBarry Smith Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this 233873b18844SBarry Smith so as to integrate back to less than the original timestep 233973b18844SBarry Smith 234073b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations 234173b18844SBarry Smith 234273b18844SBarry Smith .seealso: TSSetExactFinalTime() 234373b18844SBarry Smith @*/ 234473b18844SBarry Smith PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps) 234573b18844SBarry Smith { 234673b18844SBarry Smith PetscFunctionBegin; 234773b18844SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 234873b18844SBarry Smith PetscValidLogicalCollectiveInt(ts,steps,2); 234973b18844SBarry Smith if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps"); 235073b18844SBarry Smith if (steps > ts->total_steps) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back more than the total number of forward steps"); 235173b18844SBarry Smith ts->adjoint_max_steps = steps; 235273b18844SBarry Smith PetscFunctionReturn(0); 235373b18844SBarry Smith } 235473b18844SBarry Smith 235573b18844SBarry Smith #undef __FUNCT__ 2356dfb21088SHong Zhang #define __FUNCT__ "TSSetCostGradients" 2357c235aa8dSHong Zhang /*@ 2358dfb21088SHong Zhang TSSetCostGradients - Sets the initial value of the gradients of the cost function w.r.t. initial conditions and w.r.t. the problem parameters 23590cf82b59SBarry Smith for use by the TSAdjoint routines. 2360c235aa8dSHong Zhang 2361c235aa8dSHong Zhang Logically Collective on TS and Vec 2362c235aa8dSHong Zhang 2363c235aa8dSHong Zhang Input Parameters: 2364c235aa8dSHong Zhang + ts - the TS context obtained from TSCreate() 2365abc2977eSBarry Smith . lambda - gradients with respect to the initial condition variables, the dimension and parallel layout of these vectors is the same as the ODE solution vector 2366abc2977eSBarry Smith - mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters 2367c235aa8dSHong Zhang 2368c235aa8dSHong Zhang Level: beginner 2369c235aa8dSHong Zhang 2370abc2977eSBarry Smith Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime mu_i = df/dp|finaltime 23710cf82b59SBarry Smith 2372c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions 2373c235aa8dSHong Zhang @*/ 2374dfb21088SHong Zhang PetscErrorCode TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu) 2375c235aa8dSHong Zhang { 2376c235aa8dSHong Zhang PetscFunctionBegin; 2377c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2378abc2977eSBarry Smith PetscValidPointer(lambda,2); 2379abc2977eSBarry Smith ts->vecs_sensi = lambda; 2380abc2977eSBarry Smith ts->vecs_sensip = mu; 2381dfb21088SHong Zhang if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostIntegrand"); 2382abc2977eSBarry Smith ts->numcost = numcost; 2383ad8e2604SHong Zhang PetscFunctionReturn(0); 2384ad8e2604SHong Zhang } 2385ad8e2604SHong Zhang 2386ad8e2604SHong Zhang #undef __FUNCT__ 23875bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian" 2388ad8e2604SHong Zhang /*@C 23890cf82b59SBarry Smith TSAdjointSetRHSJacobian - Sets the function that computes the Jacobian of G w.r.t. the parameters p where y_t = G(y,p,t), as well as the location to store the matrix. 2390ad8e2604SHong Zhang 2391ad8e2604SHong Zhang Logically Collective on TS 2392ad8e2604SHong Zhang 2393ad8e2604SHong Zhang Input Parameters: 2394ad8e2604SHong Zhang + ts - The TS context obtained from TSCreate() 2395ad8e2604SHong Zhang - func - The function 2396ad8e2604SHong Zhang 2397ad8e2604SHong Zhang Calling sequence of func: 23980cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx); 239905755b9cSHong Zhang + t - current timestep 24000cf82b59SBarry Smith . y - input vector (current ODE solution) 240105755b9cSHong Zhang . A - output matrix 240205755b9cSHong Zhang - ctx - [optional] user-defined function context 2403ad8e2604SHong Zhang 2404ad8e2604SHong Zhang Level: intermediate 2405ad8e2604SHong Zhang 24060cf82b59SBarry Smith Notes: Amat has the same number of rows and the same row parallel layout as u, Amat has the same number of columns and parallel layout as p 24070cf82b59SBarry Smith 2408ad8e2604SHong Zhang .keywords: TS, sensitivity 2409ad8e2604SHong Zhang .seealso: 2410ad8e2604SHong Zhang @*/ 24115bf8c567SBarry Smith PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 2412ad8e2604SHong Zhang { 2413ad8e2604SHong Zhang PetscErrorCode ierr; 2414ad8e2604SHong Zhang 2415ad8e2604SHong Zhang PetscFunctionBegin; 2416ad8e2604SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2417ad8e2604SHong Zhang if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 2418ad8e2604SHong Zhang 2419ad8e2604SHong Zhang ts->rhsjacobianp = func; 2420ad8e2604SHong Zhang ts->rhsjacobianpctx = ctx; 2421ad8e2604SHong Zhang if(Amat) { 2422ad8e2604SHong Zhang ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 2423ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2424ad8e2604SHong Zhang ts->Jacp = Amat; 2425ad8e2604SHong Zhang } 2426ad8e2604SHong Zhang PetscFunctionReturn(0); 2427ad8e2604SHong Zhang } 2428ad8e2604SHong Zhang 2429ad8e2604SHong Zhang #undef __FUNCT__ 24305bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian" 24310cf82b59SBarry Smith /*@C 24325bf8c567SBarry Smith TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function. 2433ad8e2604SHong Zhang 2434ad8e2604SHong Zhang Collective on TS 2435ad8e2604SHong Zhang 2436ad8e2604SHong Zhang Input Parameters: 2437ad8e2604SHong Zhang . ts - The TS context obtained from TSCreate() 2438ad8e2604SHong Zhang 2439ad8e2604SHong Zhang Level: developer 2440ad8e2604SHong Zhang 2441ad8e2604SHong Zhang .keywords: TS, sensitivity 24425bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian() 2443ad8e2604SHong Zhang @*/ 24445bf8c567SBarry Smith PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat) 2445ad8e2604SHong Zhang { 2446ad8e2604SHong Zhang PetscErrorCode ierr; 2447ad8e2604SHong Zhang 2448ad8e2604SHong Zhang PetscFunctionBegin; 2449ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2450ad8e2604SHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 2451ad8e2604SHong Zhang PetscValidPointer(Amat,4); 2452ad8e2604SHong Zhang 2453ad8e2604SHong Zhang PetscStackPush("TS user JacobianP function for sensitivity analysis"); 2454ad8e2604SHong Zhang ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 2455ad8e2604SHong Zhang PetscStackPop; 2456c235aa8dSHong Zhang PetscFunctionReturn(0); 2457c235aa8dSHong Zhang } 2458c235aa8dSHong Zhang 2459c235aa8dSHong Zhang #undef __FUNCT__ 2460dfb21088SHong Zhang #define __FUNCT__ "TSSetCostIntegrand" 24616fd0887fSHong Zhang /*@C 2462dfb21088SHong Zhang TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions 24636fd0887fSHong Zhang 24646fd0887fSHong Zhang Logically Collective on TS 24656fd0887fSHong Zhang 24666fd0887fSHong Zhang Input Parameters: 24676fd0887fSHong Zhang + ts - the TS context obtained from TSCreate() 2468abc2977eSBarry Smith . numcost - number of gradients to be computed, this is the number of cost functions 24690cf82b59SBarry Smith . rf - routine for evaluating the integrand function 2470b612ec54SBarry Smith . drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y 2471b612ec54SBarry Smith . drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p 2472b612ec54SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 24736fd0887fSHong Zhang 24740cf82b59SBarry Smith Calling sequence of rf: 24750cf82b59SBarry Smith $ rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx); 24766fd0887fSHong Zhang 24776fd0887fSHong Zhang + t - current timestep 24780cf82b59SBarry Smith . y - input vector 24790cf82b59SBarry Smith . f - function result; one vector entry for each cost function 24806fd0887fSHong Zhang - ctx - [optional] user-defined function context 24816fd0887fSHong Zhang 2482b612ec54SBarry Smith Calling sequence of drdyf: 24830cf82b59SBarry Smith $ PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx); 2484b612ec54SBarry Smith 2485b612ec54SBarry Smith Calling sequence of drdpf: 24860cf82b59SBarry Smith $ PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx); 2487b612ec54SBarry Smith 2488b612ec54SBarry Smith Level: intermediate 24896fd0887fSHong Zhang 2490abc2977eSBarry Smith Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions 24910cf82b59SBarry Smith 24926fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 24936fd0887fSHong Zhang 2494dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients() 24956fd0887fSHong Zhang @*/ 2496dfb21088SHong Zhang PetscErrorCode TSSetCostIntegrand(TS ts,PetscInt numcost, PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*), 2497d8151eeaSBarry Smith PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*), 2498d8151eeaSBarry Smith PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),void *ctx) 24996fd0887fSHong Zhang { 25006fd0887fSHong Zhang PetscErrorCode ierr; 25016fd0887fSHong Zhang 25026fd0887fSHong Zhang PetscFunctionBegin; 25036fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2504dfb21088SHong Zhang if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostGradients()"); 2505c72ed514SHong Zhang if (!ts->numcost) ts->numcost=numcost; 25066fd0887fSHong Zhang 2507abc2977eSBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr); 25082c39e106SBarry Smith ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr); 25090cf82b59SBarry Smith ts->costintegrand = rf; 251005755b9cSHong Zhang ts->costintegrandctx = ctx; 2511b612ec54SBarry Smith ts->drdyfunction = drdyf; 2512b612ec54SBarry Smith ts->drdpfunction = drdpf; 25136fd0887fSHong Zhang PetscFunctionReturn(0); 25146fd0887fSHong Zhang } 25156fd0887fSHong Zhang 25166fd0887fSHong Zhang #undef __FUNCT__ 2517dfb21088SHong Zhang #define __FUNCT__ "TSGetCostIntegral" 251836eaed60SHong Zhang /*@ 2519dfb21088SHong Zhang TSGetCostIntegral - Returns the values of the integral term in the cost functions. 252036eaed60SHong Zhang It is valid to call the routine after a backward run. 252136eaed60SHong Zhang 252236eaed60SHong Zhang Not Collective 252336eaed60SHong Zhang 252436eaed60SHong Zhang Input Parameter: 252536eaed60SHong Zhang . ts - the TS context obtained from TSCreate() 252636eaed60SHong Zhang 252736eaed60SHong Zhang Output Parameter: 25282c39e106SBarry Smith . v - the vector containing the integrals for each cost function 252936eaed60SHong Zhang 253036eaed60SHong Zhang Level: intermediate 253136eaed60SHong Zhang 2532dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 253336eaed60SHong Zhang 253436eaed60SHong Zhang .keywords: TS, sensitivity analysis 253536eaed60SHong Zhang @*/ 2536dfb21088SHong Zhang PetscErrorCode TSGetCostIntegral(TS ts,Vec *v) 253736eaed60SHong Zhang { 253836eaed60SHong Zhang PetscFunctionBegin; 253936eaed60SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 254036eaed60SHong Zhang PetscValidPointer(v,2); 25412c39e106SBarry Smith *v = ts->vec_costintegral; 254236eaed60SHong Zhang PetscFunctionReturn(0); 254336eaed60SHong Zhang } 254436eaed60SHong Zhang 254536eaed60SHong Zhang #undef __FUNCT__ 2546d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand" 25476fd0887fSHong Zhang /*@ 25482c39e106SBarry Smith TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions. 25496fd0887fSHong Zhang 25506fd0887fSHong Zhang Input Parameters: 25516fd0887fSHong Zhang + ts - the TS context 25526fd0887fSHong Zhang . t - current time 25530cf82b59SBarry Smith - y - state vector, i.e. current solution 25546fd0887fSHong Zhang 25556fd0887fSHong Zhang Output Parameter: 2556abc2977eSBarry Smith . q - vector of size numcost to hold the outputs 25576fd0887fSHong Zhang 25586fd0887fSHong Zhang Note: 25596fd0887fSHong Zhang Most users should not need to explicitly call this routine, as it 25606fd0887fSHong Zhang is used internally within the sensitivity analysis context. 25616fd0887fSHong Zhang 25626fd0887fSHong Zhang Level: developer 25636fd0887fSHong Zhang 25646fd0887fSHong Zhang .keywords: TS, compute 25656fd0887fSHong Zhang 2566dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 25676fd0887fSHong Zhang @*/ 25680cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q) 25696fd0887fSHong Zhang { 25706fd0887fSHong Zhang PetscErrorCode ierr; 25716fd0887fSHong Zhang 25726fd0887fSHong Zhang PetscFunctionBegin; 25736fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25740cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 25756fd0887fSHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,4); 25766fd0887fSHong Zhang 25770cf82b59SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 2578e4680132SHong Zhang if (ts->costintegrand) { 2579e4680132SHong Zhang PetscStackPush("TS user integrand in the cost function"); 25800cf82b59SBarry Smith ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr); 25816fd0887fSHong Zhang PetscStackPop; 25826fd0887fSHong Zhang } else { 25836fd0887fSHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 25846fd0887fSHong Zhang } 25856fd0887fSHong Zhang 25860cf82b59SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 25876fd0887fSHong Zhang PetscFunctionReturn(0); 25886fd0887fSHong Zhang } 25896fd0887fSHong Zhang 25906fd0887fSHong Zhang #undef __FUNCT__ 2591d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction" 259205755b9cSHong Zhang /*@ 2593d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 259405755b9cSHong Zhang 259505755b9cSHong Zhang Collective on TS 259605755b9cSHong Zhang 259705755b9cSHong Zhang Input Parameters: 259805755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 259905755b9cSHong Zhang 260005755b9cSHong Zhang Notes: 2601d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 260205755b9cSHong Zhang so most users would not generally call this routine themselves. 260305755b9cSHong Zhang 260405755b9cSHong Zhang Level: developer 260505755b9cSHong Zhang 260605755b9cSHong Zhang .keywords: TS, sensitivity 2607d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction() 260805755b9cSHong Zhang @*/ 26090cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy) 261005755b9cSHong Zhang { 261105755b9cSHong Zhang PetscErrorCode ierr; 261205755b9cSHong Zhang 261305755b9cSHong Zhang PetscFunctionBegin; 261405755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26150cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 261605755b9cSHong Zhang 261705755b9cSHong Zhang PetscStackPush("TS user DRDY function for sensitivity analysis"); 26180cf82b59SBarry Smith ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr); 261905755b9cSHong Zhang PetscStackPop; 262005755b9cSHong Zhang PetscFunctionReturn(0); 262105755b9cSHong Zhang } 262205755b9cSHong Zhang 262305755b9cSHong Zhang #undef __FUNCT__ 2624d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction" 262505755b9cSHong Zhang /*@ 2626d4aa0a58SBarry Smith TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 262705755b9cSHong Zhang 262805755b9cSHong Zhang Collective on TS 262905755b9cSHong Zhang 263005755b9cSHong Zhang Input Parameters: 263105755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 263205755b9cSHong Zhang 263305755b9cSHong Zhang Notes: 263405755b9cSHong Zhang TSDRDPFunction() is typically used for sensitivity implementation, 263505755b9cSHong Zhang so most users would not generally call this routine themselves. 263605755b9cSHong Zhang 263705755b9cSHong Zhang Level: developer 263805755b9cSHong Zhang 263905755b9cSHong Zhang .keywords: TS, sensitivity 2640d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction() 264105755b9cSHong Zhang @*/ 26420cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp) 264305755b9cSHong Zhang { 264405755b9cSHong Zhang PetscErrorCode ierr; 264505755b9cSHong Zhang 264605755b9cSHong Zhang PetscFunctionBegin; 264705755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26480cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 264905755b9cSHong Zhang 265005755b9cSHong Zhang PetscStackPush("TS user DRDP function for sensitivity analysis"); 26510cf82b59SBarry Smith ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr); 265205755b9cSHong Zhang PetscStackPop; 265305755b9cSHong Zhang PetscFunctionReturn(0); 265405755b9cSHong Zhang } 265505755b9cSHong Zhang 265605755b9cSHong Zhang #undef __FUNCT__ 2657e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 2658ac226902SBarry Smith /*@C 2659000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 26603f2090d5SJed Brown called once at the beginning of each time step. 2661000e7ae3SMatthew Knepley 26623f9fe445SBarry Smith Logically Collective on TS 2663000e7ae3SMatthew Knepley 2664000e7ae3SMatthew Knepley Input Parameters: 2665000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2666000e7ae3SMatthew Knepley - func - The function 2667000e7ae3SMatthew Knepley 2668000e7ae3SMatthew Knepley Calling sequence of func: 2669000e7ae3SMatthew Knepley . func (TS ts); 2670000e7ae3SMatthew Knepley 2671000e7ae3SMatthew Knepley Level: intermediate 2672000e7ae3SMatthew Knepley 2673b8123daeSJed Brown Note: 2674b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 2675b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 2676b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 2677b8123daeSJed Brown 2678000e7ae3SMatthew Knepley .keywords: TS, timestep 26799be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 2680000e7ae3SMatthew Knepley @*/ 26817087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 2682000e7ae3SMatthew Knepley { 2683000e7ae3SMatthew Knepley PetscFunctionBegin; 26840700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2685ae60f76fSBarry Smith ts->prestep = func; 2686000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2687000e7ae3SMatthew Knepley } 2688000e7ae3SMatthew Knepley 2689e74ef692SMatthew Knepley #undef __FUNCT__ 26903f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 269109ee8438SJed Brown /*@ 26923f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 26933f2090d5SJed Brown 26943f2090d5SJed Brown Collective on TS 26953f2090d5SJed Brown 26963f2090d5SJed Brown Input Parameters: 26973f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 26983f2090d5SJed Brown 26993f2090d5SJed Brown Notes: 27003f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 27013f2090d5SJed Brown so most users would not generally call this routine themselves. 27023f2090d5SJed Brown 27033f2090d5SJed Brown Level: developer 27043f2090d5SJed Brown 27053f2090d5SJed Brown .keywords: TS, timestep 27069be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 27073f2090d5SJed Brown @*/ 27087087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 27093f2090d5SJed Brown { 27103f2090d5SJed Brown PetscErrorCode ierr; 27113f2090d5SJed Brown 27123f2090d5SJed Brown PetscFunctionBegin; 27130700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2714ae60f76fSBarry Smith if (ts->prestep) { 2715ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 2716312ce896SJed Brown } 27173f2090d5SJed Brown PetscFunctionReturn(0); 27183f2090d5SJed Brown } 27193f2090d5SJed Brown 27203f2090d5SJed Brown #undef __FUNCT__ 2721b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 2722b8123daeSJed Brown /*@C 2723b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 2724b8123daeSJed Brown called once at the beginning of each stage. 2725b8123daeSJed Brown 2726b8123daeSJed Brown Logically Collective on TS 2727b8123daeSJed Brown 2728b8123daeSJed Brown Input Parameters: 2729b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 2730b8123daeSJed Brown - func - The function 2731b8123daeSJed Brown 2732b8123daeSJed Brown Calling sequence of func: 2733b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 2734b8123daeSJed Brown 2735b8123daeSJed Brown Level: intermediate 2736b8123daeSJed Brown 2737b8123daeSJed Brown Note: 2738b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 2739b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 2740b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 2741b8123daeSJed Brown 2742b8123daeSJed Brown .keywords: TS, timestep 27439be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 2744b8123daeSJed Brown @*/ 2745b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 2746b8123daeSJed Brown { 2747b8123daeSJed Brown PetscFunctionBegin; 2748b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2749ae60f76fSBarry Smith ts->prestage = func; 2750b8123daeSJed Brown PetscFunctionReturn(0); 2751b8123daeSJed Brown } 2752b8123daeSJed Brown 2753b8123daeSJed Brown #undef __FUNCT__ 27549be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage" 27559be3e283SDebojyoti Ghosh /*@C 27569be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 27579be3e283SDebojyoti Ghosh called once at the end of each stage. 27589be3e283SDebojyoti Ghosh 27599be3e283SDebojyoti Ghosh Logically Collective on TS 27609be3e283SDebojyoti Ghosh 27619be3e283SDebojyoti Ghosh Input Parameters: 27629be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 27639be3e283SDebojyoti Ghosh - func - The function 27649be3e283SDebojyoti Ghosh 27659be3e283SDebojyoti Ghosh Calling sequence of func: 27669be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 27679be3e283SDebojyoti Ghosh 27689be3e283SDebojyoti Ghosh Level: intermediate 27699be3e283SDebojyoti Ghosh 27709be3e283SDebojyoti Ghosh Note: 27719be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 27729be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 27739be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 27749be3e283SDebojyoti Ghosh 27759be3e283SDebojyoti Ghosh .keywords: TS, timestep 27769be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 27779be3e283SDebojyoti Ghosh @*/ 27789be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 27799be3e283SDebojyoti Ghosh { 27809be3e283SDebojyoti Ghosh PetscFunctionBegin; 27819be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 27829be3e283SDebojyoti Ghosh ts->poststage = func; 27839be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 27849be3e283SDebojyoti Ghosh } 27859be3e283SDebojyoti Ghosh 27869be3e283SDebojyoti Ghosh #undef __FUNCT__ 2787b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 2788b8123daeSJed Brown /*@ 2789b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 2790b8123daeSJed Brown 2791b8123daeSJed Brown Collective on TS 2792b8123daeSJed Brown 2793b8123daeSJed Brown Input Parameters: 2794b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 27959be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 2796b8123daeSJed Brown 2797b8123daeSJed Brown Notes: 2798b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 2799b8123daeSJed Brown most users would not generally call this routine themselves. 2800b8123daeSJed Brown 2801b8123daeSJed Brown Level: developer 2802b8123daeSJed Brown 2803b8123daeSJed Brown .keywords: TS, timestep 28049be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 2805b8123daeSJed Brown @*/ 2806b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 2807b8123daeSJed Brown { 2808b8123daeSJed Brown PetscErrorCode ierr; 2809b8123daeSJed Brown 2810b8123daeSJed Brown PetscFunctionBegin; 2811b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2812ae60f76fSBarry Smith if (ts->prestage) { 2813ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 2814b8123daeSJed Brown } 2815b8123daeSJed Brown PetscFunctionReturn(0); 2816b8123daeSJed Brown } 2817b8123daeSJed Brown 2818b8123daeSJed Brown #undef __FUNCT__ 28199be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage" 28209be3e283SDebojyoti Ghosh /*@ 28219be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 28229be3e283SDebojyoti Ghosh 28239be3e283SDebojyoti Ghosh Collective on TS 28249be3e283SDebojyoti Ghosh 28259be3e283SDebojyoti Ghosh Input Parameters: 28269be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 28279be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 28289be3e283SDebojyoti Ghosh stageindex - Stage number 28299be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 28309be3e283SDebojyoti Ghosh of stages) with the stage solutions 28319be3e283SDebojyoti Ghosh 28329be3e283SDebojyoti Ghosh Notes: 28339be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 28349be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 28359be3e283SDebojyoti Ghosh 28369be3e283SDebojyoti Ghosh Level: developer 28379be3e283SDebojyoti Ghosh 28389be3e283SDebojyoti Ghosh .keywords: TS, timestep 28399be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 28409be3e283SDebojyoti Ghosh @*/ 28419be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 28429be3e283SDebojyoti Ghosh { 28439be3e283SDebojyoti Ghosh PetscErrorCode ierr; 28449be3e283SDebojyoti Ghosh 28459be3e283SDebojyoti Ghosh PetscFunctionBegin; 28469be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28474beae5d8SLisandro Dalcin if (ts->poststage) { 28489be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 28499be3e283SDebojyoti Ghosh } 28509be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 28519be3e283SDebojyoti Ghosh } 28529be3e283SDebojyoti Ghosh 28539be3e283SDebojyoti Ghosh #undef __FUNCT__ 2854e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 2855ac226902SBarry Smith /*@C 2856000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 28573f2090d5SJed Brown called once at the end of each time step. 2858000e7ae3SMatthew Knepley 28593f9fe445SBarry Smith Logically Collective on TS 2860000e7ae3SMatthew Knepley 2861000e7ae3SMatthew Knepley Input Parameters: 2862000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2863000e7ae3SMatthew Knepley - func - The function 2864000e7ae3SMatthew Knepley 2865000e7ae3SMatthew Knepley Calling sequence of func: 2866b8123daeSJed Brown $ func (TS ts); 2867000e7ae3SMatthew Knepley 2868000e7ae3SMatthew Knepley Level: intermediate 2869000e7ae3SMatthew Knepley 2870000e7ae3SMatthew Knepley .keywords: TS, timestep 2871b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 2872000e7ae3SMatthew Knepley @*/ 28737087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 2874000e7ae3SMatthew Knepley { 2875000e7ae3SMatthew Knepley PetscFunctionBegin; 28760700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2877ae60f76fSBarry Smith ts->poststep = func; 2878000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2879000e7ae3SMatthew Knepley } 2880000e7ae3SMatthew Knepley 2881e74ef692SMatthew Knepley #undef __FUNCT__ 28823f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 288309ee8438SJed Brown /*@ 28843f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 28853f2090d5SJed Brown 28863f2090d5SJed Brown Collective on TS 28873f2090d5SJed Brown 28883f2090d5SJed Brown Input Parameters: 28893f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 28903f2090d5SJed Brown 28913f2090d5SJed Brown Notes: 28923f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 28933f2090d5SJed Brown so most users would not generally call this routine themselves. 28943f2090d5SJed Brown 28953f2090d5SJed Brown Level: developer 28963f2090d5SJed Brown 28973f2090d5SJed Brown .keywords: TS, timestep 28983f2090d5SJed Brown @*/ 28997087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 29003f2090d5SJed Brown { 29013f2090d5SJed Brown PetscErrorCode ierr; 29023f2090d5SJed Brown 29033f2090d5SJed Brown PetscFunctionBegin; 29040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2905ae60f76fSBarry Smith if (ts->poststep) { 2906ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 290772ac3e02SJed Brown } 29083f2090d5SJed Brown PetscFunctionReturn(0); 29093f2090d5SJed Brown } 29103f2090d5SJed Brown 2911d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 2912d763cef2SBarry Smith 29134a2ae208SSatish Balay #undef __FUNCT__ 2914a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 2915d763cef2SBarry Smith /*@C 2916a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 2917d763cef2SBarry Smith timestep to display the iteration's progress. 2918d763cef2SBarry Smith 29193f9fe445SBarry Smith Logically Collective on TS 2920d763cef2SBarry Smith 2921d763cef2SBarry Smith Input Parameters: 2922d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2923e213d8f1SJed Brown . monitor - monitoring routine 2924329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 29250298fd71SBarry Smith monitor routine (use NULL if no context is desired) 2926b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 29270298fd71SBarry Smith (may be NULL) 2928d763cef2SBarry Smith 2929e213d8f1SJed Brown Calling sequence of monitor: 29300910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 2931d763cef2SBarry Smith 2932d763cef2SBarry Smith + ts - the TS context 293388c05cc5SBarry 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 293488c05cc5SBarry Smith been interpolated to) 29351f06c33eSBarry Smith . time - current time 29360910c330SBarry Smith . u - current iterate 2937d763cef2SBarry Smith - mctx - [optional] monitoring context 2938d763cef2SBarry Smith 2939d763cef2SBarry Smith Notes: 2940d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 2941d763cef2SBarry Smith already has been loaded. 2942d763cef2SBarry Smith 2943025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 2944025f1a04SBarry Smith 2945d763cef2SBarry Smith Level: intermediate 2946d763cef2SBarry Smith 2947d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2948d763cef2SBarry Smith 2949a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 2950d763cef2SBarry Smith @*/ 2951c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 2952d763cef2SBarry Smith { 2953d763cef2SBarry Smith PetscFunctionBegin; 29540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 295517186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2956d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 29578704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 2958d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 2959d763cef2SBarry Smith PetscFunctionReturn(0); 2960d763cef2SBarry Smith } 2961d763cef2SBarry Smith 29624a2ae208SSatish Balay #undef __FUNCT__ 2963a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 2964d763cef2SBarry Smith /*@C 2965a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 2966d763cef2SBarry Smith 29673f9fe445SBarry Smith Logically Collective on TS 2968d763cef2SBarry Smith 2969d763cef2SBarry Smith Input Parameters: 2970d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2971d763cef2SBarry Smith 2972d763cef2SBarry Smith Notes: 2973d763cef2SBarry Smith There is no way to remove a single, specific monitor. 2974d763cef2SBarry Smith 2975d763cef2SBarry Smith Level: intermediate 2976d763cef2SBarry Smith 2977d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2978d763cef2SBarry Smith 2979a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 2980d763cef2SBarry Smith @*/ 29817087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 2982d763cef2SBarry Smith { 2983d952e501SBarry Smith PetscErrorCode ierr; 2984d952e501SBarry Smith PetscInt i; 2985d952e501SBarry Smith 2986d763cef2SBarry Smith PetscFunctionBegin; 29870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2988d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 29898704b422SBarry Smith if (ts->monitordestroy[i]) { 29908704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 2991d952e501SBarry Smith } 2992d952e501SBarry Smith } 2993d763cef2SBarry Smith ts->numbermonitors = 0; 2994d763cef2SBarry Smith PetscFunctionReturn(0); 2995d763cef2SBarry Smith } 2996d763cef2SBarry Smith 29974a2ae208SSatish Balay #undef __FUNCT__ 2998a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 2999d8e5e3e6SSatish Balay /*@ 3000a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 30015516499fSSatish Balay 30025516499fSSatish Balay Level: intermediate 300341251cbbSSatish Balay 30045516499fSSatish Balay .keywords: TS, set, monitor 30055516499fSSatish Balay 300641251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 300741251cbbSSatish Balay @*/ 3008649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 3009d763cef2SBarry Smith { 3010dfbe8321SBarry Smith PetscErrorCode ierr; 30114d4332d5SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 3012d132466eSBarry Smith 3013d763cef2SBarry Smith PetscFunctionBegin; 30144d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3015649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 30168392e04aSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr); 3017649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3018d763cef2SBarry Smith PetscFunctionReturn(0); 3019d763cef2SBarry Smith } 3020d763cef2SBarry Smith 30214a2ae208SSatish Balay #undef __FUNCT__ 30229110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorSet" 30239110b2e7SHong Zhang /*@C 30249110b2e7SHong Zhang TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every 30259110b2e7SHong Zhang timestep to display the iteration's progress. 30269110b2e7SHong Zhang 30279110b2e7SHong Zhang Logically Collective on TS 30289110b2e7SHong Zhang 30299110b2e7SHong Zhang Input Parameters: 30309110b2e7SHong Zhang + ts - the TS context obtained from TSCreate() 30319110b2e7SHong Zhang . adjointmonitor - monitoring routine 30329110b2e7SHong Zhang . adjointmctx - [optional] user-defined context for private data for the 30339110b2e7SHong Zhang monitor routine (use NULL if no context is desired) 30349110b2e7SHong Zhang - adjointmonitordestroy - [optional] routine that frees monitor context 30359110b2e7SHong Zhang (may be NULL) 30369110b2e7SHong Zhang 30379110b2e7SHong Zhang Calling sequence of monitor: 30389110b2e7SHong Zhang $ int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx) 30399110b2e7SHong Zhang 30409110b2e7SHong Zhang + ts - the TS context 30419110b2e7SHong Zhang . 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 30429110b2e7SHong Zhang been interpolated to) 30439110b2e7SHong Zhang . time - current time 30449110b2e7SHong Zhang . u - current iterate 30459110b2e7SHong Zhang . numcost - number of cost functionos 30469110b2e7SHong Zhang . lambda - sensitivities to initial conditions 30479110b2e7SHong Zhang . mu - sensitivities to parameters 30489110b2e7SHong Zhang - adjointmctx - [optional] adjoint monitoring context 30499110b2e7SHong Zhang 30509110b2e7SHong Zhang Notes: 30519110b2e7SHong Zhang This routine adds an additional monitor to the list of monitors that 30529110b2e7SHong Zhang already has been loaded. 30539110b2e7SHong Zhang 30549110b2e7SHong Zhang Fortran notes: Only a single monitor function can be set for each TS object 30559110b2e7SHong Zhang 30569110b2e7SHong Zhang Level: intermediate 30579110b2e7SHong Zhang 30589110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 30599110b2e7SHong Zhang 30609110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel() 30619110b2e7SHong Zhang @*/ 30629110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**)) 30639110b2e7SHong Zhang { 30649110b2e7SHong Zhang PetscFunctionBegin; 30659110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30669110b2e7SHong Zhang if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set"); 30679110b2e7SHong Zhang ts->adjointmonitor[ts->numberadjointmonitors] = adjointmonitor; 30689110b2e7SHong Zhang ts->adjointmonitordestroy[ts->numberadjointmonitors] = adjointmdestroy; 30699110b2e7SHong Zhang ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx; 30709110b2e7SHong Zhang PetscFunctionReturn(0); 30719110b2e7SHong Zhang } 30729110b2e7SHong Zhang 30739110b2e7SHong Zhang #undef __FUNCT__ 30749110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorCancel" 30759110b2e7SHong Zhang /*@C 30769110b2e7SHong Zhang TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object. 30779110b2e7SHong Zhang 30789110b2e7SHong Zhang Logically Collective on TS 30799110b2e7SHong Zhang 30809110b2e7SHong Zhang Input Parameters: 30819110b2e7SHong Zhang . ts - the TS context obtained from TSCreate() 30829110b2e7SHong Zhang 30839110b2e7SHong Zhang Notes: 30849110b2e7SHong Zhang There is no way to remove a single, specific monitor. 30859110b2e7SHong Zhang 30869110b2e7SHong Zhang Level: intermediate 30879110b2e7SHong Zhang 30889110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 30899110b2e7SHong Zhang 30909110b2e7SHong Zhang .seealso: TSAdjointMonitorSet() 30919110b2e7SHong Zhang @*/ 30929110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorCancel(TS ts) 30939110b2e7SHong Zhang { 30949110b2e7SHong Zhang PetscErrorCode ierr; 30959110b2e7SHong Zhang PetscInt i; 30969110b2e7SHong Zhang 30979110b2e7SHong Zhang PetscFunctionBegin; 30989110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30999110b2e7SHong Zhang for (i=0; i<ts->numberadjointmonitors; i++) { 31009110b2e7SHong Zhang if (ts->adjointmonitordestroy[i]) { 31019110b2e7SHong Zhang ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 31029110b2e7SHong Zhang } 31039110b2e7SHong Zhang } 31049110b2e7SHong Zhang ts->numberadjointmonitors = 0; 31059110b2e7SHong Zhang PetscFunctionReturn(0); 31069110b2e7SHong Zhang } 31079110b2e7SHong Zhang 31089110b2e7SHong Zhang #undef __FUNCT__ 3109110eb670SHong Zhang #define __FUNCT__ "TSAdjointMonitorDefault" 3110110eb670SHong Zhang /*@ 3111110eb670SHong Zhang TSAdjointMonitorDefault - Sets the Default monitor 3112110eb670SHong Zhang 3113110eb670SHong Zhang Level: intermediate 3114110eb670SHong Zhang 3115110eb670SHong Zhang .keywords: TS, set, monitor 3116110eb670SHong Zhang 3117110eb670SHong Zhang .seealso: TSAdjointMonitorSet() 3118110eb670SHong Zhang @*/ 3119110eb670SHong Zhang PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 3120110eb670SHong Zhang { 3121110eb670SHong Zhang PetscErrorCode ierr; 3122110eb670SHong Zhang PetscViewer viewer = (PetscViewer) dummy; 3123110eb670SHong Zhang 3124110eb670SHong Zhang PetscFunctionBegin; 3125110eb670SHong Zhang PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3126110eb670SHong Zhang ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3127110eb670SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr); 3128110eb670SHong Zhang ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3129110eb670SHong Zhang PetscFunctionReturn(0); 3130110eb670SHong Zhang } 3131110eb670SHong Zhang 3132110eb670SHong Zhang #undef __FUNCT__ 3133cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 3134cd652676SJed Brown /*@ 3135cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 3136cd652676SJed Brown 3137cd652676SJed Brown Logically Collective on TS 3138cd652676SJed Brown 3139cd652676SJed Brown Input Argument: 3140cd652676SJed Brown . ts - time stepping context 3141cd652676SJed Brown 3142cd652676SJed Brown Output Argument: 3143cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 3144cd652676SJed Brown 3145cd652676SJed Brown Level: intermediate 3146cd652676SJed Brown 3147cd652676SJed Brown .keywords: TS, set 3148cd652676SJed Brown 3149cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 3150cd652676SJed Brown @*/ 3151cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 3152cd652676SJed Brown { 3153cd652676SJed Brown PetscFunctionBegin; 3154cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3155cd652676SJed Brown ts->retain_stages = flg; 3156cd652676SJed Brown PetscFunctionReturn(0); 3157cd652676SJed Brown } 3158cd652676SJed Brown 3159cd652676SJed Brown #undef __FUNCT__ 3160cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 3161cd652676SJed Brown /*@ 3162cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3163cd652676SJed Brown 3164cd652676SJed Brown Collective on TS 3165cd652676SJed Brown 3166cd652676SJed Brown Input Argument: 3167cd652676SJed Brown + ts - time stepping context 3168cd652676SJed Brown - t - time to interpolate to 3169cd652676SJed Brown 3170cd652676SJed Brown Output Argument: 31710910c330SBarry Smith . U - state at given time 3172cd652676SJed Brown 3173cd652676SJed Brown Notes: 3174cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 3175cd652676SJed Brown 3176cd652676SJed Brown Level: intermediate 3177cd652676SJed Brown 3178cd652676SJed Brown Developer Notes: 3179cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3180cd652676SJed Brown 3181cd652676SJed Brown .keywords: TS, set 3182cd652676SJed Brown 3183cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 3184cd652676SJed Brown @*/ 31850910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3186cd652676SJed Brown { 3187cd652676SJed Brown PetscErrorCode ierr; 3188cd652676SJed Brown 3189cd652676SJed Brown PetscFunctionBegin; 3190cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3191b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 31926712e2f1SBarry 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); 3193ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 31940910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3195cd652676SJed Brown PetscFunctionReturn(0); 3196cd652676SJed Brown } 3197cd652676SJed Brown 3198cd652676SJed Brown #undef __FUNCT__ 31994a2ae208SSatish Balay #define __FUNCT__ "TSStep" 3200d763cef2SBarry Smith /*@ 32016d9e5789SSean Farley TSStep - Steps one time step 3202d763cef2SBarry Smith 3203d763cef2SBarry Smith Collective on TS 3204d763cef2SBarry Smith 3205d763cef2SBarry Smith Input Parameter: 3206d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3207d763cef2SBarry Smith 320827829d71SBarry Smith Level: developer 3209d763cef2SBarry Smith 3210b8123daeSJed Brown Notes: 321127829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 321227829d71SBarry Smith 3213b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3214b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3215b8123daeSJed Brown 321625cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 321725cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 321825cb2221SBarry Smith 3219d763cef2SBarry Smith .keywords: TS, timestep, solve 3220d763cef2SBarry Smith 32219be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3222d763cef2SBarry Smith @*/ 3223193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3224d763cef2SBarry Smith { 32252fb8446cSMatthew G. Knepley DM dm; 3226dfbe8321SBarry Smith PetscErrorCode ierr; 3227fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3228d763cef2SBarry Smith 3229d763cef2SBarry Smith PetscFunctionBegin; 32300700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3231fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3232fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3233fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3234fffbeea8SBarry Smith " type = {Preprint},\n" 3235fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3236fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3237302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3238fffbeea8SBarry Smith 32392fb8446cSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3240d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 324168bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3242d405a339SMatthew Knepley 3243362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 324424655328SShri ts->ptime_prev = ts->ptime; 3245cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3246bbd56ea5SKarl Rupp 3247e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3248d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3249193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3250d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3251bbd56ea5SKarl Rupp 325224655328SShri ts->time_step_prev = ts->ptime - ts->ptime_prev; 3253cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3254362cd11cSLisandro Dalcin 3255362cd11cSLisandro Dalcin if (ts->reason < 0) { 3256cef5090cSJed Brown if (ts->errorifstepfailed) { 325708c7845fSBarry 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]); 325808c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3259d2daff3dSHong Zhang } 326008c7845fSBarry Smith } else if (!ts->reason) { 326108c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 326208c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 326308c7845fSBarry Smith } 32642c18e0fdSBarry Smith ts->total_steps++; 32658392e04aSShri Abhyankar ts->steprollback = PETSC_FALSE; 326608c7845fSBarry Smith PetscFunctionReturn(0); 326708c7845fSBarry Smith } 326808c7845fSBarry Smith 326908c7845fSBarry Smith #undef __FUNCT__ 327008c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep" 327108c7845fSBarry Smith /*@ 3272b957a604SHong Zhang TSAdjointStep - Steps one time step backward in the adjoint run 327308c7845fSBarry Smith 327408c7845fSBarry Smith Collective on TS 327508c7845fSBarry Smith 327608c7845fSBarry Smith Input Parameter: 327708c7845fSBarry Smith . ts - the TS context obtained from TSCreate() 327808c7845fSBarry Smith 32796a4d4014SLisandro Dalcin Level: intermediate 32806a4d4014SLisandro Dalcin 3281b957a604SHong Zhang .keywords: TS, adjoint, step 32826a4d4014SLisandro Dalcin 3283b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve() 32846a4d4014SLisandro Dalcin @*/ 328508c7845fSBarry Smith PetscErrorCode TSAdjointStep(TS ts) 32866a4d4014SLisandro Dalcin { 328708c7845fSBarry Smith DM dm; 32886a4d4014SLisandro Dalcin PetscErrorCode ierr; 32896a4d4014SLisandro Dalcin 32906a4d4014SLisandro Dalcin PetscFunctionBegin; 32914a2ae208SSatish Balay PetscValidHeaderSpecific(ts, TS_CLASSID,1); 329208c7845fSBarry Smith ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 329308c7845fSBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3294d763cef2SBarry Smith 3295d763cef2SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 329608c7845fSBarry Smith ts->ptime_prev = ts->ptime; 329708c7845fSBarry Smith ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3298685405a1SBarry Smith ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts, "-ts_view_solution");CHKERRQ(ierr); 3299d763cef2SBarry Smith 33008d0ad7a8SHong Zhang ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 330142f2b339SBarry Smith if (!ts->ops->adjointstep) 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); 330242f2b339SBarry Smith ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr); 33038d0ad7a8SHong Zhang ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 3304d763cef2SBarry Smith 3305cef5090cSJed Brown ts->time_step_prev = ts->ptime - ts->ptime_prev; 3306cef5090cSJed Brown ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3307362cd11cSLisandro Dalcin 3308362cd11cSLisandro Dalcin if (ts->reason < 0) { 3309cef5090cSJed Brown if (ts->errorifstepfailed) { 33106c4ed002SBarry 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]); 33116c4ed002SBarry Smith else if (ts->reason == TS_DIVERGED_STEP_REJECTED) 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]); 33126c4ed002SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3313cef5090cSJed Brown } 3314362cd11cSLisandro Dalcin } else if (!ts->reason) { 331573b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3316db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3317362cd11cSLisandro Dalcin } 33182c18e0fdSBarry Smith ts->total_steps--; 3319d763cef2SBarry Smith PetscFunctionReturn(0); 3320d763cef2SBarry Smith } 3321d763cef2SBarry Smith 3322d763cef2SBarry Smith #undef __FUNCT__ 332305175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 332405175c85SJed Brown /*@ 332505175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 332605175c85SJed Brown 33271c3436cfSJed Brown Collective on TS 332805175c85SJed Brown 332905175c85SJed Brown Input Arguments: 33301c3436cfSJed Brown + ts - time stepping context 33311c3436cfSJed Brown . order - desired order of accuracy 33320298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 333305175c85SJed Brown 333405175c85SJed Brown Output Arguments: 33350910c330SBarry Smith . U - state at the end of the current step 333605175c85SJed Brown 333705175c85SJed Brown Level: advanced 333805175c85SJed Brown 3339108c343cSJed Brown Notes: 3340108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3341108c343cSJed 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. 3342108c343cSJed Brown 33431c3436cfSJed Brown .seealso: TSStep(), TSAdapt 334405175c85SJed Brown @*/ 33450910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 334605175c85SJed Brown { 334705175c85SJed Brown PetscErrorCode ierr; 334805175c85SJed Brown 334905175c85SJed Brown PetscFunctionBegin; 335005175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 335105175c85SJed Brown PetscValidType(ts,1); 33520910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3353ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 33540910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 335505175c85SJed Brown PetscFunctionReturn(0); 335605175c85SJed Brown } 335705175c85SJed Brown 3358d67e68b7SBarry Smith 335905175c85SJed Brown #undef __FUNCT__ 3360d763cef2SBarry Smith #define __FUNCT__ "TSSolve" 3361d763cef2SBarry Smith /*@ 3362d763cef2SBarry Smith TSSolve - Steps the requested number of timesteps. 3363d763cef2SBarry Smith 3364d763cef2SBarry Smith Collective on TS 3365d763cef2SBarry Smith 3366d763cef2SBarry Smith Input Parameter: 3367d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3368cc708dedSBarry Smith - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 33695a3a76d0SJed Brown 3370d763cef2SBarry Smith Level: beginner 3371d763cef2SBarry Smith 33725a3a76d0SJed Brown Notes: 33735a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 33745a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 33755a3a76d0SJed Brown stepped over the final time. 33765a3a76d0SJed Brown 3377d763cef2SBarry Smith .keywords: TS, timestep, solve 3378d763cef2SBarry Smith 3379d763cef2SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep() 3380d763cef2SBarry Smith @*/ 3381cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 3382d763cef2SBarry Smith { 3383b06615a5SLisandro Dalcin Vec solution; 3384d763cef2SBarry Smith PetscErrorCode ierr; 3385d763cef2SBarry Smith 3386d763cef2SBarry Smith PetscFunctionBegin; 3387d763cef2SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3388f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 338949354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE) { /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */ 3390b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 33910910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 3392b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3393b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3394b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 33955a3a76d0SJed Brown } 33960910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3397bbd56ea5SKarl Rupp } else if (u) { 33980910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 33995a3a76d0SJed Brown } 3400b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 340168bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3402d763cef2SBarry Smith /* reset time step and iteration counters */ 3403193ac0bcSJed Brown ts->steps = 0; 34045ef26d82SJed Brown ts->ksp_its = 0; 34055ef26d82SJed Brown ts->snes_its = 0; 3406c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3407c610991cSLisandro Dalcin ts->reject = 0; 3408193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3409193ac0bcSJed Brown 3410ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3411ec8db0baSMatthew G. Knepley { 3412ec8db0baSMatthew G. Knepley DM dm; 3413ec8db0baSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3414ec8db0baSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3415ec8db0baSMatthew G. Knepley } 3416f05ece33SBarry Smith 3417193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3418193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 34190910c330SBarry Smith ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 3420cc708dedSBarry Smith ts->solvetime = ts->ptime; 3421193ac0bcSJed Brown } else { 3422d763cef2SBarry Smith /* steps the requested number of timesteps. */ 3423db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3424db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3425cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 342611b05f00SHong Zhang if (ts->vec_costintegral) ts->costintegralfwd=PETSC_TRUE; 34276fea3669SShri Abhyankar if(ts->event) { 34286fea3669SShri Abhyankar ierr = TSEventMonitorInitialize(ts);CHKERRQ(ierr); 34296fea3669SShri Abhyankar } 3430e1a7a14fSJed Brown while (!ts->reason) { 3431193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 3432193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 3433aeb4809dSShri Abhyankar if (ts->event) { 3434aeb4809dSShri Abhyankar ierr = TSEventMonitor(ts);CHKERRQ(ierr); 34351eda64f1SShri Abhyankar } 34368392e04aSShri Abhyankar if(!ts->steprollback) { 3437cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 34381eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 3439aeb4809dSShri Abhyankar } 3440193ac0bcSJed Brown } 344149354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 34420910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 3443cc708dedSBarry Smith ts->solvetime = ts->max_time; 3444b06615a5SLisandro Dalcin solution = u; 34450574a7fbSJed Brown } else { 3446ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3447cc708dedSBarry Smith ts->solvetime = ts->ptime; 3448b06615a5SLisandro Dalcin solution = ts->vec_sol; 34490574a7fbSJed Brown } 3450b06615a5SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr); 3451685405a1SBarry Smith ierr = VecViewFromOptions(solution,(PetscObject) ts,"-ts_view_solution");CHKERRQ(ierr); 3452193ac0bcSJed Brown } 3453d2daff3dSHong Zhang 3454ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 3455dc1cb503SEmil Constantinescu ierr = VecViewFromOptions(ts->vec_sol,NULL,"-ts_view_solution");CHKERRQ(ierr); 345656f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 3457ef222394SBarry Smith if (ts->adjoint_solve) { 3458ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 34592b0a91c0SBarry Smith } 3460d763cef2SBarry Smith PetscFunctionReturn(0); 3461d763cef2SBarry Smith } 3462d763cef2SBarry Smith 3463d763cef2SBarry Smith #undef __FUNCT__ 3464c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve" 3465c88f2d44SBarry Smith /*@ 3466c88f2d44SBarry Smith TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 3467c88f2d44SBarry Smith 3468c88f2d44SBarry Smith Collective on TS 3469c88f2d44SBarry Smith 3470c88f2d44SBarry Smith Input Parameter: 34712c39e106SBarry Smith . ts - the TS context obtained from TSCreate() 3472c88f2d44SBarry Smith 3473e87fd094SBarry Smith Options Database: 3474e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions 3475e87fd094SBarry Smith 3476c88f2d44SBarry Smith Level: intermediate 3477c88f2d44SBarry Smith 3478c88f2d44SBarry Smith Notes: 3479c88f2d44SBarry Smith This must be called after a call to TSSolve() that solves the forward problem 3480c88f2d44SBarry Smith 348173b18844SBarry Smith By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time 348273b18844SBarry Smith 3483c88f2d44SBarry Smith .keywords: TS, timestep, solve 3484c88f2d44SBarry Smith 3485b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep() 3486c88f2d44SBarry Smith @*/ 34872c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts) 3488c88f2d44SBarry Smith { 3489c88f2d44SBarry Smith PetscErrorCode ierr; 3490c88f2d44SBarry Smith 3491c88f2d44SBarry Smith PetscFunctionBegin; 3492c88f2d44SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3493c88f2d44SBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 349468bece0bSHong Zhang 3495c88f2d44SBarry Smith /* reset time step and iteration counters */ 3496c88f2d44SBarry Smith ts->steps = 0; 3497c88f2d44SBarry Smith ts->ksp_its = 0; 3498c88f2d44SBarry Smith ts->snes_its = 0; 3499c88f2d44SBarry Smith ts->num_snes_failures = 0; 3500c88f2d44SBarry Smith ts->reject = 0; 3501c88f2d44SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 3502c88f2d44SBarry Smith 350373b18844SBarry Smith if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps; 3504c88f2d44SBarry Smith 350573b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3506c88f2d44SBarry Smith while (!ts->reason) { 350755c52731SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 35089110b2e7SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 3509c88f2d44SBarry Smith if (ts->event) { 3510d0578d90SShri Abhyankar ierr = TSAdjointEventMonitor(ts);CHKERRQ(ierr); 3511d0578d90SShri Abhyankar } 3512616946c1SHong Zhang ierr = TSAdjointStep(ts);CHKERRQ(ierr); 3513c88f2d44SBarry Smith } 351426656371SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 351526656371SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 3516c88f2d44SBarry Smith ts->solvetime = ts->ptime; 3517685405a1SBarry Smith ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr); 3518c88f2d44SBarry Smith PetscFunctionReturn(0); 3519c88f2d44SBarry Smith } 3520c88f2d44SBarry Smith 3521c88f2d44SBarry Smith #undef __FUNCT__ 3522d763cef2SBarry Smith #define __FUNCT__ "TSMonitor" 35237db568b7SBarry Smith /*@C 3524228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 3525228d79bcSJed Brown 3526228d79bcSJed Brown Collective on TS 3527228d79bcSJed Brown 3528228d79bcSJed Brown Input Parameters: 3529228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 3530228d79bcSJed Brown . step - step number that has just completed 3531228d79bcSJed Brown . ptime - model time of the state 35320910c330SBarry Smith - u - state at the current model time 3533228d79bcSJed Brown 3534228d79bcSJed Brown Notes: 35357db568b7SBarry Smith TSMonitor() is typically used automatically within the time stepping implementations. 35367db568b7SBarry Smith Users would almost never call this routine directly. 3537228d79bcSJed Brown 35387db568b7SBarry Smith Level: developer 3539228d79bcSJed Brown 3540228d79bcSJed Brown .keywords: TS, timestep 3541228d79bcSJed Brown @*/ 35420910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 3543d763cef2SBarry Smith { 35446849ba73SBarry Smith PetscErrorCode ierr; 3545a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 3546d763cef2SBarry Smith 3547d763cef2SBarry Smith PetscFunctionBegin; 3548b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3549b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 35505edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 3551d763cef2SBarry Smith for (i=0; i<n; i++) { 35520910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 3553d763cef2SBarry Smith } 35545edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 3555d763cef2SBarry Smith PetscFunctionReturn(0); 3556d763cef2SBarry Smith } 3557d763cef2SBarry Smith 35589110b2e7SHong Zhang #undef __FUNCT__ 35599110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitor" 35607db568b7SBarry Smith /*@C 35619110b2e7SHong Zhang TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet() 35629110b2e7SHong Zhang 35639110b2e7SHong Zhang Collective on TS 35649110b2e7SHong Zhang 35659110b2e7SHong Zhang Input Parameters: 35669110b2e7SHong Zhang + ts - time stepping context obtained from TSCreate() 35679110b2e7SHong Zhang . step - step number that has just completed 35689110b2e7SHong Zhang . ptime - model time of the state 35699110b2e7SHong Zhang . u - state at the current model time 35709110b2e7SHong Zhang . numcost - number of cost functions (dimension of lambda or mu) 35719110b2e7SHong Zhang . lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 35729110b2e7SHong Zhang - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 35739110b2e7SHong Zhang 35749110b2e7SHong Zhang Notes: 35757db568b7SBarry Smith TSAdjointMonitor() is typically used automatically within the time stepping implementations. 35767db568b7SBarry Smith Users would almost never call this routine directly. 35779110b2e7SHong Zhang 35787db568b7SBarry Smith Level: developer 35799110b2e7SHong Zhang 35809110b2e7SHong Zhang .keywords: TS, timestep 35819110b2e7SHong Zhang @*/ 35829110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu) 35839110b2e7SHong Zhang { 35849110b2e7SHong Zhang PetscErrorCode ierr; 35859110b2e7SHong Zhang PetscInt i,n = ts->numberadjointmonitors; 35869110b2e7SHong Zhang 35879110b2e7SHong Zhang PetscFunctionBegin; 35889110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 35899110b2e7SHong Zhang PetscValidHeaderSpecific(u,VEC_CLASSID,4); 35909110b2e7SHong Zhang ierr = VecLockPush(u);CHKERRQ(ierr); 35919110b2e7SHong Zhang for (i=0; i<n; i++) { 35929110b2e7SHong Zhang ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 35939110b2e7SHong Zhang } 35949110b2e7SHong Zhang ierr = VecLockPop(u);CHKERRQ(ierr); 35959110b2e7SHong Zhang PetscFunctionReturn(0); 35969110b2e7SHong Zhang } 35979110b2e7SHong Zhang 3598d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 35994a2ae208SSatish Balay #undef __FUNCT__ 3600a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 3601d763cef2SBarry Smith /*@C 36027db568b7SBarry Smith TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 3603a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 3604d763cef2SBarry Smith 3605d763cef2SBarry Smith Collective on TS 3606d763cef2SBarry Smith 3607d763cef2SBarry Smith Input Parameters: 3608d763cef2SBarry Smith + host - the X display to open, or null for the local machine 3609d763cef2SBarry Smith . label - the title to put in the title bar 36107c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 3611a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 3612a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 3613d763cef2SBarry Smith 3614d763cef2SBarry Smith Output Parameter: 36150b039ecaSBarry Smith . ctx - the context 3616d763cef2SBarry Smith 3617d763cef2SBarry Smith Options Database Key: 36184f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 36197db568b7SBarry Smith . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 36207db568b7SBarry Smith . -ts_monitor_lg_error - monitor the error 36217db568b7SBarry Smith . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 36227db568b7SBarry Smith . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 3623b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 3624d763cef2SBarry Smith 3625d763cef2SBarry Smith Notes: 3626a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 3627d763cef2SBarry Smith 36287db568b7SBarry Smith One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 36297db568b7SBarry Smith 36307db568b7SBarry Smith Many of the functions that control the monitoring have two forms: TSMonitorLGSet/GetXXXX() and TSMonitorLGCtxSet/GetXXXX() the first take a TS object as the 36317db568b7SBarry Smith first argument (if that TS object does not have a TSMonitorLGCtx associated with it the function call is ignored) and the second takes a TSMonitorLGCtx object 36327db568b7SBarry Smith as the first argument. 36337db568b7SBarry Smith 36347db568b7SBarry Smith One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 36357db568b7SBarry Smith 36367db568b7SBarry Smith 3637d763cef2SBarry Smith Level: intermediate 3638d763cef2SBarry Smith 36397db568b7SBarry Smith .keywords: TS, monitor, line graph, residual 3640d763cef2SBarry Smith 36417db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 36427db568b7SBarry Smith TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 36437db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 36447db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 36457db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 36467c922b88SBarry Smith 3647d763cef2SBarry Smith @*/ 3648a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 3649d763cef2SBarry Smith { 36507f52daa2SLisandro Dalcin PetscDraw draw; 3651dfbe8321SBarry Smith PetscErrorCode ierr; 3652d763cef2SBarry Smith 3653d763cef2SBarry Smith PetscFunctionBegin; 3654b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 36557f52daa2SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 36567f52daa2SLisandro Dalcin ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 36577f52daa2SLisandro Dalcin ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 3658b6fe0379SLisandro Dalcin ierr = PetscDrawLGSetUseMarkers((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr); 3659287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 36607f52daa2SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 3661a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 3662d763cef2SBarry Smith PetscFunctionReturn(0); 3663d763cef2SBarry Smith } 3664d763cef2SBarry Smith 36654a2ae208SSatish Balay #undef __FUNCT__ 36664f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 3667b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 3668d763cef2SBarry Smith { 36690b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 3670c32365f1SBarry Smith PetscReal x = ptime,y; 3671dfbe8321SBarry Smith PetscErrorCode ierr; 3672d763cef2SBarry Smith 3673d763cef2SBarry Smith PetscFunctionBegin; 3674b06615a5SLisandro Dalcin if (!step) { 3675a9f9c1f6SBarry Smith PetscDrawAxis axis; 3676a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 3677a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 3678a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 3679a9f9c1f6SBarry Smith } 3680c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 36810b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 3682b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 36830b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 36843923b477SBarry Smith } 3685d763cef2SBarry Smith PetscFunctionReturn(0); 3686d763cef2SBarry Smith } 3687d763cef2SBarry Smith 36884a2ae208SSatish Balay #undef __FUNCT__ 3689a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 3690d763cef2SBarry Smith /*@C 3691a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 3692a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 3693d763cef2SBarry Smith 36940b039ecaSBarry Smith Collective on TSMonitorLGCtx 3695d763cef2SBarry Smith 3696d763cef2SBarry Smith Input Parameter: 36970b039ecaSBarry Smith . ctx - the monitor context 3698d763cef2SBarry Smith 3699d763cef2SBarry Smith Level: intermediate 3700d763cef2SBarry Smith 3701d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 3702d763cef2SBarry Smith 37034f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 3704d763cef2SBarry Smith @*/ 3705a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 3706d763cef2SBarry Smith { 3707dfbe8321SBarry Smith PetscErrorCode ierr; 3708d763cef2SBarry Smith 3709d763cef2SBarry Smith PetscFunctionBegin; 37107684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 37117684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 37127684fa3eSBarry Smith } 37130b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 371431152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 3715387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 3716387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 3717387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 37180b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 3719d763cef2SBarry Smith PetscFunctionReturn(0); 3720d763cef2SBarry Smith } 3721d763cef2SBarry Smith 37224a2ae208SSatish Balay #undef __FUNCT__ 37234a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 3724d763cef2SBarry Smith /*@ 3725b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 3726d763cef2SBarry Smith 3727d763cef2SBarry Smith Not Collective 3728d763cef2SBarry Smith 3729d763cef2SBarry Smith Input Parameter: 3730d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3731d763cef2SBarry Smith 3732d763cef2SBarry Smith Output Parameter: 3733d763cef2SBarry Smith . t - the current time 3734d763cef2SBarry Smith 3735d763cef2SBarry Smith Level: beginner 3736d763cef2SBarry Smith 3737b8123daeSJed Brown Note: 3738b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 37399be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 3740b8123daeSJed Brown 3741d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3742d763cef2SBarry Smith 3743d763cef2SBarry Smith .keywords: TS, get, time 3744d763cef2SBarry Smith @*/ 37457087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 3746d763cef2SBarry Smith { 3747d763cef2SBarry Smith PetscFunctionBegin; 37480700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3749f7cf8827SBarry Smith PetscValidRealPointer(t,2); 3750d763cef2SBarry Smith *t = ts->ptime; 3751d763cef2SBarry Smith PetscFunctionReturn(0); 3752d763cef2SBarry Smith } 3753d763cef2SBarry Smith 37544a2ae208SSatish Balay #undef __FUNCT__ 3755e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime" 3756e5e524a1SHong Zhang /*@ 3757e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 3758e5e524a1SHong Zhang 3759e5e524a1SHong Zhang Not Collective 3760e5e524a1SHong Zhang 3761e5e524a1SHong Zhang Input Parameter: 3762e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 3763e5e524a1SHong Zhang 3764e5e524a1SHong Zhang Output Parameter: 3765e5e524a1SHong Zhang . t - the previous time 3766e5e524a1SHong Zhang 3767e5e524a1SHong Zhang Level: beginner 3768e5e524a1SHong Zhang 3769e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3770e5e524a1SHong Zhang 3771e5e524a1SHong Zhang .keywords: TS, get, time 3772e5e524a1SHong Zhang @*/ 3773e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 3774e5e524a1SHong Zhang { 3775e5e524a1SHong Zhang PetscFunctionBegin; 3776e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3777e5e524a1SHong Zhang PetscValidRealPointer(t,2); 3778e5e524a1SHong Zhang *t = ts->ptime_prev; 3779e5e524a1SHong Zhang PetscFunctionReturn(0); 3780e5e524a1SHong Zhang } 3781e5e524a1SHong Zhang 3782e5e524a1SHong Zhang #undef __FUNCT__ 37836a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 37846a4d4014SLisandro Dalcin /*@ 37856a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 37866a4d4014SLisandro Dalcin 37873f9fe445SBarry Smith Logically Collective on TS 37886a4d4014SLisandro Dalcin 37896a4d4014SLisandro Dalcin Input Parameters: 37906a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 37916a4d4014SLisandro Dalcin - time - the time 37926a4d4014SLisandro Dalcin 37936a4d4014SLisandro Dalcin Level: intermediate 37946a4d4014SLisandro Dalcin 37956a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 37966a4d4014SLisandro Dalcin 37976a4d4014SLisandro Dalcin .keywords: TS, set, time 37986a4d4014SLisandro Dalcin @*/ 37997087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 38006a4d4014SLisandro Dalcin { 38016a4d4014SLisandro Dalcin PetscFunctionBegin; 38020700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3803c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 38046a4d4014SLisandro Dalcin ts->ptime = t; 38056a4d4014SLisandro Dalcin PetscFunctionReturn(0); 38066a4d4014SLisandro Dalcin } 38076a4d4014SLisandro Dalcin 38086a4d4014SLisandro Dalcin #undef __FUNCT__ 38094a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 3810d763cef2SBarry Smith /*@C 3811d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 3812d763cef2SBarry Smith TS options in the database. 3813d763cef2SBarry Smith 38143f9fe445SBarry Smith Logically Collective on TS 3815d763cef2SBarry Smith 3816d763cef2SBarry Smith Input Parameter: 3817d763cef2SBarry Smith + ts - The TS context 3818d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3819d763cef2SBarry Smith 3820d763cef2SBarry Smith Notes: 3821d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3822d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3823d763cef2SBarry Smith hyphen. 3824d763cef2SBarry Smith 3825d763cef2SBarry Smith Level: advanced 3826d763cef2SBarry Smith 3827d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 3828d763cef2SBarry Smith 3829d763cef2SBarry Smith .seealso: TSSetFromOptions() 3830d763cef2SBarry Smith 3831d763cef2SBarry Smith @*/ 38327087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 3833d763cef2SBarry Smith { 3834dfbe8321SBarry Smith PetscErrorCode ierr; 3835089b2837SJed Brown SNES snes; 3836d763cef2SBarry Smith 3837d763cef2SBarry Smith PetscFunctionBegin; 38380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3839d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3840089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3841089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3842d763cef2SBarry Smith PetscFunctionReturn(0); 3843d763cef2SBarry Smith } 3844d763cef2SBarry Smith 3845d763cef2SBarry Smith 38464a2ae208SSatish Balay #undef __FUNCT__ 38474a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 3848d763cef2SBarry Smith /*@C 3849d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 3850d763cef2SBarry Smith TS options in the database. 3851d763cef2SBarry Smith 38523f9fe445SBarry Smith Logically Collective on TS 3853d763cef2SBarry Smith 3854d763cef2SBarry Smith Input Parameter: 3855d763cef2SBarry Smith + ts - The TS context 3856d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3857d763cef2SBarry Smith 3858d763cef2SBarry Smith Notes: 3859d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3860d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3861d763cef2SBarry Smith hyphen. 3862d763cef2SBarry Smith 3863d763cef2SBarry Smith Level: advanced 3864d763cef2SBarry Smith 3865d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 3866d763cef2SBarry Smith 3867d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 3868d763cef2SBarry Smith 3869d763cef2SBarry Smith @*/ 38707087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 3871d763cef2SBarry Smith { 3872dfbe8321SBarry Smith PetscErrorCode ierr; 3873089b2837SJed Brown SNES snes; 3874d763cef2SBarry Smith 3875d763cef2SBarry Smith PetscFunctionBegin; 38760700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3877d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3878089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3879089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3880d763cef2SBarry Smith PetscFunctionReturn(0); 3881d763cef2SBarry Smith } 3882d763cef2SBarry Smith 38834a2ae208SSatish Balay #undef __FUNCT__ 38844a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 3885d763cef2SBarry Smith /*@C 3886d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 3887d763cef2SBarry Smith TS options in the database. 3888d763cef2SBarry Smith 3889d763cef2SBarry Smith Not Collective 3890d763cef2SBarry Smith 3891d763cef2SBarry Smith Input Parameter: 3892d763cef2SBarry Smith . ts - The TS context 3893d763cef2SBarry Smith 3894d763cef2SBarry Smith Output Parameter: 3895d763cef2SBarry Smith . prefix - A pointer to the prefix string used 3896d763cef2SBarry Smith 3897d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 3898d763cef2SBarry Smith sufficient length to hold the prefix. 3899d763cef2SBarry Smith 3900d763cef2SBarry Smith Level: intermediate 3901d763cef2SBarry Smith 3902d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 3903d763cef2SBarry Smith 3904d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 3905d763cef2SBarry Smith @*/ 39067087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 3907d763cef2SBarry Smith { 3908dfbe8321SBarry Smith PetscErrorCode ierr; 3909d763cef2SBarry Smith 3910d763cef2SBarry Smith PetscFunctionBegin; 39110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 39124482741eSBarry Smith PetscValidPointer(prefix,2); 3913d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3914d763cef2SBarry Smith PetscFunctionReturn(0); 3915d763cef2SBarry Smith } 3916d763cef2SBarry Smith 39174a2ae208SSatish Balay #undef __FUNCT__ 39184a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 3919d763cef2SBarry Smith /*@C 3920d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 3921d763cef2SBarry Smith 3922d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 3923d763cef2SBarry Smith 3924d763cef2SBarry Smith Input Parameter: 3925d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 3926d763cef2SBarry Smith 3927d763cef2SBarry Smith Output Parameters: 3928e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 3929e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 3930e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 3931e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 3932d763cef2SBarry Smith 39330298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 3934d763cef2SBarry Smith 3935d763cef2SBarry Smith Level: intermediate 3936d763cef2SBarry Smith 393726d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 3938d763cef2SBarry Smith 3939d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 3940d763cef2SBarry Smith @*/ 3941e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 3942d763cef2SBarry Smith { 3943089b2837SJed Brown PetscErrorCode ierr; 3944089b2837SJed Brown SNES snes; 394524989b8cSPeter Brune DM dm; 3946089b2837SJed Brown 3947d763cef2SBarry Smith PetscFunctionBegin; 3948089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3949e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 395024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 395124989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 3952d763cef2SBarry Smith PetscFunctionReturn(0); 3953d763cef2SBarry Smith } 3954d763cef2SBarry Smith 39551713a123SBarry Smith #undef __FUNCT__ 39562eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 39572eca1d9cSJed Brown /*@C 39582eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 39592eca1d9cSJed Brown 39602eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 39612eca1d9cSJed Brown 39622eca1d9cSJed Brown Input Parameter: 39632eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 39642eca1d9cSJed Brown 39652eca1d9cSJed Brown Output Parameters: 3966e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 3967e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 39682eca1d9cSJed Brown . f - The function to compute the matrices 39692eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 39702eca1d9cSJed Brown 39710298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 39722eca1d9cSJed Brown 39732eca1d9cSJed Brown Level: advanced 39742eca1d9cSJed Brown 39752eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 39762eca1d9cSJed Brown 39772eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 39782eca1d9cSJed Brown @*/ 3979e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 39802eca1d9cSJed Brown { 3981089b2837SJed Brown PetscErrorCode ierr; 3982089b2837SJed Brown SNES snes; 398324989b8cSPeter Brune DM dm; 39840910c330SBarry Smith 39852eca1d9cSJed Brown PetscFunctionBegin; 3986089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3987f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 3988e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 398924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 399024989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 39912eca1d9cSJed Brown PetscFunctionReturn(0); 39922eca1d9cSJed Brown } 39932eca1d9cSJed Brown 39946083293cSBarry Smith 39952eca1d9cSJed Brown #undef __FUNCT__ 399683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 39971713a123SBarry Smith /*@C 399883a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 39991713a123SBarry Smith VecView() for the solution at each timestep 40001713a123SBarry Smith 40011713a123SBarry Smith Collective on TS 40021713a123SBarry Smith 40031713a123SBarry Smith Input Parameters: 40041713a123SBarry Smith + ts - the TS context 40051713a123SBarry Smith . step - current time-step 4006142b95e3SSatish Balay . ptime - current time 40070298fd71SBarry Smith - dummy - either a viewer or NULL 40081713a123SBarry Smith 400999fdda47SBarry Smith Options Database: 401099fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 401199fdda47SBarry Smith 4012387f4636SBarry Smith Notes: the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 401399fdda47SBarry Smith will look bad 401499fdda47SBarry Smith 40151713a123SBarry Smith Level: intermediate 40161713a123SBarry Smith 40171713a123SBarry Smith .keywords: TS, vector, monitor, view 40181713a123SBarry Smith 4019a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 40201713a123SBarry Smith @*/ 40210910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 40221713a123SBarry Smith { 4023dfbe8321SBarry Smith PetscErrorCode ierr; 402483a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4025473a3ab2SBarry Smith PetscDraw draw; 40261713a123SBarry Smith 40271713a123SBarry Smith PetscFunctionBegin; 40286083293cSBarry Smith if (!step && ictx->showinitial) { 40296083293cSBarry Smith if (!ictx->initialsolution) { 40300910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 40311713a123SBarry Smith } 40320910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 40336083293cSBarry Smith } 4034b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 40350dcf80beSBarry Smith 40366083293cSBarry Smith if (ictx->showinitial) { 40376083293cSBarry Smith PetscReal pause; 40386083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 40396083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 40406083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 40416083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 40426083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 40436083293cSBarry Smith } 40440910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 4045473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 404651fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 4047473a3ab2SBarry Smith char time[32]; 4048473a3ab2SBarry Smith 4049473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 405085b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4051473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4052473a3ab2SBarry Smith h = yl + .95*(yr - yl); 405351fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4054473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4055473a3ab2SBarry Smith } 4056473a3ab2SBarry Smith 40576083293cSBarry Smith if (ictx->showinitial) { 40586083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 40596083293cSBarry Smith } 40601713a123SBarry Smith PetscFunctionReturn(0); 40611713a123SBarry Smith } 40621713a123SBarry Smith 40632d5ee99bSBarry Smith #undef __FUNCT__ 40649110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorDrawSensi" 40659110b2e7SHong Zhang /*@C 40669110b2e7SHong Zhang TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling 40679110b2e7SHong Zhang VecView() for the sensitivities to initial states at each timestep 40689110b2e7SHong Zhang 40699110b2e7SHong Zhang Collective on TS 40709110b2e7SHong Zhang 40719110b2e7SHong Zhang Input Parameters: 40729110b2e7SHong Zhang + ts - the TS context 40739110b2e7SHong Zhang . step - current time-step 40749110b2e7SHong Zhang . ptime - current time 40759110b2e7SHong Zhang . u - current state 40769110b2e7SHong Zhang . numcost - number of cost functions 40779110b2e7SHong Zhang . lambda - sensitivities to initial conditions 40789110b2e7SHong Zhang . mu - sensitivities to parameters 40799110b2e7SHong Zhang - dummy - either a viewer or NULL 40809110b2e7SHong Zhang 40819110b2e7SHong Zhang Level: intermediate 40829110b2e7SHong Zhang 40839110b2e7SHong Zhang .keywords: TS, vector, adjoint, monitor, view 40849110b2e7SHong Zhang 40859110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView() 40869110b2e7SHong Zhang @*/ 40879110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 40889110b2e7SHong Zhang { 40899110b2e7SHong Zhang PetscErrorCode ierr; 40909110b2e7SHong Zhang TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 40919110b2e7SHong Zhang PetscDraw draw; 4092a0046e2cSSatish Balay PetscReal xl,yl,xr,yr,h; 4093a0046e2cSSatish Balay char time[32]; 40949110b2e7SHong Zhang 40959110b2e7SHong Zhang PetscFunctionBegin; 40969110b2e7SHong Zhang if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 40979110b2e7SHong Zhang 40989110b2e7SHong Zhang ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr); 40999110b2e7SHong Zhang ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 41009110b2e7SHong Zhang ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 41019110b2e7SHong Zhang ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 41029110b2e7SHong Zhang h = yl + .95*(yr - yl); 41039110b2e7SHong Zhang ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 41049110b2e7SHong Zhang ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 41059110b2e7SHong Zhang 41069110b2e7SHong Zhang PetscFunctionReturn(0); 41079110b2e7SHong Zhang } 41089110b2e7SHong Zhang 41099110b2e7SHong Zhang #undef __FUNCT__ 41102d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase" 41112d5ee99bSBarry Smith /*@C 41122d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 41132d5ee99bSBarry Smith 41142d5ee99bSBarry Smith Collective on TS 41152d5ee99bSBarry Smith 41162d5ee99bSBarry Smith Input Parameters: 41172d5ee99bSBarry Smith + ts - the TS context 41182d5ee99bSBarry Smith . step - current time-step 41192d5ee99bSBarry Smith . ptime - current time 41202d5ee99bSBarry Smith - dummy - either a viewer or NULL 41212d5ee99bSBarry Smith 41222d5ee99bSBarry Smith Level: intermediate 41232d5ee99bSBarry Smith 41242d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 41252d5ee99bSBarry Smith 41262d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 41272d5ee99bSBarry Smith @*/ 41282d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 41292d5ee99bSBarry Smith { 41302d5ee99bSBarry Smith PetscErrorCode ierr; 41312d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 41322d5ee99bSBarry Smith PetscDraw draw; 41332d5ee99bSBarry Smith MPI_Comm comm; 41342d5ee99bSBarry Smith PetscInt n; 41352d5ee99bSBarry Smith PetscMPIInt size; 413651fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 41372d5ee99bSBarry Smith char time[32]; 41382d5ee99bSBarry Smith const PetscScalar *U; 41392d5ee99bSBarry Smith 41402d5ee99bSBarry Smith PetscFunctionBegin; 41412d5ee99bSBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 41422d5ee99bSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 41432d5ee99bSBarry Smith if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs"); 41442d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 41452d5ee99bSBarry Smith if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 41462d5ee99bSBarry Smith 41472d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 41482d5ee99bSBarry Smith 41492d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 41504b363babSBarry Smith ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 4151ba5783adSMatthew G Knepley if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) { 4152a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 4153a4494fc1SBarry Smith PetscFunctionReturn(0); 4154a4494fc1SBarry Smith } 41552d5ee99bSBarry Smith if (!step) ictx->color++; 41562d5ee99bSBarry Smith ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr); 41572d5ee99bSBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 41582d5ee99bSBarry Smith 41592d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 41604b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 416185b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 41622d5ee99bSBarry Smith h = yl + .95*(yr - yl); 416351fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 41642d5ee99bSBarry Smith } 41652d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 41662d5ee99bSBarry Smith PetscFunctionReturn(0); 41672d5ee99bSBarry Smith } 41682d5ee99bSBarry Smith 41691713a123SBarry Smith 41706c699258SBarry Smith #undef __FUNCT__ 417183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 41726083293cSBarry Smith /*@C 417383a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 41746083293cSBarry Smith 41756083293cSBarry Smith Collective on TS 41766083293cSBarry Smith 41776083293cSBarry Smith Input Parameters: 41786083293cSBarry Smith . ctx - the monitor context 41796083293cSBarry Smith 41806083293cSBarry Smith Level: intermediate 41816083293cSBarry Smith 41826083293cSBarry Smith .keywords: TS, vector, monitor, view 41836083293cSBarry Smith 418483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 41856083293cSBarry Smith @*/ 418683a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 41876083293cSBarry Smith { 41886083293cSBarry Smith PetscErrorCode ierr; 41896083293cSBarry Smith 41906083293cSBarry Smith PetscFunctionBegin; 41914b363babSBarry Smith ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr); 419283a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 419383a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 419483a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 41956083293cSBarry Smith PetscFunctionReturn(0); 41966083293cSBarry Smith } 41976083293cSBarry Smith 41986083293cSBarry Smith #undef __FUNCT__ 419983a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 42006083293cSBarry Smith /*@C 420183a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 42026083293cSBarry Smith 42036083293cSBarry Smith Collective on TS 42046083293cSBarry Smith 42056083293cSBarry Smith Input Parameter: 42066083293cSBarry Smith . ts - time-step context 42076083293cSBarry Smith 42086083293cSBarry Smith Output Patameter: 42096083293cSBarry Smith . ctx - the monitor context 42106083293cSBarry Smith 421199fdda47SBarry Smith Options Database: 421299fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 421399fdda47SBarry Smith 42146083293cSBarry Smith Level: intermediate 42156083293cSBarry Smith 42166083293cSBarry Smith .keywords: TS, vector, monitor, view 42176083293cSBarry Smith 421883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 42196083293cSBarry Smith @*/ 422083a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 42216083293cSBarry Smith { 42226083293cSBarry Smith PetscErrorCode ierr; 42236083293cSBarry Smith 42246083293cSBarry Smith PetscFunctionBegin; 4225b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 422683a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4227e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4228bbd56ea5SKarl Rupp 422983a4ac43SBarry Smith (*ctx)->howoften = howoften; 4230473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 42310298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4232473a3ab2SBarry Smith 4233473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 42340298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 42352d5ee99bSBarry Smith (*ctx)->color = PETSC_DRAW_WHITE; 42366083293cSBarry Smith PetscFunctionReturn(0); 42376083293cSBarry Smith } 42386083293cSBarry Smith 42396083293cSBarry Smith #undef __FUNCT__ 424083a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 42413a471f94SBarry Smith /*@C 424283a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 42433a471f94SBarry Smith VecView() for the error at each timestep 42443a471f94SBarry Smith 42453a471f94SBarry Smith Collective on TS 42463a471f94SBarry Smith 42473a471f94SBarry Smith Input Parameters: 42483a471f94SBarry Smith + ts - the TS context 42493a471f94SBarry Smith . step - current time-step 42503a471f94SBarry Smith . ptime - current time 42510298fd71SBarry Smith - dummy - either a viewer or NULL 42523a471f94SBarry Smith 42533a471f94SBarry Smith Level: intermediate 42543a471f94SBarry Smith 42553a471f94SBarry Smith .keywords: TS, vector, monitor, view 42563a471f94SBarry Smith 42573a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 42583a471f94SBarry Smith @*/ 42590910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 42603a471f94SBarry Smith { 42613a471f94SBarry Smith PetscErrorCode ierr; 426283a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 426383a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 42643a471f94SBarry Smith Vec work; 42653a471f94SBarry Smith 42663a471f94SBarry Smith PetscFunctionBegin; 4267b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 42680910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 42693a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 42700910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 42713a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 42723a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 42733a471f94SBarry Smith PetscFunctionReturn(0); 42743a471f94SBarry Smith } 42753a471f94SBarry Smith 4276af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 42773a471f94SBarry Smith #undef __FUNCT__ 42786c699258SBarry Smith #define __FUNCT__ "TSSetDM" 42796c699258SBarry Smith /*@ 42806c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 42816c699258SBarry Smith 42823f9fe445SBarry Smith Logically Collective on TS and DM 42836c699258SBarry Smith 42846c699258SBarry Smith Input Parameters: 42856c699258SBarry Smith + ts - the preconditioner context 42866c699258SBarry Smith - dm - the dm 42876c699258SBarry Smith 42886c699258SBarry Smith Level: intermediate 42896c699258SBarry Smith 42906c699258SBarry Smith 42916c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 42926c699258SBarry Smith @*/ 42937087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 42946c699258SBarry Smith { 42956c699258SBarry Smith PetscErrorCode ierr; 4296089b2837SJed Brown SNES snes; 4297942e3340SBarry Smith DMTS tsdm; 42986c699258SBarry Smith 42996c699258SBarry Smith PetscFunctionBegin; 43000700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 430170663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4302942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 43032a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4304942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4305942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 430624989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 430724989b8cSPeter Brune tsdm->originaldm = dm; 430824989b8cSPeter Brune } 430924989b8cSPeter Brune } 43106bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 431124989b8cSPeter Brune } 43126c699258SBarry Smith ts->dm = dm; 4313bbd56ea5SKarl Rupp 4314089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4315089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 43166c699258SBarry Smith PetscFunctionReturn(0); 43176c699258SBarry Smith } 43186c699258SBarry Smith 43196c699258SBarry Smith #undef __FUNCT__ 43206c699258SBarry Smith #define __FUNCT__ "TSGetDM" 43216c699258SBarry Smith /*@ 43226c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 43236c699258SBarry Smith 43243f9fe445SBarry Smith Not Collective 43256c699258SBarry Smith 43266c699258SBarry Smith Input Parameter: 43276c699258SBarry Smith . ts - the preconditioner context 43286c699258SBarry Smith 43296c699258SBarry Smith Output Parameter: 43306c699258SBarry Smith . dm - the dm 43316c699258SBarry Smith 43326c699258SBarry Smith Level: intermediate 43336c699258SBarry Smith 43346c699258SBarry Smith 43356c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 43366c699258SBarry Smith @*/ 43377087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 43386c699258SBarry Smith { 4339496e6a7aSJed Brown PetscErrorCode ierr; 4340496e6a7aSJed Brown 43416c699258SBarry Smith PetscFunctionBegin; 43420700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4343496e6a7aSJed Brown if (!ts->dm) { 4344ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4345496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4346496e6a7aSJed Brown } 43476c699258SBarry Smith *dm = ts->dm; 43486c699258SBarry Smith PetscFunctionReturn(0); 43496c699258SBarry Smith } 43501713a123SBarry Smith 43510f5c6efeSJed Brown #undef __FUNCT__ 43520f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 43530f5c6efeSJed Brown /*@ 43540f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 43550f5c6efeSJed Brown 43563f9fe445SBarry Smith Logically Collective on SNES 43570f5c6efeSJed Brown 43580f5c6efeSJed Brown Input Parameter: 4359d42a1c89SJed Brown + snes - nonlinear solver 43600910c330SBarry Smith . U - the current state at which to evaluate the residual 4361d42a1c89SJed Brown - ctx - user context, must be a TS 43620f5c6efeSJed Brown 43630f5c6efeSJed Brown Output Parameter: 43640f5c6efeSJed Brown . F - the nonlinear residual 43650f5c6efeSJed Brown 43660f5c6efeSJed Brown Notes: 43670f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 43680f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 43690f5c6efeSJed Brown 43700f5c6efeSJed Brown Level: advanced 43710f5c6efeSJed Brown 43720f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 43730f5c6efeSJed Brown @*/ 43740910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 43750f5c6efeSJed Brown { 43760f5c6efeSJed Brown TS ts = (TS)ctx; 43770f5c6efeSJed Brown PetscErrorCode ierr; 43780f5c6efeSJed Brown 43790f5c6efeSJed Brown PetscFunctionBegin; 43800f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 43810910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 43820f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 43830f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 43840910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 43850f5c6efeSJed Brown PetscFunctionReturn(0); 43860f5c6efeSJed Brown } 43870f5c6efeSJed Brown 43880f5c6efeSJed Brown #undef __FUNCT__ 43890f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 43900f5c6efeSJed Brown /*@ 43910f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 43920f5c6efeSJed Brown 43930f5c6efeSJed Brown Collective on SNES 43940f5c6efeSJed Brown 43950f5c6efeSJed Brown Input Parameter: 43960f5c6efeSJed Brown + snes - nonlinear solver 43970910c330SBarry Smith . U - the current state at which to evaluate the residual 43980f5c6efeSJed Brown - ctx - user context, must be a TS 43990f5c6efeSJed Brown 44000f5c6efeSJed Brown Output Parameter: 44010f5c6efeSJed Brown + A - the Jacobian 44020f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 44030f5c6efeSJed Brown - flag - indicates any structure change in the matrix 44040f5c6efeSJed Brown 44050f5c6efeSJed Brown Notes: 44060f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 44070f5c6efeSJed Brown 44080f5c6efeSJed Brown Level: developer 44090f5c6efeSJed Brown 44100f5c6efeSJed Brown .seealso: SNESSetJacobian() 44110f5c6efeSJed Brown @*/ 4412d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 44130f5c6efeSJed Brown { 44140f5c6efeSJed Brown TS ts = (TS)ctx; 44150f5c6efeSJed Brown PetscErrorCode ierr; 44160f5c6efeSJed Brown 44170f5c6efeSJed Brown PetscFunctionBegin; 44180f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 44190910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 44200f5c6efeSJed Brown PetscValidPointer(A,3); 442194ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 44220f5c6efeSJed Brown PetscValidPointer(B,4); 442394ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 44240f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 4425d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 44260f5c6efeSJed Brown PetscFunctionReturn(0); 44270f5c6efeSJed Brown } 4428325fc9f4SBarry Smith 44290e4ef248SJed Brown #undef __FUNCT__ 44300e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 44310e4ef248SJed Brown /*@C 44320e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 44330e4ef248SJed Brown 44340e4ef248SJed Brown Collective on TS 44350e4ef248SJed Brown 44360e4ef248SJed Brown Input Arguments: 44370e4ef248SJed Brown + ts - time stepping context 44380e4ef248SJed Brown . t - time at which to evaluate 44390910c330SBarry Smith . U - state at which to evaluate 44400e4ef248SJed Brown - ctx - context 44410e4ef248SJed Brown 44420e4ef248SJed Brown Output Arguments: 44430e4ef248SJed Brown . F - right hand side 44440e4ef248SJed Brown 44450e4ef248SJed Brown Level: intermediate 44460e4ef248SJed Brown 44470e4ef248SJed Brown Notes: 44480e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 44490e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 44500e4ef248SJed Brown 44510e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 44520e4ef248SJed Brown @*/ 44530910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 44540e4ef248SJed Brown { 44550e4ef248SJed Brown PetscErrorCode ierr; 44560e4ef248SJed Brown Mat Arhs,Brhs; 44570e4ef248SJed Brown 44580e4ef248SJed Brown PetscFunctionBegin; 44590e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 4460d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 44610910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 44620e4ef248SJed Brown PetscFunctionReturn(0); 44630e4ef248SJed Brown } 44640e4ef248SJed Brown 44650e4ef248SJed Brown #undef __FUNCT__ 44660e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 44670e4ef248SJed Brown /*@C 44680e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 44690e4ef248SJed Brown 44700e4ef248SJed Brown Collective on TS 44710e4ef248SJed Brown 44720e4ef248SJed Brown Input Arguments: 44730e4ef248SJed Brown + ts - time stepping context 44740e4ef248SJed Brown . t - time at which to evaluate 44750910c330SBarry Smith . U - state at which to evaluate 44760e4ef248SJed Brown - ctx - context 44770e4ef248SJed Brown 44780e4ef248SJed Brown Output Arguments: 44790e4ef248SJed Brown + A - pointer to operator 44800e4ef248SJed Brown . B - pointer to preconditioning matrix 44810e4ef248SJed Brown - flg - matrix structure flag 44820e4ef248SJed Brown 44830e4ef248SJed Brown Level: intermediate 44840e4ef248SJed Brown 44850e4ef248SJed Brown Notes: 44860e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 44870e4ef248SJed Brown 44880e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 44890e4ef248SJed Brown @*/ 4490d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 44910e4ef248SJed Brown { 44920e4ef248SJed Brown PetscFunctionBegin; 44930e4ef248SJed Brown PetscFunctionReturn(0); 44940e4ef248SJed Brown } 44950e4ef248SJed Brown 44960026cea9SSean Farley #undef __FUNCT__ 44970026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 44980026cea9SSean Farley /*@C 44990026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 45000026cea9SSean Farley 45010026cea9SSean Farley Collective on TS 45020026cea9SSean Farley 45030026cea9SSean Farley Input Arguments: 45040026cea9SSean Farley + ts - time stepping context 45050026cea9SSean Farley . t - time at which to evaluate 45060910c330SBarry Smith . U - state at which to evaluate 45070910c330SBarry Smith . Udot - time derivative of state vector 45080026cea9SSean Farley - ctx - context 45090026cea9SSean Farley 45100026cea9SSean Farley Output Arguments: 45110026cea9SSean Farley . F - left hand side 45120026cea9SSean Farley 45130026cea9SSean Farley Level: intermediate 45140026cea9SSean Farley 45150026cea9SSean Farley Notes: 45160910c330SBarry 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 45170026cea9SSean Farley user is required to write their own TSComputeIFunction. 45180026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 45190026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 45200026cea9SSean Farley 45210026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 45220026cea9SSean Farley @*/ 45230910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 45240026cea9SSean Farley { 45250026cea9SSean Farley PetscErrorCode ierr; 45260026cea9SSean Farley Mat A,B; 45270026cea9SSean Farley 45280026cea9SSean Farley PetscFunctionBegin; 45290298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 4530d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 45310910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 45320026cea9SSean Farley PetscFunctionReturn(0); 45330026cea9SSean Farley } 45340026cea9SSean Farley 45350026cea9SSean Farley #undef __FUNCT__ 45360026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 45370026cea9SSean Farley /*@C 4538b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 45390026cea9SSean Farley 45400026cea9SSean Farley Collective on TS 45410026cea9SSean Farley 45420026cea9SSean Farley Input Arguments: 45430026cea9SSean Farley + ts - time stepping context 45440026cea9SSean Farley . t - time at which to evaluate 45450910c330SBarry Smith . U - state at which to evaluate 45460910c330SBarry Smith . Udot - time derivative of state vector 45470026cea9SSean Farley . shift - shift to apply 45480026cea9SSean Farley - ctx - context 45490026cea9SSean Farley 45500026cea9SSean Farley Output Arguments: 45510026cea9SSean Farley + A - pointer to operator 45520026cea9SSean Farley . B - pointer to preconditioning matrix 45530026cea9SSean Farley - flg - matrix structure flag 45540026cea9SSean Farley 4555b41af12eSJed Brown Level: advanced 45560026cea9SSean Farley 45570026cea9SSean Farley Notes: 45580026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 45590026cea9SSean Farley 4560b41af12eSJed Brown It is only appropriate for problems of the form 4561b41af12eSJed Brown 4562b41af12eSJed Brown $ M Udot = F(U,t) 4563b41af12eSJed Brown 4564b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4565b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4566b41af12eSJed Brown an implicit operator of the form 4567b41af12eSJed Brown 4568b41af12eSJed Brown $ shift*M + J 4569b41af12eSJed Brown 4570b41af12eSJed 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 4571b41af12eSJed Brown a copy of M or reassemble it when requested. 4572b41af12eSJed Brown 45730026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 45740026cea9SSean Farley @*/ 4575d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 45760026cea9SSean Farley { 4577b41af12eSJed Brown PetscErrorCode ierr; 4578b41af12eSJed Brown 45790026cea9SSean Farley PetscFunctionBegin; 458094ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4581b41af12eSJed Brown ts->ijacobian.shift = shift; 45820026cea9SSean Farley PetscFunctionReturn(0); 45830026cea9SSean Farley } 4584b41af12eSJed Brown 4585e817cc15SEmil Constantinescu #undef __FUNCT__ 4586e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType" 4587e817cc15SEmil Constantinescu /*@ 4588e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 4589e817cc15SEmil Constantinescu 4590e817cc15SEmil Constantinescu Not Collective 4591e817cc15SEmil Constantinescu 4592e817cc15SEmil Constantinescu Input Parameter: 4593e817cc15SEmil Constantinescu . ts - the TS context 4594e817cc15SEmil Constantinescu 4595e817cc15SEmil Constantinescu Output Parameter: 45964e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4597e817cc15SEmil Constantinescu 4598e817cc15SEmil Constantinescu Level: beginner 4599e817cc15SEmil Constantinescu 4600e817cc15SEmil Constantinescu .keywords: TS, equation type 4601e817cc15SEmil Constantinescu 4602e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 4603e817cc15SEmil Constantinescu @*/ 4604e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4605e817cc15SEmil Constantinescu { 4606e817cc15SEmil Constantinescu PetscFunctionBegin; 4607e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4608e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 4609e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 4610e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4611e817cc15SEmil Constantinescu } 4612e817cc15SEmil Constantinescu 4613e817cc15SEmil Constantinescu #undef __FUNCT__ 4614e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType" 4615e817cc15SEmil Constantinescu /*@ 4616e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 4617e817cc15SEmil Constantinescu 4618e817cc15SEmil Constantinescu Not Collective 4619e817cc15SEmil Constantinescu 4620e817cc15SEmil Constantinescu Input Parameter: 4621e817cc15SEmil Constantinescu + ts - the TS context 46221297b224SEmil Constantinescu - equation_type - see TSEquationType 4623e817cc15SEmil Constantinescu 4624e817cc15SEmil Constantinescu Level: advanced 4625e817cc15SEmil Constantinescu 4626e817cc15SEmil Constantinescu .keywords: TS, equation type 4627e817cc15SEmil Constantinescu 4628e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 4629e817cc15SEmil Constantinescu @*/ 4630e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4631e817cc15SEmil Constantinescu { 4632e817cc15SEmil Constantinescu PetscFunctionBegin; 4633e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4634e817cc15SEmil Constantinescu ts->equation_type = equation_type; 4635e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4636e817cc15SEmil Constantinescu } 46370026cea9SSean Farley 46384af1b03aSJed Brown #undef __FUNCT__ 46394af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 46404af1b03aSJed Brown /*@ 46414af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 46424af1b03aSJed Brown 46434af1b03aSJed Brown Not Collective 46444af1b03aSJed Brown 46454af1b03aSJed Brown Input Parameter: 46464af1b03aSJed Brown . ts - the TS context 46474af1b03aSJed Brown 46484af1b03aSJed Brown Output Parameter: 46494af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 46504af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 46514af1b03aSJed Brown 4652487e0bb9SJed Brown Level: beginner 46534af1b03aSJed Brown 4654cd652676SJed Brown Notes: 4655cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 46564af1b03aSJed Brown 46574af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 46584af1b03aSJed Brown 46594af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 46604af1b03aSJed Brown @*/ 46614af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 46624af1b03aSJed Brown { 46634af1b03aSJed Brown PetscFunctionBegin; 46644af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 46654af1b03aSJed Brown PetscValidPointer(reason,2); 46664af1b03aSJed Brown *reason = ts->reason; 46674af1b03aSJed Brown PetscFunctionReturn(0); 46684af1b03aSJed Brown } 46694af1b03aSJed Brown 4670fb1732b5SBarry Smith #undef __FUNCT__ 4671d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 4672d6ad946cSShri Abhyankar /*@ 4673d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4674d6ad946cSShri Abhyankar 4675d6ad946cSShri Abhyankar Not Collective 4676d6ad946cSShri Abhyankar 4677d6ad946cSShri Abhyankar Input Parameter: 4678d6ad946cSShri Abhyankar + ts - the TS context 4679d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4680d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4681d6ad946cSShri Abhyankar 4682f5abba47SShri Abhyankar Level: advanced 4683d6ad946cSShri Abhyankar 4684d6ad946cSShri Abhyankar Notes: 4685d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 4686d6ad946cSShri Abhyankar 4687d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 4688d6ad946cSShri Abhyankar 4689d6ad946cSShri Abhyankar .seealso: TSConvergedReason 4690d6ad946cSShri Abhyankar @*/ 4691d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4692d6ad946cSShri Abhyankar { 4693d6ad946cSShri Abhyankar PetscFunctionBegin; 4694d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4695d6ad946cSShri Abhyankar ts->reason = reason; 4696d6ad946cSShri Abhyankar PetscFunctionReturn(0); 4697d6ad946cSShri Abhyankar } 4698d6ad946cSShri Abhyankar 4699d6ad946cSShri Abhyankar #undef __FUNCT__ 4700cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 4701cc708dedSBarry Smith /*@ 4702cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 4703cc708dedSBarry Smith 4704cc708dedSBarry Smith Not Collective 4705cc708dedSBarry Smith 4706cc708dedSBarry Smith Input Parameter: 4707cc708dedSBarry Smith . ts - the TS context 4708cc708dedSBarry Smith 4709cc708dedSBarry Smith Output Parameter: 4710cc708dedSBarry Smith . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 4711cc708dedSBarry Smith 4712487e0bb9SJed Brown Level: beginner 4713cc708dedSBarry Smith 4714cc708dedSBarry Smith Notes: 4715cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 4716cc708dedSBarry Smith 4717cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 4718cc708dedSBarry Smith 4719cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 4720cc708dedSBarry Smith @*/ 4721cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 4722cc708dedSBarry Smith { 4723cc708dedSBarry Smith PetscFunctionBegin; 4724cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4725cc708dedSBarry Smith PetscValidPointer(ftime,2); 4726cc708dedSBarry Smith *ftime = ts->solvetime; 4727cc708dedSBarry Smith PetscFunctionReturn(0); 4728cc708dedSBarry Smith } 4729cc708dedSBarry Smith 4730cc708dedSBarry Smith #undef __FUNCT__ 47312c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps" 47322c18e0fdSBarry Smith /*@ 47332c18e0fdSBarry Smith TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 47342c18e0fdSBarry Smith 47352c18e0fdSBarry Smith Not Collective 47362c18e0fdSBarry Smith 47372c18e0fdSBarry Smith Input Parameter: 47382c18e0fdSBarry Smith . ts - the TS context 47392c18e0fdSBarry Smith 47402c18e0fdSBarry Smith Output Parameter: 47412c18e0fdSBarry Smith . steps - the number of steps 47422c18e0fdSBarry Smith 47432c18e0fdSBarry Smith Level: beginner 47442c18e0fdSBarry Smith 47452c18e0fdSBarry Smith Notes: 47462c18e0fdSBarry Smith Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 47472c18e0fdSBarry Smith 47482c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test 47492c18e0fdSBarry Smith 47502c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 47512c18e0fdSBarry Smith @*/ 47522c18e0fdSBarry Smith PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 47532c18e0fdSBarry Smith { 47542c18e0fdSBarry Smith PetscFunctionBegin; 47552c18e0fdSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 47562c18e0fdSBarry Smith PetscValidPointer(steps,2); 47572c18e0fdSBarry Smith *steps = ts->total_steps; 47582c18e0fdSBarry Smith PetscFunctionReturn(0); 47592c18e0fdSBarry Smith } 47602c18e0fdSBarry Smith 47612c18e0fdSBarry Smith #undef __FUNCT__ 47625ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 47639f67acb7SJed Brown /*@ 47645ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 47659f67acb7SJed Brown used by the time integrator. 47669f67acb7SJed Brown 47679f67acb7SJed Brown Not Collective 47689f67acb7SJed Brown 47699f67acb7SJed Brown Input Parameter: 47709f67acb7SJed Brown . ts - TS context 47719f67acb7SJed Brown 47729f67acb7SJed Brown Output Parameter: 47739f67acb7SJed Brown . nits - number of nonlinear iterations 47749f67acb7SJed Brown 47759f67acb7SJed Brown Notes: 47769f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 47779f67acb7SJed Brown 47789f67acb7SJed Brown Level: intermediate 47799f67acb7SJed Brown 47809f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 47819f67acb7SJed Brown 47825ef26d82SJed Brown .seealso: TSGetKSPIterations() 47839f67acb7SJed Brown @*/ 47845ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 47859f67acb7SJed Brown { 47869f67acb7SJed Brown PetscFunctionBegin; 47879f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 47889f67acb7SJed Brown PetscValidIntPointer(nits,2); 47895ef26d82SJed Brown *nits = ts->snes_its; 47909f67acb7SJed Brown PetscFunctionReturn(0); 47919f67acb7SJed Brown } 47929f67acb7SJed Brown 47939f67acb7SJed Brown #undef __FUNCT__ 47945ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 47959f67acb7SJed Brown /*@ 47965ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 47979f67acb7SJed Brown used by the time integrator. 47989f67acb7SJed Brown 47999f67acb7SJed Brown Not Collective 48009f67acb7SJed Brown 48019f67acb7SJed Brown Input Parameter: 48029f67acb7SJed Brown . ts - TS context 48039f67acb7SJed Brown 48049f67acb7SJed Brown Output Parameter: 48059f67acb7SJed Brown . lits - number of linear iterations 48069f67acb7SJed Brown 48079f67acb7SJed Brown Notes: 48089f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 48099f67acb7SJed Brown 48109f67acb7SJed Brown Level: intermediate 48119f67acb7SJed Brown 48129f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 48139f67acb7SJed Brown 48145ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 48159f67acb7SJed Brown @*/ 48165ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 48179f67acb7SJed Brown { 48189f67acb7SJed Brown PetscFunctionBegin; 48199f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 48209f67acb7SJed Brown PetscValidIntPointer(lits,2); 48215ef26d82SJed Brown *lits = ts->ksp_its; 48229f67acb7SJed Brown PetscFunctionReturn(0); 48239f67acb7SJed Brown } 48249f67acb7SJed Brown 48259f67acb7SJed Brown #undef __FUNCT__ 4826cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 4827cef5090cSJed Brown /*@ 4828cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4829cef5090cSJed Brown 4830cef5090cSJed Brown Not Collective 4831cef5090cSJed Brown 4832cef5090cSJed Brown Input Parameter: 4833cef5090cSJed Brown . ts - TS context 4834cef5090cSJed Brown 4835cef5090cSJed Brown Output Parameter: 4836cef5090cSJed Brown . rejects - number of steps rejected 4837cef5090cSJed Brown 4838cef5090cSJed Brown Notes: 4839cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4840cef5090cSJed Brown 4841cef5090cSJed Brown Level: intermediate 4842cef5090cSJed Brown 4843cef5090cSJed Brown .keywords: TS, get, number 4844cef5090cSJed Brown 48455ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 4846cef5090cSJed Brown @*/ 4847cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 4848cef5090cSJed Brown { 4849cef5090cSJed Brown PetscFunctionBegin; 4850cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4851cef5090cSJed Brown PetscValidIntPointer(rejects,2); 4852cef5090cSJed Brown *rejects = ts->reject; 4853cef5090cSJed Brown PetscFunctionReturn(0); 4854cef5090cSJed Brown } 4855cef5090cSJed Brown 4856cef5090cSJed Brown #undef __FUNCT__ 4857cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 4858cef5090cSJed Brown /*@ 4859cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 4860cef5090cSJed Brown 4861cef5090cSJed Brown Not Collective 4862cef5090cSJed Brown 4863cef5090cSJed Brown Input Parameter: 4864cef5090cSJed Brown . ts - TS context 4865cef5090cSJed Brown 4866cef5090cSJed Brown Output Parameter: 4867cef5090cSJed Brown . fails - number of failed nonlinear solves 4868cef5090cSJed Brown 4869cef5090cSJed Brown Notes: 4870cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4871cef5090cSJed Brown 4872cef5090cSJed Brown Level: intermediate 4873cef5090cSJed Brown 4874cef5090cSJed Brown .keywords: TS, get, number 4875cef5090cSJed Brown 48765ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 4877cef5090cSJed Brown @*/ 4878cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 4879cef5090cSJed Brown { 4880cef5090cSJed Brown PetscFunctionBegin; 4881cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4882cef5090cSJed Brown PetscValidIntPointer(fails,2); 4883cef5090cSJed Brown *fails = ts->num_snes_failures; 4884cef5090cSJed Brown PetscFunctionReturn(0); 4885cef5090cSJed Brown } 4886cef5090cSJed Brown 4887cef5090cSJed Brown #undef __FUNCT__ 4888cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 4889cef5090cSJed Brown /*@ 4890cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 4891cef5090cSJed Brown 4892cef5090cSJed Brown Not Collective 4893cef5090cSJed Brown 4894cef5090cSJed Brown Input Parameter: 4895cef5090cSJed Brown + ts - TS context 4896cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 4897cef5090cSJed Brown 4898cef5090cSJed Brown Notes: 4899cef5090cSJed Brown The counter is reset to zero for each step 4900cef5090cSJed Brown 4901cef5090cSJed Brown Options Database Key: 4902cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4903cef5090cSJed Brown 4904cef5090cSJed Brown Level: intermediate 4905cef5090cSJed Brown 4906cef5090cSJed Brown .keywords: TS, set, maximum, number 4907cef5090cSJed Brown 49085ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4909cef5090cSJed Brown @*/ 4910cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 4911cef5090cSJed Brown { 4912cef5090cSJed Brown PetscFunctionBegin; 4913cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4914cef5090cSJed Brown ts->max_reject = rejects; 4915cef5090cSJed Brown PetscFunctionReturn(0); 4916cef5090cSJed Brown } 4917cef5090cSJed Brown 4918cef5090cSJed Brown #undef __FUNCT__ 4919cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 4920cef5090cSJed Brown /*@ 4921cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 4922cef5090cSJed Brown 4923cef5090cSJed Brown Not Collective 4924cef5090cSJed Brown 4925cef5090cSJed Brown Input Parameter: 4926cef5090cSJed Brown + ts - TS context 4927cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 4928cef5090cSJed Brown 4929cef5090cSJed Brown Notes: 4930cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 4931cef5090cSJed Brown 4932cef5090cSJed Brown Options Database Key: 4933cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4934cef5090cSJed Brown 4935cef5090cSJed Brown Level: intermediate 4936cef5090cSJed Brown 4937cef5090cSJed Brown .keywords: TS, set, maximum, number 4938cef5090cSJed Brown 49395ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 4940cef5090cSJed Brown @*/ 4941cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 4942cef5090cSJed Brown { 4943cef5090cSJed Brown PetscFunctionBegin; 4944cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4945cef5090cSJed Brown ts->max_snes_failures = fails; 4946cef5090cSJed Brown PetscFunctionReturn(0); 4947cef5090cSJed Brown } 4948cef5090cSJed Brown 4949cef5090cSJed Brown #undef __FUNCT__ 49504e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails" 4951cef5090cSJed Brown /*@ 4952cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 4953cef5090cSJed Brown 4954cef5090cSJed Brown Not Collective 4955cef5090cSJed Brown 4956cef5090cSJed Brown Input Parameter: 4957cef5090cSJed Brown + ts - TS context 4958cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 4959cef5090cSJed Brown 4960cef5090cSJed Brown Options Database Key: 4961cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 4962cef5090cSJed Brown 4963cef5090cSJed Brown Level: intermediate 4964cef5090cSJed Brown 4965cef5090cSJed Brown .keywords: TS, set, error 4966cef5090cSJed Brown 49675ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4968cef5090cSJed Brown @*/ 4969cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 4970cef5090cSJed Brown { 4971cef5090cSJed Brown PetscFunctionBegin; 4972cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4973cef5090cSJed Brown ts->errorifstepfailed = err; 4974cef5090cSJed Brown PetscFunctionReturn(0); 4975cef5090cSJed Brown } 4976cef5090cSJed Brown 4977cef5090cSJed Brown #undef __FUNCT__ 4978fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 4979fb1732b5SBarry Smith /*@C 4980fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 4981fb1732b5SBarry Smith 4982fb1732b5SBarry Smith Collective on TS 4983fb1732b5SBarry Smith 4984fb1732b5SBarry Smith Input Parameters: 4985fb1732b5SBarry Smith + ts - the TS context 4986fb1732b5SBarry Smith . step - current time-step 4987fb1732b5SBarry Smith . ptime - current time 49880910c330SBarry Smith . u - current state 4989fb1732b5SBarry Smith - viewer - binary viewer 4990fb1732b5SBarry Smith 4991fb1732b5SBarry Smith Level: intermediate 4992fb1732b5SBarry Smith 4993fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 4994fb1732b5SBarry Smith 4995fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4996fb1732b5SBarry Smith @*/ 49970910c330SBarry Smith PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 4998fb1732b5SBarry Smith { 4999fb1732b5SBarry Smith PetscErrorCode ierr; 5000ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 5001fb1732b5SBarry Smith 5002fb1732b5SBarry Smith PetscFunctionBegin; 50030910c330SBarry Smith ierr = VecView(u,v);CHKERRQ(ierr); 5004ed81e22dSJed Brown PetscFunctionReturn(0); 5005ed81e22dSJed Brown } 5006ed81e22dSJed Brown 5007ed81e22dSJed Brown #undef __FUNCT__ 5008ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 5009ed81e22dSJed Brown /*@C 5010ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5011ed81e22dSJed Brown 5012ed81e22dSJed Brown Collective on TS 5013ed81e22dSJed Brown 5014ed81e22dSJed Brown Input Parameters: 5015ed81e22dSJed Brown + ts - the TS context 5016ed81e22dSJed Brown . step - current time-step 5017ed81e22dSJed Brown . ptime - current time 50180910c330SBarry Smith . u - current state 5019ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5020ed81e22dSJed Brown 5021ed81e22dSJed Brown Level: intermediate 5022ed81e22dSJed Brown 5023ed81e22dSJed Brown Notes: 5024ed81e22dSJed 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. 5025ed81e22dSJed Brown These are named according to the file name template. 5026ed81e22dSJed Brown 5027ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5028ed81e22dSJed Brown 5029ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5030ed81e22dSJed Brown 5031ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5032ed81e22dSJed Brown @*/ 50330910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5034ed81e22dSJed Brown { 5035ed81e22dSJed Brown PetscErrorCode ierr; 5036ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 5037ed81e22dSJed Brown PetscViewer viewer; 5038ed81e22dSJed Brown 5039ed81e22dSJed Brown PetscFunctionBegin; 50408caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5041ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 50420910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 5043ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5044ed81e22dSJed Brown PetscFunctionReturn(0); 5045ed81e22dSJed Brown } 5046ed81e22dSJed Brown 5047ed81e22dSJed Brown #undef __FUNCT__ 5048ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 5049ed81e22dSJed Brown /*@C 5050ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5051ed81e22dSJed Brown 5052ed81e22dSJed Brown Collective on TS 5053ed81e22dSJed Brown 5054ed81e22dSJed Brown Input Parameters: 5055ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5056ed81e22dSJed Brown 5057ed81e22dSJed Brown Level: intermediate 5058ed81e22dSJed Brown 5059ed81e22dSJed Brown Note: 5060ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5061ed81e22dSJed Brown 5062ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5063ed81e22dSJed Brown 5064ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5065ed81e22dSJed Brown @*/ 5066ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5067ed81e22dSJed Brown { 5068ed81e22dSJed Brown PetscErrorCode ierr; 5069ed81e22dSJed Brown 5070ed81e22dSJed Brown PetscFunctionBegin; 5071ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5072fb1732b5SBarry Smith PetscFunctionReturn(0); 5073fb1732b5SBarry Smith } 5074fb1732b5SBarry Smith 507584df9cb4SJed Brown #undef __FUNCT__ 5076552698daSJed Brown #define __FUNCT__ "TSGetAdapt" 507784df9cb4SJed Brown /*@ 5078552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 507984df9cb4SJed Brown 5080ed81e22dSJed Brown Collective on TS if controller has not been created yet 508184df9cb4SJed Brown 508284df9cb4SJed Brown Input Arguments: 5083ed81e22dSJed Brown . ts - time stepping context 508484df9cb4SJed Brown 508584df9cb4SJed Brown Output Arguments: 5086ed81e22dSJed Brown . adapt - adaptive controller 508784df9cb4SJed Brown 508884df9cb4SJed Brown Level: intermediate 508984df9cb4SJed Brown 5090ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 509184df9cb4SJed Brown @*/ 5092552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 509384df9cb4SJed Brown { 509484df9cb4SJed Brown PetscErrorCode ierr; 509584df9cb4SJed Brown 509684df9cb4SJed Brown PetscFunctionBegin; 509784df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 509884df9cb4SJed Brown PetscValidPointer(adapt,2); 509984df9cb4SJed Brown if (!ts->adapt) { 5100ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 51013bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 51021c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 510384df9cb4SJed Brown } 510484df9cb4SJed Brown *adapt = ts->adapt; 510584df9cb4SJed Brown PetscFunctionReturn(0); 510684df9cb4SJed Brown } 5107d6ebe24aSShri Abhyankar 5108d6ebe24aSShri Abhyankar #undef __FUNCT__ 51091c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 51101c3436cfSJed Brown /*@ 51111c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 51121c3436cfSJed Brown 51131c3436cfSJed Brown Logically Collective 51141c3436cfSJed Brown 51151c3436cfSJed Brown Input Arguments: 51161c3436cfSJed Brown + ts - time integration context 51171c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 51180298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 51191c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 51200298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 51211c3436cfSJed Brown 5122a3cdaa26SBarry Smith Options Database keys: 5123a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5124a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5125a3cdaa26SBarry Smith 51263ff766beSShri Abhyankar Notes: 51273ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 51283ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 51293ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 51303ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 51313ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 51323ff766beSShri Abhyankar differential variables. 51333ff766beSShri Abhyankar 51341c3436cfSJed Brown Level: beginner 51351c3436cfSJed Brown 5136c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 51371c3436cfSJed Brown @*/ 51381c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 51391c3436cfSJed Brown { 51401c3436cfSJed Brown PetscErrorCode ierr; 51411c3436cfSJed Brown 51421c3436cfSJed Brown PetscFunctionBegin; 5143c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 51441c3436cfSJed Brown if (vatol) { 51451c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 51461c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 5147bbd56ea5SKarl Rupp 51481c3436cfSJed Brown ts->vatol = vatol; 51491c3436cfSJed Brown } 5150c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 51511c3436cfSJed Brown if (vrtol) { 51521c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 51531c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 5154bbd56ea5SKarl Rupp 51551c3436cfSJed Brown ts->vrtol = vrtol; 51561c3436cfSJed Brown } 51571c3436cfSJed Brown PetscFunctionReturn(0); 51581c3436cfSJed Brown } 51591c3436cfSJed Brown 51601c3436cfSJed Brown #undef __FUNCT__ 5161c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 5162c5033834SJed Brown /*@ 5163c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5164c5033834SJed Brown 5165c5033834SJed Brown Logically Collective 5166c5033834SJed Brown 5167c5033834SJed Brown Input Arguments: 5168c5033834SJed Brown . ts - time integration context 5169c5033834SJed Brown 5170c5033834SJed Brown Output Arguments: 51710298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 51720298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 51730298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 51740298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 5175c5033834SJed Brown 5176c5033834SJed Brown Level: beginner 5177c5033834SJed Brown 5178c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5179c5033834SJed Brown @*/ 5180c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5181c5033834SJed Brown { 5182c5033834SJed Brown PetscFunctionBegin; 5183c5033834SJed Brown if (atol) *atol = ts->atol; 5184c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5185c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5186c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 5187c5033834SJed Brown PetscFunctionReturn(0); 5188c5033834SJed Brown } 5189c5033834SJed Brown 5190c5033834SJed Brown #undef __FUNCT__ 51919c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2" 51929c6b16b5SShri Abhyankar /*@ 5193a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 51949c6b16b5SShri Abhyankar 51959c6b16b5SShri Abhyankar Collective on TS 51969c6b16b5SShri Abhyankar 51979c6b16b5SShri Abhyankar Input Arguments: 51989c6b16b5SShri Abhyankar + ts - time stepping context 5199a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5200a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 52019c6b16b5SShri Abhyankar 52029c6b16b5SShri Abhyankar Output Arguments: 52039c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 52049c6b16b5SShri Abhyankar 52059c6b16b5SShri Abhyankar Level: developer 52069c6b16b5SShri Abhyankar 5207deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 52089c6b16b5SShri Abhyankar @*/ 5209a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm) 52109c6b16b5SShri Abhyankar { 52119c6b16b5SShri Abhyankar PetscErrorCode ierr; 52129c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 52139c6b16b5SShri Abhyankar const PetscScalar *u,*y; 52149c6b16b5SShri Abhyankar PetscReal sum,gsum; 52159c6b16b5SShri Abhyankar PetscReal tol; 52169c6b16b5SShri Abhyankar 52179c6b16b5SShri Abhyankar PetscFunctionBegin; 52189c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5219a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5220a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5221a4868fbcSLisandro Dalcin PetscValidType(U,2); 5222a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5223a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5224a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5225a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 52269c6b16b5SShri Abhyankar 52279c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 52289c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 52299c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 52309c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 52319c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 52329c6b16b5SShri Abhyankar sum = 0.; 52339c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 52349c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 52359c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52369c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52379c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 52389c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52399c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 52409c6b16b5SShri Abhyankar } 52419c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52429c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52439c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 52449c6b16b5SShri Abhyankar const PetscScalar *atol; 52459c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52469c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 52479c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52489c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 52499c6b16b5SShri Abhyankar } 52509c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52519c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 52529c6b16b5SShri Abhyankar const PetscScalar *rtol; 52539c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52549c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 52559c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52569c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 52579c6b16b5SShri Abhyankar } 52589c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52599c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 52609c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 52619c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52629c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 52639c6b16b5SShri Abhyankar } 52649c6b16b5SShri Abhyankar } 52659c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 52669c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 52679c6b16b5SShri Abhyankar 52689c6b16b5SShri Abhyankar ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 52699c6b16b5SShri Abhyankar *norm = PetscSqrtReal(gsum / N); 52709c6b16b5SShri Abhyankar 52719c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 52729c6b16b5SShri Abhyankar PetscFunctionReturn(0); 52739c6b16b5SShri Abhyankar } 52749c6b16b5SShri Abhyankar 52759c6b16b5SShri Abhyankar #undef __FUNCT__ 52769c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity" 52779c6b16b5SShri Abhyankar /*@ 5278a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 52799c6b16b5SShri Abhyankar 52809c6b16b5SShri Abhyankar Collective on TS 52819c6b16b5SShri Abhyankar 52829c6b16b5SShri Abhyankar Input Arguments: 52839c6b16b5SShri Abhyankar + ts - time stepping context 5284a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5285a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 52869c6b16b5SShri Abhyankar 52879c6b16b5SShri Abhyankar Output Arguments: 52889c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 52899c6b16b5SShri Abhyankar 52909c6b16b5SShri Abhyankar Level: developer 52919c6b16b5SShri Abhyankar 5292deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 52939c6b16b5SShri Abhyankar @*/ 5294a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm) 52959c6b16b5SShri Abhyankar { 52969c6b16b5SShri Abhyankar PetscErrorCode ierr; 52979c6b16b5SShri Abhyankar PetscInt i,n,N,rstart,k; 52989c6b16b5SShri Abhyankar const PetscScalar *u,*y; 52999c6b16b5SShri Abhyankar PetscReal max,gmax; 53009c6b16b5SShri Abhyankar PetscReal tol; 53019c6b16b5SShri Abhyankar 53029c6b16b5SShri Abhyankar PetscFunctionBegin; 53039c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5304a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5305a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5306a4868fbcSLisandro Dalcin PetscValidType(U,2); 5307a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5308a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5309a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5310a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 53119c6b16b5SShri Abhyankar 53129c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 53139c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 53149c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 53159c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 53169c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 53179c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 53189c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 53199c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53209c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 53219c6b16b5SShri Abhyankar k = 0; 53229c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 53239c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 53249c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 53259c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53269c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 53279c6b16b5SShri Abhyankar } 53289c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53299c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 53309c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 53319c6b16b5SShri Abhyankar const PetscScalar *atol; 53329c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53339c6b16b5SShri Abhyankar k = 0; 53349c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 53359c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 53369c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 53379c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53389c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 53399c6b16b5SShri Abhyankar } 53409c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53419c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 53429c6b16b5SShri Abhyankar const PetscScalar *rtol; 53439c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 53449c6b16b5SShri Abhyankar k = 0; 53459c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 53469c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 53479c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 53489c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53499c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 53509c6b16b5SShri Abhyankar } 53519c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 53529c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 53539c6b16b5SShri Abhyankar k = 0; 53549c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 53559c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 53569c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 53579c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53589c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 53599c6b16b5SShri Abhyankar } 53609c6b16b5SShri Abhyankar } 53619c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 53629c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 53639c6b16b5SShri Abhyankar 53649c6b16b5SShri Abhyankar ierr = MPI_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 53659c6b16b5SShri Abhyankar *norm = gmax; 53669c6b16b5SShri Abhyankar 53679c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 53689c6b16b5SShri Abhyankar PetscFunctionReturn(0); 53699c6b16b5SShri Abhyankar } 53709c6b16b5SShri Abhyankar 53719c6b16b5SShri Abhyankar #undef __FUNCT__ 53727619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm" 53731c3436cfSJed Brown /*@ 5374a4868fbcSLisandro Dalcin TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors 53751c3436cfSJed Brown 53761c3436cfSJed Brown Collective on TS 53771c3436cfSJed Brown 53781c3436cfSJed Brown Input Arguments: 53791c3436cfSJed Brown + ts - time stepping context 5380a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5381a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5382a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 53837619abb3SShri 53841c3436cfSJed Brown Output Arguments: 53851c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 53861c3436cfSJed Brown 5387a4868fbcSLisandro Dalcin 5388a4868fbcSLisandro Dalcin Options Database Keys: 5389a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5390a4868fbcSLisandro Dalcin 53911c3436cfSJed Brown Level: developer 53921c3436cfSJed Brown 5393deea92deSShri .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 53941c3436cfSJed Brown @*/ 5395a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm) 53961c3436cfSJed Brown { 53978beabaa1SBarry Smith PetscErrorCode ierr; 53981c3436cfSJed Brown 53991c3436cfSJed Brown PetscFunctionBegin; 5400a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 5401a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNorm2(ts,U,Y,norm);CHKERRQ(ierr); 5402a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 5403a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm);CHKERRQ(ierr); 5404a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 54051c3436cfSJed Brown PetscFunctionReturn(0); 54061c3436cfSJed Brown } 54071c3436cfSJed Brown 54081c3436cfSJed Brown #undef __FUNCT__ 54098d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 54108d59e960SJed Brown /*@ 54118d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 54128d59e960SJed Brown 54138d59e960SJed Brown Logically Collective on TS 54148d59e960SJed Brown 54158d59e960SJed Brown Input Arguments: 54168d59e960SJed Brown + ts - time stepping context 54178d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 54188d59e960SJed Brown 54198d59e960SJed Brown Note: 54208d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 54218d59e960SJed Brown 54228d59e960SJed Brown Level: intermediate 54238d59e960SJed Brown 54248d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 54258d59e960SJed Brown @*/ 54268d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 54278d59e960SJed Brown { 54288d59e960SJed Brown PetscFunctionBegin; 54298d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 54308d59e960SJed Brown ts->cfltime_local = cfltime; 54318d59e960SJed Brown ts->cfltime = -1.; 54328d59e960SJed Brown PetscFunctionReturn(0); 54338d59e960SJed Brown } 54348d59e960SJed Brown 54358d59e960SJed Brown #undef __FUNCT__ 54368d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 54378d59e960SJed Brown /*@ 54388d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 54398d59e960SJed Brown 54408d59e960SJed Brown Collective on TS 54418d59e960SJed Brown 54428d59e960SJed Brown Input Arguments: 54438d59e960SJed Brown . ts - time stepping context 54448d59e960SJed Brown 54458d59e960SJed Brown Output Arguments: 54468d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 54478d59e960SJed Brown 54488d59e960SJed Brown Level: advanced 54498d59e960SJed Brown 54508d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 54518d59e960SJed Brown @*/ 54528d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 54538d59e960SJed Brown { 54548d59e960SJed Brown PetscErrorCode ierr; 54558d59e960SJed Brown 54568d59e960SJed Brown PetscFunctionBegin; 54578d59e960SJed Brown if (ts->cfltime < 0) { 5458ce94432eSBarry Smith ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 54598d59e960SJed Brown } 54608d59e960SJed Brown *cfltime = ts->cfltime; 54618d59e960SJed Brown PetscFunctionReturn(0); 54628d59e960SJed Brown } 54638d59e960SJed Brown 54648d59e960SJed Brown #undef __FUNCT__ 5465d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 5466d6ebe24aSShri Abhyankar /*@ 5467d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5468d6ebe24aSShri Abhyankar 5469d6ebe24aSShri Abhyankar Input Parameters: 5470d6ebe24aSShri Abhyankar . ts - the TS context. 5471d6ebe24aSShri Abhyankar . xl - lower bound. 5472d6ebe24aSShri Abhyankar . xu - upper bound. 5473d6ebe24aSShri Abhyankar 5474d6ebe24aSShri Abhyankar Notes: 5475d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 5476e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 5477d6ebe24aSShri Abhyankar 54782bd2b0e6SSatish Balay Level: advanced 54792bd2b0e6SSatish Balay 5480d6ebe24aSShri Abhyankar @*/ 5481d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5482d6ebe24aSShri Abhyankar { 5483d6ebe24aSShri Abhyankar PetscErrorCode ierr; 5484d6ebe24aSShri Abhyankar SNES snes; 5485d6ebe24aSShri Abhyankar 5486d6ebe24aSShri Abhyankar PetscFunctionBegin; 5487d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 5488d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 5489d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 5490d6ebe24aSShri Abhyankar } 5491d6ebe24aSShri Abhyankar 5492325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 5493c6db04a5SJed Brown #include <mex.h> 5494325fc9f4SBarry Smith 5495325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 5496325fc9f4SBarry Smith 5497325fc9f4SBarry Smith #undef __FUNCT__ 5498325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 5499325fc9f4SBarry Smith /* 5500325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 5501325fc9f4SBarry Smith TSSetFunctionMatlab(). 5502325fc9f4SBarry Smith 5503325fc9f4SBarry Smith Collective on TS 5504325fc9f4SBarry Smith 5505325fc9f4SBarry Smith Input Parameters: 5506325fc9f4SBarry Smith + snes - the TS context 55070910c330SBarry Smith - u - input vector 5508325fc9f4SBarry Smith 5509325fc9f4SBarry Smith Output Parameter: 5510325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 5511325fc9f4SBarry Smith 5512325fc9f4SBarry Smith Notes: 5513325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 5514325fc9f4SBarry Smith implementations, so most users would not generally call this routine 5515325fc9f4SBarry Smith themselves. 5516325fc9f4SBarry Smith 5517325fc9f4SBarry Smith Level: developer 5518325fc9f4SBarry Smith 5519325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5520325fc9f4SBarry Smith 5521325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5522325fc9f4SBarry Smith */ 55230910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 5524325fc9f4SBarry Smith { 5525325fc9f4SBarry Smith PetscErrorCode ierr; 5526325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5527325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 5528325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 5529325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 5530325fc9f4SBarry Smith 5531325fc9f4SBarry Smith PetscFunctionBegin; 5532325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 55330910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 55340910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 5535325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 55360910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 5537325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 5538325fc9f4SBarry Smith 5539325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 55400910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 55410910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 55420910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 5543bbd56ea5SKarl Rupp 5544325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5545325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 5546325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5547325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5548325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 5549325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 5550325fc9f4SBarry Smith prhs[6] = sctx->ctx; 5551325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 5552325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5553325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5554325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5555325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5556325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5557325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5558325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5559325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5560325fc9f4SBarry Smith PetscFunctionReturn(0); 5561325fc9f4SBarry Smith } 5562325fc9f4SBarry Smith 5563325fc9f4SBarry Smith 5564325fc9f4SBarry Smith #undef __FUNCT__ 5565325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 5566325fc9f4SBarry Smith /* 5567325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 5568325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 5569e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 5570325fc9f4SBarry Smith 5571325fc9f4SBarry Smith Logically Collective on TS 5572325fc9f4SBarry Smith 5573325fc9f4SBarry Smith Input Parameters: 5574325fc9f4SBarry Smith + ts - the TS context 5575325fc9f4SBarry Smith - func - function evaluation routine 5576325fc9f4SBarry Smith 5577325fc9f4SBarry Smith Calling sequence of func: 55780910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 5579325fc9f4SBarry Smith 5580325fc9f4SBarry Smith Level: beginner 5581325fc9f4SBarry Smith 5582325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5583325fc9f4SBarry Smith 5584325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5585325fc9f4SBarry Smith */ 5586cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 5587325fc9f4SBarry Smith { 5588325fc9f4SBarry Smith PetscErrorCode ierr; 5589325fc9f4SBarry Smith TSMatlabContext *sctx; 5590325fc9f4SBarry Smith 5591325fc9f4SBarry Smith PetscFunctionBegin; 5592325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5593325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5594325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5595325fc9f4SBarry Smith /* 5596325fc9f4SBarry Smith This should work, but it doesn't 5597325fc9f4SBarry Smith sctx->ctx = ctx; 5598325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5599325fc9f4SBarry Smith */ 5600325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5601bbd56ea5SKarl Rupp 56020298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 5603325fc9f4SBarry Smith PetscFunctionReturn(0); 5604325fc9f4SBarry Smith } 5605325fc9f4SBarry Smith 5606325fc9f4SBarry Smith #undef __FUNCT__ 5607325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 5608325fc9f4SBarry Smith /* 5609325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 5610325fc9f4SBarry Smith TSSetJacobianMatlab(). 5611325fc9f4SBarry Smith 5612325fc9f4SBarry Smith Collective on TS 5613325fc9f4SBarry Smith 5614325fc9f4SBarry Smith Input Parameters: 5615cdcf91faSSean Farley + ts - the TS context 56160910c330SBarry Smith . u - input vector 5617325fc9f4SBarry Smith . A, B - the matrices 5618325fc9f4SBarry Smith - ctx - user context 5619325fc9f4SBarry Smith 5620325fc9f4SBarry Smith Level: developer 5621325fc9f4SBarry Smith 5622325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5623325fc9f4SBarry Smith 5624325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5625325fc9f4SBarry Smith @*/ 5626f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 5627325fc9f4SBarry Smith { 5628325fc9f4SBarry Smith PetscErrorCode ierr; 5629325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5630325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 5631325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 5632325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 5633325fc9f4SBarry Smith 5634325fc9f4SBarry Smith PetscFunctionBegin; 5635cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 56360910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 5637325fc9f4SBarry Smith 56380910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 5639325fc9f4SBarry Smith 5640cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 56410910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 56420910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 56430910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 56440910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 5645bbd56ea5SKarl Rupp 5646325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5647325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 5648325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5649325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5650325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 5651325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 5652325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 5653325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 5654325fc9f4SBarry Smith prhs[8] = sctx->ctx; 5655325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 5656325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5657325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5658325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5659325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5660325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5661325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5662325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5663325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 5664325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 5665325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5666325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 5667325fc9f4SBarry Smith PetscFunctionReturn(0); 5668325fc9f4SBarry Smith } 5669325fc9f4SBarry Smith 5670325fc9f4SBarry Smith 5671325fc9f4SBarry Smith #undef __FUNCT__ 5672325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 5673325fc9f4SBarry Smith /* 5674325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 5675e3c5b3baSBarry 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 5676325fc9f4SBarry Smith 5677325fc9f4SBarry Smith Logically Collective on TS 5678325fc9f4SBarry Smith 5679325fc9f4SBarry Smith Input Parameters: 5680cdcf91faSSean Farley + ts - the TS context 5681325fc9f4SBarry Smith . A,B - Jacobian matrices 5682325fc9f4SBarry Smith . func - function evaluation routine 5683325fc9f4SBarry Smith - ctx - user context 5684325fc9f4SBarry Smith 5685325fc9f4SBarry Smith Calling sequence of func: 56860910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 5687325fc9f4SBarry Smith 5688325fc9f4SBarry Smith 5689325fc9f4SBarry Smith Level: developer 5690325fc9f4SBarry Smith 5691325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5692325fc9f4SBarry Smith 5693325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5694325fc9f4SBarry Smith */ 5695cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 5696325fc9f4SBarry Smith { 5697325fc9f4SBarry Smith PetscErrorCode ierr; 5698325fc9f4SBarry Smith TSMatlabContext *sctx; 5699325fc9f4SBarry Smith 5700325fc9f4SBarry Smith PetscFunctionBegin; 5701325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5702325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5703325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5704325fc9f4SBarry Smith /* 5705325fc9f4SBarry Smith This should work, but it doesn't 5706325fc9f4SBarry Smith sctx->ctx = ctx; 5707325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5708325fc9f4SBarry Smith */ 5709325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5710bbd56ea5SKarl Rupp 5711cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 5712325fc9f4SBarry Smith PetscFunctionReturn(0); 5713325fc9f4SBarry Smith } 5714325fc9f4SBarry Smith 5715b5b1a830SBarry Smith #undef __FUNCT__ 5716b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 5717b5b1a830SBarry Smith /* 5718b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 5719b5b1a830SBarry Smith 5720b5b1a830SBarry Smith Collective on TS 5721b5b1a830SBarry Smith 5722b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5723b5b1a830SBarry Smith @*/ 57240910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 5725b5b1a830SBarry Smith { 5726b5b1a830SBarry Smith PetscErrorCode ierr; 5727b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5728a530c242SBarry Smith int nlhs = 1,nrhs = 6; 5729b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 5730b5b1a830SBarry Smith long long int lx = 0,ls = 0; 5731b5b1a830SBarry Smith 5732b5b1a830SBarry Smith PetscFunctionBegin; 5733cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 57340910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 5735b5b1a830SBarry Smith 5736cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 57370910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 5738bbd56ea5SKarl Rupp 5739b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5740b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 5741b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 5742b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 5743b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 5744b5b1a830SBarry Smith prhs[5] = sctx->ctx; 5745b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 5746b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5747b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 5748b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 5749b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 5750b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 5751b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 5752b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 5753b5b1a830SBarry Smith PetscFunctionReturn(0); 5754b5b1a830SBarry Smith } 5755b5b1a830SBarry Smith 5756b5b1a830SBarry Smith 5757b5b1a830SBarry Smith #undef __FUNCT__ 5758b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 5759b5b1a830SBarry Smith /* 5760b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 5761b5b1a830SBarry Smith 5762b5b1a830SBarry Smith Level: developer 5763b5b1a830SBarry Smith 5764b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 5765b5b1a830SBarry Smith 5766b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5767b5b1a830SBarry Smith */ 5768cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 5769b5b1a830SBarry Smith { 5770b5b1a830SBarry Smith PetscErrorCode ierr; 5771b5b1a830SBarry Smith TSMatlabContext *sctx; 5772b5b1a830SBarry Smith 5773b5b1a830SBarry Smith PetscFunctionBegin; 5774b5b1a830SBarry Smith /* currently sctx is memory bleed */ 5775b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5776b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5777b5b1a830SBarry Smith /* 5778b5b1a830SBarry Smith This should work, but it doesn't 5779b5b1a830SBarry Smith sctx->ctx = ctx; 5780b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5781b5b1a830SBarry Smith */ 5782b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5783bbd56ea5SKarl Rupp 57840298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 5785b5b1a830SBarry Smith PetscFunctionReturn(0); 5786b5b1a830SBarry Smith } 5787325fc9f4SBarry Smith #endif 5788b3603a34SBarry Smith 5789b3603a34SBarry Smith #undef __FUNCT__ 57904f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 5791b3603a34SBarry Smith /*@C 57924f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 5793b3603a34SBarry Smith in a time based line graph 5794b3603a34SBarry Smith 5795b3603a34SBarry Smith Collective on TS 5796b3603a34SBarry Smith 5797b3603a34SBarry Smith Input Parameters: 5798b3603a34SBarry Smith + ts - the TS context 5799b3603a34SBarry Smith . step - current time-step 5800b3603a34SBarry Smith . ptime - current time 58017db568b7SBarry Smith . u - current solution 58027db568b7SBarry Smith - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 5803b3603a34SBarry Smith 5804b3d3934dSBarry Smith Options Database: 58059ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 5806b3d3934dSBarry Smith 5807b3603a34SBarry Smith Level: intermediate 5808b3603a34SBarry Smith 58090b039ecaSBarry Smith Notes: each process in a parallel run displays its component solutions in a separate window 5810b3603a34SBarry Smith 5811b3603a34SBarry Smith .keywords: TS, vector, monitor, view 5812b3603a34SBarry Smith 58137db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 58147db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 58157db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 58167db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 5817b3603a34SBarry Smith @*/ 58187db568b7SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 5819b3603a34SBarry Smith { 5820b3603a34SBarry Smith PetscErrorCode ierr; 58217db568b7SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 5822b3603a34SBarry Smith const PetscScalar *yy; 582358ff32f7SBarry Smith PetscInt dim; 582480666b62SBarry Smith Vec v; 5825b3603a34SBarry Smith 5826b3603a34SBarry Smith PetscFunctionBegin; 582758ff32f7SBarry Smith if (!step) { 5828a9f9c1f6SBarry Smith PetscDrawAxis axis; 5829a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5830a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 5831387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 5832387f4636SBarry Smith char **displaynames; 5833387f4636SBarry Smith PetscBool flg; 5834387f4636SBarry Smith 5835387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 5836387f4636SBarry Smith ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr); 5837387f4636SBarry Smith ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 58389ae14b6eSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 5839387f4636SBarry Smith if (flg) { 5840a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 5841387f4636SBarry Smith } 5842387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 5843387f4636SBarry Smith } 5844387f4636SBarry Smith if (ctx->displaynames) { 5845387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 5846387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 5847387f4636SBarry Smith } else if (ctx->names) { 58480910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 58490b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 5850387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 5851387f4636SBarry Smith } 58520b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 585358ff32f7SBarry Smith } 585480666b62SBarry Smith if (ctx->transform) { 5855e673d494SBarry Smith ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr); 585680666b62SBarry Smith } else { 585780666b62SBarry Smith v = u; 585880666b62SBarry Smith } 585980666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 5860e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 5861e3efe391SJed Brown { 5862e3efe391SJed Brown PetscReal *yreal; 5863e3efe391SJed Brown PetscInt i,n; 586480666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 5865785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 5866e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 58670b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 5868e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 5869e3efe391SJed Brown } 5870e3efe391SJed Brown #else 5871387f4636SBarry Smith if (ctx->displaynames) { 5872387f4636SBarry Smith PetscInt i; 5873387f4636SBarry Smith for (i=0; i<ctx->ndisplayvariables; i++) { 5874387f4636SBarry Smith ctx->displayvalues[i] = yy[ctx->displayvariables[i]]; 5875387f4636SBarry Smith } 5876387f4636SBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 5877387f4636SBarry Smith } else { 58780b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 5879387f4636SBarry Smith } 5880e3efe391SJed Brown #endif 588180666b62SBarry Smith ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 588280666b62SBarry Smith if (ctx->transform) { 588380666b62SBarry Smith ierr = VecDestroy(&v);CHKERRQ(ierr); 588480666b62SBarry Smith } 5885b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 58860b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 58873923b477SBarry Smith } 5888b3603a34SBarry Smith PetscFunctionReturn(0); 5889b3603a34SBarry Smith } 5890b3603a34SBarry Smith 5891387f4636SBarry Smith 5892b3603a34SBarry Smith #undef __FUNCT__ 589331152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames" 5894b037adc7SBarry Smith /*@C 589531152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 5896b037adc7SBarry Smith 5897b037adc7SBarry Smith Collective on TS 5898b037adc7SBarry Smith 5899b037adc7SBarry Smith Input Parameters: 5900b037adc7SBarry Smith + ts - the TS context 5901b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 5902b037adc7SBarry Smith 5903b037adc7SBarry Smith Level: intermediate 5904b037adc7SBarry Smith 59057db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 59067db568b7SBarry Smith 5907b037adc7SBarry Smith .keywords: TS, vector, monitor, view 5908b037adc7SBarry Smith 5909a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 5910b037adc7SBarry Smith @*/ 591131152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 5912b037adc7SBarry Smith { 5913b037adc7SBarry Smith PetscErrorCode ierr; 5914b037adc7SBarry Smith PetscInt i; 5915b037adc7SBarry Smith 5916b037adc7SBarry Smith PetscFunctionBegin; 5917b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 5918b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 59195537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 5920b3d3934dSBarry Smith break; 5921b3d3934dSBarry Smith } 5922b3d3934dSBarry Smith } 5923b3d3934dSBarry Smith PetscFunctionReturn(0); 5924b3d3934dSBarry Smith } 5925b3d3934dSBarry Smith 5926b3d3934dSBarry Smith #undef __FUNCT__ 5927e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames" 5928e673d494SBarry Smith /*@C 5929e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 5930e673d494SBarry Smith 5931e673d494SBarry Smith Collective on TS 5932e673d494SBarry Smith 5933e673d494SBarry Smith Input Parameters: 5934e673d494SBarry Smith + ts - the TS context 5935e673d494SBarry Smith - names - the names of the components, final string must be NULL 5936e673d494SBarry Smith 5937e673d494SBarry Smith Level: intermediate 5938e673d494SBarry Smith 5939e673d494SBarry Smith .keywords: TS, vector, monitor, view 5940e673d494SBarry Smith 5941a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 5942e673d494SBarry Smith @*/ 5943e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 5944e673d494SBarry Smith { 5945e673d494SBarry Smith PetscErrorCode ierr; 5946e673d494SBarry Smith 5947e673d494SBarry Smith PetscFunctionBegin; 5948e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 5949e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 5950e673d494SBarry Smith PetscFunctionReturn(0); 5951e673d494SBarry Smith } 5952e673d494SBarry Smith 5953e673d494SBarry Smith #undef __FUNCT__ 5954b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames" 5955b3d3934dSBarry Smith /*@C 5956b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 5957b3d3934dSBarry Smith 5958b3d3934dSBarry Smith Collective on TS 5959b3d3934dSBarry Smith 5960b3d3934dSBarry Smith Input Parameter: 5961b3d3934dSBarry Smith . ts - the TS context 5962b3d3934dSBarry Smith 5963b3d3934dSBarry Smith Output Parameter: 5964b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 5965b3d3934dSBarry Smith 5966b3d3934dSBarry Smith Level: intermediate 5967b3d3934dSBarry Smith 59687db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 59697db568b7SBarry Smith 5970b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 5971b3d3934dSBarry Smith 5972b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 5973b3d3934dSBarry Smith @*/ 5974b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 5975b3d3934dSBarry Smith { 5976b3d3934dSBarry Smith PetscInt i; 5977b3d3934dSBarry Smith 5978b3d3934dSBarry Smith PetscFunctionBegin; 5979b3d3934dSBarry Smith *names = NULL; 5980b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 5981b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 59825537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 5983b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 5984b3d3934dSBarry Smith break; 5985387f4636SBarry Smith } 5986387f4636SBarry Smith } 5987387f4636SBarry Smith PetscFunctionReturn(0); 5988387f4636SBarry Smith } 5989387f4636SBarry Smith 5990387f4636SBarry Smith #undef __FUNCT__ 5991a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables" 5992a66092f1SBarry Smith /*@C 5993a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 5994a66092f1SBarry Smith 5995a66092f1SBarry Smith Collective on TS 5996a66092f1SBarry Smith 5997a66092f1SBarry Smith Input Parameters: 5998a66092f1SBarry Smith + ctx - the TSMonitorLG context 5999a66092f1SBarry Smith . displaynames - the names of the components, final string must be NULL 6000a66092f1SBarry Smith 6001a66092f1SBarry Smith Level: intermediate 6002a66092f1SBarry Smith 6003a66092f1SBarry Smith .keywords: TS, vector, monitor, view 6004a66092f1SBarry Smith 6005a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6006a66092f1SBarry Smith @*/ 6007a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 6008a66092f1SBarry Smith { 6009a66092f1SBarry Smith PetscInt j = 0,k; 6010a66092f1SBarry Smith PetscErrorCode ierr; 6011a66092f1SBarry Smith 6012a66092f1SBarry Smith PetscFunctionBegin; 6013a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 6014a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 6015a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 6016a66092f1SBarry Smith while (displaynames[j]) j++; 6017a66092f1SBarry Smith ctx->ndisplayvariables = j; 6018a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 6019a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 6020a66092f1SBarry Smith j = 0; 6021a66092f1SBarry Smith while (displaynames[j]) { 6022a66092f1SBarry Smith k = 0; 6023a66092f1SBarry Smith while (ctx->names[k]) { 6024a66092f1SBarry Smith PetscBool flg; 6025a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 6026a66092f1SBarry Smith if (flg) { 6027a66092f1SBarry Smith ctx->displayvariables[j] = k; 6028a66092f1SBarry Smith break; 6029a66092f1SBarry Smith } 6030a66092f1SBarry Smith k++; 6031a66092f1SBarry Smith } 6032a66092f1SBarry Smith j++; 6033a66092f1SBarry Smith } 6034a66092f1SBarry Smith PetscFunctionReturn(0); 6035a66092f1SBarry Smith } 6036a66092f1SBarry Smith 6037a66092f1SBarry Smith 6038a66092f1SBarry Smith #undef __FUNCT__ 6039387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables" 6040387f4636SBarry Smith /*@C 6041387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 6042387f4636SBarry Smith 6043387f4636SBarry Smith Collective on TS 6044387f4636SBarry Smith 6045387f4636SBarry Smith Input Parameters: 6046387f4636SBarry Smith + ts - the TS context 6047387f4636SBarry Smith . displaynames - the names of the components, final string must be NULL 6048387f4636SBarry Smith 60497db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 60507db568b7SBarry Smith 6051387f4636SBarry Smith Level: intermediate 6052387f4636SBarry Smith 6053387f4636SBarry Smith .keywords: TS, vector, monitor, view 6054387f4636SBarry Smith 6055387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6056387f4636SBarry Smith @*/ 6057387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 6058387f4636SBarry Smith { 6059a66092f1SBarry Smith PetscInt i; 6060387f4636SBarry Smith PetscErrorCode ierr; 6061387f4636SBarry Smith 6062387f4636SBarry Smith PetscFunctionBegin; 6063387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6064387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 60655537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 6066b3d3934dSBarry Smith break; 6067b037adc7SBarry Smith } 6068b037adc7SBarry Smith } 6069b037adc7SBarry Smith PetscFunctionReturn(0); 6070b037adc7SBarry Smith } 6071b037adc7SBarry Smith 6072b037adc7SBarry Smith #undef __FUNCT__ 607380666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform" 607480666b62SBarry Smith /*@C 607580666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 607680666b62SBarry Smith 607780666b62SBarry Smith Collective on TS 607880666b62SBarry Smith 607980666b62SBarry Smith Input Parameters: 608080666b62SBarry Smith + ts - the TS context 608180666b62SBarry Smith . transform - the transform function 60827684fa3eSBarry Smith . destroy - function to destroy the optional context 608380666b62SBarry Smith - ctx - optional context used by transform function 608480666b62SBarry Smith 60857db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 60867db568b7SBarry Smith 608780666b62SBarry Smith Level: intermediate 608880666b62SBarry Smith 608980666b62SBarry Smith .keywords: TS, vector, monitor, view 609080666b62SBarry Smith 6091a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 609280666b62SBarry Smith @*/ 60937684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 609480666b62SBarry Smith { 609580666b62SBarry Smith PetscInt i; 6096a66092f1SBarry Smith PetscErrorCode ierr; 609780666b62SBarry Smith 609880666b62SBarry Smith PetscFunctionBegin; 609980666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 610080666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 61015537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 610280666b62SBarry Smith } 610380666b62SBarry Smith } 610480666b62SBarry Smith PetscFunctionReturn(0); 610580666b62SBarry Smith } 610680666b62SBarry Smith 610780666b62SBarry Smith #undef __FUNCT__ 6108e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform" 6109e673d494SBarry Smith /*@C 6110e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 6111e673d494SBarry Smith 6112e673d494SBarry Smith Collective on TSLGCtx 6113e673d494SBarry Smith 6114e673d494SBarry Smith Input Parameters: 6115e673d494SBarry Smith + ts - the TS context 6116e673d494SBarry Smith . transform - the transform function 61177684fa3eSBarry Smith . destroy - function to destroy the optional context 6118e673d494SBarry Smith - ctx - optional context used by transform function 6119e673d494SBarry Smith 6120e673d494SBarry Smith Level: intermediate 6121e673d494SBarry Smith 6122e673d494SBarry Smith .keywords: TS, vector, monitor, view 6123e673d494SBarry Smith 6124a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 6125e673d494SBarry Smith @*/ 61267684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 6127e673d494SBarry Smith { 6128e673d494SBarry Smith PetscFunctionBegin; 6129e673d494SBarry Smith ctx->transform = transform; 61307684fa3eSBarry Smith ctx->transformdestroy = destroy; 6131e673d494SBarry Smith ctx->transformctx = tctx; 6132e673d494SBarry Smith PetscFunctionReturn(0); 6133e673d494SBarry Smith } 6134e673d494SBarry Smith 6135b3603a34SBarry Smith #undef __FUNCT__ 61364f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 6137ef20d060SBarry Smith /*@C 61384f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 6139ef20d060SBarry Smith in a time based line graph 6140ef20d060SBarry Smith 6141ef20d060SBarry Smith Collective on TS 6142ef20d060SBarry Smith 6143ef20d060SBarry Smith Input Parameters: 6144ef20d060SBarry Smith + ts - the TS context 6145ef20d060SBarry Smith . step - current time-step 6146ef20d060SBarry Smith . ptime - current time 61477db568b7SBarry Smith . u - current solution 61487db568b7SBarry Smith - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 6149ef20d060SBarry Smith 6150ef20d060SBarry Smith Level: intermediate 6151ef20d060SBarry Smith 6152abd5a294SJed Brown Notes: 6153abd5a294SJed Brown Only for sequential solves. 6154abd5a294SJed Brown 6155abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 6156abd5a294SJed Brown 6157abd5a294SJed Brown Options Database Keys: 61584f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 6159ef20d060SBarry Smith 6160ef20d060SBarry Smith .keywords: TS, vector, monitor, view 6161ef20d060SBarry Smith 6162abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 6163ef20d060SBarry Smith @*/ 61640910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 6165ef20d060SBarry Smith { 6166ef20d060SBarry Smith PetscErrorCode ierr; 61670b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 6168ef20d060SBarry Smith const PetscScalar *yy; 6169ef20d060SBarry Smith Vec y; 6170a9f9c1f6SBarry Smith PetscInt dim; 6171ef20d060SBarry Smith 6172ef20d060SBarry Smith PetscFunctionBegin; 6173a9f9c1f6SBarry Smith if (!step) { 6174a9f9c1f6SBarry Smith PetscDrawAxis axis; 6175a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 61761ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 61770910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6178a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6179a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6180a9f9c1f6SBarry Smith } 61810910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 6182ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 61830910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 6184ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 6185e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6186e3efe391SJed Brown { 6187e3efe391SJed Brown PetscReal *yreal; 6188e3efe391SJed Brown PetscInt i,n; 6189e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 6190785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6191e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 61920b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6193e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6194e3efe391SJed Brown } 6195e3efe391SJed Brown #else 61960b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6197e3efe391SJed Brown #endif 6198ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 6199ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 6200b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 62010b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 62023923b477SBarry Smith } 6203ef20d060SBarry Smith PetscFunctionReturn(0); 6204ef20d060SBarry Smith } 6205ef20d060SBarry Smith 6206201da799SBarry Smith #undef __FUNCT__ 6207201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 6208201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6209201da799SBarry Smith { 6210201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6211201da799SBarry Smith PetscReal x = ptime,y; 6212201da799SBarry Smith PetscErrorCode ierr; 6213201da799SBarry Smith PetscInt its; 6214201da799SBarry Smith 6215201da799SBarry Smith PetscFunctionBegin; 6216201da799SBarry Smith if (!n) { 6217201da799SBarry Smith PetscDrawAxis axis; 6218bbd56ea5SKarl Rupp 6219201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6220201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 6221201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6222bbd56ea5SKarl Rupp 6223201da799SBarry Smith ctx->snes_its = 0; 6224201da799SBarry Smith } 6225201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 6226201da799SBarry Smith y = its - ctx->snes_its; 6227201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 62283fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6229201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 6230201da799SBarry Smith } 6231201da799SBarry Smith ctx->snes_its = its; 6232201da799SBarry Smith PetscFunctionReturn(0); 6233201da799SBarry Smith } 6234201da799SBarry Smith 6235201da799SBarry Smith #undef __FUNCT__ 6236201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 6237201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6238201da799SBarry Smith { 6239201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6240201da799SBarry Smith PetscReal x = ptime,y; 6241201da799SBarry Smith PetscErrorCode ierr; 6242201da799SBarry Smith PetscInt its; 6243201da799SBarry Smith 6244201da799SBarry Smith PetscFunctionBegin; 6245201da799SBarry Smith if (!n) { 6246201da799SBarry Smith PetscDrawAxis axis; 6247bbd56ea5SKarl Rupp 6248201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6249201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 6250201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6251bbd56ea5SKarl Rupp 6252201da799SBarry Smith ctx->ksp_its = 0; 6253201da799SBarry Smith } 6254201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 6255201da799SBarry Smith y = its - ctx->ksp_its; 6256201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 625799fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6258201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 6259201da799SBarry Smith } 6260201da799SBarry Smith ctx->ksp_its = its; 6261201da799SBarry Smith PetscFunctionReturn(0); 6262201da799SBarry Smith } 6263f9c1d6abSBarry Smith 6264f9c1d6abSBarry Smith #undef __FUNCT__ 6265f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 6266f9c1d6abSBarry Smith /*@ 6267f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 6268f9c1d6abSBarry Smith 6269f9c1d6abSBarry Smith Collective on TS and Vec 6270f9c1d6abSBarry Smith 6271f9c1d6abSBarry Smith Input Parameters: 6272f9c1d6abSBarry Smith + ts - the TS context 6273f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 6274f9c1d6abSBarry Smith 6275f9c1d6abSBarry Smith Output Parameters: 6276f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 6277f9c1d6abSBarry Smith 6278f9c1d6abSBarry Smith Level: developer 6279f9c1d6abSBarry Smith 6280f9c1d6abSBarry Smith .keywords: TS, compute 6281f9c1d6abSBarry Smith 6282f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 6283f9c1d6abSBarry Smith @*/ 6284f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 6285f9c1d6abSBarry Smith { 6286f9c1d6abSBarry Smith PetscErrorCode ierr; 6287f9c1d6abSBarry Smith 6288f9c1d6abSBarry Smith PetscFunctionBegin; 6289f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6290ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 6291f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 6292f9c1d6abSBarry Smith PetscFunctionReturn(0); 6293f9c1d6abSBarry Smith } 629424655328SShri 6295b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 6296b3d3934dSBarry Smith #undef __FUNCT__ 6297b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate" 6298b3d3934dSBarry Smith /*@C 6299b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 6300b3d3934dSBarry Smith 6301b3d3934dSBarry Smith Collective on TS 6302b3d3934dSBarry Smith 6303b3d3934dSBarry Smith Input Parameters: 6304b3d3934dSBarry Smith . ts - the ODE solver object 6305b3d3934dSBarry Smith 6306b3d3934dSBarry Smith Output Parameter: 6307b3d3934dSBarry Smith . ctx - the context 6308b3d3934dSBarry Smith 6309b3d3934dSBarry Smith Level: intermediate 6310b3d3934dSBarry Smith 6311b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso 6312b3d3934dSBarry Smith 6313b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 6314b3d3934dSBarry Smith 6315b3d3934dSBarry Smith @*/ 6316b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 6317b3d3934dSBarry Smith { 6318b3d3934dSBarry Smith PetscErrorCode ierr; 6319b3d3934dSBarry Smith 6320b3d3934dSBarry Smith PetscFunctionBegin; 6321a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 6322b3d3934dSBarry Smith PetscFunctionReturn(0); 6323b3d3934dSBarry Smith } 6324b3d3934dSBarry Smith 6325b3d3934dSBarry Smith #undef __FUNCT__ 6326b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope" 6327b3d3934dSBarry Smith /*@C 6328b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 6329b3d3934dSBarry Smith 6330b3d3934dSBarry Smith Collective on TS 6331b3d3934dSBarry Smith 6332b3d3934dSBarry Smith Input Parameters: 6333b3d3934dSBarry Smith + ts - the TS context 6334b3d3934dSBarry Smith . step - current time-step 6335b3d3934dSBarry Smith . ptime - current time 63367db568b7SBarry Smith . u - current solution 63377db568b7SBarry Smith - dctx - the envelope context 6338b3d3934dSBarry Smith 6339b3d3934dSBarry Smith Options Database: 6340b3d3934dSBarry Smith . -ts_monitor_envelope 6341b3d3934dSBarry Smith 6342b3d3934dSBarry Smith Level: intermediate 6343b3d3934dSBarry Smith 6344b3d3934dSBarry Smith Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 6345b3d3934dSBarry Smith 6346b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6347b3d3934dSBarry Smith 63487db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 6349b3d3934dSBarry Smith @*/ 63507db568b7SBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6351b3d3934dSBarry Smith { 6352b3d3934dSBarry Smith PetscErrorCode ierr; 63537db568b7SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 6354b3d3934dSBarry Smith 6355b3d3934dSBarry Smith PetscFunctionBegin; 6356b3d3934dSBarry Smith if (!ctx->max) { 6357b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 6358b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 6359b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 6360b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 6361b3d3934dSBarry Smith } else { 6362b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 6363b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 6364b3d3934dSBarry Smith } 6365b3d3934dSBarry Smith PetscFunctionReturn(0); 6366b3d3934dSBarry Smith } 6367b3d3934dSBarry Smith 6368b3d3934dSBarry Smith 6369b3d3934dSBarry Smith #undef __FUNCT__ 6370b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds" 6371b3d3934dSBarry Smith /*@C 6372b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 6373b3d3934dSBarry Smith 6374b3d3934dSBarry Smith Collective on TS 6375b3d3934dSBarry Smith 6376b3d3934dSBarry Smith Input Parameter: 6377b3d3934dSBarry Smith . ts - the TS context 6378b3d3934dSBarry Smith 6379b3d3934dSBarry Smith Output Parameter: 6380b3d3934dSBarry Smith + max - the maximum values 6381b3d3934dSBarry Smith - min - the minimum values 6382b3d3934dSBarry Smith 63837db568b7SBarry Smith Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 63847db568b7SBarry Smith 6385b3d3934dSBarry Smith Level: intermediate 6386b3d3934dSBarry Smith 6387b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6388b3d3934dSBarry Smith 6389b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6390b3d3934dSBarry Smith @*/ 6391b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 6392b3d3934dSBarry Smith { 6393b3d3934dSBarry Smith PetscInt i; 6394b3d3934dSBarry Smith 6395b3d3934dSBarry Smith PetscFunctionBegin; 6396b3d3934dSBarry Smith if (max) *max = NULL; 6397b3d3934dSBarry Smith if (min) *min = NULL; 6398b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6399b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 64005537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 6401b3d3934dSBarry Smith if (max) *max = ctx->max; 6402b3d3934dSBarry Smith if (min) *min = ctx->min; 6403b3d3934dSBarry Smith break; 6404b3d3934dSBarry Smith } 6405b3d3934dSBarry Smith } 6406b3d3934dSBarry Smith PetscFunctionReturn(0); 6407b3d3934dSBarry Smith } 6408b3d3934dSBarry Smith 6409b3d3934dSBarry Smith #undef __FUNCT__ 6410b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy" 6411b3d3934dSBarry Smith /*@C 6412b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 6413b3d3934dSBarry Smith 6414b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 6415b3d3934dSBarry Smith 6416b3d3934dSBarry Smith Input Parameter: 6417b3d3934dSBarry Smith . ctx - the monitor context 6418b3d3934dSBarry Smith 6419b3d3934dSBarry Smith Level: intermediate 6420b3d3934dSBarry Smith 6421b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy 6422b3d3934dSBarry Smith 64237db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 6424b3d3934dSBarry Smith @*/ 6425b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 6426b3d3934dSBarry Smith { 6427b3d3934dSBarry Smith PetscErrorCode ierr; 6428b3d3934dSBarry Smith 6429b3d3934dSBarry Smith PetscFunctionBegin; 6430b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 6431b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 6432b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 6433b3d3934dSBarry Smith PetscFunctionReturn(0); 6434b3d3934dSBarry Smith } 6435f2dee214SBarry Smith 643624655328SShri #undef __FUNCT__ 643724655328SShri #define __FUNCT__ "TSRollBack" 643824655328SShri /*@ 643924655328SShri TSRollBack - Rolls back one time step 644024655328SShri 644124655328SShri Collective on TS 644224655328SShri 644324655328SShri Input Parameter: 644424655328SShri . ts - the TS context obtained from TSCreate() 644524655328SShri 644624655328SShri Level: advanced 644724655328SShri 644824655328SShri .keywords: TS, timestep, rollback 644924655328SShri 645024655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 645124655328SShri @*/ 645224655328SShri PetscErrorCode TSRollBack(TS ts) 645324655328SShri { 645424655328SShri PetscErrorCode ierr; 645524655328SShri 645624655328SShri PetscFunctionBegin; 645724655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 645824655328SShri 645924655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 646024655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 646124655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 646224655328SShri ts->ptime = ts->ptime_prev; 64638392e04aSShri Abhyankar ts->steprollback = PETSC_TRUE; /* Flag to indicate that the step is rollbacked */ 646424655328SShri PetscFunctionReturn(0); 646524655328SShri } 6466aeb4809dSShri Abhyankar 6467ff22ae23SHong Zhang #undef __FUNCT__ 6468ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages" 6469ff22ae23SHong Zhang /*@ 6470ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 6471ff22ae23SHong Zhang 6472ff22ae23SHong Zhang Input Parameter: 6473ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 6474ff22ae23SHong Zhang 6475ff22ae23SHong Zhang Level: advanced 6476ff22ae23SHong Zhang 6477ff22ae23SHong Zhang .keywords: TS, getstages 6478ff22ae23SHong Zhang 6479ff22ae23SHong Zhang .seealso: TSCreate() 6480ff22ae23SHong Zhang @*/ 6481ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns, Vec **Y) 6482ff22ae23SHong Zhang { 6483ff22ae23SHong Zhang PetscErrorCode ierr; 6484ff22ae23SHong Zhang 6485ff22ae23SHong Zhang PetscFunctionBegin; 6486ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 6487ff22ae23SHong Zhang PetscValidPointer(ns,2); 6488ff22ae23SHong Zhang 6489ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 6490ff22ae23SHong Zhang else { 6491ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 6492ff22ae23SHong Zhang } 6493ff22ae23SHong Zhang PetscFunctionReturn(0); 6494ff22ae23SHong Zhang } 6495ff22ae23SHong Zhang 6496847ff0e1SMatthew G. Knepley #undef __FUNCT__ 6497847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor" 6498847ff0e1SMatthew G. Knepley /*@C 6499847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 6500847ff0e1SMatthew G. Knepley 6501847ff0e1SMatthew G. Knepley Collective on SNES 6502847ff0e1SMatthew G. Knepley 6503847ff0e1SMatthew G. Knepley Input Parameters: 6504847ff0e1SMatthew G. Knepley + ts - the TS context 6505847ff0e1SMatthew G. Knepley . t - current timestep 6506847ff0e1SMatthew G. Knepley . U - state vector 6507847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 6508847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 6509847ff0e1SMatthew G. Knepley - ctx - an optional user context 6510847ff0e1SMatthew G. Knepley 6511847ff0e1SMatthew G. Knepley Output Parameters: 6512847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 6513847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 6514847ff0e1SMatthew G. Knepley 6515847ff0e1SMatthew G. Knepley Level: intermediate 6516847ff0e1SMatthew G. Knepley 6517847ff0e1SMatthew G. Knepley Notes: 6518847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 6519847ff0e1SMatthew G. Knepley 6520847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 6521847ff0e1SMatthew G. Knepley 6522847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 6523847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 6524847ff0e1SMatthew G. Knepley 6525847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 6526847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 6527847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 6528847ff0e1SMatthew G. Knepley 6529847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse 6530847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 6531847ff0e1SMatthew G. Knepley @*/ 6532847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 6533847ff0e1SMatthew G. Knepley { 6534847ff0e1SMatthew G. Knepley SNES snes; 6535847ff0e1SMatthew G. Knepley MatFDColoring color; 6536847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 6537847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 6538847ff0e1SMatthew G. Knepley 6539847ff0e1SMatthew G. Knepley PetscFunctionBegin; 6540847ff0e1SMatthew G. Knepley ierr = PetscOptionsGetBool(((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 6541847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 6542847ff0e1SMatthew G. Knepley if (!color) { 6543847ff0e1SMatthew G. Knepley DM dm; 6544847ff0e1SMatthew G. Knepley ISColoring iscoloring; 6545847ff0e1SMatthew G. Knepley 6546847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 6547847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 6548847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 6549847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 6550847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 6551847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 6552847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 6553847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 6554847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 6555847ff0e1SMatthew G. Knepley } else { 6556847ff0e1SMatthew G. Knepley MatColoring mc; 6557847ff0e1SMatthew G. Knepley 6558847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 6559847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 6560847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 6561847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 6562847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 6563847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 6564847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 6565847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 6566847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 6567847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 6568847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 6569847ff0e1SMatthew G. Knepley } 6570847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 6571847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 6572847ff0e1SMatthew G. Knepley } 6573847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 6574847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 6575847ff0e1SMatthew G. Knepley if (J != B) { 6576847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 6577847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 6578847ff0e1SMatthew G. Knepley } 6579847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 6580847ff0e1SMatthew G. Knepley } 658193b34091SDebojyoti Ghosh 658293b34091SDebojyoti Ghosh #undef __FUNCT__ 6583e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone" 658493b34091SDebojyoti Ghosh /*@C 6585e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 658693b34091SDebojyoti Ghosh 658793b34091SDebojyoti Ghosh Collective on MPI_Comm 658893b34091SDebojyoti Ghosh 658993b34091SDebojyoti Ghosh Input Parameter: 659093b34091SDebojyoti Ghosh . tsin - The input TS 659193b34091SDebojyoti Ghosh 659293b34091SDebojyoti Ghosh Output Parameter: 6593e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 659493b34091SDebojyoti Ghosh 65955eca1a21SEmil Constantinescu Notes: 65965eca1a21SEmil Constantinescu This function is used to create a clone of a TS object. It is used in ARKIMEX for initializing the slope for first stage explicit methods. It will likely be replaced in the future with a mechanism of switching methods on the fly. 65975eca1a21SEmil Constantinescu 6598baa10174SEmil Constantinescu When using TSDestroy() on a clone the user has to first reset the correct TS reference in the embedded SNES object: e.g.: by running SNES snes_dup=NULL; TSGetSNES(ts,&snes_dup); ierr = TSSetSNES(ts,snes_dup); 65995eca1a21SEmil Constantinescu 66005eca1a21SEmil Constantinescu Level: developer 660193b34091SDebojyoti Ghosh 6602e5168f73SEmil Constantinescu .keywords: TS, clone 6603e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 660493b34091SDebojyoti Ghosh @*/ 6605baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 660693b34091SDebojyoti Ghosh { 660793b34091SDebojyoti Ghosh TS t; 660893b34091SDebojyoti Ghosh PetscErrorCode ierr; 6609dc846ba4SSatish Balay SNES snes_start; 6610dc846ba4SSatish Balay DM dm; 6611dc846ba4SSatish Balay TSType type; 661293b34091SDebojyoti Ghosh 661393b34091SDebojyoti Ghosh PetscFunctionBegin; 661493b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 661593b34091SDebojyoti Ghosh *tsout = NULL; 661693b34091SDebojyoti Ghosh 66177a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 661893b34091SDebojyoti Ghosh 661993b34091SDebojyoti Ghosh /* General TS description */ 662093b34091SDebojyoti Ghosh t->numbermonitors = 0; 662193b34091SDebojyoti Ghosh t->setupcalled = 0; 662293b34091SDebojyoti Ghosh t->ksp_its = 0; 662393b34091SDebojyoti Ghosh t->snes_its = 0; 662493b34091SDebojyoti Ghosh t->nwork = 0; 662593b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 662693b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 662793b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 662893b34091SDebojyoti Ghosh 662934561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start); CHKERRQ(ierr); 663034561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start); CHKERRQ(ierr); 6631d15a3a53SEmil Constantinescu 663293b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm); CHKERRQ(ierr); 663393b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm); CHKERRQ(ierr); 663493b34091SDebojyoti Ghosh 663593b34091SDebojyoti Ghosh t->adapt=tsin->adapt; 663693b34091SDebojyoti Ghosh PetscObjectReference((PetscObject)t->adapt); 663793b34091SDebojyoti Ghosh 663893b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 663993b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 664093b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 664193b34091SDebojyoti Ghosh t->time_step_orig = tsin->time_step_orig; 664293b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 664393b34091SDebojyoti Ghosh t->steps = tsin->steps; 664493b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 664593b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 664693b34091SDebojyoti Ghosh t->atol = tsin->atol; 664793b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 664893b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 664993b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 665093b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 665193b34091SDebojyoti Ghosh 665293b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type); CHKERRQ(ierr); 665393b34091SDebojyoti Ghosh ierr = TSSetType(t,type); CHKERRQ(ierr); 665493b34091SDebojyoti Ghosh 665593b34091SDebojyoti Ghosh t->vec_sol = NULL; 665693b34091SDebojyoti Ghosh 665793b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 665893b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 665993b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 666093b34091SDebojyoti Ghosh 666193b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 666293b34091SDebojyoti Ghosh 666393b34091SDebojyoti Ghosh *tsout = t; 666493b34091SDebojyoti Ghosh PetscFunctionReturn(0); 666593b34091SDebojyoti Ghosh } 6666