163dd3a1aSKris Buschelman 2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h> /*I "petscts.h" I*/ 3496e6a7aSJed Brown #include <petscdmshell.h> 41e25c274SJed Brown #include <petscdmda.h> 52d5ee99bSBarry Smith #include <petscviewer.h> 62d5ee99bSBarry Smith #include <petscdraw.h> 7d763cef2SBarry Smith 8d5ba7fb7SMatthew Knepley /* Logging support */ 9d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 10166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 11d405a339SMatthew Knepley 1249354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1349354f04SShri Abhyankar 144a2ae208SSatish Balay #undef __FUNCT__ 15e55864a3SBarry Smith #define __FUNCT__ "TSSetTypeFromOptions_Private" 16bdad233fSMatthew Knepley /* 17bdad233fSMatthew Knepley TSSetTypeFromOptions - Sets the type of ts from user options. 18bdad233fSMatthew Knepley 19bdad233fSMatthew Knepley Collective on TS 20bdad233fSMatthew Knepley 21bdad233fSMatthew Knepley Input Parameter: 22bdad233fSMatthew Knepley . ts - The ts 23bdad233fSMatthew Knepley 24bdad233fSMatthew Knepley Level: intermediate 25bdad233fSMatthew Knepley 26bdad233fSMatthew Knepley .keywords: TS, set, options, database, type 27bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType() 28bdad233fSMatthew Knepley */ 298c34d3f5SBarry Smith static PetscErrorCode TSSetTypeFromOptions_Private(PetscOptions *PetscOptionsObject,TS ts) 30bdad233fSMatthew Knepley { 31ace3abfcSBarry Smith PetscBool opt; 322fc52814SBarry Smith const char *defaultType; 33bdad233fSMatthew Knepley char typeName[256]; 34dfbe8321SBarry Smith PetscErrorCode ierr; 35bdad233fSMatthew Knepley 36bdad233fSMatthew Knepley PetscFunctionBegin; 37*6991f827SBarry Smith ierr = PetscOptionsHead(PetscOptionsObject,"Setting TSType");CHKERRQ(ierr); 38bbd56ea5SKarl Rupp if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 39bbd56ea5SKarl Rupp else defaultType = TSEULER; 40bdad233fSMatthew Knepley 410f51fdf8SToby Isaac ierr = TSRegisterAll();CHKERRQ(ierr); 42a264d7a6SBarry Smith ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 43a7cc72afSBarry Smith if (opt) { 44bdad233fSMatthew Knepley ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 45bdad233fSMatthew Knepley } else { 46bdad233fSMatthew Knepley ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 47bdad233fSMatthew Knepley } 48bdad233fSMatthew Knepley PetscFunctionReturn(0); 49bdad233fSMatthew Knepley } 50bdad233fSMatthew Knepley 512d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx { 522d5ee99bSBarry Smith PetscViewer viewer; 534b363babSBarry Smith PetscDrawAxis axis; 542d5ee99bSBarry Smith Vec initialsolution; 552d5ee99bSBarry Smith PetscBool showinitial; 562d5ee99bSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 572d5ee99bSBarry Smith PetscBool showtimestepandtime; 582d5ee99bSBarry Smith int color; 592d5ee99bSBarry Smith }; 602d5ee99bSBarry Smith 61bdad233fSMatthew Knepley #undef __FUNCT__ 62bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions" 63bdad233fSMatthew Knepley /*@ 64bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 65bdad233fSMatthew Knepley 66bdad233fSMatthew Knepley Collective on TS 67bdad233fSMatthew Knepley 68bdad233fSMatthew Knepley Input Parameter: 69bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 70bdad233fSMatthew Knepley 71bdad233fSMatthew Knepley Options Database Keys: 724d91e141SJed Brown + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP 73d2daff3dSHong Zhang . -ts_checkpoint - checkpoint for adjoint sensitivity analysis 74bdad233fSMatthew Knepley . -ts_max_steps maxsteps - maximum number of time-steps to take 753bca7d26SBarry Smith . -ts_final_time time - maximum time to compute to 76bdad233fSMatthew Knepley . -ts_dt dt - initial time step 77a3cdaa26SBarry 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 78a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 79a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 80a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 81a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 82a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 83bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 84de06c3feSJed Brown . -ts_monitor_lg_timestep - Monitor timestep size graphically 85de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 86de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 87de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 88de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 89de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 90de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 912d5ee99bSBarry Smith . -ts_monitor_draw_solution_phase - Monitor solution graphically with phase diagram 92de06c3feSJed Brown . -ts_monitor_draw_error - Monitor error graphically 9391b97e58SBarry Smith . -ts_monitor_solution_binary <filename> - Save each solution to a binary file 9491b97e58SBarry Smith - -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 9591b97e58SBarry Smith 9691b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 97bdad233fSMatthew Knepley 98bdad233fSMatthew Knepley Level: beginner 99bdad233fSMatthew Knepley 100bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 101bdad233fSMatthew Knepley 102a313700dSBarry Smith .seealso: TSGetType() 103bdad233fSMatthew Knepley @*/ 1047087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 105bdad233fSMatthew Knepley { 106bc952696SBarry Smith PetscBool opt,flg,tflg; 107dfbe8321SBarry Smith PetscErrorCode ierr; 108649052a6SBarry Smith PetscViewer monviewer; 109eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 110089b2837SJed Brown SNES snes; 1111c3436cfSJed Brown TSAdapt adapt; 11231748224SBarry Smith PetscReal time_step; 11349354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 114d1212d36SBarry Smith char dir[16]; 115*6991f827SBarry Smith const char *defaultType; 116*6991f827SBarry Smith char typeName[256]; 117bdad233fSMatthew Knepley 118bdad233fSMatthew Knepley PetscFunctionBegin; 1190700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1203194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 121*6991f827SBarry Smith if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 122*6991f827SBarry Smith else defaultType = TSEULER; 123*6991f827SBarry Smith 124*6991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 125*6991f827SBarry Smith ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 126*6991f827SBarry Smith if (opt) { 127*6991f827SBarry Smith ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 128*6991f827SBarry Smith } else { 129*6991f827SBarry Smith ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 130*6991f827SBarry Smith } 131bdad233fSMatthew Knepley 132bdad233fSMatthew Knepley /* Handle generic TS options */ 133844594baSBarry Smith if (ts->trajectory) tflg = PETSC_TRUE; 134844594baSBarry Smith else tflg = PETSC_FALSE; 135bc952696SBarry Smith ierr = PetscOptionsBool("-ts_save_trajectories","Checkpoint for adjoint sensitivity analysis","TSSetSaveTrajectories",tflg,&tflg,NULL);CHKERRQ(ierr); 136bc952696SBarry Smith if (tflg) {ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);} 1370298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1380298fd71SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 1390298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 14031748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 14131748224SBarry Smith if (flg) { 14231748224SBarry Smith ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 14331748224SBarry Smith } 14449354f04SShri 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); 14549354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 1460298fd71SBarry 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); 1470298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 1480298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 1490298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 1500298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 151bdad233fSMatthew Knepley 15256f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 15356f85f32SBarry Smith { 15456f85f32SBarry Smith PetscBool set; 15556f85f32SBarry Smith flg = PETSC_FALSE; 15656f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 15756f85f32SBarry Smith if (set) { 15856f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 15956f85f32SBarry Smith } 16056f85f32SBarry Smith } 16156f85f32SBarry Smith #endif 16256f85f32SBarry Smith 163bdad233fSMatthew Knepley /* Monitor options */ 164a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 165eabae89aSBarry Smith if (flg) { 166ce94432eSBarry Smith ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr); 167649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 168bdad233fSMatthew Knepley } 1695180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1705180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1715180491cSLisandro Dalcin 1724f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 173a7cc72afSBarry Smith if (opt) { 1740b039ecaSBarry Smith TSMonitorLGCtx ctx; 1753923b477SBarry Smith PetscInt howoften = 1; 176a80ad3e0SBarry Smith 1770298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 178ce94432eSBarry Smith ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 1794f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 180b3603a34SBarry Smith } 1814f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 182b3603a34SBarry Smith if (opt) { 1830b039ecaSBarry Smith TSMonitorLGCtx ctx; 1843923b477SBarry Smith PetscInt howoften = 1; 185b3603a34SBarry Smith 1860298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 18722d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1884f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 189bdad233fSMatthew Knepley } 1904f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 191ef20d060SBarry Smith if (opt) { 1920b039ecaSBarry Smith TSMonitorLGCtx ctx; 1933923b477SBarry Smith PetscInt howoften = 1; 194ef20d060SBarry Smith 1950298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 19622d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1974f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 198ef20d060SBarry Smith } 199201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 200201da799SBarry Smith if (opt) { 201201da799SBarry Smith TSMonitorLGCtx ctx; 202201da799SBarry Smith PetscInt howoften = 1; 203201da799SBarry Smith 2040298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 20522d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 206201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 207201da799SBarry Smith } 208201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 209201da799SBarry Smith if (opt) { 210201da799SBarry Smith TSMonitorLGCtx ctx; 211201da799SBarry Smith PetscInt howoften = 1; 212201da799SBarry Smith 2130298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 21422d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 215201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 216201da799SBarry Smith } 2178189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 2188189c53fSBarry Smith if (opt) { 2198189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2208189c53fSBarry Smith PetscInt howoften = 1; 2218189c53fSBarry Smith 2220298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 22322d28d08SBarry Smith ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 2248189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 2258189c53fSBarry Smith } 226ef20d060SBarry Smith opt = PETSC_FALSE; 2270dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 228a7cc72afSBarry Smith if (opt) { 22983a4ac43SBarry Smith TSMonitorDrawCtx ctx; 23083a4ac43SBarry Smith PetscInt howoften = 1; 231a80ad3e0SBarry Smith 2320298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 233ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 23483a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 235bdad233fSMatthew Knepley } 236fb1732b5SBarry Smith opt = PETSC_FALSE; 2372d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 2382d5ee99bSBarry Smith if (opt) { 2392d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2402d5ee99bSBarry Smith PetscReal bounds[4]; 2412d5ee99bSBarry Smith PetscInt n = 4; 2422d5ee99bSBarry Smith PetscDraw draw; 2432d5ee99bSBarry Smith 2442d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 2452d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2462d5ee99bSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr); 2472d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 2482d5ee99bSBarry Smith ierr = PetscDrawClear(draw);CHKERRQ(ierr); 2494b363babSBarry Smith ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr); 2504b363babSBarry Smith ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 251f54adfc0SBarry Smith ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 2524b363babSBarry Smith ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr); 25307995780SPeter Brune /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */ 2542d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2552d5ee99bSBarry Smith } 2562d5ee99bSBarry Smith opt = PETSC_FALSE; 2570dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 2583a471f94SBarry Smith if (opt) { 25983a4ac43SBarry Smith TSMonitorDrawCtx ctx; 26083a4ac43SBarry Smith PetscInt howoften = 1; 2613a471f94SBarry Smith 2620298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 263ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 26483a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2653a471f94SBarry Smith } 2663a471f94SBarry Smith opt = PETSC_FALSE; 26791b97e58SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 268fb1732b5SBarry Smith if (flg) { 269fb1732b5SBarry Smith PetscViewer ctx; 270fb1732b5SBarry Smith if (monfilename[0]) { 271ce94432eSBarry Smith ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 272c2fbc07fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 273fb1732b5SBarry Smith } else { 274ce94432eSBarry Smith ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts)); 2750298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr); 276fb1732b5SBarry Smith } 277fb1732b5SBarry Smith } 278ed81e22dSJed Brown opt = PETSC_FALSE; 27991b97e58SBarry 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); 280ed81e22dSJed Brown if (flg) { 281ed81e22dSJed Brown const char *ptr,*ptr2; 282ed81e22dSJed Brown char *filetemplate; 283ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 284ed81e22dSJed Brown /* Do some cursory validation of the input. */ 285ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 286ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 287ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 288ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 289ce94432eSBarry 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"); 290ed81e22dSJed Brown if (ptr2) break; 291ed81e22dSJed Brown } 292ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 293ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 294ed81e22dSJed Brown } 295bdad233fSMatthew Knepley 296d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 297d1212d36SBarry Smith if (flg) { 298d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 299d1212d36SBarry Smith int ray = 0; 300d1212d36SBarry Smith DMDADirection ddir; 301d1212d36SBarry Smith DM da; 302d1212d36SBarry Smith PetscMPIInt rank; 303d1212d36SBarry Smith 304ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 305d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 306d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 307ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 308d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 309d1212d36SBarry Smith 310d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 311b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 312d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 313d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 314ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 315d1212d36SBarry Smith if (!rank) { 316d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 317d1212d36SBarry Smith } 31851b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 319d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 320d1212d36SBarry Smith } 32151b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 32251b4a12fSMatthew G. Knepley if (flg) { 32351b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 32451b4a12fSMatthew G. Knepley int ray = 0; 32551b4a12fSMatthew G. Knepley DMDADirection ddir; 32651b4a12fSMatthew G. Knepley DM da; 32751b4a12fSMatthew G. Knepley PetscInt howoften = 1; 328d1212d36SBarry Smith 32951b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 33051b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 33151b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 33251b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 33351b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 33451b4a12fSMatthew G. Knepley 33551b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 336b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 33751b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 33851b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 33951b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 34051b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 34151b4a12fSMatthew G. Knepley } 342a7a1495cSBarry Smith 3437781c08eSBarry Smith /* 3447781c08eSBarry Smith This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui 3457781c08eSBarry Smith will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin() 3467781c08eSBarry Smith */ 347552698daSJed Brown ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr); 348e55864a3SBarry Smith ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr); 349d763cef2SBarry Smith 350*6991f827SBarry Smith /* Handle specific TS options */ 351*6991f827SBarry Smith if (ts->ops->setfromoptions) { 352*6991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 353*6991f827SBarry Smith } 354*6991f827SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 355*6991f827SBarry Smith 356*6991f827SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 357*6991f827SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 358d763cef2SBarry Smith 359bc952696SBarry Smith if (ts->trajectory) { 360bc952696SBarry Smith ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr); 361bc952696SBarry Smith } 362bc952696SBarry Smith 363*6991f827SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 364*6991f827SBarry Smith if (snes) { 365*6991f827SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 366*6991f827SBarry Smith ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr); 367d763cef2SBarry Smith } 368d763cef2SBarry Smith PetscFunctionReturn(0); 369d763cef2SBarry Smith } 370d763cef2SBarry Smith 371d763cef2SBarry Smith #undef __FUNCT__ 372bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory" 373d2daff3dSHong Zhang /*@ 374bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 375d2daff3dSHong Zhang 376d2daff3dSHong Zhang Collective on TS 377d2daff3dSHong Zhang 378d2daff3dSHong Zhang Input Parameters: 379bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 380bc952696SBarry Smith 381d2daff3dSHong Zhang 382d2daff3dSHong Zhang Level: intermediate 383d2daff3dSHong Zhang 384bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve() 385d2daff3dSHong Zhang 386bc952696SBarry Smith .keywords: TS, set, checkpoint, 387d2daff3dSHong Zhang @*/ 388bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 389d2daff3dSHong Zhang { 390bc952696SBarry Smith PetscErrorCode ierr; 391bc952696SBarry Smith 392d2daff3dSHong Zhang PetscFunctionBegin; 393d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 394bc952696SBarry Smith if (!ts->trajectory) { 395bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 396bc952696SBarry Smith /* should it set a default trajectory? */ 397bc952696SBarry Smith } 398d2daff3dSHong Zhang PetscFunctionReturn(0); 399d2daff3dSHong Zhang } 400d2daff3dSHong Zhang 401a7a1495cSBarry Smith #undef __FUNCT__ 402a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian" 403a7a1495cSBarry Smith /*@ 404a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 405a7a1495cSBarry Smith set with TSSetRHSJacobian(). 406a7a1495cSBarry Smith 407a7a1495cSBarry Smith Collective on TS and Vec 408a7a1495cSBarry Smith 409a7a1495cSBarry Smith Input Parameters: 410316643e7SJed Brown + ts - the TS context 411a7a1495cSBarry Smith . t - current timestep 4120910c330SBarry Smith - U - input vector 413a7a1495cSBarry Smith 414a7a1495cSBarry Smith Output Parameters: 415a7a1495cSBarry Smith + A - Jacobian matrix 416a7a1495cSBarry Smith . B - optional preconditioning matrix 417a7a1495cSBarry Smith - flag - flag indicating matrix structure 418a7a1495cSBarry Smith 419a7a1495cSBarry Smith Notes: 420a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 421a7a1495cSBarry Smith is used internally within the nonlinear solvers. 422a7a1495cSBarry Smith 42394b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 424a7a1495cSBarry Smith flag parameter. 425a7a1495cSBarry Smith 426a7a1495cSBarry Smith Level: developer 427a7a1495cSBarry Smith 428a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 429a7a1495cSBarry Smith 43094b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 431a7a1495cSBarry Smith @*/ 432d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 433a7a1495cSBarry Smith { 434dfbe8321SBarry Smith PetscErrorCode ierr; 435270bf2e7SJed Brown PetscObjectState Ustate; 43624989b8cSPeter Brune DM dm; 437942e3340SBarry Smith DMTS tsdm; 43824989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 43924989b8cSPeter Brune void *ctx; 44024989b8cSPeter Brune TSIJacobian ijacobianfunc; 441a7a1495cSBarry Smith 442a7a1495cSBarry Smith PetscFunctionBegin; 4430700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4440910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 4450910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 44624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 447942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 44824989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 4490298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 45059e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 4510910c330SBarry Smith if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) { 4520e4ef248SJed Brown PetscFunctionReturn(0); 453f8ede8e7SJed Brown } 454d90be118SSean Farley 455ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 456d90be118SSean Farley 457e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 45894ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 45994ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 46094ab13aaSBarry Smith if (A != B) { 46194ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 46294ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 463e1244c69SJed Brown } 464e1244c69SJed Brown ts->rhsjacobian.shift = 0; 465e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 466e1244c69SJed Brown } 467e1244c69SJed Brown 46824989b8cSPeter Brune if (rhsjacobianfunc) { 46994ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 470a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 471d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 472a7a1495cSBarry Smith PetscStackPop; 47394ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 474a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 47594ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,4); 47694ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,5); 477ef66eb69SBarry Smith } else { 47894ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 47994ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 480ef66eb69SBarry Smith } 4810e4ef248SJed Brown ts->rhsjacobian.time = t; 4820910c330SBarry Smith ts->rhsjacobian.X = U; 48359e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 484a7a1495cSBarry Smith PetscFunctionReturn(0); 485a7a1495cSBarry Smith } 486a7a1495cSBarry Smith 4874a2ae208SSatish Balay #undef __FUNCT__ 4884a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 489316643e7SJed Brown /*@ 490d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 491d763cef2SBarry Smith 492316643e7SJed Brown Collective on TS and Vec 493316643e7SJed Brown 494316643e7SJed Brown Input Parameters: 495316643e7SJed Brown + ts - the TS context 496316643e7SJed Brown . t - current time 4970910c330SBarry Smith - U - state vector 498316643e7SJed Brown 499316643e7SJed Brown Output Parameter: 500316643e7SJed Brown . y - right hand side 501316643e7SJed Brown 502316643e7SJed Brown Note: 503316643e7SJed Brown Most users should not need to explicitly call this routine, as it 504316643e7SJed Brown is used internally within the nonlinear solvers. 505316643e7SJed Brown 506316643e7SJed Brown Level: developer 507316643e7SJed Brown 508316643e7SJed Brown .keywords: TS, compute 509316643e7SJed Brown 510316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 511316643e7SJed Brown @*/ 5120910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 513d763cef2SBarry Smith { 514dfbe8321SBarry Smith PetscErrorCode ierr; 51524989b8cSPeter Brune TSRHSFunction rhsfunction; 51624989b8cSPeter Brune TSIFunction ifunction; 51724989b8cSPeter Brune void *ctx; 51824989b8cSPeter Brune DM dm; 51924989b8cSPeter Brune 520d763cef2SBarry Smith PetscFunctionBegin; 5210700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5220910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5230700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 52424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 52524989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 5260298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 527d763cef2SBarry Smith 528ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 529d763cef2SBarry Smith 5300910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 53124989b8cSPeter Brune if (rhsfunction) { 532d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 5330910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 534d763cef2SBarry Smith PetscStackPop; 535214bc6a2SJed Brown } else { 536214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 537b2cd27e8SJed Brown } 53844a41b28SSean Farley 5390910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 540d763cef2SBarry Smith PetscFunctionReturn(0); 541d763cef2SBarry Smith } 542d763cef2SBarry Smith 5434a2ae208SSatish Balay #undef __FUNCT__ 544ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 545ef20d060SBarry Smith /*@ 546ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 547ef20d060SBarry Smith 548ef20d060SBarry Smith Collective on TS and Vec 549ef20d060SBarry Smith 550ef20d060SBarry Smith Input Parameters: 551ef20d060SBarry Smith + ts - the TS context 552ef20d060SBarry Smith - t - current time 553ef20d060SBarry Smith 554ef20d060SBarry Smith Output Parameter: 5550910c330SBarry Smith . U - the solution 556ef20d060SBarry Smith 557ef20d060SBarry Smith Note: 558ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 559ef20d060SBarry Smith is used internally within the nonlinear solvers. 560ef20d060SBarry Smith 561ef20d060SBarry Smith Level: developer 562ef20d060SBarry Smith 563ef20d060SBarry Smith .keywords: TS, compute 564ef20d060SBarry Smith 565abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 566ef20d060SBarry Smith @*/ 5670910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 568ef20d060SBarry Smith { 569ef20d060SBarry Smith PetscErrorCode ierr; 570ef20d060SBarry Smith TSSolutionFunction solutionfunction; 571ef20d060SBarry Smith void *ctx; 572ef20d060SBarry Smith DM dm; 573ef20d060SBarry Smith 574ef20d060SBarry Smith PetscFunctionBegin; 575ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5760910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 577ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 578ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 579ef20d060SBarry Smith 580ef20d060SBarry Smith if (solutionfunction) { 5819b7cd975SBarry Smith PetscStackPush("TS user solution function"); 5820910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 583ef20d060SBarry Smith PetscStackPop; 584ef20d060SBarry Smith } 585ef20d060SBarry Smith PetscFunctionReturn(0); 586ef20d060SBarry Smith } 5879b7cd975SBarry Smith #undef __FUNCT__ 5889b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction" 5899b7cd975SBarry Smith /*@ 5909b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 5919b7cd975SBarry Smith 5929b7cd975SBarry Smith Collective on TS and Vec 5939b7cd975SBarry Smith 5949b7cd975SBarry Smith Input Parameters: 5959b7cd975SBarry Smith + ts - the TS context 5969b7cd975SBarry Smith - t - current time 5979b7cd975SBarry Smith 5989b7cd975SBarry Smith Output Parameter: 5999b7cd975SBarry Smith . U - the function value 6009b7cd975SBarry Smith 6019b7cd975SBarry Smith Note: 6029b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 6039b7cd975SBarry Smith is used internally within the nonlinear solvers. 6049b7cd975SBarry Smith 6059b7cd975SBarry Smith Level: developer 6069b7cd975SBarry Smith 6079b7cd975SBarry Smith .keywords: TS, compute 6089b7cd975SBarry Smith 6099b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 6109b7cd975SBarry Smith @*/ 6119b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 6129b7cd975SBarry Smith { 6139b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 6149b7cd975SBarry Smith void *ctx; 6159b7cd975SBarry Smith DM dm; 6169b7cd975SBarry Smith 6179b7cd975SBarry Smith PetscFunctionBegin; 6189b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6199b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6209b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 6219b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 6229b7cd975SBarry Smith 6239b7cd975SBarry Smith if (forcing) { 6249b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 6259b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 6269b7cd975SBarry Smith PetscStackPop; 6279b7cd975SBarry Smith } 6289b7cd975SBarry Smith PetscFunctionReturn(0); 6299b7cd975SBarry Smith } 630ef20d060SBarry Smith 631ef20d060SBarry Smith #undef __FUNCT__ 632214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 633214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 634214bc6a2SJed Brown { 6352dd45cf8SJed Brown Vec F; 636214bc6a2SJed Brown PetscErrorCode ierr; 637214bc6a2SJed Brown 638214bc6a2SJed Brown PetscFunctionBegin; 6390298fd71SBarry Smith *Frhs = NULL; 6400298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 641214bc6a2SJed Brown if (!ts->Frhs) { 6422dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 643214bc6a2SJed Brown } 644214bc6a2SJed Brown *Frhs = ts->Frhs; 645214bc6a2SJed Brown PetscFunctionReturn(0); 646214bc6a2SJed Brown } 647214bc6a2SJed Brown 648214bc6a2SJed Brown #undef __FUNCT__ 649214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 650214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 651214bc6a2SJed Brown { 652214bc6a2SJed Brown Mat A,B; 6532dd45cf8SJed Brown PetscErrorCode ierr; 654214bc6a2SJed Brown 655214bc6a2SJed Brown PetscFunctionBegin; 656c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 657c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 6580298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 659214bc6a2SJed Brown if (Arhs) { 660214bc6a2SJed Brown if (!ts->Arhs) { 661214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 662214bc6a2SJed Brown } 663214bc6a2SJed Brown *Arhs = ts->Arhs; 664214bc6a2SJed Brown } 665214bc6a2SJed Brown if (Brhs) { 666214bc6a2SJed Brown if (!ts->Brhs) { 667bdb70873SJed Brown if (A != B) { 668214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 669bdb70873SJed Brown } else { 670bdb70873SJed Brown ts->Brhs = ts->Arhs; 671bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 672bdb70873SJed Brown } 673214bc6a2SJed Brown } 674214bc6a2SJed Brown *Brhs = ts->Brhs; 675214bc6a2SJed Brown } 676214bc6a2SJed Brown PetscFunctionReturn(0); 677214bc6a2SJed Brown } 678214bc6a2SJed Brown 679214bc6a2SJed Brown #undef __FUNCT__ 680316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 681316643e7SJed Brown /*@ 6820910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 683316643e7SJed Brown 684316643e7SJed Brown Collective on TS and Vec 685316643e7SJed Brown 686316643e7SJed Brown Input Parameters: 687316643e7SJed Brown + ts - the TS context 688316643e7SJed Brown . t - current time 6890910c330SBarry Smith . U - state vector 6900910c330SBarry Smith . Udot - time derivative of state vector 691214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 692316643e7SJed Brown 693316643e7SJed Brown Output Parameter: 694316643e7SJed Brown . Y - right hand side 695316643e7SJed Brown 696316643e7SJed Brown Note: 697316643e7SJed Brown Most users should not need to explicitly call this routine, as it 698316643e7SJed Brown is used internally within the nonlinear solvers. 699316643e7SJed Brown 700316643e7SJed Brown If the user did did not write their equations in implicit form, this 701316643e7SJed Brown function recasts them in implicit form. 702316643e7SJed Brown 703316643e7SJed Brown Level: developer 704316643e7SJed Brown 705316643e7SJed Brown .keywords: TS, compute 706316643e7SJed Brown 707316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 708316643e7SJed Brown @*/ 7090910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 710316643e7SJed Brown { 711316643e7SJed Brown PetscErrorCode ierr; 71224989b8cSPeter Brune TSIFunction ifunction; 71324989b8cSPeter Brune TSRHSFunction rhsfunction; 71424989b8cSPeter Brune void *ctx; 71524989b8cSPeter Brune DM dm; 716316643e7SJed Brown 717316643e7SJed Brown PetscFunctionBegin; 7180700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7190910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7200910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 7210700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 722316643e7SJed Brown 72324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 72424989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 7250298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 72624989b8cSPeter Brune 727ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 728d90be118SSean Farley 7290910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 73024989b8cSPeter Brune if (ifunction) { 731316643e7SJed Brown PetscStackPush("TS user implicit function"); 7320910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 733316643e7SJed Brown PetscStackPop; 734214bc6a2SJed Brown } 735214bc6a2SJed Brown if (imex) { 73624989b8cSPeter Brune if (!ifunction) { 7370910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 7382dd45cf8SJed Brown } 73924989b8cSPeter Brune } else if (rhsfunction) { 74024989b8cSPeter Brune if (ifunction) { 741214bc6a2SJed Brown Vec Frhs; 742214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 7430910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 744214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 7452dd45cf8SJed Brown } else { 7460910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 7470910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 748316643e7SJed Brown } 7494a6899ffSJed Brown } 7500910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 751316643e7SJed Brown PetscFunctionReturn(0); 752316643e7SJed Brown } 753316643e7SJed Brown 754316643e7SJed Brown #undef __FUNCT__ 755316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 756316643e7SJed Brown /*@ 757316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 758316643e7SJed Brown 759316643e7SJed Brown Collective on TS and Vec 760316643e7SJed Brown 761316643e7SJed Brown Input 762316643e7SJed Brown Input Parameters: 763316643e7SJed Brown + ts - the TS context 764316643e7SJed Brown . t - current timestep 7650910c330SBarry Smith . U - state vector 7660910c330SBarry Smith . Udot - time derivative of state vector 767214bc6a2SJed Brown . shift - shift to apply, see note below 768214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 769316643e7SJed Brown 770316643e7SJed Brown Output Parameters: 771316643e7SJed Brown + A - Jacobian matrix 772316643e7SJed Brown . B - optional preconditioning matrix 773316643e7SJed Brown - flag - flag indicating matrix structure 774316643e7SJed Brown 775316643e7SJed Brown Notes: 7760910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 777316643e7SJed Brown 7780910c330SBarry Smith dF/dU + shift*dF/dUdot 779316643e7SJed Brown 780316643e7SJed Brown Most users should not need to explicitly call this routine, as it 781316643e7SJed Brown is used internally within the nonlinear solvers. 782316643e7SJed Brown 783316643e7SJed Brown Level: developer 784316643e7SJed Brown 785316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 786316643e7SJed Brown 787316643e7SJed Brown .seealso: TSSetIJacobian() 78863495f91SJed Brown @*/ 789d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 790316643e7SJed Brown { 791316643e7SJed Brown PetscErrorCode ierr; 79224989b8cSPeter Brune TSIJacobian ijacobian; 79324989b8cSPeter Brune TSRHSJacobian rhsjacobian; 79424989b8cSPeter Brune DM dm; 79524989b8cSPeter Brune void *ctx; 796316643e7SJed Brown 797316643e7SJed Brown PetscFunctionBegin; 7980700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7990910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8000910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 801316643e7SJed Brown PetscValidPointer(A,6); 80294ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 803316643e7SJed Brown PetscValidPointer(B,7); 80494ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 80524989b8cSPeter Brune 80624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 80724989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 8080298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 80924989b8cSPeter Brune 810ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 811316643e7SJed Brown 81294ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 81324989b8cSPeter Brune if (ijacobian) { 814316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 815d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 816316643e7SJed Brown PetscStackPop; 817214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 81894ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,4); 81994ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,5); 8204a6899ffSJed Brown } 821214bc6a2SJed Brown if (imex) { 822b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 82394ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 82494ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 82594ab13aaSBarry Smith if (A != B) { 82694ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 82794ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 828214bc6a2SJed Brown } 829214bc6a2SJed Brown } 830214bc6a2SJed Brown } else { 831e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 832e1244c69SJed Brown if (rhsjacobian) { 833bd373395SBarry Smith if (ijacobian) { 834e1244c69SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 835bd373395SBarry Smith } else { 836bd373395SBarry Smith ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr); 837e1244c69SJed Brown } 838d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 839e1244c69SJed Brown } 84094ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 841e1244c69SJed Brown ts->rhsjacobian.scale = -1; 842e1244c69SJed Brown ts->rhsjacobian.shift = shift; 84394ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 84494ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 84594ab13aaSBarry Smith if (A != B) { 84694ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 84794ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 848316643e7SJed Brown } 849e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 850e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 851e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 85294ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 85394ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 85494ab13aaSBarry Smith if (A != B) { 85594ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 85694ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 857e1244c69SJed Brown } 858e1244c69SJed Brown } 85994ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 86094ab13aaSBarry Smith if (A != B) { 86194ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 862214bc6a2SJed Brown } 863316643e7SJed Brown } 864316643e7SJed Brown } 86594ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 866316643e7SJed Brown PetscFunctionReturn(0); 867316643e7SJed Brown } 868316643e7SJed Brown 869316643e7SJed Brown #undef __FUNCT__ 8704a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 871d763cef2SBarry Smith /*@C 872d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 873b5abc632SBarry Smith where U_t = G(t,u). 874d763cef2SBarry Smith 8753f9fe445SBarry Smith Logically Collective on TS 876d763cef2SBarry Smith 877d763cef2SBarry Smith Input Parameters: 878d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 8790298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 880d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 881d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 8820298fd71SBarry Smith function evaluation routine (may be NULL) 883d763cef2SBarry Smith 884d763cef2SBarry Smith Calling sequence of func: 88587828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 886d763cef2SBarry Smith 887d763cef2SBarry Smith + t - current timestep 888d763cef2SBarry Smith . u - input vector 889d763cef2SBarry Smith . F - function vector 890d763cef2SBarry Smith - ctx - [optional] user-defined function context 891d763cef2SBarry Smith 892d763cef2SBarry Smith Level: beginner 893d763cef2SBarry Smith 8942bbac0d3SBarry Smith Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 8952bbac0d3SBarry Smith 896d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 897d763cef2SBarry Smith 898ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 899d763cef2SBarry Smith @*/ 900089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 901d763cef2SBarry Smith { 902089b2837SJed Brown PetscErrorCode ierr; 903089b2837SJed Brown SNES snes; 9040298fd71SBarry Smith Vec ralloc = NULL; 90524989b8cSPeter Brune DM dm; 906d763cef2SBarry Smith 907089b2837SJed Brown PetscFunctionBegin; 9080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 909ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 91024989b8cSPeter Brune 91124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 91224989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 913089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 914e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 915e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 916e856ceecSJed Brown r = ralloc; 917e856ceecSJed Brown } 918089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 919e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 920d763cef2SBarry Smith PetscFunctionReturn(0); 921d763cef2SBarry Smith } 922d763cef2SBarry Smith 9234a2ae208SSatish Balay #undef __FUNCT__ 924ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 925ef20d060SBarry Smith /*@C 926abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 927ef20d060SBarry Smith 928ef20d060SBarry Smith Logically Collective on TS 929ef20d060SBarry Smith 930ef20d060SBarry Smith Input Parameters: 931ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 932ef20d060SBarry Smith . f - routine for evaluating the solution 933ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 9340298fd71SBarry Smith function evaluation routine (may be NULL) 935ef20d060SBarry Smith 936ef20d060SBarry Smith Calling sequence of func: 937ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 938ef20d060SBarry Smith 939ef20d060SBarry Smith + t - current timestep 940ef20d060SBarry Smith . u - output vector 941ef20d060SBarry Smith - ctx - [optional] user-defined function context 942ef20d060SBarry Smith 943abd5a294SJed Brown Notes: 944abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 945abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 946abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 947abd5a294SJed Brown 9484f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 949abd5a294SJed Brown 950ef20d060SBarry Smith Level: beginner 951ef20d060SBarry Smith 952ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 953ef20d060SBarry Smith 9549b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 955ef20d060SBarry Smith @*/ 956ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 957ef20d060SBarry Smith { 958ef20d060SBarry Smith PetscErrorCode ierr; 959ef20d060SBarry Smith DM dm; 960ef20d060SBarry Smith 961ef20d060SBarry Smith PetscFunctionBegin; 962ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 963ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 964ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 965ef20d060SBarry Smith PetscFunctionReturn(0); 966ef20d060SBarry Smith } 967ef20d060SBarry Smith 968ef20d060SBarry Smith #undef __FUNCT__ 9699b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction" 9709b7cd975SBarry Smith /*@C 9719b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 9729b7cd975SBarry Smith 9739b7cd975SBarry Smith Logically Collective on TS 9749b7cd975SBarry Smith 9759b7cd975SBarry Smith Input Parameters: 9769b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 9779b7cd975SBarry Smith . f - routine for evaluating the forcing function 9789b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 9790298fd71SBarry Smith function evaluation routine (may be NULL) 9809b7cd975SBarry Smith 9819b7cd975SBarry Smith Calling sequence of func: 9829b7cd975SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 9839b7cd975SBarry Smith 9849b7cd975SBarry Smith + t - current timestep 9859b7cd975SBarry Smith . u - output vector 9869b7cd975SBarry Smith - ctx - [optional] user-defined function context 9879b7cd975SBarry Smith 9889b7cd975SBarry Smith Notes: 9899b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 9909b7cd975SBarry Smith create closed-form solutions with a non-physical forcing term. 9919b7cd975SBarry Smith 9929b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 9939b7cd975SBarry Smith 9949b7cd975SBarry Smith Level: beginner 9959b7cd975SBarry Smith 9969b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 9979b7cd975SBarry Smith 9989b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 9999b7cd975SBarry Smith @*/ 10009b7cd975SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 10019b7cd975SBarry Smith { 10029b7cd975SBarry Smith PetscErrorCode ierr; 10039b7cd975SBarry Smith DM dm; 10049b7cd975SBarry Smith 10059b7cd975SBarry Smith PetscFunctionBegin; 10069b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10079b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 10089b7cd975SBarry Smith ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr); 10099b7cd975SBarry Smith PetscFunctionReturn(0); 10109b7cd975SBarry Smith } 10119b7cd975SBarry Smith 10129b7cd975SBarry Smith #undef __FUNCT__ 10134a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 1014d763cef2SBarry Smith /*@C 1015f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1016b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1017d763cef2SBarry Smith 10183f9fe445SBarry Smith Logically Collective on TS 1019d763cef2SBarry Smith 1020d763cef2SBarry Smith Input Parameters: 1021d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1022e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1023e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1024d763cef2SBarry Smith . f - the Jacobian evaluation routine 1025d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 10260298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1027d763cef2SBarry Smith 1028f7ab8db6SBarry Smith Calling sequence of f: 1029ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1030d763cef2SBarry Smith 1031d763cef2SBarry Smith + t - current timestep 1032d763cef2SBarry Smith . u - input vector 1033e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1034e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1035d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1036d763cef2SBarry Smith 1037d763cef2SBarry Smith 1038d763cef2SBarry Smith Level: beginner 1039d763cef2SBarry Smith 1040d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1041d763cef2SBarry Smith 1042ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1043d763cef2SBarry Smith 1044d763cef2SBarry Smith @*/ 1045e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1046d763cef2SBarry Smith { 1047277b19d0SLisandro Dalcin PetscErrorCode ierr; 1048089b2837SJed Brown SNES snes; 104924989b8cSPeter Brune DM dm; 105024989b8cSPeter Brune TSIJacobian ijacobian; 1051277b19d0SLisandro Dalcin 1052d763cef2SBarry Smith PetscFunctionBegin; 10530700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1054e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1055e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1056e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1057e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1058d763cef2SBarry Smith 105924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 106024989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1061e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1062e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1063e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1064e1244c69SJed Brown } 10650298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1066089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 10675f659677SPeter Brune if (!ijacobian) { 1068e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 10690e4ef248SJed Brown } 1070e5d3d808SBarry Smith if (Amat) { 1071e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 10720e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1073bbd56ea5SKarl Rupp 1074e5d3d808SBarry Smith ts->Arhs = Amat; 10750e4ef248SJed Brown } 1076e5d3d808SBarry Smith if (Pmat) { 1077e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 10780e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1079bbd56ea5SKarl Rupp 1080e5d3d808SBarry Smith ts->Brhs = Pmat; 10810e4ef248SJed Brown } 1082d763cef2SBarry Smith PetscFunctionReturn(0); 1083d763cef2SBarry Smith } 1084d763cef2SBarry Smith 1085316643e7SJed Brown 1086316643e7SJed Brown #undef __FUNCT__ 1087316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 1088316643e7SJed Brown /*@C 1089b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1090316643e7SJed Brown 10913f9fe445SBarry Smith Logically Collective on TS 1092316643e7SJed Brown 1093316643e7SJed Brown Input Parameters: 1094316643e7SJed Brown + ts - the TS context obtained from TSCreate() 10950298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1096316643e7SJed Brown . f - the function evaluation routine 10970298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1098316643e7SJed Brown 1099316643e7SJed Brown Calling sequence of f: 1100316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1101316643e7SJed Brown 1102316643e7SJed Brown + t - time at step/stage being solved 1103316643e7SJed Brown . u - state vector 1104316643e7SJed Brown . u_t - time derivative of state vector 1105316643e7SJed Brown . F - function vector 1106316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1107316643e7SJed Brown 1108316643e7SJed Brown Important: 11092bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1110316643e7SJed Brown 1111316643e7SJed Brown Level: beginner 1112316643e7SJed Brown 1113316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1114316643e7SJed Brown 1115d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1116316643e7SJed Brown @*/ 1117089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 1118316643e7SJed Brown { 1119089b2837SJed Brown PetscErrorCode ierr; 1120089b2837SJed Brown SNES snes; 11210298fd71SBarry Smith Vec resalloc = NULL; 112224989b8cSPeter Brune DM dm; 1123316643e7SJed Brown 1124316643e7SJed Brown PetscFunctionBegin; 11250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1126ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 112724989b8cSPeter Brune 112824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 112924989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 113024989b8cSPeter Brune 1131089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1132e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 1133e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 1134e856ceecSJed Brown res = resalloc; 1135e856ceecSJed Brown } 1136089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 1137e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 1138089b2837SJed Brown PetscFunctionReturn(0); 1139089b2837SJed Brown } 1140089b2837SJed Brown 1141089b2837SJed Brown #undef __FUNCT__ 1142089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 1143089b2837SJed Brown /*@C 1144089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1145089b2837SJed Brown 1146089b2837SJed Brown Not Collective 1147089b2837SJed Brown 1148089b2837SJed Brown Input Parameter: 1149089b2837SJed Brown . ts - the TS context 1150089b2837SJed Brown 1151089b2837SJed Brown Output Parameter: 11520298fd71SBarry Smith + r - vector to hold residual (or NULL) 11530298fd71SBarry Smith . func - the function to compute residual (or NULL) 11540298fd71SBarry Smith - ctx - the function context (or NULL) 1155089b2837SJed Brown 1156089b2837SJed Brown Level: advanced 1157089b2837SJed Brown 1158089b2837SJed Brown .keywords: TS, nonlinear, get, function 1159089b2837SJed Brown 1160089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1161089b2837SJed Brown @*/ 1162089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1163089b2837SJed Brown { 1164089b2837SJed Brown PetscErrorCode ierr; 1165089b2837SJed Brown SNES snes; 116624989b8cSPeter Brune DM dm; 1167089b2837SJed Brown 1168089b2837SJed Brown PetscFunctionBegin; 1169089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1170089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11710298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 117224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 117324989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1174089b2837SJed Brown PetscFunctionReturn(0); 1175089b2837SJed Brown } 1176089b2837SJed Brown 1177089b2837SJed Brown #undef __FUNCT__ 1178089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 1179089b2837SJed Brown /*@C 1180089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1181089b2837SJed Brown 1182089b2837SJed Brown Not Collective 1183089b2837SJed Brown 1184089b2837SJed Brown Input Parameter: 1185089b2837SJed Brown . ts - the TS context 1186089b2837SJed Brown 1187089b2837SJed Brown Output Parameter: 11880298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 11890298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 11900298fd71SBarry Smith - ctx - the function context (or NULL) 1191089b2837SJed Brown 1192089b2837SJed Brown Level: advanced 1193089b2837SJed Brown 1194089b2837SJed Brown .keywords: TS, nonlinear, get, function 1195089b2837SJed Brown 11962bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1197089b2837SJed Brown @*/ 1198089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1199089b2837SJed Brown { 1200089b2837SJed Brown PetscErrorCode ierr; 1201089b2837SJed Brown SNES snes; 120224989b8cSPeter Brune DM dm; 1203089b2837SJed Brown 1204089b2837SJed Brown PetscFunctionBegin; 1205089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1206089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12070298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 120824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 120924989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1210316643e7SJed Brown PetscFunctionReturn(0); 1211316643e7SJed Brown } 1212316643e7SJed Brown 1213316643e7SJed Brown #undef __FUNCT__ 1214316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 1215316643e7SJed Brown /*@C 1216a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1217ae8867d6SBarry Smith provided with TSSetIFunction(). 1218316643e7SJed Brown 12193f9fe445SBarry Smith Logically Collective on TS 1220316643e7SJed Brown 1221316643e7SJed Brown Input Parameters: 1222316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1223e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1224e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1225316643e7SJed Brown . f - the Jacobian evaluation routine 12260298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1227316643e7SJed Brown 1228316643e7SJed Brown Calling sequence of f: 1229ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1230316643e7SJed Brown 1231316643e7SJed Brown + t - time at step/stage being solved 12321b4a444bSJed Brown . U - state vector 12331b4a444bSJed Brown . U_t - time derivative of state vector 1234316643e7SJed Brown . a - shift 1235e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1236e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1237316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1238316643e7SJed Brown 1239316643e7SJed Brown Notes: 1240e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1241316643e7SJed Brown 1242a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1243b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1244a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1245a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1246a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1247a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1248a4f0a591SBarry Smith 1249316643e7SJed Brown Level: beginner 1250316643e7SJed Brown 1251316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1252316643e7SJed Brown 1253ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1254316643e7SJed Brown 1255316643e7SJed Brown @*/ 1256e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1257316643e7SJed Brown { 1258316643e7SJed Brown PetscErrorCode ierr; 1259089b2837SJed Brown SNES snes; 126024989b8cSPeter Brune DM dm; 1261316643e7SJed Brown 1262316643e7SJed Brown PetscFunctionBegin; 12630700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1264e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1265e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1266e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1267e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 126824989b8cSPeter Brune 126924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 127024989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 127124989b8cSPeter Brune 1272089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1273e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1274316643e7SJed Brown PetscFunctionReturn(0); 1275316643e7SJed Brown } 1276316643e7SJed Brown 12774a2ae208SSatish Balay #undef __FUNCT__ 1278e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse" 1279e1244c69SJed Brown /*@ 1280e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1281e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1282e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1283e1244c69SJed Brown not been changed by the TS. 1284e1244c69SJed Brown 1285e1244c69SJed Brown Logically Collective 1286e1244c69SJed Brown 1287e1244c69SJed Brown Input Arguments: 1288e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1289e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1290e1244c69SJed Brown 1291e1244c69SJed Brown Level: intermediate 1292e1244c69SJed Brown 1293e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1294e1244c69SJed Brown @*/ 1295e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1296e1244c69SJed Brown { 1297e1244c69SJed Brown PetscFunctionBegin; 1298e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1299e1244c69SJed Brown PetscFunctionReturn(0); 1300e1244c69SJed Brown } 1301e1244c69SJed Brown 1302e1244c69SJed Brown #undef __FUNCT__ 130355849f57SBarry Smith #define __FUNCT__ "TSLoad" 130455849f57SBarry Smith /*@C 130555849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 130655849f57SBarry Smith 130755849f57SBarry Smith Collective on PetscViewer 130855849f57SBarry Smith 130955849f57SBarry Smith Input Parameters: 131055849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 131155849f57SBarry Smith some related function before a call to TSLoad(). 131255849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 131355849f57SBarry Smith 131455849f57SBarry Smith Level: intermediate 131555849f57SBarry Smith 131655849f57SBarry Smith Notes: 131755849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 131855849f57SBarry Smith 131955849f57SBarry Smith Notes for advanced users: 132055849f57SBarry Smith Most users should not need to know the details of the binary storage 132155849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 132255849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 132355849f57SBarry Smith format is 132455849f57SBarry Smith .vb 132555849f57SBarry Smith has not yet been determined 132655849f57SBarry Smith .ve 132755849f57SBarry Smith 132855849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 132955849f57SBarry Smith @*/ 1330f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 133155849f57SBarry Smith { 133255849f57SBarry Smith PetscErrorCode ierr; 133355849f57SBarry Smith PetscBool isbinary; 133455849f57SBarry Smith PetscInt classid; 133555849f57SBarry Smith char type[256]; 13362d53ad75SBarry Smith DMTS sdm; 1337ad6bc421SBarry Smith DM dm; 133855849f57SBarry Smith 133955849f57SBarry Smith PetscFunctionBegin; 1340f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 134155849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 134255849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 134355849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 134455849f57SBarry Smith 134555849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 1346ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 134755849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 1348f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1349f2c2a1b9SBarry Smith if (ts->ops->load) { 1350f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1351f2c2a1b9SBarry Smith } 1352ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1353ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1354ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1355f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1356f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 13572d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 13582d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 135955849f57SBarry Smith PetscFunctionReturn(0); 136055849f57SBarry Smith } 136155849f57SBarry Smith 13629804daf3SBarry Smith #include <petscdraw.h> 1363e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1364e04113cfSBarry Smith #include <petscviewersaws.h> 1365f05ece33SBarry Smith #endif 136655849f57SBarry Smith #undef __FUNCT__ 13674a2ae208SSatish Balay #define __FUNCT__ "TSView" 13687e2c5f70SBarry Smith /*@C 1369d763cef2SBarry Smith TSView - Prints the TS data structure. 1370d763cef2SBarry Smith 13714c49b128SBarry Smith Collective on TS 1372d763cef2SBarry Smith 1373d763cef2SBarry Smith Input Parameters: 1374d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1375d763cef2SBarry Smith - viewer - visualization context 1376d763cef2SBarry Smith 1377d763cef2SBarry Smith Options Database Key: 1378d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1379d763cef2SBarry Smith 1380d763cef2SBarry Smith Notes: 1381d763cef2SBarry Smith The available visualization contexts include 1382b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1383b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1384d763cef2SBarry Smith output where only the first processor opens 1385d763cef2SBarry Smith the file. All other processors send their 1386d763cef2SBarry Smith data to the first processor to print. 1387d763cef2SBarry Smith 1388d763cef2SBarry Smith The user can open an alternative visualization context with 1389b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1390d763cef2SBarry Smith 1391d763cef2SBarry Smith Level: beginner 1392d763cef2SBarry Smith 1393d763cef2SBarry Smith .keywords: TS, timestep, view 1394d763cef2SBarry Smith 1395b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1396d763cef2SBarry Smith @*/ 13977087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1398d763cef2SBarry Smith { 1399dfbe8321SBarry Smith PetscErrorCode ierr; 140019fd82e9SBarry Smith TSType type; 14012b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 14022d53ad75SBarry Smith DMTS sdm; 1403e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1404536b137fSBarry Smith PetscBool issaws; 1405f05ece33SBarry Smith #endif 1406d763cef2SBarry Smith 1407d763cef2SBarry Smith PetscFunctionBegin; 14080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14093050cee2SBarry Smith if (!viewer) { 1410ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 14113050cee2SBarry Smith } 14120700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1413c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1414fd16b177SBarry Smith 1415251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1416251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 141755849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 14182b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1419e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1420536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1421f05ece33SBarry Smith #endif 142232077d6dSBarry Smith if (iascii) { 1423dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 142477431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 14257c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1426d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 14275ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1428c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1429d763cef2SBarry Smith } 14305ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1431193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 14322d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 14332d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1434d52bd9f3SBarry Smith if (ts->ops->view) { 1435d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1436d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1437d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1438d52bd9f3SBarry Smith } 14390f5bd95cSBarry Smith } else if (isstring) { 1440a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1441b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 144255849f57SBarry Smith } else if (isbinary) { 144355849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 144455849f57SBarry Smith MPI_Comm comm; 144555849f57SBarry Smith PetscMPIInt rank; 144655849f57SBarry Smith char type[256]; 144755849f57SBarry Smith 144855849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 144955849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 145055849f57SBarry Smith if (!rank) { 145155849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 145255849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 145355849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 145455849f57SBarry Smith } 145555849f57SBarry Smith if (ts->ops->view) { 145655849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 145755849f57SBarry Smith } 1458f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1459f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 14602d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 14612d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 14622b0a91c0SBarry Smith } else if (isdraw) { 14632b0a91c0SBarry Smith PetscDraw draw; 14642b0a91c0SBarry Smith char str[36]; 146589fd9fafSBarry Smith PetscReal x,y,bottom,h; 14662b0a91c0SBarry Smith 14672b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 14682b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 14692b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 14702b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 14710298fd71SBarry Smith ierr = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 147289fd9fafSBarry Smith bottom = y - h; 14732b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 14742b0a91c0SBarry Smith if (ts->ops->view) { 14752b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 14762b0a91c0SBarry Smith } 14772b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1478e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1479536b137fSBarry Smith } else if (issaws) { 1480d45a07a7SBarry Smith PetscMPIInt rank; 14812657e9d9SBarry Smith const char *name; 14822657e9d9SBarry Smith 14832657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1484d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1485d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1486d45a07a7SBarry Smith char dir[1024]; 1487d45a07a7SBarry Smith 1488e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1489a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 14902657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 14912657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 14922657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1493d763cef2SBarry Smith } 14940acecf5bSBarry Smith if (ts->ops->view) { 14950acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 14960acecf5bSBarry Smith } 1497f05ece33SBarry Smith #endif 1498f05ece33SBarry Smith } 1499f05ece33SBarry Smith 1500b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1501251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1502b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1503d763cef2SBarry Smith PetscFunctionReturn(0); 1504d763cef2SBarry Smith } 1505d763cef2SBarry Smith 1506d763cef2SBarry Smith 15074a2ae208SSatish Balay #undef __FUNCT__ 15084a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1509b07ff414SBarry Smith /*@ 1510d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1511d763cef2SBarry Smith the timesteppers. 1512d763cef2SBarry Smith 15133f9fe445SBarry Smith Logically Collective on TS 1514d763cef2SBarry Smith 1515d763cef2SBarry Smith Input Parameters: 1516d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1517d763cef2SBarry Smith - usrP - optional user context 1518d763cef2SBarry Smith 1519d763cef2SBarry Smith Level: intermediate 1520d763cef2SBarry Smith 1521d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1522d763cef2SBarry Smith 1523d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1524d763cef2SBarry Smith @*/ 15257087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1526d763cef2SBarry Smith { 1527d763cef2SBarry Smith PetscFunctionBegin; 15280700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1529d763cef2SBarry Smith ts->user = usrP; 1530d763cef2SBarry Smith PetscFunctionReturn(0); 1531d763cef2SBarry Smith } 1532d763cef2SBarry Smith 15334a2ae208SSatish Balay #undef __FUNCT__ 15344a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1535b07ff414SBarry Smith /*@ 1536d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1537d763cef2SBarry Smith timestepper. 1538d763cef2SBarry Smith 1539d763cef2SBarry Smith Not Collective 1540d763cef2SBarry Smith 1541d763cef2SBarry Smith Input Parameter: 1542d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1543d763cef2SBarry Smith 1544d763cef2SBarry Smith Output Parameter: 1545d763cef2SBarry Smith . usrP - user context 1546d763cef2SBarry Smith 1547d763cef2SBarry Smith Level: intermediate 1548d763cef2SBarry Smith 1549d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1550d763cef2SBarry Smith 1551d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1552d763cef2SBarry Smith @*/ 1553e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1554d763cef2SBarry Smith { 1555d763cef2SBarry Smith PetscFunctionBegin; 15560700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1557e71120c6SJed Brown *(void**)usrP = ts->user; 1558d763cef2SBarry Smith PetscFunctionReturn(0); 1559d763cef2SBarry Smith } 1560d763cef2SBarry Smith 15614a2ae208SSatish Balay #undef __FUNCT__ 15624a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1563d763cef2SBarry Smith /*@ 1564b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1565d763cef2SBarry Smith 1566d763cef2SBarry Smith Not Collective 1567d763cef2SBarry Smith 1568d763cef2SBarry Smith Input Parameter: 1569d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1570d763cef2SBarry Smith 1571d763cef2SBarry Smith Output Parameter: 1572b8123daeSJed Brown . iter - number of steps completed so far 1573d763cef2SBarry Smith 1574d763cef2SBarry Smith Level: intermediate 1575d763cef2SBarry Smith 1576d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 15779be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 1578d763cef2SBarry Smith @*/ 15797087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 1580d763cef2SBarry Smith { 1581d763cef2SBarry Smith PetscFunctionBegin; 15820700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15834482741eSBarry Smith PetscValidIntPointer(iter,2); 1584d763cef2SBarry Smith *iter = ts->steps; 1585d763cef2SBarry Smith PetscFunctionReturn(0); 1586d763cef2SBarry Smith } 1587d763cef2SBarry Smith 15884a2ae208SSatish Balay #undef __FUNCT__ 15894a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1590d763cef2SBarry Smith /*@ 1591d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1592d763cef2SBarry Smith as well as the initial time. 1593d763cef2SBarry Smith 15943f9fe445SBarry Smith Logically Collective on TS 1595d763cef2SBarry Smith 1596d763cef2SBarry Smith Input Parameters: 1597d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1598d763cef2SBarry Smith . initial_time - the initial time 1599d763cef2SBarry Smith - time_step - the size of the timestep 1600d763cef2SBarry Smith 1601d763cef2SBarry Smith Level: intermediate 1602d763cef2SBarry Smith 1603d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1604d763cef2SBarry Smith 1605d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1606d763cef2SBarry Smith @*/ 16077087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1608d763cef2SBarry Smith { 1609e144a568SJed Brown PetscErrorCode ierr; 1610e144a568SJed Brown 1611d763cef2SBarry Smith PetscFunctionBegin; 16120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1613e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1614d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1615d763cef2SBarry Smith PetscFunctionReturn(0); 1616d763cef2SBarry Smith } 1617d763cef2SBarry Smith 16184a2ae208SSatish Balay #undef __FUNCT__ 16194a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1620d763cef2SBarry Smith /*@ 1621d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1622d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1623d763cef2SBarry Smith 16243f9fe445SBarry Smith Logically Collective on TS 1625d763cef2SBarry Smith 1626d763cef2SBarry Smith Input Parameters: 1627d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1628d763cef2SBarry Smith - time_step - the size of the timestep 1629d763cef2SBarry Smith 1630d763cef2SBarry Smith Level: intermediate 1631d763cef2SBarry Smith 1632d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1633d763cef2SBarry Smith 1634d763cef2SBarry Smith .keywords: TS, set, timestep 1635d763cef2SBarry Smith @*/ 16367087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1637d763cef2SBarry Smith { 1638d763cef2SBarry Smith PetscFunctionBegin; 16390700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1640c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1641d763cef2SBarry Smith ts->time_step = time_step; 164231748224SBarry Smith ts->time_step_orig = time_step; 1643d763cef2SBarry Smith PetscFunctionReturn(0); 1644d763cef2SBarry Smith } 1645d763cef2SBarry Smith 16464a2ae208SSatish Balay #undef __FUNCT__ 1647a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1648a43b19c4SJed Brown /*@ 164949354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 165049354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 165149354f04SShri Abhyankar or just return at the final time TS computed. 1652a43b19c4SJed Brown 1653a43b19c4SJed Brown Logically Collective on TS 1654a43b19c4SJed Brown 1655a43b19c4SJed Brown Input Parameter: 1656a43b19c4SJed Brown + ts - the time-step context 165749354f04SShri Abhyankar - eftopt - exact final time option 1658a43b19c4SJed Brown 1659a43b19c4SJed Brown Level: beginner 1660a43b19c4SJed Brown 1661a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 1662a43b19c4SJed Brown @*/ 166349354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 1664a43b19c4SJed Brown { 1665a43b19c4SJed Brown PetscFunctionBegin; 1666a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 166749354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 166849354f04SShri Abhyankar ts->exact_final_time = eftopt; 1669a43b19c4SJed Brown PetscFunctionReturn(0); 1670a43b19c4SJed Brown } 1671a43b19c4SJed Brown 1672a43b19c4SJed Brown #undef __FUNCT__ 16734a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1674d763cef2SBarry Smith /*@ 1675d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1676d763cef2SBarry Smith 1677d763cef2SBarry Smith Not Collective 1678d763cef2SBarry Smith 1679d763cef2SBarry Smith Input Parameter: 1680d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1681d763cef2SBarry Smith 1682d763cef2SBarry Smith Output Parameter: 1683d763cef2SBarry Smith . dt - the current timestep size 1684d763cef2SBarry Smith 1685d763cef2SBarry Smith Level: intermediate 1686d763cef2SBarry Smith 1687d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1688d763cef2SBarry Smith 1689d763cef2SBarry Smith .keywords: TS, get, timestep 1690d763cef2SBarry Smith @*/ 16917087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 1692d763cef2SBarry Smith { 1693d763cef2SBarry Smith PetscFunctionBegin; 16940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1695f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1696d763cef2SBarry Smith *dt = ts->time_step; 1697d763cef2SBarry Smith PetscFunctionReturn(0); 1698d763cef2SBarry Smith } 1699d763cef2SBarry Smith 17004a2ae208SSatish Balay #undef __FUNCT__ 17014a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1702d8e5e3e6SSatish Balay /*@ 1703d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1704d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1705d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1706d763cef2SBarry Smith the solution at the next timestep has been calculated. 1707d763cef2SBarry Smith 1708d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1709d763cef2SBarry Smith 1710d763cef2SBarry Smith Input Parameter: 1711d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1712d763cef2SBarry Smith 1713d763cef2SBarry Smith Output Parameter: 1714d763cef2SBarry Smith . v - the vector containing the solution 1715d763cef2SBarry Smith 1716d763cef2SBarry Smith Level: intermediate 1717d763cef2SBarry Smith 1718d763cef2SBarry Smith .seealso: TSGetTimeStep() 1719d763cef2SBarry Smith 1720d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1721d763cef2SBarry Smith @*/ 17227087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1723d763cef2SBarry Smith { 1724d763cef2SBarry Smith PetscFunctionBegin; 17250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17264482741eSBarry Smith PetscValidPointer(v,2); 17278737fe31SLisandro Dalcin *v = ts->vec_sol; 1728d763cef2SBarry Smith PetscFunctionReturn(0); 1729d763cef2SBarry Smith } 1730d763cef2SBarry Smith 1731c235aa8dSHong Zhang #undef __FUNCT__ 1732419a887dSBarry Smith #define __FUNCT__ "TSAdjointGetGradients" 1733c235aa8dSHong Zhang /*@ 1734419a887dSBarry Smith TSAdjointGetGradients - Returns the gradients from the TSAdjointSolve() 1735c235aa8dSHong Zhang 1736c235aa8dSHong Zhang Not Collective, but Vec returned is parallel if TS is parallel 1737c235aa8dSHong Zhang 1738c235aa8dSHong Zhang Input Parameter: 1739c235aa8dSHong Zhang . ts - the TS context obtained from TSCreate() 1740c235aa8dSHong Zhang 1741c235aa8dSHong Zhang Output Parameter: 1742419a887dSBarry Smith + v - vectors containing the gradients with respect to the ODE/DAE solution variables 1743419a887dSBarry Smith - w - vectors containing the gradients with respect to the problem parameters 1744c235aa8dSHong Zhang 1745c235aa8dSHong Zhang Level: intermediate 1746c235aa8dSHong Zhang 1747c235aa8dSHong Zhang .seealso: TSGetTimeStep() 1748c235aa8dSHong Zhang 1749c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity 1750c235aa8dSHong Zhang @*/ 17515bf8c567SBarry Smith PetscErrorCode TSAdjointGetGradients(TS ts,PetscInt *numberadjs,Vec **v,Vec **w) 1752c235aa8dSHong Zhang { 1753c235aa8dSHong Zhang PetscFunctionBegin; 1754c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1755b18ea86cSHong Zhang if (numberadjs) *numberadjs = ts->numberadjs; 1756419a887dSBarry Smith if (v) *v = ts->vecs_sensi; 1757419a887dSBarry Smith if (w) *w = ts->vecs_sensip; 1758c235aa8dSHong Zhang PetscFunctionReturn(0); 1759c235aa8dSHong Zhang } 1760c235aa8dSHong Zhang 1761bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 17624a2ae208SSatish Balay #undef __FUNCT__ 1763bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1764d8e5e3e6SSatish Balay /*@ 1765bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1766d763cef2SBarry Smith 1767bdad233fSMatthew Knepley Not collective 1768d763cef2SBarry Smith 1769bdad233fSMatthew Knepley Input Parameters: 1770bdad233fSMatthew Knepley + ts - The TS 1771bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1772d763cef2SBarry Smith .vb 17730910c330SBarry Smith U_t - A U = 0 (linear) 17740910c330SBarry Smith U_t - A(t) U = 0 (linear) 17750910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 1776d763cef2SBarry Smith .ve 1777d763cef2SBarry Smith 1778d763cef2SBarry Smith Level: beginner 1779d763cef2SBarry Smith 1780bdad233fSMatthew Knepley .keywords: TS, problem type 1781bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1782d763cef2SBarry Smith @*/ 17837087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1784a7cc72afSBarry Smith { 17859e2a6581SJed Brown PetscErrorCode ierr; 17869e2a6581SJed Brown 1787d763cef2SBarry Smith PetscFunctionBegin; 17880700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1789bdad233fSMatthew Knepley ts->problem_type = type; 17909e2a6581SJed Brown if (type == TS_LINEAR) { 17919e2a6581SJed Brown SNES snes; 17929e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 17939e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 17949e2a6581SJed Brown } 1795d763cef2SBarry Smith PetscFunctionReturn(0); 1796d763cef2SBarry Smith } 1797d763cef2SBarry Smith 1798bdad233fSMatthew Knepley #undef __FUNCT__ 1799bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1800bdad233fSMatthew Knepley /*@C 1801bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1802bdad233fSMatthew Knepley 1803bdad233fSMatthew Knepley Not collective 1804bdad233fSMatthew Knepley 1805bdad233fSMatthew Knepley Input Parameter: 1806bdad233fSMatthew Knepley . ts - The TS 1807bdad233fSMatthew Knepley 1808bdad233fSMatthew Knepley Output Parameter: 1809bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1810bdad233fSMatthew Knepley .vb 1811089b2837SJed Brown M U_t = A U 1812089b2837SJed Brown M(t) U_t = A(t) U 1813b5abc632SBarry Smith F(t,U,U_t) 1814bdad233fSMatthew Knepley .ve 1815bdad233fSMatthew Knepley 1816bdad233fSMatthew Knepley Level: beginner 1817bdad233fSMatthew Knepley 1818bdad233fSMatthew Knepley .keywords: TS, problem type 1819bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1820bdad233fSMatthew Knepley @*/ 18217087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1822a7cc72afSBarry Smith { 1823bdad233fSMatthew Knepley PetscFunctionBegin; 18240700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 18254482741eSBarry Smith PetscValidIntPointer(type,2); 1826bdad233fSMatthew Knepley *type = ts->problem_type; 1827bdad233fSMatthew Knepley PetscFunctionReturn(0); 1828bdad233fSMatthew Knepley } 1829d763cef2SBarry Smith 18304a2ae208SSatish Balay #undef __FUNCT__ 18314a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1832d763cef2SBarry Smith /*@ 1833d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1834d763cef2SBarry Smith of a timestepper. 1835d763cef2SBarry Smith 1836d763cef2SBarry Smith Collective on TS 1837d763cef2SBarry Smith 1838d763cef2SBarry Smith Input Parameter: 1839d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1840d763cef2SBarry Smith 1841d763cef2SBarry Smith Notes: 1842d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1843d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1844d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1845d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1846d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1847d763cef2SBarry Smith 1848d763cef2SBarry Smith Level: advanced 1849d763cef2SBarry Smith 1850d763cef2SBarry Smith .keywords: TS, timestep, setup 1851d763cef2SBarry Smith 1852d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1853d763cef2SBarry Smith @*/ 18547087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1855d763cef2SBarry Smith { 1856dfbe8321SBarry Smith PetscErrorCode ierr; 18576c6b9e74SPeter Brune DM dm; 18586c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 1859d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 18606c6b9e74SPeter Brune TSIJacobian ijac; 18616c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1862d763cef2SBarry Smith 1863d763cef2SBarry Smith PetscFunctionBegin; 18640700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1865277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1866277b19d0SLisandro Dalcin 18672c18e0fdSBarry Smith ts->total_steps = 0; 18687adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 18699596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1870d763cef2SBarry Smith } 1871277b19d0SLisandro Dalcin 1872277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1873277b19d0SLisandro Dalcin 1874c235aa8dSHong Zhang 1875552698daSJed Brown ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 18761c3436cfSJed Brown 1877e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 1878e1244c69SJed Brown Mat Amat,Pmat; 1879e1244c69SJed Brown SNES snes; 1880e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1881e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 1882e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 1883e1244c69SJed Brown * have displaced the RHS matrix */ 1884e1244c69SJed Brown if (Amat == ts->Arhs) { 1885e1244c69SJed Brown ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 1886e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 1887e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 1888e1244c69SJed Brown } 1889e1244c69SJed Brown if (Pmat == ts->Brhs) { 1890e1244c69SJed Brown ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 1891e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 1892e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 1893e1244c69SJed Brown } 1894e1244c69SJed Brown } 1895277b19d0SLisandro Dalcin if (ts->ops->setup) { 1896000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1897277b19d0SLisandro Dalcin } 1898277b19d0SLisandro Dalcin 18996c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 19006c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 19016c6b9e74SPeter Brune */ 19026c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 19030298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 19046c6b9e74SPeter Brune if (!func) { 19056c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 19066c6b9e74SPeter Brune } 19076c6b9e74SPeter 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. 19086c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 19096c6b9e74SPeter Brune */ 19100298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 19110298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 19120298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 19136c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 19146c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 19156c6b9e74SPeter Brune } 1916277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1917277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1918277b19d0SLisandro Dalcin } 1919277b19d0SLisandro Dalcin 1920277b19d0SLisandro Dalcin #undef __FUNCT__ 1921f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp" 1922f6a906c0SBarry Smith /*@ 1923f6a906c0SBarry Smith TSAdjointSetUp - Sets up the internal data structures for the later use 1924f6a906c0SBarry Smith of an adjoint solver 1925f6a906c0SBarry Smith 1926f6a906c0SBarry Smith Collective on TS 1927f6a906c0SBarry Smith 1928f6a906c0SBarry Smith Input Parameter: 1929f6a906c0SBarry Smith . ts - the TS context obtained from TSCreate() 1930f6a906c0SBarry Smith 1931f6a906c0SBarry Smith Notes: 1932f6a906c0SBarry Smith For basic use of the TS solvers the user need not explicitly call 1933f6a906c0SBarry Smith TSSetUp(), since these actions will automatically occur during 1934f6a906c0SBarry Smith the call to TSStep(). However, if one wishes to control this 1935f6a906c0SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1936f6a906c0SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1937f6a906c0SBarry Smith 1938f6a906c0SBarry Smith Level: advanced 1939f6a906c0SBarry Smith 1940f6a906c0SBarry Smith .keywords: TS, timestep, setup 1941f6a906c0SBarry Smith 1942f6a906c0SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1943f6a906c0SBarry Smith @*/ 1944f6a906c0SBarry Smith PetscErrorCode TSAdjointSetUp(TS ts) 1945f6a906c0SBarry Smith { 1946f6a906c0SBarry Smith PetscErrorCode ierr; 1947f6a906c0SBarry Smith 1948f6a906c0SBarry Smith PetscFunctionBegin; 1949f6a906c0SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1950f6a906c0SBarry Smith if (ts->adjointsetupcalled) PetscFunctionReturn(0); 1951419a887dSBarry Smith if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSAdjointSetGradients() first"); 195242f2b339SBarry Smith if (ts->ops->adjointsetup) { 195342f2b339SBarry Smith ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr); 1954f6a906c0SBarry Smith } 1955f6a906c0SBarry Smith ts->adjointsetupcalled = PETSC_TRUE; 1956f6a906c0SBarry Smith PetscFunctionReturn(0); 1957f6a906c0SBarry Smith } 1958f6a906c0SBarry Smith 1959f6a906c0SBarry Smith #undef __FUNCT__ 1960277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1961277b19d0SLisandro Dalcin /*@ 1962277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1963277b19d0SLisandro Dalcin 1964277b19d0SLisandro Dalcin Collective on TS 1965277b19d0SLisandro Dalcin 1966277b19d0SLisandro Dalcin Input Parameter: 1967277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1968277b19d0SLisandro Dalcin 1969277b19d0SLisandro Dalcin Level: beginner 1970277b19d0SLisandro Dalcin 1971277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1972277b19d0SLisandro Dalcin 1973277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1974277b19d0SLisandro Dalcin @*/ 1975277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1976277b19d0SLisandro Dalcin { 1977277b19d0SLisandro Dalcin PetscErrorCode ierr; 1978277b19d0SLisandro Dalcin 1979277b19d0SLisandro Dalcin PetscFunctionBegin; 1980277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1981b18ea86cSHong Zhang 1982277b19d0SLisandro Dalcin if (ts->ops->reset) { 1983277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1984277b19d0SLisandro Dalcin } 1985277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 1986bbd56ea5SKarl Rupp 19874e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 19884e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1989214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 19906bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1991e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 1992e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 199338637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 1994f8fee759SHong Zhang ts->vecs_sensi = 0; 1995f8fee759SHong Zhang ts->vecs_sensip = 0; 1996ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 19972c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 199836eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 1999277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2000d763cef2SBarry Smith PetscFunctionReturn(0); 2001d763cef2SBarry Smith } 2002d763cef2SBarry Smith 20034a2ae208SSatish Balay #undef __FUNCT__ 20044a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 2005d8e5e3e6SSatish Balay /*@ 2006d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2007d763cef2SBarry Smith with TSCreate(). 2008d763cef2SBarry Smith 2009d763cef2SBarry Smith Collective on TS 2010d763cef2SBarry Smith 2011d763cef2SBarry Smith Input Parameter: 2012d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2013d763cef2SBarry Smith 2014d763cef2SBarry Smith Level: beginner 2015d763cef2SBarry Smith 2016d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2017d763cef2SBarry Smith 2018d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2019d763cef2SBarry Smith @*/ 20206bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2021d763cef2SBarry Smith { 20226849ba73SBarry Smith PetscErrorCode ierr; 2023d763cef2SBarry Smith 2024d763cef2SBarry Smith PetscFunctionBegin; 20256bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 20266bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 20276bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2028d763cef2SBarry Smith 20296bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2030277b19d0SLisandro Dalcin 2031e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2032e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 20336bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 20346d4c513bSLisandro Dalcin 2035bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2036bc952696SBarry Smith 203784df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 2038aeb4809dSShri Abhyankar if ((*ts)->event) { 2039aeb4809dSShri Abhyankar ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr); 2040aeb4809dSShri Abhyankar } 20416bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 20426bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 20436bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 20446d4c513bSLisandro Dalcin 2045a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2046d763cef2SBarry Smith PetscFunctionReturn(0); 2047d763cef2SBarry Smith } 2048d763cef2SBarry Smith 20494a2ae208SSatish Balay #undef __FUNCT__ 20504a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 2051d8e5e3e6SSatish Balay /*@ 2052d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2053d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2054d763cef2SBarry Smith 2055d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2056d763cef2SBarry Smith 2057d763cef2SBarry Smith Input Parameter: 2058d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2059d763cef2SBarry Smith 2060d763cef2SBarry Smith Output Parameter: 2061d763cef2SBarry Smith . snes - the nonlinear solver context 2062d763cef2SBarry Smith 2063d763cef2SBarry Smith Notes: 2064d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2065d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 206694b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2067d763cef2SBarry Smith 2068d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 20690298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2070d763cef2SBarry Smith 2071d763cef2SBarry Smith Level: beginner 2072d763cef2SBarry Smith 2073d763cef2SBarry Smith .keywords: timestep, get, SNES 2074d763cef2SBarry Smith @*/ 20757087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2076d763cef2SBarry Smith { 2077d372ba47SLisandro Dalcin PetscErrorCode ierr; 2078d372ba47SLisandro Dalcin 2079d763cef2SBarry Smith PetscFunctionBegin; 20800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20814482741eSBarry Smith PetscValidPointer(snes,2); 2082d372ba47SLisandro Dalcin if (!ts->snes) { 2083ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 20840298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 20853bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2086d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2087496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 20889e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 20899e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 20909e2a6581SJed Brown } 2091d372ba47SLisandro Dalcin } 2092d763cef2SBarry Smith *snes = ts->snes; 2093d763cef2SBarry Smith PetscFunctionReturn(0); 2094d763cef2SBarry Smith } 2095d763cef2SBarry Smith 20964a2ae208SSatish Balay #undef __FUNCT__ 2097deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES" 2098deb2cd25SJed Brown /*@ 2099deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2100deb2cd25SJed Brown 2101deb2cd25SJed Brown Collective 2102deb2cd25SJed Brown 2103deb2cd25SJed Brown Input Parameter: 2104deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2105deb2cd25SJed Brown - snes - the nonlinear solver context 2106deb2cd25SJed Brown 2107deb2cd25SJed Brown Notes: 2108deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2109deb2cd25SJed Brown 2110deb2cd25SJed Brown Level: developer 2111deb2cd25SJed Brown 2112deb2cd25SJed Brown .keywords: timestep, set, SNES 2113deb2cd25SJed Brown @*/ 2114deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2115deb2cd25SJed Brown { 2116deb2cd25SJed Brown PetscErrorCode ierr; 2117d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2118deb2cd25SJed Brown 2119deb2cd25SJed Brown PetscFunctionBegin; 2120deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2121deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2122deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2123deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2124bbd56ea5SKarl Rupp 2125deb2cd25SJed Brown ts->snes = snes; 2126bbd56ea5SKarl Rupp 21270298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 21280298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2129740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 21300298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2131740132f1SEmil Constantinescu } 2132deb2cd25SJed Brown PetscFunctionReturn(0); 2133deb2cd25SJed Brown } 2134deb2cd25SJed Brown 2135deb2cd25SJed Brown #undef __FUNCT__ 213694b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 2137d8e5e3e6SSatish Balay /*@ 213894b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2139d763cef2SBarry Smith a TS (timestepper) context. 2140d763cef2SBarry Smith 214194b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2142d763cef2SBarry Smith 2143d763cef2SBarry Smith Input Parameter: 2144d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2145d763cef2SBarry Smith 2146d763cef2SBarry Smith Output Parameter: 214794b7f48cSBarry Smith . ksp - the nonlinear solver context 2148d763cef2SBarry Smith 2149d763cef2SBarry Smith Notes: 215094b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2151d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2152d763cef2SBarry Smith KSP and PC contexts as well. 2153d763cef2SBarry Smith 215494b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 21550298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2156d763cef2SBarry Smith 2157d763cef2SBarry Smith Level: beginner 2158d763cef2SBarry Smith 215994b7f48cSBarry Smith .keywords: timestep, get, KSP 2160d763cef2SBarry Smith @*/ 21617087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2162d763cef2SBarry Smith { 2163d372ba47SLisandro Dalcin PetscErrorCode ierr; 2164089b2837SJed Brown SNES snes; 2165d372ba47SLisandro Dalcin 2166d763cef2SBarry Smith PetscFunctionBegin; 21670700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 21684482741eSBarry Smith PetscValidPointer(ksp,2); 216917186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2170e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2171089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2172089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2173d763cef2SBarry Smith PetscFunctionReturn(0); 2174d763cef2SBarry Smith } 2175d763cef2SBarry Smith 2176d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2177d763cef2SBarry Smith 21784a2ae208SSatish Balay #undef __FUNCT__ 2179adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 2180adb62b0dSMatthew Knepley /*@ 2181adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2182adb62b0dSMatthew Knepley maximum time for iteration. 2183adb62b0dSMatthew Knepley 21843f9fe445SBarry Smith Not Collective 2185adb62b0dSMatthew Knepley 2186adb62b0dSMatthew Knepley Input Parameters: 2187adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 21880298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 21890298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2190adb62b0dSMatthew Knepley 2191adb62b0dSMatthew Knepley Level: intermediate 2192adb62b0dSMatthew Knepley 2193adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2194adb62b0dSMatthew Knepley @*/ 21957087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2196adb62b0dSMatthew Knepley { 2197adb62b0dSMatthew Knepley PetscFunctionBegin; 21980700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2199abc0a331SBarry Smith if (maxsteps) { 22004482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2201adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2202adb62b0dSMatthew Knepley } 2203abc0a331SBarry Smith if (maxtime) { 22044482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2205adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2206adb62b0dSMatthew Knepley } 2207adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2208adb62b0dSMatthew Knepley } 2209adb62b0dSMatthew Knepley 2210adb62b0dSMatthew Knepley #undef __FUNCT__ 22114a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 2212d763cef2SBarry Smith /*@ 2213d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2214d763cef2SBarry Smith maximum time for iteration. 2215d763cef2SBarry Smith 22163f9fe445SBarry Smith Logically Collective on TS 2217d763cef2SBarry Smith 2218d763cef2SBarry Smith Input Parameters: 2219d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2220d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2221d763cef2SBarry Smith - maxtime - final time to iterate to 2222d763cef2SBarry Smith 2223d763cef2SBarry Smith Options Database Keys: 2224d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 22253bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2226d763cef2SBarry Smith 2227d763cef2SBarry Smith Notes: 2228d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2229d763cef2SBarry Smith 2230d763cef2SBarry Smith Level: intermediate 2231d763cef2SBarry Smith 2232d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2233a43b19c4SJed Brown 2234a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2235d763cef2SBarry Smith @*/ 22367087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2237d763cef2SBarry Smith { 2238d763cef2SBarry Smith PetscFunctionBegin; 22390700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2240c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2241c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 224239b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 224339b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2244d763cef2SBarry Smith PetscFunctionReturn(0); 2245d763cef2SBarry Smith } 2246d763cef2SBarry Smith 22474a2ae208SSatish Balay #undef __FUNCT__ 22484a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 2249d763cef2SBarry Smith /*@ 2250d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2251d763cef2SBarry Smith for use by the TS routines. 2252d763cef2SBarry Smith 22533f9fe445SBarry Smith Logically Collective on TS and Vec 2254d763cef2SBarry Smith 2255d763cef2SBarry Smith Input Parameters: 2256d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 22570910c330SBarry Smith - u - the solution vector 2258d763cef2SBarry Smith 2259d763cef2SBarry Smith Level: beginner 2260d763cef2SBarry Smith 2261d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2262d763cef2SBarry Smith @*/ 22630910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2264d763cef2SBarry Smith { 22658737fe31SLisandro Dalcin PetscErrorCode ierr; 2266496e6a7aSJed Brown DM dm; 22678737fe31SLisandro Dalcin 2268d763cef2SBarry Smith PetscFunctionBegin; 22690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22700910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 22710910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 22726bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2273bbd56ea5SKarl Rupp 22740910c330SBarry Smith ts->vec_sol = u; 2275bbd56ea5SKarl Rupp 2276496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 22770910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2278d763cef2SBarry Smith PetscFunctionReturn(0); 2279d763cef2SBarry Smith } 2280d763cef2SBarry Smith 2281e74ef692SMatthew Knepley #undef __FUNCT__ 228273b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps" 228373b18844SBarry Smith /*@ 228473b18844SBarry Smith TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time 228573b18844SBarry Smith 228673b18844SBarry Smith Logically Collective on TS 228773b18844SBarry Smith 228873b18844SBarry Smith Input Parameters: 228973b18844SBarry Smith + ts - the TS context obtained from TSCreate() 229073b18844SBarry Smith . steps - number of steps to use 229173b18844SBarry Smith 229273b18844SBarry Smith Level: intermediate 229373b18844SBarry Smith 229473b18844SBarry Smith Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this 229573b18844SBarry Smith so as to integrate back to less than the original timestep 229673b18844SBarry Smith 229773b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations 229873b18844SBarry Smith 229973b18844SBarry Smith .seealso: TSSetExactFinalTime() 230073b18844SBarry Smith @*/ 230173b18844SBarry Smith PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps) 230273b18844SBarry Smith { 230373b18844SBarry Smith PetscFunctionBegin; 230473b18844SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 230573b18844SBarry Smith PetscValidLogicalCollectiveInt(ts,steps,2); 230673b18844SBarry Smith if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps"); 230773b18844SBarry 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"); 230873b18844SBarry Smith ts->adjoint_max_steps = steps; 230973b18844SBarry Smith PetscFunctionReturn(0); 231073b18844SBarry Smith } 231173b18844SBarry Smith 231273b18844SBarry Smith #undef __FUNCT__ 2313419a887dSBarry Smith #define __FUNCT__ "TSAdjointSetGradients" 2314c235aa8dSHong Zhang /*@ 2315419a887dSBarry Smith TSAdjointSetGradients - Sets the initial value of gradients w.r.t. initial conditions and w.r.t. the problem parameters for use by the TS routines. 2316c235aa8dSHong Zhang 2317c235aa8dSHong Zhang Logically Collective on TS and Vec 2318c235aa8dSHong Zhang 2319c235aa8dSHong Zhang Input Parameters: 2320c235aa8dSHong Zhang + ts - the TS context obtained from TSCreate() 2321419a887dSBarry Smith . u - gradients with respect to the initial condition variables 2322419a887dSBarry Smith - w - gradients with respect to the parameters 2323c235aa8dSHong Zhang 2324c235aa8dSHong Zhang Level: beginner 2325c235aa8dSHong Zhang 2326c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions 2327c235aa8dSHong Zhang @*/ 2328419a887dSBarry Smith PetscErrorCode TSAdjointSetGradients(TS ts,PetscInt numberadjs,Vec *u,Vec *w) 2329c235aa8dSHong Zhang { 2330c235aa8dSHong Zhang PetscFunctionBegin; 2331c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2332b18ea86cSHong Zhang PetscValidPointer(u,2); 2333b18ea86cSHong Zhang ts->vecs_sensi = u; 2334419a887dSBarry Smith ts->vecs_sensip = w; 2335b18ea86cSHong Zhang ts->numberadjs = numberadjs; 2336ad8e2604SHong Zhang PetscFunctionReturn(0); 2337ad8e2604SHong Zhang } 2338ad8e2604SHong Zhang 2339ad8e2604SHong Zhang #undef __FUNCT__ 23405bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian" 2341ad8e2604SHong Zhang /*@C 23425bf8c567SBarry Smith TSAdjointSetRHSJacobian - Sets the function that computes the Jacobian w.r.t. parameters. 2343ad8e2604SHong Zhang 2344ad8e2604SHong Zhang Logically Collective on TS 2345ad8e2604SHong Zhang 2346ad8e2604SHong Zhang Input Parameters: 2347ad8e2604SHong Zhang + ts - The TS context obtained from TSCreate() 2348ad8e2604SHong Zhang - func - The function 2349ad8e2604SHong Zhang 2350ad8e2604SHong Zhang Calling sequence of func: 235105755b9cSHong Zhang $ func (TS ts,PetscReal t,Vec u,Mat A,void *ctx); 235205755b9cSHong Zhang + t - current timestep 235305755b9cSHong Zhang . u - input vector 235405755b9cSHong Zhang . A - output matrix 235505755b9cSHong Zhang - ctx - [optional] user-defined function context 2356ad8e2604SHong Zhang 2357ad8e2604SHong Zhang Level: intermediate 2358ad8e2604SHong Zhang 2359ad8e2604SHong Zhang .keywords: TS, sensitivity 2360ad8e2604SHong Zhang .seealso: 2361ad8e2604SHong Zhang @*/ 23625bf8c567SBarry Smith PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 2363ad8e2604SHong Zhang { 2364ad8e2604SHong Zhang PetscErrorCode ierr; 2365ad8e2604SHong Zhang 2366ad8e2604SHong Zhang PetscFunctionBegin; 2367ad8e2604SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2368ad8e2604SHong Zhang if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 2369ad8e2604SHong Zhang 2370ad8e2604SHong Zhang ts->rhsjacobianp = func; 2371ad8e2604SHong Zhang ts->rhsjacobianpctx = ctx; 2372ad8e2604SHong Zhang if(Amat) { 2373ad8e2604SHong Zhang ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 2374ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2375ad8e2604SHong Zhang 2376ad8e2604SHong Zhang ts->Jacp = Amat; 2377ad8e2604SHong Zhang } 2378ad8e2604SHong Zhang PetscFunctionReturn(0); 2379ad8e2604SHong Zhang } 2380ad8e2604SHong Zhang 2381ad8e2604SHong Zhang #undef __FUNCT__ 23825bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian" 2383ad8e2604SHong Zhang /*@ 23845bf8c567SBarry Smith TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function. 2385ad8e2604SHong Zhang 2386ad8e2604SHong Zhang Collective on TS 2387ad8e2604SHong Zhang 2388ad8e2604SHong Zhang Input Parameters: 2389ad8e2604SHong Zhang . ts - The TS context obtained from TSCreate() 2390ad8e2604SHong Zhang 2391ad8e2604SHong Zhang Level: developer 2392ad8e2604SHong Zhang 2393ad8e2604SHong Zhang .keywords: TS, sensitivity 23945bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian() 2395ad8e2604SHong Zhang @*/ 23965bf8c567SBarry Smith PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat) 2397ad8e2604SHong Zhang { 2398ad8e2604SHong Zhang PetscErrorCode ierr; 2399ad8e2604SHong Zhang 2400ad8e2604SHong Zhang PetscFunctionBegin; 2401ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2402ad8e2604SHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 2403ad8e2604SHong Zhang PetscValidPointer(Amat,4); 2404ad8e2604SHong Zhang 2405ad8e2604SHong Zhang PetscStackPush("TS user JacobianP function for sensitivity analysis"); 2406ad8e2604SHong Zhang ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 2407ad8e2604SHong Zhang PetscStackPop; 2408ad8e2604SHong Zhang 2409c235aa8dSHong Zhang PetscFunctionReturn(0); 2410c235aa8dSHong Zhang } 2411c235aa8dSHong Zhang 2412c235aa8dSHong Zhang #undef __FUNCT__ 2413d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetCostIntegrand" 24146fd0887fSHong Zhang /*@C 24158a2f769dSBarry Smith TSAdjointSetCostIntegrand - Sets the routine for evaluating the integral term in a cost function 24166fd0887fSHong Zhang 24176fd0887fSHong Zhang Logically Collective on TS 24186fd0887fSHong Zhang 24196fd0887fSHong Zhang Input Parameters: 24206fd0887fSHong Zhang + ts - the TS context obtained from TSCreate() 24218a2f769dSBarry Smith . numberadjs - number of gradients to be computed 24226fd0887fSHong Zhang . fq - routine for evaluating the right-hand-side function 2423b612ec54SBarry Smith . drdy - array of vectors to contain the gradient of the r's with respect to y, NULL if not a function of y 2424b612ec54SBarry Smith . drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y 2425b612ec54SBarry Smith . drdp - array of vectors to contain the gradient of the r's with respect to p, NULL if not a function of p 2426b612ec54SBarry Smith . drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p 2427b612ec54SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 24286fd0887fSHong Zhang 2429b612ec54SBarry Smith Calling sequence of fq: 2430a2ae3dd2SHong Zhang $ TSCostIntegrand(TS ts,PetscReal t,Vec u,PetscReal *f,void *ctx); 24316fd0887fSHong Zhang 24326fd0887fSHong Zhang + t - current timestep 24336fd0887fSHong Zhang . u - input vector 243436eaed60SHong Zhang . f - function vector 24356fd0887fSHong Zhang - ctx - [optional] user-defined function context 24366fd0887fSHong Zhang 2437b612ec54SBarry Smith Calling sequence of drdyf: 2438b612ec54SBarry Smith $ PetscErroCode drdyf(TS ts,PetscReal t,Vec U,Vec *drdy,void *ctx); 2439b612ec54SBarry Smith 2440b612ec54SBarry Smith Calling sequence of drdpf: 2441b612ec54SBarry Smith $ PetscErroCode drdpf(TS ts,PetscReal t,Vec U,Vec *drdp,void *ctx); 2442b612ec54SBarry Smith 2443b612ec54SBarry Smith Level: intermediate 24446fd0887fSHong Zhang 24456fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 24466fd0887fSHong Zhang 24475bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian(),TSAdjointGetGradients(), TSAdjointSetGradients() 24486fd0887fSHong Zhang @*/ 24498a2f769dSBarry Smith PetscErrorCode TSAdjointSetCostIntegrand(TS ts,PetscInt numberadjs, PetscErrorCode (*fq)(TS,PetscReal,Vec,Vec,void*), 2450b612ec54SBarry Smith Vec *drdy,PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*), 2451b612ec54SBarry Smith Vec *drdp,PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),void *ctx) 24526fd0887fSHong Zhang { 24536fd0887fSHong Zhang PetscErrorCode ierr; 24546fd0887fSHong Zhang 24556fd0887fSHong Zhang PetscFunctionBegin; 24566fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2457419a887dSBarry Smith if (!ts->numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Call TSAdjointSetGradients() first so that the number of cost functions can be determined."); 2458419a887dSBarry Smith if (ts->numberadjs && ts->numberadjs!=numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSAdjointSetCostIntegrand()) is inconsistent with the one set by TSAdjointSetGradients()"); 24596fd0887fSHong Zhang 24608a2f769dSBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,numberadjs,&ts->vec_costintegral);CHKERRQ(ierr); 24612c39e106SBarry Smith ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr); 2462e4680132SHong Zhang ts->costintegrand = fq; 246305755b9cSHong Zhang ts->costintegrandctx = ctx; 24646fd0887fSHong Zhang 2465b612ec54SBarry Smith ts->drdyfunction = drdyf; 2466b612ec54SBarry Smith ts->vecs_drdy = drdy; 2467b612ec54SBarry Smith 2468b612ec54SBarry Smith ts->drdpfunction = drdpf; 2469b612ec54SBarry Smith ts->vecs_drdp = drdp; 2470b612ec54SBarry Smith 24716fd0887fSHong Zhang PetscFunctionReturn(0); 24726fd0887fSHong Zhang } 24736fd0887fSHong Zhang 24746fd0887fSHong Zhang #undef __FUNCT__ 24752c39e106SBarry Smith #define __FUNCT__ "TSAdjointGetCostIntegral" 247636eaed60SHong Zhang /*@ 24772c39e106SBarry Smith TSAdjointGetCostIntegral - Returns the values of the integral term in the cost functions. 247836eaed60SHong Zhang It is valid to call the routine after a backward run. 247936eaed60SHong Zhang 248036eaed60SHong Zhang Not Collective 248136eaed60SHong Zhang 248236eaed60SHong Zhang Input Parameter: 248336eaed60SHong Zhang . ts - the TS context obtained from TSCreate() 248436eaed60SHong Zhang 248536eaed60SHong Zhang Output Parameter: 24862c39e106SBarry Smith . v - the vector containing the integrals for each cost function 248736eaed60SHong Zhang 248836eaed60SHong Zhang Level: intermediate 248936eaed60SHong Zhang 2490d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand() 249136eaed60SHong Zhang 249236eaed60SHong Zhang .keywords: TS, sensitivity analysis 249336eaed60SHong Zhang @*/ 24942c39e106SBarry Smith PetscErrorCode TSAdjointGetCostIntegral(TS ts,Vec *v) 249536eaed60SHong Zhang { 249636eaed60SHong Zhang PetscFunctionBegin; 249736eaed60SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 249836eaed60SHong Zhang PetscValidPointer(v,2); 24992c39e106SBarry Smith *v = ts->vec_costintegral; 250036eaed60SHong Zhang PetscFunctionReturn(0); 250136eaed60SHong Zhang } 250236eaed60SHong Zhang 250336eaed60SHong Zhang #undef __FUNCT__ 2504d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand" 25056fd0887fSHong Zhang /*@ 25062c39e106SBarry Smith TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions. 25076fd0887fSHong Zhang 25086fd0887fSHong Zhang Input Parameters: 25096fd0887fSHong Zhang + ts - the TS context 25106fd0887fSHong Zhang . t - current time 251105755b9cSHong Zhang - U - state vector 25126fd0887fSHong Zhang 25136fd0887fSHong Zhang Output Parameter: 25146fd0887fSHong Zhang . q - vector of size numberadjs to hold the outputs 25156fd0887fSHong Zhang 25166fd0887fSHong Zhang Note: 25176fd0887fSHong Zhang Most users should not need to explicitly call this routine, as it 25186fd0887fSHong Zhang is used internally within the sensitivity analysis context. 25196fd0887fSHong Zhang 25206fd0887fSHong Zhang Level: developer 25216fd0887fSHong Zhang 25226fd0887fSHong Zhang .keywords: TS, compute 25236fd0887fSHong Zhang 2524d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand() 25256fd0887fSHong Zhang @*/ 2526d4aa0a58SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec U,Vec q) 25276fd0887fSHong Zhang { 25286fd0887fSHong Zhang PetscErrorCode ierr; 25296fd0887fSHong Zhang 25306fd0887fSHong Zhang PetscFunctionBegin; 25316fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25326fd0887fSHong Zhang PetscValidHeaderSpecific(U,VEC_CLASSID,3); 25336fd0887fSHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,4); 25346fd0887fSHong Zhang 25356fd0887fSHong Zhang ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,q,0);CHKERRQ(ierr); 2536e4680132SHong Zhang if (ts->costintegrand) { 2537e4680132SHong Zhang PetscStackPush("TS user integrand in the cost function"); 253805755b9cSHong Zhang ierr = (*ts->costintegrand)(ts,t,U,q,ts->costintegrandctx);CHKERRQ(ierr); 25396fd0887fSHong Zhang PetscStackPop; 25406fd0887fSHong Zhang } else { 25416fd0887fSHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 25426fd0887fSHong Zhang } 25436fd0887fSHong Zhang 25446fd0887fSHong Zhang ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,q,0);CHKERRQ(ierr); 25456fd0887fSHong Zhang PetscFunctionReturn(0); 25466fd0887fSHong Zhang } 25476fd0887fSHong Zhang 25486fd0887fSHong Zhang #undef __FUNCT__ 2549d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction" 255005755b9cSHong Zhang /*@ 2551d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 255205755b9cSHong Zhang 255305755b9cSHong Zhang Collective on TS 255405755b9cSHong Zhang 255505755b9cSHong Zhang Input Parameters: 255605755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 255705755b9cSHong Zhang 255805755b9cSHong Zhang Notes: 2559d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 256005755b9cSHong Zhang so most users would not generally call this routine themselves. 256105755b9cSHong Zhang 256205755b9cSHong Zhang Level: developer 256305755b9cSHong Zhang 256405755b9cSHong Zhang .keywords: TS, sensitivity 2565d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction() 256605755b9cSHong Zhang @*/ 2567d4aa0a58SBarry Smith PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec X,Vec *drdy) 256805755b9cSHong Zhang { 256905755b9cSHong Zhang PetscErrorCode ierr; 257005755b9cSHong Zhang 257105755b9cSHong Zhang PetscFunctionBegin; 257205755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 257305755b9cSHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 257405755b9cSHong Zhang 257505755b9cSHong Zhang PetscStackPush("TS user DRDY function for sensitivity analysis"); 2576b612ec54SBarry Smith ierr = (*ts->drdyfunction)(ts,t,X,drdy,ts->costintegrandctx); CHKERRQ(ierr); 257705755b9cSHong Zhang PetscStackPop; 257805755b9cSHong Zhang PetscFunctionReturn(0); 257905755b9cSHong Zhang } 258005755b9cSHong Zhang 258105755b9cSHong Zhang #undef __FUNCT__ 2582d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction" 258305755b9cSHong Zhang /*@ 2584d4aa0a58SBarry Smith TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 258505755b9cSHong Zhang 258605755b9cSHong Zhang Collective on TS 258705755b9cSHong Zhang 258805755b9cSHong Zhang Input Parameters: 258905755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 259005755b9cSHong Zhang 259105755b9cSHong Zhang Notes: 259205755b9cSHong Zhang TSDRDPFunction() is typically used for sensitivity implementation, 259305755b9cSHong Zhang so most users would not generally call this routine themselves. 259405755b9cSHong Zhang 259505755b9cSHong Zhang Level: developer 259605755b9cSHong Zhang 259705755b9cSHong Zhang .keywords: TS, sensitivity 2598d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction() 259905755b9cSHong Zhang @*/ 2600d4aa0a58SBarry Smith PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec X,Vec *drdp) 260105755b9cSHong Zhang { 260205755b9cSHong Zhang PetscErrorCode ierr; 260305755b9cSHong Zhang 260405755b9cSHong Zhang PetscFunctionBegin; 260505755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 260605755b9cSHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 260705755b9cSHong Zhang 260805755b9cSHong Zhang PetscStackPush("TS user DRDP function for sensitivity analysis"); 2609b612ec54SBarry Smith ierr = (*ts->drdpfunction)(ts,t,X,drdp,ts->costintegrandctx); CHKERRQ(ierr); 261005755b9cSHong Zhang PetscStackPop; 261105755b9cSHong Zhang PetscFunctionReturn(0); 261205755b9cSHong Zhang } 261305755b9cSHong Zhang 261405755b9cSHong Zhang #undef __FUNCT__ 2615e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 2616ac226902SBarry Smith /*@C 2617000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 26183f2090d5SJed Brown called once at the beginning of each time step. 2619000e7ae3SMatthew Knepley 26203f9fe445SBarry Smith Logically Collective on TS 2621000e7ae3SMatthew Knepley 2622000e7ae3SMatthew Knepley Input Parameters: 2623000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2624000e7ae3SMatthew Knepley - func - The function 2625000e7ae3SMatthew Knepley 2626000e7ae3SMatthew Knepley Calling sequence of func: 2627000e7ae3SMatthew Knepley . func (TS ts); 2628000e7ae3SMatthew Knepley 2629000e7ae3SMatthew Knepley Level: intermediate 2630000e7ae3SMatthew Knepley 2631b8123daeSJed Brown Note: 2632b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 2633b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 2634b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 2635b8123daeSJed Brown 2636000e7ae3SMatthew Knepley .keywords: TS, timestep 26379be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 2638000e7ae3SMatthew Knepley @*/ 26397087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 2640000e7ae3SMatthew Knepley { 2641000e7ae3SMatthew Knepley PetscFunctionBegin; 26420700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2643ae60f76fSBarry Smith ts->prestep = func; 2644000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2645000e7ae3SMatthew Knepley } 2646000e7ae3SMatthew Knepley 2647e74ef692SMatthew Knepley #undef __FUNCT__ 26483f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 264909ee8438SJed Brown /*@ 26503f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 26513f2090d5SJed Brown 26523f2090d5SJed Brown Collective on TS 26533f2090d5SJed Brown 26543f2090d5SJed Brown Input Parameters: 26553f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 26563f2090d5SJed Brown 26573f2090d5SJed Brown Notes: 26583f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 26593f2090d5SJed Brown so most users would not generally call this routine themselves. 26603f2090d5SJed Brown 26613f2090d5SJed Brown Level: developer 26623f2090d5SJed Brown 26633f2090d5SJed Brown .keywords: TS, timestep 26649be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 26653f2090d5SJed Brown @*/ 26667087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 26673f2090d5SJed Brown { 26683f2090d5SJed Brown PetscErrorCode ierr; 26693f2090d5SJed Brown 26703f2090d5SJed Brown PetscFunctionBegin; 26710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2672ae60f76fSBarry Smith if (ts->prestep) { 2673ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 2674312ce896SJed Brown } 26753f2090d5SJed Brown PetscFunctionReturn(0); 26763f2090d5SJed Brown } 26773f2090d5SJed Brown 26783f2090d5SJed Brown #undef __FUNCT__ 2679b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 2680b8123daeSJed Brown /*@C 2681b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 2682b8123daeSJed Brown called once at the beginning of each stage. 2683b8123daeSJed Brown 2684b8123daeSJed Brown Logically Collective on TS 2685b8123daeSJed Brown 2686b8123daeSJed Brown Input Parameters: 2687b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 2688b8123daeSJed Brown - func - The function 2689b8123daeSJed Brown 2690b8123daeSJed Brown Calling sequence of func: 2691b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 2692b8123daeSJed Brown 2693b8123daeSJed Brown Level: intermediate 2694b8123daeSJed Brown 2695b8123daeSJed Brown Note: 2696b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 2697b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 2698b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 2699b8123daeSJed Brown 2700b8123daeSJed Brown .keywords: TS, timestep 27019be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 2702b8123daeSJed Brown @*/ 2703b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 2704b8123daeSJed Brown { 2705b8123daeSJed Brown PetscFunctionBegin; 2706b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2707ae60f76fSBarry Smith ts->prestage = func; 2708b8123daeSJed Brown PetscFunctionReturn(0); 2709b8123daeSJed Brown } 2710b8123daeSJed Brown 2711b8123daeSJed Brown #undef __FUNCT__ 27129be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage" 27139be3e283SDebojyoti Ghosh /*@C 27149be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 27159be3e283SDebojyoti Ghosh called once at the end of each stage. 27169be3e283SDebojyoti Ghosh 27179be3e283SDebojyoti Ghosh Logically Collective on TS 27189be3e283SDebojyoti Ghosh 27199be3e283SDebojyoti Ghosh Input Parameters: 27209be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 27219be3e283SDebojyoti Ghosh - func - The function 27229be3e283SDebojyoti Ghosh 27239be3e283SDebojyoti Ghosh Calling sequence of func: 27249be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 27259be3e283SDebojyoti Ghosh 27269be3e283SDebojyoti Ghosh Level: intermediate 27279be3e283SDebojyoti Ghosh 27289be3e283SDebojyoti Ghosh Note: 27299be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 27309be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 27319be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 27329be3e283SDebojyoti Ghosh 27339be3e283SDebojyoti Ghosh .keywords: TS, timestep 27349be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 27359be3e283SDebojyoti Ghosh @*/ 27369be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 27379be3e283SDebojyoti Ghosh { 27389be3e283SDebojyoti Ghosh PetscFunctionBegin; 27399be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 27409be3e283SDebojyoti Ghosh ts->poststage = func; 27419be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 27429be3e283SDebojyoti Ghosh } 27439be3e283SDebojyoti Ghosh 27449be3e283SDebojyoti Ghosh #undef __FUNCT__ 2745b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 2746b8123daeSJed Brown /*@ 2747b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 2748b8123daeSJed Brown 2749b8123daeSJed Brown Collective on TS 2750b8123daeSJed Brown 2751b8123daeSJed Brown Input Parameters: 2752b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 27539be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 2754b8123daeSJed Brown 2755b8123daeSJed Brown Notes: 2756b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 2757b8123daeSJed Brown most users would not generally call this routine themselves. 2758b8123daeSJed Brown 2759b8123daeSJed Brown Level: developer 2760b8123daeSJed Brown 2761b8123daeSJed Brown .keywords: TS, timestep 27629be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 2763b8123daeSJed Brown @*/ 2764b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 2765b8123daeSJed Brown { 2766b8123daeSJed Brown PetscErrorCode ierr; 2767b8123daeSJed Brown 2768b8123daeSJed Brown PetscFunctionBegin; 2769b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2770ae60f76fSBarry Smith if (ts->prestage) { 2771ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 2772b8123daeSJed Brown } 2773b8123daeSJed Brown PetscFunctionReturn(0); 2774b8123daeSJed Brown } 2775b8123daeSJed Brown 2776b8123daeSJed Brown #undef __FUNCT__ 27779be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage" 27789be3e283SDebojyoti Ghosh /*@ 27799be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 27809be3e283SDebojyoti Ghosh 27819be3e283SDebojyoti Ghosh Collective on TS 27829be3e283SDebojyoti Ghosh 27839be3e283SDebojyoti Ghosh Input Parameters: 27849be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 27859be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 27869be3e283SDebojyoti Ghosh stageindex - Stage number 27879be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 27889be3e283SDebojyoti Ghosh of stages) with the stage solutions 27899be3e283SDebojyoti Ghosh 27909be3e283SDebojyoti Ghosh Notes: 27919be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 27929be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 27939be3e283SDebojyoti Ghosh 27949be3e283SDebojyoti Ghosh Level: developer 27959be3e283SDebojyoti Ghosh 27969be3e283SDebojyoti Ghosh .keywords: TS, timestep 27979be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 27989be3e283SDebojyoti Ghosh @*/ 27999be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 28009be3e283SDebojyoti Ghosh { 28019be3e283SDebojyoti Ghosh PetscErrorCode ierr; 28029be3e283SDebojyoti Ghosh 28039be3e283SDebojyoti Ghosh PetscFunctionBegin; 28049be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28054beae5d8SLisandro Dalcin if (ts->poststage) { 28069be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 28079be3e283SDebojyoti Ghosh } 28089be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 28099be3e283SDebojyoti Ghosh } 28109be3e283SDebojyoti Ghosh 28119be3e283SDebojyoti Ghosh #undef __FUNCT__ 2812e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 2813ac226902SBarry Smith /*@C 2814000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 28153f2090d5SJed Brown called once at the end of each time step. 2816000e7ae3SMatthew Knepley 28173f9fe445SBarry Smith Logically Collective on TS 2818000e7ae3SMatthew Knepley 2819000e7ae3SMatthew Knepley Input Parameters: 2820000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2821000e7ae3SMatthew Knepley - func - The function 2822000e7ae3SMatthew Knepley 2823000e7ae3SMatthew Knepley Calling sequence of func: 2824b8123daeSJed Brown $ func (TS ts); 2825000e7ae3SMatthew Knepley 2826000e7ae3SMatthew Knepley Level: intermediate 2827000e7ae3SMatthew Knepley 2828000e7ae3SMatthew Knepley .keywords: TS, timestep 2829b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 2830000e7ae3SMatthew Knepley @*/ 28317087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 2832000e7ae3SMatthew Knepley { 2833000e7ae3SMatthew Knepley PetscFunctionBegin; 28340700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2835ae60f76fSBarry Smith ts->poststep = func; 2836000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2837000e7ae3SMatthew Knepley } 2838000e7ae3SMatthew Knepley 2839e74ef692SMatthew Knepley #undef __FUNCT__ 28403f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 284109ee8438SJed Brown /*@ 28423f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 28433f2090d5SJed Brown 28443f2090d5SJed Brown Collective on TS 28453f2090d5SJed Brown 28463f2090d5SJed Brown Input Parameters: 28473f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 28483f2090d5SJed Brown 28493f2090d5SJed Brown Notes: 28503f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 28513f2090d5SJed Brown so most users would not generally call this routine themselves. 28523f2090d5SJed Brown 28533f2090d5SJed Brown Level: developer 28543f2090d5SJed Brown 28553f2090d5SJed Brown .keywords: TS, timestep 28563f2090d5SJed Brown @*/ 28577087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 28583f2090d5SJed Brown { 28593f2090d5SJed Brown PetscErrorCode ierr; 28603f2090d5SJed Brown 28613f2090d5SJed Brown PetscFunctionBegin; 28620700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2863ae60f76fSBarry Smith if (ts->poststep) { 2864ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 286572ac3e02SJed Brown } 28663f2090d5SJed Brown PetscFunctionReturn(0); 28673f2090d5SJed Brown } 28683f2090d5SJed Brown 2869d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 2870d763cef2SBarry Smith 28714a2ae208SSatish Balay #undef __FUNCT__ 2872a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 2873d763cef2SBarry Smith /*@C 2874a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 2875d763cef2SBarry Smith timestep to display the iteration's progress. 2876d763cef2SBarry Smith 28773f9fe445SBarry Smith Logically Collective on TS 2878d763cef2SBarry Smith 2879d763cef2SBarry Smith Input Parameters: 2880d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2881e213d8f1SJed Brown . monitor - monitoring routine 2882329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 28830298fd71SBarry Smith monitor routine (use NULL if no context is desired) 2884b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 28850298fd71SBarry Smith (may be NULL) 2886d763cef2SBarry Smith 2887e213d8f1SJed Brown Calling sequence of monitor: 28880910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 2889d763cef2SBarry Smith 2890d763cef2SBarry Smith + ts - the TS context 289188c05cc5SBarry 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 289288c05cc5SBarry Smith been interpolated to) 28931f06c33eSBarry Smith . time - current time 28940910c330SBarry Smith . u - current iterate 2895d763cef2SBarry Smith - mctx - [optional] monitoring context 2896d763cef2SBarry Smith 2897d763cef2SBarry Smith Notes: 2898d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 2899d763cef2SBarry Smith already has been loaded. 2900d763cef2SBarry Smith 2901025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 2902025f1a04SBarry Smith 2903d763cef2SBarry Smith Level: intermediate 2904d763cef2SBarry Smith 2905d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2906d763cef2SBarry Smith 2907a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 2908d763cef2SBarry Smith @*/ 2909c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 2910d763cef2SBarry Smith { 2911d763cef2SBarry Smith PetscFunctionBegin; 29120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 291317186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2914d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 29158704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 2916d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 2917d763cef2SBarry Smith PetscFunctionReturn(0); 2918d763cef2SBarry Smith } 2919d763cef2SBarry Smith 29204a2ae208SSatish Balay #undef __FUNCT__ 2921a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 2922d763cef2SBarry Smith /*@C 2923a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 2924d763cef2SBarry Smith 29253f9fe445SBarry Smith Logically Collective on TS 2926d763cef2SBarry Smith 2927d763cef2SBarry Smith Input Parameters: 2928d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2929d763cef2SBarry Smith 2930d763cef2SBarry Smith Notes: 2931d763cef2SBarry Smith There is no way to remove a single, specific monitor. 2932d763cef2SBarry Smith 2933d763cef2SBarry Smith Level: intermediate 2934d763cef2SBarry Smith 2935d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2936d763cef2SBarry Smith 2937a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 2938d763cef2SBarry Smith @*/ 29397087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 2940d763cef2SBarry Smith { 2941d952e501SBarry Smith PetscErrorCode ierr; 2942d952e501SBarry Smith PetscInt i; 2943d952e501SBarry Smith 2944d763cef2SBarry Smith PetscFunctionBegin; 29450700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2946d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 29478704b422SBarry Smith if (ts->monitordestroy[i]) { 29488704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 2949d952e501SBarry Smith } 2950d952e501SBarry Smith } 2951d763cef2SBarry Smith ts->numbermonitors = 0; 2952d763cef2SBarry Smith PetscFunctionReturn(0); 2953d763cef2SBarry Smith } 2954d763cef2SBarry Smith 29554a2ae208SSatish Balay #undef __FUNCT__ 2956a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 2957d8e5e3e6SSatish Balay /*@ 2958a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 29595516499fSSatish Balay 29605516499fSSatish Balay Level: intermediate 296141251cbbSSatish Balay 29625516499fSSatish Balay .keywords: TS, set, monitor 29635516499fSSatish Balay 296441251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 296541251cbbSSatish Balay @*/ 2966649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 2967d763cef2SBarry Smith { 2968dfbe8321SBarry Smith PetscErrorCode ierr; 2969ce94432eSBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts)); 2970d132466eSBarry Smith 2971d763cef2SBarry Smith PetscFunctionBegin; 2972649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2973971832b6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr); 2974649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2975d763cef2SBarry Smith PetscFunctionReturn(0); 2976d763cef2SBarry Smith } 2977d763cef2SBarry Smith 29784a2ae208SSatish Balay #undef __FUNCT__ 2979cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 2980cd652676SJed Brown /*@ 2981cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 2982cd652676SJed Brown 2983cd652676SJed Brown Logically Collective on TS 2984cd652676SJed Brown 2985cd652676SJed Brown Input Argument: 2986cd652676SJed Brown . ts - time stepping context 2987cd652676SJed Brown 2988cd652676SJed Brown Output Argument: 2989cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 2990cd652676SJed Brown 2991cd652676SJed Brown Level: intermediate 2992cd652676SJed Brown 2993cd652676SJed Brown .keywords: TS, set 2994cd652676SJed Brown 2995cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 2996cd652676SJed Brown @*/ 2997cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 2998cd652676SJed Brown { 2999cd652676SJed Brown PetscFunctionBegin; 3000cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3001cd652676SJed Brown ts->retain_stages = flg; 3002cd652676SJed Brown PetscFunctionReturn(0); 3003cd652676SJed Brown } 3004cd652676SJed Brown 3005cd652676SJed Brown #undef __FUNCT__ 3006cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 3007cd652676SJed Brown /*@ 3008cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3009cd652676SJed Brown 3010cd652676SJed Brown Collective on TS 3011cd652676SJed Brown 3012cd652676SJed Brown Input Argument: 3013cd652676SJed Brown + ts - time stepping context 3014cd652676SJed Brown - t - time to interpolate to 3015cd652676SJed Brown 3016cd652676SJed Brown Output Argument: 30170910c330SBarry Smith . U - state at given time 3018cd652676SJed Brown 3019cd652676SJed Brown Notes: 3020cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 3021cd652676SJed Brown 3022cd652676SJed Brown Level: intermediate 3023cd652676SJed Brown 3024cd652676SJed Brown Developer Notes: 3025cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3026cd652676SJed Brown 3027cd652676SJed Brown .keywords: TS, set 3028cd652676SJed Brown 3029cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 3030cd652676SJed Brown @*/ 30310910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3032cd652676SJed Brown { 3033cd652676SJed Brown PetscErrorCode ierr; 3034cd652676SJed Brown 3035cd652676SJed Brown PetscFunctionBegin; 3036cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3037b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 30386712e2f1SBarry 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); 3039ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 30400910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3041cd652676SJed Brown PetscFunctionReturn(0); 3042cd652676SJed Brown } 3043cd652676SJed Brown 3044cd652676SJed Brown #undef __FUNCT__ 30454a2ae208SSatish Balay #define __FUNCT__ "TSStep" 3046d763cef2SBarry Smith /*@ 30476d9e5789SSean Farley TSStep - Steps one time step 3048d763cef2SBarry Smith 3049d763cef2SBarry Smith Collective on TS 3050d763cef2SBarry Smith 3051d763cef2SBarry Smith Input Parameter: 3052d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3053d763cef2SBarry Smith 305427829d71SBarry Smith Level: developer 3055d763cef2SBarry Smith 3056b8123daeSJed Brown Notes: 305727829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 305827829d71SBarry Smith 3059b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3060b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3061b8123daeSJed Brown 306225cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 306325cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 306425cb2221SBarry Smith 3065d763cef2SBarry Smith .keywords: TS, timestep, solve 3066d763cef2SBarry Smith 30679be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3068d763cef2SBarry Smith @*/ 3069193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3070d763cef2SBarry Smith { 30712fb8446cSMatthew G. Knepley DM dm; 3072dfbe8321SBarry Smith PetscErrorCode ierr; 3073fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3074d763cef2SBarry Smith 3075d763cef2SBarry Smith PetscFunctionBegin; 30760700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3077fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3078fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3079fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3080fffbeea8SBarry Smith " type = {Preprint},\n" 3081fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3082fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3083fffbeea8SBarry Smith " year = {2014}\n}\n",&cite); 3084fffbeea8SBarry Smith 30852fb8446cSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3086d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 3087d405a339SMatthew Knepley 3088362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 308924655328SShri ts->ptime_prev = ts->ptime; 3090cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3091bbd56ea5SKarl Rupp 3092e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3093d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3094193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3095d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3096bbd56ea5SKarl Rupp 309724655328SShri ts->time_step_prev = ts->ptime - ts->ptime_prev; 3098cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3099362cd11cSLisandro Dalcin 3100362cd11cSLisandro Dalcin if (ts->reason < 0) { 3101cef5090cSJed Brown if (ts->errorifstepfailed) { 310208c7845fSBarry 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]); 310308c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3104cef5090cSJed Brown } 3105362cd11cSLisandro Dalcin } else if (!ts->reason) { 3106db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3107db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3108362cd11cSLisandro Dalcin } 31092c18e0fdSBarry Smith ts->total_steps++; 311008c7845fSBarry Smith PetscFunctionReturn(0); 311108c7845fSBarry Smith } 311208c7845fSBarry Smith 311308c7845fSBarry Smith #undef __FUNCT__ 311408c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep" 311508c7845fSBarry Smith /*@ 311608c7845fSBarry Smith TSAdjointStep - Steps one time step 311708c7845fSBarry Smith 311808c7845fSBarry Smith Collective on TS 311908c7845fSBarry Smith 312008c7845fSBarry Smith Input Parameter: 312108c7845fSBarry Smith . ts - the TS context obtained from TSCreate() 312208c7845fSBarry Smith 312308c7845fSBarry Smith Level: intermediate 312408c7845fSBarry Smith 312508c7845fSBarry Smith Notes: 312608c7845fSBarry Smith The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 312708c7845fSBarry Smith be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 312808c7845fSBarry Smith 312908c7845fSBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 313008c7845fSBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 313108c7845fSBarry Smith 313208c7845fSBarry Smith .keywords: TS, timestep, solve 313308c7845fSBarry Smith 313408c7845fSBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 313508c7845fSBarry Smith @*/ 313608c7845fSBarry Smith PetscErrorCode TSAdjointStep(TS ts) 313708c7845fSBarry Smith { 313808c7845fSBarry Smith DM dm; 313908c7845fSBarry Smith PetscErrorCode ierr; 314008c7845fSBarry Smith 314108c7845fSBarry Smith PetscFunctionBegin; 314208c7845fSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 314308c7845fSBarry Smith ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 314408c7845fSBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 314508c7845fSBarry Smith 314608c7845fSBarry Smith ts->reason = TS_CONVERGED_ITERATING; 314708c7845fSBarry Smith ts->ptime_prev = ts->ptime; 314808c7845fSBarry Smith ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 314908c7845fSBarry Smith ierr = VecViewFromOptions(ts->vec_sol, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr); 315008c7845fSBarry Smith 315108c7845fSBarry Smith ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 315242f2b339SBarry 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); 315342f2b339SBarry Smith ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr); 3154d405a339SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3155362cd11cSLisandro Dalcin 3156362cd11cSLisandro Dalcin ts->time_step_prev = ts->ptime - ts->ptime_prev; 3157362cd11cSLisandro Dalcin ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3158362cd11cSLisandro Dalcin 3159362cd11cSLisandro Dalcin if (ts->reason < 0) { 3160362cd11cSLisandro Dalcin if (ts->errorifstepfailed) { 3161362cd11cSLisandro Dalcin if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) { 3162362cd11cSLisandro Dalcin 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]); 31632a3a10ceSLisandro Dalcin } else if (ts->reason == TS_DIVERGED_STEP_REJECTED) { 31642a3a10ceSLisandro Dalcin SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_reject or make negative to attempt recovery",TSConvergedReasons[ts->reason]); 3165362cd11cSLisandro Dalcin } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3166362cd11cSLisandro Dalcin } 3167362cd11cSLisandro Dalcin } else if (!ts->reason) { 316873b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3169362cd11cSLisandro Dalcin else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3170362cd11cSLisandro Dalcin } 31712c18e0fdSBarry Smith ts->total_steps--; 3172d763cef2SBarry Smith PetscFunctionReturn(0); 3173d763cef2SBarry Smith } 3174d763cef2SBarry Smith 31754a2ae208SSatish Balay #undef __FUNCT__ 317605175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 317705175c85SJed Brown /*@ 317805175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 317905175c85SJed Brown 31801c3436cfSJed Brown Collective on TS 318105175c85SJed Brown 318205175c85SJed Brown Input Arguments: 31831c3436cfSJed Brown + ts - time stepping context 31841c3436cfSJed Brown . order - desired order of accuracy 31850298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 318605175c85SJed Brown 318705175c85SJed Brown Output Arguments: 31880910c330SBarry Smith . U - state at the end of the current step 318905175c85SJed Brown 319005175c85SJed Brown Level: advanced 319105175c85SJed Brown 3192108c343cSJed Brown Notes: 3193108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3194108c343cSJed 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. 3195108c343cSJed Brown 31961c3436cfSJed Brown .seealso: TSStep(), TSAdapt 319705175c85SJed Brown @*/ 31980910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 319905175c85SJed Brown { 320005175c85SJed Brown PetscErrorCode ierr; 320105175c85SJed Brown 320205175c85SJed Brown PetscFunctionBegin; 320305175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 320405175c85SJed Brown PetscValidType(ts,1); 32050910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3206ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 32070910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 320805175c85SJed Brown PetscFunctionReturn(0); 320905175c85SJed Brown } 321005175c85SJed Brown 3211d67e68b7SBarry Smith 321205175c85SJed Brown #undef __FUNCT__ 32136a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 32146a4d4014SLisandro Dalcin /*@ 32156a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 32166a4d4014SLisandro Dalcin 32176a4d4014SLisandro Dalcin Collective on TS 32186a4d4014SLisandro Dalcin 32196a4d4014SLisandro Dalcin Input Parameter: 32206a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 3221cc708dedSBarry Smith - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 32225a3a76d0SJed Brown 32236a4d4014SLisandro Dalcin Level: beginner 32246a4d4014SLisandro Dalcin 32255a3a76d0SJed Brown Notes: 32265a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 32275a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 32285a3a76d0SJed Brown stepped over the final time. 32295a3a76d0SJed Brown 32306a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 32316a4d4014SLisandro Dalcin 32326a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 32336a4d4014SLisandro Dalcin @*/ 3234cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 32356a4d4014SLisandro Dalcin { 3236b06615a5SLisandro Dalcin Vec solution; 32376a4d4014SLisandro Dalcin PetscErrorCode ierr; 3238f22f69f0SBarry Smith 32396a4d4014SLisandro Dalcin PetscFunctionBegin; 32400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3241f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 324249354f04SShri 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 */ 3243b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 32440910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 3245b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3246b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3247b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 32485a3a76d0SJed Brown } 32490910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3250bbd56ea5SKarl Rupp } else if (u) { 32510910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 32525a3a76d0SJed Brown } 3253d2daff3dSHong Zhang ierr = TSSetUp(ts);CHKERRQ(ierr); /*compute adj coefficients if the reverse mode is on*/ 32546a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 3255193ac0bcSJed Brown ts->steps = 0; 32565ef26d82SJed Brown ts->ksp_its = 0; 32575ef26d82SJed Brown ts->snes_its = 0; 3258c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3259c610991cSLisandro Dalcin ts->reject = 0; 3260193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3261193ac0bcSJed Brown 3262ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3263f05ece33SBarry Smith 3264193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3265193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 32660910c330SBarry Smith ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 3267cc708dedSBarry Smith ts->solvetime = ts->ptime; 3268193ac0bcSJed Brown } else { 32696a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 3270db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3271db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3272e1a7a14fSJed Brown while (!ts->reason) { 3273b06615a5SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 3274bc952696SBarry Smith ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 3275193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 3276aeb4809dSShri Abhyankar if (ts->event) { 3277aeb4809dSShri Abhyankar ierr = TSEventMonitor(ts);CHKERRQ(ierr); 32781eda64f1SShri Abhyankar if (ts->event->status != TSEVENT_PROCESSING) { 32791eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 32801eda64f1SShri Abhyankar } 32811eda64f1SShri Abhyankar } else { 32821eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 3283aeb4809dSShri Abhyankar } 3284193ac0bcSJed Brown } 328549354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 32860910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 3287cc708dedSBarry Smith ts->solvetime = ts->max_time; 3288b06615a5SLisandro Dalcin solution = u; 32890574a7fbSJed Brown } else { 3290ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3291cc708dedSBarry Smith ts->solvetime = ts->ptime; 3292b06615a5SLisandro Dalcin solution = ts->vec_sol; 32930574a7fbSJed Brown } 3294bc952696SBarry Smith ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr); 3295b06615a5SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr); 329627829d71SBarry Smith ierr = VecViewFromOptions(solution, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr); 3297193ac0bcSJed Brown } 3298d2daff3dSHong Zhang 3299ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 330056f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 33016a4d4014SLisandro Dalcin PetscFunctionReturn(0); 33026a4d4014SLisandro Dalcin } 33036a4d4014SLisandro Dalcin 33046a4d4014SLisandro Dalcin #undef __FUNCT__ 3305c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve" 3306c88f2d44SBarry Smith /*@ 3307c88f2d44SBarry Smith TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 3308c88f2d44SBarry Smith 3309c88f2d44SBarry Smith Collective on TS 3310c88f2d44SBarry Smith 3311c88f2d44SBarry Smith Input Parameter: 33122c39e106SBarry Smith . ts - the TS context obtained from TSCreate() 3313c88f2d44SBarry Smith 3314c88f2d44SBarry Smith Level: intermediate 3315c88f2d44SBarry Smith 3316c88f2d44SBarry Smith Notes: 3317c88f2d44SBarry Smith This must be called after a call to TSSolve() that solves the forward problem 3318c88f2d44SBarry Smith 331973b18844SBarry Smith By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time 332073b18844SBarry Smith 3321c88f2d44SBarry Smith .keywords: TS, timestep, solve 3322c88f2d44SBarry Smith 3323c88f2d44SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep() 3324c88f2d44SBarry Smith @*/ 33252c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts) 3326c88f2d44SBarry Smith { 3327c88f2d44SBarry Smith PetscErrorCode ierr; 3328c88f2d44SBarry Smith 3329c88f2d44SBarry Smith PetscFunctionBegin; 3330c88f2d44SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3331c88f2d44SBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3332c88f2d44SBarry Smith /* reset time step and iteration counters */ 3333c88f2d44SBarry Smith ts->steps = 0; 3334c88f2d44SBarry Smith ts->ksp_its = 0; 3335c88f2d44SBarry Smith ts->snes_its = 0; 3336c88f2d44SBarry Smith ts->num_snes_failures = 0; 3337c88f2d44SBarry Smith ts->reject = 0; 3338c88f2d44SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 3339c88f2d44SBarry Smith 334073b18844SBarry Smith if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps; 3341c88f2d44SBarry Smith 334273b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3343c88f2d44SBarry Smith while (!ts->reason) { 334473b18844SBarry Smith ierr = TSTrajectoryGet(ts->trajectory,ts,ts->adjoint_max_steps-ts->steps,ts->ptime);CHKERRQ(ierr); 334573b18844SBarry Smith ierr = TSMonitor(ts,ts->adjoint_max_steps-ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 334608c7845fSBarry Smith ierr = TSAdjointStep(ts);CHKERRQ(ierr); 3347c88f2d44SBarry Smith if (ts->event) { 3348c88f2d44SBarry Smith ierr = TSEventMonitor(ts);CHKERRQ(ierr); 3349c88f2d44SBarry Smith if (ts->event->status != TSEVENT_PROCESSING) { 3350c88f2d44SBarry Smith ierr = TSPostStep(ts);CHKERRQ(ierr); 3351c88f2d44SBarry Smith } 3352c88f2d44SBarry Smith } else { 3353c88f2d44SBarry Smith ierr = TSPostStep(ts);CHKERRQ(ierr); 3354c88f2d44SBarry Smith } 3355c88f2d44SBarry Smith } 3356c88f2d44SBarry Smith ts->solvetime = ts->ptime; 3357c88f2d44SBarry Smith PetscFunctionReturn(0); 3358c88f2d44SBarry Smith } 3359c88f2d44SBarry Smith 3360c88f2d44SBarry Smith #undef __FUNCT__ 33614a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 3362228d79bcSJed Brown /*@ 3363228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 3364228d79bcSJed Brown 3365228d79bcSJed Brown Collective on TS 3366228d79bcSJed Brown 3367228d79bcSJed Brown Input Parameters: 3368228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 3369228d79bcSJed Brown . step - step number that has just completed 3370228d79bcSJed Brown . ptime - model time of the state 33710910c330SBarry Smith - u - state at the current model time 3372228d79bcSJed Brown 3373228d79bcSJed Brown Notes: 3374228d79bcSJed Brown TSMonitor() is typically used within the time stepping implementations. 3375228d79bcSJed Brown Users might call this function when using the TSStep() interface instead of TSSolve(). 3376228d79bcSJed Brown 3377228d79bcSJed Brown Level: advanced 3378228d79bcSJed Brown 3379228d79bcSJed Brown .keywords: TS, timestep 3380228d79bcSJed Brown @*/ 33810910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 3382d763cef2SBarry Smith { 33836849ba73SBarry Smith PetscErrorCode ierr; 3384a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 3385d763cef2SBarry Smith 3386d763cef2SBarry Smith PetscFunctionBegin; 3387b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3388b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 33895edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 3390d763cef2SBarry Smith for (i=0; i<n; i++) { 33910910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 3392d763cef2SBarry Smith } 33935edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 3394d763cef2SBarry Smith PetscFunctionReturn(0); 3395d763cef2SBarry Smith } 3396d763cef2SBarry Smith 3397d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 33984a2ae208SSatish Balay #undef __FUNCT__ 3399a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 3400d763cef2SBarry Smith /*@C 3401a9f9c1f6SBarry Smith TSMonitorLGCtxCreate - Creates a line graph context for use with 3402a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 3403d763cef2SBarry Smith 3404d763cef2SBarry Smith Collective on TS 3405d763cef2SBarry Smith 3406d763cef2SBarry Smith Input Parameters: 3407d763cef2SBarry Smith + host - the X display to open, or null for the local machine 3408d763cef2SBarry Smith . label - the title to put in the title bar 34097c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 3410a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 3411a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 3412d763cef2SBarry Smith 3413d763cef2SBarry Smith Output Parameter: 34140b039ecaSBarry Smith . ctx - the context 3415d763cef2SBarry Smith 3416d763cef2SBarry Smith Options Database Key: 34174f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 34184f09c107SBarry Smith . -ts_monitor_lg_solution - 341999fdda47SBarry Smith . -ts_monitor_lg_error - 342099fdda47SBarry Smith . -ts_monitor_lg_ksp_iterations - 342199fdda47SBarry Smith . -ts_monitor_lg_snes_iterations - 342299fdda47SBarry Smith - -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true 3423d763cef2SBarry Smith 3424d763cef2SBarry Smith Notes: 3425a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 3426d763cef2SBarry Smith 3427d763cef2SBarry Smith Level: intermediate 3428d763cef2SBarry Smith 34297c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 3430d763cef2SBarry Smith 34314f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 34327c922b88SBarry Smith 3433d763cef2SBarry Smith @*/ 3434a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 3435d763cef2SBarry Smith { 3436b0a32e0cSBarry Smith PetscDraw win; 3437dfbe8321SBarry Smith PetscErrorCode ierr; 3438d763cef2SBarry Smith 3439d763cef2SBarry Smith PetscFunctionBegin; 3440b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 3441a80ad3e0SBarry Smith ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr); 34423fbbecb0SBarry Smith ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr); 34430b039ecaSBarry Smith ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr); 34443bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr); 3445287de1a7SBarry Smith ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr); 3446287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 3447a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 3448d763cef2SBarry Smith PetscFunctionReturn(0); 3449d763cef2SBarry Smith } 3450d763cef2SBarry Smith 34514a2ae208SSatish Balay #undef __FUNCT__ 34524f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 3453b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 3454d763cef2SBarry Smith { 34550b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 3456c32365f1SBarry Smith PetscReal x = ptime,y; 3457dfbe8321SBarry Smith PetscErrorCode ierr; 3458d763cef2SBarry Smith 3459d763cef2SBarry Smith PetscFunctionBegin; 3460b06615a5SLisandro Dalcin if (!step) { 3461a9f9c1f6SBarry Smith PetscDrawAxis axis; 3462a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 3463a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 3464a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 3465287de1a7SBarry Smith ierr = PetscDrawLGIndicateDataPoints(ctx->lg,PETSC_TRUE);CHKERRQ(ierr); 3466a9f9c1f6SBarry Smith } 3467c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 34680b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 3469b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 34700b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 34713923b477SBarry Smith } 3472d763cef2SBarry Smith PetscFunctionReturn(0); 3473d763cef2SBarry Smith } 3474d763cef2SBarry Smith 34754a2ae208SSatish Balay #undef __FUNCT__ 3476a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 3477d763cef2SBarry Smith /*@C 3478a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 3479a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 3480d763cef2SBarry Smith 34810b039ecaSBarry Smith Collective on TSMonitorLGCtx 3482d763cef2SBarry Smith 3483d763cef2SBarry Smith Input Parameter: 34840b039ecaSBarry Smith . ctx - the monitor context 3485d763cef2SBarry Smith 3486d763cef2SBarry Smith Level: intermediate 3487d763cef2SBarry Smith 3488d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 3489d763cef2SBarry Smith 34904f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 3491d763cef2SBarry Smith @*/ 3492a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 3493d763cef2SBarry Smith { 3494b0a32e0cSBarry Smith PetscDraw draw; 3495dfbe8321SBarry Smith PetscErrorCode ierr; 3496d763cef2SBarry Smith 3497d763cef2SBarry Smith PetscFunctionBegin; 34980b039ecaSBarry Smith ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr); 34996bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 35000b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 35010b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 3502d763cef2SBarry Smith PetscFunctionReturn(0); 3503d763cef2SBarry Smith } 3504d763cef2SBarry Smith 35054a2ae208SSatish Balay #undef __FUNCT__ 35064a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 3507d763cef2SBarry Smith /*@ 3508b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 3509d763cef2SBarry Smith 3510d763cef2SBarry Smith Not Collective 3511d763cef2SBarry Smith 3512d763cef2SBarry Smith Input Parameter: 3513d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3514d763cef2SBarry Smith 3515d763cef2SBarry Smith Output Parameter: 3516d763cef2SBarry Smith . t - the current time 3517d763cef2SBarry Smith 3518d763cef2SBarry Smith Level: beginner 3519d763cef2SBarry Smith 3520b8123daeSJed Brown Note: 3521b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 35229be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 3523b8123daeSJed Brown 3524d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3525d763cef2SBarry Smith 3526d763cef2SBarry Smith .keywords: TS, get, time 3527d763cef2SBarry Smith @*/ 35287087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 3529d763cef2SBarry Smith { 3530d763cef2SBarry Smith PetscFunctionBegin; 35310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3532f7cf8827SBarry Smith PetscValidRealPointer(t,2); 3533d763cef2SBarry Smith *t = ts->ptime; 3534d763cef2SBarry Smith PetscFunctionReturn(0); 3535d763cef2SBarry Smith } 3536d763cef2SBarry Smith 35374a2ae208SSatish Balay #undef __FUNCT__ 3538e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime" 3539e5e524a1SHong Zhang /*@ 3540e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 3541e5e524a1SHong Zhang 3542e5e524a1SHong Zhang Not Collective 3543e5e524a1SHong Zhang 3544e5e524a1SHong Zhang Input Parameter: 3545e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 3546e5e524a1SHong Zhang 3547e5e524a1SHong Zhang Output Parameter: 3548e5e524a1SHong Zhang . t - the previous time 3549e5e524a1SHong Zhang 3550e5e524a1SHong Zhang Level: beginner 3551e5e524a1SHong Zhang 3552e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3553e5e524a1SHong Zhang 3554e5e524a1SHong Zhang .keywords: TS, get, time 3555e5e524a1SHong Zhang @*/ 3556e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 3557e5e524a1SHong Zhang { 3558e5e524a1SHong Zhang PetscFunctionBegin; 3559e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3560e5e524a1SHong Zhang PetscValidRealPointer(t,2); 3561e5e524a1SHong Zhang *t = ts->ptime_prev; 3562e5e524a1SHong Zhang PetscFunctionReturn(0); 3563e5e524a1SHong Zhang } 3564e5e524a1SHong Zhang 3565e5e524a1SHong Zhang #undef __FUNCT__ 35666a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 35676a4d4014SLisandro Dalcin /*@ 35686a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 35696a4d4014SLisandro Dalcin 35703f9fe445SBarry Smith Logically Collective on TS 35716a4d4014SLisandro Dalcin 35726a4d4014SLisandro Dalcin Input Parameters: 35736a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 35746a4d4014SLisandro Dalcin - time - the time 35756a4d4014SLisandro Dalcin 35766a4d4014SLisandro Dalcin Level: intermediate 35776a4d4014SLisandro Dalcin 35786a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 35796a4d4014SLisandro Dalcin 35806a4d4014SLisandro Dalcin .keywords: TS, set, time 35816a4d4014SLisandro Dalcin @*/ 35827087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 35836a4d4014SLisandro Dalcin { 35846a4d4014SLisandro Dalcin PetscFunctionBegin; 35850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3586c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 35876a4d4014SLisandro Dalcin ts->ptime = t; 35886a4d4014SLisandro Dalcin PetscFunctionReturn(0); 35896a4d4014SLisandro Dalcin } 35906a4d4014SLisandro Dalcin 35916a4d4014SLisandro Dalcin #undef __FUNCT__ 35924a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 3593d763cef2SBarry Smith /*@C 3594d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 3595d763cef2SBarry Smith TS options in the database. 3596d763cef2SBarry Smith 35973f9fe445SBarry Smith Logically Collective on TS 3598d763cef2SBarry Smith 3599d763cef2SBarry Smith Input Parameter: 3600d763cef2SBarry Smith + ts - The TS context 3601d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3602d763cef2SBarry Smith 3603d763cef2SBarry Smith Notes: 3604d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3605d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3606d763cef2SBarry Smith hyphen. 3607d763cef2SBarry Smith 3608d763cef2SBarry Smith Level: advanced 3609d763cef2SBarry Smith 3610d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 3611d763cef2SBarry Smith 3612d763cef2SBarry Smith .seealso: TSSetFromOptions() 3613d763cef2SBarry Smith 3614d763cef2SBarry Smith @*/ 36157087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 3616d763cef2SBarry Smith { 3617dfbe8321SBarry Smith PetscErrorCode ierr; 3618089b2837SJed Brown SNES snes; 3619d763cef2SBarry Smith 3620d763cef2SBarry Smith PetscFunctionBegin; 36210700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3622d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3623089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3624089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3625d763cef2SBarry Smith PetscFunctionReturn(0); 3626d763cef2SBarry Smith } 3627d763cef2SBarry Smith 3628d763cef2SBarry Smith 36294a2ae208SSatish Balay #undef __FUNCT__ 36304a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 3631d763cef2SBarry Smith /*@C 3632d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 3633d763cef2SBarry Smith TS options in the database. 3634d763cef2SBarry Smith 36353f9fe445SBarry Smith Logically Collective on TS 3636d763cef2SBarry Smith 3637d763cef2SBarry Smith Input Parameter: 3638d763cef2SBarry Smith + ts - The TS context 3639d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3640d763cef2SBarry Smith 3641d763cef2SBarry Smith Notes: 3642d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3643d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3644d763cef2SBarry Smith hyphen. 3645d763cef2SBarry Smith 3646d763cef2SBarry Smith Level: advanced 3647d763cef2SBarry Smith 3648d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 3649d763cef2SBarry Smith 3650d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 3651d763cef2SBarry Smith 3652d763cef2SBarry Smith @*/ 36537087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 3654d763cef2SBarry Smith { 3655dfbe8321SBarry Smith PetscErrorCode ierr; 3656089b2837SJed Brown SNES snes; 3657d763cef2SBarry Smith 3658d763cef2SBarry Smith PetscFunctionBegin; 36590700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3660d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3661089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3662089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3663d763cef2SBarry Smith PetscFunctionReturn(0); 3664d763cef2SBarry Smith } 3665d763cef2SBarry Smith 36664a2ae208SSatish Balay #undef __FUNCT__ 36674a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 3668d763cef2SBarry Smith /*@C 3669d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 3670d763cef2SBarry Smith TS options in the database. 3671d763cef2SBarry Smith 3672d763cef2SBarry Smith Not Collective 3673d763cef2SBarry Smith 3674d763cef2SBarry Smith Input Parameter: 3675d763cef2SBarry Smith . ts - The TS context 3676d763cef2SBarry Smith 3677d763cef2SBarry Smith Output Parameter: 3678d763cef2SBarry Smith . prefix - A pointer to the prefix string used 3679d763cef2SBarry Smith 3680d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 3681d763cef2SBarry Smith sufficient length to hold the prefix. 3682d763cef2SBarry Smith 3683d763cef2SBarry Smith Level: intermediate 3684d763cef2SBarry Smith 3685d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 3686d763cef2SBarry Smith 3687d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 3688d763cef2SBarry Smith @*/ 36897087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 3690d763cef2SBarry Smith { 3691dfbe8321SBarry Smith PetscErrorCode ierr; 3692d763cef2SBarry Smith 3693d763cef2SBarry Smith PetscFunctionBegin; 36940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36954482741eSBarry Smith PetscValidPointer(prefix,2); 3696d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3697d763cef2SBarry Smith PetscFunctionReturn(0); 3698d763cef2SBarry Smith } 3699d763cef2SBarry Smith 37004a2ae208SSatish Balay #undef __FUNCT__ 37014a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 3702d763cef2SBarry Smith /*@C 3703d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 3704d763cef2SBarry Smith 3705d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 3706d763cef2SBarry Smith 3707d763cef2SBarry Smith Input Parameter: 3708d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 3709d763cef2SBarry Smith 3710d763cef2SBarry Smith Output Parameters: 3711e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 3712e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 3713e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 3714e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 3715d763cef2SBarry Smith 37160298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 3717d763cef2SBarry Smith 3718d763cef2SBarry Smith Level: intermediate 3719d763cef2SBarry Smith 372026d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 3721d763cef2SBarry Smith 3722d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 3723d763cef2SBarry Smith @*/ 3724e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 3725d763cef2SBarry Smith { 3726089b2837SJed Brown PetscErrorCode ierr; 3727089b2837SJed Brown SNES snes; 372824989b8cSPeter Brune DM dm; 3729089b2837SJed Brown 3730d763cef2SBarry Smith PetscFunctionBegin; 3731089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3732e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 373324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 373424989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 3735d763cef2SBarry Smith PetscFunctionReturn(0); 3736d763cef2SBarry Smith } 3737d763cef2SBarry Smith 37381713a123SBarry Smith #undef __FUNCT__ 37392eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 37402eca1d9cSJed Brown /*@C 37412eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 37422eca1d9cSJed Brown 37432eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 37442eca1d9cSJed Brown 37452eca1d9cSJed Brown Input Parameter: 37462eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 37472eca1d9cSJed Brown 37482eca1d9cSJed Brown Output Parameters: 3749e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 3750e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 37512eca1d9cSJed Brown . f - The function to compute the matrices 37522eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 37532eca1d9cSJed Brown 37540298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 37552eca1d9cSJed Brown 37562eca1d9cSJed Brown Level: advanced 37572eca1d9cSJed Brown 37582eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 37592eca1d9cSJed Brown 37602eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 37612eca1d9cSJed Brown @*/ 3762e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 37632eca1d9cSJed Brown { 3764089b2837SJed Brown PetscErrorCode ierr; 3765089b2837SJed Brown SNES snes; 376624989b8cSPeter Brune DM dm; 37670910c330SBarry Smith 37682eca1d9cSJed Brown PetscFunctionBegin; 3769089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3770f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 3771e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 377224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 377324989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 37742eca1d9cSJed Brown PetscFunctionReturn(0); 37752eca1d9cSJed Brown } 37762eca1d9cSJed Brown 37776083293cSBarry Smith 37782eca1d9cSJed Brown #undef __FUNCT__ 377983a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 37801713a123SBarry Smith /*@C 378183a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 37821713a123SBarry Smith VecView() for the solution at each timestep 37831713a123SBarry Smith 37841713a123SBarry Smith Collective on TS 37851713a123SBarry Smith 37861713a123SBarry Smith Input Parameters: 37871713a123SBarry Smith + ts - the TS context 37881713a123SBarry Smith . step - current time-step 3789142b95e3SSatish Balay . ptime - current time 37900298fd71SBarry Smith - dummy - either a viewer or NULL 37911713a123SBarry Smith 379299fdda47SBarry Smith Options Database: 379399fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 379499fdda47SBarry Smith 379599fdda47SBarry Smith Notes: the initial solution and current solution are not displayed with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 379699fdda47SBarry Smith will look bad 379799fdda47SBarry Smith 37981713a123SBarry Smith Level: intermediate 37991713a123SBarry Smith 38001713a123SBarry Smith .keywords: TS, vector, monitor, view 38011713a123SBarry Smith 3802a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 38031713a123SBarry Smith @*/ 38040910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 38051713a123SBarry Smith { 3806dfbe8321SBarry Smith PetscErrorCode ierr; 380783a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 3808473a3ab2SBarry Smith PetscDraw draw; 38091713a123SBarry Smith 38101713a123SBarry Smith PetscFunctionBegin; 38116083293cSBarry Smith if (!step && ictx->showinitial) { 38126083293cSBarry Smith if (!ictx->initialsolution) { 38130910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 38141713a123SBarry Smith } 38150910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 38166083293cSBarry Smith } 3817b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 38180dcf80beSBarry Smith 38196083293cSBarry Smith if (ictx->showinitial) { 38206083293cSBarry Smith PetscReal pause; 38216083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 38226083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 38236083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 38246083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 38256083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 38266083293cSBarry Smith } 38270910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 3828473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 3829473a3ab2SBarry Smith PetscReal xl,yl,xr,yr,tw,w,h; 3830473a3ab2SBarry Smith char time[32]; 3831473a3ab2SBarry Smith size_t len; 3832473a3ab2SBarry Smith 3833473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 3834473a3ab2SBarry Smith ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr); 3835473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 3836473a3ab2SBarry Smith ierr = PetscStrlen(time,&len);CHKERRQ(ierr); 38370298fd71SBarry Smith ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr); 3838473a3ab2SBarry Smith w = xl + .5*(xr - xl) - .5*len*tw; 3839473a3ab2SBarry Smith h = yl + .95*(yr - yl); 3840473a3ab2SBarry Smith ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 3841473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 3842473a3ab2SBarry Smith } 3843473a3ab2SBarry Smith 38446083293cSBarry Smith if (ictx->showinitial) { 38456083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 38466083293cSBarry Smith } 38471713a123SBarry Smith PetscFunctionReturn(0); 38481713a123SBarry Smith } 38491713a123SBarry Smith 38502d5ee99bSBarry Smith #undef __FUNCT__ 38512d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase" 38522d5ee99bSBarry Smith /*@C 38532d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 38542d5ee99bSBarry Smith 38552d5ee99bSBarry Smith Collective on TS 38562d5ee99bSBarry Smith 38572d5ee99bSBarry Smith Input Parameters: 38582d5ee99bSBarry Smith + ts - the TS context 38592d5ee99bSBarry Smith . step - current time-step 38602d5ee99bSBarry Smith . ptime - current time 38612d5ee99bSBarry Smith - dummy - either a viewer or NULL 38622d5ee99bSBarry Smith 38632d5ee99bSBarry Smith Level: intermediate 38642d5ee99bSBarry Smith 38652d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 38662d5ee99bSBarry Smith 38672d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 38682d5ee99bSBarry Smith @*/ 38692d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 38702d5ee99bSBarry Smith { 38712d5ee99bSBarry Smith PetscErrorCode ierr; 38722d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 38732d5ee99bSBarry Smith PetscDraw draw; 38742d5ee99bSBarry Smith MPI_Comm comm; 38752d5ee99bSBarry Smith PetscInt n; 38762d5ee99bSBarry Smith PetscMPIInt size; 38772d5ee99bSBarry Smith PetscReal xl,yl,xr,yr,tw,w,h; 38782d5ee99bSBarry Smith char time[32]; 38792d5ee99bSBarry Smith size_t len; 38802d5ee99bSBarry Smith const PetscScalar *U; 38812d5ee99bSBarry Smith 38822d5ee99bSBarry Smith PetscFunctionBegin; 38832d5ee99bSBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 38842d5ee99bSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 38852d5ee99bSBarry Smith if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs"); 38862d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 38872d5ee99bSBarry Smith if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 38882d5ee99bSBarry Smith 38892d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 38902d5ee99bSBarry Smith 38912d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 38924b363babSBarry Smith ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 3893ba5783adSMatthew G Knepley if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) { 3894a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 3895a4494fc1SBarry Smith PetscFunctionReturn(0); 3896a4494fc1SBarry Smith } 38972d5ee99bSBarry Smith if (!step) ictx->color++; 38982d5ee99bSBarry Smith ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr); 38992d5ee99bSBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 39002d5ee99bSBarry Smith 39012d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 39024b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 39032d5ee99bSBarry Smith ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr); 39042d5ee99bSBarry Smith ierr = PetscStrlen(time,&len);CHKERRQ(ierr); 39052d5ee99bSBarry Smith ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr); 39062d5ee99bSBarry Smith w = xl + .5*(xr - xl) - .5*len*tw; 39072d5ee99bSBarry Smith h = yl + .95*(yr - yl); 39082d5ee99bSBarry Smith ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 39092d5ee99bSBarry Smith } 39102d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 39112d5ee99bSBarry Smith PetscFunctionReturn(0); 39122d5ee99bSBarry Smith } 39132d5ee99bSBarry Smith 39141713a123SBarry Smith 39156c699258SBarry Smith #undef __FUNCT__ 391683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 39176083293cSBarry Smith /*@C 391883a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 39196083293cSBarry Smith 39206083293cSBarry Smith Collective on TS 39216083293cSBarry Smith 39226083293cSBarry Smith Input Parameters: 39236083293cSBarry Smith . ctx - the monitor context 39246083293cSBarry Smith 39256083293cSBarry Smith Level: intermediate 39266083293cSBarry Smith 39276083293cSBarry Smith .keywords: TS, vector, monitor, view 39286083293cSBarry Smith 392983a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 39306083293cSBarry Smith @*/ 393183a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 39326083293cSBarry Smith { 39336083293cSBarry Smith PetscErrorCode ierr; 39346083293cSBarry Smith 39356083293cSBarry Smith PetscFunctionBegin; 39364b363babSBarry Smith ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr); 393783a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 393883a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 393983a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 39406083293cSBarry Smith PetscFunctionReturn(0); 39416083293cSBarry Smith } 39426083293cSBarry Smith 39436083293cSBarry Smith #undef __FUNCT__ 394483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 39456083293cSBarry Smith /*@C 394683a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 39476083293cSBarry Smith 39486083293cSBarry Smith Collective on TS 39496083293cSBarry Smith 39506083293cSBarry Smith Input Parameter: 39516083293cSBarry Smith . ts - time-step context 39526083293cSBarry Smith 39536083293cSBarry Smith Output Patameter: 39546083293cSBarry Smith . ctx - the monitor context 39556083293cSBarry Smith 395699fdda47SBarry Smith Options Database: 395799fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 395899fdda47SBarry Smith 39596083293cSBarry Smith Level: intermediate 39606083293cSBarry Smith 39616083293cSBarry Smith .keywords: TS, vector, monitor, view 39626083293cSBarry Smith 396383a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 39646083293cSBarry Smith @*/ 396583a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 39666083293cSBarry Smith { 39676083293cSBarry Smith PetscErrorCode ierr; 39686083293cSBarry Smith 39696083293cSBarry Smith PetscFunctionBegin; 3970b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 397183a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 3972e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 3973bbd56ea5SKarl Rupp 397483a4ac43SBarry Smith (*ctx)->howoften = howoften; 3975473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 39760298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 3977473a3ab2SBarry Smith 3978473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 39790298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 39802d5ee99bSBarry Smith (*ctx)->color = PETSC_DRAW_WHITE; 39816083293cSBarry Smith PetscFunctionReturn(0); 39826083293cSBarry Smith } 39836083293cSBarry Smith 39846083293cSBarry Smith #undef __FUNCT__ 398583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 39863a471f94SBarry Smith /*@C 398783a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 39883a471f94SBarry Smith VecView() for the error at each timestep 39893a471f94SBarry Smith 39903a471f94SBarry Smith Collective on TS 39913a471f94SBarry Smith 39923a471f94SBarry Smith Input Parameters: 39933a471f94SBarry Smith + ts - the TS context 39943a471f94SBarry Smith . step - current time-step 39953a471f94SBarry Smith . ptime - current time 39960298fd71SBarry Smith - dummy - either a viewer or NULL 39973a471f94SBarry Smith 39983a471f94SBarry Smith Level: intermediate 39993a471f94SBarry Smith 40003a471f94SBarry Smith .keywords: TS, vector, monitor, view 40013a471f94SBarry Smith 40023a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 40033a471f94SBarry Smith @*/ 40040910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 40053a471f94SBarry Smith { 40063a471f94SBarry Smith PetscErrorCode ierr; 400783a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 400883a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 40093a471f94SBarry Smith Vec work; 40103a471f94SBarry Smith 40113a471f94SBarry Smith PetscFunctionBegin; 4012b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 40130910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 40143a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 40150910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 40163a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 40173a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 40183a471f94SBarry Smith PetscFunctionReturn(0); 40193a471f94SBarry Smith } 40203a471f94SBarry Smith 40212a34c10cSBarry Smith #include <petsc-private/dmimpl.h> 40223a471f94SBarry Smith #undef __FUNCT__ 40236c699258SBarry Smith #define __FUNCT__ "TSSetDM" 40246c699258SBarry Smith /*@ 40256c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 40266c699258SBarry Smith 40273f9fe445SBarry Smith Logically Collective on TS and DM 40286c699258SBarry Smith 40296c699258SBarry Smith Input Parameters: 40306c699258SBarry Smith + ts - the preconditioner context 40316c699258SBarry Smith - dm - the dm 40326c699258SBarry Smith 40336c699258SBarry Smith Level: intermediate 40346c699258SBarry Smith 40356c699258SBarry Smith 40366c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 40376c699258SBarry Smith @*/ 40387087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 40396c699258SBarry Smith { 40406c699258SBarry Smith PetscErrorCode ierr; 4041089b2837SJed Brown SNES snes; 4042942e3340SBarry Smith DMTS tsdm; 40436c699258SBarry Smith 40446c699258SBarry Smith PetscFunctionBegin; 40450700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 404670663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4047942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 40482a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4049942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4050942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 405124989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 405224989b8cSPeter Brune tsdm->originaldm = dm; 405324989b8cSPeter Brune } 405424989b8cSPeter Brune } 40556bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 405624989b8cSPeter Brune } 40576c699258SBarry Smith ts->dm = dm; 4058bbd56ea5SKarl Rupp 4059089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4060089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 40616c699258SBarry Smith PetscFunctionReturn(0); 40626c699258SBarry Smith } 40636c699258SBarry Smith 40646c699258SBarry Smith #undef __FUNCT__ 40656c699258SBarry Smith #define __FUNCT__ "TSGetDM" 40666c699258SBarry Smith /*@ 40676c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 40686c699258SBarry Smith 40693f9fe445SBarry Smith Not Collective 40706c699258SBarry Smith 40716c699258SBarry Smith Input Parameter: 40726c699258SBarry Smith . ts - the preconditioner context 40736c699258SBarry Smith 40746c699258SBarry Smith Output Parameter: 40756c699258SBarry Smith . dm - the dm 40766c699258SBarry Smith 40776c699258SBarry Smith Level: intermediate 40786c699258SBarry Smith 40796c699258SBarry Smith 40806c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 40816c699258SBarry Smith @*/ 40827087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 40836c699258SBarry Smith { 4084496e6a7aSJed Brown PetscErrorCode ierr; 4085496e6a7aSJed Brown 40866c699258SBarry Smith PetscFunctionBegin; 40870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4088496e6a7aSJed Brown if (!ts->dm) { 4089ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4090496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4091496e6a7aSJed Brown } 40926c699258SBarry Smith *dm = ts->dm; 40936c699258SBarry Smith PetscFunctionReturn(0); 40946c699258SBarry Smith } 40951713a123SBarry Smith 40960f5c6efeSJed Brown #undef __FUNCT__ 40970f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 40980f5c6efeSJed Brown /*@ 40990f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 41000f5c6efeSJed Brown 41013f9fe445SBarry Smith Logically Collective on SNES 41020f5c6efeSJed Brown 41030f5c6efeSJed Brown Input Parameter: 4104d42a1c89SJed Brown + snes - nonlinear solver 41050910c330SBarry Smith . U - the current state at which to evaluate the residual 4106d42a1c89SJed Brown - ctx - user context, must be a TS 41070f5c6efeSJed Brown 41080f5c6efeSJed Brown Output Parameter: 41090f5c6efeSJed Brown . F - the nonlinear residual 41100f5c6efeSJed Brown 41110f5c6efeSJed Brown Notes: 41120f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 41130f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 41140f5c6efeSJed Brown 41150f5c6efeSJed Brown Level: advanced 41160f5c6efeSJed Brown 41170f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 41180f5c6efeSJed Brown @*/ 41190910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 41200f5c6efeSJed Brown { 41210f5c6efeSJed Brown TS ts = (TS)ctx; 41220f5c6efeSJed Brown PetscErrorCode ierr; 41230f5c6efeSJed Brown 41240f5c6efeSJed Brown PetscFunctionBegin; 41250f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 41260910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 41270f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 41280f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 41290910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 41300f5c6efeSJed Brown PetscFunctionReturn(0); 41310f5c6efeSJed Brown } 41320f5c6efeSJed Brown 41330f5c6efeSJed Brown #undef __FUNCT__ 41340f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 41350f5c6efeSJed Brown /*@ 41360f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 41370f5c6efeSJed Brown 41380f5c6efeSJed Brown Collective on SNES 41390f5c6efeSJed Brown 41400f5c6efeSJed Brown Input Parameter: 41410f5c6efeSJed Brown + snes - nonlinear solver 41420910c330SBarry Smith . U - the current state at which to evaluate the residual 41430f5c6efeSJed Brown - ctx - user context, must be a TS 41440f5c6efeSJed Brown 41450f5c6efeSJed Brown Output Parameter: 41460f5c6efeSJed Brown + A - the Jacobian 41470f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 41480f5c6efeSJed Brown - flag - indicates any structure change in the matrix 41490f5c6efeSJed Brown 41500f5c6efeSJed Brown Notes: 41510f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 41520f5c6efeSJed Brown 41530f5c6efeSJed Brown Level: developer 41540f5c6efeSJed Brown 41550f5c6efeSJed Brown .seealso: SNESSetJacobian() 41560f5c6efeSJed Brown @*/ 4157d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 41580f5c6efeSJed Brown { 41590f5c6efeSJed Brown TS ts = (TS)ctx; 41600f5c6efeSJed Brown PetscErrorCode ierr; 41610f5c6efeSJed Brown 41620f5c6efeSJed Brown PetscFunctionBegin; 41630f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 41640910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 41650f5c6efeSJed Brown PetscValidPointer(A,3); 416694ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 41670f5c6efeSJed Brown PetscValidPointer(B,4); 416894ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 41690f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 4170d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 41710f5c6efeSJed Brown PetscFunctionReturn(0); 41720f5c6efeSJed Brown } 4173325fc9f4SBarry Smith 41740e4ef248SJed Brown #undef __FUNCT__ 41750e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 41760e4ef248SJed Brown /*@C 41770e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 41780e4ef248SJed Brown 41790e4ef248SJed Brown Collective on TS 41800e4ef248SJed Brown 41810e4ef248SJed Brown Input Arguments: 41820e4ef248SJed Brown + ts - time stepping context 41830e4ef248SJed Brown . t - time at which to evaluate 41840910c330SBarry Smith . U - state at which to evaluate 41850e4ef248SJed Brown - ctx - context 41860e4ef248SJed Brown 41870e4ef248SJed Brown Output Arguments: 41880e4ef248SJed Brown . F - right hand side 41890e4ef248SJed Brown 41900e4ef248SJed Brown Level: intermediate 41910e4ef248SJed Brown 41920e4ef248SJed Brown Notes: 41930e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 41940e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 41950e4ef248SJed Brown 41960e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 41970e4ef248SJed Brown @*/ 41980910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 41990e4ef248SJed Brown { 42000e4ef248SJed Brown PetscErrorCode ierr; 42010e4ef248SJed Brown Mat Arhs,Brhs; 42020e4ef248SJed Brown 42030e4ef248SJed Brown PetscFunctionBegin; 42040e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 4205d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 42060910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 42070e4ef248SJed Brown PetscFunctionReturn(0); 42080e4ef248SJed Brown } 42090e4ef248SJed Brown 42100e4ef248SJed Brown #undef __FUNCT__ 42110e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 42120e4ef248SJed Brown /*@C 42130e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 42140e4ef248SJed Brown 42150e4ef248SJed Brown Collective on TS 42160e4ef248SJed Brown 42170e4ef248SJed Brown Input Arguments: 42180e4ef248SJed Brown + ts - time stepping context 42190e4ef248SJed Brown . t - time at which to evaluate 42200910c330SBarry Smith . U - state at which to evaluate 42210e4ef248SJed Brown - ctx - context 42220e4ef248SJed Brown 42230e4ef248SJed Brown Output Arguments: 42240e4ef248SJed Brown + A - pointer to operator 42250e4ef248SJed Brown . B - pointer to preconditioning matrix 42260e4ef248SJed Brown - flg - matrix structure flag 42270e4ef248SJed Brown 42280e4ef248SJed Brown Level: intermediate 42290e4ef248SJed Brown 42300e4ef248SJed Brown Notes: 42310e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 42320e4ef248SJed Brown 42330e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 42340e4ef248SJed Brown @*/ 4235d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 42360e4ef248SJed Brown { 42370e4ef248SJed Brown PetscFunctionBegin; 42380e4ef248SJed Brown PetscFunctionReturn(0); 42390e4ef248SJed Brown } 42400e4ef248SJed Brown 42410026cea9SSean Farley #undef __FUNCT__ 42420026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 42430026cea9SSean Farley /*@C 42440026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 42450026cea9SSean Farley 42460026cea9SSean Farley Collective on TS 42470026cea9SSean Farley 42480026cea9SSean Farley Input Arguments: 42490026cea9SSean Farley + ts - time stepping context 42500026cea9SSean Farley . t - time at which to evaluate 42510910c330SBarry Smith . U - state at which to evaluate 42520910c330SBarry Smith . Udot - time derivative of state vector 42530026cea9SSean Farley - ctx - context 42540026cea9SSean Farley 42550026cea9SSean Farley Output Arguments: 42560026cea9SSean Farley . F - left hand side 42570026cea9SSean Farley 42580026cea9SSean Farley Level: intermediate 42590026cea9SSean Farley 42600026cea9SSean Farley Notes: 42610910c330SBarry 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 42620026cea9SSean Farley user is required to write their own TSComputeIFunction. 42630026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 42640026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 42650026cea9SSean Farley 42660026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 42670026cea9SSean Farley @*/ 42680910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 42690026cea9SSean Farley { 42700026cea9SSean Farley PetscErrorCode ierr; 42710026cea9SSean Farley Mat A,B; 42720026cea9SSean Farley 42730026cea9SSean Farley PetscFunctionBegin; 42740298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 4275d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 42760910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 42770026cea9SSean Farley PetscFunctionReturn(0); 42780026cea9SSean Farley } 42790026cea9SSean Farley 42800026cea9SSean Farley #undef __FUNCT__ 42810026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 42820026cea9SSean Farley /*@C 4283b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 42840026cea9SSean Farley 42850026cea9SSean Farley Collective on TS 42860026cea9SSean Farley 42870026cea9SSean Farley Input Arguments: 42880026cea9SSean Farley + ts - time stepping context 42890026cea9SSean Farley . t - time at which to evaluate 42900910c330SBarry Smith . U - state at which to evaluate 42910910c330SBarry Smith . Udot - time derivative of state vector 42920026cea9SSean Farley . shift - shift to apply 42930026cea9SSean Farley - ctx - context 42940026cea9SSean Farley 42950026cea9SSean Farley Output Arguments: 42960026cea9SSean Farley + A - pointer to operator 42970026cea9SSean Farley . B - pointer to preconditioning matrix 42980026cea9SSean Farley - flg - matrix structure flag 42990026cea9SSean Farley 4300b41af12eSJed Brown Level: advanced 43010026cea9SSean Farley 43020026cea9SSean Farley Notes: 43030026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 43040026cea9SSean Farley 4305b41af12eSJed Brown It is only appropriate for problems of the form 4306b41af12eSJed Brown 4307b41af12eSJed Brown $ M Udot = F(U,t) 4308b41af12eSJed Brown 4309b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4310b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4311b41af12eSJed Brown an implicit operator of the form 4312b41af12eSJed Brown 4313b41af12eSJed Brown $ shift*M + J 4314b41af12eSJed Brown 4315b41af12eSJed 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 4316b41af12eSJed Brown a copy of M or reassemble it when requested. 4317b41af12eSJed Brown 43180026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 43190026cea9SSean Farley @*/ 4320d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 43210026cea9SSean Farley { 4322b41af12eSJed Brown PetscErrorCode ierr; 4323b41af12eSJed Brown 43240026cea9SSean Farley PetscFunctionBegin; 432594ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4326b41af12eSJed Brown ts->ijacobian.shift = shift; 43270026cea9SSean Farley PetscFunctionReturn(0); 43280026cea9SSean Farley } 4329b41af12eSJed Brown 4330e817cc15SEmil Constantinescu #undef __FUNCT__ 4331e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType" 4332e817cc15SEmil Constantinescu /*@ 4333e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 4334e817cc15SEmil Constantinescu 4335e817cc15SEmil Constantinescu Not Collective 4336e817cc15SEmil Constantinescu 4337e817cc15SEmil Constantinescu Input Parameter: 4338e817cc15SEmil Constantinescu . ts - the TS context 4339e817cc15SEmil Constantinescu 4340e817cc15SEmil Constantinescu Output Parameter: 43414e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4342e817cc15SEmil Constantinescu 4343e817cc15SEmil Constantinescu Level: beginner 4344e817cc15SEmil Constantinescu 4345e817cc15SEmil Constantinescu .keywords: TS, equation type 4346e817cc15SEmil Constantinescu 4347e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 4348e817cc15SEmil Constantinescu @*/ 4349e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4350e817cc15SEmil Constantinescu { 4351e817cc15SEmil Constantinescu PetscFunctionBegin; 4352e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4353e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 4354e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 4355e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4356e817cc15SEmil Constantinescu } 4357e817cc15SEmil Constantinescu 4358e817cc15SEmil Constantinescu #undef __FUNCT__ 4359e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType" 4360e817cc15SEmil Constantinescu /*@ 4361e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 4362e817cc15SEmil Constantinescu 4363e817cc15SEmil Constantinescu Not Collective 4364e817cc15SEmil Constantinescu 4365e817cc15SEmil Constantinescu Input Parameter: 4366e817cc15SEmil Constantinescu + ts - the TS context 43674e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4368e817cc15SEmil Constantinescu 4369e817cc15SEmil Constantinescu Level: advanced 4370e817cc15SEmil Constantinescu 4371e817cc15SEmil Constantinescu .keywords: TS, equation type 4372e817cc15SEmil Constantinescu 4373e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 4374e817cc15SEmil Constantinescu @*/ 4375e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4376e817cc15SEmil Constantinescu { 4377e817cc15SEmil Constantinescu PetscFunctionBegin; 4378e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4379e817cc15SEmil Constantinescu ts->equation_type = equation_type; 4380e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4381e817cc15SEmil Constantinescu } 43820026cea9SSean Farley 43834af1b03aSJed Brown #undef __FUNCT__ 43844af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 43854af1b03aSJed Brown /*@ 43864af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 43874af1b03aSJed Brown 43884af1b03aSJed Brown Not Collective 43894af1b03aSJed Brown 43904af1b03aSJed Brown Input Parameter: 43914af1b03aSJed Brown . ts - the TS context 43924af1b03aSJed Brown 43934af1b03aSJed Brown Output Parameter: 43944af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 43954af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 43964af1b03aSJed Brown 4397487e0bb9SJed Brown Level: beginner 43984af1b03aSJed Brown 4399cd652676SJed Brown Notes: 4400cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 44014af1b03aSJed Brown 44024af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 44034af1b03aSJed Brown 44044af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 44054af1b03aSJed Brown @*/ 44064af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 44074af1b03aSJed Brown { 44084af1b03aSJed Brown PetscFunctionBegin; 44094af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 44104af1b03aSJed Brown PetscValidPointer(reason,2); 44114af1b03aSJed Brown *reason = ts->reason; 44124af1b03aSJed Brown PetscFunctionReturn(0); 44134af1b03aSJed Brown } 44144af1b03aSJed Brown 4415fb1732b5SBarry Smith #undef __FUNCT__ 4416d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 4417d6ad946cSShri Abhyankar /*@ 4418d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4419d6ad946cSShri Abhyankar 4420d6ad946cSShri Abhyankar Not Collective 4421d6ad946cSShri Abhyankar 4422d6ad946cSShri Abhyankar Input Parameter: 4423d6ad946cSShri Abhyankar + ts - the TS context 4424d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4425d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4426d6ad946cSShri Abhyankar 4427f5abba47SShri Abhyankar Level: advanced 4428d6ad946cSShri Abhyankar 4429d6ad946cSShri Abhyankar Notes: 4430d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 4431d6ad946cSShri Abhyankar 4432d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 4433d6ad946cSShri Abhyankar 4434d6ad946cSShri Abhyankar .seealso: TSConvergedReason 4435d6ad946cSShri Abhyankar @*/ 4436d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4437d6ad946cSShri Abhyankar { 4438d6ad946cSShri Abhyankar PetscFunctionBegin; 4439d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4440d6ad946cSShri Abhyankar ts->reason = reason; 4441d6ad946cSShri Abhyankar PetscFunctionReturn(0); 4442d6ad946cSShri Abhyankar } 4443d6ad946cSShri Abhyankar 4444d6ad946cSShri Abhyankar #undef __FUNCT__ 4445cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 4446cc708dedSBarry Smith /*@ 4447cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 4448cc708dedSBarry Smith 4449cc708dedSBarry Smith Not Collective 4450cc708dedSBarry Smith 4451cc708dedSBarry Smith Input Parameter: 4452cc708dedSBarry Smith . ts - the TS context 4453cc708dedSBarry Smith 4454cc708dedSBarry Smith Output Parameter: 4455cc708dedSBarry Smith . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 4456cc708dedSBarry Smith 4457487e0bb9SJed Brown Level: beginner 4458cc708dedSBarry Smith 4459cc708dedSBarry Smith Notes: 4460cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 4461cc708dedSBarry Smith 4462cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 4463cc708dedSBarry Smith 4464cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 4465cc708dedSBarry Smith @*/ 4466cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 4467cc708dedSBarry Smith { 4468cc708dedSBarry Smith PetscFunctionBegin; 4469cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4470cc708dedSBarry Smith PetscValidPointer(ftime,2); 4471cc708dedSBarry Smith *ftime = ts->solvetime; 4472cc708dedSBarry Smith PetscFunctionReturn(0); 4473cc708dedSBarry Smith } 4474cc708dedSBarry Smith 4475cc708dedSBarry Smith #undef __FUNCT__ 44762c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps" 44772c18e0fdSBarry Smith /*@ 44782c18e0fdSBarry Smith TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 44792c18e0fdSBarry Smith 44802c18e0fdSBarry Smith Not Collective 44812c18e0fdSBarry Smith 44822c18e0fdSBarry Smith Input Parameter: 44832c18e0fdSBarry Smith . ts - the TS context 44842c18e0fdSBarry Smith 44852c18e0fdSBarry Smith Output Parameter: 44862c18e0fdSBarry Smith . steps - the number of steps 44872c18e0fdSBarry Smith 44882c18e0fdSBarry Smith Level: beginner 44892c18e0fdSBarry Smith 44902c18e0fdSBarry Smith Notes: 44912c18e0fdSBarry Smith Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 44922c18e0fdSBarry Smith 44932c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test 44942c18e0fdSBarry Smith 44952c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 44962c18e0fdSBarry Smith @*/ 44972c18e0fdSBarry Smith PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 44982c18e0fdSBarry Smith { 44992c18e0fdSBarry Smith PetscFunctionBegin; 45002c18e0fdSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45012c18e0fdSBarry Smith PetscValidPointer(steps,2); 45022c18e0fdSBarry Smith *steps = ts->total_steps; 45032c18e0fdSBarry Smith PetscFunctionReturn(0); 45042c18e0fdSBarry Smith } 45052c18e0fdSBarry Smith 45062c18e0fdSBarry Smith #undef __FUNCT__ 45075ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 45089f67acb7SJed Brown /*@ 45095ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 45109f67acb7SJed Brown used by the time integrator. 45119f67acb7SJed Brown 45129f67acb7SJed Brown Not Collective 45139f67acb7SJed Brown 45149f67acb7SJed Brown Input Parameter: 45159f67acb7SJed Brown . ts - TS context 45169f67acb7SJed Brown 45179f67acb7SJed Brown Output Parameter: 45189f67acb7SJed Brown . nits - number of nonlinear iterations 45199f67acb7SJed Brown 45209f67acb7SJed Brown Notes: 45219f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 45229f67acb7SJed Brown 45239f67acb7SJed Brown Level: intermediate 45249f67acb7SJed Brown 45259f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 45269f67acb7SJed Brown 45275ef26d82SJed Brown .seealso: TSGetKSPIterations() 45289f67acb7SJed Brown @*/ 45295ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 45309f67acb7SJed Brown { 45319f67acb7SJed Brown PetscFunctionBegin; 45329f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45339f67acb7SJed Brown PetscValidIntPointer(nits,2); 45345ef26d82SJed Brown *nits = ts->snes_its; 45359f67acb7SJed Brown PetscFunctionReturn(0); 45369f67acb7SJed Brown } 45379f67acb7SJed Brown 45389f67acb7SJed Brown #undef __FUNCT__ 45395ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 45409f67acb7SJed Brown /*@ 45415ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 45429f67acb7SJed Brown used by the time integrator. 45439f67acb7SJed Brown 45449f67acb7SJed Brown Not Collective 45459f67acb7SJed Brown 45469f67acb7SJed Brown Input Parameter: 45479f67acb7SJed Brown . ts - TS context 45489f67acb7SJed Brown 45499f67acb7SJed Brown Output Parameter: 45509f67acb7SJed Brown . lits - number of linear iterations 45519f67acb7SJed Brown 45529f67acb7SJed Brown Notes: 45539f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 45549f67acb7SJed Brown 45559f67acb7SJed Brown Level: intermediate 45569f67acb7SJed Brown 45579f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 45589f67acb7SJed Brown 45595ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 45609f67acb7SJed Brown @*/ 45615ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 45629f67acb7SJed Brown { 45639f67acb7SJed Brown PetscFunctionBegin; 45649f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45659f67acb7SJed Brown PetscValidIntPointer(lits,2); 45665ef26d82SJed Brown *lits = ts->ksp_its; 45679f67acb7SJed Brown PetscFunctionReturn(0); 45689f67acb7SJed Brown } 45699f67acb7SJed Brown 45709f67acb7SJed Brown #undef __FUNCT__ 4571cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 4572cef5090cSJed Brown /*@ 4573cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4574cef5090cSJed Brown 4575cef5090cSJed Brown Not Collective 4576cef5090cSJed Brown 4577cef5090cSJed Brown Input Parameter: 4578cef5090cSJed Brown . ts - TS context 4579cef5090cSJed Brown 4580cef5090cSJed Brown Output Parameter: 4581cef5090cSJed Brown . rejects - number of steps rejected 4582cef5090cSJed Brown 4583cef5090cSJed Brown Notes: 4584cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4585cef5090cSJed Brown 4586cef5090cSJed Brown Level: intermediate 4587cef5090cSJed Brown 4588cef5090cSJed Brown .keywords: TS, get, number 4589cef5090cSJed Brown 45905ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 4591cef5090cSJed Brown @*/ 4592cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 4593cef5090cSJed Brown { 4594cef5090cSJed Brown PetscFunctionBegin; 4595cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4596cef5090cSJed Brown PetscValidIntPointer(rejects,2); 4597cef5090cSJed Brown *rejects = ts->reject; 4598cef5090cSJed Brown PetscFunctionReturn(0); 4599cef5090cSJed Brown } 4600cef5090cSJed Brown 4601cef5090cSJed Brown #undef __FUNCT__ 4602cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 4603cef5090cSJed Brown /*@ 4604cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 4605cef5090cSJed Brown 4606cef5090cSJed Brown Not Collective 4607cef5090cSJed Brown 4608cef5090cSJed Brown Input Parameter: 4609cef5090cSJed Brown . ts - TS context 4610cef5090cSJed Brown 4611cef5090cSJed Brown Output Parameter: 4612cef5090cSJed Brown . fails - number of failed nonlinear solves 4613cef5090cSJed Brown 4614cef5090cSJed Brown Notes: 4615cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4616cef5090cSJed Brown 4617cef5090cSJed Brown Level: intermediate 4618cef5090cSJed Brown 4619cef5090cSJed Brown .keywords: TS, get, number 4620cef5090cSJed Brown 46215ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 4622cef5090cSJed Brown @*/ 4623cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 4624cef5090cSJed Brown { 4625cef5090cSJed Brown PetscFunctionBegin; 4626cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4627cef5090cSJed Brown PetscValidIntPointer(fails,2); 4628cef5090cSJed Brown *fails = ts->num_snes_failures; 4629cef5090cSJed Brown PetscFunctionReturn(0); 4630cef5090cSJed Brown } 4631cef5090cSJed Brown 4632cef5090cSJed Brown #undef __FUNCT__ 4633cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 4634cef5090cSJed Brown /*@ 4635cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 4636cef5090cSJed Brown 4637cef5090cSJed Brown Not Collective 4638cef5090cSJed Brown 4639cef5090cSJed Brown Input Parameter: 4640cef5090cSJed Brown + ts - TS context 4641cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 4642cef5090cSJed Brown 4643cef5090cSJed Brown Notes: 4644cef5090cSJed Brown The counter is reset to zero for each step 4645cef5090cSJed Brown 4646cef5090cSJed Brown Options Database Key: 4647cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4648cef5090cSJed Brown 4649cef5090cSJed Brown Level: intermediate 4650cef5090cSJed Brown 4651cef5090cSJed Brown .keywords: TS, set, maximum, number 4652cef5090cSJed Brown 46535ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4654cef5090cSJed Brown @*/ 4655cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 4656cef5090cSJed Brown { 4657cef5090cSJed Brown PetscFunctionBegin; 4658cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4659cef5090cSJed Brown ts->max_reject = rejects; 4660cef5090cSJed Brown PetscFunctionReturn(0); 4661cef5090cSJed Brown } 4662cef5090cSJed Brown 4663cef5090cSJed Brown #undef __FUNCT__ 4664cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 4665cef5090cSJed Brown /*@ 4666cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 4667cef5090cSJed Brown 4668cef5090cSJed Brown Not Collective 4669cef5090cSJed Brown 4670cef5090cSJed Brown Input Parameter: 4671cef5090cSJed Brown + ts - TS context 4672cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 4673cef5090cSJed Brown 4674cef5090cSJed Brown Notes: 4675cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 4676cef5090cSJed Brown 4677cef5090cSJed Brown Options Database Key: 4678cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4679cef5090cSJed Brown 4680cef5090cSJed Brown Level: intermediate 4681cef5090cSJed Brown 4682cef5090cSJed Brown .keywords: TS, set, maximum, number 4683cef5090cSJed Brown 46845ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 4685cef5090cSJed Brown @*/ 4686cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 4687cef5090cSJed Brown { 4688cef5090cSJed Brown PetscFunctionBegin; 4689cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4690cef5090cSJed Brown ts->max_snes_failures = fails; 4691cef5090cSJed Brown PetscFunctionReturn(0); 4692cef5090cSJed Brown } 4693cef5090cSJed Brown 4694cef5090cSJed Brown #undef __FUNCT__ 46954e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails" 4696cef5090cSJed Brown /*@ 4697cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 4698cef5090cSJed Brown 4699cef5090cSJed Brown Not Collective 4700cef5090cSJed Brown 4701cef5090cSJed Brown Input Parameter: 4702cef5090cSJed Brown + ts - TS context 4703cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 4704cef5090cSJed Brown 4705cef5090cSJed Brown Options Database Key: 4706cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 4707cef5090cSJed Brown 4708cef5090cSJed Brown Level: intermediate 4709cef5090cSJed Brown 4710cef5090cSJed Brown .keywords: TS, set, error 4711cef5090cSJed Brown 47125ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4713cef5090cSJed Brown @*/ 4714cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 4715cef5090cSJed Brown { 4716cef5090cSJed Brown PetscFunctionBegin; 4717cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4718cef5090cSJed Brown ts->errorifstepfailed = err; 4719cef5090cSJed Brown PetscFunctionReturn(0); 4720cef5090cSJed Brown } 4721cef5090cSJed Brown 4722cef5090cSJed Brown #undef __FUNCT__ 4723fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 4724fb1732b5SBarry Smith /*@C 4725fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 4726fb1732b5SBarry Smith 4727fb1732b5SBarry Smith Collective on TS 4728fb1732b5SBarry Smith 4729fb1732b5SBarry Smith Input Parameters: 4730fb1732b5SBarry Smith + ts - the TS context 4731fb1732b5SBarry Smith . step - current time-step 4732fb1732b5SBarry Smith . ptime - current time 47330910c330SBarry Smith . u - current state 4734fb1732b5SBarry Smith - viewer - binary viewer 4735fb1732b5SBarry Smith 4736fb1732b5SBarry Smith Level: intermediate 4737fb1732b5SBarry Smith 4738fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 4739fb1732b5SBarry Smith 4740fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4741fb1732b5SBarry Smith @*/ 47420910c330SBarry Smith PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 4743fb1732b5SBarry Smith { 4744fb1732b5SBarry Smith PetscErrorCode ierr; 4745ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 4746fb1732b5SBarry Smith 4747fb1732b5SBarry Smith PetscFunctionBegin; 47480910c330SBarry Smith ierr = VecView(u,v);CHKERRQ(ierr); 4749ed81e22dSJed Brown PetscFunctionReturn(0); 4750ed81e22dSJed Brown } 4751ed81e22dSJed Brown 4752ed81e22dSJed Brown #undef __FUNCT__ 4753ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 4754ed81e22dSJed Brown /*@C 4755ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 4756ed81e22dSJed Brown 4757ed81e22dSJed Brown Collective on TS 4758ed81e22dSJed Brown 4759ed81e22dSJed Brown Input Parameters: 4760ed81e22dSJed Brown + ts - the TS context 4761ed81e22dSJed Brown . step - current time-step 4762ed81e22dSJed Brown . ptime - current time 47630910c330SBarry Smith . u - current state 4764ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 4765ed81e22dSJed Brown 4766ed81e22dSJed Brown Level: intermediate 4767ed81e22dSJed Brown 4768ed81e22dSJed Brown Notes: 4769ed81e22dSJed 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. 4770ed81e22dSJed Brown These are named according to the file name template. 4771ed81e22dSJed Brown 4772ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 4773ed81e22dSJed Brown 4774ed81e22dSJed Brown .keywords: TS, vector, monitor, view 4775ed81e22dSJed Brown 4776ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4777ed81e22dSJed Brown @*/ 47780910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 4779ed81e22dSJed Brown { 4780ed81e22dSJed Brown PetscErrorCode ierr; 4781ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 4782ed81e22dSJed Brown PetscViewer viewer; 4783ed81e22dSJed Brown 4784ed81e22dSJed Brown PetscFunctionBegin; 47858caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 4786ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 47870910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 4788ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 4789ed81e22dSJed Brown PetscFunctionReturn(0); 4790ed81e22dSJed Brown } 4791ed81e22dSJed Brown 4792ed81e22dSJed Brown #undef __FUNCT__ 4793ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 4794ed81e22dSJed Brown /*@C 4795ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 4796ed81e22dSJed Brown 4797ed81e22dSJed Brown Collective on TS 4798ed81e22dSJed Brown 4799ed81e22dSJed Brown Input Parameters: 4800ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 4801ed81e22dSJed Brown 4802ed81e22dSJed Brown Level: intermediate 4803ed81e22dSJed Brown 4804ed81e22dSJed Brown Note: 4805ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 4806ed81e22dSJed Brown 4807ed81e22dSJed Brown .keywords: TS, vector, monitor, view 4808ed81e22dSJed Brown 4809ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 4810ed81e22dSJed Brown @*/ 4811ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 4812ed81e22dSJed Brown { 4813ed81e22dSJed Brown PetscErrorCode ierr; 4814ed81e22dSJed Brown 4815ed81e22dSJed Brown PetscFunctionBegin; 4816ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 4817fb1732b5SBarry Smith PetscFunctionReturn(0); 4818fb1732b5SBarry Smith } 4819fb1732b5SBarry Smith 482084df9cb4SJed Brown #undef __FUNCT__ 4821552698daSJed Brown #define __FUNCT__ "TSGetAdapt" 482284df9cb4SJed Brown /*@ 4823552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 482484df9cb4SJed Brown 4825ed81e22dSJed Brown Collective on TS if controller has not been created yet 482684df9cb4SJed Brown 482784df9cb4SJed Brown Input Arguments: 4828ed81e22dSJed Brown . ts - time stepping context 482984df9cb4SJed Brown 483084df9cb4SJed Brown Output Arguments: 4831ed81e22dSJed Brown . adapt - adaptive controller 483284df9cb4SJed Brown 483384df9cb4SJed Brown Level: intermediate 483484df9cb4SJed Brown 4835ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 483684df9cb4SJed Brown @*/ 4837552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 483884df9cb4SJed Brown { 483984df9cb4SJed Brown PetscErrorCode ierr; 484084df9cb4SJed Brown 484184df9cb4SJed Brown PetscFunctionBegin; 484284df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 484384df9cb4SJed Brown PetscValidPointer(adapt,2); 484484df9cb4SJed Brown if (!ts->adapt) { 4845ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 48463bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 48471c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 484884df9cb4SJed Brown } 484984df9cb4SJed Brown *adapt = ts->adapt; 485084df9cb4SJed Brown PetscFunctionReturn(0); 485184df9cb4SJed Brown } 4852d6ebe24aSShri Abhyankar 4853d6ebe24aSShri Abhyankar #undef __FUNCT__ 48541c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 48551c3436cfSJed Brown /*@ 48561c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 48571c3436cfSJed Brown 48581c3436cfSJed Brown Logically Collective 48591c3436cfSJed Brown 48601c3436cfSJed Brown Input Arguments: 48611c3436cfSJed Brown + ts - time integration context 48621c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 48630298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 48641c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 48650298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 48661c3436cfSJed Brown 4867a3cdaa26SBarry Smith Options Database keys: 4868a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 4869a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 4870a3cdaa26SBarry Smith 48711c3436cfSJed Brown Level: beginner 48721c3436cfSJed Brown 4873c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 48741c3436cfSJed Brown @*/ 48751c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 48761c3436cfSJed Brown { 48771c3436cfSJed Brown PetscErrorCode ierr; 48781c3436cfSJed Brown 48791c3436cfSJed Brown PetscFunctionBegin; 4880c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 48811c3436cfSJed Brown if (vatol) { 48821c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 48831c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 4884bbd56ea5SKarl Rupp 48851c3436cfSJed Brown ts->vatol = vatol; 48861c3436cfSJed Brown } 4887c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 48881c3436cfSJed Brown if (vrtol) { 48891c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 48901c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 4891bbd56ea5SKarl Rupp 48921c3436cfSJed Brown ts->vrtol = vrtol; 48931c3436cfSJed Brown } 48941c3436cfSJed Brown PetscFunctionReturn(0); 48951c3436cfSJed Brown } 48961c3436cfSJed Brown 48971c3436cfSJed Brown #undef __FUNCT__ 4898c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 4899c5033834SJed Brown /*@ 4900c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 4901c5033834SJed Brown 4902c5033834SJed Brown Logically Collective 4903c5033834SJed Brown 4904c5033834SJed Brown Input Arguments: 4905c5033834SJed Brown . ts - time integration context 4906c5033834SJed Brown 4907c5033834SJed Brown Output Arguments: 49080298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 49090298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 49100298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 49110298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 4912c5033834SJed Brown 4913c5033834SJed Brown Level: beginner 4914c5033834SJed Brown 4915c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 4916c5033834SJed Brown @*/ 4917c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 4918c5033834SJed Brown { 4919c5033834SJed Brown PetscFunctionBegin; 4920c5033834SJed Brown if (atol) *atol = ts->atol; 4921c5033834SJed Brown if (vatol) *vatol = ts->vatol; 4922c5033834SJed Brown if (rtol) *rtol = ts->rtol; 4923c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 4924c5033834SJed Brown PetscFunctionReturn(0); 4925c5033834SJed Brown } 4926c5033834SJed Brown 4927c5033834SJed Brown #undef __FUNCT__ 49281c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS" 49291c3436cfSJed Brown /*@ 49301c3436cfSJed Brown TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 49311c3436cfSJed Brown 49321c3436cfSJed Brown Collective on TS 49331c3436cfSJed Brown 49341c3436cfSJed Brown Input Arguments: 49351c3436cfSJed Brown + ts - time stepping context 49361c3436cfSJed Brown - Y - state vector to be compared to ts->vec_sol 49371c3436cfSJed Brown 49381c3436cfSJed Brown Output Arguments: 49391c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 49401c3436cfSJed Brown 49411c3436cfSJed Brown Level: developer 49421c3436cfSJed Brown 49431c3436cfSJed Brown .seealso: TSSetTolerances() 49441c3436cfSJed Brown @*/ 49451c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 49461c3436cfSJed Brown { 49471c3436cfSJed Brown PetscErrorCode ierr; 49481c3436cfSJed Brown PetscInt i,n,N; 49490910c330SBarry Smith const PetscScalar *u,*y; 49500910c330SBarry Smith Vec U; 49511c3436cfSJed Brown PetscReal sum,gsum; 49521c3436cfSJed Brown 49531c3436cfSJed Brown PetscFunctionBegin; 49541c3436cfSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 49551c3436cfSJed Brown PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 49561c3436cfSJed Brown PetscValidPointer(norm,3); 49570910c330SBarry Smith U = ts->vec_sol; 49580910c330SBarry Smith PetscCheckSameTypeAndComm(U,1,Y,2); 4959ce94432eSBarry Smith if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 49601c3436cfSJed Brown 49610910c330SBarry Smith ierr = VecGetSize(U,&N);CHKERRQ(ierr); 49620910c330SBarry Smith ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 49630910c330SBarry Smith ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 49641c3436cfSJed Brown ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 49651c3436cfSJed Brown sum = 0.; 4966ceefc113SJed Brown if (ts->vatol && ts->vrtol) { 4967ceefc113SJed Brown const PetscScalar *atol,*rtol; 4968ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4969ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4970ceefc113SJed Brown for (i=0; i<n; i++) { 49710910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 49720910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4973ceefc113SJed Brown } 4974ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4975ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4976ceefc113SJed Brown } else if (ts->vatol) { /* vector atol, scalar rtol */ 4977ceefc113SJed Brown const PetscScalar *atol; 4978ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4979ceefc113SJed Brown for (i=0; i<n; i++) { 49800910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 49810910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4982ceefc113SJed Brown } 4983ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4984ceefc113SJed Brown } else if (ts->vrtol) { /* scalar atol, vector rtol */ 4985ceefc113SJed Brown const PetscScalar *rtol; 4986ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4987ceefc113SJed Brown for (i=0; i<n; i++) { 49880910c330SBarry Smith PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 49890910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4990ceefc113SJed Brown } 4991ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4992ceefc113SJed Brown } else { /* scalar atol, scalar rtol */ 49931c3436cfSJed Brown for (i=0; i<n; i++) { 49940910c330SBarry Smith PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 49950910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 49961c3436cfSJed Brown } 4997ceefc113SJed Brown } 49980910c330SBarry Smith ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 49991c3436cfSJed Brown ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 50001c3436cfSJed Brown 5001ce94432eSBarry Smith ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 50021c3436cfSJed Brown *norm = PetscSqrtReal(gsum / N); 5003620fc8aaSSatish Balay if (PetscIsInfOrNanReal(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 50041c3436cfSJed Brown PetscFunctionReturn(0); 50051c3436cfSJed Brown } 50061c3436cfSJed Brown 50071c3436cfSJed Brown #undef __FUNCT__ 50088d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 50098d59e960SJed Brown /*@ 50108d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 50118d59e960SJed Brown 50128d59e960SJed Brown Logically Collective on TS 50138d59e960SJed Brown 50148d59e960SJed Brown Input Arguments: 50158d59e960SJed Brown + ts - time stepping context 50168d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 50178d59e960SJed Brown 50188d59e960SJed Brown Note: 50198d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 50208d59e960SJed Brown 50218d59e960SJed Brown Level: intermediate 50228d59e960SJed Brown 50238d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 50248d59e960SJed Brown @*/ 50258d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 50268d59e960SJed Brown { 50278d59e960SJed Brown PetscFunctionBegin; 50288d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 50298d59e960SJed Brown ts->cfltime_local = cfltime; 50308d59e960SJed Brown ts->cfltime = -1.; 50318d59e960SJed Brown PetscFunctionReturn(0); 50328d59e960SJed Brown } 50338d59e960SJed Brown 50348d59e960SJed Brown #undef __FUNCT__ 50358d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 50368d59e960SJed Brown /*@ 50378d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 50388d59e960SJed Brown 50398d59e960SJed Brown Collective on TS 50408d59e960SJed Brown 50418d59e960SJed Brown Input Arguments: 50428d59e960SJed Brown . ts - time stepping context 50438d59e960SJed Brown 50448d59e960SJed Brown Output Arguments: 50458d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 50468d59e960SJed Brown 50478d59e960SJed Brown Level: advanced 50488d59e960SJed Brown 50498d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 50508d59e960SJed Brown @*/ 50518d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 50528d59e960SJed Brown { 50538d59e960SJed Brown PetscErrorCode ierr; 50548d59e960SJed Brown 50558d59e960SJed Brown PetscFunctionBegin; 50568d59e960SJed Brown if (ts->cfltime < 0) { 5057ce94432eSBarry Smith ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 50588d59e960SJed Brown } 50598d59e960SJed Brown *cfltime = ts->cfltime; 50608d59e960SJed Brown PetscFunctionReturn(0); 50618d59e960SJed Brown } 50628d59e960SJed Brown 50638d59e960SJed Brown #undef __FUNCT__ 5064d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 5065d6ebe24aSShri Abhyankar /*@ 5066d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5067d6ebe24aSShri Abhyankar 5068d6ebe24aSShri Abhyankar Input Parameters: 5069d6ebe24aSShri Abhyankar . ts - the TS context. 5070d6ebe24aSShri Abhyankar . xl - lower bound. 5071d6ebe24aSShri Abhyankar . xu - upper bound. 5072d6ebe24aSShri Abhyankar 5073d6ebe24aSShri Abhyankar Notes: 5074d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 5075e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 5076d6ebe24aSShri Abhyankar 50772bd2b0e6SSatish Balay Level: advanced 50782bd2b0e6SSatish Balay 5079d6ebe24aSShri Abhyankar @*/ 5080d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5081d6ebe24aSShri Abhyankar { 5082d6ebe24aSShri Abhyankar PetscErrorCode ierr; 5083d6ebe24aSShri Abhyankar SNES snes; 5084d6ebe24aSShri Abhyankar 5085d6ebe24aSShri Abhyankar PetscFunctionBegin; 5086d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 5087d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 5088d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 5089d6ebe24aSShri Abhyankar } 5090d6ebe24aSShri Abhyankar 5091325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 5092c6db04a5SJed Brown #include <mex.h> 5093325fc9f4SBarry Smith 5094325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 5095325fc9f4SBarry Smith 5096325fc9f4SBarry Smith #undef __FUNCT__ 5097325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 5098325fc9f4SBarry Smith /* 5099325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 5100325fc9f4SBarry Smith TSSetFunctionMatlab(). 5101325fc9f4SBarry Smith 5102325fc9f4SBarry Smith Collective on TS 5103325fc9f4SBarry Smith 5104325fc9f4SBarry Smith Input Parameters: 5105325fc9f4SBarry Smith + snes - the TS context 51060910c330SBarry Smith - u - input vector 5107325fc9f4SBarry Smith 5108325fc9f4SBarry Smith Output Parameter: 5109325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 5110325fc9f4SBarry Smith 5111325fc9f4SBarry Smith Notes: 5112325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 5113325fc9f4SBarry Smith implementations, so most users would not generally call this routine 5114325fc9f4SBarry Smith themselves. 5115325fc9f4SBarry Smith 5116325fc9f4SBarry Smith Level: developer 5117325fc9f4SBarry Smith 5118325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5119325fc9f4SBarry Smith 5120325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5121325fc9f4SBarry Smith */ 51220910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 5123325fc9f4SBarry Smith { 5124325fc9f4SBarry Smith PetscErrorCode ierr; 5125325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5126325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 5127325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 5128325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 5129325fc9f4SBarry Smith 5130325fc9f4SBarry Smith PetscFunctionBegin; 5131325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 51320910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 51330910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 5134325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 51350910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 5136325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 5137325fc9f4SBarry Smith 5138325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 51390910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 51400910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 51410910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 5142bbd56ea5SKarl Rupp 5143325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5144325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 5145325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5146325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5147325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 5148325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 5149325fc9f4SBarry Smith prhs[6] = sctx->ctx; 5150325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 5151325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5152325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5153325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5154325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5155325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5156325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5157325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5158325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5159325fc9f4SBarry Smith PetscFunctionReturn(0); 5160325fc9f4SBarry Smith } 5161325fc9f4SBarry Smith 5162325fc9f4SBarry Smith 5163325fc9f4SBarry Smith #undef __FUNCT__ 5164325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 5165325fc9f4SBarry Smith /* 5166325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 5167325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 5168e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 5169325fc9f4SBarry Smith 5170325fc9f4SBarry Smith Logically Collective on TS 5171325fc9f4SBarry Smith 5172325fc9f4SBarry Smith Input Parameters: 5173325fc9f4SBarry Smith + ts - the TS context 5174325fc9f4SBarry Smith - func - function evaluation routine 5175325fc9f4SBarry Smith 5176325fc9f4SBarry Smith Calling sequence of func: 51770910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 5178325fc9f4SBarry Smith 5179325fc9f4SBarry Smith Level: beginner 5180325fc9f4SBarry Smith 5181325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5182325fc9f4SBarry Smith 5183325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5184325fc9f4SBarry Smith */ 5185cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 5186325fc9f4SBarry Smith { 5187325fc9f4SBarry Smith PetscErrorCode ierr; 5188325fc9f4SBarry Smith TSMatlabContext *sctx; 5189325fc9f4SBarry Smith 5190325fc9f4SBarry Smith PetscFunctionBegin; 5191325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5192325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5193325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5194325fc9f4SBarry Smith /* 5195325fc9f4SBarry Smith This should work, but it doesn't 5196325fc9f4SBarry Smith sctx->ctx = ctx; 5197325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5198325fc9f4SBarry Smith */ 5199325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5200bbd56ea5SKarl Rupp 52010298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 5202325fc9f4SBarry Smith PetscFunctionReturn(0); 5203325fc9f4SBarry Smith } 5204325fc9f4SBarry Smith 5205325fc9f4SBarry Smith #undef __FUNCT__ 5206325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 5207325fc9f4SBarry Smith /* 5208325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 5209325fc9f4SBarry Smith TSSetJacobianMatlab(). 5210325fc9f4SBarry Smith 5211325fc9f4SBarry Smith Collective on TS 5212325fc9f4SBarry Smith 5213325fc9f4SBarry Smith Input Parameters: 5214cdcf91faSSean Farley + ts - the TS context 52150910c330SBarry Smith . u - input vector 5216325fc9f4SBarry Smith . A, B - the matrices 5217325fc9f4SBarry Smith - ctx - user context 5218325fc9f4SBarry Smith 5219325fc9f4SBarry Smith Level: developer 5220325fc9f4SBarry Smith 5221325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5222325fc9f4SBarry Smith 5223325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5224325fc9f4SBarry Smith @*/ 5225f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 5226325fc9f4SBarry Smith { 5227325fc9f4SBarry Smith PetscErrorCode ierr; 5228325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5229325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 5230325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 5231325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 5232325fc9f4SBarry Smith 5233325fc9f4SBarry Smith PetscFunctionBegin; 5234cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 52350910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 5236325fc9f4SBarry Smith 52370910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 5238325fc9f4SBarry Smith 5239cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 52400910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 52410910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 52420910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 52430910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 5244bbd56ea5SKarl Rupp 5245325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5246325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 5247325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5248325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5249325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 5250325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 5251325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 5252325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 5253325fc9f4SBarry Smith prhs[8] = sctx->ctx; 5254325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 5255325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5256325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5257325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5258325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5259325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5260325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5261325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5262325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 5263325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 5264325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5265325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 5266325fc9f4SBarry Smith PetscFunctionReturn(0); 5267325fc9f4SBarry Smith } 5268325fc9f4SBarry Smith 5269325fc9f4SBarry Smith 5270325fc9f4SBarry Smith #undef __FUNCT__ 5271325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 5272325fc9f4SBarry Smith /* 5273325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 5274e3c5b3baSBarry 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 5275325fc9f4SBarry Smith 5276325fc9f4SBarry Smith Logically Collective on TS 5277325fc9f4SBarry Smith 5278325fc9f4SBarry Smith Input Parameters: 5279cdcf91faSSean Farley + ts - the TS context 5280325fc9f4SBarry Smith . A,B - Jacobian matrices 5281325fc9f4SBarry Smith . func - function evaluation routine 5282325fc9f4SBarry Smith - ctx - user context 5283325fc9f4SBarry Smith 5284325fc9f4SBarry Smith Calling sequence of func: 52850910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 5286325fc9f4SBarry Smith 5287325fc9f4SBarry Smith 5288325fc9f4SBarry Smith Level: developer 5289325fc9f4SBarry Smith 5290325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5291325fc9f4SBarry Smith 5292325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5293325fc9f4SBarry Smith */ 5294cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 5295325fc9f4SBarry Smith { 5296325fc9f4SBarry Smith PetscErrorCode ierr; 5297325fc9f4SBarry Smith TSMatlabContext *sctx; 5298325fc9f4SBarry Smith 5299325fc9f4SBarry Smith PetscFunctionBegin; 5300325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5301325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5302325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5303325fc9f4SBarry Smith /* 5304325fc9f4SBarry Smith This should work, but it doesn't 5305325fc9f4SBarry Smith sctx->ctx = ctx; 5306325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5307325fc9f4SBarry Smith */ 5308325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5309bbd56ea5SKarl Rupp 5310cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 5311325fc9f4SBarry Smith PetscFunctionReturn(0); 5312325fc9f4SBarry Smith } 5313325fc9f4SBarry Smith 5314b5b1a830SBarry Smith #undef __FUNCT__ 5315b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 5316b5b1a830SBarry Smith /* 5317b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 5318b5b1a830SBarry Smith 5319b5b1a830SBarry Smith Collective on TS 5320b5b1a830SBarry Smith 5321b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5322b5b1a830SBarry Smith @*/ 53230910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 5324b5b1a830SBarry Smith { 5325b5b1a830SBarry Smith PetscErrorCode ierr; 5326b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5327a530c242SBarry Smith int nlhs = 1,nrhs = 6; 5328b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 5329b5b1a830SBarry Smith long long int lx = 0,ls = 0; 5330b5b1a830SBarry Smith 5331b5b1a830SBarry Smith PetscFunctionBegin; 5332cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53330910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 5334b5b1a830SBarry Smith 5335cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 53360910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 5337bbd56ea5SKarl Rupp 5338b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5339b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 5340b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 5341b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 5342b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 5343b5b1a830SBarry Smith prhs[5] = sctx->ctx; 5344b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 5345b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5346b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 5347b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 5348b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 5349b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 5350b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 5351b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 5352b5b1a830SBarry Smith PetscFunctionReturn(0); 5353b5b1a830SBarry Smith } 5354b5b1a830SBarry Smith 5355b5b1a830SBarry Smith 5356b5b1a830SBarry Smith #undef __FUNCT__ 5357b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 5358b5b1a830SBarry Smith /* 5359b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 5360b5b1a830SBarry Smith 5361b5b1a830SBarry Smith Level: developer 5362b5b1a830SBarry Smith 5363b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 5364b5b1a830SBarry Smith 5365b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5366b5b1a830SBarry Smith */ 5367cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 5368b5b1a830SBarry Smith { 5369b5b1a830SBarry Smith PetscErrorCode ierr; 5370b5b1a830SBarry Smith TSMatlabContext *sctx; 5371b5b1a830SBarry Smith 5372b5b1a830SBarry Smith PetscFunctionBegin; 5373b5b1a830SBarry Smith /* currently sctx is memory bleed */ 5374b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5375b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5376b5b1a830SBarry Smith /* 5377b5b1a830SBarry Smith This should work, but it doesn't 5378b5b1a830SBarry Smith sctx->ctx = ctx; 5379b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5380b5b1a830SBarry Smith */ 5381b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5382bbd56ea5SKarl Rupp 53830298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 5384b5b1a830SBarry Smith PetscFunctionReturn(0); 5385b5b1a830SBarry Smith } 5386325fc9f4SBarry Smith #endif 5387b3603a34SBarry Smith 53880b039ecaSBarry Smith 53890b039ecaSBarry Smith 5390b3603a34SBarry Smith #undef __FUNCT__ 53914f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 5392b3603a34SBarry Smith /*@C 53934f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 5394b3603a34SBarry Smith in a time based line graph 5395b3603a34SBarry Smith 5396b3603a34SBarry Smith Collective on TS 5397b3603a34SBarry Smith 5398b3603a34SBarry Smith Input Parameters: 5399b3603a34SBarry Smith + ts - the TS context 5400b3603a34SBarry Smith . step - current time-step 5401b3603a34SBarry Smith . ptime - current time 5402b3603a34SBarry Smith - lg - a line graph object 5403b3603a34SBarry Smith 5404b3603a34SBarry Smith Level: intermediate 5405b3603a34SBarry Smith 54060b039ecaSBarry Smith Notes: each process in a parallel run displays its component solutions in a separate window 5407b3603a34SBarry Smith 5408b3603a34SBarry Smith .keywords: TS, vector, monitor, view 5409b3603a34SBarry Smith 5410b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5411b3603a34SBarry Smith @*/ 54120910c330SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 5413b3603a34SBarry Smith { 5414b3603a34SBarry Smith PetscErrorCode ierr; 54150b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 5416b3603a34SBarry Smith const PetscScalar *yy; 541758ff32f7SBarry Smith PetscInt dim; 5418b3603a34SBarry Smith 5419b3603a34SBarry Smith PetscFunctionBegin; 542058ff32f7SBarry Smith if (!step) { 5421a9f9c1f6SBarry Smith PetscDrawAxis axis; 5422a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5423a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 54240910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 54250b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 54260b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 542758ff32f7SBarry Smith } 54280910c330SBarry Smith ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr); 5429e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 5430e3efe391SJed Brown { 5431e3efe391SJed Brown PetscReal *yreal; 5432e3efe391SJed Brown PetscInt i,n; 54330910c330SBarry Smith ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr); 5434785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 5435e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 54360b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 5437e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 5438e3efe391SJed Brown } 5439e3efe391SJed Brown #else 54400b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 5441e3efe391SJed Brown #endif 54420910c330SBarry Smith ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr); 5443b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 54440b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 54453923b477SBarry Smith } 5446b3603a34SBarry Smith PetscFunctionReturn(0); 5447b3603a34SBarry Smith } 5448b3603a34SBarry Smith 5449b3603a34SBarry Smith #undef __FUNCT__ 54504f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 5451ef20d060SBarry Smith /*@C 54524f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 5453ef20d060SBarry Smith in a time based line graph 5454ef20d060SBarry Smith 5455ef20d060SBarry Smith Collective on TS 5456ef20d060SBarry Smith 5457ef20d060SBarry Smith Input Parameters: 5458ef20d060SBarry Smith + ts - the TS context 5459ef20d060SBarry Smith . step - current time-step 5460ef20d060SBarry Smith . ptime - current time 5461ef20d060SBarry Smith - lg - a line graph object 5462ef20d060SBarry Smith 5463ef20d060SBarry Smith Level: intermediate 5464ef20d060SBarry Smith 5465abd5a294SJed Brown Notes: 5466abd5a294SJed Brown Only for sequential solves. 5467abd5a294SJed Brown 5468abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 5469abd5a294SJed Brown 5470abd5a294SJed Brown Options Database Keys: 54714f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 5472ef20d060SBarry Smith 5473ef20d060SBarry Smith .keywords: TS, vector, monitor, view 5474ef20d060SBarry Smith 5475abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 5476ef20d060SBarry Smith @*/ 54770910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 5478ef20d060SBarry Smith { 5479ef20d060SBarry Smith PetscErrorCode ierr; 54800b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 5481ef20d060SBarry Smith const PetscScalar *yy; 5482ef20d060SBarry Smith Vec y; 5483a9f9c1f6SBarry Smith PetscInt dim; 5484ef20d060SBarry Smith 5485ef20d060SBarry Smith PetscFunctionBegin; 5486a9f9c1f6SBarry Smith if (!step) { 5487a9f9c1f6SBarry Smith PetscDrawAxis axis; 5488a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 54891ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 54900910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 5491a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 5492a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 5493a9f9c1f6SBarry Smith } 54940910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 5495ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 54960910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 5497ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 5498e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 5499e3efe391SJed Brown { 5500e3efe391SJed Brown PetscReal *yreal; 5501e3efe391SJed Brown PetscInt i,n; 5502e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 5503785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 5504e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 55050b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 5506e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 5507e3efe391SJed Brown } 5508e3efe391SJed Brown #else 55090b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 5510e3efe391SJed Brown #endif 5511ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 5512ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 5513b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 55140b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 55153923b477SBarry Smith } 5516ef20d060SBarry Smith PetscFunctionReturn(0); 5517ef20d060SBarry Smith } 5518ef20d060SBarry Smith 5519201da799SBarry Smith #undef __FUNCT__ 5520201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 5521201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 5522201da799SBarry Smith { 5523201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 5524201da799SBarry Smith PetscReal x = ptime,y; 5525201da799SBarry Smith PetscErrorCode ierr; 5526201da799SBarry Smith PetscInt its; 5527201da799SBarry Smith 5528201da799SBarry Smith PetscFunctionBegin; 5529201da799SBarry Smith if (!n) { 5530201da799SBarry Smith PetscDrawAxis axis; 5531bbd56ea5SKarl Rupp 5532201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5533201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 5534201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 5535bbd56ea5SKarl Rupp 5536201da799SBarry Smith ctx->snes_its = 0; 5537201da799SBarry Smith } 5538201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 5539201da799SBarry Smith y = its - ctx->snes_its; 5540201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 55413fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 5542201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 5543201da799SBarry Smith } 5544201da799SBarry Smith ctx->snes_its = its; 5545201da799SBarry Smith PetscFunctionReturn(0); 5546201da799SBarry Smith } 5547201da799SBarry Smith 5548201da799SBarry Smith #undef __FUNCT__ 5549201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 5550201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 5551201da799SBarry Smith { 5552201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 5553201da799SBarry Smith PetscReal x = ptime,y; 5554201da799SBarry Smith PetscErrorCode ierr; 5555201da799SBarry Smith PetscInt its; 5556201da799SBarry Smith 5557201da799SBarry Smith PetscFunctionBegin; 5558201da799SBarry Smith if (!n) { 5559201da799SBarry Smith PetscDrawAxis axis; 5560bbd56ea5SKarl Rupp 5561201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5562201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 5563201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 5564bbd56ea5SKarl Rupp 5565201da799SBarry Smith ctx->ksp_its = 0; 5566201da799SBarry Smith } 5567201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 5568201da799SBarry Smith y = its - ctx->ksp_its; 5569201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 557099fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 5571201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 5572201da799SBarry Smith } 5573201da799SBarry Smith ctx->ksp_its = its; 5574201da799SBarry Smith PetscFunctionReturn(0); 5575201da799SBarry Smith } 5576f9c1d6abSBarry Smith 5577f9c1d6abSBarry Smith #undef __FUNCT__ 5578f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 5579f9c1d6abSBarry Smith /*@ 5580f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 5581f9c1d6abSBarry Smith 5582f9c1d6abSBarry Smith Collective on TS and Vec 5583f9c1d6abSBarry Smith 5584f9c1d6abSBarry Smith Input Parameters: 5585f9c1d6abSBarry Smith + ts - the TS context 5586f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 5587f9c1d6abSBarry Smith 5588f9c1d6abSBarry Smith Output Parameters: 5589f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 5590f9c1d6abSBarry Smith 5591f9c1d6abSBarry Smith Level: developer 5592f9c1d6abSBarry Smith 5593f9c1d6abSBarry Smith .keywords: TS, compute 5594f9c1d6abSBarry Smith 5595f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 5596f9c1d6abSBarry Smith @*/ 5597f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 5598f9c1d6abSBarry Smith { 5599f9c1d6abSBarry Smith PetscErrorCode ierr; 5600f9c1d6abSBarry Smith 5601f9c1d6abSBarry Smith PetscFunctionBegin; 5602f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5603ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 5604f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 5605f9c1d6abSBarry Smith PetscFunctionReturn(0); 5606f9c1d6abSBarry Smith } 560724655328SShri 560824655328SShri #undef __FUNCT__ 560924655328SShri #define __FUNCT__ "TSRollBack" 561024655328SShri /*@ 561124655328SShri TSRollBack - Rolls back one time step 561224655328SShri 561324655328SShri Collective on TS 561424655328SShri 561524655328SShri Input Parameter: 561624655328SShri . ts - the TS context obtained from TSCreate() 561724655328SShri 561824655328SShri Level: advanced 561924655328SShri 562024655328SShri .keywords: TS, timestep, rollback 562124655328SShri 562224655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 562324655328SShri @*/ 562424655328SShri PetscErrorCode TSRollBack(TS ts) 562524655328SShri { 562624655328SShri PetscErrorCode ierr; 562724655328SShri 562824655328SShri PetscFunctionBegin; 562924655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 563024655328SShri 563124655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 563224655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 563324655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 563424655328SShri ts->ptime = ts->ptime_prev; 563524655328SShri PetscFunctionReturn(0); 563624655328SShri } 5637aeb4809dSShri Abhyankar 5638ff22ae23SHong Zhang #undef __FUNCT__ 5639ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages" 5640ff22ae23SHong Zhang /*@ 5641ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 5642ff22ae23SHong Zhang 5643ff22ae23SHong Zhang Input Parameter: 5644ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 5645ff22ae23SHong Zhang 5646ff22ae23SHong Zhang Level: advanced 5647ff22ae23SHong Zhang 5648ff22ae23SHong Zhang .keywords: TS, getstages 5649ff22ae23SHong Zhang 5650ff22ae23SHong Zhang .seealso: TSCreate() 5651ff22ae23SHong Zhang @*/ 5652ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns, Vec **Y) 5653ff22ae23SHong Zhang { 5654ff22ae23SHong Zhang PetscErrorCode ierr; 5655ff22ae23SHong Zhang 5656ff22ae23SHong Zhang PetscFunctionBegin; 5657ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 5658ff22ae23SHong Zhang PetscValidPointer(ns,2); 5659ff22ae23SHong Zhang 5660ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 5661ff22ae23SHong Zhang else { 5662ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 5663ff22ae23SHong Zhang } 5664ff22ae23SHong Zhang PetscFunctionReturn(0); 5665ff22ae23SHong Zhang } 5666ff22ae23SHong Zhang 5667