163dd3a1aSKris Buschelman 2af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 3496e6a7aSJed Brown #include <petscdmshell.h> 41e25c274SJed Brown #include <petscdmda.h> 52d5ee99bSBarry Smith #include <petscviewer.h> 62d5ee99bSBarry Smith #include <petscdraw.h> 7d763cef2SBarry Smith 8d5ba7fb7SMatthew Knepley /* Logging support */ 9d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 10166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 11d405a339SMatthew Knepley 1249354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1349354f04SShri Abhyankar 142d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx { 152d5ee99bSBarry Smith PetscViewer viewer; 164b363babSBarry Smith PetscDrawAxis axis; 172d5ee99bSBarry Smith Vec initialsolution; 182d5ee99bSBarry Smith PetscBool showinitial; 192d5ee99bSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 202d5ee99bSBarry Smith PetscBool showtimestepandtime; 212d5ee99bSBarry Smith int color; 222d5ee99bSBarry Smith }; 232d5ee99bSBarry Smith 24bdad233fSMatthew Knepley #undef __FUNCT__ 25*fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSetFromOptions" 26*fde5950dSBarry Smith /*@C 27*fde5950dSBarry Smith TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 28*fde5950dSBarry Smith 29*fde5950dSBarry Smith Collective on TS 30*fde5950dSBarry Smith 31*fde5950dSBarry Smith Input Parameters: 32*fde5950dSBarry Smith + ts - TS object you wish to monitor 33*fde5950dSBarry Smith . name - the monitor type one is seeking 34*fde5950dSBarry Smith . help - message indicating what monitoring is done 35*fde5950dSBarry Smith . manual - manual page for the monitor 36*fde5950dSBarry Smith . monitor - the monitor function 37*fde5950dSBarry Smith - monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects 38*fde5950dSBarry Smith 39*fde5950dSBarry Smith Level: developer 40*fde5950dSBarry Smith 41*fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 42*fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 43*fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 44*fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 45*fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 46*fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 47*fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 48*fde5950dSBarry Smith @*/ 49*fde5950dSBarry Smith PetscErrorCode TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),PetscErrorCode (*monitorsetup)(TS,PetscViewer)) 50*fde5950dSBarry Smith { 51*fde5950dSBarry Smith PetscErrorCode ierr; 52*fde5950dSBarry Smith PetscViewer viewer; 53*fde5950dSBarry Smith PetscViewerFormat format; 54*fde5950dSBarry Smith PetscBool flg; 55*fde5950dSBarry Smith 56*fde5950dSBarry Smith PetscFunctionBegin; 57*fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 58*fde5950dSBarry Smith if (flg) { 59*fde5950dSBarry Smith ierr = PetscViewerSetFormat(viewer,format);CHKERRQ(ierr); 60*fde5950dSBarry Smith if (monitorsetup) { 61*fde5950dSBarry Smith ierr = (*monitorsetup)(ts,viewer);CHKERRQ(ierr); 62*fde5950dSBarry Smith } 63*fde5950dSBarry Smith ierr = TSMonitorSet(ts,monitor,viewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 64*fde5950dSBarry Smith } 65*fde5950dSBarry Smith PetscFunctionReturn(0); 66*fde5950dSBarry Smith } 67*fde5950dSBarry Smith 68*fde5950dSBarry Smith #undef __FUNCT__ 69*fde5950dSBarry Smith #define __FUNCT__ "TSAdjointMonitorSetFromOptions" 70*fde5950dSBarry Smith /*@C 71*fde5950dSBarry Smith TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 72*fde5950dSBarry Smith 73*fde5950dSBarry Smith Collective on TS 74*fde5950dSBarry Smith 75*fde5950dSBarry Smith Input Parameters: 76*fde5950dSBarry Smith + ts - TS object you wish to monitor 77*fde5950dSBarry Smith . name - the monitor type one is seeking 78*fde5950dSBarry Smith . help - message indicating what monitoring is done 79*fde5950dSBarry Smith . manual - manual page for the monitor 80*fde5950dSBarry Smith . monitor - the monitor function 81*fde5950dSBarry Smith - monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects 82*fde5950dSBarry Smith 83*fde5950dSBarry Smith Level: developer 84*fde5950dSBarry Smith 85*fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 86*fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 87*fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 88*fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 89*fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 90*fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 91*fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 92*fde5950dSBarry Smith @*/ 93*fde5950dSBarry Smith PetscErrorCode TSAdjointMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),PetscErrorCode (*monitorsetup)(TS,PetscViewer)) 94*fde5950dSBarry Smith { 95*fde5950dSBarry Smith PetscErrorCode ierr; 96*fde5950dSBarry Smith PetscViewer viewer; 97*fde5950dSBarry Smith PetscViewerFormat format; 98*fde5950dSBarry Smith PetscBool flg; 99*fde5950dSBarry Smith 100*fde5950dSBarry Smith PetscFunctionBegin; 101*fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 102*fde5950dSBarry Smith if (flg) { 103*fde5950dSBarry Smith ierr = PetscViewerSetFormat(viewer,format);CHKERRQ(ierr); 104*fde5950dSBarry Smith if (monitorsetup) { 105*fde5950dSBarry Smith ierr = (*monitorsetup)(ts,viewer);CHKERRQ(ierr); 106*fde5950dSBarry Smith } 107*fde5950dSBarry Smith ierr = TSAdjointMonitorSet(ts,monitor,viewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 108*fde5950dSBarry Smith } 109*fde5950dSBarry Smith PetscFunctionReturn(0); 110*fde5950dSBarry Smith } 111*fde5950dSBarry Smith 112*fde5950dSBarry Smith #undef __FUNCT__ 113bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions" 114bdad233fSMatthew Knepley /*@ 115bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 116bdad233fSMatthew Knepley 117bdad233fSMatthew Knepley Collective on TS 118bdad233fSMatthew Knepley 119bdad233fSMatthew Knepley Input Parameter: 120bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 121bdad233fSMatthew Knepley 122bdad233fSMatthew Knepley Options Database Keys: 1234d91e141SJed Brown + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP 124ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 1253e4cdcaaSBarry Smith . -ts_max_steps <maxsteps> - maximum number of time-steps to take 1263e4cdcaaSBarry Smith . -ts_final_time <time> - maximum time to compute to 1273e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 128a3cdaa26SBarry 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 129a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 130a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 131a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 132a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 133a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 134ef222394SBarry Smith . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 135847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 136bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 137de06c3feSJed Brown . -ts_monitor_lg_timestep - Monitor timestep size graphically 138de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 139de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 140de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 141de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 142de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 143de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 1443e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 1453e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 146*fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 147b3d3934dSBarry Smith . -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 148110eb670SHong Zhang . -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 149110eb670SHong Zhang . -ts_adjoint_monitor - print information at each adjoint time step 15053ea634cSHong Zhang - -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically 15153ea634cSHong Zhang 15291b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 153bdad233fSMatthew Knepley 154bdad233fSMatthew Knepley Level: beginner 155bdad233fSMatthew Knepley 156bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 157bdad233fSMatthew Knepley 158a313700dSBarry Smith .seealso: TSGetType() 159bdad233fSMatthew Knepley @*/ 1607087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 161bdad233fSMatthew Knepley { 162bc952696SBarry Smith PetscBool opt,flg,tflg; 163dfbe8321SBarry Smith PetscErrorCode ierr; 164eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 165089b2837SJed Brown SNES snes; 1661c3436cfSJed Brown TSAdapt adapt; 16731748224SBarry Smith PetscReal time_step; 16849354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 169d1212d36SBarry Smith char dir[16]; 1706991f827SBarry Smith const char *defaultType; 1716991f827SBarry Smith char typeName[256]; 172bdad233fSMatthew Knepley 173bdad233fSMatthew Knepley PetscFunctionBegin; 1740700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1753194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 1766991f827SBarry Smith if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 1776991f827SBarry Smith else defaultType = TSEULER; 1786991f827SBarry Smith 1796991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 1806991f827SBarry Smith ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 1816991f827SBarry Smith if (opt) { 1826991f827SBarry Smith ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 1836991f827SBarry Smith } else { 1846991f827SBarry Smith ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 1856991f827SBarry Smith } 186bdad233fSMatthew Knepley 187bdad233fSMatthew Knepley /* Handle generic TS options */ 188844594baSBarry Smith if (ts->trajectory) tflg = PETSC_TRUE; 189844594baSBarry Smith else tflg = PETSC_FALSE; 190920aabf2SBarry Smith ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 191bc952696SBarry Smith if (tflg) {ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);} 192ef222394SBarry Smith if (ts->adjoint_solve) tflg = PETSC_TRUE; 193ef222394SBarry Smith else tflg = PETSC_FALSE; 194ef222394SBarry Smith ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr); 195ef222394SBarry Smith if (flg) { 196ef222394SBarry Smith ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 197e87fd094SBarry Smith ts->adjoint_solve = tflg; 198ef222394SBarry Smith } 1990298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 2000298fd71SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 2010298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 20231748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 20331748224SBarry Smith if (flg) { 20431748224SBarry Smith ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 20531748224SBarry Smith } 20649354f04SShri 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); 20749354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 2080298fd71SBarry 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); 2090298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 2100298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 2110298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 2120298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 213bdad233fSMatthew Knepley 21456f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 21556f85f32SBarry Smith { 21656f85f32SBarry Smith PetscBool set; 21756f85f32SBarry Smith flg = PETSC_FALSE; 21856f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 21956f85f32SBarry Smith if (set) { 22056f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 22156f85f32SBarry Smith } 22256f85f32SBarry Smith } 22356f85f32SBarry Smith #endif 22456f85f32SBarry Smith 225bdad233fSMatthew Knepley /* Monitor options */ 226*fde5950dSBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr); 227*fde5950dSBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr); 228*fde5950dSBarry Smith ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr); 229*fde5950dSBarry Smith 2305180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2315180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 2325180491cSLisandro Dalcin 2334f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 234a7cc72afSBarry Smith if (opt) { 2350b039ecaSBarry Smith TSMonitorLGCtx ctx; 2363923b477SBarry Smith PetscInt howoften = 1; 237a80ad3e0SBarry Smith 2380298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 2397f52daa2SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 2404f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 241b3603a34SBarry Smith } 2424f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 243b3603a34SBarry Smith if (opt) { 2440b039ecaSBarry Smith TSMonitorLGCtx ctx; 2453923b477SBarry Smith PetscInt howoften = 1; 246b3603a34SBarry Smith 2470298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 24822d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 2494f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 250bdad233fSMatthew Knepley } 2514f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 252ef20d060SBarry Smith if (opt) { 2530b039ecaSBarry Smith TSMonitorLGCtx ctx; 2543923b477SBarry Smith PetscInt howoften = 1; 255ef20d060SBarry Smith 2560298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 25722d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 2584f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 259ef20d060SBarry Smith } 260201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 261201da799SBarry Smith if (opt) { 262201da799SBarry Smith TSMonitorLGCtx ctx; 263201da799SBarry Smith PetscInt howoften = 1; 264201da799SBarry Smith 2650298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2667f52daa2SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 267201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 268201da799SBarry Smith } 269201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 270201da799SBarry Smith if (opt) { 271201da799SBarry Smith TSMonitorLGCtx ctx; 272201da799SBarry Smith PetscInt howoften = 1; 273201da799SBarry Smith 2740298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2757f52daa2SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 276201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 277201da799SBarry Smith } 2788189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 2798189c53fSBarry Smith if (opt) { 2808189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2818189c53fSBarry Smith PetscInt howoften = 1; 2828189c53fSBarry Smith 2830298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 28422d28d08SBarry Smith ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 2858189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 2868189c53fSBarry Smith } 287ef20d060SBarry Smith opt = PETSC_FALSE; 2880dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 289a7cc72afSBarry Smith if (opt) { 29083a4ac43SBarry Smith TSMonitorDrawCtx ctx; 29183a4ac43SBarry Smith PetscInt howoften = 1; 292a80ad3e0SBarry Smith 2930298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 294ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 29583a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 296bdad233fSMatthew Knepley } 297fb1732b5SBarry Smith opt = PETSC_FALSE; 2989110b2e7SHong Zhang ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr); 2999110b2e7SHong Zhang if (opt) { 3009110b2e7SHong Zhang TSMonitorDrawCtx ctx; 3019110b2e7SHong Zhang PetscInt howoften = 1; 3029110b2e7SHong Zhang 3039110b2e7SHong Zhang ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr); 3049110b2e7SHong Zhang ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 3059110b2e7SHong Zhang ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3069110b2e7SHong Zhang } 3079110b2e7SHong Zhang opt = PETSC_FALSE; 3082d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 3092d5ee99bSBarry Smith if (opt) { 3102d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 3112d5ee99bSBarry Smith PetscReal bounds[4]; 3122d5ee99bSBarry Smith PetscInt n = 4; 3132d5ee99bSBarry Smith PetscDraw draw; 3142d5ee99bSBarry Smith 3152d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 3162d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 3172d5ee99bSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr); 3182d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 3192d5ee99bSBarry Smith ierr = PetscDrawClear(draw);CHKERRQ(ierr); 3204b363babSBarry Smith ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr); 3214b363babSBarry Smith ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 322f54adfc0SBarry Smith ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 3234b363babSBarry Smith ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr); 32407995780SPeter Brune /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */ 3252d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3262d5ee99bSBarry Smith } 3272d5ee99bSBarry Smith opt = PETSC_FALSE; 3280dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 3293a471f94SBarry Smith if (opt) { 33083a4ac43SBarry Smith TSMonitorDrawCtx ctx; 33183a4ac43SBarry Smith PetscInt howoften = 1; 3323a471f94SBarry Smith 3330298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 334ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 33583a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3363a471f94SBarry Smith } 337*fde5950dSBarry Smith 338ed81e22dSJed Brown opt = PETSC_FALSE; 33991b97e58SBarry 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); 340ed81e22dSJed Brown if (flg) { 341ed81e22dSJed Brown const char *ptr,*ptr2; 342ed81e22dSJed Brown char *filetemplate; 343ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 344ed81e22dSJed Brown /* Do some cursory validation of the input. */ 345ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 346ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 347ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 348ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 349ce94432eSBarry 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"); 350ed81e22dSJed Brown if (ptr2) break; 351ed81e22dSJed Brown } 352ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 353ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 354ed81e22dSJed Brown } 355bdad233fSMatthew Knepley 356d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 357d1212d36SBarry Smith if (flg) { 358d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 359d1212d36SBarry Smith int ray = 0; 360d1212d36SBarry Smith DMDADirection ddir; 361d1212d36SBarry Smith DM da; 362d1212d36SBarry Smith PetscMPIInt rank; 363d1212d36SBarry Smith 364ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 365d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 366d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 367ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 368d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 369d1212d36SBarry Smith 370d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 371b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 372d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 373d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 374ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 375d1212d36SBarry Smith if (!rank) { 376d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 377d1212d36SBarry Smith } 37851b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 379d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 380d1212d36SBarry Smith } 38151b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 38251b4a12fSMatthew G. Knepley if (flg) { 38351b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 38451b4a12fSMatthew G. Knepley int ray = 0; 38551b4a12fSMatthew G. Knepley DMDADirection ddir; 38651b4a12fSMatthew G. Knepley DM da; 38751b4a12fSMatthew G. Knepley PetscInt howoften = 1; 388d1212d36SBarry Smith 38951b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 39051b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 39151b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 39251b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 39351b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 3941c3436cfSJed Brown 39551b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 396b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 39751b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 39851b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 39951b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 40051b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 40151b4a12fSMatthew G. Knepley } 402a7a1495cSBarry Smith 403b3d3934dSBarry Smith ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 404b3d3934dSBarry Smith if (opt) { 405b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 406b3d3934dSBarry Smith 407b3d3934dSBarry Smith ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 408b3d3934dSBarry Smith ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 409b3d3934dSBarry Smith } 410b3d3934dSBarry Smith 411847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 412847ff0e1SMatthew G. Knepley ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr); 413847ff0e1SMatthew G. Knepley if (flg) { 414847ff0e1SMatthew G. Knepley DM dm; 415847ff0e1SMatthew G. Knepley DMTS tdm; 416847ff0e1SMatthew G. Knepley 417847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 418847ff0e1SMatthew G. Knepley ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr); 419847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 420847ff0e1SMatthew G. Knepley ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr); 421847ff0e1SMatthew G. Knepley ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 422847ff0e1SMatthew G. Knepley } 423847ff0e1SMatthew G. Knepley 4247781c08eSBarry Smith /* 4257781c08eSBarry Smith This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui 4267781c08eSBarry Smith will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin() 4277781c08eSBarry Smith */ 428552698daSJed Brown ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr); 429e55864a3SBarry Smith ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr); 430d763cef2SBarry Smith 431d763cef2SBarry Smith /* Handle specific TS options */ 432d763cef2SBarry Smith if (ts->ops->setfromoptions) { 4336991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 434d763cef2SBarry Smith } 435d763cef2SBarry Smith 436d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 4370633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr); 438d31af90fSJed Brown ierr = PetscOptionsEnd();CHKERRQ(ierr); 439d763cef2SBarry Smith 440bc952696SBarry Smith if (ts->trajectory) { 441bc952696SBarry Smith ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr); 442bc952696SBarry Smith } 443bc952696SBarry Smith 4446991f827SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4456991f827SBarry Smith if (snes) { 4466991f827SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 44749c41c37SLisandro Dalcin ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 448d763cef2SBarry Smith } 449d763cef2SBarry Smith PetscFunctionReturn(0); 450d763cef2SBarry Smith } 451d763cef2SBarry Smith 452d763cef2SBarry Smith #undef __FUNCT__ 453bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory" 454d2daff3dSHong Zhang /*@ 455bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 456d2daff3dSHong Zhang 457d2daff3dSHong Zhang Collective on TS 458d2daff3dSHong Zhang 459d2daff3dSHong Zhang Input Parameters: 460bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 461bc952696SBarry Smith 462d2daff3dSHong Zhang 463d2daff3dSHong Zhang Level: intermediate 464d2daff3dSHong Zhang 465bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve() 466d2daff3dSHong Zhang 467bc952696SBarry Smith .keywords: TS, set, checkpoint, 468d2daff3dSHong Zhang @*/ 469bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 470d2daff3dSHong Zhang { 471bc952696SBarry Smith PetscErrorCode ierr; 472bc952696SBarry Smith 473d2daff3dSHong Zhang PetscFunctionBegin; 474d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 475bc952696SBarry Smith if (!ts->trajectory) { 476bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 477920aabf2SBarry Smith ierr = TSTrajectorySetType(ts->trajectory,TSTRAJECTORYBASIC);CHKERRQ(ierr); 478648b50deSBarry Smith ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr); 479bc952696SBarry Smith } 480d2daff3dSHong Zhang PetscFunctionReturn(0); 481d2daff3dSHong Zhang } 482d2daff3dSHong Zhang 483a7a1495cSBarry Smith #undef __FUNCT__ 484a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian" 485a7a1495cSBarry Smith /*@ 486a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 487a7a1495cSBarry Smith set with TSSetRHSJacobian(). 488a7a1495cSBarry Smith 489a7a1495cSBarry Smith Collective on TS and Vec 490a7a1495cSBarry Smith 491a7a1495cSBarry Smith Input Parameters: 492316643e7SJed Brown + ts - the TS context 493a7a1495cSBarry Smith . t - current timestep 4940910c330SBarry Smith - U - input vector 495a7a1495cSBarry Smith 496a7a1495cSBarry Smith Output Parameters: 497a7a1495cSBarry Smith + A - Jacobian matrix 498a7a1495cSBarry Smith . B - optional preconditioning matrix 499a7a1495cSBarry Smith - flag - flag indicating matrix structure 500a7a1495cSBarry Smith 501a7a1495cSBarry Smith Notes: 502a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 503a7a1495cSBarry Smith is used internally within the nonlinear solvers. 504a7a1495cSBarry Smith 50594b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 506a7a1495cSBarry Smith flag parameter. 507a7a1495cSBarry Smith 508a7a1495cSBarry Smith Level: developer 509a7a1495cSBarry Smith 510a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 511a7a1495cSBarry Smith 51294b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 513a7a1495cSBarry Smith @*/ 514d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 515a7a1495cSBarry Smith { 516dfbe8321SBarry Smith PetscErrorCode ierr; 517270bf2e7SJed Brown PetscObjectState Ustate; 51824989b8cSPeter Brune DM dm; 519942e3340SBarry Smith DMTS tsdm; 52024989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 52124989b8cSPeter Brune void *ctx; 52224989b8cSPeter Brune TSIJacobian ijacobianfunc; 523b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 524a7a1495cSBarry Smith 525a7a1495cSBarry Smith PetscFunctionBegin; 5260700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5270910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5280910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 52924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 530942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 53124989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 5320298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 533b2df71adSDebojyoti Ghosh ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 53459e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 535b2df71adSDebojyoti Ghosh if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 5360e4ef248SJed Brown PetscFunctionReturn(0); 537f8ede8e7SJed Brown } 538d90be118SSean Farley 539ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 540d90be118SSean Farley 541e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 54294ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 54394ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 54494ab13aaSBarry Smith if (A != B) { 54594ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 54694ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 547e1244c69SJed Brown } 548e1244c69SJed Brown ts->rhsjacobian.shift = 0; 549e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 550e1244c69SJed Brown } 551e1244c69SJed Brown 55224989b8cSPeter Brune if (rhsjacobianfunc) { 5536cd88445SBarry Smith PetscBool missing; 55494ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 555a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 556d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 557a7a1495cSBarry Smith PetscStackPop; 55894ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 5596cd88445SBarry Smith if (A) { 5606cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 5616cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 5626cd88445SBarry Smith } 5636cd88445SBarry Smith if (B && B != A) { 5646cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 5656cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 5666cd88445SBarry Smith } 567ef66eb69SBarry Smith } else { 56894ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 56994ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 570ef66eb69SBarry Smith } 5710e4ef248SJed Brown ts->rhsjacobian.time = t; 5720910c330SBarry Smith ts->rhsjacobian.X = U; 57359e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 574a7a1495cSBarry Smith PetscFunctionReturn(0); 575a7a1495cSBarry Smith } 576a7a1495cSBarry Smith 5774a2ae208SSatish Balay #undef __FUNCT__ 5784a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 579316643e7SJed Brown /*@ 580d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 581d763cef2SBarry Smith 582316643e7SJed Brown Collective on TS and Vec 583316643e7SJed Brown 584316643e7SJed Brown Input Parameters: 585316643e7SJed Brown + ts - the TS context 586316643e7SJed Brown . t - current time 5870910c330SBarry Smith - U - state vector 588316643e7SJed Brown 589316643e7SJed Brown Output Parameter: 590316643e7SJed Brown . y - right hand side 591316643e7SJed Brown 592316643e7SJed Brown Note: 593316643e7SJed Brown Most users should not need to explicitly call this routine, as it 594316643e7SJed Brown is used internally within the nonlinear solvers. 595316643e7SJed Brown 596316643e7SJed Brown Level: developer 597316643e7SJed Brown 598316643e7SJed Brown .keywords: TS, compute 599316643e7SJed Brown 600316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 601316643e7SJed Brown @*/ 6020910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 603d763cef2SBarry Smith { 604dfbe8321SBarry Smith PetscErrorCode ierr; 60524989b8cSPeter Brune TSRHSFunction rhsfunction; 60624989b8cSPeter Brune TSIFunction ifunction; 60724989b8cSPeter Brune void *ctx; 60824989b8cSPeter Brune DM dm; 60924989b8cSPeter Brune 610d763cef2SBarry Smith PetscFunctionBegin; 6110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6120910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6130700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 61424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 61524989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 6160298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 617d763cef2SBarry Smith 618ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 619d763cef2SBarry Smith 6200910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 62124989b8cSPeter Brune if (rhsfunction) { 622d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 6230910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 624d763cef2SBarry Smith PetscStackPop; 625214bc6a2SJed Brown } else { 626214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 627b2cd27e8SJed Brown } 62844a41b28SSean Farley 6290910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 630d763cef2SBarry Smith PetscFunctionReturn(0); 631d763cef2SBarry Smith } 632d763cef2SBarry Smith 6334a2ae208SSatish Balay #undef __FUNCT__ 634ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 635ef20d060SBarry Smith /*@ 636ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 637ef20d060SBarry Smith 638ef20d060SBarry Smith Collective on TS and Vec 639ef20d060SBarry Smith 640ef20d060SBarry Smith Input Parameters: 641ef20d060SBarry Smith + ts - the TS context 642ef20d060SBarry Smith - t - current time 643ef20d060SBarry Smith 644ef20d060SBarry Smith Output Parameter: 6450910c330SBarry Smith . U - the solution 646ef20d060SBarry Smith 647ef20d060SBarry Smith Note: 648ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 649ef20d060SBarry Smith is used internally within the nonlinear solvers. 650ef20d060SBarry Smith 651ef20d060SBarry Smith Level: developer 652ef20d060SBarry Smith 653ef20d060SBarry Smith .keywords: TS, compute 654ef20d060SBarry Smith 655abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 656ef20d060SBarry Smith @*/ 6570910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 658ef20d060SBarry Smith { 659ef20d060SBarry Smith PetscErrorCode ierr; 660ef20d060SBarry Smith TSSolutionFunction solutionfunction; 661ef20d060SBarry Smith void *ctx; 662ef20d060SBarry Smith DM dm; 663ef20d060SBarry Smith 664ef20d060SBarry Smith PetscFunctionBegin; 665ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6660910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 667ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 668ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 669ef20d060SBarry Smith 670ef20d060SBarry Smith if (solutionfunction) { 6719b7cd975SBarry Smith PetscStackPush("TS user solution function"); 6720910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 673ef20d060SBarry Smith PetscStackPop; 674ef20d060SBarry Smith } 675ef20d060SBarry Smith PetscFunctionReturn(0); 676ef20d060SBarry Smith } 6779b7cd975SBarry Smith #undef __FUNCT__ 6789b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction" 6799b7cd975SBarry Smith /*@ 6809b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6819b7cd975SBarry Smith 6829b7cd975SBarry Smith Collective on TS and Vec 6839b7cd975SBarry Smith 6849b7cd975SBarry Smith Input Parameters: 6859b7cd975SBarry Smith + ts - the TS context 6869b7cd975SBarry Smith - t - current time 6879b7cd975SBarry Smith 6889b7cd975SBarry Smith Output Parameter: 6899b7cd975SBarry Smith . U - the function value 6909b7cd975SBarry Smith 6919b7cd975SBarry Smith Note: 6929b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 6939b7cd975SBarry Smith is used internally within the nonlinear solvers. 6949b7cd975SBarry Smith 6959b7cd975SBarry Smith Level: developer 6969b7cd975SBarry Smith 6979b7cd975SBarry Smith .keywords: TS, compute 6989b7cd975SBarry Smith 6999b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 7009b7cd975SBarry Smith @*/ 7019b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 7029b7cd975SBarry Smith { 7039b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 7049b7cd975SBarry Smith void *ctx; 7059b7cd975SBarry Smith DM dm; 7069b7cd975SBarry Smith 7079b7cd975SBarry Smith PetscFunctionBegin; 7089b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7099b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7109b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 7119b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 7129b7cd975SBarry Smith 7139b7cd975SBarry Smith if (forcing) { 7149b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 7159b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 7169b7cd975SBarry Smith PetscStackPop; 7179b7cd975SBarry Smith } 7189b7cd975SBarry Smith PetscFunctionReturn(0); 7199b7cd975SBarry Smith } 720ef20d060SBarry Smith 721ef20d060SBarry Smith #undef __FUNCT__ 722214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 723214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 724214bc6a2SJed Brown { 7252dd45cf8SJed Brown Vec F; 726214bc6a2SJed Brown PetscErrorCode ierr; 727214bc6a2SJed Brown 728214bc6a2SJed Brown PetscFunctionBegin; 7290298fd71SBarry Smith *Frhs = NULL; 7300298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 731214bc6a2SJed Brown if (!ts->Frhs) { 7322dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 733214bc6a2SJed Brown } 734214bc6a2SJed Brown *Frhs = ts->Frhs; 735214bc6a2SJed Brown PetscFunctionReturn(0); 736214bc6a2SJed Brown } 737214bc6a2SJed Brown 738214bc6a2SJed Brown #undef __FUNCT__ 739214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 740214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 741214bc6a2SJed Brown { 742214bc6a2SJed Brown Mat A,B; 7432dd45cf8SJed Brown PetscErrorCode ierr; 744214bc6a2SJed Brown 745214bc6a2SJed Brown PetscFunctionBegin; 746c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 747c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 7480298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 749214bc6a2SJed Brown if (Arhs) { 750214bc6a2SJed Brown if (!ts->Arhs) { 751214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 752214bc6a2SJed Brown } 753214bc6a2SJed Brown *Arhs = ts->Arhs; 754214bc6a2SJed Brown } 755214bc6a2SJed Brown if (Brhs) { 756214bc6a2SJed Brown if (!ts->Brhs) { 757bdb70873SJed Brown if (A != B) { 758214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 759bdb70873SJed Brown } else { 760bdb70873SJed Brown ts->Brhs = ts->Arhs; 761bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 762bdb70873SJed Brown } 763214bc6a2SJed Brown } 764214bc6a2SJed Brown *Brhs = ts->Brhs; 765214bc6a2SJed Brown } 766214bc6a2SJed Brown PetscFunctionReturn(0); 767214bc6a2SJed Brown } 768214bc6a2SJed Brown 769214bc6a2SJed Brown #undef __FUNCT__ 770316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 771316643e7SJed Brown /*@ 7720910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 773316643e7SJed Brown 774316643e7SJed Brown Collective on TS and Vec 775316643e7SJed Brown 776316643e7SJed Brown Input Parameters: 777316643e7SJed Brown + ts - the TS context 778316643e7SJed Brown . t - current time 7790910c330SBarry Smith . U - state vector 7800910c330SBarry Smith . Udot - time derivative of state vector 781214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 782316643e7SJed Brown 783316643e7SJed Brown Output Parameter: 784316643e7SJed Brown . Y - right hand side 785316643e7SJed Brown 786316643e7SJed Brown Note: 787316643e7SJed Brown Most users should not need to explicitly call this routine, as it 788316643e7SJed Brown is used internally within the nonlinear solvers. 789316643e7SJed Brown 790316643e7SJed Brown If the user did did not write their equations in implicit form, this 791316643e7SJed Brown function recasts them in implicit form. 792316643e7SJed Brown 793316643e7SJed Brown Level: developer 794316643e7SJed Brown 795316643e7SJed Brown .keywords: TS, compute 796316643e7SJed Brown 797316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 798316643e7SJed Brown @*/ 7990910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 800316643e7SJed Brown { 801316643e7SJed Brown PetscErrorCode ierr; 80224989b8cSPeter Brune TSIFunction ifunction; 80324989b8cSPeter Brune TSRHSFunction rhsfunction; 80424989b8cSPeter Brune void *ctx; 80524989b8cSPeter Brune DM dm; 806316643e7SJed Brown 807316643e7SJed Brown PetscFunctionBegin; 8080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8090910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8100910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 8110700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 812316643e7SJed Brown 81324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 81424989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 8150298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 81624989b8cSPeter Brune 817ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 818d90be118SSean Farley 8190910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 82024989b8cSPeter Brune if (ifunction) { 821316643e7SJed Brown PetscStackPush("TS user implicit function"); 8220910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 823316643e7SJed Brown PetscStackPop; 824214bc6a2SJed Brown } 825214bc6a2SJed Brown if (imex) { 82624989b8cSPeter Brune if (!ifunction) { 8270910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 8282dd45cf8SJed Brown } 82924989b8cSPeter Brune } else if (rhsfunction) { 83024989b8cSPeter Brune if (ifunction) { 831214bc6a2SJed Brown Vec Frhs; 832214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 8330910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 834214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 8352dd45cf8SJed Brown } else { 8360910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 8370910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 838316643e7SJed Brown } 8394a6899ffSJed Brown } 8400910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 841316643e7SJed Brown PetscFunctionReturn(0); 842316643e7SJed Brown } 843316643e7SJed Brown 844316643e7SJed Brown #undef __FUNCT__ 845316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 846316643e7SJed Brown /*@ 847316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 848316643e7SJed Brown 849316643e7SJed Brown Collective on TS and Vec 850316643e7SJed Brown 851316643e7SJed Brown Input 852316643e7SJed Brown Input Parameters: 853316643e7SJed Brown + ts - the TS context 854316643e7SJed Brown . t - current timestep 8550910c330SBarry Smith . U - state vector 8560910c330SBarry Smith . Udot - time derivative of state vector 857214bc6a2SJed Brown . shift - shift to apply, see note below 858214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 859316643e7SJed Brown 860316643e7SJed Brown Output Parameters: 861316643e7SJed Brown + A - Jacobian matrix 862316643e7SJed Brown . B - optional preconditioning matrix 863316643e7SJed Brown - flag - flag indicating matrix structure 864316643e7SJed Brown 865316643e7SJed Brown Notes: 8660910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 867316643e7SJed Brown 8680910c330SBarry Smith dF/dU + shift*dF/dUdot 869316643e7SJed Brown 870316643e7SJed Brown Most users should not need to explicitly call this routine, as it 871316643e7SJed Brown is used internally within the nonlinear solvers. 872316643e7SJed Brown 873316643e7SJed Brown Level: developer 874316643e7SJed Brown 875316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 876316643e7SJed Brown 877316643e7SJed Brown .seealso: TSSetIJacobian() 87863495f91SJed Brown @*/ 879d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 880316643e7SJed Brown { 881316643e7SJed Brown PetscErrorCode ierr; 88224989b8cSPeter Brune TSIJacobian ijacobian; 88324989b8cSPeter Brune TSRHSJacobian rhsjacobian; 88424989b8cSPeter Brune DM dm; 88524989b8cSPeter Brune void *ctx; 886316643e7SJed Brown 887316643e7SJed Brown PetscFunctionBegin; 8880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8890910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8900910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 891316643e7SJed Brown PetscValidPointer(A,6); 89294ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 893316643e7SJed Brown PetscValidPointer(B,7); 89494ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 89524989b8cSPeter Brune 89624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 89724989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 8980298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 89924989b8cSPeter Brune 900ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 901316643e7SJed Brown 90294ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 90324989b8cSPeter Brune if (ijacobian) { 9046cd88445SBarry Smith PetscBool missing; 905316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 906d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 907316643e7SJed Brown PetscStackPop; 9086cd88445SBarry Smith if (A) { 9096cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 9106cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 9116cd88445SBarry Smith } 9126cd88445SBarry Smith if (B && B != A) { 9136cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 9146cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 9156cd88445SBarry Smith } 9164a6899ffSJed Brown } 917214bc6a2SJed Brown if (imex) { 918b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 91994ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 92094ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 92194ab13aaSBarry Smith if (A != B) { 92294ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 92394ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 924214bc6a2SJed Brown } 925214bc6a2SJed Brown } 926214bc6a2SJed Brown } else { 927e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 928e1244c69SJed Brown if (rhsjacobian) { 929bd373395SBarry Smith if (ijacobian) { 930214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 931bd373395SBarry Smith } else { 932bd373395SBarry Smith ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr); 933214bc6a2SJed Brown } 934d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 935e1244c69SJed Brown } 93694ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 937e1244c69SJed Brown ts->rhsjacobian.scale = -1; 938e1244c69SJed Brown ts->rhsjacobian.shift = shift; 93994ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 94094ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 94194ab13aaSBarry Smith if (A != B) { 94294ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 94394ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 944316643e7SJed Brown } 945e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 946e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 947e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 94894ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 94994ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 95094ab13aaSBarry Smith if (A != B) { 95194ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 95294ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 953214bc6a2SJed Brown } 954316643e7SJed Brown } 95594ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 95694ab13aaSBarry Smith if (A != B) { 95794ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 958316643e7SJed Brown } 959316643e7SJed Brown } 960316643e7SJed Brown } 96194ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 962316643e7SJed Brown PetscFunctionReturn(0); 963316643e7SJed Brown } 964316643e7SJed Brown 965316643e7SJed Brown #undef __FUNCT__ 9664a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 967d763cef2SBarry Smith /*@C 968d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 969b5abc632SBarry Smith where U_t = G(t,u). 970d763cef2SBarry Smith 9713f9fe445SBarry Smith Logically Collective on TS 972d763cef2SBarry Smith 973d763cef2SBarry Smith Input Parameters: 974d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 9750298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 976d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 977d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 9780298fd71SBarry Smith function evaluation routine (may be NULL) 979d763cef2SBarry Smith 980d763cef2SBarry Smith Calling sequence of func: 98187828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 982d763cef2SBarry Smith 983d763cef2SBarry Smith + t - current timestep 984d763cef2SBarry Smith . u - input vector 985d763cef2SBarry Smith . F - function vector 986d763cef2SBarry Smith - ctx - [optional] user-defined function context 987d763cef2SBarry Smith 988d763cef2SBarry Smith Level: beginner 989d763cef2SBarry Smith 9902bbac0d3SBarry Smith Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 9912bbac0d3SBarry Smith 992d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 993d763cef2SBarry Smith 994ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 995d763cef2SBarry Smith @*/ 996089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 997d763cef2SBarry Smith { 998089b2837SJed Brown PetscErrorCode ierr; 999089b2837SJed Brown SNES snes; 10000298fd71SBarry Smith Vec ralloc = NULL; 100124989b8cSPeter Brune DM dm; 1002d763cef2SBarry Smith 1003089b2837SJed Brown PetscFunctionBegin; 10040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1005ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 100624989b8cSPeter Brune 100724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 100824989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 1009089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1010e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 1011e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 1012e856ceecSJed Brown r = ralloc; 1013e856ceecSJed Brown } 1014089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 1015e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1016d763cef2SBarry Smith PetscFunctionReturn(0); 1017d763cef2SBarry Smith } 1018d763cef2SBarry Smith 10194a2ae208SSatish Balay #undef __FUNCT__ 1020ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 1021ef20d060SBarry Smith /*@C 1022abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1023ef20d060SBarry Smith 1024ef20d060SBarry Smith Logically Collective on TS 1025ef20d060SBarry Smith 1026ef20d060SBarry Smith Input Parameters: 1027ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1028ef20d060SBarry Smith . f - routine for evaluating the solution 1029ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 10300298fd71SBarry Smith function evaluation routine (may be NULL) 1031ef20d060SBarry Smith 1032ef20d060SBarry Smith Calling sequence of func: 1033ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 1034ef20d060SBarry Smith 1035ef20d060SBarry Smith + t - current timestep 1036ef20d060SBarry Smith . u - output vector 1037ef20d060SBarry Smith - ctx - [optional] user-defined function context 1038ef20d060SBarry Smith 1039abd5a294SJed Brown Notes: 1040abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1041abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1042abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1043abd5a294SJed Brown 10444f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1045abd5a294SJed Brown 1046ef20d060SBarry Smith Level: beginner 1047ef20d060SBarry Smith 1048ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1049ef20d060SBarry Smith 10509b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 1051ef20d060SBarry Smith @*/ 1052ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1053ef20d060SBarry Smith { 1054ef20d060SBarry Smith PetscErrorCode ierr; 1055ef20d060SBarry Smith DM dm; 1056ef20d060SBarry Smith 1057ef20d060SBarry Smith PetscFunctionBegin; 1058ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1059ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1060ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 1061ef20d060SBarry Smith PetscFunctionReturn(0); 1062ef20d060SBarry Smith } 1063ef20d060SBarry Smith 1064ef20d060SBarry Smith #undef __FUNCT__ 10659b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction" 10669b7cd975SBarry Smith /*@C 10679b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 10689b7cd975SBarry Smith 10699b7cd975SBarry Smith Logically Collective on TS 10709b7cd975SBarry Smith 10719b7cd975SBarry Smith Input Parameters: 10729b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 10739b7cd975SBarry Smith . f - routine for evaluating the forcing function 10749b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 10750298fd71SBarry Smith function evaluation routine (may be NULL) 10769b7cd975SBarry Smith 10779b7cd975SBarry Smith Calling sequence of func: 10789b7cd975SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 10799b7cd975SBarry Smith 10809b7cd975SBarry Smith + t - current timestep 10819b7cd975SBarry Smith . u - output vector 10829b7cd975SBarry Smith - ctx - [optional] user-defined function context 10839b7cd975SBarry Smith 10849b7cd975SBarry Smith Notes: 10859b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 10869b7cd975SBarry Smith create closed-form solutions with a non-physical forcing term. 10879b7cd975SBarry Smith 10889b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 10899b7cd975SBarry Smith 10909b7cd975SBarry Smith Level: beginner 10919b7cd975SBarry Smith 10929b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 10939b7cd975SBarry Smith 10949b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 10959b7cd975SBarry Smith @*/ 10969b7cd975SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 10979b7cd975SBarry Smith { 10989b7cd975SBarry Smith PetscErrorCode ierr; 10999b7cd975SBarry Smith DM dm; 11009b7cd975SBarry Smith 11019b7cd975SBarry Smith PetscFunctionBegin; 11029b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11039b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 11049b7cd975SBarry Smith ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr); 11059b7cd975SBarry Smith PetscFunctionReturn(0); 11069b7cd975SBarry Smith } 11079b7cd975SBarry Smith 11089b7cd975SBarry Smith #undef __FUNCT__ 11094a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 1110d763cef2SBarry Smith /*@C 1111f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1112b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1113d763cef2SBarry Smith 11143f9fe445SBarry Smith Logically Collective on TS 1115d763cef2SBarry Smith 1116d763cef2SBarry Smith Input Parameters: 1117d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1118e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1119e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1120d763cef2SBarry Smith . f - the Jacobian evaluation routine 1121d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 11220298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1123d763cef2SBarry Smith 1124f7ab8db6SBarry Smith Calling sequence of f: 1125ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1126d763cef2SBarry Smith 1127d763cef2SBarry Smith + t - current timestep 1128d763cef2SBarry Smith . u - input vector 1129e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1130e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1131d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1132d763cef2SBarry Smith 11336cd88445SBarry Smith Notes: 11346cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 11356cd88445SBarry Smith 11366cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1137ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1138d763cef2SBarry Smith 1139d763cef2SBarry Smith Level: beginner 1140d763cef2SBarry Smith 1141d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1142d763cef2SBarry Smith 1143ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1144d763cef2SBarry Smith 1145d763cef2SBarry Smith @*/ 1146e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1147d763cef2SBarry Smith { 1148277b19d0SLisandro Dalcin PetscErrorCode ierr; 1149089b2837SJed Brown SNES snes; 115024989b8cSPeter Brune DM dm; 115124989b8cSPeter Brune TSIJacobian ijacobian; 1152277b19d0SLisandro Dalcin 1153d763cef2SBarry Smith PetscFunctionBegin; 11540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1155e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1156e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1157e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1158e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1159d763cef2SBarry Smith 116024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 116124989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1162e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1163e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1164e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1165e1244c69SJed Brown } 11660298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1167089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11685f659677SPeter Brune if (!ijacobian) { 1169e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 11700e4ef248SJed Brown } 1171e5d3d808SBarry Smith if (Amat) { 1172e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 11730e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1174bbd56ea5SKarl Rupp 1175e5d3d808SBarry Smith ts->Arhs = Amat; 11760e4ef248SJed Brown } 1177e5d3d808SBarry Smith if (Pmat) { 1178e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 11790e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1180bbd56ea5SKarl Rupp 1181e5d3d808SBarry Smith ts->Brhs = Pmat; 11820e4ef248SJed Brown } 1183d763cef2SBarry Smith PetscFunctionReturn(0); 1184d763cef2SBarry Smith } 1185d763cef2SBarry Smith 1186316643e7SJed Brown 1187316643e7SJed Brown #undef __FUNCT__ 1188316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 1189316643e7SJed Brown /*@C 1190b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1191316643e7SJed Brown 11923f9fe445SBarry Smith Logically Collective on TS 1193316643e7SJed Brown 1194316643e7SJed Brown Input Parameters: 1195316643e7SJed Brown + ts - the TS context obtained from TSCreate() 11960298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1197316643e7SJed Brown . f - the function evaluation routine 11980298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1199316643e7SJed Brown 1200316643e7SJed Brown Calling sequence of f: 1201316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1202316643e7SJed Brown 1203316643e7SJed Brown + t - time at step/stage being solved 1204316643e7SJed Brown . u - state vector 1205316643e7SJed Brown . u_t - time derivative of state vector 1206316643e7SJed Brown . F - function vector 1207316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1208316643e7SJed Brown 1209316643e7SJed Brown Important: 12102bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1211316643e7SJed Brown 1212316643e7SJed Brown Level: beginner 1213316643e7SJed Brown 1214316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1215316643e7SJed Brown 1216d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1217316643e7SJed Brown @*/ 1218089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 1219316643e7SJed Brown { 1220089b2837SJed Brown PetscErrorCode ierr; 1221089b2837SJed Brown SNES snes; 12220298fd71SBarry Smith Vec resalloc = NULL; 122324989b8cSPeter Brune DM dm; 1224316643e7SJed Brown 1225316643e7SJed Brown PetscFunctionBegin; 12260700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1227ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 122824989b8cSPeter Brune 122924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 123024989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 123124989b8cSPeter Brune 1232089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1233e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 1234e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 1235e856ceecSJed Brown res = resalloc; 1236e856ceecSJed Brown } 1237089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 1238e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 1239089b2837SJed Brown PetscFunctionReturn(0); 1240089b2837SJed Brown } 1241089b2837SJed Brown 1242089b2837SJed Brown #undef __FUNCT__ 1243089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 1244089b2837SJed Brown /*@C 1245089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1246089b2837SJed Brown 1247089b2837SJed Brown Not Collective 1248089b2837SJed Brown 1249089b2837SJed Brown Input Parameter: 1250089b2837SJed Brown . ts - the TS context 1251089b2837SJed Brown 1252089b2837SJed Brown Output Parameter: 12530298fd71SBarry Smith + r - vector to hold residual (or NULL) 12540298fd71SBarry Smith . func - the function to compute residual (or NULL) 12550298fd71SBarry Smith - ctx - the function context (or NULL) 1256089b2837SJed Brown 1257089b2837SJed Brown Level: advanced 1258089b2837SJed Brown 1259089b2837SJed Brown .keywords: TS, nonlinear, get, function 1260089b2837SJed Brown 1261089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1262089b2837SJed Brown @*/ 1263089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1264089b2837SJed Brown { 1265089b2837SJed Brown PetscErrorCode ierr; 1266089b2837SJed Brown SNES snes; 126724989b8cSPeter Brune DM dm; 1268089b2837SJed Brown 1269089b2837SJed Brown PetscFunctionBegin; 1270089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1271089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12720298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 127324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 127424989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1275089b2837SJed Brown PetscFunctionReturn(0); 1276089b2837SJed Brown } 1277089b2837SJed Brown 1278089b2837SJed Brown #undef __FUNCT__ 1279089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 1280089b2837SJed Brown /*@C 1281089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1282089b2837SJed Brown 1283089b2837SJed Brown Not Collective 1284089b2837SJed Brown 1285089b2837SJed Brown Input Parameter: 1286089b2837SJed Brown . ts - the TS context 1287089b2837SJed Brown 1288089b2837SJed Brown Output Parameter: 12890298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 12900298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 12910298fd71SBarry Smith - ctx - the function context (or NULL) 1292089b2837SJed Brown 1293089b2837SJed Brown Level: advanced 1294089b2837SJed Brown 1295089b2837SJed Brown .keywords: TS, nonlinear, get, function 1296089b2837SJed Brown 12972bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1298089b2837SJed Brown @*/ 1299089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1300089b2837SJed Brown { 1301089b2837SJed Brown PetscErrorCode ierr; 1302089b2837SJed Brown SNES snes; 130324989b8cSPeter Brune DM dm; 1304089b2837SJed Brown 1305089b2837SJed Brown PetscFunctionBegin; 1306089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1307089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13080298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 130924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 131024989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1311316643e7SJed Brown PetscFunctionReturn(0); 1312316643e7SJed Brown } 1313316643e7SJed Brown 1314316643e7SJed Brown #undef __FUNCT__ 1315316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 1316316643e7SJed Brown /*@C 1317a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1318ae8867d6SBarry Smith provided with TSSetIFunction(). 1319316643e7SJed Brown 13203f9fe445SBarry Smith Logically Collective on TS 1321316643e7SJed Brown 1322316643e7SJed Brown Input Parameters: 1323316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1324e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1325e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1326316643e7SJed Brown . f - the Jacobian evaluation routine 13270298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1328316643e7SJed Brown 1329316643e7SJed Brown Calling sequence of f: 1330ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1331316643e7SJed Brown 1332316643e7SJed Brown + t - time at step/stage being solved 13331b4a444bSJed Brown . U - state vector 13341b4a444bSJed Brown . U_t - time derivative of state vector 1335316643e7SJed Brown . a - shift 1336e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1337e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1338316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1339316643e7SJed Brown 1340316643e7SJed Brown Notes: 1341e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1342316643e7SJed Brown 1343895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1344895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1345895c21f2SBarry Smith 1346a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1347b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1348a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1349a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1350a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1351a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1352a4f0a591SBarry Smith 13536cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13546cd88445SBarry Smith 13556cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1356ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1357ca5f011dSBarry Smith 1358316643e7SJed Brown Level: beginner 1359316643e7SJed Brown 1360316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1361316643e7SJed Brown 1362ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1363316643e7SJed Brown 1364316643e7SJed Brown @*/ 1365e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1366316643e7SJed Brown { 1367316643e7SJed Brown PetscErrorCode ierr; 1368089b2837SJed Brown SNES snes; 136924989b8cSPeter Brune DM dm; 1370316643e7SJed Brown 1371316643e7SJed Brown PetscFunctionBegin; 13720700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1373e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1374e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1375e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1376e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 137724989b8cSPeter Brune 137824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 137924989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 138024989b8cSPeter Brune 1381089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1382e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1383316643e7SJed Brown PetscFunctionReturn(0); 1384316643e7SJed Brown } 1385316643e7SJed Brown 13864a2ae208SSatish Balay #undef __FUNCT__ 1387e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse" 1388e1244c69SJed Brown /*@ 1389e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1390e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1391e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1392e1244c69SJed Brown not been changed by the TS. 1393e1244c69SJed Brown 1394e1244c69SJed Brown Logically Collective 1395e1244c69SJed Brown 1396e1244c69SJed Brown Input Arguments: 1397e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1398e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1399e1244c69SJed Brown 1400e1244c69SJed Brown Level: intermediate 1401e1244c69SJed Brown 1402e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1403e1244c69SJed Brown @*/ 1404e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1405e1244c69SJed Brown { 1406e1244c69SJed Brown PetscFunctionBegin; 1407e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1408e1244c69SJed Brown PetscFunctionReturn(0); 1409e1244c69SJed Brown } 1410e1244c69SJed Brown 1411e1244c69SJed Brown #undef __FUNCT__ 141255849f57SBarry Smith #define __FUNCT__ "TSLoad" 141355849f57SBarry Smith /*@C 141455849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 141555849f57SBarry Smith 141655849f57SBarry Smith Collective on PetscViewer 141755849f57SBarry Smith 141855849f57SBarry Smith Input Parameters: 141955849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 142055849f57SBarry Smith some related function before a call to TSLoad(). 142155849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 142255849f57SBarry Smith 142355849f57SBarry Smith Level: intermediate 142455849f57SBarry Smith 142555849f57SBarry Smith Notes: 142655849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 142755849f57SBarry Smith 142855849f57SBarry Smith Notes for advanced users: 142955849f57SBarry Smith Most users should not need to know the details of the binary storage 143055849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 143155849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 143255849f57SBarry Smith format is 143355849f57SBarry Smith .vb 143455849f57SBarry Smith has not yet been determined 143555849f57SBarry Smith .ve 143655849f57SBarry Smith 143755849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 143855849f57SBarry Smith @*/ 1439f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 144055849f57SBarry Smith { 144155849f57SBarry Smith PetscErrorCode ierr; 144255849f57SBarry Smith PetscBool isbinary; 144355849f57SBarry Smith PetscInt classid; 144455849f57SBarry Smith char type[256]; 14452d53ad75SBarry Smith DMTS sdm; 1446ad6bc421SBarry Smith DM dm; 144755849f57SBarry Smith 144855849f57SBarry Smith PetscFunctionBegin; 1449f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 145055849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 145155849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 145255849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 145355849f57SBarry Smith 1454060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1455ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1456060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1457f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1458f2c2a1b9SBarry Smith if (ts->ops->load) { 1459f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1460f2c2a1b9SBarry Smith } 1461ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1462ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1463ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1464f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1465f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 14662d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 14672d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 146855849f57SBarry Smith PetscFunctionReturn(0); 146955849f57SBarry Smith } 147055849f57SBarry Smith 14719804daf3SBarry Smith #include <petscdraw.h> 1472e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1473e04113cfSBarry Smith #include <petscviewersaws.h> 1474f05ece33SBarry Smith #endif 147555849f57SBarry Smith #undef __FUNCT__ 14764a2ae208SSatish Balay #define __FUNCT__ "TSView" 14777e2c5f70SBarry Smith /*@C 1478d763cef2SBarry Smith TSView - Prints the TS data structure. 1479d763cef2SBarry Smith 14804c49b128SBarry Smith Collective on TS 1481d763cef2SBarry Smith 1482d763cef2SBarry Smith Input Parameters: 1483d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1484d763cef2SBarry Smith - viewer - visualization context 1485d763cef2SBarry Smith 1486d763cef2SBarry Smith Options Database Key: 1487d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1488d763cef2SBarry Smith 1489d763cef2SBarry Smith Notes: 1490d763cef2SBarry Smith The available visualization contexts include 1491b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1492b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1493d763cef2SBarry Smith output where only the first processor opens 1494d763cef2SBarry Smith the file. All other processors send their 1495d763cef2SBarry Smith data to the first processor to print. 1496d763cef2SBarry Smith 1497d763cef2SBarry Smith The user can open an alternative visualization context with 1498b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1499d763cef2SBarry Smith 1500d763cef2SBarry Smith Level: beginner 1501d763cef2SBarry Smith 1502d763cef2SBarry Smith .keywords: TS, timestep, view 1503d763cef2SBarry Smith 1504b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1505d763cef2SBarry Smith @*/ 15067087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1507d763cef2SBarry Smith { 1508dfbe8321SBarry Smith PetscErrorCode ierr; 150919fd82e9SBarry Smith TSType type; 15102b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 15112d53ad75SBarry Smith DMTS sdm; 1512e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1513536b137fSBarry Smith PetscBool issaws; 1514f05ece33SBarry Smith #endif 1515d763cef2SBarry Smith 1516d763cef2SBarry Smith PetscFunctionBegin; 15170700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15183050cee2SBarry Smith if (!viewer) { 1519ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 15203050cee2SBarry Smith } 15210700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1522c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1523fd16b177SBarry Smith 1524251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1525251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 152655849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 15272b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1528e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1529536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1530f05ece33SBarry Smith #endif 153132077d6dSBarry Smith if (iascii) { 1532dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 153377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 15347c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1535d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 15365ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1537c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1538d763cef2SBarry Smith } 15395ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1540193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 15412d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 15422d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1543d52bd9f3SBarry Smith if (ts->ops->view) { 1544d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1545d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1546d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1547d52bd9f3SBarry Smith } 15480f5bd95cSBarry Smith } else if (isstring) { 1549a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1550b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 155155849f57SBarry Smith } else if (isbinary) { 155255849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 155355849f57SBarry Smith MPI_Comm comm; 155455849f57SBarry Smith PetscMPIInt rank; 155555849f57SBarry Smith char type[256]; 155655849f57SBarry Smith 155755849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 155855849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 155955849f57SBarry Smith if (!rank) { 156055849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 156155849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 156255849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 156355849f57SBarry Smith } 156455849f57SBarry Smith if (ts->ops->view) { 156555849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 156655849f57SBarry Smith } 1567f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1568f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 15692d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 15702d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 15712b0a91c0SBarry Smith } else if (isdraw) { 15722b0a91c0SBarry Smith PetscDraw draw; 15732b0a91c0SBarry Smith char str[36]; 157489fd9fafSBarry Smith PetscReal x,y,bottom,h; 15752b0a91c0SBarry Smith 15762b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 15772b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 15782b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 15792b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 158051fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 158189fd9fafSBarry Smith bottom = y - h; 15822b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 15832b0a91c0SBarry Smith if (ts->ops->view) { 15842b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 15852b0a91c0SBarry Smith } 15862b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1587e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1588536b137fSBarry Smith } else if (issaws) { 1589d45a07a7SBarry Smith PetscMPIInt rank; 15902657e9d9SBarry Smith const char *name; 15912657e9d9SBarry Smith 15922657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1593d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1594d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1595d45a07a7SBarry Smith char dir[1024]; 1596d45a07a7SBarry Smith 1597e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1598a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 15992657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 16002657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 16012657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1602d763cef2SBarry Smith } 16030acecf5bSBarry Smith if (ts->ops->view) { 16040acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 16050acecf5bSBarry Smith } 1606f05ece33SBarry Smith #endif 1607f05ece33SBarry Smith } 1608f05ece33SBarry Smith 1609b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1610251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1611b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1612d763cef2SBarry Smith PetscFunctionReturn(0); 1613d763cef2SBarry Smith } 1614d763cef2SBarry Smith 1615d763cef2SBarry Smith 16164a2ae208SSatish Balay #undef __FUNCT__ 16174a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1618b07ff414SBarry Smith /*@ 1619d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1620d763cef2SBarry Smith the timesteppers. 1621d763cef2SBarry Smith 16223f9fe445SBarry Smith Logically Collective on TS 1623d763cef2SBarry Smith 1624d763cef2SBarry Smith Input Parameters: 1625d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1626d763cef2SBarry Smith - usrP - optional user context 1627d763cef2SBarry Smith 1628daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 1629daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1630daf670e6SBarry Smith 1631d763cef2SBarry Smith Level: intermediate 1632d763cef2SBarry Smith 1633d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1634d763cef2SBarry Smith 1635d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1636d763cef2SBarry Smith @*/ 16377087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1638d763cef2SBarry Smith { 1639d763cef2SBarry Smith PetscFunctionBegin; 16400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1641d763cef2SBarry Smith ts->user = usrP; 1642d763cef2SBarry Smith PetscFunctionReturn(0); 1643d763cef2SBarry Smith } 1644d763cef2SBarry Smith 16454a2ae208SSatish Balay #undef __FUNCT__ 16464a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1647b07ff414SBarry Smith /*@ 1648d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1649d763cef2SBarry Smith timestepper. 1650d763cef2SBarry Smith 1651d763cef2SBarry Smith Not Collective 1652d763cef2SBarry Smith 1653d763cef2SBarry Smith Input Parameter: 1654d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1655d763cef2SBarry Smith 1656d763cef2SBarry Smith Output Parameter: 1657d763cef2SBarry Smith . usrP - user context 1658d763cef2SBarry Smith 1659daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 1660daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1661daf670e6SBarry Smith 1662d763cef2SBarry Smith Level: intermediate 1663d763cef2SBarry Smith 1664d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1665d763cef2SBarry Smith 1666d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1667d763cef2SBarry Smith @*/ 1668e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1669d763cef2SBarry Smith { 1670d763cef2SBarry Smith PetscFunctionBegin; 16710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1672e71120c6SJed Brown *(void**)usrP = ts->user; 1673d763cef2SBarry Smith PetscFunctionReturn(0); 1674d763cef2SBarry Smith } 1675d763cef2SBarry Smith 16764a2ae208SSatish Balay #undef __FUNCT__ 16774a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1678d763cef2SBarry Smith /*@ 1679b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1680d763cef2SBarry Smith 1681d763cef2SBarry Smith Not Collective 1682d763cef2SBarry Smith 1683d763cef2SBarry Smith Input Parameter: 1684d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1685d763cef2SBarry Smith 1686d763cef2SBarry Smith Output Parameter: 1687b8123daeSJed Brown . iter - number of steps completed so far 1688d763cef2SBarry Smith 1689d763cef2SBarry Smith Level: intermediate 1690d763cef2SBarry Smith 1691d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 16929be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 1693d763cef2SBarry Smith @*/ 16947087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 1695d763cef2SBarry Smith { 1696d763cef2SBarry Smith PetscFunctionBegin; 16970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 16984482741eSBarry Smith PetscValidIntPointer(iter,2); 1699d763cef2SBarry Smith *iter = ts->steps; 1700d763cef2SBarry Smith PetscFunctionReturn(0); 1701d763cef2SBarry Smith } 1702d763cef2SBarry Smith 17034a2ae208SSatish Balay #undef __FUNCT__ 17044a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1705d763cef2SBarry Smith /*@ 1706d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1707d763cef2SBarry Smith as well as the initial time. 1708d763cef2SBarry Smith 17093f9fe445SBarry Smith Logically Collective on TS 1710d763cef2SBarry Smith 1711d763cef2SBarry Smith Input Parameters: 1712d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1713d763cef2SBarry Smith . initial_time - the initial time 1714d763cef2SBarry Smith - time_step - the size of the timestep 1715d763cef2SBarry Smith 1716d763cef2SBarry Smith Level: intermediate 1717d763cef2SBarry Smith 1718d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1719d763cef2SBarry Smith 1720d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1721d763cef2SBarry Smith @*/ 17227087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1723d763cef2SBarry Smith { 1724e144a568SJed Brown PetscErrorCode ierr; 1725e144a568SJed Brown 1726d763cef2SBarry Smith PetscFunctionBegin; 17270700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1728e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1729d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1730d763cef2SBarry Smith PetscFunctionReturn(0); 1731d763cef2SBarry Smith } 1732d763cef2SBarry Smith 17334a2ae208SSatish Balay #undef __FUNCT__ 17344a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1735d763cef2SBarry Smith /*@ 1736d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1737d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1738d763cef2SBarry Smith 17393f9fe445SBarry Smith Logically Collective on TS 1740d763cef2SBarry Smith 1741d763cef2SBarry Smith Input Parameters: 1742d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1743d763cef2SBarry Smith - time_step - the size of the timestep 1744d763cef2SBarry Smith 1745d763cef2SBarry Smith Level: intermediate 1746d763cef2SBarry Smith 1747d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1748d763cef2SBarry Smith 1749d763cef2SBarry Smith .keywords: TS, set, timestep 1750d763cef2SBarry Smith @*/ 17517087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1752d763cef2SBarry Smith { 1753d763cef2SBarry Smith PetscFunctionBegin; 17540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1755c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1756d763cef2SBarry Smith ts->time_step = time_step; 175731748224SBarry Smith ts->time_step_orig = time_step; 1758d763cef2SBarry Smith PetscFunctionReturn(0); 1759d763cef2SBarry Smith } 1760d763cef2SBarry Smith 17614a2ae208SSatish Balay #undef __FUNCT__ 1762a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1763a43b19c4SJed Brown /*@ 176449354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 176549354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 176649354f04SShri Abhyankar or just return at the final time TS computed. 1767a43b19c4SJed Brown 1768a43b19c4SJed Brown Logically Collective on TS 1769a43b19c4SJed Brown 1770a43b19c4SJed Brown Input Parameter: 1771a43b19c4SJed Brown + ts - the time-step context 177249354f04SShri Abhyankar - eftopt - exact final time option 1773a43b19c4SJed Brown 1774a43b19c4SJed Brown Level: beginner 1775a43b19c4SJed Brown 1776a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 1777a43b19c4SJed Brown @*/ 177849354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 1779a43b19c4SJed Brown { 1780a43b19c4SJed Brown PetscFunctionBegin; 1781a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 178249354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 178349354f04SShri Abhyankar ts->exact_final_time = eftopt; 1784a43b19c4SJed Brown PetscFunctionReturn(0); 1785a43b19c4SJed Brown } 1786a43b19c4SJed Brown 1787a43b19c4SJed Brown #undef __FUNCT__ 17884a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1789d763cef2SBarry Smith /*@ 1790d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1791d763cef2SBarry Smith 1792d763cef2SBarry Smith Not Collective 1793d763cef2SBarry Smith 1794d763cef2SBarry Smith Input Parameter: 1795d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1796d763cef2SBarry Smith 1797d763cef2SBarry Smith Output Parameter: 1798d763cef2SBarry Smith . dt - the current timestep size 1799d763cef2SBarry Smith 1800d763cef2SBarry Smith Level: intermediate 1801d763cef2SBarry Smith 1802d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1803d763cef2SBarry Smith 1804d763cef2SBarry Smith .keywords: TS, get, timestep 1805d763cef2SBarry Smith @*/ 18067087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 1807d763cef2SBarry Smith { 1808d763cef2SBarry Smith PetscFunctionBegin; 18090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1810f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1811d763cef2SBarry Smith *dt = ts->time_step; 1812d763cef2SBarry Smith PetscFunctionReturn(0); 1813d763cef2SBarry Smith } 1814d763cef2SBarry Smith 18154a2ae208SSatish Balay #undef __FUNCT__ 18164a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1817d8e5e3e6SSatish Balay /*@ 1818d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1819d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1820d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1821d763cef2SBarry Smith the solution at the next timestep has been calculated. 1822d763cef2SBarry Smith 1823d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1824d763cef2SBarry Smith 1825d763cef2SBarry Smith Input Parameter: 1826d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1827d763cef2SBarry Smith 1828d763cef2SBarry Smith Output Parameter: 1829d763cef2SBarry Smith . v - the vector containing the solution 1830d763cef2SBarry Smith 1831d763cef2SBarry Smith Level: intermediate 1832d763cef2SBarry Smith 1833d763cef2SBarry Smith .seealso: TSGetTimeStep() 1834d763cef2SBarry Smith 1835d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1836d763cef2SBarry Smith @*/ 18377087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1838d763cef2SBarry Smith { 1839d763cef2SBarry Smith PetscFunctionBegin; 18400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18414482741eSBarry Smith PetscValidPointer(v,2); 18428737fe31SLisandro Dalcin *v = ts->vec_sol; 1843d763cef2SBarry Smith PetscFunctionReturn(0); 1844d763cef2SBarry Smith } 1845d763cef2SBarry Smith 1846c235aa8dSHong Zhang #undef __FUNCT__ 1847dfb21088SHong Zhang #define __FUNCT__ "TSGetCostGradients" 1848c235aa8dSHong Zhang /*@ 1849dfb21088SHong Zhang TSGetCostGradients - Returns the gradients from the TSAdjointSolve() 1850c235aa8dSHong Zhang 1851c235aa8dSHong Zhang Not Collective, but Vec returned is parallel if TS is parallel 1852c235aa8dSHong Zhang 1853c235aa8dSHong Zhang Input Parameter: 1854c235aa8dSHong Zhang . ts - the TS context obtained from TSCreate() 1855c235aa8dSHong Zhang 1856c235aa8dSHong Zhang Output Parameter: 1857abc2977eSBarry Smith + lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 1858abc2977eSBarry Smith - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 1859c235aa8dSHong Zhang 1860c235aa8dSHong Zhang Level: intermediate 1861c235aa8dSHong Zhang 1862c235aa8dSHong Zhang .seealso: TSGetTimeStep() 1863c235aa8dSHong Zhang 1864c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity 1865c235aa8dSHong Zhang @*/ 1866dfb21088SHong Zhang PetscErrorCode TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu) 1867c235aa8dSHong Zhang { 1868c235aa8dSHong Zhang PetscFunctionBegin; 1869c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1870abc2977eSBarry Smith if (numcost) *numcost = ts->numcost; 1871abc2977eSBarry Smith if (lambda) *lambda = ts->vecs_sensi; 1872abc2977eSBarry Smith if (mu) *mu = ts->vecs_sensip; 1873c235aa8dSHong Zhang PetscFunctionReturn(0); 1874c235aa8dSHong Zhang } 1875c235aa8dSHong Zhang 1876bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 18774a2ae208SSatish Balay #undef __FUNCT__ 1878bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1879d8e5e3e6SSatish Balay /*@ 1880bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1881d763cef2SBarry Smith 1882bdad233fSMatthew Knepley Not collective 1883d763cef2SBarry Smith 1884bdad233fSMatthew Knepley Input Parameters: 1885bdad233fSMatthew Knepley + ts - The TS 1886bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1887d763cef2SBarry Smith .vb 18880910c330SBarry Smith U_t - A U = 0 (linear) 18890910c330SBarry Smith U_t - A(t) U = 0 (linear) 18900910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 1891d763cef2SBarry Smith .ve 1892d763cef2SBarry Smith 1893d763cef2SBarry Smith Level: beginner 1894d763cef2SBarry Smith 1895bdad233fSMatthew Knepley .keywords: TS, problem type 1896bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1897d763cef2SBarry Smith @*/ 18987087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1899a7cc72afSBarry Smith { 19009e2a6581SJed Brown PetscErrorCode ierr; 19019e2a6581SJed Brown 1902d763cef2SBarry Smith PetscFunctionBegin; 19030700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1904bdad233fSMatthew Knepley ts->problem_type = type; 19059e2a6581SJed Brown if (type == TS_LINEAR) { 19069e2a6581SJed Brown SNES snes; 19079e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 19089e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 19099e2a6581SJed Brown } 1910d763cef2SBarry Smith PetscFunctionReturn(0); 1911d763cef2SBarry Smith } 1912d763cef2SBarry Smith 1913bdad233fSMatthew Knepley #undef __FUNCT__ 1914bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1915bdad233fSMatthew Knepley /*@C 1916bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1917bdad233fSMatthew Knepley 1918bdad233fSMatthew Knepley Not collective 1919bdad233fSMatthew Knepley 1920bdad233fSMatthew Knepley Input Parameter: 1921bdad233fSMatthew Knepley . ts - The TS 1922bdad233fSMatthew Knepley 1923bdad233fSMatthew Knepley Output Parameter: 1924bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1925bdad233fSMatthew Knepley .vb 1926089b2837SJed Brown M U_t = A U 1927089b2837SJed Brown M(t) U_t = A(t) U 1928b5abc632SBarry Smith F(t,U,U_t) 1929bdad233fSMatthew Knepley .ve 1930bdad233fSMatthew Knepley 1931bdad233fSMatthew Knepley Level: beginner 1932bdad233fSMatthew Knepley 1933bdad233fSMatthew Knepley .keywords: TS, problem type 1934bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1935bdad233fSMatthew Knepley @*/ 19367087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1937a7cc72afSBarry Smith { 1938bdad233fSMatthew Knepley PetscFunctionBegin; 19390700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 19404482741eSBarry Smith PetscValidIntPointer(type,2); 1941bdad233fSMatthew Knepley *type = ts->problem_type; 1942bdad233fSMatthew Knepley PetscFunctionReturn(0); 1943bdad233fSMatthew Knepley } 1944d763cef2SBarry Smith 19454a2ae208SSatish Balay #undef __FUNCT__ 19464a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1947d763cef2SBarry Smith /*@ 1948d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1949d763cef2SBarry Smith of a timestepper. 1950d763cef2SBarry Smith 1951d763cef2SBarry Smith Collective on TS 1952d763cef2SBarry Smith 1953d763cef2SBarry Smith Input Parameter: 1954d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1955d763cef2SBarry Smith 1956d763cef2SBarry Smith Notes: 1957d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1958d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1959d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1960d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1961d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1962d763cef2SBarry Smith 1963d763cef2SBarry Smith Level: advanced 1964d763cef2SBarry Smith 1965d763cef2SBarry Smith .keywords: TS, timestep, setup 1966d763cef2SBarry Smith 1967d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1968d763cef2SBarry Smith @*/ 19697087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1970d763cef2SBarry Smith { 1971dfbe8321SBarry Smith PetscErrorCode ierr; 19726c6b9e74SPeter Brune DM dm; 19736c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 1974d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 19756c6b9e74SPeter Brune TSIJacobian ijac; 19766c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1977d763cef2SBarry Smith 1978d763cef2SBarry Smith PetscFunctionBegin; 19790700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1980277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1981277b19d0SLisandro Dalcin 19822c18e0fdSBarry Smith ts->total_steps = 0; 19837adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 19849596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1985d763cef2SBarry Smith } 1986277b19d0SLisandro Dalcin 1987277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1988277b19d0SLisandro Dalcin 19891c3436cfSJed Brown 1990552698daSJed Brown ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 1991277b19d0SLisandro Dalcin 1992e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 1993e1244c69SJed Brown Mat Amat,Pmat; 1994e1244c69SJed Brown SNES snes; 1995e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1996e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 1997e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 1998e1244c69SJed Brown * have displaced the RHS matrix */ 1999e1244c69SJed Brown if (Amat == ts->Arhs) { 2000e1244c69SJed Brown ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 2001e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 2002e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 2003e1244c69SJed Brown } 2004e1244c69SJed Brown if (Pmat == ts->Brhs) { 2005e1244c69SJed Brown ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 2006e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 2007e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 2008e1244c69SJed Brown } 2009e1244c69SJed Brown } 2010277b19d0SLisandro Dalcin if (ts->ops->setup) { 2011000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 2012277b19d0SLisandro Dalcin } 2013277b19d0SLisandro Dalcin 20146c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 20156c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 20166c6b9e74SPeter Brune */ 20176c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 20180298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 20196c6b9e74SPeter Brune if (!func) { 20206c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 20216c6b9e74SPeter Brune } 20226c6b9e74SPeter 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. 20236c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 20246c6b9e74SPeter Brune */ 20250298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 20260298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 20270298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 20286c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 20296c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 20306c6b9e74SPeter Brune } 2031277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2032277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2033277b19d0SLisandro Dalcin } 2034277b19d0SLisandro Dalcin 2035277b19d0SLisandro Dalcin #undef __FUNCT__ 2036f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp" 2037f6a906c0SBarry Smith /*@ 2038f6a906c0SBarry Smith TSAdjointSetUp - Sets up the internal data structures for the later use 2039f6a906c0SBarry Smith of an adjoint solver 2040f6a906c0SBarry Smith 2041f6a906c0SBarry Smith Collective on TS 2042f6a906c0SBarry Smith 2043f6a906c0SBarry Smith Input Parameter: 2044f6a906c0SBarry Smith . ts - the TS context obtained from TSCreate() 2045f6a906c0SBarry Smith 2046f6a906c0SBarry Smith Level: advanced 2047f6a906c0SBarry Smith 2048f6a906c0SBarry Smith .keywords: TS, timestep, setup 2049f6a906c0SBarry Smith 2050947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients() 2051f6a906c0SBarry Smith @*/ 2052f6a906c0SBarry Smith PetscErrorCode TSAdjointSetUp(TS ts) 2053f6a906c0SBarry Smith { 2054f6a906c0SBarry Smith PetscErrorCode ierr; 2055f6a906c0SBarry Smith 2056f6a906c0SBarry Smith PetscFunctionBegin; 2057f6a906c0SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2058f6a906c0SBarry Smith if (ts->adjointsetupcalled) PetscFunctionReturn(0); 2059dfb21088SHong Zhang if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first"); 2060d8151eeaSBarry Smith 2061947abb85SHong Zhang if (ts->vec_costintegral) { /* if there is integral in the cost function*/ 2062d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2063d8151eeaSBarry Smith if (ts->vecs_sensip){ 2064d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2065d8151eeaSBarry Smith } 2066947abb85SHong Zhang } 2067d8151eeaSBarry Smith 206842f2b339SBarry Smith if (ts->ops->adjointsetup) { 206942f2b339SBarry Smith ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr); 2070f6a906c0SBarry Smith } 2071f6a906c0SBarry Smith ts->adjointsetupcalled = PETSC_TRUE; 2072f6a906c0SBarry Smith PetscFunctionReturn(0); 2073f6a906c0SBarry Smith } 2074f6a906c0SBarry Smith 2075f6a906c0SBarry Smith #undef __FUNCT__ 2076277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 2077277b19d0SLisandro Dalcin /*@ 2078277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2079277b19d0SLisandro Dalcin 2080277b19d0SLisandro Dalcin Collective on TS 2081277b19d0SLisandro Dalcin 2082277b19d0SLisandro Dalcin Input Parameter: 2083277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2084277b19d0SLisandro Dalcin 2085277b19d0SLisandro Dalcin Level: beginner 2086277b19d0SLisandro Dalcin 2087277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 2088277b19d0SLisandro Dalcin 2089277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2090277b19d0SLisandro Dalcin @*/ 2091277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2092277b19d0SLisandro Dalcin { 2093277b19d0SLisandro Dalcin PetscErrorCode ierr; 2094277b19d0SLisandro Dalcin 2095277b19d0SLisandro Dalcin PetscFunctionBegin; 2096277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2097b18ea86cSHong Zhang 2098277b19d0SLisandro Dalcin if (ts->ops->reset) { 2099277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2100277b19d0SLisandro Dalcin } 2101277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2102e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2103bbd56ea5SKarl Rupp 21044e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 21054e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2106214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 21076bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2108e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2109e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 211038637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2111bbd56ea5SKarl Rupp 2112947abb85SHong Zhang if (ts->vec_costintegral) { 2113d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2114d8151eeaSBarry Smith if (ts->vecs_drdp){ 2115d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2116d8151eeaSBarry Smith } 2117947abb85SHong Zhang } 2118d8151eeaSBarry Smith ts->vecs_sensi = NULL; 2119d8151eeaSBarry Smith ts->vecs_sensip = NULL; 2120ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 21212c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 212236eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2123277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2124d763cef2SBarry Smith PetscFunctionReturn(0); 2125d763cef2SBarry Smith } 2126d763cef2SBarry Smith 21274a2ae208SSatish Balay #undef __FUNCT__ 21284a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 2129d8e5e3e6SSatish Balay /*@ 2130d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2131d763cef2SBarry Smith with TSCreate(). 2132d763cef2SBarry Smith 2133d763cef2SBarry Smith Collective on TS 2134d763cef2SBarry Smith 2135d763cef2SBarry Smith Input Parameter: 2136d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2137d763cef2SBarry Smith 2138d763cef2SBarry Smith Level: beginner 2139d763cef2SBarry Smith 2140d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2141d763cef2SBarry Smith 2142d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2143d763cef2SBarry Smith @*/ 21446bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2145d763cef2SBarry Smith { 21466849ba73SBarry Smith PetscErrorCode ierr; 2147d763cef2SBarry Smith 2148d763cef2SBarry Smith PetscFunctionBegin; 21496bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 21506bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 21516bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2152d763cef2SBarry Smith 21536bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2154277b19d0SLisandro Dalcin 2155e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2156e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 21576bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 21586d4c513bSLisandro Dalcin 2159bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2160bc952696SBarry Smith 216184df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 2162aeb4809dSShri Abhyankar if ((*ts)->event) { 2163aeb4809dSShri Abhyankar ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr); 2164aeb4809dSShri Abhyankar } 21656bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 21666bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 21676bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 21680dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 21696d4c513bSLisandro Dalcin 2170a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2171d763cef2SBarry Smith PetscFunctionReturn(0); 2172d763cef2SBarry Smith } 2173d763cef2SBarry Smith 21744a2ae208SSatish Balay #undef __FUNCT__ 21754a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 2176d8e5e3e6SSatish Balay /*@ 2177d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2178d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2179d763cef2SBarry Smith 2180d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2181d763cef2SBarry Smith 2182d763cef2SBarry Smith Input Parameter: 2183d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2184d763cef2SBarry Smith 2185d763cef2SBarry Smith Output Parameter: 2186d763cef2SBarry Smith . snes - the nonlinear solver context 2187d763cef2SBarry Smith 2188d763cef2SBarry Smith Notes: 2189d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2190d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 219194b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2192d763cef2SBarry Smith 2193d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 21940298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2195d763cef2SBarry Smith 2196d763cef2SBarry Smith Level: beginner 2197d763cef2SBarry Smith 2198d763cef2SBarry Smith .keywords: timestep, get, SNES 2199d763cef2SBarry Smith @*/ 22007087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2201d763cef2SBarry Smith { 2202d372ba47SLisandro Dalcin PetscErrorCode ierr; 2203d372ba47SLisandro Dalcin 2204d763cef2SBarry Smith PetscFunctionBegin; 22050700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22064482741eSBarry Smith PetscValidPointer(snes,2); 2207d372ba47SLisandro Dalcin if (!ts->snes) { 2208ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 22090298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 22103bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2211d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2212496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 22139e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 22149e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 22159e2a6581SJed Brown } 2216d372ba47SLisandro Dalcin } 2217d763cef2SBarry Smith *snes = ts->snes; 2218d763cef2SBarry Smith PetscFunctionReturn(0); 2219d763cef2SBarry Smith } 2220d763cef2SBarry Smith 22214a2ae208SSatish Balay #undef __FUNCT__ 2222deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES" 2223deb2cd25SJed Brown /*@ 2224deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2225deb2cd25SJed Brown 2226deb2cd25SJed Brown Collective 2227deb2cd25SJed Brown 2228deb2cd25SJed Brown Input Parameter: 2229deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2230deb2cd25SJed Brown - snes - the nonlinear solver context 2231deb2cd25SJed Brown 2232deb2cd25SJed Brown Notes: 2233deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2234deb2cd25SJed Brown 2235deb2cd25SJed Brown Level: developer 2236deb2cd25SJed Brown 2237deb2cd25SJed Brown .keywords: timestep, set, SNES 2238deb2cd25SJed Brown @*/ 2239deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2240deb2cd25SJed Brown { 2241deb2cd25SJed Brown PetscErrorCode ierr; 2242d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2243deb2cd25SJed Brown 2244deb2cd25SJed Brown PetscFunctionBegin; 2245deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2246deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2247deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2248deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2249bbd56ea5SKarl Rupp 2250deb2cd25SJed Brown ts->snes = snes; 2251bbd56ea5SKarl Rupp 22520298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 22530298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2254740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 22550298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2256740132f1SEmil Constantinescu } 2257deb2cd25SJed Brown PetscFunctionReturn(0); 2258deb2cd25SJed Brown } 2259deb2cd25SJed Brown 2260deb2cd25SJed Brown #undef __FUNCT__ 226194b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 2262d8e5e3e6SSatish Balay /*@ 226394b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2264d763cef2SBarry Smith a TS (timestepper) context. 2265d763cef2SBarry Smith 226694b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2267d763cef2SBarry Smith 2268d763cef2SBarry Smith Input Parameter: 2269d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2270d763cef2SBarry Smith 2271d763cef2SBarry Smith Output Parameter: 227294b7f48cSBarry Smith . ksp - the nonlinear solver context 2273d763cef2SBarry Smith 2274d763cef2SBarry Smith Notes: 227594b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2276d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2277d763cef2SBarry Smith KSP and PC contexts as well. 2278d763cef2SBarry Smith 227994b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 22800298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2281d763cef2SBarry Smith 2282d763cef2SBarry Smith Level: beginner 2283d763cef2SBarry Smith 228494b7f48cSBarry Smith .keywords: timestep, get, KSP 2285d763cef2SBarry Smith @*/ 22867087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2287d763cef2SBarry Smith { 2288d372ba47SLisandro Dalcin PetscErrorCode ierr; 2289089b2837SJed Brown SNES snes; 2290d372ba47SLisandro Dalcin 2291d763cef2SBarry Smith PetscFunctionBegin; 22920700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22934482741eSBarry Smith PetscValidPointer(ksp,2); 229417186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2295e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2296089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2297089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2298d763cef2SBarry Smith PetscFunctionReturn(0); 2299d763cef2SBarry Smith } 2300d763cef2SBarry Smith 2301d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2302d763cef2SBarry Smith 23034a2ae208SSatish Balay #undef __FUNCT__ 2304adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 2305adb62b0dSMatthew Knepley /*@ 2306adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2307adb62b0dSMatthew Knepley maximum time for iteration. 2308adb62b0dSMatthew Knepley 23093f9fe445SBarry Smith Not Collective 2310adb62b0dSMatthew Knepley 2311adb62b0dSMatthew Knepley Input Parameters: 2312adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 23130298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 23140298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2315adb62b0dSMatthew Knepley 2316adb62b0dSMatthew Knepley Level: intermediate 2317adb62b0dSMatthew Knepley 2318adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2319adb62b0dSMatthew Knepley @*/ 23207087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2321adb62b0dSMatthew Knepley { 2322adb62b0dSMatthew Knepley PetscFunctionBegin; 23230700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2324abc0a331SBarry Smith if (maxsteps) { 23254482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2326adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2327adb62b0dSMatthew Knepley } 2328abc0a331SBarry Smith if (maxtime) { 23294482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2330adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2331adb62b0dSMatthew Knepley } 2332adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2333adb62b0dSMatthew Knepley } 2334adb62b0dSMatthew Knepley 2335adb62b0dSMatthew Knepley #undef __FUNCT__ 23364a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 2337d763cef2SBarry Smith /*@ 2338d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2339d763cef2SBarry Smith maximum time for iteration. 2340d763cef2SBarry Smith 23413f9fe445SBarry Smith Logically Collective on TS 2342d763cef2SBarry Smith 2343d763cef2SBarry Smith Input Parameters: 2344d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2345d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2346d763cef2SBarry Smith - maxtime - final time to iterate to 2347d763cef2SBarry Smith 2348d763cef2SBarry Smith Options Database Keys: 2349d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 23503bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2351d763cef2SBarry Smith 2352d763cef2SBarry Smith Notes: 2353d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2354d763cef2SBarry Smith 2355d763cef2SBarry Smith Level: intermediate 2356d763cef2SBarry Smith 2357d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2358a43b19c4SJed Brown 2359a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2360d763cef2SBarry Smith @*/ 23617087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2362d763cef2SBarry Smith { 2363d763cef2SBarry Smith PetscFunctionBegin; 23640700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2365c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2366c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 236739b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 236839b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2369d763cef2SBarry Smith PetscFunctionReturn(0); 2370d763cef2SBarry Smith } 2371d763cef2SBarry Smith 23724a2ae208SSatish Balay #undef __FUNCT__ 23734a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 2374d763cef2SBarry Smith /*@ 2375d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2376d763cef2SBarry Smith for use by the TS routines. 2377d763cef2SBarry Smith 23783f9fe445SBarry Smith Logically Collective on TS and Vec 2379d763cef2SBarry Smith 2380d763cef2SBarry Smith Input Parameters: 2381d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 23820910c330SBarry Smith - u - the solution vector 2383d763cef2SBarry Smith 2384d763cef2SBarry Smith Level: beginner 2385d763cef2SBarry Smith 2386d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2387d763cef2SBarry Smith @*/ 23880910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2389d763cef2SBarry Smith { 23908737fe31SLisandro Dalcin PetscErrorCode ierr; 2391496e6a7aSJed Brown DM dm; 23928737fe31SLisandro Dalcin 2393d763cef2SBarry Smith PetscFunctionBegin; 23940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 23950910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 23960910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 23976bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2398bbd56ea5SKarl Rupp 23990910c330SBarry Smith ts->vec_sol = u; 2400bbd56ea5SKarl Rupp 2401496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 24020910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2403d763cef2SBarry Smith PetscFunctionReturn(0); 2404d763cef2SBarry Smith } 2405d763cef2SBarry Smith 2406e74ef692SMatthew Knepley #undef __FUNCT__ 240773b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps" 240873b18844SBarry Smith /*@ 240973b18844SBarry Smith TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time 241073b18844SBarry Smith 241173b18844SBarry Smith Logically Collective on TS 241273b18844SBarry Smith 241373b18844SBarry Smith Input Parameters: 241473b18844SBarry Smith + ts - the TS context obtained from TSCreate() 241573b18844SBarry Smith . steps - number of steps to use 241673b18844SBarry Smith 241773b18844SBarry Smith Level: intermediate 241873b18844SBarry Smith 241973b18844SBarry Smith Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this 242073b18844SBarry Smith so as to integrate back to less than the original timestep 242173b18844SBarry Smith 242273b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations 242373b18844SBarry Smith 242473b18844SBarry Smith .seealso: TSSetExactFinalTime() 242573b18844SBarry Smith @*/ 242673b18844SBarry Smith PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps) 242773b18844SBarry Smith { 242873b18844SBarry Smith PetscFunctionBegin; 242973b18844SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 243073b18844SBarry Smith PetscValidLogicalCollectiveInt(ts,steps,2); 243173b18844SBarry Smith if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps"); 243273b18844SBarry 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"); 243373b18844SBarry Smith ts->adjoint_max_steps = steps; 243473b18844SBarry Smith PetscFunctionReturn(0); 243573b18844SBarry Smith } 243673b18844SBarry Smith 243773b18844SBarry Smith #undef __FUNCT__ 2438dfb21088SHong Zhang #define __FUNCT__ "TSSetCostGradients" 2439c235aa8dSHong Zhang /*@ 2440dfb21088SHong Zhang TSSetCostGradients - Sets the initial value of the gradients of the cost function w.r.t. initial conditions and w.r.t. the problem parameters 24410cf82b59SBarry Smith for use by the TSAdjoint routines. 2442c235aa8dSHong Zhang 2443c235aa8dSHong Zhang Logically Collective on TS and Vec 2444c235aa8dSHong Zhang 2445c235aa8dSHong Zhang Input Parameters: 2446c235aa8dSHong Zhang + ts - the TS context obtained from TSCreate() 2447abc2977eSBarry Smith . lambda - gradients with respect to the initial condition variables, the dimension and parallel layout of these vectors is the same as the ODE solution vector 2448abc2977eSBarry Smith - mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters 2449c235aa8dSHong Zhang 2450c235aa8dSHong Zhang Level: beginner 2451c235aa8dSHong Zhang 2452abc2977eSBarry Smith Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime mu_i = df/dp|finaltime 24530cf82b59SBarry Smith 2454c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions 2455c235aa8dSHong Zhang @*/ 2456dfb21088SHong Zhang PetscErrorCode TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu) 2457c235aa8dSHong Zhang { 2458c235aa8dSHong Zhang PetscFunctionBegin; 2459c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2460abc2977eSBarry Smith PetscValidPointer(lambda,2); 2461abc2977eSBarry Smith ts->vecs_sensi = lambda; 2462abc2977eSBarry Smith ts->vecs_sensip = mu; 2463dfb21088SHong Zhang if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostIntegrand"); 2464abc2977eSBarry Smith ts->numcost = numcost; 2465ad8e2604SHong Zhang PetscFunctionReturn(0); 2466ad8e2604SHong Zhang } 2467ad8e2604SHong Zhang 2468ad8e2604SHong Zhang #undef __FUNCT__ 24695bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian" 2470ad8e2604SHong Zhang /*@C 24710cf82b59SBarry Smith TSAdjointSetRHSJacobian - Sets the function that computes the Jacobian of G w.r.t. the parameters p where y_t = G(y,p,t), as well as the location to store the matrix. 2472ad8e2604SHong Zhang 2473ad8e2604SHong Zhang Logically Collective on TS 2474ad8e2604SHong Zhang 2475ad8e2604SHong Zhang Input Parameters: 2476ad8e2604SHong Zhang + ts - The TS context obtained from TSCreate() 2477ad8e2604SHong Zhang - func - The function 2478ad8e2604SHong Zhang 2479ad8e2604SHong Zhang Calling sequence of func: 24800cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx); 248105755b9cSHong Zhang + t - current timestep 24820cf82b59SBarry Smith . y - input vector (current ODE solution) 248305755b9cSHong Zhang . A - output matrix 248405755b9cSHong Zhang - ctx - [optional] user-defined function context 2485ad8e2604SHong Zhang 2486ad8e2604SHong Zhang Level: intermediate 2487ad8e2604SHong Zhang 24880cf82b59SBarry Smith Notes: Amat has the same number of rows and the same row parallel layout as u, Amat has the same number of columns and parallel layout as p 24890cf82b59SBarry Smith 2490ad8e2604SHong Zhang .keywords: TS, sensitivity 2491ad8e2604SHong Zhang .seealso: 2492ad8e2604SHong Zhang @*/ 24935bf8c567SBarry Smith PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 2494ad8e2604SHong Zhang { 2495ad8e2604SHong Zhang PetscErrorCode ierr; 2496ad8e2604SHong Zhang 2497ad8e2604SHong Zhang PetscFunctionBegin; 2498ad8e2604SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2499ad8e2604SHong Zhang if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 2500ad8e2604SHong Zhang 2501ad8e2604SHong Zhang ts->rhsjacobianp = func; 2502ad8e2604SHong Zhang ts->rhsjacobianpctx = ctx; 2503ad8e2604SHong Zhang if(Amat) { 2504ad8e2604SHong Zhang ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 2505ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2506ad8e2604SHong Zhang ts->Jacp = Amat; 2507ad8e2604SHong Zhang } 2508ad8e2604SHong Zhang PetscFunctionReturn(0); 2509ad8e2604SHong Zhang } 2510ad8e2604SHong Zhang 2511ad8e2604SHong Zhang #undef __FUNCT__ 25125bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian" 25130cf82b59SBarry Smith /*@C 25145bf8c567SBarry Smith TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function. 2515ad8e2604SHong Zhang 2516ad8e2604SHong Zhang Collective on TS 2517ad8e2604SHong Zhang 2518ad8e2604SHong Zhang Input Parameters: 2519ad8e2604SHong Zhang . ts - The TS context obtained from TSCreate() 2520ad8e2604SHong Zhang 2521ad8e2604SHong Zhang Level: developer 2522ad8e2604SHong Zhang 2523ad8e2604SHong Zhang .keywords: TS, sensitivity 25245bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian() 2525ad8e2604SHong Zhang @*/ 25265bf8c567SBarry Smith PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat) 2527ad8e2604SHong Zhang { 2528ad8e2604SHong Zhang PetscErrorCode ierr; 2529ad8e2604SHong Zhang 2530ad8e2604SHong Zhang PetscFunctionBegin; 2531ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2532ad8e2604SHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 2533ad8e2604SHong Zhang PetscValidPointer(Amat,4); 2534ad8e2604SHong Zhang 2535ad8e2604SHong Zhang PetscStackPush("TS user JacobianP function for sensitivity analysis"); 2536ad8e2604SHong Zhang ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 2537ad8e2604SHong Zhang PetscStackPop; 2538c235aa8dSHong Zhang PetscFunctionReturn(0); 2539c235aa8dSHong Zhang } 2540c235aa8dSHong Zhang 2541c235aa8dSHong Zhang #undef __FUNCT__ 2542dfb21088SHong Zhang #define __FUNCT__ "TSSetCostIntegrand" 25436fd0887fSHong Zhang /*@C 2544dfb21088SHong Zhang TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions 25456fd0887fSHong Zhang 25466fd0887fSHong Zhang Logically Collective on TS 25476fd0887fSHong Zhang 25486fd0887fSHong Zhang Input Parameters: 25496fd0887fSHong Zhang + ts - the TS context obtained from TSCreate() 2550abc2977eSBarry Smith . numcost - number of gradients to be computed, this is the number of cost functions 25510cf82b59SBarry Smith . rf - routine for evaluating the integrand function 2552b612ec54SBarry Smith . drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y 2553b612ec54SBarry Smith . drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p 2554b612ec54SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 25556fd0887fSHong Zhang 25560cf82b59SBarry Smith Calling sequence of rf: 25570cf82b59SBarry Smith $ rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx); 25586fd0887fSHong Zhang 25596fd0887fSHong Zhang + t - current timestep 25600cf82b59SBarry Smith . y - input vector 25610cf82b59SBarry Smith . f - function result; one vector entry for each cost function 25626fd0887fSHong Zhang - ctx - [optional] user-defined function context 25636fd0887fSHong Zhang 2564b612ec54SBarry Smith Calling sequence of drdyf: 25650cf82b59SBarry Smith $ PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx); 2566b612ec54SBarry Smith 2567b612ec54SBarry Smith Calling sequence of drdpf: 25680cf82b59SBarry Smith $ PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx); 2569b612ec54SBarry Smith 2570b612ec54SBarry Smith Level: intermediate 25716fd0887fSHong Zhang 2572abc2977eSBarry Smith Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions 25730cf82b59SBarry Smith 25746fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 25756fd0887fSHong Zhang 2576dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients() 25776fd0887fSHong Zhang @*/ 2578dfb21088SHong Zhang PetscErrorCode TSSetCostIntegrand(TS ts,PetscInt numcost, PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*), 2579d8151eeaSBarry Smith PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*), 2580d8151eeaSBarry Smith PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),void *ctx) 25816fd0887fSHong Zhang { 25826fd0887fSHong Zhang PetscErrorCode ierr; 25836fd0887fSHong Zhang 25846fd0887fSHong Zhang PetscFunctionBegin; 25856fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2586dfb21088SHong Zhang if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostGradients()"); 2587c72ed514SHong Zhang if (!ts->numcost) ts->numcost=numcost; 25886fd0887fSHong Zhang 2589abc2977eSBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr); 25902c39e106SBarry Smith ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr); 25910cf82b59SBarry Smith ts->costintegrand = rf; 259205755b9cSHong Zhang ts->costintegrandctx = ctx; 2593b612ec54SBarry Smith ts->drdyfunction = drdyf; 2594b612ec54SBarry Smith ts->drdpfunction = drdpf; 25956fd0887fSHong Zhang PetscFunctionReturn(0); 25966fd0887fSHong Zhang } 25976fd0887fSHong Zhang 25986fd0887fSHong Zhang #undef __FUNCT__ 2599dfb21088SHong Zhang #define __FUNCT__ "TSGetCostIntegral" 260036eaed60SHong Zhang /*@ 2601dfb21088SHong Zhang TSGetCostIntegral - Returns the values of the integral term in the cost functions. 260236eaed60SHong Zhang It is valid to call the routine after a backward run. 260336eaed60SHong Zhang 260436eaed60SHong Zhang Not Collective 260536eaed60SHong Zhang 260636eaed60SHong Zhang Input Parameter: 260736eaed60SHong Zhang . ts - the TS context obtained from TSCreate() 260836eaed60SHong Zhang 260936eaed60SHong Zhang Output Parameter: 26102c39e106SBarry Smith . v - the vector containing the integrals for each cost function 261136eaed60SHong Zhang 261236eaed60SHong Zhang Level: intermediate 261336eaed60SHong Zhang 2614dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 261536eaed60SHong Zhang 261636eaed60SHong Zhang .keywords: TS, sensitivity analysis 261736eaed60SHong Zhang @*/ 2618dfb21088SHong Zhang PetscErrorCode TSGetCostIntegral(TS ts,Vec *v) 261936eaed60SHong Zhang { 262036eaed60SHong Zhang PetscFunctionBegin; 262136eaed60SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 262236eaed60SHong Zhang PetscValidPointer(v,2); 26232c39e106SBarry Smith *v = ts->vec_costintegral; 262436eaed60SHong Zhang PetscFunctionReturn(0); 262536eaed60SHong Zhang } 262636eaed60SHong Zhang 262736eaed60SHong Zhang #undef __FUNCT__ 2628d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand" 26296fd0887fSHong Zhang /*@ 26302c39e106SBarry Smith TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions. 26316fd0887fSHong Zhang 26326fd0887fSHong Zhang Input Parameters: 26336fd0887fSHong Zhang + ts - the TS context 26346fd0887fSHong Zhang . t - current time 26350cf82b59SBarry Smith - y - state vector, i.e. current solution 26366fd0887fSHong Zhang 26376fd0887fSHong Zhang Output Parameter: 2638abc2977eSBarry Smith . q - vector of size numcost to hold the outputs 26396fd0887fSHong Zhang 26406fd0887fSHong Zhang Note: 26416fd0887fSHong Zhang Most users should not need to explicitly call this routine, as it 26426fd0887fSHong Zhang is used internally within the sensitivity analysis context. 26436fd0887fSHong Zhang 26446fd0887fSHong Zhang Level: developer 26456fd0887fSHong Zhang 26466fd0887fSHong Zhang .keywords: TS, compute 26476fd0887fSHong Zhang 2648dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 26496fd0887fSHong Zhang @*/ 26500cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q) 26516fd0887fSHong Zhang { 26526fd0887fSHong Zhang PetscErrorCode ierr; 26536fd0887fSHong Zhang 26546fd0887fSHong Zhang PetscFunctionBegin; 26556fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26560cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 26576fd0887fSHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,4); 26586fd0887fSHong Zhang 26590cf82b59SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 2660e4680132SHong Zhang if (ts->costintegrand) { 2661e4680132SHong Zhang PetscStackPush("TS user integrand in the cost function"); 26620cf82b59SBarry Smith ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr); 26636fd0887fSHong Zhang PetscStackPop; 26646fd0887fSHong Zhang } else { 26656fd0887fSHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 26666fd0887fSHong Zhang } 26676fd0887fSHong Zhang 26680cf82b59SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 26696fd0887fSHong Zhang PetscFunctionReturn(0); 26706fd0887fSHong Zhang } 26716fd0887fSHong Zhang 26726fd0887fSHong Zhang #undef __FUNCT__ 2673d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction" 267405755b9cSHong Zhang /*@ 2675d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 267605755b9cSHong Zhang 267705755b9cSHong Zhang Collective on TS 267805755b9cSHong Zhang 267905755b9cSHong Zhang Input Parameters: 268005755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 268105755b9cSHong Zhang 268205755b9cSHong Zhang Notes: 2683d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 268405755b9cSHong Zhang so most users would not generally call this routine themselves. 268505755b9cSHong Zhang 268605755b9cSHong Zhang Level: developer 268705755b9cSHong Zhang 268805755b9cSHong Zhang .keywords: TS, sensitivity 2689d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction() 269005755b9cSHong Zhang @*/ 26910cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy) 269205755b9cSHong Zhang { 269305755b9cSHong Zhang PetscErrorCode ierr; 269405755b9cSHong Zhang 269505755b9cSHong Zhang PetscFunctionBegin; 269605755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26970cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 269805755b9cSHong Zhang 269905755b9cSHong Zhang PetscStackPush("TS user DRDY function for sensitivity analysis"); 27000cf82b59SBarry Smith ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr); 270105755b9cSHong Zhang PetscStackPop; 270205755b9cSHong Zhang PetscFunctionReturn(0); 270305755b9cSHong Zhang } 270405755b9cSHong Zhang 270505755b9cSHong Zhang #undef __FUNCT__ 2706d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction" 270705755b9cSHong Zhang /*@ 2708d4aa0a58SBarry Smith TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 270905755b9cSHong Zhang 271005755b9cSHong Zhang Collective on TS 271105755b9cSHong Zhang 271205755b9cSHong Zhang Input Parameters: 271305755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 271405755b9cSHong Zhang 271505755b9cSHong Zhang Notes: 271605755b9cSHong Zhang TSDRDPFunction() is typically used for sensitivity implementation, 271705755b9cSHong Zhang so most users would not generally call this routine themselves. 271805755b9cSHong Zhang 271905755b9cSHong Zhang Level: developer 272005755b9cSHong Zhang 272105755b9cSHong Zhang .keywords: TS, sensitivity 2722d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction() 272305755b9cSHong Zhang @*/ 27240cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp) 272505755b9cSHong Zhang { 272605755b9cSHong Zhang PetscErrorCode ierr; 272705755b9cSHong Zhang 272805755b9cSHong Zhang PetscFunctionBegin; 272905755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27300cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 273105755b9cSHong Zhang 273205755b9cSHong Zhang PetscStackPush("TS user DRDP function for sensitivity analysis"); 27330cf82b59SBarry Smith ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr); 273405755b9cSHong Zhang PetscStackPop; 273505755b9cSHong Zhang PetscFunctionReturn(0); 273605755b9cSHong Zhang } 273705755b9cSHong Zhang 273805755b9cSHong Zhang #undef __FUNCT__ 2739e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 2740ac226902SBarry Smith /*@C 2741000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 27423f2090d5SJed Brown called once at the beginning of each time step. 2743000e7ae3SMatthew Knepley 27443f9fe445SBarry Smith Logically Collective on TS 2745000e7ae3SMatthew Knepley 2746000e7ae3SMatthew Knepley Input Parameters: 2747000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2748000e7ae3SMatthew Knepley - func - The function 2749000e7ae3SMatthew Knepley 2750000e7ae3SMatthew Knepley Calling sequence of func: 2751000e7ae3SMatthew Knepley . func (TS ts); 2752000e7ae3SMatthew Knepley 2753000e7ae3SMatthew Knepley Level: intermediate 2754000e7ae3SMatthew Knepley 2755b8123daeSJed Brown Note: 2756b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 2757b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 2758b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 2759b8123daeSJed Brown 2760000e7ae3SMatthew Knepley .keywords: TS, timestep 27619be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 2762000e7ae3SMatthew Knepley @*/ 27637087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 2764000e7ae3SMatthew Knepley { 2765000e7ae3SMatthew Knepley PetscFunctionBegin; 27660700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2767ae60f76fSBarry Smith ts->prestep = func; 2768000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2769000e7ae3SMatthew Knepley } 2770000e7ae3SMatthew Knepley 2771e74ef692SMatthew Knepley #undef __FUNCT__ 27723f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 277309ee8438SJed Brown /*@ 27743f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 27753f2090d5SJed Brown 27763f2090d5SJed Brown Collective on TS 27773f2090d5SJed Brown 27783f2090d5SJed Brown Input Parameters: 27793f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 27803f2090d5SJed Brown 27813f2090d5SJed Brown Notes: 27823f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 27833f2090d5SJed Brown so most users would not generally call this routine themselves. 27843f2090d5SJed Brown 27853f2090d5SJed Brown Level: developer 27863f2090d5SJed Brown 27873f2090d5SJed Brown .keywords: TS, timestep 27889be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 27893f2090d5SJed Brown @*/ 27907087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 27913f2090d5SJed Brown { 27923f2090d5SJed Brown PetscErrorCode ierr; 27933f2090d5SJed Brown 27943f2090d5SJed Brown PetscFunctionBegin; 27950700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2796ae60f76fSBarry Smith if (ts->prestep) { 2797ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 2798312ce896SJed Brown } 27993f2090d5SJed Brown PetscFunctionReturn(0); 28003f2090d5SJed Brown } 28013f2090d5SJed Brown 28023f2090d5SJed Brown #undef __FUNCT__ 2803b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 2804b8123daeSJed Brown /*@C 2805b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 2806b8123daeSJed Brown called once at the beginning of each stage. 2807b8123daeSJed Brown 2808b8123daeSJed Brown Logically Collective on TS 2809b8123daeSJed Brown 2810b8123daeSJed Brown Input Parameters: 2811b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 2812b8123daeSJed Brown - func - The function 2813b8123daeSJed Brown 2814b8123daeSJed Brown Calling sequence of func: 2815b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 2816b8123daeSJed Brown 2817b8123daeSJed Brown Level: intermediate 2818b8123daeSJed Brown 2819b8123daeSJed Brown Note: 2820b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 2821b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 2822b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 2823b8123daeSJed Brown 2824b8123daeSJed Brown .keywords: TS, timestep 28259be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 2826b8123daeSJed Brown @*/ 2827b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 2828b8123daeSJed Brown { 2829b8123daeSJed Brown PetscFunctionBegin; 2830b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2831ae60f76fSBarry Smith ts->prestage = func; 2832b8123daeSJed Brown PetscFunctionReturn(0); 2833b8123daeSJed Brown } 2834b8123daeSJed Brown 2835b8123daeSJed Brown #undef __FUNCT__ 28369be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage" 28379be3e283SDebojyoti Ghosh /*@C 28389be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 28399be3e283SDebojyoti Ghosh called once at the end of each stage. 28409be3e283SDebojyoti Ghosh 28419be3e283SDebojyoti Ghosh Logically Collective on TS 28429be3e283SDebojyoti Ghosh 28439be3e283SDebojyoti Ghosh Input Parameters: 28449be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 28459be3e283SDebojyoti Ghosh - func - The function 28469be3e283SDebojyoti Ghosh 28479be3e283SDebojyoti Ghosh Calling sequence of func: 28489be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 28499be3e283SDebojyoti Ghosh 28509be3e283SDebojyoti Ghosh Level: intermediate 28519be3e283SDebojyoti Ghosh 28529be3e283SDebojyoti Ghosh Note: 28539be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 28549be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 28559be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 28569be3e283SDebojyoti Ghosh 28579be3e283SDebojyoti Ghosh .keywords: TS, timestep 28589be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 28599be3e283SDebojyoti Ghosh @*/ 28609be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 28619be3e283SDebojyoti Ghosh { 28629be3e283SDebojyoti Ghosh PetscFunctionBegin; 28639be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 28649be3e283SDebojyoti Ghosh ts->poststage = func; 28659be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 28669be3e283SDebojyoti Ghosh } 28679be3e283SDebojyoti Ghosh 28689be3e283SDebojyoti Ghosh #undef __FUNCT__ 2869b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 2870b8123daeSJed Brown /*@ 2871b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 2872b8123daeSJed Brown 2873b8123daeSJed Brown Collective on TS 2874b8123daeSJed Brown 2875b8123daeSJed Brown Input Parameters: 2876b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 28779be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 2878b8123daeSJed Brown 2879b8123daeSJed Brown Notes: 2880b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 2881b8123daeSJed Brown most users would not generally call this routine themselves. 2882b8123daeSJed Brown 2883b8123daeSJed Brown Level: developer 2884b8123daeSJed Brown 2885b8123daeSJed Brown .keywords: TS, timestep 28869be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 2887b8123daeSJed Brown @*/ 2888b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 2889b8123daeSJed Brown { 2890b8123daeSJed Brown PetscErrorCode ierr; 2891b8123daeSJed Brown 2892b8123daeSJed Brown PetscFunctionBegin; 2893b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2894ae60f76fSBarry Smith if (ts->prestage) { 2895ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 2896b8123daeSJed Brown } 2897b8123daeSJed Brown PetscFunctionReturn(0); 2898b8123daeSJed Brown } 2899b8123daeSJed Brown 2900b8123daeSJed Brown #undef __FUNCT__ 29019be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage" 29029be3e283SDebojyoti Ghosh /*@ 29039be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 29049be3e283SDebojyoti Ghosh 29059be3e283SDebojyoti Ghosh Collective on TS 29069be3e283SDebojyoti Ghosh 29079be3e283SDebojyoti Ghosh Input Parameters: 29089be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 29099be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 29109be3e283SDebojyoti Ghosh stageindex - Stage number 29119be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 29129be3e283SDebojyoti Ghosh of stages) with the stage solutions 29139be3e283SDebojyoti Ghosh 29149be3e283SDebojyoti Ghosh Notes: 29159be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 29169be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 29179be3e283SDebojyoti Ghosh 29189be3e283SDebojyoti Ghosh Level: developer 29199be3e283SDebojyoti Ghosh 29209be3e283SDebojyoti Ghosh .keywords: TS, timestep 29219be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 29229be3e283SDebojyoti Ghosh @*/ 29239be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 29249be3e283SDebojyoti Ghosh { 29259be3e283SDebojyoti Ghosh PetscErrorCode ierr; 29269be3e283SDebojyoti Ghosh 29279be3e283SDebojyoti Ghosh PetscFunctionBegin; 29289be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 29294beae5d8SLisandro Dalcin if (ts->poststage) { 29309be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 29319be3e283SDebojyoti Ghosh } 29329be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 29339be3e283SDebojyoti Ghosh } 29349be3e283SDebojyoti Ghosh 29359be3e283SDebojyoti Ghosh #undef __FUNCT__ 2936e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 2937ac226902SBarry Smith /*@C 2938000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 29393f2090d5SJed Brown called once at the end of each time step. 2940000e7ae3SMatthew Knepley 29413f9fe445SBarry Smith Logically Collective on TS 2942000e7ae3SMatthew Knepley 2943000e7ae3SMatthew Knepley Input Parameters: 2944000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2945000e7ae3SMatthew Knepley - func - The function 2946000e7ae3SMatthew Knepley 2947000e7ae3SMatthew Knepley Calling sequence of func: 2948b8123daeSJed Brown $ func (TS ts); 2949000e7ae3SMatthew Knepley 2950000e7ae3SMatthew Knepley Level: intermediate 2951000e7ae3SMatthew Knepley 2952000e7ae3SMatthew Knepley .keywords: TS, timestep 2953b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 2954000e7ae3SMatthew Knepley @*/ 29557087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 2956000e7ae3SMatthew Knepley { 2957000e7ae3SMatthew Knepley PetscFunctionBegin; 29580700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2959ae60f76fSBarry Smith ts->poststep = func; 2960000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2961000e7ae3SMatthew Knepley } 2962000e7ae3SMatthew Knepley 2963e74ef692SMatthew Knepley #undef __FUNCT__ 29643f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 296509ee8438SJed Brown /*@ 29663f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 29673f2090d5SJed Brown 29683f2090d5SJed Brown Collective on TS 29693f2090d5SJed Brown 29703f2090d5SJed Brown Input Parameters: 29713f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 29723f2090d5SJed Brown 29733f2090d5SJed Brown Notes: 29743f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 29753f2090d5SJed Brown so most users would not generally call this routine themselves. 29763f2090d5SJed Brown 29773f2090d5SJed Brown Level: developer 29783f2090d5SJed Brown 29793f2090d5SJed Brown .keywords: TS, timestep 29803f2090d5SJed Brown @*/ 29817087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 29823f2090d5SJed Brown { 29833f2090d5SJed Brown PetscErrorCode ierr; 29843f2090d5SJed Brown 29853f2090d5SJed Brown PetscFunctionBegin; 29860700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2987ae60f76fSBarry Smith if (ts->poststep) { 2988ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 298972ac3e02SJed Brown } 29903f2090d5SJed Brown PetscFunctionReturn(0); 29913f2090d5SJed Brown } 29923f2090d5SJed Brown 2993d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 2994d763cef2SBarry Smith 29954a2ae208SSatish Balay #undef __FUNCT__ 2996a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 2997d763cef2SBarry Smith /*@C 2998a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 2999d763cef2SBarry Smith timestep to display the iteration's progress. 3000d763cef2SBarry Smith 30013f9fe445SBarry Smith Logically Collective on TS 3002d763cef2SBarry Smith 3003d763cef2SBarry Smith Input Parameters: 3004d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3005e213d8f1SJed Brown . monitor - monitoring routine 3006329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 30070298fd71SBarry Smith monitor routine (use NULL if no context is desired) 3008b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 30090298fd71SBarry Smith (may be NULL) 3010d763cef2SBarry Smith 3011e213d8f1SJed Brown Calling sequence of monitor: 30120910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 3013d763cef2SBarry Smith 3014d763cef2SBarry Smith + ts - the TS context 301588c05cc5SBarry 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 301688c05cc5SBarry Smith been interpolated to) 30171f06c33eSBarry Smith . time - current time 30180910c330SBarry Smith . u - current iterate 3019d763cef2SBarry Smith - mctx - [optional] monitoring context 3020d763cef2SBarry Smith 3021d763cef2SBarry Smith Notes: 3022d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 3023d763cef2SBarry Smith already has been loaded. 3024d763cef2SBarry Smith 3025025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 3026025f1a04SBarry Smith 3027d763cef2SBarry Smith Level: intermediate 3028d763cef2SBarry Smith 3029d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3030d763cef2SBarry Smith 3031a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 3032d763cef2SBarry Smith @*/ 3033c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 3034d763cef2SBarry Smith { 3035d763cef2SBarry Smith PetscFunctionBegin; 30360700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 303717186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 3038d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 30398704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 3040d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 3041d763cef2SBarry Smith PetscFunctionReturn(0); 3042d763cef2SBarry Smith } 3043d763cef2SBarry Smith 30444a2ae208SSatish Balay #undef __FUNCT__ 3045a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 3046d763cef2SBarry Smith /*@C 3047a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 3048d763cef2SBarry Smith 30493f9fe445SBarry Smith Logically Collective on TS 3050d763cef2SBarry Smith 3051d763cef2SBarry Smith Input Parameters: 3052d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3053d763cef2SBarry Smith 3054d763cef2SBarry Smith Notes: 3055d763cef2SBarry Smith There is no way to remove a single, specific monitor. 3056d763cef2SBarry Smith 3057d763cef2SBarry Smith Level: intermediate 3058d763cef2SBarry Smith 3059d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3060d763cef2SBarry Smith 3061a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 3062d763cef2SBarry Smith @*/ 30637087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 3064d763cef2SBarry Smith { 3065d952e501SBarry Smith PetscErrorCode ierr; 3066d952e501SBarry Smith PetscInt i; 3067d952e501SBarry Smith 3068d763cef2SBarry Smith PetscFunctionBegin; 30690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3070d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 30718704b422SBarry Smith if (ts->monitordestroy[i]) { 30728704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 3073d952e501SBarry Smith } 3074d952e501SBarry Smith } 3075d763cef2SBarry Smith ts->numbermonitors = 0; 3076d763cef2SBarry Smith PetscFunctionReturn(0); 3077d763cef2SBarry Smith } 3078d763cef2SBarry Smith 30794a2ae208SSatish Balay #undef __FUNCT__ 3080a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 3081d8e5e3e6SSatish Balay /*@ 3082a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 30835516499fSSatish Balay 30845516499fSSatish Balay Level: intermediate 308541251cbbSSatish Balay 30865516499fSSatish Balay .keywords: TS, set, monitor 30875516499fSSatish Balay 308841251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 308941251cbbSSatish Balay @*/ 3090649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 3091d763cef2SBarry Smith { 3092dfbe8321SBarry Smith PetscErrorCode ierr; 30934d4332d5SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 3094d132466eSBarry Smith 3095d763cef2SBarry Smith PetscFunctionBegin; 30964d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3097649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 30988392e04aSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr); 3099649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3100d763cef2SBarry Smith PetscFunctionReturn(0); 3101d763cef2SBarry Smith } 3102d763cef2SBarry Smith 31034a2ae208SSatish Balay #undef __FUNCT__ 31049110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorSet" 31059110b2e7SHong Zhang /*@C 31069110b2e7SHong Zhang TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every 31079110b2e7SHong Zhang timestep to display the iteration's progress. 31089110b2e7SHong Zhang 31099110b2e7SHong Zhang Logically Collective on TS 31109110b2e7SHong Zhang 31119110b2e7SHong Zhang Input Parameters: 31129110b2e7SHong Zhang + ts - the TS context obtained from TSCreate() 31139110b2e7SHong Zhang . adjointmonitor - monitoring routine 31149110b2e7SHong Zhang . adjointmctx - [optional] user-defined context for private data for the 31159110b2e7SHong Zhang monitor routine (use NULL if no context is desired) 31169110b2e7SHong Zhang - adjointmonitordestroy - [optional] routine that frees monitor context 31179110b2e7SHong Zhang (may be NULL) 31189110b2e7SHong Zhang 31199110b2e7SHong Zhang Calling sequence of monitor: 31209110b2e7SHong Zhang $ int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx) 31219110b2e7SHong Zhang 31229110b2e7SHong Zhang + ts - the TS context 31239110b2e7SHong Zhang . steps - iteration number (after the final time step the monitor routine is called with a step of -1, this is at the final time which may have 31249110b2e7SHong Zhang been interpolated to) 31259110b2e7SHong Zhang . time - current time 31269110b2e7SHong Zhang . u - current iterate 31279110b2e7SHong Zhang . numcost - number of cost functionos 31289110b2e7SHong Zhang . lambda - sensitivities to initial conditions 31299110b2e7SHong Zhang . mu - sensitivities to parameters 31309110b2e7SHong Zhang - adjointmctx - [optional] adjoint monitoring context 31319110b2e7SHong Zhang 31329110b2e7SHong Zhang Notes: 31339110b2e7SHong Zhang This routine adds an additional monitor to the list of monitors that 31349110b2e7SHong Zhang already has been loaded. 31359110b2e7SHong Zhang 31369110b2e7SHong Zhang Fortran notes: Only a single monitor function can be set for each TS object 31379110b2e7SHong Zhang 31389110b2e7SHong Zhang Level: intermediate 31399110b2e7SHong Zhang 31409110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 31419110b2e7SHong Zhang 31429110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel() 31439110b2e7SHong Zhang @*/ 31449110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**)) 31459110b2e7SHong Zhang { 31469110b2e7SHong Zhang PetscFunctionBegin; 31479110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31489110b2e7SHong Zhang if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set"); 31499110b2e7SHong Zhang ts->adjointmonitor[ts->numberadjointmonitors] = adjointmonitor; 31509110b2e7SHong Zhang ts->adjointmonitordestroy[ts->numberadjointmonitors] = adjointmdestroy; 31519110b2e7SHong Zhang ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx; 31529110b2e7SHong Zhang PetscFunctionReturn(0); 31539110b2e7SHong Zhang } 31549110b2e7SHong Zhang 31559110b2e7SHong Zhang #undef __FUNCT__ 31569110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorCancel" 31579110b2e7SHong Zhang /*@C 31589110b2e7SHong Zhang TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object. 31599110b2e7SHong Zhang 31609110b2e7SHong Zhang Logically Collective on TS 31619110b2e7SHong Zhang 31629110b2e7SHong Zhang Input Parameters: 31639110b2e7SHong Zhang . ts - the TS context obtained from TSCreate() 31649110b2e7SHong Zhang 31659110b2e7SHong Zhang Notes: 31669110b2e7SHong Zhang There is no way to remove a single, specific monitor. 31679110b2e7SHong Zhang 31689110b2e7SHong Zhang Level: intermediate 31699110b2e7SHong Zhang 31709110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 31719110b2e7SHong Zhang 31729110b2e7SHong Zhang .seealso: TSAdjointMonitorSet() 31739110b2e7SHong Zhang @*/ 31749110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorCancel(TS ts) 31759110b2e7SHong Zhang { 31769110b2e7SHong Zhang PetscErrorCode ierr; 31779110b2e7SHong Zhang PetscInt i; 31789110b2e7SHong Zhang 31799110b2e7SHong Zhang PetscFunctionBegin; 31809110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31819110b2e7SHong Zhang for (i=0; i<ts->numberadjointmonitors; i++) { 31829110b2e7SHong Zhang if (ts->adjointmonitordestroy[i]) { 31839110b2e7SHong Zhang ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 31849110b2e7SHong Zhang } 31859110b2e7SHong Zhang } 31869110b2e7SHong Zhang ts->numberadjointmonitors = 0; 31879110b2e7SHong Zhang PetscFunctionReturn(0); 31889110b2e7SHong Zhang } 31899110b2e7SHong Zhang 31909110b2e7SHong Zhang #undef __FUNCT__ 3191110eb670SHong Zhang #define __FUNCT__ "TSAdjointMonitorDefault" 3192110eb670SHong Zhang /*@ 3193110eb670SHong Zhang TSAdjointMonitorDefault - Sets the Default monitor 3194110eb670SHong Zhang 3195110eb670SHong Zhang Level: intermediate 3196110eb670SHong Zhang 3197110eb670SHong Zhang .keywords: TS, set, monitor 3198110eb670SHong Zhang 3199110eb670SHong Zhang .seealso: TSAdjointMonitorSet() 3200110eb670SHong Zhang @*/ 3201110eb670SHong Zhang PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 3202110eb670SHong Zhang { 3203110eb670SHong Zhang PetscErrorCode ierr; 3204110eb670SHong Zhang PetscViewer viewer = (PetscViewer) dummy; 3205110eb670SHong Zhang 3206110eb670SHong Zhang PetscFunctionBegin; 3207110eb670SHong Zhang PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3208110eb670SHong Zhang ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3209110eb670SHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr); 3210110eb670SHong Zhang ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3211110eb670SHong Zhang PetscFunctionReturn(0); 3212110eb670SHong Zhang } 3213110eb670SHong Zhang 3214110eb670SHong Zhang #undef __FUNCT__ 3215cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 3216cd652676SJed Brown /*@ 3217cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 3218cd652676SJed Brown 3219cd652676SJed Brown Logically Collective on TS 3220cd652676SJed Brown 3221cd652676SJed Brown Input Argument: 3222cd652676SJed Brown . ts - time stepping context 3223cd652676SJed Brown 3224cd652676SJed Brown Output Argument: 3225cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 3226cd652676SJed Brown 3227cd652676SJed Brown Level: intermediate 3228cd652676SJed Brown 3229cd652676SJed Brown .keywords: TS, set 3230cd652676SJed Brown 3231cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 3232cd652676SJed Brown @*/ 3233cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 3234cd652676SJed Brown { 3235cd652676SJed Brown PetscFunctionBegin; 3236cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3237cd652676SJed Brown ts->retain_stages = flg; 3238cd652676SJed Brown PetscFunctionReturn(0); 3239cd652676SJed Brown } 3240cd652676SJed Brown 3241cd652676SJed Brown #undef __FUNCT__ 3242cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 3243cd652676SJed Brown /*@ 3244cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3245cd652676SJed Brown 3246cd652676SJed Brown Collective on TS 3247cd652676SJed Brown 3248cd652676SJed Brown Input Argument: 3249cd652676SJed Brown + ts - time stepping context 3250cd652676SJed Brown - t - time to interpolate to 3251cd652676SJed Brown 3252cd652676SJed Brown Output Argument: 32530910c330SBarry Smith . U - state at given time 3254cd652676SJed Brown 3255cd652676SJed Brown Notes: 3256cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 3257cd652676SJed Brown 3258cd652676SJed Brown Level: intermediate 3259cd652676SJed Brown 3260cd652676SJed Brown Developer Notes: 3261cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3262cd652676SJed Brown 3263cd652676SJed Brown .keywords: TS, set 3264cd652676SJed Brown 3265cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 3266cd652676SJed Brown @*/ 32670910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3268cd652676SJed Brown { 3269cd652676SJed Brown PetscErrorCode ierr; 3270cd652676SJed Brown 3271cd652676SJed Brown PetscFunctionBegin; 3272cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3273b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 32746712e2f1SBarry 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); 3275ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 32760910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3277cd652676SJed Brown PetscFunctionReturn(0); 3278cd652676SJed Brown } 3279cd652676SJed Brown 3280cd652676SJed Brown #undef __FUNCT__ 32814a2ae208SSatish Balay #define __FUNCT__ "TSStep" 3282d763cef2SBarry Smith /*@ 32836d9e5789SSean Farley TSStep - Steps one time step 3284d763cef2SBarry Smith 3285d763cef2SBarry Smith Collective on TS 3286d763cef2SBarry Smith 3287d763cef2SBarry Smith Input Parameter: 3288d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3289d763cef2SBarry Smith 329027829d71SBarry Smith Level: developer 3291d763cef2SBarry Smith 3292b8123daeSJed Brown Notes: 329327829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 329427829d71SBarry Smith 3295b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3296b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3297b8123daeSJed Brown 329825cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 329925cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 330025cb2221SBarry Smith 3301d763cef2SBarry Smith .keywords: TS, timestep, solve 3302d763cef2SBarry Smith 33039be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3304d763cef2SBarry Smith @*/ 3305193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3306d763cef2SBarry Smith { 33072fb8446cSMatthew G. Knepley DM dm; 3308dfbe8321SBarry Smith PetscErrorCode ierr; 3309fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3310d763cef2SBarry Smith 3311d763cef2SBarry Smith PetscFunctionBegin; 33120700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3313fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3314fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3315fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3316fffbeea8SBarry Smith " type = {Preprint},\n" 3317fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3318fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3319302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3320fffbeea8SBarry Smith 33212fb8446cSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3322d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 3323d405a339SMatthew Knepley 3324362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 332524655328SShri ts->ptime_prev = ts->ptime; 3326cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3327bbd56ea5SKarl Rupp 3328e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3329d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3330193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3331d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3332bbd56ea5SKarl Rupp 333324655328SShri ts->time_step_prev = ts->ptime - ts->ptime_prev; 3334cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3335362cd11cSLisandro Dalcin 3336362cd11cSLisandro Dalcin if (ts->reason < 0) { 3337cef5090cSJed Brown if (ts->errorifstepfailed) { 333808c7845fSBarry 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]); 333908c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3340d2daff3dSHong Zhang } 334108c7845fSBarry Smith } else if (!ts->reason) { 334208c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 334308c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 334408c7845fSBarry Smith } 33452c18e0fdSBarry Smith ts->total_steps++; 33468392e04aSShri Abhyankar ts->steprollback = PETSC_FALSE; 334708c7845fSBarry Smith PetscFunctionReturn(0); 334808c7845fSBarry Smith } 334908c7845fSBarry Smith 335008c7845fSBarry Smith #undef __FUNCT__ 335108c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep" 335208c7845fSBarry Smith /*@ 3353b957a604SHong Zhang TSAdjointStep - Steps one time step backward in the adjoint run 335408c7845fSBarry Smith 335508c7845fSBarry Smith Collective on TS 335608c7845fSBarry Smith 335708c7845fSBarry Smith Input Parameter: 335808c7845fSBarry Smith . ts - the TS context obtained from TSCreate() 335908c7845fSBarry Smith 33606a4d4014SLisandro Dalcin Level: intermediate 33616a4d4014SLisandro Dalcin 3362b957a604SHong Zhang .keywords: TS, adjoint, step 33636a4d4014SLisandro Dalcin 3364b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve() 33656a4d4014SLisandro Dalcin @*/ 336608c7845fSBarry Smith PetscErrorCode TSAdjointStep(TS ts) 33676a4d4014SLisandro Dalcin { 336808c7845fSBarry Smith DM dm; 33696a4d4014SLisandro Dalcin PetscErrorCode ierr; 33706a4d4014SLisandro Dalcin 33716a4d4014SLisandro Dalcin PetscFunctionBegin; 33724a2ae208SSatish Balay PetscValidHeaderSpecific(ts, TS_CLASSID,1); 337308c7845fSBarry Smith ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 337408c7845fSBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3375d763cef2SBarry Smith 3376d763cef2SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 337708c7845fSBarry Smith ts->ptime_prev = ts->ptime; 337808c7845fSBarry Smith ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3379685405a1SBarry Smith ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts, "-ts_view_solution");CHKERRQ(ierr); 3380d763cef2SBarry Smith 33816849ba73SBarry Smith ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 338242f2b339SBarry 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); 338342f2b339SBarry Smith ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr); 3384a6570f20SBarry Smith ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3385d763cef2SBarry Smith 3386cef5090cSJed Brown ts->time_step_prev = ts->ptime - ts->ptime_prev; 3387cef5090cSJed Brown ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3388362cd11cSLisandro Dalcin 3389362cd11cSLisandro Dalcin if (ts->reason < 0) { 3390cef5090cSJed Brown if (ts->errorifstepfailed) { 33916c4ed002SBarry 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]); 33926c4ed002SBarry Smith else if (ts->reason == TS_DIVERGED_STEP_REJECTED) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_reject or make negative to attempt recovery",TSConvergedReasons[ts->reason]); 33936c4ed002SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3394cef5090cSJed Brown } 3395362cd11cSLisandro Dalcin } else if (!ts->reason) { 339673b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3397db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3398362cd11cSLisandro Dalcin } 33992c18e0fdSBarry Smith ts->total_steps--; 3400d763cef2SBarry Smith PetscFunctionReturn(0); 3401d763cef2SBarry Smith } 3402d763cef2SBarry Smith 3403d763cef2SBarry Smith #undef __FUNCT__ 340405175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 340505175c85SJed Brown /*@ 340605175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 340705175c85SJed Brown 34081c3436cfSJed Brown Collective on TS 340905175c85SJed Brown 341005175c85SJed Brown Input Arguments: 34111c3436cfSJed Brown + ts - time stepping context 34121c3436cfSJed Brown . order - desired order of accuracy 34130298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 341405175c85SJed Brown 341505175c85SJed Brown Output Arguments: 34160910c330SBarry Smith . U - state at the end of the current step 341705175c85SJed Brown 341805175c85SJed Brown Level: advanced 341905175c85SJed Brown 3420108c343cSJed Brown Notes: 3421108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3422108c343cSJed 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. 3423108c343cSJed Brown 34241c3436cfSJed Brown .seealso: TSStep(), TSAdapt 342505175c85SJed Brown @*/ 34260910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 342705175c85SJed Brown { 342805175c85SJed Brown PetscErrorCode ierr; 342905175c85SJed Brown 343005175c85SJed Brown PetscFunctionBegin; 343105175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 343205175c85SJed Brown PetscValidType(ts,1); 34330910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3434ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 34350910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 343605175c85SJed Brown PetscFunctionReturn(0); 343705175c85SJed Brown } 343805175c85SJed Brown 3439d67e68b7SBarry Smith 344005175c85SJed Brown #undef __FUNCT__ 3441d763cef2SBarry Smith #define __FUNCT__ "TSSolve" 3442d763cef2SBarry Smith /*@ 3443d763cef2SBarry Smith TSSolve - Steps the requested number of timesteps. 3444d763cef2SBarry Smith 3445d763cef2SBarry Smith Collective on TS 3446d763cef2SBarry Smith 3447d763cef2SBarry Smith Input Parameter: 3448d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3449cc708dedSBarry Smith - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 34505a3a76d0SJed Brown 3451d763cef2SBarry Smith Level: beginner 3452d763cef2SBarry Smith 34535a3a76d0SJed Brown Notes: 34545a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 34555a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 34565a3a76d0SJed Brown stepped over the final time. 34575a3a76d0SJed Brown 3458d763cef2SBarry Smith .keywords: TS, timestep, solve 3459d763cef2SBarry Smith 3460d763cef2SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep() 3461d763cef2SBarry Smith @*/ 3462cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 3463d763cef2SBarry Smith { 3464b06615a5SLisandro Dalcin Vec solution; 3465d763cef2SBarry Smith PetscErrorCode ierr; 3466d763cef2SBarry Smith 3467d763cef2SBarry Smith PetscFunctionBegin; 3468d763cef2SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3469f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 347049354f04SShri 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 */ 3471b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 34720910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 3473b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3474b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3475b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 34765a3a76d0SJed Brown } 34770910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3478bbd56ea5SKarl Rupp } else if (u) { 34790910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 34805a3a76d0SJed Brown } 3481b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 3482d763cef2SBarry Smith /* reset time step and iteration counters */ 3483193ac0bcSJed Brown ts->steps = 0; 34845ef26d82SJed Brown ts->ksp_its = 0; 34855ef26d82SJed Brown ts->snes_its = 0; 3486c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3487c610991cSLisandro Dalcin ts->reject = 0; 3488193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3489193ac0bcSJed Brown 3490ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3491ec8db0baSMatthew G. Knepley { 3492ec8db0baSMatthew G. Knepley DM dm; 3493ec8db0baSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3494ec8db0baSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3495ec8db0baSMatthew G. Knepley } 3496f05ece33SBarry Smith 3497193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3498193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 34990910c330SBarry Smith ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 3500cc708dedSBarry Smith ts->solvetime = ts->ptime; 3501193ac0bcSJed Brown } else { 3502d763cef2SBarry Smith /* steps the requested number of timesteps. */ 3503db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3504db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3505cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 350611b05f00SHong Zhang if (ts->vec_costintegral) ts->costintegralfwd=PETSC_TRUE; 35076fea3669SShri Abhyankar if(ts->event) { 35086fea3669SShri Abhyankar ierr = TSEventMonitorInitialize(ts);CHKERRQ(ierr); 35096fea3669SShri Abhyankar } 3510e1a7a14fSJed Brown while (!ts->reason) { 3511193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 3512193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 3513aeb4809dSShri Abhyankar if (ts->event) { 3514aeb4809dSShri Abhyankar ierr = TSEventMonitor(ts);CHKERRQ(ierr); 35151eda64f1SShri Abhyankar } 35168392e04aSShri Abhyankar if(!ts->steprollback) { 3517cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 35181eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 3519aeb4809dSShri Abhyankar } 3520193ac0bcSJed Brown } 352149354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 35220910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 3523cc708dedSBarry Smith ts->solvetime = ts->max_time; 3524b06615a5SLisandro Dalcin solution = u; 35250574a7fbSJed Brown } else { 3526ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3527cc708dedSBarry Smith ts->solvetime = ts->ptime; 3528b06615a5SLisandro Dalcin solution = ts->vec_sol; 35290574a7fbSJed Brown } 3530b06615a5SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr); 3531685405a1SBarry Smith ierr = VecViewFromOptions(solution,(PetscObject) ts,"-ts_view_solution");CHKERRQ(ierr); 3532193ac0bcSJed Brown } 3533d2daff3dSHong Zhang 3534ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 3535dc1cb503SEmil Constantinescu ierr = VecViewFromOptions(ts->vec_sol,NULL,"-ts_view_solution");CHKERRQ(ierr); 353656f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 3537ef222394SBarry Smith if (ts->adjoint_solve) { 3538ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 35392b0a91c0SBarry Smith } 3540d763cef2SBarry Smith PetscFunctionReturn(0); 3541d763cef2SBarry Smith } 3542d763cef2SBarry Smith 3543d763cef2SBarry Smith #undef __FUNCT__ 3544c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve" 3545c88f2d44SBarry Smith /*@ 3546c88f2d44SBarry Smith TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 3547c88f2d44SBarry Smith 3548c88f2d44SBarry Smith Collective on TS 3549c88f2d44SBarry Smith 3550c88f2d44SBarry Smith Input Parameter: 35512c39e106SBarry Smith . ts - the TS context obtained from TSCreate() 3552c88f2d44SBarry Smith 3553e87fd094SBarry Smith Options Database: 3554e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions 3555e87fd094SBarry Smith 3556c88f2d44SBarry Smith Level: intermediate 3557c88f2d44SBarry Smith 3558c88f2d44SBarry Smith Notes: 3559c88f2d44SBarry Smith This must be called after a call to TSSolve() that solves the forward problem 3560c88f2d44SBarry Smith 356173b18844SBarry Smith By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time 356273b18844SBarry Smith 3563c88f2d44SBarry Smith .keywords: TS, timestep, solve 3564c88f2d44SBarry Smith 3565b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep() 3566c88f2d44SBarry Smith @*/ 35672c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts) 3568c88f2d44SBarry Smith { 3569c88f2d44SBarry Smith PetscErrorCode ierr; 3570c88f2d44SBarry Smith 3571c88f2d44SBarry Smith PetscFunctionBegin; 3572c88f2d44SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3573c88f2d44SBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3574c88f2d44SBarry Smith /* reset time step and iteration counters */ 3575c88f2d44SBarry Smith ts->steps = 0; 3576c88f2d44SBarry Smith ts->ksp_its = 0; 3577c88f2d44SBarry Smith ts->snes_its = 0; 3578c88f2d44SBarry Smith ts->num_snes_failures = 0; 3579c88f2d44SBarry Smith ts->reject = 0; 3580c88f2d44SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 3581c88f2d44SBarry Smith 358273b18844SBarry Smith if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps; 3583c88f2d44SBarry Smith 358473b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3585c88f2d44SBarry Smith while (!ts->reason) { 358655c52731SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 35879110b2e7SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 3588c88f2d44SBarry Smith if (ts->event) { 3589d0578d90SShri Abhyankar ierr = TSAdjointEventMonitor(ts);CHKERRQ(ierr); 3590d0578d90SShri Abhyankar } 3591616946c1SHong Zhang ierr = TSAdjointStep(ts);CHKERRQ(ierr); 3592c88f2d44SBarry Smith } 359326656371SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 359426656371SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 3595c88f2d44SBarry Smith ts->solvetime = ts->ptime; 3596685405a1SBarry Smith ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr); 3597c88f2d44SBarry Smith PetscFunctionReturn(0); 3598c88f2d44SBarry Smith } 3599c88f2d44SBarry Smith 3600c88f2d44SBarry Smith #undef __FUNCT__ 3601d763cef2SBarry Smith #define __FUNCT__ "TSMonitor" 36027db568b7SBarry Smith /*@C 3603228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 3604228d79bcSJed Brown 3605228d79bcSJed Brown Collective on TS 3606228d79bcSJed Brown 3607228d79bcSJed Brown Input Parameters: 3608228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 3609228d79bcSJed Brown . step - step number that has just completed 3610228d79bcSJed Brown . ptime - model time of the state 36110910c330SBarry Smith - u - state at the current model time 3612228d79bcSJed Brown 3613228d79bcSJed Brown Notes: 36147db568b7SBarry Smith TSMonitor() is typically used automatically within the time stepping implementations. 36157db568b7SBarry Smith Users would almost never call this routine directly. 3616228d79bcSJed Brown 36177db568b7SBarry Smith Level: developer 3618228d79bcSJed Brown 3619228d79bcSJed Brown .keywords: TS, timestep 3620228d79bcSJed Brown @*/ 36210910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 3622d763cef2SBarry Smith { 3623d763cef2SBarry Smith PetscErrorCode ierr; 3624d763cef2SBarry Smith PetscInt i,n = ts->numbermonitors; 3625d763cef2SBarry Smith 3626d763cef2SBarry Smith PetscFunctionBegin; 3627b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3628b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 36295edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 3630d763cef2SBarry Smith for (i=0; i<n; i++) { 36310910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 3632d763cef2SBarry Smith } 36335edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 3634d763cef2SBarry Smith PetscFunctionReturn(0); 3635d763cef2SBarry Smith } 3636d763cef2SBarry Smith 36379110b2e7SHong Zhang #undef __FUNCT__ 36389110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitor" 36397db568b7SBarry Smith /*@C 36409110b2e7SHong Zhang TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet() 36419110b2e7SHong Zhang 36429110b2e7SHong Zhang Collective on TS 36439110b2e7SHong Zhang 36449110b2e7SHong Zhang Input Parameters: 36459110b2e7SHong Zhang + ts - time stepping context obtained from TSCreate() 36469110b2e7SHong Zhang . step - step number that has just completed 36479110b2e7SHong Zhang . ptime - model time of the state 36489110b2e7SHong Zhang . u - state at the current model time 36499110b2e7SHong Zhang . numcost - number of cost functions (dimension of lambda or mu) 36509110b2e7SHong Zhang . lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 36519110b2e7SHong Zhang - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 36529110b2e7SHong Zhang 36539110b2e7SHong Zhang Notes: 36547db568b7SBarry Smith TSAdjointMonitor() is typically used automatically within the time stepping implementations. 36557db568b7SBarry Smith Users would almost never call this routine directly. 36569110b2e7SHong Zhang 36577db568b7SBarry Smith Level: developer 36589110b2e7SHong Zhang 36599110b2e7SHong Zhang .keywords: TS, timestep 36609110b2e7SHong Zhang @*/ 36619110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu) 36629110b2e7SHong Zhang { 36639110b2e7SHong Zhang PetscErrorCode ierr; 36649110b2e7SHong Zhang PetscInt i,n = ts->numberadjointmonitors; 36659110b2e7SHong Zhang 36669110b2e7SHong Zhang PetscFunctionBegin; 36679110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36689110b2e7SHong Zhang PetscValidHeaderSpecific(u,VEC_CLASSID,4); 36699110b2e7SHong Zhang ierr = VecLockPush(u);CHKERRQ(ierr); 36709110b2e7SHong Zhang for (i=0; i<n; i++) { 36719110b2e7SHong Zhang ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 36729110b2e7SHong Zhang } 36739110b2e7SHong Zhang ierr = VecLockPop(u);CHKERRQ(ierr); 36749110b2e7SHong Zhang PetscFunctionReturn(0); 36759110b2e7SHong Zhang } 36769110b2e7SHong Zhang 3677d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 3678d763cef2SBarry Smith #undef __FUNCT__ 3679a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 3680d763cef2SBarry Smith /*@C 36817db568b7SBarry Smith TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 3682a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 3683d763cef2SBarry Smith 3684d763cef2SBarry Smith Collective on TS 3685d763cef2SBarry Smith 3686d763cef2SBarry Smith Input Parameters: 3687d763cef2SBarry Smith + host - the X display to open, or null for the local machine 3688d763cef2SBarry Smith . label - the title to put in the title bar 36897c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 3690a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 3691a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 3692d763cef2SBarry Smith 3693d763cef2SBarry Smith Output Parameter: 36940b039ecaSBarry Smith . ctx - the context 3695d763cef2SBarry Smith 3696d763cef2SBarry Smith Options Database Key: 36974f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 36987db568b7SBarry Smith . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 36997db568b7SBarry Smith . -ts_monitor_lg_error - monitor the error 37007db568b7SBarry Smith . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 37017db568b7SBarry Smith . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 3702b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 3703d763cef2SBarry Smith 3704d763cef2SBarry Smith Notes: 3705a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 3706d763cef2SBarry Smith 37077db568b7SBarry Smith One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 37087db568b7SBarry Smith 37097db568b7SBarry Smith Many of the functions that control the monitoring have two forms: TSMonitorLGSet/GetXXXX() and TSMonitorLGCtxSet/GetXXXX() the first take a TS object as the 37107db568b7SBarry Smith first argument (if that TS object does not have a TSMonitorLGCtx associated with it the function call is ignored) and the second takes a TSMonitorLGCtx object 37117db568b7SBarry Smith as the first argument. 37127db568b7SBarry Smith 37137db568b7SBarry Smith One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 37147db568b7SBarry Smith 37157db568b7SBarry Smith 3716d763cef2SBarry Smith Level: intermediate 3717d763cef2SBarry Smith 37187db568b7SBarry Smith .keywords: TS, monitor, line graph, residual 3719d763cef2SBarry Smith 37207db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 37217db568b7SBarry Smith TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 37227db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 37237db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 37247db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 37257c922b88SBarry Smith 3726d763cef2SBarry Smith @*/ 3727a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 3728d763cef2SBarry Smith { 37297f52daa2SLisandro Dalcin PetscDraw draw; 3730dfbe8321SBarry Smith PetscErrorCode ierr; 3731d763cef2SBarry Smith 3732d763cef2SBarry Smith PetscFunctionBegin; 3733b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 37347f52daa2SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 37357f52daa2SLisandro Dalcin ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 37367f52daa2SLisandro Dalcin ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 3737b6fe0379SLisandro Dalcin ierr = PetscDrawLGSetUseMarkers((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr); 3738287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 37397f52daa2SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 3740a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 3741d763cef2SBarry Smith PetscFunctionReturn(0); 3742d763cef2SBarry Smith } 3743d763cef2SBarry Smith 37444a2ae208SSatish Balay #undef __FUNCT__ 37454f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 3746b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 3747d763cef2SBarry Smith { 37480b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 3749c32365f1SBarry Smith PetscReal x = ptime,y; 3750dfbe8321SBarry Smith PetscErrorCode ierr; 3751d763cef2SBarry Smith 3752d763cef2SBarry Smith PetscFunctionBegin; 3753b06615a5SLisandro Dalcin if (!step) { 3754a9f9c1f6SBarry Smith PetscDrawAxis axis; 3755a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 3756a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 3757a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 3758a9f9c1f6SBarry Smith } 3759c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 37600b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 3761b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 37620b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 37633923b477SBarry Smith } 3764d763cef2SBarry Smith PetscFunctionReturn(0); 3765d763cef2SBarry Smith } 3766d763cef2SBarry Smith 37674a2ae208SSatish Balay #undef __FUNCT__ 3768a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 3769d763cef2SBarry Smith /*@C 3770a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 3771a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 3772d763cef2SBarry Smith 37730b039ecaSBarry Smith Collective on TSMonitorLGCtx 3774d763cef2SBarry Smith 3775d763cef2SBarry Smith Input Parameter: 37760b039ecaSBarry Smith . ctx - the monitor context 3777d763cef2SBarry Smith 3778d763cef2SBarry Smith Level: intermediate 3779d763cef2SBarry Smith 3780d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 3781d763cef2SBarry Smith 37824f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 3783d763cef2SBarry Smith @*/ 3784a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 3785d763cef2SBarry Smith { 3786dfbe8321SBarry Smith PetscErrorCode ierr; 3787d763cef2SBarry Smith 3788d763cef2SBarry Smith PetscFunctionBegin; 37897684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 37907684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 37917684fa3eSBarry Smith } 37920b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 379331152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 3794387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 3795387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 3796387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 37970b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 3798d763cef2SBarry Smith PetscFunctionReturn(0); 3799d763cef2SBarry Smith } 3800d763cef2SBarry Smith 38014a2ae208SSatish Balay #undef __FUNCT__ 38024a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 3803d763cef2SBarry Smith /*@ 3804b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 3805d763cef2SBarry Smith 3806d763cef2SBarry Smith Not Collective 3807d763cef2SBarry Smith 3808d763cef2SBarry Smith Input Parameter: 3809d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3810d763cef2SBarry Smith 3811d763cef2SBarry Smith Output Parameter: 3812d763cef2SBarry Smith . t - the current time 3813d763cef2SBarry Smith 3814d763cef2SBarry Smith Level: beginner 3815d763cef2SBarry Smith 3816b8123daeSJed Brown Note: 3817b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 38189be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 3819b8123daeSJed Brown 3820d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3821d763cef2SBarry Smith 3822d763cef2SBarry Smith .keywords: TS, get, time 3823d763cef2SBarry Smith @*/ 38247087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 3825d763cef2SBarry Smith { 3826d763cef2SBarry Smith PetscFunctionBegin; 38270700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3828f7cf8827SBarry Smith PetscValidRealPointer(t,2); 3829d763cef2SBarry Smith *t = ts->ptime; 3830d763cef2SBarry Smith PetscFunctionReturn(0); 3831d763cef2SBarry Smith } 3832d763cef2SBarry Smith 38334a2ae208SSatish Balay #undef __FUNCT__ 3834e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime" 3835e5e524a1SHong Zhang /*@ 3836e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 3837e5e524a1SHong Zhang 3838e5e524a1SHong Zhang Not Collective 3839e5e524a1SHong Zhang 3840e5e524a1SHong Zhang Input Parameter: 3841e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 3842e5e524a1SHong Zhang 3843e5e524a1SHong Zhang Output Parameter: 3844e5e524a1SHong Zhang . t - the previous time 3845e5e524a1SHong Zhang 3846e5e524a1SHong Zhang Level: beginner 3847e5e524a1SHong Zhang 3848e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3849e5e524a1SHong Zhang 3850e5e524a1SHong Zhang .keywords: TS, get, time 3851e5e524a1SHong Zhang @*/ 3852e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 3853e5e524a1SHong Zhang { 3854e5e524a1SHong Zhang PetscFunctionBegin; 3855e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3856e5e524a1SHong Zhang PetscValidRealPointer(t,2); 3857e5e524a1SHong Zhang *t = ts->ptime_prev; 3858e5e524a1SHong Zhang PetscFunctionReturn(0); 3859e5e524a1SHong Zhang } 3860e5e524a1SHong Zhang 3861e5e524a1SHong Zhang #undef __FUNCT__ 38626a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 38636a4d4014SLisandro Dalcin /*@ 38646a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 38656a4d4014SLisandro Dalcin 38663f9fe445SBarry Smith Logically Collective on TS 38676a4d4014SLisandro Dalcin 38686a4d4014SLisandro Dalcin Input Parameters: 38696a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 38706a4d4014SLisandro Dalcin - time - the time 38716a4d4014SLisandro Dalcin 38726a4d4014SLisandro Dalcin Level: intermediate 38736a4d4014SLisandro Dalcin 38746a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 38756a4d4014SLisandro Dalcin 38766a4d4014SLisandro Dalcin .keywords: TS, set, time 38776a4d4014SLisandro Dalcin @*/ 38787087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 38796a4d4014SLisandro Dalcin { 38806a4d4014SLisandro Dalcin PetscFunctionBegin; 38810700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3882c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 38836a4d4014SLisandro Dalcin ts->ptime = t; 38846a4d4014SLisandro Dalcin PetscFunctionReturn(0); 38856a4d4014SLisandro Dalcin } 38866a4d4014SLisandro Dalcin 38876a4d4014SLisandro Dalcin #undef __FUNCT__ 38884a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 3889d763cef2SBarry Smith /*@C 3890d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 3891d763cef2SBarry Smith TS options in the database. 3892d763cef2SBarry Smith 38933f9fe445SBarry Smith Logically Collective on TS 3894d763cef2SBarry Smith 3895d763cef2SBarry Smith Input Parameter: 3896d763cef2SBarry Smith + ts - The TS context 3897d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3898d763cef2SBarry Smith 3899d763cef2SBarry Smith Notes: 3900d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3901d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3902d763cef2SBarry Smith hyphen. 3903d763cef2SBarry Smith 3904d763cef2SBarry Smith Level: advanced 3905d763cef2SBarry Smith 3906d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 3907d763cef2SBarry Smith 3908d763cef2SBarry Smith .seealso: TSSetFromOptions() 3909d763cef2SBarry Smith 3910d763cef2SBarry Smith @*/ 39117087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 3912d763cef2SBarry Smith { 3913dfbe8321SBarry Smith PetscErrorCode ierr; 3914089b2837SJed Brown SNES snes; 3915d763cef2SBarry Smith 3916d763cef2SBarry Smith PetscFunctionBegin; 39170700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3918d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3919089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3920089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3921d763cef2SBarry Smith PetscFunctionReturn(0); 3922d763cef2SBarry Smith } 3923d763cef2SBarry Smith 3924d763cef2SBarry Smith 39254a2ae208SSatish Balay #undef __FUNCT__ 39264a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 3927d763cef2SBarry Smith /*@C 3928d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 3929d763cef2SBarry Smith TS options in the database. 3930d763cef2SBarry Smith 39313f9fe445SBarry Smith Logically Collective on TS 3932d763cef2SBarry Smith 3933d763cef2SBarry Smith Input Parameter: 3934d763cef2SBarry Smith + ts - The TS context 3935d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3936d763cef2SBarry Smith 3937d763cef2SBarry Smith Notes: 3938d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3939d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3940d763cef2SBarry Smith hyphen. 3941d763cef2SBarry Smith 3942d763cef2SBarry Smith Level: advanced 3943d763cef2SBarry Smith 3944d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 3945d763cef2SBarry Smith 3946d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 3947d763cef2SBarry Smith 3948d763cef2SBarry Smith @*/ 39497087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 3950d763cef2SBarry Smith { 3951dfbe8321SBarry Smith PetscErrorCode ierr; 3952089b2837SJed Brown SNES snes; 3953d763cef2SBarry Smith 3954d763cef2SBarry Smith PetscFunctionBegin; 39550700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3956d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3957089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3958089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3959d763cef2SBarry Smith PetscFunctionReturn(0); 3960d763cef2SBarry Smith } 3961d763cef2SBarry Smith 39624a2ae208SSatish Balay #undef __FUNCT__ 39634a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 3964d763cef2SBarry Smith /*@C 3965d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 3966d763cef2SBarry Smith TS options in the database. 3967d763cef2SBarry Smith 3968d763cef2SBarry Smith Not Collective 3969d763cef2SBarry Smith 3970d763cef2SBarry Smith Input Parameter: 3971d763cef2SBarry Smith . ts - The TS context 3972d763cef2SBarry Smith 3973d763cef2SBarry Smith Output Parameter: 3974d763cef2SBarry Smith . prefix - A pointer to the prefix string used 3975d763cef2SBarry Smith 3976d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 3977d763cef2SBarry Smith sufficient length to hold the prefix. 3978d763cef2SBarry Smith 3979d763cef2SBarry Smith Level: intermediate 3980d763cef2SBarry Smith 3981d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 3982d763cef2SBarry Smith 3983d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 3984d763cef2SBarry Smith @*/ 39857087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 3986d763cef2SBarry Smith { 3987dfbe8321SBarry Smith PetscErrorCode ierr; 3988d763cef2SBarry Smith 3989d763cef2SBarry Smith PetscFunctionBegin; 39900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 39914482741eSBarry Smith PetscValidPointer(prefix,2); 3992d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3993d763cef2SBarry Smith PetscFunctionReturn(0); 3994d763cef2SBarry Smith } 3995d763cef2SBarry Smith 39964a2ae208SSatish Balay #undef __FUNCT__ 39974a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 3998d763cef2SBarry Smith /*@C 3999d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4000d763cef2SBarry Smith 4001d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4002d763cef2SBarry Smith 4003d763cef2SBarry Smith Input Parameter: 4004d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4005d763cef2SBarry Smith 4006d763cef2SBarry Smith Output Parameters: 4007e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4008e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4009e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4010e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4011d763cef2SBarry Smith 40120298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 4013d763cef2SBarry Smith 4014d763cef2SBarry Smith Level: intermediate 4015d763cef2SBarry Smith 401626d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 4017d763cef2SBarry Smith 4018d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 4019d763cef2SBarry Smith @*/ 4020e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4021d763cef2SBarry Smith { 4022089b2837SJed Brown PetscErrorCode ierr; 4023089b2837SJed Brown SNES snes; 402424989b8cSPeter Brune DM dm; 4025089b2837SJed Brown 4026d763cef2SBarry Smith PetscFunctionBegin; 4027089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4028e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 402924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 403024989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 4031d763cef2SBarry Smith PetscFunctionReturn(0); 4032d763cef2SBarry Smith } 4033d763cef2SBarry Smith 40341713a123SBarry Smith #undef __FUNCT__ 40352eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 40362eca1d9cSJed Brown /*@C 40372eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 40382eca1d9cSJed Brown 40392eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 40402eca1d9cSJed Brown 40412eca1d9cSJed Brown Input Parameter: 40422eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 40432eca1d9cSJed Brown 40442eca1d9cSJed Brown Output Parameters: 4045e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4046e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 40472eca1d9cSJed Brown . f - The function to compute the matrices 40482eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 40492eca1d9cSJed Brown 40500298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 40512eca1d9cSJed Brown 40522eca1d9cSJed Brown Level: advanced 40532eca1d9cSJed Brown 40542eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 40552eca1d9cSJed Brown 40562eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 40572eca1d9cSJed Brown @*/ 4058e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 40592eca1d9cSJed Brown { 4060089b2837SJed Brown PetscErrorCode ierr; 4061089b2837SJed Brown SNES snes; 406224989b8cSPeter Brune DM dm; 40630910c330SBarry Smith 40642eca1d9cSJed Brown PetscFunctionBegin; 4065089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4066f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4067e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 406824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 406924989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 40702eca1d9cSJed Brown PetscFunctionReturn(0); 40712eca1d9cSJed Brown } 40722eca1d9cSJed Brown 40736083293cSBarry Smith 40742eca1d9cSJed Brown #undef __FUNCT__ 407583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 40761713a123SBarry Smith /*@C 407783a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 40781713a123SBarry Smith VecView() for the solution at each timestep 40791713a123SBarry Smith 40801713a123SBarry Smith Collective on TS 40811713a123SBarry Smith 40821713a123SBarry Smith Input Parameters: 40831713a123SBarry Smith + ts - the TS context 40841713a123SBarry Smith . step - current time-step 4085142b95e3SSatish Balay . ptime - current time 40860298fd71SBarry Smith - dummy - either a viewer or NULL 40871713a123SBarry Smith 408899fdda47SBarry Smith Options Database: 408999fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 409099fdda47SBarry Smith 4091387f4636SBarry Smith Notes: the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 409299fdda47SBarry Smith will look bad 409399fdda47SBarry Smith 40941713a123SBarry Smith Level: intermediate 40951713a123SBarry Smith 40961713a123SBarry Smith .keywords: TS, vector, monitor, view 40971713a123SBarry Smith 4098a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 40991713a123SBarry Smith @*/ 41000910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 41011713a123SBarry Smith { 4102dfbe8321SBarry Smith PetscErrorCode ierr; 410383a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4104473a3ab2SBarry Smith PetscDraw draw; 41051713a123SBarry Smith 41061713a123SBarry Smith PetscFunctionBegin; 41076083293cSBarry Smith if (!step && ictx->showinitial) { 41086083293cSBarry Smith if (!ictx->initialsolution) { 41090910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 41101713a123SBarry Smith } 41110910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 41126083293cSBarry Smith } 4113b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 41140dcf80beSBarry Smith 41156083293cSBarry Smith if (ictx->showinitial) { 41166083293cSBarry Smith PetscReal pause; 41176083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 41186083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 41196083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 41206083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 41216083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 41226083293cSBarry Smith } 41230910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 4124473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 412551fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 4126473a3ab2SBarry Smith char time[32]; 4127473a3ab2SBarry Smith 4128473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 412985b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4130473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4131473a3ab2SBarry Smith h = yl + .95*(yr - yl); 413251fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4133473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4134473a3ab2SBarry Smith } 4135473a3ab2SBarry Smith 41366083293cSBarry Smith if (ictx->showinitial) { 41376083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 41386083293cSBarry Smith } 41391713a123SBarry Smith PetscFunctionReturn(0); 41401713a123SBarry Smith } 41411713a123SBarry Smith 41422d5ee99bSBarry Smith #undef __FUNCT__ 41439110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorDrawSensi" 41449110b2e7SHong Zhang /*@C 41459110b2e7SHong Zhang TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling 41469110b2e7SHong Zhang VecView() for the sensitivities to initial states at each timestep 41479110b2e7SHong Zhang 41489110b2e7SHong Zhang Collective on TS 41499110b2e7SHong Zhang 41509110b2e7SHong Zhang Input Parameters: 41519110b2e7SHong Zhang + ts - the TS context 41529110b2e7SHong Zhang . step - current time-step 41539110b2e7SHong Zhang . ptime - current time 41549110b2e7SHong Zhang . u - current state 41559110b2e7SHong Zhang . numcost - number of cost functions 41569110b2e7SHong Zhang . lambda - sensitivities to initial conditions 41579110b2e7SHong Zhang . mu - sensitivities to parameters 41589110b2e7SHong Zhang - dummy - either a viewer or NULL 41599110b2e7SHong Zhang 41609110b2e7SHong Zhang Level: intermediate 41619110b2e7SHong Zhang 41629110b2e7SHong Zhang .keywords: TS, vector, adjoint, monitor, view 41639110b2e7SHong Zhang 41649110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView() 41659110b2e7SHong Zhang @*/ 41669110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 41679110b2e7SHong Zhang { 41689110b2e7SHong Zhang PetscErrorCode ierr; 41699110b2e7SHong Zhang TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 41709110b2e7SHong Zhang PetscDraw draw; 4171a0046e2cSSatish Balay PetscReal xl,yl,xr,yr,h; 4172a0046e2cSSatish Balay char time[32]; 41739110b2e7SHong Zhang 41749110b2e7SHong Zhang PetscFunctionBegin; 41759110b2e7SHong Zhang if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 41769110b2e7SHong Zhang 41779110b2e7SHong Zhang ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr); 41789110b2e7SHong Zhang ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 41799110b2e7SHong Zhang ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 41809110b2e7SHong Zhang ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 41819110b2e7SHong Zhang h = yl + .95*(yr - yl); 41829110b2e7SHong Zhang ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 41839110b2e7SHong Zhang ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 41849110b2e7SHong Zhang 41859110b2e7SHong Zhang PetscFunctionReturn(0); 41869110b2e7SHong Zhang } 41879110b2e7SHong Zhang 41889110b2e7SHong Zhang #undef __FUNCT__ 41892d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase" 41902d5ee99bSBarry Smith /*@C 41912d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 41922d5ee99bSBarry Smith 41932d5ee99bSBarry Smith Collective on TS 41942d5ee99bSBarry Smith 41952d5ee99bSBarry Smith Input Parameters: 41962d5ee99bSBarry Smith + ts - the TS context 41972d5ee99bSBarry Smith . step - current time-step 41982d5ee99bSBarry Smith . ptime - current time 41992d5ee99bSBarry Smith - dummy - either a viewer or NULL 42002d5ee99bSBarry Smith 42012d5ee99bSBarry Smith Level: intermediate 42022d5ee99bSBarry Smith 42032d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 42042d5ee99bSBarry Smith 42052d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 42062d5ee99bSBarry Smith @*/ 42072d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 42082d5ee99bSBarry Smith { 42092d5ee99bSBarry Smith PetscErrorCode ierr; 42102d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 42112d5ee99bSBarry Smith PetscDraw draw; 42122d5ee99bSBarry Smith MPI_Comm comm; 42132d5ee99bSBarry Smith PetscInt n; 42142d5ee99bSBarry Smith PetscMPIInt size; 421551fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 42162d5ee99bSBarry Smith char time[32]; 42172d5ee99bSBarry Smith const PetscScalar *U; 42182d5ee99bSBarry Smith 42192d5ee99bSBarry Smith PetscFunctionBegin; 42202d5ee99bSBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 42212d5ee99bSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 42222d5ee99bSBarry Smith if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs"); 42232d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 42242d5ee99bSBarry Smith if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 42252d5ee99bSBarry Smith 42262d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 42272d5ee99bSBarry Smith 42282d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 42294b363babSBarry Smith ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 4230ba5783adSMatthew G Knepley if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) { 4231a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 4232a4494fc1SBarry Smith PetscFunctionReturn(0); 4233a4494fc1SBarry Smith } 42342d5ee99bSBarry Smith if (!step) ictx->color++; 42352d5ee99bSBarry Smith ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr); 42362d5ee99bSBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 42372d5ee99bSBarry Smith 42382d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 42394b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 424085b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 42412d5ee99bSBarry Smith h = yl + .95*(yr - yl); 424251fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 42432d5ee99bSBarry Smith } 42442d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 42452d5ee99bSBarry Smith PetscFunctionReturn(0); 42462d5ee99bSBarry Smith } 42472d5ee99bSBarry Smith 42481713a123SBarry Smith 42496c699258SBarry Smith #undef __FUNCT__ 425083a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 42516083293cSBarry Smith /*@C 425283a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 42536083293cSBarry Smith 42546083293cSBarry Smith Collective on TS 42556083293cSBarry Smith 42566083293cSBarry Smith Input Parameters: 42576083293cSBarry Smith . ctx - the monitor context 42586083293cSBarry Smith 42596083293cSBarry Smith Level: intermediate 42606083293cSBarry Smith 42616083293cSBarry Smith .keywords: TS, vector, monitor, view 42626083293cSBarry Smith 426383a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 42646083293cSBarry Smith @*/ 426583a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 42666083293cSBarry Smith { 42676083293cSBarry Smith PetscErrorCode ierr; 42686083293cSBarry Smith 42696083293cSBarry Smith PetscFunctionBegin; 42704b363babSBarry Smith ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr); 427183a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 427283a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 427383a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 42746083293cSBarry Smith PetscFunctionReturn(0); 42756083293cSBarry Smith } 42766083293cSBarry Smith 42776083293cSBarry Smith #undef __FUNCT__ 427883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 42796083293cSBarry Smith /*@C 428083a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 42816083293cSBarry Smith 42826083293cSBarry Smith Collective on TS 42836083293cSBarry Smith 42846083293cSBarry Smith Input Parameter: 42856083293cSBarry Smith . ts - time-step context 42866083293cSBarry Smith 42876083293cSBarry Smith Output Patameter: 42886083293cSBarry Smith . ctx - the monitor context 42896083293cSBarry Smith 429099fdda47SBarry Smith Options Database: 429199fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 429299fdda47SBarry Smith 42936083293cSBarry Smith Level: intermediate 42946083293cSBarry Smith 42956083293cSBarry Smith .keywords: TS, vector, monitor, view 42966083293cSBarry Smith 429783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 42986083293cSBarry Smith @*/ 429983a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 43006083293cSBarry Smith { 43016083293cSBarry Smith PetscErrorCode ierr; 43026083293cSBarry Smith 43036083293cSBarry Smith PetscFunctionBegin; 4304b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 430583a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4306e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4307bbd56ea5SKarl Rupp 430883a4ac43SBarry Smith (*ctx)->howoften = howoften; 4309473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 4310c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4311473a3ab2SBarry Smith 4312473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 4313c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 43142d5ee99bSBarry Smith (*ctx)->color = PETSC_DRAW_WHITE; 43156083293cSBarry Smith PetscFunctionReturn(0); 43166083293cSBarry Smith } 43176083293cSBarry Smith 43186083293cSBarry Smith #undef __FUNCT__ 431983a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 43203a471f94SBarry Smith /*@C 432183a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 43223a471f94SBarry Smith VecView() for the error at each timestep 43233a471f94SBarry Smith 43243a471f94SBarry Smith Collective on TS 43253a471f94SBarry Smith 43263a471f94SBarry Smith Input Parameters: 43273a471f94SBarry Smith + ts - the TS context 43283a471f94SBarry Smith . step - current time-step 43293a471f94SBarry Smith . ptime - current time 43300298fd71SBarry Smith - dummy - either a viewer or NULL 43313a471f94SBarry Smith 43323a471f94SBarry Smith Level: intermediate 43333a471f94SBarry Smith 43343a471f94SBarry Smith .keywords: TS, vector, monitor, view 43353a471f94SBarry Smith 43363a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 43373a471f94SBarry Smith @*/ 43380910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 43393a471f94SBarry Smith { 43403a471f94SBarry Smith PetscErrorCode ierr; 434183a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 434283a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 43433a471f94SBarry Smith Vec work; 43443a471f94SBarry Smith 43453a471f94SBarry Smith PetscFunctionBegin; 4346b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 43470910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 43483a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 43490910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 43503a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 43513a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 43523a471f94SBarry Smith PetscFunctionReturn(0); 43533a471f94SBarry Smith } 43543a471f94SBarry Smith 4355af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 43563a471f94SBarry Smith #undef __FUNCT__ 43576c699258SBarry Smith #define __FUNCT__ "TSSetDM" 43586c699258SBarry Smith /*@ 43596c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 43606c699258SBarry Smith 43613f9fe445SBarry Smith Logically Collective on TS and DM 43626c699258SBarry Smith 43636c699258SBarry Smith Input Parameters: 43646c699258SBarry Smith + ts - the preconditioner context 43656c699258SBarry Smith - dm - the dm 43666c699258SBarry Smith 43676c699258SBarry Smith Level: intermediate 43686c699258SBarry Smith 43696c699258SBarry Smith 43706c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 43716c699258SBarry Smith @*/ 43727087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 43736c699258SBarry Smith { 43746c699258SBarry Smith PetscErrorCode ierr; 4375089b2837SJed Brown SNES snes; 4376942e3340SBarry Smith DMTS tsdm; 43776c699258SBarry Smith 43786c699258SBarry Smith PetscFunctionBegin; 43790700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 438070663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4381942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 43822a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4383942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4384942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 438524989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 438624989b8cSPeter Brune tsdm->originaldm = dm; 438724989b8cSPeter Brune } 438824989b8cSPeter Brune } 43896bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 439024989b8cSPeter Brune } 43916c699258SBarry Smith ts->dm = dm; 4392bbd56ea5SKarl Rupp 4393089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4394089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 43956c699258SBarry Smith PetscFunctionReturn(0); 43966c699258SBarry Smith } 43976c699258SBarry Smith 43986c699258SBarry Smith #undef __FUNCT__ 43996c699258SBarry Smith #define __FUNCT__ "TSGetDM" 44006c699258SBarry Smith /*@ 44016c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 44026c699258SBarry Smith 44033f9fe445SBarry Smith Not Collective 44046c699258SBarry Smith 44056c699258SBarry Smith Input Parameter: 44066c699258SBarry Smith . ts - the preconditioner context 44076c699258SBarry Smith 44086c699258SBarry Smith Output Parameter: 44096c699258SBarry Smith . dm - the dm 44106c699258SBarry Smith 44116c699258SBarry Smith Level: intermediate 44126c699258SBarry Smith 44136c699258SBarry Smith 44146c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 44156c699258SBarry Smith @*/ 44167087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 44176c699258SBarry Smith { 4418496e6a7aSJed Brown PetscErrorCode ierr; 4419496e6a7aSJed Brown 44206c699258SBarry Smith PetscFunctionBegin; 44210700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4422496e6a7aSJed Brown if (!ts->dm) { 4423ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4424496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4425496e6a7aSJed Brown } 44266c699258SBarry Smith *dm = ts->dm; 44276c699258SBarry Smith PetscFunctionReturn(0); 44286c699258SBarry Smith } 44291713a123SBarry Smith 44300f5c6efeSJed Brown #undef __FUNCT__ 44310f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 44320f5c6efeSJed Brown /*@ 44330f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 44340f5c6efeSJed Brown 44353f9fe445SBarry Smith Logically Collective on SNES 44360f5c6efeSJed Brown 44370f5c6efeSJed Brown Input Parameter: 4438d42a1c89SJed Brown + snes - nonlinear solver 44390910c330SBarry Smith . U - the current state at which to evaluate the residual 4440d42a1c89SJed Brown - ctx - user context, must be a TS 44410f5c6efeSJed Brown 44420f5c6efeSJed Brown Output Parameter: 44430f5c6efeSJed Brown . F - the nonlinear residual 44440f5c6efeSJed Brown 44450f5c6efeSJed Brown Notes: 44460f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 44470f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 44480f5c6efeSJed Brown 44490f5c6efeSJed Brown Level: advanced 44500f5c6efeSJed Brown 44510f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 44520f5c6efeSJed Brown @*/ 44530910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 44540f5c6efeSJed Brown { 44550f5c6efeSJed Brown TS ts = (TS)ctx; 44560f5c6efeSJed Brown PetscErrorCode ierr; 44570f5c6efeSJed Brown 44580f5c6efeSJed Brown PetscFunctionBegin; 44590f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 44600910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 44610f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 44620f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 44630910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 44640f5c6efeSJed Brown PetscFunctionReturn(0); 44650f5c6efeSJed Brown } 44660f5c6efeSJed Brown 44670f5c6efeSJed Brown #undef __FUNCT__ 44680f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 44690f5c6efeSJed Brown /*@ 44700f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 44710f5c6efeSJed Brown 44720f5c6efeSJed Brown Collective on SNES 44730f5c6efeSJed Brown 44740f5c6efeSJed Brown Input Parameter: 44750f5c6efeSJed Brown + snes - nonlinear solver 44760910c330SBarry Smith . U - the current state at which to evaluate the residual 44770f5c6efeSJed Brown - ctx - user context, must be a TS 44780f5c6efeSJed Brown 44790f5c6efeSJed Brown Output Parameter: 44800f5c6efeSJed Brown + A - the Jacobian 44810f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 44820f5c6efeSJed Brown - flag - indicates any structure change in the matrix 44830f5c6efeSJed Brown 44840f5c6efeSJed Brown Notes: 44850f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 44860f5c6efeSJed Brown 44870f5c6efeSJed Brown Level: developer 44880f5c6efeSJed Brown 44890f5c6efeSJed Brown .seealso: SNESSetJacobian() 44900f5c6efeSJed Brown @*/ 4491d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 44920f5c6efeSJed Brown { 44930f5c6efeSJed Brown TS ts = (TS)ctx; 44940f5c6efeSJed Brown PetscErrorCode ierr; 44950f5c6efeSJed Brown 44960f5c6efeSJed Brown PetscFunctionBegin; 44970f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 44980910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 44990f5c6efeSJed Brown PetscValidPointer(A,3); 450094ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 45010f5c6efeSJed Brown PetscValidPointer(B,4); 450294ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 45030f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 4504d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 45050f5c6efeSJed Brown PetscFunctionReturn(0); 45060f5c6efeSJed Brown } 4507325fc9f4SBarry Smith 45080e4ef248SJed Brown #undef __FUNCT__ 45090e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 45100e4ef248SJed Brown /*@C 45119ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 45120e4ef248SJed Brown 45130e4ef248SJed Brown Collective on TS 45140e4ef248SJed Brown 45150e4ef248SJed Brown Input Arguments: 45160e4ef248SJed Brown + ts - time stepping context 45170e4ef248SJed Brown . t - time at which to evaluate 45180910c330SBarry Smith . U - state at which to evaluate 45190e4ef248SJed Brown - ctx - context 45200e4ef248SJed Brown 45210e4ef248SJed Brown Output Arguments: 45220e4ef248SJed Brown . F - right hand side 45230e4ef248SJed Brown 45240e4ef248SJed Brown Level: intermediate 45250e4ef248SJed Brown 45260e4ef248SJed Brown Notes: 45270e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 45280e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 45290e4ef248SJed Brown 45300e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 45310e4ef248SJed Brown @*/ 45320910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 45330e4ef248SJed Brown { 45340e4ef248SJed Brown PetscErrorCode ierr; 45350e4ef248SJed Brown Mat Arhs,Brhs; 45360e4ef248SJed Brown 45370e4ef248SJed Brown PetscFunctionBegin; 45380e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 4539d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 45400910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 45410e4ef248SJed Brown PetscFunctionReturn(0); 45420e4ef248SJed Brown } 45430e4ef248SJed Brown 45440e4ef248SJed Brown #undef __FUNCT__ 45450e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 45460e4ef248SJed Brown /*@C 45470e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 45480e4ef248SJed Brown 45490e4ef248SJed Brown Collective on TS 45500e4ef248SJed Brown 45510e4ef248SJed Brown Input Arguments: 45520e4ef248SJed Brown + ts - time stepping context 45530e4ef248SJed Brown . t - time at which to evaluate 45540910c330SBarry Smith . U - state at which to evaluate 45550e4ef248SJed Brown - ctx - context 45560e4ef248SJed Brown 45570e4ef248SJed Brown Output Arguments: 45580e4ef248SJed Brown + A - pointer to operator 45590e4ef248SJed Brown . B - pointer to preconditioning matrix 45600e4ef248SJed Brown - flg - matrix structure flag 45610e4ef248SJed Brown 45620e4ef248SJed Brown Level: intermediate 45630e4ef248SJed Brown 45640e4ef248SJed Brown Notes: 45650e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 45660e4ef248SJed Brown 45670e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 45680e4ef248SJed Brown @*/ 4569d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 45700e4ef248SJed Brown { 45710e4ef248SJed Brown PetscFunctionBegin; 45720e4ef248SJed Brown PetscFunctionReturn(0); 45730e4ef248SJed Brown } 45740e4ef248SJed Brown 45750026cea9SSean Farley #undef __FUNCT__ 45760026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 45770026cea9SSean Farley /*@C 45780026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 45790026cea9SSean Farley 45800026cea9SSean Farley Collective on TS 45810026cea9SSean Farley 45820026cea9SSean Farley Input Arguments: 45830026cea9SSean Farley + ts - time stepping context 45840026cea9SSean Farley . t - time at which to evaluate 45850910c330SBarry Smith . U - state at which to evaluate 45860910c330SBarry Smith . Udot - time derivative of state vector 45870026cea9SSean Farley - ctx - context 45880026cea9SSean Farley 45890026cea9SSean Farley Output Arguments: 45900026cea9SSean Farley . F - left hand side 45910026cea9SSean Farley 45920026cea9SSean Farley Level: intermediate 45930026cea9SSean Farley 45940026cea9SSean Farley Notes: 45950910c330SBarry 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 45960026cea9SSean Farley user is required to write their own TSComputeIFunction. 45970026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 45980026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 45990026cea9SSean Farley 46009ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 46019ae8fd06SBarry Smith 46029ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 46030026cea9SSean Farley @*/ 46040910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 46050026cea9SSean Farley { 46060026cea9SSean Farley PetscErrorCode ierr; 46070026cea9SSean Farley Mat A,B; 46080026cea9SSean Farley 46090026cea9SSean Farley PetscFunctionBegin; 46100298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 4611d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 46120910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 46130026cea9SSean Farley PetscFunctionReturn(0); 46140026cea9SSean Farley } 46150026cea9SSean Farley 46160026cea9SSean Farley #undef __FUNCT__ 46170026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 46180026cea9SSean Farley /*@C 4619b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 46200026cea9SSean Farley 46210026cea9SSean Farley Collective on TS 46220026cea9SSean Farley 46230026cea9SSean Farley Input Arguments: 46240026cea9SSean Farley + ts - time stepping context 46250026cea9SSean Farley . t - time at which to evaluate 46260910c330SBarry Smith . U - state at which to evaluate 46270910c330SBarry Smith . Udot - time derivative of state vector 46280026cea9SSean Farley . shift - shift to apply 46290026cea9SSean Farley - ctx - context 46300026cea9SSean Farley 46310026cea9SSean Farley Output Arguments: 46320026cea9SSean Farley + A - pointer to operator 46330026cea9SSean Farley . B - pointer to preconditioning matrix 46340026cea9SSean Farley - flg - matrix structure flag 46350026cea9SSean Farley 4636b41af12eSJed Brown Level: advanced 46370026cea9SSean Farley 46380026cea9SSean Farley Notes: 46390026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 46400026cea9SSean Farley 4641b41af12eSJed Brown It is only appropriate for problems of the form 4642b41af12eSJed Brown 4643b41af12eSJed Brown $ M Udot = F(U,t) 4644b41af12eSJed Brown 4645b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4646b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4647b41af12eSJed Brown an implicit operator of the form 4648b41af12eSJed Brown 4649b41af12eSJed Brown $ shift*M + J 4650b41af12eSJed Brown 4651b41af12eSJed 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 4652b41af12eSJed Brown a copy of M or reassemble it when requested. 4653b41af12eSJed Brown 46540026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 46550026cea9SSean Farley @*/ 4656d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 46570026cea9SSean Farley { 4658b41af12eSJed Brown PetscErrorCode ierr; 4659b41af12eSJed Brown 46600026cea9SSean Farley PetscFunctionBegin; 466194ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4662b41af12eSJed Brown ts->ijacobian.shift = shift; 46630026cea9SSean Farley PetscFunctionReturn(0); 46640026cea9SSean Farley } 4665b41af12eSJed Brown 4666e817cc15SEmil Constantinescu #undef __FUNCT__ 4667e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType" 4668e817cc15SEmil Constantinescu /*@ 4669e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 4670e817cc15SEmil Constantinescu 4671e817cc15SEmil Constantinescu Not Collective 4672e817cc15SEmil Constantinescu 4673e817cc15SEmil Constantinescu Input Parameter: 4674e817cc15SEmil Constantinescu . ts - the TS context 4675e817cc15SEmil Constantinescu 4676e817cc15SEmil Constantinescu Output Parameter: 46774e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4678e817cc15SEmil Constantinescu 4679e817cc15SEmil Constantinescu Level: beginner 4680e817cc15SEmil Constantinescu 4681e817cc15SEmil Constantinescu .keywords: TS, equation type 4682e817cc15SEmil Constantinescu 4683e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 4684e817cc15SEmil Constantinescu @*/ 4685e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4686e817cc15SEmil Constantinescu { 4687e817cc15SEmil Constantinescu PetscFunctionBegin; 4688e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4689e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 4690e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 4691e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4692e817cc15SEmil Constantinescu } 4693e817cc15SEmil Constantinescu 4694e817cc15SEmil Constantinescu #undef __FUNCT__ 4695e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType" 4696e817cc15SEmil Constantinescu /*@ 4697e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 4698e817cc15SEmil Constantinescu 4699e817cc15SEmil Constantinescu Not Collective 4700e817cc15SEmil Constantinescu 4701e817cc15SEmil Constantinescu Input Parameter: 4702e817cc15SEmil Constantinescu + ts - the TS context 47031297b224SEmil Constantinescu - equation_type - see TSEquationType 4704e817cc15SEmil Constantinescu 4705e817cc15SEmil Constantinescu Level: advanced 4706e817cc15SEmil Constantinescu 4707e817cc15SEmil Constantinescu .keywords: TS, equation type 4708e817cc15SEmil Constantinescu 4709e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 4710e817cc15SEmil Constantinescu @*/ 4711e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4712e817cc15SEmil Constantinescu { 4713e817cc15SEmil Constantinescu PetscFunctionBegin; 4714e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4715e817cc15SEmil Constantinescu ts->equation_type = equation_type; 4716e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4717e817cc15SEmil Constantinescu } 47180026cea9SSean Farley 47194af1b03aSJed Brown #undef __FUNCT__ 47204af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 47214af1b03aSJed Brown /*@ 47224af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 47234af1b03aSJed Brown 47244af1b03aSJed Brown Not Collective 47254af1b03aSJed Brown 47264af1b03aSJed Brown Input Parameter: 47274af1b03aSJed Brown . ts - the TS context 47284af1b03aSJed Brown 47294af1b03aSJed Brown Output Parameter: 47304af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 47314af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 47324af1b03aSJed Brown 4733487e0bb9SJed Brown Level: beginner 47344af1b03aSJed Brown 4735cd652676SJed Brown Notes: 4736cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 47374af1b03aSJed Brown 47384af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 47394af1b03aSJed Brown 47404af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 47414af1b03aSJed Brown @*/ 47424af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 47434af1b03aSJed Brown { 47444af1b03aSJed Brown PetscFunctionBegin; 47454af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 47464af1b03aSJed Brown PetscValidPointer(reason,2); 47474af1b03aSJed Brown *reason = ts->reason; 47484af1b03aSJed Brown PetscFunctionReturn(0); 47494af1b03aSJed Brown } 47504af1b03aSJed Brown 4751fb1732b5SBarry Smith #undef __FUNCT__ 4752d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 4753d6ad946cSShri Abhyankar /*@ 4754d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4755d6ad946cSShri Abhyankar 4756d6ad946cSShri Abhyankar Not Collective 4757d6ad946cSShri Abhyankar 4758d6ad946cSShri Abhyankar Input Parameter: 4759d6ad946cSShri Abhyankar + ts - the TS context 4760d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4761d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4762d6ad946cSShri Abhyankar 4763f5abba47SShri Abhyankar Level: advanced 4764d6ad946cSShri Abhyankar 4765d6ad946cSShri Abhyankar Notes: 4766d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 4767d6ad946cSShri Abhyankar 4768d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 4769d6ad946cSShri Abhyankar 4770d6ad946cSShri Abhyankar .seealso: TSConvergedReason 4771d6ad946cSShri Abhyankar @*/ 4772d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4773d6ad946cSShri Abhyankar { 4774d6ad946cSShri Abhyankar PetscFunctionBegin; 4775d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4776d6ad946cSShri Abhyankar ts->reason = reason; 4777d6ad946cSShri Abhyankar PetscFunctionReturn(0); 4778d6ad946cSShri Abhyankar } 4779d6ad946cSShri Abhyankar 4780d6ad946cSShri Abhyankar #undef __FUNCT__ 4781cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 4782cc708dedSBarry Smith /*@ 4783cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 4784cc708dedSBarry Smith 4785cc708dedSBarry Smith Not Collective 4786cc708dedSBarry Smith 4787cc708dedSBarry Smith Input Parameter: 4788cc708dedSBarry Smith . ts - the TS context 4789cc708dedSBarry Smith 4790cc708dedSBarry Smith Output Parameter: 4791cc708dedSBarry Smith . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 4792cc708dedSBarry Smith 4793487e0bb9SJed Brown Level: beginner 4794cc708dedSBarry Smith 4795cc708dedSBarry Smith Notes: 4796cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 4797cc708dedSBarry Smith 4798cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 4799cc708dedSBarry Smith 4800cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 4801cc708dedSBarry Smith @*/ 4802cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 4803cc708dedSBarry Smith { 4804cc708dedSBarry Smith PetscFunctionBegin; 4805cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4806cc708dedSBarry Smith PetscValidPointer(ftime,2); 4807cc708dedSBarry Smith *ftime = ts->solvetime; 4808cc708dedSBarry Smith PetscFunctionReturn(0); 4809cc708dedSBarry Smith } 4810cc708dedSBarry Smith 4811cc708dedSBarry Smith #undef __FUNCT__ 48122c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps" 48132c18e0fdSBarry Smith /*@ 48142c18e0fdSBarry Smith TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 48152c18e0fdSBarry Smith 48162c18e0fdSBarry Smith Not Collective 48172c18e0fdSBarry Smith 48182c18e0fdSBarry Smith Input Parameter: 48192c18e0fdSBarry Smith . ts - the TS context 48202c18e0fdSBarry Smith 48212c18e0fdSBarry Smith Output Parameter: 48222c18e0fdSBarry Smith . steps - the number of steps 48232c18e0fdSBarry Smith 48242c18e0fdSBarry Smith Level: beginner 48252c18e0fdSBarry Smith 48262c18e0fdSBarry Smith Notes: 48272c18e0fdSBarry Smith Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 48282c18e0fdSBarry Smith 48292c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test 48302c18e0fdSBarry Smith 48312c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 48322c18e0fdSBarry Smith @*/ 48332c18e0fdSBarry Smith PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 48342c18e0fdSBarry Smith { 48352c18e0fdSBarry Smith PetscFunctionBegin; 48362c18e0fdSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 48372c18e0fdSBarry Smith PetscValidPointer(steps,2); 48382c18e0fdSBarry Smith *steps = ts->total_steps; 48392c18e0fdSBarry Smith PetscFunctionReturn(0); 48402c18e0fdSBarry Smith } 48412c18e0fdSBarry Smith 48422c18e0fdSBarry Smith #undef __FUNCT__ 48435ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 48449f67acb7SJed Brown /*@ 48455ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 48469f67acb7SJed Brown used by the time integrator. 48479f67acb7SJed Brown 48489f67acb7SJed Brown Not Collective 48499f67acb7SJed Brown 48509f67acb7SJed Brown Input Parameter: 48519f67acb7SJed Brown . ts - TS context 48529f67acb7SJed Brown 48539f67acb7SJed Brown Output Parameter: 48549f67acb7SJed Brown . nits - number of nonlinear iterations 48559f67acb7SJed Brown 48569f67acb7SJed Brown Notes: 48579f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 48589f67acb7SJed Brown 48599f67acb7SJed Brown Level: intermediate 48609f67acb7SJed Brown 48619f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 48629f67acb7SJed Brown 48635ef26d82SJed Brown .seealso: TSGetKSPIterations() 48649f67acb7SJed Brown @*/ 48655ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 48669f67acb7SJed Brown { 48679f67acb7SJed Brown PetscFunctionBegin; 48689f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 48699f67acb7SJed Brown PetscValidIntPointer(nits,2); 48705ef26d82SJed Brown *nits = ts->snes_its; 48719f67acb7SJed Brown PetscFunctionReturn(0); 48729f67acb7SJed Brown } 48739f67acb7SJed Brown 48749f67acb7SJed Brown #undef __FUNCT__ 48755ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 48769f67acb7SJed Brown /*@ 48775ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 48789f67acb7SJed Brown used by the time integrator. 48799f67acb7SJed Brown 48809f67acb7SJed Brown Not Collective 48819f67acb7SJed Brown 48829f67acb7SJed Brown Input Parameter: 48839f67acb7SJed Brown . ts - TS context 48849f67acb7SJed Brown 48859f67acb7SJed Brown Output Parameter: 48869f67acb7SJed Brown . lits - number of linear iterations 48879f67acb7SJed Brown 48889f67acb7SJed Brown Notes: 48899f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 48909f67acb7SJed Brown 48919f67acb7SJed Brown Level: intermediate 48929f67acb7SJed Brown 48939f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 48949f67acb7SJed Brown 48955ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 48969f67acb7SJed Brown @*/ 48975ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 48989f67acb7SJed Brown { 48999f67acb7SJed Brown PetscFunctionBegin; 49009f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 49019f67acb7SJed Brown PetscValidIntPointer(lits,2); 49025ef26d82SJed Brown *lits = ts->ksp_its; 49039f67acb7SJed Brown PetscFunctionReturn(0); 49049f67acb7SJed Brown } 49059f67acb7SJed Brown 49069f67acb7SJed Brown #undef __FUNCT__ 4907cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 4908cef5090cSJed Brown /*@ 4909cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4910cef5090cSJed Brown 4911cef5090cSJed Brown Not Collective 4912cef5090cSJed Brown 4913cef5090cSJed Brown Input Parameter: 4914cef5090cSJed Brown . ts - TS context 4915cef5090cSJed Brown 4916cef5090cSJed Brown Output Parameter: 4917cef5090cSJed Brown . rejects - number of steps rejected 4918cef5090cSJed Brown 4919cef5090cSJed Brown Notes: 4920cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4921cef5090cSJed Brown 4922cef5090cSJed Brown Level: intermediate 4923cef5090cSJed Brown 4924cef5090cSJed Brown .keywords: TS, get, number 4925cef5090cSJed Brown 49265ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 4927cef5090cSJed Brown @*/ 4928cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 4929cef5090cSJed Brown { 4930cef5090cSJed Brown PetscFunctionBegin; 4931cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4932cef5090cSJed Brown PetscValidIntPointer(rejects,2); 4933cef5090cSJed Brown *rejects = ts->reject; 4934cef5090cSJed Brown PetscFunctionReturn(0); 4935cef5090cSJed Brown } 4936cef5090cSJed Brown 4937cef5090cSJed Brown #undef __FUNCT__ 4938cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 4939cef5090cSJed Brown /*@ 4940cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 4941cef5090cSJed Brown 4942cef5090cSJed Brown Not Collective 4943cef5090cSJed Brown 4944cef5090cSJed Brown Input Parameter: 4945cef5090cSJed Brown . ts - TS context 4946cef5090cSJed Brown 4947cef5090cSJed Brown Output Parameter: 4948cef5090cSJed Brown . fails - number of failed nonlinear solves 4949cef5090cSJed Brown 4950cef5090cSJed Brown Notes: 4951cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4952cef5090cSJed Brown 4953cef5090cSJed Brown Level: intermediate 4954cef5090cSJed Brown 4955cef5090cSJed Brown .keywords: TS, get, number 4956cef5090cSJed Brown 49575ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 4958cef5090cSJed Brown @*/ 4959cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 4960cef5090cSJed Brown { 4961cef5090cSJed Brown PetscFunctionBegin; 4962cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4963cef5090cSJed Brown PetscValidIntPointer(fails,2); 4964cef5090cSJed Brown *fails = ts->num_snes_failures; 4965cef5090cSJed Brown PetscFunctionReturn(0); 4966cef5090cSJed Brown } 4967cef5090cSJed Brown 4968cef5090cSJed Brown #undef __FUNCT__ 4969cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 4970cef5090cSJed Brown /*@ 4971cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 4972cef5090cSJed Brown 4973cef5090cSJed Brown Not Collective 4974cef5090cSJed Brown 4975cef5090cSJed Brown Input Parameter: 4976cef5090cSJed Brown + ts - TS context 4977cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 4978cef5090cSJed Brown 4979cef5090cSJed Brown Notes: 4980cef5090cSJed Brown The counter is reset to zero for each step 4981cef5090cSJed Brown 4982cef5090cSJed Brown Options Database Key: 4983cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4984cef5090cSJed Brown 4985cef5090cSJed Brown Level: intermediate 4986cef5090cSJed Brown 4987cef5090cSJed Brown .keywords: TS, set, maximum, number 4988cef5090cSJed Brown 49895ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4990cef5090cSJed Brown @*/ 4991cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 4992cef5090cSJed Brown { 4993cef5090cSJed Brown PetscFunctionBegin; 4994cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4995cef5090cSJed Brown ts->max_reject = rejects; 4996cef5090cSJed Brown PetscFunctionReturn(0); 4997cef5090cSJed Brown } 4998cef5090cSJed Brown 4999cef5090cSJed Brown #undef __FUNCT__ 5000cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 5001cef5090cSJed Brown /*@ 5002cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 5003cef5090cSJed Brown 5004cef5090cSJed Brown Not Collective 5005cef5090cSJed Brown 5006cef5090cSJed Brown Input Parameter: 5007cef5090cSJed Brown + ts - TS context 5008cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 5009cef5090cSJed Brown 5010cef5090cSJed Brown Notes: 5011cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 5012cef5090cSJed Brown 5013cef5090cSJed Brown Options Database Key: 5014cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 5015cef5090cSJed Brown 5016cef5090cSJed Brown Level: intermediate 5017cef5090cSJed Brown 5018cef5090cSJed Brown .keywords: TS, set, maximum, number 5019cef5090cSJed Brown 50205ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 5021cef5090cSJed Brown @*/ 5022cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 5023cef5090cSJed Brown { 5024cef5090cSJed Brown PetscFunctionBegin; 5025cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5026cef5090cSJed Brown ts->max_snes_failures = fails; 5027cef5090cSJed Brown PetscFunctionReturn(0); 5028cef5090cSJed Brown } 5029cef5090cSJed Brown 5030cef5090cSJed Brown #undef __FUNCT__ 50314e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails" 5032cef5090cSJed Brown /*@ 5033cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 5034cef5090cSJed Brown 5035cef5090cSJed Brown Not Collective 5036cef5090cSJed Brown 5037cef5090cSJed Brown Input Parameter: 5038cef5090cSJed Brown + ts - TS context 5039cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 5040cef5090cSJed Brown 5041cef5090cSJed Brown Options Database Key: 5042cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 5043cef5090cSJed Brown 5044cef5090cSJed Brown Level: intermediate 5045cef5090cSJed Brown 5046cef5090cSJed Brown .keywords: TS, set, error 5047cef5090cSJed Brown 50485ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5049cef5090cSJed Brown @*/ 5050cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 5051cef5090cSJed Brown { 5052cef5090cSJed Brown PetscFunctionBegin; 5053cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5054cef5090cSJed Brown ts->errorifstepfailed = err; 5055cef5090cSJed Brown PetscFunctionReturn(0); 5056cef5090cSJed Brown } 5057cef5090cSJed Brown 5058cef5090cSJed Brown #undef __FUNCT__ 5059*fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSolution" 5060fb1732b5SBarry Smith /*@C 5061*fde5950dSBarry Smith TSMonitorSolution - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file or a PetscDraw object 5062fb1732b5SBarry Smith 5063fb1732b5SBarry Smith Collective on TS 5064fb1732b5SBarry Smith 5065fb1732b5SBarry Smith Input Parameters: 5066fb1732b5SBarry Smith + ts - the TS context 5067fb1732b5SBarry Smith . step - current time-step 5068fb1732b5SBarry Smith . ptime - current time 50690910c330SBarry Smith . u - current state 5070fb1732b5SBarry Smith - viewer - binary viewer 5071fb1732b5SBarry Smith 5072fb1732b5SBarry Smith Level: intermediate 5073fb1732b5SBarry Smith 5074fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 5075fb1732b5SBarry Smith 5076fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5077fb1732b5SBarry Smith @*/ 5078*fde5950dSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 5079fb1732b5SBarry Smith { 5080fb1732b5SBarry Smith PetscErrorCode ierr; 5081ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 5082fb1732b5SBarry Smith 5083fb1732b5SBarry Smith PetscFunctionBegin; 50840910c330SBarry Smith ierr = VecView(u,v);CHKERRQ(ierr); 5085ed81e22dSJed Brown PetscFunctionReturn(0); 5086ed81e22dSJed Brown } 5087ed81e22dSJed Brown 5088ed81e22dSJed Brown #undef __FUNCT__ 5089ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 5090ed81e22dSJed Brown /*@C 5091ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5092ed81e22dSJed Brown 5093ed81e22dSJed Brown Collective on TS 5094ed81e22dSJed Brown 5095ed81e22dSJed Brown Input Parameters: 5096ed81e22dSJed Brown + ts - the TS context 5097ed81e22dSJed Brown . step - current time-step 5098ed81e22dSJed Brown . ptime - current time 50990910c330SBarry Smith . u - current state 5100ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5101ed81e22dSJed Brown 5102ed81e22dSJed Brown Level: intermediate 5103ed81e22dSJed Brown 5104ed81e22dSJed Brown Notes: 5105ed81e22dSJed 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. 5106ed81e22dSJed Brown These are named according to the file name template. 5107ed81e22dSJed Brown 5108ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5109ed81e22dSJed Brown 5110ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5111ed81e22dSJed Brown 5112ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5113ed81e22dSJed Brown @*/ 51140910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5115ed81e22dSJed Brown { 5116ed81e22dSJed Brown PetscErrorCode ierr; 5117ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 5118ed81e22dSJed Brown PetscViewer viewer; 5119ed81e22dSJed Brown 5120ed81e22dSJed Brown PetscFunctionBegin; 51218caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5122ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 51230910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 5124ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5125ed81e22dSJed Brown PetscFunctionReturn(0); 5126ed81e22dSJed Brown } 5127ed81e22dSJed Brown 5128ed81e22dSJed Brown #undef __FUNCT__ 5129ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 5130ed81e22dSJed Brown /*@C 5131ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5132ed81e22dSJed Brown 5133ed81e22dSJed Brown Collective on TS 5134ed81e22dSJed Brown 5135ed81e22dSJed Brown Input Parameters: 5136ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5137ed81e22dSJed Brown 5138ed81e22dSJed Brown Level: intermediate 5139ed81e22dSJed Brown 5140ed81e22dSJed Brown Note: 5141ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5142ed81e22dSJed Brown 5143ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5144ed81e22dSJed Brown 5145ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5146ed81e22dSJed Brown @*/ 5147ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5148ed81e22dSJed Brown { 5149ed81e22dSJed Brown PetscErrorCode ierr; 5150ed81e22dSJed Brown 5151ed81e22dSJed Brown PetscFunctionBegin; 5152ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5153fb1732b5SBarry Smith PetscFunctionReturn(0); 5154fb1732b5SBarry Smith } 5155fb1732b5SBarry Smith 515684df9cb4SJed Brown #undef __FUNCT__ 5157552698daSJed Brown #define __FUNCT__ "TSGetAdapt" 515884df9cb4SJed Brown /*@ 5159552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 516084df9cb4SJed Brown 5161ed81e22dSJed Brown Collective on TS if controller has not been created yet 516284df9cb4SJed Brown 516384df9cb4SJed Brown Input Arguments: 5164ed81e22dSJed Brown . ts - time stepping context 516584df9cb4SJed Brown 516684df9cb4SJed Brown Output Arguments: 5167ed81e22dSJed Brown . adapt - adaptive controller 516884df9cb4SJed Brown 516984df9cb4SJed Brown Level: intermediate 517084df9cb4SJed Brown 5171ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 517284df9cb4SJed Brown @*/ 5173552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 517484df9cb4SJed Brown { 517584df9cb4SJed Brown PetscErrorCode ierr; 517684df9cb4SJed Brown 517784df9cb4SJed Brown PetscFunctionBegin; 517884df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 517984df9cb4SJed Brown PetscValidPointer(adapt,2); 518084df9cb4SJed Brown if (!ts->adapt) { 5181ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 51823bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 51831c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 518484df9cb4SJed Brown } 518584df9cb4SJed Brown *adapt = ts->adapt; 518684df9cb4SJed Brown PetscFunctionReturn(0); 518784df9cb4SJed Brown } 5188d6ebe24aSShri Abhyankar 5189d6ebe24aSShri Abhyankar #undef __FUNCT__ 51901c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 51911c3436cfSJed Brown /*@ 51921c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 51931c3436cfSJed Brown 51941c3436cfSJed Brown Logically Collective 51951c3436cfSJed Brown 51961c3436cfSJed Brown Input Arguments: 51971c3436cfSJed Brown + ts - time integration context 51981c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 51990298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 52001c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 52010298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 52021c3436cfSJed Brown 5203a3cdaa26SBarry Smith Options Database keys: 5204a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5205a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5206a3cdaa26SBarry Smith 52073ff766beSShri Abhyankar Notes: 52083ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 52093ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 52103ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 52113ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 52123ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 52133ff766beSShri Abhyankar differential variables. 52143ff766beSShri Abhyankar 52151c3436cfSJed Brown Level: beginner 52161c3436cfSJed Brown 5217c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 52181c3436cfSJed Brown @*/ 52191c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 52201c3436cfSJed Brown { 52211c3436cfSJed Brown PetscErrorCode ierr; 52221c3436cfSJed Brown 52231c3436cfSJed Brown PetscFunctionBegin; 5224c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 52251c3436cfSJed Brown if (vatol) { 52261c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 52271c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 5228bbd56ea5SKarl Rupp 52291c3436cfSJed Brown ts->vatol = vatol; 52301c3436cfSJed Brown } 5231c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 52321c3436cfSJed Brown if (vrtol) { 52331c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 52341c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 5235bbd56ea5SKarl Rupp 52361c3436cfSJed Brown ts->vrtol = vrtol; 52371c3436cfSJed Brown } 52381c3436cfSJed Brown PetscFunctionReturn(0); 52391c3436cfSJed Brown } 52401c3436cfSJed Brown 52411c3436cfSJed Brown #undef __FUNCT__ 5242c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 5243c5033834SJed Brown /*@ 5244c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5245c5033834SJed Brown 5246c5033834SJed Brown Logically Collective 5247c5033834SJed Brown 5248c5033834SJed Brown Input Arguments: 5249c5033834SJed Brown . ts - time integration context 5250c5033834SJed Brown 5251c5033834SJed Brown Output Arguments: 52520298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 52530298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 52540298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 52550298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 5256c5033834SJed Brown 5257c5033834SJed Brown Level: beginner 5258c5033834SJed Brown 5259c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5260c5033834SJed Brown @*/ 5261c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5262c5033834SJed Brown { 5263c5033834SJed Brown PetscFunctionBegin; 5264c5033834SJed Brown if (atol) *atol = ts->atol; 5265c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5266c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5267c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 5268c5033834SJed Brown PetscFunctionReturn(0); 5269c5033834SJed Brown } 5270c5033834SJed Brown 5271c5033834SJed Brown #undef __FUNCT__ 52729c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2" 52739c6b16b5SShri Abhyankar /*@ 5274a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 52759c6b16b5SShri Abhyankar 52769c6b16b5SShri Abhyankar Collective on TS 52779c6b16b5SShri Abhyankar 52789c6b16b5SShri Abhyankar Input Arguments: 52799c6b16b5SShri Abhyankar + ts - time stepping context 5280a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5281a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 52829c6b16b5SShri Abhyankar 52839c6b16b5SShri Abhyankar Output Arguments: 52849c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 52859c6b16b5SShri Abhyankar 52869c6b16b5SShri Abhyankar Level: developer 52879c6b16b5SShri Abhyankar 5288deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 52899c6b16b5SShri Abhyankar @*/ 5290a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm) 52919c6b16b5SShri Abhyankar { 52929c6b16b5SShri Abhyankar PetscErrorCode ierr; 52939c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 52949c6b16b5SShri Abhyankar const PetscScalar *u,*y; 52959c6b16b5SShri Abhyankar PetscReal sum,gsum; 52969c6b16b5SShri Abhyankar PetscReal tol; 52979c6b16b5SShri Abhyankar 52989c6b16b5SShri Abhyankar PetscFunctionBegin; 52999c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5300a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5301a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5302a4868fbcSLisandro Dalcin PetscValidType(U,2); 5303a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5304a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5305a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5306a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 53079c6b16b5SShri Abhyankar 53089c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 53099c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 53109c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 53119c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 53129c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 53139c6b16b5SShri Abhyankar sum = 0.; 53149c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 53159c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 53169c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53179c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 53189c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 53199c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53209c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 53219c6b16b5SShri Abhyankar } 53229c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53239c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 53249c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 53259c6b16b5SShri Abhyankar const PetscScalar *atol; 53269c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53279c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 53289c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53299c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 53309c6b16b5SShri Abhyankar } 53319c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53329c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 53339c6b16b5SShri Abhyankar const PetscScalar *rtol; 53349c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 53359c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 53369c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53379c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 53389c6b16b5SShri Abhyankar } 53399c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 53409c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 53419c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 53429c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53439c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 53449c6b16b5SShri Abhyankar } 53459c6b16b5SShri Abhyankar } 53469c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 53479c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 53489c6b16b5SShri Abhyankar 5349b2566f29SBarry Smith ierr = MPIU_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 53509c6b16b5SShri Abhyankar *norm = PetscSqrtReal(gsum / N); 53519c6b16b5SShri Abhyankar 53529c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 53539c6b16b5SShri Abhyankar PetscFunctionReturn(0); 53549c6b16b5SShri Abhyankar } 53559c6b16b5SShri Abhyankar 53569c6b16b5SShri Abhyankar #undef __FUNCT__ 53579c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity" 53589c6b16b5SShri Abhyankar /*@ 5359a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 53609c6b16b5SShri Abhyankar 53619c6b16b5SShri Abhyankar Collective on TS 53629c6b16b5SShri Abhyankar 53639c6b16b5SShri Abhyankar Input Arguments: 53649c6b16b5SShri Abhyankar + ts - time stepping context 5365a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5366a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 53679c6b16b5SShri Abhyankar 53689c6b16b5SShri Abhyankar Output Arguments: 53699c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 53709c6b16b5SShri Abhyankar 53719c6b16b5SShri Abhyankar Level: developer 53729c6b16b5SShri Abhyankar 5373deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 53749c6b16b5SShri Abhyankar @*/ 5375a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm) 53769c6b16b5SShri Abhyankar { 53779c6b16b5SShri Abhyankar PetscErrorCode ierr; 53789c6b16b5SShri Abhyankar PetscInt i,n,N,rstart,k; 53799c6b16b5SShri Abhyankar const PetscScalar *u,*y; 53809c6b16b5SShri Abhyankar PetscReal max,gmax; 53819c6b16b5SShri Abhyankar PetscReal tol; 53829c6b16b5SShri Abhyankar 53839c6b16b5SShri Abhyankar PetscFunctionBegin; 53849c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5385a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5386a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5387a4868fbcSLisandro Dalcin PetscValidType(U,2); 5388a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5389a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5390a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5391a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 53929c6b16b5SShri Abhyankar 53939c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 53949c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 53959c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 53969c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 53979c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 53989c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 53999c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 54009c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 54019c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 54029c6b16b5SShri Abhyankar k = 0; 54039c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 54049c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 54059c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 54069c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54079c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 54089c6b16b5SShri Abhyankar } 54099c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 54109c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 54119c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 54129c6b16b5SShri Abhyankar const PetscScalar *atol; 54139c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 54149c6b16b5SShri Abhyankar k = 0; 54159c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 54169c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 54179c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 54189c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54199c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 54209c6b16b5SShri Abhyankar } 54219c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 54229c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 54239c6b16b5SShri Abhyankar const PetscScalar *rtol; 54249c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 54259c6b16b5SShri Abhyankar k = 0; 54269c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 54279c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 54289c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 54299c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54309c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 54319c6b16b5SShri Abhyankar } 54329c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 54339c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 54349c6b16b5SShri Abhyankar k = 0; 54359c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 54369c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 54379c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 54389c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54399c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 54409c6b16b5SShri Abhyankar } 54419c6b16b5SShri Abhyankar } 54429c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 54439c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 54449c6b16b5SShri Abhyankar 5445b2566f29SBarry Smith ierr = MPIU_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 54469c6b16b5SShri Abhyankar *norm = gmax; 54479c6b16b5SShri Abhyankar 54489c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 54499c6b16b5SShri Abhyankar PetscFunctionReturn(0); 54509c6b16b5SShri Abhyankar } 54519c6b16b5SShri Abhyankar 54529c6b16b5SShri Abhyankar #undef __FUNCT__ 54537619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm" 54541c3436cfSJed Brown /*@ 5455a4868fbcSLisandro Dalcin TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors 54561c3436cfSJed Brown 54571c3436cfSJed Brown Collective on TS 54581c3436cfSJed Brown 54591c3436cfSJed Brown Input Arguments: 54601c3436cfSJed Brown + ts - time stepping context 5461a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5462a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5463a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 54647619abb3SShri 54651c3436cfSJed Brown Output Arguments: 54661c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 54671c3436cfSJed Brown 5468a4868fbcSLisandro Dalcin 5469a4868fbcSLisandro Dalcin Options Database Keys: 5470a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5471a4868fbcSLisandro Dalcin 54721c3436cfSJed Brown Level: developer 54731c3436cfSJed Brown 5474deea92deSShri .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 54751c3436cfSJed Brown @*/ 5476a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm) 54771c3436cfSJed Brown { 54788beabaa1SBarry Smith PetscErrorCode ierr; 54791c3436cfSJed Brown 54801c3436cfSJed Brown PetscFunctionBegin; 5481a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 5482a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNorm2(ts,U,Y,norm);CHKERRQ(ierr); 5483a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 5484a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm);CHKERRQ(ierr); 5485a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 54861c3436cfSJed Brown PetscFunctionReturn(0); 54871c3436cfSJed Brown } 54881c3436cfSJed Brown 54891c3436cfSJed Brown #undef __FUNCT__ 54908d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 54918d59e960SJed Brown /*@ 54928d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 54938d59e960SJed Brown 54948d59e960SJed Brown Logically Collective on TS 54958d59e960SJed Brown 54968d59e960SJed Brown Input Arguments: 54978d59e960SJed Brown + ts - time stepping context 54988d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 54998d59e960SJed Brown 55008d59e960SJed Brown Note: 55018d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 55028d59e960SJed Brown 55038d59e960SJed Brown Level: intermediate 55048d59e960SJed Brown 55058d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 55068d59e960SJed Brown @*/ 55078d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 55088d59e960SJed Brown { 55098d59e960SJed Brown PetscFunctionBegin; 55108d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 55118d59e960SJed Brown ts->cfltime_local = cfltime; 55128d59e960SJed Brown ts->cfltime = -1.; 55138d59e960SJed Brown PetscFunctionReturn(0); 55148d59e960SJed Brown } 55158d59e960SJed Brown 55168d59e960SJed Brown #undef __FUNCT__ 55178d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 55188d59e960SJed Brown /*@ 55198d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 55208d59e960SJed Brown 55218d59e960SJed Brown Collective on TS 55228d59e960SJed Brown 55238d59e960SJed Brown Input Arguments: 55248d59e960SJed Brown . ts - time stepping context 55258d59e960SJed Brown 55268d59e960SJed Brown Output Arguments: 55278d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 55288d59e960SJed Brown 55298d59e960SJed Brown Level: advanced 55308d59e960SJed Brown 55318d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 55328d59e960SJed Brown @*/ 55338d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 55348d59e960SJed Brown { 55358d59e960SJed Brown PetscErrorCode ierr; 55368d59e960SJed Brown 55378d59e960SJed Brown PetscFunctionBegin; 55388d59e960SJed Brown if (ts->cfltime < 0) { 5539b2566f29SBarry Smith ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 55408d59e960SJed Brown } 55418d59e960SJed Brown *cfltime = ts->cfltime; 55428d59e960SJed Brown PetscFunctionReturn(0); 55438d59e960SJed Brown } 55448d59e960SJed Brown 55458d59e960SJed Brown #undef __FUNCT__ 5546d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 5547d6ebe24aSShri Abhyankar /*@ 5548d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5549d6ebe24aSShri Abhyankar 5550d6ebe24aSShri Abhyankar Input Parameters: 5551d6ebe24aSShri Abhyankar . ts - the TS context. 5552d6ebe24aSShri Abhyankar . xl - lower bound. 5553d6ebe24aSShri Abhyankar . xu - upper bound. 5554d6ebe24aSShri Abhyankar 5555d6ebe24aSShri Abhyankar Notes: 5556d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 5557e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 5558d6ebe24aSShri Abhyankar 55592bd2b0e6SSatish Balay Level: advanced 55602bd2b0e6SSatish Balay 5561d6ebe24aSShri Abhyankar @*/ 5562d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5563d6ebe24aSShri Abhyankar { 5564d6ebe24aSShri Abhyankar PetscErrorCode ierr; 5565d6ebe24aSShri Abhyankar SNES snes; 5566d6ebe24aSShri Abhyankar 5567d6ebe24aSShri Abhyankar PetscFunctionBegin; 5568d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 5569d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 5570d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 5571d6ebe24aSShri Abhyankar } 5572d6ebe24aSShri Abhyankar 5573325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 5574c6db04a5SJed Brown #include <mex.h> 5575325fc9f4SBarry Smith 5576325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 5577325fc9f4SBarry Smith 5578325fc9f4SBarry Smith #undef __FUNCT__ 5579325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 5580325fc9f4SBarry Smith /* 5581325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 5582325fc9f4SBarry Smith TSSetFunctionMatlab(). 5583325fc9f4SBarry Smith 5584325fc9f4SBarry Smith Collective on TS 5585325fc9f4SBarry Smith 5586325fc9f4SBarry Smith Input Parameters: 5587325fc9f4SBarry Smith + snes - the TS context 55880910c330SBarry Smith - u - input vector 5589325fc9f4SBarry Smith 5590325fc9f4SBarry Smith Output Parameter: 5591325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 5592325fc9f4SBarry Smith 5593325fc9f4SBarry Smith Notes: 5594325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 5595325fc9f4SBarry Smith implementations, so most users would not generally call this routine 5596325fc9f4SBarry Smith themselves. 5597325fc9f4SBarry Smith 5598325fc9f4SBarry Smith Level: developer 5599325fc9f4SBarry Smith 5600325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5601325fc9f4SBarry Smith 5602325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5603325fc9f4SBarry Smith */ 56040910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 5605325fc9f4SBarry Smith { 5606325fc9f4SBarry Smith PetscErrorCode ierr; 5607325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5608325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 5609325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 5610325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 5611325fc9f4SBarry Smith 5612325fc9f4SBarry Smith PetscFunctionBegin; 5613325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 56140910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 56150910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 5616325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 56170910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 5618325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 5619325fc9f4SBarry Smith 5620325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 56210910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 56220910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 56230910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 5624bbd56ea5SKarl Rupp 5625325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5626325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 5627325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5628325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5629325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 5630325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 5631325fc9f4SBarry Smith prhs[6] = sctx->ctx; 5632325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 5633325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5634325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5635325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5636325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5637325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5638325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5639325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5640325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5641325fc9f4SBarry Smith PetscFunctionReturn(0); 5642325fc9f4SBarry Smith } 5643325fc9f4SBarry Smith 5644325fc9f4SBarry Smith 5645325fc9f4SBarry Smith #undef __FUNCT__ 5646325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 5647325fc9f4SBarry Smith /* 5648325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 5649325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 5650e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 5651325fc9f4SBarry Smith 5652325fc9f4SBarry Smith Logically Collective on TS 5653325fc9f4SBarry Smith 5654325fc9f4SBarry Smith Input Parameters: 5655325fc9f4SBarry Smith + ts - the TS context 5656325fc9f4SBarry Smith - func - function evaluation routine 5657325fc9f4SBarry Smith 5658325fc9f4SBarry Smith Calling sequence of func: 56590910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 5660325fc9f4SBarry Smith 5661325fc9f4SBarry Smith Level: beginner 5662325fc9f4SBarry Smith 5663325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5664325fc9f4SBarry Smith 5665325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5666325fc9f4SBarry Smith */ 5667cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 5668325fc9f4SBarry Smith { 5669325fc9f4SBarry Smith PetscErrorCode ierr; 5670325fc9f4SBarry Smith TSMatlabContext *sctx; 5671325fc9f4SBarry Smith 5672325fc9f4SBarry Smith PetscFunctionBegin; 5673325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5674325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5675325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5676325fc9f4SBarry Smith /* 5677325fc9f4SBarry Smith This should work, but it doesn't 5678325fc9f4SBarry Smith sctx->ctx = ctx; 5679325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5680325fc9f4SBarry Smith */ 5681325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5682bbd56ea5SKarl Rupp 56830298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 5684325fc9f4SBarry Smith PetscFunctionReturn(0); 5685325fc9f4SBarry Smith } 5686325fc9f4SBarry Smith 5687325fc9f4SBarry Smith #undef __FUNCT__ 5688325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 5689325fc9f4SBarry Smith /* 5690325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 5691325fc9f4SBarry Smith TSSetJacobianMatlab(). 5692325fc9f4SBarry Smith 5693325fc9f4SBarry Smith Collective on TS 5694325fc9f4SBarry Smith 5695325fc9f4SBarry Smith Input Parameters: 5696cdcf91faSSean Farley + ts - the TS context 56970910c330SBarry Smith . u - input vector 5698325fc9f4SBarry Smith . A, B - the matrices 5699325fc9f4SBarry Smith - ctx - user context 5700325fc9f4SBarry Smith 5701325fc9f4SBarry Smith Level: developer 5702325fc9f4SBarry Smith 5703325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5704325fc9f4SBarry Smith 5705325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5706325fc9f4SBarry Smith @*/ 5707f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 5708325fc9f4SBarry Smith { 5709325fc9f4SBarry Smith PetscErrorCode ierr; 5710325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5711325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 5712325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 5713325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 5714325fc9f4SBarry Smith 5715325fc9f4SBarry Smith PetscFunctionBegin; 5716cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 57170910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 5718325fc9f4SBarry Smith 57190910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 5720325fc9f4SBarry Smith 5721cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 57220910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 57230910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 57240910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 57250910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 5726bbd56ea5SKarl Rupp 5727325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5728325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 5729325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5730325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5731325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 5732325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 5733325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 5734325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 5735325fc9f4SBarry Smith prhs[8] = sctx->ctx; 5736325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 5737325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5738325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5739325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5740325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5741325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5742325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5743325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5744325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 5745325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 5746325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5747325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 5748325fc9f4SBarry Smith PetscFunctionReturn(0); 5749325fc9f4SBarry Smith } 5750325fc9f4SBarry Smith 5751325fc9f4SBarry Smith 5752325fc9f4SBarry Smith #undef __FUNCT__ 5753325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 5754325fc9f4SBarry Smith /* 5755325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 5756e3c5b3baSBarry 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 5757325fc9f4SBarry Smith 5758325fc9f4SBarry Smith Logically Collective on TS 5759325fc9f4SBarry Smith 5760325fc9f4SBarry Smith Input Parameters: 5761cdcf91faSSean Farley + ts - the TS context 5762325fc9f4SBarry Smith . A,B - Jacobian matrices 5763325fc9f4SBarry Smith . func - function evaluation routine 5764325fc9f4SBarry Smith - ctx - user context 5765325fc9f4SBarry Smith 5766325fc9f4SBarry Smith Calling sequence of func: 57670910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 5768325fc9f4SBarry Smith 5769325fc9f4SBarry Smith 5770325fc9f4SBarry Smith Level: developer 5771325fc9f4SBarry Smith 5772325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5773325fc9f4SBarry Smith 5774325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5775325fc9f4SBarry Smith */ 5776cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 5777325fc9f4SBarry Smith { 5778325fc9f4SBarry Smith PetscErrorCode ierr; 5779325fc9f4SBarry Smith TSMatlabContext *sctx; 5780325fc9f4SBarry Smith 5781325fc9f4SBarry Smith PetscFunctionBegin; 5782325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5783325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5784325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5785325fc9f4SBarry Smith /* 5786325fc9f4SBarry Smith This should work, but it doesn't 5787325fc9f4SBarry Smith sctx->ctx = ctx; 5788325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5789325fc9f4SBarry Smith */ 5790325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5791bbd56ea5SKarl Rupp 5792cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 5793325fc9f4SBarry Smith PetscFunctionReturn(0); 5794325fc9f4SBarry Smith } 5795325fc9f4SBarry Smith 5796b5b1a830SBarry Smith #undef __FUNCT__ 5797b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 5798b5b1a830SBarry Smith /* 5799b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 5800b5b1a830SBarry Smith 5801b5b1a830SBarry Smith Collective on TS 5802b5b1a830SBarry Smith 5803b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5804b5b1a830SBarry Smith @*/ 58050910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 5806b5b1a830SBarry Smith { 5807b5b1a830SBarry Smith PetscErrorCode ierr; 5808b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5809a530c242SBarry Smith int nlhs = 1,nrhs = 6; 5810b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 5811b5b1a830SBarry Smith long long int lx = 0,ls = 0; 5812b5b1a830SBarry Smith 5813b5b1a830SBarry Smith PetscFunctionBegin; 5814cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 58150910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 5816b5b1a830SBarry Smith 5817cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 58180910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 5819bbd56ea5SKarl Rupp 5820b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5821b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 5822b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 5823b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 5824b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 5825b5b1a830SBarry Smith prhs[5] = sctx->ctx; 5826b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 5827b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5828b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 5829b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 5830b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 5831b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 5832b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 5833b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 5834b5b1a830SBarry Smith PetscFunctionReturn(0); 5835b5b1a830SBarry Smith } 5836b5b1a830SBarry Smith 5837b5b1a830SBarry Smith 5838b5b1a830SBarry Smith #undef __FUNCT__ 5839b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 5840b5b1a830SBarry Smith /* 5841b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 5842b5b1a830SBarry Smith 5843b5b1a830SBarry Smith Level: developer 5844b5b1a830SBarry Smith 5845b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 5846b5b1a830SBarry Smith 5847b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5848b5b1a830SBarry Smith */ 5849cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 5850b5b1a830SBarry Smith { 5851b5b1a830SBarry Smith PetscErrorCode ierr; 5852b5b1a830SBarry Smith TSMatlabContext *sctx; 5853b5b1a830SBarry Smith 5854b5b1a830SBarry Smith PetscFunctionBegin; 5855b5b1a830SBarry Smith /* currently sctx is memory bleed */ 5856b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5857b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5858b5b1a830SBarry Smith /* 5859b5b1a830SBarry Smith This should work, but it doesn't 5860b5b1a830SBarry Smith sctx->ctx = ctx; 5861b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5862b5b1a830SBarry Smith */ 5863b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5864bbd56ea5SKarl Rupp 58650298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 5866b5b1a830SBarry Smith PetscFunctionReturn(0); 5867b5b1a830SBarry Smith } 5868325fc9f4SBarry Smith #endif 5869b3603a34SBarry Smith 5870b3603a34SBarry Smith #undef __FUNCT__ 58714f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 5872b3603a34SBarry Smith /*@C 58734f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 5874b3603a34SBarry Smith in a time based line graph 5875b3603a34SBarry Smith 5876b3603a34SBarry Smith Collective on TS 5877b3603a34SBarry Smith 5878b3603a34SBarry Smith Input Parameters: 5879b3603a34SBarry Smith + ts - the TS context 5880b3603a34SBarry Smith . step - current time-step 5881b3603a34SBarry Smith . ptime - current time 58827db568b7SBarry Smith . u - current solution 58837db568b7SBarry Smith - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 5884b3603a34SBarry Smith 5885b3d3934dSBarry Smith Options Database: 58869ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 5887b3d3934dSBarry Smith 5888b3603a34SBarry Smith Level: intermediate 5889b3603a34SBarry Smith 58900b039ecaSBarry Smith Notes: each process in a parallel run displays its component solutions in a separate window 5891b3603a34SBarry Smith 5892b3603a34SBarry Smith .keywords: TS, vector, monitor, view 5893b3603a34SBarry Smith 58947db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 58957db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 58967db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 58977db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 5898b3603a34SBarry Smith @*/ 58997db568b7SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 5900b3603a34SBarry Smith { 5901b3603a34SBarry Smith PetscErrorCode ierr; 59027db568b7SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 5903b3603a34SBarry Smith const PetscScalar *yy; 590458ff32f7SBarry Smith PetscInt dim; 590580666b62SBarry Smith Vec v; 5906b3603a34SBarry Smith 5907b3603a34SBarry Smith PetscFunctionBegin; 590858ff32f7SBarry Smith if (!step) { 5909a9f9c1f6SBarry Smith PetscDrawAxis axis; 5910a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5911a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 5912387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 5913387f4636SBarry Smith char **displaynames; 5914387f4636SBarry Smith PetscBool flg; 5915387f4636SBarry Smith 5916387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 5917387f4636SBarry Smith ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr); 5918387f4636SBarry Smith ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 5919c5929fdfSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 5920387f4636SBarry Smith if (flg) { 5921a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 5922387f4636SBarry Smith } 5923387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 5924387f4636SBarry Smith } 5925387f4636SBarry Smith if (ctx->displaynames) { 5926387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 5927387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 5928387f4636SBarry Smith } else if (ctx->names) { 59290910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 59300b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 5931387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 5932b0bc92c6SBarry Smith } else { 5933b0bc92c6SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 5934b0bc92c6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 5935387f4636SBarry Smith } 59360b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 593758ff32f7SBarry Smith } 593880666b62SBarry Smith if (ctx->transform) { 5939e673d494SBarry Smith ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr); 594080666b62SBarry Smith } else { 594180666b62SBarry Smith v = u; 594280666b62SBarry Smith } 594380666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 5944e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 5945e3efe391SJed Brown { 5946e3efe391SJed Brown PetscReal *yreal; 5947e3efe391SJed Brown PetscInt i,n; 594880666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 5949785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 5950e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 59510b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 5952e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 5953e3efe391SJed Brown } 5954e3efe391SJed Brown #else 5955387f4636SBarry Smith if (ctx->displaynames) { 5956387f4636SBarry Smith PetscInt i; 5957387f4636SBarry Smith for (i=0; i<ctx->ndisplayvariables; i++) { 5958387f4636SBarry Smith ctx->displayvalues[i] = yy[ctx->displayvariables[i]]; 5959387f4636SBarry Smith } 5960387f4636SBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 5961387f4636SBarry Smith } else { 59620b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 5963387f4636SBarry Smith } 5964e3efe391SJed Brown #endif 596580666b62SBarry Smith ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 596680666b62SBarry Smith if (ctx->transform) { 596780666b62SBarry Smith ierr = VecDestroy(&v);CHKERRQ(ierr); 596880666b62SBarry Smith } 5969b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 59700b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 59713923b477SBarry Smith } 5972b3603a34SBarry Smith PetscFunctionReturn(0); 5973b3603a34SBarry Smith } 5974b3603a34SBarry Smith 5975387f4636SBarry Smith 5976b3603a34SBarry Smith #undef __FUNCT__ 597731152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames" 5978b037adc7SBarry Smith /*@C 597931152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 5980b037adc7SBarry Smith 5981b037adc7SBarry Smith Collective on TS 5982b037adc7SBarry Smith 5983b037adc7SBarry Smith Input Parameters: 5984b037adc7SBarry Smith + ts - the TS context 5985b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 5986b037adc7SBarry Smith 5987b037adc7SBarry Smith Level: intermediate 5988b037adc7SBarry Smith 59897db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 59907db568b7SBarry Smith 5991b037adc7SBarry Smith .keywords: TS, vector, monitor, view 5992b037adc7SBarry Smith 5993a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 5994b037adc7SBarry Smith @*/ 599531152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 5996b037adc7SBarry Smith { 5997b037adc7SBarry Smith PetscErrorCode ierr; 5998b037adc7SBarry Smith PetscInt i; 5999b037adc7SBarry Smith 6000b037adc7SBarry Smith PetscFunctionBegin; 6001b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6002b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 60035537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 6004b3d3934dSBarry Smith break; 6005b3d3934dSBarry Smith } 6006b3d3934dSBarry Smith } 6007b3d3934dSBarry Smith PetscFunctionReturn(0); 6008b3d3934dSBarry Smith } 6009b3d3934dSBarry Smith 6010b3d3934dSBarry Smith #undef __FUNCT__ 6011e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames" 6012e673d494SBarry Smith /*@C 6013e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6014e673d494SBarry Smith 6015e673d494SBarry Smith Collective on TS 6016e673d494SBarry Smith 6017e673d494SBarry Smith Input Parameters: 6018e673d494SBarry Smith + ts - the TS context 6019e673d494SBarry Smith - names - the names of the components, final string must be NULL 6020e673d494SBarry Smith 6021e673d494SBarry Smith Level: intermediate 6022e673d494SBarry Smith 6023e673d494SBarry Smith .keywords: TS, vector, monitor, view 6024e673d494SBarry Smith 6025a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 6026e673d494SBarry Smith @*/ 6027e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 6028e673d494SBarry Smith { 6029e673d494SBarry Smith PetscErrorCode ierr; 6030e673d494SBarry Smith 6031e673d494SBarry Smith PetscFunctionBegin; 6032e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 6033e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 6034e673d494SBarry Smith PetscFunctionReturn(0); 6035e673d494SBarry Smith } 6036e673d494SBarry Smith 6037e673d494SBarry Smith #undef __FUNCT__ 6038b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames" 6039b3d3934dSBarry Smith /*@C 6040b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 6041b3d3934dSBarry Smith 6042b3d3934dSBarry Smith Collective on TS 6043b3d3934dSBarry Smith 6044b3d3934dSBarry Smith Input Parameter: 6045b3d3934dSBarry Smith . ts - the TS context 6046b3d3934dSBarry Smith 6047b3d3934dSBarry Smith Output Parameter: 6048b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 6049b3d3934dSBarry Smith 6050b3d3934dSBarry Smith Level: intermediate 6051b3d3934dSBarry Smith 60527db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 60537db568b7SBarry Smith 6054b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6055b3d3934dSBarry Smith 6056b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6057b3d3934dSBarry Smith @*/ 6058b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 6059b3d3934dSBarry Smith { 6060b3d3934dSBarry Smith PetscInt i; 6061b3d3934dSBarry Smith 6062b3d3934dSBarry Smith PetscFunctionBegin; 6063b3d3934dSBarry Smith *names = NULL; 6064b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6065b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 60665537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 6067b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 6068b3d3934dSBarry Smith break; 6069387f4636SBarry Smith } 6070387f4636SBarry Smith } 6071387f4636SBarry Smith PetscFunctionReturn(0); 6072387f4636SBarry Smith } 6073387f4636SBarry Smith 6074387f4636SBarry Smith #undef __FUNCT__ 6075a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables" 6076a66092f1SBarry Smith /*@C 6077a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 6078a66092f1SBarry Smith 6079a66092f1SBarry Smith Collective on TS 6080a66092f1SBarry Smith 6081a66092f1SBarry Smith Input Parameters: 6082a66092f1SBarry Smith + ctx - the TSMonitorLG context 6083a66092f1SBarry Smith . displaynames - the names of the components, final string must be NULL 6084a66092f1SBarry Smith 6085a66092f1SBarry Smith Level: intermediate 6086a66092f1SBarry Smith 6087a66092f1SBarry Smith .keywords: TS, vector, monitor, view 6088a66092f1SBarry Smith 6089a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6090a66092f1SBarry Smith @*/ 6091a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 6092a66092f1SBarry Smith { 6093a66092f1SBarry Smith PetscInt j = 0,k; 6094a66092f1SBarry Smith PetscErrorCode ierr; 6095a66092f1SBarry Smith 6096a66092f1SBarry Smith PetscFunctionBegin; 6097a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 6098a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 6099a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 6100a66092f1SBarry Smith while (displaynames[j]) j++; 6101a66092f1SBarry Smith ctx->ndisplayvariables = j; 6102a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 6103a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 6104a66092f1SBarry Smith j = 0; 6105a66092f1SBarry Smith while (displaynames[j]) { 6106a66092f1SBarry Smith k = 0; 6107a66092f1SBarry Smith while (ctx->names[k]) { 6108a66092f1SBarry Smith PetscBool flg; 6109a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 6110a66092f1SBarry Smith if (flg) { 6111a66092f1SBarry Smith ctx->displayvariables[j] = k; 6112a66092f1SBarry Smith break; 6113a66092f1SBarry Smith } 6114a66092f1SBarry Smith k++; 6115a66092f1SBarry Smith } 6116a66092f1SBarry Smith j++; 6117a66092f1SBarry Smith } 6118a66092f1SBarry Smith PetscFunctionReturn(0); 6119a66092f1SBarry Smith } 6120a66092f1SBarry Smith 6121a66092f1SBarry Smith 6122a66092f1SBarry Smith #undef __FUNCT__ 6123387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables" 6124387f4636SBarry Smith /*@C 6125387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 6126387f4636SBarry Smith 6127387f4636SBarry Smith Collective on TS 6128387f4636SBarry Smith 6129387f4636SBarry Smith Input Parameters: 6130387f4636SBarry Smith + ts - the TS context 6131387f4636SBarry Smith . displaynames - the names of the components, final string must be NULL 6132387f4636SBarry Smith 61337db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 61347db568b7SBarry Smith 6135387f4636SBarry Smith Level: intermediate 6136387f4636SBarry Smith 6137387f4636SBarry Smith .keywords: TS, vector, monitor, view 6138387f4636SBarry Smith 6139387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6140387f4636SBarry Smith @*/ 6141387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 6142387f4636SBarry Smith { 6143a66092f1SBarry Smith PetscInt i; 6144387f4636SBarry Smith PetscErrorCode ierr; 6145387f4636SBarry Smith 6146387f4636SBarry Smith PetscFunctionBegin; 6147387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6148387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 61495537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 6150b3d3934dSBarry Smith break; 6151b037adc7SBarry Smith } 6152b037adc7SBarry Smith } 6153b037adc7SBarry Smith PetscFunctionReturn(0); 6154b037adc7SBarry Smith } 6155b037adc7SBarry Smith 6156b037adc7SBarry Smith #undef __FUNCT__ 615780666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform" 615880666b62SBarry Smith /*@C 615980666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 616080666b62SBarry Smith 616180666b62SBarry Smith Collective on TS 616280666b62SBarry Smith 616380666b62SBarry Smith Input Parameters: 616480666b62SBarry Smith + ts - the TS context 616580666b62SBarry Smith . transform - the transform function 61667684fa3eSBarry Smith . destroy - function to destroy the optional context 616780666b62SBarry Smith - ctx - optional context used by transform function 616880666b62SBarry Smith 61697db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 61707db568b7SBarry Smith 617180666b62SBarry Smith Level: intermediate 617280666b62SBarry Smith 617380666b62SBarry Smith .keywords: TS, vector, monitor, view 617480666b62SBarry Smith 6175a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 617680666b62SBarry Smith @*/ 61777684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 617880666b62SBarry Smith { 617980666b62SBarry Smith PetscInt i; 6180a66092f1SBarry Smith PetscErrorCode ierr; 618180666b62SBarry Smith 618280666b62SBarry Smith PetscFunctionBegin; 618380666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 618480666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 61855537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 618680666b62SBarry Smith } 618780666b62SBarry Smith } 618880666b62SBarry Smith PetscFunctionReturn(0); 618980666b62SBarry Smith } 619080666b62SBarry Smith 619180666b62SBarry Smith #undef __FUNCT__ 6192e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform" 6193e673d494SBarry Smith /*@C 6194e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 6195e673d494SBarry Smith 6196e673d494SBarry Smith Collective on TSLGCtx 6197e673d494SBarry Smith 6198e673d494SBarry Smith Input Parameters: 6199e673d494SBarry Smith + ts - the TS context 6200e673d494SBarry Smith . transform - the transform function 62017684fa3eSBarry Smith . destroy - function to destroy the optional context 6202e673d494SBarry Smith - ctx - optional context used by transform function 6203e673d494SBarry Smith 6204e673d494SBarry Smith Level: intermediate 6205e673d494SBarry Smith 6206e673d494SBarry Smith .keywords: TS, vector, monitor, view 6207e673d494SBarry Smith 6208a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 6209e673d494SBarry Smith @*/ 62107684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 6211e673d494SBarry Smith { 6212e673d494SBarry Smith PetscFunctionBegin; 6213e673d494SBarry Smith ctx->transform = transform; 62147684fa3eSBarry Smith ctx->transformdestroy = destroy; 6215e673d494SBarry Smith ctx->transformctx = tctx; 6216e673d494SBarry Smith PetscFunctionReturn(0); 6217e673d494SBarry Smith } 6218e673d494SBarry Smith 6219b3603a34SBarry Smith #undef __FUNCT__ 62204f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 6221ef20d060SBarry Smith /*@C 62224f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 6223ef20d060SBarry Smith in a time based line graph 6224ef20d060SBarry Smith 6225ef20d060SBarry Smith Collective on TS 6226ef20d060SBarry Smith 6227ef20d060SBarry Smith Input Parameters: 6228ef20d060SBarry Smith + ts - the TS context 6229ef20d060SBarry Smith . step - current time-step 6230ef20d060SBarry Smith . ptime - current time 62317db568b7SBarry Smith . u - current solution 62327db568b7SBarry Smith - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 6233ef20d060SBarry Smith 6234ef20d060SBarry Smith Level: intermediate 6235ef20d060SBarry Smith 6236abd5a294SJed Brown Notes: 6237abd5a294SJed Brown Only for sequential solves. 6238abd5a294SJed Brown 6239abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 6240abd5a294SJed Brown 6241abd5a294SJed Brown Options Database Keys: 62424f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 6243ef20d060SBarry Smith 6244ef20d060SBarry Smith .keywords: TS, vector, monitor, view 6245ef20d060SBarry Smith 6246abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 6247ef20d060SBarry Smith @*/ 62480910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 6249ef20d060SBarry Smith { 6250ef20d060SBarry Smith PetscErrorCode ierr; 62510b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 6252ef20d060SBarry Smith const PetscScalar *yy; 6253ef20d060SBarry Smith Vec y; 6254a9f9c1f6SBarry Smith PetscInt dim; 6255ef20d060SBarry Smith 6256ef20d060SBarry Smith PetscFunctionBegin; 6257a9f9c1f6SBarry Smith if (!step) { 6258a9f9c1f6SBarry Smith PetscDrawAxis axis; 6259a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 62601ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 62610910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6262a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6263a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6264a9f9c1f6SBarry Smith } 62650910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 6266ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 62670910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 6268ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 6269e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6270e3efe391SJed Brown { 6271e3efe391SJed Brown PetscReal *yreal; 6272e3efe391SJed Brown PetscInt i,n; 6273e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 6274785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6275e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 62760b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6277e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6278e3efe391SJed Brown } 6279e3efe391SJed Brown #else 62800b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6281e3efe391SJed Brown #endif 6282ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 6283ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 6284b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 62850b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 62863923b477SBarry Smith } 6287ef20d060SBarry Smith PetscFunctionReturn(0); 6288ef20d060SBarry Smith } 6289ef20d060SBarry Smith 6290201da799SBarry Smith #undef __FUNCT__ 6291201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 6292201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6293201da799SBarry Smith { 6294201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6295201da799SBarry Smith PetscReal x = ptime,y; 6296201da799SBarry Smith PetscErrorCode ierr; 6297201da799SBarry Smith PetscInt its; 6298201da799SBarry Smith 6299201da799SBarry Smith PetscFunctionBegin; 6300201da799SBarry Smith if (!n) { 6301201da799SBarry Smith PetscDrawAxis axis; 6302bbd56ea5SKarl Rupp 6303201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6304201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 6305201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6306bbd56ea5SKarl Rupp 6307201da799SBarry Smith ctx->snes_its = 0; 6308201da799SBarry Smith } 6309201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 6310201da799SBarry Smith y = its - ctx->snes_its; 6311201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 63123fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6313201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 6314201da799SBarry Smith } 6315201da799SBarry Smith ctx->snes_its = its; 6316201da799SBarry Smith PetscFunctionReturn(0); 6317201da799SBarry Smith } 6318201da799SBarry Smith 6319201da799SBarry Smith #undef __FUNCT__ 6320201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 6321201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6322201da799SBarry Smith { 6323201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6324201da799SBarry Smith PetscReal x = ptime,y; 6325201da799SBarry Smith PetscErrorCode ierr; 6326201da799SBarry Smith PetscInt its; 6327201da799SBarry Smith 6328201da799SBarry Smith PetscFunctionBegin; 6329201da799SBarry Smith if (!n) { 6330201da799SBarry Smith PetscDrawAxis axis; 6331bbd56ea5SKarl Rupp 6332201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6333201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 6334201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6335bbd56ea5SKarl Rupp 6336201da799SBarry Smith ctx->ksp_its = 0; 6337201da799SBarry Smith } 6338201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 6339201da799SBarry Smith y = its - ctx->ksp_its; 6340201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 634199fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6342201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 6343201da799SBarry Smith } 6344201da799SBarry Smith ctx->ksp_its = its; 6345201da799SBarry Smith PetscFunctionReturn(0); 6346201da799SBarry Smith } 6347f9c1d6abSBarry Smith 6348f9c1d6abSBarry Smith #undef __FUNCT__ 6349f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 6350f9c1d6abSBarry Smith /*@ 6351f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 6352f9c1d6abSBarry Smith 6353f9c1d6abSBarry Smith Collective on TS and Vec 6354f9c1d6abSBarry Smith 6355f9c1d6abSBarry Smith Input Parameters: 6356f9c1d6abSBarry Smith + ts - the TS context 6357f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 6358f9c1d6abSBarry Smith 6359f9c1d6abSBarry Smith Output Parameters: 6360f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 6361f9c1d6abSBarry Smith 6362f9c1d6abSBarry Smith Level: developer 6363f9c1d6abSBarry Smith 6364f9c1d6abSBarry Smith .keywords: TS, compute 6365f9c1d6abSBarry Smith 6366f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 6367f9c1d6abSBarry Smith @*/ 6368f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 6369f9c1d6abSBarry Smith { 6370f9c1d6abSBarry Smith PetscErrorCode ierr; 6371f9c1d6abSBarry Smith 6372f9c1d6abSBarry Smith PetscFunctionBegin; 6373f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6374ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 6375f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 6376f9c1d6abSBarry Smith PetscFunctionReturn(0); 6377f9c1d6abSBarry Smith } 637824655328SShri 6379b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 6380b3d3934dSBarry Smith #undef __FUNCT__ 6381b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate" 6382b3d3934dSBarry Smith /*@C 6383b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 6384b3d3934dSBarry Smith 6385b3d3934dSBarry Smith Collective on TS 6386b3d3934dSBarry Smith 6387b3d3934dSBarry Smith Input Parameters: 6388b3d3934dSBarry Smith . ts - the ODE solver object 6389b3d3934dSBarry Smith 6390b3d3934dSBarry Smith Output Parameter: 6391b3d3934dSBarry Smith . ctx - the context 6392b3d3934dSBarry Smith 6393b3d3934dSBarry Smith Level: intermediate 6394b3d3934dSBarry Smith 6395b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso 6396b3d3934dSBarry Smith 6397b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 6398b3d3934dSBarry Smith 6399b3d3934dSBarry Smith @*/ 6400b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 6401b3d3934dSBarry Smith { 6402b3d3934dSBarry Smith PetscErrorCode ierr; 6403b3d3934dSBarry Smith 6404b3d3934dSBarry Smith PetscFunctionBegin; 6405a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 6406b3d3934dSBarry Smith PetscFunctionReturn(0); 6407b3d3934dSBarry Smith } 6408b3d3934dSBarry Smith 6409b3d3934dSBarry Smith #undef __FUNCT__ 6410b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope" 6411b3d3934dSBarry Smith /*@C 6412b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 6413b3d3934dSBarry Smith 6414b3d3934dSBarry Smith Collective on TS 6415b3d3934dSBarry Smith 6416b3d3934dSBarry Smith Input Parameters: 6417b3d3934dSBarry Smith + ts - the TS context 6418b3d3934dSBarry Smith . step - current time-step 6419b3d3934dSBarry Smith . ptime - current time 64207db568b7SBarry Smith . u - current solution 64217db568b7SBarry Smith - dctx - the envelope context 6422b3d3934dSBarry Smith 6423b3d3934dSBarry Smith Options Database: 6424b3d3934dSBarry Smith . -ts_monitor_envelope 6425b3d3934dSBarry Smith 6426b3d3934dSBarry Smith Level: intermediate 6427b3d3934dSBarry Smith 6428b3d3934dSBarry Smith Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 6429b3d3934dSBarry Smith 6430b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6431b3d3934dSBarry Smith 64327db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 6433b3d3934dSBarry Smith @*/ 64347db568b7SBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6435b3d3934dSBarry Smith { 6436b3d3934dSBarry Smith PetscErrorCode ierr; 64377db568b7SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 6438b3d3934dSBarry Smith 6439b3d3934dSBarry Smith PetscFunctionBegin; 6440b3d3934dSBarry Smith if (!ctx->max) { 6441b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 6442b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 6443b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 6444b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 6445b3d3934dSBarry Smith } else { 6446b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 6447b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 6448b3d3934dSBarry Smith } 6449b3d3934dSBarry Smith PetscFunctionReturn(0); 6450b3d3934dSBarry Smith } 6451b3d3934dSBarry Smith 6452b3d3934dSBarry Smith 6453b3d3934dSBarry Smith #undef __FUNCT__ 6454b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds" 6455b3d3934dSBarry Smith /*@C 6456b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 6457b3d3934dSBarry Smith 6458b3d3934dSBarry Smith Collective on TS 6459b3d3934dSBarry Smith 6460b3d3934dSBarry Smith Input Parameter: 6461b3d3934dSBarry Smith . ts - the TS context 6462b3d3934dSBarry Smith 6463b3d3934dSBarry Smith Output Parameter: 6464b3d3934dSBarry Smith + max - the maximum values 6465b3d3934dSBarry Smith - min - the minimum values 6466b3d3934dSBarry Smith 64677db568b7SBarry Smith Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 64687db568b7SBarry Smith 6469b3d3934dSBarry Smith Level: intermediate 6470b3d3934dSBarry Smith 6471b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6472b3d3934dSBarry Smith 6473b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6474b3d3934dSBarry Smith @*/ 6475b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 6476b3d3934dSBarry Smith { 6477b3d3934dSBarry Smith PetscInt i; 6478b3d3934dSBarry Smith 6479b3d3934dSBarry Smith PetscFunctionBegin; 6480b3d3934dSBarry Smith if (max) *max = NULL; 6481b3d3934dSBarry Smith if (min) *min = NULL; 6482b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6483b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 64845537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 6485b3d3934dSBarry Smith if (max) *max = ctx->max; 6486b3d3934dSBarry Smith if (min) *min = ctx->min; 6487b3d3934dSBarry Smith break; 6488b3d3934dSBarry Smith } 6489b3d3934dSBarry Smith } 6490b3d3934dSBarry Smith PetscFunctionReturn(0); 6491b3d3934dSBarry Smith } 6492b3d3934dSBarry Smith 6493b3d3934dSBarry Smith #undef __FUNCT__ 6494b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy" 6495b3d3934dSBarry Smith /*@C 6496b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 6497b3d3934dSBarry Smith 6498b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 6499b3d3934dSBarry Smith 6500b3d3934dSBarry Smith Input Parameter: 6501b3d3934dSBarry Smith . ctx - the monitor context 6502b3d3934dSBarry Smith 6503b3d3934dSBarry Smith Level: intermediate 6504b3d3934dSBarry Smith 6505b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy 6506b3d3934dSBarry Smith 65077db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 6508b3d3934dSBarry Smith @*/ 6509b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 6510b3d3934dSBarry Smith { 6511b3d3934dSBarry Smith PetscErrorCode ierr; 6512b3d3934dSBarry Smith 6513b3d3934dSBarry Smith PetscFunctionBegin; 6514b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 6515b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 6516b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 6517b3d3934dSBarry Smith PetscFunctionReturn(0); 6518b3d3934dSBarry Smith } 6519f2dee214SBarry Smith 652024655328SShri #undef __FUNCT__ 652124655328SShri #define __FUNCT__ "TSRollBack" 652224655328SShri /*@ 652324655328SShri TSRollBack - Rolls back one time step 652424655328SShri 652524655328SShri Collective on TS 652624655328SShri 652724655328SShri Input Parameter: 652824655328SShri . ts - the TS context obtained from TSCreate() 652924655328SShri 653024655328SShri Level: advanced 653124655328SShri 653224655328SShri .keywords: TS, timestep, rollback 653324655328SShri 653424655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 653524655328SShri @*/ 653624655328SShri PetscErrorCode TSRollBack(TS ts) 653724655328SShri { 653824655328SShri PetscErrorCode ierr; 653924655328SShri 654024655328SShri PetscFunctionBegin; 654124655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 654224655328SShri 654324655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 654424655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 654524655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 654624655328SShri ts->ptime = ts->ptime_prev; 65478392e04aSShri Abhyankar ts->steprollback = PETSC_TRUE; /* Flag to indicate that the step is rollbacked */ 654824655328SShri PetscFunctionReturn(0); 654924655328SShri } 6550aeb4809dSShri Abhyankar 6551ff22ae23SHong Zhang #undef __FUNCT__ 6552ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages" 6553ff22ae23SHong Zhang /*@ 6554ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 6555ff22ae23SHong Zhang 6556ff22ae23SHong Zhang Input Parameter: 6557ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 6558ff22ae23SHong Zhang 6559ff22ae23SHong Zhang Level: advanced 6560ff22ae23SHong Zhang 6561ff22ae23SHong Zhang .keywords: TS, getstages 6562ff22ae23SHong Zhang 6563ff22ae23SHong Zhang .seealso: TSCreate() 6564ff22ae23SHong Zhang @*/ 6565ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns, Vec **Y) 6566ff22ae23SHong Zhang { 6567ff22ae23SHong Zhang PetscErrorCode ierr; 6568ff22ae23SHong Zhang 6569ff22ae23SHong Zhang PetscFunctionBegin; 6570ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 6571ff22ae23SHong Zhang PetscValidPointer(ns,2); 6572ff22ae23SHong Zhang 6573ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 6574ff22ae23SHong Zhang else { 6575ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 6576ff22ae23SHong Zhang } 6577ff22ae23SHong Zhang PetscFunctionReturn(0); 6578ff22ae23SHong Zhang } 6579ff22ae23SHong Zhang 6580847ff0e1SMatthew G. Knepley #undef __FUNCT__ 6581847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor" 6582847ff0e1SMatthew G. Knepley /*@C 6583847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 6584847ff0e1SMatthew G. Knepley 6585847ff0e1SMatthew G. Knepley Collective on SNES 6586847ff0e1SMatthew G. Knepley 6587847ff0e1SMatthew G. Knepley Input Parameters: 6588847ff0e1SMatthew G. Knepley + ts - the TS context 6589847ff0e1SMatthew G. Knepley . t - current timestep 6590847ff0e1SMatthew G. Knepley . U - state vector 6591847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 6592847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 6593847ff0e1SMatthew G. Knepley - ctx - an optional user context 6594847ff0e1SMatthew G. Knepley 6595847ff0e1SMatthew G. Knepley Output Parameters: 6596847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 6597847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 6598847ff0e1SMatthew G. Knepley 6599847ff0e1SMatthew G. Knepley Level: intermediate 6600847ff0e1SMatthew G. Knepley 6601847ff0e1SMatthew G. Knepley Notes: 6602847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 6603847ff0e1SMatthew G. Knepley 6604847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 6605847ff0e1SMatthew G. Knepley 6606847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 6607847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 6608847ff0e1SMatthew G. Knepley 6609847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 6610847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 6611847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 6612847ff0e1SMatthew G. Knepley 6613847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse 6614847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 6615847ff0e1SMatthew G. Knepley @*/ 6616847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 6617847ff0e1SMatthew G. Knepley { 6618847ff0e1SMatthew G. Knepley SNES snes; 6619847ff0e1SMatthew G. Knepley MatFDColoring color; 6620847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 6621847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 6622847ff0e1SMatthew G. Knepley 6623847ff0e1SMatthew G. Knepley PetscFunctionBegin; 6624c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 6625847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 6626847ff0e1SMatthew G. Knepley if (!color) { 6627847ff0e1SMatthew G. Knepley DM dm; 6628847ff0e1SMatthew G. Knepley ISColoring iscoloring; 6629847ff0e1SMatthew G. Knepley 6630847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 6631847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 6632847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 6633847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 6634847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 6635847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 6636847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 6637847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 6638847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 6639847ff0e1SMatthew G. Knepley } else { 6640847ff0e1SMatthew G. Knepley MatColoring mc; 6641847ff0e1SMatthew G. Knepley 6642847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 6643847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 6644847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 6645847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 6646847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 6647847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 6648847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 6649847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 6650847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 6651847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 6652847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 6653847ff0e1SMatthew G. Knepley } 6654847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 6655847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 6656847ff0e1SMatthew G. Knepley } 6657847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 6658847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 6659847ff0e1SMatthew G. Knepley if (J != B) { 6660847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 6661847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 6662847ff0e1SMatthew G. Knepley } 6663847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 6664847ff0e1SMatthew G. Knepley } 666593b34091SDebojyoti Ghosh 666693b34091SDebojyoti Ghosh #undef __FUNCT__ 6667cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSSetFunctionDomainError" 6668cb9d8021SPierre Barbier de Reuille /*@ 6669cb9d8021SPierre Barbier de Reuille TSSetFunctionDomainError - Set the function testing if the current state vector is valid 6670cb9d8021SPierre Barbier de Reuille 6671cb9d8021SPierre Barbier de Reuille Input Parameters: 6672cb9d8021SPierre Barbier de Reuille ts - the TS context 6673cb9d8021SPierre Barbier de Reuille func - function called within TSFunctionDomainError 6674cb9d8021SPierre Barbier de Reuille 6675cb9d8021SPierre Barbier de Reuille Level: intermediate 6676cb9d8021SPierre Barbier de Reuille 6677cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain 6678cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError() 6679cb9d8021SPierre Barbier de Reuille @*/ 6680cb9d8021SPierre Barbier de Reuille 6681d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 6682cb9d8021SPierre Barbier de Reuille { 6683cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 6684cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 6685cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 6686cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 6687cb9d8021SPierre Barbier de Reuille } 6688cb9d8021SPierre Barbier de Reuille 6689cb9d8021SPierre Barbier de Reuille #undef __FUNCT__ 6690cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSFunctionDomainError" 6691cb9d8021SPierre Barbier de Reuille /*@ 6692cb9d8021SPierre Barbier de Reuille TSFunctionDomainError - Check if the current state is valid 6693cb9d8021SPierre Barbier de Reuille 6694cb9d8021SPierre Barbier de Reuille Input Parameters: 6695cb9d8021SPierre Barbier de Reuille ts - the TS context 6696cb9d8021SPierre Barbier de Reuille stagetime - time of the simulation 6697d183316bSPierre Barbier de Reuille Y - state vector to check. 6698cb9d8021SPierre Barbier de Reuille 6699cb9d8021SPierre Barbier de Reuille Output Parameter: 6700cb9d8021SPierre Barbier de Reuille accept - Set to PETSC_FALSE if the current state vector is valid. 6701cb9d8021SPierre Barbier de Reuille 6702cb9d8021SPierre Barbier de Reuille Note: 6703cb9d8021SPierre Barbier de Reuille This function should be used to ensure the state is in a valid part of the space. 6704cb9d8021SPierre Barbier de Reuille For example, one can ensure here all values are positive. 6705cb9d8021SPierre Barbier de Reuille @*/ 6706d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 6707cb9d8021SPierre Barbier de Reuille { 6708cb9d8021SPierre Barbier de Reuille PetscErrorCode ierr; 6709cb9d8021SPierre Barbier de Reuille 6710cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 6711cb9d8021SPierre Barbier de Reuille 6712cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6713cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 6714cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 6715d183316bSPierre Barbier de Reuille PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 6716cb9d8021SPierre Barbier de Reuille } 6717cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 6718cb9d8021SPierre Barbier de Reuille } 67191ceb14c0SBarry Smith 67201ceb14c0SBarry Smith #undef __FUNCT__ 6721e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone" 672293b34091SDebojyoti Ghosh /*@C 6723e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 672493b34091SDebojyoti Ghosh 672593b34091SDebojyoti Ghosh Collective on MPI_Comm 672693b34091SDebojyoti Ghosh 672793b34091SDebojyoti Ghosh Input Parameter: 672893b34091SDebojyoti Ghosh . tsin - The input TS 672993b34091SDebojyoti Ghosh 673093b34091SDebojyoti Ghosh Output Parameter: 6731e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 673293b34091SDebojyoti Ghosh 67335eca1a21SEmil Constantinescu Notes: 67345eca1a21SEmil Constantinescu This function is used to create a clone of a TS object. It is used in ARKIMEX for initializing the slope for first stage explicit methods. It will likely be replaced in the future with a mechanism of switching methods on the fly. 67355eca1a21SEmil Constantinescu 6736baa10174SEmil Constantinescu When using TSDestroy() on a clone the user has to first reset the correct TS reference in the embedded SNES object: e.g.: by running SNES snes_dup=NULL; TSGetSNES(ts,&snes_dup); ierr = TSSetSNES(ts,snes_dup); 67375eca1a21SEmil Constantinescu 67385eca1a21SEmil Constantinescu Level: developer 673993b34091SDebojyoti Ghosh 6740e5168f73SEmil Constantinescu .keywords: TS, clone 6741e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 674293b34091SDebojyoti Ghosh @*/ 6743baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 674493b34091SDebojyoti Ghosh { 674593b34091SDebojyoti Ghosh TS t; 674693b34091SDebojyoti Ghosh PetscErrorCode ierr; 6747dc846ba4SSatish Balay SNES snes_start; 6748dc846ba4SSatish Balay DM dm; 6749dc846ba4SSatish Balay TSType type; 675093b34091SDebojyoti Ghosh 675193b34091SDebojyoti Ghosh PetscFunctionBegin; 675293b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 675393b34091SDebojyoti Ghosh *tsout = NULL; 675493b34091SDebojyoti Ghosh 67557a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 675693b34091SDebojyoti Ghosh 675793b34091SDebojyoti Ghosh /* General TS description */ 675893b34091SDebojyoti Ghosh t->numbermonitors = 0; 675993b34091SDebojyoti Ghosh t->setupcalled = 0; 676093b34091SDebojyoti Ghosh t->ksp_its = 0; 676193b34091SDebojyoti Ghosh t->snes_its = 0; 676293b34091SDebojyoti Ghosh t->nwork = 0; 676393b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 676493b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 676593b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 676693b34091SDebojyoti Ghosh 676734561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start); CHKERRQ(ierr); 676834561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start); CHKERRQ(ierr); 6769d15a3a53SEmil Constantinescu 677093b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm); CHKERRQ(ierr); 677193b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm); CHKERRQ(ierr); 677293b34091SDebojyoti Ghosh 677393b34091SDebojyoti Ghosh t->adapt=tsin->adapt; 677493b34091SDebojyoti Ghosh PetscObjectReference((PetscObject)t->adapt); 677593b34091SDebojyoti Ghosh 677693b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 677793b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 677893b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 677993b34091SDebojyoti Ghosh t->time_step_orig = tsin->time_step_orig; 678093b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 678193b34091SDebojyoti Ghosh t->steps = tsin->steps; 678293b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 678393b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 678493b34091SDebojyoti Ghosh t->atol = tsin->atol; 678593b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 678693b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 678793b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 678893b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 678993b34091SDebojyoti Ghosh 679093b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type); CHKERRQ(ierr); 679193b34091SDebojyoti Ghosh ierr = TSSetType(t,type); CHKERRQ(ierr); 679293b34091SDebojyoti Ghosh 679393b34091SDebojyoti Ghosh t->vec_sol = NULL; 679493b34091SDebojyoti Ghosh 679593b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 679693b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 679793b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 679893b34091SDebojyoti Ghosh 679993b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 680093b34091SDebojyoti Ghosh 68010d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 68020d4fed19SBarry Smith PetscInt i; 68030d4fed19SBarry Smith ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr); 68040d4fed19SBarry Smith for (i=0; i<10; i++) { 68050d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 68060d4fed19SBarry Smith } 68070d4fed19SBarry Smith } 680893b34091SDebojyoti Ghosh *tsout = t; 680993b34091SDebojyoti Ghosh PetscFunctionReturn(0); 681093b34091SDebojyoti Ghosh } 6811