163dd3a1aSKris Buschelman 2af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 3496e6a7aSJed Brown #include <petscdmshell.h> 41e25c274SJed Brown #include <petscdmda.h> 52d5ee99bSBarry Smith #include <petscviewer.h> 62d5ee99bSBarry Smith #include <petscdraw.h> 7d763cef2SBarry Smith 8d5ba7fb7SMatthew Knepley /* Logging support */ 9d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 108d0ad7a8SHong Zhang PetscLogEvent TS_AdjointStep, TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 11d405a339SMatthew Knepley 12feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1349354f04SShri Abhyankar 142d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx { 152d5ee99bSBarry Smith PetscViewer viewer; 162d5ee99bSBarry Smith Vec initialsolution; 172d5ee99bSBarry Smith PetscBool showinitial; 182d5ee99bSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 192d5ee99bSBarry Smith PetscBool showtimestepandtime; 202d5ee99bSBarry Smith }; 212d5ee99bSBarry Smith 22bdad233fSMatthew Knepley #undef __FUNCT__ 23fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSetFromOptions" 24fde5950dSBarry Smith /*@C 25fde5950dSBarry Smith TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 26fde5950dSBarry Smith 27fde5950dSBarry Smith Collective on TS 28fde5950dSBarry Smith 29fde5950dSBarry Smith Input Parameters: 30fde5950dSBarry Smith + ts - TS object you wish to monitor 31fde5950dSBarry Smith . name - the monitor type one is seeking 32fde5950dSBarry Smith . help - message indicating what monitoring is done 33fde5950dSBarry Smith . manual - manual page for the monitor 34fde5950dSBarry Smith . monitor - the monitor function 35fde5950dSBarry 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 36fde5950dSBarry Smith 37fde5950dSBarry Smith Level: developer 38fde5950dSBarry Smith 39fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 40fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 41fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 42fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 43fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 44fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 45fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 46fde5950dSBarry Smith @*/ 47fde5950dSBarry 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)) 48fde5950dSBarry Smith { 49fde5950dSBarry Smith PetscErrorCode ierr; 50fde5950dSBarry Smith PetscViewer viewer; 51fde5950dSBarry Smith PetscViewerFormat format; 52fde5950dSBarry Smith PetscBool flg; 53fde5950dSBarry Smith 54fde5950dSBarry Smith PetscFunctionBegin; 55fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 56fde5950dSBarry Smith if (flg) { 576a9046bcSBarry Smith ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); 58fde5950dSBarry Smith if (monitorsetup) { 59fde5950dSBarry Smith ierr = (*monitorsetup)(ts,viewer);CHKERRQ(ierr); 60fde5950dSBarry Smith } 61fde5950dSBarry Smith ierr = TSMonitorSet(ts,monitor,viewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 62fde5950dSBarry Smith } 63fde5950dSBarry Smith PetscFunctionReturn(0); 64fde5950dSBarry Smith } 65fde5950dSBarry Smith 66fde5950dSBarry Smith #undef __FUNCT__ 67fde5950dSBarry Smith #define __FUNCT__ "TSAdjointMonitorSetFromOptions" 68fde5950dSBarry Smith /*@C 69fde5950dSBarry Smith TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 70fde5950dSBarry Smith 71fde5950dSBarry Smith Collective on TS 72fde5950dSBarry Smith 73fde5950dSBarry Smith Input Parameters: 74fde5950dSBarry Smith + ts - TS object you wish to monitor 75fde5950dSBarry Smith . name - the monitor type one is seeking 76fde5950dSBarry Smith . help - message indicating what monitoring is done 77fde5950dSBarry Smith . manual - manual page for the monitor 78fde5950dSBarry Smith . monitor - the monitor function 79fde5950dSBarry 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 80fde5950dSBarry Smith 81fde5950dSBarry Smith Level: developer 82fde5950dSBarry Smith 83fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 84fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 85fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 86fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 87fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 88fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 89fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 90fde5950dSBarry Smith @*/ 91fde5950dSBarry 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)) 92fde5950dSBarry Smith { 93fde5950dSBarry Smith PetscErrorCode ierr; 94fde5950dSBarry Smith PetscViewer viewer; 95fde5950dSBarry Smith PetscViewerFormat format; 96fde5950dSBarry Smith PetscBool flg; 97fde5950dSBarry Smith 98fde5950dSBarry Smith PetscFunctionBegin; 99fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 100fde5950dSBarry Smith if (flg) { 1016a9046bcSBarry Smith ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); 102fde5950dSBarry Smith if (monitorsetup) { 103fde5950dSBarry Smith ierr = (*monitorsetup)(ts,viewer);CHKERRQ(ierr); 104fde5950dSBarry Smith } 105fde5950dSBarry Smith ierr = TSAdjointMonitorSet(ts,monitor,viewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 106fde5950dSBarry Smith } 107fde5950dSBarry Smith PetscFunctionReturn(0); 108fde5950dSBarry Smith } 109fde5950dSBarry Smith 110fde5950dSBarry Smith #undef __FUNCT__ 111bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions" 112bdad233fSMatthew Knepley /*@ 113bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 114bdad233fSMatthew Knepley 115bdad233fSMatthew Knepley Collective on TS 116bdad233fSMatthew Knepley 117bdad233fSMatthew Knepley Input Parameter: 118bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 119bdad233fSMatthew Knepley 120bdad233fSMatthew Knepley Options Database Keys: 1214d91e141SJed Brown + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP 122ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 1233e4cdcaaSBarry Smith . -ts_max_steps <maxsteps> - maximum number of time-steps to take 1243e4cdcaaSBarry Smith . -ts_final_time <time> - maximum time to compute to 1253e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 126a3cdaa26SBarry 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 127a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 128a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 129a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 130a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 131a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 132ef222394SBarry Smith . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 133847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 134bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 135de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 136de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 1376934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 138de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 139de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 140de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 141de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 1423e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 1433e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 144fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 145b3d3934dSBarry Smith . -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 146110eb670SHong Zhang . -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 147110eb670SHong Zhang . -ts_adjoint_monitor - print information at each adjoint time step 14853ea634cSHong Zhang - -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically 14953ea634cSHong Zhang 15091b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 151bdad233fSMatthew Knepley 152bdad233fSMatthew Knepley Level: beginner 153bdad233fSMatthew Knepley 154bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 155bdad233fSMatthew Knepley 156a313700dSBarry Smith .seealso: TSGetType() 157bdad233fSMatthew Knepley @*/ 1587087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 159bdad233fSMatthew Knepley { 160bc952696SBarry Smith PetscBool opt,flg,tflg; 161dfbe8321SBarry Smith PetscErrorCode ierr; 162eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 163089b2837SJed Brown SNES snes; 16431748224SBarry Smith PetscReal time_step; 16549354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 166d1212d36SBarry Smith char dir[16]; 1676991f827SBarry Smith const char *defaultType; 1686991f827SBarry Smith char typeName[256]; 169bdad233fSMatthew Knepley 170bdad233fSMatthew Knepley PetscFunctionBegin; 1710700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1723194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 1736991f827SBarry Smith if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 1746991f827SBarry Smith else defaultType = TSEULER; 1756991f827SBarry Smith 1766991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 1776991f827SBarry Smith ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 1786991f827SBarry Smith if (opt) { 1796991f827SBarry Smith ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 1806991f827SBarry Smith } else { 1816991f827SBarry Smith ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 1826991f827SBarry Smith } 183bdad233fSMatthew Knepley 184bdad233fSMatthew Knepley /* Handle generic TS options */ 1850298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1860298fd71SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 1870298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 18831748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 18931748224SBarry Smith if (flg) { 19031748224SBarry Smith ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 19131748224SBarry Smith } 19249354f04SShri 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); 19349354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 1940298fd71SBarry 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); 1950298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 1960298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 1970298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 1980298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 199bdad233fSMatthew Knepley 20056f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 20156f85f32SBarry Smith { 20256f85f32SBarry Smith PetscBool set; 20356f85f32SBarry Smith flg = PETSC_FALSE; 20456f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 20556f85f32SBarry Smith if (set) { 20656f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 20756f85f32SBarry Smith } 20856f85f32SBarry Smith } 20956f85f32SBarry Smith #endif 21056f85f32SBarry Smith 211bdad233fSMatthew Knepley /* Monitor options */ 212fde5950dSBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr); 213fde5950dSBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr); 214fde5950dSBarry Smith ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr); 215fde5950dSBarry Smith 2165180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2175180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 2185180491cSLisandro Dalcin 2194f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 220b3603a34SBarry Smith if (opt) { 2210b039ecaSBarry Smith TSMonitorLGCtx ctx; 2223923b477SBarry Smith PetscInt howoften = 1; 223b3603a34SBarry Smith 2240298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 225*6ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2264f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 227bdad233fSMatthew Knepley } 228*6ba87a44SLisandro Dalcin 2294f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 230ef20d060SBarry Smith if (opt) { 2310b039ecaSBarry Smith TSMonitorLGCtx ctx; 2323923b477SBarry Smith PetscInt howoften = 1; 233ef20d060SBarry Smith 2340298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 235*6ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2364f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 237ef20d060SBarry Smith } 2386934998bSLisandro Dalcin 2396934998bSLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 2406934998bSLisandro Dalcin if (opt) { 2416934998bSLisandro Dalcin TSMonitorLGCtx ctx; 2426934998bSLisandro Dalcin PetscInt howoften = 1; 2436934998bSLisandro Dalcin 2446934998bSLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 245*6ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2466934998bSLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 2476934998bSLisandro Dalcin } 248201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 249201da799SBarry Smith if (opt) { 250201da799SBarry Smith TSMonitorLGCtx ctx; 251201da799SBarry Smith PetscInt howoften = 1; 252201da799SBarry Smith 2530298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 254*6ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 255201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 256201da799SBarry Smith } 257201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 258201da799SBarry Smith if (opt) { 259201da799SBarry Smith TSMonitorLGCtx ctx; 260201da799SBarry Smith PetscInt howoften = 1; 261201da799SBarry Smith 2620298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 263*6ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 264201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 265201da799SBarry Smith } 2668189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 2678189c53fSBarry Smith if (opt) { 2688189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2698189c53fSBarry Smith PetscInt howoften = 1; 2708189c53fSBarry Smith 2710298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 2726934998bSLisandro Dalcin ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 2738189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 2748189c53fSBarry Smith } 275ef20d060SBarry Smith opt = PETSC_FALSE; 2760dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 277a7cc72afSBarry Smith if (opt) { 27883a4ac43SBarry Smith TSMonitorDrawCtx ctx; 27983a4ac43SBarry Smith PetscInt howoften = 1; 280a80ad3e0SBarry Smith 2810298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 2826934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 28383a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 284bdad233fSMatthew Knepley } 285fb1732b5SBarry Smith opt = PETSC_FALSE; 2869110b2e7SHong Zhang ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr); 2879110b2e7SHong Zhang if (opt) { 2889110b2e7SHong Zhang TSMonitorDrawCtx ctx; 2899110b2e7SHong Zhang PetscInt howoften = 1; 2909110b2e7SHong Zhang 2919110b2e7SHong Zhang ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr); 2926934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 2939110b2e7SHong Zhang ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2949110b2e7SHong Zhang } 2959110b2e7SHong Zhang opt = PETSC_FALSE; 2962d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 2972d5ee99bSBarry Smith if (opt) { 2982d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2992d5ee99bSBarry Smith PetscReal bounds[4]; 3002d5ee99bSBarry Smith PetscInt n = 4; 3012d5ee99bSBarry Smith PetscDraw draw; 3026934998bSLisandro Dalcin PetscDrawAxis axis; 3032d5ee99bSBarry Smith 3042d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 3052d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 3066934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr); 3072d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 3086934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr); 3096934998bSLisandro Dalcin ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 3106934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 3112d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3122d5ee99bSBarry Smith } 3132d5ee99bSBarry Smith opt = PETSC_FALSE; 3140dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 3153a471f94SBarry Smith if (opt) { 31683a4ac43SBarry Smith TSMonitorDrawCtx ctx; 31783a4ac43SBarry Smith PetscInt howoften = 1; 3183a471f94SBarry Smith 3190298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 3206934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 32183a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3223a471f94SBarry Smith } 323fde5950dSBarry Smith 324ed81e22dSJed Brown opt = PETSC_FALSE; 32591b97e58SBarry 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); 326ed81e22dSJed Brown if (flg) { 327ed81e22dSJed Brown const char *ptr,*ptr2; 328ed81e22dSJed Brown char *filetemplate; 329ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 330ed81e22dSJed Brown /* Do some cursory validation of the input. */ 331ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 332ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 333ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 334ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 335ce94432eSBarry 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"); 336ed81e22dSJed Brown if (ptr2) break; 337ed81e22dSJed Brown } 338ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 339ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 340ed81e22dSJed Brown } 341bdad233fSMatthew Knepley 342d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 343d1212d36SBarry Smith if (flg) { 344d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 345d1212d36SBarry Smith int ray = 0; 346d1212d36SBarry Smith DMDADirection ddir; 347d1212d36SBarry Smith DM da; 348d1212d36SBarry Smith PetscMPIInt rank; 349d1212d36SBarry Smith 350ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 351d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 352d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 353ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 354d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 355d1212d36SBarry Smith 356d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 357b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 358d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 359d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 360ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 361d1212d36SBarry Smith if (!rank) { 362d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 363d1212d36SBarry Smith } 36451b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 365d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 366d1212d36SBarry Smith } 36751b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 36851b4a12fSMatthew G. Knepley if (flg) { 36951b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 37051b4a12fSMatthew G. Knepley int ray = 0; 37151b4a12fSMatthew G. Knepley DMDADirection ddir; 37251b4a12fSMatthew G. Knepley DM da; 37351b4a12fSMatthew G. Knepley PetscInt howoften = 1; 374d1212d36SBarry Smith 37551b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 37651b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 37751b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 37851b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 37951b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 3801c3436cfSJed Brown 38151b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 382b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 38351b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 38451b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 38551b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 38651b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 38751b4a12fSMatthew G. Knepley } 388a7a1495cSBarry Smith 389b3d3934dSBarry Smith ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 390b3d3934dSBarry Smith if (opt) { 391b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 392b3d3934dSBarry Smith 393b3d3934dSBarry Smith ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 394b3d3934dSBarry Smith ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 395b3d3934dSBarry Smith } 396b3d3934dSBarry Smith 397847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 398847ff0e1SMatthew G. Knepley ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr); 399847ff0e1SMatthew G. Knepley if (flg) { 400847ff0e1SMatthew G. Knepley DM dm; 401847ff0e1SMatthew G. Knepley DMTS tdm; 402847ff0e1SMatthew G. Knepley 403847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 404847ff0e1SMatthew G. Knepley ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr); 405847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 406847ff0e1SMatthew G. Knepley ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr); 407847ff0e1SMatthew G. Knepley ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 408847ff0e1SMatthew G. Knepley } 409847ff0e1SMatthew G. Knepley 410ee346746SBarry Smith if (ts->adapt) { 411ee346746SBarry Smith ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr); 412ee346746SBarry Smith } 413d763cef2SBarry Smith 414d763cef2SBarry Smith /* Handle specific TS options */ 415d763cef2SBarry Smith if (ts->ops->setfromoptions) { 4166991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 417d763cef2SBarry Smith } 418fbc52257SHong Zhang 41968bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 42068bece0bSHong Zhang if (ts->trajectory) tflg = PETSC_TRUE; 42168bece0bSHong Zhang else tflg = PETSC_FALSE; 42268bece0bSHong Zhang ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 42368bece0bSHong Zhang if (tflg) { 42468bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 42568bece0bSHong Zhang } 42668bece0bSHong Zhang if (ts->adjoint_solve) tflg = PETSC_TRUE; 42768bece0bSHong Zhang else tflg = PETSC_FALSE; 42868bece0bSHong Zhang ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr); 42968bece0bSHong Zhang if (flg) { 43068bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 43168bece0bSHong Zhang ts->adjoint_solve = tflg; 43268bece0bSHong Zhang } 43368bece0bSHong Zhang if (ts->trajectory) { 43468bece0bSHong Zhang ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 43568bece0bSHong Zhang } 436d763cef2SBarry Smith 437d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 4380633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr); 439fbc52257SHong Zhang ierr = PetscOptionsEnd();CHKERRQ(ierr); 440d763cef2SBarry Smith 4416991f827SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4426991f827SBarry Smith if (snes) { 4436991f827SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 44449c41c37SLisandro Dalcin ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 445d763cef2SBarry Smith } 44668bece0bSHong Zhang 447d763cef2SBarry Smith PetscFunctionReturn(0); 448d763cef2SBarry Smith } 449d763cef2SBarry Smith 450d763cef2SBarry Smith #undef __FUNCT__ 451bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory" 452d2daff3dSHong Zhang /*@ 453bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 454d2daff3dSHong Zhang 455d2daff3dSHong Zhang Collective on TS 456d2daff3dSHong Zhang 457d2daff3dSHong Zhang Input Parameters: 458bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 459bc952696SBarry Smith 46068bece0bSHong Zhang Note: This routine should be called after all TS options have been set 461d2daff3dSHong Zhang 462d2daff3dSHong Zhang Level: intermediate 463d2daff3dSHong Zhang 464bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve() 465d2daff3dSHong Zhang 466bc952696SBarry Smith .keywords: TS, set, checkpoint, 467d2daff3dSHong Zhang @*/ 468bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 469d2daff3dSHong Zhang { 470bc952696SBarry Smith PetscErrorCode ierr; 471bc952696SBarry Smith 472d2daff3dSHong Zhang PetscFunctionBegin; 473d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 474bc952696SBarry Smith if (!ts->trajectory) { 475bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 476972caf09SHong Zhang ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 477bc952696SBarry Smith } 478d2daff3dSHong Zhang PetscFunctionReturn(0); 479d2daff3dSHong Zhang } 480d2daff3dSHong Zhang 481a7a1495cSBarry Smith #undef __FUNCT__ 482a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian" 483a7a1495cSBarry Smith /*@ 484a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 485a7a1495cSBarry Smith set with TSSetRHSJacobian(). 486a7a1495cSBarry Smith 487a7a1495cSBarry Smith Collective on TS and Vec 488a7a1495cSBarry Smith 489a7a1495cSBarry Smith Input Parameters: 490316643e7SJed Brown + ts - the TS context 491a7a1495cSBarry Smith . t - current timestep 4920910c330SBarry Smith - U - input vector 493a7a1495cSBarry Smith 494a7a1495cSBarry Smith Output Parameters: 495a7a1495cSBarry Smith + A - Jacobian matrix 496a7a1495cSBarry Smith . B - optional preconditioning matrix 497a7a1495cSBarry Smith - flag - flag indicating matrix structure 498a7a1495cSBarry Smith 499a7a1495cSBarry Smith Notes: 500a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 501a7a1495cSBarry Smith is used internally within the nonlinear solvers. 502a7a1495cSBarry Smith 50394b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 504a7a1495cSBarry Smith flag parameter. 505a7a1495cSBarry Smith 506a7a1495cSBarry Smith Level: developer 507a7a1495cSBarry Smith 508a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 509a7a1495cSBarry Smith 51094b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 511a7a1495cSBarry Smith @*/ 512d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 513a7a1495cSBarry Smith { 514dfbe8321SBarry Smith PetscErrorCode ierr; 515270bf2e7SJed Brown PetscObjectState Ustate; 51624989b8cSPeter Brune DM dm; 517942e3340SBarry Smith DMTS tsdm; 51824989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 51924989b8cSPeter Brune void *ctx; 52024989b8cSPeter Brune TSIJacobian ijacobianfunc; 521b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 522a7a1495cSBarry Smith 523a7a1495cSBarry Smith PetscFunctionBegin; 5240700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5250910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5260910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 52724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 528942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 52924989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 5300298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 531b2df71adSDebojyoti Ghosh ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 53259e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 533b2df71adSDebojyoti Ghosh if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 5340e4ef248SJed Brown PetscFunctionReturn(0); 535f8ede8e7SJed Brown } 536d90be118SSean Farley 537ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 538d90be118SSean Farley 539e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 54094ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 54194ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 54294ab13aaSBarry Smith if (A != B) { 54394ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 54494ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 545e1244c69SJed Brown } 546e1244c69SJed Brown ts->rhsjacobian.shift = 0; 547e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 548e1244c69SJed Brown } 549e1244c69SJed Brown 55024989b8cSPeter Brune if (rhsjacobianfunc) { 5516cd88445SBarry Smith PetscBool missing; 55294ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 553a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 554d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 555a7a1495cSBarry Smith PetscStackPop; 55694ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 5576cd88445SBarry Smith if (A) { 5586cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 5596cd88445SBarry 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"); 5606cd88445SBarry Smith } 5616cd88445SBarry Smith if (B && B != A) { 5626cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 5636cd88445SBarry 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"); 5646cd88445SBarry Smith } 565ef66eb69SBarry Smith } else { 56694ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 56794ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 568ef66eb69SBarry Smith } 5690e4ef248SJed Brown ts->rhsjacobian.time = t; 5700910c330SBarry Smith ts->rhsjacobian.X = U; 57159e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 572a7a1495cSBarry Smith PetscFunctionReturn(0); 573a7a1495cSBarry Smith } 574a7a1495cSBarry Smith 5754a2ae208SSatish Balay #undef __FUNCT__ 5764a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 577316643e7SJed Brown /*@ 578d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 579d763cef2SBarry Smith 580316643e7SJed Brown Collective on TS and Vec 581316643e7SJed Brown 582316643e7SJed Brown Input Parameters: 583316643e7SJed Brown + ts - the TS context 584316643e7SJed Brown . t - current time 5850910c330SBarry Smith - U - state vector 586316643e7SJed Brown 587316643e7SJed Brown Output Parameter: 588316643e7SJed Brown . y - right hand side 589316643e7SJed Brown 590316643e7SJed Brown Note: 591316643e7SJed Brown Most users should not need to explicitly call this routine, as it 592316643e7SJed Brown is used internally within the nonlinear solvers. 593316643e7SJed Brown 594316643e7SJed Brown Level: developer 595316643e7SJed Brown 596316643e7SJed Brown .keywords: TS, compute 597316643e7SJed Brown 598316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 599316643e7SJed Brown @*/ 6000910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 601d763cef2SBarry Smith { 602dfbe8321SBarry Smith PetscErrorCode ierr; 60324989b8cSPeter Brune TSRHSFunction rhsfunction; 60424989b8cSPeter Brune TSIFunction ifunction; 60524989b8cSPeter Brune void *ctx; 60624989b8cSPeter Brune DM dm; 60724989b8cSPeter Brune 608d763cef2SBarry Smith PetscFunctionBegin; 6090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6100910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6110700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 61224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 61324989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 6140298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 615d763cef2SBarry Smith 616ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 617d763cef2SBarry Smith 6180910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 61924989b8cSPeter Brune if (rhsfunction) { 620d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 6210910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 622d763cef2SBarry Smith PetscStackPop; 623214bc6a2SJed Brown } else { 624214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 625b2cd27e8SJed Brown } 62644a41b28SSean Farley 6270910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 628d763cef2SBarry Smith PetscFunctionReturn(0); 629d763cef2SBarry Smith } 630d763cef2SBarry Smith 6314a2ae208SSatish Balay #undef __FUNCT__ 632ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 633ef20d060SBarry Smith /*@ 634ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 635ef20d060SBarry Smith 636ef20d060SBarry Smith Collective on TS and Vec 637ef20d060SBarry Smith 638ef20d060SBarry Smith Input Parameters: 639ef20d060SBarry Smith + ts - the TS context 640ef20d060SBarry Smith - t - current time 641ef20d060SBarry Smith 642ef20d060SBarry Smith Output Parameter: 6430910c330SBarry Smith . U - the solution 644ef20d060SBarry Smith 645ef20d060SBarry Smith Note: 646ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 647ef20d060SBarry Smith is used internally within the nonlinear solvers. 648ef20d060SBarry Smith 649ef20d060SBarry Smith Level: developer 650ef20d060SBarry Smith 651ef20d060SBarry Smith .keywords: TS, compute 652ef20d060SBarry Smith 653abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 654ef20d060SBarry Smith @*/ 6550910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 656ef20d060SBarry Smith { 657ef20d060SBarry Smith PetscErrorCode ierr; 658ef20d060SBarry Smith TSSolutionFunction solutionfunction; 659ef20d060SBarry Smith void *ctx; 660ef20d060SBarry Smith DM dm; 661ef20d060SBarry Smith 662ef20d060SBarry Smith PetscFunctionBegin; 663ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6640910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 665ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 666ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 667ef20d060SBarry Smith 668ef20d060SBarry Smith if (solutionfunction) { 6699b7cd975SBarry Smith PetscStackPush("TS user solution function"); 6700910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 671ef20d060SBarry Smith PetscStackPop; 672ef20d060SBarry Smith } 673ef20d060SBarry Smith PetscFunctionReturn(0); 674ef20d060SBarry Smith } 6759b7cd975SBarry Smith #undef __FUNCT__ 6769b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction" 6779b7cd975SBarry Smith /*@ 6789b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6799b7cd975SBarry Smith 6809b7cd975SBarry Smith Collective on TS and Vec 6819b7cd975SBarry Smith 6829b7cd975SBarry Smith Input Parameters: 6839b7cd975SBarry Smith + ts - the TS context 6849b7cd975SBarry Smith - t - current time 6859b7cd975SBarry Smith 6869b7cd975SBarry Smith Output Parameter: 6879b7cd975SBarry Smith . U - the function value 6889b7cd975SBarry Smith 6899b7cd975SBarry Smith Note: 6909b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 6919b7cd975SBarry Smith is used internally within the nonlinear solvers. 6929b7cd975SBarry Smith 6939b7cd975SBarry Smith Level: developer 6949b7cd975SBarry Smith 6959b7cd975SBarry Smith .keywords: TS, compute 6969b7cd975SBarry Smith 6979b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 6989b7cd975SBarry Smith @*/ 6999b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 7009b7cd975SBarry Smith { 7019b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 7029b7cd975SBarry Smith void *ctx; 7039b7cd975SBarry Smith DM dm; 7049b7cd975SBarry Smith 7059b7cd975SBarry Smith PetscFunctionBegin; 7069b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7079b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7089b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 7099b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 7109b7cd975SBarry Smith 7119b7cd975SBarry Smith if (forcing) { 7129b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 7139b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 7149b7cd975SBarry Smith PetscStackPop; 7159b7cd975SBarry Smith } 7169b7cd975SBarry Smith PetscFunctionReturn(0); 7179b7cd975SBarry Smith } 718ef20d060SBarry Smith 719ef20d060SBarry Smith #undef __FUNCT__ 720214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 721214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 722214bc6a2SJed Brown { 7232dd45cf8SJed Brown Vec F; 724214bc6a2SJed Brown PetscErrorCode ierr; 725214bc6a2SJed Brown 726214bc6a2SJed Brown PetscFunctionBegin; 7270298fd71SBarry Smith *Frhs = NULL; 7280298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 729214bc6a2SJed Brown if (!ts->Frhs) { 7302dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 731214bc6a2SJed Brown } 732214bc6a2SJed Brown *Frhs = ts->Frhs; 733214bc6a2SJed Brown PetscFunctionReturn(0); 734214bc6a2SJed Brown } 735214bc6a2SJed Brown 736214bc6a2SJed Brown #undef __FUNCT__ 737214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 738214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 739214bc6a2SJed Brown { 740214bc6a2SJed Brown Mat A,B; 7412dd45cf8SJed Brown PetscErrorCode ierr; 742214bc6a2SJed Brown 743214bc6a2SJed Brown PetscFunctionBegin; 744c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 745c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 7460298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 747214bc6a2SJed Brown if (Arhs) { 748214bc6a2SJed Brown if (!ts->Arhs) { 749214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 750214bc6a2SJed Brown } 751214bc6a2SJed Brown *Arhs = ts->Arhs; 752214bc6a2SJed Brown } 753214bc6a2SJed Brown if (Brhs) { 754214bc6a2SJed Brown if (!ts->Brhs) { 755bdb70873SJed Brown if (A != B) { 756214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 757bdb70873SJed Brown } else { 758bdb70873SJed Brown ts->Brhs = ts->Arhs; 759bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 760bdb70873SJed Brown } 761214bc6a2SJed Brown } 762214bc6a2SJed Brown *Brhs = ts->Brhs; 763214bc6a2SJed Brown } 764214bc6a2SJed Brown PetscFunctionReturn(0); 765214bc6a2SJed Brown } 766214bc6a2SJed Brown 767214bc6a2SJed Brown #undef __FUNCT__ 768316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 769316643e7SJed Brown /*@ 7700910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 771316643e7SJed Brown 772316643e7SJed Brown Collective on TS and Vec 773316643e7SJed Brown 774316643e7SJed Brown Input Parameters: 775316643e7SJed Brown + ts - the TS context 776316643e7SJed Brown . t - current time 7770910c330SBarry Smith . U - state vector 7780910c330SBarry Smith . Udot - time derivative of state vector 779214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 780316643e7SJed Brown 781316643e7SJed Brown Output Parameter: 782316643e7SJed Brown . Y - right hand side 783316643e7SJed Brown 784316643e7SJed Brown Note: 785316643e7SJed Brown Most users should not need to explicitly call this routine, as it 786316643e7SJed Brown is used internally within the nonlinear solvers. 787316643e7SJed Brown 788316643e7SJed Brown If the user did did not write their equations in implicit form, this 789316643e7SJed Brown function recasts them in implicit form. 790316643e7SJed Brown 791316643e7SJed Brown Level: developer 792316643e7SJed Brown 793316643e7SJed Brown .keywords: TS, compute 794316643e7SJed Brown 795316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 796316643e7SJed Brown @*/ 7970910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 798316643e7SJed Brown { 799316643e7SJed Brown PetscErrorCode ierr; 80024989b8cSPeter Brune TSIFunction ifunction; 80124989b8cSPeter Brune TSRHSFunction rhsfunction; 80224989b8cSPeter Brune void *ctx; 80324989b8cSPeter Brune DM dm; 804316643e7SJed Brown 805316643e7SJed Brown PetscFunctionBegin; 8060700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8070910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8080910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 8090700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 810316643e7SJed Brown 81124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 81224989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 8130298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 81424989b8cSPeter Brune 815ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 816d90be118SSean Farley 8170910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 81824989b8cSPeter Brune if (ifunction) { 819316643e7SJed Brown PetscStackPush("TS user implicit function"); 8200910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 821316643e7SJed Brown PetscStackPop; 822214bc6a2SJed Brown } 823214bc6a2SJed Brown if (imex) { 82424989b8cSPeter Brune if (!ifunction) { 8250910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 8262dd45cf8SJed Brown } 82724989b8cSPeter Brune } else if (rhsfunction) { 82824989b8cSPeter Brune if (ifunction) { 829214bc6a2SJed Brown Vec Frhs; 830214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 8310910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 832214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 8332dd45cf8SJed Brown } else { 8340910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 8350910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 836316643e7SJed Brown } 8374a6899ffSJed Brown } 8380910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 839316643e7SJed Brown PetscFunctionReturn(0); 840316643e7SJed Brown } 841316643e7SJed Brown 842316643e7SJed Brown #undef __FUNCT__ 843316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 844316643e7SJed Brown /*@ 845316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 846316643e7SJed Brown 847316643e7SJed Brown Collective on TS and Vec 848316643e7SJed Brown 849316643e7SJed Brown Input 850316643e7SJed Brown Input Parameters: 851316643e7SJed Brown + ts - the TS context 852316643e7SJed Brown . t - current timestep 8530910c330SBarry Smith . U - state vector 8540910c330SBarry Smith . Udot - time derivative of state vector 855214bc6a2SJed Brown . shift - shift to apply, see note below 856214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 857316643e7SJed Brown 858316643e7SJed Brown Output Parameters: 859316643e7SJed Brown + A - Jacobian matrix 860316643e7SJed Brown . B - optional preconditioning matrix 861316643e7SJed Brown - flag - flag indicating matrix structure 862316643e7SJed Brown 863316643e7SJed Brown Notes: 8640910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 865316643e7SJed Brown 8660910c330SBarry Smith dF/dU + shift*dF/dUdot 867316643e7SJed Brown 868316643e7SJed Brown Most users should not need to explicitly call this routine, as it 869316643e7SJed Brown is used internally within the nonlinear solvers. 870316643e7SJed Brown 871316643e7SJed Brown Level: developer 872316643e7SJed Brown 873316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 874316643e7SJed Brown 875316643e7SJed Brown .seealso: TSSetIJacobian() 87663495f91SJed Brown @*/ 877d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 878316643e7SJed Brown { 879316643e7SJed Brown PetscErrorCode ierr; 88024989b8cSPeter Brune TSIJacobian ijacobian; 88124989b8cSPeter Brune TSRHSJacobian rhsjacobian; 88224989b8cSPeter Brune DM dm; 88324989b8cSPeter Brune void *ctx; 884316643e7SJed Brown 885316643e7SJed Brown PetscFunctionBegin; 8860700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8870910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8880910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 889316643e7SJed Brown PetscValidPointer(A,6); 89094ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 891316643e7SJed Brown PetscValidPointer(B,7); 89294ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 89324989b8cSPeter Brune 89424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 89524989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 8960298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 89724989b8cSPeter Brune 898ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 899316643e7SJed Brown 90094ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 90124989b8cSPeter Brune if (ijacobian) { 9026cd88445SBarry Smith PetscBool missing; 903316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 904d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 905316643e7SJed Brown PetscStackPop; 9066cd88445SBarry Smith if (A) { 9076cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 9086cd88445SBarry 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"); 9096cd88445SBarry Smith } 9106cd88445SBarry Smith if (B && B != A) { 9116cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 9126cd88445SBarry 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"); 9136cd88445SBarry Smith } 9144a6899ffSJed Brown } 915214bc6a2SJed Brown if (imex) { 916b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 91794ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 91894ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 91994ab13aaSBarry Smith if (A != B) { 92094ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 92194ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 922214bc6a2SJed Brown } 923214bc6a2SJed Brown } 924214bc6a2SJed Brown } else { 925e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 926e1244c69SJed Brown if (rhsjacobian) { 927bd373395SBarry Smith if (ijacobian) { 928214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 929bd373395SBarry Smith } else { 930bd373395SBarry Smith ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr); 931214bc6a2SJed Brown } 932d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 933e1244c69SJed Brown } 93494ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 935e1244c69SJed Brown ts->rhsjacobian.scale = -1; 936e1244c69SJed Brown ts->rhsjacobian.shift = shift; 93794ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 93894ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 93994ab13aaSBarry Smith if (A != B) { 94094ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 94194ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 942316643e7SJed Brown } 943e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 944e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 945e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 94694ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 94794ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 94894ab13aaSBarry Smith if (A != B) { 94994ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 95094ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 951214bc6a2SJed Brown } 952316643e7SJed Brown } 95394ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 95494ab13aaSBarry Smith if (A != B) { 95594ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 956316643e7SJed Brown } 957316643e7SJed Brown } 958316643e7SJed Brown } 95994ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 960316643e7SJed Brown PetscFunctionReturn(0); 961316643e7SJed Brown } 962316643e7SJed Brown 963316643e7SJed Brown #undef __FUNCT__ 9644a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 965d763cef2SBarry Smith /*@C 966d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 967b5abc632SBarry Smith where U_t = G(t,u). 968d763cef2SBarry Smith 9693f9fe445SBarry Smith Logically Collective on TS 970d763cef2SBarry Smith 971d763cef2SBarry Smith Input Parameters: 972d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 9730298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 974d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 975d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 9760298fd71SBarry Smith function evaluation routine (may be NULL) 977d763cef2SBarry Smith 978d763cef2SBarry Smith Calling sequence of func: 97987828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 980d763cef2SBarry Smith 981d763cef2SBarry Smith + t - current timestep 982d763cef2SBarry Smith . u - input vector 983d763cef2SBarry Smith . F - function vector 984d763cef2SBarry Smith - ctx - [optional] user-defined function context 985d763cef2SBarry Smith 986d763cef2SBarry Smith Level: beginner 987d763cef2SBarry Smith 9882bbac0d3SBarry Smith Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 9892bbac0d3SBarry Smith 990d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 991d763cef2SBarry Smith 992ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 993d763cef2SBarry Smith @*/ 994089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 995d763cef2SBarry Smith { 996089b2837SJed Brown PetscErrorCode ierr; 997089b2837SJed Brown SNES snes; 9980298fd71SBarry Smith Vec ralloc = NULL; 99924989b8cSPeter Brune DM dm; 1000d763cef2SBarry Smith 1001089b2837SJed Brown PetscFunctionBegin; 10020700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1003ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 100424989b8cSPeter Brune 100524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 100624989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 1007089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1008e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 1009e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 1010e856ceecSJed Brown r = ralloc; 1011e856ceecSJed Brown } 1012089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 1013e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1014d763cef2SBarry Smith PetscFunctionReturn(0); 1015d763cef2SBarry Smith } 1016d763cef2SBarry Smith 10174a2ae208SSatish Balay #undef __FUNCT__ 1018ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 1019ef20d060SBarry Smith /*@C 1020abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1021ef20d060SBarry Smith 1022ef20d060SBarry Smith Logically Collective on TS 1023ef20d060SBarry Smith 1024ef20d060SBarry Smith Input Parameters: 1025ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1026ef20d060SBarry Smith . f - routine for evaluating the solution 1027ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 10280298fd71SBarry Smith function evaluation routine (may be NULL) 1029ef20d060SBarry Smith 1030ef20d060SBarry Smith Calling sequence of func: 1031ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 1032ef20d060SBarry Smith 1033ef20d060SBarry Smith + t - current timestep 1034ef20d060SBarry Smith . u - output vector 1035ef20d060SBarry Smith - ctx - [optional] user-defined function context 1036ef20d060SBarry Smith 1037abd5a294SJed Brown Notes: 1038abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1039abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1040abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1041abd5a294SJed Brown 10424f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1043abd5a294SJed Brown 1044ef20d060SBarry Smith Level: beginner 1045ef20d060SBarry Smith 1046ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1047ef20d060SBarry Smith 10489b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 1049ef20d060SBarry Smith @*/ 1050ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1051ef20d060SBarry Smith { 1052ef20d060SBarry Smith PetscErrorCode ierr; 1053ef20d060SBarry Smith DM dm; 1054ef20d060SBarry Smith 1055ef20d060SBarry Smith PetscFunctionBegin; 1056ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1057ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1058ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 1059ef20d060SBarry Smith PetscFunctionReturn(0); 1060ef20d060SBarry Smith } 1061ef20d060SBarry Smith 1062ef20d060SBarry Smith #undef __FUNCT__ 10639b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction" 10649b7cd975SBarry Smith /*@C 10659b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 10669b7cd975SBarry Smith 10679b7cd975SBarry Smith Logically Collective on TS 10689b7cd975SBarry Smith 10699b7cd975SBarry Smith Input Parameters: 10709b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 10719b7cd975SBarry Smith . f - routine for evaluating the forcing function 10729b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 10730298fd71SBarry Smith function evaluation routine (may be NULL) 10749b7cd975SBarry Smith 10759b7cd975SBarry Smith Calling sequence of func: 10769b7cd975SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 10779b7cd975SBarry Smith 10789b7cd975SBarry Smith + t - current timestep 10799b7cd975SBarry Smith . u - output vector 10809b7cd975SBarry Smith - ctx - [optional] user-defined function context 10819b7cd975SBarry Smith 10829b7cd975SBarry Smith Notes: 10839b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 10849b7cd975SBarry Smith create closed-form solutions with a non-physical forcing term. 10859b7cd975SBarry Smith 10869b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 10879b7cd975SBarry Smith 10889b7cd975SBarry Smith Level: beginner 10899b7cd975SBarry Smith 10909b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 10919b7cd975SBarry Smith 10929b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 10939b7cd975SBarry Smith @*/ 10949b7cd975SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 10959b7cd975SBarry Smith { 10969b7cd975SBarry Smith PetscErrorCode ierr; 10979b7cd975SBarry Smith DM dm; 10989b7cd975SBarry Smith 10999b7cd975SBarry Smith PetscFunctionBegin; 11009b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11019b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 11029b7cd975SBarry Smith ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr); 11039b7cd975SBarry Smith PetscFunctionReturn(0); 11049b7cd975SBarry Smith } 11059b7cd975SBarry Smith 11069b7cd975SBarry Smith #undef __FUNCT__ 11074a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 1108d763cef2SBarry Smith /*@C 1109f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1110b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1111d763cef2SBarry Smith 11123f9fe445SBarry Smith Logically Collective on TS 1113d763cef2SBarry Smith 1114d763cef2SBarry Smith Input Parameters: 1115d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1116e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1117e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1118d763cef2SBarry Smith . f - the Jacobian evaluation routine 1119d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 11200298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1121d763cef2SBarry Smith 1122f7ab8db6SBarry Smith Calling sequence of f: 1123ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1124d763cef2SBarry Smith 1125d763cef2SBarry Smith + t - current timestep 1126d763cef2SBarry Smith . u - input vector 1127e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1128e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1129d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1130d763cef2SBarry Smith 11316cd88445SBarry Smith Notes: 11326cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 11336cd88445SBarry Smith 11346cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1135ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1136d763cef2SBarry Smith 1137d763cef2SBarry Smith Level: beginner 1138d763cef2SBarry Smith 1139d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1140d763cef2SBarry Smith 1141ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1142d763cef2SBarry Smith 1143d763cef2SBarry Smith @*/ 1144e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1145d763cef2SBarry Smith { 1146277b19d0SLisandro Dalcin PetscErrorCode ierr; 1147089b2837SJed Brown SNES snes; 114824989b8cSPeter Brune DM dm; 114924989b8cSPeter Brune TSIJacobian ijacobian; 1150277b19d0SLisandro Dalcin 1151d763cef2SBarry Smith PetscFunctionBegin; 11520700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1153e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1154e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1155e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1156e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1157d763cef2SBarry Smith 115824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 115924989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1160e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1161e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1162e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1163e1244c69SJed Brown } 11640298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1165089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11665f659677SPeter Brune if (!ijacobian) { 1167e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 11680e4ef248SJed Brown } 1169e5d3d808SBarry Smith if (Amat) { 1170e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 11710e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1172bbd56ea5SKarl Rupp 1173e5d3d808SBarry Smith ts->Arhs = Amat; 11740e4ef248SJed Brown } 1175e5d3d808SBarry Smith if (Pmat) { 1176e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 11770e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1178bbd56ea5SKarl Rupp 1179e5d3d808SBarry Smith ts->Brhs = Pmat; 11800e4ef248SJed Brown } 1181d763cef2SBarry Smith PetscFunctionReturn(0); 1182d763cef2SBarry Smith } 1183d763cef2SBarry Smith 1184316643e7SJed Brown 1185316643e7SJed Brown #undef __FUNCT__ 1186316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 1187316643e7SJed Brown /*@C 1188b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1189316643e7SJed Brown 11903f9fe445SBarry Smith Logically Collective on TS 1191316643e7SJed Brown 1192316643e7SJed Brown Input Parameters: 1193316643e7SJed Brown + ts - the TS context obtained from TSCreate() 11940298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1195316643e7SJed Brown . f - the function evaluation routine 11960298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1197316643e7SJed Brown 1198316643e7SJed Brown Calling sequence of f: 1199316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1200316643e7SJed Brown 1201316643e7SJed Brown + t - time at step/stage being solved 1202316643e7SJed Brown . u - state vector 1203316643e7SJed Brown . u_t - time derivative of state vector 1204316643e7SJed Brown . F - function vector 1205316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1206316643e7SJed Brown 1207316643e7SJed Brown Important: 12082bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1209316643e7SJed Brown 1210316643e7SJed Brown Level: beginner 1211316643e7SJed Brown 1212316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1213316643e7SJed Brown 1214d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1215316643e7SJed Brown @*/ 1216089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 1217316643e7SJed Brown { 1218089b2837SJed Brown PetscErrorCode ierr; 1219089b2837SJed Brown SNES snes; 12200298fd71SBarry Smith Vec resalloc = NULL; 122124989b8cSPeter Brune DM dm; 1222316643e7SJed Brown 1223316643e7SJed Brown PetscFunctionBegin; 12240700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1225ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 122624989b8cSPeter Brune 122724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 122824989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 122924989b8cSPeter Brune 1230089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1231e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 1232e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 1233e856ceecSJed Brown res = resalloc; 1234e856ceecSJed Brown } 1235089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 1236e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 1237089b2837SJed Brown PetscFunctionReturn(0); 1238089b2837SJed Brown } 1239089b2837SJed Brown 1240089b2837SJed Brown #undef __FUNCT__ 1241089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 1242089b2837SJed Brown /*@C 1243089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1244089b2837SJed Brown 1245089b2837SJed Brown Not Collective 1246089b2837SJed Brown 1247089b2837SJed Brown Input Parameter: 1248089b2837SJed Brown . ts - the TS context 1249089b2837SJed Brown 1250089b2837SJed Brown Output Parameter: 12510298fd71SBarry Smith + r - vector to hold residual (or NULL) 12520298fd71SBarry Smith . func - the function to compute residual (or NULL) 12530298fd71SBarry Smith - ctx - the function context (or NULL) 1254089b2837SJed Brown 1255089b2837SJed Brown Level: advanced 1256089b2837SJed Brown 1257089b2837SJed Brown .keywords: TS, nonlinear, get, function 1258089b2837SJed Brown 1259089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1260089b2837SJed Brown @*/ 1261089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1262089b2837SJed Brown { 1263089b2837SJed Brown PetscErrorCode ierr; 1264089b2837SJed Brown SNES snes; 126524989b8cSPeter Brune DM dm; 1266089b2837SJed Brown 1267089b2837SJed Brown PetscFunctionBegin; 1268089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1269089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12700298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 127124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 127224989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1273089b2837SJed Brown PetscFunctionReturn(0); 1274089b2837SJed Brown } 1275089b2837SJed Brown 1276089b2837SJed Brown #undef __FUNCT__ 1277089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 1278089b2837SJed Brown /*@C 1279089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1280089b2837SJed Brown 1281089b2837SJed Brown Not Collective 1282089b2837SJed Brown 1283089b2837SJed Brown Input Parameter: 1284089b2837SJed Brown . ts - the TS context 1285089b2837SJed Brown 1286089b2837SJed Brown Output Parameter: 12870298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 12880298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 12890298fd71SBarry Smith - ctx - the function context (or NULL) 1290089b2837SJed Brown 1291089b2837SJed Brown Level: advanced 1292089b2837SJed Brown 1293089b2837SJed Brown .keywords: TS, nonlinear, get, function 1294089b2837SJed Brown 12952bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1296089b2837SJed Brown @*/ 1297089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1298089b2837SJed Brown { 1299089b2837SJed Brown PetscErrorCode ierr; 1300089b2837SJed Brown SNES snes; 130124989b8cSPeter Brune DM dm; 1302089b2837SJed Brown 1303089b2837SJed Brown PetscFunctionBegin; 1304089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1305089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13060298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 130724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 130824989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1309316643e7SJed Brown PetscFunctionReturn(0); 1310316643e7SJed Brown } 1311316643e7SJed Brown 1312316643e7SJed Brown #undef __FUNCT__ 1313316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 1314316643e7SJed Brown /*@C 1315a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1316ae8867d6SBarry Smith provided with TSSetIFunction(). 1317316643e7SJed Brown 13183f9fe445SBarry Smith Logically Collective on TS 1319316643e7SJed Brown 1320316643e7SJed Brown Input Parameters: 1321316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1322e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1323e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1324316643e7SJed Brown . f - the Jacobian evaluation routine 13250298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1326316643e7SJed Brown 1327316643e7SJed Brown Calling sequence of f: 1328ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1329316643e7SJed Brown 1330316643e7SJed Brown + t - time at step/stage being solved 13311b4a444bSJed Brown . U - state vector 13321b4a444bSJed Brown . U_t - time derivative of state vector 1333316643e7SJed Brown . a - shift 1334e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1335e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1336316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1337316643e7SJed Brown 1338316643e7SJed Brown Notes: 1339e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1340316643e7SJed Brown 1341895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1342895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1343895c21f2SBarry Smith 1344a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1345b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1346a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1347a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1348a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1349a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1350a4f0a591SBarry Smith 13516cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13526cd88445SBarry Smith 13536cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1354ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1355ca5f011dSBarry Smith 1356316643e7SJed Brown Level: beginner 1357316643e7SJed Brown 1358316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1359316643e7SJed Brown 1360ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1361316643e7SJed Brown 1362316643e7SJed Brown @*/ 1363e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1364316643e7SJed Brown { 1365316643e7SJed Brown PetscErrorCode ierr; 1366089b2837SJed Brown SNES snes; 136724989b8cSPeter Brune DM dm; 1368316643e7SJed Brown 1369316643e7SJed Brown PetscFunctionBegin; 13700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1371e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1372e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1373e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1374e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 137524989b8cSPeter Brune 137624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 137724989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 137824989b8cSPeter Brune 1379089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1380e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1381316643e7SJed Brown PetscFunctionReturn(0); 1382316643e7SJed Brown } 1383316643e7SJed Brown 13844a2ae208SSatish Balay #undef __FUNCT__ 1385e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse" 1386e1244c69SJed Brown /*@ 1387e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1388e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1389e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1390e1244c69SJed Brown not been changed by the TS. 1391e1244c69SJed Brown 1392e1244c69SJed Brown Logically Collective 1393e1244c69SJed Brown 1394e1244c69SJed Brown Input Arguments: 1395e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1396e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1397e1244c69SJed Brown 1398e1244c69SJed Brown Level: intermediate 1399e1244c69SJed Brown 1400e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1401e1244c69SJed Brown @*/ 1402e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1403e1244c69SJed Brown { 1404e1244c69SJed Brown PetscFunctionBegin; 1405e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1406e1244c69SJed Brown PetscFunctionReturn(0); 1407e1244c69SJed Brown } 1408e1244c69SJed Brown 1409e1244c69SJed Brown #undef __FUNCT__ 141055849f57SBarry Smith #define __FUNCT__ "TSLoad" 141155849f57SBarry Smith /*@C 141255849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 141355849f57SBarry Smith 141455849f57SBarry Smith Collective on PetscViewer 141555849f57SBarry Smith 141655849f57SBarry Smith Input Parameters: 141755849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 141855849f57SBarry Smith some related function before a call to TSLoad(). 141955849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 142055849f57SBarry Smith 142155849f57SBarry Smith Level: intermediate 142255849f57SBarry Smith 142355849f57SBarry Smith Notes: 142455849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 142555849f57SBarry Smith 142655849f57SBarry Smith Notes for advanced users: 142755849f57SBarry Smith Most users should not need to know the details of the binary storage 142855849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 142955849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 143055849f57SBarry Smith format is 143155849f57SBarry Smith .vb 143255849f57SBarry Smith has not yet been determined 143355849f57SBarry Smith .ve 143455849f57SBarry Smith 143555849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 143655849f57SBarry Smith @*/ 1437f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 143855849f57SBarry Smith { 143955849f57SBarry Smith PetscErrorCode ierr; 144055849f57SBarry Smith PetscBool isbinary; 144155849f57SBarry Smith PetscInt classid; 144255849f57SBarry Smith char type[256]; 14432d53ad75SBarry Smith DMTS sdm; 1444ad6bc421SBarry Smith DM dm; 144555849f57SBarry Smith 144655849f57SBarry Smith PetscFunctionBegin; 1447f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 144855849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 144955849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 145055849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 145155849f57SBarry Smith 1452060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1453ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1454060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1455f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1456f2c2a1b9SBarry Smith if (ts->ops->load) { 1457f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1458f2c2a1b9SBarry Smith } 1459ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1460ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1461ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1462f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1463f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 14642d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 14652d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 146655849f57SBarry Smith PetscFunctionReturn(0); 146755849f57SBarry Smith } 146855849f57SBarry Smith 14699804daf3SBarry Smith #include <petscdraw.h> 1470e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1471e04113cfSBarry Smith #include <petscviewersaws.h> 1472f05ece33SBarry Smith #endif 147355849f57SBarry Smith #undef __FUNCT__ 14744a2ae208SSatish Balay #define __FUNCT__ "TSView" 14757e2c5f70SBarry Smith /*@C 1476d763cef2SBarry Smith TSView - Prints the TS data structure. 1477d763cef2SBarry Smith 14784c49b128SBarry Smith Collective on TS 1479d763cef2SBarry Smith 1480d763cef2SBarry Smith Input Parameters: 1481d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1482d763cef2SBarry Smith - viewer - visualization context 1483d763cef2SBarry Smith 1484d763cef2SBarry Smith Options Database Key: 1485d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1486d763cef2SBarry Smith 1487d763cef2SBarry Smith Notes: 1488d763cef2SBarry Smith The available visualization contexts include 1489b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1490b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1491d763cef2SBarry Smith output where only the first processor opens 1492d763cef2SBarry Smith the file. All other processors send their 1493d763cef2SBarry Smith data to the first processor to print. 1494d763cef2SBarry Smith 1495d763cef2SBarry Smith The user can open an alternative visualization context with 1496b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1497d763cef2SBarry Smith 1498d763cef2SBarry Smith Level: beginner 1499d763cef2SBarry Smith 1500d763cef2SBarry Smith .keywords: TS, timestep, view 1501d763cef2SBarry Smith 1502b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1503d763cef2SBarry Smith @*/ 15047087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1505d763cef2SBarry Smith { 1506dfbe8321SBarry Smith PetscErrorCode ierr; 150719fd82e9SBarry Smith TSType type; 15082b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 15092d53ad75SBarry Smith DMTS sdm; 1510e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1511536b137fSBarry Smith PetscBool issaws; 1512f05ece33SBarry Smith #endif 1513d763cef2SBarry Smith 1514d763cef2SBarry Smith PetscFunctionBegin; 15150700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15163050cee2SBarry Smith if (!viewer) { 1517ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 15183050cee2SBarry Smith } 15190700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1520c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1521fd16b177SBarry Smith 1522251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1523251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 152455849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 15252b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1526e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1527536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1528f05ece33SBarry Smith #endif 152932077d6dSBarry Smith if (iascii) { 1530dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 153177431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 15327c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1533d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 15345ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1535c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1536d763cef2SBarry Smith } 15375ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1538193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 15392d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 15402d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1541d52bd9f3SBarry Smith if (ts->ops->view) { 1542d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1543d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1544d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1545d52bd9f3SBarry Smith } 15460f5bd95cSBarry Smith } else if (isstring) { 1547a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1548b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 154955849f57SBarry Smith } else if (isbinary) { 155055849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 155155849f57SBarry Smith MPI_Comm comm; 155255849f57SBarry Smith PetscMPIInt rank; 155355849f57SBarry Smith char type[256]; 155455849f57SBarry Smith 155555849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 155655849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 155755849f57SBarry Smith if (!rank) { 155855849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 155955849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 156055849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 156155849f57SBarry Smith } 156255849f57SBarry Smith if (ts->ops->view) { 156355849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 156455849f57SBarry Smith } 1565f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1566f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 15672d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 15682d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 15692b0a91c0SBarry Smith } else if (isdraw) { 15702b0a91c0SBarry Smith PetscDraw draw; 15712b0a91c0SBarry Smith char str[36]; 157289fd9fafSBarry Smith PetscReal x,y,bottom,h; 15732b0a91c0SBarry Smith 15742b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 15752b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 15762b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 15772b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 157851fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 157989fd9fafSBarry Smith bottom = y - h; 15802b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 15812b0a91c0SBarry Smith if (ts->ops->view) { 15822b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 15832b0a91c0SBarry Smith } 15842b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1585e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1586536b137fSBarry Smith } else if (issaws) { 1587d45a07a7SBarry Smith PetscMPIInt rank; 15882657e9d9SBarry Smith const char *name; 15892657e9d9SBarry Smith 15902657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1591d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1592d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1593d45a07a7SBarry Smith char dir[1024]; 1594d45a07a7SBarry Smith 1595e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1596a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 15972657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 15982657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 15992657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1600d763cef2SBarry Smith } 16010acecf5bSBarry Smith if (ts->ops->view) { 16020acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 16030acecf5bSBarry Smith } 1604f05ece33SBarry Smith #endif 1605f05ece33SBarry Smith } 1606f05ece33SBarry Smith 1607b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1608251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1609b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1610d763cef2SBarry Smith PetscFunctionReturn(0); 1611d763cef2SBarry Smith } 1612d763cef2SBarry Smith 1613d763cef2SBarry Smith 16144a2ae208SSatish Balay #undef __FUNCT__ 16154a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1616b07ff414SBarry Smith /*@ 1617d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1618d763cef2SBarry Smith the timesteppers. 1619d763cef2SBarry Smith 16203f9fe445SBarry Smith Logically Collective on TS 1621d763cef2SBarry Smith 1622d763cef2SBarry Smith Input Parameters: 1623d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1624d763cef2SBarry Smith - usrP - optional user context 1625d763cef2SBarry Smith 1626daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 1627daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1628daf670e6SBarry Smith 1629d763cef2SBarry Smith Level: intermediate 1630d763cef2SBarry Smith 1631d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1632d763cef2SBarry Smith 1633d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1634d763cef2SBarry Smith @*/ 16357087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1636d763cef2SBarry Smith { 1637d763cef2SBarry Smith PetscFunctionBegin; 16380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1639d763cef2SBarry Smith ts->user = usrP; 1640d763cef2SBarry Smith PetscFunctionReturn(0); 1641d763cef2SBarry Smith } 1642d763cef2SBarry Smith 16434a2ae208SSatish Balay #undef __FUNCT__ 16444a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1645b07ff414SBarry Smith /*@ 1646d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1647d763cef2SBarry Smith timestepper. 1648d763cef2SBarry Smith 1649d763cef2SBarry Smith Not Collective 1650d763cef2SBarry Smith 1651d763cef2SBarry Smith Input Parameter: 1652d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1653d763cef2SBarry Smith 1654d763cef2SBarry Smith Output Parameter: 1655d763cef2SBarry Smith . usrP - user context 1656d763cef2SBarry Smith 1657daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 1658daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1659daf670e6SBarry Smith 1660d763cef2SBarry Smith Level: intermediate 1661d763cef2SBarry Smith 1662d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1663d763cef2SBarry Smith 1664d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1665d763cef2SBarry Smith @*/ 1666e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1667d763cef2SBarry Smith { 1668d763cef2SBarry Smith PetscFunctionBegin; 16690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1670e71120c6SJed Brown *(void**)usrP = ts->user; 1671d763cef2SBarry Smith PetscFunctionReturn(0); 1672d763cef2SBarry Smith } 1673d763cef2SBarry Smith 16744a2ae208SSatish Balay #undef __FUNCT__ 16754a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1676d763cef2SBarry Smith /*@ 1677b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1678d763cef2SBarry Smith 1679d763cef2SBarry Smith Not Collective 1680d763cef2SBarry Smith 1681d763cef2SBarry Smith Input Parameter: 1682d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1683d763cef2SBarry Smith 1684d763cef2SBarry Smith Output Parameter: 1685b8123daeSJed Brown . iter - number of steps completed so far 1686d763cef2SBarry Smith 1687d763cef2SBarry Smith Level: intermediate 1688d763cef2SBarry Smith 1689d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 16909be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 1691d763cef2SBarry Smith @*/ 16927087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 1693d763cef2SBarry Smith { 1694d763cef2SBarry Smith PetscFunctionBegin; 16950700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 16964482741eSBarry Smith PetscValidIntPointer(iter,2); 1697d763cef2SBarry Smith *iter = ts->steps; 1698d763cef2SBarry Smith PetscFunctionReturn(0); 1699d763cef2SBarry Smith } 1700d763cef2SBarry Smith 17014a2ae208SSatish Balay #undef __FUNCT__ 17024a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1703d763cef2SBarry Smith /*@ 1704d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1705d763cef2SBarry Smith as well as the initial time. 1706d763cef2SBarry Smith 17073f9fe445SBarry Smith Logically Collective on TS 1708d763cef2SBarry Smith 1709d763cef2SBarry Smith Input Parameters: 1710d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1711d763cef2SBarry Smith . initial_time - the initial time 1712d763cef2SBarry Smith - time_step - the size of the timestep 1713d763cef2SBarry Smith 1714d763cef2SBarry Smith Level: intermediate 1715d763cef2SBarry Smith 1716d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1717d763cef2SBarry Smith 1718d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1719d763cef2SBarry Smith @*/ 17207087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1721d763cef2SBarry Smith { 1722e144a568SJed Brown PetscErrorCode ierr; 1723e144a568SJed Brown 1724d763cef2SBarry Smith PetscFunctionBegin; 17250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1726e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1727d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1728d763cef2SBarry Smith PetscFunctionReturn(0); 1729d763cef2SBarry Smith } 1730d763cef2SBarry Smith 17314a2ae208SSatish Balay #undef __FUNCT__ 17324a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1733d763cef2SBarry Smith /*@ 1734d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1735d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1736d763cef2SBarry Smith 17373f9fe445SBarry Smith Logically Collective on TS 1738d763cef2SBarry Smith 1739d763cef2SBarry Smith Input Parameters: 1740d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1741d763cef2SBarry Smith - time_step - the size of the timestep 1742d763cef2SBarry Smith 1743d763cef2SBarry Smith Level: intermediate 1744d763cef2SBarry Smith 1745d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1746d763cef2SBarry Smith 1747d763cef2SBarry Smith .keywords: TS, set, timestep 1748d763cef2SBarry Smith @*/ 17497087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1750d763cef2SBarry Smith { 1751d763cef2SBarry Smith PetscFunctionBegin; 17520700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1753c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1754d763cef2SBarry Smith ts->time_step = time_step; 175531748224SBarry Smith ts->time_step_orig = time_step; 1756d763cef2SBarry Smith PetscFunctionReturn(0); 1757d763cef2SBarry Smith } 1758d763cef2SBarry Smith 17594a2ae208SSatish Balay #undef __FUNCT__ 1760a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1761a43b19c4SJed Brown /*@ 176249354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 176349354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 176449354f04SShri Abhyankar or just return at the final time TS computed. 1765a43b19c4SJed Brown 1766a43b19c4SJed Brown Logically Collective on TS 1767a43b19c4SJed Brown 1768a43b19c4SJed Brown Input Parameter: 1769a43b19c4SJed Brown + ts - the time-step context 177049354f04SShri Abhyankar - eftopt - exact final time option 1771a43b19c4SJed Brown 1772feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 1773feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 1774feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 1775feed9e9dSBarry Smith 1776feed9e9dSBarry Smith Options Database: 1777feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 1778feed9e9dSBarry Smith 1779ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 1780ee346746SBarry Smith then the final time you selected. 1781ee346746SBarry Smith 1782a43b19c4SJed Brown Level: beginner 1783a43b19c4SJed Brown 1784a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 1785a43b19c4SJed Brown @*/ 178649354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 1787a43b19c4SJed Brown { 1788a43b19c4SJed Brown PetscFunctionBegin; 1789a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 179049354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 179149354f04SShri Abhyankar ts->exact_final_time = eftopt; 1792a43b19c4SJed Brown PetscFunctionReturn(0); 1793a43b19c4SJed Brown } 1794a43b19c4SJed Brown 1795a43b19c4SJed Brown #undef __FUNCT__ 17964a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1797d763cef2SBarry Smith /*@ 1798d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1799d763cef2SBarry Smith 1800d763cef2SBarry Smith Not Collective 1801d763cef2SBarry Smith 1802d763cef2SBarry Smith Input Parameter: 1803d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1804d763cef2SBarry Smith 1805d763cef2SBarry Smith Output Parameter: 1806d763cef2SBarry Smith . dt - the current timestep size 1807d763cef2SBarry Smith 1808d763cef2SBarry Smith Level: intermediate 1809d763cef2SBarry Smith 1810d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1811d763cef2SBarry Smith 1812d763cef2SBarry Smith .keywords: TS, get, timestep 1813d763cef2SBarry Smith @*/ 18147087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 1815d763cef2SBarry Smith { 1816d763cef2SBarry Smith PetscFunctionBegin; 18170700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1818f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1819d763cef2SBarry Smith *dt = ts->time_step; 1820d763cef2SBarry Smith PetscFunctionReturn(0); 1821d763cef2SBarry Smith } 1822d763cef2SBarry Smith 18234a2ae208SSatish Balay #undef __FUNCT__ 18244a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1825d8e5e3e6SSatish Balay /*@ 1826d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1827d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1828d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1829d763cef2SBarry Smith the solution at the next timestep has been calculated. 1830d763cef2SBarry Smith 1831d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1832d763cef2SBarry Smith 1833d763cef2SBarry Smith Input Parameter: 1834d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1835d763cef2SBarry Smith 1836d763cef2SBarry Smith Output Parameter: 1837d763cef2SBarry Smith . v - the vector containing the solution 1838d763cef2SBarry Smith 1839d763cef2SBarry Smith Level: intermediate 1840d763cef2SBarry Smith 1841d763cef2SBarry Smith .seealso: TSGetTimeStep() 1842d763cef2SBarry Smith 1843d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1844d763cef2SBarry Smith @*/ 18457087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1846d763cef2SBarry Smith { 1847d763cef2SBarry Smith PetscFunctionBegin; 18480700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18494482741eSBarry Smith PetscValidPointer(v,2); 18508737fe31SLisandro Dalcin *v = ts->vec_sol; 1851d763cef2SBarry Smith PetscFunctionReturn(0); 1852d763cef2SBarry Smith } 1853d763cef2SBarry Smith 1854c235aa8dSHong Zhang #undef __FUNCT__ 1855dfb21088SHong Zhang #define __FUNCT__ "TSGetCostGradients" 1856c235aa8dSHong Zhang /*@ 1857dfb21088SHong Zhang TSGetCostGradients - Returns the gradients from the TSAdjointSolve() 1858c235aa8dSHong Zhang 1859c235aa8dSHong Zhang Not Collective, but Vec returned is parallel if TS is parallel 1860c235aa8dSHong Zhang 1861c235aa8dSHong Zhang Input Parameter: 1862c235aa8dSHong Zhang . ts - the TS context obtained from TSCreate() 1863c235aa8dSHong Zhang 1864c235aa8dSHong Zhang Output Parameter: 1865abc2977eSBarry Smith + lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 1866abc2977eSBarry Smith - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 1867c235aa8dSHong Zhang 1868c235aa8dSHong Zhang Level: intermediate 1869c235aa8dSHong Zhang 1870c235aa8dSHong Zhang .seealso: TSGetTimeStep() 1871c235aa8dSHong Zhang 1872c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity 1873c235aa8dSHong Zhang @*/ 1874dfb21088SHong Zhang PetscErrorCode TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu) 1875c235aa8dSHong Zhang { 1876c235aa8dSHong Zhang PetscFunctionBegin; 1877c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1878abc2977eSBarry Smith if (numcost) *numcost = ts->numcost; 1879abc2977eSBarry Smith if (lambda) *lambda = ts->vecs_sensi; 1880abc2977eSBarry Smith if (mu) *mu = ts->vecs_sensip; 1881c235aa8dSHong Zhang PetscFunctionReturn(0); 1882c235aa8dSHong Zhang } 1883c235aa8dSHong Zhang 1884bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 18854a2ae208SSatish Balay #undef __FUNCT__ 1886bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1887d8e5e3e6SSatish Balay /*@ 1888bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1889d763cef2SBarry Smith 1890bdad233fSMatthew Knepley Not collective 1891d763cef2SBarry Smith 1892bdad233fSMatthew Knepley Input Parameters: 1893bdad233fSMatthew Knepley + ts - The TS 1894bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1895d763cef2SBarry Smith .vb 18960910c330SBarry Smith U_t - A U = 0 (linear) 18970910c330SBarry Smith U_t - A(t) U = 0 (linear) 18980910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 1899d763cef2SBarry Smith .ve 1900d763cef2SBarry Smith 1901d763cef2SBarry Smith Level: beginner 1902d763cef2SBarry Smith 1903bdad233fSMatthew Knepley .keywords: TS, problem type 1904bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1905d763cef2SBarry Smith @*/ 19067087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1907a7cc72afSBarry Smith { 19089e2a6581SJed Brown PetscErrorCode ierr; 19099e2a6581SJed Brown 1910d763cef2SBarry Smith PetscFunctionBegin; 19110700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1912bdad233fSMatthew Knepley ts->problem_type = type; 19139e2a6581SJed Brown if (type == TS_LINEAR) { 19149e2a6581SJed Brown SNES snes; 19159e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 19169e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 19179e2a6581SJed Brown } 1918d763cef2SBarry Smith PetscFunctionReturn(0); 1919d763cef2SBarry Smith } 1920d763cef2SBarry Smith 1921bdad233fSMatthew Knepley #undef __FUNCT__ 1922bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1923bdad233fSMatthew Knepley /*@C 1924bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1925bdad233fSMatthew Knepley 1926bdad233fSMatthew Knepley Not collective 1927bdad233fSMatthew Knepley 1928bdad233fSMatthew Knepley Input Parameter: 1929bdad233fSMatthew Knepley . ts - The TS 1930bdad233fSMatthew Knepley 1931bdad233fSMatthew Knepley Output Parameter: 1932bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1933bdad233fSMatthew Knepley .vb 1934089b2837SJed Brown M U_t = A U 1935089b2837SJed Brown M(t) U_t = A(t) U 1936b5abc632SBarry Smith F(t,U,U_t) 1937bdad233fSMatthew Knepley .ve 1938bdad233fSMatthew Knepley 1939bdad233fSMatthew Knepley Level: beginner 1940bdad233fSMatthew Knepley 1941bdad233fSMatthew Knepley .keywords: TS, problem type 1942bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1943bdad233fSMatthew Knepley @*/ 19447087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1945a7cc72afSBarry Smith { 1946bdad233fSMatthew Knepley PetscFunctionBegin; 19470700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 19484482741eSBarry Smith PetscValidIntPointer(type,2); 1949bdad233fSMatthew Knepley *type = ts->problem_type; 1950bdad233fSMatthew Knepley PetscFunctionReturn(0); 1951bdad233fSMatthew Knepley } 1952d763cef2SBarry Smith 19534a2ae208SSatish Balay #undef __FUNCT__ 19544a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1955d763cef2SBarry Smith /*@ 1956d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1957d763cef2SBarry Smith of a timestepper. 1958d763cef2SBarry Smith 1959d763cef2SBarry Smith Collective on TS 1960d763cef2SBarry Smith 1961d763cef2SBarry Smith Input Parameter: 1962d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1963d763cef2SBarry Smith 1964d763cef2SBarry Smith Notes: 1965d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1966d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1967d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1968d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1969d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1970d763cef2SBarry Smith 1971d763cef2SBarry Smith Level: advanced 1972d763cef2SBarry Smith 1973d763cef2SBarry Smith .keywords: TS, timestep, setup 1974d763cef2SBarry Smith 1975d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1976d763cef2SBarry Smith @*/ 19777087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1978d763cef2SBarry Smith { 1979dfbe8321SBarry Smith PetscErrorCode ierr; 19806c6b9e74SPeter Brune DM dm; 19816c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 1982d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 19836c6b9e74SPeter Brune TSIJacobian ijac; 19846c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1985d763cef2SBarry Smith 1986d763cef2SBarry Smith PetscFunctionBegin; 19870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1988277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1989277b19d0SLisandro Dalcin 19902c18e0fdSBarry Smith ts->total_steps = 0; 19917adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 19929596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1993d763cef2SBarry Smith } 1994277b19d0SLisandro Dalcin 1995277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1996277b19d0SLisandro Dalcin 19971c3436cfSJed Brown 1998e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 1999e1244c69SJed Brown Mat Amat,Pmat; 2000e1244c69SJed Brown SNES snes; 2001e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2002e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 2003e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2004e1244c69SJed Brown * have displaced the RHS matrix */ 2005e1244c69SJed Brown if (Amat == ts->Arhs) { 2006e1244c69SJed Brown ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 2007e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 2008e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 2009e1244c69SJed Brown } 2010e1244c69SJed Brown if (Pmat == ts->Brhs) { 2011e1244c69SJed Brown ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 2012e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 2013e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 2014e1244c69SJed Brown } 2015e1244c69SJed Brown } 2016277b19d0SLisandro Dalcin if (ts->ops->setup) { 2017000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 2018277b19d0SLisandro Dalcin } 2019277b19d0SLisandro Dalcin 20206c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 20216c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 20226c6b9e74SPeter Brune */ 20236c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 20240298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 20256c6b9e74SPeter Brune if (!func) { 20266c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 20276c6b9e74SPeter Brune } 20286c6b9e74SPeter 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. 20296c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 20306c6b9e74SPeter Brune */ 20310298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 20320298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 20330298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 20346c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 20356c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 20366c6b9e74SPeter Brune } 2037277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2038277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2039277b19d0SLisandro Dalcin } 2040277b19d0SLisandro Dalcin 2041277b19d0SLisandro Dalcin #undef __FUNCT__ 2042f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp" 2043f6a906c0SBarry Smith /*@ 2044f6a906c0SBarry Smith TSAdjointSetUp - Sets up the internal data structures for the later use 2045f6a906c0SBarry Smith of an adjoint solver 2046f6a906c0SBarry Smith 2047f6a906c0SBarry Smith Collective on TS 2048f6a906c0SBarry Smith 2049f6a906c0SBarry Smith Input Parameter: 2050f6a906c0SBarry Smith . ts - the TS context obtained from TSCreate() 2051f6a906c0SBarry Smith 2052f6a906c0SBarry Smith Level: advanced 2053f6a906c0SBarry Smith 2054f6a906c0SBarry Smith .keywords: TS, timestep, setup 2055f6a906c0SBarry Smith 2056947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients() 2057f6a906c0SBarry Smith @*/ 2058f6a906c0SBarry Smith PetscErrorCode TSAdjointSetUp(TS ts) 2059f6a906c0SBarry Smith { 2060f6a906c0SBarry Smith PetscErrorCode ierr; 2061f6a906c0SBarry Smith 2062f6a906c0SBarry Smith PetscFunctionBegin; 2063f6a906c0SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2064f6a906c0SBarry Smith if (ts->adjointsetupcalled) PetscFunctionReturn(0); 2065dfb21088SHong Zhang if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first"); 2066d8151eeaSBarry Smith 2067947abb85SHong Zhang if (ts->vec_costintegral) { /* if there is integral in the cost function*/ 2068d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2069d8151eeaSBarry Smith if (ts->vecs_sensip){ 2070d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2071d8151eeaSBarry Smith } 2072947abb85SHong Zhang } 2073d8151eeaSBarry Smith 207442f2b339SBarry Smith if (ts->ops->adjointsetup) { 207542f2b339SBarry Smith ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr); 2076f6a906c0SBarry Smith } 2077f6a906c0SBarry Smith ts->adjointsetupcalled = PETSC_TRUE; 2078f6a906c0SBarry Smith PetscFunctionReturn(0); 2079f6a906c0SBarry Smith } 2080f6a906c0SBarry Smith 2081f6a906c0SBarry Smith #undef __FUNCT__ 2082277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 2083277b19d0SLisandro Dalcin /*@ 2084277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2085277b19d0SLisandro Dalcin 2086277b19d0SLisandro Dalcin Collective on TS 2087277b19d0SLisandro Dalcin 2088277b19d0SLisandro Dalcin Input Parameter: 2089277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2090277b19d0SLisandro Dalcin 2091277b19d0SLisandro Dalcin Level: beginner 2092277b19d0SLisandro Dalcin 2093277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 2094277b19d0SLisandro Dalcin 2095277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2096277b19d0SLisandro Dalcin @*/ 2097277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2098277b19d0SLisandro Dalcin { 2099277b19d0SLisandro Dalcin PetscErrorCode ierr; 2100277b19d0SLisandro Dalcin 2101277b19d0SLisandro Dalcin PetscFunctionBegin; 2102277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2103b18ea86cSHong Zhang 2104277b19d0SLisandro Dalcin if (ts->ops->reset) { 2105277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2106277b19d0SLisandro Dalcin } 2107277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2108e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2109bbd56ea5SKarl Rupp 21104e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 21114e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2112214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 21136bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2114e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2115e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 211638637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2117bbd56ea5SKarl Rupp 2118947abb85SHong Zhang if (ts->vec_costintegral) { 2119d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2120d8151eeaSBarry Smith if (ts->vecs_drdp){ 2121d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2122d8151eeaSBarry Smith } 2123947abb85SHong Zhang } 2124d8151eeaSBarry Smith ts->vecs_sensi = NULL; 2125d8151eeaSBarry Smith ts->vecs_sensip = NULL; 2126ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 21272c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 212836eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2129277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2130d763cef2SBarry Smith PetscFunctionReturn(0); 2131d763cef2SBarry Smith } 2132d763cef2SBarry Smith 21334a2ae208SSatish Balay #undef __FUNCT__ 21344a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 2135d8e5e3e6SSatish Balay /*@ 2136d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2137d763cef2SBarry Smith with TSCreate(). 2138d763cef2SBarry Smith 2139d763cef2SBarry Smith Collective on TS 2140d763cef2SBarry Smith 2141d763cef2SBarry Smith Input Parameter: 2142d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2143d763cef2SBarry Smith 2144d763cef2SBarry Smith Level: beginner 2145d763cef2SBarry Smith 2146d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2147d763cef2SBarry Smith 2148d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2149d763cef2SBarry Smith @*/ 21506bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2151d763cef2SBarry Smith { 21526849ba73SBarry Smith PetscErrorCode ierr; 2153d763cef2SBarry Smith 2154d763cef2SBarry Smith PetscFunctionBegin; 21556bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 21566bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 21576bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2158d763cef2SBarry Smith 21596bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2160277b19d0SLisandro Dalcin 2161e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2162e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 21636bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 21646d4c513bSLisandro Dalcin 2165bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2166bc952696SBarry Smith 216784df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 2168aeb4809dSShri Abhyankar if ((*ts)->event) { 2169aeb4809dSShri Abhyankar ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr); 2170aeb4809dSShri Abhyankar } 21716bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 21726bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 21736bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 21740dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 21756d4c513bSLisandro Dalcin 2176a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2177d763cef2SBarry Smith PetscFunctionReturn(0); 2178d763cef2SBarry Smith } 2179d763cef2SBarry Smith 21804a2ae208SSatish Balay #undef __FUNCT__ 21814a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 2182d8e5e3e6SSatish Balay /*@ 2183d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2184d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2185d763cef2SBarry Smith 2186d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2187d763cef2SBarry Smith 2188d763cef2SBarry Smith Input Parameter: 2189d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2190d763cef2SBarry Smith 2191d763cef2SBarry Smith Output Parameter: 2192d763cef2SBarry Smith . snes - the nonlinear solver context 2193d763cef2SBarry Smith 2194d763cef2SBarry Smith Notes: 2195d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2196d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 219794b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2198d763cef2SBarry Smith 2199d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 22000298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2201d763cef2SBarry Smith 2202d763cef2SBarry Smith Level: beginner 2203d763cef2SBarry Smith 2204d763cef2SBarry Smith .keywords: timestep, get, SNES 2205d763cef2SBarry Smith @*/ 22067087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2207d763cef2SBarry Smith { 2208d372ba47SLisandro Dalcin PetscErrorCode ierr; 2209d372ba47SLisandro Dalcin 2210d763cef2SBarry Smith PetscFunctionBegin; 22110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22124482741eSBarry Smith PetscValidPointer(snes,2); 2213d372ba47SLisandro Dalcin if (!ts->snes) { 2214ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 22150298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 22163bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2217d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2218496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 22199e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 22209e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 22219e2a6581SJed Brown } 2222d372ba47SLisandro Dalcin } 2223d763cef2SBarry Smith *snes = ts->snes; 2224d763cef2SBarry Smith PetscFunctionReturn(0); 2225d763cef2SBarry Smith } 2226d763cef2SBarry Smith 22274a2ae208SSatish Balay #undef __FUNCT__ 2228deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES" 2229deb2cd25SJed Brown /*@ 2230deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2231deb2cd25SJed Brown 2232deb2cd25SJed Brown Collective 2233deb2cd25SJed Brown 2234deb2cd25SJed Brown Input Parameter: 2235deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2236deb2cd25SJed Brown - snes - the nonlinear solver context 2237deb2cd25SJed Brown 2238deb2cd25SJed Brown Notes: 2239deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2240deb2cd25SJed Brown 2241deb2cd25SJed Brown Level: developer 2242deb2cd25SJed Brown 2243deb2cd25SJed Brown .keywords: timestep, set, SNES 2244deb2cd25SJed Brown @*/ 2245deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2246deb2cd25SJed Brown { 2247deb2cd25SJed Brown PetscErrorCode ierr; 2248d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2249deb2cd25SJed Brown 2250deb2cd25SJed Brown PetscFunctionBegin; 2251deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2252deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2253deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2254deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2255bbd56ea5SKarl Rupp 2256deb2cd25SJed Brown ts->snes = snes; 2257bbd56ea5SKarl Rupp 22580298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 22590298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2260740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 22610298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2262740132f1SEmil Constantinescu } 2263deb2cd25SJed Brown PetscFunctionReturn(0); 2264deb2cd25SJed Brown } 2265deb2cd25SJed Brown 2266deb2cd25SJed Brown #undef __FUNCT__ 226794b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 2268d8e5e3e6SSatish Balay /*@ 226994b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2270d763cef2SBarry Smith a TS (timestepper) context. 2271d763cef2SBarry Smith 227294b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2273d763cef2SBarry Smith 2274d763cef2SBarry Smith Input Parameter: 2275d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2276d763cef2SBarry Smith 2277d763cef2SBarry Smith Output Parameter: 227894b7f48cSBarry Smith . ksp - the nonlinear solver context 2279d763cef2SBarry Smith 2280d763cef2SBarry Smith Notes: 228194b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2282d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2283d763cef2SBarry Smith KSP and PC contexts as well. 2284d763cef2SBarry Smith 228594b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 22860298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2287d763cef2SBarry Smith 2288d763cef2SBarry Smith Level: beginner 2289d763cef2SBarry Smith 229094b7f48cSBarry Smith .keywords: timestep, get, KSP 2291d763cef2SBarry Smith @*/ 22927087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2293d763cef2SBarry Smith { 2294d372ba47SLisandro Dalcin PetscErrorCode ierr; 2295089b2837SJed Brown SNES snes; 2296d372ba47SLisandro Dalcin 2297d763cef2SBarry Smith PetscFunctionBegin; 22980700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22994482741eSBarry Smith PetscValidPointer(ksp,2); 230017186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2301e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2302089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2303089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2304d763cef2SBarry Smith PetscFunctionReturn(0); 2305d763cef2SBarry Smith } 2306d763cef2SBarry Smith 2307d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2308d763cef2SBarry Smith 23094a2ae208SSatish Balay #undef __FUNCT__ 2310adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 2311adb62b0dSMatthew Knepley /*@ 2312adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2313adb62b0dSMatthew Knepley maximum time for iteration. 2314adb62b0dSMatthew Knepley 23153f9fe445SBarry Smith Not Collective 2316adb62b0dSMatthew Knepley 2317adb62b0dSMatthew Knepley Input Parameters: 2318adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 23190298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 23200298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2321adb62b0dSMatthew Knepley 2322adb62b0dSMatthew Knepley Level: intermediate 2323adb62b0dSMatthew Knepley 2324adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2325adb62b0dSMatthew Knepley @*/ 23267087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2327adb62b0dSMatthew Knepley { 2328adb62b0dSMatthew Knepley PetscFunctionBegin; 23290700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2330abc0a331SBarry Smith if (maxsteps) { 23314482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2332adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2333adb62b0dSMatthew Knepley } 2334abc0a331SBarry Smith if (maxtime) { 23354482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2336adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2337adb62b0dSMatthew Knepley } 2338adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2339adb62b0dSMatthew Knepley } 2340adb62b0dSMatthew Knepley 2341adb62b0dSMatthew Knepley #undef __FUNCT__ 23424a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 2343d763cef2SBarry Smith /*@ 2344d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2345d763cef2SBarry Smith maximum time for iteration. 2346d763cef2SBarry Smith 23473f9fe445SBarry Smith Logically Collective on TS 2348d763cef2SBarry Smith 2349d763cef2SBarry Smith Input Parameters: 2350d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2351d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2352d763cef2SBarry Smith - maxtime - final time to iterate to 2353d763cef2SBarry Smith 2354d763cef2SBarry Smith Options Database Keys: 2355d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 23563bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2357d763cef2SBarry Smith 2358d763cef2SBarry Smith Notes: 2359d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2360d763cef2SBarry Smith 2361d763cef2SBarry Smith Level: intermediate 2362d763cef2SBarry Smith 2363d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2364a43b19c4SJed Brown 2365a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2366d763cef2SBarry Smith @*/ 23677087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2368d763cef2SBarry Smith { 2369d763cef2SBarry Smith PetscFunctionBegin; 23700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2371c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2372c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 237339b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 237439b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2375d763cef2SBarry Smith PetscFunctionReturn(0); 2376d763cef2SBarry Smith } 2377d763cef2SBarry Smith 23784a2ae208SSatish Balay #undef __FUNCT__ 23794a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 2380d763cef2SBarry Smith /*@ 2381d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2382d763cef2SBarry Smith for use by the TS routines. 2383d763cef2SBarry Smith 23843f9fe445SBarry Smith Logically Collective on TS and Vec 2385d763cef2SBarry Smith 2386d763cef2SBarry Smith Input Parameters: 2387d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 23880910c330SBarry Smith - u - the solution vector 2389d763cef2SBarry Smith 2390d763cef2SBarry Smith Level: beginner 2391d763cef2SBarry Smith 2392d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2393d763cef2SBarry Smith @*/ 23940910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2395d763cef2SBarry Smith { 23968737fe31SLisandro Dalcin PetscErrorCode ierr; 2397496e6a7aSJed Brown DM dm; 23988737fe31SLisandro Dalcin 2399d763cef2SBarry Smith PetscFunctionBegin; 24000700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 24010910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 24020910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 24036bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2404bbd56ea5SKarl Rupp 24050910c330SBarry Smith ts->vec_sol = u; 2406bbd56ea5SKarl Rupp 2407496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 24080910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2409d763cef2SBarry Smith PetscFunctionReturn(0); 2410d763cef2SBarry Smith } 2411d763cef2SBarry Smith 2412e74ef692SMatthew Knepley #undef __FUNCT__ 241373b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps" 241473b18844SBarry Smith /*@ 241573b18844SBarry Smith TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time 241673b18844SBarry Smith 241773b18844SBarry Smith Logically Collective on TS 241873b18844SBarry Smith 241973b18844SBarry Smith Input Parameters: 242073b18844SBarry Smith + ts - the TS context obtained from TSCreate() 242173b18844SBarry Smith . steps - number of steps to use 242273b18844SBarry Smith 242373b18844SBarry Smith Level: intermediate 242473b18844SBarry Smith 242573b18844SBarry Smith Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this 242673b18844SBarry Smith so as to integrate back to less than the original timestep 242773b18844SBarry Smith 242873b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations 242973b18844SBarry Smith 243073b18844SBarry Smith .seealso: TSSetExactFinalTime() 243173b18844SBarry Smith @*/ 243273b18844SBarry Smith PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps) 243373b18844SBarry Smith { 243473b18844SBarry Smith PetscFunctionBegin; 243573b18844SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 243673b18844SBarry Smith PetscValidLogicalCollectiveInt(ts,steps,2); 243773b18844SBarry Smith if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps"); 243873b18844SBarry 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"); 243973b18844SBarry Smith ts->adjoint_max_steps = steps; 244073b18844SBarry Smith PetscFunctionReturn(0); 244173b18844SBarry Smith } 244273b18844SBarry Smith 244373b18844SBarry Smith #undef __FUNCT__ 2444dfb21088SHong Zhang #define __FUNCT__ "TSSetCostGradients" 2445c235aa8dSHong Zhang /*@ 2446dfb21088SHong 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 24470cf82b59SBarry Smith for use by the TSAdjoint routines. 2448c235aa8dSHong Zhang 2449c235aa8dSHong Zhang Logically Collective on TS and Vec 2450c235aa8dSHong Zhang 2451c235aa8dSHong Zhang Input Parameters: 2452c235aa8dSHong Zhang + ts - the TS context obtained from TSCreate() 2453abc2977eSBarry 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 2454abc2977eSBarry Smith - mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters 2455c235aa8dSHong Zhang 2456c235aa8dSHong Zhang Level: beginner 2457c235aa8dSHong Zhang 2458abc2977eSBarry Smith Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime mu_i = df/dp|finaltime 24590cf82b59SBarry Smith 2460c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions 2461c235aa8dSHong Zhang @*/ 2462dfb21088SHong Zhang PetscErrorCode TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu) 2463c235aa8dSHong Zhang { 2464c235aa8dSHong Zhang PetscFunctionBegin; 2465c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2466abc2977eSBarry Smith PetscValidPointer(lambda,2); 2467abc2977eSBarry Smith ts->vecs_sensi = lambda; 2468abc2977eSBarry Smith ts->vecs_sensip = mu; 2469dfb21088SHong 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"); 2470abc2977eSBarry Smith ts->numcost = numcost; 2471ad8e2604SHong Zhang PetscFunctionReturn(0); 2472ad8e2604SHong Zhang } 2473ad8e2604SHong Zhang 2474ad8e2604SHong Zhang #undef __FUNCT__ 24755bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian" 2476ad8e2604SHong Zhang /*@C 24770cf82b59SBarry 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. 2478ad8e2604SHong Zhang 2479ad8e2604SHong Zhang Logically Collective on TS 2480ad8e2604SHong Zhang 2481ad8e2604SHong Zhang Input Parameters: 2482ad8e2604SHong Zhang + ts - The TS context obtained from TSCreate() 2483ad8e2604SHong Zhang - func - The function 2484ad8e2604SHong Zhang 2485ad8e2604SHong Zhang Calling sequence of func: 24860cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx); 248705755b9cSHong Zhang + t - current timestep 24880cf82b59SBarry Smith . y - input vector (current ODE solution) 248905755b9cSHong Zhang . A - output matrix 249005755b9cSHong Zhang - ctx - [optional] user-defined function context 2491ad8e2604SHong Zhang 2492ad8e2604SHong Zhang Level: intermediate 2493ad8e2604SHong Zhang 24940cf82b59SBarry 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 24950cf82b59SBarry Smith 2496ad8e2604SHong Zhang .keywords: TS, sensitivity 2497ad8e2604SHong Zhang .seealso: 2498ad8e2604SHong Zhang @*/ 24995bf8c567SBarry Smith PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 2500ad8e2604SHong Zhang { 2501ad8e2604SHong Zhang PetscErrorCode ierr; 2502ad8e2604SHong Zhang 2503ad8e2604SHong Zhang PetscFunctionBegin; 2504ad8e2604SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2505ad8e2604SHong Zhang if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 2506ad8e2604SHong Zhang 2507ad8e2604SHong Zhang ts->rhsjacobianp = func; 2508ad8e2604SHong Zhang ts->rhsjacobianpctx = ctx; 2509ad8e2604SHong Zhang if(Amat) { 2510ad8e2604SHong Zhang ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 2511ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2512ad8e2604SHong Zhang ts->Jacp = Amat; 2513ad8e2604SHong Zhang } 2514ad8e2604SHong Zhang PetscFunctionReturn(0); 2515ad8e2604SHong Zhang } 2516ad8e2604SHong Zhang 2517ad8e2604SHong Zhang #undef __FUNCT__ 25185bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian" 25190cf82b59SBarry Smith /*@C 25205bf8c567SBarry Smith TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function. 2521ad8e2604SHong Zhang 2522ad8e2604SHong Zhang Collective on TS 2523ad8e2604SHong Zhang 2524ad8e2604SHong Zhang Input Parameters: 2525ad8e2604SHong Zhang . ts - The TS context obtained from TSCreate() 2526ad8e2604SHong Zhang 2527ad8e2604SHong Zhang Level: developer 2528ad8e2604SHong Zhang 2529ad8e2604SHong Zhang .keywords: TS, sensitivity 25305bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian() 2531ad8e2604SHong Zhang @*/ 25325bf8c567SBarry Smith PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat) 2533ad8e2604SHong Zhang { 2534ad8e2604SHong Zhang PetscErrorCode ierr; 2535ad8e2604SHong Zhang 2536ad8e2604SHong Zhang PetscFunctionBegin; 2537ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2538ad8e2604SHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 2539ad8e2604SHong Zhang PetscValidPointer(Amat,4); 2540ad8e2604SHong Zhang 2541ad8e2604SHong Zhang PetscStackPush("TS user JacobianP function for sensitivity analysis"); 2542ad8e2604SHong Zhang ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 2543ad8e2604SHong Zhang PetscStackPop; 2544c235aa8dSHong Zhang PetscFunctionReturn(0); 2545c235aa8dSHong Zhang } 2546c235aa8dSHong Zhang 2547c235aa8dSHong Zhang #undef __FUNCT__ 2548dfb21088SHong Zhang #define __FUNCT__ "TSSetCostIntegrand" 25496fd0887fSHong Zhang /*@C 2550dfb21088SHong Zhang TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions 25516fd0887fSHong Zhang 25526fd0887fSHong Zhang Logically Collective on TS 25536fd0887fSHong Zhang 25546fd0887fSHong Zhang Input Parameters: 25556fd0887fSHong Zhang + ts - the TS context obtained from TSCreate() 2556abc2977eSBarry Smith . numcost - number of gradients to be computed, this is the number of cost functions 25570cf82b59SBarry Smith . rf - routine for evaluating the integrand function 2558b612ec54SBarry Smith . drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y 2559b612ec54SBarry Smith . drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p 2560b1cb36f3SHong Zhang . fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run 2561b612ec54SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 25626fd0887fSHong Zhang 25630cf82b59SBarry Smith Calling sequence of rf: 25640cf82b59SBarry Smith $ rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx); 25656fd0887fSHong Zhang 25666fd0887fSHong Zhang + t - current timestep 25670cf82b59SBarry Smith . y - input vector 25680cf82b59SBarry Smith . f - function result; one vector entry for each cost function 25696fd0887fSHong Zhang - ctx - [optional] user-defined function context 25706fd0887fSHong Zhang 2571b612ec54SBarry Smith Calling sequence of drdyf: 25720cf82b59SBarry Smith $ PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx); 2573b612ec54SBarry Smith 2574b612ec54SBarry Smith Calling sequence of drdpf: 25750cf82b59SBarry Smith $ PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx); 2576b612ec54SBarry Smith 2577b612ec54SBarry Smith Level: intermediate 25786fd0887fSHong Zhang 2579abc2977eSBarry Smith Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions 25800cf82b59SBarry Smith 25816fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 25826fd0887fSHong Zhang 2583dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients() 25846fd0887fSHong Zhang @*/ 2585dfb21088SHong Zhang PetscErrorCode TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*), 2586d8151eeaSBarry Smith PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*), 2587b1cb36f3SHong Zhang PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*), 2588b1cb36f3SHong Zhang PetscBool fwd,void *ctx) 25896fd0887fSHong Zhang { 25906fd0887fSHong Zhang PetscErrorCode ierr; 25916fd0887fSHong Zhang 25926fd0887fSHong Zhang PetscFunctionBegin; 25936fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2594dfb21088SHong 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()"); 2595c72ed514SHong Zhang if (!ts->numcost) ts->numcost=numcost; 25966fd0887fSHong Zhang 2597b1cb36f3SHong Zhang ts->costintegralfwd = fwd; /* Evaluate the cost integral in forward run if fwd is true */ 2598abc2977eSBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr); 25992c39e106SBarry Smith ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr); 26000cf82b59SBarry Smith ts->costintegrand = rf; 260105755b9cSHong Zhang ts->costintegrandctx = ctx; 2602b612ec54SBarry Smith ts->drdyfunction = drdyf; 2603b612ec54SBarry Smith ts->drdpfunction = drdpf; 26046fd0887fSHong Zhang PetscFunctionReturn(0); 26056fd0887fSHong Zhang } 26066fd0887fSHong Zhang 26076fd0887fSHong Zhang #undef __FUNCT__ 2608dfb21088SHong Zhang #define __FUNCT__ "TSGetCostIntegral" 260936eaed60SHong Zhang /*@ 2610dfb21088SHong Zhang TSGetCostIntegral - Returns the values of the integral term in the cost functions. 261136eaed60SHong Zhang It is valid to call the routine after a backward run. 261236eaed60SHong Zhang 261336eaed60SHong Zhang Not Collective 261436eaed60SHong Zhang 261536eaed60SHong Zhang Input Parameter: 261636eaed60SHong Zhang . ts - the TS context obtained from TSCreate() 261736eaed60SHong Zhang 261836eaed60SHong Zhang Output Parameter: 26192c39e106SBarry Smith . v - the vector containing the integrals for each cost function 262036eaed60SHong Zhang 262136eaed60SHong Zhang Level: intermediate 262236eaed60SHong Zhang 2623dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 262436eaed60SHong Zhang 262536eaed60SHong Zhang .keywords: TS, sensitivity analysis 262636eaed60SHong Zhang @*/ 2627dfb21088SHong Zhang PetscErrorCode TSGetCostIntegral(TS ts,Vec *v) 262836eaed60SHong Zhang { 262936eaed60SHong Zhang PetscFunctionBegin; 263036eaed60SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 263136eaed60SHong Zhang PetscValidPointer(v,2); 26322c39e106SBarry Smith *v = ts->vec_costintegral; 263336eaed60SHong Zhang PetscFunctionReturn(0); 263436eaed60SHong Zhang } 263536eaed60SHong Zhang 263636eaed60SHong Zhang #undef __FUNCT__ 2637d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand" 26386fd0887fSHong Zhang /*@ 26392c39e106SBarry Smith TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions. 26406fd0887fSHong Zhang 26416fd0887fSHong Zhang Input Parameters: 26426fd0887fSHong Zhang + ts - the TS context 26436fd0887fSHong Zhang . t - current time 26440cf82b59SBarry Smith - y - state vector, i.e. current solution 26456fd0887fSHong Zhang 26466fd0887fSHong Zhang Output Parameter: 2647abc2977eSBarry Smith . q - vector of size numcost to hold the outputs 26486fd0887fSHong Zhang 26496fd0887fSHong Zhang Note: 26506fd0887fSHong Zhang Most users should not need to explicitly call this routine, as it 26516fd0887fSHong Zhang is used internally within the sensitivity analysis context. 26526fd0887fSHong Zhang 26536fd0887fSHong Zhang Level: developer 26546fd0887fSHong Zhang 26556fd0887fSHong Zhang .keywords: TS, compute 26566fd0887fSHong Zhang 2657dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 26586fd0887fSHong Zhang @*/ 26590cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q) 26606fd0887fSHong Zhang { 26616fd0887fSHong Zhang PetscErrorCode ierr; 26626fd0887fSHong Zhang 26636fd0887fSHong Zhang PetscFunctionBegin; 26646fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26650cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 26666fd0887fSHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,4); 26676fd0887fSHong Zhang 26680cf82b59SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 2669e4680132SHong Zhang if (ts->costintegrand) { 2670e4680132SHong Zhang PetscStackPush("TS user integrand in the cost function"); 26710cf82b59SBarry Smith ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr); 26726fd0887fSHong Zhang PetscStackPop; 26736fd0887fSHong Zhang } else { 26746fd0887fSHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 26756fd0887fSHong Zhang } 26766fd0887fSHong Zhang 26770cf82b59SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 26786fd0887fSHong Zhang PetscFunctionReturn(0); 26796fd0887fSHong Zhang } 26806fd0887fSHong Zhang 26816fd0887fSHong Zhang #undef __FUNCT__ 2682d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction" 268305755b9cSHong Zhang /*@ 2684d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 268505755b9cSHong Zhang 268605755b9cSHong Zhang Collective on TS 268705755b9cSHong Zhang 268805755b9cSHong Zhang Input Parameters: 268905755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 269005755b9cSHong Zhang 269105755b9cSHong Zhang Notes: 2692d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 269305755b9cSHong Zhang so most users would not generally call this routine themselves. 269405755b9cSHong Zhang 269505755b9cSHong Zhang Level: developer 269605755b9cSHong Zhang 269705755b9cSHong Zhang .keywords: TS, sensitivity 2698d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction() 269905755b9cSHong Zhang @*/ 27000cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy) 270105755b9cSHong Zhang { 270205755b9cSHong Zhang PetscErrorCode ierr; 270305755b9cSHong Zhang 270405755b9cSHong Zhang PetscFunctionBegin; 270505755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27060cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 270705755b9cSHong Zhang 270805755b9cSHong Zhang PetscStackPush("TS user DRDY function for sensitivity analysis"); 27090cf82b59SBarry Smith ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr); 271005755b9cSHong Zhang PetscStackPop; 271105755b9cSHong Zhang PetscFunctionReturn(0); 271205755b9cSHong Zhang } 271305755b9cSHong Zhang 271405755b9cSHong Zhang #undef __FUNCT__ 2715d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction" 271605755b9cSHong Zhang /*@ 2717d4aa0a58SBarry Smith TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 271805755b9cSHong Zhang 271905755b9cSHong Zhang Collective on TS 272005755b9cSHong Zhang 272105755b9cSHong Zhang Input Parameters: 272205755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 272305755b9cSHong Zhang 272405755b9cSHong Zhang Notes: 272505755b9cSHong Zhang TSDRDPFunction() is typically used for sensitivity implementation, 272605755b9cSHong Zhang so most users would not generally call this routine themselves. 272705755b9cSHong Zhang 272805755b9cSHong Zhang Level: developer 272905755b9cSHong Zhang 273005755b9cSHong Zhang .keywords: TS, sensitivity 2731d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction() 273205755b9cSHong Zhang @*/ 27330cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp) 273405755b9cSHong Zhang { 273505755b9cSHong Zhang PetscErrorCode ierr; 273605755b9cSHong Zhang 273705755b9cSHong Zhang PetscFunctionBegin; 273805755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27390cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 274005755b9cSHong Zhang 274105755b9cSHong Zhang PetscStackPush("TS user DRDP function for sensitivity analysis"); 27420cf82b59SBarry Smith ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr); 274305755b9cSHong Zhang PetscStackPop; 274405755b9cSHong Zhang PetscFunctionReturn(0); 274505755b9cSHong Zhang } 274605755b9cSHong Zhang 274705755b9cSHong Zhang #undef __FUNCT__ 2748e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 2749ac226902SBarry Smith /*@C 2750000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 27513f2090d5SJed Brown called once at the beginning of each time step. 2752000e7ae3SMatthew Knepley 27533f9fe445SBarry Smith Logically Collective on TS 2754000e7ae3SMatthew Knepley 2755000e7ae3SMatthew Knepley Input Parameters: 2756000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2757000e7ae3SMatthew Knepley - func - The function 2758000e7ae3SMatthew Knepley 2759000e7ae3SMatthew Knepley Calling sequence of func: 2760000e7ae3SMatthew Knepley . func (TS ts); 2761000e7ae3SMatthew Knepley 2762000e7ae3SMatthew Knepley Level: intermediate 2763000e7ae3SMatthew Knepley 2764b8123daeSJed Brown Note: 2765b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 2766b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 2767b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 2768b8123daeSJed Brown 2769000e7ae3SMatthew Knepley .keywords: TS, timestep 27709be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 2771000e7ae3SMatthew Knepley @*/ 27727087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 2773000e7ae3SMatthew Knepley { 2774000e7ae3SMatthew Knepley PetscFunctionBegin; 27750700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2776ae60f76fSBarry Smith ts->prestep = func; 2777000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2778000e7ae3SMatthew Knepley } 2779000e7ae3SMatthew Knepley 2780e74ef692SMatthew Knepley #undef __FUNCT__ 27813f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 278209ee8438SJed Brown /*@ 27833f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 27843f2090d5SJed Brown 27853f2090d5SJed Brown Collective on TS 27863f2090d5SJed Brown 27873f2090d5SJed Brown Input Parameters: 27883f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 27893f2090d5SJed Brown 27903f2090d5SJed Brown Notes: 27913f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 27923f2090d5SJed Brown so most users would not generally call this routine themselves. 27933f2090d5SJed Brown 27943f2090d5SJed Brown Level: developer 27953f2090d5SJed Brown 27963f2090d5SJed Brown .keywords: TS, timestep 27979be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 27983f2090d5SJed Brown @*/ 27997087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 28003f2090d5SJed Brown { 28013f2090d5SJed Brown PetscErrorCode ierr; 28023f2090d5SJed Brown 28033f2090d5SJed Brown PetscFunctionBegin; 28040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2805ae60f76fSBarry Smith if (ts->prestep) { 2806ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 2807312ce896SJed Brown } 28083f2090d5SJed Brown PetscFunctionReturn(0); 28093f2090d5SJed Brown } 28103f2090d5SJed Brown 28113f2090d5SJed Brown #undef __FUNCT__ 2812b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 2813b8123daeSJed Brown /*@C 2814b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 2815b8123daeSJed Brown called once at the beginning of each stage. 2816b8123daeSJed Brown 2817b8123daeSJed Brown Logically Collective on TS 2818b8123daeSJed Brown 2819b8123daeSJed Brown Input Parameters: 2820b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 2821b8123daeSJed Brown - func - The function 2822b8123daeSJed Brown 2823b8123daeSJed Brown Calling sequence of func: 2824b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 2825b8123daeSJed Brown 2826b8123daeSJed Brown Level: intermediate 2827b8123daeSJed Brown 2828b8123daeSJed Brown Note: 2829b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 2830b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 2831b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 2832b8123daeSJed Brown 2833b8123daeSJed Brown .keywords: TS, timestep 28349be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 2835b8123daeSJed Brown @*/ 2836b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 2837b8123daeSJed Brown { 2838b8123daeSJed Brown PetscFunctionBegin; 2839b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2840ae60f76fSBarry Smith ts->prestage = func; 2841b8123daeSJed Brown PetscFunctionReturn(0); 2842b8123daeSJed Brown } 2843b8123daeSJed Brown 2844b8123daeSJed Brown #undef __FUNCT__ 28459be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage" 28469be3e283SDebojyoti Ghosh /*@C 28479be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 28489be3e283SDebojyoti Ghosh called once at the end of each stage. 28499be3e283SDebojyoti Ghosh 28509be3e283SDebojyoti Ghosh Logically Collective on TS 28519be3e283SDebojyoti Ghosh 28529be3e283SDebojyoti Ghosh Input Parameters: 28539be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 28549be3e283SDebojyoti Ghosh - func - The function 28559be3e283SDebojyoti Ghosh 28569be3e283SDebojyoti Ghosh Calling sequence of func: 28579be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 28589be3e283SDebojyoti Ghosh 28599be3e283SDebojyoti Ghosh Level: intermediate 28609be3e283SDebojyoti Ghosh 28619be3e283SDebojyoti Ghosh Note: 28629be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 28639be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 28649be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 28659be3e283SDebojyoti Ghosh 28669be3e283SDebojyoti Ghosh .keywords: TS, timestep 28679be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 28689be3e283SDebojyoti Ghosh @*/ 28699be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 28709be3e283SDebojyoti Ghosh { 28719be3e283SDebojyoti Ghosh PetscFunctionBegin; 28729be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 28739be3e283SDebojyoti Ghosh ts->poststage = func; 28749be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 28759be3e283SDebojyoti Ghosh } 28769be3e283SDebojyoti Ghosh 28779be3e283SDebojyoti Ghosh #undef __FUNCT__ 2878b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 2879b8123daeSJed Brown /*@ 2880b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 2881b8123daeSJed Brown 2882b8123daeSJed Brown Collective on TS 2883b8123daeSJed Brown 2884b8123daeSJed Brown Input Parameters: 2885b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 28869be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 2887b8123daeSJed Brown 2888b8123daeSJed Brown Notes: 2889b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 2890b8123daeSJed Brown most users would not generally call this routine themselves. 2891b8123daeSJed Brown 2892b8123daeSJed Brown Level: developer 2893b8123daeSJed Brown 2894b8123daeSJed Brown .keywords: TS, timestep 28959be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 2896b8123daeSJed Brown @*/ 2897b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 2898b8123daeSJed Brown { 2899b8123daeSJed Brown PetscErrorCode ierr; 2900b8123daeSJed Brown 2901b8123daeSJed Brown PetscFunctionBegin; 2902b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2903ae60f76fSBarry Smith if (ts->prestage) { 2904ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 2905b8123daeSJed Brown } 2906b8123daeSJed Brown PetscFunctionReturn(0); 2907b8123daeSJed Brown } 2908b8123daeSJed Brown 2909b8123daeSJed Brown #undef __FUNCT__ 29109be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage" 29119be3e283SDebojyoti Ghosh /*@ 29129be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 29139be3e283SDebojyoti Ghosh 29149be3e283SDebojyoti Ghosh Collective on TS 29159be3e283SDebojyoti Ghosh 29169be3e283SDebojyoti Ghosh Input Parameters: 29179be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 29189be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 29199be3e283SDebojyoti Ghosh stageindex - Stage number 29209be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 29219be3e283SDebojyoti Ghosh of stages) with the stage solutions 29229be3e283SDebojyoti Ghosh 29239be3e283SDebojyoti Ghosh Notes: 29249be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 29259be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 29269be3e283SDebojyoti Ghosh 29279be3e283SDebojyoti Ghosh Level: developer 29289be3e283SDebojyoti Ghosh 29299be3e283SDebojyoti Ghosh .keywords: TS, timestep 29309be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 29319be3e283SDebojyoti Ghosh @*/ 29329be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 29339be3e283SDebojyoti Ghosh { 29349be3e283SDebojyoti Ghosh PetscErrorCode ierr; 29359be3e283SDebojyoti Ghosh 29369be3e283SDebojyoti Ghosh PetscFunctionBegin; 29379be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 29384beae5d8SLisandro Dalcin if (ts->poststage) { 29399be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 29409be3e283SDebojyoti Ghosh } 29419be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 29429be3e283SDebojyoti Ghosh } 29439be3e283SDebojyoti Ghosh 29449be3e283SDebojyoti Ghosh #undef __FUNCT__ 2945e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 2946ac226902SBarry Smith /*@C 2947000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 29483f2090d5SJed Brown called once at the end of each time step. 2949000e7ae3SMatthew Knepley 29503f9fe445SBarry Smith Logically Collective on TS 2951000e7ae3SMatthew Knepley 2952000e7ae3SMatthew Knepley Input Parameters: 2953000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2954000e7ae3SMatthew Knepley - func - The function 2955000e7ae3SMatthew Knepley 2956000e7ae3SMatthew Knepley Calling sequence of func: 2957b8123daeSJed Brown $ func (TS ts); 2958000e7ae3SMatthew Knepley 2959000e7ae3SMatthew Knepley Level: intermediate 2960000e7ae3SMatthew Knepley 2961000e7ae3SMatthew Knepley .keywords: TS, timestep 2962b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 2963000e7ae3SMatthew Knepley @*/ 29647087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 2965000e7ae3SMatthew Knepley { 2966000e7ae3SMatthew Knepley PetscFunctionBegin; 29670700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2968ae60f76fSBarry Smith ts->poststep = func; 2969000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2970000e7ae3SMatthew Knepley } 2971000e7ae3SMatthew Knepley 2972e74ef692SMatthew Knepley #undef __FUNCT__ 29733f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 297409ee8438SJed Brown /*@ 29753f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 29763f2090d5SJed Brown 29773f2090d5SJed Brown Collective on TS 29783f2090d5SJed Brown 29793f2090d5SJed Brown Input Parameters: 29803f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 29813f2090d5SJed Brown 29823f2090d5SJed Brown Notes: 29833f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 29843f2090d5SJed Brown so most users would not generally call this routine themselves. 29853f2090d5SJed Brown 29863f2090d5SJed Brown Level: developer 29873f2090d5SJed Brown 29883f2090d5SJed Brown .keywords: TS, timestep 29893f2090d5SJed Brown @*/ 29907087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 29913f2090d5SJed Brown { 29923f2090d5SJed Brown PetscErrorCode ierr; 29933f2090d5SJed Brown 29943f2090d5SJed Brown PetscFunctionBegin; 29950700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2996ae60f76fSBarry Smith if (ts->poststep) { 2997ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 299872ac3e02SJed Brown } 29993f2090d5SJed Brown PetscFunctionReturn(0); 30003f2090d5SJed Brown } 30013f2090d5SJed Brown 3002d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 3003d763cef2SBarry Smith 30044a2ae208SSatish Balay #undef __FUNCT__ 3005a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 3006d763cef2SBarry Smith /*@C 3007a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 3008d763cef2SBarry Smith timestep to display the iteration's progress. 3009d763cef2SBarry Smith 30103f9fe445SBarry Smith Logically Collective on TS 3011d763cef2SBarry Smith 3012d763cef2SBarry Smith Input Parameters: 3013d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3014e213d8f1SJed Brown . monitor - monitoring routine 3015329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 30160298fd71SBarry Smith monitor routine (use NULL if no context is desired) 3017b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 30180298fd71SBarry Smith (may be NULL) 3019d763cef2SBarry Smith 3020e213d8f1SJed Brown Calling sequence of monitor: 30210910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 3022d763cef2SBarry Smith 3023d763cef2SBarry Smith + ts - the TS context 302488c05cc5SBarry 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 302588c05cc5SBarry Smith been interpolated to) 30261f06c33eSBarry Smith . time - current time 30270910c330SBarry Smith . u - current iterate 3028d763cef2SBarry Smith - mctx - [optional] monitoring context 3029d763cef2SBarry Smith 3030d763cef2SBarry Smith Notes: 3031d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 3032d763cef2SBarry Smith already has been loaded. 3033d763cef2SBarry Smith 3034025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 3035025f1a04SBarry Smith 3036d763cef2SBarry Smith Level: intermediate 3037d763cef2SBarry Smith 3038d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3039d763cef2SBarry Smith 3040a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 3041d763cef2SBarry Smith @*/ 3042c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 3043d763cef2SBarry Smith { 3044d763cef2SBarry Smith PetscFunctionBegin; 30450700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 304617186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 3047d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 30488704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 3049d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 3050d763cef2SBarry Smith PetscFunctionReturn(0); 3051d763cef2SBarry Smith } 3052d763cef2SBarry Smith 30534a2ae208SSatish Balay #undef __FUNCT__ 3054a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 3055d763cef2SBarry Smith /*@C 3056a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 3057d763cef2SBarry Smith 30583f9fe445SBarry Smith Logically Collective on TS 3059d763cef2SBarry Smith 3060d763cef2SBarry Smith Input Parameters: 3061d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3062d763cef2SBarry Smith 3063d763cef2SBarry Smith Notes: 3064d763cef2SBarry Smith There is no way to remove a single, specific monitor. 3065d763cef2SBarry Smith 3066d763cef2SBarry Smith Level: intermediate 3067d763cef2SBarry Smith 3068d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3069d763cef2SBarry Smith 3070a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 3071d763cef2SBarry Smith @*/ 30727087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 3073d763cef2SBarry Smith { 3074d952e501SBarry Smith PetscErrorCode ierr; 3075d952e501SBarry Smith PetscInt i; 3076d952e501SBarry Smith 3077d763cef2SBarry Smith PetscFunctionBegin; 30780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3079d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 30808704b422SBarry Smith if (ts->monitordestroy[i]) { 30818704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 3082d952e501SBarry Smith } 3083d952e501SBarry Smith } 3084d763cef2SBarry Smith ts->numbermonitors = 0; 3085d763cef2SBarry Smith PetscFunctionReturn(0); 3086d763cef2SBarry Smith } 3087d763cef2SBarry Smith 30884a2ae208SSatish Balay #undef __FUNCT__ 3089a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 3090d8e5e3e6SSatish Balay /*@ 3091a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 30925516499fSSatish Balay 30935516499fSSatish Balay Level: intermediate 309441251cbbSSatish Balay 30955516499fSSatish Balay .keywords: TS, set, monitor 30965516499fSSatish Balay 309741251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 309841251cbbSSatish Balay @*/ 3099649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 3100d763cef2SBarry Smith { 3101dfbe8321SBarry Smith PetscErrorCode ierr; 31024d4332d5SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 310341aca3d6SBarry Smith PetscBool iascii,ibinary; 3104d132466eSBarry Smith 3105d763cef2SBarry Smith PetscFunctionBegin; 31064d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 310741aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 310841aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr); 310941aca3d6SBarry Smith if (iascii) { 3110649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 31118392e04aSShri 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); 3112649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 311341aca3d6SBarry Smith } else if (ibinary) { 311441aca3d6SBarry Smith PetscMPIInt rank; 311541aca3d6SBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 311641aca3d6SBarry Smith if (!rank) { 311741aca3d6SBarry Smith ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr); 311841aca3d6SBarry Smith } else { 311941aca3d6SBarry Smith ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr); 312041aca3d6SBarry Smith } 312141aca3d6SBarry Smith } 3122d763cef2SBarry Smith PetscFunctionReturn(0); 3123d763cef2SBarry Smith } 3124d763cef2SBarry Smith 31254a2ae208SSatish Balay #undef __FUNCT__ 31269110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorSet" 31279110b2e7SHong Zhang /*@C 31289110b2e7SHong Zhang TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every 31299110b2e7SHong Zhang timestep to display the iteration's progress. 31309110b2e7SHong Zhang 31319110b2e7SHong Zhang Logically Collective on TS 31329110b2e7SHong Zhang 31339110b2e7SHong Zhang Input Parameters: 31349110b2e7SHong Zhang + ts - the TS context obtained from TSCreate() 31359110b2e7SHong Zhang . adjointmonitor - monitoring routine 31369110b2e7SHong Zhang . adjointmctx - [optional] user-defined context for private data for the 31379110b2e7SHong Zhang monitor routine (use NULL if no context is desired) 31389110b2e7SHong Zhang - adjointmonitordestroy - [optional] routine that frees monitor context 31399110b2e7SHong Zhang (may be NULL) 31409110b2e7SHong Zhang 31419110b2e7SHong Zhang Calling sequence of monitor: 31429110b2e7SHong Zhang $ int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx) 31439110b2e7SHong Zhang 31449110b2e7SHong Zhang + ts - the TS context 31459110b2e7SHong 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 31469110b2e7SHong Zhang been interpolated to) 31479110b2e7SHong Zhang . time - current time 31489110b2e7SHong Zhang . u - current iterate 31499110b2e7SHong Zhang . numcost - number of cost functionos 31509110b2e7SHong Zhang . lambda - sensitivities to initial conditions 31519110b2e7SHong Zhang . mu - sensitivities to parameters 31529110b2e7SHong Zhang - adjointmctx - [optional] adjoint monitoring context 31539110b2e7SHong Zhang 31549110b2e7SHong Zhang Notes: 31559110b2e7SHong Zhang This routine adds an additional monitor to the list of monitors that 31569110b2e7SHong Zhang already has been loaded. 31579110b2e7SHong Zhang 31589110b2e7SHong Zhang Fortran notes: Only a single monitor function can be set for each TS object 31599110b2e7SHong Zhang 31609110b2e7SHong Zhang Level: intermediate 31619110b2e7SHong Zhang 31629110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 31639110b2e7SHong Zhang 31649110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel() 31659110b2e7SHong Zhang @*/ 31669110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**)) 31679110b2e7SHong Zhang { 31689110b2e7SHong Zhang PetscFunctionBegin; 31699110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31709110b2e7SHong Zhang if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set"); 31719110b2e7SHong Zhang ts->adjointmonitor[ts->numberadjointmonitors] = adjointmonitor; 31729110b2e7SHong Zhang ts->adjointmonitordestroy[ts->numberadjointmonitors] = adjointmdestroy; 31739110b2e7SHong Zhang ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx; 31749110b2e7SHong Zhang PetscFunctionReturn(0); 31759110b2e7SHong Zhang } 31769110b2e7SHong Zhang 31779110b2e7SHong Zhang #undef __FUNCT__ 31789110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorCancel" 31799110b2e7SHong Zhang /*@C 31809110b2e7SHong Zhang TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object. 31819110b2e7SHong Zhang 31829110b2e7SHong Zhang Logically Collective on TS 31839110b2e7SHong Zhang 31849110b2e7SHong Zhang Input Parameters: 31859110b2e7SHong Zhang . ts - the TS context obtained from TSCreate() 31869110b2e7SHong Zhang 31879110b2e7SHong Zhang Notes: 31889110b2e7SHong Zhang There is no way to remove a single, specific monitor. 31899110b2e7SHong Zhang 31909110b2e7SHong Zhang Level: intermediate 31919110b2e7SHong Zhang 31929110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 31939110b2e7SHong Zhang 31949110b2e7SHong Zhang .seealso: TSAdjointMonitorSet() 31959110b2e7SHong Zhang @*/ 31969110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorCancel(TS ts) 31979110b2e7SHong Zhang { 31989110b2e7SHong Zhang PetscErrorCode ierr; 31999110b2e7SHong Zhang PetscInt i; 32009110b2e7SHong Zhang 32019110b2e7SHong Zhang PetscFunctionBegin; 32029110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 32039110b2e7SHong Zhang for (i=0; i<ts->numberadjointmonitors; i++) { 32049110b2e7SHong Zhang if (ts->adjointmonitordestroy[i]) { 32059110b2e7SHong Zhang ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 32069110b2e7SHong Zhang } 32079110b2e7SHong Zhang } 32089110b2e7SHong Zhang ts->numberadjointmonitors = 0; 32099110b2e7SHong Zhang PetscFunctionReturn(0); 32109110b2e7SHong Zhang } 32119110b2e7SHong Zhang 32129110b2e7SHong Zhang #undef __FUNCT__ 3213110eb670SHong Zhang #define __FUNCT__ "TSAdjointMonitorDefault" 3214110eb670SHong Zhang /*@ 3215110eb670SHong Zhang TSAdjointMonitorDefault - Sets the Default monitor 3216110eb670SHong Zhang 3217110eb670SHong Zhang Level: intermediate 3218110eb670SHong Zhang 3219110eb670SHong Zhang .keywords: TS, set, monitor 3220110eb670SHong Zhang 3221110eb670SHong Zhang .seealso: TSAdjointMonitorSet() 3222110eb670SHong Zhang @*/ 3223110eb670SHong Zhang PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 3224110eb670SHong Zhang { 3225110eb670SHong Zhang PetscErrorCode ierr; 3226110eb670SHong Zhang PetscViewer viewer = (PetscViewer) dummy; 3227110eb670SHong Zhang 3228110eb670SHong Zhang PetscFunctionBegin; 3229110eb670SHong Zhang PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3230110eb670SHong Zhang ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3231110eb670SHong 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); 3232110eb670SHong Zhang ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3233110eb670SHong Zhang PetscFunctionReturn(0); 3234110eb670SHong Zhang } 3235110eb670SHong Zhang 3236110eb670SHong Zhang #undef __FUNCT__ 3237cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 3238cd652676SJed Brown /*@ 3239cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 3240cd652676SJed Brown 3241cd652676SJed Brown Logically Collective on TS 3242cd652676SJed Brown 3243cd652676SJed Brown Input Argument: 3244cd652676SJed Brown . ts - time stepping context 3245cd652676SJed Brown 3246cd652676SJed Brown Output Argument: 3247cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 3248cd652676SJed Brown 3249cd652676SJed Brown Level: intermediate 3250cd652676SJed Brown 3251cd652676SJed Brown .keywords: TS, set 3252cd652676SJed Brown 3253cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 3254cd652676SJed Brown @*/ 3255cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 3256cd652676SJed Brown { 3257cd652676SJed Brown PetscFunctionBegin; 3258cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3259cd652676SJed Brown ts->retain_stages = flg; 3260cd652676SJed Brown PetscFunctionReturn(0); 3261cd652676SJed Brown } 3262cd652676SJed Brown 3263cd652676SJed Brown #undef __FUNCT__ 3264cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 3265cd652676SJed Brown /*@ 3266cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3267cd652676SJed Brown 3268cd652676SJed Brown Collective on TS 3269cd652676SJed Brown 3270cd652676SJed Brown Input Argument: 3271cd652676SJed Brown + ts - time stepping context 3272cd652676SJed Brown - t - time to interpolate to 3273cd652676SJed Brown 3274cd652676SJed Brown Output Argument: 32750910c330SBarry Smith . U - state at given time 3276cd652676SJed Brown 3277cd652676SJed Brown Notes: 3278cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 3279cd652676SJed Brown 3280cd652676SJed Brown Level: intermediate 3281cd652676SJed Brown 3282cd652676SJed Brown Developer Notes: 3283cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3284cd652676SJed Brown 3285cd652676SJed Brown .keywords: TS, set 3286cd652676SJed Brown 3287cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 3288cd652676SJed Brown @*/ 32890910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3290cd652676SJed Brown { 3291cd652676SJed Brown PetscErrorCode ierr; 3292cd652676SJed Brown 3293cd652676SJed Brown PetscFunctionBegin; 3294cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3295b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 32966712e2f1SBarry 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); 3297ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 32980910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3299cd652676SJed Brown PetscFunctionReturn(0); 3300cd652676SJed Brown } 3301cd652676SJed Brown 3302cd652676SJed Brown #undef __FUNCT__ 33034a2ae208SSatish Balay #define __FUNCT__ "TSStep" 3304d763cef2SBarry Smith /*@ 33056d9e5789SSean Farley TSStep - Steps one time step 3306d763cef2SBarry Smith 3307d763cef2SBarry Smith Collective on TS 3308d763cef2SBarry Smith 3309d763cef2SBarry Smith Input Parameter: 3310d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3311d763cef2SBarry Smith 331227829d71SBarry Smith Level: developer 3313d763cef2SBarry Smith 3314b8123daeSJed Brown Notes: 331527829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 331627829d71SBarry Smith 3317b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3318b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3319b8123daeSJed Brown 332025cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 332125cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 332225cb2221SBarry Smith 3323d763cef2SBarry Smith .keywords: TS, timestep, solve 3324d763cef2SBarry Smith 33259be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3326d763cef2SBarry Smith @*/ 3327193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3328d763cef2SBarry Smith { 33292fb8446cSMatthew G. Knepley DM dm; 3330dfbe8321SBarry Smith PetscErrorCode ierr; 3331fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3332d763cef2SBarry Smith 3333d763cef2SBarry Smith PetscFunctionBegin; 33340700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3335feed9e9dSBarry Smith if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSStep()"); 3336feed9e9dSBarry Smith 3337fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3338fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3339fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3340fffbeea8SBarry Smith " type = {Preprint},\n" 3341fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3342fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3343302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3344fffbeea8SBarry Smith 33452fb8446cSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3346d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 334768bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3348d405a339SMatthew Knepley 3349362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 335024655328SShri ts->ptime_prev = ts->ptime; 3351cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3352bbd56ea5SKarl Rupp 3353e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3354d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3355193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3356d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3357bbd56ea5SKarl Rupp 335824655328SShri ts->time_step_prev = ts->ptime - ts->ptime_prev; 3359cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3360362cd11cSLisandro Dalcin 3361362cd11cSLisandro Dalcin if (ts->reason < 0) { 3362cef5090cSJed Brown if (ts->errorifstepfailed) { 336308c7845fSBarry 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]); 336408c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3365d2daff3dSHong Zhang } 336608c7845fSBarry Smith } else if (!ts->reason) { 336708c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 336808c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 336908c7845fSBarry Smith } 33702c18e0fdSBarry Smith ts->total_steps++; 33718392e04aSShri Abhyankar ts->steprollback = PETSC_FALSE; 337208c7845fSBarry Smith PetscFunctionReturn(0); 337308c7845fSBarry Smith } 337408c7845fSBarry Smith 337508c7845fSBarry Smith #undef __FUNCT__ 337608c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep" 337708c7845fSBarry Smith /*@ 3378b957a604SHong Zhang TSAdjointStep - Steps one time step backward in the adjoint run 337908c7845fSBarry Smith 338008c7845fSBarry Smith Collective on TS 338108c7845fSBarry Smith 338208c7845fSBarry Smith Input Parameter: 338308c7845fSBarry Smith . ts - the TS context obtained from TSCreate() 338408c7845fSBarry Smith 33856a4d4014SLisandro Dalcin Level: intermediate 33866a4d4014SLisandro Dalcin 3387b957a604SHong Zhang .keywords: TS, adjoint, step 33886a4d4014SLisandro Dalcin 3389b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve() 33906a4d4014SLisandro Dalcin @*/ 339108c7845fSBarry Smith PetscErrorCode TSAdjointStep(TS ts) 33926a4d4014SLisandro Dalcin { 339308c7845fSBarry Smith DM dm; 33946a4d4014SLisandro Dalcin PetscErrorCode ierr; 33956a4d4014SLisandro Dalcin 33966a4d4014SLisandro Dalcin PetscFunctionBegin; 33974a2ae208SSatish Balay PetscValidHeaderSpecific(ts, TS_CLASSID,1); 339808c7845fSBarry Smith ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 339908c7845fSBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3400d763cef2SBarry Smith 3401d763cef2SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 340208c7845fSBarry Smith ts->ptime_prev = ts->ptime; 340308c7845fSBarry Smith ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3404685405a1SBarry Smith ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts, "-ts_view_solution");CHKERRQ(ierr); 3405d763cef2SBarry Smith 34068d0ad7a8SHong Zhang ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 340742f2b339SBarry 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); 340842f2b339SBarry Smith ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr); 34098d0ad7a8SHong Zhang ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 3410d763cef2SBarry Smith 3411cef5090cSJed Brown ts->time_step_prev = ts->ptime - ts->ptime_prev; 3412cef5090cSJed Brown ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3413362cd11cSLisandro Dalcin 3414362cd11cSLisandro Dalcin if (ts->reason < 0) { 3415cef5090cSJed Brown if (ts->errorifstepfailed) { 34166c4ed002SBarry 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]); 34176c4ed002SBarry 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]); 34186c4ed002SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3419cef5090cSJed Brown } 3420362cd11cSLisandro Dalcin } else if (!ts->reason) { 342173b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3422362cd11cSLisandro Dalcin } 34232c18e0fdSBarry Smith ts->total_steps--; 3424d763cef2SBarry Smith PetscFunctionReturn(0); 3425d763cef2SBarry Smith } 3426d763cef2SBarry Smith 3427d763cef2SBarry Smith #undef __FUNCT__ 342805175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 342905175c85SJed Brown /*@ 343005175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 343105175c85SJed Brown 34321c3436cfSJed Brown Collective on TS 343305175c85SJed Brown 343405175c85SJed Brown Input Arguments: 34351c3436cfSJed Brown + ts - time stepping context 34361c3436cfSJed Brown . order - desired order of accuracy 34370298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 343805175c85SJed Brown 343905175c85SJed Brown Output Arguments: 34400910c330SBarry Smith . U - state at the end of the current step 344105175c85SJed Brown 344205175c85SJed Brown Level: advanced 344305175c85SJed Brown 3444108c343cSJed Brown Notes: 3445108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3446108c343cSJed 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. 3447108c343cSJed Brown 34481c3436cfSJed Brown .seealso: TSStep(), TSAdapt 344905175c85SJed Brown @*/ 34500910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 345105175c85SJed Brown { 345205175c85SJed Brown PetscErrorCode ierr; 345305175c85SJed Brown 345405175c85SJed Brown PetscFunctionBegin; 345505175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 345605175c85SJed Brown PetscValidType(ts,1); 34570910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3458ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 34590910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 346005175c85SJed Brown PetscFunctionReturn(0); 346105175c85SJed Brown } 346205175c85SJed Brown 3463b1cb36f3SHong Zhang #undef __FUNCT__ 3464b1cb36f3SHong Zhang #define __FUNCT__ "TSForwardCostIntegral" 3465b1cb36f3SHong Zhang /*@ 3466b1cb36f3SHong Zhang TSForwardCostIntegral - Evaluate the cost integral in the forward run. 3467b1cb36f3SHong Zhang 3468b1cb36f3SHong Zhang Collective on TS 3469b1cb36f3SHong Zhang 3470b1cb36f3SHong Zhang Input Arguments: 3471b1cb36f3SHong Zhang . ts - time stepping context 3472b1cb36f3SHong Zhang 3473b1cb36f3SHong Zhang Level: advanced 3474b1cb36f3SHong Zhang 3475b1cb36f3SHong Zhang Notes: 3476b1cb36f3SHong Zhang This function cannot be called until TSStep() has been completed. 3477b1cb36f3SHong Zhang 3478b1cb36f3SHong Zhang .seealso: TSSolve(), TSAdjointCostIntegral() 3479b1cb36f3SHong Zhang @*/ 3480b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts) 3481b1cb36f3SHong Zhang { 3482b1cb36f3SHong Zhang PetscErrorCode ierr; 3483b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3484b1cb36f3SHong Zhang if (!ts->ops->forwardintegral) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide integral evaluation in the forward run",((PetscObject)ts)->type_name); 3485b1cb36f3SHong Zhang ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr); 3486b1cb36f3SHong Zhang PetscFunctionReturn(0); 3487b1cb36f3SHong Zhang } 3488d67e68b7SBarry Smith 348905175c85SJed Brown #undef __FUNCT__ 3490d763cef2SBarry Smith #define __FUNCT__ "TSSolve" 3491d763cef2SBarry Smith /*@ 3492d763cef2SBarry Smith TSSolve - Steps the requested number of timesteps. 3493d763cef2SBarry Smith 3494d763cef2SBarry Smith Collective on TS 3495d763cef2SBarry Smith 3496d763cef2SBarry Smith Input Parameter: 3497d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3498cc708dedSBarry Smith - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 34995a3a76d0SJed Brown 3500d763cef2SBarry Smith Level: beginner 3501d763cef2SBarry Smith 35025a3a76d0SJed Brown Notes: 35035a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 35045a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 35055a3a76d0SJed Brown stepped over the final time. 35065a3a76d0SJed Brown 3507d763cef2SBarry Smith .keywords: TS, timestep, solve 3508d763cef2SBarry Smith 3509d763cef2SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep() 3510d763cef2SBarry Smith @*/ 3511cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 3512d763cef2SBarry Smith { 3513b06615a5SLisandro Dalcin Vec solution; 3514d763cef2SBarry Smith PetscErrorCode ierr; 3515d763cef2SBarry Smith 3516d763cef2SBarry Smith PetscFunctionBegin; 3517d763cef2SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3518f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 3519feed9e9dSBarry Smith if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSSolve()"); 3520ee346746SBarry Smith if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && !ts->adapt) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE"); 3521feed9e9dSBarry Smith 352249354f04SShri 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 */ 3523b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 35240910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 3525b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3526b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3527b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 35285a3a76d0SJed Brown } 35290910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3530bbd56ea5SKarl Rupp } else if (u) { 35310910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 35325a3a76d0SJed Brown } 3533b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 353468bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3535d763cef2SBarry Smith /* reset time step and iteration counters */ 3536193ac0bcSJed Brown ts->steps = 0; 35375ef26d82SJed Brown ts->ksp_its = 0; 35385ef26d82SJed Brown ts->snes_its = 0; 3539c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3540c610991cSLisandro Dalcin ts->reject = 0; 3541193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3542193ac0bcSJed Brown 3543ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3544ec8db0baSMatthew G. Knepley { 3545ec8db0baSMatthew G. Knepley DM dm; 3546ec8db0baSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3547ec8db0baSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3548ec8db0baSMatthew G. Knepley } 3549f05ece33SBarry Smith 3550193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3551193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 35520910c330SBarry Smith ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 3553cc708dedSBarry Smith ts->solvetime = ts->ptime; 3554193ac0bcSJed Brown } else { 3555d763cef2SBarry Smith /* steps the requested number of timesteps. */ 3556db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3557db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3558cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 35596fea3669SShri Abhyankar if(ts->event) { 35606fea3669SShri Abhyankar ierr = TSEventMonitorInitialize(ts);CHKERRQ(ierr); 35616fea3669SShri Abhyankar } 3562e1a7a14fSJed Brown while (!ts->reason) { 3563193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 3564193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 3565b1cb36f3SHong Zhang if (!ts->steprollback && ts->vec_costintegral && ts->costintegralfwd) { 3566b1cb36f3SHong Zhang ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr); 3567b1cb36f3SHong Zhang } 3568aeb4809dSShri Abhyankar if (ts->event) { 3569aeb4809dSShri Abhyankar ierr = TSEventMonitor(ts);CHKERRQ(ierr); 35701eda64f1SShri Abhyankar } 35718392e04aSShri Abhyankar if(!ts->steprollback) { 3572cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 35731eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 3574aeb4809dSShri Abhyankar } 3575193ac0bcSJed Brown } 357649354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 35770910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 3578cc708dedSBarry Smith ts->solvetime = ts->max_time; 3579b06615a5SLisandro Dalcin solution = u; 35800574a7fbSJed Brown } else { 3581ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3582cc708dedSBarry Smith ts->solvetime = ts->ptime; 3583b06615a5SLisandro Dalcin solution = ts->vec_sol; 35840574a7fbSJed Brown } 3585b06615a5SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr); 3586685405a1SBarry Smith ierr = VecViewFromOptions(solution,(PetscObject) ts,"-ts_view_solution");CHKERRQ(ierr); 3587193ac0bcSJed Brown } 3588d2daff3dSHong Zhang 3589ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 3590dc1cb503SEmil Constantinescu ierr = VecViewFromOptions(ts->vec_sol,NULL,"-ts_view_solution");CHKERRQ(ierr); 359156f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 3592ef222394SBarry Smith if (ts->adjoint_solve) { 3593ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 35942b0a91c0SBarry Smith } 3595d763cef2SBarry Smith PetscFunctionReturn(0); 3596d763cef2SBarry Smith } 3597d763cef2SBarry Smith 3598d763cef2SBarry Smith #undef __FUNCT__ 3599b1cb36f3SHong Zhang #define __FUNCT__ "TSAdjointCostIntegral" 3600b1cb36f3SHong Zhang /*@ 3601b1cb36f3SHong Zhang TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run. 3602b1cb36f3SHong Zhang 3603b1cb36f3SHong Zhang Collective on TS 3604b1cb36f3SHong Zhang 3605b1cb36f3SHong Zhang Input Arguments: 3606b1cb36f3SHong Zhang . ts - time stepping context 3607b1cb36f3SHong Zhang 3608b1cb36f3SHong Zhang Level: advanced 3609b1cb36f3SHong Zhang 3610b1cb36f3SHong Zhang Notes: 3611b1cb36f3SHong Zhang This function cannot be called until TSAdjointStep() has been completed. 3612b1cb36f3SHong Zhang 3613b1cb36f3SHong Zhang .seealso: TSAdjointSolve(), TSAdjointStep 3614b1cb36f3SHong Zhang @*/ 3615b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts) 3616b1cb36f3SHong Zhang { 3617b1cb36f3SHong Zhang PetscErrorCode ierr; 3618b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3619b1cb36f3SHong Zhang if (!ts->ops->adjointintegral) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide integral evaluation in the adjoint run",((PetscObject)ts)->type_name); 3620b1cb36f3SHong Zhang ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr); 3621b1cb36f3SHong Zhang PetscFunctionReturn(0); 3622b1cb36f3SHong Zhang } 3623b1cb36f3SHong Zhang 3624b1cb36f3SHong Zhang #undef __FUNCT__ 3625c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve" 3626c88f2d44SBarry Smith /*@ 3627c88f2d44SBarry Smith TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 3628c88f2d44SBarry Smith 3629c88f2d44SBarry Smith Collective on TS 3630c88f2d44SBarry Smith 3631c88f2d44SBarry Smith Input Parameter: 36322c39e106SBarry Smith . ts - the TS context obtained from TSCreate() 3633c88f2d44SBarry Smith 3634e87fd094SBarry Smith Options Database: 3635e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions 3636e87fd094SBarry Smith 3637c88f2d44SBarry Smith Level: intermediate 3638c88f2d44SBarry Smith 3639c88f2d44SBarry Smith Notes: 3640c88f2d44SBarry Smith This must be called after a call to TSSolve() that solves the forward problem 3641c88f2d44SBarry Smith 364273b18844SBarry Smith By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time 364373b18844SBarry Smith 3644c88f2d44SBarry Smith .keywords: TS, timestep, solve 3645c88f2d44SBarry Smith 3646b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep() 3647c88f2d44SBarry Smith @*/ 36482c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts) 3649c88f2d44SBarry Smith { 3650c88f2d44SBarry Smith PetscErrorCode ierr; 3651c88f2d44SBarry Smith 3652c88f2d44SBarry Smith PetscFunctionBegin; 3653c88f2d44SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3654c88f2d44SBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 365568bece0bSHong Zhang 3656c88f2d44SBarry Smith /* reset time step and iteration counters */ 3657c88f2d44SBarry Smith ts->steps = 0; 3658c88f2d44SBarry Smith ts->ksp_its = 0; 3659c88f2d44SBarry Smith ts->snes_its = 0; 3660c88f2d44SBarry Smith ts->num_snes_failures = 0; 3661c88f2d44SBarry Smith ts->reject = 0; 3662c88f2d44SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 3663c88f2d44SBarry Smith 366473b18844SBarry Smith if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps; 3665c88f2d44SBarry Smith 366673b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3667c88f2d44SBarry Smith while (!ts->reason) { 366855c52731SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 36699110b2e7SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 3670c88f2d44SBarry Smith if (ts->event) { 3671d0578d90SShri Abhyankar ierr = TSAdjointEventMonitor(ts);CHKERRQ(ierr); 3672d0578d90SShri Abhyankar } 3673616946c1SHong Zhang ierr = TSAdjointStep(ts);CHKERRQ(ierr); 3674b1cb36f3SHong Zhang if (ts->vec_costintegral && !ts->costintegralfwd) { 3675b1cb36f3SHong Zhang ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr); 3676b1cb36f3SHong Zhang } 3677c88f2d44SBarry Smith } 367826656371SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 367926656371SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 3680c88f2d44SBarry Smith ts->solvetime = ts->ptime; 368139c6b90dSHong Zhang ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-tstrajectory_view");CHKERRQ(ierr); 3682685405a1SBarry Smith ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr); 3683c88f2d44SBarry Smith PetscFunctionReturn(0); 3684c88f2d44SBarry Smith } 3685c88f2d44SBarry Smith 3686c88f2d44SBarry Smith #undef __FUNCT__ 3687d763cef2SBarry Smith #define __FUNCT__ "TSMonitor" 36887db568b7SBarry Smith /*@C 3689228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 3690228d79bcSJed Brown 3691228d79bcSJed Brown Collective on TS 3692228d79bcSJed Brown 3693228d79bcSJed Brown Input Parameters: 3694228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 3695228d79bcSJed Brown . step - step number that has just completed 3696228d79bcSJed Brown . ptime - model time of the state 36970910c330SBarry Smith - u - state at the current model time 3698228d79bcSJed Brown 3699228d79bcSJed Brown Notes: 37007db568b7SBarry Smith TSMonitor() is typically used automatically within the time stepping implementations. 37017db568b7SBarry Smith Users would almost never call this routine directly. 3702228d79bcSJed Brown 37037db568b7SBarry Smith Level: developer 3704228d79bcSJed Brown 3705228d79bcSJed Brown .keywords: TS, timestep 3706228d79bcSJed Brown @*/ 37070910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 3708d763cef2SBarry Smith { 37096849ba73SBarry Smith PetscErrorCode ierr; 3710a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 3711d763cef2SBarry Smith 3712d763cef2SBarry Smith PetscFunctionBegin; 3713b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3714b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 37155edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 3716d763cef2SBarry Smith for (i=0; i<n; i++) { 37170910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 3718d763cef2SBarry Smith } 37195edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 3720d763cef2SBarry Smith PetscFunctionReturn(0); 3721d763cef2SBarry Smith } 3722d763cef2SBarry Smith 37239110b2e7SHong Zhang #undef __FUNCT__ 37249110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitor" 37257db568b7SBarry Smith /*@C 37269110b2e7SHong Zhang TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet() 37279110b2e7SHong Zhang 37289110b2e7SHong Zhang Collective on TS 37299110b2e7SHong Zhang 37309110b2e7SHong Zhang Input Parameters: 37319110b2e7SHong Zhang + ts - time stepping context obtained from TSCreate() 37329110b2e7SHong Zhang . step - step number that has just completed 37339110b2e7SHong Zhang . ptime - model time of the state 37349110b2e7SHong Zhang . u - state at the current model time 37359110b2e7SHong Zhang . numcost - number of cost functions (dimension of lambda or mu) 37369110b2e7SHong Zhang . lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 37379110b2e7SHong Zhang - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 37389110b2e7SHong Zhang 37399110b2e7SHong Zhang Notes: 37407db568b7SBarry Smith TSAdjointMonitor() is typically used automatically within the time stepping implementations. 37417db568b7SBarry Smith Users would almost never call this routine directly. 37429110b2e7SHong Zhang 37437db568b7SBarry Smith Level: developer 37449110b2e7SHong Zhang 37459110b2e7SHong Zhang .keywords: TS, timestep 37469110b2e7SHong Zhang @*/ 37479110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu) 37489110b2e7SHong Zhang { 37499110b2e7SHong Zhang PetscErrorCode ierr; 37509110b2e7SHong Zhang PetscInt i,n = ts->numberadjointmonitors; 37519110b2e7SHong Zhang 37529110b2e7SHong Zhang PetscFunctionBegin; 37539110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 37549110b2e7SHong Zhang PetscValidHeaderSpecific(u,VEC_CLASSID,4); 37559110b2e7SHong Zhang ierr = VecLockPush(u);CHKERRQ(ierr); 37569110b2e7SHong Zhang for (i=0; i<n; i++) { 37579110b2e7SHong Zhang ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 37589110b2e7SHong Zhang } 37599110b2e7SHong Zhang ierr = VecLockPop(u);CHKERRQ(ierr); 37609110b2e7SHong Zhang PetscFunctionReturn(0); 37619110b2e7SHong Zhang } 37629110b2e7SHong Zhang 3763d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 37644a2ae208SSatish Balay #undef __FUNCT__ 3765a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 3766d763cef2SBarry Smith /*@C 37677db568b7SBarry Smith TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 3768a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 3769d763cef2SBarry Smith 3770d763cef2SBarry Smith Collective on TS 3771d763cef2SBarry Smith 3772d763cef2SBarry Smith Input Parameters: 3773d763cef2SBarry Smith + host - the X display to open, or null for the local machine 3774d763cef2SBarry Smith . label - the title to put in the title bar 37757c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 3776a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 3777a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 3778d763cef2SBarry Smith 3779d763cef2SBarry Smith Output Parameter: 37800b039ecaSBarry Smith . ctx - the context 3781d763cef2SBarry Smith 3782d763cef2SBarry Smith Options Database Key: 37834f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 37847db568b7SBarry Smith . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 37857db568b7SBarry Smith . -ts_monitor_lg_error - monitor the error 37867db568b7SBarry Smith . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 37877db568b7SBarry Smith . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 3788b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 3789d763cef2SBarry Smith 3790d763cef2SBarry Smith Notes: 3791a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 3792d763cef2SBarry Smith 37937db568b7SBarry Smith One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 37947db568b7SBarry Smith 37957db568b7SBarry 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 37967db568b7SBarry 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 37977db568b7SBarry Smith as the first argument. 37987db568b7SBarry Smith 37997db568b7SBarry Smith One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 38007db568b7SBarry Smith 38017db568b7SBarry Smith 3802d763cef2SBarry Smith Level: intermediate 3803d763cef2SBarry Smith 38047db568b7SBarry Smith .keywords: TS, monitor, line graph, residual 3805d763cef2SBarry Smith 38067db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 38077db568b7SBarry Smith TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 38087db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 38097db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 38107db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 38117c922b88SBarry Smith 3812d763cef2SBarry Smith @*/ 3813a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 3814d763cef2SBarry Smith { 38157f52daa2SLisandro Dalcin PetscDraw draw; 3816dfbe8321SBarry Smith PetscErrorCode ierr; 3817d763cef2SBarry Smith 3818d763cef2SBarry Smith PetscFunctionBegin; 3819b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 38207f52daa2SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 38217f52daa2SLisandro Dalcin ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 38227f52daa2SLisandro Dalcin ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 3823b6fe0379SLisandro Dalcin ierr = PetscDrawLGSetUseMarkers((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr); 3824287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 38257f52daa2SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 3826a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 3827d763cef2SBarry Smith PetscFunctionReturn(0); 3828d763cef2SBarry Smith } 3829d763cef2SBarry Smith 38304a2ae208SSatish Balay #undef __FUNCT__ 38314f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 3832b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 3833d763cef2SBarry Smith { 38340b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 3835c32365f1SBarry Smith PetscReal x = ptime,y; 3836dfbe8321SBarry Smith PetscErrorCode ierr; 3837d763cef2SBarry Smith 3838d763cef2SBarry Smith PetscFunctionBegin; 3839b06615a5SLisandro Dalcin if (!step) { 3840a9f9c1f6SBarry Smith PetscDrawAxis axis; 3841a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 38426934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr); 3843a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 3844a9f9c1f6SBarry Smith } 3845c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 38460b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 3847b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 38480b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 38496934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 38503923b477SBarry Smith } 3851d763cef2SBarry Smith PetscFunctionReturn(0); 3852d763cef2SBarry Smith } 3853d763cef2SBarry Smith 38544a2ae208SSatish Balay #undef __FUNCT__ 3855a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 3856d763cef2SBarry Smith /*@C 3857a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 3858a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 3859d763cef2SBarry Smith 38600b039ecaSBarry Smith Collective on TSMonitorLGCtx 3861d763cef2SBarry Smith 3862d763cef2SBarry Smith Input Parameter: 38630b039ecaSBarry Smith . ctx - the monitor context 3864d763cef2SBarry Smith 3865d763cef2SBarry Smith Level: intermediate 3866d763cef2SBarry Smith 3867d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 3868d763cef2SBarry Smith 38694f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 3870d763cef2SBarry Smith @*/ 3871a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 3872d763cef2SBarry Smith { 3873dfbe8321SBarry Smith PetscErrorCode ierr; 3874d763cef2SBarry Smith 3875d763cef2SBarry Smith PetscFunctionBegin; 38767684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 38777684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 38787684fa3eSBarry Smith } 38790b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 388031152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 3881387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 3882387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 3883387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 38840b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 3885d763cef2SBarry Smith PetscFunctionReturn(0); 3886d763cef2SBarry Smith } 3887d763cef2SBarry Smith 38884a2ae208SSatish Balay #undef __FUNCT__ 38894a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 3890d763cef2SBarry Smith /*@ 3891b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 3892d763cef2SBarry Smith 3893d763cef2SBarry Smith Not Collective 3894d763cef2SBarry Smith 3895d763cef2SBarry Smith Input Parameter: 3896d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3897d763cef2SBarry Smith 3898d763cef2SBarry Smith Output Parameter: 3899d763cef2SBarry Smith . t - the current time 3900d763cef2SBarry Smith 3901d763cef2SBarry Smith Level: beginner 3902d763cef2SBarry Smith 3903b8123daeSJed Brown Note: 3904b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 39059be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 3906b8123daeSJed Brown 3907d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3908d763cef2SBarry Smith 3909d763cef2SBarry Smith .keywords: TS, get, time 3910d763cef2SBarry Smith @*/ 39117087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 3912d763cef2SBarry Smith { 3913d763cef2SBarry Smith PetscFunctionBegin; 39140700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3915f7cf8827SBarry Smith PetscValidRealPointer(t,2); 3916d763cef2SBarry Smith *t = ts->ptime; 3917d763cef2SBarry Smith PetscFunctionReturn(0); 3918d763cef2SBarry Smith } 3919d763cef2SBarry Smith 39204a2ae208SSatish Balay #undef __FUNCT__ 3921e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime" 3922e5e524a1SHong Zhang /*@ 3923e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 3924e5e524a1SHong Zhang 3925e5e524a1SHong Zhang Not Collective 3926e5e524a1SHong Zhang 3927e5e524a1SHong Zhang Input Parameter: 3928e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 3929e5e524a1SHong Zhang 3930e5e524a1SHong Zhang Output Parameter: 3931e5e524a1SHong Zhang . t - the previous time 3932e5e524a1SHong Zhang 3933e5e524a1SHong Zhang Level: beginner 3934e5e524a1SHong Zhang 3935e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3936e5e524a1SHong Zhang 3937e5e524a1SHong Zhang .keywords: TS, get, time 3938e5e524a1SHong Zhang @*/ 3939e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 3940e5e524a1SHong Zhang { 3941e5e524a1SHong Zhang PetscFunctionBegin; 3942e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3943e5e524a1SHong Zhang PetscValidRealPointer(t,2); 3944e5e524a1SHong Zhang *t = ts->ptime_prev; 3945e5e524a1SHong Zhang PetscFunctionReturn(0); 3946e5e524a1SHong Zhang } 3947e5e524a1SHong Zhang 3948e5e524a1SHong Zhang #undef __FUNCT__ 39496a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 39506a4d4014SLisandro Dalcin /*@ 39516a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 39526a4d4014SLisandro Dalcin 39533f9fe445SBarry Smith Logically Collective on TS 39546a4d4014SLisandro Dalcin 39556a4d4014SLisandro Dalcin Input Parameters: 39566a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 39576a4d4014SLisandro Dalcin - time - the time 39586a4d4014SLisandro Dalcin 39596a4d4014SLisandro Dalcin Level: intermediate 39606a4d4014SLisandro Dalcin 39616a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 39626a4d4014SLisandro Dalcin 39636a4d4014SLisandro Dalcin .keywords: TS, set, time 39646a4d4014SLisandro Dalcin @*/ 39657087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 39666a4d4014SLisandro Dalcin { 39676a4d4014SLisandro Dalcin PetscFunctionBegin; 39680700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3969c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 39706a4d4014SLisandro Dalcin ts->ptime = t; 39716a4d4014SLisandro Dalcin PetscFunctionReturn(0); 39726a4d4014SLisandro Dalcin } 39736a4d4014SLisandro Dalcin 39746a4d4014SLisandro Dalcin #undef __FUNCT__ 39754a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 3976d763cef2SBarry Smith /*@C 3977d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 3978d763cef2SBarry Smith TS options in the database. 3979d763cef2SBarry Smith 39803f9fe445SBarry Smith Logically Collective on TS 3981d763cef2SBarry Smith 3982d763cef2SBarry Smith Input Parameter: 3983d763cef2SBarry Smith + ts - The TS context 3984d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3985d763cef2SBarry Smith 3986d763cef2SBarry Smith Notes: 3987d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3988d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3989d763cef2SBarry Smith hyphen. 3990d763cef2SBarry Smith 3991d763cef2SBarry Smith Level: advanced 3992d763cef2SBarry Smith 3993d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 3994d763cef2SBarry Smith 3995d763cef2SBarry Smith .seealso: TSSetFromOptions() 3996d763cef2SBarry Smith 3997d763cef2SBarry Smith @*/ 39987087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 3999d763cef2SBarry Smith { 4000dfbe8321SBarry Smith PetscErrorCode ierr; 4001089b2837SJed Brown SNES snes; 4002d763cef2SBarry Smith 4003d763cef2SBarry Smith PetscFunctionBegin; 40040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4005d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4006089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4007089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4008d763cef2SBarry Smith PetscFunctionReturn(0); 4009d763cef2SBarry Smith } 4010d763cef2SBarry Smith 4011d763cef2SBarry Smith 40124a2ae208SSatish Balay #undef __FUNCT__ 40134a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 4014d763cef2SBarry Smith /*@C 4015d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4016d763cef2SBarry Smith TS options in the database. 4017d763cef2SBarry Smith 40183f9fe445SBarry Smith Logically Collective on TS 4019d763cef2SBarry Smith 4020d763cef2SBarry Smith Input Parameter: 4021d763cef2SBarry Smith + ts - The TS context 4022d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4023d763cef2SBarry Smith 4024d763cef2SBarry Smith Notes: 4025d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4026d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4027d763cef2SBarry Smith hyphen. 4028d763cef2SBarry Smith 4029d763cef2SBarry Smith Level: advanced 4030d763cef2SBarry Smith 4031d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 4032d763cef2SBarry Smith 4033d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 4034d763cef2SBarry Smith 4035d763cef2SBarry Smith @*/ 40367087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4037d763cef2SBarry Smith { 4038dfbe8321SBarry Smith PetscErrorCode ierr; 4039089b2837SJed Brown SNES snes; 4040d763cef2SBarry Smith 4041d763cef2SBarry Smith PetscFunctionBegin; 40420700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4043d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4044089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4045089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4046d763cef2SBarry Smith PetscFunctionReturn(0); 4047d763cef2SBarry Smith } 4048d763cef2SBarry Smith 40494a2ae208SSatish Balay #undef __FUNCT__ 40504a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 4051d763cef2SBarry Smith /*@C 4052d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4053d763cef2SBarry Smith TS options in the database. 4054d763cef2SBarry Smith 4055d763cef2SBarry Smith Not Collective 4056d763cef2SBarry Smith 4057d763cef2SBarry Smith Input Parameter: 4058d763cef2SBarry Smith . ts - The TS context 4059d763cef2SBarry Smith 4060d763cef2SBarry Smith Output Parameter: 4061d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4062d763cef2SBarry Smith 4063d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 4064d763cef2SBarry Smith sufficient length to hold the prefix. 4065d763cef2SBarry Smith 4066d763cef2SBarry Smith Level: intermediate 4067d763cef2SBarry Smith 4068d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 4069d763cef2SBarry Smith 4070d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 4071d763cef2SBarry Smith @*/ 40727087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4073d763cef2SBarry Smith { 4074dfbe8321SBarry Smith PetscErrorCode ierr; 4075d763cef2SBarry Smith 4076d763cef2SBarry Smith PetscFunctionBegin; 40770700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 40784482741eSBarry Smith PetscValidPointer(prefix,2); 4079d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4080d763cef2SBarry Smith PetscFunctionReturn(0); 4081d763cef2SBarry Smith } 4082d763cef2SBarry Smith 40834a2ae208SSatish Balay #undef __FUNCT__ 40844a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 4085d763cef2SBarry Smith /*@C 4086d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4087d763cef2SBarry Smith 4088d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4089d763cef2SBarry Smith 4090d763cef2SBarry Smith Input Parameter: 4091d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4092d763cef2SBarry Smith 4093d763cef2SBarry Smith Output Parameters: 4094e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4095e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4096e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4097e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4098d763cef2SBarry Smith 40990298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 4100d763cef2SBarry Smith 4101d763cef2SBarry Smith Level: intermediate 4102d763cef2SBarry Smith 410326d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 4104d763cef2SBarry Smith 4105d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 4106d763cef2SBarry Smith @*/ 4107e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4108d763cef2SBarry Smith { 4109089b2837SJed Brown PetscErrorCode ierr; 4110089b2837SJed Brown SNES snes; 411124989b8cSPeter Brune DM dm; 4112089b2837SJed Brown 4113d763cef2SBarry Smith PetscFunctionBegin; 4114089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4115e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 411624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 411724989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 4118d763cef2SBarry Smith PetscFunctionReturn(0); 4119d763cef2SBarry Smith } 4120d763cef2SBarry Smith 41211713a123SBarry Smith #undef __FUNCT__ 41222eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 41232eca1d9cSJed Brown /*@C 41242eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 41252eca1d9cSJed Brown 41262eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 41272eca1d9cSJed Brown 41282eca1d9cSJed Brown Input Parameter: 41292eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 41302eca1d9cSJed Brown 41312eca1d9cSJed Brown Output Parameters: 4132e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4133e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 41342eca1d9cSJed Brown . f - The function to compute the matrices 41352eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 41362eca1d9cSJed Brown 41370298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 41382eca1d9cSJed Brown 41392eca1d9cSJed Brown Level: advanced 41402eca1d9cSJed Brown 41412eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 41422eca1d9cSJed Brown 41432eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 41442eca1d9cSJed Brown @*/ 4145e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 41462eca1d9cSJed Brown { 4147089b2837SJed Brown PetscErrorCode ierr; 4148089b2837SJed Brown SNES snes; 414924989b8cSPeter Brune DM dm; 41500910c330SBarry Smith 41512eca1d9cSJed Brown PetscFunctionBegin; 4152089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4153f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4154e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 415524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 415624989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 41572eca1d9cSJed Brown PetscFunctionReturn(0); 41582eca1d9cSJed Brown } 41592eca1d9cSJed Brown 41606083293cSBarry Smith 41612eca1d9cSJed Brown #undef __FUNCT__ 416283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 41631713a123SBarry Smith /*@C 416483a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 41651713a123SBarry Smith VecView() for the solution at each timestep 41661713a123SBarry Smith 41671713a123SBarry Smith Collective on TS 41681713a123SBarry Smith 41691713a123SBarry Smith Input Parameters: 41701713a123SBarry Smith + ts - the TS context 41711713a123SBarry Smith . step - current time-step 4172142b95e3SSatish Balay . ptime - current time 41730298fd71SBarry Smith - dummy - either a viewer or NULL 41741713a123SBarry Smith 417599fdda47SBarry Smith Options Database: 417699fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 417799fdda47SBarry Smith 4178387f4636SBarry 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 417999fdda47SBarry Smith will look bad 418099fdda47SBarry Smith 41811713a123SBarry Smith Level: intermediate 41821713a123SBarry Smith 41831713a123SBarry Smith .keywords: TS, vector, monitor, view 41841713a123SBarry Smith 4185a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 41861713a123SBarry Smith @*/ 41870910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 41881713a123SBarry Smith { 4189dfbe8321SBarry Smith PetscErrorCode ierr; 419083a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4191473a3ab2SBarry Smith PetscDraw draw; 41921713a123SBarry Smith 41931713a123SBarry Smith PetscFunctionBegin; 41946083293cSBarry Smith if (!step && ictx->showinitial) { 41956083293cSBarry Smith if (!ictx->initialsolution) { 41960910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 41971713a123SBarry Smith } 41980910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 41996083293cSBarry Smith } 4200b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 42010dcf80beSBarry Smith 42026083293cSBarry Smith if (ictx->showinitial) { 42036083293cSBarry Smith PetscReal pause; 42046083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 42056083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 42066083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 42076083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 42086083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 42096083293cSBarry Smith } 42100910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 4211473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 421251fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 4213473a3ab2SBarry Smith char time[32]; 4214473a3ab2SBarry Smith 4215473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 421685b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4217473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4218473a3ab2SBarry Smith h = yl + .95*(yr - yl); 421951fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4220473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4221473a3ab2SBarry Smith } 4222473a3ab2SBarry Smith 42236083293cSBarry Smith if (ictx->showinitial) { 42246083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 42256083293cSBarry Smith } 42261713a123SBarry Smith PetscFunctionReturn(0); 42271713a123SBarry Smith } 42281713a123SBarry Smith 42292d5ee99bSBarry Smith #undef __FUNCT__ 42309110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorDrawSensi" 42319110b2e7SHong Zhang /*@C 42329110b2e7SHong Zhang TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling 42339110b2e7SHong Zhang VecView() for the sensitivities to initial states at each timestep 42349110b2e7SHong Zhang 42359110b2e7SHong Zhang Collective on TS 42369110b2e7SHong Zhang 42379110b2e7SHong Zhang Input Parameters: 42389110b2e7SHong Zhang + ts - the TS context 42399110b2e7SHong Zhang . step - current time-step 42409110b2e7SHong Zhang . ptime - current time 42419110b2e7SHong Zhang . u - current state 42429110b2e7SHong Zhang . numcost - number of cost functions 42439110b2e7SHong Zhang . lambda - sensitivities to initial conditions 42449110b2e7SHong Zhang . mu - sensitivities to parameters 42459110b2e7SHong Zhang - dummy - either a viewer or NULL 42469110b2e7SHong Zhang 42479110b2e7SHong Zhang Level: intermediate 42489110b2e7SHong Zhang 42499110b2e7SHong Zhang .keywords: TS, vector, adjoint, monitor, view 42509110b2e7SHong Zhang 42519110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView() 42529110b2e7SHong Zhang @*/ 42539110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 42549110b2e7SHong Zhang { 42559110b2e7SHong Zhang PetscErrorCode ierr; 42569110b2e7SHong Zhang TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 42579110b2e7SHong Zhang PetscDraw draw; 4258a0046e2cSSatish Balay PetscReal xl,yl,xr,yr,h; 4259a0046e2cSSatish Balay char time[32]; 42609110b2e7SHong Zhang 42619110b2e7SHong Zhang PetscFunctionBegin; 42629110b2e7SHong Zhang if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 42639110b2e7SHong Zhang 42649110b2e7SHong Zhang ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr); 42659110b2e7SHong Zhang ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 42669110b2e7SHong Zhang ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 42679110b2e7SHong Zhang ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 42689110b2e7SHong Zhang h = yl + .95*(yr - yl); 42699110b2e7SHong Zhang ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 42709110b2e7SHong Zhang ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 42719110b2e7SHong Zhang PetscFunctionReturn(0); 42729110b2e7SHong Zhang } 42739110b2e7SHong Zhang 42749110b2e7SHong Zhang #undef __FUNCT__ 42752d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase" 42762d5ee99bSBarry Smith /*@C 42772d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 42782d5ee99bSBarry Smith 42792d5ee99bSBarry Smith Collective on TS 42802d5ee99bSBarry Smith 42812d5ee99bSBarry Smith Input Parameters: 42822d5ee99bSBarry Smith + ts - the TS context 42832d5ee99bSBarry Smith . step - current time-step 42842d5ee99bSBarry Smith . ptime - current time 42852d5ee99bSBarry Smith - dummy - either a viewer or NULL 42862d5ee99bSBarry Smith 42872d5ee99bSBarry Smith Level: intermediate 42882d5ee99bSBarry Smith 42892d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 42902d5ee99bSBarry Smith 42912d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 42922d5ee99bSBarry Smith @*/ 42932d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 42942d5ee99bSBarry Smith { 42952d5ee99bSBarry Smith PetscErrorCode ierr; 42962d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 42972d5ee99bSBarry Smith PetscDraw draw; 42986934998bSLisandro Dalcin PetscDrawAxis axis; 42992d5ee99bSBarry Smith PetscInt n; 43002d5ee99bSBarry Smith PetscMPIInt size; 43016934998bSLisandro Dalcin PetscReal U0,U1,xl,yl,xr,yr,h; 43022d5ee99bSBarry Smith char time[32]; 43032d5ee99bSBarry Smith const PetscScalar *U; 43042d5ee99bSBarry Smith 43052d5ee99bSBarry Smith PetscFunctionBegin; 43066934998bSLisandro Dalcin ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr); 43076934998bSLisandro Dalcin if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs"); 43082d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 43096934998bSLisandro Dalcin if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 43102d5ee99bSBarry Smith 43112d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 43126934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr); 43136934998bSLisandro Dalcin ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 43146934998bSLisandro Dalcin if (!step) { 43156934998bSLisandro Dalcin ierr = PetscDrawClear(draw);CHKERRQ(ierr); 43166934998bSLisandro Dalcin ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); 43176934998bSLisandro Dalcin } 43182d5ee99bSBarry Smith 43192d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 43206934998bSLisandro Dalcin U0 = PetscRealPart(U[0]); 43216934998bSLisandro Dalcin U1 = PetscRealPart(U[1]); 4322a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 43236934998bSLisandro Dalcin if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0); 43242d5ee99bSBarry Smith 43256934998bSLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 43266934998bSLisandro Dalcin ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr); 43272d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 43284b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 432985b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 43302d5ee99bSBarry Smith h = yl + .95*(yr - yl); 433151fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 43322d5ee99bSBarry Smith } 43336934998bSLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 43342d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 43356934998bSLisandro Dalcin ierr = PetscDrawSave(draw);CHKERRQ(ierr); 43362d5ee99bSBarry Smith PetscFunctionReturn(0); 43372d5ee99bSBarry Smith } 43382d5ee99bSBarry Smith 43391713a123SBarry Smith 43406c699258SBarry Smith #undef __FUNCT__ 434183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 43426083293cSBarry Smith /*@C 434383a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 43446083293cSBarry Smith 43456083293cSBarry Smith Collective on TS 43466083293cSBarry Smith 43476083293cSBarry Smith Input Parameters: 43486083293cSBarry Smith . ctx - the monitor context 43496083293cSBarry Smith 43506083293cSBarry Smith Level: intermediate 43516083293cSBarry Smith 43526083293cSBarry Smith .keywords: TS, vector, monitor, view 43536083293cSBarry Smith 435483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 43556083293cSBarry Smith @*/ 435683a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 43576083293cSBarry Smith { 43586083293cSBarry Smith PetscErrorCode ierr; 43596083293cSBarry Smith 43606083293cSBarry Smith PetscFunctionBegin; 436183a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 436283a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 436383a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 43646083293cSBarry Smith PetscFunctionReturn(0); 43656083293cSBarry Smith } 43666083293cSBarry Smith 43676083293cSBarry Smith #undef __FUNCT__ 436883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 43696083293cSBarry Smith /*@C 437083a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 43716083293cSBarry Smith 43726083293cSBarry Smith Collective on TS 43736083293cSBarry Smith 43746083293cSBarry Smith Input Parameter: 43756083293cSBarry Smith . ts - time-step context 43766083293cSBarry Smith 43776083293cSBarry Smith Output Patameter: 43786083293cSBarry Smith . ctx - the monitor context 43796083293cSBarry Smith 438099fdda47SBarry Smith Options Database: 438199fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 438299fdda47SBarry Smith 43836083293cSBarry Smith Level: intermediate 43846083293cSBarry Smith 43856083293cSBarry Smith .keywords: TS, vector, monitor, view 43866083293cSBarry Smith 438783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 43886083293cSBarry Smith @*/ 438983a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 43906083293cSBarry Smith { 43916083293cSBarry Smith PetscErrorCode ierr; 43926083293cSBarry Smith 43936083293cSBarry Smith PetscFunctionBegin; 4394b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 439583a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4396e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4397bbd56ea5SKarl Rupp 439883a4ac43SBarry Smith (*ctx)->howoften = howoften; 4399473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 4400c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4401473a3ab2SBarry Smith 4402473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 4403c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 44046083293cSBarry Smith PetscFunctionReturn(0); 44056083293cSBarry Smith } 44066083293cSBarry Smith 44076083293cSBarry Smith #undef __FUNCT__ 440883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 44093a471f94SBarry Smith /*@C 441083a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 44113a471f94SBarry Smith VecView() for the error at each timestep 44123a471f94SBarry Smith 44133a471f94SBarry Smith Collective on TS 44143a471f94SBarry Smith 44153a471f94SBarry Smith Input Parameters: 44163a471f94SBarry Smith + ts - the TS context 44173a471f94SBarry Smith . step - current time-step 44183a471f94SBarry Smith . ptime - current time 44190298fd71SBarry Smith - dummy - either a viewer or NULL 44203a471f94SBarry Smith 44213a471f94SBarry Smith Level: intermediate 44223a471f94SBarry Smith 44233a471f94SBarry Smith .keywords: TS, vector, monitor, view 44243a471f94SBarry Smith 44253a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 44263a471f94SBarry Smith @*/ 44270910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 44283a471f94SBarry Smith { 44293a471f94SBarry Smith PetscErrorCode ierr; 443083a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 443183a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 44323a471f94SBarry Smith Vec work; 44333a471f94SBarry Smith 44343a471f94SBarry Smith PetscFunctionBegin; 4435b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 44360910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 44373a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 44380910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 44393a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 44403a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 44413a471f94SBarry Smith PetscFunctionReturn(0); 44423a471f94SBarry Smith } 44433a471f94SBarry Smith 4444af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 44453a471f94SBarry Smith #undef __FUNCT__ 44466c699258SBarry Smith #define __FUNCT__ "TSSetDM" 44476c699258SBarry Smith /*@ 44486c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 44496c699258SBarry Smith 44503f9fe445SBarry Smith Logically Collective on TS and DM 44516c699258SBarry Smith 44526c699258SBarry Smith Input Parameters: 44536c699258SBarry Smith + ts - the preconditioner context 44546c699258SBarry Smith - dm - the dm 44556c699258SBarry Smith 44566c699258SBarry Smith Level: intermediate 44576c699258SBarry Smith 44586c699258SBarry Smith 44596c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 44606c699258SBarry Smith @*/ 44617087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 44626c699258SBarry Smith { 44636c699258SBarry Smith PetscErrorCode ierr; 4464089b2837SJed Brown SNES snes; 4465942e3340SBarry Smith DMTS tsdm; 44666c699258SBarry Smith 44676c699258SBarry Smith PetscFunctionBegin; 44680700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 446970663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4470942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 44712a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4472942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4473942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 447424989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 447524989b8cSPeter Brune tsdm->originaldm = dm; 447624989b8cSPeter Brune } 447724989b8cSPeter Brune } 44786bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 447924989b8cSPeter Brune } 44806c699258SBarry Smith ts->dm = dm; 4481bbd56ea5SKarl Rupp 4482089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4483089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 44846c699258SBarry Smith PetscFunctionReturn(0); 44856c699258SBarry Smith } 44866c699258SBarry Smith 44876c699258SBarry Smith #undef __FUNCT__ 44886c699258SBarry Smith #define __FUNCT__ "TSGetDM" 44896c699258SBarry Smith /*@ 44906c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 44916c699258SBarry Smith 44923f9fe445SBarry Smith Not Collective 44936c699258SBarry Smith 44946c699258SBarry Smith Input Parameter: 44956c699258SBarry Smith . ts - the preconditioner context 44966c699258SBarry Smith 44976c699258SBarry Smith Output Parameter: 44986c699258SBarry Smith . dm - the dm 44996c699258SBarry Smith 45006c699258SBarry Smith Level: intermediate 45016c699258SBarry Smith 45026c699258SBarry Smith 45036c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 45046c699258SBarry Smith @*/ 45057087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 45066c699258SBarry Smith { 4507496e6a7aSJed Brown PetscErrorCode ierr; 4508496e6a7aSJed Brown 45096c699258SBarry Smith PetscFunctionBegin; 45100700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4511496e6a7aSJed Brown if (!ts->dm) { 4512ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4513496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4514496e6a7aSJed Brown } 45156c699258SBarry Smith *dm = ts->dm; 45166c699258SBarry Smith PetscFunctionReturn(0); 45176c699258SBarry Smith } 45181713a123SBarry Smith 45190f5c6efeSJed Brown #undef __FUNCT__ 45200f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 45210f5c6efeSJed Brown /*@ 45220f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 45230f5c6efeSJed Brown 45243f9fe445SBarry Smith Logically Collective on SNES 45250f5c6efeSJed Brown 45260f5c6efeSJed Brown Input Parameter: 4527d42a1c89SJed Brown + snes - nonlinear solver 45280910c330SBarry Smith . U - the current state at which to evaluate the residual 4529d42a1c89SJed Brown - ctx - user context, must be a TS 45300f5c6efeSJed Brown 45310f5c6efeSJed Brown Output Parameter: 45320f5c6efeSJed Brown . F - the nonlinear residual 45330f5c6efeSJed Brown 45340f5c6efeSJed Brown Notes: 45350f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 45360f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 45370f5c6efeSJed Brown 45380f5c6efeSJed Brown Level: advanced 45390f5c6efeSJed Brown 45400f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 45410f5c6efeSJed Brown @*/ 45420910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 45430f5c6efeSJed Brown { 45440f5c6efeSJed Brown TS ts = (TS)ctx; 45450f5c6efeSJed Brown PetscErrorCode ierr; 45460f5c6efeSJed Brown 45470f5c6efeSJed Brown PetscFunctionBegin; 45480f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 45490910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 45500f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 45510f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 45520910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 45530f5c6efeSJed Brown PetscFunctionReturn(0); 45540f5c6efeSJed Brown } 45550f5c6efeSJed Brown 45560f5c6efeSJed Brown #undef __FUNCT__ 45570f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 45580f5c6efeSJed Brown /*@ 45590f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 45600f5c6efeSJed Brown 45610f5c6efeSJed Brown Collective on SNES 45620f5c6efeSJed Brown 45630f5c6efeSJed Brown Input Parameter: 45640f5c6efeSJed Brown + snes - nonlinear solver 45650910c330SBarry Smith . U - the current state at which to evaluate the residual 45660f5c6efeSJed Brown - ctx - user context, must be a TS 45670f5c6efeSJed Brown 45680f5c6efeSJed Brown Output Parameter: 45690f5c6efeSJed Brown + A - the Jacobian 45700f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 45710f5c6efeSJed Brown - flag - indicates any structure change in the matrix 45720f5c6efeSJed Brown 45730f5c6efeSJed Brown Notes: 45740f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 45750f5c6efeSJed Brown 45760f5c6efeSJed Brown Level: developer 45770f5c6efeSJed Brown 45780f5c6efeSJed Brown .seealso: SNESSetJacobian() 45790f5c6efeSJed Brown @*/ 4580d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 45810f5c6efeSJed Brown { 45820f5c6efeSJed Brown TS ts = (TS)ctx; 45830f5c6efeSJed Brown PetscErrorCode ierr; 45840f5c6efeSJed Brown 45850f5c6efeSJed Brown PetscFunctionBegin; 45860f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 45870910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 45880f5c6efeSJed Brown PetscValidPointer(A,3); 458994ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 45900f5c6efeSJed Brown PetscValidPointer(B,4); 459194ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 45920f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 4593d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 45940f5c6efeSJed Brown PetscFunctionReturn(0); 45950f5c6efeSJed Brown } 4596325fc9f4SBarry Smith 45970e4ef248SJed Brown #undef __FUNCT__ 45980e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 45990e4ef248SJed Brown /*@C 46009ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 46010e4ef248SJed Brown 46020e4ef248SJed Brown Collective on TS 46030e4ef248SJed Brown 46040e4ef248SJed Brown Input Arguments: 46050e4ef248SJed Brown + ts - time stepping context 46060e4ef248SJed Brown . t - time at which to evaluate 46070910c330SBarry Smith . U - state at which to evaluate 46080e4ef248SJed Brown - ctx - context 46090e4ef248SJed Brown 46100e4ef248SJed Brown Output Arguments: 46110e4ef248SJed Brown . F - right hand side 46120e4ef248SJed Brown 46130e4ef248SJed Brown Level: intermediate 46140e4ef248SJed Brown 46150e4ef248SJed Brown Notes: 46160e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 46170e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 46180e4ef248SJed Brown 46190e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 46200e4ef248SJed Brown @*/ 46210910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 46220e4ef248SJed Brown { 46230e4ef248SJed Brown PetscErrorCode ierr; 46240e4ef248SJed Brown Mat Arhs,Brhs; 46250e4ef248SJed Brown 46260e4ef248SJed Brown PetscFunctionBegin; 46270e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 4628d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 46290910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 46300e4ef248SJed Brown PetscFunctionReturn(0); 46310e4ef248SJed Brown } 46320e4ef248SJed Brown 46330e4ef248SJed Brown #undef __FUNCT__ 46340e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 46350e4ef248SJed Brown /*@C 46360e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 46370e4ef248SJed Brown 46380e4ef248SJed Brown Collective on TS 46390e4ef248SJed Brown 46400e4ef248SJed Brown Input Arguments: 46410e4ef248SJed Brown + ts - time stepping context 46420e4ef248SJed Brown . t - time at which to evaluate 46430910c330SBarry Smith . U - state at which to evaluate 46440e4ef248SJed Brown - ctx - context 46450e4ef248SJed Brown 46460e4ef248SJed Brown Output Arguments: 46470e4ef248SJed Brown + A - pointer to operator 46480e4ef248SJed Brown . B - pointer to preconditioning matrix 46490e4ef248SJed Brown - flg - matrix structure flag 46500e4ef248SJed Brown 46510e4ef248SJed Brown Level: intermediate 46520e4ef248SJed Brown 46530e4ef248SJed Brown Notes: 46540e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 46550e4ef248SJed Brown 46560e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 46570e4ef248SJed Brown @*/ 4658d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 46590e4ef248SJed Brown { 46600e4ef248SJed Brown PetscFunctionBegin; 46610e4ef248SJed Brown PetscFunctionReturn(0); 46620e4ef248SJed Brown } 46630e4ef248SJed Brown 46640026cea9SSean Farley #undef __FUNCT__ 46650026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 46660026cea9SSean Farley /*@C 46670026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 46680026cea9SSean Farley 46690026cea9SSean Farley Collective on TS 46700026cea9SSean Farley 46710026cea9SSean Farley Input Arguments: 46720026cea9SSean Farley + ts - time stepping context 46730026cea9SSean Farley . t - time at which to evaluate 46740910c330SBarry Smith . U - state at which to evaluate 46750910c330SBarry Smith . Udot - time derivative of state vector 46760026cea9SSean Farley - ctx - context 46770026cea9SSean Farley 46780026cea9SSean Farley Output Arguments: 46790026cea9SSean Farley . F - left hand side 46800026cea9SSean Farley 46810026cea9SSean Farley Level: intermediate 46820026cea9SSean Farley 46830026cea9SSean Farley Notes: 46840910c330SBarry 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 46850026cea9SSean Farley user is required to write their own TSComputeIFunction. 46860026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 46870026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 46880026cea9SSean Farley 46899ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 46909ae8fd06SBarry Smith 46919ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 46920026cea9SSean Farley @*/ 46930910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 46940026cea9SSean Farley { 46950026cea9SSean Farley PetscErrorCode ierr; 46960026cea9SSean Farley Mat A,B; 46970026cea9SSean Farley 46980026cea9SSean Farley PetscFunctionBegin; 46990298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 4700d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 47010910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 47020026cea9SSean Farley PetscFunctionReturn(0); 47030026cea9SSean Farley } 47040026cea9SSean Farley 47050026cea9SSean Farley #undef __FUNCT__ 47060026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 47070026cea9SSean Farley /*@C 4708b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 47090026cea9SSean Farley 47100026cea9SSean Farley Collective on TS 47110026cea9SSean Farley 47120026cea9SSean Farley Input Arguments: 47130026cea9SSean Farley + ts - time stepping context 47140026cea9SSean Farley . t - time at which to evaluate 47150910c330SBarry Smith . U - state at which to evaluate 47160910c330SBarry Smith . Udot - time derivative of state vector 47170026cea9SSean Farley . shift - shift to apply 47180026cea9SSean Farley - ctx - context 47190026cea9SSean Farley 47200026cea9SSean Farley Output Arguments: 47210026cea9SSean Farley + A - pointer to operator 47220026cea9SSean Farley . B - pointer to preconditioning matrix 47230026cea9SSean Farley - flg - matrix structure flag 47240026cea9SSean Farley 4725b41af12eSJed Brown Level: advanced 47260026cea9SSean Farley 47270026cea9SSean Farley Notes: 47280026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 47290026cea9SSean Farley 4730b41af12eSJed Brown It is only appropriate for problems of the form 4731b41af12eSJed Brown 4732b41af12eSJed Brown $ M Udot = F(U,t) 4733b41af12eSJed Brown 4734b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4735b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4736b41af12eSJed Brown an implicit operator of the form 4737b41af12eSJed Brown 4738b41af12eSJed Brown $ shift*M + J 4739b41af12eSJed Brown 4740b41af12eSJed 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 4741b41af12eSJed Brown a copy of M or reassemble it when requested. 4742b41af12eSJed Brown 47430026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 47440026cea9SSean Farley @*/ 4745d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 47460026cea9SSean Farley { 4747b41af12eSJed Brown PetscErrorCode ierr; 4748b41af12eSJed Brown 47490026cea9SSean Farley PetscFunctionBegin; 475094ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4751b41af12eSJed Brown ts->ijacobian.shift = shift; 47520026cea9SSean Farley PetscFunctionReturn(0); 47530026cea9SSean Farley } 4754b41af12eSJed Brown 4755e817cc15SEmil Constantinescu #undef __FUNCT__ 4756e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType" 4757e817cc15SEmil Constantinescu /*@ 4758e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 4759e817cc15SEmil Constantinescu 4760e817cc15SEmil Constantinescu Not Collective 4761e817cc15SEmil Constantinescu 4762e817cc15SEmil Constantinescu Input Parameter: 4763e817cc15SEmil Constantinescu . ts - the TS context 4764e817cc15SEmil Constantinescu 4765e817cc15SEmil Constantinescu Output Parameter: 47664e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4767e817cc15SEmil Constantinescu 4768e817cc15SEmil Constantinescu Level: beginner 4769e817cc15SEmil Constantinescu 4770e817cc15SEmil Constantinescu .keywords: TS, equation type 4771e817cc15SEmil Constantinescu 4772e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 4773e817cc15SEmil Constantinescu @*/ 4774e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4775e817cc15SEmil Constantinescu { 4776e817cc15SEmil Constantinescu PetscFunctionBegin; 4777e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4778e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 4779e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 4780e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4781e817cc15SEmil Constantinescu } 4782e817cc15SEmil Constantinescu 4783e817cc15SEmil Constantinescu #undef __FUNCT__ 4784e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType" 4785e817cc15SEmil Constantinescu /*@ 4786e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 4787e817cc15SEmil Constantinescu 4788e817cc15SEmil Constantinescu Not Collective 4789e817cc15SEmil Constantinescu 4790e817cc15SEmil Constantinescu Input Parameter: 4791e817cc15SEmil Constantinescu + ts - the TS context 47921297b224SEmil Constantinescu - equation_type - see TSEquationType 4793e817cc15SEmil Constantinescu 4794e817cc15SEmil Constantinescu Level: advanced 4795e817cc15SEmil Constantinescu 4796e817cc15SEmil Constantinescu .keywords: TS, equation type 4797e817cc15SEmil Constantinescu 4798e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 4799e817cc15SEmil Constantinescu @*/ 4800e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4801e817cc15SEmil Constantinescu { 4802e817cc15SEmil Constantinescu PetscFunctionBegin; 4803e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4804e817cc15SEmil Constantinescu ts->equation_type = equation_type; 4805e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4806e817cc15SEmil Constantinescu } 48070026cea9SSean Farley 48084af1b03aSJed Brown #undef __FUNCT__ 48094af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 48104af1b03aSJed Brown /*@ 48114af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 48124af1b03aSJed Brown 48134af1b03aSJed Brown Not Collective 48144af1b03aSJed Brown 48154af1b03aSJed Brown Input Parameter: 48164af1b03aSJed Brown . ts - the TS context 48174af1b03aSJed Brown 48184af1b03aSJed Brown Output Parameter: 48194af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 48204af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 48214af1b03aSJed Brown 4822487e0bb9SJed Brown Level: beginner 48234af1b03aSJed Brown 4824cd652676SJed Brown Notes: 4825cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 48264af1b03aSJed Brown 48274af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 48284af1b03aSJed Brown 48294af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 48304af1b03aSJed Brown @*/ 48314af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 48324af1b03aSJed Brown { 48334af1b03aSJed Brown PetscFunctionBegin; 48344af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 48354af1b03aSJed Brown PetscValidPointer(reason,2); 48364af1b03aSJed Brown *reason = ts->reason; 48374af1b03aSJed Brown PetscFunctionReturn(0); 48384af1b03aSJed Brown } 48394af1b03aSJed Brown 4840fb1732b5SBarry Smith #undef __FUNCT__ 4841d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 4842d6ad946cSShri Abhyankar /*@ 4843d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4844d6ad946cSShri Abhyankar 4845d6ad946cSShri Abhyankar Not Collective 4846d6ad946cSShri Abhyankar 4847d6ad946cSShri Abhyankar Input Parameter: 4848d6ad946cSShri Abhyankar + ts - the TS context 4849d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4850d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4851d6ad946cSShri Abhyankar 4852f5abba47SShri Abhyankar Level: advanced 4853d6ad946cSShri Abhyankar 4854d6ad946cSShri Abhyankar Notes: 4855d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 4856d6ad946cSShri Abhyankar 4857d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 4858d6ad946cSShri Abhyankar 4859d6ad946cSShri Abhyankar .seealso: TSConvergedReason 4860d6ad946cSShri Abhyankar @*/ 4861d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4862d6ad946cSShri Abhyankar { 4863d6ad946cSShri Abhyankar PetscFunctionBegin; 4864d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4865d6ad946cSShri Abhyankar ts->reason = reason; 4866d6ad946cSShri Abhyankar PetscFunctionReturn(0); 4867d6ad946cSShri Abhyankar } 4868d6ad946cSShri Abhyankar 4869d6ad946cSShri Abhyankar #undef __FUNCT__ 4870cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 4871cc708dedSBarry Smith /*@ 4872cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 4873cc708dedSBarry Smith 4874cc708dedSBarry Smith Not Collective 4875cc708dedSBarry Smith 4876cc708dedSBarry Smith Input Parameter: 4877cc708dedSBarry Smith . ts - the TS context 4878cc708dedSBarry Smith 4879cc708dedSBarry Smith Output Parameter: 4880cc708dedSBarry Smith . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 4881cc708dedSBarry Smith 4882487e0bb9SJed Brown Level: beginner 4883cc708dedSBarry Smith 4884cc708dedSBarry Smith Notes: 4885cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 4886cc708dedSBarry Smith 4887cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 4888cc708dedSBarry Smith 4889cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 4890cc708dedSBarry Smith @*/ 4891cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 4892cc708dedSBarry Smith { 4893cc708dedSBarry Smith PetscFunctionBegin; 4894cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4895cc708dedSBarry Smith PetscValidPointer(ftime,2); 4896cc708dedSBarry Smith *ftime = ts->solvetime; 4897cc708dedSBarry Smith PetscFunctionReturn(0); 4898cc708dedSBarry Smith } 4899cc708dedSBarry Smith 4900cc708dedSBarry Smith #undef __FUNCT__ 49012c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps" 49022c18e0fdSBarry Smith /*@ 49032c18e0fdSBarry Smith TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 49042c18e0fdSBarry Smith 49052c18e0fdSBarry Smith Not Collective 49062c18e0fdSBarry Smith 49072c18e0fdSBarry Smith Input Parameter: 49082c18e0fdSBarry Smith . ts - the TS context 49092c18e0fdSBarry Smith 49102c18e0fdSBarry Smith Output Parameter: 49112c18e0fdSBarry Smith . steps - the number of steps 49122c18e0fdSBarry Smith 49132c18e0fdSBarry Smith Level: beginner 49142c18e0fdSBarry Smith 49152c18e0fdSBarry Smith Notes: 49162c18e0fdSBarry Smith Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 49172c18e0fdSBarry Smith 49182c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test 49192c18e0fdSBarry Smith 49202c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 49212c18e0fdSBarry Smith @*/ 49222c18e0fdSBarry Smith PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 49232c18e0fdSBarry Smith { 49242c18e0fdSBarry Smith PetscFunctionBegin; 49252c18e0fdSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 49262c18e0fdSBarry Smith PetscValidPointer(steps,2); 49272c18e0fdSBarry Smith *steps = ts->total_steps; 49282c18e0fdSBarry Smith PetscFunctionReturn(0); 49292c18e0fdSBarry Smith } 49302c18e0fdSBarry Smith 49312c18e0fdSBarry Smith #undef __FUNCT__ 49325ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 49339f67acb7SJed Brown /*@ 49345ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 49359f67acb7SJed Brown used by the time integrator. 49369f67acb7SJed Brown 49379f67acb7SJed Brown Not Collective 49389f67acb7SJed Brown 49399f67acb7SJed Brown Input Parameter: 49409f67acb7SJed Brown . ts - TS context 49419f67acb7SJed Brown 49429f67acb7SJed Brown Output Parameter: 49439f67acb7SJed Brown . nits - number of nonlinear iterations 49449f67acb7SJed Brown 49459f67acb7SJed Brown Notes: 49469f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 49479f67acb7SJed Brown 49489f67acb7SJed Brown Level: intermediate 49499f67acb7SJed Brown 49509f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 49519f67acb7SJed Brown 49525ef26d82SJed Brown .seealso: TSGetKSPIterations() 49539f67acb7SJed Brown @*/ 49545ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 49559f67acb7SJed Brown { 49569f67acb7SJed Brown PetscFunctionBegin; 49579f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 49589f67acb7SJed Brown PetscValidIntPointer(nits,2); 49595ef26d82SJed Brown *nits = ts->snes_its; 49609f67acb7SJed Brown PetscFunctionReturn(0); 49619f67acb7SJed Brown } 49629f67acb7SJed Brown 49639f67acb7SJed Brown #undef __FUNCT__ 49645ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 49659f67acb7SJed Brown /*@ 49665ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 49679f67acb7SJed Brown used by the time integrator. 49689f67acb7SJed Brown 49699f67acb7SJed Brown Not Collective 49709f67acb7SJed Brown 49719f67acb7SJed Brown Input Parameter: 49729f67acb7SJed Brown . ts - TS context 49739f67acb7SJed Brown 49749f67acb7SJed Brown Output Parameter: 49759f67acb7SJed Brown . lits - number of linear iterations 49769f67acb7SJed Brown 49779f67acb7SJed Brown Notes: 49789f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 49799f67acb7SJed Brown 49809f67acb7SJed Brown Level: intermediate 49819f67acb7SJed Brown 49829f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 49839f67acb7SJed Brown 49845ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 49859f67acb7SJed Brown @*/ 49865ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 49879f67acb7SJed Brown { 49889f67acb7SJed Brown PetscFunctionBegin; 49899f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 49909f67acb7SJed Brown PetscValidIntPointer(lits,2); 49915ef26d82SJed Brown *lits = ts->ksp_its; 49929f67acb7SJed Brown PetscFunctionReturn(0); 49939f67acb7SJed Brown } 49949f67acb7SJed Brown 49959f67acb7SJed Brown #undef __FUNCT__ 4996cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 4997cef5090cSJed Brown /*@ 4998cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4999cef5090cSJed Brown 5000cef5090cSJed Brown Not Collective 5001cef5090cSJed Brown 5002cef5090cSJed Brown Input Parameter: 5003cef5090cSJed Brown . ts - TS context 5004cef5090cSJed Brown 5005cef5090cSJed Brown Output Parameter: 5006cef5090cSJed Brown . rejects - number of steps rejected 5007cef5090cSJed Brown 5008cef5090cSJed Brown Notes: 5009cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5010cef5090cSJed Brown 5011cef5090cSJed Brown Level: intermediate 5012cef5090cSJed Brown 5013cef5090cSJed Brown .keywords: TS, get, number 5014cef5090cSJed Brown 50155ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 5016cef5090cSJed Brown @*/ 5017cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 5018cef5090cSJed Brown { 5019cef5090cSJed Brown PetscFunctionBegin; 5020cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5021cef5090cSJed Brown PetscValidIntPointer(rejects,2); 5022cef5090cSJed Brown *rejects = ts->reject; 5023cef5090cSJed Brown PetscFunctionReturn(0); 5024cef5090cSJed Brown } 5025cef5090cSJed Brown 5026cef5090cSJed Brown #undef __FUNCT__ 5027cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 5028cef5090cSJed Brown /*@ 5029cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 5030cef5090cSJed Brown 5031cef5090cSJed Brown Not Collective 5032cef5090cSJed Brown 5033cef5090cSJed Brown Input Parameter: 5034cef5090cSJed Brown . ts - TS context 5035cef5090cSJed Brown 5036cef5090cSJed Brown Output Parameter: 5037cef5090cSJed Brown . fails - number of failed nonlinear solves 5038cef5090cSJed Brown 5039cef5090cSJed Brown Notes: 5040cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5041cef5090cSJed Brown 5042cef5090cSJed Brown Level: intermediate 5043cef5090cSJed Brown 5044cef5090cSJed Brown .keywords: TS, get, number 5045cef5090cSJed Brown 50465ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 5047cef5090cSJed Brown @*/ 5048cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 5049cef5090cSJed Brown { 5050cef5090cSJed Brown PetscFunctionBegin; 5051cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5052cef5090cSJed Brown PetscValidIntPointer(fails,2); 5053cef5090cSJed Brown *fails = ts->num_snes_failures; 5054cef5090cSJed Brown PetscFunctionReturn(0); 5055cef5090cSJed Brown } 5056cef5090cSJed Brown 5057cef5090cSJed Brown #undef __FUNCT__ 5058cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 5059cef5090cSJed Brown /*@ 5060cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 5061cef5090cSJed Brown 5062cef5090cSJed Brown Not Collective 5063cef5090cSJed Brown 5064cef5090cSJed Brown Input Parameter: 5065cef5090cSJed Brown + ts - TS context 5066cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 5067cef5090cSJed Brown 5068cef5090cSJed Brown Notes: 5069cef5090cSJed Brown The counter is reset to zero for each step 5070cef5090cSJed Brown 5071cef5090cSJed Brown Options Database Key: 5072cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 5073cef5090cSJed Brown 5074cef5090cSJed Brown Level: intermediate 5075cef5090cSJed Brown 5076cef5090cSJed Brown .keywords: TS, set, maximum, number 5077cef5090cSJed Brown 50785ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5079cef5090cSJed Brown @*/ 5080cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 5081cef5090cSJed Brown { 5082cef5090cSJed Brown PetscFunctionBegin; 5083cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5084cef5090cSJed Brown ts->max_reject = rejects; 5085cef5090cSJed Brown PetscFunctionReturn(0); 5086cef5090cSJed Brown } 5087cef5090cSJed Brown 5088cef5090cSJed Brown #undef __FUNCT__ 5089cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 5090cef5090cSJed Brown /*@ 5091cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 5092cef5090cSJed Brown 5093cef5090cSJed Brown Not Collective 5094cef5090cSJed Brown 5095cef5090cSJed Brown Input Parameter: 5096cef5090cSJed Brown + ts - TS context 5097cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 5098cef5090cSJed Brown 5099cef5090cSJed Brown Notes: 5100cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 5101cef5090cSJed Brown 5102cef5090cSJed Brown Options Database Key: 5103cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 5104cef5090cSJed Brown 5105cef5090cSJed Brown Level: intermediate 5106cef5090cSJed Brown 5107cef5090cSJed Brown .keywords: TS, set, maximum, number 5108cef5090cSJed Brown 51095ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 5110cef5090cSJed Brown @*/ 5111cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 5112cef5090cSJed Brown { 5113cef5090cSJed Brown PetscFunctionBegin; 5114cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5115cef5090cSJed Brown ts->max_snes_failures = fails; 5116cef5090cSJed Brown PetscFunctionReturn(0); 5117cef5090cSJed Brown } 5118cef5090cSJed Brown 5119cef5090cSJed Brown #undef __FUNCT__ 51204e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails" 5121cef5090cSJed Brown /*@ 5122cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 5123cef5090cSJed Brown 5124cef5090cSJed Brown Not Collective 5125cef5090cSJed Brown 5126cef5090cSJed Brown Input Parameter: 5127cef5090cSJed Brown + ts - TS context 5128cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 5129cef5090cSJed Brown 5130cef5090cSJed Brown Options Database Key: 5131cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 5132cef5090cSJed Brown 5133cef5090cSJed Brown Level: intermediate 5134cef5090cSJed Brown 5135cef5090cSJed Brown .keywords: TS, set, error 5136cef5090cSJed Brown 51375ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5138cef5090cSJed Brown @*/ 5139cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 5140cef5090cSJed Brown { 5141cef5090cSJed Brown PetscFunctionBegin; 5142cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5143cef5090cSJed Brown ts->errorifstepfailed = err; 5144cef5090cSJed Brown PetscFunctionReturn(0); 5145cef5090cSJed Brown } 5146cef5090cSJed Brown 5147cef5090cSJed Brown #undef __FUNCT__ 5148fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSolution" 5149fb1732b5SBarry Smith /*@C 5150fde5950dSBarry 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 5151fb1732b5SBarry Smith 5152fb1732b5SBarry Smith Collective on TS 5153fb1732b5SBarry Smith 5154fb1732b5SBarry Smith Input Parameters: 5155fb1732b5SBarry Smith + ts - the TS context 5156fb1732b5SBarry Smith . step - current time-step 5157fb1732b5SBarry Smith . ptime - current time 51580910c330SBarry Smith . u - current state 5159fb1732b5SBarry Smith - viewer - binary viewer 5160fb1732b5SBarry Smith 5161fb1732b5SBarry Smith Level: intermediate 5162fb1732b5SBarry Smith 5163fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 5164fb1732b5SBarry Smith 5165fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5166fb1732b5SBarry Smith @*/ 5167fde5950dSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 5168fb1732b5SBarry Smith { 5169fb1732b5SBarry Smith PetscErrorCode ierr; 5170ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 5171fb1732b5SBarry Smith 5172fb1732b5SBarry Smith PetscFunctionBegin; 51730910c330SBarry Smith ierr = VecView(u,v);CHKERRQ(ierr); 5174ed81e22dSJed Brown PetscFunctionReturn(0); 5175ed81e22dSJed Brown } 5176ed81e22dSJed Brown 5177ed81e22dSJed Brown #undef __FUNCT__ 5178ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 5179ed81e22dSJed Brown /*@C 5180ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5181ed81e22dSJed Brown 5182ed81e22dSJed Brown Collective on TS 5183ed81e22dSJed Brown 5184ed81e22dSJed Brown Input Parameters: 5185ed81e22dSJed Brown + ts - the TS context 5186ed81e22dSJed Brown . step - current time-step 5187ed81e22dSJed Brown . ptime - current time 51880910c330SBarry Smith . u - current state 5189ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5190ed81e22dSJed Brown 5191ed81e22dSJed Brown Level: intermediate 5192ed81e22dSJed Brown 5193ed81e22dSJed Brown Notes: 5194ed81e22dSJed 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. 5195ed81e22dSJed Brown These are named according to the file name template. 5196ed81e22dSJed Brown 5197ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5198ed81e22dSJed Brown 5199ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5200ed81e22dSJed Brown 5201ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5202ed81e22dSJed Brown @*/ 52030910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5204ed81e22dSJed Brown { 5205ed81e22dSJed Brown PetscErrorCode ierr; 5206ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 5207ed81e22dSJed Brown PetscViewer viewer; 5208ed81e22dSJed Brown 5209ed81e22dSJed Brown PetscFunctionBegin; 52108caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5211ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 52120910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 5213ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5214ed81e22dSJed Brown PetscFunctionReturn(0); 5215ed81e22dSJed Brown } 5216ed81e22dSJed Brown 5217ed81e22dSJed Brown #undef __FUNCT__ 5218ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 5219ed81e22dSJed Brown /*@C 5220ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5221ed81e22dSJed Brown 5222ed81e22dSJed Brown Collective on TS 5223ed81e22dSJed Brown 5224ed81e22dSJed Brown Input Parameters: 5225ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5226ed81e22dSJed Brown 5227ed81e22dSJed Brown Level: intermediate 5228ed81e22dSJed Brown 5229ed81e22dSJed Brown Note: 5230ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5231ed81e22dSJed Brown 5232ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5233ed81e22dSJed Brown 5234ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5235ed81e22dSJed Brown @*/ 5236ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5237ed81e22dSJed Brown { 5238ed81e22dSJed Brown PetscErrorCode ierr; 5239ed81e22dSJed Brown 5240ed81e22dSJed Brown PetscFunctionBegin; 5241ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5242fb1732b5SBarry Smith PetscFunctionReturn(0); 5243fb1732b5SBarry Smith } 5244fb1732b5SBarry Smith 524584df9cb4SJed Brown #undef __FUNCT__ 5246552698daSJed Brown #define __FUNCT__ "TSGetAdapt" 524784df9cb4SJed Brown /*@ 5248552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 524984df9cb4SJed Brown 5250ed81e22dSJed Brown Collective on TS if controller has not been created yet 525184df9cb4SJed Brown 525284df9cb4SJed Brown Input Arguments: 5253ed81e22dSJed Brown . ts - time stepping context 525484df9cb4SJed Brown 525584df9cb4SJed Brown Output Arguments: 5256ed81e22dSJed Brown . adapt - adaptive controller 525784df9cb4SJed Brown 525884df9cb4SJed Brown Level: intermediate 525984df9cb4SJed Brown 5260ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 526184df9cb4SJed Brown @*/ 5262552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 526384df9cb4SJed Brown { 526484df9cb4SJed Brown PetscErrorCode ierr; 526584df9cb4SJed Brown 526684df9cb4SJed Brown PetscFunctionBegin; 526784df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5268ee346746SBarry Smith if (adapt) PetscValidPointer(adapt,2); 526984df9cb4SJed Brown if (!ts->adapt) { 5270ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 52713bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 52721c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 527384df9cb4SJed Brown } 5274ee346746SBarry Smith if (adapt) *adapt = ts->adapt; 527584df9cb4SJed Brown PetscFunctionReturn(0); 527684df9cb4SJed Brown } 5277d6ebe24aSShri Abhyankar 5278d6ebe24aSShri Abhyankar #undef __FUNCT__ 52791c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 52801c3436cfSJed Brown /*@ 52811c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 52821c3436cfSJed Brown 52831c3436cfSJed Brown Logically Collective 52841c3436cfSJed Brown 52851c3436cfSJed Brown Input Arguments: 52861c3436cfSJed Brown + ts - time integration context 52871c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 52880298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 52891c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 52900298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 52911c3436cfSJed Brown 5292a3cdaa26SBarry Smith Options Database keys: 5293a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5294a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5295a3cdaa26SBarry Smith 52963ff766beSShri Abhyankar Notes: 52973ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 52983ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 52993ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 53003ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 53013ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 53023ff766beSShri Abhyankar differential variables. 53033ff766beSShri Abhyankar 53041c3436cfSJed Brown Level: beginner 53051c3436cfSJed Brown 5306c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 53071c3436cfSJed Brown @*/ 53081c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 53091c3436cfSJed Brown { 53101c3436cfSJed Brown PetscErrorCode ierr; 53111c3436cfSJed Brown 53121c3436cfSJed Brown PetscFunctionBegin; 5313c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 53141c3436cfSJed Brown if (vatol) { 53151c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 53161c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 5317bbd56ea5SKarl Rupp 53181c3436cfSJed Brown ts->vatol = vatol; 53191c3436cfSJed Brown } 5320c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 53211c3436cfSJed Brown if (vrtol) { 53221c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 53231c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 5324bbd56ea5SKarl Rupp 53251c3436cfSJed Brown ts->vrtol = vrtol; 53261c3436cfSJed Brown } 53271c3436cfSJed Brown PetscFunctionReturn(0); 53281c3436cfSJed Brown } 53291c3436cfSJed Brown 53301c3436cfSJed Brown #undef __FUNCT__ 5331c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 5332c5033834SJed Brown /*@ 5333c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5334c5033834SJed Brown 5335c5033834SJed Brown Logically Collective 5336c5033834SJed Brown 5337c5033834SJed Brown Input Arguments: 5338c5033834SJed Brown . ts - time integration context 5339c5033834SJed Brown 5340c5033834SJed Brown Output Arguments: 53410298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 53420298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 53430298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 53440298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 5345c5033834SJed Brown 5346c5033834SJed Brown Level: beginner 5347c5033834SJed Brown 5348c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5349c5033834SJed Brown @*/ 5350c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5351c5033834SJed Brown { 5352c5033834SJed Brown PetscFunctionBegin; 5353c5033834SJed Brown if (atol) *atol = ts->atol; 5354c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5355c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5356c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 5357c5033834SJed Brown PetscFunctionReturn(0); 5358c5033834SJed Brown } 5359c5033834SJed Brown 5360c5033834SJed Brown #undef __FUNCT__ 53619c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2" 53629c6b16b5SShri Abhyankar /*@ 5363a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 53649c6b16b5SShri Abhyankar 53659c6b16b5SShri Abhyankar Collective on TS 53669c6b16b5SShri Abhyankar 53679c6b16b5SShri Abhyankar Input Arguments: 53689c6b16b5SShri Abhyankar + ts - time stepping context 5369a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5370a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 53719c6b16b5SShri Abhyankar 53729c6b16b5SShri Abhyankar Output Arguments: 53739c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 53749c6b16b5SShri Abhyankar 53759c6b16b5SShri Abhyankar Level: developer 53769c6b16b5SShri Abhyankar 5377deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 53789c6b16b5SShri Abhyankar @*/ 5379a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm) 53809c6b16b5SShri Abhyankar { 53819c6b16b5SShri Abhyankar PetscErrorCode ierr; 53829c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 53839c6b16b5SShri Abhyankar const PetscScalar *u,*y; 53849c6b16b5SShri Abhyankar PetscReal sum,gsum; 53859c6b16b5SShri Abhyankar PetscReal tol; 53869c6b16b5SShri Abhyankar 53879c6b16b5SShri Abhyankar PetscFunctionBegin; 53889c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5389a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5390a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5391a4868fbcSLisandro Dalcin PetscValidType(U,2); 5392a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5393a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5394a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5395a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 53969c6b16b5SShri Abhyankar 53979c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 53989c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 53999c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 54009c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 54019c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 54029c6b16b5SShri Abhyankar sum = 0.; 54039c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 54049c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 54059c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 54069c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 54079c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 54089c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54099c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 54109c6b16b5SShri Abhyankar } 54119c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 54129c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 54139c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 54149c6b16b5SShri Abhyankar const PetscScalar *atol; 54159c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 54169c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 54179c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54189c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 54199c6b16b5SShri Abhyankar } 54209c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 54219c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 54229c6b16b5SShri Abhyankar const PetscScalar *rtol; 54239c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 54249c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 54259c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54269c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 54279c6b16b5SShri Abhyankar } 54289c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 54299c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 54309c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 54319c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54329c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 54339c6b16b5SShri Abhyankar } 54349c6b16b5SShri Abhyankar } 54359c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 54369c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 54379c6b16b5SShri Abhyankar 5438b2566f29SBarry Smith ierr = MPIU_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 54399c6b16b5SShri Abhyankar *norm = PetscSqrtReal(gsum / N); 54409c6b16b5SShri Abhyankar 54419c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 54429c6b16b5SShri Abhyankar PetscFunctionReturn(0); 54439c6b16b5SShri Abhyankar } 54449c6b16b5SShri Abhyankar 54459c6b16b5SShri Abhyankar #undef __FUNCT__ 54469c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity" 54479c6b16b5SShri Abhyankar /*@ 5448a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 54499c6b16b5SShri Abhyankar 54509c6b16b5SShri Abhyankar Collective on TS 54519c6b16b5SShri Abhyankar 54529c6b16b5SShri Abhyankar Input Arguments: 54539c6b16b5SShri Abhyankar + ts - time stepping context 5454a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5455a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 54569c6b16b5SShri Abhyankar 54579c6b16b5SShri Abhyankar Output Arguments: 54589c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 54599c6b16b5SShri Abhyankar 54609c6b16b5SShri Abhyankar Level: developer 54619c6b16b5SShri Abhyankar 5462deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 54639c6b16b5SShri Abhyankar @*/ 5464a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm) 54659c6b16b5SShri Abhyankar { 54669c6b16b5SShri Abhyankar PetscErrorCode ierr; 54679c6b16b5SShri Abhyankar PetscInt i,n,N,rstart,k; 54689c6b16b5SShri Abhyankar const PetscScalar *u,*y; 54699c6b16b5SShri Abhyankar PetscReal max,gmax; 54709c6b16b5SShri Abhyankar PetscReal tol; 54719c6b16b5SShri Abhyankar 54729c6b16b5SShri Abhyankar PetscFunctionBegin; 54739c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5474a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5475a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5476a4868fbcSLisandro Dalcin PetscValidType(U,2); 5477a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5478a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5479a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5480a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 54819c6b16b5SShri Abhyankar 54829c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 54839c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 54849c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 54859c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 54869c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 54879c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 54889c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 54899c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 54909c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 54919c6b16b5SShri Abhyankar k = 0; 54929c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 54939c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 54949c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 54959c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54969c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 54979c6b16b5SShri Abhyankar } 54989c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 54999c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 55009c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 55019c6b16b5SShri Abhyankar const PetscScalar *atol; 55029c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 55039c6b16b5SShri Abhyankar k = 0; 55049c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 55059c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 55069c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 55079c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55089c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 55099c6b16b5SShri Abhyankar } 55109c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 55119c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 55129c6b16b5SShri Abhyankar const PetscScalar *rtol; 55139c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 55149c6b16b5SShri Abhyankar k = 0; 55159c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 55169c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 55179c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 55189c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55199c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 55209c6b16b5SShri Abhyankar } 55219c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 55229c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 55239c6b16b5SShri Abhyankar k = 0; 55249c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 55259c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 55269c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 55279c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55289c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 55299c6b16b5SShri Abhyankar } 55309c6b16b5SShri Abhyankar } 55319c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 55329c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 55339c6b16b5SShri Abhyankar 5534b2566f29SBarry Smith ierr = MPIU_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 55359c6b16b5SShri Abhyankar *norm = gmax; 55369c6b16b5SShri Abhyankar 55379c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 55389c6b16b5SShri Abhyankar PetscFunctionReturn(0); 55399c6b16b5SShri Abhyankar } 55409c6b16b5SShri Abhyankar 55419c6b16b5SShri Abhyankar #undef __FUNCT__ 55427619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm" 55431c3436cfSJed Brown /*@ 5544a4868fbcSLisandro Dalcin TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors 55451c3436cfSJed Brown 55461c3436cfSJed Brown Collective on TS 55471c3436cfSJed Brown 55481c3436cfSJed Brown Input Arguments: 55491c3436cfSJed Brown + ts - time stepping context 5550a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5551a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5552a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 55537619abb3SShri 55541c3436cfSJed Brown Output Arguments: 55551c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 55561c3436cfSJed Brown 5557a4868fbcSLisandro Dalcin 5558a4868fbcSLisandro Dalcin Options Database Keys: 5559a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5560a4868fbcSLisandro Dalcin 55611c3436cfSJed Brown Level: developer 55621c3436cfSJed Brown 5563deea92deSShri .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 55641c3436cfSJed Brown @*/ 5565a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm) 55661c3436cfSJed Brown { 55678beabaa1SBarry Smith PetscErrorCode ierr; 55681c3436cfSJed Brown 55691c3436cfSJed Brown PetscFunctionBegin; 5570a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 5571a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNorm2(ts,U,Y,norm);CHKERRQ(ierr); 5572a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 5573a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm);CHKERRQ(ierr); 5574a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 55751c3436cfSJed Brown PetscFunctionReturn(0); 55761c3436cfSJed Brown } 55771c3436cfSJed Brown 55781c3436cfSJed Brown #undef __FUNCT__ 55798d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 55808d59e960SJed Brown /*@ 55818d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 55828d59e960SJed Brown 55838d59e960SJed Brown Logically Collective on TS 55848d59e960SJed Brown 55858d59e960SJed Brown Input Arguments: 55868d59e960SJed Brown + ts - time stepping context 55878d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 55888d59e960SJed Brown 55898d59e960SJed Brown Note: 55908d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 55918d59e960SJed Brown 55928d59e960SJed Brown Level: intermediate 55938d59e960SJed Brown 55948d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 55958d59e960SJed Brown @*/ 55968d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 55978d59e960SJed Brown { 55988d59e960SJed Brown PetscFunctionBegin; 55998d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 56008d59e960SJed Brown ts->cfltime_local = cfltime; 56018d59e960SJed Brown ts->cfltime = -1.; 56028d59e960SJed Brown PetscFunctionReturn(0); 56038d59e960SJed Brown } 56048d59e960SJed Brown 56058d59e960SJed Brown #undef __FUNCT__ 56068d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 56078d59e960SJed Brown /*@ 56088d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 56098d59e960SJed Brown 56108d59e960SJed Brown Collective on TS 56118d59e960SJed Brown 56128d59e960SJed Brown Input Arguments: 56138d59e960SJed Brown . ts - time stepping context 56148d59e960SJed Brown 56158d59e960SJed Brown Output Arguments: 56168d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 56178d59e960SJed Brown 56188d59e960SJed Brown Level: advanced 56198d59e960SJed Brown 56208d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 56218d59e960SJed Brown @*/ 56228d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 56238d59e960SJed Brown { 56248d59e960SJed Brown PetscErrorCode ierr; 56258d59e960SJed Brown 56268d59e960SJed Brown PetscFunctionBegin; 56278d59e960SJed Brown if (ts->cfltime < 0) { 5628b2566f29SBarry Smith ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 56298d59e960SJed Brown } 56308d59e960SJed Brown *cfltime = ts->cfltime; 56318d59e960SJed Brown PetscFunctionReturn(0); 56328d59e960SJed Brown } 56338d59e960SJed Brown 56348d59e960SJed Brown #undef __FUNCT__ 5635d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 5636d6ebe24aSShri Abhyankar /*@ 5637d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5638d6ebe24aSShri Abhyankar 5639d6ebe24aSShri Abhyankar Input Parameters: 5640d6ebe24aSShri Abhyankar . ts - the TS context. 5641d6ebe24aSShri Abhyankar . xl - lower bound. 5642d6ebe24aSShri Abhyankar . xu - upper bound. 5643d6ebe24aSShri Abhyankar 5644d6ebe24aSShri Abhyankar Notes: 5645d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 5646e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 5647d6ebe24aSShri Abhyankar 56482bd2b0e6SSatish Balay Level: advanced 56492bd2b0e6SSatish Balay 5650d6ebe24aSShri Abhyankar @*/ 5651d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5652d6ebe24aSShri Abhyankar { 5653d6ebe24aSShri Abhyankar PetscErrorCode ierr; 5654d6ebe24aSShri Abhyankar SNES snes; 5655d6ebe24aSShri Abhyankar 5656d6ebe24aSShri Abhyankar PetscFunctionBegin; 5657d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 5658d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 5659d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 5660d6ebe24aSShri Abhyankar } 5661d6ebe24aSShri Abhyankar 5662325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 5663c6db04a5SJed Brown #include <mex.h> 5664325fc9f4SBarry Smith 5665325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 5666325fc9f4SBarry Smith 5667325fc9f4SBarry Smith #undef __FUNCT__ 5668325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 5669325fc9f4SBarry Smith /* 5670325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 5671325fc9f4SBarry Smith TSSetFunctionMatlab(). 5672325fc9f4SBarry Smith 5673325fc9f4SBarry Smith Collective on TS 5674325fc9f4SBarry Smith 5675325fc9f4SBarry Smith Input Parameters: 5676325fc9f4SBarry Smith + snes - the TS context 56770910c330SBarry Smith - u - input vector 5678325fc9f4SBarry Smith 5679325fc9f4SBarry Smith Output Parameter: 5680325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 5681325fc9f4SBarry Smith 5682325fc9f4SBarry Smith Notes: 5683325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 5684325fc9f4SBarry Smith implementations, so most users would not generally call this routine 5685325fc9f4SBarry Smith themselves. 5686325fc9f4SBarry Smith 5687325fc9f4SBarry Smith Level: developer 5688325fc9f4SBarry Smith 5689325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5690325fc9f4SBarry Smith 5691325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5692325fc9f4SBarry Smith */ 56930910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 5694325fc9f4SBarry Smith { 5695325fc9f4SBarry Smith PetscErrorCode ierr; 5696325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5697325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 5698325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 5699325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 5700325fc9f4SBarry Smith 5701325fc9f4SBarry Smith PetscFunctionBegin; 5702325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 57030910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 57040910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 5705325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 57060910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 5707325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 5708325fc9f4SBarry Smith 5709325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 57100910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 57110910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 57120910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 5713bbd56ea5SKarl Rupp 5714325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5715325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 5716325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5717325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5718325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 5719325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 5720325fc9f4SBarry Smith prhs[6] = sctx->ctx; 5721325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 5722325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5723325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5724325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5725325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5726325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5727325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5728325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5729325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5730325fc9f4SBarry Smith PetscFunctionReturn(0); 5731325fc9f4SBarry Smith } 5732325fc9f4SBarry Smith 5733325fc9f4SBarry Smith 5734325fc9f4SBarry Smith #undef __FUNCT__ 5735325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 5736325fc9f4SBarry Smith /* 5737325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 5738325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 5739e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 5740325fc9f4SBarry Smith 5741325fc9f4SBarry Smith Logically Collective on TS 5742325fc9f4SBarry Smith 5743325fc9f4SBarry Smith Input Parameters: 5744325fc9f4SBarry Smith + ts - the TS context 5745325fc9f4SBarry Smith - func - function evaluation routine 5746325fc9f4SBarry Smith 5747325fc9f4SBarry Smith Calling sequence of func: 57480910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 5749325fc9f4SBarry Smith 5750325fc9f4SBarry Smith Level: beginner 5751325fc9f4SBarry Smith 5752325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5753325fc9f4SBarry Smith 5754325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5755325fc9f4SBarry Smith */ 5756cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 5757325fc9f4SBarry Smith { 5758325fc9f4SBarry Smith PetscErrorCode ierr; 5759325fc9f4SBarry Smith TSMatlabContext *sctx; 5760325fc9f4SBarry Smith 5761325fc9f4SBarry Smith PetscFunctionBegin; 5762325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5763325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5764325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5765325fc9f4SBarry Smith /* 5766325fc9f4SBarry Smith This should work, but it doesn't 5767325fc9f4SBarry Smith sctx->ctx = ctx; 5768325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5769325fc9f4SBarry Smith */ 5770325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5771bbd56ea5SKarl Rupp 57720298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 5773325fc9f4SBarry Smith PetscFunctionReturn(0); 5774325fc9f4SBarry Smith } 5775325fc9f4SBarry Smith 5776325fc9f4SBarry Smith #undef __FUNCT__ 5777325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 5778325fc9f4SBarry Smith /* 5779325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 5780325fc9f4SBarry Smith TSSetJacobianMatlab(). 5781325fc9f4SBarry Smith 5782325fc9f4SBarry Smith Collective on TS 5783325fc9f4SBarry Smith 5784325fc9f4SBarry Smith Input Parameters: 5785cdcf91faSSean Farley + ts - the TS context 57860910c330SBarry Smith . u - input vector 5787325fc9f4SBarry Smith . A, B - the matrices 5788325fc9f4SBarry Smith - ctx - user context 5789325fc9f4SBarry Smith 5790325fc9f4SBarry Smith Level: developer 5791325fc9f4SBarry Smith 5792325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5793325fc9f4SBarry Smith 5794325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5795325fc9f4SBarry Smith @*/ 5796f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 5797325fc9f4SBarry Smith { 5798325fc9f4SBarry Smith PetscErrorCode ierr; 5799325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5800325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 5801325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 5802325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 5803325fc9f4SBarry Smith 5804325fc9f4SBarry Smith PetscFunctionBegin; 5805cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 58060910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 5807325fc9f4SBarry Smith 58080910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 5809325fc9f4SBarry Smith 5810cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 58110910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 58120910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 58130910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 58140910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 5815bbd56ea5SKarl Rupp 5816325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5817325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 5818325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5819325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5820325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 5821325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 5822325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 5823325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 5824325fc9f4SBarry Smith prhs[8] = sctx->ctx; 5825325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 5826325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5827325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5828325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5829325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5830325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5831325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5832325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5833325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 5834325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 5835325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5836325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 5837325fc9f4SBarry Smith PetscFunctionReturn(0); 5838325fc9f4SBarry Smith } 5839325fc9f4SBarry Smith 5840325fc9f4SBarry Smith 5841325fc9f4SBarry Smith #undef __FUNCT__ 5842325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 5843325fc9f4SBarry Smith /* 5844325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 5845e3c5b3baSBarry 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 5846325fc9f4SBarry Smith 5847325fc9f4SBarry Smith Logically Collective on TS 5848325fc9f4SBarry Smith 5849325fc9f4SBarry Smith Input Parameters: 5850cdcf91faSSean Farley + ts - the TS context 5851325fc9f4SBarry Smith . A,B - Jacobian matrices 5852325fc9f4SBarry Smith . func - function evaluation routine 5853325fc9f4SBarry Smith - ctx - user context 5854325fc9f4SBarry Smith 5855325fc9f4SBarry Smith Calling sequence of func: 58560910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 5857325fc9f4SBarry Smith 5858325fc9f4SBarry Smith 5859325fc9f4SBarry Smith Level: developer 5860325fc9f4SBarry Smith 5861325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5862325fc9f4SBarry Smith 5863325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5864325fc9f4SBarry Smith */ 5865cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 5866325fc9f4SBarry Smith { 5867325fc9f4SBarry Smith PetscErrorCode ierr; 5868325fc9f4SBarry Smith TSMatlabContext *sctx; 5869325fc9f4SBarry Smith 5870325fc9f4SBarry Smith PetscFunctionBegin; 5871325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5872325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5873325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5874325fc9f4SBarry Smith /* 5875325fc9f4SBarry Smith This should work, but it doesn't 5876325fc9f4SBarry Smith sctx->ctx = ctx; 5877325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5878325fc9f4SBarry Smith */ 5879325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5880bbd56ea5SKarl Rupp 5881cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 5882325fc9f4SBarry Smith PetscFunctionReturn(0); 5883325fc9f4SBarry Smith } 5884325fc9f4SBarry Smith 5885b5b1a830SBarry Smith #undef __FUNCT__ 5886b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 5887b5b1a830SBarry Smith /* 5888b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 5889b5b1a830SBarry Smith 5890b5b1a830SBarry Smith Collective on TS 5891b5b1a830SBarry Smith 5892b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5893b5b1a830SBarry Smith @*/ 58940910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 5895b5b1a830SBarry Smith { 5896b5b1a830SBarry Smith PetscErrorCode ierr; 5897b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5898a530c242SBarry Smith int nlhs = 1,nrhs = 6; 5899b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 5900b5b1a830SBarry Smith long long int lx = 0,ls = 0; 5901b5b1a830SBarry Smith 5902b5b1a830SBarry Smith PetscFunctionBegin; 5903cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 59040910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 5905b5b1a830SBarry Smith 5906cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 59070910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 5908bbd56ea5SKarl Rupp 5909b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5910b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 5911b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 5912b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 5913b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 5914b5b1a830SBarry Smith prhs[5] = sctx->ctx; 5915b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 5916b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5917b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 5918b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 5919b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 5920b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 5921b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 5922b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 5923b5b1a830SBarry Smith PetscFunctionReturn(0); 5924b5b1a830SBarry Smith } 5925b5b1a830SBarry Smith 5926b5b1a830SBarry Smith 5927b5b1a830SBarry Smith #undef __FUNCT__ 5928b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 5929b5b1a830SBarry Smith /* 5930b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 5931b5b1a830SBarry Smith 5932b5b1a830SBarry Smith Level: developer 5933b5b1a830SBarry Smith 5934b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 5935b5b1a830SBarry Smith 5936b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5937b5b1a830SBarry Smith */ 5938cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 5939b5b1a830SBarry Smith { 5940b5b1a830SBarry Smith PetscErrorCode ierr; 5941b5b1a830SBarry Smith TSMatlabContext *sctx; 5942b5b1a830SBarry Smith 5943b5b1a830SBarry Smith PetscFunctionBegin; 5944b5b1a830SBarry Smith /* currently sctx is memory bleed */ 5945b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5946b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5947b5b1a830SBarry Smith /* 5948b5b1a830SBarry Smith This should work, but it doesn't 5949b5b1a830SBarry Smith sctx->ctx = ctx; 5950b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5951b5b1a830SBarry Smith */ 5952b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5953bbd56ea5SKarl Rupp 59540298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 5955b5b1a830SBarry Smith PetscFunctionReturn(0); 5956b5b1a830SBarry Smith } 5957325fc9f4SBarry Smith #endif 5958b3603a34SBarry Smith 5959b3603a34SBarry Smith #undef __FUNCT__ 59604f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 5961b3603a34SBarry Smith /*@C 59624f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 5963b3603a34SBarry Smith in a time based line graph 5964b3603a34SBarry Smith 5965b3603a34SBarry Smith Collective on TS 5966b3603a34SBarry Smith 5967b3603a34SBarry Smith Input Parameters: 5968b3603a34SBarry Smith + ts - the TS context 5969b3603a34SBarry Smith . step - current time-step 5970b3603a34SBarry Smith . ptime - current time 59717db568b7SBarry Smith . u - current solution 59727db568b7SBarry Smith - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 5973b3603a34SBarry Smith 5974b3d3934dSBarry Smith Options Database: 59759ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 5976b3d3934dSBarry Smith 5977b3603a34SBarry Smith Level: intermediate 5978b3603a34SBarry Smith 59796934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component solutions in a separate window 5980b3603a34SBarry Smith 5981b3603a34SBarry Smith .keywords: TS, vector, monitor, view 5982b3603a34SBarry Smith 59837db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 59847db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 59857db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 59867db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 5987b3603a34SBarry Smith @*/ 59887db568b7SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 5989b3603a34SBarry Smith { 5990b3603a34SBarry Smith PetscErrorCode ierr; 59917db568b7SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 5992b3603a34SBarry Smith const PetscScalar *yy; 599380666b62SBarry Smith Vec v; 5994b3603a34SBarry Smith 5995b3603a34SBarry Smith PetscFunctionBegin; 599658ff32f7SBarry Smith if (!step) { 5997a9f9c1f6SBarry Smith PetscDrawAxis axis; 59986934998bSLisandro Dalcin PetscInt dim; 5999a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6000a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 6001387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 6002387f4636SBarry Smith char **displaynames; 6003387f4636SBarry Smith PetscBool flg; 6004387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6005387f4636SBarry Smith ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr); 6006387f4636SBarry Smith ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 6007c5929fdfSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 6008387f4636SBarry Smith if (flg) { 6009a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 6010387f4636SBarry Smith } 6011387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 6012387f4636SBarry Smith } 6013387f4636SBarry Smith if (ctx->displaynames) { 6014387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 6015387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 6016387f4636SBarry Smith } else if (ctx->names) { 60170910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 60180b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6019387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 6020b0bc92c6SBarry Smith } else { 6021b0bc92c6SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6022b0bc92c6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6023387f4636SBarry Smith } 60240b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 602558ff32f7SBarry Smith } 60266934998bSLisandro Dalcin 60276934998bSLisandro Dalcin if (!ctx->transform) v = u; 60286934998bSLisandro Dalcin else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);} 602980666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 60306934998bSLisandro Dalcin if (ctx->displaynames) { 60316934998bSLisandro Dalcin PetscInt i; 60326934998bSLisandro Dalcin for (i=0; i<ctx->ndisplayvariables; i++) 60336934998bSLisandro Dalcin ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]); 60346934998bSLisandro Dalcin ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 60356934998bSLisandro Dalcin } else { 6036e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6037e3efe391SJed Brown PetscInt i,n; 60386934998bSLisandro Dalcin PetscReal *yreal; 603980666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 6040785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6041e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 60420b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6043e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6044e3efe391SJed Brown #else 60450b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6046e3efe391SJed Brown #endif 604780666b62SBarry Smith } 60486934998bSLisandro Dalcin ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 60496934998bSLisandro Dalcin if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);} 60506934998bSLisandro Dalcin 6051b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 60520b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 60536934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 60543923b477SBarry Smith } 6055b3603a34SBarry Smith PetscFunctionReturn(0); 6056b3603a34SBarry Smith } 6057b3603a34SBarry Smith 6058387f4636SBarry Smith 6059b3603a34SBarry Smith #undef __FUNCT__ 606031152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames" 6061b037adc7SBarry Smith /*@C 606231152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6063b037adc7SBarry Smith 6064b037adc7SBarry Smith Collective on TS 6065b037adc7SBarry Smith 6066b037adc7SBarry Smith Input Parameters: 6067b037adc7SBarry Smith + ts - the TS context 6068b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 6069b037adc7SBarry Smith 6070b037adc7SBarry Smith Level: intermediate 6071b037adc7SBarry Smith 60727db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 60737db568b7SBarry Smith 6074b037adc7SBarry Smith .keywords: TS, vector, monitor, view 6075b037adc7SBarry Smith 6076a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 6077b037adc7SBarry Smith @*/ 607831152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 6079b037adc7SBarry Smith { 6080b037adc7SBarry Smith PetscErrorCode ierr; 6081b037adc7SBarry Smith PetscInt i; 6082b037adc7SBarry Smith 6083b037adc7SBarry Smith PetscFunctionBegin; 6084b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6085b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 60865537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 6087b3d3934dSBarry Smith break; 6088b3d3934dSBarry Smith } 6089b3d3934dSBarry Smith } 6090b3d3934dSBarry Smith PetscFunctionReturn(0); 6091b3d3934dSBarry Smith } 6092b3d3934dSBarry Smith 6093b3d3934dSBarry Smith #undef __FUNCT__ 6094e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames" 6095e673d494SBarry Smith /*@C 6096e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6097e673d494SBarry Smith 6098e673d494SBarry Smith Collective on TS 6099e673d494SBarry Smith 6100e673d494SBarry Smith Input Parameters: 6101e673d494SBarry Smith + ts - the TS context 6102e673d494SBarry Smith - names - the names of the components, final string must be NULL 6103e673d494SBarry Smith 6104e673d494SBarry Smith Level: intermediate 6105e673d494SBarry Smith 6106e673d494SBarry Smith .keywords: TS, vector, monitor, view 6107e673d494SBarry Smith 6108a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 6109e673d494SBarry Smith @*/ 6110e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 6111e673d494SBarry Smith { 6112e673d494SBarry Smith PetscErrorCode ierr; 6113e673d494SBarry Smith 6114e673d494SBarry Smith PetscFunctionBegin; 6115e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 6116e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 6117e673d494SBarry Smith PetscFunctionReturn(0); 6118e673d494SBarry Smith } 6119e673d494SBarry Smith 6120e673d494SBarry Smith #undef __FUNCT__ 6121b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames" 6122b3d3934dSBarry Smith /*@C 6123b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 6124b3d3934dSBarry Smith 6125b3d3934dSBarry Smith Collective on TS 6126b3d3934dSBarry Smith 6127b3d3934dSBarry Smith Input Parameter: 6128b3d3934dSBarry Smith . ts - the TS context 6129b3d3934dSBarry Smith 6130b3d3934dSBarry Smith Output Parameter: 6131b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 6132b3d3934dSBarry Smith 6133b3d3934dSBarry Smith Level: intermediate 6134b3d3934dSBarry Smith 61357db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 61367db568b7SBarry Smith 6137b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6138b3d3934dSBarry Smith 6139b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6140b3d3934dSBarry Smith @*/ 6141b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 6142b3d3934dSBarry Smith { 6143b3d3934dSBarry Smith PetscInt i; 6144b3d3934dSBarry Smith 6145b3d3934dSBarry Smith PetscFunctionBegin; 6146b3d3934dSBarry Smith *names = NULL; 6147b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6148b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 61495537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 6150b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 6151b3d3934dSBarry Smith break; 6152387f4636SBarry Smith } 6153387f4636SBarry Smith } 6154387f4636SBarry Smith PetscFunctionReturn(0); 6155387f4636SBarry Smith } 6156387f4636SBarry Smith 6157387f4636SBarry Smith #undef __FUNCT__ 6158a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables" 6159a66092f1SBarry Smith /*@C 6160a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 6161a66092f1SBarry Smith 6162a66092f1SBarry Smith Collective on TS 6163a66092f1SBarry Smith 6164a66092f1SBarry Smith Input Parameters: 6165a66092f1SBarry Smith + ctx - the TSMonitorLG context 6166a66092f1SBarry Smith . displaynames - the names of the components, final string must be NULL 6167a66092f1SBarry Smith 6168a66092f1SBarry Smith Level: intermediate 6169a66092f1SBarry Smith 6170a66092f1SBarry Smith .keywords: TS, vector, monitor, view 6171a66092f1SBarry Smith 6172a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6173a66092f1SBarry Smith @*/ 6174a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 6175a66092f1SBarry Smith { 6176a66092f1SBarry Smith PetscInt j = 0,k; 6177a66092f1SBarry Smith PetscErrorCode ierr; 6178a66092f1SBarry Smith 6179a66092f1SBarry Smith PetscFunctionBegin; 6180a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 6181a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 6182a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 6183a66092f1SBarry Smith while (displaynames[j]) j++; 6184a66092f1SBarry Smith ctx->ndisplayvariables = j; 6185a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 6186a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 6187a66092f1SBarry Smith j = 0; 6188a66092f1SBarry Smith while (displaynames[j]) { 6189a66092f1SBarry Smith k = 0; 6190a66092f1SBarry Smith while (ctx->names[k]) { 6191a66092f1SBarry Smith PetscBool flg; 6192a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 6193a66092f1SBarry Smith if (flg) { 6194a66092f1SBarry Smith ctx->displayvariables[j] = k; 6195a66092f1SBarry Smith break; 6196a66092f1SBarry Smith } 6197a66092f1SBarry Smith k++; 6198a66092f1SBarry Smith } 6199a66092f1SBarry Smith j++; 6200a66092f1SBarry Smith } 6201a66092f1SBarry Smith PetscFunctionReturn(0); 6202a66092f1SBarry Smith } 6203a66092f1SBarry Smith 6204a66092f1SBarry Smith 6205a66092f1SBarry Smith #undef __FUNCT__ 6206387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables" 6207387f4636SBarry Smith /*@C 6208387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 6209387f4636SBarry Smith 6210387f4636SBarry Smith Collective on TS 6211387f4636SBarry Smith 6212387f4636SBarry Smith Input Parameters: 6213387f4636SBarry Smith + ts - the TS context 6214387f4636SBarry Smith . displaynames - the names of the components, final string must be NULL 6215387f4636SBarry Smith 62167db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 62177db568b7SBarry Smith 6218387f4636SBarry Smith Level: intermediate 6219387f4636SBarry Smith 6220387f4636SBarry Smith .keywords: TS, vector, monitor, view 6221387f4636SBarry Smith 6222387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6223387f4636SBarry Smith @*/ 6224387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 6225387f4636SBarry Smith { 6226a66092f1SBarry Smith PetscInt i; 6227387f4636SBarry Smith PetscErrorCode ierr; 6228387f4636SBarry Smith 6229387f4636SBarry Smith PetscFunctionBegin; 6230387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6231387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 62325537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 6233b3d3934dSBarry Smith break; 6234b037adc7SBarry Smith } 6235b037adc7SBarry Smith } 6236b037adc7SBarry Smith PetscFunctionReturn(0); 6237b037adc7SBarry Smith } 6238b037adc7SBarry Smith 6239b037adc7SBarry Smith #undef __FUNCT__ 624080666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform" 624180666b62SBarry Smith /*@C 624280666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 624380666b62SBarry Smith 624480666b62SBarry Smith Collective on TS 624580666b62SBarry Smith 624680666b62SBarry Smith Input Parameters: 624780666b62SBarry Smith + ts - the TS context 624880666b62SBarry Smith . transform - the transform function 62497684fa3eSBarry Smith . destroy - function to destroy the optional context 625080666b62SBarry Smith - ctx - optional context used by transform function 625180666b62SBarry Smith 62527db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 62537db568b7SBarry Smith 625480666b62SBarry Smith Level: intermediate 625580666b62SBarry Smith 625680666b62SBarry Smith .keywords: TS, vector, monitor, view 625780666b62SBarry Smith 6258a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 625980666b62SBarry Smith @*/ 62607684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 626180666b62SBarry Smith { 626280666b62SBarry Smith PetscInt i; 6263a66092f1SBarry Smith PetscErrorCode ierr; 626480666b62SBarry Smith 626580666b62SBarry Smith PetscFunctionBegin; 626680666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 626780666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 62685537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 626980666b62SBarry Smith } 627080666b62SBarry Smith } 627180666b62SBarry Smith PetscFunctionReturn(0); 627280666b62SBarry Smith } 627380666b62SBarry Smith 627480666b62SBarry Smith #undef __FUNCT__ 6275e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform" 6276e673d494SBarry Smith /*@C 6277e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 6278e673d494SBarry Smith 6279e673d494SBarry Smith Collective on TSLGCtx 6280e673d494SBarry Smith 6281e673d494SBarry Smith Input Parameters: 6282e673d494SBarry Smith + ts - the TS context 6283e673d494SBarry Smith . transform - the transform function 62847684fa3eSBarry Smith . destroy - function to destroy the optional context 6285e673d494SBarry Smith - ctx - optional context used by transform function 6286e673d494SBarry Smith 6287e673d494SBarry Smith Level: intermediate 6288e673d494SBarry Smith 6289e673d494SBarry Smith .keywords: TS, vector, monitor, view 6290e673d494SBarry Smith 6291a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 6292e673d494SBarry Smith @*/ 62937684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 6294e673d494SBarry Smith { 6295e673d494SBarry Smith PetscFunctionBegin; 6296e673d494SBarry Smith ctx->transform = transform; 62977684fa3eSBarry Smith ctx->transformdestroy = destroy; 6298e673d494SBarry Smith ctx->transformctx = tctx; 6299e673d494SBarry Smith PetscFunctionReturn(0); 6300e673d494SBarry Smith } 6301e673d494SBarry Smith 6302b3603a34SBarry Smith #undef __FUNCT__ 63034f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 6304ef20d060SBarry Smith /*@C 63054f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 6306ef20d060SBarry Smith in a time based line graph 6307ef20d060SBarry Smith 6308ef20d060SBarry Smith Collective on TS 6309ef20d060SBarry Smith 6310ef20d060SBarry Smith Input Parameters: 6311ef20d060SBarry Smith + ts - the TS context 6312ef20d060SBarry Smith . step - current time-step 6313ef20d060SBarry Smith . ptime - current time 63147db568b7SBarry Smith . u - current solution 63157db568b7SBarry Smith - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 6316ef20d060SBarry Smith 6317ef20d060SBarry Smith Level: intermediate 6318ef20d060SBarry Smith 63196934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component errors in a separate window 6320abd5a294SJed Brown 6321abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 6322abd5a294SJed Brown 6323abd5a294SJed Brown Options Database Keys: 63244f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 6325ef20d060SBarry Smith 6326ef20d060SBarry Smith .keywords: TS, vector, monitor, view 6327ef20d060SBarry Smith 6328abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 6329ef20d060SBarry Smith @*/ 63300910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 6331ef20d060SBarry Smith { 6332ef20d060SBarry Smith PetscErrorCode ierr; 63330b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 6334ef20d060SBarry Smith const PetscScalar *yy; 6335ef20d060SBarry Smith Vec y; 6336ef20d060SBarry Smith 6337ef20d060SBarry Smith PetscFunctionBegin; 6338a9f9c1f6SBarry Smith if (!step) { 6339a9f9c1f6SBarry Smith PetscDrawAxis axis; 63406934998bSLisandro Dalcin PetscInt dim; 6341a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 63421ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 63430910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6344a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6345a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6346a9f9c1f6SBarry Smith } 63470910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 6348ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 63490910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 6350ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 6351e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6352e3efe391SJed Brown { 6353e3efe391SJed Brown PetscReal *yreal; 6354e3efe391SJed Brown PetscInt i,n; 6355e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 6356785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6357e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 63580b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6359e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6360e3efe391SJed Brown } 6361e3efe391SJed Brown #else 63620b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6363e3efe391SJed Brown #endif 6364ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 6365ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 6366b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 63670b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 63686934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 63693923b477SBarry Smith } 6370ef20d060SBarry Smith PetscFunctionReturn(0); 6371ef20d060SBarry Smith } 6372ef20d060SBarry Smith 6373201da799SBarry Smith #undef __FUNCT__ 6374201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 6375201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6376201da799SBarry Smith { 6377201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6378201da799SBarry Smith PetscReal x = ptime,y; 6379201da799SBarry Smith PetscErrorCode ierr; 6380201da799SBarry Smith PetscInt its; 6381201da799SBarry Smith 6382201da799SBarry Smith PetscFunctionBegin; 6383201da799SBarry Smith if (!n) { 6384201da799SBarry Smith PetscDrawAxis axis; 6385201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6386201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 6387201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6388201da799SBarry Smith ctx->snes_its = 0; 6389201da799SBarry Smith } 6390201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 6391201da799SBarry Smith y = its - ctx->snes_its; 6392201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 63933fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6394201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 63956934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 6396201da799SBarry Smith } 6397201da799SBarry Smith ctx->snes_its = its; 6398201da799SBarry Smith PetscFunctionReturn(0); 6399201da799SBarry Smith } 6400201da799SBarry Smith 6401201da799SBarry Smith #undef __FUNCT__ 6402201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 6403201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6404201da799SBarry Smith { 6405201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6406201da799SBarry Smith PetscReal x = ptime,y; 6407201da799SBarry Smith PetscErrorCode ierr; 6408201da799SBarry Smith PetscInt its; 6409201da799SBarry Smith 6410201da799SBarry Smith PetscFunctionBegin; 6411201da799SBarry Smith if (!n) { 6412201da799SBarry Smith PetscDrawAxis axis; 6413201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6414201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 6415201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6416201da799SBarry Smith ctx->ksp_its = 0; 6417201da799SBarry Smith } 6418201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 6419201da799SBarry Smith y = its - ctx->ksp_its; 6420201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 642199fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6422201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 64236934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 6424201da799SBarry Smith } 6425201da799SBarry Smith ctx->ksp_its = its; 6426201da799SBarry Smith PetscFunctionReturn(0); 6427201da799SBarry Smith } 6428f9c1d6abSBarry Smith 6429f9c1d6abSBarry Smith #undef __FUNCT__ 6430f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 6431f9c1d6abSBarry Smith /*@ 6432f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 6433f9c1d6abSBarry Smith 6434f9c1d6abSBarry Smith Collective on TS and Vec 6435f9c1d6abSBarry Smith 6436f9c1d6abSBarry Smith Input Parameters: 6437f9c1d6abSBarry Smith + ts - the TS context 6438f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 6439f9c1d6abSBarry Smith 6440f9c1d6abSBarry Smith Output Parameters: 6441f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 6442f9c1d6abSBarry Smith 6443f9c1d6abSBarry Smith Level: developer 6444f9c1d6abSBarry Smith 6445f9c1d6abSBarry Smith .keywords: TS, compute 6446f9c1d6abSBarry Smith 6447f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 6448f9c1d6abSBarry Smith @*/ 6449f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 6450f9c1d6abSBarry Smith { 6451f9c1d6abSBarry Smith PetscErrorCode ierr; 6452f9c1d6abSBarry Smith 6453f9c1d6abSBarry Smith PetscFunctionBegin; 6454f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6455ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 6456f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 6457f9c1d6abSBarry Smith PetscFunctionReturn(0); 6458f9c1d6abSBarry Smith } 645924655328SShri 6460b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 6461b3d3934dSBarry Smith #undef __FUNCT__ 6462b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate" 6463b3d3934dSBarry Smith /*@C 6464b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 6465b3d3934dSBarry Smith 6466b3d3934dSBarry Smith Collective on TS 6467b3d3934dSBarry Smith 6468b3d3934dSBarry Smith Input Parameters: 6469b3d3934dSBarry Smith . ts - the ODE solver object 6470b3d3934dSBarry Smith 6471b3d3934dSBarry Smith Output Parameter: 6472b3d3934dSBarry Smith . ctx - the context 6473b3d3934dSBarry Smith 6474b3d3934dSBarry Smith Level: intermediate 6475b3d3934dSBarry Smith 6476b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso 6477b3d3934dSBarry Smith 6478b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 6479b3d3934dSBarry Smith 6480b3d3934dSBarry Smith @*/ 6481b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 6482b3d3934dSBarry Smith { 6483b3d3934dSBarry Smith PetscErrorCode ierr; 6484b3d3934dSBarry Smith 6485b3d3934dSBarry Smith PetscFunctionBegin; 6486a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 6487b3d3934dSBarry Smith PetscFunctionReturn(0); 6488b3d3934dSBarry Smith } 6489b3d3934dSBarry Smith 6490b3d3934dSBarry Smith #undef __FUNCT__ 6491b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope" 6492b3d3934dSBarry Smith /*@C 6493b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 6494b3d3934dSBarry Smith 6495b3d3934dSBarry Smith Collective on TS 6496b3d3934dSBarry Smith 6497b3d3934dSBarry Smith Input Parameters: 6498b3d3934dSBarry Smith + ts - the TS context 6499b3d3934dSBarry Smith . step - current time-step 6500b3d3934dSBarry Smith . ptime - current time 65017db568b7SBarry Smith . u - current solution 65027db568b7SBarry Smith - dctx - the envelope context 6503b3d3934dSBarry Smith 6504b3d3934dSBarry Smith Options Database: 6505b3d3934dSBarry Smith . -ts_monitor_envelope 6506b3d3934dSBarry Smith 6507b3d3934dSBarry Smith Level: intermediate 6508b3d3934dSBarry Smith 6509b3d3934dSBarry Smith Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 6510b3d3934dSBarry Smith 6511b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6512b3d3934dSBarry Smith 65137db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 6514b3d3934dSBarry Smith @*/ 65157db568b7SBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6516b3d3934dSBarry Smith { 6517b3d3934dSBarry Smith PetscErrorCode ierr; 65187db568b7SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 6519b3d3934dSBarry Smith 6520b3d3934dSBarry Smith PetscFunctionBegin; 6521b3d3934dSBarry Smith if (!ctx->max) { 6522b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 6523b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 6524b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 6525b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 6526b3d3934dSBarry Smith } else { 6527b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 6528b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 6529b3d3934dSBarry Smith } 6530b3d3934dSBarry Smith PetscFunctionReturn(0); 6531b3d3934dSBarry Smith } 6532b3d3934dSBarry Smith 6533b3d3934dSBarry Smith 6534b3d3934dSBarry Smith #undef __FUNCT__ 6535b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds" 6536b3d3934dSBarry Smith /*@C 6537b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 6538b3d3934dSBarry Smith 6539b3d3934dSBarry Smith Collective on TS 6540b3d3934dSBarry Smith 6541b3d3934dSBarry Smith Input Parameter: 6542b3d3934dSBarry Smith . ts - the TS context 6543b3d3934dSBarry Smith 6544b3d3934dSBarry Smith Output Parameter: 6545b3d3934dSBarry Smith + max - the maximum values 6546b3d3934dSBarry Smith - min - the minimum values 6547b3d3934dSBarry Smith 65487db568b7SBarry Smith Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 65497db568b7SBarry Smith 6550b3d3934dSBarry Smith Level: intermediate 6551b3d3934dSBarry Smith 6552b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6553b3d3934dSBarry Smith 6554b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6555b3d3934dSBarry Smith @*/ 6556b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 6557b3d3934dSBarry Smith { 6558b3d3934dSBarry Smith PetscInt i; 6559b3d3934dSBarry Smith 6560b3d3934dSBarry Smith PetscFunctionBegin; 6561b3d3934dSBarry Smith if (max) *max = NULL; 6562b3d3934dSBarry Smith if (min) *min = NULL; 6563b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6564b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 65655537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 6566b3d3934dSBarry Smith if (max) *max = ctx->max; 6567b3d3934dSBarry Smith if (min) *min = ctx->min; 6568b3d3934dSBarry Smith break; 6569b3d3934dSBarry Smith } 6570b3d3934dSBarry Smith } 6571b3d3934dSBarry Smith PetscFunctionReturn(0); 6572b3d3934dSBarry Smith } 6573b3d3934dSBarry Smith 6574b3d3934dSBarry Smith #undef __FUNCT__ 6575b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy" 6576b3d3934dSBarry Smith /*@C 6577b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 6578b3d3934dSBarry Smith 6579b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 6580b3d3934dSBarry Smith 6581b3d3934dSBarry Smith Input Parameter: 6582b3d3934dSBarry Smith . ctx - the monitor context 6583b3d3934dSBarry Smith 6584b3d3934dSBarry Smith Level: intermediate 6585b3d3934dSBarry Smith 6586b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy 6587b3d3934dSBarry Smith 65887db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 6589b3d3934dSBarry Smith @*/ 6590b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 6591b3d3934dSBarry Smith { 6592b3d3934dSBarry Smith PetscErrorCode ierr; 6593b3d3934dSBarry Smith 6594b3d3934dSBarry Smith PetscFunctionBegin; 6595b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 6596b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 6597b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 6598b3d3934dSBarry Smith PetscFunctionReturn(0); 6599b3d3934dSBarry Smith } 6600f2dee214SBarry Smith 660124655328SShri #undef __FUNCT__ 660224655328SShri #define __FUNCT__ "TSRollBack" 660324655328SShri /*@ 660424655328SShri TSRollBack - Rolls back one time step 660524655328SShri 660624655328SShri Collective on TS 660724655328SShri 660824655328SShri Input Parameter: 660924655328SShri . ts - the TS context obtained from TSCreate() 661024655328SShri 661124655328SShri Level: advanced 661224655328SShri 661324655328SShri .keywords: TS, timestep, rollback 661424655328SShri 661524655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 661624655328SShri @*/ 661724655328SShri PetscErrorCode TSRollBack(TS ts) 661824655328SShri { 661924655328SShri PetscErrorCode ierr; 662024655328SShri 662124655328SShri PetscFunctionBegin; 662224655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 662324655328SShri 662424655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 662524655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 662624655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 662724655328SShri ts->ptime = ts->ptime_prev; 66288392e04aSShri Abhyankar ts->steprollback = PETSC_TRUE; /* Flag to indicate that the step is rollbacked */ 662924655328SShri PetscFunctionReturn(0); 663024655328SShri } 6631aeb4809dSShri Abhyankar 6632ff22ae23SHong Zhang #undef __FUNCT__ 6633ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages" 6634ff22ae23SHong Zhang /*@ 6635ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 6636ff22ae23SHong Zhang 6637ff22ae23SHong Zhang Input Parameter: 6638ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 6639ff22ae23SHong Zhang 6640ff22ae23SHong Zhang Level: advanced 6641ff22ae23SHong Zhang 6642ff22ae23SHong Zhang .keywords: TS, getstages 6643ff22ae23SHong Zhang 6644ff22ae23SHong Zhang .seealso: TSCreate() 6645ff22ae23SHong Zhang @*/ 6646ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns, Vec **Y) 6647ff22ae23SHong Zhang { 6648ff22ae23SHong Zhang PetscErrorCode ierr; 6649ff22ae23SHong Zhang 6650ff22ae23SHong Zhang PetscFunctionBegin; 6651ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 6652ff22ae23SHong Zhang PetscValidPointer(ns,2); 6653ff22ae23SHong Zhang 6654ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 6655ff22ae23SHong Zhang else { 6656ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 6657ff22ae23SHong Zhang } 6658ff22ae23SHong Zhang PetscFunctionReturn(0); 6659ff22ae23SHong Zhang } 6660ff22ae23SHong Zhang 6661847ff0e1SMatthew G. Knepley #undef __FUNCT__ 6662847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor" 6663847ff0e1SMatthew G. Knepley /*@C 6664847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 6665847ff0e1SMatthew G. Knepley 6666847ff0e1SMatthew G. Knepley Collective on SNES 6667847ff0e1SMatthew G. Knepley 6668847ff0e1SMatthew G. Knepley Input Parameters: 6669847ff0e1SMatthew G. Knepley + ts - the TS context 6670847ff0e1SMatthew G. Knepley . t - current timestep 6671847ff0e1SMatthew G. Knepley . U - state vector 6672847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 6673847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 6674847ff0e1SMatthew G. Knepley - ctx - an optional user context 6675847ff0e1SMatthew G. Knepley 6676847ff0e1SMatthew G. Knepley Output Parameters: 6677847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 6678847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 6679847ff0e1SMatthew G. Knepley 6680847ff0e1SMatthew G. Knepley Level: intermediate 6681847ff0e1SMatthew G. Knepley 6682847ff0e1SMatthew G. Knepley Notes: 6683847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 6684847ff0e1SMatthew G. Knepley 6685847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 6686847ff0e1SMatthew G. Knepley 6687847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 6688847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 6689847ff0e1SMatthew G. Knepley 6690847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 6691847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 6692847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 6693847ff0e1SMatthew G. Knepley 6694847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse 6695847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 6696847ff0e1SMatthew G. Knepley @*/ 6697847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 6698847ff0e1SMatthew G. Knepley { 6699847ff0e1SMatthew G. Knepley SNES snes; 6700847ff0e1SMatthew G. Knepley MatFDColoring color; 6701847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 6702847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 6703847ff0e1SMatthew G. Knepley 6704847ff0e1SMatthew G. Knepley PetscFunctionBegin; 6705c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 6706847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 6707847ff0e1SMatthew G. Knepley if (!color) { 6708847ff0e1SMatthew G. Knepley DM dm; 6709847ff0e1SMatthew G. Knepley ISColoring iscoloring; 6710847ff0e1SMatthew G. Knepley 6711847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 6712847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 6713847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 6714847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 6715847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 6716847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 6717847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 6718847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 6719847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 6720847ff0e1SMatthew G. Knepley } else { 6721847ff0e1SMatthew G. Knepley MatColoring mc; 6722847ff0e1SMatthew G. Knepley 6723847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 6724847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 6725847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 6726847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 6727847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 6728847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 6729847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 6730847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 6731847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 6732847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 6733847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 6734847ff0e1SMatthew G. Knepley } 6735847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 6736847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 6737847ff0e1SMatthew G. Knepley } 6738847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 6739847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 6740847ff0e1SMatthew G. Knepley if (J != B) { 6741847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 6742847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 6743847ff0e1SMatthew G. Knepley } 6744847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 6745847ff0e1SMatthew G. Knepley } 674693b34091SDebojyoti Ghosh 674793b34091SDebojyoti Ghosh #undef __FUNCT__ 6748cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSSetFunctionDomainError" 6749cb9d8021SPierre Barbier de Reuille /*@ 6750cb9d8021SPierre Barbier de Reuille TSSetFunctionDomainError - Set the function testing if the current state vector is valid 6751cb9d8021SPierre Barbier de Reuille 6752cb9d8021SPierre Barbier de Reuille Input Parameters: 6753cb9d8021SPierre Barbier de Reuille ts - the TS context 6754cb9d8021SPierre Barbier de Reuille func - function called within TSFunctionDomainError 6755cb9d8021SPierre Barbier de Reuille 6756cb9d8021SPierre Barbier de Reuille Level: intermediate 6757cb9d8021SPierre Barbier de Reuille 6758cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain 6759cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError() 6760cb9d8021SPierre Barbier de Reuille @*/ 6761cb9d8021SPierre Barbier de Reuille 6762d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 6763cb9d8021SPierre Barbier de Reuille { 6764cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 6765cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 6766cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 6767cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 6768cb9d8021SPierre Barbier de Reuille } 6769cb9d8021SPierre Barbier de Reuille 6770cb9d8021SPierre Barbier de Reuille #undef __FUNCT__ 6771cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSFunctionDomainError" 6772cb9d8021SPierre Barbier de Reuille /*@ 6773cb9d8021SPierre Barbier de Reuille TSFunctionDomainError - Check if the current state is valid 6774cb9d8021SPierre Barbier de Reuille 6775cb9d8021SPierre Barbier de Reuille Input Parameters: 6776cb9d8021SPierre Barbier de Reuille ts - the TS context 6777cb9d8021SPierre Barbier de Reuille stagetime - time of the simulation 6778d183316bSPierre Barbier de Reuille Y - state vector to check. 6779cb9d8021SPierre Barbier de Reuille 6780cb9d8021SPierre Barbier de Reuille Output Parameter: 6781cb9d8021SPierre Barbier de Reuille accept - Set to PETSC_FALSE if the current state vector is valid. 6782cb9d8021SPierre Barbier de Reuille 6783cb9d8021SPierre Barbier de Reuille Note: 6784cb9d8021SPierre Barbier de Reuille This function should be used to ensure the state is in a valid part of the space. 6785cb9d8021SPierre Barbier de Reuille For example, one can ensure here all values are positive. 678696a0c994SBarry Smith 678796a0c994SBarry Smith Level: advanced 6788cb9d8021SPierre Barbier de Reuille @*/ 6789d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 6790cb9d8021SPierre Barbier de Reuille { 6791cb9d8021SPierre Barbier de Reuille PetscErrorCode ierr; 6792cb9d8021SPierre Barbier de Reuille 6793cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 6794cb9d8021SPierre Barbier de Reuille 6795cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6796cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 6797cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 6798d183316bSPierre Barbier de Reuille PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 6799cb9d8021SPierre Barbier de Reuille } 6800cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 6801cb9d8021SPierre Barbier de Reuille } 68021ceb14c0SBarry Smith 68031ceb14c0SBarry Smith #undef __FUNCT__ 6804e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone" 680593b34091SDebojyoti Ghosh /*@C 6806e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 680793b34091SDebojyoti Ghosh 680893b34091SDebojyoti Ghosh Collective on MPI_Comm 680993b34091SDebojyoti Ghosh 681093b34091SDebojyoti Ghosh Input Parameter: 681193b34091SDebojyoti Ghosh . tsin - The input TS 681293b34091SDebojyoti Ghosh 681393b34091SDebojyoti Ghosh Output Parameter: 6814e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 681593b34091SDebojyoti Ghosh 68165eca1a21SEmil Constantinescu Notes: 68175eca1a21SEmil 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. 68185eca1a21SEmil Constantinescu 6819baa10174SEmil 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); 68205eca1a21SEmil Constantinescu 68215eca1a21SEmil Constantinescu Level: developer 682293b34091SDebojyoti Ghosh 6823e5168f73SEmil Constantinescu .keywords: TS, clone 6824e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 682593b34091SDebojyoti Ghosh @*/ 6826baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 682793b34091SDebojyoti Ghosh { 682893b34091SDebojyoti Ghosh TS t; 682993b34091SDebojyoti Ghosh PetscErrorCode ierr; 6830dc846ba4SSatish Balay SNES snes_start; 6831dc846ba4SSatish Balay DM dm; 6832dc846ba4SSatish Balay TSType type; 683393b34091SDebojyoti Ghosh 683493b34091SDebojyoti Ghosh PetscFunctionBegin; 683593b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 683693b34091SDebojyoti Ghosh *tsout = NULL; 683793b34091SDebojyoti Ghosh 68387a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 683993b34091SDebojyoti Ghosh 684093b34091SDebojyoti Ghosh /* General TS description */ 684193b34091SDebojyoti Ghosh t->numbermonitors = 0; 684293b34091SDebojyoti Ghosh t->setupcalled = 0; 684393b34091SDebojyoti Ghosh t->ksp_its = 0; 684493b34091SDebojyoti Ghosh t->snes_its = 0; 684593b34091SDebojyoti Ghosh t->nwork = 0; 684693b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 684793b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 684893b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 684993b34091SDebojyoti Ghosh 685034561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start); CHKERRQ(ierr); 685134561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start); CHKERRQ(ierr); 6852d15a3a53SEmil Constantinescu 685393b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm); CHKERRQ(ierr); 685493b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm); CHKERRQ(ierr); 685593b34091SDebojyoti Ghosh 685693b34091SDebojyoti Ghosh t->adapt=tsin->adapt; 685793b34091SDebojyoti Ghosh PetscObjectReference((PetscObject)t->adapt); 685893b34091SDebojyoti Ghosh 685993b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 686093b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 686193b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 686293b34091SDebojyoti Ghosh t->time_step_orig = tsin->time_step_orig; 686393b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 686493b34091SDebojyoti Ghosh t->steps = tsin->steps; 686593b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 686693b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 686793b34091SDebojyoti Ghosh t->atol = tsin->atol; 686893b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 686993b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 687093b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 687193b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 687293b34091SDebojyoti Ghosh 687393b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type); CHKERRQ(ierr); 687493b34091SDebojyoti Ghosh ierr = TSSetType(t,type); CHKERRQ(ierr); 687593b34091SDebojyoti Ghosh 687693b34091SDebojyoti Ghosh t->vec_sol = NULL; 687793b34091SDebojyoti Ghosh 687893b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 687993b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 688093b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 688193b34091SDebojyoti Ghosh 688293b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 688393b34091SDebojyoti Ghosh 68840d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 68850d4fed19SBarry Smith PetscInt i; 68860d4fed19SBarry Smith ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr); 68870d4fed19SBarry Smith for (i=0; i<10; i++) { 68880d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 68890d4fed19SBarry Smith } 68900d4fed19SBarry Smith } 689193b34091SDebojyoti Ghosh *tsout = t; 689293b34091SDebojyoti Ghosh PetscFunctionReturn(0); 689393b34091SDebojyoti Ghosh } 6894