163dd3a1aSKris Buschelman 2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h> /*I "petscts.h" I*/ 3496e6a7aSJed Brown #include <petscdmshell.h> 41e25c274SJed Brown #include <petscdmda.h> 52d5ee99bSBarry Smith #include <petscviewer.h> 62d5ee99bSBarry Smith #include <petscdraw.h> 7d763cef2SBarry Smith 8d5ba7fb7SMatthew Knepley /* Logging support */ 9d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 10166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 11d405a339SMatthew Knepley 1249354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1349354f04SShri Abhyankar 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 36*ef222394SBarry 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 46*ef222394SBarry Smith . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 47bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 48de06c3feSJed Brown . -ts_monitor_lg_timestep - Monitor timestep size graphically 49de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 50de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 51de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 52de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 53de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 54de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 553e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 563e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 5791b97e58SBarry Smith . -ts_monitor_solution_binary <filename> - Save each solution to a binary file 58b3d3934dSBarry Smith . -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 59b3d3934dSBarry Smith - -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 6091b97e58SBarry Smith 6191b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 62bdad233fSMatthew Knepley 63bdad233fSMatthew Knepley Level: beginner 64bdad233fSMatthew Knepley 65bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 66bdad233fSMatthew Knepley 67a313700dSBarry Smith .seealso: TSGetType() 68bdad233fSMatthew Knepley @*/ 697087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 70bdad233fSMatthew Knepley { 71bc952696SBarry Smith PetscBool opt,flg,tflg; 72dfbe8321SBarry Smith PetscErrorCode ierr; 73649052a6SBarry Smith PetscViewer monviewer; 74eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 75089b2837SJed Brown SNES snes; 761c3436cfSJed Brown TSAdapt adapt; 7731748224SBarry Smith PetscReal time_step; 7849354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 79d1212d36SBarry Smith char dir[16]; 806991f827SBarry Smith const char *defaultType; 816991f827SBarry Smith char typeName[256]; 82bdad233fSMatthew Knepley 83bdad233fSMatthew Knepley PetscFunctionBegin; 840700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 853194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 866991f827SBarry Smith if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 876991f827SBarry Smith else defaultType = TSEULER; 886991f827SBarry Smith 896991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 906991f827SBarry Smith ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 916991f827SBarry Smith if (opt) { 926991f827SBarry Smith ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 936991f827SBarry Smith } else { 946991f827SBarry Smith ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 956991f827SBarry Smith } 96bdad233fSMatthew Knepley 97bdad233fSMatthew Knepley /* Handle generic TS options */ 98844594baSBarry Smith if (ts->trajectory) tflg = PETSC_TRUE; 99844594baSBarry Smith else tflg = PETSC_FALSE; 100920aabf2SBarry Smith ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 101bc952696SBarry Smith if (tflg) {ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);} 102*ef222394SBarry Smith if (ts->adjoint_solve) tflg = PETSC_TRUE; 103*ef222394SBarry Smith else tflg = PETSC_FALSE; 104*ef222394SBarry Smith ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr); 105*ef222394SBarry Smith if (flg) { 106*ef222394SBarry Smith if (!ts->trajectory) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"Must also set -ts_save_trajectory when setting -ts_adjoint_solve"); 107*ef222394SBarry Smith ts->adjoint_solve = tflg; 108*ef222394SBarry Smith ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 109*ef222394SBarry Smith } 1100298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1110298fd71SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 1120298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 11331748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 11431748224SBarry Smith if (flg) { 11531748224SBarry Smith ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 11631748224SBarry Smith } 11749354f04SShri 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); 11849354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 1190298fd71SBarry 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); 1200298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 1210298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 1220298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 1230298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 124bdad233fSMatthew Knepley 12556f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 12656f85f32SBarry Smith { 12756f85f32SBarry Smith PetscBool set; 12856f85f32SBarry Smith flg = PETSC_FALSE; 12956f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 13056f85f32SBarry Smith if (set) { 13156f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 13256f85f32SBarry Smith } 13356f85f32SBarry Smith } 13456f85f32SBarry Smith #endif 13556f85f32SBarry Smith 136bdad233fSMatthew Knepley /* Monitor options */ 137a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 138eabae89aSBarry Smith if (flg) { 139ce94432eSBarry Smith ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr); 140649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 141bdad233fSMatthew Knepley } 1425180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1435180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1445180491cSLisandro Dalcin 1454f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 146a7cc72afSBarry Smith if (opt) { 1470b039ecaSBarry Smith TSMonitorLGCtx ctx; 1483923b477SBarry Smith PetscInt howoften = 1; 149a80ad3e0SBarry Smith 1500298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 151ce94432eSBarry Smith ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 1524f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 153b3603a34SBarry Smith } 1544f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 155b3603a34SBarry Smith if (opt) { 1560b039ecaSBarry Smith TSMonitorLGCtx ctx; 1573923b477SBarry Smith PetscInt howoften = 1; 158b3603a34SBarry Smith 1590298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",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,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 162bdad233fSMatthew Knepley } 1634f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 164ef20d060SBarry Smith if (opt) { 1650b039ecaSBarry Smith TSMonitorLGCtx ctx; 1663923b477SBarry Smith PetscInt howoften = 1; 167ef20d060SBarry Smith 1680298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 16922d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1704f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 171ef20d060SBarry Smith } 172201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&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_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 17822d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 179201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 180201da799SBarry Smith } 181201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 182201da799SBarry Smith if (opt) { 183201da799SBarry Smith TSMonitorLGCtx ctx; 184201da799SBarry Smith PetscInt howoften = 1; 185201da799SBarry Smith 1860298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 18722d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 188201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 189201da799SBarry Smith } 1908189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 1918189c53fSBarry Smith if (opt) { 1928189c53fSBarry Smith TSMonitorSPEigCtx ctx; 1938189c53fSBarry Smith PetscInt howoften = 1; 1948189c53fSBarry Smith 1950298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 19622d28d08SBarry Smith ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1978189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 1988189c53fSBarry Smith } 199ef20d060SBarry Smith opt = PETSC_FALSE; 2000dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 201a7cc72afSBarry Smith if (opt) { 20283a4ac43SBarry Smith TSMonitorDrawCtx ctx; 20383a4ac43SBarry Smith PetscInt howoften = 1; 204a80ad3e0SBarry Smith 2050298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 206ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 20783a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 208bdad233fSMatthew Knepley } 209fb1732b5SBarry Smith opt = PETSC_FALSE; 2102d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 2112d5ee99bSBarry Smith if (opt) { 2122d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2132d5ee99bSBarry Smith PetscReal bounds[4]; 2142d5ee99bSBarry Smith PetscInt n = 4; 2152d5ee99bSBarry Smith PetscDraw draw; 2162d5ee99bSBarry Smith 2172d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 2182d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2192d5ee99bSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr); 2202d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 2212d5ee99bSBarry Smith ierr = PetscDrawClear(draw);CHKERRQ(ierr); 2224b363babSBarry Smith ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr); 2234b363babSBarry Smith ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 224f54adfc0SBarry Smith ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 2254b363babSBarry Smith ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr); 22607995780SPeter Brune /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */ 2272d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2282d5ee99bSBarry Smith } 2292d5ee99bSBarry Smith opt = PETSC_FALSE; 2300dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 2313a471f94SBarry Smith if (opt) { 23283a4ac43SBarry Smith TSMonitorDrawCtx ctx; 23383a4ac43SBarry Smith PetscInt howoften = 1; 2343a471f94SBarry Smith 2350298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 236ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 23783a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2383a471f94SBarry Smith } 2393a471f94SBarry Smith opt = PETSC_FALSE; 24091b97e58SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 241fb1732b5SBarry Smith if (flg) { 242fb1732b5SBarry Smith PetscViewer ctx; 243fb1732b5SBarry Smith if (monfilename[0]) { 244ce94432eSBarry Smith ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 245c2fbc07fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 246fb1732b5SBarry Smith } else { 247ce94432eSBarry Smith ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts)); 2480298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr); 249fb1732b5SBarry Smith } 250fb1732b5SBarry Smith } 251ed81e22dSJed Brown opt = PETSC_FALSE; 25291b97e58SBarry 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); 253ed81e22dSJed Brown if (flg) { 254ed81e22dSJed Brown const char *ptr,*ptr2; 255ed81e22dSJed Brown char *filetemplate; 256ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 257ed81e22dSJed Brown /* Do some cursory validation of the input. */ 258ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 259ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 260ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 261ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 262ce94432eSBarry 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"); 263ed81e22dSJed Brown if (ptr2) break; 264ed81e22dSJed Brown } 265ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 266ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 267ed81e22dSJed Brown } 268bdad233fSMatthew Knepley 269d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 270d1212d36SBarry Smith if (flg) { 271d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 272d1212d36SBarry Smith int ray = 0; 273d1212d36SBarry Smith DMDADirection ddir; 274d1212d36SBarry Smith DM da; 275d1212d36SBarry Smith PetscMPIInt rank; 276d1212d36SBarry Smith 277ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 278d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 279d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 280ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 281d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 282d1212d36SBarry Smith 283d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 284b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 285d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 286d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 287ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 288d1212d36SBarry Smith if (!rank) { 289d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 290d1212d36SBarry Smith } 29151b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 292d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 293d1212d36SBarry Smith } 29451b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 29551b4a12fSMatthew G. Knepley if (flg) { 29651b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 29751b4a12fSMatthew G. Knepley int ray = 0; 29851b4a12fSMatthew G. Knepley DMDADirection ddir; 29951b4a12fSMatthew G. Knepley DM da; 30051b4a12fSMatthew G. Knepley PetscInt howoften = 1; 30151b4a12fSMatthew G. Knepley 30251b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 30351b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 30451b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 30551b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 30651b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 30751b4a12fSMatthew G. Knepley 30851b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 309b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 31051b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 31151b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 31251b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 31351b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 31451b4a12fSMatthew G. Knepley } 315d1212d36SBarry Smith 316b3d3934dSBarry Smith ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 317b3d3934dSBarry Smith if (opt) { 318b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 319b3d3934dSBarry Smith 320b3d3934dSBarry Smith ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 321b3d3934dSBarry Smith ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 322b3d3934dSBarry Smith } 323b3d3934dSBarry Smith 3247781c08eSBarry Smith /* 3257781c08eSBarry Smith This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui 3267781c08eSBarry Smith will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin() 3277781c08eSBarry Smith */ 328552698daSJed Brown ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr); 329e55864a3SBarry Smith ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr); 330d52bd9f3SBarry Smith 331bdad233fSMatthew Knepley /* Handle specific TS options */ 332abc0a331SBarry Smith if (ts->ops->setfromoptions) { 3336991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 334bdad233fSMatthew Knepley } 3356991f827SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 3365d973c19SBarry Smith 3375d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 3385d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 339bdad233fSMatthew Knepley 340bc952696SBarry Smith if (ts->trajectory) { 341bc952696SBarry Smith ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr); 342bc952696SBarry Smith } 343bc952696SBarry Smith 3446991f827SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3456991f827SBarry Smith if (snes) { 3466991f827SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 3476991f827SBarry Smith ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr); 348bdad233fSMatthew Knepley } 349bdad233fSMatthew Knepley PetscFunctionReturn(0); 350bdad233fSMatthew Knepley } 351bdad233fSMatthew Knepley 352bdad233fSMatthew Knepley #undef __FUNCT__ 353bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory" 354d2daff3dSHong Zhang /*@ 355bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 356d2daff3dSHong Zhang 357d2daff3dSHong Zhang Collective on TS 358d2daff3dSHong Zhang 359d2daff3dSHong Zhang Input Parameters: 360bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 361bc952696SBarry Smith 362d2daff3dSHong Zhang 363d2daff3dSHong Zhang Level: intermediate 364d2daff3dSHong Zhang 365bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve() 366d2daff3dSHong Zhang 367bc952696SBarry Smith .keywords: TS, set, checkpoint, 368d2daff3dSHong Zhang @*/ 369bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 370d2daff3dSHong Zhang { 371bc952696SBarry Smith PetscErrorCode ierr; 372bc952696SBarry Smith 373d2daff3dSHong Zhang PetscFunctionBegin; 374d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 375bc952696SBarry Smith if (!ts->trajectory) { 376bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 377920aabf2SBarry Smith ierr = TSTrajectorySetType(ts->trajectory,TSTRAJECTORYBASIC);CHKERRQ(ierr); 378bc952696SBarry Smith } 379d2daff3dSHong Zhang PetscFunctionReturn(0); 380d2daff3dSHong Zhang } 381d2daff3dSHong Zhang 382cdcf91faSSean Farley #undef __FUNCT__ 3834a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian" 384a7a1495cSBarry Smith /*@ 3858c385f81SBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 386a7a1495cSBarry Smith set with TSSetRHSJacobian(). 387a7a1495cSBarry Smith 388a7a1495cSBarry Smith Collective on TS and Vec 389a7a1495cSBarry Smith 390a7a1495cSBarry Smith Input Parameters: 391316643e7SJed Brown + ts - the TS context 392a7a1495cSBarry Smith . t - current timestep 3930910c330SBarry Smith - U - input vector 394a7a1495cSBarry Smith 395a7a1495cSBarry Smith Output Parameters: 396a7a1495cSBarry Smith + A - Jacobian matrix 397a7a1495cSBarry Smith . B - optional preconditioning matrix 398a7a1495cSBarry Smith - flag - flag indicating matrix structure 399a7a1495cSBarry Smith 400a7a1495cSBarry Smith Notes: 401a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 402a7a1495cSBarry Smith is used internally within the nonlinear solvers. 403a7a1495cSBarry Smith 40494b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 405a7a1495cSBarry Smith flag parameter. 406a7a1495cSBarry Smith 407a7a1495cSBarry Smith Level: developer 408a7a1495cSBarry Smith 409a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 410a7a1495cSBarry Smith 41194b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 412a7a1495cSBarry Smith @*/ 413d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 414a7a1495cSBarry Smith { 415dfbe8321SBarry Smith PetscErrorCode ierr; 416270bf2e7SJed Brown PetscObjectState Ustate; 41724989b8cSPeter Brune DM dm; 418942e3340SBarry Smith DMTS tsdm; 41924989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 42024989b8cSPeter Brune void *ctx; 42124989b8cSPeter Brune TSIJacobian ijacobianfunc; 422a7a1495cSBarry Smith 423a7a1495cSBarry Smith PetscFunctionBegin; 4240700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4250910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 4260910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 42724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 428942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 42924989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 4300298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 43159e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 4320910c330SBarry Smith if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) { 4330e4ef248SJed Brown PetscFunctionReturn(0); 434f8ede8e7SJed Brown } 435d90be118SSean Farley 436ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 437d90be118SSean Farley 438e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 43994ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 44094ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 44194ab13aaSBarry Smith if (A != B) { 44294ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 44394ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 444e1244c69SJed Brown } 445e1244c69SJed Brown ts->rhsjacobian.shift = 0; 446e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 447e1244c69SJed Brown } 448e1244c69SJed Brown 44924989b8cSPeter Brune if (rhsjacobianfunc) { 45094ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 451a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 452d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 453a7a1495cSBarry Smith PetscStackPop; 45494ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 455a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 45694ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,4); 45794ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,5); 458ef66eb69SBarry Smith } else { 45994ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 46094ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 461ef66eb69SBarry Smith } 4620e4ef248SJed Brown ts->rhsjacobian.time = t; 4630910c330SBarry Smith ts->rhsjacobian.X = U; 46459e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 465a7a1495cSBarry Smith PetscFunctionReturn(0); 466a7a1495cSBarry Smith } 467a7a1495cSBarry Smith 4684a2ae208SSatish Balay #undef __FUNCT__ 4694a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 470316643e7SJed Brown /*@ 471d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 472d763cef2SBarry Smith 473316643e7SJed Brown Collective on TS and Vec 474316643e7SJed Brown 475316643e7SJed Brown Input Parameters: 476316643e7SJed Brown + ts - the TS context 477316643e7SJed Brown . t - current time 4780910c330SBarry Smith - U - state vector 479316643e7SJed Brown 480316643e7SJed Brown Output Parameter: 481316643e7SJed Brown . y - right hand side 482316643e7SJed Brown 483316643e7SJed Brown Note: 484316643e7SJed Brown Most users should not need to explicitly call this routine, as it 485316643e7SJed Brown is used internally within the nonlinear solvers. 486316643e7SJed Brown 487316643e7SJed Brown Level: developer 488316643e7SJed Brown 489316643e7SJed Brown .keywords: TS, compute 490316643e7SJed Brown 491316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 492316643e7SJed Brown @*/ 4930910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 494d763cef2SBarry Smith { 495dfbe8321SBarry Smith PetscErrorCode ierr; 49624989b8cSPeter Brune TSRHSFunction rhsfunction; 49724989b8cSPeter Brune TSIFunction ifunction; 49824989b8cSPeter Brune void *ctx; 49924989b8cSPeter Brune DM dm; 50024989b8cSPeter Brune 501d763cef2SBarry Smith PetscFunctionBegin; 5020700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5030910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5040700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 50524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 50624989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 5070298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 508d763cef2SBarry Smith 509ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 510d763cef2SBarry Smith 5110910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 51224989b8cSPeter Brune if (rhsfunction) { 513d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 5140910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 515d763cef2SBarry Smith PetscStackPop; 516214bc6a2SJed Brown } else { 517214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 518b2cd27e8SJed Brown } 51944a41b28SSean Farley 5200910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 521d763cef2SBarry Smith PetscFunctionReturn(0); 522d763cef2SBarry Smith } 523d763cef2SBarry Smith 5244a2ae208SSatish Balay #undef __FUNCT__ 525ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 526ef20d060SBarry Smith /*@ 527ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 528ef20d060SBarry Smith 529ef20d060SBarry Smith Collective on TS and Vec 530ef20d060SBarry Smith 531ef20d060SBarry Smith Input Parameters: 532ef20d060SBarry Smith + ts - the TS context 533ef20d060SBarry Smith - t - current time 534ef20d060SBarry Smith 535ef20d060SBarry Smith Output Parameter: 5360910c330SBarry Smith . U - the solution 537ef20d060SBarry Smith 538ef20d060SBarry Smith Note: 539ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 540ef20d060SBarry Smith is used internally within the nonlinear solvers. 541ef20d060SBarry Smith 542ef20d060SBarry Smith Level: developer 543ef20d060SBarry Smith 544ef20d060SBarry Smith .keywords: TS, compute 545ef20d060SBarry Smith 546abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 547ef20d060SBarry Smith @*/ 5480910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 549ef20d060SBarry Smith { 550ef20d060SBarry Smith PetscErrorCode ierr; 551ef20d060SBarry Smith TSSolutionFunction solutionfunction; 552ef20d060SBarry Smith void *ctx; 553ef20d060SBarry Smith DM dm; 554ef20d060SBarry Smith 555ef20d060SBarry Smith PetscFunctionBegin; 556ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5570910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 558ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 559ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 560ef20d060SBarry Smith 561ef20d060SBarry Smith if (solutionfunction) { 5629b7cd975SBarry Smith PetscStackPush("TS user solution function"); 5630910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 564ef20d060SBarry Smith PetscStackPop; 565ef20d060SBarry Smith } 566ef20d060SBarry Smith PetscFunctionReturn(0); 567ef20d060SBarry Smith } 5689b7cd975SBarry Smith #undef __FUNCT__ 5699b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction" 5709b7cd975SBarry Smith /*@ 5719b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 5729b7cd975SBarry Smith 5739b7cd975SBarry Smith Collective on TS and Vec 5749b7cd975SBarry Smith 5759b7cd975SBarry Smith Input Parameters: 5769b7cd975SBarry Smith + ts - the TS context 5779b7cd975SBarry Smith - t - current time 5789b7cd975SBarry Smith 5799b7cd975SBarry Smith Output Parameter: 5809b7cd975SBarry Smith . U - the function value 5819b7cd975SBarry Smith 5829b7cd975SBarry Smith Note: 5839b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 5849b7cd975SBarry Smith is used internally within the nonlinear solvers. 5859b7cd975SBarry Smith 5869b7cd975SBarry Smith Level: developer 5879b7cd975SBarry Smith 5889b7cd975SBarry Smith .keywords: TS, compute 5899b7cd975SBarry Smith 5909b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 5919b7cd975SBarry Smith @*/ 5929b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 5939b7cd975SBarry Smith { 5949b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 5959b7cd975SBarry Smith void *ctx; 5969b7cd975SBarry Smith DM dm; 5979b7cd975SBarry Smith 5989b7cd975SBarry Smith PetscFunctionBegin; 5999b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6009b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6019b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 6029b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 6039b7cd975SBarry Smith 6049b7cd975SBarry Smith if (forcing) { 6059b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 6069b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 6079b7cd975SBarry Smith PetscStackPop; 6089b7cd975SBarry Smith } 6099b7cd975SBarry Smith PetscFunctionReturn(0); 6109b7cd975SBarry Smith } 611ef20d060SBarry Smith 612ef20d060SBarry Smith #undef __FUNCT__ 613214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 614214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 615214bc6a2SJed Brown { 6162dd45cf8SJed Brown Vec F; 617214bc6a2SJed Brown PetscErrorCode ierr; 618214bc6a2SJed Brown 619214bc6a2SJed Brown PetscFunctionBegin; 6200298fd71SBarry Smith *Frhs = NULL; 6210298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 622214bc6a2SJed Brown if (!ts->Frhs) { 6232dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 624214bc6a2SJed Brown } 625214bc6a2SJed Brown *Frhs = ts->Frhs; 626214bc6a2SJed Brown PetscFunctionReturn(0); 627214bc6a2SJed Brown } 628214bc6a2SJed Brown 629214bc6a2SJed Brown #undef __FUNCT__ 630214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 631214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 632214bc6a2SJed Brown { 633214bc6a2SJed Brown Mat A,B; 6342dd45cf8SJed Brown PetscErrorCode ierr; 635214bc6a2SJed Brown 636214bc6a2SJed Brown PetscFunctionBegin; 637c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 638c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 6390298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 640214bc6a2SJed Brown if (Arhs) { 641214bc6a2SJed Brown if (!ts->Arhs) { 642214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 643214bc6a2SJed Brown } 644214bc6a2SJed Brown *Arhs = ts->Arhs; 645214bc6a2SJed Brown } 646214bc6a2SJed Brown if (Brhs) { 647214bc6a2SJed Brown if (!ts->Brhs) { 648bdb70873SJed Brown if (A != B) { 649214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 650bdb70873SJed Brown } else { 651bdb70873SJed Brown ts->Brhs = ts->Arhs; 652bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 653bdb70873SJed Brown } 654214bc6a2SJed Brown } 655214bc6a2SJed Brown *Brhs = ts->Brhs; 656214bc6a2SJed Brown } 657214bc6a2SJed Brown PetscFunctionReturn(0); 658214bc6a2SJed Brown } 659214bc6a2SJed Brown 660214bc6a2SJed Brown #undef __FUNCT__ 661316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 662316643e7SJed Brown /*@ 6630910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 664316643e7SJed Brown 665316643e7SJed Brown Collective on TS and Vec 666316643e7SJed Brown 667316643e7SJed Brown Input Parameters: 668316643e7SJed Brown + ts - the TS context 669316643e7SJed Brown . t - current time 6700910c330SBarry Smith . U - state vector 6710910c330SBarry Smith . Udot - time derivative of state vector 672214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 673316643e7SJed Brown 674316643e7SJed Brown Output Parameter: 675316643e7SJed Brown . Y - right hand side 676316643e7SJed Brown 677316643e7SJed Brown Note: 678316643e7SJed Brown Most users should not need to explicitly call this routine, as it 679316643e7SJed Brown is used internally within the nonlinear solvers. 680316643e7SJed Brown 681316643e7SJed Brown If the user did did not write their equations in implicit form, this 682316643e7SJed Brown function recasts them in implicit form. 683316643e7SJed Brown 684316643e7SJed Brown Level: developer 685316643e7SJed Brown 686316643e7SJed Brown .keywords: TS, compute 687316643e7SJed Brown 688316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 689316643e7SJed Brown @*/ 6900910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 691316643e7SJed Brown { 692316643e7SJed Brown PetscErrorCode ierr; 69324989b8cSPeter Brune TSIFunction ifunction; 69424989b8cSPeter Brune TSRHSFunction rhsfunction; 69524989b8cSPeter Brune void *ctx; 69624989b8cSPeter Brune DM dm; 697316643e7SJed Brown 698316643e7SJed Brown PetscFunctionBegin; 6990700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7000910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7010910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 7020700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 703316643e7SJed Brown 70424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 70524989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 7060298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 70724989b8cSPeter Brune 708ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 709d90be118SSean Farley 7100910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 71124989b8cSPeter Brune if (ifunction) { 712316643e7SJed Brown PetscStackPush("TS user implicit function"); 7130910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 714316643e7SJed Brown PetscStackPop; 715214bc6a2SJed Brown } 716214bc6a2SJed Brown if (imex) { 71724989b8cSPeter Brune if (!ifunction) { 7180910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 7192dd45cf8SJed Brown } 72024989b8cSPeter Brune } else if (rhsfunction) { 72124989b8cSPeter Brune if (ifunction) { 722214bc6a2SJed Brown Vec Frhs; 723214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 7240910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 725214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 7262dd45cf8SJed Brown } else { 7270910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 7280910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 729316643e7SJed Brown } 7304a6899ffSJed Brown } 7310910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 732316643e7SJed Brown PetscFunctionReturn(0); 733316643e7SJed Brown } 734316643e7SJed Brown 735316643e7SJed Brown #undef __FUNCT__ 736316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 737316643e7SJed Brown /*@ 738316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 739316643e7SJed Brown 740316643e7SJed Brown Collective on TS and Vec 741316643e7SJed Brown 742316643e7SJed Brown Input 743316643e7SJed Brown Input Parameters: 744316643e7SJed Brown + ts - the TS context 745316643e7SJed Brown . t - current timestep 7460910c330SBarry Smith . U - state vector 7470910c330SBarry Smith . Udot - time derivative of state vector 748214bc6a2SJed Brown . shift - shift to apply, see note below 749214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 750316643e7SJed Brown 751316643e7SJed Brown Output Parameters: 752316643e7SJed Brown + A - Jacobian matrix 753316643e7SJed Brown . B - optional preconditioning matrix 754316643e7SJed Brown - flag - flag indicating matrix structure 755316643e7SJed Brown 756316643e7SJed Brown Notes: 7570910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 758316643e7SJed Brown 7590910c330SBarry Smith dF/dU + shift*dF/dUdot 760316643e7SJed Brown 761316643e7SJed Brown Most users should not need to explicitly call this routine, as it 762316643e7SJed Brown is used internally within the nonlinear solvers. 763316643e7SJed Brown 764316643e7SJed Brown Level: developer 765316643e7SJed Brown 766316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 767316643e7SJed Brown 768316643e7SJed Brown .seealso: TSSetIJacobian() 76963495f91SJed Brown @*/ 770d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 771316643e7SJed Brown { 772316643e7SJed Brown PetscErrorCode ierr; 77324989b8cSPeter Brune TSIJacobian ijacobian; 77424989b8cSPeter Brune TSRHSJacobian rhsjacobian; 77524989b8cSPeter Brune DM dm; 77624989b8cSPeter Brune void *ctx; 777316643e7SJed Brown 778316643e7SJed Brown PetscFunctionBegin; 7790700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7800910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7810910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 782316643e7SJed Brown PetscValidPointer(A,6); 78394ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 784316643e7SJed Brown PetscValidPointer(B,7); 78594ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 78624989b8cSPeter Brune 78724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 78824989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 7890298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 79024989b8cSPeter Brune 791ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 792316643e7SJed Brown 79394ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 79424989b8cSPeter Brune if (ijacobian) { 795316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 796d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 797316643e7SJed Brown PetscStackPop; 798214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 79994ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,4); 80094ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,5); 8014a6899ffSJed Brown } 802214bc6a2SJed Brown if (imex) { 803b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 80494ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 80594ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 80694ab13aaSBarry Smith if (A != B) { 80794ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 80894ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 809214bc6a2SJed Brown } 810214bc6a2SJed Brown } 811214bc6a2SJed Brown } else { 812e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 813e1244c69SJed Brown if (rhsjacobian) { 814bd373395SBarry Smith if (ijacobian) { 815e1244c69SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 816bd373395SBarry Smith } else { 817bd373395SBarry Smith ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr); 818e1244c69SJed Brown } 819d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 820e1244c69SJed Brown } 82194ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 822e1244c69SJed Brown ts->rhsjacobian.scale = -1; 823e1244c69SJed Brown ts->rhsjacobian.shift = shift; 82494ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 82594ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 82694ab13aaSBarry Smith if (A != B) { 82794ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 82894ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 829316643e7SJed Brown } 830e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 831e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 832e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 83394ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 83494ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 83594ab13aaSBarry Smith if (A != B) { 83694ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 83794ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 838e1244c69SJed Brown } 839e1244c69SJed Brown } 84094ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 84194ab13aaSBarry Smith if (A != B) { 84294ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 843214bc6a2SJed Brown } 844316643e7SJed Brown } 845316643e7SJed Brown } 84694ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 847316643e7SJed Brown PetscFunctionReturn(0); 848316643e7SJed Brown } 849316643e7SJed Brown 850316643e7SJed Brown #undef __FUNCT__ 8514a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 852d763cef2SBarry Smith /*@C 853d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 854b5abc632SBarry Smith where U_t = G(t,u). 855d763cef2SBarry Smith 8563f9fe445SBarry Smith Logically Collective on TS 857d763cef2SBarry Smith 858d763cef2SBarry Smith Input Parameters: 859d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 8600298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 861d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 862d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 8630298fd71SBarry Smith function evaluation routine (may be NULL) 864d763cef2SBarry Smith 865d763cef2SBarry Smith Calling sequence of func: 86687828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 867d763cef2SBarry Smith 868d763cef2SBarry Smith + t - current timestep 869d763cef2SBarry Smith . u - input vector 870d763cef2SBarry Smith . F - function vector 871d763cef2SBarry Smith - ctx - [optional] user-defined function context 872d763cef2SBarry Smith 873d763cef2SBarry Smith Level: beginner 874d763cef2SBarry Smith 8752bbac0d3SBarry Smith Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 8762bbac0d3SBarry Smith 877d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 878d763cef2SBarry Smith 879ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 880d763cef2SBarry Smith @*/ 881089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 882d763cef2SBarry Smith { 883089b2837SJed Brown PetscErrorCode ierr; 884089b2837SJed Brown SNES snes; 8850298fd71SBarry Smith Vec ralloc = NULL; 88624989b8cSPeter Brune DM dm; 887d763cef2SBarry Smith 888089b2837SJed Brown PetscFunctionBegin; 8890700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 890ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 89124989b8cSPeter Brune 89224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 89324989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 894089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 895e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 896e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 897e856ceecSJed Brown r = ralloc; 898e856ceecSJed Brown } 899089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 900e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 901d763cef2SBarry Smith PetscFunctionReturn(0); 902d763cef2SBarry Smith } 903d763cef2SBarry Smith 9044a2ae208SSatish Balay #undef __FUNCT__ 905ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 906ef20d060SBarry Smith /*@C 907abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 908ef20d060SBarry Smith 909ef20d060SBarry Smith Logically Collective on TS 910ef20d060SBarry Smith 911ef20d060SBarry Smith Input Parameters: 912ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 913ef20d060SBarry Smith . f - routine for evaluating the solution 914ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 9150298fd71SBarry Smith function evaluation routine (may be NULL) 916ef20d060SBarry Smith 917ef20d060SBarry Smith Calling sequence of func: 918ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 919ef20d060SBarry Smith 920ef20d060SBarry Smith + t - current timestep 921ef20d060SBarry Smith . u - output vector 922ef20d060SBarry Smith - ctx - [optional] user-defined function context 923ef20d060SBarry Smith 924abd5a294SJed Brown Notes: 925abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 926abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 927abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 928abd5a294SJed Brown 9294f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 930abd5a294SJed Brown 931ef20d060SBarry Smith Level: beginner 932ef20d060SBarry Smith 933ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 934ef20d060SBarry Smith 9359b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 936ef20d060SBarry Smith @*/ 937ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 938ef20d060SBarry Smith { 939ef20d060SBarry Smith PetscErrorCode ierr; 940ef20d060SBarry Smith DM dm; 941ef20d060SBarry Smith 942ef20d060SBarry Smith PetscFunctionBegin; 943ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 944ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 945ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 946ef20d060SBarry Smith PetscFunctionReturn(0); 947ef20d060SBarry Smith } 948ef20d060SBarry Smith 949ef20d060SBarry Smith #undef __FUNCT__ 9509b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction" 9519b7cd975SBarry Smith /*@C 9529b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 9539b7cd975SBarry Smith 9549b7cd975SBarry Smith Logically Collective on TS 9559b7cd975SBarry Smith 9569b7cd975SBarry Smith Input Parameters: 9579b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 9589b7cd975SBarry Smith . f - routine for evaluating the forcing function 9599b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 9600298fd71SBarry Smith function evaluation routine (may be NULL) 9619b7cd975SBarry Smith 9629b7cd975SBarry Smith Calling sequence of func: 9639b7cd975SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 9649b7cd975SBarry Smith 9659b7cd975SBarry Smith + t - current timestep 9669b7cd975SBarry Smith . u - output vector 9679b7cd975SBarry Smith - ctx - [optional] user-defined function context 9689b7cd975SBarry Smith 9699b7cd975SBarry Smith Notes: 9709b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 9719b7cd975SBarry Smith create closed-form solutions with a non-physical forcing term. 9729b7cd975SBarry Smith 9739b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 9749b7cd975SBarry Smith 9759b7cd975SBarry Smith Level: beginner 9769b7cd975SBarry Smith 9779b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 9789b7cd975SBarry Smith 9799b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 9809b7cd975SBarry Smith @*/ 9819b7cd975SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 9829b7cd975SBarry Smith { 9839b7cd975SBarry Smith PetscErrorCode ierr; 9849b7cd975SBarry Smith DM dm; 9859b7cd975SBarry Smith 9869b7cd975SBarry Smith PetscFunctionBegin; 9879b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9889b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 9899b7cd975SBarry Smith ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr); 9909b7cd975SBarry Smith PetscFunctionReturn(0); 9919b7cd975SBarry Smith } 9929b7cd975SBarry Smith 9939b7cd975SBarry Smith #undef __FUNCT__ 9944a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 995d763cef2SBarry Smith /*@C 996f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 997b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 998d763cef2SBarry Smith 9993f9fe445SBarry Smith Logically Collective on TS 1000d763cef2SBarry Smith 1001d763cef2SBarry Smith Input Parameters: 1002d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1003e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1004e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1005d763cef2SBarry Smith . f - the Jacobian evaluation routine 1006d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 10070298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1008d763cef2SBarry Smith 1009f7ab8db6SBarry Smith Calling sequence of f: 1010ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1011d763cef2SBarry Smith 1012d763cef2SBarry Smith + t - current timestep 1013d763cef2SBarry Smith . u - input vector 1014e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1015e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1016d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1017d763cef2SBarry Smith 1018d763cef2SBarry Smith 1019d763cef2SBarry Smith Level: beginner 1020d763cef2SBarry Smith 1021d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1022d763cef2SBarry Smith 1023ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1024d763cef2SBarry Smith 1025d763cef2SBarry Smith @*/ 1026e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1027d763cef2SBarry Smith { 1028277b19d0SLisandro Dalcin PetscErrorCode ierr; 1029089b2837SJed Brown SNES snes; 103024989b8cSPeter Brune DM dm; 103124989b8cSPeter Brune TSIJacobian ijacobian; 1032277b19d0SLisandro Dalcin 1033d763cef2SBarry Smith PetscFunctionBegin; 10340700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1035e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1036e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1037e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1038e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1039d763cef2SBarry Smith 104024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 104124989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1042e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1043e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1044e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1045e1244c69SJed Brown } 10460298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1047089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 10485f659677SPeter Brune if (!ijacobian) { 1049e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 10500e4ef248SJed Brown } 1051e5d3d808SBarry Smith if (Amat) { 1052e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 10530e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1054bbd56ea5SKarl Rupp 1055e5d3d808SBarry Smith ts->Arhs = Amat; 10560e4ef248SJed Brown } 1057e5d3d808SBarry Smith if (Pmat) { 1058e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 10590e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1060bbd56ea5SKarl Rupp 1061e5d3d808SBarry Smith ts->Brhs = Pmat; 10620e4ef248SJed Brown } 1063d763cef2SBarry Smith PetscFunctionReturn(0); 1064d763cef2SBarry Smith } 1065d763cef2SBarry Smith 1066316643e7SJed Brown 1067316643e7SJed Brown #undef __FUNCT__ 1068316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 1069316643e7SJed Brown /*@C 1070b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1071316643e7SJed Brown 10723f9fe445SBarry Smith Logically Collective on TS 1073316643e7SJed Brown 1074316643e7SJed Brown Input Parameters: 1075316643e7SJed Brown + ts - the TS context obtained from TSCreate() 10760298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1077316643e7SJed Brown . f - the function evaluation routine 10780298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1079316643e7SJed Brown 1080316643e7SJed Brown Calling sequence of f: 1081316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1082316643e7SJed Brown 1083316643e7SJed Brown + t - time at step/stage being solved 1084316643e7SJed Brown . u - state vector 1085316643e7SJed Brown . u_t - time derivative of state vector 1086316643e7SJed Brown . F - function vector 1087316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1088316643e7SJed Brown 1089316643e7SJed Brown Important: 10902bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1091316643e7SJed Brown 1092316643e7SJed Brown Level: beginner 1093316643e7SJed Brown 1094316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1095316643e7SJed Brown 1096d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1097316643e7SJed Brown @*/ 1098089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 1099316643e7SJed Brown { 1100089b2837SJed Brown PetscErrorCode ierr; 1101089b2837SJed Brown SNES snes; 11020298fd71SBarry Smith Vec resalloc = NULL; 110324989b8cSPeter Brune DM dm; 1104316643e7SJed Brown 1105316643e7SJed Brown PetscFunctionBegin; 11060700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1107ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 110824989b8cSPeter Brune 110924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 111024989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 111124989b8cSPeter Brune 1112089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1113e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 1114e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 1115e856ceecSJed Brown res = resalloc; 1116e856ceecSJed Brown } 1117089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 1118e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 1119089b2837SJed Brown PetscFunctionReturn(0); 1120089b2837SJed Brown } 1121089b2837SJed Brown 1122089b2837SJed Brown #undef __FUNCT__ 1123089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 1124089b2837SJed Brown /*@C 1125089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1126089b2837SJed Brown 1127089b2837SJed Brown Not Collective 1128089b2837SJed Brown 1129089b2837SJed Brown Input Parameter: 1130089b2837SJed Brown . ts - the TS context 1131089b2837SJed Brown 1132089b2837SJed Brown Output Parameter: 11330298fd71SBarry Smith + r - vector to hold residual (or NULL) 11340298fd71SBarry Smith . func - the function to compute residual (or NULL) 11350298fd71SBarry Smith - ctx - the function context (or NULL) 1136089b2837SJed Brown 1137089b2837SJed Brown Level: advanced 1138089b2837SJed Brown 1139089b2837SJed Brown .keywords: TS, nonlinear, get, function 1140089b2837SJed Brown 1141089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1142089b2837SJed Brown @*/ 1143089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1144089b2837SJed Brown { 1145089b2837SJed Brown PetscErrorCode ierr; 1146089b2837SJed Brown SNES snes; 114724989b8cSPeter Brune DM dm; 1148089b2837SJed Brown 1149089b2837SJed Brown PetscFunctionBegin; 1150089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1151089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11520298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 115324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 115424989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1155089b2837SJed Brown PetscFunctionReturn(0); 1156089b2837SJed Brown } 1157089b2837SJed Brown 1158089b2837SJed Brown #undef __FUNCT__ 1159089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 1160089b2837SJed Brown /*@C 1161089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1162089b2837SJed Brown 1163089b2837SJed Brown Not Collective 1164089b2837SJed Brown 1165089b2837SJed Brown Input Parameter: 1166089b2837SJed Brown . ts - the TS context 1167089b2837SJed Brown 1168089b2837SJed Brown Output Parameter: 11690298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 11700298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 11710298fd71SBarry Smith - ctx - the function context (or NULL) 1172089b2837SJed Brown 1173089b2837SJed Brown Level: advanced 1174089b2837SJed Brown 1175089b2837SJed Brown .keywords: TS, nonlinear, get, function 1176089b2837SJed Brown 11772bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1178089b2837SJed Brown @*/ 1179089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1180089b2837SJed Brown { 1181089b2837SJed Brown PetscErrorCode ierr; 1182089b2837SJed Brown SNES snes; 118324989b8cSPeter Brune DM dm; 1184089b2837SJed Brown 1185089b2837SJed Brown PetscFunctionBegin; 1186089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1187089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11880298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 118924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 119024989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1191316643e7SJed Brown PetscFunctionReturn(0); 1192316643e7SJed Brown } 1193316643e7SJed Brown 1194316643e7SJed Brown #undef __FUNCT__ 1195316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 1196316643e7SJed Brown /*@C 1197a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1198ae8867d6SBarry Smith provided with TSSetIFunction(). 1199316643e7SJed Brown 12003f9fe445SBarry Smith Logically Collective on TS 1201316643e7SJed Brown 1202316643e7SJed Brown Input Parameters: 1203316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1204e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1205e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1206316643e7SJed Brown . f - the Jacobian evaluation routine 12070298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1208316643e7SJed Brown 1209316643e7SJed Brown Calling sequence of f: 1210ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1211316643e7SJed Brown 1212316643e7SJed Brown + t - time at step/stage being solved 12131b4a444bSJed Brown . U - state vector 12141b4a444bSJed Brown . U_t - time derivative of state vector 1215316643e7SJed Brown . a - shift 1216e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1217e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1218316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1219316643e7SJed Brown 1220316643e7SJed Brown Notes: 1221e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1222316643e7SJed Brown 1223a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1224b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1225a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1226a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1227a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1228a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1229a4f0a591SBarry Smith 1230316643e7SJed Brown Level: beginner 1231316643e7SJed Brown 1232316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1233316643e7SJed Brown 1234ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1235316643e7SJed Brown 1236316643e7SJed Brown @*/ 1237e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1238316643e7SJed Brown { 1239316643e7SJed Brown PetscErrorCode ierr; 1240089b2837SJed Brown SNES snes; 124124989b8cSPeter Brune DM dm; 1242316643e7SJed Brown 1243316643e7SJed Brown PetscFunctionBegin; 12440700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1245e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1246e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1247e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1248e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 124924989b8cSPeter Brune 125024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 125124989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 125224989b8cSPeter Brune 1253089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1254e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1255316643e7SJed Brown PetscFunctionReturn(0); 1256316643e7SJed Brown } 1257316643e7SJed Brown 12584a2ae208SSatish Balay #undef __FUNCT__ 1259e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse" 1260e1244c69SJed Brown /*@ 1261e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1262e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1263e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1264e1244c69SJed Brown not been changed by the TS. 1265e1244c69SJed Brown 1266e1244c69SJed Brown Logically Collective 1267e1244c69SJed Brown 1268e1244c69SJed Brown Input Arguments: 1269e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1270e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1271e1244c69SJed Brown 1272e1244c69SJed Brown Level: intermediate 1273e1244c69SJed Brown 1274e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1275e1244c69SJed Brown @*/ 1276e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1277e1244c69SJed Brown { 1278e1244c69SJed Brown PetscFunctionBegin; 1279e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1280e1244c69SJed Brown PetscFunctionReturn(0); 1281e1244c69SJed Brown } 1282e1244c69SJed Brown 1283e1244c69SJed Brown #undef __FUNCT__ 128455849f57SBarry Smith #define __FUNCT__ "TSLoad" 128555849f57SBarry Smith /*@C 128655849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 128755849f57SBarry Smith 128855849f57SBarry Smith Collective on PetscViewer 128955849f57SBarry Smith 129055849f57SBarry Smith Input Parameters: 129155849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 129255849f57SBarry Smith some related function before a call to TSLoad(). 129355849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 129455849f57SBarry Smith 129555849f57SBarry Smith Level: intermediate 129655849f57SBarry Smith 129755849f57SBarry Smith Notes: 129855849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 129955849f57SBarry Smith 130055849f57SBarry Smith Notes for advanced users: 130155849f57SBarry Smith Most users should not need to know the details of the binary storage 130255849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 130355849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 130455849f57SBarry Smith format is 130555849f57SBarry Smith .vb 130655849f57SBarry Smith has not yet been determined 130755849f57SBarry Smith .ve 130855849f57SBarry Smith 130955849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 131055849f57SBarry Smith @*/ 1311f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 131255849f57SBarry Smith { 131355849f57SBarry Smith PetscErrorCode ierr; 131455849f57SBarry Smith PetscBool isbinary; 131555849f57SBarry Smith PetscInt classid; 131655849f57SBarry Smith char type[256]; 13172d53ad75SBarry Smith DMTS sdm; 1318ad6bc421SBarry Smith DM dm; 131955849f57SBarry Smith 132055849f57SBarry Smith PetscFunctionBegin; 1321f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 132255849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 132355849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 132455849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 132555849f57SBarry Smith 132655849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 1327ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 132855849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 1329f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1330f2c2a1b9SBarry Smith if (ts->ops->load) { 1331f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1332f2c2a1b9SBarry Smith } 1333ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1334ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1335ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1336f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1337f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 13382d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 13392d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 134055849f57SBarry Smith PetscFunctionReturn(0); 134155849f57SBarry Smith } 134255849f57SBarry Smith 13439804daf3SBarry Smith #include <petscdraw.h> 1344e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1345e04113cfSBarry Smith #include <petscviewersaws.h> 1346f05ece33SBarry Smith #endif 134755849f57SBarry Smith #undef __FUNCT__ 13484a2ae208SSatish Balay #define __FUNCT__ "TSView" 13497e2c5f70SBarry Smith /*@C 1350d763cef2SBarry Smith TSView - Prints the TS data structure. 1351d763cef2SBarry Smith 13524c49b128SBarry Smith Collective on TS 1353d763cef2SBarry Smith 1354d763cef2SBarry Smith Input Parameters: 1355d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1356d763cef2SBarry Smith - viewer - visualization context 1357d763cef2SBarry Smith 1358d763cef2SBarry Smith Options Database Key: 1359d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1360d763cef2SBarry Smith 1361d763cef2SBarry Smith Notes: 1362d763cef2SBarry Smith The available visualization contexts include 1363b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1364b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1365d763cef2SBarry Smith output where only the first processor opens 1366d763cef2SBarry Smith the file. All other processors send their 1367d763cef2SBarry Smith data to the first processor to print. 1368d763cef2SBarry Smith 1369d763cef2SBarry Smith The user can open an alternative visualization context with 1370b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1371d763cef2SBarry Smith 1372d763cef2SBarry Smith Level: beginner 1373d763cef2SBarry Smith 1374d763cef2SBarry Smith .keywords: TS, timestep, view 1375d763cef2SBarry Smith 1376b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1377d763cef2SBarry Smith @*/ 13787087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1379d763cef2SBarry Smith { 1380dfbe8321SBarry Smith PetscErrorCode ierr; 138119fd82e9SBarry Smith TSType type; 13822b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 13832d53ad75SBarry Smith DMTS sdm; 1384e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1385536b137fSBarry Smith PetscBool issaws; 1386f05ece33SBarry Smith #endif 1387d763cef2SBarry Smith 1388d763cef2SBarry Smith PetscFunctionBegin; 13890700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13903050cee2SBarry Smith if (!viewer) { 1391ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 13923050cee2SBarry Smith } 13930700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1394c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1395fd16b177SBarry Smith 1396251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1397251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 139855849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 13992b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1400e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1401536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1402f05ece33SBarry Smith #endif 140332077d6dSBarry Smith if (iascii) { 1404dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 140577431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 14067c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1407d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 14085ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1409c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1410d763cef2SBarry Smith } 14115ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1412193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 14132d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 14142d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1415d52bd9f3SBarry Smith if (ts->ops->view) { 1416d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1417d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1418d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1419d52bd9f3SBarry Smith } 14200f5bd95cSBarry Smith } else if (isstring) { 1421a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1422b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 142355849f57SBarry Smith } else if (isbinary) { 142455849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 142555849f57SBarry Smith MPI_Comm comm; 142655849f57SBarry Smith PetscMPIInt rank; 142755849f57SBarry Smith char type[256]; 142855849f57SBarry Smith 142955849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 143055849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 143155849f57SBarry Smith if (!rank) { 143255849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 143355849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 143455849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 143555849f57SBarry Smith } 143655849f57SBarry Smith if (ts->ops->view) { 143755849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 143855849f57SBarry Smith } 1439f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1440f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 14412d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 14422d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 14432b0a91c0SBarry Smith } else if (isdraw) { 14442b0a91c0SBarry Smith PetscDraw draw; 14452b0a91c0SBarry Smith char str[36]; 144689fd9fafSBarry Smith PetscReal x,y,bottom,h; 14472b0a91c0SBarry Smith 14482b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 14492b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 14502b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 14512b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 14520298fd71SBarry Smith ierr = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 145389fd9fafSBarry Smith bottom = y - h; 14542b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 14552b0a91c0SBarry Smith if (ts->ops->view) { 14562b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 14572b0a91c0SBarry Smith } 14582b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1459e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1460536b137fSBarry Smith } else if (issaws) { 1461d45a07a7SBarry Smith PetscMPIInt rank; 14622657e9d9SBarry Smith const char *name; 14632657e9d9SBarry Smith 14642657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1465d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1466d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1467d45a07a7SBarry Smith char dir[1024]; 1468d45a07a7SBarry Smith 1469e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1470a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 14712657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 14722657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 14732657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1474d763cef2SBarry Smith } 14750acecf5bSBarry Smith if (ts->ops->view) { 14760acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 14770acecf5bSBarry Smith } 1478f05ece33SBarry Smith #endif 1479f05ece33SBarry Smith } 1480f05ece33SBarry Smith 1481b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1482251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1483b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1484d763cef2SBarry Smith PetscFunctionReturn(0); 1485d763cef2SBarry Smith } 1486d763cef2SBarry Smith 1487d763cef2SBarry Smith 14884a2ae208SSatish Balay #undef __FUNCT__ 14894a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1490b07ff414SBarry Smith /*@ 1491d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1492d763cef2SBarry Smith the timesteppers. 1493d763cef2SBarry Smith 14943f9fe445SBarry Smith Logically Collective on TS 1495d763cef2SBarry Smith 1496d763cef2SBarry Smith Input Parameters: 1497d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1498d763cef2SBarry Smith - usrP - optional user context 1499d763cef2SBarry Smith 1500d763cef2SBarry Smith Level: intermediate 1501d763cef2SBarry Smith 1502d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1503d763cef2SBarry Smith 1504d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1505d763cef2SBarry Smith @*/ 15067087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1507d763cef2SBarry Smith { 1508d763cef2SBarry Smith PetscFunctionBegin; 15090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1510d763cef2SBarry Smith ts->user = usrP; 1511d763cef2SBarry Smith PetscFunctionReturn(0); 1512d763cef2SBarry Smith } 1513d763cef2SBarry Smith 15144a2ae208SSatish Balay #undef __FUNCT__ 15154a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1516b07ff414SBarry Smith /*@ 1517d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1518d763cef2SBarry Smith timestepper. 1519d763cef2SBarry Smith 1520d763cef2SBarry Smith Not Collective 1521d763cef2SBarry Smith 1522d763cef2SBarry Smith Input Parameter: 1523d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1524d763cef2SBarry Smith 1525d763cef2SBarry Smith Output Parameter: 1526d763cef2SBarry Smith . usrP - user context 1527d763cef2SBarry Smith 1528d763cef2SBarry Smith Level: intermediate 1529d763cef2SBarry Smith 1530d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1531d763cef2SBarry Smith 1532d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1533d763cef2SBarry Smith @*/ 1534e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1535d763cef2SBarry Smith { 1536d763cef2SBarry Smith PetscFunctionBegin; 15370700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1538e71120c6SJed Brown *(void**)usrP = ts->user; 1539d763cef2SBarry Smith PetscFunctionReturn(0); 1540d763cef2SBarry Smith } 1541d763cef2SBarry Smith 15424a2ae208SSatish Balay #undef __FUNCT__ 15434a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1544d763cef2SBarry Smith /*@ 1545b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1546d763cef2SBarry Smith 1547d763cef2SBarry Smith Not Collective 1548d763cef2SBarry Smith 1549d763cef2SBarry Smith Input Parameter: 1550d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1551d763cef2SBarry Smith 1552d763cef2SBarry Smith Output Parameter: 1553b8123daeSJed Brown . iter - number of steps completed so far 1554d763cef2SBarry Smith 1555d763cef2SBarry Smith Level: intermediate 1556d763cef2SBarry Smith 1557d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 15589be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 1559d763cef2SBarry Smith @*/ 15607087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 1561d763cef2SBarry Smith { 1562d763cef2SBarry Smith PetscFunctionBegin; 15630700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15644482741eSBarry Smith PetscValidIntPointer(iter,2); 1565d763cef2SBarry Smith *iter = ts->steps; 1566d763cef2SBarry Smith PetscFunctionReturn(0); 1567d763cef2SBarry Smith } 1568d763cef2SBarry Smith 15694a2ae208SSatish Balay #undef __FUNCT__ 15704a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1571d763cef2SBarry Smith /*@ 1572d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1573d763cef2SBarry Smith as well as the initial time. 1574d763cef2SBarry Smith 15753f9fe445SBarry Smith Logically Collective on TS 1576d763cef2SBarry Smith 1577d763cef2SBarry Smith Input Parameters: 1578d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1579d763cef2SBarry Smith . initial_time - the initial time 1580d763cef2SBarry Smith - time_step - the size of the timestep 1581d763cef2SBarry Smith 1582d763cef2SBarry Smith Level: intermediate 1583d763cef2SBarry Smith 1584d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1585d763cef2SBarry Smith 1586d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1587d763cef2SBarry Smith @*/ 15887087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1589d763cef2SBarry Smith { 1590e144a568SJed Brown PetscErrorCode ierr; 1591e144a568SJed Brown 1592d763cef2SBarry Smith PetscFunctionBegin; 15930700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1594e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1595d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1596d763cef2SBarry Smith PetscFunctionReturn(0); 1597d763cef2SBarry Smith } 1598d763cef2SBarry Smith 15994a2ae208SSatish Balay #undef __FUNCT__ 16004a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1601d763cef2SBarry Smith /*@ 1602d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1603d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1604d763cef2SBarry Smith 16053f9fe445SBarry Smith Logically Collective on TS 1606d763cef2SBarry Smith 1607d763cef2SBarry Smith Input Parameters: 1608d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1609d763cef2SBarry Smith - time_step - the size of the timestep 1610d763cef2SBarry Smith 1611d763cef2SBarry Smith Level: intermediate 1612d763cef2SBarry Smith 1613d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1614d763cef2SBarry Smith 1615d763cef2SBarry Smith .keywords: TS, set, timestep 1616d763cef2SBarry Smith @*/ 16177087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1618d763cef2SBarry Smith { 1619d763cef2SBarry Smith PetscFunctionBegin; 16200700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1621c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1622d763cef2SBarry Smith ts->time_step = time_step; 162331748224SBarry Smith ts->time_step_orig = time_step; 1624d763cef2SBarry Smith PetscFunctionReturn(0); 1625d763cef2SBarry Smith } 1626d763cef2SBarry Smith 16274a2ae208SSatish Balay #undef __FUNCT__ 1628a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1629a43b19c4SJed Brown /*@ 163049354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 163149354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 163249354f04SShri Abhyankar or just return at the final time TS computed. 1633a43b19c4SJed Brown 1634a43b19c4SJed Brown Logically Collective on TS 1635a43b19c4SJed Brown 1636a43b19c4SJed Brown Input Parameter: 1637a43b19c4SJed Brown + ts - the time-step context 163849354f04SShri Abhyankar - eftopt - exact final time option 1639a43b19c4SJed Brown 1640a43b19c4SJed Brown Level: beginner 1641a43b19c4SJed Brown 1642a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 1643a43b19c4SJed Brown @*/ 164449354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 1645a43b19c4SJed Brown { 1646a43b19c4SJed Brown PetscFunctionBegin; 1647a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 164849354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 164949354f04SShri Abhyankar ts->exact_final_time = eftopt; 1650a43b19c4SJed Brown PetscFunctionReturn(0); 1651a43b19c4SJed Brown } 1652a43b19c4SJed Brown 1653a43b19c4SJed Brown #undef __FUNCT__ 16544a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1655d763cef2SBarry Smith /*@ 1656d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1657d763cef2SBarry Smith 1658d763cef2SBarry Smith Not Collective 1659d763cef2SBarry Smith 1660d763cef2SBarry Smith Input Parameter: 1661d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1662d763cef2SBarry Smith 1663d763cef2SBarry Smith Output Parameter: 1664d763cef2SBarry Smith . dt - the current timestep size 1665d763cef2SBarry Smith 1666d763cef2SBarry Smith Level: intermediate 1667d763cef2SBarry Smith 1668d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1669d763cef2SBarry Smith 1670d763cef2SBarry Smith .keywords: TS, get, timestep 1671d763cef2SBarry Smith @*/ 16727087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 1673d763cef2SBarry Smith { 1674d763cef2SBarry Smith PetscFunctionBegin; 16750700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1676f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1677d763cef2SBarry Smith *dt = ts->time_step; 1678d763cef2SBarry Smith PetscFunctionReturn(0); 1679d763cef2SBarry Smith } 1680d763cef2SBarry Smith 16814a2ae208SSatish Balay #undef __FUNCT__ 16824a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1683d8e5e3e6SSatish Balay /*@ 1684d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1685d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1686d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1687d763cef2SBarry Smith the solution at the next timestep has been calculated. 1688d763cef2SBarry Smith 1689d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1690d763cef2SBarry Smith 1691d763cef2SBarry Smith Input Parameter: 1692d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1693d763cef2SBarry Smith 1694d763cef2SBarry Smith Output Parameter: 1695d763cef2SBarry Smith . v - the vector containing the solution 1696d763cef2SBarry Smith 1697d763cef2SBarry Smith Level: intermediate 1698d763cef2SBarry Smith 1699d763cef2SBarry Smith .seealso: TSGetTimeStep() 1700d763cef2SBarry Smith 1701d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1702d763cef2SBarry Smith @*/ 17037087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1704d763cef2SBarry Smith { 1705d763cef2SBarry Smith PetscFunctionBegin; 17060700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17074482741eSBarry Smith PetscValidPointer(v,2); 17088737fe31SLisandro Dalcin *v = ts->vec_sol; 1709d763cef2SBarry Smith PetscFunctionReturn(0); 1710d763cef2SBarry Smith } 1711d763cef2SBarry Smith 1712c235aa8dSHong Zhang #undef __FUNCT__ 17130cf82b59SBarry Smith #define __FUNCT__ "TSAdjointGetCostGradients" 1714c235aa8dSHong Zhang /*@ 17150cf82b59SBarry Smith TSAdjointGetCostGradients - Returns the gradients from the TSAdjointSolve() 1716c235aa8dSHong Zhang 1717c235aa8dSHong Zhang Not Collective, but Vec returned is parallel if TS is parallel 1718c235aa8dSHong Zhang 1719c235aa8dSHong Zhang Input Parameter: 1720c235aa8dSHong Zhang . ts - the TS context obtained from TSCreate() 1721c235aa8dSHong Zhang 1722c235aa8dSHong Zhang Output Parameter: 1723abc2977eSBarry Smith + lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 1724abc2977eSBarry Smith - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 1725c235aa8dSHong Zhang 1726c235aa8dSHong Zhang Level: intermediate 1727c235aa8dSHong Zhang 1728c235aa8dSHong Zhang .seealso: TSGetTimeStep() 1729c235aa8dSHong Zhang 1730c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity 1731c235aa8dSHong Zhang @*/ 1732abc2977eSBarry Smith PetscErrorCode TSAdjointGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu) 1733c235aa8dSHong Zhang { 1734c235aa8dSHong Zhang PetscFunctionBegin; 1735c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1736abc2977eSBarry Smith if (numcost) *numcost = ts->numcost; 1737abc2977eSBarry Smith if (lambda) *lambda = ts->vecs_sensi; 1738abc2977eSBarry Smith if (mu) *mu = ts->vecs_sensip; 1739c235aa8dSHong Zhang PetscFunctionReturn(0); 1740c235aa8dSHong Zhang } 1741c235aa8dSHong Zhang 1742bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 17434a2ae208SSatish Balay #undef __FUNCT__ 1744bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1745d8e5e3e6SSatish Balay /*@ 1746bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1747d763cef2SBarry Smith 1748bdad233fSMatthew Knepley Not collective 1749d763cef2SBarry Smith 1750bdad233fSMatthew Knepley Input Parameters: 1751bdad233fSMatthew Knepley + ts - The TS 1752bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1753d763cef2SBarry Smith .vb 17540910c330SBarry Smith U_t - A U = 0 (linear) 17550910c330SBarry Smith U_t - A(t) U = 0 (linear) 17560910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 1757d763cef2SBarry Smith .ve 1758d763cef2SBarry Smith 1759d763cef2SBarry Smith Level: beginner 1760d763cef2SBarry Smith 1761bdad233fSMatthew Knepley .keywords: TS, problem type 1762bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1763d763cef2SBarry Smith @*/ 17647087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1765a7cc72afSBarry Smith { 17669e2a6581SJed Brown PetscErrorCode ierr; 17679e2a6581SJed Brown 1768d763cef2SBarry Smith PetscFunctionBegin; 17690700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1770bdad233fSMatthew Knepley ts->problem_type = type; 17719e2a6581SJed Brown if (type == TS_LINEAR) { 17729e2a6581SJed Brown SNES snes; 17739e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 17749e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 17759e2a6581SJed Brown } 1776d763cef2SBarry Smith PetscFunctionReturn(0); 1777d763cef2SBarry Smith } 1778d763cef2SBarry Smith 1779bdad233fSMatthew Knepley #undef __FUNCT__ 1780bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1781bdad233fSMatthew Knepley /*@C 1782bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1783bdad233fSMatthew Knepley 1784bdad233fSMatthew Knepley Not collective 1785bdad233fSMatthew Knepley 1786bdad233fSMatthew Knepley Input Parameter: 1787bdad233fSMatthew Knepley . ts - The TS 1788bdad233fSMatthew Knepley 1789bdad233fSMatthew Knepley Output Parameter: 1790bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1791bdad233fSMatthew Knepley .vb 1792089b2837SJed Brown M U_t = A U 1793089b2837SJed Brown M(t) U_t = A(t) U 1794b5abc632SBarry Smith F(t,U,U_t) 1795bdad233fSMatthew Knepley .ve 1796bdad233fSMatthew Knepley 1797bdad233fSMatthew Knepley Level: beginner 1798bdad233fSMatthew Knepley 1799bdad233fSMatthew Knepley .keywords: TS, problem type 1800bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1801bdad233fSMatthew Knepley @*/ 18027087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1803a7cc72afSBarry Smith { 1804bdad233fSMatthew Knepley PetscFunctionBegin; 18050700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 18064482741eSBarry Smith PetscValidIntPointer(type,2); 1807bdad233fSMatthew Knepley *type = ts->problem_type; 1808bdad233fSMatthew Knepley PetscFunctionReturn(0); 1809bdad233fSMatthew Knepley } 1810d763cef2SBarry Smith 18114a2ae208SSatish Balay #undef __FUNCT__ 18124a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1813d763cef2SBarry Smith /*@ 1814d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1815d763cef2SBarry Smith of a timestepper. 1816d763cef2SBarry Smith 1817d763cef2SBarry Smith Collective on TS 1818d763cef2SBarry Smith 1819d763cef2SBarry Smith Input Parameter: 1820d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1821d763cef2SBarry Smith 1822d763cef2SBarry Smith Notes: 1823d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1824d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1825d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1826d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1827d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1828d763cef2SBarry Smith 1829d763cef2SBarry Smith Level: advanced 1830d763cef2SBarry Smith 1831d763cef2SBarry Smith .keywords: TS, timestep, setup 1832d763cef2SBarry Smith 1833d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1834d763cef2SBarry Smith @*/ 18357087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1836d763cef2SBarry Smith { 1837dfbe8321SBarry Smith PetscErrorCode ierr; 18386c6b9e74SPeter Brune DM dm; 18396c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 1840d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 18416c6b9e74SPeter Brune TSIJacobian ijac; 18426c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1843d763cef2SBarry Smith 1844d763cef2SBarry Smith PetscFunctionBegin; 18450700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1846277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1847277b19d0SLisandro Dalcin 18482c18e0fdSBarry Smith ts->total_steps = 0; 18497adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 18509596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1851d763cef2SBarry Smith } 1852277b19d0SLisandro Dalcin 1853277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1854277b19d0SLisandro Dalcin 1855c235aa8dSHong Zhang 1856552698daSJed Brown ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 18571c3436cfSJed Brown 1858e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 1859e1244c69SJed Brown Mat Amat,Pmat; 1860e1244c69SJed Brown SNES snes; 1861e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1862e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 1863e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 1864e1244c69SJed Brown * have displaced the RHS matrix */ 1865e1244c69SJed Brown if (Amat == ts->Arhs) { 1866e1244c69SJed Brown ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 1867e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 1868e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 1869e1244c69SJed Brown } 1870e1244c69SJed Brown if (Pmat == ts->Brhs) { 1871e1244c69SJed Brown ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 1872e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 1873e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 1874e1244c69SJed Brown } 1875e1244c69SJed Brown } 1876277b19d0SLisandro Dalcin if (ts->ops->setup) { 1877000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1878277b19d0SLisandro Dalcin } 1879277b19d0SLisandro Dalcin 18806c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 18816c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 18826c6b9e74SPeter Brune */ 18836c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 18840298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 18856c6b9e74SPeter Brune if (!func) { 18866c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 18876c6b9e74SPeter Brune } 18886c6b9e74SPeter 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. 18896c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 18906c6b9e74SPeter Brune */ 18910298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 18920298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 18930298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 18946c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 18956c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 18966c6b9e74SPeter Brune } 1897277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1898277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1899277b19d0SLisandro Dalcin } 1900277b19d0SLisandro Dalcin 1901277b19d0SLisandro Dalcin #undef __FUNCT__ 1902f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp" 1903f6a906c0SBarry Smith /*@ 1904f6a906c0SBarry Smith TSAdjointSetUp - Sets up the internal data structures for the later use 1905f6a906c0SBarry Smith of an adjoint solver 1906f6a906c0SBarry Smith 1907f6a906c0SBarry Smith Collective on TS 1908f6a906c0SBarry Smith 1909f6a906c0SBarry Smith Input Parameter: 1910f6a906c0SBarry Smith . ts - the TS context obtained from TSCreate() 1911f6a906c0SBarry Smith 1912f6a906c0SBarry Smith Notes: 1913f6a906c0SBarry Smith For basic use of the TS solvers the user need not explicitly call 1914f6a906c0SBarry Smith TSSetUp(), since these actions will automatically occur during 1915f6a906c0SBarry Smith the call to TSStep(). However, if one wishes to control this 1916f6a906c0SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1917f6a906c0SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1918f6a906c0SBarry Smith 1919f6a906c0SBarry Smith Level: advanced 1920f6a906c0SBarry Smith 1921f6a906c0SBarry Smith .keywords: TS, timestep, setup 1922f6a906c0SBarry Smith 1923f6a906c0SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1924f6a906c0SBarry Smith @*/ 1925f6a906c0SBarry Smith PetscErrorCode TSAdjointSetUp(TS ts) 1926f6a906c0SBarry Smith { 1927f6a906c0SBarry Smith PetscErrorCode ierr; 1928f6a906c0SBarry Smith 1929f6a906c0SBarry Smith PetscFunctionBegin; 1930f6a906c0SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1931f6a906c0SBarry Smith if (ts->adjointsetupcalled) PetscFunctionReturn(0); 19320cf82b59SBarry Smith if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSAdjointSetCostGradients() first"); 1933d8151eeaSBarry Smith 1934d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 1935d8151eeaSBarry Smith if (ts->vecs_sensip){ 1936d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 1937d8151eeaSBarry Smith } 1938d8151eeaSBarry Smith 193942f2b339SBarry Smith if (ts->ops->adjointsetup) { 194042f2b339SBarry Smith ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr); 1941f6a906c0SBarry Smith } 1942f6a906c0SBarry Smith ts->adjointsetupcalled = PETSC_TRUE; 1943f6a906c0SBarry Smith PetscFunctionReturn(0); 1944f6a906c0SBarry Smith } 1945f6a906c0SBarry Smith 1946f6a906c0SBarry Smith #undef __FUNCT__ 1947277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1948277b19d0SLisandro Dalcin /*@ 1949277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1950277b19d0SLisandro Dalcin 1951277b19d0SLisandro Dalcin Collective on TS 1952277b19d0SLisandro Dalcin 1953277b19d0SLisandro Dalcin Input Parameter: 1954277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1955277b19d0SLisandro Dalcin 1956277b19d0SLisandro Dalcin Level: beginner 1957277b19d0SLisandro Dalcin 1958277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1959277b19d0SLisandro Dalcin 1960277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1961277b19d0SLisandro Dalcin @*/ 1962277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1963277b19d0SLisandro Dalcin { 1964277b19d0SLisandro Dalcin PetscErrorCode ierr; 1965277b19d0SLisandro Dalcin 1966277b19d0SLisandro Dalcin PetscFunctionBegin; 1967277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1968b18ea86cSHong Zhang 1969277b19d0SLisandro Dalcin if (ts->ops->reset) { 1970277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1971277b19d0SLisandro Dalcin } 1972277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 1973bbd56ea5SKarl Rupp 19744e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 19754e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1976214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 19776bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1978e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 1979e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 198038637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 1981d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 1982d8151eeaSBarry Smith if (ts->vecs_drdp){ 1983d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 1984d8151eeaSBarry Smith } 1985d8151eeaSBarry Smith ts->vecs_sensi = NULL; 1986d8151eeaSBarry Smith ts->vecs_sensip = NULL; 1987ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 19882c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 198936eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 1990277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1991d763cef2SBarry Smith PetscFunctionReturn(0); 1992d763cef2SBarry Smith } 1993d763cef2SBarry Smith 19944a2ae208SSatish Balay #undef __FUNCT__ 19954a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1996d8e5e3e6SSatish Balay /*@ 1997d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1998d763cef2SBarry Smith with TSCreate(). 1999d763cef2SBarry Smith 2000d763cef2SBarry Smith Collective on TS 2001d763cef2SBarry Smith 2002d763cef2SBarry Smith Input Parameter: 2003d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2004d763cef2SBarry Smith 2005d763cef2SBarry Smith Level: beginner 2006d763cef2SBarry Smith 2007d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2008d763cef2SBarry Smith 2009d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2010d763cef2SBarry Smith @*/ 20116bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2012d763cef2SBarry Smith { 20136849ba73SBarry Smith PetscErrorCode ierr; 2014d763cef2SBarry Smith 2015d763cef2SBarry Smith PetscFunctionBegin; 20166bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 20176bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 20186bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2019d763cef2SBarry Smith 20206bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2021277b19d0SLisandro Dalcin 2022e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2023e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 20246bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 20256d4c513bSLisandro Dalcin 2026bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2027bc952696SBarry Smith 202884df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 2029aeb4809dSShri Abhyankar if ((*ts)->event) { 2030aeb4809dSShri Abhyankar ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr); 2031aeb4809dSShri Abhyankar } 20326bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 20336bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 20346bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 20356d4c513bSLisandro Dalcin 2036a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2037d763cef2SBarry Smith PetscFunctionReturn(0); 2038d763cef2SBarry Smith } 2039d763cef2SBarry Smith 20404a2ae208SSatish Balay #undef __FUNCT__ 20414a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 2042d8e5e3e6SSatish Balay /*@ 2043d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2044d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2045d763cef2SBarry Smith 2046d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2047d763cef2SBarry Smith 2048d763cef2SBarry Smith Input Parameter: 2049d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2050d763cef2SBarry Smith 2051d763cef2SBarry Smith Output Parameter: 2052d763cef2SBarry Smith . snes - the nonlinear solver context 2053d763cef2SBarry Smith 2054d763cef2SBarry Smith Notes: 2055d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2056d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 205794b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2058d763cef2SBarry Smith 2059d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 20600298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2061d763cef2SBarry Smith 2062d763cef2SBarry Smith Level: beginner 2063d763cef2SBarry Smith 2064d763cef2SBarry Smith .keywords: timestep, get, SNES 2065d763cef2SBarry Smith @*/ 20667087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2067d763cef2SBarry Smith { 2068d372ba47SLisandro Dalcin PetscErrorCode ierr; 2069d372ba47SLisandro Dalcin 2070d763cef2SBarry Smith PetscFunctionBegin; 20710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20724482741eSBarry Smith PetscValidPointer(snes,2); 2073d372ba47SLisandro Dalcin if (!ts->snes) { 2074ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 20750298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 20763bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2077d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2078496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 20799e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 20809e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 20819e2a6581SJed Brown } 2082d372ba47SLisandro Dalcin } 2083d763cef2SBarry Smith *snes = ts->snes; 2084d763cef2SBarry Smith PetscFunctionReturn(0); 2085d763cef2SBarry Smith } 2086d763cef2SBarry Smith 20874a2ae208SSatish Balay #undef __FUNCT__ 2088deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES" 2089deb2cd25SJed Brown /*@ 2090deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2091deb2cd25SJed Brown 2092deb2cd25SJed Brown Collective 2093deb2cd25SJed Brown 2094deb2cd25SJed Brown Input Parameter: 2095deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2096deb2cd25SJed Brown - snes - the nonlinear solver context 2097deb2cd25SJed Brown 2098deb2cd25SJed Brown Notes: 2099deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2100deb2cd25SJed Brown 2101deb2cd25SJed Brown Level: developer 2102deb2cd25SJed Brown 2103deb2cd25SJed Brown .keywords: timestep, set, SNES 2104deb2cd25SJed Brown @*/ 2105deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2106deb2cd25SJed Brown { 2107deb2cd25SJed Brown PetscErrorCode ierr; 2108d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2109deb2cd25SJed Brown 2110deb2cd25SJed Brown PetscFunctionBegin; 2111deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2112deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2113deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2114deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2115bbd56ea5SKarl Rupp 2116deb2cd25SJed Brown ts->snes = snes; 2117bbd56ea5SKarl Rupp 21180298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 21190298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2120740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 21210298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2122740132f1SEmil Constantinescu } 2123deb2cd25SJed Brown PetscFunctionReturn(0); 2124deb2cd25SJed Brown } 2125deb2cd25SJed Brown 2126deb2cd25SJed Brown #undef __FUNCT__ 212794b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 2128d8e5e3e6SSatish Balay /*@ 212994b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2130d763cef2SBarry Smith a TS (timestepper) context. 2131d763cef2SBarry Smith 213294b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2133d763cef2SBarry Smith 2134d763cef2SBarry Smith Input Parameter: 2135d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2136d763cef2SBarry Smith 2137d763cef2SBarry Smith Output Parameter: 213894b7f48cSBarry Smith . ksp - the nonlinear solver context 2139d763cef2SBarry Smith 2140d763cef2SBarry Smith Notes: 214194b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2142d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2143d763cef2SBarry Smith KSP and PC contexts as well. 2144d763cef2SBarry Smith 214594b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 21460298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2147d763cef2SBarry Smith 2148d763cef2SBarry Smith Level: beginner 2149d763cef2SBarry Smith 215094b7f48cSBarry Smith .keywords: timestep, get, KSP 2151d763cef2SBarry Smith @*/ 21527087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2153d763cef2SBarry Smith { 2154d372ba47SLisandro Dalcin PetscErrorCode ierr; 2155089b2837SJed Brown SNES snes; 2156d372ba47SLisandro Dalcin 2157d763cef2SBarry Smith PetscFunctionBegin; 21580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 21594482741eSBarry Smith PetscValidPointer(ksp,2); 216017186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2161e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2162089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2163089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2164d763cef2SBarry Smith PetscFunctionReturn(0); 2165d763cef2SBarry Smith } 2166d763cef2SBarry Smith 2167d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2168d763cef2SBarry Smith 21694a2ae208SSatish Balay #undef __FUNCT__ 2170adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 2171adb62b0dSMatthew Knepley /*@ 2172adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2173adb62b0dSMatthew Knepley maximum time for iteration. 2174adb62b0dSMatthew Knepley 21753f9fe445SBarry Smith Not Collective 2176adb62b0dSMatthew Knepley 2177adb62b0dSMatthew Knepley Input Parameters: 2178adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 21790298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 21800298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2181adb62b0dSMatthew Knepley 2182adb62b0dSMatthew Knepley Level: intermediate 2183adb62b0dSMatthew Knepley 2184adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2185adb62b0dSMatthew Knepley @*/ 21867087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2187adb62b0dSMatthew Knepley { 2188adb62b0dSMatthew Knepley PetscFunctionBegin; 21890700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2190abc0a331SBarry Smith if (maxsteps) { 21914482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2192adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2193adb62b0dSMatthew Knepley } 2194abc0a331SBarry Smith if (maxtime) { 21954482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2196adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2197adb62b0dSMatthew Knepley } 2198adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2199adb62b0dSMatthew Knepley } 2200adb62b0dSMatthew Knepley 2201adb62b0dSMatthew Knepley #undef __FUNCT__ 22024a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 2203d763cef2SBarry Smith /*@ 2204d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2205d763cef2SBarry Smith maximum time for iteration. 2206d763cef2SBarry Smith 22073f9fe445SBarry Smith Logically Collective on TS 2208d763cef2SBarry Smith 2209d763cef2SBarry Smith Input Parameters: 2210d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2211d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2212d763cef2SBarry Smith - maxtime - final time to iterate to 2213d763cef2SBarry Smith 2214d763cef2SBarry Smith Options Database Keys: 2215d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 22163bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2217d763cef2SBarry Smith 2218d763cef2SBarry Smith Notes: 2219d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2220d763cef2SBarry Smith 2221d763cef2SBarry Smith Level: intermediate 2222d763cef2SBarry Smith 2223d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2224a43b19c4SJed Brown 2225a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2226d763cef2SBarry Smith @*/ 22277087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2228d763cef2SBarry Smith { 2229d763cef2SBarry Smith PetscFunctionBegin; 22300700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2231c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2232c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 223339b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 223439b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2235d763cef2SBarry Smith PetscFunctionReturn(0); 2236d763cef2SBarry Smith } 2237d763cef2SBarry Smith 22384a2ae208SSatish Balay #undef __FUNCT__ 22394a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 2240d763cef2SBarry Smith /*@ 2241d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2242d763cef2SBarry Smith for use by the TS routines. 2243d763cef2SBarry Smith 22443f9fe445SBarry Smith Logically Collective on TS and Vec 2245d763cef2SBarry Smith 2246d763cef2SBarry Smith Input Parameters: 2247d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 22480910c330SBarry Smith - u - the solution vector 2249d763cef2SBarry Smith 2250d763cef2SBarry Smith Level: beginner 2251d763cef2SBarry Smith 2252d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2253d763cef2SBarry Smith @*/ 22540910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2255d763cef2SBarry Smith { 22568737fe31SLisandro Dalcin PetscErrorCode ierr; 2257496e6a7aSJed Brown DM dm; 22588737fe31SLisandro Dalcin 2259d763cef2SBarry Smith PetscFunctionBegin; 22600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22610910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 22620910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 22636bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2264bbd56ea5SKarl Rupp 22650910c330SBarry Smith ts->vec_sol = u; 2266bbd56ea5SKarl Rupp 2267496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 22680910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2269d763cef2SBarry Smith PetscFunctionReturn(0); 2270d763cef2SBarry Smith } 2271d763cef2SBarry Smith 2272e74ef692SMatthew Knepley #undef __FUNCT__ 227373b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps" 227473b18844SBarry Smith /*@ 227573b18844SBarry Smith TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time 227673b18844SBarry Smith 227773b18844SBarry Smith Logically Collective on TS 227873b18844SBarry Smith 227973b18844SBarry Smith Input Parameters: 228073b18844SBarry Smith + ts - the TS context obtained from TSCreate() 228173b18844SBarry Smith . steps - number of steps to use 228273b18844SBarry Smith 228373b18844SBarry Smith Level: intermediate 228473b18844SBarry Smith 228573b18844SBarry Smith Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this 228673b18844SBarry Smith so as to integrate back to less than the original timestep 228773b18844SBarry Smith 228873b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations 228973b18844SBarry Smith 229073b18844SBarry Smith .seealso: TSSetExactFinalTime() 229173b18844SBarry Smith @*/ 229273b18844SBarry Smith PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps) 229373b18844SBarry Smith { 229473b18844SBarry Smith PetscFunctionBegin; 229573b18844SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 229673b18844SBarry Smith PetscValidLogicalCollectiveInt(ts,steps,2); 229773b18844SBarry Smith if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps"); 229873b18844SBarry 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"); 229973b18844SBarry Smith ts->adjoint_max_steps = steps; 230073b18844SBarry Smith PetscFunctionReturn(0); 230173b18844SBarry Smith } 230273b18844SBarry Smith 230373b18844SBarry Smith #undef __FUNCT__ 23040cf82b59SBarry Smith #define __FUNCT__ "TSAdjointSetCostGradients" 2305c235aa8dSHong Zhang /*@ 23060cf82b59SBarry Smith TSAdjointSetCostGradients - Sets the initial value of the gradients of the cost function w.r.t. initial conditions and w.r.t. the problem parameters 23070cf82b59SBarry Smith for use by the TSAdjoint routines. 2308c235aa8dSHong Zhang 2309c235aa8dSHong Zhang Logically Collective on TS and Vec 2310c235aa8dSHong Zhang 2311c235aa8dSHong Zhang Input Parameters: 2312c235aa8dSHong Zhang + ts - the TS context obtained from TSCreate() 2313abc2977eSBarry 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 2314abc2977eSBarry Smith - mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters 2315c235aa8dSHong Zhang 2316c235aa8dSHong Zhang Level: beginner 2317c235aa8dSHong Zhang 2318abc2977eSBarry Smith Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime mu_i = df/dp|finaltime 23190cf82b59SBarry Smith 2320c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions 2321c235aa8dSHong Zhang @*/ 2322abc2977eSBarry Smith PetscErrorCode TSAdjointSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu) 2323c235aa8dSHong Zhang { 2324c235aa8dSHong Zhang PetscFunctionBegin; 2325c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2326abc2977eSBarry Smith PetscValidPointer(lambda,2); 2327abc2977eSBarry Smith ts->vecs_sensi = lambda; 2328abc2977eSBarry Smith ts->vecs_sensip = mu; 2329abc2977eSBarry Smith ts->numcost = numcost; 2330ad8e2604SHong Zhang PetscFunctionReturn(0); 2331ad8e2604SHong Zhang } 2332ad8e2604SHong Zhang 2333ad8e2604SHong Zhang #undef __FUNCT__ 23345bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian" 2335ad8e2604SHong Zhang /*@C 23360cf82b59SBarry 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. 2337ad8e2604SHong Zhang 2338ad8e2604SHong Zhang Logically Collective on TS 2339ad8e2604SHong Zhang 2340ad8e2604SHong Zhang Input Parameters: 2341ad8e2604SHong Zhang + ts - The TS context obtained from TSCreate() 2342ad8e2604SHong Zhang - func - The function 2343ad8e2604SHong Zhang 2344ad8e2604SHong Zhang Calling sequence of func: 23450cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx); 234605755b9cSHong Zhang + t - current timestep 23470cf82b59SBarry Smith . y - input vector (current ODE solution) 234805755b9cSHong Zhang . A - output matrix 234905755b9cSHong Zhang - ctx - [optional] user-defined function context 2350ad8e2604SHong Zhang 2351ad8e2604SHong Zhang Level: intermediate 2352ad8e2604SHong Zhang 23530cf82b59SBarry 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 23540cf82b59SBarry Smith 2355ad8e2604SHong Zhang .keywords: TS, sensitivity 2356ad8e2604SHong Zhang .seealso: 2357ad8e2604SHong Zhang @*/ 23585bf8c567SBarry Smith PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 2359ad8e2604SHong Zhang { 2360ad8e2604SHong Zhang PetscErrorCode ierr; 2361ad8e2604SHong Zhang 2362ad8e2604SHong Zhang PetscFunctionBegin; 2363ad8e2604SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2364ad8e2604SHong Zhang if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 2365ad8e2604SHong Zhang 2366ad8e2604SHong Zhang ts->rhsjacobianp = func; 2367ad8e2604SHong Zhang ts->rhsjacobianpctx = ctx; 2368ad8e2604SHong Zhang if(Amat) { 2369ad8e2604SHong Zhang ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 2370ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2371ad8e2604SHong Zhang ts->Jacp = Amat; 2372ad8e2604SHong Zhang } 2373ad8e2604SHong Zhang PetscFunctionReturn(0); 2374ad8e2604SHong Zhang } 2375ad8e2604SHong Zhang 2376ad8e2604SHong Zhang #undef __FUNCT__ 23775bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian" 23780cf82b59SBarry Smith /*@C 23795bf8c567SBarry Smith TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function. 2380ad8e2604SHong Zhang 2381ad8e2604SHong Zhang Collective on TS 2382ad8e2604SHong Zhang 2383ad8e2604SHong Zhang Input Parameters: 2384ad8e2604SHong Zhang . ts - The TS context obtained from TSCreate() 2385ad8e2604SHong Zhang 2386ad8e2604SHong Zhang Level: developer 2387ad8e2604SHong Zhang 2388ad8e2604SHong Zhang .keywords: TS, sensitivity 23895bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian() 2390ad8e2604SHong Zhang @*/ 23915bf8c567SBarry Smith PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat) 2392ad8e2604SHong Zhang { 2393ad8e2604SHong Zhang PetscErrorCode ierr; 2394ad8e2604SHong Zhang 2395ad8e2604SHong Zhang PetscFunctionBegin; 2396ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2397ad8e2604SHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 2398ad8e2604SHong Zhang PetscValidPointer(Amat,4); 2399ad8e2604SHong Zhang 2400ad8e2604SHong Zhang PetscStackPush("TS user JacobianP function for sensitivity analysis"); 2401ad8e2604SHong Zhang ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 2402ad8e2604SHong Zhang PetscStackPop; 2403c235aa8dSHong Zhang PetscFunctionReturn(0); 2404c235aa8dSHong Zhang } 2405c235aa8dSHong Zhang 2406c235aa8dSHong Zhang #undef __FUNCT__ 2407d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetCostIntegrand" 24086fd0887fSHong Zhang /*@C 24090cf82b59SBarry Smith TSAdjointSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions 24106fd0887fSHong Zhang 24116fd0887fSHong Zhang Logically Collective on TS 24126fd0887fSHong Zhang 24136fd0887fSHong Zhang Input Parameters: 24146fd0887fSHong Zhang + ts - the TS context obtained from TSCreate() 2415abc2977eSBarry Smith . numcost - number of gradients to be computed, this is the number of cost functions 24160cf82b59SBarry Smith . rf - routine for evaluating the integrand function 2417b612ec54SBarry Smith . drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y 2418b612ec54SBarry Smith . drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p 2419b612ec54SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 24206fd0887fSHong Zhang 24210cf82b59SBarry Smith Calling sequence of rf: 24220cf82b59SBarry Smith $ rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx); 24236fd0887fSHong Zhang 24246fd0887fSHong Zhang + t - current timestep 24250cf82b59SBarry Smith . y - input vector 24260cf82b59SBarry Smith . f - function result; one vector entry for each cost function 24276fd0887fSHong Zhang - ctx - [optional] user-defined function context 24286fd0887fSHong Zhang 2429b612ec54SBarry Smith Calling sequence of drdyf: 24300cf82b59SBarry Smith $ PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx); 2431b612ec54SBarry Smith 2432b612ec54SBarry Smith Calling sequence of drdpf: 24330cf82b59SBarry Smith $ PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx); 2434b612ec54SBarry Smith 2435b612ec54SBarry Smith Level: intermediate 24366fd0887fSHong Zhang 2437abc2977eSBarry Smith Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions 24380cf82b59SBarry Smith 24396fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 24406fd0887fSHong Zhang 24410cf82b59SBarry Smith .seealso: TSAdjointSetRHSJacobian(),TSAdjointGetCostGradients(), TSAdjointSetCostGradients() 24426fd0887fSHong Zhang @*/ 2443abc2977eSBarry Smith PetscErrorCode TSAdjointSetCostIntegrand(TS ts,PetscInt numcost, PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*), 2444d8151eeaSBarry Smith PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*), 2445d8151eeaSBarry Smith PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),void *ctx) 24466fd0887fSHong Zhang { 24476fd0887fSHong Zhang PetscErrorCode ierr; 24486fd0887fSHong Zhang 24496fd0887fSHong Zhang PetscFunctionBegin; 24506fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2451abc2977eSBarry Smith if (!ts->numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Call TSAdjointSetCostGradients() first so that the number of cost functions can be determined."); 2452abc2977eSBarry Smith if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSAdjointSetCostIntegrand()) is inconsistent with the one set by TSAdjointSetCostGradients()"); 24536fd0887fSHong Zhang 2454abc2977eSBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr); 24552c39e106SBarry Smith ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr); 24560cf82b59SBarry Smith ts->costintegrand = rf; 245705755b9cSHong Zhang ts->costintegrandctx = ctx; 2458b612ec54SBarry Smith ts->drdyfunction = drdyf; 2459b612ec54SBarry Smith ts->drdpfunction = drdpf; 24606fd0887fSHong Zhang PetscFunctionReturn(0); 24616fd0887fSHong Zhang } 24626fd0887fSHong Zhang 24636fd0887fSHong Zhang #undef __FUNCT__ 24642c39e106SBarry Smith #define __FUNCT__ "TSAdjointGetCostIntegral" 246536eaed60SHong Zhang /*@ 24662c39e106SBarry Smith TSAdjointGetCostIntegral - Returns the values of the integral term in the cost functions. 246736eaed60SHong Zhang It is valid to call the routine after a backward run. 246836eaed60SHong Zhang 246936eaed60SHong Zhang Not Collective 247036eaed60SHong Zhang 247136eaed60SHong Zhang Input Parameter: 247236eaed60SHong Zhang . ts - the TS context obtained from TSCreate() 247336eaed60SHong Zhang 247436eaed60SHong Zhang Output Parameter: 24752c39e106SBarry Smith . v - the vector containing the integrals for each cost function 247636eaed60SHong Zhang 247736eaed60SHong Zhang Level: intermediate 247836eaed60SHong Zhang 2479d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand() 248036eaed60SHong Zhang 248136eaed60SHong Zhang .keywords: TS, sensitivity analysis 248236eaed60SHong Zhang @*/ 24832c39e106SBarry Smith PetscErrorCode TSAdjointGetCostIntegral(TS ts,Vec *v) 248436eaed60SHong Zhang { 248536eaed60SHong Zhang PetscFunctionBegin; 248636eaed60SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 248736eaed60SHong Zhang PetscValidPointer(v,2); 24882c39e106SBarry Smith *v = ts->vec_costintegral; 248936eaed60SHong Zhang PetscFunctionReturn(0); 249036eaed60SHong Zhang } 249136eaed60SHong Zhang 249236eaed60SHong Zhang #undef __FUNCT__ 2493d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand" 24946fd0887fSHong Zhang /*@ 24952c39e106SBarry Smith TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions. 24966fd0887fSHong Zhang 24976fd0887fSHong Zhang Input Parameters: 24986fd0887fSHong Zhang + ts - the TS context 24996fd0887fSHong Zhang . t - current time 25000cf82b59SBarry Smith - y - state vector, i.e. current solution 25016fd0887fSHong Zhang 25026fd0887fSHong Zhang Output Parameter: 2503abc2977eSBarry Smith . q - vector of size numcost to hold the outputs 25046fd0887fSHong Zhang 25056fd0887fSHong Zhang Note: 25066fd0887fSHong Zhang Most users should not need to explicitly call this routine, as it 25076fd0887fSHong Zhang is used internally within the sensitivity analysis context. 25086fd0887fSHong Zhang 25096fd0887fSHong Zhang Level: developer 25106fd0887fSHong Zhang 25116fd0887fSHong Zhang .keywords: TS, compute 25126fd0887fSHong Zhang 2513d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand() 25146fd0887fSHong Zhang @*/ 25150cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q) 25166fd0887fSHong Zhang { 25176fd0887fSHong Zhang PetscErrorCode ierr; 25186fd0887fSHong Zhang 25196fd0887fSHong Zhang PetscFunctionBegin; 25206fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25210cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 25226fd0887fSHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,4); 25236fd0887fSHong Zhang 25240cf82b59SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 2525e4680132SHong Zhang if (ts->costintegrand) { 2526e4680132SHong Zhang PetscStackPush("TS user integrand in the cost function"); 25270cf82b59SBarry Smith ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr); 25286fd0887fSHong Zhang PetscStackPop; 25296fd0887fSHong Zhang } else { 25306fd0887fSHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 25316fd0887fSHong Zhang } 25326fd0887fSHong Zhang 25330cf82b59SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 25346fd0887fSHong Zhang PetscFunctionReturn(0); 25356fd0887fSHong Zhang } 25366fd0887fSHong Zhang 25376fd0887fSHong Zhang #undef __FUNCT__ 2538d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction" 253905755b9cSHong Zhang /*@ 2540d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 254105755b9cSHong Zhang 254205755b9cSHong Zhang Collective on TS 254305755b9cSHong Zhang 254405755b9cSHong Zhang Input Parameters: 254505755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 254605755b9cSHong Zhang 254705755b9cSHong Zhang Notes: 2548d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 254905755b9cSHong Zhang so most users would not generally call this routine themselves. 255005755b9cSHong Zhang 255105755b9cSHong Zhang Level: developer 255205755b9cSHong Zhang 255305755b9cSHong Zhang .keywords: TS, sensitivity 2554d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction() 255505755b9cSHong Zhang @*/ 25560cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy) 255705755b9cSHong Zhang { 255805755b9cSHong Zhang PetscErrorCode ierr; 255905755b9cSHong Zhang 256005755b9cSHong Zhang PetscFunctionBegin; 256105755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25620cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 256305755b9cSHong Zhang 256405755b9cSHong Zhang PetscStackPush("TS user DRDY function for sensitivity analysis"); 25650cf82b59SBarry Smith ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr); 256605755b9cSHong Zhang PetscStackPop; 256705755b9cSHong Zhang PetscFunctionReturn(0); 256805755b9cSHong Zhang } 256905755b9cSHong Zhang 257005755b9cSHong Zhang #undef __FUNCT__ 2571d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction" 257205755b9cSHong Zhang /*@ 2573d4aa0a58SBarry Smith TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 257405755b9cSHong Zhang 257505755b9cSHong Zhang Collective on TS 257605755b9cSHong Zhang 257705755b9cSHong Zhang Input Parameters: 257805755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 257905755b9cSHong Zhang 258005755b9cSHong Zhang Notes: 258105755b9cSHong Zhang TSDRDPFunction() is typically used for sensitivity implementation, 258205755b9cSHong Zhang so most users would not generally call this routine themselves. 258305755b9cSHong Zhang 258405755b9cSHong Zhang Level: developer 258505755b9cSHong Zhang 258605755b9cSHong Zhang .keywords: TS, sensitivity 2587d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction() 258805755b9cSHong Zhang @*/ 25890cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp) 259005755b9cSHong Zhang { 259105755b9cSHong Zhang PetscErrorCode ierr; 259205755b9cSHong Zhang 259305755b9cSHong Zhang PetscFunctionBegin; 259405755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25950cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 259605755b9cSHong Zhang 259705755b9cSHong Zhang PetscStackPush("TS user DRDP function for sensitivity analysis"); 25980cf82b59SBarry Smith ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr); 259905755b9cSHong Zhang PetscStackPop; 260005755b9cSHong Zhang PetscFunctionReturn(0); 260105755b9cSHong Zhang } 260205755b9cSHong Zhang 260305755b9cSHong Zhang #undef __FUNCT__ 2604e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 2605ac226902SBarry Smith /*@C 2606000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 26073f2090d5SJed Brown called once at the beginning of each time step. 2608000e7ae3SMatthew Knepley 26093f9fe445SBarry Smith Logically Collective on TS 2610000e7ae3SMatthew Knepley 2611000e7ae3SMatthew Knepley Input Parameters: 2612000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2613000e7ae3SMatthew Knepley - func - The function 2614000e7ae3SMatthew Knepley 2615000e7ae3SMatthew Knepley Calling sequence of func: 2616000e7ae3SMatthew Knepley . func (TS ts); 2617000e7ae3SMatthew Knepley 2618000e7ae3SMatthew Knepley Level: intermediate 2619000e7ae3SMatthew Knepley 2620b8123daeSJed Brown Note: 2621b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 2622b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 2623b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 2624b8123daeSJed Brown 2625000e7ae3SMatthew Knepley .keywords: TS, timestep 26269be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 2627000e7ae3SMatthew Knepley @*/ 26287087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 2629000e7ae3SMatthew Knepley { 2630000e7ae3SMatthew Knepley PetscFunctionBegin; 26310700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2632ae60f76fSBarry Smith ts->prestep = func; 2633000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2634000e7ae3SMatthew Knepley } 2635000e7ae3SMatthew Knepley 2636e74ef692SMatthew Knepley #undef __FUNCT__ 26373f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 263809ee8438SJed Brown /*@ 26393f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 26403f2090d5SJed Brown 26413f2090d5SJed Brown Collective on TS 26423f2090d5SJed Brown 26433f2090d5SJed Brown Input Parameters: 26443f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 26453f2090d5SJed Brown 26463f2090d5SJed Brown Notes: 26473f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 26483f2090d5SJed Brown so most users would not generally call this routine themselves. 26493f2090d5SJed Brown 26503f2090d5SJed Brown Level: developer 26513f2090d5SJed Brown 26523f2090d5SJed Brown .keywords: TS, timestep 26539be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 26543f2090d5SJed Brown @*/ 26557087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 26563f2090d5SJed Brown { 26573f2090d5SJed Brown PetscErrorCode ierr; 26583f2090d5SJed Brown 26593f2090d5SJed Brown PetscFunctionBegin; 26600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2661ae60f76fSBarry Smith if (ts->prestep) { 2662ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 2663312ce896SJed Brown } 26643f2090d5SJed Brown PetscFunctionReturn(0); 26653f2090d5SJed Brown } 26663f2090d5SJed Brown 26673f2090d5SJed Brown #undef __FUNCT__ 2668b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 2669b8123daeSJed Brown /*@C 2670b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 2671b8123daeSJed Brown called once at the beginning of each stage. 2672b8123daeSJed Brown 2673b8123daeSJed Brown Logically Collective on TS 2674b8123daeSJed Brown 2675b8123daeSJed Brown Input Parameters: 2676b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 2677b8123daeSJed Brown - func - The function 2678b8123daeSJed Brown 2679b8123daeSJed Brown Calling sequence of func: 2680b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 2681b8123daeSJed Brown 2682b8123daeSJed Brown Level: intermediate 2683b8123daeSJed Brown 2684b8123daeSJed Brown Note: 2685b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 2686b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 2687b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 2688b8123daeSJed Brown 2689b8123daeSJed Brown .keywords: TS, timestep 26909be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 2691b8123daeSJed Brown @*/ 2692b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 2693b8123daeSJed Brown { 2694b8123daeSJed Brown PetscFunctionBegin; 2695b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2696ae60f76fSBarry Smith ts->prestage = func; 2697b8123daeSJed Brown PetscFunctionReturn(0); 2698b8123daeSJed Brown } 2699b8123daeSJed Brown 2700b8123daeSJed Brown #undef __FUNCT__ 27019be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage" 27029be3e283SDebojyoti Ghosh /*@C 27039be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 27049be3e283SDebojyoti Ghosh called once at the end of each stage. 27059be3e283SDebojyoti Ghosh 27069be3e283SDebojyoti Ghosh Logically Collective on TS 27079be3e283SDebojyoti Ghosh 27089be3e283SDebojyoti Ghosh Input Parameters: 27099be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 27109be3e283SDebojyoti Ghosh - func - The function 27119be3e283SDebojyoti Ghosh 27129be3e283SDebojyoti Ghosh Calling sequence of func: 27139be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 27149be3e283SDebojyoti Ghosh 27159be3e283SDebojyoti Ghosh Level: intermediate 27169be3e283SDebojyoti Ghosh 27179be3e283SDebojyoti Ghosh Note: 27189be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 27199be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 27209be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 27219be3e283SDebojyoti Ghosh 27229be3e283SDebojyoti Ghosh .keywords: TS, timestep 27239be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 27249be3e283SDebojyoti Ghosh @*/ 27259be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 27269be3e283SDebojyoti Ghosh { 27279be3e283SDebojyoti Ghosh PetscFunctionBegin; 27289be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 27299be3e283SDebojyoti Ghosh ts->poststage = func; 27309be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 27319be3e283SDebojyoti Ghosh } 27329be3e283SDebojyoti Ghosh 27339be3e283SDebojyoti Ghosh #undef __FUNCT__ 2734b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 2735b8123daeSJed Brown /*@ 2736b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 2737b8123daeSJed Brown 2738b8123daeSJed Brown Collective on TS 2739b8123daeSJed Brown 2740b8123daeSJed Brown Input Parameters: 2741b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 27429be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 2743b8123daeSJed Brown 2744b8123daeSJed Brown Notes: 2745b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 2746b8123daeSJed Brown most users would not generally call this routine themselves. 2747b8123daeSJed Brown 2748b8123daeSJed Brown Level: developer 2749b8123daeSJed Brown 2750b8123daeSJed Brown .keywords: TS, timestep 27519be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 2752b8123daeSJed Brown @*/ 2753b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 2754b8123daeSJed Brown { 2755b8123daeSJed Brown PetscErrorCode ierr; 2756b8123daeSJed Brown 2757b8123daeSJed Brown PetscFunctionBegin; 2758b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2759ae60f76fSBarry Smith if (ts->prestage) { 2760ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 2761b8123daeSJed Brown } 2762b8123daeSJed Brown PetscFunctionReturn(0); 2763b8123daeSJed Brown } 2764b8123daeSJed Brown 2765b8123daeSJed Brown #undef __FUNCT__ 27669be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage" 27679be3e283SDebojyoti Ghosh /*@ 27689be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 27699be3e283SDebojyoti Ghosh 27709be3e283SDebojyoti Ghosh Collective on TS 27719be3e283SDebojyoti Ghosh 27729be3e283SDebojyoti Ghosh Input Parameters: 27739be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 27749be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 27759be3e283SDebojyoti Ghosh stageindex - Stage number 27769be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 27779be3e283SDebojyoti Ghosh of stages) with the stage solutions 27789be3e283SDebojyoti Ghosh 27799be3e283SDebojyoti Ghosh Notes: 27809be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 27819be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 27829be3e283SDebojyoti Ghosh 27839be3e283SDebojyoti Ghosh Level: developer 27849be3e283SDebojyoti Ghosh 27859be3e283SDebojyoti Ghosh .keywords: TS, timestep 27869be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 27879be3e283SDebojyoti Ghosh @*/ 27889be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 27899be3e283SDebojyoti Ghosh { 27909be3e283SDebojyoti Ghosh PetscErrorCode ierr; 27919be3e283SDebojyoti Ghosh 27929be3e283SDebojyoti Ghosh PetscFunctionBegin; 27939be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27944beae5d8SLisandro Dalcin if (ts->poststage) { 27959be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 27969be3e283SDebojyoti Ghosh } 27979be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 27989be3e283SDebojyoti Ghosh } 27999be3e283SDebojyoti Ghosh 28009be3e283SDebojyoti Ghosh #undef __FUNCT__ 2801e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 2802ac226902SBarry Smith /*@C 2803000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 28043f2090d5SJed Brown called once at the end of each time step. 2805000e7ae3SMatthew Knepley 28063f9fe445SBarry Smith Logically Collective on TS 2807000e7ae3SMatthew Knepley 2808000e7ae3SMatthew Knepley Input Parameters: 2809000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2810000e7ae3SMatthew Knepley - func - The function 2811000e7ae3SMatthew Knepley 2812000e7ae3SMatthew Knepley Calling sequence of func: 2813b8123daeSJed Brown $ func (TS ts); 2814000e7ae3SMatthew Knepley 2815000e7ae3SMatthew Knepley Level: intermediate 2816000e7ae3SMatthew Knepley 2817000e7ae3SMatthew Knepley .keywords: TS, timestep 2818b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 2819000e7ae3SMatthew Knepley @*/ 28207087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 2821000e7ae3SMatthew Knepley { 2822000e7ae3SMatthew Knepley PetscFunctionBegin; 28230700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2824ae60f76fSBarry Smith ts->poststep = func; 2825000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2826000e7ae3SMatthew Knepley } 2827000e7ae3SMatthew Knepley 2828e74ef692SMatthew Knepley #undef __FUNCT__ 28293f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 283009ee8438SJed Brown /*@ 28313f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 28323f2090d5SJed Brown 28333f2090d5SJed Brown Collective on TS 28343f2090d5SJed Brown 28353f2090d5SJed Brown Input Parameters: 28363f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 28373f2090d5SJed Brown 28383f2090d5SJed Brown Notes: 28393f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 28403f2090d5SJed Brown so most users would not generally call this routine themselves. 28413f2090d5SJed Brown 28423f2090d5SJed Brown Level: developer 28433f2090d5SJed Brown 28443f2090d5SJed Brown .keywords: TS, timestep 28453f2090d5SJed Brown @*/ 28467087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 28473f2090d5SJed Brown { 28483f2090d5SJed Brown PetscErrorCode ierr; 28493f2090d5SJed Brown 28503f2090d5SJed Brown PetscFunctionBegin; 28510700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2852ae60f76fSBarry Smith if (ts->poststep) { 2853ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 285472ac3e02SJed Brown } 28553f2090d5SJed Brown PetscFunctionReturn(0); 28563f2090d5SJed Brown } 28573f2090d5SJed Brown 2858d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 2859d763cef2SBarry Smith 28604a2ae208SSatish Balay #undef __FUNCT__ 2861a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 2862d763cef2SBarry Smith /*@C 2863a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 2864d763cef2SBarry Smith timestep to display the iteration's progress. 2865d763cef2SBarry Smith 28663f9fe445SBarry Smith Logically Collective on TS 2867d763cef2SBarry Smith 2868d763cef2SBarry Smith Input Parameters: 2869d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2870e213d8f1SJed Brown . monitor - monitoring routine 2871329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 28720298fd71SBarry Smith monitor routine (use NULL if no context is desired) 2873b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 28740298fd71SBarry Smith (may be NULL) 2875d763cef2SBarry Smith 2876e213d8f1SJed Brown Calling sequence of monitor: 28770910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 2878d763cef2SBarry Smith 2879d763cef2SBarry Smith + ts - the TS context 288088c05cc5SBarry 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 288188c05cc5SBarry Smith been interpolated to) 28821f06c33eSBarry Smith . time - current time 28830910c330SBarry Smith . u - current iterate 2884d763cef2SBarry Smith - mctx - [optional] monitoring context 2885d763cef2SBarry Smith 2886d763cef2SBarry Smith Notes: 2887d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 2888d763cef2SBarry Smith already has been loaded. 2889d763cef2SBarry Smith 2890025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 2891025f1a04SBarry Smith 2892d763cef2SBarry Smith Level: intermediate 2893d763cef2SBarry Smith 2894d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2895d763cef2SBarry Smith 2896a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 2897d763cef2SBarry Smith @*/ 2898c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 2899d763cef2SBarry Smith { 2900d763cef2SBarry Smith PetscFunctionBegin; 29010700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 290217186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2903d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 29048704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 2905d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 2906d763cef2SBarry Smith PetscFunctionReturn(0); 2907d763cef2SBarry Smith } 2908d763cef2SBarry Smith 29094a2ae208SSatish Balay #undef __FUNCT__ 2910a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 2911d763cef2SBarry Smith /*@C 2912a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 2913d763cef2SBarry Smith 29143f9fe445SBarry Smith Logically Collective on TS 2915d763cef2SBarry Smith 2916d763cef2SBarry Smith Input Parameters: 2917d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2918d763cef2SBarry Smith 2919d763cef2SBarry Smith Notes: 2920d763cef2SBarry Smith There is no way to remove a single, specific monitor. 2921d763cef2SBarry Smith 2922d763cef2SBarry Smith Level: intermediate 2923d763cef2SBarry Smith 2924d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2925d763cef2SBarry Smith 2926a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 2927d763cef2SBarry Smith @*/ 29287087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 2929d763cef2SBarry Smith { 2930d952e501SBarry Smith PetscErrorCode ierr; 2931d952e501SBarry Smith PetscInt i; 2932d952e501SBarry Smith 2933d763cef2SBarry Smith PetscFunctionBegin; 29340700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2935d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 29368704b422SBarry Smith if (ts->monitordestroy[i]) { 29378704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 2938d952e501SBarry Smith } 2939d952e501SBarry Smith } 2940d763cef2SBarry Smith ts->numbermonitors = 0; 2941d763cef2SBarry Smith PetscFunctionReturn(0); 2942d763cef2SBarry Smith } 2943d763cef2SBarry Smith 29444a2ae208SSatish Balay #undef __FUNCT__ 2945a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 2946d8e5e3e6SSatish Balay /*@ 2947a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 29485516499fSSatish Balay 29495516499fSSatish Balay Level: intermediate 295041251cbbSSatish Balay 29515516499fSSatish Balay .keywords: TS, set, monitor 29525516499fSSatish Balay 295341251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 295441251cbbSSatish Balay @*/ 2955649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 2956d763cef2SBarry Smith { 2957dfbe8321SBarry Smith PetscErrorCode ierr; 2958ce94432eSBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts)); 2959d132466eSBarry Smith 2960d763cef2SBarry Smith PetscFunctionBegin; 2961649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2962971832b6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr); 2963649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2964d763cef2SBarry Smith PetscFunctionReturn(0); 2965d763cef2SBarry Smith } 2966d763cef2SBarry Smith 29674a2ae208SSatish Balay #undef __FUNCT__ 2968cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 2969cd652676SJed Brown /*@ 2970cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 2971cd652676SJed Brown 2972cd652676SJed Brown Logically Collective on TS 2973cd652676SJed Brown 2974cd652676SJed Brown Input Argument: 2975cd652676SJed Brown . ts - time stepping context 2976cd652676SJed Brown 2977cd652676SJed Brown Output Argument: 2978cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 2979cd652676SJed Brown 2980cd652676SJed Brown Level: intermediate 2981cd652676SJed Brown 2982cd652676SJed Brown .keywords: TS, set 2983cd652676SJed Brown 2984cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 2985cd652676SJed Brown @*/ 2986cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 2987cd652676SJed Brown { 2988cd652676SJed Brown PetscFunctionBegin; 2989cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2990cd652676SJed Brown ts->retain_stages = flg; 2991cd652676SJed Brown PetscFunctionReturn(0); 2992cd652676SJed Brown } 2993cd652676SJed Brown 2994cd652676SJed Brown #undef __FUNCT__ 2995cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 2996cd652676SJed Brown /*@ 2997cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 2998cd652676SJed Brown 2999cd652676SJed Brown Collective on TS 3000cd652676SJed Brown 3001cd652676SJed Brown Input Argument: 3002cd652676SJed Brown + ts - time stepping context 3003cd652676SJed Brown - t - time to interpolate to 3004cd652676SJed Brown 3005cd652676SJed Brown Output Argument: 30060910c330SBarry Smith . U - state at given time 3007cd652676SJed Brown 3008cd652676SJed Brown Notes: 3009cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 3010cd652676SJed Brown 3011cd652676SJed Brown Level: intermediate 3012cd652676SJed Brown 3013cd652676SJed Brown Developer Notes: 3014cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3015cd652676SJed Brown 3016cd652676SJed Brown .keywords: TS, set 3017cd652676SJed Brown 3018cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 3019cd652676SJed Brown @*/ 30200910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3021cd652676SJed Brown { 3022cd652676SJed Brown PetscErrorCode ierr; 3023cd652676SJed Brown 3024cd652676SJed Brown PetscFunctionBegin; 3025cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3026b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 30276712e2f1SBarry 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); 3028ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 30290910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3030cd652676SJed Brown PetscFunctionReturn(0); 3031cd652676SJed Brown } 3032cd652676SJed Brown 3033cd652676SJed Brown #undef __FUNCT__ 30344a2ae208SSatish Balay #define __FUNCT__ "TSStep" 3035d763cef2SBarry Smith /*@ 30366d9e5789SSean Farley TSStep - Steps one time step 3037d763cef2SBarry Smith 3038d763cef2SBarry Smith Collective on TS 3039d763cef2SBarry Smith 3040d763cef2SBarry Smith Input Parameter: 3041d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3042d763cef2SBarry Smith 304327829d71SBarry Smith Level: developer 3044d763cef2SBarry Smith 3045b8123daeSJed Brown Notes: 304627829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 304727829d71SBarry Smith 3048b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3049b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3050b8123daeSJed Brown 305125cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 305225cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 305325cb2221SBarry Smith 3054d763cef2SBarry Smith .keywords: TS, timestep, solve 3055d763cef2SBarry Smith 30569be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3057d763cef2SBarry Smith @*/ 3058193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3059d763cef2SBarry Smith { 30602fb8446cSMatthew G. Knepley DM dm; 3061dfbe8321SBarry Smith PetscErrorCode ierr; 3062fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3063d763cef2SBarry Smith 3064d763cef2SBarry Smith PetscFunctionBegin; 30650700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3066fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3067fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3068fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3069fffbeea8SBarry Smith " type = {Preprint},\n" 3070fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3071fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3072fffbeea8SBarry Smith " year = {2014}\n}\n",&cite); 3073fffbeea8SBarry Smith 30742fb8446cSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3075d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 3076d405a339SMatthew Knepley 3077362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 307824655328SShri ts->ptime_prev = ts->ptime; 3079cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3080bbd56ea5SKarl Rupp 3081e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3082d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3083193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3084d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3085bbd56ea5SKarl Rupp 308624655328SShri ts->time_step_prev = ts->ptime - ts->ptime_prev; 3087cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3088362cd11cSLisandro Dalcin 3089362cd11cSLisandro Dalcin if (ts->reason < 0) { 3090cef5090cSJed Brown if (ts->errorifstepfailed) { 309108c7845fSBarry 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]); 309208c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3093d2daff3dSHong Zhang } 309408c7845fSBarry Smith } else if (!ts->reason) { 309508c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 309608c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 309708c7845fSBarry Smith } 30982c18e0fdSBarry Smith ts->total_steps++; 309908c7845fSBarry Smith PetscFunctionReturn(0); 310008c7845fSBarry Smith } 310108c7845fSBarry Smith 310208c7845fSBarry Smith #undef __FUNCT__ 310308c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep" 310408c7845fSBarry Smith /*@ 310508c7845fSBarry Smith TSAdjointStep - Steps one time step 310608c7845fSBarry Smith 310708c7845fSBarry Smith Collective on TS 310808c7845fSBarry Smith 310908c7845fSBarry Smith Input Parameter: 311008c7845fSBarry Smith . ts - the TS context obtained from TSCreate() 311108c7845fSBarry Smith 31126a4d4014SLisandro Dalcin Level: intermediate 31136a4d4014SLisandro Dalcin 31147087cfbeSBarry Smith Notes: 31156a4d4014SLisandro Dalcin The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 31166a4d4014SLisandro Dalcin be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 31176a4d4014SLisandro Dalcin 31186a4d4014SLisandro Dalcin This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 3119f22f69f0SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 31206a4d4014SLisandro Dalcin 31210700a824SBarry Smith .keywords: TS, timestep, solve 31226a4d4014SLisandro Dalcin 31236a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 31246a4d4014SLisandro Dalcin @*/ 312508c7845fSBarry Smith PetscErrorCode TSAdjointStep(TS ts) 31266a4d4014SLisandro Dalcin { 312708c7845fSBarry Smith DM dm; 31286a4d4014SLisandro Dalcin PetscErrorCode ierr; 31296a4d4014SLisandro Dalcin 31306a4d4014SLisandro Dalcin PetscFunctionBegin; 31314a2ae208SSatish Balay PetscValidHeaderSpecific(ts, TS_CLASSID,1); 313208c7845fSBarry Smith ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 313308c7845fSBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3134d763cef2SBarry Smith 3135d763cef2SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 313608c7845fSBarry Smith ts->ptime_prev = ts->ptime; 313708c7845fSBarry Smith ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 313808c7845fSBarry Smith ierr = VecViewFromOptions(ts->vec_sol, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr); 3139d763cef2SBarry Smith 31406849ba73SBarry Smith ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 314142f2b339SBarry 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); 314242f2b339SBarry Smith ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr); 3143a6570f20SBarry Smith ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3144d763cef2SBarry Smith 3145cef5090cSJed Brown ts->time_step_prev = ts->ptime - ts->ptime_prev; 3146cef5090cSJed Brown ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3147362cd11cSLisandro Dalcin 3148362cd11cSLisandro Dalcin if (ts->reason < 0) { 3149cef5090cSJed Brown if (ts->errorifstepfailed) { 3150cef5090cSJed Brown if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) { 3151ce94432eSBarry Smith SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]); 31522a3a10ceSLisandro Dalcin } else if (ts->reason == TS_DIVERGED_STEP_REJECTED) { 31532a3a10ceSLisandro Dalcin SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_reject or make negative to attempt recovery",TSConvergedReasons[ts->reason]); 3154ce94432eSBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3155cef5090cSJed Brown } 3156362cd11cSLisandro Dalcin } else if (!ts->reason) { 315773b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3158db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3159362cd11cSLisandro Dalcin } 31602c18e0fdSBarry Smith ts->total_steps--; 3161d763cef2SBarry Smith PetscFunctionReturn(0); 3162d763cef2SBarry Smith } 3163d763cef2SBarry Smith 3164d763cef2SBarry Smith #undef __FUNCT__ 316505175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 316605175c85SJed Brown /*@ 316705175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 316805175c85SJed Brown 31691c3436cfSJed Brown Collective on TS 317005175c85SJed Brown 317105175c85SJed Brown Input Arguments: 31721c3436cfSJed Brown + ts - time stepping context 31731c3436cfSJed Brown . order - desired order of accuracy 31740298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 317505175c85SJed Brown 317605175c85SJed Brown Output Arguments: 31770910c330SBarry Smith . U - state at the end of the current step 317805175c85SJed Brown 317905175c85SJed Brown Level: advanced 318005175c85SJed Brown 3181108c343cSJed Brown Notes: 3182108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3183108c343cSJed 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. 3184108c343cSJed Brown 31851c3436cfSJed Brown .seealso: TSStep(), TSAdapt 318605175c85SJed Brown @*/ 31870910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 318805175c85SJed Brown { 318905175c85SJed Brown PetscErrorCode ierr; 319005175c85SJed Brown 319105175c85SJed Brown PetscFunctionBegin; 319205175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 319305175c85SJed Brown PetscValidType(ts,1); 31940910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3195ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 31960910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 319705175c85SJed Brown PetscFunctionReturn(0); 319805175c85SJed Brown } 319905175c85SJed Brown 3200d67e68b7SBarry Smith 320105175c85SJed Brown #undef __FUNCT__ 3202d763cef2SBarry Smith #define __FUNCT__ "TSSolve" 3203d763cef2SBarry Smith /*@ 3204d763cef2SBarry Smith TSSolve - Steps the requested number of timesteps. 3205d763cef2SBarry Smith 3206d763cef2SBarry Smith Collective on TS 3207d763cef2SBarry Smith 3208d763cef2SBarry Smith Input Parameter: 3209d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3210cc708dedSBarry Smith - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 32115a3a76d0SJed Brown 3212d763cef2SBarry Smith Level: beginner 3213d763cef2SBarry Smith 32145a3a76d0SJed Brown Notes: 32155a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 32165a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 32175a3a76d0SJed Brown stepped over the final time. 32185a3a76d0SJed Brown 3219d763cef2SBarry Smith .keywords: TS, timestep, solve 3220d763cef2SBarry Smith 3221d763cef2SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep() 3222d763cef2SBarry Smith @*/ 3223cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 3224d763cef2SBarry Smith { 3225b06615a5SLisandro Dalcin Vec solution; 3226d763cef2SBarry Smith PetscErrorCode ierr; 3227d763cef2SBarry Smith 3228d763cef2SBarry Smith PetscFunctionBegin; 3229d763cef2SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3230f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 323149354f04SShri 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 */ 3232b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 32330910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 3234b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3235b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3236b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 32375a3a76d0SJed Brown } 32380910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3239bbd56ea5SKarl Rupp } else if (u) { 32400910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 32415a3a76d0SJed Brown } 3242a74656a8SBarry Smith ierr = TSSetUp(ts);CHKERRQ(ierr); 3243d763cef2SBarry Smith /* reset time step and iteration counters */ 3244193ac0bcSJed Brown ts->steps = 0; 32455ef26d82SJed Brown ts->ksp_its = 0; 32465ef26d82SJed Brown ts->snes_its = 0; 3247c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3248c610991cSLisandro Dalcin ts->reject = 0; 3249193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3250193ac0bcSJed Brown 3251ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3252f05ece33SBarry Smith 3253193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3254193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 32550910c330SBarry Smith ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 3256cc708dedSBarry Smith ts->solvetime = ts->ptime; 3257193ac0bcSJed Brown } else { 3258d763cef2SBarry Smith /* steps the requested number of timesteps. */ 3259db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3260db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3261e1a7a14fSJed Brown while (!ts->reason) { 3262b06615a5SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 3263bc952696SBarry Smith ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 3264193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 3265aeb4809dSShri Abhyankar if (ts->event) { 3266aeb4809dSShri Abhyankar ierr = TSEventMonitor(ts);CHKERRQ(ierr); 32671eda64f1SShri Abhyankar if (ts->event->status != TSEVENT_PROCESSING) { 3268193ac0bcSJed Brown ierr = TSPostStep(ts);CHKERRQ(ierr); 3269193ac0bcSJed Brown } 32701eda64f1SShri Abhyankar } else { 32711eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 3272aeb4809dSShri Abhyankar } 3273193ac0bcSJed Brown } 327449354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 32750910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 3276cc708dedSBarry Smith ts->solvetime = ts->max_time; 3277b06615a5SLisandro Dalcin solution = u; 32780574a7fbSJed Brown } else { 3279ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3280cc708dedSBarry Smith ts->solvetime = ts->ptime; 3281b06615a5SLisandro Dalcin solution = ts->vec_sol; 32820574a7fbSJed Brown } 3283bc952696SBarry Smith ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr); 3284b06615a5SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr); 328527829d71SBarry Smith ierr = VecViewFromOptions(solution, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr); 3286193ac0bcSJed Brown } 3287d2daff3dSHong Zhang 3288ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 328956f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 3290*ef222394SBarry Smith if (ts->adjoint_solve) { 3291*ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 3292*ef222394SBarry Smith } 3293d763cef2SBarry Smith PetscFunctionReturn(0); 3294d763cef2SBarry Smith } 3295d763cef2SBarry Smith 3296d763cef2SBarry Smith #undef __FUNCT__ 3297c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve" 3298c88f2d44SBarry Smith /*@ 3299c88f2d44SBarry Smith TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 3300c88f2d44SBarry Smith 3301c88f2d44SBarry Smith Collective on TS 3302c88f2d44SBarry Smith 3303c88f2d44SBarry Smith Input Parameter: 33042c39e106SBarry Smith . ts - the TS context obtained from TSCreate() 3305c88f2d44SBarry Smith 3306c88f2d44SBarry Smith Level: intermediate 3307c88f2d44SBarry Smith 3308c88f2d44SBarry Smith Notes: 3309c88f2d44SBarry Smith This must be called after a call to TSSolve() that solves the forward problem 3310c88f2d44SBarry Smith 331173b18844SBarry Smith By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time 331273b18844SBarry Smith 3313c88f2d44SBarry Smith .keywords: TS, timestep, solve 3314c88f2d44SBarry Smith 3315c88f2d44SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep() 3316c88f2d44SBarry Smith @*/ 33172c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts) 3318c88f2d44SBarry Smith { 3319c88f2d44SBarry Smith PetscErrorCode ierr; 3320c88f2d44SBarry Smith 3321c88f2d44SBarry Smith PetscFunctionBegin; 3322c88f2d44SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3323c88f2d44SBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3324c88f2d44SBarry Smith /* reset time step and iteration counters */ 3325c88f2d44SBarry Smith ts->steps = 0; 3326c88f2d44SBarry Smith ts->ksp_its = 0; 3327c88f2d44SBarry Smith ts->snes_its = 0; 3328c88f2d44SBarry Smith ts->num_snes_failures = 0; 3329c88f2d44SBarry Smith ts->reject = 0; 3330c88f2d44SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 3331c88f2d44SBarry Smith 333273b18844SBarry Smith if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps; 3333c88f2d44SBarry Smith 333473b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3335c88f2d44SBarry Smith while (!ts->reason) { 333673b18844SBarry Smith ierr = TSTrajectoryGet(ts->trajectory,ts,ts->adjoint_max_steps-ts->steps,ts->ptime);CHKERRQ(ierr); 333773b18844SBarry Smith ierr = TSMonitor(ts,ts->adjoint_max_steps-ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 333808c7845fSBarry Smith ierr = TSAdjointStep(ts);CHKERRQ(ierr); 3339c88f2d44SBarry Smith if (ts->event) { 3340c88f2d44SBarry Smith ierr = TSEventMonitor(ts);CHKERRQ(ierr); 3341c88f2d44SBarry Smith if (ts->event->status != TSEVENT_PROCESSING) { 3342c88f2d44SBarry Smith ierr = TSPostStep(ts);CHKERRQ(ierr); 3343c88f2d44SBarry Smith } 3344c88f2d44SBarry Smith } else { 3345c88f2d44SBarry Smith ierr = TSPostStep(ts);CHKERRQ(ierr); 3346c88f2d44SBarry Smith } 3347c88f2d44SBarry Smith } 3348c88f2d44SBarry Smith ts->solvetime = ts->ptime; 3349c88f2d44SBarry Smith PetscFunctionReturn(0); 3350c88f2d44SBarry Smith } 3351c88f2d44SBarry Smith 3352c88f2d44SBarry Smith #undef __FUNCT__ 3353d763cef2SBarry Smith #define __FUNCT__ "TSMonitor" 3354228d79bcSJed Brown /*@ 3355228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 3356228d79bcSJed Brown 3357228d79bcSJed Brown Collective on TS 3358228d79bcSJed Brown 3359228d79bcSJed Brown Input Parameters: 3360228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 3361228d79bcSJed Brown . step - step number that has just completed 3362228d79bcSJed Brown . ptime - model time of the state 33630910c330SBarry Smith - u - state at the current model time 3364228d79bcSJed Brown 3365228d79bcSJed Brown Notes: 3366228d79bcSJed Brown TSMonitor() is typically used within the time stepping implementations. 3367228d79bcSJed Brown Users might call this function when using the TSStep() interface instead of TSSolve(). 3368228d79bcSJed Brown 3369228d79bcSJed Brown Level: advanced 3370228d79bcSJed Brown 3371228d79bcSJed Brown .keywords: TS, timestep 3372228d79bcSJed Brown @*/ 33730910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 3374d763cef2SBarry Smith { 3375d763cef2SBarry Smith PetscErrorCode ierr; 3376d763cef2SBarry Smith PetscInt i,n = ts->numbermonitors; 3377d763cef2SBarry Smith 3378d763cef2SBarry Smith PetscFunctionBegin; 3379b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3380b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 33815edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 3382d763cef2SBarry Smith for (i=0; i<n; i++) { 33830910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 3384d763cef2SBarry Smith } 33855edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 3386d763cef2SBarry Smith PetscFunctionReturn(0); 3387d763cef2SBarry Smith } 3388d763cef2SBarry Smith 3389d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 3390d763cef2SBarry Smith #undef __FUNCT__ 3391a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 3392d763cef2SBarry Smith /*@C 3393a9f9c1f6SBarry Smith TSMonitorLGCtxCreate - Creates a line graph context for use with 3394a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 3395d763cef2SBarry Smith 3396d763cef2SBarry Smith Collective on TS 3397d763cef2SBarry Smith 3398d763cef2SBarry Smith Input Parameters: 3399d763cef2SBarry Smith + host - the X display to open, or null for the local machine 3400d763cef2SBarry Smith . label - the title to put in the title bar 34017c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 3402a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 3403a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 3404d763cef2SBarry Smith 3405d763cef2SBarry Smith Output Parameter: 34060b039ecaSBarry Smith . ctx - the context 3407d763cef2SBarry Smith 3408d763cef2SBarry Smith Options Database Key: 34094f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 34104f09c107SBarry Smith . -ts_monitor_lg_solution - 341199fdda47SBarry Smith . -ts_monitor_lg_error - 341299fdda47SBarry Smith . -ts_monitor_lg_ksp_iterations - 341399fdda47SBarry Smith . -ts_monitor_lg_snes_iterations - 341499fdda47SBarry Smith - -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true 3415d763cef2SBarry Smith 3416d763cef2SBarry Smith Notes: 3417a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 3418d763cef2SBarry Smith 3419d763cef2SBarry Smith Level: intermediate 3420d763cef2SBarry Smith 34217c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 3422d763cef2SBarry Smith 34234f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 34247c922b88SBarry Smith 3425d763cef2SBarry Smith @*/ 3426a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 3427d763cef2SBarry Smith { 3428b0a32e0cSBarry Smith PetscDraw win; 3429dfbe8321SBarry Smith PetscErrorCode ierr; 3430d763cef2SBarry Smith 3431d763cef2SBarry Smith PetscFunctionBegin; 3432b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 3433a80ad3e0SBarry Smith ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr); 34343fbbecb0SBarry Smith ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr); 34350b039ecaSBarry Smith ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr); 34363bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr); 3437287de1a7SBarry Smith ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr); 3438287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 3439a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 3440d763cef2SBarry Smith PetscFunctionReturn(0); 3441d763cef2SBarry Smith } 3442d763cef2SBarry Smith 34434a2ae208SSatish Balay #undef __FUNCT__ 34444f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 3445b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 3446d763cef2SBarry Smith { 34470b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 3448c32365f1SBarry Smith PetscReal x = ptime,y; 3449dfbe8321SBarry Smith PetscErrorCode ierr; 3450d763cef2SBarry Smith 3451d763cef2SBarry Smith PetscFunctionBegin; 3452b06615a5SLisandro Dalcin if (!step) { 3453a9f9c1f6SBarry Smith PetscDrawAxis axis; 3454a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 3455a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 3456a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 3457287de1a7SBarry Smith ierr = PetscDrawLGIndicateDataPoints(ctx->lg,PETSC_TRUE);CHKERRQ(ierr); 3458a9f9c1f6SBarry Smith } 3459c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 34600b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 3461b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 34620b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 34633923b477SBarry Smith } 3464d763cef2SBarry Smith PetscFunctionReturn(0); 3465d763cef2SBarry Smith } 3466d763cef2SBarry Smith 34674a2ae208SSatish Balay #undef __FUNCT__ 3468a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 3469d763cef2SBarry Smith /*@C 3470a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 3471a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 3472d763cef2SBarry Smith 34730b039ecaSBarry Smith Collective on TSMonitorLGCtx 3474d763cef2SBarry Smith 3475d763cef2SBarry Smith Input Parameter: 34760b039ecaSBarry Smith . ctx - the monitor context 3477d763cef2SBarry Smith 3478d763cef2SBarry Smith Level: intermediate 3479d763cef2SBarry Smith 3480d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 3481d763cef2SBarry Smith 34824f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 3483d763cef2SBarry Smith @*/ 3484a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 3485d763cef2SBarry Smith { 3486b0a32e0cSBarry Smith PetscDraw draw; 3487dfbe8321SBarry Smith PetscErrorCode ierr; 3488d763cef2SBarry Smith 3489d763cef2SBarry Smith PetscFunctionBegin; 34907684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 34917684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 34927684fa3eSBarry Smith } 34930b039ecaSBarry Smith ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr); 34946bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 34950b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 349631152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 3497387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 3498387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 3499387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 35000b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 3501d763cef2SBarry Smith PetscFunctionReturn(0); 3502d763cef2SBarry Smith } 3503d763cef2SBarry Smith 35044a2ae208SSatish Balay #undef __FUNCT__ 35054a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 3506d763cef2SBarry Smith /*@ 3507b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 3508d763cef2SBarry Smith 3509d763cef2SBarry Smith Not Collective 3510d763cef2SBarry Smith 3511d763cef2SBarry Smith Input Parameter: 3512d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3513d763cef2SBarry Smith 3514d763cef2SBarry Smith Output Parameter: 3515d763cef2SBarry Smith . t - the current time 3516d763cef2SBarry Smith 3517d763cef2SBarry Smith Level: beginner 3518d763cef2SBarry Smith 3519b8123daeSJed Brown Note: 3520b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 35219be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 3522b8123daeSJed Brown 3523d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3524d763cef2SBarry Smith 3525d763cef2SBarry Smith .keywords: TS, get, time 3526d763cef2SBarry Smith @*/ 35277087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 3528d763cef2SBarry Smith { 3529d763cef2SBarry Smith PetscFunctionBegin; 35300700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3531f7cf8827SBarry Smith PetscValidRealPointer(t,2); 3532d763cef2SBarry Smith *t = ts->ptime; 3533d763cef2SBarry Smith PetscFunctionReturn(0); 3534d763cef2SBarry Smith } 3535d763cef2SBarry Smith 35364a2ae208SSatish Balay #undef __FUNCT__ 3537e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime" 3538e5e524a1SHong Zhang /*@ 3539e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 3540e5e524a1SHong Zhang 3541e5e524a1SHong Zhang Not Collective 3542e5e524a1SHong Zhang 3543e5e524a1SHong Zhang Input Parameter: 3544e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 3545e5e524a1SHong Zhang 3546e5e524a1SHong Zhang Output Parameter: 3547e5e524a1SHong Zhang . t - the previous time 3548e5e524a1SHong Zhang 3549e5e524a1SHong Zhang Level: beginner 3550e5e524a1SHong Zhang 3551e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3552e5e524a1SHong Zhang 3553e5e524a1SHong Zhang .keywords: TS, get, time 3554e5e524a1SHong Zhang @*/ 3555e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 3556e5e524a1SHong Zhang { 3557e5e524a1SHong Zhang PetscFunctionBegin; 3558e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3559e5e524a1SHong Zhang PetscValidRealPointer(t,2); 3560e5e524a1SHong Zhang *t = ts->ptime_prev; 3561e5e524a1SHong Zhang PetscFunctionReturn(0); 3562e5e524a1SHong Zhang } 3563e5e524a1SHong Zhang 3564e5e524a1SHong Zhang #undef __FUNCT__ 35656a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 35666a4d4014SLisandro Dalcin /*@ 35676a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 35686a4d4014SLisandro Dalcin 35693f9fe445SBarry Smith Logically Collective on TS 35706a4d4014SLisandro Dalcin 35716a4d4014SLisandro Dalcin Input Parameters: 35726a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 35736a4d4014SLisandro Dalcin - time - the time 35746a4d4014SLisandro Dalcin 35756a4d4014SLisandro Dalcin Level: intermediate 35766a4d4014SLisandro Dalcin 35776a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 35786a4d4014SLisandro Dalcin 35796a4d4014SLisandro Dalcin .keywords: TS, set, time 35806a4d4014SLisandro Dalcin @*/ 35817087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 35826a4d4014SLisandro Dalcin { 35836a4d4014SLisandro Dalcin PetscFunctionBegin; 35840700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3585c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 35866a4d4014SLisandro Dalcin ts->ptime = t; 35876a4d4014SLisandro Dalcin PetscFunctionReturn(0); 35886a4d4014SLisandro Dalcin } 35896a4d4014SLisandro Dalcin 35906a4d4014SLisandro Dalcin #undef __FUNCT__ 35914a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 3592d763cef2SBarry Smith /*@C 3593d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 3594d763cef2SBarry Smith TS options in the database. 3595d763cef2SBarry Smith 35963f9fe445SBarry Smith Logically Collective on TS 3597d763cef2SBarry Smith 3598d763cef2SBarry Smith Input Parameter: 3599d763cef2SBarry Smith + ts - The TS context 3600d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3601d763cef2SBarry Smith 3602d763cef2SBarry Smith Notes: 3603d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3604d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3605d763cef2SBarry Smith hyphen. 3606d763cef2SBarry Smith 3607d763cef2SBarry Smith Level: advanced 3608d763cef2SBarry Smith 3609d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 3610d763cef2SBarry Smith 3611d763cef2SBarry Smith .seealso: TSSetFromOptions() 3612d763cef2SBarry Smith 3613d763cef2SBarry Smith @*/ 36147087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 3615d763cef2SBarry Smith { 3616dfbe8321SBarry Smith PetscErrorCode ierr; 3617089b2837SJed Brown SNES snes; 3618d763cef2SBarry Smith 3619d763cef2SBarry Smith PetscFunctionBegin; 36200700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3621d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3622089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3623089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3624d763cef2SBarry Smith PetscFunctionReturn(0); 3625d763cef2SBarry Smith } 3626d763cef2SBarry Smith 3627d763cef2SBarry Smith 36284a2ae208SSatish Balay #undef __FUNCT__ 36294a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 3630d763cef2SBarry Smith /*@C 3631d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 3632d763cef2SBarry Smith TS options in the database. 3633d763cef2SBarry Smith 36343f9fe445SBarry Smith Logically Collective on TS 3635d763cef2SBarry Smith 3636d763cef2SBarry Smith Input Parameter: 3637d763cef2SBarry Smith + ts - The TS context 3638d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3639d763cef2SBarry Smith 3640d763cef2SBarry Smith Notes: 3641d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3642d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3643d763cef2SBarry Smith hyphen. 3644d763cef2SBarry Smith 3645d763cef2SBarry Smith Level: advanced 3646d763cef2SBarry Smith 3647d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 3648d763cef2SBarry Smith 3649d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 3650d763cef2SBarry Smith 3651d763cef2SBarry Smith @*/ 36527087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 3653d763cef2SBarry Smith { 3654dfbe8321SBarry Smith PetscErrorCode ierr; 3655089b2837SJed Brown SNES snes; 3656d763cef2SBarry Smith 3657d763cef2SBarry Smith PetscFunctionBegin; 36580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3659d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3660089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3661089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3662d763cef2SBarry Smith PetscFunctionReturn(0); 3663d763cef2SBarry Smith } 3664d763cef2SBarry Smith 36654a2ae208SSatish Balay #undef __FUNCT__ 36664a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 3667d763cef2SBarry Smith /*@C 3668d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 3669d763cef2SBarry Smith TS options in the database. 3670d763cef2SBarry Smith 3671d763cef2SBarry Smith Not Collective 3672d763cef2SBarry Smith 3673d763cef2SBarry Smith Input Parameter: 3674d763cef2SBarry Smith . ts - The TS context 3675d763cef2SBarry Smith 3676d763cef2SBarry Smith Output Parameter: 3677d763cef2SBarry Smith . prefix - A pointer to the prefix string used 3678d763cef2SBarry Smith 3679d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 3680d763cef2SBarry Smith sufficient length to hold the prefix. 3681d763cef2SBarry Smith 3682d763cef2SBarry Smith Level: intermediate 3683d763cef2SBarry Smith 3684d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 3685d763cef2SBarry Smith 3686d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 3687d763cef2SBarry Smith @*/ 36887087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 3689d763cef2SBarry Smith { 3690dfbe8321SBarry Smith PetscErrorCode ierr; 3691d763cef2SBarry Smith 3692d763cef2SBarry Smith PetscFunctionBegin; 36930700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36944482741eSBarry Smith PetscValidPointer(prefix,2); 3695d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3696d763cef2SBarry Smith PetscFunctionReturn(0); 3697d763cef2SBarry Smith } 3698d763cef2SBarry Smith 36994a2ae208SSatish Balay #undef __FUNCT__ 37004a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 3701d763cef2SBarry Smith /*@C 3702d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 3703d763cef2SBarry Smith 3704d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 3705d763cef2SBarry Smith 3706d763cef2SBarry Smith Input Parameter: 3707d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 3708d763cef2SBarry Smith 3709d763cef2SBarry Smith Output Parameters: 3710e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 3711e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 3712e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 3713e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 3714d763cef2SBarry Smith 37150298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 3716d763cef2SBarry Smith 3717d763cef2SBarry Smith Level: intermediate 3718d763cef2SBarry Smith 371926d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 3720d763cef2SBarry Smith 3721d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 3722d763cef2SBarry Smith @*/ 3723e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 3724d763cef2SBarry Smith { 3725089b2837SJed Brown PetscErrorCode ierr; 3726089b2837SJed Brown SNES snes; 372724989b8cSPeter Brune DM dm; 3728089b2837SJed Brown 3729d763cef2SBarry Smith PetscFunctionBegin; 3730089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3731e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 373224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 373324989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 3734d763cef2SBarry Smith PetscFunctionReturn(0); 3735d763cef2SBarry Smith } 3736d763cef2SBarry Smith 37371713a123SBarry Smith #undef __FUNCT__ 37382eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 37392eca1d9cSJed Brown /*@C 37402eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 37412eca1d9cSJed Brown 37422eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 37432eca1d9cSJed Brown 37442eca1d9cSJed Brown Input Parameter: 37452eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 37462eca1d9cSJed Brown 37472eca1d9cSJed Brown Output Parameters: 3748e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 3749e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 37502eca1d9cSJed Brown . f - The function to compute the matrices 37512eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 37522eca1d9cSJed Brown 37530298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 37542eca1d9cSJed Brown 37552eca1d9cSJed Brown Level: advanced 37562eca1d9cSJed Brown 37572eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 37582eca1d9cSJed Brown 37592eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 37602eca1d9cSJed Brown @*/ 3761e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 37622eca1d9cSJed Brown { 3763089b2837SJed Brown PetscErrorCode ierr; 3764089b2837SJed Brown SNES snes; 376524989b8cSPeter Brune DM dm; 37660910c330SBarry Smith 37672eca1d9cSJed Brown PetscFunctionBegin; 3768089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3769f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 3770e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 377124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 377224989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 37732eca1d9cSJed Brown PetscFunctionReturn(0); 37742eca1d9cSJed Brown } 37752eca1d9cSJed Brown 37766083293cSBarry Smith 37772eca1d9cSJed Brown #undef __FUNCT__ 377883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 37791713a123SBarry Smith /*@C 378083a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 37811713a123SBarry Smith VecView() for the solution at each timestep 37821713a123SBarry Smith 37831713a123SBarry Smith Collective on TS 37841713a123SBarry Smith 37851713a123SBarry Smith Input Parameters: 37861713a123SBarry Smith + ts - the TS context 37871713a123SBarry Smith . step - current time-step 3788142b95e3SSatish Balay . ptime - current time 37890298fd71SBarry Smith - dummy - either a viewer or NULL 37901713a123SBarry Smith 379199fdda47SBarry Smith Options Database: 379299fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 379399fdda47SBarry Smith 3794387f4636SBarry 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 379599fdda47SBarry Smith will look bad 379699fdda47SBarry Smith 37971713a123SBarry Smith Level: intermediate 37981713a123SBarry Smith 37991713a123SBarry Smith .keywords: TS, vector, monitor, view 38001713a123SBarry Smith 3801a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 38021713a123SBarry Smith @*/ 38030910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 38041713a123SBarry Smith { 3805dfbe8321SBarry Smith PetscErrorCode ierr; 380683a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 3807473a3ab2SBarry Smith PetscDraw draw; 38081713a123SBarry Smith 38091713a123SBarry Smith PetscFunctionBegin; 38106083293cSBarry Smith if (!step && ictx->showinitial) { 38116083293cSBarry Smith if (!ictx->initialsolution) { 38120910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 38131713a123SBarry Smith } 38140910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 38156083293cSBarry Smith } 3816b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 38170dcf80beSBarry Smith 38186083293cSBarry Smith if (ictx->showinitial) { 38196083293cSBarry Smith PetscReal pause; 38206083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 38216083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 38226083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 38236083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 38246083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 38256083293cSBarry Smith } 38260910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 3827473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 3828473a3ab2SBarry Smith PetscReal xl,yl,xr,yr,tw,w,h; 3829473a3ab2SBarry Smith char time[32]; 3830473a3ab2SBarry Smith size_t len; 3831473a3ab2SBarry Smith 3832473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 383385b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 3834473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 3835473a3ab2SBarry Smith ierr = PetscStrlen(time,&len);CHKERRQ(ierr); 38360298fd71SBarry Smith ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr); 3837473a3ab2SBarry Smith w = xl + .5*(xr - xl) - .5*len*tw; 3838473a3ab2SBarry Smith h = yl + .95*(yr - yl); 3839473a3ab2SBarry Smith ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 3840473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 3841473a3ab2SBarry Smith } 3842473a3ab2SBarry Smith 38436083293cSBarry Smith if (ictx->showinitial) { 38446083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 38456083293cSBarry Smith } 38461713a123SBarry Smith PetscFunctionReturn(0); 38471713a123SBarry Smith } 38481713a123SBarry Smith 38492d5ee99bSBarry Smith #undef __FUNCT__ 38502d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase" 38512d5ee99bSBarry Smith /*@C 38522d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 38532d5ee99bSBarry Smith 38542d5ee99bSBarry Smith Collective on TS 38552d5ee99bSBarry Smith 38562d5ee99bSBarry Smith Input Parameters: 38572d5ee99bSBarry Smith + ts - the TS context 38582d5ee99bSBarry Smith . step - current time-step 38592d5ee99bSBarry Smith . ptime - current time 38602d5ee99bSBarry Smith - dummy - either a viewer or NULL 38612d5ee99bSBarry Smith 38622d5ee99bSBarry Smith Level: intermediate 38632d5ee99bSBarry Smith 38642d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 38652d5ee99bSBarry Smith 38662d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 38672d5ee99bSBarry Smith @*/ 38682d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 38692d5ee99bSBarry Smith { 38702d5ee99bSBarry Smith PetscErrorCode ierr; 38712d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 38722d5ee99bSBarry Smith PetscDraw draw; 38732d5ee99bSBarry Smith MPI_Comm comm; 38742d5ee99bSBarry Smith PetscInt n; 38752d5ee99bSBarry Smith PetscMPIInt size; 38762d5ee99bSBarry Smith PetscReal xl,yl,xr,yr,tw,w,h; 38772d5ee99bSBarry Smith char time[32]; 38782d5ee99bSBarry Smith size_t len; 38792d5ee99bSBarry Smith const PetscScalar *U; 38802d5ee99bSBarry Smith 38812d5ee99bSBarry Smith PetscFunctionBegin; 38822d5ee99bSBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 38832d5ee99bSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 38842d5ee99bSBarry Smith if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs"); 38852d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 38862d5ee99bSBarry Smith if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 38872d5ee99bSBarry Smith 38882d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 38892d5ee99bSBarry Smith 38902d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 38914b363babSBarry Smith ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 3892ba5783adSMatthew G Knepley if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) { 3893a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 3894a4494fc1SBarry Smith PetscFunctionReturn(0); 3895a4494fc1SBarry Smith } 38962d5ee99bSBarry Smith if (!step) ictx->color++; 38972d5ee99bSBarry Smith ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr); 38982d5ee99bSBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 38992d5ee99bSBarry Smith 39002d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 39014b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 390285b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 39032d5ee99bSBarry Smith ierr = PetscStrlen(time,&len);CHKERRQ(ierr); 39042d5ee99bSBarry Smith ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr); 39052d5ee99bSBarry Smith w = xl + .5*(xr - xl) - .5*len*tw; 39062d5ee99bSBarry Smith h = yl + .95*(yr - yl); 39072d5ee99bSBarry Smith ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 39082d5ee99bSBarry Smith } 39092d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 39102d5ee99bSBarry Smith PetscFunctionReturn(0); 39112d5ee99bSBarry Smith } 39122d5ee99bSBarry Smith 39131713a123SBarry Smith 39146c699258SBarry Smith #undef __FUNCT__ 391583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 39166083293cSBarry Smith /*@C 391783a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 39186083293cSBarry Smith 39196083293cSBarry Smith Collective on TS 39206083293cSBarry Smith 39216083293cSBarry Smith Input Parameters: 39226083293cSBarry Smith . ctx - the monitor context 39236083293cSBarry Smith 39246083293cSBarry Smith Level: intermediate 39256083293cSBarry Smith 39266083293cSBarry Smith .keywords: TS, vector, monitor, view 39276083293cSBarry Smith 392883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 39296083293cSBarry Smith @*/ 393083a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 39316083293cSBarry Smith { 39326083293cSBarry Smith PetscErrorCode ierr; 39336083293cSBarry Smith 39346083293cSBarry Smith PetscFunctionBegin; 39354b363babSBarry Smith ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr); 393683a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 393783a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 393883a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 39396083293cSBarry Smith PetscFunctionReturn(0); 39406083293cSBarry Smith } 39416083293cSBarry Smith 39426083293cSBarry Smith #undef __FUNCT__ 394383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 39446083293cSBarry Smith /*@C 394583a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 39466083293cSBarry Smith 39476083293cSBarry Smith Collective on TS 39486083293cSBarry Smith 39496083293cSBarry Smith Input Parameter: 39506083293cSBarry Smith . ts - time-step context 39516083293cSBarry Smith 39526083293cSBarry Smith Output Patameter: 39536083293cSBarry Smith . ctx - the monitor context 39546083293cSBarry Smith 395599fdda47SBarry Smith Options Database: 395699fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 395799fdda47SBarry Smith 39586083293cSBarry Smith Level: intermediate 39596083293cSBarry Smith 39606083293cSBarry Smith .keywords: TS, vector, monitor, view 39616083293cSBarry Smith 396283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 39636083293cSBarry Smith @*/ 396483a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 39656083293cSBarry Smith { 39666083293cSBarry Smith PetscErrorCode ierr; 39676083293cSBarry Smith 39686083293cSBarry Smith PetscFunctionBegin; 3969b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 397083a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 3971e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 3972bbd56ea5SKarl Rupp 397383a4ac43SBarry Smith (*ctx)->howoften = howoften; 3974473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 39750298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 3976473a3ab2SBarry Smith 3977473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 39780298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 39792d5ee99bSBarry Smith (*ctx)->color = PETSC_DRAW_WHITE; 39806083293cSBarry Smith PetscFunctionReturn(0); 39816083293cSBarry Smith } 39826083293cSBarry Smith 39836083293cSBarry Smith #undef __FUNCT__ 398483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 39853a471f94SBarry Smith /*@C 398683a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 39873a471f94SBarry Smith VecView() for the error at each timestep 39883a471f94SBarry Smith 39893a471f94SBarry Smith Collective on TS 39903a471f94SBarry Smith 39913a471f94SBarry Smith Input Parameters: 39923a471f94SBarry Smith + ts - the TS context 39933a471f94SBarry Smith . step - current time-step 39943a471f94SBarry Smith . ptime - current time 39950298fd71SBarry Smith - dummy - either a viewer or NULL 39963a471f94SBarry Smith 39973a471f94SBarry Smith Level: intermediate 39983a471f94SBarry Smith 39993a471f94SBarry Smith .keywords: TS, vector, monitor, view 40003a471f94SBarry Smith 40013a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 40023a471f94SBarry Smith @*/ 40030910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 40043a471f94SBarry Smith { 40053a471f94SBarry Smith PetscErrorCode ierr; 400683a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 400783a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 40083a471f94SBarry Smith Vec work; 40093a471f94SBarry Smith 40103a471f94SBarry Smith PetscFunctionBegin; 4011b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 40120910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 40133a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 40140910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 40153a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 40163a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 40173a471f94SBarry Smith PetscFunctionReturn(0); 40183a471f94SBarry Smith } 40193a471f94SBarry Smith 40202a34c10cSBarry Smith #include <petsc-private/dmimpl.h> 40213a471f94SBarry Smith #undef __FUNCT__ 40226c699258SBarry Smith #define __FUNCT__ "TSSetDM" 40236c699258SBarry Smith /*@ 40246c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 40256c699258SBarry Smith 40263f9fe445SBarry Smith Logically Collective on TS and DM 40276c699258SBarry Smith 40286c699258SBarry Smith Input Parameters: 40296c699258SBarry Smith + ts - the preconditioner context 40306c699258SBarry Smith - dm - the dm 40316c699258SBarry Smith 40326c699258SBarry Smith Level: intermediate 40336c699258SBarry Smith 40346c699258SBarry Smith 40356c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 40366c699258SBarry Smith @*/ 40377087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 40386c699258SBarry Smith { 40396c699258SBarry Smith PetscErrorCode ierr; 4040089b2837SJed Brown SNES snes; 4041942e3340SBarry Smith DMTS tsdm; 40426c699258SBarry Smith 40436c699258SBarry Smith PetscFunctionBegin; 40440700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 404570663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4046942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 40472a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4048942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4049942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 405024989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 405124989b8cSPeter Brune tsdm->originaldm = dm; 405224989b8cSPeter Brune } 405324989b8cSPeter Brune } 40546bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 405524989b8cSPeter Brune } 40566c699258SBarry Smith ts->dm = dm; 4057bbd56ea5SKarl Rupp 4058089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4059089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 40606c699258SBarry Smith PetscFunctionReturn(0); 40616c699258SBarry Smith } 40626c699258SBarry Smith 40636c699258SBarry Smith #undef __FUNCT__ 40646c699258SBarry Smith #define __FUNCT__ "TSGetDM" 40656c699258SBarry Smith /*@ 40666c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 40676c699258SBarry Smith 40683f9fe445SBarry Smith Not Collective 40696c699258SBarry Smith 40706c699258SBarry Smith Input Parameter: 40716c699258SBarry Smith . ts - the preconditioner context 40726c699258SBarry Smith 40736c699258SBarry Smith Output Parameter: 40746c699258SBarry Smith . dm - the dm 40756c699258SBarry Smith 40766c699258SBarry Smith Level: intermediate 40776c699258SBarry Smith 40786c699258SBarry Smith 40796c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 40806c699258SBarry Smith @*/ 40817087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 40826c699258SBarry Smith { 4083496e6a7aSJed Brown PetscErrorCode ierr; 4084496e6a7aSJed Brown 40856c699258SBarry Smith PetscFunctionBegin; 40860700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4087496e6a7aSJed Brown if (!ts->dm) { 4088ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4089496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4090496e6a7aSJed Brown } 40916c699258SBarry Smith *dm = ts->dm; 40926c699258SBarry Smith PetscFunctionReturn(0); 40936c699258SBarry Smith } 40941713a123SBarry Smith 40950f5c6efeSJed Brown #undef __FUNCT__ 40960f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 40970f5c6efeSJed Brown /*@ 40980f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 40990f5c6efeSJed Brown 41003f9fe445SBarry Smith Logically Collective on SNES 41010f5c6efeSJed Brown 41020f5c6efeSJed Brown Input Parameter: 4103d42a1c89SJed Brown + snes - nonlinear solver 41040910c330SBarry Smith . U - the current state at which to evaluate the residual 4105d42a1c89SJed Brown - ctx - user context, must be a TS 41060f5c6efeSJed Brown 41070f5c6efeSJed Brown Output Parameter: 41080f5c6efeSJed Brown . F - the nonlinear residual 41090f5c6efeSJed Brown 41100f5c6efeSJed Brown Notes: 41110f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 41120f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 41130f5c6efeSJed Brown 41140f5c6efeSJed Brown Level: advanced 41150f5c6efeSJed Brown 41160f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 41170f5c6efeSJed Brown @*/ 41180910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 41190f5c6efeSJed Brown { 41200f5c6efeSJed Brown TS ts = (TS)ctx; 41210f5c6efeSJed Brown PetscErrorCode ierr; 41220f5c6efeSJed Brown 41230f5c6efeSJed Brown PetscFunctionBegin; 41240f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 41250910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 41260f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 41270f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 41280910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 41290f5c6efeSJed Brown PetscFunctionReturn(0); 41300f5c6efeSJed Brown } 41310f5c6efeSJed Brown 41320f5c6efeSJed Brown #undef __FUNCT__ 41330f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 41340f5c6efeSJed Brown /*@ 41350f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 41360f5c6efeSJed Brown 41370f5c6efeSJed Brown Collective on SNES 41380f5c6efeSJed Brown 41390f5c6efeSJed Brown Input Parameter: 41400f5c6efeSJed Brown + snes - nonlinear solver 41410910c330SBarry Smith . U - the current state at which to evaluate the residual 41420f5c6efeSJed Brown - ctx - user context, must be a TS 41430f5c6efeSJed Brown 41440f5c6efeSJed Brown Output Parameter: 41450f5c6efeSJed Brown + A - the Jacobian 41460f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 41470f5c6efeSJed Brown - flag - indicates any structure change in the matrix 41480f5c6efeSJed Brown 41490f5c6efeSJed Brown Notes: 41500f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 41510f5c6efeSJed Brown 41520f5c6efeSJed Brown Level: developer 41530f5c6efeSJed Brown 41540f5c6efeSJed Brown .seealso: SNESSetJacobian() 41550f5c6efeSJed Brown @*/ 4156d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 41570f5c6efeSJed Brown { 41580f5c6efeSJed Brown TS ts = (TS)ctx; 41590f5c6efeSJed Brown PetscErrorCode ierr; 41600f5c6efeSJed Brown 41610f5c6efeSJed Brown PetscFunctionBegin; 41620f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 41630910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 41640f5c6efeSJed Brown PetscValidPointer(A,3); 416594ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 41660f5c6efeSJed Brown PetscValidPointer(B,4); 416794ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 41680f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 4169d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 41700f5c6efeSJed Brown PetscFunctionReturn(0); 41710f5c6efeSJed Brown } 4172325fc9f4SBarry Smith 41730e4ef248SJed Brown #undef __FUNCT__ 41740e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 41750e4ef248SJed Brown /*@C 41760e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 41770e4ef248SJed Brown 41780e4ef248SJed Brown Collective on TS 41790e4ef248SJed Brown 41800e4ef248SJed Brown Input Arguments: 41810e4ef248SJed Brown + ts - time stepping context 41820e4ef248SJed Brown . t - time at which to evaluate 41830910c330SBarry Smith . U - state at which to evaluate 41840e4ef248SJed Brown - ctx - context 41850e4ef248SJed Brown 41860e4ef248SJed Brown Output Arguments: 41870e4ef248SJed Brown . F - right hand side 41880e4ef248SJed Brown 41890e4ef248SJed Brown Level: intermediate 41900e4ef248SJed Brown 41910e4ef248SJed Brown Notes: 41920e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 41930e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 41940e4ef248SJed Brown 41950e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 41960e4ef248SJed Brown @*/ 41970910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 41980e4ef248SJed Brown { 41990e4ef248SJed Brown PetscErrorCode ierr; 42000e4ef248SJed Brown Mat Arhs,Brhs; 42010e4ef248SJed Brown 42020e4ef248SJed Brown PetscFunctionBegin; 42030e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 4204d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 42050910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 42060e4ef248SJed Brown PetscFunctionReturn(0); 42070e4ef248SJed Brown } 42080e4ef248SJed Brown 42090e4ef248SJed Brown #undef __FUNCT__ 42100e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 42110e4ef248SJed Brown /*@C 42120e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 42130e4ef248SJed Brown 42140e4ef248SJed Brown Collective on TS 42150e4ef248SJed Brown 42160e4ef248SJed Brown Input Arguments: 42170e4ef248SJed Brown + ts - time stepping context 42180e4ef248SJed Brown . t - time at which to evaluate 42190910c330SBarry Smith . U - state at which to evaluate 42200e4ef248SJed Brown - ctx - context 42210e4ef248SJed Brown 42220e4ef248SJed Brown Output Arguments: 42230e4ef248SJed Brown + A - pointer to operator 42240e4ef248SJed Brown . B - pointer to preconditioning matrix 42250e4ef248SJed Brown - flg - matrix structure flag 42260e4ef248SJed Brown 42270e4ef248SJed Brown Level: intermediate 42280e4ef248SJed Brown 42290e4ef248SJed Brown Notes: 42300e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 42310e4ef248SJed Brown 42320e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 42330e4ef248SJed Brown @*/ 4234d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 42350e4ef248SJed Brown { 42360e4ef248SJed Brown PetscFunctionBegin; 42370e4ef248SJed Brown PetscFunctionReturn(0); 42380e4ef248SJed Brown } 42390e4ef248SJed Brown 42400026cea9SSean Farley #undef __FUNCT__ 42410026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 42420026cea9SSean Farley /*@C 42430026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 42440026cea9SSean Farley 42450026cea9SSean Farley Collective on TS 42460026cea9SSean Farley 42470026cea9SSean Farley Input Arguments: 42480026cea9SSean Farley + ts - time stepping context 42490026cea9SSean Farley . t - time at which to evaluate 42500910c330SBarry Smith . U - state at which to evaluate 42510910c330SBarry Smith . Udot - time derivative of state vector 42520026cea9SSean Farley - ctx - context 42530026cea9SSean Farley 42540026cea9SSean Farley Output Arguments: 42550026cea9SSean Farley . F - left hand side 42560026cea9SSean Farley 42570026cea9SSean Farley Level: intermediate 42580026cea9SSean Farley 42590026cea9SSean Farley Notes: 42600910c330SBarry 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 42610026cea9SSean Farley user is required to write their own TSComputeIFunction. 42620026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 42630026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 42640026cea9SSean Farley 42650026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 42660026cea9SSean Farley @*/ 42670910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 42680026cea9SSean Farley { 42690026cea9SSean Farley PetscErrorCode ierr; 42700026cea9SSean Farley Mat A,B; 42710026cea9SSean Farley 42720026cea9SSean Farley PetscFunctionBegin; 42730298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 4274d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 42750910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 42760026cea9SSean Farley PetscFunctionReturn(0); 42770026cea9SSean Farley } 42780026cea9SSean Farley 42790026cea9SSean Farley #undef __FUNCT__ 42800026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 42810026cea9SSean Farley /*@C 4282b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 42830026cea9SSean Farley 42840026cea9SSean Farley Collective on TS 42850026cea9SSean Farley 42860026cea9SSean Farley Input Arguments: 42870026cea9SSean Farley + ts - time stepping context 42880026cea9SSean Farley . t - time at which to evaluate 42890910c330SBarry Smith . U - state at which to evaluate 42900910c330SBarry Smith . Udot - time derivative of state vector 42910026cea9SSean Farley . shift - shift to apply 42920026cea9SSean Farley - ctx - context 42930026cea9SSean Farley 42940026cea9SSean Farley Output Arguments: 42950026cea9SSean Farley + A - pointer to operator 42960026cea9SSean Farley . B - pointer to preconditioning matrix 42970026cea9SSean Farley - flg - matrix structure flag 42980026cea9SSean Farley 4299b41af12eSJed Brown Level: advanced 43000026cea9SSean Farley 43010026cea9SSean Farley Notes: 43020026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 43030026cea9SSean Farley 4304b41af12eSJed Brown It is only appropriate for problems of the form 4305b41af12eSJed Brown 4306b41af12eSJed Brown $ M Udot = F(U,t) 4307b41af12eSJed Brown 4308b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4309b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4310b41af12eSJed Brown an implicit operator of the form 4311b41af12eSJed Brown 4312b41af12eSJed Brown $ shift*M + J 4313b41af12eSJed Brown 4314b41af12eSJed 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 4315b41af12eSJed Brown a copy of M or reassemble it when requested. 4316b41af12eSJed Brown 43170026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 43180026cea9SSean Farley @*/ 4319d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 43200026cea9SSean Farley { 4321b41af12eSJed Brown PetscErrorCode ierr; 4322b41af12eSJed Brown 43230026cea9SSean Farley PetscFunctionBegin; 432494ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4325b41af12eSJed Brown ts->ijacobian.shift = shift; 43260026cea9SSean Farley PetscFunctionReturn(0); 43270026cea9SSean Farley } 4328b41af12eSJed Brown 4329e817cc15SEmil Constantinescu #undef __FUNCT__ 4330e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType" 4331e817cc15SEmil Constantinescu /*@ 4332e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 4333e817cc15SEmil Constantinescu 4334e817cc15SEmil Constantinescu Not Collective 4335e817cc15SEmil Constantinescu 4336e817cc15SEmil Constantinescu Input Parameter: 4337e817cc15SEmil Constantinescu . ts - the TS context 4338e817cc15SEmil Constantinescu 4339e817cc15SEmil Constantinescu Output Parameter: 43404e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4341e817cc15SEmil Constantinescu 4342e817cc15SEmil Constantinescu Level: beginner 4343e817cc15SEmil Constantinescu 4344e817cc15SEmil Constantinescu .keywords: TS, equation type 4345e817cc15SEmil Constantinescu 4346e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 4347e817cc15SEmil Constantinescu @*/ 4348e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4349e817cc15SEmil Constantinescu { 4350e817cc15SEmil Constantinescu PetscFunctionBegin; 4351e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4352e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 4353e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 4354e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4355e817cc15SEmil Constantinescu } 4356e817cc15SEmil Constantinescu 4357e817cc15SEmil Constantinescu #undef __FUNCT__ 4358e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType" 4359e817cc15SEmil Constantinescu /*@ 4360e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 4361e817cc15SEmil Constantinescu 4362e817cc15SEmil Constantinescu Not Collective 4363e817cc15SEmil Constantinescu 4364e817cc15SEmil Constantinescu Input Parameter: 4365e817cc15SEmil Constantinescu + ts - the TS context 43664e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4367e817cc15SEmil Constantinescu 4368e817cc15SEmil Constantinescu Level: advanced 4369e817cc15SEmil Constantinescu 4370e817cc15SEmil Constantinescu .keywords: TS, equation type 4371e817cc15SEmil Constantinescu 4372e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 4373e817cc15SEmil Constantinescu @*/ 4374e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4375e817cc15SEmil Constantinescu { 4376e817cc15SEmil Constantinescu PetscFunctionBegin; 4377e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4378e817cc15SEmil Constantinescu ts->equation_type = equation_type; 4379e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4380e817cc15SEmil Constantinescu } 43810026cea9SSean Farley 43824af1b03aSJed Brown #undef __FUNCT__ 43834af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 43844af1b03aSJed Brown /*@ 43854af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 43864af1b03aSJed Brown 43874af1b03aSJed Brown Not Collective 43884af1b03aSJed Brown 43894af1b03aSJed Brown Input Parameter: 43904af1b03aSJed Brown . ts - the TS context 43914af1b03aSJed Brown 43924af1b03aSJed Brown Output Parameter: 43934af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 43944af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 43954af1b03aSJed Brown 4396487e0bb9SJed Brown Level: beginner 43974af1b03aSJed Brown 4398cd652676SJed Brown Notes: 4399cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 44004af1b03aSJed Brown 44014af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 44024af1b03aSJed Brown 44034af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 44044af1b03aSJed Brown @*/ 44054af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 44064af1b03aSJed Brown { 44074af1b03aSJed Brown PetscFunctionBegin; 44084af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 44094af1b03aSJed Brown PetscValidPointer(reason,2); 44104af1b03aSJed Brown *reason = ts->reason; 44114af1b03aSJed Brown PetscFunctionReturn(0); 44124af1b03aSJed Brown } 44134af1b03aSJed Brown 4414fb1732b5SBarry Smith #undef __FUNCT__ 4415d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 4416d6ad946cSShri Abhyankar /*@ 4417d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4418d6ad946cSShri Abhyankar 4419d6ad946cSShri Abhyankar Not Collective 4420d6ad946cSShri Abhyankar 4421d6ad946cSShri Abhyankar Input Parameter: 4422d6ad946cSShri Abhyankar + ts - the TS context 4423d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4424d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4425d6ad946cSShri Abhyankar 4426f5abba47SShri Abhyankar Level: advanced 4427d6ad946cSShri Abhyankar 4428d6ad946cSShri Abhyankar Notes: 4429d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 4430d6ad946cSShri Abhyankar 4431d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 4432d6ad946cSShri Abhyankar 4433d6ad946cSShri Abhyankar .seealso: TSConvergedReason 4434d6ad946cSShri Abhyankar @*/ 4435d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4436d6ad946cSShri Abhyankar { 4437d6ad946cSShri Abhyankar PetscFunctionBegin; 4438d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4439d6ad946cSShri Abhyankar ts->reason = reason; 4440d6ad946cSShri Abhyankar PetscFunctionReturn(0); 4441d6ad946cSShri Abhyankar } 4442d6ad946cSShri Abhyankar 4443d6ad946cSShri Abhyankar #undef __FUNCT__ 4444cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 4445cc708dedSBarry Smith /*@ 4446cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 4447cc708dedSBarry Smith 4448cc708dedSBarry Smith Not Collective 4449cc708dedSBarry Smith 4450cc708dedSBarry Smith Input Parameter: 4451cc708dedSBarry Smith . ts - the TS context 4452cc708dedSBarry Smith 4453cc708dedSBarry Smith Output Parameter: 4454cc708dedSBarry Smith . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 4455cc708dedSBarry Smith 4456487e0bb9SJed Brown Level: beginner 4457cc708dedSBarry Smith 4458cc708dedSBarry Smith Notes: 4459cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 4460cc708dedSBarry Smith 4461cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 4462cc708dedSBarry Smith 4463cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 4464cc708dedSBarry Smith @*/ 4465cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 4466cc708dedSBarry Smith { 4467cc708dedSBarry Smith PetscFunctionBegin; 4468cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4469cc708dedSBarry Smith PetscValidPointer(ftime,2); 4470cc708dedSBarry Smith *ftime = ts->solvetime; 4471cc708dedSBarry Smith PetscFunctionReturn(0); 4472cc708dedSBarry Smith } 4473cc708dedSBarry Smith 4474cc708dedSBarry Smith #undef __FUNCT__ 44752c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps" 44762c18e0fdSBarry Smith /*@ 44772c18e0fdSBarry Smith TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 44782c18e0fdSBarry Smith 44792c18e0fdSBarry Smith Not Collective 44802c18e0fdSBarry Smith 44812c18e0fdSBarry Smith Input Parameter: 44822c18e0fdSBarry Smith . ts - the TS context 44832c18e0fdSBarry Smith 44842c18e0fdSBarry Smith Output Parameter: 44852c18e0fdSBarry Smith . steps - the number of steps 44862c18e0fdSBarry Smith 44872c18e0fdSBarry Smith Level: beginner 44882c18e0fdSBarry Smith 44892c18e0fdSBarry Smith Notes: 44902c18e0fdSBarry Smith Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 44912c18e0fdSBarry Smith 44922c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test 44932c18e0fdSBarry Smith 44942c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 44952c18e0fdSBarry Smith @*/ 44962c18e0fdSBarry Smith PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 44972c18e0fdSBarry Smith { 44982c18e0fdSBarry Smith PetscFunctionBegin; 44992c18e0fdSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45002c18e0fdSBarry Smith PetscValidPointer(steps,2); 45012c18e0fdSBarry Smith *steps = ts->total_steps; 45022c18e0fdSBarry Smith PetscFunctionReturn(0); 45032c18e0fdSBarry Smith } 45042c18e0fdSBarry Smith 45052c18e0fdSBarry Smith #undef __FUNCT__ 45065ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 45079f67acb7SJed Brown /*@ 45085ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 45099f67acb7SJed Brown used by the time integrator. 45109f67acb7SJed Brown 45119f67acb7SJed Brown Not Collective 45129f67acb7SJed Brown 45139f67acb7SJed Brown Input Parameter: 45149f67acb7SJed Brown . ts - TS context 45159f67acb7SJed Brown 45169f67acb7SJed Brown Output Parameter: 45179f67acb7SJed Brown . nits - number of nonlinear iterations 45189f67acb7SJed Brown 45199f67acb7SJed Brown Notes: 45209f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 45219f67acb7SJed Brown 45229f67acb7SJed Brown Level: intermediate 45239f67acb7SJed Brown 45249f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 45259f67acb7SJed Brown 45265ef26d82SJed Brown .seealso: TSGetKSPIterations() 45279f67acb7SJed Brown @*/ 45285ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 45299f67acb7SJed Brown { 45309f67acb7SJed Brown PetscFunctionBegin; 45319f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45329f67acb7SJed Brown PetscValidIntPointer(nits,2); 45335ef26d82SJed Brown *nits = ts->snes_its; 45349f67acb7SJed Brown PetscFunctionReturn(0); 45359f67acb7SJed Brown } 45369f67acb7SJed Brown 45379f67acb7SJed Brown #undef __FUNCT__ 45385ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 45399f67acb7SJed Brown /*@ 45405ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 45419f67acb7SJed Brown used by the time integrator. 45429f67acb7SJed Brown 45439f67acb7SJed Brown Not Collective 45449f67acb7SJed Brown 45459f67acb7SJed Brown Input Parameter: 45469f67acb7SJed Brown . ts - TS context 45479f67acb7SJed Brown 45489f67acb7SJed Brown Output Parameter: 45499f67acb7SJed Brown . lits - number of linear iterations 45509f67acb7SJed Brown 45519f67acb7SJed Brown Notes: 45529f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 45539f67acb7SJed Brown 45549f67acb7SJed Brown Level: intermediate 45559f67acb7SJed Brown 45569f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 45579f67acb7SJed Brown 45585ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 45599f67acb7SJed Brown @*/ 45605ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 45619f67acb7SJed Brown { 45629f67acb7SJed Brown PetscFunctionBegin; 45639f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45649f67acb7SJed Brown PetscValidIntPointer(lits,2); 45655ef26d82SJed Brown *lits = ts->ksp_its; 45669f67acb7SJed Brown PetscFunctionReturn(0); 45679f67acb7SJed Brown } 45689f67acb7SJed Brown 45699f67acb7SJed Brown #undef __FUNCT__ 4570cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 4571cef5090cSJed Brown /*@ 4572cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4573cef5090cSJed Brown 4574cef5090cSJed Brown Not Collective 4575cef5090cSJed Brown 4576cef5090cSJed Brown Input Parameter: 4577cef5090cSJed Brown . ts - TS context 4578cef5090cSJed Brown 4579cef5090cSJed Brown Output Parameter: 4580cef5090cSJed Brown . rejects - number of steps rejected 4581cef5090cSJed Brown 4582cef5090cSJed Brown Notes: 4583cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4584cef5090cSJed Brown 4585cef5090cSJed Brown Level: intermediate 4586cef5090cSJed Brown 4587cef5090cSJed Brown .keywords: TS, get, number 4588cef5090cSJed Brown 45895ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 4590cef5090cSJed Brown @*/ 4591cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 4592cef5090cSJed Brown { 4593cef5090cSJed Brown PetscFunctionBegin; 4594cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4595cef5090cSJed Brown PetscValidIntPointer(rejects,2); 4596cef5090cSJed Brown *rejects = ts->reject; 4597cef5090cSJed Brown PetscFunctionReturn(0); 4598cef5090cSJed Brown } 4599cef5090cSJed Brown 4600cef5090cSJed Brown #undef __FUNCT__ 4601cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 4602cef5090cSJed Brown /*@ 4603cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 4604cef5090cSJed Brown 4605cef5090cSJed Brown Not Collective 4606cef5090cSJed Brown 4607cef5090cSJed Brown Input Parameter: 4608cef5090cSJed Brown . ts - TS context 4609cef5090cSJed Brown 4610cef5090cSJed Brown Output Parameter: 4611cef5090cSJed Brown . fails - number of failed nonlinear solves 4612cef5090cSJed Brown 4613cef5090cSJed Brown Notes: 4614cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4615cef5090cSJed Brown 4616cef5090cSJed Brown Level: intermediate 4617cef5090cSJed Brown 4618cef5090cSJed Brown .keywords: TS, get, number 4619cef5090cSJed Brown 46205ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 4621cef5090cSJed Brown @*/ 4622cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 4623cef5090cSJed Brown { 4624cef5090cSJed Brown PetscFunctionBegin; 4625cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4626cef5090cSJed Brown PetscValidIntPointer(fails,2); 4627cef5090cSJed Brown *fails = ts->num_snes_failures; 4628cef5090cSJed Brown PetscFunctionReturn(0); 4629cef5090cSJed Brown } 4630cef5090cSJed Brown 4631cef5090cSJed Brown #undef __FUNCT__ 4632cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 4633cef5090cSJed Brown /*@ 4634cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 4635cef5090cSJed Brown 4636cef5090cSJed Brown Not Collective 4637cef5090cSJed Brown 4638cef5090cSJed Brown Input Parameter: 4639cef5090cSJed Brown + ts - TS context 4640cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 4641cef5090cSJed Brown 4642cef5090cSJed Brown Notes: 4643cef5090cSJed Brown The counter is reset to zero for each step 4644cef5090cSJed Brown 4645cef5090cSJed Brown Options Database Key: 4646cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4647cef5090cSJed Brown 4648cef5090cSJed Brown Level: intermediate 4649cef5090cSJed Brown 4650cef5090cSJed Brown .keywords: TS, set, maximum, number 4651cef5090cSJed Brown 46525ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4653cef5090cSJed Brown @*/ 4654cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 4655cef5090cSJed Brown { 4656cef5090cSJed Brown PetscFunctionBegin; 4657cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4658cef5090cSJed Brown ts->max_reject = rejects; 4659cef5090cSJed Brown PetscFunctionReturn(0); 4660cef5090cSJed Brown } 4661cef5090cSJed Brown 4662cef5090cSJed Brown #undef __FUNCT__ 4663cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 4664cef5090cSJed Brown /*@ 4665cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 4666cef5090cSJed Brown 4667cef5090cSJed Brown Not Collective 4668cef5090cSJed Brown 4669cef5090cSJed Brown Input Parameter: 4670cef5090cSJed Brown + ts - TS context 4671cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 4672cef5090cSJed Brown 4673cef5090cSJed Brown Notes: 4674cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 4675cef5090cSJed Brown 4676cef5090cSJed Brown Options Database Key: 4677cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4678cef5090cSJed Brown 4679cef5090cSJed Brown Level: intermediate 4680cef5090cSJed Brown 4681cef5090cSJed Brown .keywords: TS, set, maximum, number 4682cef5090cSJed Brown 46835ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 4684cef5090cSJed Brown @*/ 4685cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 4686cef5090cSJed Brown { 4687cef5090cSJed Brown PetscFunctionBegin; 4688cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4689cef5090cSJed Brown ts->max_snes_failures = fails; 4690cef5090cSJed Brown PetscFunctionReturn(0); 4691cef5090cSJed Brown } 4692cef5090cSJed Brown 4693cef5090cSJed Brown #undef __FUNCT__ 46944e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails" 4695cef5090cSJed Brown /*@ 4696cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 4697cef5090cSJed Brown 4698cef5090cSJed Brown Not Collective 4699cef5090cSJed Brown 4700cef5090cSJed Brown Input Parameter: 4701cef5090cSJed Brown + ts - TS context 4702cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 4703cef5090cSJed Brown 4704cef5090cSJed Brown Options Database Key: 4705cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 4706cef5090cSJed Brown 4707cef5090cSJed Brown Level: intermediate 4708cef5090cSJed Brown 4709cef5090cSJed Brown .keywords: TS, set, error 4710cef5090cSJed Brown 47115ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4712cef5090cSJed Brown @*/ 4713cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 4714cef5090cSJed Brown { 4715cef5090cSJed Brown PetscFunctionBegin; 4716cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4717cef5090cSJed Brown ts->errorifstepfailed = err; 4718cef5090cSJed Brown PetscFunctionReturn(0); 4719cef5090cSJed Brown } 4720cef5090cSJed Brown 4721cef5090cSJed Brown #undef __FUNCT__ 4722fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 4723fb1732b5SBarry Smith /*@C 4724fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 4725fb1732b5SBarry Smith 4726fb1732b5SBarry Smith Collective on TS 4727fb1732b5SBarry Smith 4728fb1732b5SBarry Smith Input Parameters: 4729fb1732b5SBarry Smith + ts - the TS context 4730fb1732b5SBarry Smith . step - current time-step 4731fb1732b5SBarry Smith . ptime - current time 47320910c330SBarry Smith . u - current state 4733fb1732b5SBarry Smith - viewer - binary viewer 4734fb1732b5SBarry Smith 4735fb1732b5SBarry Smith Level: intermediate 4736fb1732b5SBarry Smith 4737fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 4738fb1732b5SBarry Smith 4739fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4740fb1732b5SBarry Smith @*/ 47410910c330SBarry Smith PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 4742fb1732b5SBarry Smith { 4743fb1732b5SBarry Smith PetscErrorCode ierr; 4744ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 4745fb1732b5SBarry Smith 4746fb1732b5SBarry Smith PetscFunctionBegin; 47470910c330SBarry Smith ierr = VecView(u,v);CHKERRQ(ierr); 4748ed81e22dSJed Brown PetscFunctionReturn(0); 4749ed81e22dSJed Brown } 4750ed81e22dSJed Brown 4751ed81e22dSJed Brown #undef __FUNCT__ 4752ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 4753ed81e22dSJed Brown /*@C 4754ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 4755ed81e22dSJed Brown 4756ed81e22dSJed Brown Collective on TS 4757ed81e22dSJed Brown 4758ed81e22dSJed Brown Input Parameters: 4759ed81e22dSJed Brown + ts - the TS context 4760ed81e22dSJed Brown . step - current time-step 4761ed81e22dSJed Brown . ptime - current time 47620910c330SBarry Smith . u - current state 4763ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 4764ed81e22dSJed Brown 4765ed81e22dSJed Brown Level: intermediate 4766ed81e22dSJed Brown 4767ed81e22dSJed Brown Notes: 4768ed81e22dSJed 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. 4769ed81e22dSJed Brown These are named according to the file name template. 4770ed81e22dSJed Brown 4771ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 4772ed81e22dSJed Brown 4773ed81e22dSJed Brown .keywords: TS, vector, monitor, view 4774ed81e22dSJed Brown 4775ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4776ed81e22dSJed Brown @*/ 47770910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 4778ed81e22dSJed Brown { 4779ed81e22dSJed Brown PetscErrorCode ierr; 4780ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 4781ed81e22dSJed Brown PetscViewer viewer; 4782ed81e22dSJed Brown 4783ed81e22dSJed Brown PetscFunctionBegin; 47848caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 4785ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 47860910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 4787ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 4788ed81e22dSJed Brown PetscFunctionReturn(0); 4789ed81e22dSJed Brown } 4790ed81e22dSJed Brown 4791ed81e22dSJed Brown #undef __FUNCT__ 4792ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 4793ed81e22dSJed Brown /*@C 4794ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 4795ed81e22dSJed Brown 4796ed81e22dSJed Brown Collective on TS 4797ed81e22dSJed Brown 4798ed81e22dSJed Brown Input Parameters: 4799ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 4800ed81e22dSJed Brown 4801ed81e22dSJed Brown Level: intermediate 4802ed81e22dSJed Brown 4803ed81e22dSJed Brown Note: 4804ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 4805ed81e22dSJed Brown 4806ed81e22dSJed Brown .keywords: TS, vector, monitor, view 4807ed81e22dSJed Brown 4808ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 4809ed81e22dSJed Brown @*/ 4810ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 4811ed81e22dSJed Brown { 4812ed81e22dSJed Brown PetscErrorCode ierr; 4813ed81e22dSJed Brown 4814ed81e22dSJed Brown PetscFunctionBegin; 4815ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 4816fb1732b5SBarry Smith PetscFunctionReturn(0); 4817fb1732b5SBarry Smith } 4818fb1732b5SBarry Smith 481984df9cb4SJed Brown #undef __FUNCT__ 4820552698daSJed Brown #define __FUNCT__ "TSGetAdapt" 482184df9cb4SJed Brown /*@ 4822552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 482384df9cb4SJed Brown 4824ed81e22dSJed Brown Collective on TS if controller has not been created yet 482584df9cb4SJed Brown 482684df9cb4SJed Brown Input Arguments: 4827ed81e22dSJed Brown . ts - time stepping context 482884df9cb4SJed Brown 482984df9cb4SJed Brown Output Arguments: 4830ed81e22dSJed Brown . adapt - adaptive controller 483184df9cb4SJed Brown 483284df9cb4SJed Brown Level: intermediate 483384df9cb4SJed Brown 4834ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 483584df9cb4SJed Brown @*/ 4836552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 483784df9cb4SJed Brown { 483884df9cb4SJed Brown PetscErrorCode ierr; 483984df9cb4SJed Brown 484084df9cb4SJed Brown PetscFunctionBegin; 484184df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 484284df9cb4SJed Brown PetscValidPointer(adapt,2); 484384df9cb4SJed Brown if (!ts->adapt) { 4844ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 48453bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 48461c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 484784df9cb4SJed Brown } 484884df9cb4SJed Brown *adapt = ts->adapt; 484984df9cb4SJed Brown PetscFunctionReturn(0); 485084df9cb4SJed Brown } 4851d6ebe24aSShri Abhyankar 4852d6ebe24aSShri Abhyankar #undef __FUNCT__ 48531c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 48541c3436cfSJed Brown /*@ 48551c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 48561c3436cfSJed Brown 48571c3436cfSJed Brown Logically Collective 48581c3436cfSJed Brown 48591c3436cfSJed Brown Input Arguments: 48601c3436cfSJed Brown + ts - time integration context 48611c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 48620298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 48631c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 48640298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 48651c3436cfSJed Brown 4866a3cdaa26SBarry Smith Options Database keys: 4867a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 4868a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 4869a3cdaa26SBarry Smith 48701c3436cfSJed Brown Level: beginner 48711c3436cfSJed Brown 4872c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 48731c3436cfSJed Brown @*/ 48741c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 48751c3436cfSJed Brown { 48761c3436cfSJed Brown PetscErrorCode ierr; 48771c3436cfSJed Brown 48781c3436cfSJed Brown PetscFunctionBegin; 4879c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 48801c3436cfSJed Brown if (vatol) { 48811c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 48821c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 4883bbd56ea5SKarl Rupp 48841c3436cfSJed Brown ts->vatol = vatol; 48851c3436cfSJed Brown } 4886c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 48871c3436cfSJed Brown if (vrtol) { 48881c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 48891c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 4890bbd56ea5SKarl Rupp 48911c3436cfSJed Brown ts->vrtol = vrtol; 48921c3436cfSJed Brown } 48931c3436cfSJed Brown PetscFunctionReturn(0); 48941c3436cfSJed Brown } 48951c3436cfSJed Brown 48961c3436cfSJed Brown #undef __FUNCT__ 4897c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 4898c5033834SJed Brown /*@ 4899c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 4900c5033834SJed Brown 4901c5033834SJed Brown Logically Collective 4902c5033834SJed Brown 4903c5033834SJed Brown Input Arguments: 4904c5033834SJed Brown . ts - time integration context 4905c5033834SJed Brown 4906c5033834SJed Brown Output Arguments: 49070298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 49080298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 49090298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 49100298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 4911c5033834SJed Brown 4912c5033834SJed Brown Level: beginner 4913c5033834SJed Brown 4914c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 4915c5033834SJed Brown @*/ 4916c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 4917c5033834SJed Brown { 4918c5033834SJed Brown PetscFunctionBegin; 4919c5033834SJed Brown if (atol) *atol = ts->atol; 4920c5033834SJed Brown if (vatol) *vatol = ts->vatol; 4921c5033834SJed Brown if (rtol) *rtol = ts->rtol; 4922c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 4923c5033834SJed Brown PetscFunctionReturn(0); 4924c5033834SJed Brown } 4925c5033834SJed Brown 4926c5033834SJed Brown #undef __FUNCT__ 49271c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS" 49281c3436cfSJed Brown /*@ 49291c3436cfSJed Brown TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 49301c3436cfSJed Brown 49311c3436cfSJed Brown Collective on TS 49321c3436cfSJed Brown 49331c3436cfSJed Brown Input Arguments: 49341c3436cfSJed Brown + ts - time stepping context 49351c3436cfSJed Brown - Y - state vector to be compared to ts->vec_sol 49361c3436cfSJed Brown 49371c3436cfSJed Brown Output Arguments: 49381c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 49391c3436cfSJed Brown 49401c3436cfSJed Brown Level: developer 49411c3436cfSJed Brown 49421c3436cfSJed Brown .seealso: TSSetTolerances() 49431c3436cfSJed Brown @*/ 49441c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 49451c3436cfSJed Brown { 49461c3436cfSJed Brown PetscErrorCode ierr; 49471c3436cfSJed Brown PetscInt i,n,N; 49480910c330SBarry Smith const PetscScalar *u,*y; 49490910c330SBarry Smith Vec U; 49501c3436cfSJed Brown PetscReal sum,gsum; 49511c3436cfSJed Brown 49521c3436cfSJed Brown PetscFunctionBegin; 49531c3436cfSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 49541c3436cfSJed Brown PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 49551c3436cfSJed Brown PetscValidPointer(norm,3); 49560910c330SBarry Smith U = ts->vec_sol; 49570910c330SBarry Smith PetscCheckSameTypeAndComm(U,1,Y,2); 4958ce94432eSBarry Smith if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 49591c3436cfSJed Brown 49600910c330SBarry Smith ierr = VecGetSize(U,&N);CHKERRQ(ierr); 49610910c330SBarry Smith ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 49620910c330SBarry Smith ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 49631c3436cfSJed Brown ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 49641c3436cfSJed Brown sum = 0.; 4965ceefc113SJed Brown if (ts->vatol && ts->vrtol) { 4966ceefc113SJed Brown const PetscScalar *atol,*rtol; 4967ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4968ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4969ceefc113SJed Brown for (i=0; i<n; i++) { 49700910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 49710910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4972ceefc113SJed Brown } 4973ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4974ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4975ceefc113SJed Brown } else if (ts->vatol) { /* vector atol, scalar rtol */ 4976ceefc113SJed Brown const PetscScalar *atol; 4977ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4978ceefc113SJed Brown for (i=0; i<n; i++) { 49790910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 49800910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4981ceefc113SJed Brown } 4982ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4983ceefc113SJed Brown } else if (ts->vrtol) { /* scalar atol, vector rtol */ 4984ceefc113SJed Brown const PetscScalar *rtol; 4985ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4986ceefc113SJed Brown for (i=0; i<n; i++) { 49870910c330SBarry Smith PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 49880910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4989ceefc113SJed Brown } 4990ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4991ceefc113SJed Brown } else { /* scalar atol, scalar rtol */ 49921c3436cfSJed Brown for (i=0; i<n; i++) { 49930910c330SBarry Smith PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 49940910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 49951c3436cfSJed Brown } 4996ceefc113SJed Brown } 49970910c330SBarry Smith ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 49981c3436cfSJed Brown ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 49991c3436cfSJed Brown 5000ce94432eSBarry Smith ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 50011c3436cfSJed Brown *norm = PetscSqrtReal(gsum / N); 5002620fc8aaSSatish Balay if (PetscIsInfOrNanReal(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 50031c3436cfSJed Brown PetscFunctionReturn(0); 50041c3436cfSJed Brown } 50051c3436cfSJed Brown 50061c3436cfSJed Brown #undef __FUNCT__ 50078d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 50088d59e960SJed Brown /*@ 50098d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 50108d59e960SJed Brown 50118d59e960SJed Brown Logically Collective on TS 50128d59e960SJed Brown 50138d59e960SJed Brown Input Arguments: 50148d59e960SJed Brown + ts - time stepping context 50158d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 50168d59e960SJed Brown 50178d59e960SJed Brown Note: 50188d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 50198d59e960SJed Brown 50208d59e960SJed Brown Level: intermediate 50218d59e960SJed Brown 50228d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 50238d59e960SJed Brown @*/ 50248d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 50258d59e960SJed Brown { 50268d59e960SJed Brown PetscFunctionBegin; 50278d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 50288d59e960SJed Brown ts->cfltime_local = cfltime; 50298d59e960SJed Brown ts->cfltime = -1.; 50308d59e960SJed Brown PetscFunctionReturn(0); 50318d59e960SJed Brown } 50328d59e960SJed Brown 50338d59e960SJed Brown #undef __FUNCT__ 50348d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 50358d59e960SJed Brown /*@ 50368d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 50378d59e960SJed Brown 50388d59e960SJed Brown Collective on TS 50398d59e960SJed Brown 50408d59e960SJed Brown Input Arguments: 50418d59e960SJed Brown . ts - time stepping context 50428d59e960SJed Brown 50438d59e960SJed Brown Output Arguments: 50448d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 50458d59e960SJed Brown 50468d59e960SJed Brown Level: advanced 50478d59e960SJed Brown 50488d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 50498d59e960SJed Brown @*/ 50508d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 50518d59e960SJed Brown { 50528d59e960SJed Brown PetscErrorCode ierr; 50538d59e960SJed Brown 50548d59e960SJed Brown PetscFunctionBegin; 50558d59e960SJed Brown if (ts->cfltime < 0) { 5056ce94432eSBarry Smith ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 50578d59e960SJed Brown } 50588d59e960SJed Brown *cfltime = ts->cfltime; 50598d59e960SJed Brown PetscFunctionReturn(0); 50608d59e960SJed Brown } 50618d59e960SJed Brown 50628d59e960SJed Brown #undef __FUNCT__ 5063d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 5064d6ebe24aSShri Abhyankar /*@ 5065d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5066d6ebe24aSShri Abhyankar 5067d6ebe24aSShri Abhyankar Input Parameters: 5068d6ebe24aSShri Abhyankar . ts - the TS context. 5069d6ebe24aSShri Abhyankar . xl - lower bound. 5070d6ebe24aSShri Abhyankar . xu - upper bound. 5071d6ebe24aSShri Abhyankar 5072d6ebe24aSShri Abhyankar Notes: 5073d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 5074e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 5075d6ebe24aSShri Abhyankar 50762bd2b0e6SSatish Balay Level: advanced 50772bd2b0e6SSatish Balay 5078d6ebe24aSShri Abhyankar @*/ 5079d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5080d6ebe24aSShri Abhyankar { 5081d6ebe24aSShri Abhyankar PetscErrorCode ierr; 5082d6ebe24aSShri Abhyankar SNES snes; 5083d6ebe24aSShri Abhyankar 5084d6ebe24aSShri Abhyankar PetscFunctionBegin; 5085d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 5086d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 5087d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 5088d6ebe24aSShri Abhyankar } 5089d6ebe24aSShri Abhyankar 5090325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 5091c6db04a5SJed Brown #include <mex.h> 5092325fc9f4SBarry Smith 5093325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 5094325fc9f4SBarry Smith 5095325fc9f4SBarry Smith #undef __FUNCT__ 5096325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 5097325fc9f4SBarry Smith /* 5098325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 5099325fc9f4SBarry Smith TSSetFunctionMatlab(). 5100325fc9f4SBarry Smith 5101325fc9f4SBarry Smith Collective on TS 5102325fc9f4SBarry Smith 5103325fc9f4SBarry Smith Input Parameters: 5104325fc9f4SBarry Smith + snes - the TS context 51050910c330SBarry Smith - u - input vector 5106325fc9f4SBarry Smith 5107325fc9f4SBarry Smith Output Parameter: 5108325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 5109325fc9f4SBarry Smith 5110325fc9f4SBarry Smith Notes: 5111325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 5112325fc9f4SBarry Smith implementations, so most users would not generally call this routine 5113325fc9f4SBarry Smith themselves. 5114325fc9f4SBarry Smith 5115325fc9f4SBarry Smith Level: developer 5116325fc9f4SBarry Smith 5117325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5118325fc9f4SBarry Smith 5119325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5120325fc9f4SBarry Smith */ 51210910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 5122325fc9f4SBarry Smith { 5123325fc9f4SBarry Smith PetscErrorCode ierr; 5124325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5125325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 5126325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 5127325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 5128325fc9f4SBarry Smith 5129325fc9f4SBarry Smith PetscFunctionBegin; 5130325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 51310910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 51320910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 5133325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 51340910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 5135325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 5136325fc9f4SBarry Smith 5137325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 51380910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 51390910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 51400910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 5141bbd56ea5SKarl Rupp 5142325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5143325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 5144325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5145325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5146325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 5147325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 5148325fc9f4SBarry Smith prhs[6] = sctx->ctx; 5149325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 5150325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5151325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5152325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5153325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5154325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5155325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5156325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5157325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5158325fc9f4SBarry Smith PetscFunctionReturn(0); 5159325fc9f4SBarry Smith } 5160325fc9f4SBarry Smith 5161325fc9f4SBarry Smith 5162325fc9f4SBarry Smith #undef __FUNCT__ 5163325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 5164325fc9f4SBarry Smith /* 5165325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 5166325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 5167e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 5168325fc9f4SBarry Smith 5169325fc9f4SBarry Smith Logically Collective on TS 5170325fc9f4SBarry Smith 5171325fc9f4SBarry Smith Input Parameters: 5172325fc9f4SBarry Smith + ts - the TS context 5173325fc9f4SBarry Smith - func - function evaluation routine 5174325fc9f4SBarry Smith 5175325fc9f4SBarry Smith Calling sequence of func: 51760910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 5177325fc9f4SBarry Smith 5178325fc9f4SBarry Smith Level: beginner 5179325fc9f4SBarry Smith 5180325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5181325fc9f4SBarry Smith 5182325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5183325fc9f4SBarry Smith */ 5184cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 5185325fc9f4SBarry Smith { 5186325fc9f4SBarry Smith PetscErrorCode ierr; 5187325fc9f4SBarry Smith TSMatlabContext *sctx; 5188325fc9f4SBarry Smith 5189325fc9f4SBarry Smith PetscFunctionBegin; 5190325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5191325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5192325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5193325fc9f4SBarry Smith /* 5194325fc9f4SBarry Smith This should work, but it doesn't 5195325fc9f4SBarry Smith sctx->ctx = ctx; 5196325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5197325fc9f4SBarry Smith */ 5198325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5199bbd56ea5SKarl Rupp 52000298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 5201325fc9f4SBarry Smith PetscFunctionReturn(0); 5202325fc9f4SBarry Smith } 5203325fc9f4SBarry Smith 5204325fc9f4SBarry Smith #undef __FUNCT__ 5205325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 5206325fc9f4SBarry Smith /* 5207325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 5208325fc9f4SBarry Smith TSSetJacobianMatlab(). 5209325fc9f4SBarry Smith 5210325fc9f4SBarry Smith Collective on TS 5211325fc9f4SBarry Smith 5212325fc9f4SBarry Smith Input Parameters: 5213cdcf91faSSean Farley + ts - the TS context 52140910c330SBarry Smith . u - input vector 5215325fc9f4SBarry Smith . A, B - the matrices 5216325fc9f4SBarry Smith - ctx - user context 5217325fc9f4SBarry Smith 5218325fc9f4SBarry Smith Level: developer 5219325fc9f4SBarry Smith 5220325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5221325fc9f4SBarry Smith 5222325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5223325fc9f4SBarry Smith @*/ 5224f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 5225325fc9f4SBarry Smith { 5226325fc9f4SBarry Smith PetscErrorCode ierr; 5227325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5228325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 5229325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 5230325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 5231325fc9f4SBarry Smith 5232325fc9f4SBarry Smith PetscFunctionBegin; 5233cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 52340910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 5235325fc9f4SBarry Smith 52360910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 5237325fc9f4SBarry Smith 5238cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 52390910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 52400910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 52410910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 52420910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 5243bbd56ea5SKarl Rupp 5244325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5245325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 5246325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5247325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5248325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 5249325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 5250325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 5251325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 5252325fc9f4SBarry Smith prhs[8] = sctx->ctx; 5253325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 5254325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5255325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5256325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5257325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5258325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5259325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5260325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5261325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 5262325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 5263325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5264325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 5265325fc9f4SBarry Smith PetscFunctionReturn(0); 5266325fc9f4SBarry Smith } 5267325fc9f4SBarry Smith 5268325fc9f4SBarry Smith 5269325fc9f4SBarry Smith #undef __FUNCT__ 5270325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 5271325fc9f4SBarry Smith /* 5272325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 5273e3c5b3baSBarry 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 5274325fc9f4SBarry Smith 5275325fc9f4SBarry Smith Logically Collective on TS 5276325fc9f4SBarry Smith 5277325fc9f4SBarry Smith Input Parameters: 5278cdcf91faSSean Farley + ts - the TS context 5279325fc9f4SBarry Smith . A,B - Jacobian matrices 5280325fc9f4SBarry Smith . func - function evaluation routine 5281325fc9f4SBarry Smith - ctx - user context 5282325fc9f4SBarry Smith 5283325fc9f4SBarry Smith Calling sequence of func: 52840910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 5285325fc9f4SBarry Smith 5286325fc9f4SBarry Smith 5287325fc9f4SBarry Smith Level: developer 5288325fc9f4SBarry Smith 5289325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5290325fc9f4SBarry Smith 5291325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5292325fc9f4SBarry Smith */ 5293cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 5294325fc9f4SBarry Smith { 5295325fc9f4SBarry Smith PetscErrorCode ierr; 5296325fc9f4SBarry Smith TSMatlabContext *sctx; 5297325fc9f4SBarry Smith 5298325fc9f4SBarry Smith PetscFunctionBegin; 5299325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5300325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5301325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5302325fc9f4SBarry Smith /* 5303325fc9f4SBarry Smith This should work, but it doesn't 5304325fc9f4SBarry Smith sctx->ctx = ctx; 5305325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5306325fc9f4SBarry Smith */ 5307325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5308bbd56ea5SKarl Rupp 5309cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 5310325fc9f4SBarry Smith PetscFunctionReturn(0); 5311325fc9f4SBarry Smith } 5312325fc9f4SBarry Smith 5313b5b1a830SBarry Smith #undef __FUNCT__ 5314b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 5315b5b1a830SBarry Smith /* 5316b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 5317b5b1a830SBarry Smith 5318b5b1a830SBarry Smith Collective on TS 5319b5b1a830SBarry Smith 5320b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5321b5b1a830SBarry Smith @*/ 53220910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 5323b5b1a830SBarry Smith { 5324b5b1a830SBarry Smith PetscErrorCode ierr; 5325b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5326a530c242SBarry Smith int nlhs = 1,nrhs = 6; 5327b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 5328b5b1a830SBarry Smith long long int lx = 0,ls = 0; 5329b5b1a830SBarry Smith 5330b5b1a830SBarry Smith PetscFunctionBegin; 5331cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53320910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 5333b5b1a830SBarry Smith 5334cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 53350910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 5336bbd56ea5SKarl Rupp 5337b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5338b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 5339b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 5340b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 5341b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 5342b5b1a830SBarry Smith prhs[5] = sctx->ctx; 5343b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 5344b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5345b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 5346b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 5347b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 5348b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 5349b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 5350b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 5351b5b1a830SBarry Smith PetscFunctionReturn(0); 5352b5b1a830SBarry Smith } 5353b5b1a830SBarry Smith 5354b5b1a830SBarry Smith 5355b5b1a830SBarry Smith #undef __FUNCT__ 5356b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 5357b5b1a830SBarry Smith /* 5358b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 5359b5b1a830SBarry Smith 5360b5b1a830SBarry Smith Level: developer 5361b5b1a830SBarry Smith 5362b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 5363b5b1a830SBarry Smith 5364b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5365b5b1a830SBarry Smith */ 5366cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 5367b5b1a830SBarry Smith { 5368b5b1a830SBarry Smith PetscErrorCode ierr; 5369b5b1a830SBarry Smith TSMatlabContext *sctx; 5370b5b1a830SBarry Smith 5371b5b1a830SBarry Smith PetscFunctionBegin; 5372b5b1a830SBarry Smith /* currently sctx is memory bleed */ 5373b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5374b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5375b5b1a830SBarry Smith /* 5376b5b1a830SBarry Smith This should work, but it doesn't 5377b5b1a830SBarry Smith sctx->ctx = ctx; 5378b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5379b5b1a830SBarry Smith */ 5380b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5381bbd56ea5SKarl Rupp 53820298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 5383b5b1a830SBarry Smith PetscFunctionReturn(0); 5384b5b1a830SBarry Smith } 5385325fc9f4SBarry Smith #endif 5386b3603a34SBarry Smith 5387b3603a34SBarry Smith #undef __FUNCT__ 53884f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 5389b3603a34SBarry Smith /*@C 53904f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 5391b3603a34SBarry Smith in a time based line graph 5392b3603a34SBarry Smith 5393b3603a34SBarry Smith Collective on TS 5394b3603a34SBarry Smith 5395b3603a34SBarry Smith Input Parameters: 5396b3603a34SBarry Smith + ts - the TS context 5397b3603a34SBarry Smith . step - current time-step 5398b3603a34SBarry Smith . ptime - current time 5399b3603a34SBarry Smith - lg - a line graph object 5400b3603a34SBarry Smith 5401b3d3934dSBarry Smith Options Database: 54029ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 5403b3d3934dSBarry Smith 5404b3603a34SBarry Smith Level: intermediate 5405b3603a34SBarry Smith 54060b039ecaSBarry Smith Notes: each process in a parallel run displays its component solutions in a separate window 5407b3603a34SBarry Smith 5408b3603a34SBarry Smith .keywords: TS, vector, monitor, view 5409b3603a34SBarry Smith 5410b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5411b3603a34SBarry Smith @*/ 54120910c330SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 5413b3603a34SBarry Smith { 5414b3603a34SBarry Smith PetscErrorCode ierr; 54150b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 5416b3603a34SBarry Smith const PetscScalar *yy; 541758ff32f7SBarry Smith PetscInt dim; 541880666b62SBarry Smith Vec v; 5419b3603a34SBarry Smith 5420b3603a34SBarry Smith PetscFunctionBegin; 542158ff32f7SBarry Smith if (!step) { 5422a9f9c1f6SBarry Smith PetscDrawAxis axis; 5423a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5424a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 5425387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 5426387f4636SBarry Smith char **displaynames; 5427387f4636SBarry Smith PetscBool flg; 5428387f4636SBarry Smith 5429387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 5430387f4636SBarry Smith ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr); 5431387f4636SBarry Smith ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 54329ae14b6eSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 5433387f4636SBarry Smith if (flg) { 5434a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 5435387f4636SBarry Smith } 5436387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 5437387f4636SBarry Smith } 5438387f4636SBarry Smith if (ctx->displaynames) { 5439387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 5440387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 5441387f4636SBarry Smith } else if (ctx->names) { 54420910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 54430b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 5444387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 5445387f4636SBarry Smith } 54460b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 544758ff32f7SBarry Smith } 544880666b62SBarry Smith if (ctx->transform) { 5449e673d494SBarry Smith ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr); 545080666b62SBarry Smith } else { 545180666b62SBarry Smith v = u; 545280666b62SBarry Smith } 545380666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 5454e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 5455e3efe391SJed Brown { 5456e3efe391SJed Brown PetscReal *yreal; 5457e3efe391SJed Brown PetscInt i,n; 545880666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 5459785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 5460e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 54610b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 5462e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 5463e3efe391SJed Brown } 5464e3efe391SJed Brown #else 5465387f4636SBarry Smith if (ctx->displaynames) { 5466387f4636SBarry Smith PetscInt i; 5467387f4636SBarry Smith for (i=0; i<ctx->ndisplayvariables; i++) { 5468387f4636SBarry Smith ctx->displayvalues[i] = yy[ctx->displayvariables[i]]; 5469387f4636SBarry Smith } 5470387f4636SBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 5471387f4636SBarry Smith } else { 54720b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 5473387f4636SBarry Smith } 5474e3efe391SJed Brown #endif 547580666b62SBarry Smith ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 547680666b62SBarry Smith if (ctx->transform) { 547780666b62SBarry Smith ierr = VecDestroy(&v);CHKERRQ(ierr); 547880666b62SBarry Smith } 5479b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 54800b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 54813923b477SBarry Smith } 5482b3603a34SBarry Smith PetscFunctionReturn(0); 5483b3603a34SBarry Smith } 5484b3603a34SBarry Smith 5485387f4636SBarry Smith 5486b3603a34SBarry Smith #undef __FUNCT__ 548731152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames" 5488b037adc7SBarry Smith /*@C 548931152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 5490b037adc7SBarry Smith 5491b037adc7SBarry Smith Collective on TS 5492b037adc7SBarry Smith 5493b037adc7SBarry Smith Input Parameters: 5494b037adc7SBarry Smith + ts - the TS context 5495b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 5496b037adc7SBarry Smith 5497b037adc7SBarry Smith Level: intermediate 5498b037adc7SBarry Smith 5499b037adc7SBarry Smith .keywords: TS, vector, monitor, view 5500b037adc7SBarry Smith 5501a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 5502b037adc7SBarry Smith @*/ 550331152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 5504b037adc7SBarry Smith { 5505b037adc7SBarry Smith PetscErrorCode ierr; 5506b037adc7SBarry Smith PetscInt i; 5507b037adc7SBarry Smith 5508b037adc7SBarry Smith PetscFunctionBegin; 5509b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 5510b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 5511e414155dSBarry Smith ierr = TSMonitorLGCtxSetVariableNames(ts->monitorcontext[i],names);CHKERRQ(ierr); 5512b3d3934dSBarry Smith break; 5513b3d3934dSBarry Smith } 5514b3d3934dSBarry Smith } 5515b3d3934dSBarry Smith PetscFunctionReturn(0); 5516b3d3934dSBarry Smith } 5517b3d3934dSBarry Smith 5518b3d3934dSBarry Smith #undef __FUNCT__ 5519e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames" 5520e673d494SBarry Smith /*@C 5521e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 5522e673d494SBarry Smith 5523e673d494SBarry Smith Collective on TS 5524e673d494SBarry Smith 5525e673d494SBarry Smith Input Parameters: 5526e673d494SBarry Smith + ts - the TS context 5527e673d494SBarry Smith - names - the names of the components, final string must be NULL 5528e673d494SBarry Smith 5529e673d494SBarry Smith Level: intermediate 5530e673d494SBarry Smith 5531e673d494SBarry Smith .keywords: TS, vector, monitor, view 5532e673d494SBarry Smith 5533a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 5534e673d494SBarry Smith @*/ 5535e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 5536e673d494SBarry Smith { 5537e673d494SBarry Smith PetscErrorCode ierr; 5538e673d494SBarry Smith 5539e673d494SBarry Smith PetscFunctionBegin; 5540e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 5541e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 5542e673d494SBarry Smith PetscFunctionReturn(0); 5543e673d494SBarry Smith } 5544e673d494SBarry Smith 5545e673d494SBarry Smith #undef __FUNCT__ 5546b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames" 5547b3d3934dSBarry Smith /*@C 5548b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 5549b3d3934dSBarry Smith 5550b3d3934dSBarry Smith Collective on TS 5551b3d3934dSBarry Smith 5552b3d3934dSBarry Smith Input Parameter: 5553b3d3934dSBarry Smith . ts - the TS context 5554b3d3934dSBarry Smith 5555b3d3934dSBarry Smith Output Parameter: 5556b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 5557b3d3934dSBarry Smith 5558b3d3934dSBarry Smith Level: intermediate 5559b3d3934dSBarry Smith 5560b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 5561b3d3934dSBarry Smith 5562b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 5563b3d3934dSBarry Smith @*/ 5564b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 5565b3d3934dSBarry Smith { 5566b3d3934dSBarry Smith PetscInt i; 5567b3d3934dSBarry Smith 5568b3d3934dSBarry Smith PetscFunctionBegin; 5569b3d3934dSBarry Smith *names = NULL; 5570b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 5571b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 5572b3d3934dSBarry Smith TSMonitorLGCtx ctx = ts->monitorcontext[i]; 5573b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 5574b3d3934dSBarry Smith break; 5575387f4636SBarry Smith } 5576387f4636SBarry Smith } 5577387f4636SBarry Smith PetscFunctionReturn(0); 5578387f4636SBarry Smith } 5579387f4636SBarry Smith 5580387f4636SBarry Smith #undef __FUNCT__ 5581a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables" 5582a66092f1SBarry Smith /*@C 5583a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 5584a66092f1SBarry Smith 5585a66092f1SBarry Smith Collective on TS 5586a66092f1SBarry Smith 5587a66092f1SBarry Smith Input Parameters: 5588a66092f1SBarry Smith + ctx - the TSMonitorLG context 5589a66092f1SBarry Smith . displaynames - the names of the components, final string must be NULL 5590a66092f1SBarry Smith 5591a66092f1SBarry Smith Level: intermediate 5592a66092f1SBarry Smith 5593a66092f1SBarry Smith .keywords: TS, vector, monitor, view 5594a66092f1SBarry Smith 5595a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 5596a66092f1SBarry Smith @*/ 5597a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 5598a66092f1SBarry Smith { 5599a66092f1SBarry Smith PetscInt j = 0,k; 5600a66092f1SBarry Smith PetscErrorCode ierr; 5601a66092f1SBarry Smith 5602a66092f1SBarry Smith PetscFunctionBegin; 5603a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 5604a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 5605a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 5606a66092f1SBarry Smith while (displaynames[j]) j++; 5607a66092f1SBarry Smith ctx->ndisplayvariables = j; 5608a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 5609a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 5610a66092f1SBarry Smith j = 0; 5611a66092f1SBarry Smith while (displaynames[j]) { 5612a66092f1SBarry Smith k = 0; 5613a66092f1SBarry Smith while (ctx->names[k]) { 5614a66092f1SBarry Smith PetscBool flg; 5615a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 5616a66092f1SBarry Smith if (flg) { 5617a66092f1SBarry Smith ctx->displayvariables[j] = k; 5618a66092f1SBarry Smith break; 5619a66092f1SBarry Smith } 5620a66092f1SBarry Smith k++; 5621a66092f1SBarry Smith } 5622a66092f1SBarry Smith j++; 5623a66092f1SBarry Smith } 5624a66092f1SBarry Smith PetscFunctionReturn(0); 5625a66092f1SBarry Smith } 5626a66092f1SBarry Smith 5627a66092f1SBarry Smith 5628a66092f1SBarry Smith #undef __FUNCT__ 5629387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables" 5630387f4636SBarry Smith /*@C 5631387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 5632387f4636SBarry Smith 5633387f4636SBarry Smith Collective on TS 5634387f4636SBarry Smith 5635387f4636SBarry Smith Input Parameters: 5636387f4636SBarry Smith + ts - the TS context 5637387f4636SBarry Smith . displaynames - the names of the components, final string must be NULL 5638387f4636SBarry Smith 5639387f4636SBarry Smith Level: intermediate 5640387f4636SBarry Smith 5641387f4636SBarry Smith .keywords: TS, vector, monitor, view 5642387f4636SBarry Smith 5643387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 5644387f4636SBarry Smith @*/ 5645387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 5646387f4636SBarry Smith { 5647a66092f1SBarry Smith PetscInt i; 5648387f4636SBarry Smith PetscErrorCode ierr; 5649387f4636SBarry Smith 5650387f4636SBarry Smith PetscFunctionBegin; 5651387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 5652387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 5653a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 5654b3d3934dSBarry Smith break; 5655b037adc7SBarry Smith } 5656b037adc7SBarry Smith } 5657b037adc7SBarry Smith PetscFunctionReturn(0); 5658b037adc7SBarry Smith } 5659b037adc7SBarry Smith 5660b037adc7SBarry Smith #undef __FUNCT__ 566180666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform" 566280666b62SBarry Smith /*@C 566380666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 566480666b62SBarry Smith 566580666b62SBarry Smith Collective on TS 566680666b62SBarry Smith 566780666b62SBarry Smith Input Parameters: 566880666b62SBarry Smith + ts - the TS context 566980666b62SBarry Smith . transform - the transform function 56707684fa3eSBarry Smith . destroy - function to destroy the optional context 567180666b62SBarry Smith - ctx - optional context used by transform function 567280666b62SBarry Smith 567380666b62SBarry Smith Level: intermediate 567480666b62SBarry Smith 567580666b62SBarry Smith .keywords: TS, vector, monitor, view 567680666b62SBarry Smith 5677a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 567880666b62SBarry Smith @*/ 56797684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 568080666b62SBarry Smith { 568180666b62SBarry Smith PetscInt i; 5682a66092f1SBarry Smith PetscErrorCode ierr; 568380666b62SBarry Smith 568480666b62SBarry Smith PetscFunctionBegin; 568580666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 568680666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 5687a66092f1SBarry Smith ierr = TSMonitorLGCtxSetTransform(ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 568880666b62SBarry Smith } 568980666b62SBarry Smith } 569080666b62SBarry Smith PetscFunctionReturn(0); 569180666b62SBarry Smith } 569280666b62SBarry Smith 569380666b62SBarry Smith #undef __FUNCT__ 5694e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform" 5695e673d494SBarry Smith /*@C 5696e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 5697e673d494SBarry Smith 5698e673d494SBarry Smith Collective on TSLGCtx 5699e673d494SBarry Smith 5700e673d494SBarry Smith Input Parameters: 5701e673d494SBarry Smith + ts - the TS context 5702e673d494SBarry Smith . transform - the transform function 57037684fa3eSBarry Smith . destroy - function to destroy the optional context 5704e673d494SBarry Smith - ctx - optional context used by transform function 5705e673d494SBarry Smith 5706e673d494SBarry Smith Level: intermediate 5707e673d494SBarry Smith 5708e673d494SBarry Smith .keywords: TS, vector, monitor, view 5709e673d494SBarry Smith 5710a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 5711e673d494SBarry Smith @*/ 57127684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 5713e673d494SBarry Smith { 5714e673d494SBarry Smith PetscFunctionBegin; 5715e673d494SBarry Smith ctx->transform = transform; 57167684fa3eSBarry Smith ctx->transformdestroy = destroy; 5717e673d494SBarry Smith ctx->transformctx = tctx; 5718e673d494SBarry Smith PetscFunctionReturn(0); 5719e673d494SBarry Smith } 5720e673d494SBarry Smith 5721e673d494SBarry Smith #undef __FUNCT__ 57224f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 5723ef20d060SBarry Smith /*@C 57244f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 5725ef20d060SBarry Smith in a time based line graph 5726ef20d060SBarry Smith 5727ef20d060SBarry Smith Collective on TS 5728ef20d060SBarry Smith 5729ef20d060SBarry Smith Input Parameters: 5730ef20d060SBarry Smith + ts - the TS context 5731ef20d060SBarry Smith . step - current time-step 5732ef20d060SBarry Smith . ptime - current time 5733ef20d060SBarry Smith - lg - a line graph object 5734ef20d060SBarry Smith 5735ef20d060SBarry Smith Level: intermediate 5736ef20d060SBarry Smith 5737abd5a294SJed Brown Notes: 5738abd5a294SJed Brown Only for sequential solves. 5739abd5a294SJed Brown 5740abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 5741abd5a294SJed Brown 5742abd5a294SJed Brown Options Database Keys: 57434f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 5744ef20d060SBarry Smith 5745ef20d060SBarry Smith .keywords: TS, vector, monitor, view 5746ef20d060SBarry Smith 5747abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 5748ef20d060SBarry Smith @*/ 57490910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 5750ef20d060SBarry Smith { 5751ef20d060SBarry Smith PetscErrorCode ierr; 57520b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 5753ef20d060SBarry Smith const PetscScalar *yy; 5754ef20d060SBarry Smith Vec y; 5755a9f9c1f6SBarry Smith PetscInt dim; 5756ef20d060SBarry Smith 5757ef20d060SBarry Smith PetscFunctionBegin; 5758a9f9c1f6SBarry Smith if (!step) { 5759a9f9c1f6SBarry Smith PetscDrawAxis axis; 5760a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 57611ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 57620910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 5763a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 5764a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 5765a9f9c1f6SBarry Smith } 57660910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 5767ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 57680910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 5769ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 5770e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 5771e3efe391SJed Brown { 5772e3efe391SJed Brown PetscReal *yreal; 5773e3efe391SJed Brown PetscInt i,n; 5774e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 5775785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 5776e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 57770b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 5778e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 5779e3efe391SJed Brown } 5780e3efe391SJed Brown #else 57810b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 5782e3efe391SJed Brown #endif 5783ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 5784ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 5785b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 57860b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 57873923b477SBarry Smith } 5788ef20d060SBarry Smith PetscFunctionReturn(0); 5789ef20d060SBarry Smith } 5790ef20d060SBarry Smith 5791201da799SBarry Smith #undef __FUNCT__ 5792201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 5793201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 5794201da799SBarry Smith { 5795201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 5796201da799SBarry Smith PetscReal x = ptime,y; 5797201da799SBarry Smith PetscErrorCode ierr; 5798201da799SBarry Smith PetscInt its; 5799201da799SBarry Smith 5800201da799SBarry Smith PetscFunctionBegin; 5801201da799SBarry Smith if (!n) { 5802201da799SBarry Smith PetscDrawAxis axis; 5803bbd56ea5SKarl Rupp 5804201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5805201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 5806201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 5807bbd56ea5SKarl Rupp 5808201da799SBarry Smith ctx->snes_its = 0; 5809201da799SBarry Smith } 5810201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 5811201da799SBarry Smith y = its - ctx->snes_its; 5812201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 58133fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 5814201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 5815201da799SBarry Smith } 5816201da799SBarry Smith ctx->snes_its = its; 5817201da799SBarry Smith PetscFunctionReturn(0); 5818201da799SBarry Smith } 5819201da799SBarry Smith 5820201da799SBarry Smith #undef __FUNCT__ 5821201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 5822201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 5823201da799SBarry Smith { 5824201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 5825201da799SBarry Smith PetscReal x = ptime,y; 5826201da799SBarry Smith PetscErrorCode ierr; 5827201da799SBarry Smith PetscInt its; 5828201da799SBarry Smith 5829201da799SBarry Smith PetscFunctionBegin; 5830201da799SBarry Smith if (!n) { 5831201da799SBarry Smith PetscDrawAxis axis; 5832bbd56ea5SKarl Rupp 5833201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5834201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 5835201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 5836bbd56ea5SKarl Rupp 5837201da799SBarry Smith ctx->ksp_its = 0; 5838201da799SBarry Smith } 5839201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 5840201da799SBarry Smith y = its - ctx->ksp_its; 5841201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 584299fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 5843201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 5844201da799SBarry Smith } 5845201da799SBarry Smith ctx->ksp_its = its; 5846201da799SBarry Smith PetscFunctionReturn(0); 5847201da799SBarry Smith } 5848f9c1d6abSBarry Smith 5849f9c1d6abSBarry Smith #undef __FUNCT__ 5850f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 5851f9c1d6abSBarry Smith /*@ 5852f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 5853f9c1d6abSBarry Smith 5854f9c1d6abSBarry Smith Collective on TS and Vec 5855f9c1d6abSBarry Smith 5856f9c1d6abSBarry Smith Input Parameters: 5857f9c1d6abSBarry Smith + ts - the TS context 5858f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 5859f9c1d6abSBarry Smith 5860f9c1d6abSBarry Smith Output Parameters: 5861f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 5862f9c1d6abSBarry Smith 5863f9c1d6abSBarry Smith Level: developer 5864f9c1d6abSBarry Smith 5865f9c1d6abSBarry Smith .keywords: TS, compute 5866f9c1d6abSBarry Smith 5867f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 5868f9c1d6abSBarry Smith @*/ 5869f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 5870f9c1d6abSBarry Smith { 5871f9c1d6abSBarry Smith PetscErrorCode ierr; 5872f9c1d6abSBarry Smith 5873f9c1d6abSBarry Smith PetscFunctionBegin; 5874f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5875ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 5876f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 5877f9c1d6abSBarry Smith PetscFunctionReturn(0); 5878f9c1d6abSBarry Smith } 5879b3d3934dSBarry Smith 5880b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 5881b3d3934dSBarry Smith #undef __FUNCT__ 5882b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate" 5883b3d3934dSBarry Smith /*@C 5884b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 5885b3d3934dSBarry Smith 5886b3d3934dSBarry Smith Collective on TS 5887b3d3934dSBarry Smith 5888b3d3934dSBarry Smith Input Parameters: 5889b3d3934dSBarry Smith . ts - the ODE solver object 5890b3d3934dSBarry Smith 5891b3d3934dSBarry Smith Output Parameter: 5892b3d3934dSBarry Smith . ctx - the context 5893b3d3934dSBarry Smith 5894b3d3934dSBarry Smith Level: intermediate 5895b3d3934dSBarry Smith 5896b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso 5897b3d3934dSBarry Smith 5898b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 5899b3d3934dSBarry Smith 5900b3d3934dSBarry Smith @*/ 5901b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 5902b3d3934dSBarry Smith { 5903b3d3934dSBarry Smith PetscErrorCode ierr; 5904b3d3934dSBarry Smith 5905b3d3934dSBarry Smith PetscFunctionBegin; 5906a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 5907b3d3934dSBarry Smith PetscFunctionReturn(0); 5908b3d3934dSBarry Smith } 5909b3d3934dSBarry Smith 5910b3d3934dSBarry Smith #undef __FUNCT__ 5911b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope" 5912b3d3934dSBarry Smith /*@C 5913b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 5914b3d3934dSBarry Smith 5915b3d3934dSBarry Smith Collective on TS 5916b3d3934dSBarry Smith 5917b3d3934dSBarry Smith Input Parameters: 5918b3d3934dSBarry Smith + ts - the TS context 5919b3d3934dSBarry Smith . step - current time-step 5920b3d3934dSBarry Smith . ptime - current time 5921b3d3934dSBarry Smith - ctx - the envelope context 5922b3d3934dSBarry Smith 5923b3d3934dSBarry Smith Options Database: 5924b3d3934dSBarry Smith . -ts_monitor_envelope 5925b3d3934dSBarry Smith 5926b3d3934dSBarry Smith Level: intermediate 5927b3d3934dSBarry Smith 5928b3d3934dSBarry Smith Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 5929b3d3934dSBarry Smith 5930b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 5931b3d3934dSBarry Smith 5932b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds() 5933b3d3934dSBarry Smith @*/ 5934b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 5935b3d3934dSBarry Smith { 5936b3d3934dSBarry Smith PetscErrorCode ierr; 5937b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dummy; 5938b3d3934dSBarry Smith 5939b3d3934dSBarry Smith PetscFunctionBegin; 5940b3d3934dSBarry Smith if (!ctx->max) { 5941b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 5942b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 5943b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 5944b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 5945b3d3934dSBarry Smith } else { 5946b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 5947b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 5948b3d3934dSBarry Smith } 5949b3d3934dSBarry Smith PetscFunctionReturn(0); 5950b3d3934dSBarry Smith } 5951b3d3934dSBarry Smith 5952b3d3934dSBarry Smith 5953b3d3934dSBarry Smith #undef __FUNCT__ 5954b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds" 5955b3d3934dSBarry Smith /*@C 5956b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 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 + max - the maximum values 5965b3d3934dSBarry Smith - min - the minimum values 5966b3d3934dSBarry Smith 5967b3d3934dSBarry Smith Level: intermediate 5968b3d3934dSBarry Smith 5969b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 5970b3d3934dSBarry Smith 5971b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 5972b3d3934dSBarry Smith @*/ 5973b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 5974b3d3934dSBarry Smith { 5975b3d3934dSBarry Smith PetscInt i; 5976b3d3934dSBarry Smith 5977b3d3934dSBarry Smith PetscFunctionBegin; 5978b3d3934dSBarry Smith if (max) *max = NULL; 5979b3d3934dSBarry Smith if (min) *min = NULL; 5980b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 5981b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 5982b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx = ts->monitorcontext[i]; 5983b3d3934dSBarry Smith if (max) *max = ctx->max; 5984b3d3934dSBarry Smith if (min) *min = ctx->min; 5985b3d3934dSBarry Smith break; 5986b3d3934dSBarry Smith } 5987b3d3934dSBarry Smith } 5988b3d3934dSBarry Smith PetscFunctionReturn(0); 5989b3d3934dSBarry Smith } 5990b3d3934dSBarry Smith 5991b3d3934dSBarry Smith #undef __FUNCT__ 5992b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy" 5993b3d3934dSBarry Smith /*@C 5994b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 5995b3d3934dSBarry Smith 5996b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 5997b3d3934dSBarry Smith 5998b3d3934dSBarry Smith Input Parameter: 5999b3d3934dSBarry Smith . ctx - the monitor context 6000b3d3934dSBarry Smith 6001b3d3934dSBarry Smith Level: intermediate 6002b3d3934dSBarry Smith 6003b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy 6004b3d3934dSBarry Smith 6005b3d3934dSBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 6006b3d3934dSBarry Smith @*/ 6007b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 6008b3d3934dSBarry Smith { 6009b3d3934dSBarry Smith PetscErrorCode ierr; 6010b3d3934dSBarry Smith 6011b3d3934dSBarry Smith PetscFunctionBegin; 6012b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 6013b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 6014b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 6015b3d3934dSBarry Smith PetscFunctionReturn(0); 6016b3d3934dSBarry Smith } 6017f2dee214SBarry Smith 601824655328SShri #undef __FUNCT__ 601924655328SShri #define __FUNCT__ "TSRollBack" 602024655328SShri /*@ 602124655328SShri TSRollBack - Rolls back one time step 602224655328SShri 602324655328SShri Collective on TS 602424655328SShri 602524655328SShri Input Parameter: 602624655328SShri . ts - the TS context obtained from TSCreate() 602724655328SShri 602824655328SShri Level: advanced 602924655328SShri 603024655328SShri .keywords: TS, timestep, rollback 603124655328SShri 603224655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 603324655328SShri @*/ 603424655328SShri PetscErrorCode TSRollBack(TS ts) 603524655328SShri { 603624655328SShri PetscErrorCode ierr; 603724655328SShri 603824655328SShri PetscFunctionBegin; 603924655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 604024655328SShri 604124655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 604224655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 604324655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 604424655328SShri ts->ptime = ts->ptime_prev; 604524655328SShri PetscFunctionReturn(0); 604624655328SShri } 6047aeb4809dSShri Abhyankar 6048ff22ae23SHong Zhang #undef __FUNCT__ 6049ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages" 6050ff22ae23SHong Zhang /*@ 6051ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 6052ff22ae23SHong Zhang 6053ff22ae23SHong Zhang Input Parameter: 6054ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 6055ff22ae23SHong Zhang 6056ff22ae23SHong Zhang Level: advanced 6057ff22ae23SHong Zhang 6058ff22ae23SHong Zhang .keywords: TS, getstages 6059ff22ae23SHong Zhang 6060ff22ae23SHong Zhang .seealso: TSCreate() 6061ff22ae23SHong Zhang @*/ 6062ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns, Vec **Y) 6063ff22ae23SHong Zhang { 6064ff22ae23SHong Zhang PetscErrorCode ierr; 6065ff22ae23SHong Zhang 6066ff22ae23SHong Zhang PetscFunctionBegin; 6067ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 6068ff22ae23SHong Zhang PetscValidPointer(ns,2); 6069ff22ae23SHong Zhang 6070ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 6071ff22ae23SHong Zhang else { 6072ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 6073ff22ae23SHong Zhang } 6074ff22ae23SHong Zhang PetscFunctionReturn(0); 6075ff22ae23SHong Zhang } 6076ff22ae23SHong Zhang 6077