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 22fde5950dSBarry Smith /*@C 23fde5950dSBarry Smith TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 24fde5950dSBarry Smith 25fde5950dSBarry Smith Collective on TS 26fde5950dSBarry Smith 27fde5950dSBarry Smith Input Parameters: 28fde5950dSBarry Smith + ts - TS object you wish to monitor 29fde5950dSBarry Smith . name - the monitor type one is seeking 30fde5950dSBarry Smith . help - message indicating what monitoring is done 31fde5950dSBarry Smith . manual - manual page for the monitor 32fde5950dSBarry Smith . monitor - the monitor function 33fde5950dSBarry 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 34fde5950dSBarry Smith 35fde5950dSBarry Smith Level: developer 36fde5950dSBarry Smith 37fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 38fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 39fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 40fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 41fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 42fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 43fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 44fde5950dSBarry Smith @*/ 45721cd6eeSBarry Smith PetscErrorCode TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*)) 46fde5950dSBarry Smith { 47fde5950dSBarry Smith PetscErrorCode ierr; 48fde5950dSBarry Smith PetscViewer viewer; 49fde5950dSBarry Smith PetscViewerFormat format; 50fde5950dSBarry Smith PetscBool flg; 51fde5950dSBarry Smith 52fde5950dSBarry Smith PetscFunctionBegin; 53fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 54fde5950dSBarry Smith if (flg) { 55721cd6eeSBarry Smith PetscViewerAndFormat *vf; 56721cd6eeSBarry Smith ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr); 57721cd6eeSBarry Smith ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 58fde5950dSBarry Smith if (monitorsetup) { 59721cd6eeSBarry Smith ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr); 60fde5950dSBarry Smith } 61721cd6eeSBarry Smith ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr); 62fde5950dSBarry Smith } 63fde5950dSBarry Smith PetscFunctionReturn(0); 64fde5950dSBarry Smith } 65fde5950dSBarry Smith 66fde5950dSBarry Smith /*@C 67fde5950dSBarry Smith TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 68fde5950dSBarry Smith 69fde5950dSBarry Smith Collective on TS 70fde5950dSBarry Smith 71fde5950dSBarry Smith Input Parameters: 72fde5950dSBarry Smith + ts - TS object you wish to monitor 73fde5950dSBarry Smith . name - the monitor type one is seeking 74fde5950dSBarry Smith . help - message indicating what monitoring is done 75fde5950dSBarry Smith . manual - manual page for the monitor 76fde5950dSBarry Smith . monitor - the monitor function 77fde5950dSBarry 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 78fde5950dSBarry Smith 79fde5950dSBarry Smith Level: developer 80fde5950dSBarry Smith 81fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 82fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 83fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 84fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 85fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 86fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 87fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 88fde5950dSBarry Smith @*/ 89721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*)) 90fde5950dSBarry Smith { 91fde5950dSBarry Smith PetscErrorCode ierr; 92fde5950dSBarry Smith PetscViewer viewer; 93fde5950dSBarry Smith PetscViewerFormat format; 94fde5950dSBarry Smith PetscBool flg; 95fde5950dSBarry Smith 96fde5950dSBarry Smith PetscFunctionBegin; 97fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 98fde5950dSBarry Smith if (flg) { 99721cd6eeSBarry Smith PetscViewerAndFormat *vf; 100721cd6eeSBarry Smith ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr); 101721cd6eeSBarry Smith ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 102fde5950dSBarry Smith if (monitorsetup) { 103721cd6eeSBarry Smith ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr); 104fde5950dSBarry Smith } 105721cd6eeSBarry Smith ierr = TSAdjointMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr); 106fde5950dSBarry Smith } 107fde5950dSBarry Smith PetscFunctionReturn(0); 108fde5950dSBarry Smith } 109fde5950dSBarry Smith 110bdad233fSMatthew Knepley /*@ 111bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 112bdad233fSMatthew Knepley 113bdad233fSMatthew Knepley Collective on TS 114bdad233fSMatthew Knepley 115bdad233fSMatthew Knepley Input Parameter: 116bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 117bdad233fSMatthew Knepley 118bdad233fSMatthew Knepley Options Database Keys: 119b6a60446SDebojyoti Ghosh + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE 120ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 1213e4cdcaaSBarry Smith . -ts_max_steps <maxsteps> - maximum number of time-steps to take 1223e4cdcaaSBarry Smith . -ts_final_time <time> - maximum time to compute to 1233e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 124a3cdaa26SBarry 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 125a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 126a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 127a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 128a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 129a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 130ef222394SBarry Smith . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 131847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 132bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 133de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 134de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 1356934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 136de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 137de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 138de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 139de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 1403e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 1413e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 142fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 143e4160dc7SJulian Andrej . -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu) 144110eb670SHong Zhang . -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 145110eb670SHong Zhang . -ts_adjoint_monitor - print information at each adjoint time step 14653ea634cSHong Zhang - -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically 14753ea634cSHong Zhang 14891b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 149bdad233fSMatthew Knepley 150bdad233fSMatthew Knepley Level: beginner 151bdad233fSMatthew Knepley 152bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 153bdad233fSMatthew Knepley 154a313700dSBarry Smith .seealso: TSGetType() 155bdad233fSMatthew Knepley @*/ 1567087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 157bdad233fSMatthew Knepley { 158bc952696SBarry Smith PetscBool opt,flg,tflg; 159dfbe8321SBarry Smith PetscErrorCode ierr; 160eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 16131748224SBarry Smith PetscReal time_step; 16249354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 163d1212d36SBarry Smith char dir[16]; 164cd11d68dSLisandro Dalcin TSIFunction ifun; 1656991f827SBarry Smith const char *defaultType; 1666991f827SBarry Smith char typeName[256]; 167bdad233fSMatthew Knepley 168bdad233fSMatthew Knepley PetscFunctionBegin; 1690700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1706991f827SBarry Smith 1716991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 172cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 173cd11d68dSLisandro Dalcin 174cd11d68dSLisandro Dalcin ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 175cd11d68dSLisandro Dalcin if (((PetscObject)ts)->type_name) 176cd11d68dSLisandro Dalcin defaultType = ((PetscObject)ts)->type_name; 177cd11d68dSLisandro Dalcin else 178cd11d68dSLisandro Dalcin defaultType = ifun ? TSBEULER : TSEULER; 1796991f827SBarry Smith ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr); 1806991f827SBarry Smith if (opt) { 1816991f827SBarry Smith ierr = TSSetType(ts,typeName);CHKERRQ(ierr); 1826991f827SBarry Smith } else { 1836991f827SBarry Smith ierr = TSSetType(ts,defaultType);CHKERRQ(ierr); 1846991f827SBarry Smith } 185bdad233fSMatthew Knepley 186bdad233fSMatthew Knepley /* Handle generic TS options */ 1870298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1880298fd71SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 1890298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 19031748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 191cd11d68dSLisandro Dalcin if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);} 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 */ 212cd11d68dSLisandro Dalcin ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and 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); 2256ba87a44SLisandro 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 } 2286ba87a44SLisandro 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); 2356ba87a44SLisandro 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); 2456ba87a44SLisandro 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); 2546ba87a44SLisandro 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); 2636ba87a44SLisandro 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 */ 4204f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 42168bece0bSHong Zhang ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 42268bece0bSHong Zhang if (tflg) { 42368bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 42468bece0bSHong Zhang } 4254f122a70SLisandro Dalcin tflg = ts->adjoint_solve ? PETSC_TRUE : PETSC_FALSE; 42668bece0bSHong Zhang ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr); 42768bece0bSHong Zhang if (flg) { 42868bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 42968bece0bSHong Zhang ts->adjoint_solve = tflg; 43068bece0bSHong Zhang } 431d763cef2SBarry Smith 432d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 4330633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr); 434fbc52257SHong Zhang ierr = PetscOptionsEnd();CHKERRQ(ierr); 435d763cef2SBarry Smith 4364f122a70SLisandro Dalcin if (ts->trajectory) { 4374f122a70SLisandro Dalcin ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 438d763cef2SBarry Smith } 43968bece0bSHong Zhang 4404f122a70SLisandro Dalcin ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr); 4414f122a70SLisandro Dalcin if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);} 4424f122a70SLisandro Dalcin ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr); 443d763cef2SBarry Smith PetscFunctionReturn(0); 444d763cef2SBarry Smith } 445d763cef2SBarry Smith 446d2daff3dSHong Zhang /*@ 447bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 448d2daff3dSHong Zhang 449d2daff3dSHong Zhang Collective on TS 450d2daff3dSHong Zhang 451d2daff3dSHong Zhang Input Parameters: 452bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 453bc952696SBarry Smith 45468bece0bSHong Zhang Note: This routine should be called after all TS options have been set 455d2daff3dSHong Zhang 456d2daff3dSHong Zhang Level: intermediate 457d2daff3dSHong Zhang 458bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve() 459d2daff3dSHong Zhang 460bc952696SBarry Smith .keywords: TS, set, checkpoint, 461d2daff3dSHong Zhang @*/ 462bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 463d2daff3dSHong Zhang { 464bc952696SBarry Smith PetscErrorCode ierr; 465bc952696SBarry Smith 466d2daff3dSHong Zhang PetscFunctionBegin; 467d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 468bc952696SBarry Smith if (!ts->trajectory) { 469bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 470972caf09SHong Zhang ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 471bc952696SBarry Smith } 472d2daff3dSHong Zhang PetscFunctionReturn(0); 473d2daff3dSHong Zhang } 474d2daff3dSHong Zhang 475a7a1495cSBarry Smith /*@ 476a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 477a7a1495cSBarry Smith set with TSSetRHSJacobian(). 478a7a1495cSBarry Smith 479a7a1495cSBarry Smith Collective on TS and Vec 480a7a1495cSBarry Smith 481a7a1495cSBarry Smith Input Parameters: 482316643e7SJed Brown + ts - the TS context 483a7a1495cSBarry Smith . t - current timestep 4840910c330SBarry Smith - U - input vector 485a7a1495cSBarry Smith 486a7a1495cSBarry Smith Output Parameters: 487a7a1495cSBarry Smith + A - Jacobian matrix 488a7a1495cSBarry Smith . B - optional preconditioning matrix 489a7a1495cSBarry Smith - flag - flag indicating matrix structure 490a7a1495cSBarry Smith 491a7a1495cSBarry Smith Notes: 492a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 493a7a1495cSBarry Smith is used internally within the nonlinear solvers. 494a7a1495cSBarry Smith 49594b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 496a7a1495cSBarry Smith flag parameter. 497a7a1495cSBarry Smith 498a7a1495cSBarry Smith Level: developer 499a7a1495cSBarry Smith 500a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 501a7a1495cSBarry Smith 50294b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 503a7a1495cSBarry Smith @*/ 504d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 505a7a1495cSBarry Smith { 506dfbe8321SBarry Smith PetscErrorCode ierr; 507270bf2e7SJed Brown PetscObjectState Ustate; 50824989b8cSPeter Brune DM dm; 509942e3340SBarry Smith DMTS tsdm; 51024989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 51124989b8cSPeter Brune void *ctx; 51224989b8cSPeter Brune TSIJacobian ijacobianfunc; 513b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 514a7a1495cSBarry Smith 515a7a1495cSBarry Smith PetscFunctionBegin; 5160700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5170910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5180910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 51924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 520942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 52124989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 5220298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 523b2df71adSDebojyoti Ghosh ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 52459e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 525b2df71adSDebojyoti Ghosh if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 5260e4ef248SJed Brown PetscFunctionReturn(0); 527f8ede8e7SJed Brown } 528d90be118SSean Farley 529ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 530d90be118SSean Farley 531e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 53294ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 53394ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 53494ab13aaSBarry Smith if (A != B) { 53594ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 53694ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 537e1244c69SJed Brown } 538e1244c69SJed Brown ts->rhsjacobian.shift = 0; 539e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 540e1244c69SJed Brown } 541e1244c69SJed Brown 54224989b8cSPeter Brune if (rhsjacobianfunc) { 5436cd88445SBarry Smith PetscBool missing; 54494ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 545a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 546d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 547a7a1495cSBarry Smith PetscStackPop; 54894ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 5496cd88445SBarry Smith if (A) { 5506cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 5516cd88445SBarry 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"); 5526cd88445SBarry Smith } 5536cd88445SBarry Smith if (B && B != A) { 5546cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 5556cd88445SBarry 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"); 5566cd88445SBarry Smith } 557ef66eb69SBarry Smith } else { 55894ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 55994ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 560ef66eb69SBarry Smith } 5610e4ef248SJed Brown ts->rhsjacobian.time = t; 5620910c330SBarry Smith ts->rhsjacobian.X = U; 56359e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 564a7a1495cSBarry Smith PetscFunctionReturn(0); 565a7a1495cSBarry Smith } 566a7a1495cSBarry Smith 567316643e7SJed Brown /*@ 568d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 569d763cef2SBarry Smith 570316643e7SJed Brown Collective on TS and Vec 571316643e7SJed Brown 572316643e7SJed Brown Input Parameters: 573316643e7SJed Brown + ts - the TS context 574316643e7SJed Brown . t - current time 5750910c330SBarry Smith - U - state vector 576316643e7SJed Brown 577316643e7SJed Brown Output Parameter: 578316643e7SJed Brown . y - right hand side 579316643e7SJed Brown 580316643e7SJed Brown Note: 581316643e7SJed Brown Most users should not need to explicitly call this routine, as it 582316643e7SJed Brown is used internally within the nonlinear solvers. 583316643e7SJed Brown 584316643e7SJed Brown Level: developer 585316643e7SJed Brown 586316643e7SJed Brown .keywords: TS, compute 587316643e7SJed Brown 588316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 589316643e7SJed Brown @*/ 5900910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 591d763cef2SBarry Smith { 592dfbe8321SBarry Smith PetscErrorCode ierr; 59324989b8cSPeter Brune TSRHSFunction rhsfunction; 59424989b8cSPeter Brune TSIFunction ifunction; 59524989b8cSPeter Brune void *ctx; 59624989b8cSPeter Brune DM dm; 59724989b8cSPeter Brune 598d763cef2SBarry Smith PetscFunctionBegin; 5990700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6000910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6010700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 60224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 60324989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 6040298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 605d763cef2SBarry Smith 606ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 607d763cef2SBarry Smith 6080910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 60924989b8cSPeter Brune if (rhsfunction) { 610d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 6110910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 612d763cef2SBarry Smith PetscStackPop; 613214bc6a2SJed Brown } else { 614214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 615b2cd27e8SJed Brown } 61644a41b28SSean Farley 6170910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 618d763cef2SBarry Smith PetscFunctionReturn(0); 619d763cef2SBarry Smith } 620d763cef2SBarry Smith 621ef20d060SBarry Smith /*@ 622ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 623ef20d060SBarry Smith 624ef20d060SBarry Smith Collective on TS and Vec 625ef20d060SBarry Smith 626ef20d060SBarry Smith Input Parameters: 627ef20d060SBarry Smith + ts - the TS context 628ef20d060SBarry Smith - t - current time 629ef20d060SBarry Smith 630ef20d060SBarry Smith Output Parameter: 6310910c330SBarry Smith . U - the solution 632ef20d060SBarry Smith 633ef20d060SBarry Smith Note: 634ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 635ef20d060SBarry Smith is used internally within the nonlinear solvers. 636ef20d060SBarry Smith 637ef20d060SBarry Smith Level: developer 638ef20d060SBarry Smith 639ef20d060SBarry Smith .keywords: TS, compute 640ef20d060SBarry Smith 641abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 642ef20d060SBarry Smith @*/ 6430910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 644ef20d060SBarry Smith { 645ef20d060SBarry Smith PetscErrorCode ierr; 646ef20d060SBarry Smith TSSolutionFunction solutionfunction; 647ef20d060SBarry Smith void *ctx; 648ef20d060SBarry Smith DM dm; 649ef20d060SBarry Smith 650ef20d060SBarry Smith PetscFunctionBegin; 651ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6520910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 653ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 654ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 655ef20d060SBarry Smith 656ef20d060SBarry Smith if (solutionfunction) { 6579b7cd975SBarry Smith PetscStackPush("TS user solution function"); 6580910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 659ef20d060SBarry Smith PetscStackPop; 660ef20d060SBarry Smith } 661ef20d060SBarry Smith PetscFunctionReturn(0); 662ef20d060SBarry Smith } 6639b7cd975SBarry Smith /*@ 6649b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6659b7cd975SBarry Smith 6669b7cd975SBarry Smith Collective on TS and Vec 6679b7cd975SBarry Smith 6689b7cd975SBarry Smith Input Parameters: 6699b7cd975SBarry Smith + ts - the TS context 6709b7cd975SBarry Smith - t - current time 6719b7cd975SBarry Smith 6729b7cd975SBarry Smith Output Parameter: 6739b7cd975SBarry Smith . U - the function value 6749b7cd975SBarry Smith 6759b7cd975SBarry Smith Note: 6769b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 6779b7cd975SBarry Smith is used internally within the nonlinear solvers. 6789b7cd975SBarry Smith 6799b7cd975SBarry Smith Level: developer 6809b7cd975SBarry Smith 6819b7cd975SBarry Smith .keywords: TS, compute 6829b7cd975SBarry Smith 6839b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 6849b7cd975SBarry Smith @*/ 6859b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 6869b7cd975SBarry Smith { 6879b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 6889b7cd975SBarry Smith void *ctx; 6899b7cd975SBarry Smith DM dm; 6909b7cd975SBarry Smith 6919b7cd975SBarry Smith PetscFunctionBegin; 6929b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6939b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6949b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 6959b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 6969b7cd975SBarry Smith 6979b7cd975SBarry Smith if (forcing) { 6989b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 6999b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 7009b7cd975SBarry Smith PetscStackPop; 7019b7cd975SBarry Smith } 7029b7cd975SBarry Smith PetscFunctionReturn(0); 7039b7cd975SBarry Smith } 704ef20d060SBarry Smith 705214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 706214bc6a2SJed Brown { 7072dd45cf8SJed Brown Vec F; 708214bc6a2SJed Brown PetscErrorCode ierr; 709214bc6a2SJed Brown 710214bc6a2SJed Brown PetscFunctionBegin; 7110298fd71SBarry Smith *Frhs = NULL; 7120298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 713214bc6a2SJed Brown if (!ts->Frhs) { 7142dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 715214bc6a2SJed Brown } 716214bc6a2SJed Brown *Frhs = ts->Frhs; 717214bc6a2SJed Brown PetscFunctionReturn(0); 718214bc6a2SJed Brown } 719214bc6a2SJed Brown 720214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 721214bc6a2SJed Brown { 722214bc6a2SJed Brown Mat A,B; 7232dd45cf8SJed Brown PetscErrorCode ierr; 724214bc6a2SJed Brown 725214bc6a2SJed Brown PetscFunctionBegin; 726c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 727c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 7280298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 729214bc6a2SJed Brown if (Arhs) { 730214bc6a2SJed Brown if (!ts->Arhs) { 731214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 732214bc6a2SJed Brown } 733214bc6a2SJed Brown *Arhs = ts->Arhs; 734214bc6a2SJed Brown } 735214bc6a2SJed Brown if (Brhs) { 736214bc6a2SJed Brown if (!ts->Brhs) { 737bdb70873SJed Brown if (A != B) { 738214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 739bdb70873SJed Brown } else { 740bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 74151699248SLisandro Dalcin ts->Brhs = ts->Arhs; 742bdb70873SJed Brown } 743214bc6a2SJed Brown } 744214bc6a2SJed Brown *Brhs = ts->Brhs; 745214bc6a2SJed Brown } 746214bc6a2SJed Brown PetscFunctionReturn(0); 747214bc6a2SJed Brown } 748214bc6a2SJed Brown 749316643e7SJed Brown /*@ 7500910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 751316643e7SJed Brown 752316643e7SJed Brown Collective on TS and Vec 753316643e7SJed Brown 754316643e7SJed Brown Input Parameters: 755316643e7SJed Brown + ts - the TS context 756316643e7SJed Brown . t - current time 7570910c330SBarry Smith . U - state vector 7580910c330SBarry Smith . Udot - time derivative of state vector 759214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 760316643e7SJed Brown 761316643e7SJed Brown Output Parameter: 762316643e7SJed Brown . Y - right hand side 763316643e7SJed Brown 764316643e7SJed Brown Note: 765316643e7SJed Brown Most users should not need to explicitly call this routine, as it 766316643e7SJed Brown is used internally within the nonlinear solvers. 767316643e7SJed Brown 768316643e7SJed Brown If the user did did not write their equations in implicit form, this 769316643e7SJed Brown function recasts them in implicit form. 770316643e7SJed Brown 771316643e7SJed Brown Level: developer 772316643e7SJed Brown 773316643e7SJed Brown .keywords: TS, compute 774316643e7SJed Brown 775316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 776316643e7SJed Brown @*/ 7770910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 778316643e7SJed Brown { 779316643e7SJed Brown PetscErrorCode ierr; 78024989b8cSPeter Brune TSIFunction ifunction; 78124989b8cSPeter Brune TSRHSFunction rhsfunction; 78224989b8cSPeter Brune void *ctx; 78324989b8cSPeter Brune DM dm; 784316643e7SJed Brown 785316643e7SJed Brown PetscFunctionBegin; 7860700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7870910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7880910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 7890700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 790316643e7SJed Brown 79124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 79224989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 7930298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 79424989b8cSPeter Brune 795ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 796d90be118SSean Farley 7970910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 79824989b8cSPeter Brune if (ifunction) { 799316643e7SJed Brown PetscStackPush("TS user implicit function"); 8000910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 801316643e7SJed Brown PetscStackPop; 802214bc6a2SJed Brown } 803214bc6a2SJed Brown if (imex) { 80424989b8cSPeter Brune if (!ifunction) { 8050910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 8062dd45cf8SJed Brown } 80724989b8cSPeter Brune } else if (rhsfunction) { 80824989b8cSPeter Brune if (ifunction) { 809214bc6a2SJed Brown Vec Frhs; 810214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 8110910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 812214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 8132dd45cf8SJed Brown } else { 8140910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 8150910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 816316643e7SJed Brown } 8174a6899ffSJed Brown } 8180910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 819316643e7SJed Brown PetscFunctionReturn(0); 820316643e7SJed Brown } 821316643e7SJed Brown 822316643e7SJed Brown /*@ 823316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 824316643e7SJed Brown 825316643e7SJed Brown Collective on TS and Vec 826316643e7SJed Brown 827316643e7SJed Brown Input 828316643e7SJed Brown Input Parameters: 829316643e7SJed Brown + ts - the TS context 830316643e7SJed Brown . t - current timestep 8310910c330SBarry Smith . U - state vector 8320910c330SBarry Smith . Udot - time derivative of state vector 833214bc6a2SJed Brown . shift - shift to apply, see note below 834214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 835316643e7SJed Brown 836316643e7SJed Brown Output Parameters: 837316643e7SJed Brown + A - Jacobian matrix 838316643e7SJed Brown . B - optional preconditioning matrix 839316643e7SJed Brown - flag - flag indicating matrix structure 840316643e7SJed Brown 841316643e7SJed Brown Notes: 8420910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 843316643e7SJed Brown 8440910c330SBarry Smith dF/dU + shift*dF/dUdot 845316643e7SJed Brown 846316643e7SJed Brown Most users should not need to explicitly call this routine, as it 847316643e7SJed Brown is used internally within the nonlinear solvers. 848316643e7SJed Brown 849316643e7SJed Brown Level: developer 850316643e7SJed Brown 851316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 852316643e7SJed Brown 853316643e7SJed Brown .seealso: TSSetIJacobian() 85463495f91SJed Brown @*/ 855d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 856316643e7SJed Brown { 857316643e7SJed Brown PetscErrorCode ierr; 85824989b8cSPeter Brune TSIJacobian ijacobian; 85924989b8cSPeter Brune TSRHSJacobian rhsjacobian; 86024989b8cSPeter Brune DM dm; 86124989b8cSPeter Brune void *ctx; 862316643e7SJed Brown 863316643e7SJed Brown PetscFunctionBegin; 8640700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8650910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8660910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 867316643e7SJed Brown PetscValidPointer(A,6); 86894ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 869316643e7SJed Brown PetscValidPointer(B,7); 87094ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 87124989b8cSPeter Brune 87224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 87324989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 8740298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 87524989b8cSPeter Brune 876ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 877316643e7SJed Brown 87894ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 87924989b8cSPeter Brune if (ijacobian) { 8806cd88445SBarry Smith PetscBool missing; 881316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 882d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 883316643e7SJed Brown PetscStackPop; 8846cd88445SBarry Smith if (A) { 8856cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 8866cd88445SBarry 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"); 8876cd88445SBarry Smith } 8886cd88445SBarry Smith if (B && B != A) { 8896cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 8906cd88445SBarry 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"); 8916cd88445SBarry Smith } 8924a6899ffSJed Brown } 893214bc6a2SJed Brown if (imex) { 894b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 8954c26be97Sstefano_zampini PetscBool assembled; 89694ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 8974c26be97Sstefano_zampini ierr = MatAssembled(A,&assembled);CHKERRQ(ierr); 8984c26be97Sstefano_zampini if (!assembled) { 8994c26be97Sstefano_zampini ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9004c26be97Sstefano_zampini ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9014c26be97Sstefano_zampini } 90294ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 90394ab13aaSBarry Smith if (A != B) { 90494ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 9054c26be97Sstefano_zampini ierr = MatAssembled(B,&assembled);CHKERRQ(ierr); 9064c26be97Sstefano_zampini if (!assembled) { 9074c26be97Sstefano_zampini ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9084c26be97Sstefano_zampini ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9094c26be97Sstefano_zampini } 91094ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 911214bc6a2SJed Brown } 912214bc6a2SJed Brown } 913214bc6a2SJed Brown } else { 914e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 915e1244c69SJed Brown if (rhsjacobian) { 916214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 917d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 918e1244c69SJed Brown } 91994ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 920e1244c69SJed Brown ts->rhsjacobian.scale = -1; 921e1244c69SJed Brown ts->rhsjacobian.shift = shift; 92294ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 92394ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 92494ab13aaSBarry Smith if (A != B) { 92594ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 92694ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 927316643e7SJed Brown } 928e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 929e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 930e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 93194ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 93294ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 93394ab13aaSBarry Smith if (A != B) { 93494ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 93594ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 936214bc6a2SJed Brown } 937316643e7SJed Brown } 93894ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 93994ab13aaSBarry Smith if (A != B) { 94094ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 941316643e7SJed Brown } 942316643e7SJed Brown } 943316643e7SJed Brown } 94494ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 945316643e7SJed Brown PetscFunctionReturn(0); 946316643e7SJed Brown } 947316643e7SJed Brown 948d763cef2SBarry Smith /*@C 949d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 950b5abc632SBarry Smith where U_t = G(t,u). 951d763cef2SBarry Smith 9523f9fe445SBarry Smith Logically Collective on TS 953d763cef2SBarry Smith 954d763cef2SBarry Smith Input Parameters: 955d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 9560298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 957d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 958d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 9590298fd71SBarry Smith function evaluation routine (may be NULL) 960d763cef2SBarry Smith 961d763cef2SBarry Smith Calling sequence of func: 96287828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 963d763cef2SBarry Smith 964d763cef2SBarry Smith + t - current timestep 965d763cef2SBarry Smith . u - input vector 966d763cef2SBarry Smith . F - function vector 967d763cef2SBarry Smith - ctx - [optional] user-defined function context 968d763cef2SBarry Smith 969d763cef2SBarry Smith Level: beginner 970d763cef2SBarry Smith 9712bbac0d3SBarry Smith Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 9722bbac0d3SBarry Smith 973d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 974d763cef2SBarry Smith 975ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 976d763cef2SBarry Smith @*/ 977089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 978d763cef2SBarry Smith { 979089b2837SJed Brown PetscErrorCode ierr; 980089b2837SJed Brown SNES snes; 9810298fd71SBarry Smith Vec ralloc = NULL; 98224989b8cSPeter Brune DM dm; 983d763cef2SBarry Smith 984089b2837SJed Brown PetscFunctionBegin; 9850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 986ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 98724989b8cSPeter Brune 98824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 98924989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 990089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 991e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 992e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 993e856ceecSJed Brown r = ralloc; 994e856ceecSJed Brown } 995089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 996e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 997d763cef2SBarry Smith PetscFunctionReturn(0); 998d763cef2SBarry Smith } 999d763cef2SBarry Smith 1000ef20d060SBarry Smith /*@C 1001abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1002ef20d060SBarry Smith 1003ef20d060SBarry Smith Logically Collective on TS 1004ef20d060SBarry Smith 1005ef20d060SBarry Smith Input Parameters: 1006ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1007ef20d060SBarry Smith . f - routine for evaluating the solution 1008ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 10090298fd71SBarry Smith function evaluation routine (may be NULL) 1010ef20d060SBarry Smith 1011ef20d060SBarry Smith Calling sequence of func: 1012ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 1013ef20d060SBarry Smith 1014ef20d060SBarry Smith + t - current timestep 1015ef20d060SBarry Smith . u - output vector 1016ef20d060SBarry Smith - ctx - [optional] user-defined function context 1017ef20d060SBarry Smith 1018abd5a294SJed Brown Notes: 1019abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1020abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1021abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1022abd5a294SJed Brown 10234f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1024abd5a294SJed Brown 1025ef20d060SBarry Smith Level: beginner 1026ef20d060SBarry Smith 1027ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1028ef20d060SBarry Smith 10299b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 1030ef20d060SBarry Smith @*/ 1031ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1032ef20d060SBarry Smith { 1033ef20d060SBarry Smith PetscErrorCode ierr; 1034ef20d060SBarry Smith DM dm; 1035ef20d060SBarry Smith 1036ef20d060SBarry Smith PetscFunctionBegin; 1037ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1038ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1039ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 1040ef20d060SBarry Smith PetscFunctionReturn(0); 1041ef20d060SBarry Smith } 1042ef20d060SBarry Smith 10439b7cd975SBarry Smith /*@C 10449b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 10459b7cd975SBarry Smith 10469b7cd975SBarry Smith Logically Collective on TS 10479b7cd975SBarry Smith 10489b7cd975SBarry Smith Input Parameters: 10499b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 1050e162b725SBarry Smith . func - routine for evaluating the forcing function 10519b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 10520298fd71SBarry Smith function evaluation routine (may be NULL) 10539b7cd975SBarry Smith 10549b7cd975SBarry Smith Calling sequence of func: 1055e162b725SBarry Smith $ func (TS ts,PetscReal t,Vec f,void *ctx); 10569b7cd975SBarry Smith 10579b7cd975SBarry Smith + t - current timestep 1058e162b725SBarry Smith . f - output vector 10599b7cd975SBarry Smith - ctx - [optional] user-defined function context 10609b7cd975SBarry Smith 10619b7cd975SBarry Smith Notes: 10629b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1063e162b725SBarry Smith create closed-form solutions with a non-physical forcing term. It allows you to use the Method of Manufactored Solution without directly editing the 1064e162b725SBarry Smith definition of the problem you are solving and hence possibly introducing bugs. 1065e162b725SBarry Smith 1066e162b725SBarry Smith This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0 1067e162b725SBarry Smith 1068e162b725SBarry Smith This forcing function does not depend on the solution to the equations, it can only depend on spatial location, time, and possibly parameters, the 1069e162b725SBarry Smith parameters can be passed in the ctx variable. 10709b7cd975SBarry Smith 10719b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 10729b7cd975SBarry Smith 10739b7cd975SBarry Smith Level: beginner 10749b7cd975SBarry Smith 10759b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 10769b7cd975SBarry Smith 10779b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 10789b7cd975SBarry Smith @*/ 1079e162b725SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx) 10809b7cd975SBarry Smith { 10819b7cd975SBarry Smith PetscErrorCode ierr; 10829b7cd975SBarry Smith DM dm; 10839b7cd975SBarry Smith 10849b7cd975SBarry Smith PetscFunctionBegin; 10859b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10869b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1087e162b725SBarry Smith ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr); 10889b7cd975SBarry Smith PetscFunctionReturn(0); 10899b7cd975SBarry Smith } 10909b7cd975SBarry Smith 1091d763cef2SBarry Smith /*@C 1092f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1093b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1094d763cef2SBarry Smith 10953f9fe445SBarry Smith Logically Collective on TS 1096d763cef2SBarry Smith 1097d763cef2SBarry Smith Input Parameters: 1098d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1099e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1100e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1101d763cef2SBarry Smith . f - the Jacobian evaluation routine 1102d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 11030298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1104d763cef2SBarry Smith 1105f7ab8db6SBarry Smith Calling sequence of f: 1106ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1107d763cef2SBarry Smith 1108d763cef2SBarry Smith + t - current timestep 1109d763cef2SBarry Smith . u - input vector 1110e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1111e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1112d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1113d763cef2SBarry Smith 11146cd88445SBarry Smith Notes: 11156cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 11166cd88445SBarry Smith 11176cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1118ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1119d763cef2SBarry Smith 1120d763cef2SBarry Smith Level: beginner 1121d763cef2SBarry Smith 1122d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1123d763cef2SBarry Smith 1124ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1125d763cef2SBarry Smith 1126d763cef2SBarry Smith @*/ 1127e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1128d763cef2SBarry Smith { 1129277b19d0SLisandro Dalcin PetscErrorCode ierr; 1130089b2837SJed Brown SNES snes; 113124989b8cSPeter Brune DM dm; 113224989b8cSPeter Brune TSIJacobian ijacobian; 1133277b19d0SLisandro Dalcin 1134d763cef2SBarry Smith PetscFunctionBegin; 11350700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1136e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1137e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1138e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1139e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1140d763cef2SBarry Smith 114124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 114224989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1143e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1144e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1145e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1146e1244c69SJed Brown } 11470298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1148089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11495f659677SPeter Brune if (!ijacobian) { 1150e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 11510e4ef248SJed Brown } 1152e5d3d808SBarry Smith if (Amat) { 1153e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 11540e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1155e5d3d808SBarry Smith ts->Arhs = Amat; 11560e4ef248SJed Brown } 1157e5d3d808SBarry Smith if (Pmat) { 1158e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 11590e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1160e5d3d808SBarry Smith ts->Brhs = Pmat; 11610e4ef248SJed Brown } 1162d763cef2SBarry Smith PetscFunctionReturn(0); 1163d763cef2SBarry Smith } 1164d763cef2SBarry Smith 1165316643e7SJed Brown 1166316643e7SJed Brown /*@C 1167b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1168316643e7SJed Brown 11693f9fe445SBarry Smith Logically Collective on TS 1170316643e7SJed Brown 1171316643e7SJed Brown Input Parameters: 1172316643e7SJed Brown + ts - the TS context obtained from TSCreate() 11730298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1174316643e7SJed Brown . f - the function evaluation routine 11750298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1176316643e7SJed Brown 1177316643e7SJed Brown Calling sequence of f: 1178316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1179316643e7SJed Brown 1180316643e7SJed Brown + t - time at step/stage being solved 1181316643e7SJed Brown . u - state vector 1182316643e7SJed Brown . u_t - time derivative of state vector 1183316643e7SJed Brown . F - function vector 1184316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1185316643e7SJed Brown 1186316643e7SJed Brown Important: 11872bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1188316643e7SJed Brown 1189316643e7SJed Brown Level: beginner 1190316643e7SJed Brown 1191316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1192316643e7SJed Brown 1193d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1194316643e7SJed Brown @*/ 119551699248SLisandro Dalcin PetscErrorCode TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx) 1196316643e7SJed Brown { 1197089b2837SJed Brown PetscErrorCode ierr; 1198089b2837SJed Brown SNES snes; 119951699248SLisandro Dalcin Vec ralloc = NULL; 120024989b8cSPeter Brune DM dm; 1201316643e7SJed Brown 1202316643e7SJed Brown PetscFunctionBegin; 12030700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 120451699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 120524989b8cSPeter Brune 120624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 120724989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 120824989b8cSPeter Brune 1209089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 121051699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 121151699248SLisandro Dalcin ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 121251699248SLisandro Dalcin r = ralloc; 1213e856ceecSJed Brown } 121451699248SLisandro Dalcin ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 121551699248SLisandro Dalcin ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1216089b2837SJed Brown PetscFunctionReturn(0); 1217089b2837SJed Brown } 1218089b2837SJed Brown 1219089b2837SJed Brown /*@C 1220089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1221089b2837SJed Brown 1222089b2837SJed Brown Not Collective 1223089b2837SJed Brown 1224089b2837SJed Brown Input Parameter: 1225089b2837SJed Brown . ts - the TS context 1226089b2837SJed Brown 1227089b2837SJed Brown Output Parameter: 12280298fd71SBarry Smith + r - vector to hold residual (or NULL) 12290298fd71SBarry Smith . func - the function to compute residual (or NULL) 12300298fd71SBarry Smith - ctx - the function context (or NULL) 1231089b2837SJed Brown 1232089b2837SJed Brown Level: advanced 1233089b2837SJed Brown 1234089b2837SJed Brown .keywords: TS, nonlinear, get, function 1235089b2837SJed Brown 1236089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1237089b2837SJed Brown @*/ 1238089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1239089b2837SJed Brown { 1240089b2837SJed Brown PetscErrorCode ierr; 1241089b2837SJed Brown SNES snes; 124224989b8cSPeter Brune DM dm; 1243089b2837SJed Brown 1244089b2837SJed Brown PetscFunctionBegin; 1245089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1246089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12470298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 124824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 124924989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1250089b2837SJed Brown PetscFunctionReturn(0); 1251089b2837SJed Brown } 1252089b2837SJed Brown 1253089b2837SJed Brown /*@C 1254089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1255089b2837SJed Brown 1256089b2837SJed Brown Not Collective 1257089b2837SJed Brown 1258089b2837SJed Brown Input Parameter: 1259089b2837SJed Brown . ts - the TS context 1260089b2837SJed Brown 1261089b2837SJed Brown Output Parameter: 12620298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 12630298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 12640298fd71SBarry Smith - ctx - the function context (or NULL) 1265089b2837SJed Brown 1266089b2837SJed Brown Level: advanced 1267089b2837SJed Brown 1268089b2837SJed Brown .keywords: TS, nonlinear, get, function 1269089b2837SJed Brown 12702bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1271089b2837SJed Brown @*/ 1272089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1273089b2837SJed Brown { 1274089b2837SJed Brown PetscErrorCode ierr; 1275089b2837SJed Brown SNES snes; 127624989b8cSPeter Brune DM dm; 1277089b2837SJed Brown 1278089b2837SJed Brown PetscFunctionBegin; 1279089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1280089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12810298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 128224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 128324989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1284316643e7SJed Brown PetscFunctionReturn(0); 1285316643e7SJed Brown } 1286316643e7SJed Brown 1287316643e7SJed Brown /*@C 1288a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1289ae8867d6SBarry Smith provided with TSSetIFunction(). 1290316643e7SJed Brown 12913f9fe445SBarry Smith Logically Collective on TS 1292316643e7SJed Brown 1293316643e7SJed Brown Input Parameters: 1294316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1295e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1296e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1297316643e7SJed Brown . f - the Jacobian evaluation routine 12980298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1299316643e7SJed Brown 1300316643e7SJed Brown Calling sequence of f: 1301ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1302316643e7SJed Brown 1303316643e7SJed Brown + t - time at step/stage being solved 13041b4a444bSJed Brown . U - state vector 13051b4a444bSJed Brown . U_t - time derivative of state vector 1306316643e7SJed Brown . a - shift 1307e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1308e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1309316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1310316643e7SJed Brown 1311316643e7SJed Brown Notes: 1312e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1313316643e7SJed Brown 1314895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1315895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1316895c21f2SBarry Smith 1317a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1318b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1319a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1320a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1321a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1322a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1323a4f0a591SBarry Smith 13246cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13256cd88445SBarry Smith 13266cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1327ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1328ca5f011dSBarry Smith 1329316643e7SJed Brown Level: beginner 1330316643e7SJed Brown 1331316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1332316643e7SJed Brown 1333ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1334316643e7SJed Brown 1335316643e7SJed Brown @*/ 1336e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1337316643e7SJed Brown { 1338316643e7SJed Brown PetscErrorCode ierr; 1339089b2837SJed Brown SNES snes; 134024989b8cSPeter Brune DM dm; 1341316643e7SJed Brown 1342316643e7SJed Brown PetscFunctionBegin; 13430700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1344e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1345e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1346e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1347e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 134824989b8cSPeter Brune 134924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 135024989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 135124989b8cSPeter Brune 1352089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1353e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1354316643e7SJed Brown PetscFunctionReturn(0); 1355316643e7SJed Brown } 1356316643e7SJed Brown 1357e1244c69SJed Brown /*@ 1358e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1359e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1360e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1361e1244c69SJed Brown not been changed by the TS. 1362e1244c69SJed Brown 1363e1244c69SJed Brown Logically Collective 1364e1244c69SJed Brown 1365e1244c69SJed Brown Input Arguments: 1366e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1367e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1368e1244c69SJed Brown 1369e1244c69SJed Brown Level: intermediate 1370e1244c69SJed Brown 1371e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1372e1244c69SJed Brown @*/ 1373e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1374e1244c69SJed Brown { 1375e1244c69SJed Brown PetscFunctionBegin; 1376e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1377e1244c69SJed Brown PetscFunctionReturn(0); 1378e1244c69SJed Brown } 1379e1244c69SJed Brown 1380efe9872eSLisandro Dalcin /*@C 1381efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1382efe9872eSLisandro Dalcin 1383efe9872eSLisandro Dalcin Logically Collective on TS 1384efe9872eSLisandro Dalcin 1385efe9872eSLisandro Dalcin Input Parameters: 1386efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1387efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1388efe9872eSLisandro Dalcin . fun - the function evaluation routine 1389efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1390efe9872eSLisandro Dalcin 1391efe9872eSLisandro Dalcin Calling sequence of fun: 1392efe9872eSLisandro Dalcin $ fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1393efe9872eSLisandro Dalcin 1394efe9872eSLisandro Dalcin + t - time at step/stage being solved 1395efe9872eSLisandro Dalcin . U - state vector 1396efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1397efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1398efe9872eSLisandro Dalcin . F - function vector 1399efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1400efe9872eSLisandro Dalcin 1401efe9872eSLisandro Dalcin Level: beginner 1402efe9872eSLisandro Dalcin 1403efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function 1404efe9872eSLisandro Dalcin 1405efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1406efe9872eSLisandro Dalcin @*/ 1407efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx) 1408efe9872eSLisandro Dalcin { 1409efe9872eSLisandro Dalcin DM dm; 1410efe9872eSLisandro Dalcin PetscErrorCode ierr; 1411efe9872eSLisandro Dalcin 1412efe9872eSLisandro Dalcin PetscFunctionBegin; 1413efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1414efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2); 1415efe9872eSLisandro Dalcin ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr); 1416efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1417efe9872eSLisandro Dalcin ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1418efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1419efe9872eSLisandro Dalcin } 1420efe9872eSLisandro Dalcin 1421efe9872eSLisandro Dalcin /*@C 1422efe9872eSLisandro Dalcin TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1423efe9872eSLisandro Dalcin 1424efe9872eSLisandro Dalcin Not Collective 1425efe9872eSLisandro Dalcin 1426efe9872eSLisandro Dalcin Input Parameter: 1427efe9872eSLisandro Dalcin . ts - the TS context 1428efe9872eSLisandro Dalcin 1429efe9872eSLisandro Dalcin Output Parameter: 1430efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1431efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1432efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1433efe9872eSLisandro Dalcin 1434efe9872eSLisandro Dalcin Level: advanced 1435efe9872eSLisandro Dalcin 1436efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function 1437efe9872eSLisandro Dalcin 1438efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction() 1439efe9872eSLisandro Dalcin @*/ 1440efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx) 1441efe9872eSLisandro Dalcin { 1442efe9872eSLisandro Dalcin PetscErrorCode ierr; 1443efe9872eSLisandro Dalcin SNES snes; 1444efe9872eSLisandro Dalcin DM dm; 1445efe9872eSLisandro Dalcin 1446efe9872eSLisandro Dalcin PetscFunctionBegin; 1447efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1448efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1449efe9872eSLisandro Dalcin ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 1450efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1451efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1452efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1453efe9872eSLisandro Dalcin } 1454efe9872eSLisandro Dalcin 1455efe9872eSLisandro Dalcin /*@C 1456bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1457efe9872eSLisandro Dalcin where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function(). 1458efe9872eSLisandro Dalcin 1459efe9872eSLisandro Dalcin Logically Collective on TS 1460efe9872eSLisandro Dalcin 1461efe9872eSLisandro Dalcin Input Parameters: 1462efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1463efe9872eSLisandro Dalcin . J - Jacobian matrix 1464efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1465efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1466efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1467efe9872eSLisandro Dalcin 1468efe9872eSLisandro Dalcin Calling sequence of jac: 1469bc77d74cSLisandro Dalcin $ jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx); 1470efe9872eSLisandro Dalcin 1471efe9872eSLisandro Dalcin + t - time at step/stage being solved 1472efe9872eSLisandro Dalcin . U - state vector 1473efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1474efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1475efe9872eSLisandro Dalcin . v - shift for U_t 1476efe9872eSLisandro Dalcin . a - shift for U_tt 1477efe9872eSLisandro Dalcin . J - Jacobian of G(U) = F(t,U,W+v*U,W'+a*U), equivalent to dF/dU + v*dF/dU_t + a*dF/dU_tt 1478efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1479efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1480efe9872eSLisandro Dalcin 1481efe9872eSLisandro Dalcin Notes: 1482efe9872eSLisandro Dalcin The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve. 1483efe9872eSLisandro Dalcin 1484efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1485efe9872eSLisandro Dalcin the Jacobian of G(U) = F(t,U,W+v*U,W'+a*U) where F(t,U,U_t,U_tt) = 0 is the DAE to be solved. 1486efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1487bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1488efe9872eSLisandro Dalcin 1489efe9872eSLisandro Dalcin Level: beginner 1490efe9872eSLisandro Dalcin 1491efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian 1492efe9872eSLisandro Dalcin 1493efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1494efe9872eSLisandro Dalcin @*/ 1495efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx) 1496efe9872eSLisandro Dalcin { 1497efe9872eSLisandro Dalcin DM dm; 1498efe9872eSLisandro Dalcin PetscErrorCode ierr; 1499efe9872eSLisandro Dalcin 1500efe9872eSLisandro Dalcin PetscFunctionBegin; 1501efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1502efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2); 1503efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3); 1504efe9872eSLisandro Dalcin ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr); 1505efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1506efe9872eSLisandro Dalcin ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1507efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1508efe9872eSLisandro Dalcin } 1509efe9872eSLisandro Dalcin 1510efe9872eSLisandro Dalcin /*@C 1511efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1512efe9872eSLisandro Dalcin 1513efe9872eSLisandro Dalcin Not Collective, but parallel objects are returned if TS is parallel 1514efe9872eSLisandro Dalcin 1515efe9872eSLisandro Dalcin Input Parameter: 1516efe9872eSLisandro Dalcin . ts - The TS context obtained from TSCreate() 1517efe9872eSLisandro Dalcin 1518efe9872eSLisandro Dalcin Output Parameters: 1519efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1520efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1521efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1522efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1523efe9872eSLisandro Dalcin 1524efe9872eSLisandro Dalcin Notes: You can pass in NULL for any return argument you do not need. 1525efe9872eSLisandro Dalcin 1526efe9872eSLisandro Dalcin Level: advanced 1527efe9872eSLisandro Dalcin 1528efe9872eSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 1529efe9872eSLisandro Dalcin 1530efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian 1531efe9872eSLisandro Dalcin @*/ 1532efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx) 1533efe9872eSLisandro Dalcin { 1534efe9872eSLisandro Dalcin PetscErrorCode ierr; 1535efe9872eSLisandro Dalcin SNES snes; 1536efe9872eSLisandro Dalcin DM dm; 1537efe9872eSLisandro Dalcin 1538efe9872eSLisandro Dalcin PetscFunctionBegin; 1539efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1540efe9872eSLisandro Dalcin ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 1541efe9872eSLisandro Dalcin ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr); 1542efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1543efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1544efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1545efe9872eSLisandro Dalcin } 1546efe9872eSLisandro Dalcin 1547efe9872eSLisandro Dalcin /*@ 1548efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1549efe9872eSLisandro Dalcin 1550efe9872eSLisandro Dalcin Collective on TS and Vec 1551efe9872eSLisandro Dalcin 1552efe9872eSLisandro Dalcin Input Parameters: 1553efe9872eSLisandro Dalcin + ts - the TS context 1554efe9872eSLisandro Dalcin . t - current time 1555efe9872eSLisandro Dalcin . U - state vector 1556efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1557efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1558efe9872eSLisandro Dalcin 1559efe9872eSLisandro Dalcin Output Parameter: 1560efe9872eSLisandro Dalcin . F - the residual vector 1561efe9872eSLisandro Dalcin 1562efe9872eSLisandro Dalcin Note: 1563efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1564efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1565efe9872eSLisandro Dalcin 1566efe9872eSLisandro Dalcin Level: developer 1567efe9872eSLisandro Dalcin 1568efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector 1569efe9872eSLisandro Dalcin 1570efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1571efe9872eSLisandro Dalcin @*/ 1572efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F) 1573efe9872eSLisandro Dalcin { 1574efe9872eSLisandro Dalcin DM dm; 1575efe9872eSLisandro Dalcin TSI2Function I2Function; 1576efe9872eSLisandro Dalcin void *ctx; 1577efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1578efe9872eSLisandro Dalcin PetscErrorCode ierr; 1579efe9872eSLisandro Dalcin 1580efe9872eSLisandro Dalcin PetscFunctionBegin; 1581efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1582efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1583efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1584efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1585efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F,VEC_CLASSID,6); 1586efe9872eSLisandro Dalcin 1587efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1588efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr); 1589efe9872eSLisandro Dalcin ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 1590efe9872eSLisandro Dalcin 1591efe9872eSLisandro Dalcin if (!I2Function) { 1592efe9872eSLisandro Dalcin ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr); 1593efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1594efe9872eSLisandro Dalcin } 1595efe9872eSLisandro Dalcin 1596efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1597efe9872eSLisandro Dalcin 1598efe9872eSLisandro Dalcin PetscStackPush("TS user implicit function"); 1599efe9872eSLisandro Dalcin ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr); 1600efe9872eSLisandro Dalcin PetscStackPop; 1601efe9872eSLisandro Dalcin 1602efe9872eSLisandro Dalcin if (rhsfunction) { 1603efe9872eSLisandro Dalcin Vec Frhs; 1604efe9872eSLisandro Dalcin ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 1605efe9872eSLisandro Dalcin ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 1606efe9872eSLisandro Dalcin ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr); 1607efe9872eSLisandro Dalcin } 1608efe9872eSLisandro Dalcin 1609efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1610efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1611efe9872eSLisandro Dalcin } 1612efe9872eSLisandro Dalcin 1613efe9872eSLisandro Dalcin /*@ 1614efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1615efe9872eSLisandro Dalcin 1616efe9872eSLisandro Dalcin Collective on TS and Vec 1617efe9872eSLisandro Dalcin 1618efe9872eSLisandro Dalcin Input Parameters: 1619efe9872eSLisandro Dalcin + ts - the TS context 1620efe9872eSLisandro Dalcin . t - current timestep 1621efe9872eSLisandro Dalcin . U - state vector 1622efe9872eSLisandro Dalcin . V - time derivative of state vector 1623efe9872eSLisandro Dalcin . A - second time derivative of state vector 1624efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1625efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1626efe9872eSLisandro Dalcin 1627efe9872eSLisandro Dalcin Output Parameters: 1628efe9872eSLisandro Dalcin + J - Jacobian matrix 1629efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1630efe9872eSLisandro Dalcin 1631efe9872eSLisandro Dalcin Notes: 1632efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1633efe9872eSLisandro Dalcin 1634efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1635efe9872eSLisandro Dalcin 1636efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1637efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1638efe9872eSLisandro Dalcin 1639efe9872eSLisandro Dalcin Level: developer 1640efe9872eSLisandro Dalcin 1641efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix 1642efe9872eSLisandro Dalcin 1643efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1644efe9872eSLisandro Dalcin @*/ 1645efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P) 1646efe9872eSLisandro Dalcin { 1647efe9872eSLisandro Dalcin DM dm; 1648efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1649efe9872eSLisandro Dalcin void *ctx; 1650efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1651efe9872eSLisandro Dalcin PetscErrorCode ierr; 1652efe9872eSLisandro Dalcin 1653efe9872eSLisandro Dalcin PetscFunctionBegin; 1654efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1655efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1656efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1657efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1658efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J,MAT_CLASSID,8); 1659efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P,MAT_CLASSID,9); 1660efe9872eSLisandro Dalcin 1661efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1662efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr); 1663efe9872eSLisandro Dalcin ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 1664efe9872eSLisandro Dalcin 1665efe9872eSLisandro Dalcin if (!I2Jacobian) { 1666efe9872eSLisandro Dalcin ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr); 1667efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1668efe9872eSLisandro Dalcin } 1669efe9872eSLisandro Dalcin 1670efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1671efe9872eSLisandro Dalcin 1672efe9872eSLisandro Dalcin PetscStackPush("TS user implicit Jacobian"); 1673efe9872eSLisandro Dalcin ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr); 1674efe9872eSLisandro Dalcin PetscStackPop; 1675efe9872eSLisandro Dalcin 1676efe9872eSLisandro Dalcin if (rhsjacobian) { 1677efe9872eSLisandro Dalcin Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 1678efe9872eSLisandro Dalcin ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr); 1679efe9872eSLisandro Dalcin ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr); 1680efe9872eSLisandro Dalcin ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr); 1681efe9872eSLisandro Dalcin if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);} 1682efe9872eSLisandro Dalcin } 1683efe9872eSLisandro Dalcin 1684efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1685efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1686efe9872eSLisandro Dalcin } 1687efe9872eSLisandro Dalcin 1688efe9872eSLisandro Dalcin /*@ 1689efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1690efe9872eSLisandro Dalcin for use by the TS routines handling second order equations. 1691efe9872eSLisandro Dalcin 1692efe9872eSLisandro Dalcin Logically Collective on TS and Vec 1693efe9872eSLisandro Dalcin 1694efe9872eSLisandro Dalcin Input Parameters: 1695efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1696efe9872eSLisandro Dalcin . u - the solution vector 1697efe9872eSLisandro Dalcin - v - the time derivative vector 1698efe9872eSLisandro Dalcin 1699efe9872eSLisandro Dalcin Level: beginner 1700efe9872eSLisandro Dalcin 1701efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions 1702efe9872eSLisandro Dalcin @*/ 1703efe9872eSLisandro Dalcin PetscErrorCode TS2SetSolution(TS ts,Vec u,Vec v) 1704efe9872eSLisandro Dalcin { 1705efe9872eSLisandro Dalcin PetscErrorCode ierr; 1706efe9872eSLisandro Dalcin 1707efe9872eSLisandro Dalcin PetscFunctionBegin; 1708efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1709efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1710efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v,VEC_CLASSID,3); 1711efe9872eSLisandro Dalcin ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 1712efe9872eSLisandro Dalcin ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr); 1713efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 1714efe9872eSLisandro Dalcin ts->vec_dot = v; 1715efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1716efe9872eSLisandro Dalcin } 1717efe9872eSLisandro Dalcin 1718efe9872eSLisandro Dalcin /*@ 1719efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 1720efe9872eSLisandro Dalcin for second order equations. It is valid to call this routine inside the function 1721efe9872eSLisandro Dalcin that you are evaluating in order to move to the new timestep. This vector not 1722efe9872eSLisandro Dalcin changed until the solution at the next timestep has been calculated. 1723efe9872eSLisandro Dalcin 1724efe9872eSLisandro Dalcin Not Collective, but Vec returned is parallel if TS is parallel 1725efe9872eSLisandro Dalcin 1726efe9872eSLisandro Dalcin Input Parameter: 1727efe9872eSLisandro Dalcin . ts - the TS context obtained from TSCreate() 1728efe9872eSLisandro Dalcin 1729efe9872eSLisandro Dalcin Output Parameter: 1730efe9872eSLisandro Dalcin + u - the vector containing the solution 1731efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1732efe9872eSLisandro Dalcin 1733efe9872eSLisandro Dalcin Level: intermediate 1734efe9872eSLisandro Dalcin 1735efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime() 1736efe9872eSLisandro Dalcin 1737efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution 1738efe9872eSLisandro Dalcin @*/ 1739efe9872eSLisandro Dalcin PetscErrorCode TS2GetSolution(TS ts,Vec *u,Vec *v) 1740efe9872eSLisandro Dalcin { 1741efe9872eSLisandro Dalcin PetscFunctionBegin; 1742efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1743efe9872eSLisandro Dalcin if (u) PetscValidPointer(u,2); 1744efe9872eSLisandro Dalcin if (v) PetscValidPointer(v,3); 1745efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1746efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 1747efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1748efe9872eSLisandro Dalcin } 1749efe9872eSLisandro Dalcin 175055849f57SBarry Smith /*@C 175155849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 175255849f57SBarry Smith 175355849f57SBarry Smith Collective on PetscViewer 175455849f57SBarry Smith 175555849f57SBarry Smith Input Parameters: 175655849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 175755849f57SBarry Smith some related function before a call to TSLoad(). 175855849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 175955849f57SBarry Smith 176055849f57SBarry Smith Level: intermediate 176155849f57SBarry Smith 176255849f57SBarry Smith Notes: 176355849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 176455849f57SBarry Smith 176555849f57SBarry Smith Notes for advanced users: 176655849f57SBarry Smith Most users should not need to know the details of the binary storage 176755849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 176855849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 176955849f57SBarry Smith format is 177055849f57SBarry Smith .vb 177155849f57SBarry Smith has not yet been determined 177255849f57SBarry Smith .ve 177355849f57SBarry Smith 177455849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 177555849f57SBarry Smith @*/ 1776f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 177755849f57SBarry Smith { 177855849f57SBarry Smith PetscErrorCode ierr; 177955849f57SBarry Smith PetscBool isbinary; 178055849f57SBarry Smith PetscInt classid; 178155849f57SBarry Smith char type[256]; 17822d53ad75SBarry Smith DMTS sdm; 1783ad6bc421SBarry Smith DM dm; 178455849f57SBarry Smith 178555849f57SBarry Smith PetscFunctionBegin; 1786f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 178755849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 178855849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 178955849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 179055849f57SBarry Smith 1791060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1792ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1793060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1794f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1795f2c2a1b9SBarry Smith if (ts->ops->load) { 1796f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1797f2c2a1b9SBarry Smith } 1798ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1799ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1800ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1801f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1802f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 18032d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 18042d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 180555849f57SBarry Smith PetscFunctionReturn(0); 180655849f57SBarry Smith } 180755849f57SBarry Smith 18089804daf3SBarry Smith #include <petscdraw.h> 1809e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1810e04113cfSBarry Smith #include <petscviewersaws.h> 1811f05ece33SBarry Smith #endif 18127e2c5f70SBarry Smith /*@C 1813d763cef2SBarry Smith TSView - Prints the TS data structure. 1814d763cef2SBarry Smith 18154c49b128SBarry Smith Collective on TS 1816d763cef2SBarry Smith 1817d763cef2SBarry Smith Input Parameters: 1818d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1819d763cef2SBarry Smith - viewer - visualization context 1820d763cef2SBarry Smith 1821d763cef2SBarry Smith Options Database Key: 1822d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1823d763cef2SBarry Smith 1824d763cef2SBarry Smith Notes: 1825d763cef2SBarry Smith The available visualization contexts include 1826b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1827b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1828d763cef2SBarry Smith output where only the first processor opens 1829d763cef2SBarry Smith the file. All other processors send their 1830d763cef2SBarry Smith data to the first processor to print. 1831d763cef2SBarry Smith 1832d763cef2SBarry Smith The user can open an alternative visualization context with 1833b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1834d763cef2SBarry Smith 1835d763cef2SBarry Smith Level: beginner 1836d763cef2SBarry Smith 1837d763cef2SBarry Smith .keywords: TS, timestep, view 1838d763cef2SBarry Smith 1839b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1840d763cef2SBarry Smith @*/ 18417087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1842d763cef2SBarry Smith { 1843dfbe8321SBarry Smith PetscErrorCode ierr; 184419fd82e9SBarry Smith TSType type; 18452b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 18462d53ad75SBarry Smith DMTS sdm; 1847e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1848536b137fSBarry Smith PetscBool issaws; 1849f05ece33SBarry Smith #endif 1850d763cef2SBarry Smith 1851d763cef2SBarry Smith PetscFunctionBegin; 18520700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18533050cee2SBarry Smith if (!viewer) { 1854ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 18553050cee2SBarry Smith } 18560700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1857c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1858fd16b177SBarry Smith 1859251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1860251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 186155849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 18622b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1863e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1864536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1865f05ece33SBarry Smith #endif 186632077d6dSBarry Smith if (iascii) { 1867dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 186877431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 18697c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1870d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 18715ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1872c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1873d763cef2SBarry Smith } 18745ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1875193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 1876a0af407cSBarry Smith if (ts->vrtol) { 1877a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of relative error tolerances, ");CHKERRQ(ierr); 1878a0af407cSBarry Smith } else { 1879a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr); 1880a0af407cSBarry Smith } 1881a0af407cSBarry Smith if (ts->vatol) { 1882a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of absolute error tolerances\n");CHKERRQ(ierr); 1883a0af407cSBarry Smith } else { 1884a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr); 1885a0af407cSBarry Smith } 18862d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 18872d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1888d52bd9f3SBarry Smith if (ts->ops->view) { 1889d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1890d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1891d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1892d52bd9f3SBarry Smith } 18930f5bd95cSBarry Smith } else if (isstring) { 1894a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1895b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 189655849f57SBarry Smith } else if (isbinary) { 189755849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 189855849f57SBarry Smith MPI_Comm comm; 189955849f57SBarry Smith PetscMPIInt rank; 190055849f57SBarry Smith char type[256]; 190155849f57SBarry Smith 190255849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 190355849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 190455849f57SBarry Smith if (!rank) { 190555849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 190655849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 190755849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 190855849f57SBarry Smith } 190955849f57SBarry Smith if (ts->ops->view) { 191055849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 191155849f57SBarry Smith } 1912f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1913f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 19142d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19152d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 19162b0a91c0SBarry Smith } else if (isdraw) { 19172b0a91c0SBarry Smith PetscDraw draw; 19182b0a91c0SBarry Smith char str[36]; 191989fd9fafSBarry Smith PetscReal x,y,bottom,h; 19202b0a91c0SBarry Smith 19212b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 19222b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 19232b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 19242b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 192551fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 192689fd9fafSBarry Smith bottom = y - h; 19272b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 19282b0a91c0SBarry Smith if (ts->ops->view) { 19292b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 19302b0a91c0SBarry Smith } 19312b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1932e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1933536b137fSBarry Smith } else if (issaws) { 1934d45a07a7SBarry Smith PetscMPIInt rank; 19352657e9d9SBarry Smith const char *name; 19362657e9d9SBarry Smith 19372657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1938d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1939d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1940d45a07a7SBarry Smith char dir[1024]; 1941d45a07a7SBarry Smith 1942e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1943a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 19442657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 19452657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 19462657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1947d763cef2SBarry Smith } 19480acecf5bSBarry Smith if (ts->ops->view) { 19490acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 19500acecf5bSBarry Smith } 1951f05ece33SBarry Smith #endif 1952f05ece33SBarry Smith } 1953f05ece33SBarry Smith 1954b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1955251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1956b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1957d763cef2SBarry Smith PetscFunctionReturn(0); 1958d763cef2SBarry Smith } 1959d763cef2SBarry Smith 1960d763cef2SBarry Smith 1961b07ff414SBarry Smith /*@ 1962d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1963d763cef2SBarry Smith the timesteppers. 1964d763cef2SBarry Smith 19653f9fe445SBarry Smith Logically Collective on TS 1966d763cef2SBarry Smith 1967d763cef2SBarry Smith Input Parameters: 1968d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1969d763cef2SBarry Smith - usrP - optional user context 1970d763cef2SBarry Smith 1971daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 1972daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1973daf670e6SBarry Smith 1974d763cef2SBarry Smith Level: intermediate 1975d763cef2SBarry Smith 1976d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1977d763cef2SBarry Smith 1978d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1979d763cef2SBarry Smith @*/ 19807087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1981d763cef2SBarry Smith { 1982d763cef2SBarry Smith PetscFunctionBegin; 19830700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1984d763cef2SBarry Smith ts->user = usrP; 1985d763cef2SBarry Smith PetscFunctionReturn(0); 1986d763cef2SBarry Smith } 1987d763cef2SBarry Smith 1988b07ff414SBarry Smith /*@ 1989d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1990d763cef2SBarry Smith timestepper. 1991d763cef2SBarry Smith 1992d763cef2SBarry Smith Not Collective 1993d763cef2SBarry Smith 1994d763cef2SBarry Smith Input Parameter: 1995d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1996d763cef2SBarry Smith 1997d763cef2SBarry Smith Output Parameter: 1998d763cef2SBarry Smith . usrP - user context 1999d763cef2SBarry Smith 2000daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 2001daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2002daf670e6SBarry Smith 2003d763cef2SBarry Smith Level: intermediate 2004d763cef2SBarry Smith 2005d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 2006d763cef2SBarry Smith 2007d763cef2SBarry Smith .seealso: TSSetApplicationContext() 2008d763cef2SBarry Smith @*/ 2009e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 2010d763cef2SBarry Smith { 2011d763cef2SBarry Smith PetscFunctionBegin; 20120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2013e71120c6SJed Brown *(void**)usrP = ts->user; 2014d763cef2SBarry Smith PetscFunctionReturn(0); 2015d763cef2SBarry Smith } 2016d763cef2SBarry Smith 2017d763cef2SBarry Smith /*@ 2018b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 2019d763cef2SBarry Smith 2020d763cef2SBarry Smith Not Collective 2021d763cef2SBarry Smith 2022d763cef2SBarry Smith Input Parameter: 2023d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2024d763cef2SBarry Smith 2025d763cef2SBarry Smith Output Parameter: 2026b8123daeSJed Brown . iter - number of steps completed so far 2027d763cef2SBarry Smith 2028d763cef2SBarry Smith Level: intermediate 2029d763cef2SBarry Smith 2030d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 20319be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 2032d763cef2SBarry Smith @*/ 20337087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 2034d763cef2SBarry Smith { 2035d763cef2SBarry Smith PetscFunctionBegin; 20360700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20374482741eSBarry Smith PetscValidIntPointer(iter,2); 2038d763cef2SBarry Smith *iter = ts->steps; 2039d763cef2SBarry Smith PetscFunctionReturn(0); 2040d763cef2SBarry Smith } 2041d763cef2SBarry Smith 2042d763cef2SBarry Smith /*@ 2043d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 2044d763cef2SBarry Smith as well as the initial time. 2045d763cef2SBarry Smith 20463f9fe445SBarry Smith Logically Collective on TS 2047d763cef2SBarry Smith 2048d763cef2SBarry Smith Input Parameters: 2049d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2050d763cef2SBarry Smith . initial_time - the initial time 2051d763cef2SBarry Smith - time_step - the size of the timestep 2052d763cef2SBarry Smith 2053d763cef2SBarry Smith Level: intermediate 2054d763cef2SBarry Smith 2055d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 2056d763cef2SBarry Smith 2057d763cef2SBarry Smith .keywords: TS, set, initial, timestep 2058d763cef2SBarry Smith @*/ 20597087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 2060d763cef2SBarry Smith { 2061e144a568SJed Brown PetscErrorCode ierr; 2062e144a568SJed Brown 2063d763cef2SBarry Smith PetscFunctionBegin; 20640700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2065e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 2066d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 2067d763cef2SBarry Smith PetscFunctionReturn(0); 2068d763cef2SBarry Smith } 2069d763cef2SBarry Smith 2070d763cef2SBarry Smith /*@ 2071d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2072d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2073d763cef2SBarry Smith 20743f9fe445SBarry Smith Logically Collective on TS 2075d763cef2SBarry Smith 2076d763cef2SBarry Smith Input Parameters: 2077d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2078d763cef2SBarry Smith - time_step - the size of the timestep 2079d763cef2SBarry Smith 2080d763cef2SBarry Smith Level: intermediate 2081d763cef2SBarry Smith 2082d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2083d763cef2SBarry Smith 2084d763cef2SBarry Smith .keywords: TS, set, timestep 2085d763cef2SBarry Smith @*/ 20867087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 2087d763cef2SBarry Smith { 2088d763cef2SBarry Smith PetscFunctionBegin; 20890700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2090c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 2091d763cef2SBarry Smith ts->time_step = time_step; 2092d763cef2SBarry Smith PetscFunctionReturn(0); 2093d763cef2SBarry Smith } 2094d763cef2SBarry Smith 2095a43b19c4SJed Brown /*@ 209649354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 209749354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 209849354f04SShri Abhyankar or just return at the final time TS computed. 2099a43b19c4SJed Brown 2100a43b19c4SJed Brown Logically Collective on TS 2101a43b19c4SJed Brown 2102a43b19c4SJed Brown Input Parameter: 2103a43b19c4SJed Brown + ts - the time-step context 210449354f04SShri Abhyankar - eftopt - exact final time option 2105a43b19c4SJed Brown 2106feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2107feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2108feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2109feed9e9dSBarry Smith 2110feed9e9dSBarry Smith Options Database: 2111feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2112feed9e9dSBarry Smith 2113ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 2114ee346746SBarry Smith then the final time you selected. 2115ee346746SBarry Smith 2116a43b19c4SJed Brown Level: beginner 2117a43b19c4SJed Brown 2118a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 2119a43b19c4SJed Brown @*/ 212049354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 2121a43b19c4SJed Brown { 2122a43b19c4SJed Brown PetscFunctionBegin; 2123a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 212449354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 212549354f04SShri Abhyankar ts->exact_final_time = eftopt; 2126a43b19c4SJed Brown PetscFunctionReturn(0); 2127a43b19c4SJed Brown } 2128a43b19c4SJed Brown 2129d763cef2SBarry Smith /*@ 2130d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2131d763cef2SBarry Smith 2132d763cef2SBarry Smith Not Collective 2133d763cef2SBarry Smith 2134d763cef2SBarry Smith Input Parameter: 2135d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2136d763cef2SBarry Smith 2137d763cef2SBarry Smith Output Parameter: 2138d763cef2SBarry Smith . dt - the current timestep size 2139d763cef2SBarry Smith 2140d763cef2SBarry Smith Level: intermediate 2141d763cef2SBarry Smith 2142d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2143d763cef2SBarry Smith 2144d763cef2SBarry Smith .keywords: TS, get, timestep 2145d763cef2SBarry Smith @*/ 21467087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 2147d763cef2SBarry Smith { 2148d763cef2SBarry Smith PetscFunctionBegin; 21490700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2150f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 2151d763cef2SBarry Smith *dt = ts->time_step; 2152d763cef2SBarry Smith PetscFunctionReturn(0); 2153d763cef2SBarry Smith } 2154d763cef2SBarry Smith 2155d8e5e3e6SSatish Balay /*@ 2156d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2157d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2158d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2159d763cef2SBarry Smith the solution at the next timestep has been calculated. 2160d763cef2SBarry Smith 2161d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 2162d763cef2SBarry Smith 2163d763cef2SBarry Smith Input Parameter: 2164d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2165d763cef2SBarry Smith 2166d763cef2SBarry Smith Output Parameter: 2167d763cef2SBarry Smith . v - the vector containing the solution 2168d763cef2SBarry Smith 216963e21af5SBarry Smith Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested 217063e21af5SBarry Smith final time. It returns the solution at the next timestep. 217163e21af5SBarry Smith 2172d763cef2SBarry Smith Level: intermediate 2173d763cef2SBarry Smith 2174b2bf4f3aSDebojyoti Ghosh .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents() 2175d763cef2SBarry Smith 2176d763cef2SBarry Smith .keywords: TS, timestep, get, solution 2177d763cef2SBarry Smith @*/ 21787087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 2179d763cef2SBarry Smith { 2180d763cef2SBarry Smith PetscFunctionBegin; 21810700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 21824482741eSBarry Smith PetscValidPointer(v,2); 21838737fe31SLisandro Dalcin *v = ts->vec_sol; 2184d763cef2SBarry Smith PetscFunctionReturn(0); 2185d763cef2SBarry Smith } 2186d763cef2SBarry Smith 218703fe5f5eSDebojyoti Ghosh /*@ 2188b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 218903fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2190b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 219103fe5f5eSDebojyoti Ghosh structure as the solution vector. 219203fe5f5eSDebojyoti Ghosh 219303fe5f5eSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 219403fe5f5eSDebojyoti Ghosh 219503fe5f5eSDebojyoti Ghosh Parameters : 219603fe5f5eSDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 2197b2bf4f3aSDebojyoti Ghosh . n - If v is PETSC_NULL, then the number of solution components is 2198b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 219903fe5f5eSDebojyoti Ghosh returned in v. 2200b2bf4f3aSDebojyoti Ghosh . v - the vector containing the n-th solution component 220103fe5f5eSDebojyoti Ghosh (may be PETSC_NULL to use this function to find out 2202b2bf4f3aSDebojyoti Ghosh the number of solutions components). 220303fe5f5eSDebojyoti Ghosh 22044cdd57e5SDebojyoti Ghosh Level: advanced 220503fe5f5eSDebojyoti Ghosh 220603fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution() 220703fe5f5eSDebojyoti Ghosh 220803fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution 220903fe5f5eSDebojyoti Ghosh @*/ 2210b2bf4f3aSDebojyoti Ghosh PetscErrorCode TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v) 221103fe5f5eSDebojyoti Ghosh { 221203fe5f5eSDebojyoti Ghosh PetscErrorCode ierr; 221303fe5f5eSDebojyoti Ghosh 221403fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 221503fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2216b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 221703fe5f5eSDebojyoti Ghosh else { 2218b2bf4f3aSDebojyoti Ghosh ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr); 221903fe5f5eSDebojyoti Ghosh } 222003fe5f5eSDebojyoti Ghosh PetscFunctionReturn(0); 222103fe5f5eSDebojyoti Ghosh } 222203fe5f5eSDebojyoti Ghosh 22234cdd57e5SDebojyoti Ghosh /*@ 22244cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 22254cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 22264cdd57e5SDebojyoti Ghosh 22274cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 22284cdd57e5SDebojyoti Ghosh 22294cdd57e5SDebojyoti Ghosh Parameters : 22304cdd57e5SDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 22314cdd57e5SDebojyoti Ghosh . v - the vector containing the auxiliary solution 22324cdd57e5SDebojyoti Ghosh 22334cdd57e5SDebojyoti Ghosh Level: intermediate 22344cdd57e5SDebojyoti Ghosh 22354cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution() 22364cdd57e5SDebojyoti Ghosh 22374cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution 22384cdd57e5SDebojyoti Ghosh @*/ 22394cdd57e5SDebojyoti Ghosh PetscErrorCode TSGetAuxSolution(TS ts,Vec *v) 22404cdd57e5SDebojyoti Ghosh { 22414cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 22424cdd57e5SDebojyoti Ghosh 22434cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 22444cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2245f6356ec7SDebojyoti Ghosh if (ts->ops->getauxsolution) { 22464cdd57e5SDebojyoti Ghosh ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr); 2247f6356ec7SDebojyoti Ghosh } else { 2248f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v); CHKERRQ(ierr); 2249f6356ec7SDebojyoti Ghosh } 22504cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 22514cdd57e5SDebojyoti Ghosh } 22524cdd57e5SDebojyoti Ghosh 22534cdd57e5SDebojyoti Ghosh /*@ 22544cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 22554cdd57e5SDebojyoti Ghosh TSType has an error estimation functionality. 22564cdd57e5SDebojyoti Ghosh 22574cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 22584cdd57e5SDebojyoti Ghosh 22599657682dSDebojyoti Ghosh Note: MUST call after TSSetUp() 22609657682dSDebojyoti Ghosh 22614cdd57e5SDebojyoti Ghosh Parameters : 22624cdd57e5SDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 2263657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 22644cdd57e5SDebojyoti Ghosh . v - the vector containing the error (same size as the solution). 22654cdd57e5SDebojyoti Ghosh 22664cdd57e5SDebojyoti Ghosh Level: intermediate 22674cdd57e5SDebojyoti Ghosh 226857df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError() 22694cdd57e5SDebojyoti Ghosh 22704cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error 22714cdd57e5SDebojyoti Ghosh @*/ 22720a01e1b2SEmil Constantinescu PetscErrorCode TSGetTimeError(TS ts,PetscInt n,Vec *v) 22734cdd57e5SDebojyoti Ghosh { 22744cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 22754cdd57e5SDebojyoti Ghosh 22764cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 22774cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2278f6356ec7SDebojyoti Ghosh if (ts->ops->gettimeerror) { 22790a01e1b2SEmil Constantinescu ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr); 2280f6356ec7SDebojyoti Ghosh } else { 2281f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v);CHKERRQ(ierr); 2282f6356ec7SDebojyoti Ghosh } 22834cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 22844cdd57e5SDebojyoti Ghosh } 22854cdd57e5SDebojyoti Ghosh 228657df6a1bSDebojyoti Ghosh /*@ 228757df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 228857df6a1bSDebojyoti Ghosh TSType has an error estimation functionality. This can be used 228957df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 229057df6a1bSDebojyoti Ghosh 229157df6a1bSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 229257df6a1bSDebojyoti Ghosh 229357df6a1bSDebojyoti Ghosh Parameters : 229457df6a1bSDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 229557df6a1bSDebojyoti Ghosh . v - the vector containing the error (same size as the solution). 229657df6a1bSDebojyoti Ghosh 229757df6a1bSDebojyoti Ghosh Level: intermediate 229857df6a1bSDebojyoti Ghosh 229957df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError) 230057df6a1bSDebojyoti Ghosh 230157df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error 230257df6a1bSDebojyoti Ghosh @*/ 230357df6a1bSDebojyoti Ghosh PetscErrorCode TSSetTimeError(TS ts,Vec v) 230457df6a1bSDebojyoti Ghosh { 230557df6a1bSDebojyoti Ghosh PetscErrorCode ierr; 230657df6a1bSDebojyoti Ghosh 230757df6a1bSDebojyoti Ghosh PetscFunctionBegin; 230857df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 23099657682dSDebojyoti Ghosh if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first"); 231057df6a1bSDebojyoti Ghosh if (ts->ops->settimeerror) { 231157df6a1bSDebojyoti Ghosh ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr); 231257df6a1bSDebojyoti Ghosh } 231357df6a1bSDebojyoti Ghosh PetscFunctionReturn(0); 231457df6a1bSDebojyoti Ghosh } 231557df6a1bSDebojyoti Ghosh 2316c235aa8dSHong Zhang /*@ 2317dfb21088SHong Zhang TSGetCostGradients - Returns the gradients from the TSAdjointSolve() 2318c235aa8dSHong Zhang 2319c235aa8dSHong Zhang Not Collective, but Vec returned is parallel if TS is parallel 2320c235aa8dSHong Zhang 2321c235aa8dSHong Zhang Input Parameter: 2322c235aa8dSHong Zhang . ts - the TS context obtained from TSCreate() 2323c235aa8dSHong Zhang 2324c235aa8dSHong Zhang Output Parameter: 2325abc2977eSBarry Smith + lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 2326abc2977eSBarry Smith - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 2327c235aa8dSHong Zhang 2328c235aa8dSHong Zhang Level: intermediate 2329c235aa8dSHong Zhang 2330c235aa8dSHong Zhang .seealso: TSGetTimeStep() 2331c235aa8dSHong Zhang 2332c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity 2333c235aa8dSHong Zhang @*/ 2334dfb21088SHong Zhang PetscErrorCode TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu) 2335c235aa8dSHong Zhang { 2336c235aa8dSHong Zhang PetscFunctionBegin; 2337c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2338abc2977eSBarry Smith if (numcost) *numcost = ts->numcost; 2339abc2977eSBarry Smith if (lambda) *lambda = ts->vecs_sensi; 2340abc2977eSBarry Smith if (mu) *mu = ts->vecs_sensip; 2341c235aa8dSHong Zhang PetscFunctionReturn(0); 2342c235aa8dSHong Zhang } 2343c235aa8dSHong Zhang 2344bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2345d8e5e3e6SSatish Balay /*@ 2346bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2347d763cef2SBarry Smith 2348bdad233fSMatthew Knepley Not collective 2349d763cef2SBarry Smith 2350bdad233fSMatthew Knepley Input Parameters: 2351bdad233fSMatthew Knepley + ts - The TS 2352bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2353d763cef2SBarry Smith .vb 23540910c330SBarry Smith U_t - A U = 0 (linear) 23550910c330SBarry Smith U_t - A(t) U = 0 (linear) 23560910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2357d763cef2SBarry Smith .ve 2358d763cef2SBarry Smith 2359d763cef2SBarry Smith Level: beginner 2360d763cef2SBarry Smith 2361bdad233fSMatthew Knepley .keywords: TS, problem type 2362bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2363d763cef2SBarry Smith @*/ 23647087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2365a7cc72afSBarry Smith { 23669e2a6581SJed Brown PetscErrorCode ierr; 23679e2a6581SJed Brown 2368d763cef2SBarry Smith PetscFunctionBegin; 23690700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2370bdad233fSMatthew Knepley ts->problem_type = type; 23719e2a6581SJed Brown if (type == TS_LINEAR) { 23729e2a6581SJed Brown SNES snes; 23739e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 23749e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 23759e2a6581SJed Brown } 2376d763cef2SBarry Smith PetscFunctionReturn(0); 2377d763cef2SBarry Smith } 2378d763cef2SBarry Smith 2379bdad233fSMatthew Knepley /*@C 2380bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2381bdad233fSMatthew Knepley 2382bdad233fSMatthew Knepley Not collective 2383bdad233fSMatthew Knepley 2384bdad233fSMatthew Knepley Input Parameter: 2385bdad233fSMatthew Knepley . ts - The TS 2386bdad233fSMatthew Knepley 2387bdad233fSMatthew Knepley Output Parameter: 2388bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2389bdad233fSMatthew Knepley .vb 2390089b2837SJed Brown M U_t = A U 2391089b2837SJed Brown M(t) U_t = A(t) U 2392b5abc632SBarry Smith F(t,U,U_t) 2393bdad233fSMatthew Knepley .ve 2394bdad233fSMatthew Knepley 2395bdad233fSMatthew Knepley Level: beginner 2396bdad233fSMatthew Knepley 2397bdad233fSMatthew Knepley .keywords: TS, problem type 2398bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2399bdad233fSMatthew Knepley @*/ 24007087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2401a7cc72afSBarry Smith { 2402bdad233fSMatthew Knepley PetscFunctionBegin; 24030700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 24044482741eSBarry Smith PetscValidIntPointer(type,2); 2405bdad233fSMatthew Knepley *type = ts->problem_type; 2406bdad233fSMatthew Knepley PetscFunctionReturn(0); 2407bdad233fSMatthew Knepley } 2408d763cef2SBarry Smith 2409d763cef2SBarry Smith /*@ 2410d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 2411d763cef2SBarry Smith of a timestepper. 2412d763cef2SBarry Smith 2413d763cef2SBarry Smith Collective on TS 2414d763cef2SBarry Smith 2415d763cef2SBarry Smith Input Parameter: 2416d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2417d763cef2SBarry Smith 2418d763cef2SBarry Smith Notes: 2419d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 2420d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 2421d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 2422d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 2423d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 2424d763cef2SBarry Smith 2425d763cef2SBarry Smith Level: advanced 2426d763cef2SBarry Smith 2427d763cef2SBarry Smith .keywords: TS, timestep, setup 2428d763cef2SBarry Smith 2429d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 2430d763cef2SBarry Smith @*/ 24317087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 2432d763cef2SBarry Smith { 2433dfbe8321SBarry Smith PetscErrorCode ierr; 24346c6b9e74SPeter Brune DM dm; 24356c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 2436d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 2437cd11d68dSLisandro Dalcin TSIFunction ifun; 24386c6b9e74SPeter Brune TSIJacobian ijac; 2439efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 24406c6b9e74SPeter Brune TSRHSJacobian rhsjac; 2441d763cef2SBarry Smith 2442d763cef2SBarry Smith PetscFunctionBegin; 24430700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2444277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 2445277b19d0SLisandro Dalcin 24467adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 2447cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 2448cd11d68dSLisandro Dalcin ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr); 2449d763cef2SBarry Smith } 2450277b19d0SLisandro Dalcin 2451277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 2452277b19d0SLisandro Dalcin 2453e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 2454e1244c69SJed Brown Mat Amat,Pmat; 2455e1244c69SJed Brown SNES snes; 2456e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2457e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 2458e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2459e1244c69SJed Brown * have displaced the RHS matrix */ 2460e1244c69SJed Brown if (Amat == ts->Arhs) { 2461e1244c69SJed Brown ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 2462e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 2463e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 2464e1244c69SJed Brown } 2465e1244c69SJed Brown if (Pmat == ts->Brhs) { 2466e1244c69SJed Brown ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 2467e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 2468e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 2469e1244c69SJed Brown } 2470e1244c69SJed Brown } 2471277b19d0SLisandro Dalcin if (ts->ops->setup) { 2472000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 2473277b19d0SLisandro Dalcin } 2474277b19d0SLisandro Dalcin 2475a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 24766c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 24776c6b9e74SPeter Brune */ 24786c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 24790298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 24806c6b9e74SPeter Brune if (!func) { 24816c6b9e74SPeter Brune ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 24826c6b9e74SPeter Brune } 2483a6772fa2SLisandro Dalcin /* If the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it. 24846c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 24856c6b9e74SPeter Brune */ 24860298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 24870298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 2488efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr); 24890298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 2490efe9872eSLisandro Dalcin if (!jac && (ijac || i2jac || rhsjac)) { 24916c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 24926c6b9e74SPeter Brune } 2493c0517034SDebojyoti Ghosh 2494c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 2495c0517034SDebojyoti Ghosh if (ts->ops->startingmethod) { 2496c0517034SDebojyoti Ghosh ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr); 2497c0517034SDebojyoti Ghosh } 2498c0517034SDebojyoti Ghosh 2499277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2500277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2501277b19d0SLisandro Dalcin } 2502277b19d0SLisandro Dalcin 2503f6a906c0SBarry Smith /*@ 2504f6a906c0SBarry Smith TSAdjointSetUp - Sets up the internal data structures for the later use 2505f6a906c0SBarry Smith of an adjoint solver 2506f6a906c0SBarry Smith 2507f6a906c0SBarry Smith Collective on TS 2508f6a906c0SBarry Smith 2509f6a906c0SBarry Smith Input Parameter: 2510f6a906c0SBarry Smith . ts - the TS context obtained from TSCreate() 2511f6a906c0SBarry Smith 2512f6a906c0SBarry Smith Level: advanced 2513f6a906c0SBarry Smith 2514f6a906c0SBarry Smith .keywords: TS, timestep, setup 2515f6a906c0SBarry Smith 2516947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients() 2517f6a906c0SBarry Smith @*/ 2518f6a906c0SBarry Smith PetscErrorCode TSAdjointSetUp(TS ts) 2519f6a906c0SBarry Smith { 2520f6a906c0SBarry Smith PetscErrorCode ierr; 2521f6a906c0SBarry Smith 2522f6a906c0SBarry Smith PetscFunctionBegin; 2523f6a906c0SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2524f6a906c0SBarry Smith if (ts->adjointsetupcalled) PetscFunctionReturn(0); 2525dfb21088SHong Zhang if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first"); 2526d8151eeaSBarry Smith 2527947abb85SHong Zhang if (ts->vec_costintegral) { /* if there is integral in the cost function*/ 2528d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2529d8151eeaSBarry Smith if (ts->vecs_sensip){ 2530d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2531d8151eeaSBarry Smith } 2532947abb85SHong Zhang } 2533d8151eeaSBarry Smith 253442f2b339SBarry Smith if (ts->ops->adjointsetup) { 253542f2b339SBarry Smith ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr); 2536f6a906c0SBarry Smith } 2537f6a906c0SBarry Smith ts->adjointsetupcalled = PETSC_TRUE; 2538f6a906c0SBarry Smith PetscFunctionReturn(0); 2539f6a906c0SBarry Smith } 2540f6a906c0SBarry Smith 2541277b19d0SLisandro Dalcin /*@ 2542277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2543277b19d0SLisandro Dalcin 2544277b19d0SLisandro Dalcin Collective on TS 2545277b19d0SLisandro Dalcin 2546277b19d0SLisandro Dalcin Input Parameter: 2547277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2548277b19d0SLisandro Dalcin 2549277b19d0SLisandro Dalcin Level: beginner 2550277b19d0SLisandro Dalcin 2551277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 2552277b19d0SLisandro Dalcin 2553277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2554277b19d0SLisandro Dalcin @*/ 2555277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2556277b19d0SLisandro Dalcin { 2557277b19d0SLisandro Dalcin PetscErrorCode ierr; 2558277b19d0SLisandro Dalcin 2559277b19d0SLisandro Dalcin PetscFunctionBegin; 2560277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2561b18ea86cSHong Zhang 2562277b19d0SLisandro Dalcin if (ts->ops->reset) { 2563277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2564277b19d0SLisandro Dalcin } 2565277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2566e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2567bbd56ea5SKarl Rupp 25684e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 25694e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2570214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 25716bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2572efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 2573e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2574e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 257538637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2576bbd56ea5SKarl Rupp 2577947abb85SHong Zhang if (ts->vec_costintegral) { 2578d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2579d8151eeaSBarry Smith if (ts->vecs_drdp){ 2580d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2581d8151eeaSBarry Smith } 2582947abb85SHong Zhang } 2583d8151eeaSBarry Smith ts->vecs_sensi = NULL; 2584d8151eeaSBarry Smith ts->vecs_sensip = NULL; 2585ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 25862c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 258736eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2588277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2589d763cef2SBarry Smith PetscFunctionReturn(0); 2590d763cef2SBarry Smith } 2591d763cef2SBarry Smith 2592d8e5e3e6SSatish Balay /*@ 2593d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2594d763cef2SBarry Smith with TSCreate(). 2595d763cef2SBarry Smith 2596d763cef2SBarry Smith Collective on TS 2597d763cef2SBarry Smith 2598d763cef2SBarry Smith Input Parameter: 2599d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2600d763cef2SBarry Smith 2601d763cef2SBarry Smith Level: beginner 2602d763cef2SBarry Smith 2603d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2604d763cef2SBarry Smith 2605d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2606d763cef2SBarry Smith @*/ 26076bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2608d763cef2SBarry Smith { 26096849ba73SBarry Smith PetscErrorCode ierr; 2610d763cef2SBarry Smith 2611d763cef2SBarry Smith PetscFunctionBegin; 26126bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 26136bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 26146bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2615d763cef2SBarry Smith 26166bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2617277b19d0SLisandro Dalcin 2618e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2619e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 26206bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 26216d4c513bSLisandro Dalcin 2622bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2623bc952696SBarry Smith 262484df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 26256427ac75SLisandro Dalcin ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr); 26266427ac75SLisandro Dalcin 26276bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 26286bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 26296bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 26300dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 26316d4c513bSLisandro Dalcin 2632a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2633d763cef2SBarry Smith PetscFunctionReturn(0); 2634d763cef2SBarry Smith } 2635d763cef2SBarry Smith 2636d8e5e3e6SSatish Balay /*@ 2637d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2638d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2639d763cef2SBarry Smith 2640d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2641d763cef2SBarry Smith 2642d763cef2SBarry Smith Input Parameter: 2643d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2644d763cef2SBarry Smith 2645d763cef2SBarry Smith Output Parameter: 2646d763cef2SBarry Smith . snes - the nonlinear solver context 2647d763cef2SBarry Smith 2648d763cef2SBarry Smith Notes: 2649d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2650d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 265194b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2652d763cef2SBarry Smith 2653d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 26540298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2655d763cef2SBarry Smith 2656d763cef2SBarry Smith Level: beginner 2657d763cef2SBarry Smith 2658d763cef2SBarry Smith .keywords: timestep, get, SNES 2659d763cef2SBarry Smith @*/ 26607087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2661d763cef2SBarry Smith { 2662d372ba47SLisandro Dalcin PetscErrorCode ierr; 2663d372ba47SLisandro Dalcin 2664d763cef2SBarry Smith PetscFunctionBegin; 26650700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26664482741eSBarry Smith PetscValidPointer(snes,2); 2667d372ba47SLisandro Dalcin if (!ts->snes) { 2668ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 26690298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 26703bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2671d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2672496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 26739e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 26749e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 26759e2a6581SJed Brown } 2676d372ba47SLisandro Dalcin } 2677d763cef2SBarry Smith *snes = ts->snes; 2678d763cef2SBarry Smith PetscFunctionReturn(0); 2679d763cef2SBarry Smith } 2680d763cef2SBarry Smith 2681deb2cd25SJed Brown /*@ 2682deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2683deb2cd25SJed Brown 2684deb2cd25SJed Brown Collective 2685deb2cd25SJed Brown 2686deb2cd25SJed Brown Input Parameter: 2687deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2688deb2cd25SJed Brown - snes - the nonlinear solver context 2689deb2cd25SJed Brown 2690deb2cd25SJed Brown Notes: 2691deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2692deb2cd25SJed Brown 2693deb2cd25SJed Brown Level: developer 2694deb2cd25SJed Brown 2695deb2cd25SJed Brown .keywords: timestep, set, SNES 2696deb2cd25SJed Brown @*/ 2697deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2698deb2cd25SJed Brown { 2699deb2cd25SJed Brown PetscErrorCode ierr; 2700d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2701deb2cd25SJed Brown 2702deb2cd25SJed Brown PetscFunctionBegin; 2703deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2704deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2705deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2706deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2707bbd56ea5SKarl Rupp 2708deb2cd25SJed Brown ts->snes = snes; 2709bbd56ea5SKarl Rupp 27100298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 27110298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2712740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 27130298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2714740132f1SEmil Constantinescu } 2715deb2cd25SJed Brown PetscFunctionReturn(0); 2716deb2cd25SJed Brown } 2717deb2cd25SJed Brown 2718d8e5e3e6SSatish Balay /*@ 271994b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2720d763cef2SBarry Smith a TS (timestepper) context. 2721d763cef2SBarry Smith 272294b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2723d763cef2SBarry Smith 2724d763cef2SBarry Smith Input Parameter: 2725d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2726d763cef2SBarry Smith 2727d763cef2SBarry Smith Output Parameter: 272894b7f48cSBarry Smith . ksp - the nonlinear solver context 2729d763cef2SBarry Smith 2730d763cef2SBarry Smith Notes: 273194b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2732d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2733d763cef2SBarry Smith KSP and PC contexts as well. 2734d763cef2SBarry Smith 273594b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 27360298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2737d763cef2SBarry Smith 2738d763cef2SBarry Smith Level: beginner 2739d763cef2SBarry Smith 274094b7f48cSBarry Smith .keywords: timestep, get, KSP 2741d763cef2SBarry Smith @*/ 27427087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2743d763cef2SBarry Smith { 2744d372ba47SLisandro Dalcin PetscErrorCode ierr; 2745089b2837SJed Brown SNES snes; 2746d372ba47SLisandro Dalcin 2747d763cef2SBarry Smith PetscFunctionBegin; 27480700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27494482741eSBarry Smith PetscValidPointer(ksp,2); 275017186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2751e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2752089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2753089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2754d763cef2SBarry Smith PetscFunctionReturn(0); 2755d763cef2SBarry Smith } 2756d763cef2SBarry Smith 2757d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2758d763cef2SBarry Smith 2759adb62b0dSMatthew Knepley /*@ 2760adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2761adb62b0dSMatthew Knepley maximum time for iteration. 2762adb62b0dSMatthew Knepley 27633f9fe445SBarry Smith Not Collective 2764adb62b0dSMatthew Knepley 2765adb62b0dSMatthew Knepley Input Parameters: 2766adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 27670298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 27680298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2769adb62b0dSMatthew Knepley 2770adb62b0dSMatthew Knepley Level: intermediate 2771adb62b0dSMatthew Knepley 2772adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2773adb62b0dSMatthew Knepley @*/ 27747087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2775adb62b0dSMatthew Knepley { 2776adb62b0dSMatthew Knepley PetscFunctionBegin; 27770700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2778abc0a331SBarry Smith if (maxsteps) { 27794482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2780adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2781adb62b0dSMatthew Knepley } 2782abc0a331SBarry Smith if (maxtime) { 27834482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2784adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2785adb62b0dSMatthew Knepley } 2786adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2787adb62b0dSMatthew Knepley } 2788adb62b0dSMatthew Knepley 2789d763cef2SBarry Smith /*@ 2790d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2791d763cef2SBarry Smith maximum time for iteration. 2792d763cef2SBarry Smith 27933f9fe445SBarry Smith Logically Collective on TS 2794d763cef2SBarry Smith 2795d763cef2SBarry Smith Input Parameters: 2796d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2797d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2798d763cef2SBarry Smith - maxtime - final time to iterate to 2799d763cef2SBarry Smith 2800d763cef2SBarry Smith Options Database Keys: 2801d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 28023bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2803d763cef2SBarry Smith 2804d763cef2SBarry Smith Notes: 2805d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2806d763cef2SBarry Smith 2807d763cef2SBarry Smith Level: intermediate 2808d763cef2SBarry Smith 2809d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2810a43b19c4SJed Brown 2811a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2812d763cef2SBarry Smith @*/ 28137087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2814d763cef2SBarry Smith { 2815d763cef2SBarry Smith PetscFunctionBegin; 28160700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2817c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2818c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 281939b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 282039b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2821d763cef2SBarry Smith PetscFunctionReturn(0); 2822d763cef2SBarry Smith } 2823d763cef2SBarry Smith 2824d763cef2SBarry Smith /*@ 2825d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2826d763cef2SBarry Smith for use by the TS routines. 2827d763cef2SBarry Smith 28283f9fe445SBarry Smith Logically Collective on TS and Vec 2829d763cef2SBarry Smith 2830d763cef2SBarry Smith Input Parameters: 2831d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 28320910c330SBarry Smith - u - the solution vector 2833d763cef2SBarry Smith 2834d763cef2SBarry Smith Level: beginner 2835d763cef2SBarry Smith 2836d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2837d763cef2SBarry Smith @*/ 28380910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2839d763cef2SBarry Smith { 28408737fe31SLisandro Dalcin PetscErrorCode ierr; 2841496e6a7aSJed Brown DM dm; 28428737fe31SLisandro Dalcin 2843d763cef2SBarry Smith PetscFunctionBegin; 28440700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28450910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 28460910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 28476bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 28480910c330SBarry Smith ts->vec_sol = u; 2849bbd56ea5SKarl Rupp 2850496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 28510910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2852d763cef2SBarry Smith PetscFunctionReturn(0); 2853d763cef2SBarry Smith } 2854d763cef2SBarry Smith 285573b18844SBarry Smith /*@ 285673b18844SBarry Smith TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time 285773b18844SBarry Smith 285873b18844SBarry Smith Logically Collective on TS 285973b18844SBarry Smith 286073b18844SBarry Smith Input Parameters: 286173b18844SBarry Smith + ts - the TS context obtained from TSCreate() 286273b18844SBarry Smith . steps - number of steps to use 286373b18844SBarry Smith 286473b18844SBarry Smith Level: intermediate 286573b18844SBarry Smith 286673b18844SBarry Smith Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this 286773b18844SBarry Smith so as to integrate back to less than the original timestep 286873b18844SBarry Smith 286973b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations 287073b18844SBarry Smith 287173b18844SBarry Smith .seealso: TSSetExactFinalTime() 287273b18844SBarry Smith @*/ 287373b18844SBarry Smith PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps) 287473b18844SBarry Smith { 287573b18844SBarry Smith PetscFunctionBegin; 287673b18844SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 287773b18844SBarry Smith PetscValidLogicalCollectiveInt(ts,steps,2); 287873b18844SBarry Smith if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps"); 287973b18844SBarry 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"); 288073b18844SBarry Smith ts->adjoint_max_steps = steps; 288173b18844SBarry Smith PetscFunctionReturn(0); 288273b18844SBarry Smith } 288373b18844SBarry Smith 2884c235aa8dSHong Zhang /*@ 2885dfb21088SHong 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 28860cf82b59SBarry Smith for use by the TSAdjoint routines. 2887c235aa8dSHong Zhang 2888c235aa8dSHong Zhang Logically Collective on TS and Vec 2889c235aa8dSHong Zhang 2890c235aa8dSHong Zhang Input Parameters: 2891c235aa8dSHong Zhang + ts - the TS context obtained from TSCreate() 2892abc2977eSBarry 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 2893abc2977eSBarry Smith - mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters 2894c235aa8dSHong Zhang 2895c235aa8dSHong Zhang Level: beginner 2896c235aa8dSHong Zhang 2897abc2977eSBarry Smith Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime mu_i = df/dp|finaltime 28980cf82b59SBarry Smith 2899c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions 2900c235aa8dSHong Zhang @*/ 2901dfb21088SHong Zhang PetscErrorCode TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu) 2902c235aa8dSHong Zhang { 2903c235aa8dSHong Zhang PetscFunctionBegin; 2904c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2905abc2977eSBarry Smith PetscValidPointer(lambda,2); 2906abc2977eSBarry Smith ts->vecs_sensi = lambda; 2907abc2977eSBarry Smith ts->vecs_sensip = mu; 2908dfb21088SHong 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"); 2909abc2977eSBarry Smith ts->numcost = numcost; 2910ad8e2604SHong Zhang PetscFunctionReturn(0); 2911ad8e2604SHong Zhang } 2912ad8e2604SHong Zhang 2913ad8e2604SHong Zhang /*@C 29140cf82b59SBarry 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. 2915ad8e2604SHong Zhang 2916ad8e2604SHong Zhang Logically Collective on TS 2917ad8e2604SHong Zhang 2918ad8e2604SHong Zhang Input Parameters: 2919ad8e2604SHong Zhang + ts - The TS context obtained from TSCreate() 2920ad8e2604SHong Zhang - func - The function 2921ad8e2604SHong Zhang 2922ad8e2604SHong Zhang Calling sequence of func: 29230cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx); 292405755b9cSHong Zhang + t - current timestep 29250cf82b59SBarry Smith . y - input vector (current ODE solution) 292605755b9cSHong Zhang . A - output matrix 292705755b9cSHong Zhang - ctx - [optional] user-defined function context 2928ad8e2604SHong Zhang 2929ad8e2604SHong Zhang Level: intermediate 2930ad8e2604SHong Zhang 29310cf82b59SBarry 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 29320cf82b59SBarry Smith 2933ad8e2604SHong Zhang .keywords: TS, sensitivity 2934ad8e2604SHong Zhang .seealso: 2935ad8e2604SHong Zhang @*/ 29365bf8c567SBarry Smith PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 2937ad8e2604SHong Zhang { 2938ad8e2604SHong Zhang PetscErrorCode ierr; 2939ad8e2604SHong Zhang 2940ad8e2604SHong Zhang PetscFunctionBegin; 2941ad8e2604SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2942ad8e2604SHong Zhang if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 2943ad8e2604SHong Zhang 2944ad8e2604SHong Zhang ts->rhsjacobianp = func; 2945ad8e2604SHong Zhang ts->rhsjacobianpctx = ctx; 2946ad8e2604SHong Zhang if(Amat) { 2947ad8e2604SHong Zhang ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 2948ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2949ad8e2604SHong Zhang ts->Jacp = Amat; 2950ad8e2604SHong Zhang } 2951ad8e2604SHong Zhang PetscFunctionReturn(0); 2952ad8e2604SHong Zhang } 2953ad8e2604SHong Zhang 29540cf82b59SBarry Smith /*@C 29555bf8c567SBarry Smith TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function. 2956ad8e2604SHong Zhang 2957ad8e2604SHong Zhang Collective on TS 2958ad8e2604SHong Zhang 2959ad8e2604SHong Zhang Input Parameters: 2960ad8e2604SHong Zhang . ts - The TS context obtained from TSCreate() 2961ad8e2604SHong Zhang 2962ad8e2604SHong Zhang Level: developer 2963ad8e2604SHong Zhang 2964ad8e2604SHong Zhang .keywords: TS, sensitivity 29655bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian() 2966ad8e2604SHong Zhang @*/ 29675bf8c567SBarry Smith PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat) 2968ad8e2604SHong Zhang { 2969ad8e2604SHong Zhang PetscErrorCode ierr; 2970ad8e2604SHong Zhang 2971ad8e2604SHong Zhang PetscFunctionBegin; 2972ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2973ad8e2604SHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 2974ad8e2604SHong Zhang PetscValidPointer(Amat,4); 2975ad8e2604SHong Zhang 2976ad8e2604SHong Zhang PetscStackPush("TS user JacobianP function for sensitivity analysis"); 2977ad8e2604SHong Zhang ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 2978ad8e2604SHong Zhang PetscStackPop; 2979c235aa8dSHong Zhang PetscFunctionReturn(0); 2980c235aa8dSHong Zhang } 2981c235aa8dSHong Zhang 29826fd0887fSHong Zhang /*@C 2983dfb21088SHong Zhang TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions 29846fd0887fSHong Zhang 29856fd0887fSHong Zhang Logically Collective on TS 29866fd0887fSHong Zhang 29876fd0887fSHong Zhang Input Parameters: 29886fd0887fSHong Zhang + ts - the TS context obtained from TSCreate() 2989abc2977eSBarry Smith . numcost - number of gradients to be computed, this is the number of cost functions 29900cf82b59SBarry Smith . rf - routine for evaluating the integrand function 2991b612ec54SBarry Smith . drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y 2992b612ec54SBarry Smith . drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p 2993feaa1ddbSSatish Balay . fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run 2994b612ec54SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 29956fd0887fSHong Zhang 29960cf82b59SBarry Smith Calling sequence of rf: 29970cf82b59SBarry Smith $ rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx); 29986fd0887fSHong Zhang 29996fd0887fSHong Zhang + t - current timestep 30000cf82b59SBarry Smith . y - input vector 30010cf82b59SBarry Smith . f - function result; one vector entry for each cost function 30026fd0887fSHong Zhang - ctx - [optional] user-defined function context 30036fd0887fSHong Zhang 3004b612ec54SBarry Smith Calling sequence of drdyf: 30050cf82b59SBarry Smith $ PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx); 3006b612ec54SBarry Smith 3007b612ec54SBarry Smith Calling sequence of drdpf: 30080cf82b59SBarry Smith $ PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx); 3009b612ec54SBarry Smith 3010b612ec54SBarry Smith Level: intermediate 30116fd0887fSHong Zhang 3012abc2977eSBarry Smith Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions 30130cf82b59SBarry Smith 30146fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 30156fd0887fSHong Zhang 3016dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients() 30176fd0887fSHong Zhang @*/ 3018dfb21088SHong Zhang PetscErrorCode TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*), 3019d8151eeaSBarry Smith PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*), 3020b1cb36f3SHong Zhang PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*), 3021b1cb36f3SHong Zhang PetscBool fwd,void *ctx) 30226fd0887fSHong Zhang { 30236fd0887fSHong Zhang PetscErrorCode ierr; 30246fd0887fSHong Zhang 30256fd0887fSHong Zhang PetscFunctionBegin; 30266fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3027dfb21088SHong 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()"); 3028c72ed514SHong Zhang if (!ts->numcost) ts->numcost=numcost; 30296fd0887fSHong Zhang 3030b1cb36f3SHong Zhang ts->costintegralfwd = fwd; /* Evaluate the cost integral in forward run if fwd is true */ 3031abc2977eSBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr); 30322c39e106SBarry Smith ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr); 30330cf82b59SBarry Smith ts->costintegrand = rf; 303405755b9cSHong Zhang ts->costintegrandctx = ctx; 3035b612ec54SBarry Smith ts->drdyfunction = drdyf; 3036b612ec54SBarry Smith ts->drdpfunction = drdpf; 30376fd0887fSHong Zhang PetscFunctionReturn(0); 30386fd0887fSHong Zhang } 30396fd0887fSHong Zhang 304036eaed60SHong Zhang /*@ 3041dfb21088SHong Zhang TSGetCostIntegral - Returns the values of the integral term in the cost functions. 304236eaed60SHong Zhang It is valid to call the routine after a backward run. 304336eaed60SHong Zhang 304436eaed60SHong Zhang Not Collective 304536eaed60SHong Zhang 304636eaed60SHong Zhang Input Parameter: 304736eaed60SHong Zhang . ts - the TS context obtained from TSCreate() 304836eaed60SHong Zhang 304936eaed60SHong Zhang Output Parameter: 30502c39e106SBarry Smith . v - the vector containing the integrals for each cost function 305136eaed60SHong Zhang 305236eaed60SHong Zhang Level: intermediate 305336eaed60SHong Zhang 3054dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 305536eaed60SHong Zhang 305636eaed60SHong Zhang .keywords: TS, sensitivity analysis 305736eaed60SHong Zhang @*/ 3058dfb21088SHong Zhang PetscErrorCode TSGetCostIntegral(TS ts,Vec *v) 305936eaed60SHong Zhang { 306036eaed60SHong Zhang PetscFunctionBegin; 306136eaed60SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 306236eaed60SHong Zhang PetscValidPointer(v,2); 30632c39e106SBarry Smith *v = ts->vec_costintegral; 306436eaed60SHong Zhang PetscFunctionReturn(0); 306536eaed60SHong Zhang } 306636eaed60SHong Zhang 30676fd0887fSHong Zhang /*@ 30682c39e106SBarry Smith TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions. 30696fd0887fSHong Zhang 30706fd0887fSHong Zhang Input Parameters: 30716fd0887fSHong Zhang + ts - the TS context 30726fd0887fSHong Zhang . t - current time 30730cf82b59SBarry Smith - y - state vector, i.e. current solution 30746fd0887fSHong Zhang 30756fd0887fSHong Zhang Output Parameter: 3076abc2977eSBarry Smith . q - vector of size numcost to hold the outputs 30776fd0887fSHong Zhang 30786fd0887fSHong Zhang Note: 30796fd0887fSHong Zhang Most users should not need to explicitly call this routine, as it 30806fd0887fSHong Zhang is used internally within the sensitivity analysis context. 30816fd0887fSHong Zhang 30826fd0887fSHong Zhang Level: developer 30836fd0887fSHong Zhang 30846fd0887fSHong Zhang .keywords: TS, compute 30856fd0887fSHong Zhang 3086dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 30876fd0887fSHong Zhang @*/ 30880cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q) 30896fd0887fSHong Zhang { 30906fd0887fSHong Zhang PetscErrorCode ierr; 30916fd0887fSHong Zhang 30926fd0887fSHong Zhang PetscFunctionBegin; 30936fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30940cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 30956fd0887fSHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,4); 30966fd0887fSHong Zhang 30970cf82b59SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 3098e4680132SHong Zhang if (ts->costintegrand) { 3099e4680132SHong Zhang PetscStackPush("TS user integrand in the cost function"); 31000cf82b59SBarry Smith ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr); 31016fd0887fSHong Zhang PetscStackPop; 31026fd0887fSHong Zhang } else { 31036fd0887fSHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 31046fd0887fSHong Zhang } 31056fd0887fSHong Zhang 31060cf82b59SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 31076fd0887fSHong Zhang PetscFunctionReturn(0); 31086fd0887fSHong Zhang } 31096fd0887fSHong Zhang 311005755b9cSHong Zhang /*@ 3111d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 311205755b9cSHong Zhang 311305755b9cSHong Zhang Collective on TS 311405755b9cSHong Zhang 311505755b9cSHong Zhang Input Parameters: 311605755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 311705755b9cSHong Zhang 311805755b9cSHong Zhang Notes: 3119d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 312005755b9cSHong Zhang so most users would not generally call this routine themselves. 312105755b9cSHong Zhang 312205755b9cSHong Zhang Level: developer 312305755b9cSHong Zhang 312405755b9cSHong Zhang .keywords: TS, sensitivity 3125d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction() 312605755b9cSHong Zhang @*/ 31270cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy) 312805755b9cSHong Zhang { 312905755b9cSHong Zhang PetscErrorCode ierr; 313005755b9cSHong Zhang 313105755b9cSHong Zhang PetscFunctionBegin; 313205755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31330cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 313405755b9cSHong Zhang 313505755b9cSHong Zhang PetscStackPush("TS user DRDY function for sensitivity analysis"); 31360cf82b59SBarry Smith ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr); 313705755b9cSHong Zhang PetscStackPop; 313805755b9cSHong Zhang PetscFunctionReturn(0); 313905755b9cSHong Zhang } 314005755b9cSHong Zhang 314105755b9cSHong Zhang /*@ 3142d4aa0a58SBarry Smith TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 314305755b9cSHong Zhang 314405755b9cSHong Zhang Collective on TS 314505755b9cSHong Zhang 314605755b9cSHong Zhang Input Parameters: 314705755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 314805755b9cSHong Zhang 314905755b9cSHong Zhang Notes: 315005755b9cSHong Zhang TSDRDPFunction() is typically used for sensitivity implementation, 315105755b9cSHong Zhang so most users would not generally call this routine themselves. 315205755b9cSHong Zhang 315305755b9cSHong Zhang Level: developer 315405755b9cSHong Zhang 315505755b9cSHong Zhang .keywords: TS, sensitivity 3156d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction() 315705755b9cSHong Zhang @*/ 31580cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp) 315905755b9cSHong Zhang { 316005755b9cSHong Zhang PetscErrorCode ierr; 316105755b9cSHong Zhang 316205755b9cSHong Zhang PetscFunctionBegin; 316305755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31640cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 316505755b9cSHong Zhang 316605755b9cSHong Zhang PetscStackPush("TS user DRDP function for sensitivity analysis"); 31670cf82b59SBarry Smith ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr); 316805755b9cSHong Zhang PetscStackPop; 316905755b9cSHong Zhang PetscFunctionReturn(0); 317005755b9cSHong Zhang } 317105755b9cSHong Zhang 3172ac226902SBarry Smith /*@C 3173000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 31743f2090d5SJed Brown called once at the beginning of each time step. 3175000e7ae3SMatthew Knepley 31763f9fe445SBarry Smith Logically Collective on TS 3177000e7ae3SMatthew Knepley 3178000e7ae3SMatthew Knepley Input Parameters: 3179000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3180000e7ae3SMatthew Knepley - func - The function 3181000e7ae3SMatthew Knepley 3182000e7ae3SMatthew Knepley Calling sequence of func: 3183000e7ae3SMatthew Knepley . func (TS ts); 3184000e7ae3SMatthew Knepley 3185000e7ae3SMatthew Knepley Level: intermediate 3186000e7ae3SMatthew Knepley 3187b8123daeSJed Brown Note: 3188b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 3189b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 3190b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 3191b8123daeSJed Brown 3192000e7ae3SMatthew Knepley .keywords: TS, timestep 31939be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 3194000e7ae3SMatthew Knepley @*/ 31957087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3196000e7ae3SMatthew Knepley { 3197000e7ae3SMatthew Knepley PetscFunctionBegin; 31980700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3199ae60f76fSBarry Smith ts->prestep = func; 3200000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3201000e7ae3SMatthew Knepley } 3202000e7ae3SMatthew Knepley 320309ee8438SJed Brown /*@ 32043f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 32053f2090d5SJed Brown 32063f2090d5SJed Brown Collective on TS 32073f2090d5SJed Brown 32083f2090d5SJed Brown Input Parameters: 32093f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 32103f2090d5SJed Brown 32113f2090d5SJed Brown Notes: 32123f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 32133f2090d5SJed Brown so most users would not generally call this routine themselves. 32143f2090d5SJed Brown 32153f2090d5SJed Brown Level: developer 32163f2090d5SJed Brown 32173f2090d5SJed Brown .keywords: TS, timestep 32189be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 32193f2090d5SJed Brown @*/ 32207087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 32213f2090d5SJed Brown { 32223f2090d5SJed Brown PetscErrorCode ierr; 32233f2090d5SJed Brown 32243f2090d5SJed Brown PetscFunctionBegin; 32250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3226ae60f76fSBarry Smith if (ts->prestep) { 3227ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 3228312ce896SJed Brown } 32293f2090d5SJed Brown PetscFunctionReturn(0); 32303f2090d5SJed Brown } 32313f2090d5SJed Brown 3232b8123daeSJed Brown /*@C 3233b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3234b8123daeSJed Brown called once at the beginning of each stage. 3235b8123daeSJed Brown 3236b8123daeSJed Brown Logically Collective on TS 3237b8123daeSJed Brown 3238b8123daeSJed Brown Input Parameters: 3239b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 3240b8123daeSJed Brown - func - The function 3241b8123daeSJed Brown 3242b8123daeSJed Brown Calling sequence of func: 3243b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 3244b8123daeSJed Brown 3245b8123daeSJed Brown Level: intermediate 3246b8123daeSJed Brown 3247b8123daeSJed Brown Note: 3248b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 3249b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 3250b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 3251b8123daeSJed Brown 3252b8123daeSJed Brown .keywords: TS, timestep 32539be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3254b8123daeSJed Brown @*/ 3255b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 3256b8123daeSJed Brown { 3257b8123daeSJed Brown PetscFunctionBegin; 3258b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3259ae60f76fSBarry Smith ts->prestage = func; 3260b8123daeSJed Brown PetscFunctionReturn(0); 3261b8123daeSJed Brown } 3262b8123daeSJed Brown 32639be3e283SDebojyoti Ghosh /*@C 32649be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 32659be3e283SDebojyoti Ghosh called once at the end of each stage. 32669be3e283SDebojyoti Ghosh 32679be3e283SDebojyoti Ghosh Logically Collective on TS 32689be3e283SDebojyoti Ghosh 32699be3e283SDebojyoti Ghosh Input Parameters: 32709be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 32719be3e283SDebojyoti Ghosh - func - The function 32729be3e283SDebojyoti Ghosh 32739be3e283SDebojyoti Ghosh Calling sequence of func: 32749be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 32759be3e283SDebojyoti Ghosh 32769be3e283SDebojyoti Ghosh Level: intermediate 32779be3e283SDebojyoti Ghosh 32789be3e283SDebojyoti Ghosh Note: 32799be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 32809be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 32819be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 32829be3e283SDebojyoti Ghosh 32839be3e283SDebojyoti Ghosh .keywords: TS, timestep 32849be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 32859be3e283SDebojyoti Ghosh @*/ 32869be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 32879be3e283SDebojyoti Ghosh { 32889be3e283SDebojyoti Ghosh PetscFunctionBegin; 32899be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 32909be3e283SDebojyoti Ghosh ts->poststage = func; 32919be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 32929be3e283SDebojyoti Ghosh } 32939be3e283SDebojyoti Ghosh 3294c688d042SShri Abhyankar /*@C 3295c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 3296c688d042SShri Abhyankar called once at the end of each step evaluation. 3297c688d042SShri Abhyankar 3298c688d042SShri Abhyankar Logically Collective on TS 3299c688d042SShri Abhyankar 3300c688d042SShri Abhyankar Input Parameters: 3301c688d042SShri Abhyankar + ts - The TS context obtained from TSCreate() 3302c688d042SShri Abhyankar - func - The function 3303c688d042SShri Abhyankar 3304c688d042SShri Abhyankar Calling sequence of func: 3305c688d042SShri Abhyankar . PetscErrorCode func(TS ts); 3306c688d042SShri Abhyankar 3307c688d042SShri Abhyankar Level: intermediate 3308c688d042SShri Abhyankar 3309c688d042SShri Abhyankar Note: 33101785ff2aSShri Abhyankar Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling 33111785ff2aSShri Abhyankar thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep() 3312e7e94ed4SShri Abhyankar may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step 3313e7e94ed4SShri Abhyankar solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step 3314e7e94ed4SShri Abhyankar with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime() 3315c688d042SShri Abhyankar 3316c688d042SShri Abhyankar .keywords: TS, timestep 3317c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3318c688d042SShri Abhyankar @*/ 3319c688d042SShri Abhyankar PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS)) 3320c688d042SShri Abhyankar { 3321c688d042SShri Abhyankar PetscFunctionBegin; 3322c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3323c688d042SShri Abhyankar ts->postevaluate = func; 3324c688d042SShri Abhyankar PetscFunctionReturn(0); 3325c688d042SShri Abhyankar } 3326c688d042SShri Abhyankar 3327b8123daeSJed Brown /*@ 3328b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 3329b8123daeSJed Brown 3330b8123daeSJed Brown Collective on TS 3331b8123daeSJed Brown 3332b8123daeSJed Brown Input Parameters: 3333b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 33349be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3335b8123daeSJed Brown 3336b8123daeSJed Brown Notes: 3337b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 3338b8123daeSJed Brown most users would not generally call this routine themselves. 3339b8123daeSJed Brown 3340b8123daeSJed Brown Level: developer 3341b8123daeSJed Brown 3342b8123daeSJed Brown .keywords: TS, timestep 33439be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 3344b8123daeSJed Brown @*/ 3345b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3346b8123daeSJed Brown { 3347b8123daeSJed Brown PetscErrorCode ierr; 3348b8123daeSJed Brown 3349b8123daeSJed Brown PetscFunctionBegin; 3350b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3351ae60f76fSBarry Smith if (ts->prestage) { 3352ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 3353b8123daeSJed Brown } 3354b8123daeSJed Brown PetscFunctionReturn(0); 3355b8123daeSJed Brown } 3356b8123daeSJed Brown 33579be3e283SDebojyoti Ghosh /*@ 33589be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 33599be3e283SDebojyoti Ghosh 33609be3e283SDebojyoti Ghosh Collective on TS 33619be3e283SDebojyoti Ghosh 33629be3e283SDebojyoti Ghosh Input Parameters: 33639be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 33649be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 33659be3e283SDebojyoti Ghosh stageindex - Stage number 33669be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 33679be3e283SDebojyoti Ghosh of stages) with the stage solutions 33689be3e283SDebojyoti Ghosh 33699be3e283SDebojyoti Ghosh Notes: 33709be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 33719be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 33729be3e283SDebojyoti Ghosh 33739be3e283SDebojyoti Ghosh Level: developer 33749be3e283SDebojyoti Ghosh 33759be3e283SDebojyoti Ghosh .keywords: TS, timestep 33769be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 33779be3e283SDebojyoti Ghosh @*/ 33789be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 33799be3e283SDebojyoti Ghosh { 33809be3e283SDebojyoti Ghosh PetscErrorCode ierr; 33819be3e283SDebojyoti Ghosh 33829be3e283SDebojyoti Ghosh PetscFunctionBegin; 33839be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 33844beae5d8SLisandro Dalcin if (ts->poststage) { 33859be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 33869be3e283SDebojyoti Ghosh } 33879be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 33889be3e283SDebojyoti Ghosh } 33899be3e283SDebojyoti Ghosh 3390c688d042SShri Abhyankar /*@ 3391c688d042SShri Abhyankar TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate() 3392c688d042SShri Abhyankar 3393c688d042SShri Abhyankar Collective on TS 3394c688d042SShri Abhyankar 3395c688d042SShri Abhyankar Input Parameters: 3396c688d042SShri Abhyankar . ts - The TS context obtained from TSCreate() 3397c688d042SShri Abhyankar 3398c688d042SShri Abhyankar Notes: 3399c688d042SShri Abhyankar TSPostEvaluate() is typically used within time stepping implementations, 3400c688d042SShri Abhyankar most users would not generally call this routine themselves. 3401c688d042SShri Abhyankar 3402c688d042SShri Abhyankar Level: developer 3403c688d042SShri Abhyankar 3404c688d042SShri Abhyankar .keywords: TS, timestep 3405c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep() 3406c688d042SShri Abhyankar @*/ 3407c688d042SShri Abhyankar PetscErrorCode TSPostEvaluate(TS ts) 3408c688d042SShri Abhyankar { 3409c688d042SShri Abhyankar PetscErrorCode ierr; 3410c688d042SShri Abhyankar 3411c688d042SShri Abhyankar PetscFunctionBegin; 3412c688d042SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3413c688d042SShri Abhyankar if (ts->postevaluate) { 3414c688d042SShri Abhyankar PetscStackCallStandard((*ts->postevaluate),(ts)); 3415c688d042SShri Abhyankar } 3416c688d042SShri Abhyankar PetscFunctionReturn(0); 3417c688d042SShri Abhyankar } 3418c688d042SShri Abhyankar 3419ac226902SBarry Smith /*@C 3420000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 34213f2090d5SJed Brown called once at the end of each time step. 3422000e7ae3SMatthew Knepley 34233f9fe445SBarry Smith Logically Collective on TS 3424000e7ae3SMatthew Knepley 3425000e7ae3SMatthew Knepley Input Parameters: 3426000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3427000e7ae3SMatthew Knepley - func - The function 3428000e7ae3SMatthew Knepley 3429000e7ae3SMatthew Knepley Calling sequence of func: 3430b8123daeSJed Brown $ func (TS ts); 3431000e7ae3SMatthew Knepley 34321785ff2aSShri Abhyankar Notes: 34331785ff2aSShri Abhyankar The function set by TSSetPostStep() is called after each successful step. The solution vector X 34341785ff2aSShri Abhyankar obtained by TSGetSolution() may be different than that computed at the step end if the event handler 34351785ff2aSShri Abhyankar locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead. 34361785ff2aSShri Abhyankar 3437000e7ae3SMatthew Knepley Level: intermediate 3438000e7ae3SMatthew Knepley 3439000e7ae3SMatthew Knepley .keywords: TS, timestep 34401785ff2aSShri Abhyankar .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 3441000e7ae3SMatthew Knepley @*/ 34427087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 3443000e7ae3SMatthew Knepley { 3444000e7ae3SMatthew Knepley PetscFunctionBegin; 34450700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3446ae60f76fSBarry Smith ts->poststep = func; 3447000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3448000e7ae3SMatthew Knepley } 3449000e7ae3SMatthew Knepley 345009ee8438SJed Brown /*@ 34513f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 34523f2090d5SJed Brown 34533f2090d5SJed Brown Collective on TS 34543f2090d5SJed Brown 34553f2090d5SJed Brown Input Parameters: 34563f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 34573f2090d5SJed Brown 34583f2090d5SJed Brown Notes: 34593f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 34603f2090d5SJed Brown so most users would not generally call this routine themselves. 34613f2090d5SJed Brown 34623f2090d5SJed Brown Level: developer 34633f2090d5SJed Brown 34643f2090d5SJed Brown .keywords: TS, timestep 34653f2090d5SJed Brown @*/ 34667087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 34673f2090d5SJed Brown { 34683f2090d5SJed Brown PetscErrorCode ierr; 34693f2090d5SJed Brown 34703f2090d5SJed Brown PetscFunctionBegin; 34710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3472ae60f76fSBarry Smith if (ts->poststep) { 3473ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 347472ac3e02SJed Brown } 34753f2090d5SJed Brown PetscFunctionReturn(0); 34763f2090d5SJed Brown } 34773f2090d5SJed Brown 3478d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 3479d763cef2SBarry Smith 3480d763cef2SBarry Smith /*@C 3481a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 3482d763cef2SBarry Smith timestep to display the iteration's progress. 3483d763cef2SBarry Smith 34843f9fe445SBarry Smith Logically Collective on TS 3485d763cef2SBarry Smith 3486d763cef2SBarry Smith Input Parameters: 3487d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3488e213d8f1SJed Brown . monitor - monitoring routine 3489329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 34900298fd71SBarry Smith monitor routine (use NULL if no context is desired) 3491b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 34920298fd71SBarry Smith (may be NULL) 3493d763cef2SBarry Smith 3494e213d8f1SJed Brown Calling sequence of monitor: 34950910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 3496d763cef2SBarry Smith 3497d763cef2SBarry Smith + ts - the TS context 349863e21af5SBarry Smith . steps - iteration number (after the final time step the monitor routine may be called with a step of -1, this indicates the solution has been interpolated to this time) 34991f06c33eSBarry Smith . time - current time 35000910c330SBarry Smith . u - current iterate 3501d763cef2SBarry Smith - mctx - [optional] monitoring context 3502d763cef2SBarry Smith 3503d763cef2SBarry Smith Notes: 3504d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 3505d763cef2SBarry Smith already has been loaded. 3506d763cef2SBarry Smith 3507025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 3508025f1a04SBarry Smith 3509d763cef2SBarry Smith Level: intermediate 3510d763cef2SBarry Smith 3511d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3512d763cef2SBarry Smith 3513a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 3514d763cef2SBarry Smith @*/ 3515c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 3516d763cef2SBarry Smith { 351778064530SBarry Smith PetscErrorCode ierr; 351878064530SBarry Smith PetscInt i; 351978064530SBarry Smith PetscBool identical; 352078064530SBarry Smith 3521d763cef2SBarry Smith PetscFunctionBegin; 35220700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 352378064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 352478064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr); 352578064530SBarry Smith if (identical) PetscFunctionReturn(0); 352678064530SBarry Smith } 352717186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 3528d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 35298704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 3530d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 3531d763cef2SBarry Smith PetscFunctionReturn(0); 3532d763cef2SBarry Smith } 3533d763cef2SBarry Smith 3534d763cef2SBarry Smith /*@C 3535a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 3536d763cef2SBarry Smith 35373f9fe445SBarry Smith Logically Collective on TS 3538d763cef2SBarry Smith 3539d763cef2SBarry Smith Input Parameters: 3540d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3541d763cef2SBarry Smith 3542d763cef2SBarry Smith Notes: 3543d763cef2SBarry Smith There is no way to remove a single, specific monitor. 3544d763cef2SBarry Smith 3545d763cef2SBarry Smith Level: intermediate 3546d763cef2SBarry Smith 3547d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3548d763cef2SBarry Smith 3549a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 3550d763cef2SBarry Smith @*/ 35517087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 3552d763cef2SBarry Smith { 3553d952e501SBarry Smith PetscErrorCode ierr; 3554d952e501SBarry Smith PetscInt i; 3555d952e501SBarry Smith 3556d763cef2SBarry Smith PetscFunctionBegin; 35570700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3558d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 35598704b422SBarry Smith if (ts->monitordestroy[i]) { 35608704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 3561d952e501SBarry Smith } 3562d952e501SBarry Smith } 3563d763cef2SBarry Smith ts->numbermonitors = 0; 3564d763cef2SBarry Smith PetscFunctionReturn(0); 3565d763cef2SBarry Smith } 3566d763cef2SBarry Smith 3567721cd6eeSBarry Smith /*@C 3568721cd6eeSBarry Smith TSMonitorDefault - The Default monitor, prints the timestep and time for each step 35695516499fSSatish Balay 35705516499fSSatish Balay Level: intermediate 357141251cbbSSatish Balay 35725516499fSSatish Balay .keywords: TS, set, monitor 35735516499fSSatish Balay 357463e21af5SBarry Smith .seealso: TSMonitorSet() 357541251cbbSSatish Balay @*/ 3576721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf) 3577d763cef2SBarry Smith { 3578dfbe8321SBarry Smith PetscErrorCode ierr; 3579721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 358041aca3d6SBarry Smith PetscBool iascii,ibinary; 3581d132466eSBarry Smith 3582d763cef2SBarry Smith PetscFunctionBegin; 35834d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 358441aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 358541aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr); 3586721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 358741aca3d6SBarry Smith if (iascii) { 3588649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 358963e21af5SBarry Smith if (step == -1){ /* this indicates it is an interpolated solution */ 359063e21af5SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr); 359163e21af5SBarry Smith } else { 35928392e04aSShri 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); 359363e21af5SBarry Smith } 3594649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 359541aca3d6SBarry Smith } else if (ibinary) { 359641aca3d6SBarry Smith PetscMPIInt rank; 359741aca3d6SBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 359841aca3d6SBarry Smith if (!rank) { 3599450a797fSBarry Smith PetscBool skipHeader; 3600450a797fSBarry Smith PetscInt classid = REAL_FILE_CLASSID; 3601450a797fSBarry Smith 3602450a797fSBarry Smith ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr); 3603450a797fSBarry Smith if (!skipHeader) { 3604450a797fSBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 3605450a797fSBarry Smith } 360641aca3d6SBarry Smith ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr); 360741aca3d6SBarry Smith } else { 360841aca3d6SBarry Smith ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr); 360941aca3d6SBarry Smith } 361041aca3d6SBarry Smith } 3611721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3612d763cef2SBarry Smith PetscFunctionReturn(0); 3613d763cef2SBarry Smith } 3614d763cef2SBarry Smith 36159110b2e7SHong Zhang /*@C 36169110b2e7SHong Zhang TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every 36179110b2e7SHong Zhang timestep to display the iteration's progress. 36189110b2e7SHong Zhang 36199110b2e7SHong Zhang Logically Collective on TS 36209110b2e7SHong Zhang 36219110b2e7SHong Zhang Input Parameters: 36229110b2e7SHong Zhang + ts - the TS context obtained from TSCreate() 36239110b2e7SHong Zhang . adjointmonitor - monitoring routine 36249110b2e7SHong Zhang . adjointmctx - [optional] user-defined context for private data for the 36259110b2e7SHong Zhang monitor routine (use NULL if no context is desired) 36269110b2e7SHong Zhang - adjointmonitordestroy - [optional] routine that frees monitor context 36279110b2e7SHong Zhang (may be NULL) 36289110b2e7SHong Zhang 36299110b2e7SHong Zhang Calling sequence of monitor: 36309110b2e7SHong Zhang $ int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx) 36319110b2e7SHong Zhang 36329110b2e7SHong Zhang + ts - the TS context 36339110b2e7SHong 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 36349110b2e7SHong Zhang been interpolated to) 36359110b2e7SHong Zhang . time - current time 36369110b2e7SHong Zhang . u - current iterate 36379110b2e7SHong Zhang . numcost - number of cost functionos 36389110b2e7SHong Zhang . lambda - sensitivities to initial conditions 36399110b2e7SHong Zhang . mu - sensitivities to parameters 36409110b2e7SHong Zhang - adjointmctx - [optional] adjoint monitoring context 36419110b2e7SHong Zhang 36429110b2e7SHong Zhang Notes: 36439110b2e7SHong Zhang This routine adds an additional monitor to the list of monitors that 36449110b2e7SHong Zhang already has been loaded. 36459110b2e7SHong Zhang 36469110b2e7SHong Zhang Fortran notes: Only a single monitor function can be set for each TS object 36479110b2e7SHong Zhang 36489110b2e7SHong Zhang Level: intermediate 36499110b2e7SHong Zhang 36509110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 36519110b2e7SHong Zhang 36529110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel() 36539110b2e7SHong Zhang @*/ 36549110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**)) 36559110b2e7SHong Zhang { 365678064530SBarry Smith PetscErrorCode ierr; 365778064530SBarry Smith PetscInt i; 365878064530SBarry Smith PetscBool identical; 365978064530SBarry Smith 36609110b2e7SHong Zhang PetscFunctionBegin; 36619110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 366278064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 366378064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);CHKERRQ(ierr); 366478064530SBarry Smith if (identical) PetscFunctionReturn(0); 366578064530SBarry Smith } 36669110b2e7SHong Zhang if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set"); 36679110b2e7SHong Zhang ts->adjointmonitor[ts->numberadjointmonitors] = adjointmonitor; 36689110b2e7SHong Zhang ts->adjointmonitordestroy[ts->numberadjointmonitors] = adjointmdestroy; 36699110b2e7SHong Zhang ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx; 36709110b2e7SHong Zhang PetscFunctionReturn(0); 36719110b2e7SHong Zhang } 36729110b2e7SHong Zhang 36739110b2e7SHong Zhang /*@C 36749110b2e7SHong Zhang TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object. 36759110b2e7SHong Zhang 36769110b2e7SHong Zhang Logically Collective on TS 36779110b2e7SHong Zhang 36789110b2e7SHong Zhang Input Parameters: 36799110b2e7SHong Zhang . ts - the TS context obtained from TSCreate() 36809110b2e7SHong Zhang 36819110b2e7SHong Zhang Notes: 36829110b2e7SHong Zhang There is no way to remove a single, specific monitor. 36839110b2e7SHong Zhang 36849110b2e7SHong Zhang Level: intermediate 36859110b2e7SHong Zhang 36869110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 36879110b2e7SHong Zhang 36889110b2e7SHong Zhang .seealso: TSAdjointMonitorSet() 36899110b2e7SHong Zhang @*/ 36909110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorCancel(TS ts) 36919110b2e7SHong Zhang { 36929110b2e7SHong Zhang PetscErrorCode ierr; 36939110b2e7SHong Zhang PetscInt i; 36949110b2e7SHong Zhang 36959110b2e7SHong Zhang PetscFunctionBegin; 36969110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36979110b2e7SHong Zhang for (i=0; i<ts->numberadjointmonitors; i++) { 36989110b2e7SHong Zhang if (ts->adjointmonitordestroy[i]) { 36999110b2e7SHong Zhang ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 37009110b2e7SHong Zhang } 37019110b2e7SHong Zhang } 37029110b2e7SHong Zhang ts->numberadjointmonitors = 0; 37039110b2e7SHong Zhang PetscFunctionReturn(0); 37049110b2e7SHong Zhang } 37059110b2e7SHong Zhang 3706721cd6eeSBarry Smith /*@C 3707721cd6eeSBarry Smith TSAdjointMonitorDefault - the default monitor of adjoint computations 3708110eb670SHong Zhang 3709110eb670SHong Zhang Level: intermediate 3710110eb670SHong Zhang 3711110eb670SHong Zhang .keywords: TS, set, monitor 3712110eb670SHong Zhang 3713110eb670SHong Zhang .seealso: TSAdjointMonitorSet() 3714110eb670SHong Zhang @*/ 3715721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf) 3716110eb670SHong Zhang { 3717110eb670SHong Zhang PetscErrorCode ierr; 3718721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 3719110eb670SHong Zhang 3720110eb670SHong Zhang PetscFunctionBegin; 3721110eb670SHong Zhang PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3722721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 3723110eb670SHong Zhang ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3724110eb670SHong 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); 3725110eb670SHong Zhang ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3726721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3727110eb670SHong Zhang PetscFunctionReturn(0); 3728110eb670SHong Zhang } 3729110eb670SHong Zhang 3730cd652676SJed Brown /*@ 3731cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3732cd652676SJed Brown 3733cd652676SJed Brown Collective on TS 3734cd652676SJed Brown 3735cd652676SJed Brown Input Argument: 3736cd652676SJed Brown + ts - time stepping context 3737cd652676SJed Brown - t - time to interpolate to 3738cd652676SJed Brown 3739cd652676SJed Brown Output Argument: 37400910c330SBarry Smith . U - state at given time 3741cd652676SJed Brown 3742cd652676SJed Brown Level: intermediate 3743cd652676SJed Brown 3744cd652676SJed Brown Developer Notes: 3745cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3746cd652676SJed Brown 3747cd652676SJed Brown .keywords: TS, set 3748cd652676SJed Brown 3749874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve() 3750cd652676SJed Brown @*/ 37510910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3752cd652676SJed Brown { 3753cd652676SJed Brown PetscErrorCode ierr; 3754cd652676SJed Brown 3755cd652676SJed Brown PetscFunctionBegin; 3756cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3757b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3758be5899b3SLisandro Dalcin if (t < ts->ptime_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_prev,(double)ts->ptime); 3759ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 37600910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3761cd652676SJed Brown PetscFunctionReturn(0); 3762cd652676SJed Brown } 3763cd652676SJed Brown 3764d763cef2SBarry Smith /*@ 37656d9e5789SSean Farley TSStep - Steps one time step 3766d763cef2SBarry Smith 3767d763cef2SBarry Smith Collective on TS 3768d763cef2SBarry Smith 3769d763cef2SBarry Smith Input Parameter: 3770d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3771d763cef2SBarry Smith 377227829d71SBarry Smith Level: developer 3773d763cef2SBarry Smith 3774b8123daeSJed Brown Notes: 377527829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 377627829d71SBarry Smith 3777b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3778b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3779b8123daeSJed Brown 378025cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 378125cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 378225cb2221SBarry Smith 3783d763cef2SBarry Smith .keywords: TS, timestep, solve 3784d763cef2SBarry Smith 37859be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3786d763cef2SBarry Smith @*/ 3787193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3788d763cef2SBarry Smith { 3789dfbe8321SBarry Smith PetscErrorCode ierr; 3790fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3791be5899b3SLisandro Dalcin PetscReal ptime; 3792d763cef2SBarry Smith 3793d763cef2SBarry Smith PetscFunctionBegin; 37940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3795fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3796fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3797fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3798fffbeea8SBarry Smith " type = {Preprint},\n" 3799fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3800fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3801302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3802fffbeea8SBarry Smith 3803d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 380468bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3805d405a339SMatthew Knepley 3806a6772fa2SLisandro Dalcin 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()"); 3807be5899b3SLisandro Dalcin 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"); 3808a6772fa2SLisandro Dalcin 3809be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 3810362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3811be5899b3SLisandro Dalcin ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev; 3812e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3813d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3814193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3815d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3816be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 3817be5899b3SLisandro Dalcin ts->steps++; ts->total_steps++; 3818be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 381928d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3820362cd11cSLisandro Dalcin 3821362cd11cSLisandro Dalcin if (ts->reason < 0) { 3822cef5090cSJed Brown if (ts->errorifstepfailed) { 382308c7845fSBarry 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]); 382408c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3825d2daff3dSHong Zhang } 382608c7845fSBarry Smith } else if (!ts->reason) { 382708c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 382808c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 382908c7845fSBarry Smith } 383008c7845fSBarry Smith PetscFunctionReturn(0); 383108c7845fSBarry Smith } 383208c7845fSBarry Smith 383308c7845fSBarry Smith /*@ 3834b957a604SHong Zhang TSAdjointStep - Steps one time step backward in the adjoint run 383508c7845fSBarry Smith 383608c7845fSBarry Smith Collective on TS 383708c7845fSBarry Smith 383808c7845fSBarry Smith Input Parameter: 383908c7845fSBarry Smith . ts - the TS context obtained from TSCreate() 384008c7845fSBarry Smith 38416a4d4014SLisandro Dalcin Level: intermediate 38426a4d4014SLisandro Dalcin 3843b957a604SHong Zhang .keywords: TS, adjoint, step 38446a4d4014SLisandro Dalcin 3845b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve() 38466a4d4014SLisandro Dalcin @*/ 384708c7845fSBarry Smith PetscErrorCode TSAdjointStep(TS ts) 38486a4d4014SLisandro Dalcin { 384908c7845fSBarry Smith DM dm; 38506a4d4014SLisandro Dalcin PetscErrorCode ierr; 38516a4d4014SLisandro Dalcin 38526a4d4014SLisandro Dalcin PetscFunctionBegin; 38534a2ae208SSatish Balay PetscValidHeaderSpecific(ts,TS_CLASSID,1); 385408c7845fSBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 385508c7845fSBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3856d763cef2SBarry Smith 3857685405a1SBarry Smith ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr); 3858d763cef2SBarry Smith 3859be5899b3SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3860be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime; 386142f2b339SBarry 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); 3862be5899b3SLisandro Dalcin ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 386342f2b339SBarry Smith ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr); 38648d0ad7a8SHong Zhang ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 3865be5899b3SLisandro Dalcin ts->steps++; ts->total_steps--; 3866362cd11cSLisandro Dalcin 3867362cd11cSLisandro Dalcin if (ts->reason < 0) { 3868cef5090cSJed Brown if (ts->errorifstepfailed) { 38696c4ed002SBarry 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]); 38706c4ed002SBarry 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]); 38716c4ed002SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3872cef5090cSJed Brown } 3873362cd11cSLisandro Dalcin } else if (!ts->reason) { 387473b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3875362cd11cSLisandro Dalcin } 3876d763cef2SBarry Smith PetscFunctionReturn(0); 3877d763cef2SBarry Smith } 3878d763cef2SBarry Smith 38797cbde773SLisandro Dalcin /*@ 38807cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 38817cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 38827cbde773SLisandro Dalcin 38837cbde773SLisandro Dalcin Collective on TS 38847cbde773SLisandro Dalcin 38857cbde773SLisandro Dalcin Input Arguments: 38867cbde773SLisandro Dalcin + ts - time stepping context 38877cbde773SLisandro Dalcin . wnormtype - norm type, either NORM_2 or NORM_INFINITY 38887cbde773SLisandro Dalcin - order - optional, desired order for the error evaluation or PETSC_DECIDE 38897cbde773SLisandro Dalcin 38907cbde773SLisandro Dalcin Output Arguments: 38917cbde773SLisandro Dalcin + order - optional, the actual order of the error evaluation 38927cbde773SLisandro Dalcin - wlte - the weighted local truncation error norm 38937cbde773SLisandro Dalcin 38947cbde773SLisandro Dalcin Level: advanced 38957cbde773SLisandro Dalcin 38967cbde773SLisandro Dalcin Notes: 38977cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 38987cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 38997cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 39007cbde773SLisandro Dalcin 39017cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm() 39027cbde773SLisandro Dalcin @*/ 39037cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte) 39047cbde773SLisandro Dalcin { 39057cbde773SLisandro Dalcin PetscErrorCode ierr; 39067cbde773SLisandro Dalcin 39077cbde773SLisandro Dalcin PetscFunctionBegin; 39087cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 39097cbde773SLisandro Dalcin PetscValidType(ts,1); 39107cbde773SLisandro Dalcin PetscValidLogicalCollectiveEnum(ts,wnormtype,4); 39117cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order,3); 39127cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts,*order,3); 39137cbde773SLisandro Dalcin PetscValidRealPointer(wlte,4); 39147cbde773SLisandro Dalcin if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 39157cbde773SLisandro Dalcin if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name); 39167cbde773SLisandro Dalcin ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr); 39177cbde773SLisandro Dalcin PetscFunctionReturn(0); 39187cbde773SLisandro Dalcin } 39197cbde773SLisandro Dalcin 392005175c85SJed Brown /*@ 392105175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 392205175c85SJed Brown 39231c3436cfSJed Brown Collective on TS 392405175c85SJed Brown 392505175c85SJed Brown Input Arguments: 39261c3436cfSJed Brown + ts - time stepping context 39271c3436cfSJed Brown . order - desired order of accuracy 39280298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 392905175c85SJed Brown 393005175c85SJed Brown Output Arguments: 39310910c330SBarry Smith . U - state at the end of the current step 393205175c85SJed Brown 393305175c85SJed Brown Level: advanced 393405175c85SJed Brown 3935108c343cSJed Brown Notes: 3936108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3937108c343cSJed 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. 3938108c343cSJed Brown 39391c3436cfSJed Brown .seealso: TSStep(), TSAdapt 394005175c85SJed Brown @*/ 39410910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 394205175c85SJed Brown { 394305175c85SJed Brown PetscErrorCode ierr; 394405175c85SJed Brown 394505175c85SJed Brown PetscFunctionBegin; 394605175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 394705175c85SJed Brown PetscValidType(ts,1); 39480910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3949ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 39500910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 395105175c85SJed Brown PetscFunctionReturn(0); 395205175c85SJed Brown } 395305175c85SJed Brown 3954b1cb36f3SHong Zhang /*@ 3955b1cb36f3SHong Zhang TSForwardCostIntegral - Evaluate the cost integral in the forward run. 3956b1cb36f3SHong Zhang 3957b1cb36f3SHong Zhang Collective on TS 3958b1cb36f3SHong Zhang 3959b1cb36f3SHong Zhang Input Arguments: 3960b1cb36f3SHong Zhang . ts - time stepping context 3961b1cb36f3SHong Zhang 3962b1cb36f3SHong Zhang Level: advanced 3963b1cb36f3SHong Zhang 3964b1cb36f3SHong Zhang Notes: 3965b1cb36f3SHong Zhang This function cannot be called until TSStep() has been completed. 3966b1cb36f3SHong Zhang 3967b1cb36f3SHong Zhang .seealso: TSSolve(), TSAdjointCostIntegral() 3968b1cb36f3SHong Zhang @*/ 3969b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts) 3970b1cb36f3SHong Zhang { 3971b1cb36f3SHong Zhang PetscErrorCode ierr; 3972b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3973b1cb36f3SHong 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); 3974b1cb36f3SHong Zhang ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr); 3975b1cb36f3SHong Zhang PetscFunctionReturn(0); 3976b1cb36f3SHong Zhang } 3977d67e68b7SBarry Smith 3978d763cef2SBarry Smith /*@ 3979d763cef2SBarry Smith TSSolve - Steps the requested number of timesteps. 3980d763cef2SBarry Smith 3981d763cef2SBarry Smith Collective on TS 3982d763cef2SBarry Smith 3983d763cef2SBarry Smith Input Parameter: 3984d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 398563e21af5SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used, 398663e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 39875a3a76d0SJed Brown 3988d763cef2SBarry Smith Level: beginner 3989d763cef2SBarry Smith 39905a3a76d0SJed Brown Notes: 39915a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 39925a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 39935a3a76d0SJed Brown stepped over the final time. 39945a3a76d0SJed Brown 3995d763cef2SBarry Smith .keywords: TS, timestep, solve 3996d763cef2SBarry Smith 399763e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime() 3998d763cef2SBarry Smith @*/ 3999cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 4000d763cef2SBarry Smith { 4001b06615a5SLisandro Dalcin Vec solution; 4002d763cef2SBarry Smith PetscErrorCode ierr; 4003d763cef2SBarry Smith 4004d763cef2SBarry Smith PetscFunctionBegin; 4005d763cef2SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4006f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 4007feed9e9dSBarry Smith 400849354f04SShri 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 */ 4009b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 40100910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 4011b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 4012b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 4013b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 40145a3a76d0SJed Brown } 40150910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 4016bbd56ea5SKarl Rupp } else if (u) { 40170910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 40185a3a76d0SJed Brown } 4019b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 402068bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 4021a6772fa2SLisandro Dalcin 4022a6772fa2SLisandro Dalcin 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()"); 4023a6772fa2SLisandro Dalcin 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"); 4024a6772fa2SLisandro Dalcin 4025e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 4026e7069c78SShri restarts the step after an event. Resetting these counters in such a case causes 4027e7069c78SShri TSTrajectory to incorrectly save the output files 4028e7069c78SShri */ 4029938c94caSShri Abhyankar 4030193ac0bcSJed Brown ts->steps = 0; 40315ef26d82SJed Brown ts->ksp_its = 0; 40325ef26d82SJed Brown ts->snes_its = 0; 4033c610991cSLisandro Dalcin ts->num_snes_failures = 0; 4034c610991cSLisandro Dalcin ts->reject = 0; 4035193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 4036193ac0bcSJed Brown 4037ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 4038f05ece33SBarry Smith 4039193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 4040193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 4041a6772fa2SLisandro Dalcin if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 4042cc708dedSBarry Smith ts->solvetime = ts->ptime; 4043a6772fa2SLisandro Dalcin solution = ts->vec_sol; 4044be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 4045db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 4046db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 4047e7069c78SShri 4048938c94caSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 40496427ac75SLisandro Dalcin ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 4050e7069c78SShri 405128d5b5d6SLisandro Dalcin ts->steprollback = PETSC_FALSE; 405228d5b5d6SLisandro Dalcin ts->steprestart = PETSC_TRUE; 40536427ac75SLisandro Dalcin 4054e1a7a14fSJed Brown while (!ts->reason) { 4055938c94caSShri Abhyankar ierr = TSMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 40569687d888SLisandro Dalcin if (!ts->steprollback) { 40579687d888SLisandro Dalcin ierr = TSPreStep(ts);CHKERRQ(ierr); 40589687d888SLisandro Dalcin } 4059193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 4060e783b05fSHong Zhang if (ts->vec_costintegral && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */ 4061b1cb36f3SHong Zhang ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr); 4062b1cb36f3SHong Zhang } 406310b82f12SShri Abhyankar ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 4064e783b05fSHong Zhang ierr = TSEventHandler(ts);CHKERRQ(ierr); /* The right-hand side may be changed due to event. Be careful with Any computation using the RHS information after this point. */ 4065e783b05fSHong Zhang if (!ts->steprollback) { 4066938c94caSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 40671eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 4068aeb4809dSShri Abhyankar } 4069193ac0bcSJed Brown } 4070938c94caSShri Abhyankar ierr = TSMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 40716427ac75SLisandro Dalcin 407249354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 40730910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 4074cc708dedSBarry Smith ts->solvetime = ts->max_time; 4075b06615a5SLisandro Dalcin solution = u; 407663e21af5SBarry Smith ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr); 40770574a7fbSJed Brown } else { 4078ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 4079cc708dedSBarry Smith ts->solvetime = ts->ptime; 4080b06615a5SLisandro Dalcin solution = ts->vec_sol; 40810574a7fbSJed Brown } 4082193ac0bcSJed Brown } 4083d2daff3dSHong Zhang 4084ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 408563e21af5SBarry Smith ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr); 408656f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 4087ef222394SBarry Smith if (ts->adjoint_solve) { 4088ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 40892b0a91c0SBarry Smith } 4090d763cef2SBarry Smith PetscFunctionReturn(0); 4091d763cef2SBarry Smith } 4092d763cef2SBarry Smith 4093b1cb36f3SHong Zhang /*@ 4094b1cb36f3SHong Zhang TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run. 4095b1cb36f3SHong Zhang 4096b1cb36f3SHong Zhang Collective on TS 4097b1cb36f3SHong Zhang 4098b1cb36f3SHong Zhang Input Arguments: 4099b1cb36f3SHong Zhang . ts - time stepping context 4100b1cb36f3SHong Zhang 4101b1cb36f3SHong Zhang Level: advanced 4102b1cb36f3SHong Zhang 4103b1cb36f3SHong Zhang Notes: 4104b1cb36f3SHong Zhang This function cannot be called until TSAdjointStep() has been completed. 4105b1cb36f3SHong Zhang 4106b1cb36f3SHong Zhang .seealso: TSAdjointSolve(), TSAdjointStep 4107b1cb36f3SHong Zhang @*/ 4108b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts) 4109b1cb36f3SHong Zhang { 4110b1cb36f3SHong Zhang PetscErrorCode ierr; 4111b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4112b1cb36f3SHong 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); 4113b1cb36f3SHong Zhang ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr); 4114b1cb36f3SHong Zhang PetscFunctionReturn(0); 4115b1cb36f3SHong Zhang } 4116b1cb36f3SHong Zhang 4117c88f2d44SBarry Smith /*@ 4118c88f2d44SBarry Smith TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 4119c88f2d44SBarry Smith 4120c88f2d44SBarry Smith Collective on TS 4121c88f2d44SBarry Smith 4122c88f2d44SBarry Smith Input Parameter: 41232c39e106SBarry Smith . ts - the TS context obtained from TSCreate() 4124c88f2d44SBarry Smith 4125e87fd094SBarry Smith Options Database: 4126e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions 4127e87fd094SBarry Smith 4128c88f2d44SBarry Smith Level: intermediate 4129c88f2d44SBarry Smith 4130c88f2d44SBarry Smith Notes: 4131c88f2d44SBarry Smith This must be called after a call to TSSolve() that solves the forward problem 4132c88f2d44SBarry Smith 413373b18844SBarry Smith By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time 413473b18844SBarry Smith 4135c88f2d44SBarry Smith .keywords: TS, timestep, solve 4136c88f2d44SBarry Smith 4137b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep() 4138c88f2d44SBarry Smith @*/ 41392c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts) 4140c88f2d44SBarry Smith { 4141c88f2d44SBarry Smith PetscErrorCode ierr; 4142c88f2d44SBarry Smith 4143c88f2d44SBarry Smith PetscFunctionBegin; 4144c88f2d44SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4145c88f2d44SBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 414668bece0bSHong Zhang 4147c88f2d44SBarry Smith /* reset time step and iteration counters */ 4148c88f2d44SBarry Smith ts->steps = 0; 4149c88f2d44SBarry Smith ts->ksp_its = 0; 4150c88f2d44SBarry Smith ts->snes_its = 0; 4151c88f2d44SBarry Smith ts->num_snes_failures = 0; 4152c88f2d44SBarry Smith ts->reject = 0; 4153c88f2d44SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 4154c88f2d44SBarry Smith 415573b18844SBarry Smith if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps; 4156c88f2d44SBarry Smith 415773b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 4158c88f2d44SBarry Smith while (!ts->reason) { 415955c52731SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 41609110b2e7SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 41616427ac75SLisandro Dalcin ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr); 4162616946c1SHong Zhang ierr = TSAdjointStep(ts);CHKERRQ(ierr); 4163b1cb36f3SHong Zhang if (ts->vec_costintegral && !ts->costintegralfwd) { 4164b1cb36f3SHong Zhang ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr); 4165b1cb36f3SHong Zhang } 4166c88f2d44SBarry Smith } 416726656371SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 416826656371SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 4169c88f2d44SBarry Smith ts->solvetime = ts->ptime; 4170e210cd0eSHong Zhang ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr); 4171685405a1SBarry Smith ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr); 4172c88f2d44SBarry Smith PetscFunctionReturn(0); 4173c88f2d44SBarry Smith } 4174c88f2d44SBarry Smith 41757db568b7SBarry Smith /*@C 4176228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 4177228d79bcSJed Brown 4178228d79bcSJed Brown Collective on TS 4179228d79bcSJed Brown 4180228d79bcSJed Brown Input Parameters: 4181228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 4182228d79bcSJed Brown . step - step number that has just completed 4183228d79bcSJed Brown . ptime - model time of the state 41840910c330SBarry Smith - u - state at the current model time 4185228d79bcSJed Brown 4186228d79bcSJed Brown Notes: 41877db568b7SBarry Smith TSMonitor() is typically used automatically within the time stepping implementations. 41887db568b7SBarry Smith Users would almost never call this routine directly. 4189228d79bcSJed Brown 419063e21af5SBarry Smith A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions 419163e21af5SBarry Smith 41927db568b7SBarry Smith Level: developer 4193228d79bcSJed Brown 4194228d79bcSJed Brown .keywords: TS, timestep 4195228d79bcSJed Brown @*/ 41960910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 4197d763cef2SBarry Smith { 4198d6152f81SLisandro Dalcin DM dm; 4199a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 4200d6152f81SLisandro Dalcin PetscErrorCode ierr; 4201d763cef2SBarry Smith 4202d763cef2SBarry Smith PetscFunctionBegin; 4203b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4204b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4205d6152f81SLisandro Dalcin 4206d6152f81SLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 4207d6152f81SLisandro Dalcin ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr); 4208d6152f81SLisandro Dalcin 42095edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 4210d763cef2SBarry Smith for (i=0; i<n; i++) { 42110910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 4212d763cef2SBarry Smith } 42135edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 4214d763cef2SBarry Smith PetscFunctionReturn(0); 4215d763cef2SBarry Smith } 4216d763cef2SBarry Smith 42177db568b7SBarry Smith /*@C 42189110b2e7SHong Zhang TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet() 42199110b2e7SHong Zhang 42209110b2e7SHong Zhang Collective on TS 42219110b2e7SHong Zhang 42229110b2e7SHong Zhang Input Parameters: 42239110b2e7SHong Zhang + ts - time stepping context obtained from TSCreate() 42249110b2e7SHong Zhang . step - step number that has just completed 42259110b2e7SHong Zhang . ptime - model time of the state 42269110b2e7SHong Zhang . u - state at the current model time 42279110b2e7SHong Zhang . numcost - number of cost functions (dimension of lambda or mu) 42289110b2e7SHong Zhang . lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 42299110b2e7SHong Zhang - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 42309110b2e7SHong Zhang 42319110b2e7SHong Zhang Notes: 42327db568b7SBarry Smith TSAdjointMonitor() is typically used automatically within the time stepping implementations. 42337db568b7SBarry Smith Users would almost never call this routine directly. 42349110b2e7SHong Zhang 42357db568b7SBarry Smith Level: developer 42369110b2e7SHong Zhang 42379110b2e7SHong Zhang .keywords: TS, timestep 42389110b2e7SHong Zhang @*/ 42399110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu) 42409110b2e7SHong Zhang { 42419110b2e7SHong Zhang PetscErrorCode ierr; 42429110b2e7SHong Zhang PetscInt i,n = ts->numberadjointmonitors; 42439110b2e7SHong Zhang 42449110b2e7SHong Zhang PetscFunctionBegin; 42459110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 42469110b2e7SHong Zhang PetscValidHeaderSpecific(u,VEC_CLASSID,4); 42479110b2e7SHong Zhang ierr = VecLockPush(u);CHKERRQ(ierr); 42489110b2e7SHong Zhang for (i=0; i<n; i++) { 42499110b2e7SHong Zhang ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 42509110b2e7SHong Zhang } 42519110b2e7SHong Zhang ierr = VecLockPop(u);CHKERRQ(ierr); 42529110b2e7SHong Zhang PetscFunctionReturn(0); 42539110b2e7SHong Zhang } 42549110b2e7SHong Zhang 4255d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 4256d763cef2SBarry Smith /*@C 42577db568b7SBarry Smith TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 4258a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 4259d763cef2SBarry Smith 4260d763cef2SBarry Smith Collective on TS 4261d763cef2SBarry Smith 4262d763cef2SBarry Smith Input Parameters: 4263d763cef2SBarry Smith + host - the X display to open, or null for the local machine 4264d763cef2SBarry Smith . label - the title to put in the title bar 42657c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 4266a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 4267a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 4268d763cef2SBarry Smith 4269d763cef2SBarry Smith Output Parameter: 42700b039ecaSBarry Smith . ctx - the context 4271d763cef2SBarry Smith 4272d763cef2SBarry Smith Options Database Key: 42734f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 42747db568b7SBarry Smith . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 42757db568b7SBarry Smith . -ts_monitor_lg_error - monitor the error 42767db568b7SBarry Smith . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 42777db568b7SBarry Smith . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 4278b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 4279d763cef2SBarry Smith 4280d763cef2SBarry Smith Notes: 4281a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 4282d763cef2SBarry Smith 42837db568b7SBarry Smith One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 42847db568b7SBarry Smith 42857db568b7SBarry 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 42867db568b7SBarry 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 42877db568b7SBarry Smith as the first argument. 42887db568b7SBarry Smith 42897db568b7SBarry Smith One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 42907db568b7SBarry Smith 42917db568b7SBarry Smith 4292d763cef2SBarry Smith Level: intermediate 4293d763cef2SBarry Smith 42947db568b7SBarry Smith .keywords: TS, monitor, line graph, residual 4295d763cef2SBarry Smith 42967db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 42977db568b7SBarry Smith TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 42987db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 42997db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 43007db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 43017c922b88SBarry Smith 4302d763cef2SBarry Smith @*/ 4303a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 4304d763cef2SBarry Smith { 43057f52daa2SLisandro Dalcin PetscDraw draw; 4306dfbe8321SBarry Smith PetscErrorCode ierr; 4307d763cef2SBarry Smith 4308d763cef2SBarry Smith PetscFunctionBegin; 4309b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 43107f52daa2SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 43117f52daa2SLisandro Dalcin ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 43127f52daa2SLisandro Dalcin ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 4313287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 43147f52daa2SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 4315a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 4316d763cef2SBarry Smith PetscFunctionReturn(0); 4317d763cef2SBarry Smith } 4318d763cef2SBarry Smith 4319b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 4320d763cef2SBarry Smith { 43210b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4322c32365f1SBarry Smith PetscReal x = ptime,y; 4323dfbe8321SBarry Smith PetscErrorCode ierr; 4324d763cef2SBarry Smith 4325d763cef2SBarry Smith PetscFunctionBegin; 432663e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */ 4327b06615a5SLisandro Dalcin if (!step) { 4328a9f9c1f6SBarry Smith PetscDrawAxis axis; 4329a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 43306934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr); 4331a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4332a9f9c1f6SBarry Smith } 4333c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 43340b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4335b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 43360b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 43376934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 43383923b477SBarry Smith } 4339d763cef2SBarry Smith PetscFunctionReturn(0); 4340d763cef2SBarry Smith } 4341d763cef2SBarry Smith 4342d763cef2SBarry Smith /*@C 4343a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 4344a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 4345d763cef2SBarry Smith 43460b039ecaSBarry Smith Collective on TSMonitorLGCtx 4347d763cef2SBarry Smith 4348d763cef2SBarry Smith Input Parameter: 43490b039ecaSBarry Smith . ctx - the monitor context 4350d763cef2SBarry Smith 4351d763cef2SBarry Smith Level: intermediate 4352d763cef2SBarry Smith 4353d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 4354d763cef2SBarry Smith 43554f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 4356d763cef2SBarry Smith @*/ 4357a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 4358d763cef2SBarry Smith { 4359dfbe8321SBarry Smith PetscErrorCode ierr; 4360d763cef2SBarry Smith 4361d763cef2SBarry Smith PetscFunctionBegin; 43627684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 43637684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 43647684fa3eSBarry Smith } 43650b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 436631152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 4367387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 4368387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 4369387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 43700b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 4371d763cef2SBarry Smith PetscFunctionReturn(0); 4372d763cef2SBarry Smith } 4373d763cef2SBarry Smith 4374d763cef2SBarry Smith /*@ 4375b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4376d763cef2SBarry Smith 4377d763cef2SBarry Smith Not Collective 4378d763cef2SBarry Smith 4379d763cef2SBarry Smith Input Parameter: 4380d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 4381d763cef2SBarry Smith 4382d763cef2SBarry Smith Output Parameter: 438363e21af5SBarry Smith . t - the current time. This time may not corresponds to the final time set with TSSetDuration(), use TSGetSolveTime(). 4384d763cef2SBarry Smith 4385d763cef2SBarry Smith Level: beginner 4386d763cef2SBarry Smith 4387b8123daeSJed Brown Note: 4388b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 43899be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 4390b8123daeSJed Brown 439163e21af5SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep(), TSGetSolveTime() 4392d763cef2SBarry Smith 4393d763cef2SBarry Smith .keywords: TS, get, time 4394d763cef2SBarry Smith @*/ 43957087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 4396d763cef2SBarry Smith { 4397d763cef2SBarry Smith PetscFunctionBegin; 43980700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4399f7cf8827SBarry Smith PetscValidRealPointer(t,2); 4400d763cef2SBarry Smith *t = ts->ptime; 4401d763cef2SBarry Smith PetscFunctionReturn(0); 4402d763cef2SBarry Smith } 4403d763cef2SBarry Smith 4404e5e524a1SHong Zhang /*@ 4405e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4406e5e524a1SHong Zhang 4407e5e524a1SHong Zhang Not Collective 4408e5e524a1SHong Zhang 4409e5e524a1SHong Zhang Input Parameter: 4410e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 4411e5e524a1SHong Zhang 4412e5e524a1SHong Zhang Output Parameter: 4413e5e524a1SHong Zhang . t - the previous time 4414e5e524a1SHong Zhang 4415e5e524a1SHong Zhang Level: beginner 4416e5e524a1SHong Zhang 4417e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 4418e5e524a1SHong Zhang 4419e5e524a1SHong Zhang .keywords: TS, get, time 4420e5e524a1SHong Zhang @*/ 4421e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 4422e5e524a1SHong Zhang { 4423e5e524a1SHong Zhang PetscFunctionBegin; 4424e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4425e5e524a1SHong Zhang PetscValidRealPointer(t,2); 4426e5e524a1SHong Zhang *t = ts->ptime_prev; 4427e5e524a1SHong Zhang PetscFunctionReturn(0); 4428e5e524a1SHong Zhang } 4429e5e524a1SHong Zhang 44306a4d4014SLisandro Dalcin /*@ 44316a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 44326a4d4014SLisandro Dalcin 44333f9fe445SBarry Smith Logically Collective on TS 44346a4d4014SLisandro Dalcin 44356a4d4014SLisandro Dalcin Input Parameters: 44366a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 44376a4d4014SLisandro Dalcin - time - the time 44386a4d4014SLisandro Dalcin 44396a4d4014SLisandro Dalcin Level: intermediate 44406a4d4014SLisandro Dalcin 44416a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 44426a4d4014SLisandro Dalcin 44436a4d4014SLisandro Dalcin .keywords: TS, set, time 44446a4d4014SLisandro Dalcin @*/ 44457087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 44466a4d4014SLisandro Dalcin { 44476a4d4014SLisandro Dalcin PetscFunctionBegin; 44480700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4449c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 44506a4d4014SLisandro Dalcin ts->ptime = t; 44516a4d4014SLisandro Dalcin PetscFunctionReturn(0); 44526a4d4014SLisandro Dalcin } 44536a4d4014SLisandro Dalcin 4454d763cef2SBarry Smith /*@C 4455d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4456d763cef2SBarry Smith TS options in the database. 4457d763cef2SBarry Smith 44583f9fe445SBarry Smith Logically Collective on TS 4459d763cef2SBarry Smith 4460d763cef2SBarry Smith Input Parameter: 4461d763cef2SBarry Smith + ts - The TS context 4462d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4463d763cef2SBarry Smith 4464d763cef2SBarry Smith Notes: 4465d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4466d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4467d763cef2SBarry Smith hyphen. 4468d763cef2SBarry Smith 4469d763cef2SBarry Smith Level: advanced 4470d763cef2SBarry Smith 4471d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 4472d763cef2SBarry Smith 4473d763cef2SBarry Smith .seealso: TSSetFromOptions() 4474d763cef2SBarry Smith 4475d763cef2SBarry Smith @*/ 44767087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 4477d763cef2SBarry Smith { 4478dfbe8321SBarry Smith PetscErrorCode ierr; 4479089b2837SJed Brown SNES snes; 4480d763cef2SBarry Smith 4481d763cef2SBarry Smith PetscFunctionBegin; 44820700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4483d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4484089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4485089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4486d763cef2SBarry Smith PetscFunctionReturn(0); 4487d763cef2SBarry Smith } 4488d763cef2SBarry Smith 4489d763cef2SBarry Smith 4490d763cef2SBarry Smith /*@C 4491d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4492d763cef2SBarry Smith TS options in the database. 4493d763cef2SBarry Smith 44943f9fe445SBarry Smith Logically Collective on TS 4495d763cef2SBarry Smith 4496d763cef2SBarry Smith Input Parameter: 4497d763cef2SBarry Smith + ts - The TS context 4498d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4499d763cef2SBarry Smith 4500d763cef2SBarry Smith Notes: 4501d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4502d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4503d763cef2SBarry Smith hyphen. 4504d763cef2SBarry Smith 4505d763cef2SBarry Smith Level: advanced 4506d763cef2SBarry Smith 4507d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 4508d763cef2SBarry Smith 4509d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 4510d763cef2SBarry Smith 4511d763cef2SBarry Smith @*/ 45127087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4513d763cef2SBarry Smith { 4514dfbe8321SBarry Smith PetscErrorCode ierr; 4515089b2837SJed Brown SNES snes; 4516d763cef2SBarry Smith 4517d763cef2SBarry Smith PetscFunctionBegin; 45180700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4519d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4520089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4521089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4522d763cef2SBarry Smith PetscFunctionReturn(0); 4523d763cef2SBarry Smith } 4524d763cef2SBarry Smith 4525d763cef2SBarry Smith /*@C 4526d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4527d763cef2SBarry Smith TS options in the database. 4528d763cef2SBarry Smith 4529d763cef2SBarry Smith Not Collective 4530d763cef2SBarry Smith 4531d763cef2SBarry Smith Input Parameter: 4532d763cef2SBarry Smith . ts - The TS context 4533d763cef2SBarry Smith 4534d763cef2SBarry Smith Output Parameter: 4535d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4536d763cef2SBarry Smith 4537d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 4538d763cef2SBarry Smith sufficient length to hold the prefix. 4539d763cef2SBarry Smith 4540d763cef2SBarry Smith Level: intermediate 4541d763cef2SBarry Smith 4542d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 4543d763cef2SBarry Smith 4544d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 4545d763cef2SBarry Smith @*/ 45467087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4547d763cef2SBarry Smith { 4548dfbe8321SBarry Smith PetscErrorCode ierr; 4549d763cef2SBarry Smith 4550d763cef2SBarry Smith PetscFunctionBegin; 45510700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45524482741eSBarry Smith PetscValidPointer(prefix,2); 4553d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4554d763cef2SBarry Smith PetscFunctionReturn(0); 4555d763cef2SBarry Smith } 4556d763cef2SBarry Smith 4557d763cef2SBarry Smith /*@C 4558d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4559d763cef2SBarry Smith 4560d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4561d763cef2SBarry Smith 4562d763cef2SBarry Smith Input Parameter: 4563d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4564d763cef2SBarry Smith 4565d763cef2SBarry Smith Output Parameters: 4566e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4567e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4568e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4569e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4570d763cef2SBarry Smith 45710298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 4572d763cef2SBarry Smith 4573d763cef2SBarry Smith Level: intermediate 4574d763cef2SBarry Smith 457526d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 4576d763cef2SBarry Smith 4577d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 4578d763cef2SBarry Smith @*/ 4579e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4580d763cef2SBarry Smith { 4581089b2837SJed Brown PetscErrorCode ierr; 458224989b8cSPeter Brune DM dm; 4583089b2837SJed Brown 4584d763cef2SBarry Smith PetscFunctionBegin; 4585*23a57915SBarry Smith if (Amat || Pmat) { 4586*23a57915SBarry Smith SNES snes; 4587089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4588*23a57915SBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4589e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 4590*23a57915SBarry Smith } 459124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 459224989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 4593d763cef2SBarry Smith PetscFunctionReturn(0); 4594d763cef2SBarry Smith } 4595d763cef2SBarry Smith 45962eca1d9cSJed Brown /*@C 45972eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 45982eca1d9cSJed Brown 45992eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 46002eca1d9cSJed Brown 46012eca1d9cSJed Brown Input Parameter: 46022eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 46032eca1d9cSJed Brown 46042eca1d9cSJed Brown Output Parameters: 4605e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4606e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 46072eca1d9cSJed Brown . f - The function to compute the matrices 46082eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 46092eca1d9cSJed Brown 46100298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 46112eca1d9cSJed Brown 46122eca1d9cSJed Brown Level: advanced 46132eca1d9cSJed Brown 46142eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 46152eca1d9cSJed Brown 46162eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 46172eca1d9cSJed Brown @*/ 4618e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 46192eca1d9cSJed Brown { 4620089b2837SJed Brown PetscErrorCode ierr; 462124989b8cSPeter Brune DM dm; 46220910c330SBarry Smith 46232eca1d9cSJed Brown PetscFunctionBegin; 4624c0aab802Sstefano_zampini if (Amat || Pmat) { 4625c0aab802Sstefano_zampini SNES snes; 4626089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4627f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4628e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 4629c0aab802Sstefano_zampini } 463024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 463124989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 46322eca1d9cSJed Brown PetscFunctionReturn(0); 46332eca1d9cSJed Brown } 46342eca1d9cSJed Brown 46356083293cSBarry Smith 46361713a123SBarry Smith /*@C 463783a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 46381713a123SBarry Smith VecView() for the solution at each timestep 46391713a123SBarry Smith 46401713a123SBarry Smith Collective on TS 46411713a123SBarry Smith 46421713a123SBarry Smith Input Parameters: 46431713a123SBarry Smith + ts - the TS context 46441713a123SBarry Smith . step - current time-step 4645142b95e3SSatish Balay . ptime - current time 46460298fd71SBarry Smith - dummy - either a viewer or NULL 46471713a123SBarry Smith 464899fdda47SBarry Smith Options Database: 464999fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 465099fdda47SBarry Smith 4651387f4636SBarry 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 465299fdda47SBarry Smith will look bad 465399fdda47SBarry Smith 46541713a123SBarry Smith Level: intermediate 46551713a123SBarry Smith 46561713a123SBarry Smith .keywords: TS, vector, monitor, view 46571713a123SBarry Smith 4658a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 46591713a123SBarry Smith @*/ 46600910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 46611713a123SBarry Smith { 4662dfbe8321SBarry Smith PetscErrorCode ierr; 466383a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4664473a3ab2SBarry Smith PetscDraw draw; 46651713a123SBarry Smith 46661713a123SBarry Smith PetscFunctionBegin; 46676083293cSBarry Smith if (!step && ictx->showinitial) { 46686083293cSBarry Smith if (!ictx->initialsolution) { 46690910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 46701713a123SBarry Smith } 46710910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 46726083293cSBarry Smith } 4673b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 46740dcf80beSBarry Smith 46756083293cSBarry Smith if (ictx->showinitial) { 46766083293cSBarry Smith PetscReal pause; 46776083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 46786083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 46796083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 46806083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 46816083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 46826083293cSBarry Smith } 46830910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 4684473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 468551fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 4686473a3ab2SBarry Smith char time[32]; 4687473a3ab2SBarry Smith 4688473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 468985b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4690473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4691473a3ab2SBarry Smith h = yl + .95*(yr - yl); 469251fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4693473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4694473a3ab2SBarry Smith } 4695473a3ab2SBarry Smith 46966083293cSBarry Smith if (ictx->showinitial) { 46976083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 46986083293cSBarry Smith } 46991713a123SBarry Smith PetscFunctionReturn(0); 47001713a123SBarry Smith } 47011713a123SBarry Smith 47029110b2e7SHong Zhang /*@C 47039110b2e7SHong Zhang TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling 47049110b2e7SHong Zhang VecView() for the sensitivities to initial states at each timestep 47059110b2e7SHong Zhang 47069110b2e7SHong Zhang Collective on TS 47079110b2e7SHong Zhang 47089110b2e7SHong Zhang Input Parameters: 47099110b2e7SHong Zhang + ts - the TS context 47109110b2e7SHong Zhang . step - current time-step 47119110b2e7SHong Zhang . ptime - current time 47129110b2e7SHong Zhang . u - current state 47139110b2e7SHong Zhang . numcost - number of cost functions 47149110b2e7SHong Zhang . lambda - sensitivities to initial conditions 47159110b2e7SHong Zhang . mu - sensitivities to parameters 47169110b2e7SHong Zhang - dummy - either a viewer or NULL 47179110b2e7SHong Zhang 47189110b2e7SHong Zhang Level: intermediate 47199110b2e7SHong Zhang 47209110b2e7SHong Zhang .keywords: TS, vector, adjoint, monitor, view 47219110b2e7SHong Zhang 47229110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView() 47239110b2e7SHong Zhang @*/ 47249110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 47259110b2e7SHong Zhang { 47269110b2e7SHong Zhang PetscErrorCode ierr; 47279110b2e7SHong Zhang TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 47289110b2e7SHong Zhang PetscDraw draw; 4729a0046e2cSSatish Balay PetscReal xl,yl,xr,yr,h; 4730a0046e2cSSatish Balay char time[32]; 47319110b2e7SHong Zhang 47329110b2e7SHong Zhang PetscFunctionBegin; 47339110b2e7SHong Zhang if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 47349110b2e7SHong Zhang 47359110b2e7SHong Zhang ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr); 47369110b2e7SHong Zhang ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 47379110b2e7SHong Zhang ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 47389110b2e7SHong Zhang ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 47399110b2e7SHong Zhang h = yl + .95*(yr - yl); 47409110b2e7SHong Zhang ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 47419110b2e7SHong Zhang ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 47429110b2e7SHong Zhang PetscFunctionReturn(0); 47439110b2e7SHong Zhang } 47449110b2e7SHong Zhang 47452d5ee99bSBarry Smith /*@C 47462d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 47472d5ee99bSBarry Smith 47482d5ee99bSBarry Smith Collective on TS 47492d5ee99bSBarry Smith 47502d5ee99bSBarry Smith Input Parameters: 47512d5ee99bSBarry Smith + ts - the TS context 47522d5ee99bSBarry Smith . step - current time-step 47532d5ee99bSBarry Smith . ptime - current time 47542d5ee99bSBarry Smith - dummy - either a viewer or NULL 47552d5ee99bSBarry Smith 47562d5ee99bSBarry Smith Level: intermediate 47572d5ee99bSBarry Smith 47582d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 47592d5ee99bSBarry Smith 47602d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 47612d5ee99bSBarry Smith @*/ 47622d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 47632d5ee99bSBarry Smith { 47642d5ee99bSBarry Smith PetscErrorCode ierr; 47652d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 47662d5ee99bSBarry Smith PetscDraw draw; 47676934998bSLisandro Dalcin PetscDrawAxis axis; 47682d5ee99bSBarry Smith PetscInt n; 47692d5ee99bSBarry Smith PetscMPIInt size; 47706934998bSLisandro Dalcin PetscReal U0,U1,xl,yl,xr,yr,h; 47712d5ee99bSBarry Smith char time[32]; 47722d5ee99bSBarry Smith const PetscScalar *U; 47732d5ee99bSBarry Smith 47742d5ee99bSBarry Smith PetscFunctionBegin; 47756934998bSLisandro Dalcin ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr); 47766934998bSLisandro Dalcin if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs"); 47772d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 47786934998bSLisandro Dalcin if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 47792d5ee99bSBarry Smith 47802d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 47816934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr); 47826934998bSLisandro Dalcin ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 47836934998bSLisandro Dalcin if (!step) { 47846934998bSLisandro Dalcin ierr = PetscDrawClear(draw);CHKERRQ(ierr); 47856934998bSLisandro Dalcin ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); 47866934998bSLisandro Dalcin } 47872d5ee99bSBarry Smith 47882d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 47896934998bSLisandro Dalcin U0 = PetscRealPart(U[0]); 47906934998bSLisandro Dalcin U1 = PetscRealPart(U[1]); 4791a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 47926934998bSLisandro Dalcin if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0); 47932d5ee99bSBarry Smith 47946934998bSLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 47956934998bSLisandro Dalcin ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr); 47962d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 47974b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 479885b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 47992d5ee99bSBarry Smith h = yl + .95*(yr - yl); 480051fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 48012d5ee99bSBarry Smith } 48026934998bSLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 48032d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 48046934998bSLisandro Dalcin ierr = PetscDrawSave(draw);CHKERRQ(ierr); 48052d5ee99bSBarry Smith PetscFunctionReturn(0); 48062d5ee99bSBarry Smith } 48072d5ee99bSBarry Smith 48081713a123SBarry Smith 48096083293cSBarry Smith /*@C 481083a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 48116083293cSBarry Smith 48126083293cSBarry Smith Collective on TS 48136083293cSBarry Smith 48146083293cSBarry Smith Input Parameters: 48156083293cSBarry Smith . ctx - the monitor context 48166083293cSBarry Smith 48176083293cSBarry Smith Level: intermediate 48186083293cSBarry Smith 48196083293cSBarry Smith .keywords: TS, vector, monitor, view 48206083293cSBarry Smith 482183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 48226083293cSBarry Smith @*/ 482383a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 48246083293cSBarry Smith { 48256083293cSBarry Smith PetscErrorCode ierr; 48266083293cSBarry Smith 48276083293cSBarry Smith PetscFunctionBegin; 482883a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 482983a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 483083a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 48316083293cSBarry Smith PetscFunctionReturn(0); 48326083293cSBarry Smith } 48336083293cSBarry Smith 48346083293cSBarry Smith /*@C 483583a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 48366083293cSBarry Smith 48376083293cSBarry Smith Collective on TS 48386083293cSBarry Smith 48396083293cSBarry Smith Input Parameter: 48406083293cSBarry Smith . ts - time-step context 48416083293cSBarry Smith 48426083293cSBarry Smith Output Patameter: 48436083293cSBarry Smith . ctx - the monitor context 48446083293cSBarry Smith 484599fdda47SBarry Smith Options Database: 484699fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 484799fdda47SBarry Smith 48486083293cSBarry Smith Level: intermediate 48496083293cSBarry Smith 48506083293cSBarry Smith .keywords: TS, vector, monitor, view 48516083293cSBarry Smith 485283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 48536083293cSBarry Smith @*/ 485483a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 48556083293cSBarry Smith { 48566083293cSBarry Smith PetscErrorCode ierr; 48576083293cSBarry Smith 48586083293cSBarry Smith PetscFunctionBegin; 4859b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 486083a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4861e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4862bbd56ea5SKarl Rupp 486383a4ac43SBarry Smith (*ctx)->howoften = howoften; 4864473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 4865c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4866473a3ab2SBarry Smith 4867473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 4868c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 48696083293cSBarry Smith PetscFunctionReturn(0); 48706083293cSBarry Smith } 48716083293cSBarry Smith 48723a471f94SBarry Smith /*@C 487383a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 48743a471f94SBarry Smith VecView() for the error at each timestep 48753a471f94SBarry Smith 48763a471f94SBarry Smith Collective on TS 48773a471f94SBarry Smith 48783a471f94SBarry Smith Input Parameters: 48793a471f94SBarry Smith + ts - the TS context 48803a471f94SBarry Smith . step - current time-step 48813a471f94SBarry Smith . ptime - current time 48820298fd71SBarry Smith - dummy - either a viewer or NULL 48833a471f94SBarry Smith 48843a471f94SBarry Smith Level: intermediate 48853a471f94SBarry Smith 48863a471f94SBarry Smith .keywords: TS, vector, monitor, view 48873a471f94SBarry Smith 48883a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 48893a471f94SBarry Smith @*/ 48900910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 48913a471f94SBarry Smith { 48923a471f94SBarry Smith PetscErrorCode ierr; 489383a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 489483a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 48953a471f94SBarry Smith Vec work; 48963a471f94SBarry Smith 48973a471f94SBarry Smith PetscFunctionBegin; 4898b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 48990910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 49003a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 49010910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 49023a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 49033a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 49043a471f94SBarry Smith PetscFunctionReturn(0); 49053a471f94SBarry Smith } 49063a471f94SBarry Smith 4907af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 49086c699258SBarry Smith /*@ 49092a808120SBarry Smith TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS 49106c699258SBarry Smith 49113f9fe445SBarry Smith Logically Collective on TS and DM 49126c699258SBarry Smith 49136c699258SBarry Smith Input Parameters: 49142a808120SBarry Smith + ts - the ODE integrator object 49152a808120SBarry Smith - dm - the dm, cannot be NULL 49166c699258SBarry Smith 49176c699258SBarry Smith Level: intermediate 49186c699258SBarry Smith 49196c699258SBarry Smith 49206c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 49216c699258SBarry Smith @*/ 49227087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 49236c699258SBarry Smith { 49246c699258SBarry Smith PetscErrorCode ierr; 4925089b2837SJed Brown SNES snes; 4926942e3340SBarry Smith DMTS tsdm; 49276c699258SBarry Smith 49286c699258SBarry Smith PetscFunctionBegin; 49290700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 49302a808120SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 493170663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4932942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 49332a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4934942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4935942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 493624989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 493724989b8cSPeter Brune tsdm->originaldm = dm; 493824989b8cSPeter Brune } 493924989b8cSPeter Brune } 49406bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 494124989b8cSPeter Brune } 49426c699258SBarry Smith ts->dm = dm; 4943bbd56ea5SKarl Rupp 4944089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4945089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 49466c699258SBarry Smith PetscFunctionReturn(0); 49476c699258SBarry Smith } 49486c699258SBarry Smith 49496c699258SBarry Smith /*@ 49506c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 49516c699258SBarry Smith 49523f9fe445SBarry Smith Not Collective 49536c699258SBarry Smith 49546c699258SBarry Smith Input Parameter: 49556c699258SBarry Smith . ts - the preconditioner context 49566c699258SBarry Smith 49576c699258SBarry Smith Output Parameter: 49586c699258SBarry Smith . dm - the dm 49596c699258SBarry Smith 49606c699258SBarry Smith Level: intermediate 49616c699258SBarry Smith 49626c699258SBarry Smith 49636c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 49646c699258SBarry Smith @*/ 49657087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 49666c699258SBarry Smith { 4967496e6a7aSJed Brown PetscErrorCode ierr; 4968496e6a7aSJed Brown 49696c699258SBarry Smith PetscFunctionBegin; 49700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4971496e6a7aSJed Brown if (!ts->dm) { 4972ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4973496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4974496e6a7aSJed Brown } 49756c699258SBarry Smith *dm = ts->dm; 49766c699258SBarry Smith PetscFunctionReturn(0); 49776c699258SBarry Smith } 49781713a123SBarry Smith 49790f5c6efeSJed Brown /*@ 49800f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 49810f5c6efeSJed Brown 49823f9fe445SBarry Smith Logically Collective on SNES 49830f5c6efeSJed Brown 49840f5c6efeSJed Brown Input Parameter: 4985d42a1c89SJed Brown + snes - nonlinear solver 49860910c330SBarry Smith . U - the current state at which to evaluate the residual 4987d42a1c89SJed Brown - ctx - user context, must be a TS 49880f5c6efeSJed Brown 49890f5c6efeSJed Brown Output Parameter: 49900f5c6efeSJed Brown . F - the nonlinear residual 49910f5c6efeSJed Brown 49920f5c6efeSJed Brown Notes: 49930f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 49940f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 49950f5c6efeSJed Brown 49960f5c6efeSJed Brown Level: advanced 49970f5c6efeSJed Brown 49980f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 49990f5c6efeSJed Brown @*/ 50000910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 50010f5c6efeSJed Brown { 50020f5c6efeSJed Brown TS ts = (TS)ctx; 50030f5c6efeSJed Brown PetscErrorCode ierr; 50040f5c6efeSJed Brown 50050f5c6efeSJed Brown PetscFunctionBegin; 50060f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 50070910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 50080f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 50090f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 50100910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 50110f5c6efeSJed Brown PetscFunctionReturn(0); 50120f5c6efeSJed Brown } 50130f5c6efeSJed Brown 50140f5c6efeSJed Brown /*@ 50150f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 50160f5c6efeSJed Brown 50170f5c6efeSJed Brown Collective on SNES 50180f5c6efeSJed Brown 50190f5c6efeSJed Brown Input Parameter: 50200f5c6efeSJed Brown + snes - nonlinear solver 50210910c330SBarry Smith . U - the current state at which to evaluate the residual 50220f5c6efeSJed Brown - ctx - user context, must be a TS 50230f5c6efeSJed Brown 50240f5c6efeSJed Brown Output Parameter: 50250f5c6efeSJed Brown + A - the Jacobian 50260f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 50270f5c6efeSJed Brown - flag - indicates any structure change in the matrix 50280f5c6efeSJed Brown 50290f5c6efeSJed Brown Notes: 50300f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 50310f5c6efeSJed Brown 50320f5c6efeSJed Brown Level: developer 50330f5c6efeSJed Brown 50340f5c6efeSJed Brown .seealso: SNESSetJacobian() 50350f5c6efeSJed Brown @*/ 5036d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 50370f5c6efeSJed Brown { 50380f5c6efeSJed Brown TS ts = (TS)ctx; 50390f5c6efeSJed Brown PetscErrorCode ierr; 50400f5c6efeSJed Brown 50410f5c6efeSJed Brown PetscFunctionBegin; 50420f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 50430910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 50440f5c6efeSJed Brown PetscValidPointer(A,3); 504594ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 50460f5c6efeSJed Brown PetscValidPointer(B,4); 504794ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 50480f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 5049d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 50500f5c6efeSJed Brown PetscFunctionReturn(0); 50510f5c6efeSJed Brown } 5052325fc9f4SBarry Smith 50530e4ef248SJed Brown /*@C 50549ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 50550e4ef248SJed Brown 50560e4ef248SJed Brown Collective on TS 50570e4ef248SJed Brown 50580e4ef248SJed Brown Input Arguments: 50590e4ef248SJed Brown + ts - time stepping context 50600e4ef248SJed Brown . t - time at which to evaluate 50610910c330SBarry Smith . U - state at which to evaluate 50620e4ef248SJed Brown - ctx - context 50630e4ef248SJed Brown 50640e4ef248SJed Brown Output Arguments: 50650e4ef248SJed Brown . F - right hand side 50660e4ef248SJed Brown 50670e4ef248SJed Brown Level: intermediate 50680e4ef248SJed Brown 50690e4ef248SJed Brown Notes: 50700e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 50710e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 50720e4ef248SJed Brown 50730e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 50740e4ef248SJed Brown @*/ 50750910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 50760e4ef248SJed Brown { 50770e4ef248SJed Brown PetscErrorCode ierr; 50780e4ef248SJed Brown Mat Arhs,Brhs; 50790e4ef248SJed Brown 50800e4ef248SJed Brown PetscFunctionBegin; 50810e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 5082d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 50830910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 50840e4ef248SJed Brown PetscFunctionReturn(0); 50850e4ef248SJed Brown } 50860e4ef248SJed Brown 50870e4ef248SJed Brown /*@C 50880e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 50890e4ef248SJed Brown 50900e4ef248SJed Brown Collective on TS 50910e4ef248SJed Brown 50920e4ef248SJed Brown Input Arguments: 50930e4ef248SJed Brown + ts - time stepping context 50940e4ef248SJed Brown . t - time at which to evaluate 50950910c330SBarry Smith . U - state at which to evaluate 50960e4ef248SJed Brown - ctx - context 50970e4ef248SJed Brown 50980e4ef248SJed Brown Output Arguments: 50990e4ef248SJed Brown + A - pointer to operator 51000e4ef248SJed Brown . B - pointer to preconditioning matrix 51010e4ef248SJed Brown - flg - matrix structure flag 51020e4ef248SJed Brown 51030e4ef248SJed Brown Level: intermediate 51040e4ef248SJed Brown 51050e4ef248SJed Brown Notes: 51060e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 51070e4ef248SJed Brown 51080e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 51090e4ef248SJed Brown @*/ 5110d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 51110e4ef248SJed Brown { 51120e4ef248SJed Brown PetscFunctionBegin; 51130e4ef248SJed Brown PetscFunctionReturn(0); 51140e4ef248SJed Brown } 51150e4ef248SJed Brown 51160026cea9SSean Farley /*@C 51170026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 51180026cea9SSean Farley 51190026cea9SSean Farley Collective on TS 51200026cea9SSean Farley 51210026cea9SSean Farley Input Arguments: 51220026cea9SSean Farley + ts - time stepping context 51230026cea9SSean Farley . t - time at which to evaluate 51240910c330SBarry Smith . U - state at which to evaluate 51250910c330SBarry Smith . Udot - time derivative of state vector 51260026cea9SSean Farley - ctx - context 51270026cea9SSean Farley 51280026cea9SSean Farley Output Arguments: 51290026cea9SSean Farley . F - left hand side 51300026cea9SSean Farley 51310026cea9SSean Farley Level: intermediate 51320026cea9SSean Farley 51330026cea9SSean Farley Notes: 51340910c330SBarry 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 51350026cea9SSean Farley user is required to write their own TSComputeIFunction. 51360026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 51370026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 51380026cea9SSean Farley 51399ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 51409ae8fd06SBarry Smith 51419ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 51420026cea9SSean Farley @*/ 51430910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 51440026cea9SSean Farley { 51450026cea9SSean Farley PetscErrorCode ierr; 51460026cea9SSean Farley Mat A,B; 51470026cea9SSean Farley 51480026cea9SSean Farley PetscFunctionBegin; 51490298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 5150d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 51510910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 51520026cea9SSean Farley PetscFunctionReturn(0); 51530026cea9SSean Farley } 51540026cea9SSean Farley 51550026cea9SSean Farley /*@C 5156b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 51570026cea9SSean Farley 51580026cea9SSean Farley Collective on TS 51590026cea9SSean Farley 51600026cea9SSean Farley Input Arguments: 51610026cea9SSean Farley + ts - time stepping context 51620026cea9SSean Farley . t - time at which to evaluate 51630910c330SBarry Smith . U - state at which to evaluate 51640910c330SBarry Smith . Udot - time derivative of state vector 51650026cea9SSean Farley . shift - shift to apply 51660026cea9SSean Farley - ctx - context 51670026cea9SSean Farley 51680026cea9SSean Farley Output Arguments: 51690026cea9SSean Farley + A - pointer to operator 51700026cea9SSean Farley . B - pointer to preconditioning matrix 51710026cea9SSean Farley - flg - matrix structure flag 51720026cea9SSean Farley 5173b41af12eSJed Brown Level: advanced 51740026cea9SSean Farley 51750026cea9SSean Farley Notes: 51760026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 51770026cea9SSean Farley 5178b41af12eSJed Brown It is only appropriate for problems of the form 5179b41af12eSJed Brown 5180b41af12eSJed Brown $ M Udot = F(U,t) 5181b41af12eSJed Brown 5182b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 5183b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 5184b41af12eSJed Brown an implicit operator of the form 5185b41af12eSJed Brown 5186b41af12eSJed Brown $ shift*M + J 5187b41af12eSJed Brown 5188b41af12eSJed 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 5189b41af12eSJed Brown a copy of M or reassemble it when requested. 5190b41af12eSJed Brown 51910026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 51920026cea9SSean Farley @*/ 5193d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 51940026cea9SSean Farley { 5195b41af12eSJed Brown PetscErrorCode ierr; 5196b41af12eSJed Brown 51970026cea9SSean Farley PetscFunctionBegin; 519894ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 5199b41af12eSJed Brown ts->ijacobian.shift = shift; 52000026cea9SSean Farley PetscFunctionReturn(0); 52010026cea9SSean Farley } 5202b41af12eSJed Brown 5203e817cc15SEmil Constantinescu /*@ 5204e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 5205e817cc15SEmil Constantinescu 5206e817cc15SEmil Constantinescu Not Collective 5207e817cc15SEmil Constantinescu 5208e817cc15SEmil Constantinescu Input Parameter: 5209e817cc15SEmil Constantinescu . ts - the TS context 5210e817cc15SEmil Constantinescu 5211e817cc15SEmil Constantinescu Output Parameter: 52124e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 5213e817cc15SEmil Constantinescu 5214e817cc15SEmil Constantinescu Level: beginner 5215e817cc15SEmil Constantinescu 5216e817cc15SEmil Constantinescu .keywords: TS, equation type 5217e817cc15SEmil Constantinescu 5218e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 5219e817cc15SEmil Constantinescu @*/ 5220e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 5221e817cc15SEmil Constantinescu { 5222e817cc15SEmil Constantinescu PetscFunctionBegin; 5223e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5224e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 5225e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 5226e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5227e817cc15SEmil Constantinescu } 5228e817cc15SEmil Constantinescu 5229e817cc15SEmil Constantinescu /*@ 5230e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 5231e817cc15SEmil Constantinescu 5232e817cc15SEmil Constantinescu Not Collective 5233e817cc15SEmil Constantinescu 5234e817cc15SEmil Constantinescu Input Parameter: 5235e817cc15SEmil Constantinescu + ts - the TS context 52361297b224SEmil Constantinescu - equation_type - see TSEquationType 5237e817cc15SEmil Constantinescu 5238e817cc15SEmil Constantinescu Level: advanced 5239e817cc15SEmil Constantinescu 5240e817cc15SEmil Constantinescu .keywords: TS, equation type 5241e817cc15SEmil Constantinescu 5242e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 5243e817cc15SEmil Constantinescu @*/ 5244e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 5245e817cc15SEmil Constantinescu { 5246e817cc15SEmil Constantinescu PetscFunctionBegin; 5247e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5248e817cc15SEmil Constantinescu ts->equation_type = equation_type; 5249e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5250e817cc15SEmil Constantinescu } 52510026cea9SSean Farley 52524af1b03aSJed Brown /*@ 52534af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 52544af1b03aSJed Brown 52554af1b03aSJed Brown Not Collective 52564af1b03aSJed Brown 52574af1b03aSJed Brown Input Parameter: 52584af1b03aSJed Brown . ts - the TS context 52594af1b03aSJed Brown 52604af1b03aSJed Brown Output Parameter: 52614af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 52624af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 52634af1b03aSJed Brown 5264487e0bb9SJed Brown Level: beginner 52654af1b03aSJed Brown 5266cd652676SJed Brown Notes: 5267cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 52684af1b03aSJed Brown 52694af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 52704af1b03aSJed Brown 52714af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 52724af1b03aSJed Brown @*/ 52734af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 52744af1b03aSJed Brown { 52754af1b03aSJed Brown PetscFunctionBegin; 52764af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 52774af1b03aSJed Brown PetscValidPointer(reason,2); 52784af1b03aSJed Brown *reason = ts->reason; 52794af1b03aSJed Brown PetscFunctionReturn(0); 52804af1b03aSJed Brown } 52814af1b03aSJed Brown 5282d6ad946cSShri Abhyankar /*@ 5283d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 5284d6ad946cSShri Abhyankar 5285d6ad946cSShri Abhyankar Not Collective 5286d6ad946cSShri Abhyankar 5287d6ad946cSShri Abhyankar Input Parameter: 5288d6ad946cSShri Abhyankar + ts - the TS context 5289d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 5290d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 5291d6ad946cSShri Abhyankar 5292f5abba47SShri Abhyankar Level: advanced 5293d6ad946cSShri Abhyankar 5294d6ad946cSShri Abhyankar Notes: 5295d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 5296d6ad946cSShri Abhyankar 5297d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 5298d6ad946cSShri Abhyankar 5299d6ad946cSShri Abhyankar .seealso: TSConvergedReason 5300d6ad946cSShri Abhyankar @*/ 5301d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 5302d6ad946cSShri Abhyankar { 5303d6ad946cSShri Abhyankar PetscFunctionBegin; 5304d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5305d6ad946cSShri Abhyankar ts->reason = reason; 5306d6ad946cSShri Abhyankar PetscFunctionReturn(0); 5307d6ad946cSShri Abhyankar } 5308d6ad946cSShri Abhyankar 5309cc708dedSBarry Smith /*@ 5310cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 5311cc708dedSBarry Smith 5312cc708dedSBarry Smith Not Collective 5313cc708dedSBarry Smith 5314cc708dedSBarry Smith Input Parameter: 5315cc708dedSBarry Smith . ts - the TS context 5316cc708dedSBarry Smith 5317cc708dedSBarry Smith Output Parameter: 531863e21af5SBarry Smith . ftime - the final time. This time corresponds to the final time set with TSSetDuration() 5319cc708dedSBarry Smith 5320487e0bb9SJed Brown Level: beginner 5321cc708dedSBarry Smith 5322cc708dedSBarry Smith Notes: 5323cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 5324cc708dedSBarry Smith 5325cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 5326cc708dedSBarry Smith 5327cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 5328cc708dedSBarry Smith @*/ 5329cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 5330cc708dedSBarry Smith { 5331cc708dedSBarry Smith PetscFunctionBegin; 5332cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5333cc708dedSBarry Smith PetscValidPointer(ftime,2); 5334cc708dedSBarry Smith *ftime = ts->solvetime; 5335cc708dedSBarry Smith PetscFunctionReturn(0); 5336cc708dedSBarry Smith } 5337cc708dedSBarry Smith 53382c18e0fdSBarry Smith /*@ 53392c18e0fdSBarry Smith TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 53402c18e0fdSBarry Smith 53412c18e0fdSBarry Smith Not Collective 53422c18e0fdSBarry Smith 53432c18e0fdSBarry Smith Input Parameter: 53442c18e0fdSBarry Smith . ts - the TS context 53452c18e0fdSBarry Smith 53462c18e0fdSBarry Smith Output Parameter: 53472c18e0fdSBarry Smith . steps - the number of steps 53482c18e0fdSBarry Smith 53492c18e0fdSBarry Smith Level: beginner 53502c18e0fdSBarry Smith 53512c18e0fdSBarry Smith Notes: 53522c18e0fdSBarry Smith Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 53532c18e0fdSBarry Smith 53542c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test 53552c18e0fdSBarry Smith 53562c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 53572c18e0fdSBarry Smith @*/ 53582c18e0fdSBarry Smith PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 53592c18e0fdSBarry Smith { 53602c18e0fdSBarry Smith PetscFunctionBegin; 53612c18e0fdSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53622c18e0fdSBarry Smith PetscValidPointer(steps,2); 53632c18e0fdSBarry Smith *steps = ts->total_steps; 53642c18e0fdSBarry Smith PetscFunctionReturn(0); 53652c18e0fdSBarry Smith } 53662c18e0fdSBarry Smith 53679f67acb7SJed Brown /*@ 53685ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 53699f67acb7SJed Brown used by the time integrator. 53709f67acb7SJed Brown 53719f67acb7SJed Brown Not Collective 53729f67acb7SJed Brown 53739f67acb7SJed Brown Input Parameter: 53749f67acb7SJed Brown . ts - TS context 53759f67acb7SJed Brown 53769f67acb7SJed Brown Output Parameter: 53779f67acb7SJed Brown . nits - number of nonlinear iterations 53789f67acb7SJed Brown 53799f67acb7SJed Brown Notes: 53809f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 53819f67acb7SJed Brown 53829f67acb7SJed Brown Level: intermediate 53839f67acb7SJed Brown 53849f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 53859f67acb7SJed Brown 53865ef26d82SJed Brown .seealso: TSGetKSPIterations() 53879f67acb7SJed Brown @*/ 53885ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 53899f67acb7SJed Brown { 53909f67acb7SJed Brown PetscFunctionBegin; 53919f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53929f67acb7SJed Brown PetscValidIntPointer(nits,2); 53935ef26d82SJed Brown *nits = ts->snes_its; 53949f67acb7SJed Brown PetscFunctionReturn(0); 53959f67acb7SJed Brown } 53969f67acb7SJed Brown 53979f67acb7SJed Brown /*@ 53985ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 53999f67acb7SJed Brown used by the time integrator. 54009f67acb7SJed Brown 54019f67acb7SJed Brown Not Collective 54029f67acb7SJed Brown 54039f67acb7SJed Brown Input Parameter: 54049f67acb7SJed Brown . ts - TS context 54059f67acb7SJed Brown 54069f67acb7SJed Brown Output Parameter: 54079f67acb7SJed Brown . lits - number of linear iterations 54089f67acb7SJed Brown 54099f67acb7SJed Brown Notes: 54109f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 54119f67acb7SJed Brown 54129f67acb7SJed Brown Level: intermediate 54139f67acb7SJed Brown 54149f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 54159f67acb7SJed Brown 54165ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 54179f67acb7SJed Brown @*/ 54185ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 54199f67acb7SJed Brown { 54209f67acb7SJed Brown PetscFunctionBegin; 54219f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 54229f67acb7SJed Brown PetscValidIntPointer(lits,2); 54235ef26d82SJed Brown *lits = ts->ksp_its; 54249f67acb7SJed Brown PetscFunctionReturn(0); 54259f67acb7SJed Brown } 54269f67acb7SJed Brown 5427cef5090cSJed Brown /*@ 5428cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 5429cef5090cSJed Brown 5430cef5090cSJed Brown Not Collective 5431cef5090cSJed Brown 5432cef5090cSJed Brown Input Parameter: 5433cef5090cSJed Brown . ts - TS context 5434cef5090cSJed Brown 5435cef5090cSJed Brown Output Parameter: 5436cef5090cSJed Brown . rejects - number of steps rejected 5437cef5090cSJed Brown 5438cef5090cSJed Brown Notes: 5439cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5440cef5090cSJed Brown 5441cef5090cSJed Brown Level: intermediate 5442cef5090cSJed Brown 5443cef5090cSJed Brown .keywords: TS, get, number 5444cef5090cSJed Brown 54455ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 5446cef5090cSJed Brown @*/ 5447cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 5448cef5090cSJed Brown { 5449cef5090cSJed Brown PetscFunctionBegin; 5450cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5451cef5090cSJed Brown PetscValidIntPointer(rejects,2); 5452cef5090cSJed Brown *rejects = ts->reject; 5453cef5090cSJed Brown PetscFunctionReturn(0); 5454cef5090cSJed Brown } 5455cef5090cSJed Brown 5456cef5090cSJed Brown /*@ 5457cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 5458cef5090cSJed Brown 5459cef5090cSJed Brown Not Collective 5460cef5090cSJed Brown 5461cef5090cSJed Brown Input Parameter: 5462cef5090cSJed Brown . ts - TS context 5463cef5090cSJed Brown 5464cef5090cSJed Brown Output Parameter: 5465cef5090cSJed Brown . fails - number of failed nonlinear solves 5466cef5090cSJed Brown 5467cef5090cSJed Brown Notes: 5468cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5469cef5090cSJed Brown 5470cef5090cSJed Brown Level: intermediate 5471cef5090cSJed Brown 5472cef5090cSJed Brown .keywords: TS, get, number 5473cef5090cSJed Brown 54745ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 5475cef5090cSJed Brown @*/ 5476cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 5477cef5090cSJed Brown { 5478cef5090cSJed Brown PetscFunctionBegin; 5479cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5480cef5090cSJed Brown PetscValidIntPointer(fails,2); 5481cef5090cSJed Brown *fails = ts->num_snes_failures; 5482cef5090cSJed Brown PetscFunctionReturn(0); 5483cef5090cSJed Brown } 5484cef5090cSJed Brown 5485cef5090cSJed Brown /*@ 5486cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 5487cef5090cSJed Brown 5488cef5090cSJed Brown Not Collective 5489cef5090cSJed Brown 5490cef5090cSJed Brown Input Parameter: 5491cef5090cSJed Brown + ts - TS context 5492cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 5493cef5090cSJed Brown 5494cef5090cSJed Brown Notes: 5495cef5090cSJed Brown The counter is reset to zero for each step 5496cef5090cSJed Brown 5497cef5090cSJed Brown Options Database Key: 5498cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 5499cef5090cSJed Brown 5500cef5090cSJed Brown Level: intermediate 5501cef5090cSJed Brown 5502cef5090cSJed Brown .keywords: TS, set, maximum, number 5503cef5090cSJed Brown 55045ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5505cef5090cSJed Brown @*/ 5506cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 5507cef5090cSJed Brown { 5508cef5090cSJed Brown PetscFunctionBegin; 5509cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5510cef5090cSJed Brown ts->max_reject = rejects; 5511cef5090cSJed Brown PetscFunctionReturn(0); 5512cef5090cSJed Brown } 5513cef5090cSJed Brown 5514cef5090cSJed Brown /*@ 5515cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 5516cef5090cSJed Brown 5517cef5090cSJed Brown Not Collective 5518cef5090cSJed Brown 5519cef5090cSJed Brown Input Parameter: 5520cef5090cSJed Brown + ts - TS context 5521cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 5522cef5090cSJed Brown 5523cef5090cSJed Brown Notes: 5524cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 5525cef5090cSJed Brown 5526cef5090cSJed Brown Options Database Key: 5527cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 5528cef5090cSJed Brown 5529cef5090cSJed Brown Level: intermediate 5530cef5090cSJed Brown 5531cef5090cSJed Brown .keywords: TS, set, maximum, number 5532cef5090cSJed Brown 55335ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 5534cef5090cSJed Brown @*/ 5535cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 5536cef5090cSJed Brown { 5537cef5090cSJed Brown PetscFunctionBegin; 5538cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5539cef5090cSJed Brown ts->max_snes_failures = fails; 5540cef5090cSJed Brown PetscFunctionReturn(0); 5541cef5090cSJed Brown } 5542cef5090cSJed Brown 5543cef5090cSJed Brown /*@ 5544cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 5545cef5090cSJed Brown 5546cef5090cSJed Brown Not Collective 5547cef5090cSJed Brown 5548cef5090cSJed Brown Input Parameter: 5549cef5090cSJed Brown + ts - TS context 5550cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 5551cef5090cSJed Brown 5552cef5090cSJed Brown Options Database Key: 5553cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 5554cef5090cSJed Brown 5555cef5090cSJed Brown Level: intermediate 5556cef5090cSJed Brown 5557cef5090cSJed Brown .keywords: TS, set, error 5558cef5090cSJed Brown 55595ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5560cef5090cSJed Brown @*/ 5561cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 5562cef5090cSJed Brown { 5563cef5090cSJed Brown PetscFunctionBegin; 5564cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5565cef5090cSJed Brown ts->errorifstepfailed = err; 5566cef5090cSJed Brown PetscFunctionReturn(0); 5567cef5090cSJed Brown } 5568cef5090cSJed Brown 5569fb1732b5SBarry Smith /*@C 5570fde5950dSBarry 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 5571fb1732b5SBarry Smith 5572fb1732b5SBarry Smith Collective on TS 5573fb1732b5SBarry Smith 5574fb1732b5SBarry Smith Input Parameters: 5575fb1732b5SBarry Smith + ts - the TS context 5576fb1732b5SBarry Smith . step - current time-step 5577fb1732b5SBarry Smith . ptime - current time 55780910c330SBarry Smith . u - current state 5579721cd6eeSBarry Smith - vf - viewer and its format 5580fb1732b5SBarry Smith 5581fb1732b5SBarry Smith Level: intermediate 5582fb1732b5SBarry Smith 5583fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 5584fb1732b5SBarry Smith 5585fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5586fb1732b5SBarry Smith @*/ 5587721cd6eeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf) 5588fb1732b5SBarry Smith { 5589fb1732b5SBarry Smith PetscErrorCode ierr; 5590fb1732b5SBarry Smith 5591fb1732b5SBarry Smith PetscFunctionBegin; 5592721cd6eeSBarry Smith ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr); 5593721cd6eeSBarry Smith ierr = VecView(u,vf->viewer);CHKERRQ(ierr); 5594721cd6eeSBarry Smith ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr); 5595ed81e22dSJed Brown PetscFunctionReturn(0); 5596ed81e22dSJed Brown } 5597ed81e22dSJed Brown 5598ed81e22dSJed Brown /*@C 5599ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5600ed81e22dSJed Brown 5601ed81e22dSJed Brown Collective on TS 5602ed81e22dSJed Brown 5603ed81e22dSJed Brown Input Parameters: 5604ed81e22dSJed Brown + ts - the TS context 5605ed81e22dSJed Brown . step - current time-step 5606ed81e22dSJed Brown . ptime - current time 56070910c330SBarry Smith . u - current state 5608ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5609ed81e22dSJed Brown 5610ed81e22dSJed Brown Level: intermediate 5611ed81e22dSJed Brown 5612ed81e22dSJed Brown Notes: 5613ed81e22dSJed 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. 5614ed81e22dSJed Brown These are named according to the file name template. 5615ed81e22dSJed Brown 5616ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5617ed81e22dSJed Brown 5618ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5619ed81e22dSJed Brown 5620ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5621ed81e22dSJed Brown @*/ 56220910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5623ed81e22dSJed Brown { 5624ed81e22dSJed Brown PetscErrorCode ierr; 5625ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 5626ed81e22dSJed Brown PetscViewer viewer; 5627ed81e22dSJed Brown 5628ed81e22dSJed Brown PetscFunctionBegin; 562963e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 56308caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5631ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 56320910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 5633ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5634ed81e22dSJed Brown PetscFunctionReturn(0); 5635ed81e22dSJed Brown } 5636ed81e22dSJed Brown 5637ed81e22dSJed Brown /*@C 5638ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5639ed81e22dSJed Brown 5640ed81e22dSJed Brown Collective on TS 5641ed81e22dSJed Brown 5642ed81e22dSJed Brown Input Parameters: 5643ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5644ed81e22dSJed Brown 5645ed81e22dSJed Brown Level: intermediate 5646ed81e22dSJed Brown 5647ed81e22dSJed Brown Note: 5648ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5649ed81e22dSJed Brown 5650ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5651ed81e22dSJed Brown 5652ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5653ed81e22dSJed Brown @*/ 5654ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5655ed81e22dSJed Brown { 5656ed81e22dSJed Brown PetscErrorCode ierr; 5657ed81e22dSJed Brown 5658ed81e22dSJed Brown PetscFunctionBegin; 5659ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5660fb1732b5SBarry Smith PetscFunctionReturn(0); 5661fb1732b5SBarry Smith } 5662fb1732b5SBarry Smith 566384df9cb4SJed Brown /*@ 5664552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 566584df9cb4SJed Brown 5666ed81e22dSJed Brown Collective on TS if controller has not been created yet 566784df9cb4SJed Brown 566884df9cb4SJed Brown Input Arguments: 5669ed81e22dSJed Brown . ts - time stepping context 567084df9cb4SJed Brown 567184df9cb4SJed Brown Output Arguments: 5672ed81e22dSJed Brown . adapt - adaptive controller 567384df9cb4SJed Brown 567484df9cb4SJed Brown Level: intermediate 567584df9cb4SJed Brown 5676ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 567784df9cb4SJed Brown @*/ 5678552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 567984df9cb4SJed Brown { 568084df9cb4SJed Brown PetscErrorCode ierr; 568184df9cb4SJed Brown 568284df9cb4SJed Brown PetscFunctionBegin; 568384df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5684bec58848SLisandro Dalcin PetscValidPointer(adapt,2); 568584df9cb4SJed Brown if (!ts->adapt) { 5686ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 56873bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 56881c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 568984df9cb4SJed Brown } 5690bec58848SLisandro Dalcin *adapt = ts->adapt; 569184df9cb4SJed Brown PetscFunctionReturn(0); 569284df9cb4SJed Brown } 5693d6ebe24aSShri Abhyankar 56941c3436cfSJed Brown /*@ 56951c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 56961c3436cfSJed Brown 56971c3436cfSJed Brown Logically Collective 56981c3436cfSJed Brown 56991c3436cfSJed Brown Input Arguments: 57001c3436cfSJed Brown + ts - time integration context 57011c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 57020298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 57031c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 57040298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 57051c3436cfSJed Brown 5706a3cdaa26SBarry Smith Options Database keys: 5707a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5708a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5709a3cdaa26SBarry Smith 57103ff766beSShri Abhyankar Notes: 57113ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 57123ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 57133ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 57143ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 57153ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 57163ff766beSShri Abhyankar differential variables. 57173ff766beSShri Abhyankar 57181c3436cfSJed Brown Level: beginner 57191c3436cfSJed Brown 5720c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 57211c3436cfSJed Brown @*/ 57221c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 57231c3436cfSJed Brown { 57241c3436cfSJed Brown PetscErrorCode ierr; 57251c3436cfSJed Brown 57261c3436cfSJed Brown PetscFunctionBegin; 5727c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 57281c3436cfSJed Brown if (vatol) { 57291c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 57301c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 57311c3436cfSJed Brown ts->vatol = vatol; 57321c3436cfSJed Brown } 5733c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 57341c3436cfSJed Brown if (vrtol) { 57351c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 57361c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 57371c3436cfSJed Brown ts->vrtol = vrtol; 57381c3436cfSJed Brown } 57391c3436cfSJed Brown PetscFunctionReturn(0); 57401c3436cfSJed Brown } 57411c3436cfSJed Brown 5742c5033834SJed Brown /*@ 5743c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5744c5033834SJed Brown 5745c5033834SJed Brown Logically Collective 5746c5033834SJed Brown 5747c5033834SJed Brown Input Arguments: 5748c5033834SJed Brown . ts - time integration context 5749c5033834SJed Brown 5750c5033834SJed Brown Output Arguments: 57510298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 57520298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 57530298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 57540298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 5755c5033834SJed Brown 5756c5033834SJed Brown Level: beginner 5757c5033834SJed Brown 5758c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5759c5033834SJed Brown @*/ 5760c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5761c5033834SJed Brown { 5762c5033834SJed Brown PetscFunctionBegin; 5763c5033834SJed Brown if (atol) *atol = ts->atol; 5764c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5765c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5766c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 5767c5033834SJed Brown PetscFunctionReturn(0); 5768c5033834SJed Brown } 5769c5033834SJed Brown 57709c6b16b5SShri Abhyankar /*@ 5771a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 57729c6b16b5SShri Abhyankar 57739c6b16b5SShri Abhyankar Collective on TS 57749c6b16b5SShri Abhyankar 57759c6b16b5SShri Abhyankar Input Arguments: 57769c6b16b5SShri Abhyankar + ts - time stepping context 5777a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5778a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 57799c6b16b5SShri Abhyankar 57809c6b16b5SShri Abhyankar Output Arguments: 57817453f775SEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 57827453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 57837453f775SEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 57849c6b16b5SShri Abhyankar 57859c6b16b5SShri Abhyankar Level: developer 57869c6b16b5SShri Abhyankar 5787deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 57889c6b16b5SShri Abhyankar @*/ 57897453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 57909c6b16b5SShri Abhyankar { 57919c6b16b5SShri Abhyankar PetscErrorCode ierr; 57929c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 57937453f775SEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 57947453f775SEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 57959c6b16b5SShri Abhyankar const PetscScalar *u,*y; 57967453f775SEmil Constantinescu PetscReal sum,suma,sumr,gsum,gsuma,gsumr,diff; 57977453f775SEmil Constantinescu PetscReal tol,tola,tolr; 57987453f775SEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 57999c6b16b5SShri Abhyankar 58009c6b16b5SShri Abhyankar PetscFunctionBegin; 58019c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5802a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5803a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5804a4868fbcSLisandro Dalcin PetscValidType(U,2); 5805a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5806a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5807a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 58088a175baeSEmil Constantinescu PetscValidPointer(norma,5); 58098a175baeSEmil Constantinescu PetscValidPointer(normr,6); 5810a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 58119c6b16b5SShri Abhyankar 58129c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 58139c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 58149c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 58159c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 58169c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 58177453f775SEmil Constantinescu sum = 0.; n_loc = 0; 58187453f775SEmil Constantinescu suma = 0.; na_loc = 0; 58197453f775SEmil Constantinescu sumr = 0.; nr_loc = 0; 58209c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 58219c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 58229c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58239c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58249c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 58257453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 58267453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 58277453f775SEmil Constantinescu if(tola>0.){ 58287453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 58297453f775SEmil Constantinescu na_loc++; 58307453f775SEmil Constantinescu } 58317453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58327453f775SEmil Constantinescu if(tolr>0.){ 58337453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 58347453f775SEmil Constantinescu nr_loc++; 58357453f775SEmil Constantinescu } 58367453f775SEmil Constantinescu tol=tola+tolr; 58377453f775SEmil Constantinescu if(tol>0.){ 58387453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 58397453f775SEmil Constantinescu n_loc++; 58407453f775SEmil Constantinescu } 58419c6b16b5SShri Abhyankar } 58429c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58439c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58449c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 58459c6b16b5SShri Abhyankar const PetscScalar *atol; 58469c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58479c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 58487453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 58497453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 58507453f775SEmil Constantinescu if(tola>0.){ 58517453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 58527453f775SEmil Constantinescu na_loc++; 58537453f775SEmil Constantinescu } 58547453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58557453f775SEmil Constantinescu if(tolr>0.){ 58567453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 58577453f775SEmil Constantinescu nr_loc++; 58587453f775SEmil Constantinescu } 58597453f775SEmil Constantinescu tol=tola+tolr; 58607453f775SEmil Constantinescu if(tol>0.){ 58617453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 58627453f775SEmil Constantinescu n_loc++; 58637453f775SEmil Constantinescu } 58649c6b16b5SShri Abhyankar } 58659c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58669c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 58679c6b16b5SShri Abhyankar const PetscScalar *rtol; 58689c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58699c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 58707453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 58717453f775SEmil Constantinescu tola = ts->atol; 58727453f775SEmil Constantinescu if(tola>0.){ 58737453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 58747453f775SEmil Constantinescu na_loc++; 58757453f775SEmil Constantinescu } 58767453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58777453f775SEmil Constantinescu if(tolr>0.){ 58787453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 58797453f775SEmil Constantinescu nr_loc++; 58807453f775SEmil Constantinescu } 58817453f775SEmil Constantinescu tol=tola+tolr; 58827453f775SEmil Constantinescu if(tol>0.){ 58837453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 58847453f775SEmil Constantinescu n_loc++; 58857453f775SEmil Constantinescu } 58869c6b16b5SShri Abhyankar } 58879c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58889c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 58899c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 58907453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 58917453f775SEmil Constantinescu tola = ts->atol; 58927453f775SEmil Constantinescu if(tola>0.){ 58937453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 58947453f775SEmil Constantinescu na_loc++; 58957453f775SEmil Constantinescu } 58967453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58977453f775SEmil Constantinescu if(tolr>0.){ 58987453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 58997453f775SEmil Constantinescu nr_loc++; 59007453f775SEmil Constantinescu } 59017453f775SEmil Constantinescu tol=tola+tolr; 59027453f775SEmil Constantinescu if(tol>0.){ 59037453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 59047453f775SEmil Constantinescu n_loc++; 59057453f775SEmil Constantinescu } 59069c6b16b5SShri Abhyankar } 59079c6b16b5SShri Abhyankar } 59089c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 59099c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 59109c6b16b5SShri Abhyankar 59117453f775SEmil Constantinescu err_loc[0] = sum; 59127453f775SEmil Constantinescu err_loc[1] = suma; 59137453f775SEmil Constantinescu err_loc[2] = sumr; 59147453f775SEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 59157453f775SEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 59167453f775SEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 59177453f775SEmil Constantinescu 5918a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 59197453f775SEmil Constantinescu 59207453f775SEmil Constantinescu gsum = err_glb[0]; 59217453f775SEmil Constantinescu gsuma = err_glb[1]; 59227453f775SEmil Constantinescu gsumr = err_glb[2]; 59237453f775SEmil Constantinescu n_glb = err_glb[3]; 59247453f775SEmil Constantinescu na_glb = err_glb[4]; 59257453f775SEmil Constantinescu nr_glb = err_glb[5]; 59267453f775SEmil Constantinescu 5927b1316ef9SEmil Constantinescu *norm = 0.; 5928b1316ef9SEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 5929b1316ef9SEmil Constantinescu *norma = 0.; 5930b1316ef9SEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 5931b1316ef9SEmil Constantinescu *normr = 0.; 5932b1316ef9SEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 59339c6b16b5SShri Abhyankar 59349c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 59357453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 59367453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 59379c6b16b5SShri Abhyankar PetscFunctionReturn(0); 59389c6b16b5SShri Abhyankar } 59399c6b16b5SShri Abhyankar 59409c6b16b5SShri Abhyankar /*@ 5941a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 59429c6b16b5SShri Abhyankar 59439c6b16b5SShri Abhyankar Collective on TS 59449c6b16b5SShri Abhyankar 59459c6b16b5SShri Abhyankar Input Arguments: 59469c6b16b5SShri Abhyankar + ts - time stepping context 5947a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5948a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 59499c6b16b5SShri Abhyankar 59509c6b16b5SShri Abhyankar Output Arguments: 59517453f775SEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 59527453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 59537453f775SEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 59549c6b16b5SShri Abhyankar 59559c6b16b5SShri Abhyankar Level: developer 59569c6b16b5SShri Abhyankar 5957deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 59589c6b16b5SShri Abhyankar @*/ 59597453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 59609c6b16b5SShri Abhyankar { 59619c6b16b5SShri Abhyankar PetscErrorCode ierr; 59627453f775SEmil Constantinescu PetscInt i,n,N,rstart; 59639c6b16b5SShri Abhyankar const PetscScalar *u,*y; 59647453f775SEmil Constantinescu PetscReal max,gmax,maxa,gmaxa,maxr,gmaxr; 59657453f775SEmil Constantinescu PetscReal tol,tola,tolr,diff; 59667453f775SEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 59679c6b16b5SShri Abhyankar 59689c6b16b5SShri Abhyankar PetscFunctionBegin; 59699c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5970a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5971a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5972a4868fbcSLisandro Dalcin PetscValidType(U,2); 5973a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5974a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5975a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 59768a175baeSEmil Constantinescu PetscValidPointer(norma,5); 59778a175baeSEmil Constantinescu PetscValidPointer(normr,6); 5978a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 59799c6b16b5SShri Abhyankar 59809c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 59819c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 59829c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 59839c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 59849c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 59857453f775SEmil Constantinescu 59867453f775SEmil Constantinescu max=0.; 59877453f775SEmil Constantinescu maxa=0.; 59887453f775SEmil Constantinescu maxr=0.; 59897453f775SEmil Constantinescu 59907453f775SEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 59919c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 59929c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59939c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59947453f775SEmil Constantinescu 59957453f775SEmil Constantinescu for (i=0; i<n; i++) { 59967453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 59977453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 59987453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59997453f775SEmil Constantinescu tol = tola+tolr; 60007453f775SEmil Constantinescu if(tola>0.){ 60017453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 60027453f775SEmil Constantinescu } 60037453f775SEmil Constantinescu if(tolr>0.){ 60047453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 60057453f775SEmil Constantinescu } 60067453f775SEmil Constantinescu if(tol>0.){ 60077453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 60087453f775SEmil Constantinescu } 60099c6b16b5SShri Abhyankar } 60109c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 60119c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60129c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 60139c6b16b5SShri Abhyankar const PetscScalar *atol; 60149c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 60157453f775SEmil Constantinescu for (i=0; i<n; i++) { 60167453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 60177453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 60187453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60197453f775SEmil Constantinescu tol = tola+tolr; 60207453f775SEmil Constantinescu if(tola>0.){ 60217453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 60227453f775SEmil Constantinescu } 60237453f775SEmil Constantinescu if(tolr>0.){ 60247453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 60257453f775SEmil Constantinescu } 60267453f775SEmil Constantinescu if(tol>0.){ 60277453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 60287453f775SEmil Constantinescu } 60299c6b16b5SShri Abhyankar } 60309c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 60319c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 60329c6b16b5SShri Abhyankar const PetscScalar *rtol; 60339c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60347453f775SEmil Constantinescu 60357453f775SEmil Constantinescu for (i=0; i<n; i++) { 60367453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 60377453f775SEmil Constantinescu tola = ts->atol; 60387453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60397453f775SEmil Constantinescu tol = tola+tolr; 60407453f775SEmil Constantinescu if(tola>0.){ 60417453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 60427453f775SEmil Constantinescu } 60437453f775SEmil Constantinescu if(tolr>0.){ 60447453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 60457453f775SEmil Constantinescu } 60467453f775SEmil Constantinescu if(tol>0.){ 60477453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 60487453f775SEmil Constantinescu } 60499c6b16b5SShri Abhyankar } 60509c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60519c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 60527453f775SEmil Constantinescu 60537453f775SEmil Constantinescu for (i=0; i<n; i++) { 60547453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 60557453f775SEmil Constantinescu tola = ts->atol; 60567453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60577453f775SEmil Constantinescu tol = tola+tolr; 60587453f775SEmil Constantinescu if(tola>0.){ 60597453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 60607453f775SEmil Constantinescu } 60617453f775SEmil Constantinescu if(tolr>0.){ 60627453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 60637453f775SEmil Constantinescu } 60647453f775SEmil Constantinescu if(tol>0.){ 60657453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 60667453f775SEmil Constantinescu } 60679c6b16b5SShri Abhyankar } 60689c6b16b5SShri Abhyankar } 60699c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 60709c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 60717453f775SEmil Constantinescu err_loc[0] = max; 60727453f775SEmil Constantinescu err_loc[1] = maxa; 60737453f775SEmil Constantinescu err_loc[2] = maxr; 6074a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 60757453f775SEmil Constantinescu gmax = err_glb[0]; 60767453f775SEmil Constantinescu gmaxa = err_glb[1]; 60777453f775SEmil Constantinescu gmaxr = err_glb[2]; 60789c6b16b5SShri Abhyankar 60799c6b16b5SShri Abhyankar *norm = gmax; 60807453f775SEmil Constantinescu *norma = gmaxa; 60817453f775SEmil Constantinescu *normr = gmaxr; 60829c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 60837453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 60847453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 60859c6b16b5SShri Abhyankar PetscFunctionReturn(0); 60869c6b16b5SShri Abhyankar } 60879c6b16b5SShri Abhyankar 60881c3436cfSJed Brown /*@ 60898a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 60901c3436cfSJed Brown 60911c3436cfSJed Brown Collective on TS 60921c3436cfSJed Brown 60931c3436cfSJed Brown Input Arguments: 60941c3436cfSJed Brown + ts - time stepping context 6095a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 6096a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 6097a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 60987619abb3SShri 60991c3436cfSJed Brown Output Arguments: 61008a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 61018a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 61028a175baeSEmil Constantinescu . normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 6103a4868fbcSLisandro Dalcin 6104a4868fbcSLisandro Dalcin Options Database Keys: 6105a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 6106a4868fbcSLisandro Dalcin 61071c3436cfSJed Brown Level: developer 61081c3436cfSJed Brown 61098a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm 61101c3436cfSJed Brown @*/ 61117453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 61121c3436cfSJed Brown { 61138beabaa1SBarry Smith PetscErrorCode ierr; 61141c3436cfSJed Brown 61151c3436cfSJed Brown PetscFunctionBegin; 6116a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 61177453f775SEmil Constantinescu ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 6118a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 61197453f775SEmil Constantinescu ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 6120a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 61211c3436cfSJed Brown PetscFunctionReturn(0); 61221c3436cfSJed Brown } 61231c3436cfSJed Brown 61248a175baeSEmil Constantinescu 61258a175baeSEmil Constantinescu /*@ 61268a175baeSEmil Constantinescu TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances 61278a175baeSEmil Constantinescu 61288a175baeSEmil Constantinescu Collective on TS 61298a175baeSEmil Constantinescu 61308a175baeSEmil Constantinescu Input Arguments: 61318a175baeSEmil Constantinescu + ts - time stepping context 61328a175baeSEmil Constantinescu . E - error vector 61338a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 61348a175baeSEmil Constantinescu - Y - state vector, previous time step 61358a175baeSEmil Constantinescu 61368a175baeSEmil Constantinescu Output Arguments: 61378a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 61388a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 61398a175baeSEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 61408a175baeSEmil Constantinescu 61418a175baeSEmil Constantinescu Level: developer 61428a175baeSEmil Constantinescu 61438a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity() 61448a175baeSEmil Constantinescu @*/ 61458a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 61468a175baeSEmil Constantinescu { 61478a175baeSEmil Constantinescu PetscErrorCode ierr; 61488a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 61498a175baeSEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 61508a175baeSEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 61518a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 61528a175baeSEmil Constantinescu PetscReal err,sum,suma,sumr,gsum,gsuma,gsumr; 61538a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 61548a175baeSEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 61558a175baeSEmil Constantinescu 61568a175baeSEmil Constantinescu PetscFunctionBegin; 61578a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 61588a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 61598a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 61608a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 61618a175baeSEmil Constantinescu PetscValidType(E,2); 61628a175baeSEmil Constantinescu PetscValidType(U,3); 61638a175baeSEmil Constantinescu PetscValidType(Y,4); 61648a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 61658a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 61668a175baeSEmil Constantinescu PetscValidPointer(norm,5); 61678a175baeSEmil Constantinescu PetscValidPointer(norma,6); 61688a175baeSEmil Constantinescu PetscValidPointer(normr,7); 61698a175baeSEmil Constantinescu 61708a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 61718a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 61728a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 61738a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 61748a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 61758a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 61768a175baeSEmil Constantinescu sum = 0.; n_loc = 0; 61778a175baeSEmil Constantinescu suma = 0.; na_loc = 0; 61788a175baeSEmil Constantinescu sumr = 0.; nr_loc = 0; 61798a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { 61808a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 61818a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61828a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61838a175baeSEmil Constantinescu for (i=0; i<n; i++) { 61848a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 61858a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 61868a175baeSEmil Constantinescu if(tola>0.){ 61878a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 61888a175baeSEmil Constantinescu na_loc++; 61898a175baeSEmil Constantinescu } 61908a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61918a175baeSEmil Constantinescu if(tolr>0.){ 61928a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 61938a175baeSEmil Constantinescu nr_loc++; 61948a175baeSEmil Constantinescu } 61958a175baeSEmil Constantinescu tol=tola+tolr; 61968a175baeSEmil Constantinescu if(tol>0.){ 61978a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 61988a175baeSEmil Constantinescu n_loc++; 61998a175baeSEmil Constantinescu } 62008a175baeSEmil Constantinescu } 62018a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 62028a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 62038a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 62048a175baeSEmil Constantinescu const PetscScalar *atol; 62058a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 62068a175baeSEmil Constantinescu for (i=0; i<n; i++) { 62078a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 62088a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 62098a175baeSEmil Constantinescu if(tola>0.){ 62108a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 62118a175baeSEmil Constantinescu na_loc++; 62128a175baeSEmil Constantinescu } 62138a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 62148a175baeSEmil Constantinescu if(tolr>0.){ 62158a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 62168a175baeSEmil Constantinescu nr_loc++; 62178a175baeSEmil Constantinescu } 62188a175baeSEmil Constantinescu tol=tola+tolr; 62198a175baeSEmil Constantinescu if(tol>0.){ 62208a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 62218a175baeSEmil Constantinescu n_loc++; 62228a175baeSEmil Constantinescu } 62238a175baeSEmil Constantinescu } 62248a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 62258a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 62268a175baeSEmil Constantinescu const PetscScalar *rtol; 62278a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 62288a175baeSEmil Constantinescu for (i=0; i<n; i++) { 62298a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 62308a175baeSEmil Constantinescu tola = ts->atol; 62318a175baeSEmil Constantinescu if(tola>0.){ 62328a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 62338a175baeSEmil Constantinescu na_loc++; 62348a175baeSEmil Constantinescu } 62358a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 62368a175baeSEmil Constantinescu if(tolr>0.){ 62378a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 62388a175baeSEmil Constantinescu nr_loc++; 62398a175baeSEmil Constantinescu } 62408a175baeSEmil Constantinescu tol=tola+tolr; 62418a175baeSEmil Constantinescu if(tol>0.){ 62428a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 62438a175baeSEmil Constantinescu n_loc++; 62448a175baeSEmil Constantinescu } 62458a175baeSEmil Constantinescu } 62468a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 62478a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 62488a175baeSEmil Constantinescu for (i=0; i<n; i++) { 62498a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 62508a175baeSEmil Constantinescu tola = ts->atol; 62518a175baeSEmil Constantinescu if(tola>0.){ 62528a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 62538a175baeSEmil Constantinescu na_loc++; 62548a175baeSEmil Constantinescu } 62558a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 62568a175baeSEmil Constantinescu if(tolr>0.){ 62578a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 62588a175baeSEmil Constantinescu nr_loc++; 62598a175baeSEmil Constantinescu } 62608a175baeSEmil Constantinescu tol=tola+tolr; 62618a175baeSEmil Constantinescu if(tol>0.){ 62628a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 62638a175baeSEmil Constantinescu n_loc++; 62648a175baeSEmil Constantinescu } 62658a175baeSEmil Constantinescu } 62668a175baeSEmil Constantinescu } 62678a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 62688a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 62698a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 62708a175baeSEmil Constantinescu 62718a175baeSEmil Constantinescu err_loc[0] = sum; 62728a175baeSEmil Constantinescu err_loc[1] = suma; 62738a175baeSEmil Constantinescu err_loc[2] = sumr; 62748a175baeSEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 62758a175baeSEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 62768a175baeSEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 62778a175baeSEmil Constantinescu 6278a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 62798a175baeSEmil Constantinescu 62808a175baeSEmil Constantinescu gsum = err_glb[0]; 62818a175baeSEmil Constantinescu gsuma = err_glb[1]; 62828a175baeSEmil Constantinescu gsumr = err_glb[2]; 62838a175baeSEmil Constantinescu n_glb = err_glb[3]; 62848a175baeSEmil Constantinescu na_glb = err_glb[4]; 62858a175baeSEmil Constantinescu nr_glb = err_glb[5]; 62868a175baeSEmil Constantinescu 62878a175baeSEmil Constantinescu *norm = 0.; 62888a175baeSEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 62898a175baeSEmil Constantinescu *norma = 0.; 62908a175baeSEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 62918a175baeSEmil Constantinescu *normr = 0.; 62928a175baeSEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 62938a175baeSEmil Constantinescu 62948a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 62958a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 62968a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 62978a175baeSEmil Constantinescu PetscFunctionReturn(0); 62988a175baeSEmil Constantinescu } 62998a175baeSEmil Constantinescu 63008a175baeSEmil Constantinescu /*@ 63018a175baeSEmil Constantinescu TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances 63028a175baeSEmil Constantinescu Collective on TS 63038a175baeSEmil Constantinescu 63048a175baeSEmil Constantinescu Input Arguments: 63058a175baeSEmil Constantinescu + ts - time stepping context 63068a175baeSEmil Constantinescu . E - error vector 63078a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 63088a175baeSEmil Constantinescu - Y - state vector, previous time step 63098a175baeSEmil Constantinescu 63108a175baeSEmil Constantinescu Output Arguments: 63118a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 63128a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 63138a175baeSEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 63148a175baeSEmil Constantinescu 63158a175baeSEmil Constantinescu Level: developer 63168a175baeSEmil Constantinescu 63178a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2() 63188a175baeSEmil Constantinescu @*/ 63198a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 63208a175baeSEmil Constantinescu { 63218a175baeSEmil Constantinescu PetscErrorCode ierr; 63228a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 63238a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 63248a175baeSEmil Constantinescu PetscReal err,max,gmax,maxa,gmaxa,maxr,gmaxr; 63258a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 63268a175baeSEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 63278a175baeSEmil Constantinescu 63288a175baeSEmil Constantinescu PetscFunctionBegin; 63298a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 63308a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 63318a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 63328a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 63338a175baeSEmil Constantinescu PetscValidType(E,2); 63348a175baeSEmil Constantinescu PetscValidType(U,3); 63358a175baeSEmil Constantinescu PetscValidType(Y,4); 63368a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 63378a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 63388a175baeSEmil Constantinescu PetscValidPointer(norm,5); 63398a175baeSEmil Constantinescu PetscValidPointer(norma,6); 63408a175baeSEmil Constantinescu PetscValidPointer(normr,7); 63418a175baeSEmil Constantinescu 63428a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 63438a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 63448a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 63458a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 63468a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 63478a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 63488a175baeSEmil Constantinescu 63498a175baeSEmil Constantinescu max=0.; 63508a175baeSEmil Constantinescu maxa=0.; 63518a175baeSEmil Constantinescu maxr=0.; 63528a175baeSEmil Constantinescu 63538a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 63548a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 63558a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 63568a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 63578a175baeSEmil Constantinescu 63588a175baeSEmil Constantinescu for (i=0; i<n; i++) { 63598a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 63608a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 63618a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 63628a175baeSEmil Constantinescu tol = tola+tolr; 63638a175baeSEmil Constantinescu if(tola>0.){ 63648a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 63658a175baeSEmil Constantinescu } 63668a175baeSEmil Constantinescu if(tolr>0.){ 63678a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 63688a175baeSEmil Constantinescu } 63698a175baeSEmil Constantinescu if(tol>0.){ 63708a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 63718a175baeSEmil Constantinescu } 63728a175baeSEmil Constantinescu } 63738a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 63748a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 63758a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 63768a175baeSEmil Constantinescu const PetscScalar *atol; 63778a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 63788a175baeSEmil Constantinescu for (i=0; i<n; i++) { 63798a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 63808a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 63818a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 63828a175baeSEmil Constantinescu tol = tola+tolr; 63838a175baeSEmil Constantinescu if(tola>0.){ 63848a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 63858a175baeSEmil Constantinescu } 63868a175baeSEmil Constantinescu if(tolr>0.){ 63878a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 63888a175baeSEmil Constantinescu } 63898a175baeSEmil Constantinescu if(tol>0.){ 63908a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 63918a175baeSEmil Constantinescu } 63928a175baeSEmil Constantinescu } 63938a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 63948a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 63958a175baeSEmil Constantinescu const PetscScalar *rtol; 63968a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 63978a175baeSEmil Constantinescu 63988a175baeSEmil Constantinescu for (i=0; i<n; i++) { 63998a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 64008a175baeSEmil Constantinescu tola = ts->atol; 64018a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 64028a175baeSEmil Constantinescu tol = tola+tolr; 64038a175baeSEmil Constantinescu if(tola>0.){ 64048a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 64058a175baeSEmil Constantinescu } 64068a175baeSEmil Constantinescu if(tolr>0.){ 64078a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 64088a175baeSEmil Constantinescu } 64098a175baeSEmil Constantinescu if(tol>0.){ 64108a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 64118a175baeSEmil Constantinescu } 64128a175baeSEmil Constantinescu } 64138a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 64148a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 64158a175baeSEmil Constantinescu 64168a175baeSEmil Constantinescu for (i=0; i<n; i++) { 64178a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 64188a175baeSEmil Constantinescu tola = ts->atol; 64198a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 64208a175baeSEmil Constantinescu tol = tola+tolr; 64218a175baeSEmil Constantinescu if(tola>0.){ 64228a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 64238a175baeSEmil Constantinescu } 64248a175baeSEmil Constantinescu if(tolr>0.){ 64258a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 64268a175baeSEmil Constantinescu } 64278a175baeSEmil Constantinescu if(tol>0.){ 64288a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 64298a175baeSEmil Constantinescu } 64308a175baeSEmil Constantinescu } 64318a175baeSEmil Constantinescu } 64328a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 64338a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 64348a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 64358a175baeSEmil Constantinescu err_loc[0] = max; 64368a175baeSEmil Constantinescu err_loc[1] = maxa; 64378a175baeSEmil Constantinescu err_loc[2] = maxr; 6438a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 64398a175baeSEmil Constantinescu gmax = err_glb[0]; 64408a175baeSEmil Constantinescu gmaxa = err_glb[1]; 64418a175baeSEmil Constantinescu gmaxr = err_glb[2]; 64428a175baeSEmil Constantinescu 64438a175baeSEmil Constantinescu *norm = gmax; 64448a175baeSEmil Constantinescu *norma = gmaxa; 64458a175baeSEmil Constantinescu *normr = gmaxr; 64468a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 64478a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 64488a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 64498a175baeSEmil Constantinescu PetscFunctionReturn(0); 64508a175baeSEmil Constantinescu } 64518a175baeSEmil Constantinescu 64528a175baeSEmil Constantinescu /*@ 64538a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 64548a175baeSEmil Constantinescu 64558a175baeSEmil Constantinescu Collective on TS 64568a175baeSEmil Constantinescu 64578a175baeSEmil Constantinescu Input Arguments: 64588a175baeSEmil Constantinescu + ts - time stepping context 64598a175baeSEmil Constantinescu . E - error vector 64608a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 64618a175baeSEmil Constantinescu . Y - state vector, previous time step 64628a175baeSEmil Constantinescu - wnormtype - norm type, either NORM_2 or NORM_INFINITY 64638a175baeSEmil Constantinescu 64648a175baeSEmil Constantinescu Output Arguments: 64658a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 64668a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 64678a175baeSEmil Constantinescu . normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 64688a175baeSEmil Constantinescu 64698a175baeSEmil Constantinescu Options Database Keys: 64708a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 64718a175baeSEmil Constantinescu 64728a175baeSEmil Constantinescu Level: developer 64738a175baeSEmil Constantinescu 64748a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 64758a175baeSEmil Constantinescu @*/ 64768a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 64778a175baeSEmil Constantinescu { 64788a175baeSEmil Constantinescu PetscErrorCode ierr; 64798a175baeSEmil Constantinescu 64808a175baeSEmil Constantinescu PetscFunctionBegin; 64818a175baeSEmil Constantinescu if (wnormtype == NORM_2) { 64828a175baeSEmil Constantinescu ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 64838a175baeSEmil Constantinescu } else if(wnormtype == NORM_INFINITY) { 64848a175baeSEmil Constantinescu ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 64858a175baeSEmil Constantinescu } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 64868a175baeSEmil Constantinescu PetscFunctionReturn(0); 64878a175baeSEmil Constantinescu } 64888a175baeSEmil Constantinescu 64898a175baeSEmil Constantinescu 64908d59e960SJed Brown /*@ 64918d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 64928d59e960SJed Brown 64938d59e960SJed Brown Logically Collective on TS 64948d59e960SJed Brown 64958d59e960SJed Brown Input Arguments: 64968d59e960SJed Brown + ts - time stepping context 64978d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 64988d59e960SJed Brown 64998d59e960SJed Brown Note: 65008d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 65018d59e960SJed Brown 65028d59e960SJed Brown Level: intermediate 65038d59e960SJed Brown 65048d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 65058d59e960SJed Brown @*/ 65068d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 65078d59e960SJed Brown { 65088d59e960SJed Brown PetscFunctionBegin; 65098d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 65108d59e960SJed Brown ts->cfltime_local = cfltime; 65118d59e960SJed Brown ts->cfltime = -1.; 65128d59e960SJed Brown PetscFunctionReturn(0); 65138d59e960SJed Brown } 65148d59e960SJed Brown 65158d59e960SJed Brown /*@ 65168d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 65178d59e960SJed Brown 65188d59e960SJed Brown Collective on TS 65198d59e960SJed Brown 65208d59e960SJed Brown Input Arguments: 65218d59e960SJed Brown . ts - time stepping context 65228d59e960SJed Brown 65238d59e960SJed Brown Output Arguments: 65248d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 65258d59e960SJed Brown 65268d59e960SJed Brown Level: advanced 65278d59e960SJed Brown 65288d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 65298d59e960SJed Brown @*/ 65308d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 65318d59e960SJed Brown { 65328d59e960SJed Brown PetscErrorCode ierr; 65338d59e960SJed Brown 65348d59e960SJed Brown PetscFunctionBegin; 65358d59e960SJed Brown if (ts->cfltime < 0) { 6536b2566f29SBarry Smith ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 65378d59e960SJed Brown } 65388d59e960SJed Brown *cfltime = ts->cfltime; 65398d59e960SJed Brown PetscFunctionReturn(0); 65408d59e960SJed Brown } 65418d59e960SJed Brown 6542d6ebe24aSShri Abhyankar /*@ 6543d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 6544d6ebe24aSShri Abhyankar 6545d6ebe24aSShri Abhyankar Input Parameters: 6546d6ebe24aSShri Abhyankar . ts - the TS context. 6547d6ebe24aSShri Abhyankar . xl - lower bound. 6548d6ebe24aSShri Abhyankar . xu - upper bound. 6549d6ebe24aSShri Abhyankar 6550d6ebe24aSShri Abhyankar Notes: 6551d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 6552e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 6553d6ebe24aSShri Abhyankar 65542bd2b0e6SSatish Balay Level: advanced 65552bd2b0e6SSatish Balay 6556d6ebe24aSShri Abhyankar @*/ 6557d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 6558d6ebe24aSShri Abhyankar { 6559d6ebe24aSShri Abhyankar PetscErrorCode ierr; 6560d6ebe24aSShri Abhyankar SNES snes; 6561d6ebe24aSShri Abhyankar 6562d6ebe24aSShri Abhyankar PetscFunctionBegin; 6563d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 6564d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 6565d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 6566d6ebe24aSShri Abhyankar } 6567d6ebe24aSShri Abhyankar 6568325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 6569c6db04a5SJed Brown #include <mex.h> 6570325fc9f4SBarry Smith 6571325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 6572325fc9f4SBarry Smith 6573325fc9f4SBarry Smith /* 6574325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 6575325fc9f4SBarry Smith TSSetFunctionMatlab(). 6576325fc9f4SBarry Smith 6577325fc9f4SBarry Smith Collective on TS 6578325fc9f4SBarry Smith 6579325fc9f4SBarry Smith Input Parameters: 6580325fc9f4SBarry Smith + snes - the TS context 65810910c330SBarry Smith - u - input vector 6582325fc9f4SBarry Smith 6583325fc9f4SBarry Smith Output Parameter: 6584325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 6585325fc9f4SBarry Smith 6586325fc9f4SBarry Smith Notes: 6587325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 6588325fc9f4SBarry Smith implementations, so most users would not generally call this routine 6589325fc9f4SBarry Smith themselves. 6590325fc9f4SBarry Smith 6591325fc9f4SBarry Smith Level: developer 6592325fc9f4SBarry Smith 6593325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6594325fc9f4SBarry Smith 6595325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6596325fc9f4SBarry Smith */ 65970910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 6598325fc9f4SBarry Smith { 6599325fc9f4SBarry Smith PetscErrorCode ierr; 6600325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6601325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 6602325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 6603325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 6604325fc9f4SBarry Smith 6605325fc9f4SBarry Smith PetscFunctionBegin; 6606325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 66070910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 66080910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 6609325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 66100910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 6611325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 6612325fc9f4SBarry Smith 6613325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 66140910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 66150910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 66160910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 6617bbd56ea5SKarl Rupp 6618325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6619325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 6620325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6621325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6622325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 6623325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 6624325fc9f4SBarry Smith prhs[6] = sctx->ctx; 6625325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 6626325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6627325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6628325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6629325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6630325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6631325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6632325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6633325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6634325fc9f4SBarry Smith PetscFunctionReturn(0); 6635325fc9f4SBarry Smith } 6636325fc9f4SBarry Smith 6637325fc9f4SBarry Smith 6638325fc9f4SBarry Smith /* 6639325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 6640325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 6641e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 6642325fc9f4SBarry Smith 6643325fc9f4SBarry Smith Logically Collective on TS 6644325fc9f4SBarry Smith 6645325fc9f4SBarry Smith Input Parameters: 6646325fc9f4SBarry Smith + ts - the TS context 6647325fc9f4SBarry Smith - func - function evaluation routine 6648325fc9f4SBarry Smith 6649325fc9f4SBarry Smith Calling sequence of func: 66500910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 6651325fc9f4SBarry Smith 6652325fc9f4SBarry Smith Level: beginner 6653325fc9f4SBarry Smith 6654325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6655325fc9f4SBarry Smith 6656325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6657325fc9f4SBarry Smith */ 6658cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 6659325fc9f4SBarry Smith { 6660325fc9f4SBarry Smith PetscErrorCode ierr; 6661325fc9f4SBarry Smith TSMatlabContext *sctx; 6662325fc9f4SBarry Smith 6663325fc9f4SBarry Smith PetscFunctionBegin; 6664325fc9f4SBarry Smith /* currently sctx is memory bleed */ 666595dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6666325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6667325fc9f4SBarry Smith /* 6668325fc9f4SBarry Smith This should work, but it doesn't 6669325fc9f4SBarry Smith sctx->ctx = ctx; 6670325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6671325fc9f4SBarry Smith */ 6672325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6673bbd56ea5SKarl Rupp 66740298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 6675325fc9f4SBarry Smith PetscFunctionReturn(0); 6676325fc9f4SBarry Smith } 6677325fc9f4SBarry Smith 6678325fc9f4SBarry Smith /* 6679325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 6680325fc9f4SBarry Smith TSSetJacobianMatlab(). 6681325fc9f4SBarry Smith 6682325fc9f4SBarry Smith Collective on TS 6683325fc9f4SBarry Smith 6684325fc9f4SBarry Smith Input Parameters: 6685cdcf91faSSean Farley + ts - the TS context 66860910c330SBarry Smith . u - input vector 6687325fc9f4SBarry Smith . A, B - the matrices 6688325fc9f4SBarry Smith - ctx - user context 6689325fc9f4SBarry Smith 6690325fc9f4SBarry Smith Level: developer 6691325fc9f4SBarry Smith 6692325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6693325fc9f4SBarry Smith 6694325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6695325fc9f4SBarry Smith @*/ 6696f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 6697325fc9f4SBarry Smith { 6698325fc9f4SBarry Smith PetscErrorCode ierr; 6699325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6700325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 6701325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 6702325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 6703325fc9f4SBarry Smith 6704325fc9f4SBarry Smith PetscFunctionBegin; 6705cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 67060910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 6707325fc9f4SBarry Smith 67080910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 6709325fc9f4SBarry Smith 6710cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 67110910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 67120910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 67130910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 67140910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 6715bbd56ea5SKarl Rupp 6716325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6717325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 6718325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6719325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6720325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 6721325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 6722325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 6723325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 6724325fc9f4SBarry Smith prhs[8] = sctx->ctx; 6725325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 6726325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6727325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6728325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6729325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6730325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6731325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6732325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6733325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 6734325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 6735325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6736325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 6737325fc9f4SBarry Smith PetscFunctionReturn(0); 6738325fc9f4SBarry Smith } 6739325fc9f4SBarry Smith 6740325fc9f4SBarry Smith 6741325fc9f4SBarry Smith /* 6742325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 6743e3c5b3baSBarry 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 6744325fc9f4SBarry Smith 6745325fc9f4SBarry Smith Logically Collective on TS 6746325fc9f4SBarry Smith 6747325fc9f4SBarry Smith Input Parameters: 6748cdcf91faSSean Farley + ts - the TS context 6749325fc9f4SBarry Smith . A,B - Jacobian matrices 6750325fc9f4SBarry Smith . func - function evaluation routine 6751325fc9f4SBarry Smith - ctx - user context 6752325fc9f4SBarry Smith 6753325fc9f4SBarry Smith Calling sequence of func: 67540910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 6755325fc9f4SBarry Smith 6756325fc9f4SBarry Smith 6757325fc9f4SBarry Smith Level: developer 6758325fc9f4SBarry Smith 6759325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6760325fc9f4SBarry Smith 6761325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6762325fc9f4SBarry Smith */ 6763cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 6764325fc9f4SBarry Smith { 6765325fc9f4SBarry Smith PetscErrorCode ierr; 6766325fc9f4SBarry Smith TSMatlabContext *sctx; 6767325fc9f4SBarry Smith 6768325fc9f4SBarry Smith PetscFunctionBegin; 6769325fc9f4SBarry Smith /* currently sctx is memory bleed */ 677095dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6771325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6772325fc9f4SBarry Smith /* 6773325fc9f4SBarry Smith This should work, but it doesn't 6774325fc9f4SBarry Smith sctx->ctx = ctx; 6775325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6776325fc9f4SBarry Smith */ 6777325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6778bbd56ea5SKarl Rupp 6779cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 6780325fc9f4SBarry Smith PetscFunctionReturn(0); 6781325fc9f4SBarry Smith } 6782325fc9f4SBarry Smith 6783b5b1a830SBarry Smith /* 6784b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 6785b5b1a830SBarry Smith 6786b5b1a830SBarry Smith Collective on TS 6787b5b1a830SBarry Smith 6788b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6789b5b1a830SBarry Smith @*/ 67900910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 6791b5b1a830SBarry Smith { 6792b5b1a830SBarry Smith PetscErrorCode ierr; 6793b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6794a530c242SBarry Smith int nlhs = 1,nrhs = 6; 6795b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 6796b5b1a830SBarry Smith long long int lx = 0,ls = 0; 6797b5b1a830SBarry Smith 6798b5b1a830SBarry Smith PetscFunctionBegin; 6799cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 68000910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 6801b5b1a830SBarry Smith 6802cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 68030910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 6804bbd56ea5SKarl Rupp 6805b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6806b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 6807b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 6808b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 6809b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 6810b5b1a830SBarry Smith prhs[5] = sctx->ctx; 6811b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 6812b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6813b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 6814b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 6815b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 6816b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 6817b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 6818b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 6819b5b1a830SBarry Smith PetscFunctionReturn(0); 6820b5b1a830SBarry Smith } 6821b5b1a830SBarry Smith 6822b5b1a830SBarry Smith 6823b5b1a830SBarry Smith /* 6824b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 6825b5b1a830SBarry Smith 6826b5b1a830SBarry Smith Level: developer 6827b5b1a830SBarry Smith 6828b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 6829b5b1a830SBarry Smith 6830b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6831b5b1a830SBarry Smith */ 6832cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 6833b5b1a830SBarry Smith { 6834b5b1a830SBarry Smith PetscErrorCode ierr; 6835b5b1a830SBarry Smith TSMatlabContext *sctx; 6836b5b1a830SBarry Smith 6837b5b1a830SBarry Smith PetscFunctionBegin; 6838b5b1a830SBarry Smith /* currently sctx is memory bleed */ 683995dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6840b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6841b5b1a830SBarry Smith /* 6842b5b1a830SBarry Smith This should work, but it doesn't 6843b5b1a830SBarry Smith sctx->ctx = ctx; 6844b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6845b5b1a830SBarry Smith */ 6846b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6847bbd56ea5SKarl Rupp 68480298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 6849b5b1a830SBarry Smith PetscFunctionReturn(0); 6850b5b1a830SBarry Smith } 6851325fc9f4SBarry Smith #endif 6852b3603a34SBarry Smith 6853b3603a34SBarry Smith /*@C 68544f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 6855b3603a34SBarry Smith in a time based line graph 6856b3603a34SBarry Smith 6857b3603a34SBarry Smith Collective on TS 6858b3603a34SBarry Smith 6859b3603a34SBarry Smith Input Parameters: 6860b3603a34SBarry Smith + ts - the TS context 6861b3603a34SBarry Smith . step - current time-step 6862b3603a34SBarry Smith . ptime - current time 68637db568b7SBarry Smith . u - current solution 68647db568b7SBarry Smith - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 6865b3603a34SBarry Smith 6866b3d3934dSBarry Smith Options Database: 68679ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 6868b3d3934dSBarry Smith 6869b3603a34SBarry Smith Level: intermediate 6870b3603a34SBarry Smith 68716934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component solutions in a separate window 6872b3603a34SBarry Smith 6873b3603a34SBarry Smith .keywords: TS, vector, monitor, view 6874b3603a34SBarry Smith 68757db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 68767db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 68777db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 68787db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 6879b3603a34SBarry Smith @*/ 68807db568b7SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6881b3603a34SBarry Smith { 6882b3603a34SBarry Smith PetscErrorCode ierr; 68837db568b7SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 6884b3603a34SBarry Smith const PetscScalar *yy; 688580666b62SBarry Smith Vec v; 6886b3603a34SBarry Smith 6887b3603a34SBarry Smith PetscFunctionBegin; 688863e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 688958ff32f7SBarry Smith if (!step) { 6890a9f9c1f6SBarry Smith PetscDrawAxis axis; 68916934998bSLisandro Dalcin PetscInt dim; 6892a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6893a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 6894bab0a581SBarry Smith if (!ctx->names) { 6895bab0a581SBarry Smith PetscBool flg; 6896bab0a581SBarry Smith /* user provides names of variables to plot but no names has been set so assume names are integer values */ 6897bab0a581SBarry Smith ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr); 6898bab0a581SBarry Smith if (flg) { 6899bab0a581SBarry Smith PetscInt i,n; 6900bab0a581SBarry Smith char **names; 6901bab0a581SBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 6902bab0a581SBarry Smith ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr); 6903bab0a581SBarry Smith for (i=0; i<n; i++) { 6904bab0a581SBarry Smith ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr); 6905bab0a581SBarry Smith ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr); 6906bab0a581SBarry Smith } 6907bab0a581SBarry Smith names[n] = NULL; 6908bab0a581SBarry Smith ctx->names = names; 6909bab0a581SBarry Smith } 6910bab0a581SBarry Smith } 6911387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 6912387f4636SBarry Smith char **displaynames; 6913387f4636SBarry Smith PetscBool flg; 6914387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 691595dccacaSBarry Smith ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr); 6916387f4636SBarry Smith ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 6917c5929fdfSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 6918387f4636SBarry Smith if (flg) { 6919a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 6920387f4636SBarry Smith } 6921387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 6922387f4636SBarry Smith } 6923387f4636SBarry Smith if (ctx->displaynames) { 6924387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 6925387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 6926387f4636SBarry Smith } else if (ctx->names) { 69270910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 69280b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6929387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 6930b0bc92c6SBarry Smith } else { 6931b0bc92c6SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6932b0bc92c6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6933387f4636SBarry Smith } 69340b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 693558ff32f7SBarry Smith } 69366934998bSLisandro Dalcin 69376934998bSLisandro Dalcin if (!ctx->transform) v = u; 69386934998bSLisandro Dalcin else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);} 693980666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 69406934998bSLisandro Dalcin if (ctx->displaynames) { 69416934998bSLisandro Dalcin PetscInt i; 69426934998bSLisandro Dalcin for (i=0; i<ctx->ndisplayvariables; i++) 69436934998bSLisandro Dalcin ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]); 69446934998bSLisandro Dalcin ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 69456934998bSLisandro Dalcin } else { 6946e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6947e3efe391SJed Brown PetscInt i,n; 69486934998bSLisandro Dalcin PetscReal *yreal; 694980666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 6950785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6951e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 69520b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6953e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6954e3efe391SJed Brown #else 69550b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6956e3efe391SJed Brown #endif 695780666b62SBarry Smith } 69586934998bSLisandro Dalcin ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 69596934998bSLisandro Dalcin if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);} 69606934998bSLisandro Dalcin 6961b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 69620b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 69636934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 69643923b477SBarry Smith } 6965b3603a34SBarry Smith PetscFunctionReturn(0); 6966b3603a34SBarry Smith } 6967b3603a34SBarry Smith 6968387f4636SBarry Smith 6969b037adc7SBarry Smith /*@C 697031152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6971b037adc7SBarry Smith 6972b037adc7SBarry Smith Collective on TS 6973b037adc7SBarry Smith 6974b037adc7SBarry Smith Input Parameters: 6975b037adc7SBarry Smith + ts - the TS context 6976b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 6977b037adc7SBarry Smith 6978b037adc7SBarry Smith Level: intermediate 6979b037adc7SBarry Smith 69807db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 69817db568b7SBarry Smith 6982b037adc7SBarry Smith .keywords: TS, vector, monitor, view 6983b037adc7SBarry Smith 6984a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 6985b037adc7SBarry Smith @*/ 698631152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 6987b037adc7SBarry Smith { 6988b037adc7SBarry Smith PetscErrorCode ierr; 6989b037adc7SBarry Smith PetscInt i; 6990b037adc7SBarry Smith 6991b037adc7SBarry Smith PetscFunctionBegin; 6992b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6993b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 69945537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 6995b3d3934dSBarry Smith break; 6996b3d3934dSBarry Smith } 6997b3d3934dSBarry Smith } 6998b3d3934dSBarry Smith PetscFunctionReturn(0); 6999b3d3934dSBarry Smith } 7000b3d3934dSBarry Smith 7001e673d494SBarry Smith /*@C 7002e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 7003e673d494SBarry Smith 7004e673d494SBarry Smith Collective on TS 7005e673d494SBarry Smith 7006e673d494SBarry Smith Input Parameters: 7007e673d494SBarry Smith + ts - the TS context 7008e673d494SBarry Smith - names - the names of the components, final string must be NULL 7009e673d494SBarry Smith 7010e673d494SBarry Smith Level: intermediate 7011e673d494SBarry Smith 7012e673d494SBarry Smith .keywords: TS, vector, monitor, view 7013e673d494SBarry Smith 7014a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 7015e673d494SBarry Smith @*/ 7016e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 7017e673d494SBarry Smith { 7018e673d494SBarry Smith PetscErrorCode ierr; 7019e673d494SBarry Smith 7020e673d494SBarry Smith PetscFunctionBegin; 7021e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 7022e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 7023e673d494SBarry Smith PetscFunctionReturn(0); 7024e673d494SBarry Smith } 7025e673d494SBarry Smith 7026b3d3934dSBarry Smith /*@C 7027b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 7028b3d3934dSBarry Smith 7029b3d3934dSBarry Smith Collective on TS 7030b3d3934dSBarry Smith 7031b3d3934dSBarry Smith Input Parameter: 7032b3d3934dSBarry Smith . ts - the TS context 7033b3d3934dSBarry Smith 7034b3d3934dSBarry Smith Output Parameter: 7035b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 7036b3d3934dSBarry Smith 7037b3d3934dSBarry Smith Level: intermediate 7038b3d3934dSBarry Smith 70397db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 70407db568b7SBarry Smith 7041b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7042b3d3934dSBarry Smith 7043b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 7044b3d3934dSBarry Smith @*/ 7045b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 7046b3d3934dSBarry Smith { 7047b3d3934dSBarry Smith PetscInt i; 7048b3d3934dSBarry Smith 7049b3d3934dSBarry Smith PetscFunctionBegin; 7050b3d3934dSBarry Smith *names = NULL; 7051b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7052b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 70535537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 7054b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 7055b3d3934dSBarry Smith break; 7056387f4636SBarry Smith } 7057387f4636SBarry Smith } 7058387f4636SBarry Smith PetscFunctionReturn(0); 7059387f4636SBarry Smith } 7060387f4636SBarry Smith 7061a66092f1SBarry Smith /*@C 7062a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 7063a66092f1SBarry Smith 7064a66092f1SBarry Smith Collective on TS 7065a66092f1SBarry Smith 7066a66092f1SBarry Smith Input Parameters: 7067a66092f1SBarry Smith + ctx - the TSMonitorLG context 7068a66092f1SBarry Smith . displaynames - the names of the components, final string must be NULL 7069a66092f1SBarry Smith 7070a66092f1SBarry Smith Level: intermediate 7071a66092f1SBarry Smith 7072a66092f1SBarry Smith .keywords: TS, vector, monitor, view 7073a66092f1SBarry Smith 7074a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 7075a66092f1SBarry Smith @*/ 7076a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 7077a66092f1SBarry Smith { 7078a66092f1SBarry Smith PetscInt j = 0,k; 7079a66092f1SBarry Smith PetscErrorCode ierr; 7080a66092f1SBarry Smith 7081a66092f1SBarry Smith PetscFunctionBegin; 7082a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 7083a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 7084a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 7085a66092f1SBarry Smith while (displaynames[j]) j++; 7086a66092f1SBarry Smith ctx->ndisplayvariables = j; 7087a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 7088a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 7089a66092f1SBarry Smith j = 0; 7090a66092f1SBarry Smith while (displaynames[j]) { 7091a66092f1SBarry Smith k = 0; 7092a66092f1SBarry Smith while (ctx->names[k]) { 7093a66092f1SBarry Smith PetscBool flg; 7094a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 7095a66092f1SBarry Smith if (flg) { 7096a66092f1SBarry Smith ctx->displayvariables[j] = k; 7097a66092f1SBarry Smith break; 7098a66092f1SBarry Smith } 7099a66092f1SBarry Smith k++; 7100a66092f1SBarry Smith } 7101a66092f1SBarry Smith j++; 7102a66092f1SBarry Smith } 7103a66092f1SBarry Smith PetscFunctionReturn(0); 7104a66092f1SBarry Smith } 7105a66092f1SBarry Smith 7106a66092f1SBarry Smith 7107387f4636SBarry Smith /*@C 7108387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 7109387f4636SBarry Smith 7110387f4636SBarry Smith Collective on TS 7111387f4636SBarry Smith 7112387f4636SBarry Smith Input Parameters: 7113387f4636SBarry Smith + ts - the TS context 7114387f4636SBarry Smith . displaynames - the names of the components, final string must be NULL 7115387f4636SBarry Smith 71167db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 71177db568b7SBarry Smith 7118387f4636SBarry Smith Level: intermediate 7119387f4636SBarry Smith 7120387f4636SBarry Smith .keywords: TS, vector, monitor, view 7121387f4636SBarry Smith 7122387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 7123387f4636SBarry Smith @*/ 7124387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 7125387f4636SBarry Smith { 7126a66092f1SBarry Smith PetscInt i; 7127387f4636SBarry Smith PetscErrorCode ierr; 7128387f4636SBarry Smith 7129387f4636SBarry Smith PetscFunctionBegin; 7130387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7131387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 71325537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 7133b3d3934dSBarry Smith break; 7134b037adc7SBarry Smith } 7135b037adc7SBarry Smith } 7136b037adc7SBarry Smith PetscFunctionReturn(0); 7137b037adc7SBarry Smith } 7138b037adc7SBarry Smith 713980666b62SBarry Smith /*@C 714080666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 714180666b62SBarry Smith 714280666b62SBarry Smith Collective on TS 714380666b62SBarry Smith 714480666b62SBarry Smith Input Parameters: 714580666b62SBarry Smith + ts - the TS context 714680666b62SBarry Smith . transform - the transform function 71477684fa3eSBarry Smith . destroy - function to destroy the optional context 714880666b62SBarry Smith - ctx - optional context used by transform function 714980666b62SBarry Smith 71507db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 71517db568b7SBarry Smith 715280666b62SBarry Smith Level: intermediate 715380666b62SBarry Smith 715480666b62SBarry Smith .keywords: TS, vector, monitor, view 715580666b62SBarry Smith 7156a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 715780666b62SBarry Smith @*/ 71587684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 715980666b62SBarry Smith { 716080666b62SBarry Smith PetscInt i; 7161a66092f1SBarry Smith PetscErrorCode ierr; 716280666b62SBarry Smith 716380666b62SBarry Smith PetscFunctionBegin; 716480666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 716580666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 71665537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 716780666b62SBarry Smith } 716880666b62SBarry Smith } 716980666b62SBarry Smith PetscFunctionReturn(0); 717080666b62SBarry Smith } 717180666b62SBarry Smith 7172e673d494SBarry Smith /*@C 7173e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 7174e673d494SBarry Smith 7175e673d494SBarry Smith Collective on TSLGCtx 7176e673d494SBarry Smith 7177e673d494SBarry Smith Input Parameters: 7178e673d494SBarry Smith + ts - the TS context 7179e673d494SBarry Smith . transform - the transform function 71807684fa3eSBarry Smith . destroy - function to destroy the optional context 7181e673d494SBarry Smith - ctx - optional context used by transform function 7182e673d494SBarry Smith 7183e673d494SBarry Smith Level: intermediate 7184e673d494SBarry Smith 7185e673d494SBarry Smith .keywords: TS, vector, monitor, view 7186e673d494SBarry Smith 7187a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 7188e673d494SBarry Smith @*/ 71897684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 7190e673d494SBarry Smith { 7191e673d494SBarry Smith PetscFunctionBegin; 7192e673d494SBarry Smith ctx->transform = transform; 71937684fa3eSBarry Smith ctx->transformdestroy = destroy; 7194e673d494SBarry Smith ctx->transformctx = tctx; 7195e673d494SBarry Smith PetscFunctionReturn(0); 7196e673d494SBarry Smith } 7197e673d494SBarry Smith 7198ef20d060SBarry Smith /*@C 71994f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 7200ef20d060SBarry Smith in a time based line graph 7201ef20d060SBarry Smith 7202ef20d060SBarry Smith Collective on TS 7203ef20d060SBarry Smith 7204ef20d060SBarry Smith Input Parameters: 7205ef20d060SBarry Smith + ts - the TS context 7206ef20d060SBarry Smith . step - current time-step 7207ef20d060SBarry Smith . ptime - current time 72087db568b7SBarry Smith . u - current solution 72097db568b7SBarry Smith - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 7210ef20d060SBarry Smith 7211ef20d060SBarry Smith Level: intermediate 7212ef20d060SBarry Smith 72136934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component errors in a separate window 7214abd5a294SJed Brown 7215abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 7216abd5a294SJed Brown 7217abd5a294SJed Brown Options Database Keys: 72184f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 7219ef20d060SBarry Smith 7220ef20d060SBarry Smith .keywords: TS, vector, monitor, view 7221ef20d060SBarry Smith 7222abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 7223ef20d060SBarry Smith @*/ 72240910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 7225ef20d060SBarry Smith { 7226ef20d060SBarry Smith PetscErrorCode ierr; 72270b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 7228ef20d060SBarry Smith const PetscScalar *yy; 7229ef20d060SBarry Smith Vec y; 7230ef20d060SBarry Smith 7231ef20d060SBarry Smith PetscFunctionBegin; 7232a9f9c1f6SBarry Smith if (!step) { 7233a9f9c1f6SBarry Smith PetscDrawAxis axis; 72346934998bSLisandro Dalcin PetscInt dim; 7235a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 72361ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 72370910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 7238a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 7239a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 7240a9f9c1f6SBarry Smith } 72410910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 7242ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 72430910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 7244ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 7245e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 7246e3efe391SJed Brown { 7247e3efe391SJed Brown PetscReal *yreal; 7248e3efe391SJed Brown PetscInt i,n; 7249e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 7250785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 7251e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 72520b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 7253e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 7254e3efe391SJed Brown } 7255e3efe391SJed Brown #else 72560b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 7257e3efe391SJed Brown #endif 7258ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 7259ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 7260b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 72610b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 72626934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 72633923b477SBarry Smith } 7264ef20d060SBarry Smith PetscFunctionReturn(0); 7265ef20d060SBarry Smith } 7266ef20d060SBarry Smith 7267201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 7268201da799SBarry Smith { 7269201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 7270201da799SBarry Smith PetscReal x = ptime,y; 7271201da799SBarry Smith PetscErrorCode ierr; 7272201da799SBarry Smith PetscInt its; 7273201da799SBarry Smith 7274201da799SBarry Smith PetscFunctionBegin; 727563e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 7276201da799SBarry Smith if (!n) { 7277201da799SBarry Smith PetscDrawAxis axis; 7278201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 7279201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 7280201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 7281201da799SBarry Smith ctx->snes_its = 0; 7282201da799SBarry Smith } 7283201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 7284201da799SBarry Smith y = its - ctx->snes_its; 7285201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 72863fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 7287201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 72886934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 7289201da799SBarry Smith } 7290201da799SBarry Smith ctx->snes_its = its; 7291201da799SBarry Smith PetscFunctionReturn(0); 7292201da799SBarry Smith } 7293201da799SBarry Smith 7294201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 7295201da799SBarry Smith { 7296201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 7297201da799SBarry Smith PetscReal x = ptime,y; 7298201da799SBarry Smith PetscErrorCode ierr; 7299201da799SBarry Smith PetscInt its; 7300201da799SBarry Smith 7301201da799SBarry Smith PetscFunctionBegin; 730263e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 7303201da799SBarry Smith if (!n) { 7304201da799SBarry Smith PetscDrawAxis axis; 7305201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 7306201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 7307201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 7308201da799SBarry Smith ctx->ksp_its = 0; 7309201da799SBarry Smith } 7310201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 7311201da799SBarry Smith y = its - ctx->ksp_its; 7312201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 731399fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 7314201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 73156934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 7316201da799SBarry Smith } 7317201da799SBarry Smith ctx->ksp_its = its; 7318201da799SBarry Smith PetscFunctionReturn(0); 7319201da799SBarry Smith } 7320f9c1d6abSBarry Smith 7321f9c1d6abSBarry Smith /*@ 7322f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 7323f9c1d6abSBarry Smith 7324f9c1d6abSBarry Smith Collective on TS and Vec 7325f9c1d6abSBarry Smith 7326f9c1d6abSBarry Smith Input Parameters: 7327f9c1d6abSBarry Smith + ts - the TS context 7328f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 7329f9c1d6abSBarry Smith 7330f9c1d6abSBarry Smith Output Parameters: 7331f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 7332f9c1d6abSBarry Smith 7333f9c1d6abSBarry Smith Level: developer 7334f9c1d6abSBarry Smith 7335f9c1d6abSBarry Smith .keywords: TS, compute 7336f9c1d6abSBarry Smith 7337f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 7338f9c1d6abSBarry Smith @*/ 7339f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 7340f9c1d6abSBarry Smith { 7341f9c1d6abSBarry Smith PetscErrorCode ierr; 7342f9c1d6abSBarry Smith 7343f9c1d6abSBarry Smith PetscFunctionBegin; 7344f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7345ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 7346f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 7347f9c1d6abSBarry Smith PetscFunctionReturn(0); 7348f9c1d6abSBarry Smith } 734924655328SShri 7350b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 7351b3d3934dSBarry Smith /*@C 7352b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 7353b3d3934dSBarry Smith 7354b3d3934dSBarry Smith Collective on TS 7355b3d3934dSBarry Smith 7356b3d3934dSBarry Smith Input Parameters: 7357b3d3934dSBarry Smith . ts - the ODE solver object 7358b3d3934dSBarry Smith 7359b3d3934dSBarry Smith Output Parameter: 7360b3d3934dSBarry Smith . ctx - the context 7361b3d3934dSBarry Smith 7362b3d3934dSBarry Smith Level: intermediate 7363b3d3934dSBarry Smith 7364b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso 7365b3d3934dSBarry Smith 7366b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 7367b3d3934dSBarry Smith 7368b3d3934dSBarry Smith @*/ 7369b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 7370b3d3934dSBarry Smith { 7371b3d3934dSBarry Smith PetscErrorCode ierr; 7372b3d3934dSBarry Smith 7373b3d3934dSBarry Smith PetscFunctionBegin; 7374a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 7375b3d3934dSBarry Smith PetscFunctionReturn(0); 7376b3d3934dSBarry Smith } 7377b3d3934dSBarry Smith 7378b3d3934dSBarry Smith /*@C 7379b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 7380b3d3934dSBarry Smith 7381b3d3934dSBarry Smith Collective on TS 7382b3d3934dSBarry Smith 7383b3d3934dSBarry Smith Input Parameters: 7384b3d3934dSBarry Smith + ts - the TS context 7385b3d3934dSBarry Smith . step - current time-step 7386b3d3934dSBarry Smith . ptime - current time 73877db568b7SBarry Smith . u - current solution 73887db568b7SBarry Smith - dctx - the envelope context 7389b3d3934dSBarry Smith 7390b3d3934dSBarry Smith Options Database: 7391b3d3934dSBarry Smith . -ts_monitor_envelope 7392b3d3934dSBarry Smith 7393b3d3934dSBarry Smith Level: intermediate 7394b3d3934dSBarry Smith 7395b3d3934dSBarry Smith Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 7396b3d3934dSBarry Smith 7397b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7398b3d3934dSBarry Smith 73997db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 7400b3d3934dSBarry Smith @*/ 74017db568b7SBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 7402b3d3934dSBarry Smith { 7403b3d3934dSBarry Smith PetscErrorCode ierr; 74047db568b7SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 7405b3d3934dSBarry Smith 7406b3d3934dSBarry Smith PetscFunctionBegin; 7407b3d3934dSBarry Smith if (!ctx->max) { 7408b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 7409b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 7410b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 7411b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 7412b3d3934dSBarry Smith } else { 7413b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 7414b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 7415b3d3934dSBarry Smith } 7416b3d3934dSBarry Smith PetscFunctionReturn(0); 7417b3d3934dSBarry Smith } 7418b3d3934dSBarry Smith 7419b3d3934dSBarry Smith 7420b3d3934dSBarry Smith /*@C 7421b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 7422b3d3934dSBarry Smith 7423b3d3934dSBarry Smith Collective on TS 7424b3d3934dSBarry Smith 7425b3d3934dSBarry Smith Input Parameter: 7426b3d3934dSBarry Smith . ts - the TS context 7427b3d3934dSBarry Smith 7428b3d3934dSBarry Smith Output Parameter: 7429b3d3934dSBarry Smith + max - the maximum values 7430b3d3934dSBarry Smith - min - the minimum values 7431b3d3934dSBarry Smith 74327db568b7SBarry Smith Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 74337db568b7SBarry Smith 7434b3d3934dSBarry Smith Level: intermediate 7435b3d3934dSBarry Smith 7436b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7437b3d3934dSBarry Smith 7438b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 7439b3d3934dSBarry Smith @*/ 7440b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 7441b3d3934dSBarry Smith { 7442b3d3934dSBarry Smith PetscInt i; 7443b3d3934dSBarry Smith 7444b3d3934dSBarry Smith PetscFunctionBegin; 7445b3d3934dSBarry Smith if (max) *max = NULL; 7446b3d3934dSBarry Smith if (min) *min = NULL; 7447b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7448b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 74495537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 7450b3d3934dSBarry Smith if (max) *max = ctx->max; 7451b3d3934dSBarry Smith if (min) *min = ctx->min; 7452b3d3934dSBarry Smith break; 7453b3d3934dSBarry Smith } 7454b3d3934dSBarry Smith } 7455b3d3934dSBarry Smith PetscFunctionReturn(0); 7456b3d3934dSBarry Smith } 7457b3d3934dSBarry Smith 7458b3d3934dSBarry Smith /*@C 7459b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 7460b3d3934dSBarry Smith 7461b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 7462b3d3934dSBarry Smith 7463b3d3934dSBarry Smith Input Parameter: 7464b3d3934dSBarry Smith . ctx - the monitor context 7465b3d3934dSBarry Smith 7466b3d3934dSBarry Smith Level: intermediate 7467b3d3934dSBarry Smith 7468b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy 7469b3d3934dSBarry Smith 74707db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 7471b3d3934dSBarry Smith @*/ 7472b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 7473b3d3934dSBarry Smith { 7474b3d3934dSBarry Smith PetscErrorCode ierr; 7475b3d3934dSBarry Smith 7476b3d3934dSBarry Smith PetscFunctionBegin; 7477b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 7478b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 7479b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 7480b3d3934dSBarry Smith PetscFunctionReturn(0); 7481b3d3934dSBarry Smith } 7482f2dee214SBarry Smith 748324655328SShri /*@ 748424655328SShri TSRollBack - Rolls back one time step 748524655328SShri 748624655328SShri Collective on TS 748724655328SShri 748824655328SShri Input Parameter: 748924655328SShri . ts - the TS context obtained from TSCreate() 749024655328SShri 749124655328SShri Level: advanced 749224655328SShri 749324655328SShri .keywords: TS, timestep, rollback 749424655328SShri 749524655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 749624655328SShri @*/ 749724655328SShri PetscErrorCode TSRollBack(TS ts) 749824655328SShri { 749924655328SShri PetscErrorCode ierr; 750024655328SShri 750124655328SShri PetscFunctionBegin; 750224655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7503b3de5cdeSLisandro Dalcin if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called"); 750424655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 750524655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 750624655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 750724655328SShri ts->ptime = ts->ptime_prev; 7508be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 7509be5899b3SLisandro Dalcin ts->steps--; ts->total_steps--; 751010b82f12SShri Abhyankar ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 7511b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 751224655328SShri PetscFunctionReturn(0); 751324655328SShri } 7514aeb4809dSShri Abhyankar 7515ff22ae23SHong Zhang /*@ 7516ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 7517ff22ae23SHong Zhang 7518ff22ae23SHong Zhang Input Parameter: 7519ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 7520ff22ae23SHong Zhang 7521ff22ae23SHong Zhang Level: advanced 7522ff22ae23SHong Zhang 7523ff22ae23SHong Zhang .keywords: TS, getstages 7524ff22ae23SHong Zhang 7525ff22ae23SHong Zhang .seealso: TSCreate() 7526ff22ae23SHong Zhang @*/ 7527ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns,Vec **Y) 7528ff22ae23SHong Zhang { 7529ff22ae23SHong Zhang PetscErrorCode ierr; 7530ff22ae23SHong Zhang 7531ff22ae23SHong Zhang PetscFunctionBegin; 7532ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7533ff22ae23SHong Zhang PetscValidPointer(ns,2); 7534ff22ae23SHong Zhang 7535ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 7536ff22ae23SHong Zhang else { 7537ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 7538ff22ae23SHong Zhang } 7539ff22ae23SHong Zhang PetscFunctionReturn(0); 7540ff22ae23SHong Zhang } 7541ff22ae23SHong Zhang 7542847ff0e1SMatthew G. Knepley /*@C 7543847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 7544847ff0e1SMatthew G. Knepley 7545847ff0e1SMatthew G. Knepley Collective on SNES 7546847ff0e1SMatthew G. Knepley 7547847ff0e1SMatthew G. Knepley Input Parameters: 7548847ff0e1SMatthew G. Knepley + ts - the TS context 7549847ff0e1SMatthew G. Knepley . t - current timestep 7550847ff0e1SMatthew G. Knepley . U - state vector 7551847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 7552847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 7553847ff0e1SMatthew G. Knepley - ctx - an optional user context 7554847ff0e1SMatthew G. Knepley 7555847ff0e1SMatthew G. Knepley Output Parameters: 7556847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 7557847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 7558847ff0e1SMatthew G. Knepley 7559847ff0e1SMatthew G. Knepley Level: intermediate 7560847ff0e1SMatthew G. Knepley 7561847ff0e1SMatthew G. Knepley Notes: 7562847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 7563847ff0e1SMatthew G. Knepley 7564847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 7565847ff0e1SMatthew G. Knepley 7566847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 7567847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 7568847ff0e1SMatthew G. Knepley 7569847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 7570847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 7571847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 7572847ff0e1SMatthew G. Knepley 7573847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse 7574847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 7575847ff0e1SMatthew G. Knepley @*/ 7576847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 7577847ff0e1SMatthew G. Knepley { 7578847ff0e1SMatthew G. Knepley SNES snes; 7579847ff0e1SMatthew G. Knepley MatFDColoring color; 7580847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 7581847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 7582847ff0e1SMatthew G. Knepley 7583847ff0e1SMatthew G. Knepley PetscFunctionBegin; 7584c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 7585847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 7586847ff0e1SMatthew G. Knepley if (!color) { 7587847ff0e1SMatthew G. Knepley DM dm; 7588847ff0e1SMatthew G. Knepley ISColoring iscoloring; 7589847ff0e1SMatthew G. Knepley 7590847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 7591847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 7592847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 7593847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 7594847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7595847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7596847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7597847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7598847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7599847ff0e1SMatthew G. Knepley } else { 7600847ff0e1SMatthew G. Knepley MatColoring mc; 7601847ff0e1SMatthew G. Knepley 7602847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 7603847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 7604847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 7605847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 7606847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 7607847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 7608847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7609847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7610847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7611847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7612847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7613847ff0e1SMatthew G. Knepley } 7614847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 7615847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 7616847ff0e1SMatthew G. Knepley } 7617847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 7618847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 7619847ff0e1SMatthew G. Knepley if (J != B) { 7620847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7621847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7622847ff0e1SMatthew G. Knepley } 7623847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 7624847ff0e1SMatthew G. Knepley } 762593b34091SDebojyoti Ghosh 7626cb9d8021SPierre Barbier de Reuille /*@ 7627cb9d8021SPierre Barbier de Reuille TSSetFunctionDomainError - Set the function testing if the current state vector is valid 7628cb9d8021SPierre Barbier de Reuille 7629cb9d8021SPierre Barbier de Reuille Input Parameters: 7630cb9d8021SPierre Barbier de Reuille ts - the TS context 7631cb9d8021SPierre Barbier de Reuille func - function called within TSFunctionDomainError 7632cb9d8021SPierre Barbier de Reuille 7633cb9d8021SPierre Barbier de Reuille Level: intermediate 7634cb9d8021SPierre Barbier de Reuille 7635cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain 7636cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError() 7637cb9d8021SPierre Barbier de Reuille @*/ 7638cb9d8021SPierre Barbier de Reuille 7639d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 7640cb9d8021SPierre Barbier de Reuille { 7641cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7642cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7643cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 7644cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7645cb9d8021SPierre Barbier de Reuille } 7646cb9d8021SPierre Barbier de Reuille 7647cb9d8021SPierre Barbier de Reuille /*@ 7648cb9d8021SPierre Barbier de Reuille TSFunctionDomainError - Check if the current state is valid 7649cb9d8021SPierre Barbier de Reuille 7650cb9d8021SPierre Barbier de Reuille Input Parameters: 7651cb9d8021SPierre Barbier de Reuille ts - the TS context 7652cb9d8021SPierre Barbier de Reuille stagetime - time of the simulation 7653d183316bSPierre Barbier de Reuille Y - state vector to check. 7654cb9d8021SPierre Barbier de Reuille 7655cb9d8021SPierre Barbier de Reuille Output Parameter: 7656cb9d8021SPierre Barbier de Reuille accept - Set to PETSC_FALSE if the current state vector is valid. 7657cb9d8021SPierre Barbier de Reuille 7658cb9d8021SPierre Barbier de Reuille Note: 7659cb9d8021SPierre Barbier de Reuille This function should be used to ensure the state is in a valid part of the space. 7660cb9d8021SPierre Barbier de Reuille For example, one can ensure here all values are positive. 766196a0c994SBarry Smith 766296a0c994SBarry Smith Level: advanced 7663cb9d8021SPierre Barbier de Reuille @*/ 7664d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 7665cb9d8021SPierre Barbier de Reuille { 7666cb9d8021SPierre Barbier de Reuille PetscErrorCode ierr; 7667cb9d8021SPierre Barbier de Reuille 7668cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7669cb9d8021SPierre Barbier de Reuille 7670cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7671cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 7672cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 7673d183316bSPierre Barbier de Reuille PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 7674cb9d8021SPierre Barbier de Reuille } 7675cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7676cb9d8021SPierre Barbier de Reuille } 76771ceb14c0SBarry Smith 767893b34091SDebojyoti Ghosh /*@C 7679e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 768093b34091SDebojyoti Ghosh 768193b34091SDebojyoti Ghosh Collective on MPI_Comm 768293b34091SDebojyoti Ghosh 768393b34091SDebojyoti Ghosh Input Parameter: 768493b34091SDebojyoti Ghosh . tsin - The input TS 768593b34091SDebojyoti Ghosh 768693b34091SDebojyoti Ghosh Output Parameter: 7687e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 768893b34091SDebojyoti Ghosh 76895eca1a21SEmil Constantinescu Notes: 76905eca1a21SEmil 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. 76915eca1a21SEmil Constantinescu 7692baa10174SEmil 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); 76935eca1a21SEmil Constantinescu 76945eca1a21SEmil Constantinescu Level: developer 769593b34091SDebojyoti Ghosh 7696e5168f73SEmil Constantinescu .keywords: TS, clone 7697e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 769893b34091SDebojyoti Ghosh @*/ 7699baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 770093b34091SDebojyoti Ghosh { 770193b34091SDebojyoti Ghosh TS t; 770293b34091SDebojyoti Ghosh PetscErrorCode ierr; 7703dc846ba4SSatish Balay SNES snes_start; 7704dc846ba4SSatish Balay DM dm; 7705dc846ba4SSatish Balay TSType type; 770693b34091SDebojyoti Ghosh 770793b34091SDebojyoti Ghosh PetscFunctionBegin; 770893b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 770993b34091SDebojyoti Ghosh *tsout = NULL; 771093b34091SDebojyoti Ghosh 77117a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 771293b34091SDebojyoti Ghosh 771393b34091SDebojyoti Ghosh /* General TS description */ 771493b34091SDebojyoti Ghosh t->numbermonitors = 0; 771593b34091SDebojyoti Ghosh t->setupcalled = 0; 771693b34091SDebojyoti Ghosh t->ksp_its = 0; 771793b34091SDebojyoti Ghosh t->snes_its = 0; 771893b34091SDebojyoti Ghosh t->nwork = 0; 771993b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 772093b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 772193b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 772293b34091SDebojyoti Ghosh 772334561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr); 772434561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr); 7725d15a3a53SEmil Constantinescu 772693b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr); 772793b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm);CHKERRQ(ierr); 772893b34091SDebojyoti Ghosh 772993b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 773051699248SLisandro Dalcin ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr); 773193b34091SDebojyoti Ghosh 7732e7069c78SShri t->trajectory = tsin->trajectory; 7733e7069c78SShri ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr); 7734e7069c78SShri 7735e7069c78SShri t->event = tsin->event; 77366b10a48eSSatish Balay if (t->event) t->event->refct++; 7737e7069c78SShri 773893b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 773993b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 7740e7069c78SShri t->ptime_prev = tsin->ptime_prev; 774193b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 774293b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 774393b34091SDebojyoti Ghosh t->steps = tsin->steps; 7744e7069c78SShri t->total_steps = tsin->total_steps; 774593b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 774693b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 774793b34091SDebojyoti Ghosh t->atol = tsin->atol; 774893b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 774993b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 775093b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 775193b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 775293b34091SDebojyoti Ghosh 775393b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type);CHKERRQ(ierr); 775493b34091SDebojyoti Ghosh ierr = TSSetType(t,type);CHKERRQ(ierr); 775593b34091SDebojyoti Ghosh 775693b34091SDebojyoti Ghosh t->vec_sol = NULL; 775793b34091SDebojyoti Ghosh 775893b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 775993b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 776093b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 776193b34091SDebojyoti Ghosh 776293b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 776393b34091SDebojyoti Ghosh 77640d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 77650d4fed19SBarry Smith PetscInt i; 77660d4fed19SBarry Smith ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr); 77670d4fed19SBarry Smith for (i=0; i<10; i++) { 77680d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 77690d4fed19SBarry Smith } 77700d4fed19SBarry Smith } 777193b34091SDebojyoti Ghosh *tsout = t; 777293b34091SDebojyoti Ghosh PetscFunctionReturn(0); 777393b34091SDebojyoti Ghosh } 7774