163dd3a1aSKris Buschelman 2af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 3496e6a7aSJed Brown #include <petscdmshell.h> 41e25c274SJed Brown #include <petscdmda.h> 52d5ee99bSBarry Smith #include <petscviewer.h> 62d5ee99bSBarry Smith #include <petscdraw.h> 7d763cef2SBarry Smith 8d5ba7fb7SMatthew Knepley /* Logging support */ 9d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 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 36ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 373e4cdcaaSBarry Smith . -ts_max_steps <maxsteps> - maximum number of time-steps to take 383e4cdcaaSBarry Smith . -ts_final_time <time> - maximum time to compute to 393e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 40a3cdaa26SBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> whether to stop at the exact given final time and how to compute the solution at that ti,e 41a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 42a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 43a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 44a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 45a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 46ef222394SBarry Smith . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 47847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 48bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 49de06c3feSJed Brown . -ts_monitor_lg_timestep - Monitor timestep size graphically 50de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 51de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 52de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 53de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 54de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 55de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 563e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 573e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 5891b97e58SBarry Smith . -ts_monitor_solution_binary <filename> - Save each solution to a binary file 59b3d3934dSBarry Smith . -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 60110eb670SHong Zhang . -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 61110eb670SHong Zhang . -ts_adjoint_monitor - print information at each adjoint time step 6253ea634cSHong Zhang - -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically 6391b97e58SBarry Smith 6491b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 65bdad233fSMatthew Knepley 66bdad233fSMatthew Knepley Level: beginner 67bdad233fSMatthew Knepley 68bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 69bdad233fSMatthew Knepley 70a313700dSBarry Smith .seealso: TSGetType() 71bdad233fSMatthew Knepley @*/ 727087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 73bdad233fSMatthew Knepley { 74bc952696SBarry Smith PetscBool opt,flg,tflg; 75dfbe8321SBarry Smith PetscErrorCode ierr; 76649052a6SBarry Smith PetscViewer monviewer; 77eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 78089b2837SJed Brown SNES snes; 791c3436cfSJed Brown TSAdapt adapt; 8031748224SBarry Smith PetscReal time_step; 8149354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 82d1212d36SBarry Smith char dir[16]; 836991f827SBarry Smith const char *defaultType; 846991f827SBarry Smith char typeName[256]; 85bdad233fSMatthew Knepley 86bdad233fSMatthew Knepley PetscFunctionBegin; 870700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 883194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 896991f827SBarry Smith if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 906991f827SBarry Smith else defaultType = TSEULER; 916991f827SBarry Smith 926991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 936991f827SBarry Smith ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 946991f827SBarry Smith if (opt) { 956991f827SBarry Smith ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 966991f827SBarry Smith } else { 976991f827SBarry Smith ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 986991f827SBarry Smith } 99bdad233fSMatthew Knepley 100bdad233fSMatthew Knepley /* Handle generic TS options */ 101844594baSBarry Smith if (ts->trajectory) tflg = PETSC_TRUE; 102844594baSBarry Smith else tflg = PETSC_FALSE; 103920aabf2SBarry Smith ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 104bc952696SBarry Smith if (tflg) {ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);} 105ef222394SBarry Smith if (ts->adjoint_solve) tflg = PETSC_TRUE; 106ef222394SBarry Smith else tflg = PETSC_FALSE; 107ef222394SBarry Smith ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr); 108ef222394SBarry Smith if (flg) { 109ef222394SBarry Smith ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 110e87fd094SBarry Smith ts->adjoint_solve = tflg; 111ef222394SBarry Smith } 1120298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1130298fd71SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 1140298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 11531748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 11631748224SBarry Smith if (flg) { 11731748224SBarry Smith ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 11831748224SBarry Smith } 11949354f04SShri 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); 12049354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 1210298fd71SBarry 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); 1220298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 1230298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 1240298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 1250298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 126bdad233fSMatthew Knepley 12756f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 12856f85f32SBarry Smith { 12956f85f32SBarry Smith PetscBool set; 13056f85f32SBarry Smith flg = PETSC_FALSE; 13156f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 13256f85f32SBarry Smith if (set) { 13356f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 13456f85f32SBarry Smith } 13556f85f32SBarry Smith } 13656f85f32SBarry Smith #endif 13756f85f32SBarry Smith 138bdad233fSMatthew Knepley /* Monitor options */ 139a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 140eabae89aSBarry Smith if (flg) { 141ce94432eSBarry Smith ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr); 142649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 143bdad233fSMatthew Knepley } 1445180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1455180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1465180491cSLisandro Dalcin 1474f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 148a7cc72afSBarry Smith if (opt) { 1490b039ecaSBarry Smith TSMonitorLGCtx ctx; 1503923b477SBarry Smith PetscInt howoften = 1; 151a80ad3e0SBarry Smith 1520298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 1537f52daa2SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 1544f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 155b3603a34SBarry Smith } 1564f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 157b3603a34SBarry Smith if (opt) { 1580b039ecaSBarry Smith TSMonitorLGCtx ctx; 1593923b477SBarry Smith PetscInt howoften = 1; 160b3603a34SBarry Smith 1610298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 16222d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1634f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 164bdad233fSMatthew Knepley } 1654f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 166ef20d060SBarry Smith if (opt) { 1670b039ecaSBarry Smith TSMonitorLGCtx ctx; 1683923b477SBarry Smith PetscInt howoften = 1; 169ef20d060SBarry Smith 1700298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 17122d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1724f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 173ef20d060SBarry Smith } 174201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 175201da799SBarry Smith if (opt) { 176201da799SBarry Smith TSMonitorLGCtx ctx; 177201da799SBarry Smith PetscInt howoften = 1; 178201da799SBarry Smith 1790298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 1807f52daa2SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 181201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 182201da799SBarry Smith } 183201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 184201da799SBarry Smith if (opt) { 185201da799SBarry Smith TSMonitorLGCtx ctx; 186201da799SBarry Smith PetscInt howoften = 1; 187201da799SBarry Smith 1880298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 1897f52daa2SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 190201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 191201da799SBarry Smith } 1928189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 1938189c53fSBarry Smith if (opt) { 1948189c53fSBarry Smith TSMonitorSPEigCtx ctx; 1958189c53fSBarry Smith PetscInt howoften = 1; 1968189c53fSBarry Smith 1970298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 19822d28d08SBarry Smith ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1998189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 2008189c53fSBarry Smith } 201ef20d060SBarry Smith opt = PETSC_FALSE; 2020dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 203a7cc72afSBarry Smith if (opt) { 20483a4ac43SBarry Smith TSMonitorDrawCtx ctx; 20583a4ac43SBarry Smith PetscInt howoften = 1; 206a80ad3e0SBarry Smith 2070298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 208ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 20983a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 210bdad233fSMatthew Knepley } 211fb1732b5SBarry Smith opt = PETSC_FALSE; 2129110b2e7SHong Zhang ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr); 2139110b2e7SHong Zhang if (opt) { 2149110b2e7SHong Zhang TSMonitorDrawCtx ctx; 2159110b2e7SHong Zhang PetscInt howoften = 1; 2169110b2e7SHong Zhang 2179110b2e7SHong Zhang ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr); 2189110b2e7SHong Zhang ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 2199110b2e7SHong Zhang ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2209110b2e7SHong Zhang } 2219110b2e7SHong Zhang opt = PETSC_FALSE; 2222d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 2232d5ee99bSBarry Smith if (opt) { 2242d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2252d5ee99bSBarry Smith PetscReal bounds[4]; 2262d5ee99bSBarry Smith PetscInt n = 4; 2272d5ee99bSBarry Smith PetscDraw draw; 2282d5ee99bSBarry Smith 2292d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 2302d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2312d5ee99bSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr); 2322d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 2332d5ee99bSBarry Smith ierr = PetscDrawClear(draw);CHKERRQ(ierr); 2344b363babSBarry Smith ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr); 2354b363babSBarry Smith ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 236f54adfc0SBarry Smith ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 2374b363babSBarry Smith ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr); 23807995780SPeter Brune /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */ 2392d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2402d5ee99bSBarry Smith } 2412d5ee99bSBarry Smith opt = PETSC_FALSE; 2420dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 2433a471f94SBarry Smith if (opt) { 24483a4ac43SBarry Smith TSMonitorDrawCtx ctx; 24583a4ac43SBarry Smith PetscInt howoften = 1; 2463a471f94SBarry Smith 2470298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 248ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 24983a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2503a471f94SBarry Smith } 2513a471f94SBarry Smith opt = PETSC_FALSE; 25291b97e58SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 253fb1732b5SBarry Smith if (flg) { 254fb1732b5SBarry Smith PetscViewer ctx; 255fb1732b5SBarry Smith if (monfilename[0]) { 256ce94432eSBarry Smith ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 257c2fbc07fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 258fb1732b5SBarry Smith } else { 259ce94432eSBarry Smith ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts)); 2600298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr); 261fb1732b5SBarry Smith } 262fb1732b5SBarry Smith } 263ed81e22dSJed Brown opt = PETSC_FALSE; 26491b97e58SBarry 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); 265ed81e22dSJed Brown if (flg) { 266ed81e22dSJed Brown const char *ptr,*ptr2; 267ed81e22dSJed Brown char *filetemplate; 268ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 269ed81e22dSJed Brown /* Do some cursory validation of the input. */ 270ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 271ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 272ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 273ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 274ce94432eSBarry 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"); 275ed81e22dSJed Brown if (ptr2) break; 276ed81e22dSJed Brown } 277ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 278ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 279ed81e22dSJed Brown } 280bdad233fSMatthew Knepley 281d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 282d1212d36SBarry Smith if (flg) { 283d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 284d1212d36SBarry Smith int ray = 0; 285d1212d36SBarry Smith DMDADirection ddir; 286d1212d36SBarry Smith DM da; 287d1212d36SBarry Smith PetscMPIInt rank; 288d1212d36SBarry Smith 289ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 290d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 291d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 292ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 293d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 294d1212d36SBarry Smith 295d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 296b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 297d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 298d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 299ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 300d1212d36SBarry Smith if (!rank) { 301d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 302d1212d36SBarry Smith } 30351b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 304d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 305d1212d36SBarry Smith } 30651b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 30751b4a12fSMatthew G. Knepley if (flg) { 30851b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 30951b4a12fSMatthew G. Knepley int ray = 0; 31051b4a12fSMatthew G. Knepley DMDADirection ddir; 31151b4a12fSMatthew G. Knepley DM da; 31251b4a12fSMatthew G. Knepley PetscInt howoften = 1; 313d1212d36SBarry Smith 31451b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 31551b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 31651b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 31751b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 31851b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 3191c3436cfSJed Brown 32051b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 321b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 32251b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 32351b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 32451b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 32551b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 32651b4a12fSMatthew G. Knepley } 327a7a1495cSBarry Smith 328b3d3934dSBarry Smith ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 329b3d3934dSBarry Smith if (opt) { 330b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 331b3d3934dSBarry Smith 332b3d3934dSBarry Smith ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 333b3d3934dSBarry Smith ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 334b3d3934dSBarry Smith } 335b3d3934dSBarry Smith 336847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 337847ff0e1SMatthew G. Knepley ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr); 338847ff0e1SMatthew G. Knepley if (flg) { 339847ff0e1SMatthew G. Knepley DM dm; 340847ff0e1SMatthew G. Knepley DMTS tdm; 341847ff0e1SMatthew G. Knepley 342847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 343847ff0e1SMatthew G. Knepley ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr); 344847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 345847ff0e1SMatthew G. Knepley ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr); 346847ff0e1SMatthew G. Knepley ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 347847ff0e1SMatthew G. Knepley } 348847ff0e1SMatthew G. Knepley 349110eb670SHong Zhang ierr = PetscOptionsString("-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 350110eb670SHong Zhang if (flg) { 351110eb670SHong Zhang ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr); 352110eb670SHong Zhang ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 353110eb670SHong Zhang } 354110eb670SHong Zhang 3557781c08eSBarry Smith /* 3567781c08eSBarry Smith This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui 3577781c08eSBarry Smith will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin() 3587781c08eSBarry Smith */ 359552698daSJed Brown ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr); 360e55864a3SBarry Smith ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr); 361d763cef2SBarry Smith 362d763cef2SBarry Smith /* Handle specific TS options */ 363d763cef2SBarry Smith if (ts->ops->setfromoptions) { 3646991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 365d763cef2SBarry Smith } 3666991f827SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 367d763cef2SBarry Smith 368d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 369d763cef2SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 370d763cef2SBarry Smith 371bc952696SBarry Smith if (ts->trajectory) { 372bc952696SBarry Smith ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr); 373bc952696SBarry Smith } 374bc952696SBarry Smith 3756991f827SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3766991f827SBarry Smith if (snes) { 3776991f827SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 37849c41c37SLisandro Dalcin ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 379d763cef2SBarry Smith } 380d763cef2SBarry Smith PetscFunctionReturn(0); 381d763cef2SBarry Smith } 382d763cef2SBarry Smith 383d763cef2SBarry Smith #undef __FUNCT__ 384bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory" 385d2daff3dSHong Zhang /*@ 386bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 387d2daff3dSHong Zhang 388d2daff3dSHong Zhang Collective on TS 389d2daff3dSHong Zhang 390d2daff3dSHong Zhang Input Parameters: 391bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 392bc952696SBarry Smith 393d2daff3dSHong Zhang 394d2daff3dSHong Zhang Level: intermediate 395d2daff3dSHong Zhang 396bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve() 397d2daff3dSHong Zhang 398bc952696SBarry Smith .keywords: TS, set, checkpoint, 399d2daff3dSHong Zhang @*/ 400bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 401d2daff3dSHong Zhang { 402bc952696SBarry Smith PetscErrorCode ierr; 403bc952696SBarry Smith 404d2daff3dSHong Zhang PetscFunctionBegin; 405d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 406bc952696SBarry Smith if (!ts->trajectory) { 407bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 408920aabf2SBarry Smith ierr = TSTrajectorySetType(ts->trajectory,TSTRAJECTORYBASIC);CHKERRQ(ierr); 409648b50deSBarry Smith ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr); 410bc952696SBarry Smith } 411d2daff3dSHong Zhang PetscFunctionReturn(0); 412d2daff3dSHong Zhang } 413d2daff3dSHong Zhang 414a7a1495cSBarry Smith #undef __FUNCT__ 415a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian" 416a7a1495cSBarry Smith /*@ 417a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 418a7a1495cSBarry Smith set with TSSetRHSJacobian(). 419a7a1495cSBarry Smith 420a7a1495cSBarry Smith Collective on TS and Vec 421a7a1495cSBarry Smith 422a7a1495cSBarry Smith Input Parameters: 423316643e7SJed Brown + ts - the TS context 424a7a1495cSBarry Smith . t - current timestep 4250910c330SBarry Smith - U - input vector 426a7a1495cSBarry Smith 427a7a1495cSBarry Smith Output Parameters: 428a7a1495cSBarry Smith + A - Jacobian matrix 429a7a1495cSBarry Smith . B - optional preconditioning matrix 430a7a1495cSBarry Smith - flag - flag indicating matrix structure 431a7a1495cSBarry Smith 432a7a1495cSBarry Smith Notes: 433a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 434a7a1495cSBarry Smith is used internally within the nonlinear solvers. 435a7a1495cSBarry Smith 43694b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 437a7a1495cSBarry Smith flag parameter. 438a7a1495cSBarry Smith 439a7a1495cSBarry Smith Level: developer 440a7a1495cSBarry Smith 441a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 442a7a1495cSBarry Smith 44394b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 444a7a1495cSBarry Smith @*/ 445d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 446a7a1495cSBarry Smith { 447dfbe8321SBarry Smith PetscErrorCode ierr; 448270bf2e7SJed Brown PetscObjectState Ustate; 44924989b8cSPeter Brune DM dm; 450942e3340SBarry Smith DMTS tsdm; 45124989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 45224989b8cSPeter Brune void *ctx; 45324989b8cSPeter Brune TSIJacobian ijacobianfunc; 454b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 455a7a1495cSBarry Smith 456a7a1495cSBarry Smith PetscFunctionBegin; 4570700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4580910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 4590910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 46024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 461942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 46224989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 4630298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 464b2df71adSDebojyoti Ghosh ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 46559e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 466b2df71adSDebojyoti Ghosh if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 4670e4ef248SJed Brown PetscFunctionReturn(0); 468f8ede8e7SJed Brown } 469d90be118SSean Farley 470ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 471d90be118SSean Farley 472e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 47394ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 47494ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 47594ab13aaSBarry Smith if (A != B) { 47694ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 47794ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 478e1244c69SJed Brown } 479e1244c69SJed Brown ts->rhsjacobian.shift = 0; 480e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 481e1244c69SJed Brown } 482e1244c69SJed Brown 48324989b8cSPeter Brune if (rhsjacobianfunc) { 4846cd88445SBarry Smith PetscBool missing; 48594ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 486a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 487d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 488a7a1495cSBarry Smith PetscStackPop; 48994ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 4906cd88445SBarry Smith if (A) { 4916cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 4926cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 4936cd88445SBarry Smith } 4946cd88445SBarry Smith if (B && B != A) { 4956cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 4966cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 4976cd88445SBarry Smith } 498ef66eb69SBarry Smith } else { 49994ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 50094ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 501ef66eb69SBarry Smith } 5020e4ef248SJed Brown ts->rhsjacobian.time = t; 5030910c330SBarry Smith ts->rhsjacobian.X = U; 50459e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 505a7a1495cSBarry Smith PetscFunctionReturn(0); 506a7a1495cSBarry Smith } 507a7a1495cSBarry Smith 5084a2ae208SSatish Balay #undef __FUNCT__ 5094a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 510316643e7SJed Brown /*@ 511d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 512d763cef2SBarry Smith 513316643e7SJed Brown Collective on TS and Vec 514316643e7SJed Brown 515316643e7SJed Brown Input Parameters: 516316643e7SJed Brown + ts - the TS context 517316643e7SJed Brown . t - current time 5180910c330SBarry Smith - U - state vector 519316643e7SJed Brown 520316643e7SJed Brown Output Parameter: 521316643e7SJed Brown . y - right hand side 522316643e7SJed Brown 523316643e7SJed Brown Note: 524316643e7SJed Brown Most users should not need to explicitly call this routine, as it 525316643e7SJed Brown is used internally within the nonlinear solvers. 526316643e7SJed Brown 527316643e7SJed Brown Level: developer 528316643e7SJed Brown 529316643e7SJed Brown .keywords: TS, compute 530316643e7SJed Brown 531316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 532316643e7SJed Brown @*/ 5330910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 534d763cef2SBarry Smith { 535dfbe8321SBarry Smith PetscErrorCode ierr; 53624989b8cSPeter Brune TSRHSFunction rhsfunction; 53724989b8cSPeter Brune TSIFunction ifunction; 53824989b8cSPeter Brune void *ctx; 53924989b8cSPeter Brune DM dm; 54024989b8cSPeter Brune 541d763cef2SBarry Smith PetscFunctionBegin; 5420700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5430910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5440700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 54524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 54624989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 5470298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 548d763cef2SBarry Smith 549ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 550d763cef2SBarry Smith 5510910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 55224989b8cSPeter Brune if (rhsfunction) { 553d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 5540910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 555d763cef2SBarry Smith PetscStackPop; 556214bc6a2SJed Brown } else { 557214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 558b2cd27e8SJed Brown } 55944a41b28SSean Farley 5600910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 561d763cef2SBarry Smith PetscFunctionReturn(0); 562d763cef2SBarry Smith } 563d763cef2SBarry Smith 5644a2ae208SSatish Balay #undef __FUNCT__ 565ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 566ef20d060SBarry Smith /*@ 567ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 568ef20d060SBarry Smith 569ef20d060SBarry Smith Collective on TS and Vec 570ef20d060SBarry Smith 571ef20d060SBarry Smith Input Parameters: 572ef20d060SBarry Smith + ts - the TS context 573ef20d060SBarry Smith - t - current time 574ef20d060SBarry Smith 575ef20d060SBarry Smith Output Parameter: 5760910c330SBarry Smith . U - the solution 577ef20d060SBarry Smith 578ef20d060SBarry Smith Note: 579ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 580ef20d060SBarry Smith is used internally within the nonlinear solvers. 581ef20d060SBarry Smith 582ef20d060SBarry Smith Level: developer 583ef20d060SBarry Smith 584ef20d060SBarry Smith .keywords: TS, compute 585ef20d060SBarry Smith 586abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 587ef20d060SBarry Smith @*/ 5880910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 589ef20d060SBarry Smith { 590ef20d060SBarry Smith PetscErrorCode ierr; 591ef20d060SBarry Smith TSSolutionFunction solutionfunction; 592ef20d060SBarry Smith void *ctx; 593ef20d060SBarry Smith DM dm; 594ef20d060SBarry Smith 595ef20d060SBarry Smith PetscFunctionBegin; 596ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5970910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 598ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 599ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 600ef20d060SBarry Smith 601ef20d060SBarry Smith if (solutionfunction) { 6029b7cd975SBarry Smith PetscStackPush("TS user solution function"); 6030910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 604ef20d060SBarry Smith PetscStackPop; 605ef20d060SBarry Smith } 606ef20d060SBarry Smith PetscFunctionReturn(0); 607ef20d060SBarry Smith } 6089b7cd975SBarry Smith #undef __FUNCT__ 6099b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction" 6109b7cd975SBarry Smith /*@ 6119b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6129b7cd975SBarry Smith 6139b7cd975SBarry Smith Collective on TS and Vec 6149b7cd975SBarry Smith 6159b7cd975SBarry Smith Input Parameters: 6169b7cd975SBarry Smith + ts - the TS context 6179b7cd975SBarry Smith - t - current time 6189b7cd975SBarry Smith 6199b7cd975SBarry Smith Output Parameter: 6209b7cd975SBarry Smith . U - the function value 6219b7cd975SBarry Smith 6229b7cd975SBarry Smith Note: 6239b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 6249b7cd975SBarry Smith is used internally within the nonlinear solvers. 6259b7cd975SBarry Smith 6269b7cd975SBarry Smith Level: developer 6279b7cd975SBarry Smith 6289b7cd975SBarry Smith .keywords: TS, compute 6299b7cd975SBarry Smith 6309b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 6319b7cd975SBarry Smith @*/ 6329b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 6339b7cd975SBarry Smith { 6349b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 6359b7cd975SBarry Smith void *ctx; 6369b7cd975SBarry Smith DM dm; 6379b7cd975SBarry Smith 6389b7cd975SBarry Smith PetscFunctionBegin; 6399b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6409b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6419b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 6429b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 6439b7cd975SBarry Smith 6449b7cd975SBarry Smith if (forcing) { 6459b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 6469b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 6479b7cd975SBarry Smith PetscStackPop; 6489b7cd975SBarry Smith } 6499b7cd975SBarry Smith PetscFunctionReturn(0); 6509b7cd975SBarry Smith } 651ef20d060SBarry Smith 652ef20d060SBarry Smith #undef __FUNCT__ 653214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 654214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 655214bc6a2SJed Brown { 6562dd45cf8SJed Brown Vec F; 657214bc6a2SJed Brown PetscErrorCode ierr; 658214bc6a2SJed Brown 659214bc6a2SJed Brown PetscFunctionBegin; 6600298fd71SBarry Smith *Frhs = NULL; 6610298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 662214bc6a2SJed Brown if (!ts->Frhs) { 6632dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 664214bc6a2SJed Brown } 665214bc6a2SJed Brown *Frhs = ts->Frhs; 666214bc6a2SJed Brown PetscFunctionReturn(0); 667214bc6a2SJed Brown } 668214bc6a2SJed Brown 669214bc6a2SJed Brown #undef __FUNCT__ 670214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 671214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 672214bc6a2SJed Brown { 673214bc6a2SJed Brown Mat A,B; 6742dd45cf8SJed Brown PetscErrorCode ierr; 675214bc6a2SJed Brown 676214bc6a2SJed Brown PetscFunctionBegin; 677c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 678c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 6790298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 680214bc6a2SJed Brown if (Arhs) { 681214bc6a2SJed Brown if (!ts->Arhs) { 682214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 683214bc6a2SJed Brown } 684214bc6a2SJed Brown *Arhs = ts->Arhs; 685214bc6a2SJed Brown } 686214bc6a2SJed Brown if (Brhs) { 687214bc6a2SJed Brown if (!ts->Brhs) { 688bdb70873SJed Brown if (A != B) { 689214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 690bdb70873SJed Brown } else { 691bdb70873SJed Brown ts->Brhs = ts->Arhs; 692bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 693bdb70873SJed Brown } 694214bc6a2SJed Brown } 695214bc6a2SJed Brown *Brhs = ts->Brhs; 696214bc6a2SJed Brown } 697214bc6a2SJed Brown PetscFunctionReturn(0); 698214bc6a2SJed Brown } 699214bc6a2SJed Brown 700214bc6a2SJed Brown #undef __FUNCT__ 701316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 702316643e7SJed Brown /*@ 7030910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 704316643e7SJed Brown 705316643e7SJed Brown Collective on TS and Vec 706316643e7SJed Brown 707316643e7SJed Brown Input Parameters: 708316643e7SJed Brown + ts - the TS context 709316643e7SJed Brown . t - current time 7100910c330SBarry Smith . U - state vector 7110910c330SBarry Smith . Udot - time derivative of state vector 712214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 713316643e7SJed Brown 714316643e7SJed Brown Output Parameter: 715316643e7SJed Brown . Y - right hand side 716316643e7SJed Brown 717316643e7SJed Brown Note: 718316643e7SJed Brown Most users should not need to explicitly call this routine, as it 719316643e7SJed Brown is used internally within the nonlinear solvers. 720316643e7SJed Brown 721316643e7SJed Brown If the user did did not write their equations in implicit form, this 722316643e7SJed Brown function recasts them in implicit form. 723316643e7SJed Brown 724316643e7SJed Brown Level: developer 725316643e7SJed Brown 726316643e7SJed Brown .keywords: TS, compute 727316643e7SJed Brown 728316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 729316643e7SJed Brown @*/ 7300910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 731316643e7SJed Brown { 732316643e7SJed Brown PetscErrorCode ierr; 73324989b8cSPeter Brune TSIFunction ifunction; 73424989b8cSPeter Brune TSRHSFunction rhsfunction; 73524989b8cSPeter Brune void *ctx; 73624989b8cSPeter Brune DM dm; 737316643e7SJed Brown 738316643e7SJed Brown PetscFunctionBegin; 7390700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7400910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7410910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 7420700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 743316643e7SJed Brown 74424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 74524989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 7460298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 74724989b8cSPeter Brune 748ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 749d90be118SSean Farley 7500910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 75124989b8cSPeter Brune if (ifunction) { 752316643e7SJed Brown PetscStackPush("TS user implicit function"); 7530910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 754316643e7SJed Brown PetscStackPop; 755214bc6a2SJed Brown } 756214bc6a2SJed Brown if (imex) { 75724989b8cSPeter Brune if (!ifunction) { 7580910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 7592dd45cf8SJed Brown } 76024989b8cSPeter Brune } else if (rhsfunction) { 76124989b8cSPeter Brune if (ifunction) { 762214bc6a2SJed Brown Vec Frhs; 763214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 7640910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 765214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 7662dd45cf8SJed Brown } else { 7670910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 7680910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 769316643e7SJed Brown } 7704a6899ffSJed Brown } 7710910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 772316643e7SJed Brown PetscFunctionReturn(0); 773316643e7SJed Brown } 774316643e7SJed Brown 775316643e7SJed Brown #undef __FUNCT__ 776316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 777316643e7SJed Brown /*@ 778316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 779316643e7SJed Brown 780316643e7SJed Brown Collective on TS and Vec 781316643e7SJed Brown 782316643e7SJed Brown Input 783316643e7SJed Brown Input Parameters: 784316643e7SJed Brown + ts - the TS context 785316643e7SJed Brown . t - current timestep 7860910c330SBarry Smith . U - state vector 7870910c330SBarry Smith . Udot - time derivative of state vector 788214bc6a2SJed Brown . shift - shift to apply, see note below 789214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 790316643e7SJed Brown 791316643e7SJed Brown Output Parameters: 792316643e7SJed Brown + A - Jacobian matrix 793316643e7SJed Brown . B - optional preconditioning matrix 794316643e7SJed Brown - flag - flag indicating matrix structure 795316643e7SJed Brown 796316643e7SJed Brown Notes: 7970910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 798316643e7SJed Brown 7990910c330SBarry Smith dF/dU + shift*dF/dUdot 800316643e7SJed Brown 801316643e7SJed Brown Most users should not need to explicitly call this routine, as it 802316643e7SJed Brown is used internally within the nonlinear solvers. 803316643e7SJed Brown 804316643e7SJed Brown Level: developer 805316643e7SJed Brown 806316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 807316643e7SJed Brown 808316643e7SJed Brown .seealso: TSSetIJacobian() 80963495f91SJed Brown @*/ 810d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 811316643e7SJed Brown { 812316643e7SJed Brown PetscErrorCode ierr; 81324989b8cSPeter Brune TSIJacobian ijacobian; 81424989b8cSPeter Brune TSRHSJacobian rhsjacobian; 81524989b8cSPeter Brune DM dm; 81624989b8cSPeter Brune void *ctx; 817316643e7SJed Brown 818316643e7SJed Brown PetscFunctionBegin; 8190700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8200910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8210910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 822316643e7SJed Brown PetscValidPointer(A,6); 82394ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 824316643e7SJed Brown PetscValidPointer(B,7); 82594ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 82624989b8cSPeter Brune 82724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 82824989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 8290298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 83024989b8cSPeter Brune 831ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 832316643e7SJed Brown 83394ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 83424989b8cSPeter Brune if (ijacobian) { 8356cd88445SBarry Smith PetscBool missing; 836316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 837d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 838316643e7SJed Brown PetscStackPop; 8396cd88445SBarry Smith if (A) { 8406cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 8416cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 8426cd88445SBarry Smith } 8436cd88445SBarry Smith if (B && B != A) { 8446cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 8456cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 8466cd88445SBarry Smith } 8474a6899ffSJed Brown } 848214bc6a2SJed Brown if (imex) { 849b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 85094ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 85194ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 85294ab13aaSBarry Smith if (A != B) { 85394ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 85494ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 855214bc6a2SJed Brown } 856214bc6a2SJed Brown } 857214bc6a2SJed Brown } else { 858e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 859e1244c69SJed Brown if (rhsjacobian) { 860bd373395SBarry Smith if (ijacobian) { 861214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 862bd373395SBarry Smith } else { 863bd373395SBarry Smith ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr); 864214bc6a2SJed Brown } 865d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 866e1244c69SJed Brown } 86794ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 868e1244c69SJed Brown ts->rhsjacobian.scale = -1; 869e1244c69SJed Brown ts->rhsjacobian.shift = shift; 87094ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 87194ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 87294ab13aaSBarry Smith if (A != B) { 87394ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 87494ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 875316643e7SJed Brown } 876e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 877e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 878e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 87994ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 88094ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 88194ab13aaSBarry Smith if (A != B) { 88294ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 88394ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 884214bc6a2SJed Brown } 885316643e7SJed Brown } 88694ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 88794ab13aaSBarry Smith if (A != B) { 88894ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 889316643e7SJed Brown } 890316643e7SJed Brown } 891316643e7SJed Brown } 89294ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 893316643e7SJed Brown PetscFunctionReturn(0); 894316643e7SJed Brown } 895316643e7SJed Brown 896316643e7SJed Brown #undef __FUNCT__ 8974a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 898d763cef2SBarry Smith /*@C 899d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 900b5abc632SBarry Smith where U_t = G(t,u). 901d763cef2SBarry Smith 9023f9fe445SBarry Smith Logically Collective on TS 903d763cef2SBarry Smith 904d763cef2SBarry Smith Input Parameters: 905d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 9060298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 907d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 908d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 9090298fd71SBarry Smith function evaluation routine (may be NULL) 910d763cef2SBarry Smith 911d763cef2SBarry Smith Calling sequence of func: 91287828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 913d763cef2SBarry Smith 914d763cef2SBarry Smith + t - current timestep 915d763cef2SBarry Smith . u - input vector 916d763cef2SBarry Smith . F - function vector 917d763cef2SBarry Smith - ctx - [optional] user-defined function context 918d763cef2SBarry Smith 919d763cef2SBarry Smith Level: beginner 920d763cef2SBarry Smith 9212bbac0d3SBarry Smith Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 9222bbac0d3SBarry Smith 923d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 924d763cef2SBarry Smith 925ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 926d763cef2SBarry Smith @*/ 927089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 928d763cef2SBarry Smith { 929089b2837SJed Brown PetscErrorCode ierr; 930089b2837SJed Brown SNES snes; 9310298fd71SBarry Smith Vec ralloc = NULL; 93224989b8cSPeter Brune DM dm; 933d763cef2SBarry Smith 934089b2837SJed Brown PetscFunctionBegin; 9350700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 936ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 93724989b8cSPeter Brune 93824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 93924989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 940089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 941e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 942e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 943e856ceecSJed Brown r = ralloc; 944e856ceecSJed Brown } 945089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 946e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 947d763cef2SBarry Smith PetscFunctionReturn(0); 948d763cef2SBarry Smith } 949d763cef2SBarry Smith 9504a2ae208SSatish Balay #undef __FUNCT__ 951ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 952ef20d060SBarry Smith /*@C 953abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 954ef20d060SBarry Smith 955ef20d060SBarry Smith Logically Collective on TS 956ef20d060SBarry Smith 957ef20d060SBarry Smith Input Parameters: 958ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 959ef20d060SBarry Smith . f - routine for evaluating the solution 960ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 9610298fd71SBarry Smith function evaluation routine (may be NULL) 962ef20d060SBarry Smith 963ef20d060SBarry Smith Calling sequence of func: 964ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 965ef20d060SBarry Smith 966ef20d060SBarry Smith + t - current timestep 967ef20d060SBarry Smith . u - output vector 968ef20d060SBarry Smith - ctx - [optional] user-defined function context 969ef20d060SBarry Smith 970abd5a294SJed Brown Notes: 971abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 972abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 973abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 974abd5a294SJed Brown 9754f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 976abd5a294SJed Brown 977ef20d060SBarry Smith Level: beginner 978ef20d060SBarry Smith 979ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 980ef20d060SBarry Smith 9819b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 982ef20d060SBarry Smith @*/ 983ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 984ef20d060SBarry Smith { 985ef20d060SBarry Smith PetscErrorCode ierr; 986ef20d060SBarry Smith DM dm; 987ef20d060SBarry Smith 988ef20d060SBarry Smith PetscFunctionBegin; 989ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 990ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 991ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 992ef20d060SBarry Smith PetscFunctionReturn(0); 993ef20d060SBarry Smith } 994ef20d060SBarry Smith 995ef20d060SBarry Smith #undef __FUNCT__ 9969b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction" 9979b7cd975SBarry Smith /*@C 9989b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 9999b7cd975SBarry Smith 10009b7cd975SBarry Smith Logically Collective on TS 10019b7cd975SBarry Smith 10029b7cd975SBarry Smith Input Parameters: 10039b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 10049b7cd975SBarry Smith . f - routine for evaluating the forcing function 10059b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 10060298fd71SBarry Smith function evaluation routine (may be NULL) 10079b7cd975SBarry Smith 10089b7cd975SBarry Smith Calling sequence of func: 10099b7cd975SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 10109b7cd975SBarry Smith 10119b7cd975SBarry Smith + t - current timestep 10129b7cd975SBarry Smith . u - output vector 10139b7cd975SBarry Smith - ctx - [optional] user-defined function context 10149b7cd975SBarry Smith 10159b7cd975SBarry Smith Notes: 10169b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 10179b7cd975SBarry Smith create closed-form solutions with a non-physical forcing term. 10189b7cd975SBarry Smith 10199b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 10209b7cd975SBarry Smith 10219b7cd975SBarry Smith Level: beginner 10229b7cd975SBarry Smith 10239b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 10249b7cd975SBarry Smith 10259b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 10269b7cd975SBarry Smith @*/ 10279b7cd975SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 10289b7cd975SBarry Smith { 10299b7cd975SBarry Smith PetscErrorCode ierr; 10309b7cd975SBarry Smith DM dm; 10319b7cd975SBarry Smith 10329b7cd975SBarry Smith PetscFunctionBegin; 10339b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10349b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 10359b7cd975SBarry Smith ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr); 10369b7cd975SBarry Smith PetscFunctionReturn(0); 10379b7cd975SBarry Smith } 10389b7cd975SBarry Smith 10399b7cd975SBarry Smith #undef __FUNCT__ 10404a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 1041d763cef2SBarry Smith /*@C 1042f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1043b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1044d763cef2SBarry Smith 10453f9fe445SBarry Smith Logically Collective on TS 1046d763cef2SBarry Smith 1047d763cef2SBarry Smith Input Parameters: 1048d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1049e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1050e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1051d763cef2SBarry Smith . f - the Jacobian evaluation routine 1052d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 10530298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1054d763cef2SBarry Smith 1055f7ab8db6SBarry Smith Calling sequence of f: 1056ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1057d763cef2SBarry Smith 1058d763cef2SBarry Smith + t - current timestep 1059d763cef2SBarry Smith . u - input vector 1060e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1061e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1062d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1063d763cef2SBarry Smith 10646cd88445SBarry Smith Notes: 10656cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 10666cd88445SBarry Smith 10676cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1068ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1069d763cef2SBarry Smith 1070d763cef2SBarry Smith Level: beginner 1071d763cef2SBarry Smith 1072d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1073d763cef2SBarry Smith 1074ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1075d763cef2SBarry Smith 1076d763cef2SBarry Smith @*/ 1077e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1078d763cef2SBarry Smith { 1079277b19d0SLisandro Dalcin PetscErrorCode ierr; 1080089b2837SJed Brown SNES snes; 108124989b8cSPeter Brune DM dm; 108224989b8cSPeter Brune TSIJacobian ijacobian; 1083277b19d0SLisandro Dalcin 1084d763cef2SBarry Smith PetscFunctionBegin; 10850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1086e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1087e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1088e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1089e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1090d763cef2SBarry Smith 109124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 109224989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1093e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1094e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1095e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1096e1244c69SJed Brown } 10970298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1098089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 10995f659677SPeter Brune if (!ijacobian) { 1100e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 11010e4ef248SJed Brown } 1102e5d3d808SBarry Smith if (Amat) { 1103e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 11040e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1105bbd56ea5SKarl Rupp 1106e5d3d808SBarry Smith ts->Arhs = Amat; 11070e4ef248SJed Brown } 1108e5d3d808SBarry Smith if (Pmat) { 1109e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 11100e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1111bbd56ea5SKarl Rupp 1112e5d3d808SBarry Smith ts->Brhs = Pmat; 11130e4ef248SJed Brown } 1114d763cef2SBarry Smith PetscFunctionReturn(0); 1115d763cef2SBarry Smith } 1116d763cef2SBarry Smith 1117316643e7SJed Brown 1118316643e7SJed Brown #undef __FUNCT__ 1119316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 1120316643e7SJed Brown /*@C 1121b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1122316643e7SJed Brown 11233f9fe445SBarry Smith Logically Collective on TS 1124316643e7SJed Brown 1125316643e7SJed Brown Input Parameters: 1126316643e7SJed Brown + ts - the TS context obtained from TSCreate() 11270298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1128316643e7SJed Brown . f - the function evaluation routine 11290298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1130316643e7SJed Brown 1131316643e7SJed Brown Calling sequence of f: 1132316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1133316643e7SJed Brown 1134316643e7SJed Brown + t - time at step/stage being solved 1135316643e7SJed Brown . u - state vector 1136316643e7SJed Brown . u_t - time derivative of state vector 1137316643e7SJed Brown . F - function vector 1138316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1139316643e7SJed Brown 1140316643e7SJed Brown Important: 11412bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1142316643e7SJed Brown 1143316643e7SJed Brown Level: beginner 1144316643e7SJed Brown 1145316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1146316643e7SJed Brown 1147d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1148316643e7SJed Brown @*/ 1149089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 1150316643e7SJed Brown { 1151089b2837SJed Brown PetscErrorCode ierr; 1152089b2837SJed Brown SNES snes; 11530298fd71SBarry Smith Vec resalloc = NULL; 115424989b8cSPeter Brune DM dm; 1155316643e7SJed Brown 1156316643e7SJed Brown PetscFunctionBegin; 11570700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1158ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 115924989b8cSPeter Brune 116024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 116124989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 116224989b8cSPeter Brune 1163089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1164e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 1165e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 1166e856ceecSJed Brown res = resalloc; 1167e856ceecSJed Brown } 1168089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 1169e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 1170089b2837SJed Brown PetscFunctionReturn(0); 1171089b2837SJed Brown } 1172089b2837SJed Brown 1173089b2837SJed Brown #undef __FUNCT__ 1174089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 1175089b2837SJed Brown /*@C 1176089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1177089b2837SJed Brown 1178089b2837SJed Brown Not Collective 1179089b2837SJed Brown 1180089b2837SJed Brown Input Parameter: 1181089b2837SJed Brown . ts - the TS context 1182089b2837SJed Brown 1183089b2837SJed Brown Output Parameter: 11840298fd71SBarry Smith + r - vector to hold residual (or NULL) 11850298fd71SBarry Smith . func - the function to compute residual (or NULL) 11860298fd71SBarry Smith - ctx - the function context (or NULL) 1187089b2837SJed Brown 1188089b2837SJed Brown Level: advanced 1189089b2837SJed Brown 1190089b2837SJed Brown .keywords: TS, nonlinear, get, function 1191089b2837SJed Brown 1192089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1193089b2837SJed Brown @*/ 1194089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1195089b2837SJed Brown { 1196089b2837SJed Brown PetscErrorCode ierr; 1197089b2837SJed Brown SNES snes; 119824989b8cSPeter Brune DM dm; 1199089b2837SJed Brown 1200089b2837SJed Brown PetscFunctionBegin; 1201089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1202089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12030298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 120424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 120524989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1206089b2837SJed Brown PetscFunctionReturn(0); 1207089b2837SJed Brown } 1208089b2837SJed Brown 1209089b2837SJed Brown #undef __FUNCT__ 1210089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 1211089b2837SJed Brown /*@C 1212089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1213089b2837SJed Brown 1214089b2837SJed Brown Not Collective 1215089b2837SJed Brown 1216089b2837SJed Brown Input Parameter: 1217089b2837SJed Brown . ts - the TS context 1218089b2837SJed Brown 1219089b2837SJed Brown Output Parameter: 12200298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 12210298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 12220298fd71SBarry Smith - ctx - the function context (or NULL) 1223089b2837SJed Brown 1224089b2837SJed Brown Level: advanced 1225089b2837SJed Brown 1226089b2837SJed Brown .keywords: TS, nonlinear, get, function 1227089b2837SJed Brown 12282bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1229089b2837SJed Brown @*/ 1230089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1231089b2837SJed Brown { 1232089b2837SJed Brown PetscErrorCode ierr; 1233089b2837SJed Brown SNES snes; 123424989b8cSPeter Brune DM dm; 1235089b2837SJed Brown 1236089b2837SJed Brown PetscFunctionBegin; 1237089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1238089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12390298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 124024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 124124989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1242316643e7SJed Brown PetscFunctionReturn(0); 1243316643e7SJed Brown } 1244316643e7SJed Brown 1245316643e7SJed Brown #undef __FUNCT__ 1246316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 1247316643e7SJed Brown /*@C 1248a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1249ae8867d6SBarry Smith provided with TSSetIFunction(). 1250316643e7SJed Brown 12513f9fe445SBarry Smith Logically Collective on TS 1252316643e7SJed Brown 1253316643e7SJed Brown Input Parameters: 1254316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1255e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1256e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1257316643e7SJed Brown . f - the Jacobian evaluation routine 12580298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1259316643e7SJed Brown 1260316643e7SJed Brown Calling sequence of f: 1261ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1262316643e7SJed Brown 1263316643e7SJed Brown + t - time at step/stage being solved 12641b4a444bSJed Brown . U - state vector 12651b4a444bSJed Brown . U_t - time derivative of state vector 1266316643e7SJed Brown . a - shift 1267e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1268e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1269316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1270316643e7SJed Brown 1271316643e7SJed Brown Notes: 1272e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1273316643e7SJed Brown 1274895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1275895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1276895c21f2SBarry Smith 1277a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1278b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1279a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1280a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1281a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1282a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1283a4f0a591SBarry Smith 12846cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 12856cd88445SBarry Smith 12866cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1287ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1288ca5f011dSBarry Smith 1289316643e7SJed Brown Level: beginner 1290316643e7SJed Brown 1291316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1292316643e7SJed Brown 1293ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1294316643e7SJed Brown 1295316643e7SJed Brown @*/ 1296e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1297316643e7SJed Brown { 1298316643e7SJed Brown PetscErrorCode ierr; 1299089b2837SJed Brown SNES snes; 130024989b8cSPeter Brune DM dm; 1301316643e7SJed Brown 1302316643e7SJed Brown PetscFunctionBegin; 13030700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1304e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1305e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1306e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1307e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 130824989b8cSPeter Brune 130924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 131024989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 131124989b8cSPeter Brune 1312089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1313e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1314316643e7SJed Brown PetscFunctionReturn(0); 1315316643e7SJed Brown } 1316316643e7SJed Brown 13174a2ae208SSatish Balay #undef __FUNCT__ 1318e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse" 1319e1244c69SJed Brown /*@ 1320e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1321e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1322e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1323e1244c69SJed Brown not been changed by the TS. 1324e1244c69SJed Brown 1325e1244c69SJed Brown Logically Collective 1326e1244c69SJed Brown 1327e1244c69SJed Brown Input Arguments: 1328e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1329e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1330e1244c69SJed Brown 1331e1244c69SJed Brown Level: intermediate 1332e1244c69SJed Brown 1333e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1334e1244c69SJed Brown @*/ 1335e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1336e1244c69SJed Brown { 1337e1244c69SJed Brown PetscFunctionBegin; 1338e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1339e1244c69SJed Brown PetscFunctionReturn(0); 1340e1244c69SJed Brown } 1341e1244c69SJed Brown 1342e1244c69SJed Brown #undef __FUNCT__ 134355849f57SBarry Smith #define __FUNCT__ "TSLoad" 134455849f57SBarry Smith /*@C 134555849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 134655849f57SBarry Smith 134755849f57SBarry Smith Collective on PetscViewer 134855849f57SBarry Smith 134955849f57SBarry Smith Input Parameters: 135055849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 135155849f57SBarry Smith some related function before a call to TSLoad(). 135255849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 135355849f57SBarry Smith 135455849f57SBarry Smith Level: intermediate 135555849f57SBarry Smith 135655849f57SBarry Smith Notes: 135755849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 135855849f57SBarry Smith 135955849f57SBarry Smith Notes for advanced users: 136055849f57SBarry Smith Most users should not need to know the details of the binary storage 136155849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 136255849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 136355849f57SBarry Smith format is 136455849f57SBarry Smith .vb 136555849f57SBarry Smith has not yet been determined 136655849f57SBarry Smith .ve 136755849f57SBarry Smith 136855849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 136955849f57SBarry Smith @*/ 1370f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 137155849f57SBarry Smith { 137255849f57SBarry Smith PetscErrorCode ierr; 137355849f57SBarry Smith PetscBool isbinary; 137455849f57SBarry Smith PetscInt classid; 137555849f57SBarry Smith char type[256]; 13762d53ad75SBarry Smith DMTS sdm; 1377ad6bc421SBarry Smith DM dm; 137855849f57SBarry Smith 137955849f57SBarry Smith PetscFunctionBegin; 1380f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 138155849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 138255849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 138355849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 138455849f57SBarry Smith 1385060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1386ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1387060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1388f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1389f2c2a1b9SBarry Smith if (ts->ops->load) { 1390f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1391f2c2a1b9SBarry Smith } 1392ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1393ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1394ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1395f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1396f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 13972d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 13982d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 139955849f57SBarry Smith PetscFunctionReturn(0); 140055849f57SBarry Smith } 140155849f57SBarry Smith 14029804daf3SBarry Smith #include <petscdraw.h> 1403e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1404e04113cfSBarry Smith #include <petscviewersaws.h> 1405f05ece33SBarry Smith #endif 140655849f57SBarry Smith #undef __FUNCT__ 14074a2ae208SSatish Balay #define __FUNCT__ "TSView" 14087e2c5f70SBarry Smith /*@C 1409d763cef2SBarry Smith TSView - Prints the TS data structure. 1410d763cef2SBarry Smith 14114c49b128SBarry Smith Collective on TS 1412d763cef2SBarry Smith 1413d763cef2SBarry Smith Input Parameters: 1414d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1415d763cef2SBarry Smith - viewer - visualization context 1416d763cef2SBarry Smith 1417d763cef2SBarry Smith Options Database Key: 1418d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1419d763cef2SBarry Smith 1420d763cef2SBarry Smith Notes: 1421d763cef2SBarry Smith The available visualization contexts include 1422b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1423b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1424d763cef2SBarry Smith output where only the first processor opens 1425d763cef2SBarry Smith the file. All other processors send their 1426d763cef2SBarry Smith data to the first processor to print. 1427d763cef2SBarry Smith 1428d763cef2SBarry Smith The user can open an alternative visualization context with 1429b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1430d763cef2SBarry Smith 1431d763cef2SBarry Smith Level: beginner 1432d763cef2SBarry Smith 1433d763cef2SBarry Smith .keywords: TS, timestep, view 1434d763cef2SBarry Smith 1435b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1436d763cef2SBarry Smith @*/ 14377087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1438d763cef2SBarry Smith { 1439dfbe8321SBarry Smith PetscErrorCode ierr; 144019fd82e9SBarry Smith TSType type; 14412b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 14422d53ad75SBarry Smith DMTS sdm; 1443e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1444536b137fSBarry Smith PetscBool issaws; 1445f05ece33SBarry Smith #endif 1446d763cef2SBarry Smith 1447d763cef2SBarry Smith PetscFunctionBegin; 14480700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14493050cee2SBarry Smith if (!viewer) { 1450ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 14513050cee2SBarry Smith } 14520700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1453c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1454fd16b177SBarry Smith 1455251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1456251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 145755849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 14582b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1459e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1460536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1461f05ece33SBarry Smith #endif 146232077d6dSBarry Smith if (iascii) { 1463dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 146477431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 14657c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1466d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 14675ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1468c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1469d763cef2SBarry Smith } 14705ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1471193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 14722d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 14732d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1474d52bd9f3SBarry Smith if (ts->ops->view) { 1475d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1476d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1477d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1478d52bd9f3SBarry Smith } 14790f5bd95cSBarry Smith } else if (isstring) { 1480a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1481b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 148255849f57SBarry Smith } else if (isbinary) { 148355849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 148455849f57SBarry Smith MPI_Comm comm; 148555849f57SBarry Smith PetscMPIInt rank; 148655849f57SBarry Smith char type[256]; 148755849f57SBarry Smith 148855849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 148955849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 149055849f57SBarry Smith if (!rank) { 149155849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 149255849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 149355849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 149455849f57SBarry Smith } 149555849f57SBarry Smith if (ts->ops->view) { 149655849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 149755849f57SBarry Smith } 1498f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1499f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 15002d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 15012d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 15022b0a91c0SBarry Smith } else if (isdraw) { 15032b0a91c0SBarry Smith PetscDraw draw; 15042b0a91c0SBarry Smith char str[36]; 150589fd9fafSBarry Smith PetscReal x,y,bottom,h; 15062b0a91c0SBarry Smith 15072b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 15082b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 15092b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 15102b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 151151fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 151289fd9fafSBarry Smith bottom = y - h; 15132b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 15142b0a91c0SBarry Smith if (ts->ops->view) { 15152b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 15162b0a91c0SBarry Smith } 15172b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1518e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1519536b137fSBarry Smith } else if (issaws) { 1520d45a07a7SBarry Smith PetscMPIInt rank; 15212657e9d9SBarry Smith const char *name; 15222657e9d9SBarry Smith 15232657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1524d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1525d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1526d45a07a7SBarry Smith char dir[1024]; 1527d45a07a7SBarry Smith 1528e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1529a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 15302657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 15312657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 15322657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1533d763cef2SBarry Smith } 15340acecf5bSBarry Smith if (ts->ops->view) { 15350acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 15360acecf5bSBarry Smith } 1537f05ece33SBarry Smith #endif 1538f05ece33SBarry Smith } 1539f05ece33SBarry Smith 1540b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1541251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1542b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1543d763cef2SBarry Smith PetscFunctionReturn(0); 1544d763cef2SBarry Smith } 1545d763cef2SBarry Smith 1546d763cef2SBarry Smith 15474a2ae208SSatish Balay #undef __FUNCT__ 15484a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1549b07ff414SBarry Smith /*@ 1550d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1551d763cef2SBarry Smith the timesteppers. 1552d763cef2SBarry Smith 15533f9fe445SBarry Smith Logically Collective on TS 1554d763cef2SBarry Smith 1555d763cef2SBarry Smith Input Parameters: 1556d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1557d763cef2SBarry Smith - usrP - optional user context 1558d763cef2SBarry Smith 1559daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 1560daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1561daf670e6SBarry Smith 1562d763cef2SBarry Smith Level: intermediate 1563d763cef2SBarry Smith 1564d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1565d763cef2SBarry Smith 1566d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1567d763cef2SBarry Smith @*/ 15687087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1569d763cef2SBarry Smith { 1570d763cef2SBarry Smith PetscFunctionBegin; 15710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1572d763cef2SBarry Smith ts->user = usrP; 1573d763cef2SBarry Smith PetscFunctionReturn(0); 1574d763cef2SBarry Smith } 1575d763cef2SBarry Smith 15764a2ae208SSatish Balay #undef __FUNCT__ 15774a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1578b07ff414SBarry Smith /*@ 1579d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1580d763cef2SBarry Smith timestepper. 1581d763cef2SBarry Smith 1582d763cef2SBarry Smith Not Collective 1583d763cef2SBarry Smith 1584d763cef2SBarry Smith Input Parameter: 1585d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1586d763cef2SBarry Smith 1587d763cef2SBarry Smith Output Parameter: 1588d763cef2SBarry Smith . usrP - user context 1589d763cef2SBarry Smith 1590daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 1591daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1592daf670e6SBarry Smith 1593d763cef2SBarry Smith Level: intermediate 1594d763cef2SBarry Smith 1595d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1596d763cef2SBarry Smith 1597d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1598d763cef2SBarry Smith @*/ 1599e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1600d763cef2SBarry Smith { 1601d763cef2SBarry Smith PetscFunctionBegin; 16020700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1603e71120c6SJed Brown *(void**)usrP = ts->user; 1604d763cef2SBarry Smith PetscFunctionReturn(0); 1605d763cef2SBarry Smith } 1606d763cef2SBarry Smith 16074a2ae208SSatish Balay #undef __FUNCT__ 16084a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1609d763cef2SBarry Smith /*@ 1610b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1611d763cef2SBarry Smith 1612d763cef2SBarry Smith Not Collective 1613d763cef2SBarry Smith 1614d763cef2SBarry Smith Input Parameter: 1615d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1616d763cef2SBarry Smith 1617d763cef2SBarry Smith Output Parameter: 1618b8123daeSJed Brown . iter - number of steps completed so far 1619d763cef2SBarry Smith 1620d763cef2SBarry Smith Level: intermediate 1621d763cef2SBarry Smith 1622d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 16239be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 1624d763cef2SBarry Smith @*/ 16257087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 1626d763cef2SBarry Smith { 1627d763cef2SBarry Smith PetscFunctionBegin; 16280700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 16294482741eSBarry Smith PetscValidIntPointer(iter,2); 1630d763cef2SBarry Smith *iter = ts->steps; 1631d763cef2SBarry Smith PetscFunctionReturn(0); 1632d763cef2SBarry Smith } 1633d763cef2SBarry Smith 16344a2ae208SSatish Balay #undef __FUNCT__ 16354a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1636d763cef2SBarry Smith /*@ 1637d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1638d763cef2SBarry Smith as well as the initial time. 1639d763cef2SBarry Smith 16403f9fe445SBarry Smith Logically Collective on TS 1641d763cef2SBarry Smith 1642d763cef2SBarry Smith Input Parameters: 1643d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1644d763cef2SBarry Smith . initial_time - the initial time 1645d763cef2SBarry Smith - time_step - the size of the timestep 1646d763cef2SBarry Smith 1647d763cef2SBarry Smith Level: intermediate 1648d763cef2SBarry Smith 1649d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1650d763cef2SBarry Smith 1651d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1652d763cef2SBarry Smith @*/ 16537087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1654d763cef2SBarry Smith { 1655e144a568SJed Brown PetscErrorCode ierr; 1656e144a568SJed Brown 1657d763cef2SBarry Smith PetscFunctionBegin; 16580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1659e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1660d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1661d763cef2SBarry Smith PetscFunctionReturn(0); 1662d763cef2SBarry Smith } 1663d763cef2SBarry Smith 16644a2ae208SSatish Balay #undef __FUNCT__ 16654a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1666d763cef2SBarry Smith /*@ 1667d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1668d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1669d763cef2SBarry Smith 16703f9fe445SBarry Smith Logically Collective on TS 1671d763cef2SBarry Smith 1672d763cef2SBarry Smith Input Parameters: 1673d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1674d763cef2SBarry Smith - time_step - the size of the timestep 1675d763cef2SBarry Smith 1676d763cef2SBarry Smith Level: intermediate 1677d763cef2SBarry Smith 1678d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1679d763cef2SBarry Smith 1680d763cef2SBarry Smith .keywords: TS, set, timestep 1681d763cef2SBarry Smith @*/ 16827087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1683d763cef2SBarry Smith { 1684d763cef2SBarry Smith PetscFunctionBegin; 16850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1686c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1687d763cef2SBarry Smith ts->time_step = time_step; 168831748224SBarry Smith ts->time_step_orig = time_step; 1689d763cef2SBarry Smith PetscFunctionReturn(0); 1690d763cef2SBarry Smith } 1691d763cef2SBarry Smith 16924a2ae208SSatish Balay #undef __FUNCT__ 1693a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1694a43b19c4SJed Brown /*@ 169549354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 169649354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 169749354f04SShri Abhyankar or just return at the final time TS computed. 1698a43b19c4SJed Brown 1699a43b19c4SJed Brown Logically Collective on TS 1700a43b19c4SJed Brown 1701a43b19c4SJed Brown Input Parameter: 1702a43b19c4SJed Brown + ts - the time-step context 170349354f04SShri Abhyankar - eftopt - exact final time option 1704a43b19c4SJed Brown 1705a43b19c4SJed Brown Level: beginner 1706a43b19c4SJed Brown 1707a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 1708a43b19c4SJed Brown @*/ 170949354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 1710a43b19c4SJed Brown { 1711a43b19c4SJed Brown PetscFunctionBegin; 1712a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 171349354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 171449354f04SShri Abhyankar ts->exact_final_time = eftopt; 1715a43b19c4SJed Brown PetscFunctionReturn(0); 1716a43b19c4SJed Brown } 1717a43b19c4SJed Brown 1718a43b19c4SJed Brown #undef __FUNCT__ 17194a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1720d763cef2SBarry Smith /*@ 1721d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1722d763cef2SBarry Smith 1723d763cef2SBarry Smith Not Collective 1724d763cef2SBarry Smith 1725d763cef2SBarry Smith Input Parameter: 1726d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1727d763cef2SBarry Smith 1728d763cef2SBarry Smith Output Parameter: 1729d763cef2SBarry Smith . dt - the current timestep size 1730d763cef2SBarry Smith 1731d763cef2SBarry Smith Level: intermediate 1732d763cef2SBarry Smith 1733d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1734d763cef2SBarry Smith 1735d763cef2SBarry Smith .keywords: TS, get, timestep 1736d763cef2SBarry Smith @*/ 17377087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 1738d763cef2SBarry Smith { 1739d763cef2SBarry Smith PetscFunctionBegin; 17400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1741f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1742d763cef2SBarry Smith *dt = ts->time_step; 1743d763cef2SBarry Smith PetscFunctionReturn(0); 1744d763cef2SBarry Smith } 1745d763cef2SBarry Smith 17464a2ae208SSatish Balay #undef __FUNCT__ 17474a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1748d8e5e3e6SSatish Balay /*@ 1749d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1750d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1751d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1752d763cef2SBarry Smith the solution at the next timestep has been calculated. 1753d763cef2SBarry Smith 1754d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1755d763cef2SBarry Smith 1756d763cef2SBarry Smith Input Parameter: 1757d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1758d763cef2SBarry Smith 1759d763cef2SBarry Smith Output Parameter: 1760d763cef2SBarry Smith . v - the vector containing the solution 1761d763cef2SBarry Smith 1762d763cef2SBarry Smith Level: intermediate 1763d763cef2SBarry Smith 1764d763cef2SBarry Smith .seealso: TSGetTimeStep() 1765d763cef2SBarry Smith 1766d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1767d763cef2SBarry Smith @*/ 17687087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1769d763cef2SBarry Smith { 1770d763cef2SBarry Smith PetscFunctionBegin; 17710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17724482741eSBarry Smith PetscValidPointer(v,2); 17738737fe31SLisandro Dalcin *v = ts->vec_sol; 1774d763cef2SBarry Smith PetscFunctionReturn(0); 1775d763cef2SBarry Smith } 1776d763cef2SBarry Smith 1777c235aa8dSHong Zhang #undef __FUNCT__ 1778dfb21088SHong Zhang #define __FUNCT__ "TSGetCostGradients" 1779c235aa8dSHong Zhang /*@ 1780dfb21088SHong Zhang TSGetCostGradients - Returns the gradients from the TSAdjointSolve() 1781c235aa8dSHong Zhang 1782c235aa8dSHong Zhang Not Collective, but Vec returned is parallel if TS is parallel 1783c235aa8dSHong Zhang 1784c235aa8dSHong Zhang Input Parameter: 1785c235aa8dSHong Zhang . ts - the TS context obtained from TSCreate() 1786c235aa8dSHong Zhang 1787c235aa8dSHong Zhang Output Parameter: 1788abc2977eSBarry Smith + lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 1789abc2977eSBarry Smith - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 1790c235aa8dSHong Zhang 1791c235aa8dSHong Zhang Level: intermediate 1792c235aa8dSHong Zhang 1793c235aa8dSHong Zhang .seealso: TSGetTimeStep() 1794c235aa8dSHong Zhang 1795c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity 1796c235aa8dSHong Zhang @*/ 1797dfb21088SHong Zhang PetscErrorCode TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu) 1798c235aa8dSHong Zhang { 1799c235aa8dSHong Zhang PetscFunctionBegin; 1800c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1801abc2977eSBarry Smith if (numcost) *numcost = ts->numcost; 1802abc2977eSBarry Smith if (lambda) *lambda = ts->vecs_sensi; 1803abc2977eSBarry Smith if (mu) *mu = ts->vecs_sensip; 1804c235aa8dSHong Zhang PetscFunctionReturn(0); 1805c235aa8dSHong Zhang } 1806c235aa8dSHong Zhang 1807bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 18084a2ae208SSatish Balay #undef __FUNCT__ 1809bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1810d8e5e3e6SSatish Balay /*@ 1811bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1812d763cef2SBarry Smith 1813bdad233fSMatthew Knepley Not collective 1814d763cef2SBarry Smith 1815bdad233fSMatthew Knepley Input Parameters: 1816bdad233fSMatthew Knepley + ts - The TS 1817bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1818d763cef2SBarry Smith .vb 18190910c330SBarry Smith U_t - A U = 0 (linear) 18200910c330SBarry Smith U_t - A(t) U = 0 (linear) 18210910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 1822d763cef2SBarry Smith .ve 1823d763cef2SBarry Smith 1824d763cef2SBarry Smith Level: beginner 1825d763cef2SBarry Smith 1826bdad233fSMatthew Knepley .keywords: TS, problem type 1827bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1828d763cef2SBarry Smith @*/ 18297087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1830a7cc72afSBarry Smith { 18319e2a6581SJed Brown PetscErrorCode ierr; 18329e2a6581SJed Brown 1833d763cef2SBarry Smith PetscFunctionBegin; 18340700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1835bdad233fSMatthew Knepley ts->problem_type = type; 18369e2a6581SJed Brown if (type == TS_LINEAR) { 18379e2a6581SJed Brown SNES snes; 18389e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 18399e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 18409e2a6581SJed Brown } 1841d763cef2SBarry Smith PetscFunctionReturn(0); 1842d763cef2SBarry Smith } 1843d763cef2SBarry Smith 1844bdad233fSMatthew Knepley #undef __FUNCT__ 1845bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1846bdad233fSMatthew Knepley /*@C 1847bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1848bdad233fSMatthew Knepley 1849bdad233fSMatthew Knepley Not collective 1850bdad233fSMatthew Knepley 1851bdad233fSMatthew Knepley Input Parameter: 1852bdad233fSMatthew Knepley . ts - The TS 1853bdad233fSMatthew Knepley 1854bdad233fSMatthew Knepley Output Parameter: 1855bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1856bdad233fSMatthew Knepley .vb 1857089b2837SJed Brown M U_t = A U 1858089b2837SJed Brown M(t) U_t = A(t) U 1859b5abc632SBarry Smith F(t,U,U_t) 1860bdad233fSMatthew Knepley .ve 1861bdad233fSMatthew Knepley 1862bdad233fSMatthew Knepley Level: beginner 1863bdad233fSMatthew Knepley 1864bdad233fSMatthew Knepley .keywords: TS, problem type 1865bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1866bdad233fSMatthew Knepley @*/ 18677087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1868a7cc72afSBarry Smith { 1869bdad233fSMatthew Knepley PetscFunctionBegin; 18700700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 18714482741eSBarry Smith PetscValidIntPointer(type,2); 1872bdad233fSMatthew Knepley *type = ts->problem_type; 1873bdad233fSMatthew Knepley PetscFunctionReturn(0); 1874bdad233fSMatthew Knepley } 1875d763cef2SBarry Smith 18764a2ae208SSatish Balay #undef __FUNCT__ 18774a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1878d763cef2SBarry Smith /*@ 1879d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1880d763cef2SBarry Smith of a timestepper. 1881d763cef2SBarry Smith 1882d763cef2SBarry Smith Collective on TS 1883d763cef2SBarry Smith 1884d763cef2SBarry Smith Input Parameter: 1885d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1886d763cef2SBarry Smith 1887d763cef2SBarry Smith Notes: 1888d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1889d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1890d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1891d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1892d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1893d763cef2SBarry Smith 1894d763cef2SBarry Smith Level: advanced 1895d763cef2SBarry Smith 1896d763cef2SBarry Smith .keywords: TS, timestep, setup 1897d763cef2SBarry Smith 1898d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1899d763cef2SBarry Smith @*/ 19007087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1901d763cef2SBarry Smith { 1902dfbe8321SBarry Smith PetscErrorCode ierr; 19036c6b9e74SPeter Brune DM dm; 19046c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 1905d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 19066c6b9e74SPeter Brune TSIJacobian ijac; 19076c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1908d763cef2SBarry Smith 1909d763cef2SBarry Smith PetscFunctionBegin; 19100700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1911277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1912277b19d0SLisandro Dalcin 19132c18e0fdSBarry Smith ts->total_steps = 0; 19147adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 19159596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1916d763cef2SBarry Smith } 1917277b19d0SLisandro Dalcin 1918277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1919277b19d0SLisandro Dalcin 19201c3436cfSJed Brown 1921552698daSJed Brown ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 1922277b19d0SLisandro Dalcin 1923e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 1924e1244c69SJed Brown Mat Amat,Pmat; 1925e1244c69SJed Brown SNES snes; 1926e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1927e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 1928e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 1929e1244c69SJed Brown * have displaced the RHS matrix */ 1930e1244c69SJed Brown if (Amat == ts->Arhs) { 1931e1244c69SJed Brown ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 1932e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 1933e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 1934e1244c69SJed Brown } 1935e1244c69SJed Brown if (Pmat == ts->Brhs) { 1936e1244c69SJed Brown ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 1937e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 1938e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 1939e1244c69SJed Brown } 1940e1244c69SJed Brown } 1941277b19d0SLisandro Dalcin if (ts->ops->setup) { 1942000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1943277b19d0SLisandro Dalcin } 1944277b19d0SLisandro Dalcin 19456c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 19466c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 19476c6b9e74SPeter Brune */ 19486c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 19490298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 19506c6b9e74SPeter Brune if (!func) { 19516c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 19526c6b9e74SPeter Brune } 19536c6b9e74SPeter 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. 19546c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 19556c6b9e74SPeter Brune */ 19560298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 19570298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 19580298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 19596c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 19606c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 19616c6b9e74SPeter Brune } 1962277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1963277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1964277b19d0SLisandro Dalcin } 1965277b19d0SLisandro Dalcin 1966277b19d0SLisandro Dalcin #undef __FUNCT__ 1967f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp" 1968f6a906c0SBarry Smith /*@ 1969f6a906c0SBarry Smith TSAdjointSetUp - Sets up the internal data structures for the later use 1970f6a906c0SBarry Smith of an adjoint solver 1971f6a906c0SBarry Smith 1972f6a906c0SBarry Smith Collective on TS 1973f6a906c0SBarry Smith 1974f6a906c0SBarry Smith Input Parameter: 1975f6a906c0SBarry Smith . ts - the TS context obtained from TSCreate() 1976f6a906c0SBarry Smith 1977f6a906c0SBarry Smith Level: advanced 1978f6a906c0SBarry Smith 1979f6a906c0SBarry Smith .keywords: TS, timestep, setup 1980f6a906c0SBarry Smith 1981947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients() 1982f6a906c0SBarry Smith @*/ 1983f6a906c0SBarry Smith PetscErrorCode TSAdjointSetUp(TS ts) 1984f6a906c0SBarry Smith { 1985f6a906c0SBarry Smith PetscErrorCode ierr; 1986f6a906c0SBarry Smith 1987f6a906c0SBarry Smith PetscFunctionBegin; 1988f6a906c0SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1989f6a906c0SBarry Smith if (ts->adjointsetupcalled) PetscFunctionReturn(0); 1990dfb21088SHong Zhang if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first"); 1991d8151eeaSBarry Smith 1992947abb85SHong Zhang if (ts->vec_costintegral) { /* if there is integral in the cost function*/ 1993d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 1994d8151eeaSBarry Smith if (ts->vecs_sensip){ 1995d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 1996d8151eeaSBarry Smith } 1997947abb85SHong Zhang } 1998d8151eeaSBarry Smith 199942f2b339SBarry Smith if (ts->ops->adjointsetup) { 200042f2b339SBarry Smith ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr); 2001f6a906c0SBarry Smith } 2002f6a906c0SBarry Smith ts->adjointsetupcalled = PETSC_TRUE; 2003f6a906c0SBarry Smith PetscFunctionReturn(0); 2004f6a906c0SBarry Smith } 2005f6a906c0SBarry Smith 2006f6a906c0SBarry Smith #undef __FUNCT__ 2007277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 2008277b19d0SLisandro Dalcin /*@ 2009277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2010277b19d0SLisandro Dalcin 2011277b19d0SLisandro Dalcin Collective on TS 2012277b19d0SLisandro Dalcin 2013277b19d0SLisandro Dalcin Input Parameter: 2014277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2015277b19d0SLisandro Dalcin 2016277b19d0SLisandro Dalcin Level: beginner 2017277b19d0SLisandro Dalcin 2018277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 2019277b19d0SLisandro Dalcin 2020277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2021277b19d0SLisandro Dalcin @*/ 2022277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2023277b19d0SLisandro Dalcin { 2024277b19d0SLisandro Dalcin PetscErrorCode ierr; 2025277b19d0SLisandro Dalcin 2026277b19d0SLisandro Dalcin PetscFunctionBegin; 2027277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2028b18ea86cSHong Zhang 2029277b19d0SLisandro Dalcin if (ts->ops->reset) { 2030277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2031277b19d0SLisandro Dalcin } 2032277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2033e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2034bbd56ea5SKarl Rupp 20354e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 20364e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2037214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 20386bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2039e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2040e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 204138637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2042bbd56ea5SKarl Rupp 2043947abb85SHong Zhang if (ts->vec_costintegral) { 2044d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2045d8151eeaSBarry Smith if (ts->vecs_drdp){ 2046d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2047d8151eeaSBarry Smith } 2048947abb85SHong Zhang } 2049d8151eeaSBarry Smith ts->vecs_sensi = NULL; 2050d8151eeaSBarry Smith ts->vecs_sensip = NULL; 2051ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 20522c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 205336eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2054277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2055d763cef2SBarry Smith PetscFunctionReturn(0); 2056d763cef2SBarry Smith } 2057d763cef2SBarry Smith 20584a2ae208SSatish Balay #undef __FUNCT__ 20594a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 2060d8e5e3e6SSatish Balay /*@ 2061d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2062d763cef2SBarry Smith with TSCreate(). 2063d763cef2SBarry Smith 2064d763cef2SBarry Smith Collective on TS 2065d763cef2SBarry Smith 2066d763cef2SBarry Smith Input Parameter: 2067d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2068d763cef2SBarry Smith 2069d763cef2SBarry Smith Level: beginner 2070d763cef2SBarry Smith 2071d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2072d763cef2SBarry Smith 2073d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2074d763cef2SBarry Smith @*/ 20756bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2076d763cef2SBarry Smith { 20776849ba73SBarry Smith PetscErrorCode ierr; 2078d763cef2SBarry Smith 2079d763cef2SBarry Smith PetscFunctionBegin; 20806bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 20816bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 20826bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2083d763cef2SBarry Smith 20846bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2085277b19d0SLisandro Dalcin 2086e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2087e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 20886bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 20896d4c513bSLisandro Dalcin 2090bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2091bc952696SBarry Smith 209284df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 2093aeb4809dSShri Abhyankar if ((*ts)->event) { 2094aeb4809dSShri Abhyankar ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr); 2095aeb4809dSShri Abhyankar } 20966bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 20976bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 20986bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 20990dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 21006d4c513bSLisandro Dalcin 2101a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2102d763cef2SBarry Smith PetscFunctionReturn(0); 2103d763cef2SBarry Smith } 2104d763cef2SBarry Smith 21054a2ae208SSatish Balay #undef __FUNCT__ 21064a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 2107d8e5e3e6SSatish Balay /*@ 2108d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2109d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2110d763cef2SBarry Smith 2111d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2112d763cef2SBarry Smith 2113d763cef2SBarry Smith Input Parameter: 2114d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2115d763cef2SBarry Smith 2116d763cef2SBarry Smith Output Parameter: 2117d763cef2SBarry Smith . snes - the nonlinear solver context 2118d763cef2SBarry Smith 2119d763cef2SBarry Smith Notes: 2120d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2121d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 212294b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2123d763cef2SBarry Smith 2124d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 21250298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2126d763cef2SBarry Smith 2127d763cef2SBarry Smith Level: beginner 2128d763cef2SBarry Smith 2129d763cef2SBarry Smith .keywords: timestep, get, SNES 2130d763cef2SBarry Smith @*/ 21317087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2132d763cef2SBarry Smith { 2133d372ba47SLisandro Dalcin PetscErrorCode ierr; 2134d372ba47SLisandro Dalcin 2135d763cef2SBarry Smith PetscFunctionBegin; 21360700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 21374482741eSBarry Smith PetscValidPointer(snes,2); 2138d372ba47SLisandro Dalcin if (!ts->snes) { 2139ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 21400298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 21413bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2142d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2143496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 21449e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 21459e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 21469e2a6581SJed Brown } 2147d372ba47SLisandro Dalcin } 2148d763cef2SBarry Smith *snes = ts->snes; 2149d763cef2SBarry Smith PetscFunctionReturn(0); 2150d763cef2SBarry Smith } 2151d763cef2SBarry Smith 21524a2ae208SSatish Balay #undef __FUNCT__ 2153deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES" 2154deb2cd25SJed Brown /*@ 2155deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2156deb2cd25SJed Brown 2157deb2cd25SJed Brown Collective 2158deb2cd25SJed Brown 2159deb2cd25SJed Brown Input Parameter: 2160deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2161deb2cd25SJed Brown - snes - the nonlinear solver context 2162deb2cd25SJed Brown 2163deb2cd25SJed Brown Notes: 2164deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2165deb2cd25SJed Brown 2166deb2cd25SJed Brown Level: developer 2167deb2cd25SJed Brown 2168deb2cd25SJed Brown .keywords: timestep, set, SNES 2169deb2cd25SJed Brown @*/ 2170deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2171deb2cd25SJed Brown { 2172deb2cd25SJed Brown PetscErrorCode ierr; 2173d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2174deb2cd25SJed Brown 2175deb2cd25SJed Brown PetscFunctionBegin; 2176deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2177deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2178deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2179deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2180bbd56ea5SKarl Rupp 2181deb2cd25SJed Brown ts->snes = snes; 2182bbd56ea5SKarl Rupp 21830298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 21840298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2185740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 21860298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2187740132f1SEmil Constantinescu } 2188deb2cd25SJed Brown PetscFunctionReturn(0); 2189deb2cd25SJed Brown } 2190deb2cd25SJed Brown 2191deb2cd25SJed Brown #undef __FUNCT__ 219294b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 2193d8e5e3e6SSatish Balay /*@ 219494b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2195d763cef2SBarry Smith a TS (timestepper) context. 2196d763cef2SBarry Smith 219794b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2198d763cef2SBarry Smith 2199d763cef2SBarry Smith Input Parameter: 2200d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2201d763cef2SBarry Smith 2202d763cef2SBarry Smith Output Parameter: 220394b7f48cSBarry Smith . ksp - the nonlinear solver context 2204d763cef2SBarry Smith 2205d763cef2SBarry Smith Notes: 220694b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2207d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2208d763cef2SBarry Smith KSP and PC contexts as well. 2209d763cef2SBarry Smith 221094b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 22110298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2212d763cef2SBarry Smith 2213d763cef2SBarry Smith Level: beginner 2214d763cef2SBarry Smith 221594b7f48cSBarry Smith .keywords: timestep, get, KSP 2216d763cef2SBarry Smith @*/ 22177087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2218d763cef2SBarry Smith { 2219d372ba47SLisandro Dalcin PetscErrorCode ierr; 2220089b2837SJed Brown SNES snes; 2221d372ba47SLisandro Dalcin 2222d763cef2SBarry Smith PetscFunctionBegin; 22230700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22244482741eSBarry Smith PetscValidPointer(ksp,2); 222517186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2226e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2227089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2228089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2229d763cef2SBarry Smith PetscFunctionReturn(0); 2230d763cef2SBarry Smith } 2231d763cef2SBarry Smith 2232d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2233d763cef2SBarry Smith 22344a2ae208SSatish Balay #undef __FUNCT__ 2235adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 2236adb62b0dSMatthew Knepley /*@ 2237adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2238adb62b0dSMatthew Knepley maximum time for iteration. 2239adb62b0dSMatthew Knepley 22403f9fe445SBarry Smith Not Collective 2241adb62b0dSMatthew Knepley 2242adb62b0dSMatthew Knepley Input Parameters: 2243adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 22440298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 22450298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2246adb62b0dSMatthew Knepley 2247adb62b0dSMatthew Knepley Level: intermediate 2248adb62b0dSMatthew Knepley 2249adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2250adb62b0dSMatthew Knepley @*/ 22517087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2252adb62b0dSMatthew Knepley { 2253adb62b0dSMatthew Knepley PetscFunctionBegin; 22540700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2255abc0a331SBarry Smith if (maxsteps) { 22564482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2257adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2258adb62b0dSMatthew Knepley } 2259abc0a331SBarry Smith if (maxtime) { 22604482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2261adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2262adb62b0dSMatthew Knepley } 2263adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2264adb62b0dSMatthew Knepley } 2265adb62b0dSMatthew Knepley 2266adb62b0dSMatthew Knepley #undef __FUNCT__ 22674a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 2268d763cef2SBarry Smith /*@ 2269d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2270d763cef2SBarry Smith maximum time for iteration. 2271d763cef2SBarry Smith 22723f9fe445SBarry Smith Logically Collective on TS 2273d763cef2SBarry Smith 2274d763cef2SBarry Smith Input Parameters: 2275d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2276d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2277d763cef2SBarry Smith - maxtime - final time to iterate to 2278d763cef2SBarry Smith 2279d763cef2SBarry Smith Options Database Keys: 2280d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 22813bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2282d763cef2SBarry Smith 2283d763cef2SBarry Smith Notes: 2284d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2285d763cef2SBarry Smith 2286d763cef2SBarry Smith Level: intermediate 2287d763cef2SBarry Smith 2288d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2289a43b19c4SJed Brown 2290a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2291d763cef2SBarry Smith @*/ 22927087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2293d763cef2SBarry Smith { 2294d763cef2SBarry Smith PetscFunctionBegin; 22950700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2296c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2297c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 229839b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 229939b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2300d763cef2SBarry Smith PetscFunctionReturn(0); 2301d763cef2SBarry Smith } 2302d763cef2SBarry Smith 23034a2ae208SSatish Balay #undef __FUNCT__ 23044a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 2305d763cef2SBarry Smith /*@ 2306d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2307d763cef2SBarry Smith for use by the TS routines. 2308d763cef2SBarry Smith 23093f9fe445SBarry Smith Logically Collective on TS and Vec 2310d763cef2SBarry Smith 2311d763cef2SBarry Smith Input Parameters: 2312d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 23130910c330SBarry Smith - u - the solution vector 2314d763cef2SBarry Smith 2315d763cef2SBarry Smith Level: beginner 2316d763cef2SBarry Smith 2317d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2318d763cef2SBarry Smith @*/ 23190910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2320d763cef2SBarry Smith { 23218737fe31SLisandro Dalcin PetscErrorCode ierr; 2322496e6a7aSJed Brown DM dm; 23238737fe31SLisandro Dalcin 2324d763cef2SBarry Smith PetscFunctionBegin; 23250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 23260910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 23270910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 23286bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2329bbd56ea5SKarl Rupp 23300910c330SBarry Smith ts->vec_sol = u; 2331bbd56ea5SKarl Rupp 2332496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 23330910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2334d763cef2SBarry Smith PetscFunctionReturn(0); 2335d763cef2SBarry Smith } 2336d763cef2SBarry Smith 2337e74ef692SMatthew Knepley #undef __FUNCT__ 233873b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps" 233973b18844SBarry Smith /*@ 234073b18844SBarry Smith TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time 234173b18844SBarry Smith 234273b18844SBarry Smith Logically Collective on TS 234373b18844SBarry Smith 234473b18844SBarry Smith Input Parameters: 234573b18844SBarry Smith + ts - the TS context obtained from TSCreate() 234673b18844SBarry Smith . steps - number of steps to use 234773b18844SBarry Smith 234873b18844SBarry Smith Level: intermediate 234973b18844SBarry Smith 235073b18844SBarry Smith Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this 235173b18844SBarry Smith so as to integrate back to less than the original timestep 235273b18844SBarry Smith 235373b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations 235473b18844SBarry Smith 235573b18844SBarry Smith .seealso: TSSetExactFinalTime() 235673b18844SBarry Smith @*/ 235773b18844SBarry Smith PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps) 235873b18844SBarry Smith { 235973b18844SBarry Smith PetscFunctionBegin; 236073b18844SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 236173b18844SBarry Smith PetscValidLogicalCollectiveInt(ts,steps,2); 236273b18844SBarry Smith if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps"); 236373b18844SBarry 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"); 236473b18844SBarry Smith ts->adjoint_max_steps = steps; 236573b18844SBarry Smith PetscFunctionReturn(0); 236673b18844SBarry Smith } 236773b18844SBarry Smith 236873b18844SBarry Smith #undef __FUNCT__ 2369dfb21088SHong Zhang #define __FUNCT__ "TSSetCostGradients" 2370c235aa8dSHong Zhang /*@ 2371dfb21088SHong Zhang TSSetCostGradients - Sets the initial value of the gradients of the cost function w.r.t. initial conditions and w.r.t. the problem parameters 23720cf82b59SBarry Smith for use by the TSAdjoint routines. 2373c235aa8dSHong Zhang 2374c235aa8dSHong Zhang Logically Collective on TS and Vec 2375c235aa8dSHong Zhang 2376c235aa8dSHong Zhang Input Parameters: 2377c235aa8dSHong Zhang + ts - the TS context obtained from TSCreate() 2378abc2977eSBarry 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 2379abc2977eSBarry Smith - mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters 2380c235aa8dSHong Zhang 2381c235aa8dSHong Zhang Level: beginner 2382c235aa8dSHong Zhang 2383abc2977eSBarry Smith Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime mu_i = df/dp|finaltime 23840cf82b59SBarry Smith 2385c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions 2386c235aa8dSHong Zhang @*/ 2387dfb21088SHong Zhang PetscErrorCode TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu) 2388c235aa8dSHong Zhang { 2389c235aa8dSHong Zhang PetscFunctionBegin; 2390c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2391abc2977eSBarry Smith PetscValidPointer(lambda,2); 2392abc2977eSBarry Smith ts->vecs_sensi = lambda; 2393abc2977eSBarry Smith ts->vecs_sensip = mu; 2394dfb21088SHong Zhang if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostIntegrand"); 2395abc2977eSBarry Smith ts->numcost = numcost; 2396ad8e2604SHong Zhang PetscFunctionReturn(0); 2397ad8e2604SHong Zhang } 2398ad8e2604SHong Zhang 2399ad8e2604SHong Zhang #undef __FUNCT__ 24005bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian" 2401ad8e2604SHong Zhang /*@C 24020cf82b59SBarry 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. 2403ad8e2604SHong Zhang 2404ad8e2604SHong Zhang Logically Collective on TS 2405ad8e2604SHong Zhang 2406ad8e2604SHong Zhang Input Parameters: 2407ad8e2604SHong Zhang + ts - The TS context obtained from TSCreate() 2408ad8e2604SHong Zhang - func - The function 2409ad8e2604SHong Zhang 2410ad8e2604SHong Zhang Calling sequence of func: 24110cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx); 241205755b9cSHong Zhang + t - current timestep 24130cf82b59SBarry Smith . y - input vector (current ODE solution) 241405755b9cSHong Zhang . A - output matrix 241505755b9cSHong Zhang - ctx - [optional] user-defined function context 2416ad8e2604SHong Zhang 2417ad8e2604SHong Zhang Level: intermediate 2418ad8e2604SHong Zhang 24190cf82b59SBarry 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 24200cf82b59SBarry Smith 2421ad8e2604SHong Zhang .keywords: TS, sensitivity 2422ad8e2604SHong Zhang .seealso: 2423ad8e2604SHong Zhang @*/ 24245bf8c567SBarry Smith PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 2425ad8e2604SHong Zhang { 2426ad8e2604SHong Zhang PetscErrorCode ierr; 2427ad8e2604SHong Zhang 2428ad8e2604SHong Zhang PetscFunctionBegin; 2429ad8e2604SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2430ad8e2604SHong Zhang if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 2431ad8e2604SHong Zhang 2432ad8e2604SHong Zhang ts->rhsjacobianp = func; 2433ad8e2604SHong Zhang ts->rhsjacobianpctx = ctx; 2434ad8e2604SHong Zhang if(Amat) { 2435ad8e2604SHong Zhang ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 2436ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2437ad8e2604SHong Zhang ts->Jacp = Amat; 2438ad8e2604SHong Zhang } 2439ad8e2604SHong Zhang PetscFunctionReturn(0); 2440ad8e2604SHong Zhang } 2441ad8e2604SHong Zhang 2442ad8e2604SHong Zhang #undef __FUNCT__ 24435bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian" 24440cf82b59SBarry Smith /*@C 24455bf8c567SBarry Smith TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function. 2446ad8e2604SHong Zhang 2447ad8e2604SHong Zhang Collective on TS 2448ad8e2604SHong Zhang 2449ad8e2604SHong Zhang Input Parameters: 2450ad8e2604SHong Zhang . ts - The TS context obtained from TSCreate() 2451ad8e2604SHong Zhang 2452ad8e2604SHong Zhang Level: developer 2453ad8e2604SHong Zhang 2454ad8e2604SHong Zhang .keywords: TS, sensitivity 24555bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian() 2456ad8e2604SHong Zhang @*/ 24575bf8c567SBarry Smith PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat) 2458ad8e2604SHong Zhang { 2459ad8e2604SHong Zhang PetscErrorCode ierr; 2460ad8e2604SHong Zhang 2461ad8e2604SHong Zhang PetscFunctionBegin; 2462ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2463ad8e2604SHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 2464ad8e2604SHong Zhang PetscValidPointer(Amat,4); 2465ad8e2604SHong Zhang 2466ad8e2604SHong Zhang PetscStackPush("TS user JacobianP function for sensitivity analysis"); 2467ad8e2604SHong Zhang ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 2468ad8e2604SHong Zhang PetscStackPop; 2469c235aa8dSHong Zhang PetscFunctionReturn(0); 2470c235aa8dSHong Zhang } 2471c235aa8dSHong Zhang 2472c235aa8dSHong Zhang #undef __FUNCT__ 2473dfb21088SHong Zhang #define __FUNCT__ "TSSetCostIntegrand" 24746fd0887fSHong Zhang /*@C 2475dfb21088SHong Zhang TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions 24766fd0887fSHong Zhang 24776fd0887fSHong Zhang Logically Collective on TS 24786fd0887fSHong Zhang 24796fd0887fSHong Zhang Input Parameters: 24806fd0887fSHong Zhang + ts - the TS context obtained from TSCreate() 2481abc2977eSBarry Smith . numcost - number of gradients to be computed, this is the number of cost functions 24820cf82b59SBarry Smith . rf - routine for evaluating the integrand function 2483b612ec54SBarry Smith . drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y 2484b612ec54SBarry Smith . drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p 2485b612ec54SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 24866fd0887fSHong Zhang 24870cf82b59SBarry Smith Calling sequence of rf: 24880cf82b59SBarry Smith $ rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx); 24896fd0887fSHong Zhang 24906fd0887fSHong Zhang + t - current timestep 24910cf82b59SBarry Smith . y - input vector 24920cf82b59SBarry Smith . f - function result; one vector entry for each cost function 24936fd0887fSHong Zhang - ctx - [optional] user-defined function context 24946fd0887fSHong Zhang 2495b612ec54SBarry Smith Calling sequence of drdyf: 24960cf82b59SBarry Smith $ PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx); 2497b612ec54SBarry Smith 2498b612ec54SBarry Smith Calling sequence of drdpf: 24990cf82b59SBarry Smith $ PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx); 2500b612ec54SBarry Smith 2501b612ec54SBarry Smith Level: intermediate 25026fd0887fSHong Zhang 2503abc2977eSBarry Smith Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions 25040cf82b59SBarry Smith 25056fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 25066fd0887fSHong Zhang 2507dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients() 25086fd0887fSHong Zhang @*/ 2509dfb21088SHong Zhang PetscErrorCode TSSetCostIntegrand(TS ts,PetscInt numcost, PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*), 2510d8151eeaSBarry Smith PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*), 2511d8151eeaSBarry Smith PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),void *ctx) 25126fd0887fSHong Zhang { 25136fd0887fSHong Zhang PetscErrorCode ierr; 25146fd0887fSHong Zhang 25156fd0887fSHong Zhang PetscFunctionBegin; 25166fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2517dfb21088SHong Zhang if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostGradients()"); 2518c72ed514SHong Zhang if (!ts->numcost) ts->numcost=numcost; 25196fd0887fSHong Zhang 2520abc2977eSBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr); 25212c39e106SBarry Smith ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr); 25220cf82b59SBarry Smith ts->costintegrand = rf; 252305755b9cSHong Zhang ts->costintegrandctx = ctx; 2524b612ec54SBarry Smith ts->drdyfunction = drdyf; 2525b612ec54SBarry Smith ts->drdpfunction = drdpf; 25266fd0887fSHong Zhang PetscFunctionReturn(0); 25276fd0887fSHong Zhang } 25286fd0887fSHong Zhang 25296fd0887fSHong Zhang #undef __FUNCT__ 2530dfb21088SHong Zhang #define __FUNCT__ "TSGetCostIntegral" 253136eaed60SHong Zhang /*@ 2532dfb21088SHong Zhang TSGetCostIntegral - Returns the values of the integral term in the cost functions. 253336eaed60SHong Zhang It is valid to call the routine after a backward run. 253436eaed60SHong Zhang 253536eaed60SHong Zhang Not Collective 253636eaed60SHong Zhang 253736eaed60SHong Zhang Input Parameter: 253836eaed60SHong Zhang . ts - the TS context obtained from TSCreate() 253936eaed60SHong Zhang 254036eaed60SHong Zhang Output Parameter: 25412c39e106SBarry Smith . v - the vector containing the integrals for each cost function 254236eaed60SHong Zhang 254336eaed60SHong Zhang Level: intermediate 254436eaed60SHong Zhang 2545dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 254636eaed60SHong Zhang 254736eaed60SHong Zhang .keywords: TS, sensitivity analysis 254836eaed60SHong Zhang @*/ 2549dfb21088SHong Zhang PetscErrorCode TSGetCostIntegral(TS ts,Vec *v) 255036eaed60SHong Zhang { 255136eaed60SHong Zhang PetscFunctionBegin; 255236eaed60SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 255336eaed60SHong Zhang PetscValidPointer(v,2); 25542c39e106SBarry Smith *v = ts->vec_costintegral; 255536eaed60SHong Zhang PetscFunctionReturn(0); 255636eaed60SHong Zhang } 255736eaed60SHong Zhang 255836eaed60SHong Zhang #undef __FUNCT__ 2559d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand" 25606fd0887fSHong Zhang /*@ 25612c39e106SBarry Smith TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions. 25626fd0887fSHong Zhang 25636fd0887fSHong Zhang Input Parameters: 25646fd0887fSHong Zhang + ts - the TS context 25656fd0887fSHong Zhang . t - current time 25660cf82b59SBarry Smith - y - state vector, i.e. current solution 25676fd0887fSHong Zhang 25686fd0887fSHong Zhang Output Parameter: 2569abc2977eSBarry Smith . q - vector of size numcost to hold the outputs 25706fd0887fSHong Zhang 25716fd0887fSHong Zhang Note: 25726fd0887fSHong Zhang Most users should not need to explicitly call this routine, as it 25736fd0887fSHong Zhang is used internally within the sensitivity analysis context. 25746fd0887fSHong Zhang 25756fd0887fSHong Zhang Level: developer 25766fd0887fSHong Zhang 25776fd0887fSHong Zhang .keywords: TS, compute 25786fd0887fSHong Zhang 2579dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 25806fd0887fSHong Zhang @*/ 25810cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q) 25826fd0887fSHong Zhang { 25836fd0887fSHong Zhang PetscErrorCode ierr; 25846fd0887fSHong Zhang 25856fd0887fSHong Zhang PetscFunctionBegin; 25866fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25870cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 25886fd0887fSHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,4); 25896fd0887fSHong Zhang 25900cf82b59SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 2591e4680132SHong Zhang if (ts->costintegrand) { 2592e4680132SHong Zhang PetscStackPush("TS user integrand in the cost function"); 25930cf82b59SBarry Smith ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr); 25946fd0887fSHong Zhang PetscStackPop; 25956fd0887fSHong Zhang } else { 25966fd0887fSHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 25976fd0887fSHong Zhang } 25986fd0887fSHong Zhang 25990cf82b59SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 26006fd0887fSHong Zhang PetscFunctionReturn(0); 26016fd0887fSHong Zhang } 26026fd0887fSHong Zhang 26036fd0887fSHong Zhang #undef __FUNCT__ 2604d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction" 260505755b9cSHong Zhang /*@ 2606d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 260705755b9cSHong Zhang 260805755b9cSHong Zhang Collective on TS 260905755b9cSHong Zhang 261005755b9cSHong Zhang Input Parameters: 261105755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 261205755b9cSHong Zhang 261305755b9cSHong Zhang Notes: 2614d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 261505755b9cSHong Zhang so most users would not generally call this routine themselves. 261605755b9cSHong Zhang 261705755b9cSHong Zhang Level: developer 261805755b9cSHong Zhang 261905755b9cSHong Zhang .keywords: TS, sensitivity 2620d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction() 262105755b9cSHong Zhang @*/ 26220cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy) 262305755b9cSHong Zhang { 262405755b9cSHong Zhang PetscErrorCode ierr; 262505755b9cSHong Zhang 262605755b9cSHong Zhang PetscFunctionBegin; 262705755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26280cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 262905755b9cSHong Zhang 263005755b9cSHong Zhang PetscStackPush("TS user DRDY function for sensitivity analysis"); 26310cf82b59SBarry Smith ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr); 263205755b9cSHong Zhang PetscStackPop; 263305755b9cSHong Zhang PetscFunctionReturn(0); 263405755b9cSHong Zhang } 263505755b9cSHong Zhang 263605755b9cSHong Zhang #undef __FUNCT__ 2637d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction" 263805755b9cSHong Zhang /*@ 2639d4aa0a58SBarry Smith TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 264005755b9cSHong Zhang 264105755b9cSHong Zhang Collective on TS 264205755b9cSHong Zhang 264305755b9cSHong Zhang Input Parameters: 264405755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 264505755b9cSHong Zhang 264605755b9cSHong Zhang Notes: 264705755b9cSHong Zhang TSDRDPFunction() is typically used for sensitivity implementation, 264805755b9cSHong Zhang so most users would not generally call this routine themselves. 264905755b9cSHong Zhang 265005755b9cSHong Zhang Level: developer 265105755b9cSHong Zhang 265205755b9cSHong Zhang .keywords: TS, sensitivity 2653d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction() 265405755b9cSHong Zhang @*/ 26550cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp) 265605755b9cSHong Zhang { 265705755b9cSHong Zhang PetscErrorCode ierr; 265805755b9cSHong Zhang 265905755b9cSHong Zhang PetscFunctionBegin; 266005755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26610cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 266205755b9cSHong Zhang 266305755b9cSHong Zhang PetscStackPush("TS user DRDP function for sensitivity analysis"); 26640cf82b59SBarry Smith ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr); 266505755b9cSHong Zhang PetscStackPop; 266605755b9cSHong Zhang PetscFunctionReturn(0); 266705755b9cSHong Zhang } 266805755b9cSHong Zhang 266905755b9cSHong Zhang #undef __FUNCT__ 2670e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 2671ac226902SBarry Smith /*@C 2672000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 26733f2090d5SJed Brown called once at the beginning of each time step. 2674000e7ae3SMatthew Knepley 26753f9fe445SBarry Smith Logically Collective on TS 2676000e7ae3SMatthew Knepley 2677000e7ae3SMatthew Knepley Input Parameters: 2678000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2679000e7ae3SMatthew Knepley - func - The function 2680000e7ae3SMatthew Knepley 2681000e7ae3SMatthew Knepley Calling sequence of func: 2682000e7ae3SMatthew Knepley . func (TS ts); 2683000e7ae3SMatthew Knepley 2684000e7ae3SMatthew Knepley Level: intermediate 2685000e7ae3SMatthew Knepley 2686b8123daeSJed Brown Note: 2687b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 2688b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 2689b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 2690b8123daeSJed Brown 2691000e7ae3SMatthew Knepley .keywords: TS, timestep 26929be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 2693000e7ae3SMatthew Knepley @*/ 26947087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 2695000e7ae3SMatthew Knepley { 2696000e7ae3SMatthew Knepley PetscFunctionBegin; 26970700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2698ae60f76fSBarry Smith ts->prestep = func; 2699000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2700000e7ae3SMatthew Knepley } 2701000e7ae3SMatthew Knepley 2702e74ef692SMatthew Knepley #undef __FUNCT__ 27033f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 270409ee8438SJed Brown /*@ 27053f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 27063f2090d5SJed Brown 27073f2090d5SJed Brown Collective on TS 27083f2090d5SJed Brown 27093f2090d5SJed Brown Input Parameters: 27103f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 27113f2090d5SJed Brown 27123f2090d5SJed Brown Notes: 27133f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 27143f2090d5SJed Brown so most users would not generally call this routine themselves. 27153f2090d5SJed Brown 27163f2090d5SJed Brown Level: developer 27173f2090d5SJed Brown 27183f2090d5SJed Brown .keywords: TS, timestep 27199be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 27203f2090d5SJed Brown @*/ 27217087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 27223f2090d5SJed Brown { 27233f2090d5SJed Brown PetscErrorCode ierr; 27243f2090d5SJed Brown 27253f2090d5SJed Brown PetscFunctionBegin; 27260700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2727ae60f76fSBarry Smith if (ts->prestep) { 2728ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 2729312ce896SJed Brown } 27303f2090d5SJed Brown PetscFunctionReturn(0); 27313f2090d5SJed Brown } 27323f2090d5SJed Brown 27333f2090d5SJed Brown #undef __FUNCT__ 2734b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 2735b8123daeSJed Brown /*@C 2736b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 2737b8123daeSJed Brown called once at the beginning of each stage. 2738b8123daeSJed Brown 2739b8123daeSJed Brown Logically Collective on TS 2740b8123daeSJed Brown 2741b8123daeSJed Brown Input Parameters: 2742b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 2743b8123daeSJed Brown - func - The function 2744b8123daeSJed Brown 2745b8123daeSJed Brown Calling sequence of func: 2746b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 2747b8123daeSJed Brown 2748b8123daeSJed Brown Level: intermediate 2749b8123daeSJed Brown 2750b8123daeSJed Brown Note: 2751b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 2752b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 2753b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 2754b8123daeSJed Brown 2755b8123daeSJed Brown .keywords: TS, timestep 27569be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 2757b8123daeSJed Brown @*/ 2758b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 2759b8123daeSJed Brown { 2760b8123daeSJed Brown PetscFunctionBegin; 2761b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2762ae60f76fSBarry Smith ts->prestage = func; 2763b8123daeSJed Brown PetscFunctionReturn(0); 2764b8123daeSJed Brown } 2765b8123daeSJed Brown 2766b8123daeSJed Brown #undef __FUNCT__ 27679be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage" 27689be3e283SDebojyoti Ghosh /*@C 27699be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 27709be3e283SDebojyoti Ghosh called once at the end of each stage. 27719be3e283SDebojyoti Ghosh 27729be3e283SDebojyoti Ghosh Logically Collective on TS 27739be3e283SDebojyoti Ghosh 27749be3e283SDebojyoti Ghosh Input Parameters: 27759be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 27769be3e283SDebojyoti Ghosh - func - The function 27779be3e283SDebojyoti Ghosh 27789be3e283SDebojyoti Ghosh Calling sequence of func: 27799be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 27809be3e283SDebojyoti Ghosh 27819be3e283SDebojyoti Ghosh Level: intermediate 27829be3e283SDebojyoti Ghosh 27839be3e283SDebojyoti Ghosh Note: 27849be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 27859be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 27869be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 27879be3e283SDebojyoti Ghosh 27889be3e283SDebojyoti Ghosh .keywords: TS, timestep 27899be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 27909be3e283SDebojyoti Ghosh @*/ 27919be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 27929be3e283SDebojyoti Ghosh { 27939be3e283SDebojyoti Ghosh PetscFunctionBegin; 27949be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 27959be3e283SDebojyoti Ghosh ts->poststage = func; 27969be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 27979be3e283SDebojyoti Ghosh } 27989be3e283SDebojyoti Ghosh 27999be3e283SDebojyoti Ghosh #undef __FUNCT__ 2800b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 2801b8123daeSJed Brown /*@ 2802b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 2803b8123daeSJed Brown 2804b8123daeSJed Brown Collective on TS 2805b8123daeSJed Brown 2806b8123daeSJed Brown Input Parameters: 2807b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 28089be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 2809b8123daeSJed Brown 2810b8123daeSJed Brown Notes: 2811b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 2812b8123daeSJed Brown most users would not generally call this routine themselves. 2813b8123daeSJed Brown 2814b8123daeSJed Brown Level: developer 2815b8123daeSJed Brown 2816b8123daeSJed Brown .keywords: TS, timestep 28179be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 2818b8123daeSJed Brown @*/ 2819b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 2820b8123daeSJed Brown { 2821b8123daeSJed Brown PetscErrorCode ierr; 2822b8123daeSJed Brown 2823b8123daeSJed Brown PetscFunctionBegin; 2824b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2825ae60f76fSBarry Smith if (ts->prestage) { 2826ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 2827b8123daeSJed Brown } 2828b8123daeSJed Brown PetscFunctionReturn(0); 2829b8123daeSJed Brown } 2830b8123daeSJed Brown 2831b8123daeSJed Brown #undef __FUNCT__ 28329be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage" 28339be3e283SDebojyoti Ghosh /*@ 28349be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 28359be3e283SDebojyoti Ghosh 28369be3e283SDebojyoti Ghosh Collective on TS 28379be3e283SDebojyoti Ghosh 28389be3e283SDebojyoti Ghosh Input Parameters: 28399be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 28409be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 28419be3e283SDebojyoti Ghosh stageindex - Stage number 28429be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 28439be3e283SDebojyoti Ghosh of stages) with the stage solutions 28449be3e283SDebojyoti Ghosh 28459be3e283SDebojyoti Ghosh Notes: 28469be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 28479be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 28489be3e283SDebojyoti Ghosh 28499be3e283SDebojyoti Ghosh Level: developer 28509be3e283SDebojyoti Ghosh 28519be3e283SDebojyoti Ghosh .keywords: TS, timestep 28529be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 28539be3e283SDebojyoti Ghosh @*/ 28549be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 28559be3e283SDebojyoti Ghosh { 28569be3e283SDebojyoti Ghosh PetscErrorCode ierr; 28579be3e283SDebojyoti Ghosh 28589be3e283SDebojyoti Ghosh PetscFunctionBegin; 28599be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28604beae5d8SLisandro Dalcin if (ts->poststage) { 28619be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 28629be3e283SDebojyoti Ghosh } 28639be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 28649be3e283SDebojyoti Ghosh } 28659be3e283SDebojyoti Ghosh 28669be3e283SDebojyoti Ghosh #undef __FUNCT__ 2867e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 2868ac226902SBarry Smith /*@C 2869000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 28703f2090d5SJed Brown called once at the end of each time step. 2871000e7ae3SMatthew Knepley 28723f9fe445SBarry Smith Logically Collective on TS 2873000e7ae3SMatthew Knepley 2874000e7ae3SMatthew Knepley Input Parameters: 2875000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2876000e7ae3SMatthew Knepley - func - The function 2877000e7ae3SMatthew Knepley 2878000e7ae3SMatthew Knepley Calling sequence of func: 2879b8123daeSJed Brown $ func (TS ts); 2880000e7ae3SMatthew Knepley 2881000e7ae3SMatthew Knepley Level: intermediate 2882000e7ae3SMatthew Knepley 2883000e7ae3SMatthew Knepley .keywords: TS, timestep 2884b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 2885000e7ae3SMatthew Knepley @*/ 28867087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 2887000e7ae3SMatthew Knepley { 2888000e7ae3SMatthew Knepley PetscFunctionBegin; 28890700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2890ae60f76fSBarry Smith ts->poststep = func; 2891000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2892000e7ae3SMatthew Knepley } 2893000e7ae3SMatthew Knepley 2894e74ef692SMatthew Knepley #undef __FUNCT__ 28953f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 289609ee8438SJed Brown /*@ 28973f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 28983f2090d5SJed Brown 28993f2090d5SJed Brown Collective on TS 29003f2090d5SJed Brown 29013f2090d5SJed Brown Input Parameters: 29023f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 29033f2090d5SJed Brown 29043f2090d5SJed Brown Notes: 29053f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 29063f2090d5SJed Brown so most users would not generally call this routine themselves. 29073f2090d5SJed Brown 29083f2090d5SJed Brown Level: developer 29093f2090d5SJed Brown 29103f2090d5SJed Brown .keywords: TS, timestep 29113f2090d5SJed Brown @*/ 29127087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 29133f2090d5SJed Brown { 29143f2090d5SJed Brown PetscErrorCode ierr; 29153f2090d5SJed Brown 29163f2090d5SJed Brown PetscFunctionBegin; 29170700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2918ae60f76fSBarry Smith if (ts->poststep) { 2919ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 292072ac3e02SJed Brown } 29213f2090d5SJed Brown PetscFunctionReturn(0); 29223f2090d5SJed Brown } 29233f2090d5SJed Brown 2924d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 2925d763cef2SBarry Smith 29264a2ae208SSatish Balay #undef __FUNCT__ 2927a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 2928d763cef2SBarry Smith /*@C 2929a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 2930d763cef2SBarry Smith timestep to display the iteration's progress. 2931d763cef2SBarry Smith 29323f9fe445SBarry Smith Logically Collective on TS 2933d763cef2SBarry Smith 2934d763cef2SBarry Smith Input Parameters: 2935d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2936e213d8f1SJed Brown . monitor - monitoring routine 2937329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 29380298fd71SBarry Smith monitor routine (use NULL if no context is desired) 2939b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 29400298fd71SBarry Smith (may be NULL) 2941d763cef2SBarry Smith 2942e213d8f1SJed Brown Calling sequence of monitor: 29430910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 2944d763cef2SBarry Smith 2945d763cef2SBarry Smith + ts - the TS context 294688c05cc5SBarry 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 294788c05cc5SBarry Smith been interpolated to) 29481f06c33eSBarry Smith . time - current time 29490910c330SBarry Smith . u - current iterate 2950d763cef2SBarry Smith - mctx - [optional] monitoring context 2951d763cef2SBarry Smith 2952d763cef2SBarry Smith Notes: 2953d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 2954d763cef2SBarry Smith already has been loaded. 2955d763cef2SBarry Smith 2956025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 2957025f1a04SBarry Smith 2958d763cef2SBarry Smith Level: intermediate 2959d763cef2SBarry Smith 2960d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2961d763cef2SBarry Smith 2962a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 2963d763cef2SBarry Smith @*/ 2964c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 2965d763cef2SBarry Smith { 2966d763cef2SBarry Smith PetscFunctionBegin; 29670700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 296817186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2969d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 29708704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 2971d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 2972d763cef2SBarry Smith PetscFunctionReturn(0); 2973d763cef2SBarry Smith } 2974d763cef2SBarry Smith 29754a2ae208SSatish Balay #undef __FUNCT__ 2976a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 2977d763cef2SBarry Smith /*@C 2978a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 2979d763cef2SBarry Smith 29803f9fe445SBarry Smith Logically Collective on TS 2981d763cef2SBarry Smith 2982d763cef2SBarry Smith Input Parameters: 2983d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2984d763cef2SBarry Smith 2985d763cef2SBarry Smith Notes: 2986d763cef2SBarry Smith There is no way to remove a single, specific monitor. 2987d763cef2SBarry Smith 2988d763cef2SBarry Smith Level: intermediate 2989d763cef2SBarry Smith 2990d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2991d763cef2SBarry Smith 2992a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 2993d763cef2SBarry Smith @*/ 29947087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 2995d763cef2SBarry Smith { 2996d952e501SBarry Smith PetscErrorCode ierr; 2997d952e501SBarry Smith PetscInt i; 2998d952e501SBarry Smith 2999d763cef2SBarry Smith PetscFunctionBegin; 30000700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3001d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 30028704b422SBarry Smith if (ts->monitordestroy[i]) { 30038704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 3004d952e501SBarry Smith } 3005d952e501SBarry Smith } 3006d763cef2SBarry Smith ts->numbermonitors = 0; 3007d763cef2SBarry Smith PetscFunctionReturn(0); 3008d763cef2SBarry Smith } 3009d763cef2SBarry Smith 30104a2ae208SSatish Balay #undef __FUNCT__ 3011a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 3012d8e5e3e6SSatish Balay /*@ 3013a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 30145516499fSSatish Balay 30155516499fSSatish Balay Level: intermediate 301641251cbbSSatish Balay 30175516499fSSatish Balay .keywords: TS, set, monitor 30185516499fSSatish Balay 301941251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 302041251cbbSSatish Balay @*/ 3021649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 3022d763cef2SBarry Smith { 3023dfbe8321SBarry Smith PetscErrorCode ierr; 30244d4332d5SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 3025d132466eSBarry Smith 3026d763cef2SBarry Smith PetscFunctionBegin; 30274d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3028649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 30298392e04aSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr); 3030649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3031d763cef2SBarry Smith PetscFunctionReturn(0); 3032d763cef2SBarry Smith } 3033d763cef2SBarry Smith 30344a2ae208SSatish Balay #undef __FUNCT__ 30359110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorSet" 30369110b2e7SHong Zhang /*@C 30379110b2e7SHong Zhang TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every 30389110b2e7SHong Zhang timestep to display the iteration's progress. 30399110b2e7SHong Zhang 30409110b2e7SHong Zhang Logically Collective on TS 30419110b2e7SHong Zhang 30429110b2e7SHong Zhang Input Parameters: 30439110b2e7SHong Zhang + ts - the TS context obtained from TSCreate() 30449110b2e7SHong Zhang . adjointmonitor - monitoring routine 30459110b2e7SHong Zhang . adjointmctx - [optional] user-defined context for private data for the 30469110b2e7SHong Zhang monitor routine (use NULL if no context is desired) 30479110b2e7SHong Zhang - adjointmonitordestroy - [optional] routine that frees monitor context 30489110b2e7SHong Zhang (may be NULL) 30499110b2e7SHong Zhang 30509110b2e7SHong Zhang Calling sequence of monitor: 30519110b2e7SHong Zhang $ int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx) 30529110b2e7SHong Zhang 30539110b2e7SHong Zhang + ts - the TS context 30549110b2e7SHong Zhang . steps - iteration number (after the final time step the monitor routine is called with a step of -1, this is at the final time which may have 30559110b2e7SHong Zhang been interpolated to) 30569110b2e7SHong Zhang . time - current time 30579110b2e7SHong Zhang . u - current iterate 30589110b2e7SHong Zhang . numcost - number of cost functionos 30599110b2e7SHong Zhang . lambda - sensitivities to initial conditions 30609110b2e7SHong Zhang . mu - sensitivities to parameters 30619110b2e7SHong Zhang - adjointmctx - [optional] adjoint monitoring context 30629110b2e7SHong Zhang 30639110b2e7SHong Zhang Notes: 30649110b2e7SHong Zhang This routine adds an additional monitor to the list of monitors that 30659110b2e7SHong Zhang already has been loaded. 30669110b2e7SHong Zhang 30679110b2e7SHong Zhang Fortran notes: Only a single monitor function can be set for each TS object 30689110b2e7SHong Zhang 30699110b2e7SHong Zhang Level: intermediate 30709110b2e7SHong Zhang 30719110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 30729110b2e7SHong Zhang 30739110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel() 30749110b2e7SHong Zhang @*/ 30759110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**)) 30769110b2e7SHong Zhang { 30779110b2e7SHong Zhang PetscFunctionBegin; 30789110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30799110b2e7SHong Zhang if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set"); 30809110b2e7SHong Zhang ts->adjointmonitor[ts->numberadjointmonitors] = adjointmonitor; 30819110b2e7SHong Zhang ts->adjointmonitordestroy[ts->numberadjointmonitors] = adjointmdestroy; 30829110b2e7SHong Zhang ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx; 30839110b2e7SHong Zhang PetscFunctionReturn(0); 30849110b2e7SHong Zhang } 30859110b2e7SHong Zhang 30869110b2e7SHong Zhang #undef __FUNCT__ 30879110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorCancel" 30889110b2e7SHong Zhang /*@C 30899110b2e7SHong Zhang TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object. 30909110b2e7SHong Zhang 30919110b2e7SHong Zhang Logically Collective on TS 30929110b2e7SHong Zhang 30939110b2e7SHong Zhang Input Parameters: 30949110b2e7SHong Zhang . ts - the TS context obtained from TSCreate() 30959110b2e7SHong Zhang 30969110b2e7SHong Zhang Notes: 30979110b2e7SHong Zhang There is no way to remove a single, specific monitor. 30989110b2e7SHong Zhang 30999110b2e7SHong Zhang Level: intermediate 31009110b2e7SHong Zhang 31019110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 31029110b2e7SHong Zhang 31039110b2e7SHong Zhang .seealso: TSAdjointMonitorSet() 31049110b2e7SHong Zhang @*/ 31059110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorCancel(TS ts) 31069110b2e7SHong Zhang { 31079110b2e7SHong Zhang PetscErrorCode ierr; 31089110b2e7SHong Zhang PetscInt i; 31099110b2e7SHong Zhang 31109110b2e7SHong Zhang PetscFunctionBegin; 31119110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31129110b2e7SHong Zhang for (i=0; i<ts->numberadjointmonitors; i++) { 31139110b2e7SHong Zhang if (ts->adjointmonitordestroy[i]) { 31149110b2e7SHong Zhang ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 31159110b2e7SHong Zhang } 31169110b2e7SHong Zhang } 31179110b2e7SHong Zhang ts->numberadjointmonitors = 0; 31189110b2e7SHong Zhang PetscFunctionReturn(0); 31199110b2e7SHong Zhang } 31209110b2e7SHong Zhang 31219110b2e7SHong Zhang #undef __FUNCT__ 3122110eb670SHong Zhang #define __FUNCT__ "TSAdjointMonitorDefault" 3123110eb670SHong Zhang /*@ 3124110eb670SHong Zhang TSAdjointMonitorDefault - Sets the Default monitor 3125110eb670SHong Zhang 3126110eb670SHong Zhang Level: intermediate 3127110eb670SHong Zhang 3128110eb670SHong Zhang .keywords: TS, set, monitor 3129110eb670SHong Zhang 3130110eb670SHong Zhang .seealso: TSAdjointMonitorSet() 3131110eb670SHong Zhang @*/ 3132110eb670SHong Zhang PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 3133110eb670SHong Zhang { 3134110eb670SHong Zhang PetscErrorCode ierr; 3135110eb670SHong Zhang PetscViewer viewer = (PetscViewer) dummy; 3136110eb670SHong Zhang 3137110eb670SHong Zhang PetscFunctionBegin; 3138110eb670SHong Zhang PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3139d763cef2SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3140d763cef2SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr); 3141d763cef2SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3142d763cef2SBarry Smith PetscFunctionReturn(0); 3143d763cef2SBarry Smith } 3144d763cef2SBarry Smith 3145d763cef2SBarry Smith #undef __FUNCT__ 3146cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 3147cd652676SJed Brown /*@ 3148cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 3149cd652676SJed Brown 3150cd652676SJed Brown Logically Collective on TS 3151cd652676SJed Brown 3152cd652676SJed Brown Input Argument: 3153cd652676SJed Brown . ts - time stepping context 3154cd652676SJed Brown 3155cd652676SJed Brown Output Argument: 3156cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 3157cd652676SJed Brown 3158cd652676SJed Brown Level: intermediate 3159cd652676SJed Brown 3160cd652676SJed Brown .keywords: TS, set 3161cd652676SJed Brown 3162cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 3163cd652676SJed Brown @*/ 3164cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 3165cd652676SJed Brown { 3166cd652676SJed Brown PetscFunctionBegin; 3167cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3168cd652676SJed Brown ts->retain_stages = flg; 3169cd652676SJed Brown PetscFunctionReturn(0); 3170cd652676SJed Brown } 3171cd652676SJed Brown 3172cd652676SJed Brown #undef __FUNCT__ 3173cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 3174cd652676SJed Brown /*@ 3175cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3176cd652676SJed Brown 3177cd652676SJed Brown Collective on TS 3178cd652676SJed Brown 3179cd652676SJed Brown Input Argument: 3180cd652676SJed Brown + ts - time stepping context 3181cd652676SJed Brown - t - time to interpolate to 3182cd652676SJed Brown 3183cd652676SJed Brown Output Argument: 31840910c330SBarry Smith . U - state at given time 3185cd652676SJed Brown 3186cd652676SJed Brown Notes: 3187cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 3188cd652676SJed Brown 3189cd652676SJed Brown Level: intermediate 3190cd652676SJed Brown 3191cd652676SJed Brown Developer Notes: 3192cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3193cd652676SJed Brown 3194cd652676SJed Brown .keywords: TS, set 3195cd652676SJed Brown 3196cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 3197cd652676SJed Brown @*/ 31980910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3199cd652676SJed Brown { 3200cd652676SJed Brown PetscErrorCode ierr; 3201cd652676SJed Brown 3202cd652676SJed Brown PetscFunctionBegin; 3203cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3204b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 32056712e2f1SBarry 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); 3206ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 32070910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3208cd652676SJed Brown PetscFunctionReturn(0); 3209cd652676SJed Brown } 3210cd652676SJed Brown 3211cd652676SJed Brown #undef __FUNCT__ 32124a2ae208SSatish Balay #define __FUNCT__ "TSStep" 3213d763cef2SBarry Smith /*@ 32146d9e5789SSean Farley TSStep - Steps one time step 3215d763cef2SBarry Smith 3216d763cef2SBarry Smith Collective on TS 3217d763cef2SBarry Smith 3218d763cef2SBarry Smith Input Parameter: 3219d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3220d763cef2SBarry Smith 322127829d71SBarry Smith Level: developer 3222d763cef2SBarry Smith 3223b8123daeSJed Brown Notes: 322427829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 322527829d71SBarry Smith 3226b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3227b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3228b8123daeSJed Brown 322925cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 323025cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 323125cb2221SBarry Smith 3232d763cef2SBarry Smith .keywords: TS, timestep, solve 3233d763cef2SBarry Smith 32349be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3235d763cef2SBarry Smith @*/ 3236193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3237d763cef2SBarry Smith { 32382fb8446cSMatthew G. Knepley DM dm; 3239dfbe8321SBarry Smith PetscErrorCode ierr; 3240fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3241d763cef2SBarry Smith 3242d763cef2SBarry Smith PetscFunctionBegin; 32430700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3244fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3245fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3246fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3247fffbeea8SBarry Smith " type = {Preprint},\n" 3248fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3249fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3250302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3251fffbeea8SBarry Smith 32522fb8446cSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3253d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 3254d405a339SMatthew Knepley 3255362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 325624655328SShri ts->ptime_prev = ts->ptime; 3257cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3258bbd56ea5SKarl Rupp 3259e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3260d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3261193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3262d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3263bbd56ea5SKarl Rupp 326424655328SShri ts->time_step_prev = ts->ptime - ts->ptime_prev; 3265cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3266362cd11cSLisandro Dalcin 3267362cd11cSLisandro Dalcin if (ts->reason < 0) { 3268cef5090cSJed Brown if (ts->errorifstepfailed) { 326908c7845fSBarry 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]); 327008c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3271d2daff3dSHong Zhang } 327208c7845fSBarry Smith } else if (!ts->reason) { 327308c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 327408c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 327508c7845fSBarry Smith } 32762c18e0fdSBarry Smith ts->total_steps++; 32778392e04aSShri Abhyankar ts->steprollback = PETSC_FALSE; 327808c7845fSBarry Smith PetscFunctionReturn(0); 327908c7845fSBarry Smith } 328008c7845fSBarry Smith 328108c7845fSBarry Smith #undef __FUNCT__ 328208c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep" 328308c7845fSBarry Smith /*@ 3284b957a604SHong Zhang TSAdjointStep - Steps one time step backward in the adjoint run 328508c7845fSBarry Smith 328608c7845fSBarry Smith Collective on TS 328708c7845fSBarry Smith 328808c7845fSBarry Smith Input Parameter: 328908c7845fSBarry Smith . ts - the TS context obtained from TSCreate() 329008c7845fSBarry Smith 32916a4d4014SLisandro Dalcin Level: intermediate 32926a4d4014SLisandro Dalcin 3293b957a604SHong Zhang .keywords: TS, adjoint, step 32946a4d4014SLisandro Dalcin 3295b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve() 32966a4d4014SLisandro Dalcin @*/ 329708c7845fSBarry Smith PetscErrorCode TSAdjointStep(TS ts) 32986a4d4014SLisandro Dalcin { 329908c7845fSBarry Smith DM dm; 33006a4d4014SLisandro Dalcin PetscErrorCode ierr; 33016a4d4014SLisandro Dalcin 33026a4d4014SLisandro Dalcin PetscFunctionBegin; 33034a2ae208SSatish Balay PetscValidHeaderSpecific(ts, TS_CLASSID,1); 330408c7845fSBarry Smith ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 330508c7845fSBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3306d763cef2SBarry Smith 3307d763cef2SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 330808c7845fSBarry Smith ts->ptime_prev = ts->ptime; 330908c7845fSBarry Smith ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3310685405a1SBarry Smith ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts, "-ts_view_solution");CHKERRQ(ierr); 3311d763cef2SBarry Smith 33126849ba73SBarry Smith ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 331342f2b339SBarry 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); 331442f2b339SBarry Smith ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr); 3315a6570f20SBarry Smith ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3316d763cef2SBarry Smith 3317cef5090cSJed Brown ts->time_step_prev = ts->ptime - ts->ptime_prev; 3318cef5090cSJed Brown ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3319362cd11cSLisandro Dalcin 3320362cd11cSLisandro Dalcin if (ts->reason < 0) { 3321cef5090cSJed Brown if (ts->errorifstepfailed) { 33226c4ed002SBarry 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]); 33236c4ed002SBarry Smith else if (ts->reason == TS_DIVERGED_STEP_REJECTED) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_reject or make negative to attempt recovery",TSConvergedReasons[ts->reason]); 33246c4ed002SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3325cef5090cSJed Brown } 3326362cd11cSLisandro Dalcin } else if (!ts->reason) { 332773b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3328db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3329362cd11cSLisandro Dalcin } 33302c18e0fdSBarry Smith ts->total_steps--; 3331d763cef2SBarry Smith PetscFunctionReturn(0); 3332d763cef2SBarry Smith } 3333d763cef2SBarry Smith 3334d763cef2SBarry Smith #undef __FUNCT__ 333505175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 333605175c85SJed Brown /*@ 333705175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 333805175c85SJed Brown 33391c3436cfSJed Brown Collective on TS 334005175c85SJed Brown 334105175c85SJed Brown Input Arguments: 33421c3436cfSJed Brown + ts - time stepping context 33431c3436cfSJed Brown . order - desired order of accuracy 33440298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 334505175c85SJed Brown 334605175c85SJed Brown Output Arguments: 33470910c330SBarry Smith . U - state at the end of the current step 334805175c85SJed Brown 334905175c85SJed Brown Level: advanced 335005175c85SJed Brown 3351108c343cSJed Brown Notes: 3352108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3353108c343cSJed 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. 3354108c343cSJed Brown 33551c3436cfSJed Brown .seealso: TSStep(), TSAdapt 335605175c85SJed Brown @*/ 33570910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 335805175c85SJed Brown { 335905175c85SJed Brown PetscErrorCode ierr; 336005175c85SJed Brown 336105175c85SJed Brown PetscFunctionBegin; 336205175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 336305175c85SJed Brown PetscValidType(ts,1); 33640910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3365ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 33660910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 336705175c85SJed Brown PetscFunctionReturn(0); 336805175c85SJed Brown } 336905175c85SJed Brown 3370d67e68b7SBarry Smith 337105175c85SJed Brown #undef __FUNCT__ 3372d763cef2SBarry Smith #define __FUNCT__ "TSSolve" 3373d763cef2SBarry Smith /*@ 3374d763cef2SBarry Smith TSSolve - Steps the requested number of timesteps. 3375d763cef2SBarry Smith 3376d763cef2SBarry Smith Collective on TS 3377d763cef2SBarry Smith 3378d763cef2SBarry Smith Input Parameter: 3379d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3380cc708dedSBarry Smith - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 33815a3a76d0SJed Brown 3382d763cef2SBarry Smith Level: beginner 3383d763cef2SBarry Smith 33845a3a76d0SJed Brown Notes: 33855a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 33865a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 33875a3a76d0SJed Brown stepped over the final time. 33885a3a76d0SJed Brown 3389d763cef2SBarry Smith .keywords: TS, timestep, solve 3390d763cef2SBarry Smith 3391d763cef2SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep() 3392d763cef2SBarry Smith @*/ 3393cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 3394d763cef2SBarry Smith { 3395b06615a5SLisandro Dalcin Vec solution; 3396d763cef2SBarry Smith PetscErrorCode ierr; 3397d763cef2SBarry Smith 3398d763cef2SBarry Smith PetscFunctionBegin; 3399d763cef2SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3400f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 340149354f04SShri 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 */ 3402b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 34030910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 3404b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3405b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3406b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 34075a3a76d0SJed Brown } 34080910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3409bbd56ea5SKarl Rupp } else if (u) { 34100910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 34115a3a76d0SJed Brown } 3412b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 3413d763cef2SBarry Smith /* reset time step and iteration counters */ 3414193ac0bcSJed Brown ts->steps = 0; 34155ef26d82SJed Brown ts->ksp_its = 0; 34165ef26d82SJed Brown ts->snes_its = 0; 3417c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3418c610991cSLisandro Dalcin ts->reject = 0; 3419193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3420193ac0bcSJed Brown 3421ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3422ec8db0baSMatthew G. Knepley { 3423ec8db0baSMatthew G. Knepley DM dm; 3424ec8db0baSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3425ec8db0baSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3426ec8db0baSMatthew G. Knepley } 3427f05ece33SBarry Smith 3428193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3429193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 34300910c330SBarry Smith ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 3431cc708dedSBarry Smith ts->solvetime = ts->ptime; 3432193ac0bcSJed Brown } else { 3433d763cef2SBarry Smith /* steps the requested number of timesteps. */ 3434db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3435db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3436cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 343711b05f00SHong Zhang if (ts->vec_costintegral) ts->costintegralfwd=PETSC_TRUE; 34386fea3669SShri Abhyankar if(ts->event) { 34396fea3669SShri Abhyankar ierr = TSEventMonitorInitialize(ts);CHKERRQ(ierr); 34406fea3669SShri Abhyankar } 3441e1a7a14fSJed Brown while (!ts->reason) { 3442193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 3443193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 3444aeb4809dSShri Abhyankar if (ts->event) { 3445aeb4809dSShri Abhyankar ierr = TSEventMonitor(ts);CHKERRQ(ierr); 34461eda64f1SShri Abhyankar } 34478392e04aSShri Abhyankar if(!ts->steprollback) { 3448cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 34491eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 3450aeb4809dSShri Abhyankar } 3451193ac0bcSJed Brown } 345249354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 34530910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 3454cc708dedSBarry Smith ts->solvetime = ts->max_time; 3455b06615a5SLisandro Dalcin solution = u; 34560574a7fbSJed Brown } else { 3457ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3458cc708dedSBarry Smith ts->solvetime = ts->ptime; 3459b06615a5SLisandro Dalcin solution = ts->vec_sol; 34600574a7fbSJed Brown } 3461b06615a5SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr); 3462685405a1SBarry Smith ierr = VecViewFromOptions(solution,(PetscObject) ts,"-ts_view_solution");CHKERRQ(ierr); 3463193ac0bcSJed Brown } 3464d2daff3dSHong Zhang 3465ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 3466dc1cb503SEmil Constantinescu ierr = VecViewFromOptions(ts->vec_sol,NULL,"-ts_view_solution");CHKERRQ(ierr); 346756f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 3468ef222394SBarry Smith if (ts->adjoint_solve) { 3469ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 34702b0a91c0SBarry Smith } 3471d763cef2SBarry Smith PetscFunctionReturn(0); 3472d763cef2SBarry Smith } 3473d763cef2SBarry Smith 3474d763cef2SBarry Smith #undef __FUNCT__ 3475c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve" 3476c88f2d44SBarry Smith /*@ 3477c88f2d44SBarry Smith TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 3478c88f2d44SBarry Smith 3479c88f2d44SBarry Smith Collective on TS 3480c88f2d44SBarry Smith 3481c88f2d44SBarry Smith Input Parameter: 34822c39e106SBarry Smith . ts - the TS context obtained from TSCreate() 3483c88f2d44SBarry Smith 3484e87fd094SBarry Smith Options Database: 3485e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions 3486e87fd094SBarry Smith 3487c88f2d44SBarry Smith Level: intermediate 3488c88f2d44SBarry Smith 3489c88f2d44SBarry Smith Notes: 3490c88f2d44SBarry Smith This must be called after a call to TSSolve() that solves the forward problem 3491c88f2d44SBarry Smith 349273b18844SBarry Smith By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time 349373b18844SBarry Smith 3494c88f2d44SBarry Smith .keywords: TS, timestep, solve 3495c88f2d44SBarry Smith 3496b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep() 3497c88f2d44SBarry Smith @*/ 34982c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts) 3499c88f2d44SBarry Smith { 3500c88f2d44SBarry Smith PetscErrorCode ierr; 3501c88f2d44SBarry Smith 3502c88f2d44SBarry Smith PetscFunctionBegin; 3503c88f2d44SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3504c88f2d44SBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3505c88f2d44SBarry Smith /* reset time step and iteration counters */ 3506c88f2d44SBarry Smith ts->steps = 0; 3507c88f2d44SBarry Smith ts->ksp_its = 0; 3508c88f2d44SBarry Smith ts->snes_its = 0; 3509c88f2d44SBarry Smith ts->num_snes_failures = 0; 3510c88f2d44SBarry Smith ts->reject = 0; 3511c88f2d44SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 3512c88f2d44SBarry Smith 351373b18844SBarry Smith if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps; 3514c88f2d44SBarry Smith 351573b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3516c88f2d44SBarry Smith while (!ts->reason) { 351755c52731SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 35189110b2e7SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 3519c88f2d44SBarry Smith if (ts->event) { 3520d0578d90SShri Abhyankar ierr = TSAdjointEventMonitor(ts);CHKERRQ(ierr); 3521d0578d90SShri Abhyankar } 3522616946c1SHong Zhang ierr = TSAdjointStep(ts);CHKERRQ(ierr); 3523c88f2d44SBarry Smith } 352426656371SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 352526656371SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 3526c88f2d44SBarry Smith ts->solvetime = ts->ptime; 3527685405a1SBarry Smith ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr); 3528c88f2d44SBarry Smith PetscFunctionReturn(0); 3529c88f2d44SBarry Smith } 3530c88f2d44SBarry Smith 3531c88f2d44SBarry Smith #undef __FUNCT__ 3532d763cef2SBarry Smith #define __FUNCT__ "TSMonitor" 35337db568b7SBarry Smith /*@C 3534228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 3535228d79bcSJed Brown 3536228d79bcSJed Brown Collective on TS 3537228d79bcSJed Brown 3538228d79bcSJed Brown Input Parameters: 3539228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 3540228d79bcSJed Brown . step - step number that has just completed 3541228d79bcSJed Brown . ptime - model time of the state 35420910c330SBarry Smith - u - state at the current model time 3543228d79bcSJed Brown 3544228d79bcSJed Brown Notes: 35457db568b7SBarry Smith TSMonitor() is typically used automatically within the time stepping implementations. 35467db568b7SBarry Smith Users would almost never call this routine directly. 3547228d79bcSJed Brown 35487db568b7SBarry Smith Level: developer 3549228d79bcSJed Brown 3550228d79bcSJed Brown .keywords: TS, timestep 3551228d79bcSJed Brown @*/ 35520910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 3553d763cef2SBarry Smith { 3554d763cef2SBarry Smith PetscErrorCode ierr; 3555d763cef2SBarry Smith PetscInt i,n = ts->numbermonitors; 3556d763cef2SBarry Smith 3557d763cef2SBarry Smith PetscFunctionBegin; 3558b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3559b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 35605edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 3561d763cef2SBarry Smith for (i=0; i<n; i++) { 35620910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 3563d763cef2SBarry Smith } 35645edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 3565d763cef2SBarry Smith PetscFunctionReturn(0); 3566d763cef2SBarry Smith } 3567d763cef2SBarry Smith 35689110b2e7SHong Zhang #undef __FUNCT__ 35699110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitor" 35707db568b7SBarry Smith /*@C 35719110b2e7SHong Zhang TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet() 35729110b2e7SHong Zhang 35739110b2e7SHong Zhang Collective on TS 35749110b2e7SHong Zhang 35759110b2e7SHong Zhang Input Parameters: 35769110b2e7SHong Zhang + ts - time stepping context obtained from TSCreate() 35779110b2e7SHong Zhang . step - step number that has just completed 35789110b2e7SHong Zhang . ptime - model time of the state 35799110b2e7SHong Zhang . u - state at the current model time 35809110b2e7SHong Zhang . numcost - number of cost functions (dimension of lambda or mu) 35819110b2e7SHong Zhang . lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 35829110b2e7SHong Zhang - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 35839110b2e7SHong Zhang 35849110b2e7SHong Zhang Notes: 35857db568b7SBarry Smith TSAdjointMonitor() is typically used automatically within the time stepping implementations. 35867db568b7SBarry Smith Users would almost never call this routine directly. 35879110b2e7SHong Zhang 35887db568b7SBarry Smith Level: developer 35899110b2e7SHong Zhang 35909110b2e7SHong Zhang .keywords: TS, timestep 35919110b2e7SHong Zhang @*/ 35929110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu) 35939110b2e7SHong Zhang { 35949110b2e7SHong Zhang PetscErrorCode ierr; 35959110b2e7SHong Zhang PetscInt i,n = ts->numberadjointmonitors; 35969110b2e7SHong Zhang 35979110b2e7SHong Zhang PetscFunctionBegin; 35989110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 35999110b2e7SHong Zhang PetscValidHeaderSpecific(u,VEC_CLASSID,4); 36009110b2e7SHong Zhang ierr = VecLockPush(u);CHKERRQ(ierr); 36019110b2e7SHong Zhang for (i=0; i<n; i++) { 36029110b2e7SHong Zhang ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 36039110b2e7SHong Zhang } 36049110b2e7SHong Zhang ierr = VecLockPop(u);CHKERRQ(ierr); 36059110b2e7SHong Zhang PetscFunctionReturn(0); 36069110b2e7SHong Zhang } 36079110b2e7SHong Zhang 3608d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 3609d763cef2SBarry Smith #undef __FUNCT__ 3610a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 3611d763cef2SBarry Smith /*@C 36127db568b7SBarry Smith TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 3613a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 3614d763cef2SBarry Smith 3615d763cef2SBarry Smith Collective on TS 3616d763cef2SBarry Smith 3617d763cef2SBarry Smith Input Parameters: 3618d763cef2SBarry Smith + host - the X display to open, or null for the local machine 3619d763cef2SBarry Smith . label - the title to put in the title bar 36207c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 3621a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 3622a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 3623d763cef2SBarry Smith 3624d763cef2SBarry Smith Output Parameter: 36250b039ecaSBarry Smith . ctx - the context 3626d763cef2SBarry Smith 3627d763cef2SBarry Smith Options Database Key: 36284f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 36297db568b7SBarry Smith . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 36307db568b7SBarry Smith . -ts_monitor_lg_error - monitor the error 36317db568b7SBarry Smith . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 36327db568b7SBarry Smith . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 3633b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 3634d763cef2SBarry Smith 3635d763cef2SBarry Smith Notes: 3636a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 3637d763cef2SBarry Smith 36387db568b7SBarry Smith One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 36397db568b7SBarry Smith 36407db568b7SBarry Smith Many of the functions that control the monitoring have two forms: TSMonitorLGSet/GetXXXX() and TSMonitorLGCtxSet/GetXXXX() the first take a TS object as the 36417db568b7SBarry Smith first argument (if that TS object does not have a TSMonitorLGCtx associated with it the function call is ignored) and the second takes a TSMonitorLGCtx object 36427db568b7SBarry Smith as the first argument. 36437db568b7SBarry Smith 36447db568b7SBarry Smith One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 36457db568b7SBarry Smith 36467db568b7SBarry Smith 3647d763cef2SBarry Smith Level: intermediate 3648d763cef2SBarry Smith 36497db568b7SBarry Smith .keywords: TS, monitor, line graph, residual 3650d763cef2SBarry Smith 36517db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 36527db568b7SBarry Smith TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 36537db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 36547db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 36557db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 36567c922b88SBarry Smith 3657d763cef2SBarry Smith @*/ 3658a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 3659d763cef2SBarry Smith { 36607f52daa2SLisandro Dalcin PetscDraw draw; 3661dfbe8321SBarry Smith PetscErrorCode ierr; 3662d763cef2SBarry Smith 3663d763cef2SBarry Smith PetscFunctionBegin; 3664b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 36657f52daa2SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 36667f52daa2SLisandro Dalcin ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 36677f52daa2SLisandro Dalcin ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 3668b6fe0379SLisandro Dalcin ierr = PetscDrawLGSetUseMarkers((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr); 3669287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 36707f52daa2SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 3671a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 3672d763cef2SBarry Smith PetscFunctionReturn(0); 3673d763cef2SBarry Smith } 3674d763cef2SBarry Smith 36754a2ae208SSatish Balay #undef __FUNCT__ 36764f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 3677b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 3678d763cef2SBarry Smith { 36790b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 3680c32365f1SBarry Smith PetscReal x = ptime,y; 3681dfbe8321SBarry Smith PetscErrorCode ierr; 3682d763cef2SBarry Smith 3683d763cef2SBarry Smith PetscFunctionBegin; 3684b06615a5SLisandro Dalcin if (!step) { 3685a9f9c1f6SBarry Smith PetscDrawAxis axis; 3686a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 3687a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 3688a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 3689a9f9c1f6SBarry Smith } 3690c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 36910b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 3692b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 36930b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 36943923b477SBarry Smith } 3695d763cef2SBarry Smith PetscFunctionReturn(0); 3696d763cef2SBarry Smith } 3697d763cef2SBarry Smith 36984a2ae208SSatish Balay #undef __FUNCT__ 3699a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 3700d763cef2SBarry Smith /*@C 3701a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 3702a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 3703d763cef2SBarry Smith 37040b039ecaSBarry Smith Collective on TSMonitorLGCtx 3705d763cef2SBarry Smith 3706d763cef2SBarry Smith Input Parameter: 37070b039ecaSBarry Smith . ctx - the monitor context 3708d763cef2SBarry Smith 3709d763cef2SBarry Smith Level: intermediate 3710d763cef2SBarry Smith 3711d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 3712d763cef2SBarry Smith 37134f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 3714d763cef2SBarry Smith @*/ 3715a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 3716d763cef2SBarry Smith { 3717dfbe8321SBarry Smith PetscErrorCode ierr; 3718d763cef2SBarry Smith 3719d763cef2SBarry Smith PetscFunctionBegin; 37207684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 37217684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 37227684fa3eSBarry Smith } 37230b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 372431152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 3725387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 3726387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 3727387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 37280b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 3729d763cef2SBarry Smith PetscFunctionReturn(0); 3730d763cef2SBarry Smith } 3731d763cef2SBarry Smith 37324a2ae208SSatish Balay #undef __FUNCT__ 37334a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 3734d763cef2SBarry Smith /*@ 3735b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 3736d763cef2SBarry Smith 3737d763cef2SBarry Smith Not Collective 3738d763cef2SBarry Smith 3739d763cef2SBarry Smith Input Parameter: 3740d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3741d763cef2SBarry Smith 3742d763cef2SBarry Smith Output Parameter: 3743d763cef2SBarry Smith . t - the current time 3744d763cef2SBarry Smith 3745d763cef2SBarry Smith Level: beginner 3746d763cef2SBarry Smith 3747b8123daeSJed Brown Note: 3748b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 37499be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 3750b8123daeSJed Brown 3751d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3752d763cef2SBarry Smith 3753d763cef2SBarry Smith .keywords: TS, get, time 3754d763cef2SBarry Smith @*/ 37557087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 3756d763cef2SBarry Smith { 3757d763cef2SBarry Smith PetscFunctionBegin; 37580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3759f7cf8827SBarry Smith PetscValidRealPointer(t,2); 3760d763cef2SBarry Smith *t = ts->ptime; 3761d763cef2SBarry Smith PetscFunctionReturn(0); 3762d763cef2SBarry Smith } 3763d763cef2SBarry Smith 37644a2ae208SSatish Balay #undef __FUNCT__ 3765e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime" 3766e5e524a1SHong Zhang /*@ 3767e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 3768e5e524a1SHong Zhang 3769e5e524a1SHong Zhang Not Collective 3770e5e524a1SHong Zhang 3771e5e524a1SHong Zhang Input Parameter: 3772e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 3773e5e524a1SHong Zhang 3774e5e524a1SHong Zhang Output Parameter: 3775e5e524a1SHong Zhang . t - the previous time 3776e5e524a1SHong Zhang 3777e5e524a1SHong Zhang Level: beginner 3778e5e524a1SHong Zhang 3779e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3780e5e524a1SHong Zhang 3781e5e524a1SHong Zhang .keywords: TS, get, time 3782e5e524a1SHong Zhang @*/ 3783e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 3784e5e524a1SHong Zhang { 3785e5e524a1SHong Zhang PetscFunctionBegin; 3786e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3787e5e524a1SHong Zhang PetscValidRealPointer(t,2); 3788e5e524a1SHong Zhang *t = ts->ptime_prev; 3789e5e524a1SHong Zhang PetscFunctionReturn(0); 3790e5e524a1SHong Zhang } 3791e5e524a1SHong Zhang 3792e5e524a1SHong Zhang #undef __FUNCT__ 37936a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 37946a4d4014SLisandro Dalcin /*@ 37956a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 37966a4d4014SLisandro Dalcin 37973f9fe445SBarry Smith Logically Collective on TS 37986a4d4014SLisandro Dalcin 37996a4d4014SLisandro Dalcin Input Parameters: 38006a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 38016a4d4014SLisandro Dalcin - time - the time 38026a4d4014SLisandro Dalcin 38036a4d4014SLisandro Dalcin Level: intermediate 38046a4d4014SLisandro Dalcin 38056a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 38066a4d4014SLisandro Dalcin 38076a4d4014SLisandro Dalcin .keywords: TS, set, time 38086a4d4014SLisandro Dalcin @*/ 38097087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 38106a4d4014SLisandro Dalcin { 38116a4d4014SLisandro Dalcin PetscFunctionBegin; 38120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3813c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 38146a4d4014SLisandro Dalcin ts->ptime = t; 38156a4d4014SLisandro Dalcin PetscFunctionReturn(0); 38166a4d4014SLisandro Dalcin } 38176a4d4014SLisandro Dalcin 38186a4d4014SLisandro Dalcin #undef __FUNCT__ 38194a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 3820d763cef2SBarry Smith /*@C 3821d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 3822d763cef2SBarry Smith TS options in the database. 3823d763cef2SBarry Smith 38243f9fe445SBarry Smith Logically Collective on TS 3825d763cef2SBarry Smith 3826d763cef2SBarry Smith Input Parameter: 3827d763cef2SBarry Smith + ts - The TS context 3828d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3829d763cef2SBarry Smith 3830d763cef2SBarry Smith Notes: 3831d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3832d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3833d763cef2SBarry Smith hyphen. 3834d763cef2SBarry Smith 3835d763cef2SBarry Smith Level: advanced 3836d763cef2SBarry Smith 3837d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 3838d763cef2SBarry Smith 3839d763cef2SBarry Smith .seealso: TSSetFromOptions() 3840d763cef2SBarry Smith 3841d763cef2SBarry Smith @*/ 38427087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 3843d763cef2SBarry Smith { 3844dfbe8321SBarry Smith PetscErrorCode ierr; 3845089b2837SJed Brown SNES snes; 3846d763cef2SBarry Smith 3847d763cef2SBarry Smith PetscFunctionBegin; 38480700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3849d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3850089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3851089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3852d763cef2SBarry Smith PetscFunctionReturn(0); 3853d763cef2SBarry Smith } 3854d763cef2SBarry Smith 3855d763cef2SBarry Smith 38564a2ae208SSatish Balay #undef __FUNCT__ 38574a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 3858d763cef2SBarry Smith /*@C 3859d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 3860d763cef2SBarry Smith TS options in the database. 3861d763cef2SBarry Smith 38623f9fe445SBarry Smith Logically Collective on TS 3863d763cef2SBarry Smith 3864d763cef2SBarry Smith Input Parameter: 3865d763cef2SBarry Smith + ts - The TS context 3866d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3867d763cef2SBarry Smith 3868d763cef2SBarry Smith Notes: 3869d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3870d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3871d763cef2SBarry Smith hyphen. 3872d763cef2SBarry Smith 3873d763cef2SBarry Smith Level: advanced 3874d763cef2SBarry Smith 3875d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 3876d763cef2SBarry Smith 3877d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 3878d763cef2SBarry Smith 3879d763cef2SBarry Smith @*/ 38807087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 3881d763cef2SBarry Smith { 3882dfbe8321SBarry Smith PetscErrorCode ierr; 3883089b2837SJed Brown SNES snes; 3884d763cef2SBarry Smith 3885d763cef2SBarry Smith PetscFunctionBegin; 38860700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3887d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3888089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3889089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3890d763cef2SBarry Smith PetscFunctionReturn(0); 3891d763cef2SBarry Smith } 3892d763cef2SBarry Smith 38934a2ae208SSatish Balay #undef __FUNCT__ 38944a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 3895d763cef2SBarry Smith /*@C 3896d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 3897d763cef2SBarry Smith TS options in the database. 3898d763cef2SBarry Smith 3899d763cef2SBarry Smith Not Collective 3900d763cef2SBarry Smith 3901d763cef2SBarry Smith Input Parameter: 3902d763cef2SBarry Smith . ts - The TS context 3903d763cef2SBarry Smith 3904d763cef2SBarry Smith Output Parameter: 3905d763cef2SBarry Smith . prefix - A pointer to the prefix string used 3906d763cef2SBarry Smith 3907d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 3908d763cef2SBarry Smith sufficient length to hold the prefix. 3909d763cef2SBarry Smith 3910d763cef2SBarry Smith Level: intermediate 3911d763cef2SBarry Smith 3912d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 3913d763cef2SBarry Smith 3914d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 3915d763cef2SBarry Smith @*/ 39167087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 3917d763cef2SBarry Smith { 3918dfbe8321SBarry Smith PetscErrorCode ierr; 3919d763cef2SBarry Smith 3920d763cef2SBarry Smith PetscFunctionBegin; 39210700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 39224482741eSBarry Smith PetscValidPointer(prefix,2); 3923d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3924d763cef2SBarry Smith PetscFunctionReturn(0); 3925d763cef2SBarry Smith } 3926d763cef2SBarry Smith 39274a2ae208SSatish Balay #undef __FUNCT__ 39284a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 3929d763cef2SBarry Smith /*@C 3930d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 3931d763cef2SBarry Smith 3932d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 3933d763cef2SBarry Smith 3934d763cef2SBarry Smith Input Parameter: 3935d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 3936d763cef2SBarry Smith 3937d763cef2SBarry Smith Output Parameters: 3938e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 3939e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 3940e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 3941e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 3942d763cef2SBarry Smith 39430298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 3944d763cef2SBarry Smith 3945d763cef2SBarry Smith Level: intermediate 3946d763cef2SBarry Smith 394726d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 3948d763cef2SBarry Smith 3949d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 3950d763cef2SBarry Smith @*/ 3951e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 3952d763cef2SBarry Smith { 3953089b2837SJed Brown PetscErrorCode ierr; 3954089b2837SJed Brown SNES snes; 395524989b8cSPeter Brune DM dm; 3956089b2837SJed Brown 3957d763cef2SBarry Smith PetscFunctionBegin; 3958089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3959e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 396024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 396124989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 3962d763cef2SBarry Smith PetscFunctionReturn(0); 3963d763cef2SBarry Smith } 3964d763cef2SBarry Smith 39651713a123SBarry Smith #undef __FUNCT__ 39662eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 39672eca1d9cSJed Brown /*@C 39682eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 39692eca1d9cSJed Brown 39702eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 39712eca1d9cSJed Brown 39722eca1d9cSJed Brown Input Parameter: 39732eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 39742eca1d9cSJed Brown 39752eca1d9cSJed Brown Output Parameters: 3976e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 3977e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 39782eca1d9cSJed Brown . f - The function to compute the matrices 39792eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 39802eca1d9cSJed Brown 39810298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 39822eca1d9cSJed Brown 39832eca1d9cSJed Brown Level: advanced 39842eca1d9cSJed Brown 39852eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 39862eca1d9cSJed Brown 39872eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 39882eca1d9cSJed Brown @*/ 3989e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 39902eca1d9cSJed Brown { 3991089b2837SJed Brown PetscErrorCode ierr; 3992089b2837SJed Brown SNES snes; 399324989b8cSPeter Brune DM dm; 39940910c330SBarry Smith 39952eca1d9cSJed Brown PetscFunctionBegin; 3996089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3997f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 3998e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 399924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 400024989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 40012eca1d9cSJed Brown PetscFunctionReturn(0); 40022eca1d9cSJed Brown } 40032eca1d9cSJed Brown 40046083293cSBarry Smith 40052eca1d9cSJed Brown #undef __FUNCT__ 400683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 40071713a123SBarry Smith /*@C 400883a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 40091713a123SBarry Smith VecView() for the solution at each timestep 40101713a123SBarry Smith 40111713a123SBarry Smith Collective on TS 40121713a123SBarry Smith 40131713a123SBarry Smith Input Parameters: 40141713a123SBarry Smith + ts - the TS context 40151713a123SBarry Smith . step - current time-step 4016142b95e3SSatish Balay . ptime - current time 40170298fd71SBarry Smith - dummy - either a viewer or NULL 40181713a123SBarry Smith 401999fdda47SBarry Smith Options Database: 402099fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 402199fdda47SBarry Smith 4022387f4636SBarry 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 402399fdda47SBarry Smith will look bad 402499fdda47SBarry Smith 40251713a123SBarry Smith Level: intermediate 40261713a123SBarry Smith 40271713a123SBarry Smith .keywords: TS, vector, monitor, view 40281713a123SBarry Smith 4029a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 40301713a123SBarry Smith @*/ 40310910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 40321713a123SBarry Smith { 4033dfbe8321SBarry Smith PetscErrorCode ierr; 403483a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4035473a3ab2SBarry Smith PetscDraw draw; 40361713a123SBarry Smith 40371713a123SBarry Smith PetscFunctionBegin; 40386083293cSBarry Smith if (!step && ictx->showinitial) { 40396083293cSBarry Smith if (!ictx->initialsolution) { 40400910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 40411713a123SBarry Smith } 40420910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 40436083293cSBarry Smith } 4044b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 40450dcf80beSBarry Smith 40466083293cSBarry Smith if (ictx->showinitial) { 40476083293cSBarry Smith PetscReal pause; 40486083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 40496083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 40506083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 40516083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 40526083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 40536083293cSBarry Smith } 40540910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 4055473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 405651fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 4057473a3ab2SBarry Smith char time[32]; 4058473a3ab2SBarry Smith 4059473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 406085b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4061473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4062473a3ab2SBarry Smith h = yl + .95*(yr - yl); 406351fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4064473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4065473a3ab2SBarry Smith } 4066473a3ab2SBarry Smith 40676083293cSBarry Smith if (ictx->showinitial) { 40686083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 40696083293cSBarry Smith } 40701713a123SBarry Smith PetscFunctionReturn(0); 40711713a123SBarry Smith } 40721713a123SBarry Smith 40732d5ee99bSBarry Smith #undef __FUNCT__ 40749110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorDrawSensi" 40759110b2e7SHong Zhang /*@C 40769110b2e7SHong Zhang TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling 40779110b2e7SHong Zhang VecView() for the sensitivities to initial states at each timestep 40789110b2e7SHong Zhang 40799110b2e7SHong Zhang Collective on TS 40809110b2e7SHong Zhang 40819110b2e7SHong Zhang Input Parameters: 40829110b2e7SHong Zhang + ts - the TS context 40839110b2e7SHong Zhang . step - current time-step 40849110b2e7SHong Zhang . ptime - current time 40859110b2e7SHong Zhang . u - current state 40869110b2e7SHong Zhang . numcost - number of cost functions 40879110b2e7SHong Zhang . lambda - sensitivities to initial conditions 40889110b2e7SHong Zhang . mu - sensitivities to parameters 40899110b2e7SHong Zhang - dummy - either a viewer or NULL 40909110b2e7SHong Zhang 40919110b2e7SHong Zhang Level: intermediate 40929110b2e7SHong Zhang 40939110b2e7SHong Zhang .keywords: TS, vector, adjoint, monitor, view 40949110b2e7SHong Zhang 40959110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView() 40969110b2e7SHong Zhang @*/ 40979110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 40989110b2e7SHong Zhang { 40999110b2e7SHong Zhang PetscErrorCode ierr; 41009110b2e7SHong Zhang TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 41019110b2e7SHong Zhang PetscDraw draw; 4102a0046e2cSSatish Balay PetscReal xl,yl,xr,yr,h; 4103a0046e2cSSatish Balay char time[32]; 41049110b2e7SHong Zhang 41059110b2e7SHong Zhang PetscFunctionBegin; 41069110b2e7SHong Zhang if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 41079110b2e7SHong Zhang 41089110b2e7SHong Zhang ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr); 41099110b2e7SHong Zhang ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 41109110b2e7SHong Zhang ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 41119110b2e7SHong Zhang ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 41129110b2e7SHong Zhang h = yl + .95*(yr - yl); 41139110b2e7SHong Zhang ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 41149110b2e7SHong Zhang ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 41159110b2e7SHong Zhang 41169110b2e7SHong Zhang PetscFunctionReturn(0); 41179110b2e7SHong Zhang } 41189110b2e7SHong Zhang 41199110b2e7SHong Zhang #undef __FUNCT__ 41202d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase" 41212d5ee99bSBarry Smith /*@C 41222d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 41232d5ee99bSBarry Smith 41242d5ee99bSBarry Smith Collective on TS 41252d5ee99bSBarry Smith 41262d5ee99bSBarry Smith Input Parameters: 41272d5ee99bSBarry Smith + ts - the TS context 41282d5ee99bSBarry Smith . step - current time-step 41292d5ee99bSBarry Smith . ptime - current time 41302d5ee99bSBarry Smith - dummy - either a viewer or NULL 41312d5ee99bSBarry Smith 41322d5ee99bSBarry Smith Level: intermediate 41332d5ee99bSBarry Smith 41342d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 41352d5ee99bSBarry Smith 41362d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 41372d5ee99bSBarry Smith @*/ 41382d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 41392d5ee99bSBarry Smith { 41402d5ee99bSBarry Smith PetscErrorCode ierr; 41412d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 41422d5ee99bSBarry Smith PetscDraw draw; 41432d5ee99bSBarry Smith MPI_Comm comm; 41442d5ee99bSBarry Smith PetscInt n; 41452d5ee99bSBarry Smith PetscMPIInt size; 414651fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 41472d5ee99bSBarry Smith char time[32]; 41482d5ee99bSBarry Smith const PetscScalar *U; 41492d5ee99bSBarry Smith 41502d5ee99bSBarry Smith PetscFunctionBegin; 41512d5ee99bSBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 41522d5ee99bSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 41532d5ee99bSBarry Smith if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs"); 41542d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 41552d5ee99bSBarry Smith if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 41562d5ee99bSBarry Smith 41572d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 41582d5ee99bSBarry Smith 41592d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 41604b363babSBarry Smith ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 4161ba5783adSMatthew G Knepley if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) { 4162a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 4163a4494fc1SBarry Smith PetscFunctionReturn(0); 4164a4494fc1SBarry Smith } 41652d5ee99bSBarry Smith if (!step) ictx->color++; 41662d5ee99bSBarry Smith ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr); 41672d5ee99bSBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 41682d5ee99bSBarry Smith 41692d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 41704b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 417185b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 41722d5ee99bSBarry Smith h = yl + .95*(yr - yl); 417351fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 41742d5ee99bSBarry Smith } 41752d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 41762d5ee99bSBarry Smith PetscFunctionReturn(0); 41772d5ee99bSBarry Smith } 41782d5ee99bSBarry Smith 41791713a123SBarry Smith 41806c699258SBarry Smith #undef __FUNCT__ 418183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 41826083293cSBarry Smith /*@C 418383a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 41846083293cSBarry Smith 41856083293cSBarry Smith Collective on TS 41866083293cSBarry Smith 41876083293cSBarry Smith Input Parameters: 41886083293cSBarry Smith . ctx - the monitor context 41896083293cSBarry Smith 41906083293cSBarry Smith Level: intermediate 41916083293cSBarry Smith 41926083293cSBarry Smith .keywords: TS, vector, monitor, view 41936083293cSBarry Smith 419483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 41956083293cSBarry Smith @*/ 419683a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 41976083293cSBarry Smith { 41986083293cSBarry Smith PetscErrorCode ierr; 41996083293cSBarry Smith 42006083293cSBarry Smith PetscFunctionBegin; 42014b363babSBarry Smith ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr); 420283a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 420383a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 420483a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 42056083293cSBarry Smith PetscFunctionReturn(0); 42066083293cSBarry Smith } 42076083293cSBarry Smith 42086083293cSBarry Smith #undef __FUNCT__ 420983a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 42106083293cSBarry Smith /*@C 421183a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 42126083293cSBarry Smith 42136083293cSBarry Smith Collective on TS 42146083293cSBarry Smith 42156083293cSBarry Smith Input Parameter: 42166083293cSBarry Smith . ts - time-step context 42176083293cSBarry Smith 42186083293cSBarry Smith Output Patameter: 42196083293cSBarry Smith . ctx - the monitor context 42206083293cSBarry Smith 422199fdda47SBarry Smith Options Database: 422299fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 422399fdda47SBarry Smith 42246083293cSBarry Smith Level: intermediate 42256083293cSBarry Smith 42266083293cSBarry Smith .keywords: TS, vector, monitor, view 42276083293cSBarry Smith 422883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 42296083293cSBarry Smith @*/ 423083a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 42316083293cSBarry Smith { 42326083293cSBarry Smith PetscErrorCode ierr; 42336083293cSBarry Smith 42346083293cSBarry Smith PetscFunctionBegin; 4235b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 423683a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4237e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4238bbd56ea5SKarl Rupp 423983a4ac43SBarry Smith (*ctx)->howoften = howoften; 4240473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 42410298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4242473a3ab2SBarry Smith 4243473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 42440298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 42452d5ee99bSBarry Smith (*ctx)->color = PETSC_DRAW_WHITE; 42466083293cSBarry Smith PetscFunctionReturn(0); 42476083293cSBarry Smith } 42486083293cSBarry Smith 42496083293cSBarry Smith #undef __FUNCT__ 425083a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 42513a471f94SBarry Smith /*@C 425283a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 42533a471f94SBarry Smith VecView() for the error at each timestep 42543a471f94SBarry Smith 42553a471f94SBarry Smith Collective on TS 42563a471f94SBarry Smith 42573a471f94SBarry Smith Input Parameters: 42583a471f94SBarry Smith + ts - the TS context 42593a471f94SBarry Smith . step - current time-step 42603a471f94SBarry Smith . ptime - current time 42610298fd71SBarry Smith - dummy - either a viewer or NULL 42623a471f94SBarry Smith 42633a471f94SBarry Smith Level: intermediate 42643a471f94SBarry Smith 42653a471f94SBarry Smith .keywords: TS, vector, monitor, view 42663a471f94SBarry Smith 42673a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 42683a471f94SBarry Smith @*/ 42690910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 42703a471f94SBarry Smith { 42713a471f94SBarry Smith PetscErrorCode ierr; 427283a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 427383a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 42743a471f94SBarry Smith Vec work; 42753a471f94SBarry Smith 42763a471f94SBarry Smith PetscFunctionBegin; 4277b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 42780910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 42793a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 42800910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 42813a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 42823a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 42833a471f94SBarry Smith PetscFunctionReturn(0); 42843a471f94SBarry Smith } 42853a471f94SBarry Smith 4286af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 42873a471f94SBarry Smith #undef __FUNCT__ 42886c699258SBarry Smith #define __FUNCT__ "TSSetDM" 42896c699258SBarry Smith /*@ 42906c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 42916c699258SBarry Smith 42923f9fe445SBarry Smith Logically Collective on TS and DM 42936c699258SBarry Smith 42946c699258SBarry Smith Input Parameters: 42956c699258SBarry Smith + ts - the preconditioner context 42966c699258SBarry Smith - dm - the dm 42976c699258SBarry Smith 42986c699258SBarry Smith Level: intermediate 42996c699258SBarry Smith 43006c699258SBarry Smith 43016c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 43026c699258SBarry Smith @*/ 43037087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 43046c699258SBarry Smith { 43056c699258SBarry Smith PetscErrorCode ierr; 4306089b2837SJed Brown SNES snes; 4307942e3340SBarry Smith DMTS tsdm; 43086c699258SBarry Smith 43096c699258SBarry Smith PetscFunctionBegin; 43100700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 431170663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4312942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 43132a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4314942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4315942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 431624989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 431724989b8cSPeter Brune tsdm->originaldm = dm; 431824989b8cSPeter Brune } 431924989b8cSPeter Brune } 43206bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 432124989b8cSPeter Brune } 43226c699258SBarry Smith ts->dm = dm; 4323bbd56ea5SKarl Rupp 4324089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4325089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 43266c699258SBarry Smith PetscFunctionReturn(0); 43276c699258SBarry Smith } 43286c699258SBarry Smith 43296c699258SBarry Smith #undef __FUNCT__ 43306c699258SBarry Smith #define __FUNCT__ "TSGetDM" 43316c699258SBarry Smith /*@ 43326c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 43336c699258SBarry Smith 43343f9fe445SBarry Smith Not Collective 43356c699258SBarry Smith 43366c699258SBarry Smith Input Parameter: 43376c699258SBarry Smith . ts - the preconditioner context 43386c699258SBarry Smith 43396c699258SBarry Smith Output Parameter: 43406c699258SBarry Smith . dm - the dm 43416c699258SBarry Smith 43426c699258SBarry Smith Level: intermediate 43436c699258SBarry Smith 43446c699258SBarry Smith 43456c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 43466c699258SBarry Smith @*/ 43477087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 43486c699258SBarry Smith { 4349496e6a7aSJed Brown PetscErrorCode ierr; 4350496e6a7aSJed Brown 43516c699258SBarry Smith PetscFunctionBegin; 43520700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4353496e6a7aSJed Brown if (!ts->dm) { 4354ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4355496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4356496e6a7aSJed Brown } 43576c699258SBarry Smith *dm = ts->dm; 43586c699258SBarry Smith PetscFunctionReturn(0); 43596c699258SBarry Smith } 43601713a123SBarry Smith 43610f5c6efeSJed Brown #undef __FUNCT__ 43620f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 43630f5c6efeSJed Brown /*@ 43640f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 43650f5c6efeSJed Brown 43663f9fe445SBarry Smith Logically Collective on SNES 43670f5c6efeSJed Brown 43680f5c6efeSJed Brown Input Parameter: 4369d42a1c89SJed Brown + snes - nonlinear solver 43700910c330SBarry Smith . U - the current state at which to evaluate the residual 4371d42a1c89SJed Brown - ctx - user context, must be a TS 43720f5c6efeSJed Brown 43730f5c6efeSJed Brown Output Parameter: 43740f5c6efeSJed Brown . F - the nonlinear residual 43750f5c6efeSJed Brown 43760f5c6efeSJed Brown Notes: 43770f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 43780f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 43790f5c6efeSJed Brown 43800f5c6efeSJed Brown Level: advanced 43810f5c6efeSJed Brown 43820f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 43830f5c6efeSJed Brown @*/ 43840910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 43850f5c6efeSJed Brown { 43860f5c6efeSJed Brown TS ts = (TS)ctx; 43870f5c6efeSJed Brown PetscErrorCode ierr; 43880f5c6efeSJed Brown 43890f5c6efeSJed Brown PetscFunctionBegin; 43900f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 43910910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 43920f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 43930f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 43940910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 43950f5c6efeSJed Brown PetscFunctionReturn(0); 43960f5c6efeSJed Brown } 43970f5c6efeSJed Brown 43980f5c6efeSJed Brown #undef __FUNCT__ 43990f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 44000f5c6efeSJed Brown /*@ 44010f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 44020f5c6efeSJed Brown 44030f5c6efeSJed Brown Collective on SNES 44040f5c6efeSJed Brown 44050f5c6efeSJed Brown Input Parameter: 44060f5c6efeSJed Brown + snes - nonlinear solver 44070910c330SBarry Smith . U - the current state at which to evaluate the residual 44080f5c6efeSJed Brown - ctx - user context, must be a TS 44090f5c6efeSJed Brown 44100f5c6efeSJed Brown Output Parameter: 44110f5c6efeSJed Brown + A - the Jacobian 44120f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 44130f5c6efeSJed Brown - flag - indicates any structure change in the matrix 44140f5c6efeSJed Brown 44150f5c6efeSJed Brown Notes: 44160f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 44170f5c6efeSJed Brown 44180f5c6efeSJed Brown Level: developer 44190f5c6efeSJed Brown 44200f5c6efeSJed Brown .seealso: SNESSetJacobian() 44210f5c6efeSJed Brown @*/ 4422d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 44230f5c6efeSJed Brown { 44240f5c6efeSJed Brown TS ts = (TS)ctx; 44250f5c6efeSJed Brown PetscErrorCode ierr; 44260f5c6efeSJed Brown 44270f5c6efeSJed Brown PetscFunctionBegin; 44280f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 44290910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 44300f5c6efeSJed Brown PetscValidPointer(A,3); 443194ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 44320f5c6efeSJed Brown PetscValidPointer(B,4); 443394ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 44340f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 4435d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 44360f5c6efeSJed Brown PetscFunctionReturn(0); 44370f5c6efeSJed Brown } 4438325fc9f4SBarry Smith 44390e4ef248SJed Brown #undef __FUNCT__ 44400e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 44410e4ef248SJed Brown /*@C 44420e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 44430e4ef248SJed Brown 44440e4ef248SJed Brown Collective on TS 44450e4ef248SJed Brown 44460e4ef248SJed Brown Input Arguments: 44470e4ef248SJed Brown + ts - time stepping context 44480e4ef248SJed Brown . t - time at which to evaluate 44490910c330SBarry Smith . U - state at which to evaluate 44500e4ef248SJed Brown - ctx - context 44510e4ef248SJed Brown 44520e4ef248SJed Brown Output Arguments: 44530e4ef248SJed Brown . F - right hand side 44540e4ef248SJed Brown 44550e4ef248SJed Brown Level: intermediate 44560e4ef248SJed Brown 44570e4ef248SJed Brown Notes: 44580e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 44590e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 44600e4ef248SJed Brown 44610e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 44620e4ef248SJed Brown @*/ 44630910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 44640e4ef248SJed Brown { 44650e4ef248SJed Brown PetscErrorCode ierr; 44660e4ef248SJed Brown Mat Arhs,Brhs; 44670e4ef248SJed Brown 44680e4ef248SJed Brown PetscFunctionBegin; 44690e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 4470d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 44710910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 44720e4ef248SJed Brown PetscFunctionReturn(0); 44730e4ef248SJed Brown } 44740e4ef248SJed Brown 44750e4ef248SJed Brown #undef __FUNCT__ 44760e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 44770e4ef248SJed Brown /*@C 44780e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 44790e4ef248SJed Brown 44800e4ef248SJed Brown Collective on TS 44810e4ef248SJed Brown 44820e4ef248SJed Brown Input Arguments: 44830e4ef248SJed Brown + ts - time stepping context 44840e4ef248SJed Brown . t - time at which to evaluate 44850910c330SBarry Smith . U - state at which to evaluate 44860e4ef248SJed Brown - ctx - context 44870e4ef248SJed Brown 44880e4ef248SJed Brown Output Arguments: 44890e4ef248SJed Brown + A - pointer to operator 44900e4ef248SJed Brown . B - pointer to preconditioning matrix 44910e4ef248SJed Brown - flg - matrix structure flag 44920e4ef248SJed Brown 44930e4ef248SJed Brown Level: intermediate 44940e4ef248SJed Brown 44950e4ef248SJed Brown Notes: 44960e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 44970e4ef248SJed Brown 44980e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 44990e4ef248SJed Brown @*/ 4500d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 45010e4ef248SJed Brown { 45020e4ef248SJed Brown PetscFunctionBegin; 45030e4ef248SJed Brown PetscFunctionReturn(0); 45040e4ef248SJed Brown } 45050e4ef248SJed Brown 45060026cea9SSean Farley #undef __FUNCT__ 45070026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 45080026cea9SSean Farley /*@C 45090026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 45100026cea9SSean Farley 45110026cea9SSean Farley Collective on TS 45120026cea9SSean Farley 45130026cea9SSean Farley Input Arguments: 45140026cea9SSean Farley + ts - time stepping context 45150026cea9SSean Farley . t - time at which to evaluate 45160910c330SBarry Smith . U - state at which to evaluate 45170910c330SBarry Smith . Udot - time derivative of state vector 45180026cea9SSean Farley - ctx - context 45190026cea9SSean Farley 45200026cea9SSean Farley Output Arguments: 45210026cea9SSean Farley . F - left hand side 45220026cea9SSean Farley 45230026cea9SSean Farley Level: intermediate 45240026cea9SSean Farley 45250026cea9SSean Farley Notes: 45260910c330SBarry 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 45270026cea9SSean Farley user is required to write their own TSComputeIFunction. 45280026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 45290026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 45300026cea9SSean Farley 45310026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 45320026cea9SSean Farley @*/ 45330910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 45340026cea9SSean Farley { 45350026cea9SSean Farley PetscErrorCode ierr; 45360026cea9SSean Farley Mat A,B; 45370026cea9SSean Farley 45380026cea9SSean Farley PetscFunctionBegin; 45390298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 4540d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 45410910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 45420026cea9SSean Farley PetscFunctionReturn(0); 45430026cea9SSean Farley } 45440026cea9SSean Farley 45450026cea9SSean Farley #undef __FUNCT__ 45460026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 45470026cea9SSean Farley /*@C 4548b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 45490026cea9SSean Farley 45500026cea9SSean Farley Collective on TS 45510026cea9SSean Farley 45520026cea9SSean Farley Input Arguments: 45530026cea9SSean Farley + ts - time stepping context 45540026cea9SSean Farley . t - time at which to evaluate 45550910c330SBarry Smith . U - state at which to evaluate 45560910c330SBarry Smith . Udot - time derivative of state vector 45570026cea9SSean Farley . shift - shift to apply 45580026cea9SSean Farley - ctx - context 45590026cea9SSean Farley 45600026cea9SSean Farley Output Arguments: 45610026cea9SSean Farley + A - pointer to operator 45620026cea9SSean Farley . B - pointer to preconditioning matrix 45630026cea9SSean Farley - flg - matrix structure flag 45640026cea9SSean Farley 4565b41af12eSJed Brown Level: advanced 45660026cea9SSean Farley 45670026cea9SSean Farley Notes: 45680026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 45690026cea9SSean Farley 4570b41af12eSJed Brown It is only appropriate for problems of the form 4571b41af12eSJed Brown 4572b41af12eSJed Brown $ M Udot = F(U,t) 4573b41af12eSJed Brown 4574b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4575b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4576b41af12eSJed Brown an implicit operator of the form 4577b41af12eSJed Brown 4578b41af12eSJed Brown $ shift*M + J 4579b41af12eSJed Brown 4580b41af12eSJed 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 4581b41af12eSJed Brown a copy of M or reassemble it when requested. 4582b41af12eSJed Brown 45830026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 45840026cea9SSean Farley @*/ 4585d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 45860026cea9SSean Farley { 4587b41af12eSJed Brown PetscErrorCode ierr; 4588b41af12eSJed Brown 45890026cea9SSean Farley PetscFunctionBegin; 459094ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4591b41af12eSJed Brown ts->ijacobian.shift = shift; 45920026cea9SSean Farley PetscFunctionReturn(0); 45930026cea9SSean Farley } 4594b41af12eSJed Brown 4595e817cc15SEmil Constantinescu #undef __FUNCT__ 4596e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType" 4597e817cc15SEmil Constantinescu /*@ 4598e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 4599e817cc15SEmil Constantinescu 4600e817cc15SEmil Constantinescu Not Collective 4601e817cc15SEmil Constantinescu 4602e817cc15SEmil Constantinescu Input Parameter: 4603e817cc15SEmil Constantinescu . ts - the TS context 4604e817cc15SEmil Constantinescu 4605e817cc15SEmil Constantinescu Output Parameter: 46064e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4607e817cc15SEmil Constantinescu 4608e817cc15SEmil Constantinescu Level: beginner 4609e817cc15SEmil Constantinescu 4610e817cc15SEmil Constantinescu .keywords: TS, equation type 4611e817cc15SEmil Constantinescu 4612e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 4613e817cc15SEmil Constantinescu @*/ 4614e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4615e817cc15SEmil Constantinescu { 4616e817cc15SEmil Constantinescu PetscFunctionBegin; 4617e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4618e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 4619e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 4620e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4621e817cc15SEmil Constantinescu } 4622e817cc15SEmil Constantinescu 4623e817cc15SEmil Constantinescu #undef __FUNCT__ 4624e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType" 4625e817cc15SEmil Constantinescu /*@ 4626e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 4627e817cc15SEmil Constantinescu 4628e817cc15SEmil Constantinescu Not Collective 4629e817cc15SEmil Constantinescu 4630e817cc15SEmil Constantinescu Input Parameter: 4631e817cc15SEmil Constantinescu + ts - the TS context 46321297b224SEmil Constantinescu - equation_type - see TSEquationType 4633e817cc15SEmil Constantinescu 4634e817cc15SEmil Constantinescu Level: advanced 4635e817cc15SEmil Constantinescu 4636e817cc15SEmil Constantinescu .keywords: TS, equation type 4637e817cc15SEmil Constantinescu 4638e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 4639e817cc15SEmil Constantinescu @*/ 4640e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4641e817cc15SEmil Constantinescu { 4642e817cc15SEmil Constantinescu PetscFunctionBegin; 4643e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4644e817cc15SEmil Constantinescu ts->equation_type = equation_type; 4645e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4646e817cc15SEmil Constantinescu } 46470026cea9SSean Farley 46484af1b03aSJed Brown #undef __FUNCT__ 46494af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 46504af1b03aSJed Brown /*@ 46514af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 46524af1b03aSJed Brown 46534af1b03aSJed Brown Not Collective 46544af1b03aSJed Brown 46554af1b03aSJed Brown Input Parameter: 46564af1b03aSJed Brown . ts - the TS context 46574af1b03aSJed Brown 46584af1b03aSJed Brown Output Parameter: 46594af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 46604af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 46614af1b03aSJed Brown 4662487e0bb9SJed Brown Level: beginner 46634af1b03aSJed Brown 4664cd652676SJed Brown Notes: 4665cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 46664af1b03aSJed Brown 46674af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 46684af1b03aSJed Brown 46694af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 46704af1b03aSJed Brown @*/ 46714af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 46724af1b03aSJed Brown { 46734af1b03aSJed Brown PetscFunctionBegin; 46744af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 46754af1b03aSJed Brown PetscValidPointer(reason,2); 46764af1b03aSJed Brown *reason = ts->reason; 46774af1b03aSJed Brown PetscFunctionReturn(0); 46784af1b03aSJed Brown } 46794af1b03aSJed Brown 4680fb1732b5SBarry Smith #undef __FUNCT__ 4681d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 4682d6ad946cSShri Abhyankar /*@ 4683d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4684d6ad946cSShri Abhyankar 4685d6ad946cSShri Abhyankar Not Collective 4686d6ad946cSShri Abhyankar 4687d6ad946cSShri Abhyankar Input Parameter: 4688d6ad946cSShri Abhyankar + ts - the TS context 4689d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4690d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4691d6ad946cSShri Abhyankar 4692f5abba47SShri Abhyankar Level: advanced 4693d6ad946cSShri Abhyankar 4694d6ad946cSShri Abhyankar Notes: 4695d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 4696d6ad946cSShri Abhyankar 4697d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 4698d6ad946cSShri Abhyankar 4699d6ad946cSShri Abhyankar .seealso: TSConvergedReason 4700d6ad946cSShri Abhyankar @*/ 4701d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4702d6ad946cSShri Abhyankar { 4703d6ad946cSShri Abhyankar PetscFunctionBegin; 4704d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4705d6ad946cSShri Abhyankar ts->reason = reason; 4706d6ad946cSShri Abhyankar PetscFunctionReturn(0); 4707d6ad946cSShri Abhyankar } 4708d6ad946cSShri Abhyankar 4709d6ad946cSShri Abhyankar #undef __FUNCT__ 4710cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 4711cc708dedSBarry Smith /*@ 4712cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 4713cc708dedSBarry Smith 4714cc708dedSBarry Smith Not Collective 4715cc708dedSBarry Smith 4716cc708dedSBarry Smith Input Parameter: 4717cc708dedSBarry Smith . ts - the TS context 4718cc708dedSBarry Smith 4719cc708dedSBarry Smith Output Parameter: 4720cc708dedSBarry Smith . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 4721cc708dedSBarry Smith 4722487e0bb9SJed Brown Level: beginner 4723cc708dedSBarry Smith 4724cc708dedSBarry Smith Notes: 4725cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 4726cc708dedSBarry Smith 4727cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 4728cc708dedSBarry Smith 4729cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 4730cc708dedSBarry Smith @*/ 4731cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 4732cc708dedSBarry Smith { 4733cc708dedSBarry Smith PetscFunctionBegin; 4734cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4735cc708dedSBarry Smith PetscValidPointer(ftime,2); 4736cc708dedSBarry Smith *ftime = ts->solvetime; 4737cc708dedSBarry Smith PetscFunctionReturn(0); 4738cc708dedSBarry Smith } 4739cc708dedSBarry Smith 4740cc708dedSBarry Smith #undef __FUNCT__ 47412c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps" 47422c18e0fdSBarry Smith /*@ 47432c18e0fdSBarry Smith TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 47442c18e0fdSBarry Smith 47452c18e0fdSBarry Smith Not Collective 47462c18e0fdSBarry Smith 47472c18e0fdSBarry Smith Input Parameter: 47482c18e0fdSBarry Smith . ts - the TS context 47492c18e0fdSBarry Smith 47502c18e0fdSBarry Smith Output Parameter: 47512c18e0fdSBarry Smith . steps - the number of steps 47522c18e0fdSBarry Smith 47532c18e0fdSBarry Smith Level: beginner 47542c18e0fdSBarry Smith 47552c18e0fdSBarry Smith Notes: 47562c18e0fdSBarry Smith Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 47572c18e0fdSBarry Smith 47582c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test 47592c18e0fdSBarry Smith 47602c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 47612c18e0fdSBarry Smith @*/ 47622c18e0fdSBarry Smith PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 47632c18e0fdSBarry Smith { 47642c18e0fdSBarry Smith PetscFunctionBegin; 47652c18e0fdSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 47662c18e0fdSBarry Smith PetscValidPointer(steps,2); 47672c18e0fdSBarry Smith *steps = ts->total_steps; 47682c18e0fdSBarry Smith PetscFunctionReturn(0); 47692c18e0fdSBarry Smith } 47702c18e0fdSBarry Smith 47712c18e0fdSBarry Smith #undef __FUNCT__ 47725ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 47739f67acb7SJed Brown /*@ 47745ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 47759f67acb7SJed Brown used by the time integrator. 47769f67acb7SJed Brown 47779f67acb7SJed Brown Not Collective 47789f67acb7SJed Brown 47799f67acb7SJed Brown Input Parameter: 47809f67acb7SJed Brown . ts - TS context 47819f67acb7SJed Brown 47829f67acb7SJed Brown Output Parameter: 47839f67acb7SJed Brown . nits - number of nonlinear iterations 47849f67acb7SJed Brown 47859f67acb7SJed Brown Notes: 47869f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 47879f67acb7SJed Brown 47889f67acb7SJed Brown Level: intermediate 47899f67acb7SJed Brown 47909f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 47919f67acb7SJed Brown 47925ef26d82SJed Brown .seealso: TSGetKSPIterations() 47939f67acb7SJed Brown @*/ 47945ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 47959f67acb7SJed Brown { 47969f67acb7SJed Brown PetscFunctionBegin; 47979f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 47989f67acb7SJed Brown PetscValidIntPointer(nits,2); 47995ef26d82SJed Brown *nits = ts->snes_its; 48009f67acb7SJed Brown PetscFunctionReturn(0); 48019f67acb7SJed Brown } 48029f67acb7SJed Brown 48039f67acb7SJed Brown #undef __FUNCT__ 48045ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 48059f67acb7SJed Brown /*@ 48065ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 48079f67acb7SJed Brown used by the time integrator. 48089f67acb7SJed Brown 48099f67acb7SJed Brown Not Collective 48109f67acb7SJed Brown 48119f67acb7SJed Brown Input Parameter: 48129f67acb7SJed Brown . ts - TS context 48139f67acb7SJed Brown 48149f67acb7SJed Brown Output Parameter: 48159f67acb7SJed Brown . lits - number of linear iterations 48169f67acb7SJed Brown 48179f67acb7SJed Brown Notes: 48189f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 48199f67acb7SJed Brown 48209f67acb7SJed Brown Level: intermediate 48219f67acb7SJed Brown 48229f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 48239f67acb7SJed Brown 48245ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 48259f67acb7SJed Brown @*/ 48265ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 48279f67acb7SJed Brown { 48289f67acb7SJed Brown PetscFunctionBegin; 48299f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 48309f67acb7SJed Brown PetscValidIntPointer(lits,2); 48315ef26d82SJed Brown *lits = ts->ksp_its; 48329f67acb7SJed Brown PetscFunctionReturn(0); 48339f67acb7SJed Brown } 48349f67acb7SJed Brown 48359f67acb7SJed Brown #undef __FUNCT__ 4836cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 4837cef5090cSJed Brown /*@ 4838cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4839cef5090cSJed Brown 4840cef5090cSJed Brown Not Collective 4841cef5090cSJed Brown 4842cef5090cSJed Brown Input Parameter: 4843cef5090cSJed Brown . ts - TS context 4844cef5090cSJed Brown 4845cef5090cSJed Brown Output Parameter: 4846cef5090cSJed Brown . rejects - number of steps rejected 4847cef5090cSJed Brown 4848cef5090cSJed Brown Notes: 4849cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4850cef5090cSJed Brown 4851cef5090cSJed Brown Level: intermediate 4852cef5090cSJed Brown 4853cef5090cSJed Brown .keywords: TS, get, number 4854cef5090cSJed Brown 48555ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 4856cef5090cSJed Brown @*/ 4857cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 4858cef5090cSJed Brown { 4859cef5090cSJed Brown PetscFunctionBegin; 4860cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4861cef5090cSJed Brown PetscValidIntPointer(rejects,2); 4862cef5090cSJed Brown *rejects = ts->reject; 4863cef5090cSJed Brown PetscFunctionReturn(0); 4864cef5090cSJed Brown } 4865cef5090cSJed Brown 4866cef5090cSJed Brown #undef __FUNCT__ 4867cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 4868cef5090cSJed Brown /*@ 4869cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 4870cef5090cSJed Brown 4871cef5090cSJed Brown Not Collective 4872cef5090cSJed Brown 4873cef5090cSJed Brown Input Parameter: 4874cef5090cSJed Brown . ts - TS context 4875cef5090cSJed Brown 4876cef5090cSJed Brown Output Parameter: 4877cef5090cSJed Brown . fails - number of failed nonlinear solves 4878cef5090cSJed Brown 4879cef5090cSJed Brown Notes: 4880cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4881cef5090cSJed Brown 4882cef5090cSJed Brown Level: intermediate 4883cef5090cSJed Brown 4884cef5090cSJed Brown .keywords: TS, get, number 4885cef5090cSJed Brown 48865ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 4887cef5090cSJed Brown @*/ 4888cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 4889cef5090cSJed Brown { 4890cef5090cSJed Brown PetscFunctionBegin; 4891cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4892cef5090cSJed Brown PetscValidIntPointer(fails,2); 4893cef5090cSJed Brown *fails = ts->num_snes_failures; 4894cef5090cSJed Brown PetscFunctionReturn(0); 4895cef5090cSJed Brown } 4896cef5090cSJed Brown 4897cef5090cSJed Brown #undef __FUNCT__ 4898cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 4899cef5090cSJed Brown /*@ 4900cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 4901cef5090cSJed Brown 4902cef5090cSJed Brown Not Collective 4903cef5090cSJed Brown 4904cef5090cSJed Brown Input Parameter: 4905cef5090cSJed Brown + ts - TS context 4906cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 4907cef5090cSJed Brown 4908cef5090cSJed Brown Notes: 4909cef5090cSJed Brown The counter is reset to zero for each step 4910cef5090cSJed Brown 4911cef5090cSJed Brown Options Database Key: 4912cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4913cef5090cSJed Brown 4914cef5090cSJed Brown Level: intermediate 4915cef5090cSJed Brown 4916cef5090cSJed Brown .keywords: TS, set, maximum, number 4917cef5090cSJed Brown 49185ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4919cef5090cSJed Brown @*/ 4920cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 4921cef5090cSJed Brown { 4922cef5090cSJed Brown PetscFunctionBegin; 4923cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4924cef5090cSJed Brown ts->max_reject = rejects; 4925cef5090cSJed Brown PetscFunctionReturn(0); 4926cef5090cSJed Brown } 4927cef5090cSJed Brown 4928cef5090cSJed Brown #undef __FUNCT__ 4929cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 4930cef5090cSJed Brown /*@ 4931cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 4932cef5090cSJed Brown 4933cef5090cSJed Brown Not Collective 4934cef5090cSJed Brown 4935cef5090cSJed Brown Input Parameter: 4936cef5090cSJed Brown + ts - TS context 4937cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 4938cef5090cSJed Brown 4939cef5090cSJed Brown Notes: 4940cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 4941cef5090cSJed Brown 4942cef5090cSJed Brown Options Database Key: 4943cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4944cef5090cSJed Brown 4945cef5090cSJed Brown Level: intermediate 4946cef5090cSJed Brown 4947cef5090cSJed Brown .keywords: TS, set, maximum, number 4948cef5090cSJed Brown 49495ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 4950cef5090cSJed Brown @*/ 4951cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 4952cef5090cSJed Brown { 4953cef5090cSJed Brown PetscFunctionBegin; 4954cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4955cef5090cSJed Brown ts->max_snes_failures = fails; 4956cef5090cSJed Brown PetscFunctionReturn(0); 4957cef5090cSJed Brown } 4958cef5090cSJed Brown 4959cef5090cSJed Brown #undef __FUNCT__ 49604e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails" 4961cef5090cSJed Brown /*@ 4962cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 4963cef5090cSJed Brown 4964cef5090cSJed Brown Not Collective 4965cef5090cSJed Brown 4966cef5090cSJed Brown Input Parameter: 4967cef5090cSJed Brown + ts - TS context 4968cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 4969cef5090cSJed Brown 4970cef5090cSJed Brown Options Database Key: 4971cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 4972cef5090cSJed Brown 4973cef5090cSJed Brown Level: intermediate 4974cef5090cSJed Brown 4975cef5090cSJed Brown .keywords: TS, set, error 4976cef5090cSJed Brown 49775ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4978cef5090cSJed Brown @*/ 4979cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 4980cef5090cSJed Brown { 4981cef5090cSJed Brown PetscFunctionBegin; 4982cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4983cef5090cSJed Brown ts->errorifstepfailed = err; 4984cef5090cSJed Brown PetscFunctionReturn(0); 4985cef5090cSJed Brown } 4986cef5090cSJed Brown 4987cef5090cSJed Brown #undef __FUNCT__ 4988fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 4989fb1732b5SBarry Smith /*@C 4990fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 4991fb1732b5SBarry Smith 4992fb1732b5SBarry Smith Collective on TS 4993fb1732b5SBarry Smith 4994fb1732b5SBarry Smith Input Parameters: 4995fb1732b5SBarry Smith + ts - the TS context 4996fb1732b5SBarry Smith . step - current time-step 4997fb1732b5SBarry Smith . ptime - current time 49980910c330SBarry Smith . u - current state 4999fb1732b5SBarry Smith - viewer - binary viewer 5000fb1732b5SBarry Smith 5001fb1732b5SBarry Smith Level: intermediate 5002fb1732b5SBarry Smith 5003fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 5004fb1732b5SBarry Smith 5005fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5006fb1732b5SBarry Smith @*/ 50070910c330SBarry Smith PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 5008fb1732b5SBarry Smith { 5009fb1732b5SBarry Smith PetscErrorCode ierr; 5010ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 5011fb1732b5SBarry Smith 5012fb1732b5SBarry Smith PetscFunctionBegin; 50130910c330SBarry Smith ierr = VecView(u,v);CHKERRQ(ierr); 5014ed81e22dSJed Brown PetscFunctionReturn(0); 5015ed81e22dSJed Brown } 5016ed81e22dSJed Brown 5017ed81e22dSJed Brown #undef __FUNCT__ 5018ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 5019ed81e22dSJed Brown /*@C 5020ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5021ed81e22dSJed Brown 5022ed81e22dSJed Brown Collective on TS 5023ed81e22dSJed Brown 5024ed81e22dSJed Brown Input Parameters: 5025ed81e22dSJed Brown + ts - the TS context 5026ed81e22dSJed Brown . step - current time-step 5027ed81e22dSJed Brown . ptime - current time 50280910c330SBarry Smith . u - current state 5029ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5030ed81e22dSJed Brown 5031ed81e22dSJed Brown Level: intermediate 5032ed81e22dSJed Brown 5033ed81e22dSJed Brown Notes: 5034ed81e22dSJed 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. 5035ed81e22dSJed Brown These are named according to the file name template. 5036ed81e22dSJed Brown 5037ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5038ed81e22dSJed Brown 5039ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5040ed81e22dSJed Brown 5041ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5042ed81e22dSJed Brown @*/ 50430910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5044ed81e22dSJed Brown { 5045ed81e22dSJed Brown PetscErrorCode ierr; 5046ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 5047ed81e22dSJed Brown PetscViewer viewer; 5048ed81e22dSJed Brown 5049ed81e22dSJed Brown PetscFunctionBegin; 50508caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5051ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 50520910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 5053ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5054ed81e22dSJed Brown PetscFunctionReturn(0); 5055ed81e22dSJed Brown } 5056ed81e22dSJed Brown 5057ed81e22dSJed Brown #undef __FUNCT__ 5058ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 5059ed81e22dSJed Brown /*@C 5060ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5061ed81e22dSJed Brown 5062ed81e22dSJed Brown Collective on TS 5063ed81e22dSJed Brown 5064ed81e22dSJed Brown Input Parameters: 5065ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5066ed81e22dSJed Brown 5067ed81e22dSJed Brown Level: intermediate 5068ed81e22dSJed Brown 5069ed81e22dSJed Brown Note: 5070ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5071ed81e22dSJed Brown 5072ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5073ed81e22dSJed Brown 5074ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5075ed81e22dSJed Brown @*/ 5076ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5077ed81e22dSJed Brown { 5078ed81e22dSJed Brown PetscErrorCode ierr; 5079ed81e22dSJed Brown 5080ed81e22dSJed Brown PetscFunctionBegin; 5081ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5082fb1732b5SBarry Smith PetscFunctionReturn(0); 5083fb1732b5SBarry Smith } 5084fb1732b5SBarry Smith 508584df9cb4SJed Brown #undef __FUNCT__ 5086552698daSJed Brown #define __FUNCT__ "TSGetAdapt" 508784df9cb4SJed Brown /*@ 5088552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 508984df9cb4SJed Brown 5090ed81e22dSJed Brown Collective on TS if controller has not been created yet 509184df9cb4SJed Brown 509284df9cb4SJed Brown Input Arguments: 5093ed81e22dSJed Brown . ts - time stepping context 509484df9cb4SJed Brown 509584df9cb4SJed Brown Output Arguments: 5096ed81e22dSJed Brown . adapt - adaptive controller 509784df9cb4SJed Brown 509884df9cb4SJed Brown Level: intermediate 509984df9cb4SJed Brown 5100ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 510184df9cb4SJed Brown @*/ 5102552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 510384df9cb4SJed Brown { 510484df9cb4SJed Brown PetscErrorCode ierr; 510584df9cb4SJed Brown 510684df9cb4SJed Brown PetscFunctionBegin; 510784df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 510884df9cb4SJed Brown PetscValidPointer(adapt,2); 510984df9cb4SJed Brown if (!ts->adapt) { 5110ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 51113bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 51121c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 511384df9cb4SJed Brown } 511484df9cb4SJed Brown *adapt = ts->adapt; 511584df9cb4SJed Brown PetscFunctionReturn(0); 511684df9cb4SJed Brown } 5117d6ebe24aSShri Abhyankar 5118d6ebe24aSShri Abhyankar #undef __FUNCT__ 51191c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 51201c3436cfSJed Brown /*@ 51211c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 51221c3436cfSJed Brown 51231c3436cfSJed Brown Logically Collective 51241c3436cfSJed Brown 51251c3436cfSJed Brown Input Arguments: 51261c3436cfSJed Brown + ts - time integration context 51271c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 51280298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 51291c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 51300298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 51311c3436cfSJed Brown 5132a3cdaa26SBarry Smith Options Database keys: 5133a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5134a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5135a3cdaa26SBarry Smith 51363ff766beSShri Abhyankar Notes: 51373ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 51383ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 51393ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 51403ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 51413ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 51423ff766beSShri Abhyankar differential variables. 51433ff766beSShri Abhyankar 51441c3436cfSJed Brown Level: beginner 51451c3436cfSJed Brown 5146c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 51471c3436cfSJed Brown @*/ 51481c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 51491c3436cfSJed Brown { 51501c3436cfSJed Brown PetscErrorCode ierr; 51511c3436cfSJed Brown 51521c3436cfSJed Brown PetscFunctionBegin; 5153c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 51541c3436cfSJed Brown if (vatol) { 51551c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 51561c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 5157bbd56ea5SKarl Rupp 51581c3436cfSJed Brown ts->vatol = vatol; 51591c3436cfSJed Brown } 5160c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 51611c3436cfSJed Brown if (vrtol) { 51621c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 51631c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 5164bbd56ea5SKarl Rupp 51651c3436cfSJed Brown ts->vrtol = vrtol; 51661c3436cfSJed Brown } 51671c3436cfSJed Brown PetscFunctionReturn(0); 51681c3436cfSJed Brown } 51691c3436cfSJed Brown 51701c3436cfSJed Brown #undef __FUNCT__ 5171c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 5172c5033834SJed Brown /*@ 5173c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5174c5033834SJed Brown 5175c5033834SJed Brown Logically Collective 5176c5033834SJed Brown 5177c5033834SJed Brown Input Arguments: 5178c5033834SJed Brown . ts - time integration context 5179c5033834SJed Brown 5180c5033834SJed Brown Output Arguments: 51810298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 51820298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 51830298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 51840298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 5185c5033834SJed Brown 5186c5033834SJed Brown Level: beginner 5187c5033834SJed Brown 5188c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5189c5033834SJed Brown @*/ 5190c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5191c5033834SJed Brown { 5192c5033834SJed Brown PetscFunctionBegin; 5193c5033834SJed Brown if (atol) *atol = ts->atol; 5194c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5195c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5196c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 5197c5033834SJed Brown PetscFunctionReturn(0); 5198c5033834SJed Brown } 5199c5033834SJed Brown 5200c5033834SJed Brown #undef __FUNCT__ 52019c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2" 52029c6b16b5SShri Abhyankar /*@ 5203a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 52049c6b16b5SShri Abhyankar 52059c6b16b5SShri Abhyankar Collective on TS 52069c6b16b5SShri Abhyankar 52079c6b16b5SShri Abhyankar Input Arguments: 52089c6b16b5SShri Abhyankar + ts - time stepping context 5209a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5210a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 52119c6b16b5SShri Abhyankar 52129c6b16b5SShri Abhyankar Output Arguments: 52139c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 52149c6b16b5SShri Abhyankar 52159c6b16b5SShri Abhyankar Level: developer 52169c6b16b5SShri Abhyankar 5217deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 52189c6b16b5SShri Abhyankar @*/ 5219a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm) 52209c6b16b5SShri Abhyankar { 52219c6b16b5SShri Abhyankar PetscErrorCode ierr; 52229c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 52239c6b16b5SShri Abhyankar const PetscScalar *u,*y; 52249c6b16b5SShri Abhyankar PetscReal sum,gsum; 52259c6b16b5SShri Abhyankar PetscReal tol; 52269c6b16b5SShri Abhyankar 52279c6b16b5SShri Abhyankar PetscFunctionBegin; 52289c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5229a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5230a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5231a4868fbcSLisandro Dalcin PetscValidType(U,2); 5232a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5233a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5234a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5235a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 52369c6b16b5SShri Abhyankar 52379c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 52389c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 52399c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 52409c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 52419c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 52429c6b16b5SShri Abhyankar sum = 0.; 52439c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 52449c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 52459c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52469c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52479c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 52489c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52499c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 52509c6b16b5SShri Abhyankar } 52519c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52529c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52539c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 52549c6b16b5SShri Abhyankar const PetscScalar *atol; 52559c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52569c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 52579c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52589c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 52599c6b16b5SShri Abhyankar } 52609c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52619c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 52629c6b16b5SShri Abhyankar const PetscScalar *rtol; 52639c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52649c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 52659c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52669c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 52679c6b16b5SShri Abhyankar } 52689c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52699c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 52709c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 52719c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52729c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 52739c6b16b5SShri Abhyankar } 52749c6b16b5SShri Abhyankar } 52759c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 52769c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 52779c6b16b5SShri Abhyankar 52789c6b16b5SShri Abhyankar ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 52799c6b16b5SShri Abhyankar *norm = PetscSqrtReal(gsum / N); 52809c6b16b5SShri Abhyankar 52819c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 52829c6b16b5SShri Abhyankar PetscFunctionReturn(0); 52839c6b16b5SShri Abhyankar } 52849c6b16b5SShri Abhyankar 52859c6b16b5SShri Abhyankar #undef __FUNCT__ 52869c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity" 52879c6b16b5SShri Abhyankar /*@ 5288a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 52899c6b16b5SShri Abhyankar 52909c6b16b5SShri Abhyankar Collective on TS 52919c6b16b5SShri Abhyankar 52929c6b16b5SShri Abhyankar Input Arguments: 52939c6b16b5SShri Abhyankar + ts - time stepping context 5294a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5295a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 52969c6b16b5SShri Abhyankar 52979c6b16b5SShri Abhyankar Output Arguments: 52989c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 52999c6b16b5SShri Abhyankar 53009c6b16b5SShri Abhyankar Level: developer 53019c6b16b5SShri Abhyankar 5302deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 53039c6b16b5SShri Abhyankar @*/ 5304a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm) 53059c6b16b5SShri Abhyankar { 53069c6b16b5SShri Abhyankar PetscErrorCode ierr; 53079c6b16b5SShri Abhyankar PetscInt i,n,N,rstart,k; 53089c6b16b5SShri Abhyankar const PetscScalar *u,*y; 53099c6b16b5SShri Abhyankar PetscReal max,gmax; 53109c6b16b5SShri Abhyankar PetscReal tol; 53119c6b16b5SShri Abhyankar 53129c6b16b5SShri Abhyankar PetscFunctionBegin; 53139c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5314a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5315a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5316a4868fbcSLisandro Dalcin PetscValidType(U,2); 5317a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5318a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5319a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5320a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 53219c6b16b5SShri Abhyankar 53229c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 53239c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 53249c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 53259c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 53269c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 53279c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 53289c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 53299c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53309c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 53319c6b16b5SShri Abhyankar k = 0; 53329c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 53339c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 53349c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 53359c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53369c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 53379c6b16b5SShri Abhyankar } 53389c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53399c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 53409c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 53419c6b16b5SShri Abhyankar const PetscScalar *atol; 53429c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53439c6b16b5SShri Abhyankar k = 0; 53449c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 53459c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 53469c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 53479c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53489c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 53499c6b16b5SShri Abhyankar } 53509c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53519c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 53529c6b16b5SShri Abhyankar const PetscScalar *rtol; 53539c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 53549c6b16b5SShri Abhyankar k = 0; 53559c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 53569c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 53579c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 53589c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53599c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 53609c6b16b5SShri Abhyankar } 53619c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 53629c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 53639c6b16b5SShri Abhyankar k = 0; 53649c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 53659c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 53669c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 53679c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53689c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 53699c6b16b5SShri Abhyankar } 53709c6b16b5SShri Abhyankar } 53719c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 53729c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 53739c6b16b5SShri Abhyankar 53749c6b16b5SShri Abhyankar ierr = MPI_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 53759c6b16b5SShri Abhyankar *norm = gmax; 53769c6b16b5SShri Abhyankar 53779c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 53789c6b16b5SShri Abhyankar PetscFunctionReturn(0); 53799c6b16b5SShri Abhyankar } 53809c6b16b5SShri Abhyankar 53819c6b16b5SShri Abhyankar #undef __FUNCT__ 53827619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm" 53831c3436cfSJed Brown /*@ 5384a4868fbcSLisandro Dalcin TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors 53851c3436cfSJed Brown 53861c3436cfSJed Brown Collective on TS 53871c3436cfSJed Brown 53881c3436cfSJed Brown Input Arguments: 53891c3436cfSJed Brown + ts - time stepping context 5390a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5391a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5392a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 53937619abb3SShri 53941c3436cfSJed Brown Output Arguments: 53951c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 53961c3436cfSJed Brown 5397a4868fbcSLisandro Dalcin 5398a4868fbcSLisandro Dalcin Options Database Keys: 5399a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5400a4868fbcSLisandro Dalcin 54011c3436cfSJed Brown Level: developer 54021c3436cfSJed Brown 5403deea92deSShri .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 54041c3436cfSJed Brown @*/ 5405a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm) 54061c3436cfSJed Brown { 54078beabaa1SBarry Smith PetscErrorCode ierr; 54081c3436cfSJed Brown 54091c3436cfSJed Brown PetscFunctionBegin; 5410a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 5411a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNorm2(ts,U,Y,norm);CHKERRQ(ierr); 5412a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 5413a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm);CHKERRQ(ierr); 5414a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 54151c3436cfSJed Brown PetscFunctionReturn(0); 54161c3436cfSJed Brown } 54171c3436cfSJed Brown 54181c3436cfSJed Brown #undef __FUNCT__ 54198d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 54208d59e960SJed Brown /*@ 54218d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 54228d59e960SJed Brown 54238d59e960SJed Brown Logically Collective on TS 54248d59e960SJed Brown 54258d59e960SJed Brown Input Arguments: 54268d59e960SJed Brown + ts - time stepping context 54278d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 54288d59e960SJed Brown 54298d59e960SJed Brown Note: 54308d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 54318d59e960SJed Brown 54328d59e960SJed Brown Level: intermediate 54338d59e960SJed Brown 54348d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 54358d59e960SJed Brown @*/ 54368d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 54378d59e960SJed Brown { 54388d59e960SJed Brown PetscFunctionBegin; 54398d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 54408d59e960SJed Brown ts->cfltime_local = cfltime; 54418d59e960SJed Brown ts->cfltime = -1.; 54428d59e960SJed Brown PetscFunctionReturn(0); 54438d59e960SJed Brown } 54448d59e960SJed Brown 54458d59e960SJed Brown #undef __FUNCT__ 54468d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 54478d59e960SJed Brown /*@ 54488d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 54498d59e960SJed Brown 54508d59e960SJed Brown Collective on TS 54518d59e960SJed Brown 54528d59e960SJed Brown Input Arguments: 54538d59e960SJed Brown . ts - time stepping context 54548d59e960SJed Brown 54558d59e960SJed Brown Output Arguments: 54568d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 54578d59e960SJed Brown 54588d59e960SJed Brown Level: advanced 54598d59e960SJed Brown 54608d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 54618d59e960SJed Brown @*/ 54628d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 54638d59e960SJed Brown { 54648d59e960SJed Brown PetscErrorCode ierr; 54658d59e960SJed Brown 54668d59e960SJed Brown PetscFunctionBegin; 54678d59e960SJed Brown if (ts->cfltime < 0) { 5468ce94432eSBarry Smith ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 54698d59e960SJed Brown } 54708d59e960SJed Brown *cfltime = ts->cfltime; 54718d59e960SJed Brown PetscFunctionReturn(0); 54728d59e960SJed Brown } 54738d59e960SJed Brown 54748d59e960SJed Brown #undef __FUNCT__ 5475d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 5476d6ebe24aSShri Abhyankar /*@ 5477d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5478d6ebe24aSShri Abhyankar 5479d6ebe24aSShri Abhyankar Input Parameters: 5480d6ebe24aSShri Abhyankar . ts - the TS context. 5481d6ebe24aSShri Abhyankar . xl - lower bound. 5482d6ebe24aSShri Abhyankar . xu - upper bound. 5483d6ebe24aSShri Abhyankar 5484d6ebe24aSShri Abhyankar Notes: 5485d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 5486e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 5487d6ebe24aSShri Abhyankar 54882bd2b0e6SSatish Balay Level: advanced 54892bd2b0e6SSatish Balay 5490d6ebe24aSShri Abhyankar @*/ 5491d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5492d6ebe24aSShri Abhyankar { 5493d6ebe24aSShri Abhyankar PetscErrorCode ierr; 5494d6ebe24aSShri Abhyankar SNES snes; 5495d6ebe24aSShri Abhyankar 5496d6ebe24aSShri Abhyankar PetscFunctionBegin; 5497d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 5498d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 5499d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 5500d6ebe24aSShri Abhyankar } 5501d6ebe24aSShri Abhyankar 5502325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 5503c6db04a5SJed Brown #include <mex.h> 5504325fc9f4SBarry Smith 5505325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 5506325fc9f4SBarry Smith 5507325fc9f4SBarry Smith #undef __FUNCT__ 5508325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 5509325fc9f4SBarry Smith /* 5510325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 5511325fc9f4SBarry Smith TSSetFunctionMatlab(). 5512325fc9f4SBarry Smith 5513325fc9f4SBarry Smith Collective on TS 5514325fc9f4SBarry Smith 5515325fc9f4SBarry Smith Input Parameters: 5516325fc9f4SBarry Smith + snes - the TS context 55170910c330SBarry Smith - u - input vector 5518325fc9f4SBarry Smith 5519325fc9f4SBarry Smith Output Parameter: 5520325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 5521325fc9f4SBarry Smith 5522325fc9f4SBarry Smith Notes: 5523325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 5524325fc9f4SBarry Smith implementations, so most users would not generally call this routine 5525325fc9f4SBarry Smith themselves. 5526325fc9f4SBarry Smith 5527325fc9f4SBarry Smith Level: developer 5528325fc9f4SBarry Smith 5529325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5530325fc9f4SBarry Smith 5531325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5532325fc9f4SBarry Smith */ 55330910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 5534325fc9f4SBarry Smith { 5535325fc9f4SBarry Smith PetscErrorCode ierr; 5536325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5537325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 5538325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 5539325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 5540325fc9f4SBarry Smith 5541325fc9f4SBarry Smith PetscFunctionBegin; 5542325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 55430910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 55440910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 5545325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 55460910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 5547325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 5548325fc9f4SBarry Smith 5549325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 55500910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 55510910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 55520910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 5553bbd56ea5SKarl Rupp 5554325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5555325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 5556325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5557325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5558325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 5559325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 5560325fc9f4SBarry Smith prhs[6] = sctx->ctx; 5561325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 5562325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5563325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5564325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5565325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5566325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5567325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5568325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5569325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5570325fc9f4SBarry Smith PetscFunctionReturn(0); 5571325fc9f4SBarry Smith } 5572325fc9f4SBarry Smith 5573325fc9f4SBarry Smith 5574325fc9f4SBarry Smith #undef __FUNCT__ 5575325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 5576325fc9f4SBarry Smith /* 5577325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 5578325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 5579e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 5580325fc9f4SBarry Smith 5581325fc9f4SBarry Smith Logically Collective on TS 5582325fc9f4SBarry Smith 5583325fc9f4SBarry Smith Input Parameters: 5584325fc9f4SBarry Smith + ts - the TS context 5585325fc9f4SBarry Smith - func - function evaluation routine 5586325fc9f4SBarry Smith 5587325fc9f4SBarry Smith Calling sequence of func: 55880910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 5589325fc9f4SBarry Smith 5590325fc9f4SBarry Smith Level: beginner 5591325fc9f4SBarry Smith 5592325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5593325fc9f4SBarry Smith 5594325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5595325fc9f4SBarry Smith */ 5596cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 5597325fc9f4SBarry Smith { 5598325fc9f4SBarry Smith PetscErrorCode ierr; 5599325fc9f4SBarry Smith TSMatlabContext *sctx; 5600325fc9f4SBarry Smith 5601325fc9f4SBarry Smith PetscFunctionBegin; 5602325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5603325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5604325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5605325fc9f4SBarry Smith /* 5606325fc9f4SBarry Smith This should work, but it doesn't 5607325fc9f4SBarry Smith sctx->ctx = ctx; 5608325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5609325fc9f4SBarry Smith */ 5610325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5611bbd56ea5SKarl Rupp 56120298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 5613325fc9f4SBarry Smith PetscFunctionReturn(0); 5614325fc9f4SBarry Smith } 5615325fc9f4SBarry Smith 5616325fc9f4SBarry Smith #undef __FUNCT__ 5617325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 5618325fc9f4SBarry Smith /* 5619325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 5620325fc9f4SBarry Smith TSSetJacobianMatlab(). 5621325fc9f4SBarry Smith 5622325fc9f4SBarry Smith Collective on TS 5623325fc9f4SBarry Smith 5624325fc9f4SBarry Smith Input Parameters: 5625cdcf91faSSean Farley + ts - the TS context 56260910c330SBarry Smith . u - input vector 5627325fc9f4SBarry Smith . A, B - the matrices 5628325fc9f4SBarry Smith - ctx - user context 5629325fc9f4SBarry Smith 5630325fc9f4SBarry Smith Level: developer 5631325fc9f4SBarry Smith 5632325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5633325fc9f4SBarry Smith 5634325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5635325fc9f4SBarry Smith @*/ 5636f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 5637325fc9f4SBarry Smith { 5638325fc9f4SBarry Smith PetscErrorCode ierr; 5639325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5640325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 5641325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 5642325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 5643325fc9f4SBarry Smith 5644325fc9f4SBarry Smith PetscFunctionBegin; 5645cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 56460910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 5647325fc9f4SBarry Smith 56480910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 5649325fc9f4SBarry Smith 5650cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 56510910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 56520910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 56530910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 56540910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 5655bbd56ea5SKarl Rupp 5656325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5657325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 5658325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5659325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5660325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 5661325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 5662325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 5663325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 5664325fc9f4SBarry Smith prhs[8] = sctx->ctx; 5665325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 5666325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5667325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5668325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5669325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5670325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5671325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5672325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5673325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 5674325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 5675325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5676325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 5677325fc9f4SBarry Smith PetscFunctionReturn(0); 5678325fc9f4SBarry Smith } 5679325fc9f4SBarry Smith 5680325fc9f4SBarry Smith 5681325fc9f4SBarry Smith #undef __FUNCT__ 5682325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 5683325fc9f4SBarry Smith /* 5684325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 5685e3c5b3baSBarry 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 5686325fc9f4SBarry Smith 5687325fc9f4SBarry Smith Logically Collective on TS 5688325fc9f4SBarry Smith 5689325fc9f4SBarry Smith Input Parameters: 5690cdcf91faSSean Farley + ts - the TS context 5691325fc9f4SBarry Smith . A,B - Jacobian matrices 5692325fc9f4SBarry Smith . func - function evaluation routine 5693325fc9f4SBarry Smith - ctx - user context 5694325fc9f4SBarry Smith 5695325fc9f4SBarry Smith Calling sequence of func: 56960910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 5697325fc9f4SBarry Smith 5698325fc9f4SBarry Smith 5699325fc9f4SBarry Smith Level: developer 5700325fc9f4SBarry Smith 5701325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5702325fc9f4SBarry Smith 5703325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5704325fc9f4SBarry Smith */ 5705cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 5706325fc9f4SBarry Smith { 5707325fc9f4SBarry Smith PetscErrorCode ierr; 5708325fc9f4SBarry Smith TSMatlabContext *sctx; 5709325fc9f4SBarry Smith 5710325fc9f4SBarry Smith PetscFunctionBegin; 5711325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5712325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5713325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5714325fc9f4SBarry Smith /* 5715325fc9f4SBarry Smith This should work, but it doesn't 5716325fc9f4SBarry Smith sctx->ctx = ctx; 5717325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5718325fc9f4SBarry Smith */ 5719325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5720bbd56ea5SKarl Rupp 5721cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 5722325fc9f4SBarry Smith PetscFunctionReturn(0); 5723325fc9f4SBarry Smith } 5724325fc9f4SBarry Smith 5725b5b1a830SBarry Smith #undef __FUNCT__ 5726b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 5727b5b1a830SBarry Smith /* 5728b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 5729b5b1a830SBarry Smith 5730b5b1a830SBarry Smith Collective on TS 5731b5b1a830SBarry Smith 5732b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5733b5b1a830SBarry Smith @*/ 57340910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 5735b5b1a830SBarry Smith { 5736b5b1a830SBarry Smith PetscErrorCode ierr; 5737b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5738a530c242SBarry Smith int nlhs = 1,nrhs = 6; 5739b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 5740b5b1a830SBarry Smith long long int lx = 0,ls = 0; 5741b5b1a830SBarry Smith 5742b5b1a830SBarry Smith PetscFunctionBegin; 5743cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 57440910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 5745b5b1a830SBarry Smith 5746cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 57470910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 5748bbd56ea5SKarl Rupp 5749b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5750b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 5751b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 5752b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 5753b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 5754b5b1a830SBarry Smith prhs[5] = sctx->ctx; 5755b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 5756b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5757b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 5758b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 5759b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 5760b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 5761b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 5762b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 5763b5b1a830SBarry Smith PetscFunctionReturn(0); 5764b5b1a830SBarry Smith } 5765b5b1a830SBarry Smith 5766b5b1a830SBarry Smith 5767b5b1a830SBarry Smith #undef __FUNCT__ 5768b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 5769b5b1a830SBarry Smith /* 5770b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 5771b5b1a830SBarry Smith 5772b5b1a830SBarry Smith Level: developer 5773b5b1a830SBarry Smith 5774b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 5775b5b1a830SBarry Smith 5776b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5777b5b1a830SBarry Smith */ 5778cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 5779b5b1a830SBarry Smith { 5780b5b1a830SBarry Smith PetscErrorCode ierr; 5781b5b1a830SBarry Smith TSMatlabContext *sctx; 5782b5b1a830SBarry Smith 5783b5b1a830SBarry Smith PetscFunctionBegin; 5784b5b1a830SBarry Smith /* currently sctx is memory bleed */ 5785b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5786b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5787b5b1a830SBarry Smith /* 5788b5b1a830SBarry Smith This should work, but it doesn't 5789b5b1a830SBarry Smith sctx->ctx = ctx; 5790b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5791b5b1a830SBarry Smith */ 5792b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5793bbd56ea5SKarl Rupp 57940298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 5795b5b1a830SBarry Smith PetscFunctionReturn(0); 5796b5b1a830SBarry Smith } 5797325fc9f4SBarry Smith #endif 5798b3603a34SBarry Smith 5799b3603a34SBarry Smith #undef __FUNCT__ 58004f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 5801b3603a34SBarry Smith /*@C 58024f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 5803b3603a34SBarry Smith in a time based line graph 5804b3603a34SBarry Smith 5805b3603a34SBarry Smith Collective on TS 5806b3603a34SBarry Smith 5807b3603a34SBarry Smith Input Parameters: 5808b3603a34SBarry Smith + ts - the TS context 5809b3603a34SBarry Smith . step - current time-step 5810b3603a34SBarry Smith . ptime - current time 58117db568b7SBarry Smith . u - current solution 58127db568b7SBarry Smith - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 5813b3603a34SBarry Smith 5814b3d3934dSBarry Smith Options Database: 58159ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 5816b3d3934dSBarry Smith 5817b3603a34SBarry Smith Level: intermediate 5818b3603a34SBarry Smith 58190b039ecaSBarry Smith Notes: each process in a parallel run displays its component solutions in a separate window 5820b3603a34SBarry Smith 5821b3603a34SBarry Smith .keywords: TS, vector, monitor, view 5822b3603a34SBarry Smith 58237db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 58247db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 58257db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 58267db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 5827b3603a34SBarry Smith @*/ 58287db568b7SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 5829b3603a34SBarry Smith { 5830b3603a34SBarry Smith PetscErrorCode ierr; 58317db568b7SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 5832b3603a34SBarry Smith const PetscScalar *yy; 583358ff32f7SBarry Smith PetscInt dim; 583480666b62SBarry Smith Vec v; 5835b3603a34SBarry Smith 5836b3603a34SBarry Smith PetscFunctionBegin; 583758ff32f7SBarry Smith if (!step) { 5838a9f9c1f6SBarry Smith PetscDrawAxis axis; 5839a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5840a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 5841387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 5842387f4636SBarry Smith char **displaynames; 5843387f4636SBarry Smith PetscBool flg; 5844387f4636SBarry Smith 5845387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 5846387f4636SBarry Smith ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr); 5847387f4636SBarry Smith ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 58489ae14b6eSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 5849387f4636SBarry Smith if (flg) { 5850a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 5851387f4636SBarry Smith } 5852387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 5853387f4636SBarry Smith } 5854387f4636SBarry Smith if (ctx->displaynames) { 5855387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 5856387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 5857387f4636SBarry Smith } else if (ctx->names) { 58580910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 58590b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 5860387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 5861387f4636SBarry Smith } 58620b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 586358ff32f7SBarry Smith } 586480666b62SBarry Smith if (ctx->transform) { 5865e673d494SBarry Smith ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr); 586680666b62SBarry Smith } else { 586780666b62SBarry Smith v = u; 586880666b62SBarry Smith } 586980666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 5870e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 5871e3efe391SJed Brown { 5872e3efe391SJed Brown PetscReal *yreal; 5873e3efe391SJed Brown PetscInt i,n; 587480666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 5875785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 5876e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 58770b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 5878e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 5879e3efe391SJed Brown } 5880e3efe391SJed Brown #else 5881387f4636SBarry Smith if (ctx->displaynames) { 5882387f4636SBarry Smith PetscInt i; 5883387f4636SBarry Smith for (i=0; i<ctx->ndisplayvariables; i++) { 5884387f4636SBarry Smith ctx->displayvalues[i] = yy[ctx->displayvariables[i]]; 5885387f4636SBarry Smith } 5886387f4636SBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 5887387f4636SBarry Smith } else { 58880b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 5889387f4636SBarry Smith } 5890e3efe391SJed Brown #endif 589180666b62SBarry Smith ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 589280666b62SBarry Smith if (ctx->transform) { 589380666b62SBarry Smith ierr = VecDestroy(&v);CHKERRQ(ierr); 589480666b62SBarry Smith } 5895b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 58960b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 58973923b477SBarry Smith } 5898b3603a34SBarry Smith PetscFunctionReturn(0); 5899b3603a34SBarry Smith } 5900b3603a34SBarry Smith 5901387f4636SBarry Smith 5902b3603a34SBarry Smith #undef __FUNCT__ 590331152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames" 5904b037adc7SBarry Smith /*@C 590531152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 5906b037adc7SBarry Smith 5907b037adc7SBarry Smith Collective on TS 5908b037adc7SBarry Smith 5909b037adc7SBarry Smith Input Parameters: 5910b037adc7SBarry Smith + ts - the TS context 5911b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 5912b037adc7SBarry Smith 5913b037adc7SBarry Smith Level: intermediate 5914b037adc7SBarry Smith 59157db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 59167db568b7SBarry Smith 5917b037adc7SBarry Smith .keywords: TS, vector, monitor, view 5918b037adc7SBarry Smith 5919a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 5920b037adc7SBarry Smith @*/ 592131152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 5922b037adc7SBarry Smith { 5923b037adc7SBarry Smith PetscErrorCode ierr; 5924b037adc7SBarry Smith PetscInt i; 5925b037adc7SBarry Smith 5926b037adc7SBarry Smith PetscFunctionBegin; 5927b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 5928b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 59295537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 5930b3d3934dSBarry Smith break; 5931b3d3934dSBarry Smith } 5932b3d3934dSBarry Smith } 5933b3d3934dSBarry Smith PetscFunctionReturn(0); 5934b3d3934dSBarry Smith } 5935b3d3934dSBarry Smith 5936b3d3934dSBarry Smith #undef __FUNCT__ 5937e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames" 5938e673d494SBarry Smith /*@C 5939e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 5940e673d494SBarry Smith 5941e673d494SBarry Smith Collective on TS 5942e673d494SBarry Smith 5943e673d494SBarry Smith Input Parameters: 5944e673d494SBarry Smith + ts - the TS context 5945e673d494SBarry Smith - names - the names of the components, final string must be NULL 5946e673d494SBarry Smith 5947e673d494SBarry Smith Level: intermediate 5948e673d494SBarry Smith 5949e673d494SBarry Smith .keywords: TS, vector, monitor, view 5950e673d494SBarry Smith 5951a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 5952e673d494SBarry Smith @*/ 5953e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 5954e673d494SBarry Smith { 5955e673d494SBarry Smith PetscErrorCode ierr; 5956e673d494SBarry Smith 5957e673d494SBarry Smith PetscFunctionBegin; 5958e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 5959e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 5960e673d494SBarry Smith PetscFunctionReturn(0); 5961e673d494SBarry Smith } 5962e673d494SBarry Smith 5963e673d494SBarry Smith #undef __FUNCT__ 5964b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames" 5965b3d3934dSBarry Smith /*@C 5966b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 5967b3d3934dSBarry Smith 5968b3d3934dSBarry Smith Collective on TS 5969b3d3934dSBarry Smith 5970b3d3934dSBarry Smith Input Parameter: 5971b3d3934dSBarry Smith . ts - the TS context 5972b3d3934dSBarry Smith 5973b3d3934dSBarry Smith Output Parameter: 5974b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 5975b3d3934dSBarry Smith 5976b3d3934dSBarry Smith Level: intermediate 5977b3d3934dSBarry Smith 59787db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 59797db568b7SBarry Smith 5980b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 5981b3d3934dSBarry Smith 5982b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 5983b3d3934dSBarry Smith @*/ 5984b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 5985b3d3934dSBarry Smith { 5986b3d3934dSBarry Smith PetscInt i; 5987b3d3934dSBarry Smith 5988b3d3934dSBarry Smith PetscFunctionBegin; 5989b3d3934dSBarry Smith *names = NULL; 5990b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 5991b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 59925537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 5993b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 5994b3d3934dSBarry Smith break; 5995387f4636SBarry Smith } 5996387f4636SBarry Smith } 5997387f4636SBarry Smith PetscFunctionReturn(0); 5998387f4636SBarry Smith } 5999387f4636SBarry Smith 6000387f4636SBarry Smith #undef __FUNCT__ 6001a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables" 6002a66092f1SBarry Smith /*@C 6003a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 6004a66092f1SBarry Smith 6005a66092f1SBarry Smith Collective on TS 6006a66092f1SBarry Smith 6007a66092f1SBarry Smith Input Parameters: 6008a66092f1SBarry Smith + ctx - the TSMonitorLG context 6009a66092f1SBarry Smith . displaynames - the names of the components, final string must be NULL 6010a66092f1SBarry Smith 6011a66092f1SBarry Smith Level: intermediate 6012a66092f1SBarry Smith 6013a66092f1SBarry Smith .keywords: TS, vector, monitor, view 6014a66092f1SBarry Smith 6015a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6016a66092f1SBarry Smith @*/ 6017a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 6018a66092f1SBarry Smith { 6019a66092f1SBarry Smith PetscInt j = 0,k; 6020a66092f1SBarry Smith PetscErrorCode ierr; 6021a66092f1SBarry Smith 6022a66092f1SBarry Smith PetscFunctionBegin; 6023a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 6024a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 6025a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 6026a66092f1SBarry Smith while (displaynames[j]) j++; 6027a66092f1SBarry Smith ctx->ndisplayvariables = j; 6028a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 6029a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 6030a66092f1SBarry Smith j = 0; 6031a66092f1SBarry Smith while (displaynames[j]) { 6032a66092f1SBarry Smith k = 0; 6033a66092f1SBarry Smith while (ctx->names[k]) { 6034a66092f1SBarry Smith PetscBool flg; 6035a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 6036a66092f1SBarry Smith if (flg) { 6037a66092f1SBarry Smith ctx->displayvariables[j] = k; 6038a66092f1SBarry Smith break; 6039a66092f1SBarry Smith } 6040a66092f1SBarry Smith k++; 6041a66092f1SBarry Smith } 6042a66092f1SBarry Smith j++; 6043a66092f1SBarry Smith } 6044a66092f1SBarry Smith PetscFunctionReturn(0); 6045a66092f1SBarry Smith } 6046a66092f1SBarry Smith 6047a66092f1SBarry Smith 6048a66092f1SBarry Smith #undef __FUNCT__ 6049387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables" 6050387f4636SBarry Smith /*@C 6051387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 6052387f4636SBarry Smith 6053387f4636SBarry Smith Collective on TS 6054387f4636SBarry Smith 6055387f4636SBarry Smith Input Parameters: 6056387f4636SBarry Smith + ts - the TS context 6057387f4636SBarry Smith . displaynames - the names of the components, final string must be NULL 6058387f4636SBarry Smith 60597db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 60607db568b7SBarry Smith 6061387f4636SBarry Smith Level: intermediate 6062387f4636SBarry Smith 6063387f4636SBarry Smith .keywords: TS, vector, monitor, view 6064387f4636SBarry Smith 6065387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6066387f4636SBarry Smith @*/ 6067387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 6068387f4636SBarry Smith { 6069a66092f1SBarry Smith PetscInt i; 6070387f4636SBarry Smith PetscErrorCode ierr; 6071387f4636SBarry Smith 6072387f4636SBarry Smith PetscFunctionBegin; 6073387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6074387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 60755537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 6076b3d3934dSBarry Smith break; 6077b037adc7SBarry Smith } 6078b037adc7SBarry Smith } 6079b037adc7SBarry Smith PetscFunctionReturn(0); 6080b037adc7SBarry Smith } 6081b037adc7SBarry Smith 6082b037adc7SBarry Smith #undef __FUNCT__ 608380666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform" 608480666b62SBarry Smith /*@C 608580666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 608680666b62SBarry Smith 608780666b62SBarry Smith Collective on TS 608880666b62SBarry Smith 608980666b62SBarry Smith Input Parameters: 609080666b62SBarry Smith + ts - the TS context 609180666b62SBarry Smith . transform - the transform function 60927684fa3eSBarry Smith . destroy - function to destroy the optional context 609380666b62SBarry Smith - ctx - optional context used by transform function 609480666b62SBarry Smith 60957db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 60967db568b7SBarry Smith 609780666b62SBarry Smith Level: intermediate 609880666b62SBarry Smith 609980666b62SBarry Smith .keywords: TS, vector, monitor, view 610080666b62SBarry Smith 6101a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 610280666b62SBarry Smith @*/ 61037684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 610480666b62SBarry Smith { 610580666b62SBarry Smith PetscInt i; 6106a66092f1SBarry Smith PetscErrorCode ierr; 610780666b62SBarry Smith 610880666b62SBarry Smith PetscFunctionBegin; 610980666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 611080666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 61115537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 611280666b62SBarry Smith } 611380666b62SBarry Smith } 611480666b62SBarry Smith PetscFunctionReturn(0); 611580666b62SBarry Smith } 611680666b62SBarry Smith 611780666b62SBarry Smith #undef __FUNCT__ 6118e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform" 6119e673d494SBarry Smith /*@C 6120e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 6121e673d494SBarry Smith 6122e673d494SBarry Smith Collective on TSLGCtx 6123e673d494SBarry Smith 6124e673d494SBarry Smith Input Parameters: 6125e673d494SBarry Smith + ts - the TS context 6126e673d494SBarry Smith . transform - the transform function 61277684fa3eSBarry Smith . destroy - function to destroy the optional context 6128e673d494SBarry Smith - ctx - optional context used by transform function 6129e673d494SBarry Smith 6130e673d494SBarry Smith Level: intermediate 6131e673d494SBarry Smith 6132e673d494SBarry Smith .keywords: TS, vector, monitor, view 6133e673d494SBarry Smith 6134a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 6135e673d494SBarry Smith @*/ 61367684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 6137e673d494SBarry Smith { 6138e673d494SBarry Smith PetscFunctionBegin; 6139e673d494SBarry Smith ctx->transform = transform; 61407684fa3eSBarry Smith ctx->transformdestroy = destroy; 6141e673d494SBarry Smith ctx->transformctx = tctx; 6142e673d494SBarry Smith PetscFunctionReturn(0); 6143e673d494SBarry Smith } 6144e673d494SBarry Smith 6145b3603a34SBarry Smith #undef __FUNCT__ 61464f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 6147ef20d060SBarry Smith /*@C 61484f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 6149ef20d060SBarry Smith in a time based line graph 6150ef20d060SBarry Smith 6151ef20d060SBarry Smith Collective on TS 6152ef20d060SBarry Smith 6153ef20d060SBarry Smith Input Parameters: 6154ef20d060SBarry Smith + ts - the TS context 6155ef20d060SBarry Smith . step - current time-step 6156ef20d060SBarry Smith . ptime - current time 61577db568b7SBarry Smith . u - current solution 61587db568b7SBarry Smith - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 6159ef20d060SBarry Smith 6160ef20d060SBarry Smith Level: intermediate 6161ef20d060SBarry Smith 6162abd5a294SJed Brown Notes: 6163abd5a294SJed Brown Only for sequential solves. 6164abd5a294SJed Brown 6165abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 6166abd5a294SJed Brown 6167abd5a294SJed Brown Options Database Keys: 61684f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 6169ef20d060SBarry Smith 6170ef20d060SBarry Smith .keywords: TS, vector, monitor, view 6171ef20d060SBarry Smith 6172abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 6173ef20d060SBarry Smith @*/ 61740910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 6175ef20d060SBarry Smith { 6176ef20d060SBarry Smith PetscErrorCode ierr; 61770b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 6178ef20d060SBarry Smith const PetscScalar *yy; 6179ef20d060SBarry Smith Vec y; 6180a9f9c1f6SBarry Smith PetscInt dim; 6181ef20d060SBarry Smith 6182ef20d060SBarry Smith PetscFunctionBegin; 6183a9f9c1f6SBarry Smith if (!step) { 6184a9f9c1f6SBarry Smith PetscDrawAxis axis; 6185a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 61861ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 61870910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6188a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6189a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6190a9f9c1f6SBarry Smith } 61910910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 6192ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 61930910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 6194ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 6195e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6196e3efe391SJed Brown { 6197e3efe391SJed Brown PetscReal *yreal; 6198e3efe391SJed Brown PetscInt i,n; 6199e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 6200785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6201e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 62020b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6203e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6204e3efe391SJed Brown } 6205e3efe391SJed Brown #else 62060b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6207e3efe391SJed Brown #endif 6208ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 6209ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 6210b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 62110b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 62123923b477SBarry Smith } 6213ef20d060SBarry Smith PetscFunctionReturn(0); 6214ef20d060SBarry Smith } 6215ef20d060SBarry Smith 6216201da799SBarry Smith #undef __FUNCT__ 6217201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 6218201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6219201da799SBarry Smith { 6220201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6221201da799SBarry Smith PetscReal x = ptime,y; 6222201da799SBarry Smith PetscErrorCode ierr; 6223201da799SBarry Smith PetscInt its; 6224201da799SBarry Smith 6225201da799SBarry Smith PetscFunctionBegin; 6226201da799SBarry Smith if (!n) { 6227201da799SBarry Smith PetscDrawAxis axis; 6228bbd56ea5SKarl Rupp 6229201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6230201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 6231201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6232bbd56ea5SKarl Rupp 6233201da799SBarry Smith ctx->snes_its = 0; 6234201da799SBarry Smith } 6235201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 6236201da799SBarry Smith y = its - ctx->snes_its; 6237201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 62383fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6239201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 6240201da799SBarry Smith } 6241201da799SBarry Smith ctx->snes_its = its; 6242201da799SBarry Smith PetscFunctionReturn(0); 6243201da799SBarry Smith } 6244201da799SBarry Smith 6245201da799SBarry Smith #undef __FUNCT__ 6246201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 6247201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6248201da799SBarry Smith { 6249201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6250201da799SBarry Smith PetscReal x = ptime,y; 6251201da799SBarry Smith PetscErrorCode ierr; 6252201da799SBarry Smith PetscInt its; 6253201da799SBarry Smith 6254201da799SBarry Smith PetscFunctionBegin; 6255201da799SBarry Smith if (!n) { 6256201da799SBarry Smith PetscDrawAxis axis; 6257bbd56ea5SKarl Rupp 6258201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6259201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 6260201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6261bbd56ea5SKarl Rupp 6262201da799SBarry Smith ctx->ksp_its = 0; 6263201da799SBarry Smith } 6264201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 6265201da799SBarry Smith y = its - ctx->ksp_its; 6266201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 626799fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6268201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 6269201da799SBarry Smith } 6270201da799SBarry Smith ctx->ksp_its = its; 6271201da799SBarry Smith PetscFunctionReturn(0); 6272201da799SBarry Smith } 6273f9c1d6abSBarry Smith 6274f9c1d6abSBarry Smith #undef __FUNCT__ 6275f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 6276f9c1d6abSBarry Smith /*@ 6277f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 6278f9c1d6abSBarry Smith 6279f9c1d6abSBarry Smith Collective on TS and Vec 6280f9c1d6abSBarry Smith 6281f9c1d6abSBarry Smith Input Parameters: 6282f9c1d6abSBarry Smith + ts - the TS context 6283f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 6284f9c1d6abSBarry Smith 6285f9c1d6abSBarry Smith Output Parameters: 6286f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 6287f9c1d6abSBarry Smith 6288f9c1d6abSBarry Smith Level: developer 6289f9c1d6abSBarry Smith 6290f9c1d6abSBarry Smith .keywords: TS, compute 6291f9c1d6abSBarry Smith 6292f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 6293f9c1d6abSBarry Smith @*/ 6294f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 6295f9c1d6abSBarry Smith { 6296f9c1d6abSBarry Smith PetscErrorCode ierr; 6297f9c1d6abSBarry Smith 6298f9c1d6abSBarry Smith PetscFunctionBegin; 6299f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6300ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 6301f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 6302f9c1d6abSBarry Smith PetscFunctionReturn(0); 6303f9c1d6abSBarry Smith } 630424655328SShri 6305b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 6306b3d3934dSBarry Smith #undef __FUNCT__ 6307b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate" 6308b3d3934dSBarry Smith /*@C 6309b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 6310b3d3934dSBarry Smith 6311b3d3934dSBarry Smith Collective on TS 6312b3d3934dSBarry Smith 6313b3d3934dSBarry Smith Input Parameters: 6314b3d3934dSBarry Smith . ts - the ODE solver object 6315b3d3934dSBarry Smith 6316b3d3934dSBarry Smith Output Parameter: 6317b3d3934dSBarry Smith . ctx - the context 6318b3d3934dSBarry Smith 6319b3d3934dSBarry Smith Level: intermediate 6320b3d3934dSBarry Smith 6321b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso 6322b3d3934dSBarry Smith 6323b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 6324b3d3934dSBarry Smith 6325b3d3934dSBarry Smith @*/ 6326b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 6327b3d3934dSBarry Smith { 6328b3d3934dSBarry Smith PetscErrorCode ierr; 6329b3d3934dSBarry Smith 6330b3d3934dSBarry Smith PetscFunctionBegin; 6331a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 6332b3d3934dSBarry Smith PetscFunctionReturn(0); 6333b3d3934dSBarry Smith } 6334b3d3934dSBarry Smith 6335b3d3934dSBarry Smith #undef __FUNCT__ 6336b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope" 6337b3d3934dSBarry Smith /*@C 6338b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 6339b3d3934dSBarry Smith 6340b3d3934dSBarry Smith Collective on TS 6341b3d3934dSBarry Smith 6342b3d3934dSBarry Smith Input Parameters: 6343b3d3934dSBarry Smith + ts - the TS context 6344b3d3934dSBarry Smith . step - current time-step 6345b3d3934dSBarry Smith . ptime - current time 63467db568b7SBarry Smith . u - current solution 63477db568b7SBarry Smith - dctx - the envelope context 6348b3d3934dSBarry Smith 6349b3d3934dSBarry Smith Options Database: 6350b3d3934dSBarry Smith . -ts_monitor_envelope 6351b3d3934dSBarry Smith 6352b3d3934dSBarry Smith Level: intermediate 6353b3d3934dSBarry Smith 6354b3d3934dSBarry Smith Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 6355b3d3934dSBarry Smith 6356b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6357b3d3934dSBarry Smith 63587db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 6359b3d3934dSBarry Smith @*/ 63607db568b7SBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6361b3d3934dSBarry Smith { 6362b3d3934dSBarry Smith PetscErrorCode ierr; 63637db568b7SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 6364b3d3934dSBarry Smith 6365b3d3934dSBarry Smith PetscFunctionBegin; 6366b3d3934dSBarry Smith if (!ctx->max) { 6367b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 6368b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 6369b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 6370b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 6371b3d3934dSBarry Smith } else { 6372b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 6373b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 6374b3d3934dSBarry Smith } 6375b3d3934dSBarry Smith PetscFunctionReturn(0); 6376b3d3934dSBarry Smith } 6377b3d3934dSBarry Smith 6378b3d3934dSBarry Smith 6379b3d3934dSBarry Smith #undef __FUNCT__ 6380b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds" 6381b3d3934dSBarry Smith /*@C 6382b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 6383b3d3934dSBarry Smith 6384b3d3934dSBarry Smith Collective on TS 6385b3d3934dSBarry Smith 6386b3d3934dSBarry Smith Input Parameter: 6387b3d3934dSBarry Smith . ts - the TS context 6388b3d3934dSBarry Smith 6389b3d3934dSBarry Smith Output Parameter: 6390b3d3934dSBarry Smith + max - the maximum values 6391b3d3934dSBarry Smith - min - the minimum values 6392b3d3934dSBarry Smith 63937db568b7SBarry Smith Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 63947db568b7SBarry Smith 6395b3d3934dSBarry Smith Level: intermediate 6396b3d3934dSBarry Smith 6397b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6398b3d3934dSBarry Smith 6399b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6400b3d3934dSBarry Smith @*/ 6401b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 6402b3d3934dSBarry Smith { 6403b3d3934dSBarry Smith PetscInt i; 6404b3d3934dSBarry Smith 6405b3d3934dSBarry Smith PetscFunctionBegin; 6406b3d3934dSBarry Smith if (max) *max = NULL; 6407b3d3934dSBarry Smith if (min) *min = NULL; 6408b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6409b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 64105537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 6411b3d3934dSBarry Smith if (max) *max = ctx->max; 6412b3d3934dSBarry Smith if (min) *min = ctx->min; 6413b3d3934dSBarry Smith break; 6414b3d3934dSBarry Smith } 6415b3d3934dSBarry Smith } 6416b3d3934dSBarry Smith PetscFunctionReturn(0); 6417b3d3934dSBarry Smith } 6418b3d3934dSBarry Smith 6419b3d3934dSBarry Smith #undef __FUNCT__ 6420b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy" 6421b3d3934dSBarry Smith /*@C 6422b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 6423b3d3934dSBarry Smith 6424b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 6425b3d3934dSBarry Smith 6426b3d3934dSBarry Smith Input Parameter: 6427b3d3934dSBarry Smith . ctx - the monitor context 6428b3d3934dSBarry Smith 6429b3d3934dSBarry Smith Level: intermediate 6430b3d3934dSBarry Smith 6431b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy 6432b3d3934dSBarry Smith 64337db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 6434b3d3934dSBarry Smith @*/ 6435b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 6436b3d3934dSBarry Smith { 6437b3d3934dSBarry Smith PetscErrorCode ierr; 6438b3d3934dSBarry Smith 6439b3d3934dSBarry Smith PetscFunctionBegin; 6440b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 6441b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 6442b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 6443b3d3934dSBarry Smith PetscFunctionReturn(0); 6444b3d3934dSBarry Smith } 6445f2dee214SBarry Smith 644624655328SShri #undef __FUNCT__ 644724655328SShri #define __FUNCT__ "TSRollBack" 644824655328SShri /*@ 644924655328SShri TSRollBack - Rolls back one time step 645024655328SShri 645124655328SShri Collective on TS 645224655328SShri 645324655328SShri Input Parameter: 645424655328SShri . ts - the TS context obtained from TSCreate() 645524655328SShri 645624655328SShri Level: advanced 645724655328SShri 645824655328SShri .keywords: TS, timestep, rollback 645924655328SShri 646024655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 646124655328SShri @*/ 646224655328SShri PetscErrorCode TSRollBack(TS ts) 646324655328SShri { 646424655328SShri PetscErrorCode ierr; 646524655328SShri 646624655328SShri PetscFunctionBegin; 646724655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 646824655328SShri 646924655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 647024655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 647124655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 647224655328SShri ts->ptime = ts->ptime_prev; 64738392e04aSShri Abhyankar ts->steprollback = PETSC_TRUE; /* Flag to indicate that the step is rollbacked */ 647424655328SShri PetscFunctionReturn(0); 647524655328SShri } 6476aeb4809dSShri Abhyankar 6477ff22ae23SHong Zhang #undef __FUNCT__ 6478ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages" 6479ff22ae23SHong Zhang /*@ 6480ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 6481ff22ae23SHong Zhang 6482ff22ae23SHong Zhang Input Parameter: 6483ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 6484ff22ae23SHong Zhang 6485ff22ae23SHong Zhang Level: advanced 6486ff22ae23SHong Zhang 6487ff22ae23SHong Zhang .keywords: TS, getstages 6488ff22ae23SHong Zhang 6489ff22ae23SHong Zhang .seealso: TSCreate() 6490ff22ae23SHong Zhang @*/ 6491ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns, Vec **Y) 6492ff22ae23SHong Zhang { 6493ff22ae23SHong Zhang PetscErrorCode ierr; 6494ff22ae23SHong Zhang 6495ff22ae23SHong Zhang PetscFunctionBegin; 6496ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 6497ff22ae23SHong Zhang PetscValidPointer(ns,2); 6498ff22ae23SHong Zhang 6499ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 6500ff22ae23SHong Zhang else { 6501ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 6502ff22ae23SHong Zhang } 6503ff22ae23SHong Zhang PetscFunctionReturn(0); 6504ff22ae23SHong Zhang } 6505ff22ae23SHong Zhang 6506847ff0e1SMatthew G. Knepley #undef __FUNCT__ 6507847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor" 6508847ff0e1SMatthew G. Knepley /*@C 6509847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 6510847ff0e1SMatthew G. Knepley 6511847ff0e1SMatthew G. Knepley Collective on SNES 6512847ff0e1SMatthew G. Knepley 6513847ff0e1SMatthew G. Knepley Input Parameters: 6514847ff0e1SMatthew G. Knepley + ts - the TS context 6515847ff0e1SMatthew G. Knepley . t - current timestep 6516847ff0e1SMatthew G. Knepley . U - state vector 6517847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 6518847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 6519847ff0e1SMatthew G. Knepley - ctx - an optional user context 6520847ff0e1SMatthew G. Knepley 6521847ff0e1SMatthew G. Knepley Output Parameters: 6522847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 6523847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 6524847ff0e1SMatthew G. Knepley 6525847ff0e1SMatthew G. Knepley Level: intermediate 6526847ff0e1SMatthew G. Knepley 6527847ff0e1SMatthew G. Knepley Notes: 6528847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 6529847ff0e1SMatthew G. Knepley 6530847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 6531847ff0e1SMatthew G. Knepley 6532847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 6533847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 6534847ff0e1SMatthew G. Knepley 6535847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 6536847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 6537847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 6538847ff0e1SMatthew G. Knepley 6539847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse 6540847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 6541847ff0e1SMatthew G. Knepley @*/ 6542847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 6543847ff0e1SMatthew G. Knepley { 6544847ff0e1SMatthew G. Knepley SNES snes; 6545847ff0e1SMatthew G. Knepley MatFDColoring color; 6546847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 6547847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 6548847ff0e1SMatthew G. Knepley 6549847ff0e1SMatthew G. Knepley PetscFunctionBegin; 6550847ff0e1SMatthew G. Knepley ierr = PetscOptionsGetBool(((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 6551847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 6552847ff0e1SMatthew G. Knepley if (!color) { 6553847ff0e1SMatthew G. Knepley DM dm; 6554847ff0e1SMatthew G. Knepley ISColoring iscoloring; 6555847ff0e1SMatthew G. Knepley 6556847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 6557847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 6558847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 6559847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 6560847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 6561847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 6562847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 6563847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 6564847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 6565847ff0e1SMatthew G. Knepley } else { 6566847ff0e1SMatthew G. Knepley MatColoring mc; 6567847ff0e1SMatthew G. Knepley 6568847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 6569847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 6570847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 6571847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 6572847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 6573847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 6574847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 6575847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 6576847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 6577847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 6578847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 6579847ff0e1SMatthew G. Knepley } 6580847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 6581847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 6582847ff0e1SMatthew G. Knepley } 6583847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 6584847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 6585847ff0e1SMatthew G. Knepley if (J != B) { 6586847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 6587847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 6588847ff0e1SMatthew G. Knepley } 6589847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 6590847ff0e1SMatthew G. Knepley } 6591cb9d8021SPierre Barbier de Reuille 6592cb9d8021SPierre Barbier de Reuille #undef __FUNCT__ 6593cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSSetFunctionDomainError" 6594cb9d8021SPierre Barbier de Reuille /*@ 6595cb9d8021SPierre Barbier de Reuille TSSetFunctionDomainError - Set the function testing if the current state vector is valid 6596cb9d8021SPierre Barbier de Reuille 6597cb9d8021SPierre Barbier de Reuille Input Parameters: 6598cb9d8021SPierre Barbier de Reuille ts - the TS context 6599cb9d8021SPierre Barbier de Reuille func - function called within TSFunctionDomainError 6600cb9d8021SPierre Barbier de Reuille 6601cb9d8021SPierre Barbier de Reuille Level: intermediate 6602cb9d8021SPierre Barbier de Reuille 6603cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain 6604cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError() 6605cb9d8021SPierre Barbier de Reuille @*/ 6606cb9d8021SPierre Barbier de Reuille 6607d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 6608cb9d8021SPierre Barbier de Reuille { 6609cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 6610cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 6611cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 6612cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 6613cb9d8021SPierre Barbier de Reuille } 6614cb9d8021SPierre Barbier de Reuille 6615cb9d8021SPierre Barbier de Reuille #undef __FUNCT__ 6616cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSFunctionDomainError" 6617cb9d8021SPierre Barbier de Reuille /*@ 6618cb9d8021SPierre Barbier de Reuille TSFunctionDomainError - Check if the current state is valid 6619cb9d8021SPierre Barbier de Reuille 6620cb9d8021SPierre Barbier de Reuille Input Parameters: 6621cb9d8021SPierre Barbier de Reuille ts - the TS context 6622cb9d8021SPierre Barbier de Reuille stagetime - time of the simulation 6623d183316bSPierre Barbier de Reuille Y - state vector to check. 6624cb9d8021SPierre Barbier de Reuille 6625cb9d8021SPierre Barbier de Reuille Output Parameter: 6626cb9d8021SPierre Barbier de Reuille accept - Set to PETSC_FALSE if the current state vector is valid. 6627cb9d8021SPierre Barbier de Reuille 6628cb9d8021SPierre Barbier de Reuille Note: 6629cb9d8021SPierre Barbier de Reuille This function should be used to ensure the state is in a valid part of the space. 6630cb9d8021SPierre Barbier de Reuille For example, one can ensure here all values are positive. 6631cb9d8021SPierre Barbier de Reuille @*/ 6632d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 6633cb9d8021SPierre Barbier de Reuille { 6634cb9d8021SPierre Barbier de Reuille PetscErrorCode ierr; 6635cb9d8021SPierre Barbier de Reuille 6636cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 6637cb9d8021SPierre Barbier de Reuille 6638cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6639cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 6640cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 6641d183316bSPierre Barbier de Reuille PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 6642cb9d8021SPierre Barbier de Reuille } 6643cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 6644cb9d8021SPierre Barbier de Reuille } 6645*1ceb14c0SBarry Smith 6646*1ceb14c0SBarry Smith #undef __FUNCT__ 6647e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone" 664893b34091SDebojyoti Ghosh /*@C 6649e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 665093b34091SDebojyoti Ghosh 665193b34091SDebojyoti Ghosh Collective on MPI_Comm 665293b34091SDebojyoti Ghosh 665393b34091SDebojyoti Ghosh Input Parameter: 665493b34091SDebojyoti Ghosh . tsin - The input TS 665593b34091SDebojyoti Ghosh 665693b34091SDebojyoti Ghosh Output Parameter: 6657e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 665893b34091SDebojyoti Ghosh 66595eca1a21SEmil Constantinescu Notes: 66605eca1a21SEmil Constantinescu This function is used to create a clone of a TS object. It is used in ARKIMEX for initializing the slope for first stage explicit methods. It will likely be replaced in the future with a mechanism of switching methods on the fly. 66615eca1a21SEmil Constantinescu 6662baa10174SEmil Constantinescu When using TSDestroy() on a clone the user has to first reset the correct TS reference in the embedded SNES object: e.g.: by running SNES snes_dup=NULL; TSGetSNES(ts,&snes_dup); ierr = TSSetSNES(ts,snes_dup); 66635eca1a21SEmil Constantinescu 66645eca1a21SEmil Constantinescu Level: developer 666593b34091SDebojyoti Ghosh 6666e5168f73SEmil Constantinescu .keywords: TS, clone 6667e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 666893b34091SDebojyoti Ghosh @*/ 6669baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 667093b34091SDebojyoti Ghosh { 667193b34091SDebojyoti Ghosh TS t; 667293b34091SDebojyoti Ghosh PetscErrorCode ierr; 6673dc846ba4SSatish Balay SNES snes_start; 6674dc846ba4SSatish Balay DM dm; 6675dc846ba4SSatish Balay TSType type; 667693b34091SDebojyoti Ghosh 667793b34091SDebojyoti Ghosh PetscFunctionBegin; 667893b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 667993b34091SDebojyoti Ghosh *tsout = NULL; 668093b34091SDebojyoti Ghosh 66817a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 668293b34091SDebojyoti Ghosh 668393b34091SDebojyoti Ghosh /* General TS description */ 668493b34091SDebojyoti Ghosh t->numbermonitors = 0; 668593b34091SDebojyoti Ghosh t->setupcalled = 0; 668693b34091SDebojyoti Ghosh t->ksp_its = 0; 668793b34091SDebojyoti Ghosh t->snes_its = 0; 668893b34091SDebojyoti Ghosh t->nwork = 0; 668993b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 669093b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 669193b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 669293b34091SDebojyoti Ghosh 669334561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start); CHKERRQ(ierr); 669434561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start); CHKERRQ(ierr); 6695d15a3a53SEmil Constantinescu 669693b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm); CHKERRQ(ierr); 669793b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm); CHKERRQ(ierr); 669893b34091SDebojyoti Ghosh 669993b34091SDebojyoti Ghosh t->adapt=tsin->adapt; 670093b34091SDebojyoti Ghosh PetscObjectReference((PetscObject)t->adapt); 670193b34091SDebojyoti Ghosh 670293b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 670393b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 670493b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 670593b34091SDebojyoti Ghosh t->time_step_orig = tsin->time_step_orig; 670693b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 670793b34091SDebojyoti Ghosh t->steps = tsin->steps; 670893b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 670993b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 671093b34091SDebojyoti Ghosh t->atol = tsin->atol; 671193b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 671293b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 671393b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 671493b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 671593b34091SDebojyoti Ghosh 671693b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type); CHKERRQ(ierr); 671793b34091SDebojyoti Ghosh ierr = TSSetType(t,type); CHKERRQ(ierr); 671893b34091SDebojyoti Ghosh 671993b34091SDebojyoti Ghosh t->vec_sol = NULL; 672093b34091SDebojyoti Ghosh 672193b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 672293b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 672393b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 672493b34091SDebojyoti Ghosh 672593b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 672693b34091SDebojyoti Ghosh 672793b34091SDebojyoti Ghosh *tsout = t; 672893b34091SDebojyoti Ghosh PetscFunctionReturn(0); 672993b34091SDebojyoti Ghosh } 6730