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 /*@ 44778fbdcc8SBarry Smith TSGetTrajectory - Gets the trajectory from a TS if it exists 44878fbdcc8SBarry Smith 44978fbdcc8SBarry Smith Collective on TS 45078fbdcc8SBarry Smith 45178fbdcc8SBarry Smith Input Parameters: 45278fbdcc8SBarry Smith . ts - the TS context obtained from TSCreate() 45378fbdcc8SBarry Smith 45478fbdcc8SBarry Smith Output Parameters; 45578fbdcc8SBarry Smith . tr - the TSTrajectory object, if it exists 45678fbdcc8SBarry Smith 45778fbdcc8SBarry Smith Note: This routine should be called after all TS options have been set 45878fbdcc8SBarry Smith 45978fbdcc8SBarry Smith Level: advanced 46078fbdcc8SBarry Smith 46178fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate() 46278fbdcc8SBarry Smith 46378fbdcc8SBarry Smith .keywords: TS, set, checkpoint, 46478fbdcc8SBarry Smith @*/ 46578fbdcc8SBarry Smith PetscErrorCode TSGetTrajectory(TS ts,TSTrajectory *tr) 46678fbdcc8SBarry Smith { 46778fbdcc8SBarry Smith PetscFunctionBegin; 46878fbdcc8SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 46978fbdcc8SBarry Smith *tr = ts->trajectory; 47078fbdcc8SBarry Smith PetscFunctionReturn(0); 47178fbdcc8SBarry Smith } 47278fbdcc8SBarry Smith 47378fbdcc8SBarry Smith /*@ 474bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 475d2daff3dSHong Zhang 476d2daff3dSHong Zhang Collective on TS 477d2daff3dSHong Zhang 478d2daff3dSHong Zhang Input Parameters: 479bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 480bc952696SBarry Smith 48178fbdcc8SBarry Smith Options Database: 48278fbdcc8SBarry Smith + -ts_save_trajectory - saves the trajectory to a file 48378fbdcc8SBarry Smith - -ts_trajectory_type type 48478fbdcc8SBarry Smith 48568bece0bSHong Zhang Note: This routine should be called after all TS options have been set 486d2daff3dSHong Zhang 48778fbdcc8SBarry Smith The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/bin/PetscBinaryIOTrajectory.py and 48878fbdcc8SBarry Smith MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m 48978fbdcc8SBarry Smith 490d2daff3dSHong Zhang Level: intermediate 491d2daff3dSHong Zhang 49278fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectoryType, TSSetTrajectoryType() 493d2daff3dSHong Zhang 494bc952696SBarry Smith .keywords: TS, set, checkpoint, 495d2daff3dSHong Zhang @*/ 496bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 497d2daff3dSHong Zhang { 498bc952696SBarry Smith PetscErrorCode ierr; 499bc952696SBarry Smith 500d2daff3dSHong Zhang PetscFunctionBegin; 501d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 502bc952696SBarry Smith if (!ts->trajectory) { 503bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 504972caf09SHong Zhang ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 505bc952696SBarry Smith } 506d2daff3dSHong Zhang PetscFunctionReturn(0); 507d2daff3dSHong Zhang } 508d2daff3dSHong Zhang 509a7a1495cSBarry Smith /*@ 510a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 511a7a1495cSBarry Smith set with TSSetRHSJacobian(). 512a7a1495cSBarry Smith 513a7a1495cSBarry Smith Collective on TS and Vec 514a7a1495cSBarry Smith 515a7a1495cSBarry Smith Input Parameters: 516316643e7SJed Brown + ts - the TS context 517a7a1495cSBarry Smith . t - current timestep 5180910c330SBarry Smith - U - input vector 519a7a1495cSBarry Smith 520a7a1495cSBarry Smith Output Parameters: 521a7a1495cSBarry Smith + A - Jacobian matrix 522a7a1495cSBarry Smith . B - optional preconditioning matrix 523a7a1495cSBarry Smith - flag - flag indicating matrix structure 524a7a1495cSBarry Smith 525a7a1495cSBarry Smith Notes: 526a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 527a7a1495cSBarry Smith is used internally within the nonlinear solvers. 528a7a1495cSBarry Smith 52994b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 530a7a1495cSBarry Smith flag parameter. 531a7a1495cSBarry Smith 532a7a1495cSBarry Smith Level: developer 533a7a1495cSBarry Smith 534a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 535a7a1495cSBarry Smith 53694b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 537a7a1495cSBarry Smith @*/ 538d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 539a7a1495cSBarry Smith { 540dfbe8321SBarry Smith PetscErrorCode ierr; 541270bf2e7SJed Brown PetscObjectState Ustate; 54224989b8cSPeter Brune DM dm; 543942e3340SBarry Smith DMTS tsdm; 54424989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 54524989b8cSPeter Brune void *ctx; 54624989b8cSPeter Brune TSIJacobian ijacobianfunc; 547b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 548a7a1495cSBarry Smith 549a7a1495cSBarry Smith PetscFunctionBegin; 5500700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5510910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5520910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 55324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 554942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 55524989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 5560298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 557b2df71adSDebojyoti Ghosh ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 55859e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 559b2df71adSDebojyoti Ghosh if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 5600e4ef248SJed Brown PetscFunctionReturn(0); 561f8ede8e7SJed Brown } 562d90be118SSean Farley 563ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 564d90be118SSean Farley 565e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 56694ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 56794ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 56894ab13aaSBarry Smith if (A != B) { 56994ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 57094ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 571e1244c69SJed Brown } 572e1244c69SJed Brown ts->rhsjacobian.shift = 0; 573e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 574e1244c69SJed Brown } 575e1244c69SJed Brown 57624989b8cSPeter Brune if (rhsjacobianfunc) { 5776cd88445SBarry Smith PetscBool missing; 57894ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 579a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 580d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 581a7a1495cSBarry Smith PetscStackPop; 58294ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 5836cd88445SBarry Smith if (A) { 5846cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 5856cd88445SBarry 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"); 5866cd88445SBarry Smith } 5876cd88445SBarry Smith if (B && B != A) { 5886cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 5896cd88445SBarry 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"); 5906cd88445SBarry Smith } 591ef66eb69SBarry Smith } else { 59294ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 59394ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 594ef66eb69SBarry Smith } 5950e4ef248SJed Brown ts->rhsjacobian.time = t; 5960910c330SBarry Smith ts->rhsjacobian.X = U; 59759e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 598a7a1495cSBarry Smith PetscFunctionReturn(0); 599a7a1495cSBarry Smith } 600a7a1495cSBarry Smith 601316643e7SJed Brown /*@ 602d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 603d763cef2SBarry Smith 604316643e7SJed Brown Collective on TS and Vec 605316643e7SJed Brown 606316643e7SJed Brown Input Parameters: 607316643e7SJed Brown + ts - the TS context 608316643e7SJed Brown . t - current time 6090910c330SBarry Smith - U - state vector 610316643e7SJed Brown 611316643e7SJed Brown Output Parameter: 612316643e7SJed Brown . y - right hand side 613316643e7SJed Brown 614316643e7SJed Brown Note: 615316643e7SJed Brown Most users should not need to explicitly call this routine, as it 616316643e7SJed Brown is used internally within the nonlinear solvers. 617316643e7SJed Brown 618316643e7SJed Brown Level: developer 619316643e7SJed Brown 620316643e7SJed Brown .keywords: TS, compute 621316643e7SJed Brown 622316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 623316643e7SJed Brown @*/ 6240910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 625d763cef2SBarry Smith { 626dfbe8321SBarry Smith PetscErrorCode ierr; 62724989b8cSPeter Brune TSRHSFunction rhsfunction; 62824989b8cSPeter Brune TSIFunction ifunction; 62924989b8cSPeter Brune void *ctx; 63024989b8cSPeter Brune DM dm; 63124989b8cSPeter Brune 632d763cef2SBarry Smith PetscFunctionBegin; 6330700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6340910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6350700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 63624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 63724989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 6380298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 639d763cef2SBarry Smith 640ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 641d763cef2SBarry Smith 6420910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 64324989b8cSPeter Brune if (rhsfunction) { 644d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 6450910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 646d763cef2SBarry Smith PetscStackPop; 647214bc6a2SJed Brown } else { 648214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 649b2cd27e8SJed Brown } 65044a41b28SSean Farley 6510910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 652d763cef2SBarry Smith PetscFunctionReturn(0); 653d763cef2SBarry Smith } 654d763cef2SBarry Smith 655ef20d060SBarry Smith /*@ 656ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 657ef20d060SBarry Smith 658ef20d060SBarry Smith Collective on TS and Vec 659ef20d060SBarry Smith 660ef20d060SBarry Smith Input Parameters: 661ef20d060SBarry Smith + ts - the TS context 662ef20d060SBarry Smith - t - current time 663ef20d060SBarry Smith 664ef20d060SBarry Smith Output Parameter: 6650910c330SBarry Smith . U - the solution 666ef20d060SBarry Smith 667ef20d060SBarry Smith Note: 668ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 669ef20d060SBarry Smith is used internally within the nonlinear solvers. 670ef20d060SBarry Smith 671ef20d060SBarry Smith Level: developer 672ef20d060SBarry Smith 673ef20d060SBarry Smith .keywords: TS, compute 674ef20d060SBarry Smith 675abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 676ef20d060SBarry Smith @*/ 6770910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 678ef20d060SBarry Smith { 679ef20d060SBarry Smith PetscErrorCode ierr; 680ef20d060SBarry Smith TSSolutionFunction solutionfunction; 681ef20d060SBarry Smith void *ctx; 682ef20d060SBarry Smith DM dm; 683ef20d060SBarry Smith 684ef20d060SBarry Smith PetscFunctionBegin; 685ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6860910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 687ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 688ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 689ef20d060SBarry Smith 690ef20d060SBarry Smith if (solutionfunction) { 6919b7cd975SBarry Smith PetscStackPush("TS user solution function"); 6920910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 693ef20d060SBarry Smith PetscStackPop; 694ef20d060SBarry Smith } 695ef20d060SBarry Smith PetscFunctionReturn(0); 696ef20d060SBarry Smith } 6979b7cd975SBarry Smith /*@ 6989b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6999b7cd975SBarry Smith 7009b7cd975SBarry Smith Collective on TS and Vec 7019b7cd975SBarry Smith 7029b7cd975SBarry Smith Input Parameters: 7039b7cd975SBarry Smith + ts - the TS context 7049b7cd975SBarry Smith - t - current time 7059b7cd975SBarry Smith 7069b7cd975SBarry Smith Output Parameter: 7079b7cd975SBarry Smith . U - the function value 7089b7cd975SBarry Smith 7099b7cd975SBarry Smith Note: 7109b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 7119b7cd975SBarry Smith is used internally within the nonlinear solvers. 7129b7cd975SBarry Smith 7139b7cd975SBarry Smith Level: developer 7149b7cd975SBarry Smith 7159b7cd975SBarry Smith .keywords: TS, compute 7169b7cd975SBarry Smith 7179b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 7189b7cd975SBarry Smith @*/ 7199b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 7209b7cd975SBarry Smith { 7219b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 7229b7cd975SBarry Smith void *ctx; 7239b7cd975SBarry Smith DM dm; 7249b7cd975SBarry Smith 7259b7cd975SBarry Smith PetscFunctionBegin; 7269b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7279b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7289b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 7299b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 7309b7cd975SBarry Smith 7319b7cd975SBarry Smith if (forcing) { 7329b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 7339b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 7349b7cd975SBarry Smith PetscStackPop; 7359b7cd975SBarry Smith } 7369b7cd975SBarry Smith PetscFunctionReturn(0); 7379b7cd975SBarry Smith } 738ef20d060SBarry Smith 739214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 740214bc6a2SJed Brown { 7412dd45cf8SJed Brown Vec F; 742214bc6a2SJed Brown PetscErrorCode ierr; 743214bc6a2SJed Brown 744214bc6a2SJed Brown PetscFunctionBegin; 7450298fd71SBarry Smith *Frhs = NULL; 7460298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 747214bc6a2SJed Brown if (!ts->Frhs) { 7482dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 749214bc6a2SJed Brown } 750214bc6a2SJed Brown *Frhs = ts->Frhs; 751214bc6a2SJed Brown PetscFunctionReturn(0); 752214bc6a2SJed Brown } 753214bc6a2SJed Brown 754214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 755214bc6a2SJed Brown { 756214bc6a2SJed Brown Mat A,B; 7572dd45cf8SJed Brown PetscErrorCode ierr; 758*ecb5c246SBarry Smith TSIJacobian ijacobian; 759214bc6a2SJed Brown 760214bc6a2SJed Brown PetscFunctionBegin; 761c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 762c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 763*ecb5c246SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr); 764214bc6a2SJed Brown if (Arhs) { 765214bc6a2SJed Brown if (!ts->Arhs) { 766*ecb5c246SBarry Smith if (ijacobian) { 767214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 768*ecb5c246SBarry Smith } else { 769*ecb5c246SBarry Smith ts->Arhs = A; 770*ecb5c246SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 771*ecb5c246SBarry Smith } 772214bc6a2SJed Brown } 773214bc6a2SJed Brown *Arhs = ts->Arhs; 774214bc6a2SJed Brown } 775214bc6a2SJed Brown if (Brhs) { 776214bc6a2SJed Brown if (!ts->Brhs) { 777bdb70873SJed Brown if (A != B) { 778*ecb5c246SBarry Smith if (ijacobian) { 779214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 780bdb70873SJed Brown } else { 781*ecb5c246SBarry Smith ts->Brhs = B; 782*ecb5c246SBarry Smith ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 783*ecb5c246SBarry Smith } 784*ecb5c246SBarry Smith } else { 785bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 78651699248SLisandro Dalcin ts->Brhs = ts->Arhs; 787bdb70873SJed Brown } 788214bc6a2SJed Brown } 789214bc6a2SJed Brown *Brhs = ts->Brhs; 790214bc6a2SJed Brown } 791214bc6a2SJed Brown PetscFunctionReturn(0); 792214bc6a2SJed Brown } 793214bc6a2SJed Brown 794316643e7SJed Brown /*@ 7950910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 796316643e7SJed Brown 797316643e7SJed Brown Collective on TS and Vec 798316643e7SJed Brown 799316643e7SJed Brown Input Parameters: 800316643e7SJed Brown + ts - the TS context 801316643e7SJed Brown . t - current time 8020910c330SBarry Smith . U - state vector 8030910c330SBarry Smith . Udot - time derivative of state vector 804214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 805316643e7SJed Brown 806316643e7SJed Brown Output Parameter: 807316643e7SJed Brown . Y - right hand side 808316643e7SJed Brown 809316643e7SJed Brown Note: 810316643e7SJed Brown Most users should not need to explicitly call this routine, as it 811316643e7SJed Brown is used internally within the nonlinear solvers. 812316643e7SJed Brown 813316643e7SJed Brown If the user did did not write their equations in implicit form, this 814316643e7SJed Brown function recasts them in implicit form. 815316643e7SJed Brown 816316643e7SJed Brown Level: developer 817316643e7SJed Brown 818316643e7SJed Brown .keywords: TS, compute 819316643e7SJed Brown 820316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 821316643e7SJed Brown @*/ 8220910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 823316643e7SJed Brown { 824316643e7SJed Brown PetscErrorCode ierr; 82524989b8cSPeter Brune TSIFunction ifunction; 82624989b8cSPeter Brune TSRHSFunction rhsfunction; 82724989b8cSPeter Brune void *ctx; 82824989b8cSPeter Brune DM dm; 829316643e7SJed Brown 830316643e7SJed Brown PetscFunctionBegin; 8310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8320910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8330910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 8340700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 835316643e7SJed Brown 83624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 83724989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 8380298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 83924989b8cSPeter Brune 840ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 841d90be118SSean Farley 8420910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 84324989b8cSPeter Brune if (ifunction) { 844316643e7SJed Brown PetscStackPush("TS user implicit function"); 8450910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 846316643e7SJed Brown PetscStackPop; 847214bc6a2SJed Brown } 848214bc6a2SJed Brown if (imex) { 84924989b8cSPeter Brune if (!ifunction) { 8500910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 8512dd45cf8SJed Brown } 85224989b8cSPeter Brune } else if (rhsfunction) { 85324989b8cSPeter Brune if (ifunction) { 854214bc6a2SJed Brown Vec Frhs; 855214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 8560910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 857214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 8582dd45cf8SJed Brown } else { 8590910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 8600910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 861316643e7SJed Brown } 8624a6899ffSJed Brown } 8630910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 864316643e7SJed Brown PetscFunctionReturn(0); 865316643e7SJed Brown } 866316643e7SJed Brown 867316643e7SJed Brown /*@ 868316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 869316643e7SJed Brown 870316643e7SJed Brown Collective on TS and Vec 871316643e7SJed Brown 872316643e7SJed Brown Input 873316643e7SJed Brown Input Parameters: 874316643e7SJed Brown + ts - the TS context 875316643e7SJed Brown . t - current timestep 8760910c330SBarry Smith . U - state vector 8770910c330SBarry Smith . Udot - time derivative of state vector 878214bc6a2SJed Brown . shift - shift to apply, see note below 879214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 880316643e7SJed Brown 881316643e7SJed Brown Output Parameters: 882316643e7SJed Brown + A - Jacobian matrix 883316643e7SJed Brown . B - optional preconditioning matrix 884316643e7SJed Brown - flag - flag indicating matrix structure 885316643e7SJed Brown 886316643e7SJed Brown Notes: 8870910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 888316643e7SJed Brown 8890910c330SBarry Smith dF/dU + shift*dF/dUdot 890316643e7SJed Brown 891316643e7SJed Brown Most users should not need to explicitly call this routine, as it 892316643e7SJed Brown is used internally within the nonlinear solvers. 893316643e7SJed Brown 894316643e7SJed Brown Level: developer 895316643e7SJed Brown 896316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 897316643e7SJed Brown 898316643e7SJed Brown .seealso: TSSetIJacobian() 89963495f91SJed Brown @*/ 900d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 901316643e7SJed Brown { 902316643e7SJed Brown PetscErrorCode ierr; 90324989b8cSPeter Brune TSIJacobian ijacobian; 90424989b8cSPeter Brune TSRHSJacobian rhsjacobian; 90524989b8cSPeter Brune DM dm; 90624989b8cSPeter Brune void *ctx; 907316643e7SJed Brown 908316643e7SJed Brown PetscFunctionBegin; 9090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9100910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 9110910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 912316643e7SJed Brown PetscValidPointer(A,6); 91394ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 914316643e7SJed Brown PetscValidPointer(B,7); 91594ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 91624989b8cSPeter Brune 91724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 91824989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 9190298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 92024989b8cSPeter Brune 921ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 922316643e7SJed Brown 92394ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 92424989b8cSPeter Brune if (ijacobian) { 9256cd88445SBarry Smith PetscBool missing; 926316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 927d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 928316643e7SJed Brown PetscStackPop; 9296cd88445SBarry Smith if (A) { 9306cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 9316cd88445SBarry 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"); 9326cd88445SBarry Smith } 9336cd88445SBarry Smith if (B && B != A) { 9346cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 9356cd88445SBarry 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"); 9366cd88445SBarry Smith } 9374a6899ffSJed Brown } 938214bc6a2SJed Brown if (imex) { 939b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 9404c26be97Sstefano_zampini PetscBool assembled; 94194ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 9424c26be97Sstefano_zampini ierr = MatAssembled(A,&assembled);CHKERRQ(ierr); 9434c26be97Sstefano_zampini if (!assembled) { 9444c26be97Sstefano_zampini ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9454c26be97Sstefano_zampini ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9464c26be97Sstefano_zampini } 94794ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 94894ab13aaSBarry Smith if (A != B) { 94994ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 9504c26be97Sstefano_zampini ierr = MatAssembled(B,&assembled);CHKERRQ(ierr); 9514c26be97Sstefano_zampini if (!assembled) { 9524c26be97Sstefano_zampini ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9534c26be97Sstefano_zampini ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9544c26be97Sstefano_zampini } 95594ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 956214bc6a2SJed Brown } 957214bc6a2SJed Brown } 958214bc6a2SJed Brown } else { 959e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 960e1244c69SJed Brown if (rhsjacobian) { 961214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 962d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 963e1244c69SJed Brown } 96494ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 965e1244c69SJed Brown ts->rhsjacobian.scale = -1; 966e1244c69SJed Brown ts->rhsjacobian.shift = shift; 96794ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 96894ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 96994ab13aaSBarry Smith if (A != B) { 97094ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 97194ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 972316643e7SJed Brown } 973e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 974e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 975e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 97694ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 97794ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 97894ab13aaSBarry Smith if (A != B) { 97994ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 98094ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 981214bc6a2SJed Brown } 982316643e7SJed Brown } 98394ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 98494ab13aaSBarry Smith if (A != B) { 98594ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 986316643e7SJed Brown } 987316643e7SJed Brown } 988316643e7SJed Brown } 98994ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 990316643e7SJed Brown PetscFunctionReturn(0); 991316643e7SJed Brown } 992316643e7SJed Brown 993d763cef2SBarry Smith /*@C 994d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 995b5abc632SBarry Smith where U_t = G(t,u). 996d763cef2SBarry Smith 9973f9fe445SBarry Smith Logically Collective on TS 998d763cef2SBarry Smith 999d763cef2SBarry Smith Input Parameters: 1000d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 10010298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 1002d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 1003d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 10040298fd71SBarry Smith function evaluation routine (may be NULL) 1005d763cef2SBarry Smith 1006d763cef2SBarry Smith Calling sequence of func: 100787828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 1008d763cef2SBarry Smith 1009d763cef2SBarry Smith + t - current timestep 1010d763cef2SBarry Smith . u - input vector 1011d763cef2SBarry Smith . F - function vector 1012d763cef2SBarry Smith - ctx - [optional] user-defined function context 1013d763cef2SBarry Smith 1014d763cef2SBarry Smith Level: beginner 1015d763cef2SBarry Smith 10162bbac0d3SBarry Smith Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 10172bbac0d3SBarry Smith 1018d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1019d763cef2SBarry Smith 1020ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 1021d763cef2SBarry Smith @*/ 1022089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 1023d763cef2SBarry Smith { 1024089b2837SJed Brown PetscErrorCode ierr; 1025089b2837SJed Brown SNES snes; 10260298fd71SBarry Smith Vec ralloc = NULL; 102724989b8cSPeter Brune DM dm; 1028d763cef2SBarry Smith 1029089b2837SJed Brown PetscFunctionBegin; 10300700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1031ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 103224989b8cSPeter Brune 103324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 103424989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 1035089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1036e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 1037e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 1038e856ceecSJed Brown r = ralloc; 1039e856ceecSJed Brown } 1040089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 1041e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1042d763cef2SBarry Smith PetscFunctionReturn(0); 1043d763cef2SBarry Smith } 1044d763cef2SBarry Smith 1045ef20d060SBarry Smith /*@C 1046abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1047ef20d060SBarry Smith 1048ef20d060SBarry Smith Logically Collective on TS 1049ef20d060SBarry Smith 1050ef20d060SBarry Smith Input Parameters: 1051ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1052ef20d060SBarry Smith . f - routine for evaluating the solution 1053ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 10540298fd71SBarry Smith function evaluation routine (may be NULL) 1055ef20d060SBarry Smith 1056ef20d060SBarry Smith Calling sequence of func: 1057ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 1058ef20d060SBarry Smith 1059ef20d060SBarry Smith + t - current timestep 1060ef20d060SBarry Smith . u - output vector 1061ef20d060SBarry Smith - ctx - [optional] user-defined function context 1062ef20d060SBarry Smith 1063abd5a294SJed Brown Notes: 1064abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1065abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1066abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1067abd5a294SJed Brown 10684f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1069abd5a294SJed Brown 1070ef20d060SBarry Smith Level: beginner 1071ef20d060SBarry Smith 1072ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1073ef20d060SBarry Smith 10749b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 1075ef20d060SBarry Smith @*/ 1076ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1077ef20d060SBarry Smith { 1078ef20d060SBarry Smith PetscErrorCode ierr; 1079ef20d060SBarry Smith DM dm; 1080ef20d060SBarry Smith 1081ef20d060SBarry Smith PetscFunctionBegin; 1082ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1083ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1084ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 1085ef20d060SBarry Smith PetscFunctionReturn(0); 1086ef20d060SBarry Smith } 1087ef20d060SBarry Smith 10889b7cd975SBarry Smith /*@C 10899b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 10909b7cd975SBarry Smith 10919b7cd975SBarry Smith Logically Collective on TS 10929b7cd975SBarry Smith 10939b7cd975SBarry Smith Input Parameters: 10949b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 1095e162b725SBarry Smith . func - routine for evaluating the forcing function 10969b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 10970298fd71SBarry Smith function evaluation routine (may be NULL) 10989b7cd975SBarry Smith 10999b7cd975SBarry Smith Calling sequence of func: 1100e162b725SBarry Smith $ func (TS ts,PetscReal t,Vec f,void *ctx); 11019b7cd975SBarry Smith 11029b7cd975SBarry Smith + t - current timestep 1103e162b725SBarry Smith . f - output vector 11049b7cd975SBarry Smith - ctx - [optional] user-defined function context 11059b7cd975SBarry Smith 11069b7cd975SBarry Smith Notes: 11079b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1108e162b725SBarry 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 1109e162b725SBarry Smith definition of the problem you are solving and hence possibly introducing bugs. 1110e162b725SBarry Smith 1111e162b725SBarry Smith This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0 1112e162b725SBarry Smith 1113e162b725SBarry 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 1114e162b725SBarry Smith parameters can be passed in the ctx variable. 11159b7cd975SBarry Smith 11169b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 11179b7cd975SBarry Smith 11189b7cd975SBarry Smith Level: beginner 11199b7cd975SBarry Smith 11209b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 11219b7cd975SBarry Smith 11229b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 11239b7cd975SBarry Smith @*/ 1124e162b725SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx) 11259b7cd975SBarry Smith { 11269b7cd975SBarry Smith PetscErrorCode ierr; 11279b7cd975SBarry Smith DM dm; 11289b7cd975SBarry Smith 11299b7cd975SBarry Smith PetscFunctionBegin; 11309b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11319b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1132e162b725SBarry Smith ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr); 11339b7cd975SBarry Smith PetscFunctionReturn(0); 11349b7cd975SBarry Smith } 11359b7cd975SBarry Smith 1136d763cef2SBarry Smith /*@C 1137f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1138b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1139d763cef2SBarry Smith 11403f9fe445SBarry Smith Logically Collective on TS 1141d763cef2SBarry Smith 1142d763cef2SBarry Smith Input Parameters: 1143d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1144e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1145e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1146d763cef2SBarry Smith . f - the Jacobian evaluation routine 1147d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 11480298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1149d763cef2SBarry Smith 1150f7ab8db6SBarry Smith Calling sequence of f: 1151ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1152d763cef2SBarry Smith 1153d763cef2SBarry Smith + t - current timestep 1154d763cef2SBarry Smith . u - input vector 1155e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1156e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1157d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1158d763cef2SBarry Smith 11596cd88445SBarry Smith Notes: 11606cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 11616cd88445SBarry Smith 11626cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1163ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1164d763cef2SBarry Smith 1165d763cef2SBarry Smith Level: beginner 1166d763cef2SBarry Smith 1167d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1168d763cef2SBarry Smith 1169ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1170d763cef2SBarry Smith 1171d763cef2SBarry Smith @*/ 1172e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1173d763cef2SBarry Smith { 1174277b19d0SLisandro Dalcin PetscErrorCode ierr; 1175089b2837SJed Brown SNES snes; 117624989b8cSPeter Brune DM dm; 117724989b8cSPeter Brune TSIJacobian ijacobian; 1178277b19d0SLisandro Dalcin 1179d763cef2SBarry Smith PetscFunctionBegin; 11800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1181e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1182e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1183e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1184e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1185d763cef2SBarry Smith 118624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 118724989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1188e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1189e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1190e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1191e1244c69SJed Brown } 11920298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1193089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11945f659677SPeter Brune if (!ijacobian) { 1195e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 11960e4ef248SJed Brown } 1197e5d3d808SBarry Smith if (Amat) { 1198e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 11990e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1200e5d3d808SBarry Smith ts->Arhs = Amat; 12010e4ef248SJed Brown } 1202e5d3d808SBarry Smith if (Pmat) { 1203e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 12040e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1205e5d3d808SBarry Smith ts->Brhs = Pmat; 12060e4ef248SJed Brown } 1207d763cef2SBarry Smith PetscFunctionReturn(0); 1208d763cef2SBarry Smith } 1209d763cef2SBarry Smith 1210316643e7SJed Brown 1211316643e7SJed Brown /*@C 1212b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1213316643e7SJed Brown 12143f9fe445SBarry Smith Logically Collective on TS 1215316643e7SJed Brown 1216316643e7SJed Brown Input Parameters: 1217316643e7SJed Brown + ts - the TS context obtained from TSCreate() 12180298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1219316643e7SJed Brown . f - the function evaluation routine 12200298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1221316643e7SJed Brown 1222316643e7SJed Brown Calling sequence of f: 1223316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1224316643e7SJed Brown 1225316643e7SJed Brown + t - time at step/stage being solved 1226316643e7SJed Brown . u - state vector 1227316643e7SJed Brown . u_t - time derivative of state vector 1228316643e7SJed Brown . F - function vector 1229316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1230316643e7SJed Brown 1231316643e7SJed Brown Important: 12322bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1233316643e7SJed Brown 1234316643e7SJed Brown Level: beginner 1235316643e7SJed Brown 1236316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1237316643e7SJed Brown 1238d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1239316643e7SJed Brown @*/ 124051699248SLisandro Dalcin PetscErrorCode TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx) 1241316643e7SJed Brown { 1242089b2837SJed Brown PetscErrorCode ierr; 1243089b2837SJed Brown SNES snes; 124451699248SLisandro Dalcin Vec ralloc = NULL; 124524989b8cSPeter Brune DM dm; 1246316643e7SJed Brown 1247316643e7SJed Brown PetscFunctionBegin; 12480700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 124951699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 125024989b8cSPeter Brune 125124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 125224989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 125324989b8cSPeter Brune 1254089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 125551699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 125651699248SLisandro Dalcin ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 125751699248SLisandro Dalcin r = ralloc; 1258e856ceecSJed Brown } 125951699248SLisandro Dalcin ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 126051699248SLisandro Dalcin ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1261089b2837SJed Brown PetscFunctionReturn(0); 1262089b2837SJed Brown } 1263089b2837SJed Brown 1264089b2837SJed Brown /*@C 1265089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1266089b2837SJed Brown 1267089b2837SJed Brown Not Collective 1268089b2837SJed Brown 1269089b2837SJed Brown Input Parameter: 1270089b2837SJed Brown . ts - the TS context 1271089b2837SJed Brown 1272089b2837SJed Brown Output Parameter: 12730298fd71SBarry Smith + r - vector to hold residual (or NULL) 12740298fd71SBarry Smith . func - the function to compute residual (or NULL) 12750298fd71SBarry Smith - ctx - the function context (or NULL) 1276089b2837SJed Brown 1277089b2837SJed Brown Level: advanced 1278089b2837SJed Brown 1279089b2837SJed Brown .keywords: TS, nonlinear, get, function 1280089b2837SJed Brown 1281089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1282089b2837SJed Brown @*/ 1283089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1284089b2837SJed Brown { 1285089b2837SJed Brown PetscErrorCode ierr; 1286089b2837SJed Brown SNES snes; 128724989b8cSPeter Brune DM dm; 1288089b2837SJed Brown 1289089b2837SJed Brown PetscFunctionBegin; 1290089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1291089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12920298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 129324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 129424989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1295089b2837SJed Brown PetscFunctionReturn(0); 1296089b2837SJed Brown } 1297089b2837SJed Brown 1298089b2837SJed Brown /*@C 1299089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1300089b2837SJed Brown 1301089b2837SJed Brown Not Collective 1302089b2837SJed Brown 1303089b2837SJed Brown Input Parameter: 1304089b2837SJed Brown . ts - the TS context 1305089b2837SJed Brown 1306089b2837SJed Brown Output Parameter: 13070298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 13080298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 13090298fd71SBarry Smith - ctx - the function context (or NULL) 1310089b2837SJed Brown 1311089b2837SJed Brown Level: advanced 1312089b2837SJed Brown 1313089b2837SJed Brown .keywords: TS, nonlinear, get, function 1314089b2837SJed Brown 13152bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1316089b2837SJed Brown @*/ 1317089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1318089b2837SJed Brown { 1319089b2837SJed Brown PetscErrorCode ierr; 1320089b2837SJed Brown SNES snes; 132124989b8cSPeter Brune DM dm; 1322089b2837SJed Brown 1323089b2837SJed Brown PetscFunctionBegin; 1324089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1325089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13260298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 132724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 132824989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1329316643e7SJed Brown PetscFunctionReturn(0); 1330316643e7SJed Brown } 1331316643e7SJed Brown 1332316643e7SJed Brown /*@C 1333a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1334ae8867d6SBarry Smith provided with TSSetIFunction(). 1335316643e7SJed Brown 13363f9fe445SBarry Smith Logically Collective on TS 1337316643e7SJed Brown 1338316643e7SJed Brown Input Parameters: 1339316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1340e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1341e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1342316643e7SJed Brown . f - the Jacobian evaluation routine 13430298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1344316643e7SJed Brown 1345316643e7SJed Brown Calling sequence of f: 1346ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1347316643e7SJed Brown 1348316643e7SJed Brown + t - time at step/stage being solved 13491b4a444bSJed Brown . U - state vector 13501b4a444bSJed Brown . U_t - time derivative of state vector 1351316643e7SJed Brown . a - shift 1352e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1353e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1354316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1355316643e7SJed Brown 1356316643e7SJed Brown Notes: 1357e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1358316643e7SJed Brown 1359895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1360895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1361895c21f2SBarry Smith 1362a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1363b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1364a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1365a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1366a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1367a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1368a4f0a591SBarry Smith 13696cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13706cd88445SBarry Smith 13716cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1372ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1373ca5f011dSBarry Smith 1374316643e7SJed Brown Level: beginner 1375316643e7SJed Brown 1376316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1377316643e7SJed Brown 1378ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1379316643e7SJed Brown 1380316643e7SJed Brown @*/ 1381e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1382316643e7SJed Brown { 1383316643e7SJed Brown PetscErrorCode ierr; 1384089b2837SJed Brown SNES snes; 138524989b8cSPeter Brune DM dm; 1386316643e7SJed Brown 1387316643e7SJed Brown PetscFunctionBegin; 13880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1389e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1390e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1391e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1392e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 139324989b8cSPeter Brune 139424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 139524989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 139624989b8cSPeter Brune 1397089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1398e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1399316643e7SJed Brown PetscFunctionReturn(0); 1400316643e7SJed Brown } 1401316643e7SJed Brown 1402e1244c69SJed Brown /*@ 1403e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1404e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1405e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1406e1244c69SJed Brown not been changed by the TS. 1407e1244c69SJed Brown 1408e1244c69SJed Brown Logically Collective 1409e1244c69SJed Brown 1410e1244c69SJed Brown Input Arguments: 1411e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1412e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1413e1244c69SJed Brown 1414e1244c69SJed Brown Level: intermediate 1415e1244c69SJed Brown 1416e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1417e1244c69SJed Brown @*/ 1418e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1419e1244c69SJed Brown { 1420e1244c69SJed Brown PetscFunctionBegin; 1421e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1422e1244c69SJed Brown PetscFunctionReturn(0); 1423e1244c69SJed Brown } 1424e1244c69SJed Brown 1425efe9872eSLisandro Dalcin /*@C 1426efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1427efe9872eSLisandro Dalcin 1428efe9872eSLisandro Dalcin Logically Collective on TS 1429efe9872eSLisandro Dalcin 1430efe9872eSLisandro Dalcin Input Parameters: 1431efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1432efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1433efe9872eSLisandro Dalcin . fun - the function evaluation routine 1434efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1435efe9872eSLisandro Dalcin 1436efe9872eSLisandro Dalcin Calling sequence of fun: 1437efe9872eSLisandro Dalcin $ fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1438efe9872eSLisandro Dalcin 1439efe9872eSLisandro Dalcin + t - time at step/stage being solved 1440efe9872eSLisandro Dalcin . U - state vector 1441efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1442efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1443efe9872eSLisandro Dalcin . F - function vector 1444efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1445efe9872eSLisandro Dalcin 1446efe9872eSLisandro Dalcin Level: beginner 1447efe9872eSLisandro Dalcin 1448efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function 1449efe9872eSLisandro Dalcin 1450efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1451efe9872eSLisandro Dalcin @*/ 1452efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx) 1453efe9872eSLisandro Dalcin { 1454efe9872eSLisandro Dalcin DM dm; 1455efe9872eSLisandro Dalcin PetscErrorCode ierr; 1456efe9872eSLisandro Dalcin 1457efe9872eSLisandro Dalcin PetscFunctionBegin; 1458efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1459efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2); 1460efe9872eSLisandro Dalcin ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr); 1461efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1462efe9872eSLisandro Dalcin ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1463efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1464efe9872eSLisandro Dalcin } 1465efe9872eSLisandro Dalcin 1466efe9872eSLisandro Dalcin /*@C 1467efe9872eSLisandro Dalcin TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1468efe9872eSLisandro Dalcin 1469efe9872eSLisandro Dalcin Not Collective 1470efe9872eSLisandro Dalcin 1471efe9872eSLisandro Dalcin Input Parameter: 1472efe9872eSLisandro Dalcin . ts - the TS context 1473efe9872eSLisandro Dalcin 1474efe9872eSLisandro Dalcin Output Parameter: 1475efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1476efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1477efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1478efe9872eSLisandro Dalcin 1479efe9872eSLisandro Dalcin Level: advanced 1480efe9872eSLisandro Dalcin 1481efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function 1482efe9872eSLisandro Dalcin 1483efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction() 1484efe9872eSLisandro Dalcin @*/ 1485efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx) 1486efe9872eSLisandro Dalcin { 1487efe9872eSLisandro Dalcin PetscErrorCode ierr; 1488efe9872eSLisandro Dalcin SNES snes; 1489efe9872eSLisandro Dalcin DM dm; 1490efe9872eSLisandro Dalcin 1491efe9872eSLisandro Dalcin PetscFunctionBegin; 1492efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1493efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1494efe9872eSLisandro Dalcin ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 1495efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1496efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1497efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1498efe9872eSLisandro Dalcin } 1499efe9872eSLisandro Dalcin 1500efe9872eSLisandro Dalcin /*@C 1501bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1502efe9872eSLisandro Dalcin where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function(). 1503efe9872eSLisandro Dalcin 1504efe9872eSLisandro Dalcin Logically Collective on TS 1505efe9872eSLisandro Dalcin 1506efe9872eSLisandro Dalcin Input Parameters: 1507efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1508efe9872eSLisandro Dalcin . J - Jacobian matrix 1509efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1510efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1511efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1512efe9872eSLisandro Dalcin 1513efe9872eSLisandro Dalcin Calling sequence of jac: 1514bc77d74cSLisandro Dalcin $ jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx); 1515efe9872eSLisandro Dalcin 1516efe9872eSLisandro Dalcin + t - time at step/stage being solved 1517efe9872eSLisandro Dalcin . U - state vector 1518efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1519efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1520efe9872eSLisandro Dalcin . v - shift for U_t 1521efe9872eSLisandro Dalcin . a - shift for U_tt 1522efe9872eSLisandro 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 1523efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1524efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1525efe9872eSLisandro Dalcin 1526efe9872eSLisandro Dalcin Notes: 1527efe9872eSLisandro Dalcin The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve. 1528efe9872eSLisandro Dalcin 1529efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1530efe9872eSLisandro 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. 1531efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1532bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1533efe9872eSLisandro Dalcin 1534efe9872eSLisandro Dalcin Level: beginner 1535efe9872eSLisandro Dalcin 1536efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian 1537efe9872eSLisandro Dalcin 1538efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1539efe9872eSLisandro Dalcin @*/ 1540efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx) 1541efe9872eSLisandro Dalcin { 1542efe9872eSLisandro Dalcin DM dm; 1543efe9872eSLisandro Dalcin PetscErrorCode ierr; 1544efe9872eSLisandro Dalcin 1545efe9872eSLisandro Dalcin PetscFunctionBegin; 1546efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1547efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2); 1548efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3); 1549efe9872eSLisandro Dalcin ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr); 1550efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1551efe9872eSLisandro Dalcin ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1552efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1553efe9872eSLisandro Dalcin } 1554efe9872eSLisandro Dalcin 1555efe9872eSLisandro Dalcin /*@C 1556efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1557efe9872eSLisandro Dalcin 1558efe9872eSLisandro Dalcin Not Collective, but parallel objects are returned if TS is parallel 1559efe9872eSLisandro Dalcin 1560efe9872eSLisandro Dalcin Input Parameter: 1561efe9872eSLisandro Dalcin . ts - The TS context obtained from TSCreate() 1562efe9872eSLisandro Dalcin 1563efe9872eSLisandro Dalcin Output Parameters: 1564efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1565efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1566efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1567efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1568efe9872eSLisandro Dalcin 1569efe9872eSLisandro Dalcin Notes: You can pass in NULL for any return argument you do not need. 1570efe9872eSLisandro Dalcin 1571efe9872eSLisandro Dalcin Level: advanced 1572efe9872eSLisandro Dalcin 1573efe9872eSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 1574efe9872eSLisandro Dalcin 1575efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian 1576efe9872eSLisandro Dalcin @*/ 1577efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx) 1578efe9872eSLisandro Dalcin { 1579efe9872eSLisandro Dalcin PetscErrorCode ierr; 1580efe9872eSLisandro Dalcin SNES snes; 1581efe9872eSLisandro Dalcin DM dm; 1582efe9872eSLisandro Dalcin 1583efe9872eSLisandro Dalcin PetscFunctionBegin; 1584efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1585efe9872eSLisandro Dalcin ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 1586efe9872eSLisandro Dalcin ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr); 1587efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1588efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1589efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1590efe9872eSLisandro Dalcin } 1591efe9872eSLisandro Dalcin 1592efe9872eSLisandro Dalcin /*@ 1593efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1594efe9872eSLisandro Dalcin 1595efe9872eSLisandro Dalcin Collective on TS and Vec 1596efe9872eSLisandro Dalcin 1597efe9872eSLisandro Dalcin Input Parameters: 1598efe9872eSLisandro Dalcin + ts - the TS context 1599efe9872eSLisandro Dalcin . t - current time 1600efe9872eSLisandro Dalcin . U - state vector 1601efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1602efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1603efe9872eSLisandro Dalcin 1604efe9872eSLisandro Dalcin Output Parameter: 1605efe9872eSLisandro Dalcin . F - the residual vector 1606efe9872eSLisandro Dalcin 1607efe9872eSLisandro Dalcin Note: 1608efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1609efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1610efe9872eSLisandro Dalcin 1611efe9872eSLisandro Dalcin Level: developer 1612efe9872eSLisandro Dalcin 1613efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector 1614efe9872eSLisandro Dalcin 1615efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1616efe9872eSLisandro Dalcin @*/ 1617efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F) 1618efe9872eSLisandro Dalcin { 1619efe9872eSLisandro Dalcin DM dm; 1620efe9872eSLisandro Dalcin TSI2Function I2Function; 1621efe9872eSLisandro Dalcin void *ctx; 1622efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1623efe9872eSLisandro Dalcin PetscErrorCode ierr; 1624efe9872eSLisandro Dalcin 1625efe9872eSLisandro Dalcin PetscFunctionBegin; 1626efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1627efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1628efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1629efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1630efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F,VEC_CLASSID,6); 1631efe9872eSLisandro Dalcin 1632efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1633efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr); 1634efe9872eSLisandro Dalcin ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 1635efe9872eSLisandro Dalcin 1636efe9872eSLisandro Dalcin if (!I2Function) { 1637efe9872eSLisandro Dalcin ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr); 1638efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1639efe9872eSLisandro Dalcin } 1640efe9872eSLisandro Dalcin 1641efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1642efe9872eSLisandro Dalcin 1643efe9872eSLisandro Dalcin PetscStackPush("TS user implicit function"); 1644efe9872eSLisandro Dalcin ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr); 1645efe9872eSLisandro Dalcin PetscStackPop; 1646efe9872eSLisandro Dalcin 1647efe9872eSLisandro Dalcin if (rhsfunction) { 1648efe9872eSLisandro Dalcin Vec Frhs; 1649efe9872eSLisandro Dalcin ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 1650efe9872eSLisandro Dalcin ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 1651efe9872eSLisandro Dalcin ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr); 1652efe9872eSLisandro Dalcin } 1653efe9872eSLisandro Dalcin 1654efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1655efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1656efe9872eSLisandro Dalcin } 1657efe9872eSLisandro Dalcin 1658efe9872eSLisandro Dalcin /*@ 1659efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1660efe9872eSLisandro Dalcin 1661efe9872eSLisandro Dalcin Collective on TS and Vec 1662efe9872eSLisandro Dalcin 1663efe9872eSLisandro Dalcin Input Parameters: 1664efe9872eSLisandro Dalcin + ts - the TS context 1665efe9872eSLisandro Dalcin . t - current timestep 1666efe9872eSLisandro Dalcin . U - state vector 1667efe9872eSLisandro Dalcin . V - time derivative of state vector 1668efe9872eSLisandro Dalcin . A - second time derivative of state vector 1669efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1670efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1671efe9872eSLisandro Dalcin 1672efe9872eSLisandro Dalcin Output Parameters: 1673efe9872eSLisandro Dalcin + J - Jacobian matrix 1674efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1675efe9872eSLisandro Dalcin 1676efe9872eSLisandro Dalcin Notes: 1677efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1678efe9872eSLisandro Dalcin 1679efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1680efe9872eSLisandro Dalcin 1681efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1682efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1683efe9872eSLisandro Dalcin 1684efe9872eSLisandro Dalcin Level: developer 1685efe9872eSLisandro Dalcin 1686efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix 1687efe9872eSLisandro Dalcin 1688efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1689efe9872eSLisandro Dalcin @*/ 1690efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P) 1691efe9872eSLisandro Dalcin { 1692efe9872eSLisandro Dalcin DM dm; 1693efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1694efe9872eSLisandro Dalcin void *ctx; 1695efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1696efe9872eSLisandro Dalcin PetscErrorCode ierr; 1697efe9872eSLisandro Dalcin 1698efe9872eSLisandro Dalcin PetscFunctionBegin; 1699efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1700efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1701efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1702efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1703efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J,MAT_CLASSID,8); 1704efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P,MAT_CLASSID,9); 1705efe9872eSLisandro Dalcin 1706efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1707efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr); 1708efe9872eSLisandro Dalcin ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 1709efe9872eSLisandro Dalcin 1710efe9872eSLisandro Dalcin if (!I2Jacobian) { 1711efe9872eSLisandro Dalcin ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr); 1712efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1713efe9872eSLisandro Dalcin } 1714efe9872eSLisandro Dalcin 1715efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1716efe9872eSLisandro Dalcin 1717efe9872eSLisandro Dalcin PetscStackPush("TS user implicit Jacobian"); 1718efe9872eSLisandro Dalcin ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr); 1719efe9872eSLisandro Dalcin PetscStackPop; 1720efe9872eSLisandro Dalcin 1721efe9872eSLisandro Dalcin if (rhsjacobian) { 1722efe9872eSLisandro Dalcin Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 1723efe9872eSLisandro Dalcin ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr); 1724efe9872eSLisandro Dalcin ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr); 1725efe9872eSLisandro Dalcin ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr); 1726efe9872eSLisandro Dalcin if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);} 1727efe9872eSLisandro Dalcin } 1728efe9872eSLisandro Dalcin 1729efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1730efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1731efe9872eSLisandro Dalcin } 1732efe9872eSLisandro Dalcin 1733efe9872eSLisandro Dalcin /*@ 1734efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1735efe9872eSLisandro Dalcin for use by the TS routines handling second order equations. 1736efe9872eSLisandro Dalcin 1737efe9872eSLisandro Dalcin Logically Collective on TS and Vec 1738efe9872eSLisandro Dalcin 1739efe9872eSLisandro Dalcin Input Parameters: 1740efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1741efe9872eSLisandro Dalcin . u - the solution vector 1742efe9872eSLisandro Dalcin - v - the time derivative vector 1743efe9872eSLisandro Dalcin 1744efe9872eSLisandro Dalcin Level: beginner 1745efe9872eSLisandro Dalcin 1746efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions 1747efe9872eSLisandro Dalcin @*/ 1748efe9872eSLisandro Dalcin PetscErrorCode TS2SetSolution(TS ts,Vec u,Vec v) 1749efe9872eSLisandro Dalcin { 1750efe9872eSLisandro Dalcin PetscErrorCode ierr; 1751efe9872eSLisandro Dalcin 1752efe9872eSLisandro Dalcin PetscFunctionBegin; 1753efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1754efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1755efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v,VEC_CLASSID,3); 1756efe9872eSLisandro Dalcin ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 1757efe9872eSLisandro Dalcin ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr); 1758efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 1759efe9872eSLisandro Dalcin ts->vec_dot = v; 1760efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1761efe9872eSLisandro Dalcin } 1762efe9872eSLisandro Dalcin 1763efe9872eSLisandro Dalcin /*@ 1764efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 1765efe9872eSLisandro Dalcin for second order equations. It is valid to call this routine inside the function 1766efe9872eSLisandro Dalcin that you are evaluating in order to move to the new timestep. This vector not 1767efe9872eSLisandro Dalcin changed until the solution at the next timestep has been calculated. 1768efe9872eSLisandro Dalcin 1769efe9872eSLisandro Dalcin Not Collective, but Vec returned is parallel if TS is parallel 1770efe9872eSLisandro Dalcin 1771efe9872eSLisandro Dalcin Input Parameter: 1772efe9872eSLisandro Dalcin . ts - the TS context obtained from TSCreate() 1773efe9872eSLisandro Dalcin 1774efe9872eSLisandro Dalcin Output Parameter: 1775efe9872eSLisandro Dalcin + u - the vector containing the solution 1776efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1777efe9872eSLisandro Dalcin 1778efe9872eSLisandro Dalcin Level: intermediate 1779efe9872eSLisandro Dalcin 1780efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime() 1781efe9872eSLisandro Dalcin 1782efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution 1783efe9872eSLisandro Dalcin @*/ 1784efe9872eSLisandro Dalcin PetscErrorCode TS2GetSolution(TS ts,Vec *u,Vec *v) 1785efe9872eSLisandro Dalcin { 1786efe9872eSLisandro Dalcin PetscFunctionBegin; 1787efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1788efe9872eSLisandro Dalcin if (u) PetscValidPointer(u,2); 1789efe9872eSLisandro Dalcin if (v) PetscValidPointer(v,3); 1790efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1791efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 1792efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1793efe9872eSLisandro Dalcin } 1794efe9872eSLisandro Dalcin 179555849f57SBarry Smith /*@C 179655849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 179755849f57SBarry Smith 179855849f57SBarry Smith Collective on PetscViewer 179955849f57SBarry Smith 180055849f57SBarry Smith Input Parameters: 180155849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 180255849f57SBarry Smith some related function before a call to TSLoad(). 180355849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 180455849f57SBarry Smith 180555849f57SBarry Smith Level: intermediate 180655849f57SBarry Smith 180755849f57SBarry Smith Notes: 180855849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 180955849f57SBarry Smith 181055849f57SBarry Smith Notes for advanced users: 181155849f57SBarry Smith Most users should not need to know the details of the binary storage 181255849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 181355849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 181455849f57SBarry Smith format is 181555849f57SBarry Smith .vb 181655849f57SBarry Smith has not yet been determined 181755849f57SBarry Smith .ve 181855849f57SBarry Smith 181955849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 182055849f57SBarry Smith @*/ 1821f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 182255849f57SBarry Smith { 182355849f57SBarry Smith PetscErrorCode ierr; 182455849f57SBarry Smith PetscBool isbinary; 182555849f57SBarry Smith PetscInt classid; 182655849f57SBarry Smith char type[256]; 18272d53ad75SBarry Smith DMTS sdm; 1828ad6bc421SBarry Smith DM dm; 182955849f57SBarry Smith 183055849f57SBarry Smith PetscFunctionBegin; 1831f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 183255849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 183355849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 183455849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 183555849f57SBarry Smith 1836060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1837ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1838060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1839f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1840f2c2a1b9SBarry Smith if (ts->ops->load) { 1841f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1842f2c2a1b9SBarry Smith } 1843ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1844ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1845ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1846f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1847f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 18482d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 18492d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 185055849f57SBarry Smith PetscFunctionReturn(0); 185155849f57SBarry Smith } 185255849f57SBarry Smith 18539804daf3SBarry Smith #include <petscdraw.h> 1854e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1855e04113cfSBarry Smith #include <petscviewersaws.h> 1856f05ece33SBarry Smith #endif 18577e2c5f70SBarry Smith /*@C 1858d763cef2SBarry Smith TSView - Prints the TS data structure. 1859d763cef2SBarry Smith 18604c49b128SBarry Smith Collective on TS 1861d763cef2SBarry Smith 1862d763cef2SBarry Smith Input Parameters: 1863d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1864d763cef2SBarry Smith - viewer - visualization context 1865d763cef2SBarry Smith 1866d763cef2SBarry Smith Options Database Key: 1867d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1868d763cef2SBarry Smith 1869d763cef2SBarry Smith Notes: 1870d763cef2SBarry Smith The available visualization contexts include 1871b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1872b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1873d763cef2SBarry Smith output where only the first processor opens 1874d763cef2SBarry Smith the file. All other processors send their 1875d763cef2SBarry Smith data to the first processor to print. 1876d763cef2SBarry Smith 1877d763cef2SBarry Smith The user can open an alternative visualization context with 1878b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1879d763cef2SBarry Smith 1880d763cef2SBarry Smith Level: beginner 1881d763cef2SBarry Smith 1882d763cef2SBarry Smith .keywords: TS, timestep, view 1883d763cef2SBarry Smith 1884b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1885d763cef2SBarry Smith @*/ 18867087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1887d763cef2SBarry Smith { 1888dfbe8321SBarry Smith PetscErrorCode ierr; 188919fd82e9SBarry Smith TSType type; 18902b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 18912d53ad75SBarry Smith DMTS sdm; 1892e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1893536b137fSBarry Smith PetscBool issaws; 1894f05ece33SBarry Smith #endif 1895d763cef2SBarry Smith 1896d763cef2SBarry Smith PetscFunctionBegin; 18970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18983050cee2SBarry Smith if (!viewer) { 1899ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 19003050cee2SBarry Smith } 19010700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1902c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1903fd16b177SBarry Smith 1904251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1905251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 190655849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 19072b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1908e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1909536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1910f05ece33SBarry Smith #endif 191132077d6dSBarry Smith if (iascii) { 1912dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 191377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 19147c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1915d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 19165ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1917c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1918d763cef2SBarry Smith } 19195ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1920193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 1921a0af407cSBarry Smith if (ts->vrtol) { 1922a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of relative error tolerances, ");CHKERRQ(ierr); 1923a0af407cSBarry Smith } else { 1924a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr); 1925a0af407cSBarry Smith } 1926a0af407cSBarry Smith if (ts->vatol) { 1927a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of absolute error tolerances\n");CHKERRQ(ierr); 1928a0af407cSBarry Smith } else { 1929a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr); 1930a0af407cSBarry Smith } 19312d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19322d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1933d52bd9f3SBarry Smith if (ts->ops->view) { 1934d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1935d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1936d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1937d52bd9f3SBarry Smith } 19380f5bd95cSBarry Smith } else if (isstring) { 1939a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1940b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 194155849f57SBarry Smith } else if (isbinary) { 194255849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 194355849f57SBarry Smith MPI_Comm comm; 194455849f57SBarry Smith PetscMPIInt rank; 194555849f57SBarry Smith char type[256]; 194655849f57SBarry Smith 194755849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 194855849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 194955849f57SBarry Smith if (!rank) { 195055849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 195155849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 195255849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 195355849f57SBarry Smith } 195455849f57SBarry Smith if (ts->ops->view) { 195555849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 195655849f57SBarry Smith } 1957f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1958f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 19592d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19602d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 19612b0a91c0SBarry Smith } else if (isdraw) { 19622b0a91c0SBarry Smith PetscDraw draw; 19632b0a91c0SBarry Smith char str[36]; 196489fd9fafSBarry Smith PetscReal x,y,bottom,h; 19652b0a91c0SBarry Smith 19662b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 19672b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 19682b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 19692b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 197051fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 197189fd9fafSBarry Smith bottom = y - h; 19722b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 19732b0a91c0SBarry Smith if (ts->ops->view) { 19742b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 19752b0a91c0SBarry Smith } 19762b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1977e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1978536b137fSBarry Smith } else if (issaws) { 1979d45a07a7SBarry Smith PetscMPIInt rank; 19802657e9d9SBarry Smith const char *name; 19812657e9d9SBarry Smith 19822657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1983d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1984d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1985d45a07a7SBarry Smith char dir[1024]; 1986d45a07a7SBarry Smith 1987e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1988a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 19892657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 19902657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 19912657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1992d763cef2SBarry Smith } 19930acecf5bSBarry Smith if (ts->ops->view) { 19940acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 19950acecf5bSBarry Smith } 1996f05ece33SBarry Smith #endif 1997f05ece33SBarry Smith } 1998f05ece33SBarry Smith 1999b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2000251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 2001b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2002d763cef2SBarry Smith PetscFunctionReturn(0); 2003d763cef2SBarry Smith } 2004d763cef2SBarry Smith 2005d763cef2SBarry Smith 2006b07ff414SBarry Smith /*@ 2007d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2008d763cef2SBarry Smith the timesteppers. 2009d763cef2SBarry Smith 20103f9fe445SBarry Smith Logically Collective on TS 2011d763cef2SBarry Smith 2012d763cef2SBarry Smith Input Parameters: 2013d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2014d763cef2SBarry Smith - usrP - optional user context 2015d763cef2SBarry Smith 2016daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 2017daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2018daf670e6SBarry Smith 2019d763cef2SBarry Smith Level: intermediate 2020d763cef2SBarry Smith 2021d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 2022d763cef2SBarry Smith 2023d763cef2SBarry Smith .seealso: TSGetApplicationContext() 2024d763cef2SBarry Smith @*/ 20257087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 2026d763cef2SBarry Smith { 2027d763cef2SBarry Smith PetscFunctionBegin; 20280700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2029d763cef2SBarry Smith ts->user = usrP; 2030d763cef2SBarry Smith PetscFunctionReturn(0); 2031d763cef2SBarry Smith } 2032d763cef2SBarry Smith 2033b07ff414SBarry Smith /*@ 2034d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2035d763cef2SBarry Smith timestepper. 2036d763cef2SBarry Smith 2037d763cef2SBarry Smith Not Collective 2038d763cef2SBarry Smith 2039d763cef2SBarry Smith Input Parameter: 2040d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2041d763cef2SBarry Smith 2042d763cef2SBarry Smith Output Parameter: 2043d763cef2SBarry Smith . usrP - user context 2044d763cef2SBarry Smith 2045daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 2046daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2047daf670e6SBarry Smith 2048d763cef2SBarry Smith Level: intermediate 2049d763cef2SBarry Smith 2050d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 2051d763cef2SBarry Smith 2052d763cef2SBarry Smith .seealso: TSSetApplicationContext() 2053d763cef2SBarry Smith @*/ 2054e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 2055d763cef2SBarry Smith { 2056d763cef2SBarry Smith PetscFunctionBegin; 20570700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2058e71120c6SJed Brown *(void**)usrP = ts->user; 2059d763cef2SBarry Smith PetscFunctionReturn(0); 2060d763cef2SBarry Smith } 2061d763cef2SBarry Smith 2062d763cef2SBarry Smith /*@ 2063b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 2064d763cef2SBarry Smith 2065d763cef2SBarry Smith Not Collective 2066d763cef2SBarry Smith 2067d763cef2SBarry Smith Input Parameter: 2068d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2069d763cef2SBarry Smith 2070d763cef2SBarry Smith Output Parameter: 2071b8123daeSJed Brown . iter - number of steps completed so far 2072d763cef2SBarry Smith 2073d763cef2SBarry Smith Level: intermediate 2074d763cef2SBarry Smith 2075d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 20769be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 2077d763cef2SBarry Smith @*/ 20787087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 2079d763cef2SBarry Smith { 2080d763cef2SBarry Smith PetscFunctionBegin; 20810700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20824482741eSBarry Smith PetscValidIntPointer(iter,2); 2083d763cef2SBarry Smith *iter = ts->steps; 2084d763cef2SBarry Smith PetscFunctionReturn(0); 2085d763cef2SBarry Smith } 2086d763cef2SBarry Smith 2087d763cef2SBarry Smith /*@ 2088d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 2089d763cef2SBarry Smith as well as the initial time. 2090d763cef2SBarry Smith 20913f9fe445SBarry Smith Logically Collective on TS 2092d763cef2SBarry Smith 2093d763cef2SBarry Smith Input Parameters: 2094d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2095d763cef2SBarry Smith . initial_time - the initial time 2096d763cef2SBarry Smith - time_step - the size of the timestep 2097d763cef2SBarry Smith 2098d763cef2SBarry Smith Level: intermediate 2099d763cef2SBarry Smith 2100d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 2101d763cef2SBarry Smith 2102d763cef2SBarry Smith .keywords: TS, set, initial, timestep 2103d763cef2SBarry Smith @*/ 21047087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 2105d763cef2SBarry Smith { 2106e144a568SJed Brown PetscErrorCode ierr; 2107e144a568SJed Brown 2108d763cef2SBarry Smith PetscFunctionBegin; 21090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2110e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 2111d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 2112d763cef2SBarry Smith PetscFunctionReturn(0); 2113d763cef2SBarry Smith } 2114d763cef2SBarry Smith 2115d763cef2SBarry Smith /*@ 2116d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2117d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2118d763cef2SBarry Smith 21193f9fe445SBarry Smith Logically Collective on TS 2120d763cef2SBarry Smith 2121d763cef2SBarry Smith Input Parameters: 2122d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2123d763cef2SBarry Smith - time_step - the size of the timestep 2124d763cef2SBarry Smith 2125d763cef2SBarry Smith Level: intermediate 2126d763cef2SBarry Smith 2127d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2128d763cef2SBarry Smith 2129d763cef2SBarry Smith .keywords: TS, set, timestep 2130d763cef2SBarry Smith @*/ 21317087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 2132d763cef2SBarry Smith { 2133d763cef2SBarry Smith PetscFunctionBegin; 21340700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2135c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 2136d763cef2SBarry Smith ts->time_step = time_step; 2137d763cef2SBarry Smith PetscFunctionReturn(0); 2138d763cef2SBarry Smith } 2139d763cef2SBarry Smith 2140a43b19c4SJed Brown /*@ 214149354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 214249354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 214349354f04SShri Abhyankar or just return at the final time TS computed. 2144a43b19c4SJed Brown 2145a43b19c4SJed Brown Logically Collective on TS 2146a43b19c4SJed Brown 2147a43b19c4SJed Brown Input Parameter: 2148a43b19c4SJed Brown + ts - the time-step context 214949354f04SShri Abhyankar - eftopt - exact final time option 2150a43b19c4SJed Brown 2151feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2152feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2153feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2154feed9e9dSBarry Smith 2155feed9e9dSBarry Smith Options Database: 2156feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2157feed9e9dSBarry Smith 2158ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 2159ee346746SBarry Smith then the final time you selected. 2160ee346746SBarry Smith 2161a43b19c4SJed Brown Level: beginner 2162a43b19c4SJed Brown 2163a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 2164a43b19c4SJed Brown @*/ 216549354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 2166a43b19c4SJed Brown { 2167a43b19c4SJed Brown PetscFunctionBegin; 2168a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 216949354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 217049354f04SShri Abhyankar ts->exact_final_time = eftopt; 2171a43b19c4SJed Brown PetscFunctionReturn(0); 2172a43b19c4SJed Brown } 2173a43b19c4SJed Brown 2174d763cef2SBarry Smith /*@ 2175d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2176d763cef2SBarry Smith 2177d763cef2SBarry Smith Not Collective 2178d763cef2SBarry Smith 2179d763cef2SBarry Smith Input Parameter: 2180d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2181d763cef2SBarry Smith 2182d763cef2SBarry Smith Output Parameter: 2183d763cef2SBarry Smith . dt - the current timestep size 2184d763cef2SBarry Smith 2185d763cef2SBarry Smith Level: intermediate 2186d763cef2SBarry Smith 2187d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2188d763cef2SBarry Smith 2189d763cef2SBarry Smith .keywords: TS, get, timestep 2190d763cef2SBarry Smith @*/ 21917087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 2192d763cef2SBarry Smith { 2193d763cef2SBarry Smith PetscFunctionBegin; 21940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2195f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 2196d763cef2SBarry Smith *dt = ts->time_step; 2197d763cef2SBarry Smith PetscFunctionReturn(0); 2198d763cef2SBarry Smith } 2199d763cef2SBarry Smith 2200d8e5e3e6SSatish Balay /*@ 2201d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2202d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2203d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2204d763cef2SBarry Smith the solution at the next timestep has been calculated. 2205d763cef2SBarry Smith 2206d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 2207d763cef2SBarry Smith 2208d763cef2SBarry Smith Input Parameter: 2209d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2210d763cef2SBarry Smith 2211d763cef2SBarry Smith Output Parameter: 2212d763cef2SBarry Smith . v - the vector containing the solution 2213d763cef2SBarry Smith 221463e21af5SBarry Smith Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested 221563e21af5SBarry Smith final time. It returns the solution at the next timestep. 221663e21af5SBarry Smith 2217d763cef2SBarry Smith Level: intermediate 2218d763cef2SBarry Smith 2219b2bf4f3aSDebojyoti Ghosh .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents() 2220d763cef2SBarry Smith 2221d763cef2SBarry Smith .keywords: TS, timestep, get, solution 2222d763cef2SBarry Smith @*/ 22237087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 2224d763cef2SBarry Smith { 2225d763cef2SBarry Smith PetscFunctionBegin; 22260700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22274482741eSBarry Smith PetscValidPointer(v,2); 22288737fe31SLisandro Dalcin *v = ts->vec_sol; 2229d763cef2SBarry Smith PetscFunctionReturn(0); 2230d763cef2SBarry Smith } 2231d763cef2SBarry Smith 223203fe5f5eSDebojyoti Ghosh /*@ 2233b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 223403fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2235b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 223603fe5f5eSDebojyoti Ghosh structure as the solution vector. 223703fe5f5eSDebojyoti Ghosh 223803fe5f5eSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 223903fe5f5eSDebojyoti Ghosh 224003fe5f5eSDebojyoti Ghosh Parameters : 224103fe5f5eSDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 2242b2bf4f3aSDebojyoti Ghosh . n - If v is PETSC_NULL, then the number of solution components is 2243b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 224403fe5f5eSDebojyoti Ghosh returned in v. 2245b2bf4f3aSDebojyoti Ghosh . v - the vector containing the n-th solution component 224603fe5f5eSDebojyoti Ghosh (may be PETSC_NULL to use this function to find out 2247b2bf4f3aSDebojyoti Ghosh the number of solutions components). 224803fe5f5eSDebojyoti Ghosh 22494cdd57e5SDebojyoti Ghosh Level: advanced 225003fe5f5eSDebojyoti Ghosh 225103fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution() 225203fe5f5eSDebojyoti Ghosh 225303fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution 225403fe5f5eSDebojyoti Ghosh @*/ 2255b2bf4f3aSDebojyoti Ghosh PetscErrorCode TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v) 225603fe5f5eSDebojyoti Ghosh { 225703fe5f5eSDebojyoti Ghosh PetscErrorCode ierr; 225803fe5f5eSDebojyoti Ghosh 225903fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 226003fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2261b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 226203fe5f5eSDebojyoti Ghosh else { 2263b2bf4f3aSDebojyoti Ghosh ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr); 226403fe5f5eSDebojyoti Ghosh } 226503fe5f5eSDebojyoti Ghosh PetscFunctionReturn(0); 226603fe5f5eSDebojyoti Ghosh } 226703fe5f5eSDebojyoti Ghosh 22684cdd57e5SDebojyoti Ghosh /*@ 22694cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 22704cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 22714cdd57e5SDebojyoti Ghosh 22724cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 22734cdd57e5SDebojyoti Ghosh 22744cdd57e5SDebojyoti Ghosh Parameters : 22754cdd57e5SDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 22764cdd57e5SDebojyoti Ghosh . v - the vector containing the auxiliary solution 22774cdd57e5SDebojyoti Ghosh 22784cdd57e5SDebojyoti Ghosh Level: intermediate 22794cdd57e5SDebojyoti Ghosh 22804cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution() 22814cdd57e5SDebojyoti Ghosh 22824cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution 22834cdd57e5SDebojyoti Ghosh @*/ 22844cdd57e5SDebojyoti Ghosh PetscErrorCode TSGetAuxSolution(TS ts,Vec *v) 22854cdd57e5SDebojyoti Ghosh { 22864cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 22874cdd57e5SDebojyoti Ghosh 22884cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 22894cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2290f6356ec7SDebojyoti Ghosh if (ts->ops->getauxsolution) { 22914cdd57e5SDebojyoti Ghosh ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr); 2292f6356ec7SDebojyoti Ghosh } else { 2293f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v); CHKERRQ(ierr); 2294f6356ec7SDebojyoti Ghosh } 22954cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 22964cdd57e5SDebojyoti Ghosh } 22974cdd57e5SDebojyoti Ghosh 22984cdd57e5SDebojyoti Ghosh /*@ 22994cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 23004cdd57e5SDebojyoti Ghosh TSType has an error estimation functionality. 23014cdd57e5SDebojyoti Ghosh 23024cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 23034cdd57e5SDebojyoti Ghosh 23049657682dSDebojyoti Ghosh Note: MUST call after TSSetUp() 23059657682dSDebojyoti Ghosh 23064cdd57e5SDebojyoti Ghosh Parameters : 23074cdd57e5SDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 2308657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 23094cdd57e5SDebojyoti Ghosh . v - the vector containing the error (same size as the solution). 23104cdd57e5SDebojyoti Ghosh 23114cdd57e5SDebojyoti Ghosh Level: intermediate 23124cdd57e5SDebojyoti Ghosh 231357df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError() 23144cdd57e5SDebojyoti Ghosh 23154cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error 23164cdd57e5SDebojyoti Ghosh @*/ 23170a01e1b2SEmil Constantinescu PetscErrorCode TSGetTimeError(TS ts,PetscInt n,Vec *v) 23184cdd57e5SDebojyoti Ghosh { 23194cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 23204cdd57e5SDebojyoti Ghosh 23214cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23224cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2323f6356ec7SDebojyoti Ghosh if (ts->ops->gettimeerror) { 23240a01e1b2SEmil Constantinescu ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr); 2325f6356ec7SDebojyoti Ghosh } else { 2326f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v);CHKERRQ(ierr); 2327f6356ec7SDebojyoti Ghosh } 23284cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 23294cdd57e5SDebojyoti Ghosh } 23304cdd57e5SDebojyoti Ghosh 233157df6a1bSDebojyoti Ghosh /*@ 233257df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 233357df6a1bSDebojyoti Ghosh TSType has an error estimation functionality. This can be used 233457df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 233557df6a1bSDebojyoti Ghosh 233657df6a1bSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 233757df6a1bSDebojyoti Ghosh 233857df6a1bSDebojyoti Ghosh Parameters : 233957df6a1bSDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 234057df6a1bSDebojyoti Ghosh . v - the vector containing the error (same size as the solution). 234157df6a1bSDebojyoti Ghosh 234257df6a1bSDebojyoti Ghosh Level: intermediate 234357df6a1bSDebojyoti Ghosh 234457df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError) 234557df6a1bSDebojyoti Ghosh 234657df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error 234757df6a1bSDebojyoti Ghosh @*/ 234857df6a1bSDebojyoti Ghosh PetscErrorCode TSSetTimeError(TS ts,Vec v) 234957df6a1bSDebojyoti Ghosh { 235057df6a1bSDebojyoti Ghosh PetscErrorCode ierr; 235157df6a1bSDebojyoti Ghosh 235257df6a1bSDebojyoti Ghosh PetscFunctionBegin; 235357df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 23549657682dSDebojyoti Ghosh if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first"); 235557df6a1bSDebojyoti Ghosh if (ts->ops->settimeerror) { 235657df6a1bSDebojyoti Ghosh ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr); 235757df6a1bSDebojyoti Ghosh } 235857df6a1bSDebojyoti Ghosh PetscFunctionReturn(0); 235957df6a1bSDebojyoti Ghosh } 236057df6a1bSDebojyoti Ghosh 2361c235aa8dSHong Zhang /*@ 2362dfb21088SHong Zhang TSGetCostGradients - Returns the gradients from the TSAdjointSolve() 2363c235aa8dSHong Zhang 2364c235aa8dSHong Zhang Not Collective, but Vec returned is parallel if TS is parallel 2365c235aa8dSHong Zhang 2366c235aa8dSHong Zhang Input Parameter: 2367c235aa8dSHong Zhang . ts - the TS context obtained from TSCreate() 2368c235aa8dSHong Zhang 2369c235aa8dSHong Zhang Output Parameter: 2370abc2977eSBarry Smith + lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 2371abc2977eSBarry Smith - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 2372c235aa8dSHong Zhang 2373c235aa8dSHong Zhang Level: intermediate 2374c235aa8dSHong Zhang 2375c235aa8dSHong Zhang .seealso: TSGetTimeStep() 2376c235aa8dSHong Zhang 2377c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity 2378c235aa8dSHong Zhang @*/ 2379dfb21088SHong Zhang PetscErrorCode TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu) 2380c235aa8dSHong Zhang { 2381c235aa8dSHong Zhang PetscFunctionBegin; 2382c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2383abc2977eSBarry Smith if (numcost) *numcost = ts->numcost; 2384abc2977eSBarry Smith if (lambda) *lambda = ts->vecs_sensi; 2385abc2977eSBarry Smith if (mu) *mu = ts->vecs_sensip; 2386c235aa8dSHong Zhang PetscFunctionReturn(0); 2387c235aa8dSHong Zhang } 2388c235aa8dSHong Zhang 2389bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2390d8e5e3e6SSatish Balay /*@ 2391bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2392d763cef2SBarry Smith 2393bdad233fSMatthew Knepley Not collective 2394d763cef2SBarry Smith 2395bdad233fSMatthew Knepley Input Parameters: 2396bdad233fSMatthew Knepley + ts - The TS 2397bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2398d763cef2SBarry Smith .vb 23990910c330SBarry Smith U_t - A U = 0 (linear) 24000910c330SBarry Smith U_t - A(t) U = 0 (linear) 24010910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2402d763cef2SBarry Smith .ve 2403d763cef2SBarry Smith 2404d763cef2SBarry Smith Level: beginner 2405d763cef2SBarry Smith 2406bdad233fSMatthew Knepley .keywords: TS, problem type 2407bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2408d763cef2SBarry Smith @*/ 24097087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2410a7cc72afSBarry Smith { 24119e2a6581SJed Brown PetscErrorCode ierr; 24129e2a6581SJed Brown 2413d763cef2SBarry Smith PetscFunctionBegin; 24140700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2415bdad233fSMatthew Knepley ts->problem_type = type; 24169e2a6581SJed Brown if (type == TS_LINEAR) { 24179e2a6581SJed Brown SNES snes; 24189e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 24199e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 24209e2a6581SJed Brown } 2421d763cef2SBarry Smith PetscFunctionReturn(0); 2422d763cef2SBarry Smith } 2423d763cef2SBarry Smith 2424bdad233fSMatthew Knepley /*@C 2425bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2426bdad233fSMatthew Knepley 2427bdad233fSMatthew Knepley Not collective 2428bdad233fSMatthew Knepley 2429bdad233fSMatthew Knepley Input Parameter: 2430bdad233fSMatthew Knepley . ts - The TS 2431bdad233fSMatthew Knepley 2432bdad233fSMatthew Knepley Output Parameter: 2433bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2434bdad233fSMatthew Knepley .vb 2435089b2837SJed Brown M U_t = A U 2436089b2837SJed Brown M(t) U_t = A(t) U 2437b5abc632SBarry Smith F(t,U,U_t) 2438bdad233fSMatthew Knepley .ve 2439bdad233fSMatthew Knepley 2440bdad233fSMatthew Knepley Level: beginner 2441bdad233fSMatthew Knepley 2442bdad233fSMatthew Knepley .keywords: TS, problem type 2443bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2444bdad233fSMatthew Knepley @*/ 24457087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2446a7cc72afSBarry Smith { 2447bdad233fSMatthew Knepley PetscFunctionBegin; 24480700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 24494482741eSBarry Smith PetscValidIntPointer(type,2); 2450bdad233fSMatthew Knepley *type = ts->problem_type; 2451bdad233fSMatthew Knepley PetscFunctionReturn(0); 2452bdad233fSMatthew Knepley } 2453d763cef2SBarry Smith 2454d763cef2SBarry Smith /*@ 2455d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 2456d763cef2SBarry Smith of a timestepper. 2457d763cef2SBarry Smith 2458d763cef2SBarry Smith Collective on TS 2459d763cef2SBarry Smith 2460d763cef2SBarry Smith Input Parameter: 2461d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2462d763cef2SBarry Smith 2463d763cef2SBarry Smith Notes: 2464d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 2465d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 2466d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 2467d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 2468d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 2469d763cef2SBarry Smith 2470d763cef2SBarry Smith Level: advanced 2471d763cef2SBarry Smith 2472d763cef2SBarry Smith .keywords: TS, timestep, setup 2473d763cef2SBarry Smith 2474d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 2475d763cef2SBarry Smith @*/ 24767087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 2477d763cef2SBarry Smith { 2478dfbe8321SBarry Smith PetscErrorCode ierr; 24796c6b9e74SPeter Brune DM dm; 24806c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 2481d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 2482cd11d68dSLisandro Dalcin TSIFunction ifun; 24836c6b9e74SPeter Brune TSIJacobian ijac; 2484efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 24856c6b9e74SPeter Brune TSRHSJacobian rhsjac; 2486d763cef2SBarry Smith 2487d763cef2SBarry Smith PetscFunctionBegin; 24880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2489277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 2490277b19d0SLisandro Dalcin 24917adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 2492cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 2493cd11d68dSLisandro Dalcin ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr); 2494d763cef2SBarry Smith } 2495277b19d0SLisandro Dalcin 2496277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 2497277b19d0SLisandro Dalcin 2498e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 2499e1244c69SJed Brown Mat Amat,Pmat; 2500e1244c69SJed Brown SNES snes; 2501e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2502e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 2503e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2504e1244c69SJed Brown * have displaced the RHS matrix */ 2505e1244c69SJed Brown if (Amat == ts->Arhs) { 2506e1244c69SJed Brown ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 2507e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 2508e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 2509e1244c69SJed Brown } 2510e1244c69SJed Brown if (Pmat == ts->Brhs) { 2511e1244c69SJed Brown ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 2512e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 2513e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 2514e1244c69SJed Brown } 2515e1244c69SJed Brown } 2516277b19d0SLisandro Dalcin if (ts->ops->setup) { 2517000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 2518277b19d0SLisandro Dalcin } 2519277b19d0SLisandro Dalcin 2520a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 25216c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 25226c6b9e74SPeter Brune */ 25236c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 25240298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 25256c6b9e74SPeter Brune if (!func) { 25266c6b9e74SPeter Brune ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 25276c6b9e74SPeter Brune } 2528a6772fa2SLisandro 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. 25296c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 25306c6b9e74SPeter Brune */ 25310298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 25320298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 2533efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr); 25340298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 2535efe9872eSLisandro Dalcin if (!jac && (ijac || i2jac || rhsjac)) { 25366c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 25376c6b9e74SPeter Brune } 2538c0517034SDebojyoti Ghosh 2539c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 2540c0517034SDebojyoti Ghosh if (ts->ops->startingmethod) { 2541c0517034SDebojyoti Ghosh ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr); 2542c0517034SDebojyoti Ghosh } 2543c0517034SDebojyoti Ghosh 2544277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2545277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2546277b19d0SLisandro Dalcin } 2547277b19d0SLisandro Dalcin 2548f6a906c0SBarry Smith /*@ 2549f6a906c0SBarry Smith TSAdjointSetUp - Sets up the internal data structures for the later use 2550f6a906c0SBarry Smith of an adjoint solver 2551f6a906c0SBarry Smith 2552f6a906c0SBarry Smith Collective on TS 2553f6a906c0SBarry Smith 2554f6a906c0SBarry Smith Input Parameter: 2555f6a906c0SBarry Smith . ts - the TS context obtained from TSCreate() 2556f6a906c0SBarry Smith 2557f6a906c0SBarry Smith Level: advanced 2558f6a906c0SBarry Smith 2559f6a906c0SBarry Smith .keywords: TS, timestep, setup 2560f6a906c0SBarry Smith 2561947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients() 2562f6a906c0SBarry Smith @*/ 2563f6a906c0SBarry Smith PetscErrorCode TSAdjointSetUp(TS ts) 2564f6a906c0SBarry Smith { 2565f6a906c0SBarry Smith PetscErrorCode ierr; 2566f6a906c0SBarry Smith 2567f6a906c0SBarry Smith PetscFunctionBegin; 2568f6a906c0SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2569f6a906c0SBarry Smith if (ts->adjointsetupcalled) PetscFunctionReturn(0); 2570dfb21088SHong Zhang if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first"); 2571d8151eeaSBarry Smith 2572947abb85SHong Zhang if (ts->vec_costintegral) { /* if there is integral in the cost function*/ 2573d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2574d8151eeaSBarry Smith if (ts->vecs_sensip){ 2575d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2576d8151eeaSBarry Smith } 2577947abb85SHong Zhang } 2578d8151eeaSBarry Smith 257942f2b339SBarry Smith if (ts->ops->adjointsetup) { 258042f2b339SBarry Smith ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr); 2581f6a906c0SBarry Smith } 2582f6a906c0SBarry Smith ts->adjointsetupcalled = PETSC_TRUE; 2583f6a906c0SBarry Smith PetscFunctionReturn(0); 2584f6a906c0SBarry Smith } 2585f6a906c0SBarry Smith 2586277b19d0SLisandro Dalcin /*@ 2587277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2588277b19d0SLisandro Dalcin 2589277b19d0SLisandro Dalcin Collective on TS 2590277b19d0SLisandro Dalcin 2591277b19d0SLisandro Dalcin Input Parameter: 2592277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2593277b19d0SLisandro Dalcin 2594277b19d0SLisandro Dalcin Level: beginner 2595277b19d0SLisandro Dalcin 2596277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 2597277b19d0SLisandro Dalcin 2598277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2599277b19d0SLisandro Dalcin @*/ 2600277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2601277b19d0SLisandro Dalcin { 2602277b19d0SLisandro Dalcin PetscErrorCode ierr; 2603277b19d0SLisandro Dalcin 2604277b19d0SLisandro Dalcin PetscFunctionBegin; 2605277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2606b18ea86cSHong Zhang 2607277b19d0SLisandro Dalcin if (ts->ops->reset) { 2608277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2609277b19d0SLisandro Dalcin } 2610277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2611e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2612bbd56ea5SKarl Rupp 26134e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 26144e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2615214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 26166bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2617efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 2618e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2619e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 262038637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2621bbd56ea5SKarl Rupp 2622947abb85SHong Zhang if (ts->vec_costintegral) { 2623d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2624d8151eeaSBarry Smith if (ts->vecs_drdp){ 2625d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2626d8151eeaSBarry Smith } 2627947abb85SHong Zhang } 2628d8151eeaSBarry Smith ts->vecs_sensi = NULL; 2629d8151eeaSBarry Smith ts->vecs_sensip = NULL; 2630ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 26312c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 263236eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2633277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2634d763cef2SBarry Smith PetscFunctionReturn(0); 2635d763cef2SBarry Smith } 2636d763cef2SBarry Smith 2637d8e5e3e6SSatish Balay /*@ 2638d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2639d763cef2SBarry Smith with TSCreate(). 2640d763cef2SBarry Smith 2641d763cef2SBarry Smith Collective on TS 2642d763cef2SBarry Smith 2643d763cef2SBarry Smith Input Parameter: 2644d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2645d763cef2SBarry Smith 2646d763cef2SBarry Smith Level: beginner 2647d763cef2SBarry Smith 2648d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2649d763cef2SBarry Smith 2650d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2651d763cef2SBarry Smith @*/ 26526bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2653d763cef2SBarry Smith { 26546849ba73SBarry Smith PetscErrorCode ierr; 2655d763cef2SBarry Smith 2656d763cef2SBarry Smith PetscFunctionBegin; 26576bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 26586bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 26596bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2660d763cef2SBarry Smith 26616bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2662277b19d0SLisandro Dalcin 2663e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2664e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 26656bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 26666d4c513bSLisandro Dalcin 2667bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2668bc952696SBarry Smith 266984df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 26706427ac75SLisandro Dalcin ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr); 26716427ac75SLisandro Dalcin 26726bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 26736bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 26746bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 26750dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 26766d4c513bSLisandro Dalcin 2677a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2678d763cef2SBarry Smith PetscFunctionReturn(0); 2679d763cef2SBarry Smith } 2680d763cef2SBarry Smith 2681d8e5e3e6SSatish Balay /*@ 2682d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2683d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2684d763cef2SBarry Smith 2685d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2686d763cef2SBarry Smith 2687d763cef2SBarry Smith Input Parameter: 2688d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2689d763cef2SBarry Smith 2690d763cef2SBarry Smith Output Parameter: 2691d763cef2SBarry Smith . snes - the nonlinear solver context 2692d763cef2SBarry Smith 2693d763cef2SBarry Smith Notes: 2694d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2695d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 269694b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2697d763cef2SBarry Smith 2698d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 26990298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2700d763cef2SBarry Smith 2701d763cef2SBarry Smith Level: beginner 2702d763cef2SBarry Smith 2703d763cef2SBarry Smith .keywords: timestep, get, SNES 2704d763cef2SBarry Smith @*/ 27057087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2706d763cef2SBarry Smith { 2707d372ba47SLisandro Dalcin PetscErrorCode ierr; 2708d372ba47SLisandro Dalcin 2709d763cef2SBarry Smith PetscFunctionBegin; 27100700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27114482741eSBarry Smith PetscValidPointer(snes,2); 2712d372ba47SLisandro Dalcin if (!ts->snes) { 2713ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 27140298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 27153bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2716d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2717496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 27189e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 27199e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 27209e2a6581SJed Brown } 2721d372ba47SLisandro Dalcin } 2722d763cef2SBarry Smith *snes = ts->snes; 2723d763cef2SBarry Smith PetscFunctionReturn(0); 2724d763cef2SBarry Smith } 2725d763cef2SBarry Smith 2726deb2cd25SJed Brown /*@ 2727deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2728deb2cd25SJed Brown 2729deb2cd25SJed Brown Collective 2730deb2cd25SJed Brown 2731deb2cd25SJed Brown Input Parameter: 2732deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2733deb2cd25SJed Brown - snes - the nonlinear solver context 2734deb2cd25SJed Brown 2735deb2cd25SJed Brown Notes: 2736deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2737deb2cd25SJed Brown 2738deb2cd25SJed Brown Level: developer 2739deb2cd25SJed Brown 2740deb2cd25SJed Brown .keywords: timestep, set, SNES 2741deb2cd25SJed Brown @*/ 2742deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2743deb2cd25SJed Brown { 2744deb2cd25SJed Brown PetscErrorCode ierr; 2745d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2746deb2cd25SJed Brown 2747deb2cd25SJed Brown PetscFunctionBegin; 2748deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2749deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2750deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2751deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2752bbd56ea5SKarl Rupp 2753deb2cd25SJed Brown ts->snes = snes; 2754bbd56ea5SKarl Rupp 27550298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 27560298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2757740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 27580298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2759740132f1SEmil Constantinescu } 2760deb2cd25SJed Brown PetscFunctionReturn(0); 2761deb2cd25SJed Brown } 2762deb2cd25SJed Brown 2763d8e5e3e6SSatish Balay /*@ 276494b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2765d763cef2SBarry Smith a TS (timestepper) context. 2766d763cef2SBarry Smith 276794b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2768d763cef2SBarry Smith 2769d763cef2SBarry Smith Input Parameter: 2770d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2771d763cef2SBarry Smith 2772d763cef2SBarry Smith Output Parameter: 277394b7f48cSBarry Smith . ksp - the nonlinear solver context 2774d763cef2SBarry Smith 2775d763cef2SBarry Smith Notes: 277694b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2777d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2778d763cef2SBarry Smith KSP and PC contexts as well. 2779d763cef2SBarry Smith 278094b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 27810298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2782d763cef2SBarry Smith 2783d763cef2SBarry Smith Level: beginner 2784d763cef2SBarry Smith 278594b7f48cSBarry Smith .keywords: timestep, get, KSP 2786d763cef2SBarry Smith @*/ 27877087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2788d763cef2SBarry Smith { 2789d372ba47SLisandro Dalcin PetscErrorCode ierr; 2790089b2837SJed Brown SNES snes; 2791d372ba47SLisandro Dalcin 2792d763cef2SBarry Smith PetscFunctionBegin; 27930700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27944482741eSBarry Smith PetscValidPointer(ksp,2); 279517186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2796e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2797089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2798089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2799d763cef2SBarry Smith PetscFunctionReturn(0); 2800d763cef2SBarry Smith } 2801d763cef2SBarry Smith 2802d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2803d763cef2SBarry Smith 2804adb62b0dSMatthew Knepley /*@ 2805adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2806adb62b0dSMatthew Knepley maximum time for iteration. 2807adb62b0dSMatthew Knepley 28083f9fe445SBarry Smith Not Collective 2809adb62b0dSMatthew Knepley 2810adb62b0dSMatthew Knepley Input Parameters: 2811adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 28120298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 28130298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2814adb62b0dSMatthew Knepley 2815adb62b0dSMatthew Knepley Level: intermediate 2816adb62b0dSMatthew Knepley 2817adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2818adb62b0dSMatthew Knepley @*/ 28197087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2820adb62b0dSMatthew Knepley { 2821adb62b0dSMatthew Knepley PetscFunctionBegin; 28220700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2823abc0a331SBarry Smith if (maxsteps) { 28244482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2825adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2826adb62b0dSMatthew Knepley } 2827abc0a331SBarry Smith if (maxtime) { 28284482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2829adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2830adb62b0dSMatthew Knepley } 2831adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2832adb62b0dSMatthew Knepley } 2833adb62b0dSMatthew Knepley 2834d763cef2SBarry Smith /*@ 2835d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2836d763cef2SBarry Smith maximum time for iteration. 2837d763cef2SBarry Smith 28383f9fe445SBarry Smith Logically Collective on TS 2839d763cef2SBarry Smith 2840d763cef2SBarry Smith Input Parameters: 2841d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2842d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2843d763cef2SBarry Smith - maxtime - final time to iterate to 2844d763cef2SBarry Smith 2845d763cef2SBarry Smith Options Database Keys: 2846d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 28473bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2848d763cef2SBarry Smith 2849d763cef2SBarry Smith Notes: 2850d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2851d763cef2SBarry Smith 2852d763cef2SBarry Smith Level: intermediate 2853d763cef2SBarry Smith 2854d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2855a43b19c4SJed Brown 2856a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2857d763cef2SBarry Smith @*/ 28587087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2859d763cef2SBarry Smith { 2860d763cef2SBarry Smith PetscFunctionBegin; 28610700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2862c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2863c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 286439b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 286539b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2866d763cef2SBarry Smith PetscFunctionReturn(0); 2867d763cef2SBarry Smith } 2868d763cef2SBarry Smith 2869d763cef2SBarry Smith /*@ 2870d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2871d763cef2SBarry Smith for use by the TS routines. 2872d763cef2SBarry Smith 28733f9fe445SBarry Smith Logically Collective on TS and Vec 2874d763cef2SBarry Smith 2875d763cef2SBarry Smith Input Parameters: 2876d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 28770910c330SBarry Smith - u - the solution vector 2878d763cef2SBarry Smith 2879d763cef2SBarry Smith Level: beginner 2880d763cef2SBarry Smith 2881d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2882d763cef2SBarry Smith @*/ 28830910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2884d763cef2SBarry Smith { 28858737fe31SLisandro Dalcin PetscErrorCode ierr; 2886496e6a7aSJed Brown DM dm; 28878737fe31SLisandro Dalcin 2888d763cef2SBarry Smith PetscFunctionBegin; 28890700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28900910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 28910910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 28926bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 28930910c330SBarry Smith ts->vec_sol = u; 2894bbd56ea5SKarl Rupp 2895496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 28960910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2897d763cef2SBarry Smith PetscFunctionReturn(0); 2898d763cef2SBarry Smith } 2899d763cef2SBarry Smith 290073b18844SBarry Smith /*@ 290173b18844SBarry Smith TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time 290273b18844SBarry Smith 290373b18844SBarry Smith Logically Collective on TS 290473b18844SBarry Smith 290573b18844SBarry Smith Input Parameters: 290673b18844SBarry Smith + ts - the TS context obtained from TSCreate() 290773b18844SBarry Smith . steps - number of steps to use 290873b18844SBarry Smith 290973b18844SBarry Smith Level: intermediate 291073b18844SBarry Smith 291173b18844SBarry Smith Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this 291273b18844SBarry Smith so as to integrate back to less than the original timestep 291373b18844SBarry Smith 291473b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations 291573b18844SBarry Smith 291673b18844SBarry Smith .seealso: TSSetExactFinalTime() 291773b18844SBarry Smith @*/ 291873b18844SBarry Smith PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps) 291973b18844SBarry Smith { 292073b18844SBarry Smith PetscFunctionBegin; 292173b18844SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 292273b18844SBarry Smith PetscValidLogicalCollectiveInt(ts,steps,2); 292373b18844SBarry Smith if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps"); 292473b18844SBarry 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"); 292573b18844SBarry Smith ts->adjoint_max_steps = steps; 292673b18844SBarry Smith PetscFunctionReturn(0); 292773b18844SBarry Smith } 292873b18844SBarry Smith 2929c235aa8dSHong Zhang /*@ 2930dfb21088SHong 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 29310cf82b59SBarry Smith for use by the TSAdjoint routines. 2932c235aa8dSHong Zhang 2933c235aa8dSHong Zhang Logically Collective on TS and Vec 2934c235aa8dSHong Zhang 2935c235aa8dSHong Zhang Input Parameters: 2936c235aa8dSHong Zhang + ts - the TS context obtained from TSCreate() 2937abc2977eSBarry 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 2938abc2977eSBarry Smith - mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters 2939c235aa8dSHong Zhang 2940c235aa8dSHong Zhang Level: beginner 2941c235aa8dSHong Zhang 2942abc2977eSBarry Smith Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime mu_i = df/dp|finaltime 29430cf82b59SBarry Smith 2944c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions 2945c235aa8dSHong Zhang @*/ 2946dfb21088SHong Zhang PetscErrorCode TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu) 2947c235aa8dSHong Zhang { 2948c235aa8dSHong Zhang PetscFunctionBegin; 2949c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2950abc2977eSBarry Smith PetscValidPointer(lambda,2); 2951abc2977eSBarry Smith ts->vecs_sensi = lambda; 2952abc2977eSBarry Smith ts->vecs_sensip = mu; 2953dfb21088SHong 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"); 2954abc2977eSBarry Smith ts->numcost = numcost; 2955ad8e2604SHong Zhang PetscFunctionReturn(0); 2956ad8e2604SHong Zhang } 2957ad8e2604SHong Zhang 2958ad8e2604SHong Zhang /*@C 29590cf82b59SBarry 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. 2960ad8e2604SHong Zhang 2961ad8e2604SHong Zhang Logically Collective on TS 2962ad8e2604SHong Zhang 2963ad8e2604SHong Zhang Input Parameters: 2964ad8e2604SHong Zhang + ts - The TS context obtained from TSCreate() 2965ad8e2604SHong Zhang - func - The function 2966ad8e2604SHong Zhang 2967ad8e2604SHong Zhang Calling sequence of func: 29680cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx); 296905755b9cSHong Zhang + t - current timestep 29700cf82b59SBarry Smith . y - input vector (current ODE solution) 297105755b9cSHong Zhang . A - output matrix 297205755b9cSHong Zhang - ctx - [optional] user-defined function context 2973ad8e2604SHong Zhang 2974ad8e2604SHong Zhang Level: intermediate 2975ad8e2604SHong Zhang 29760cf82b59SBarry 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 29770cf82b59SBarry Smith 2978ad8e2604SHong Zhang .keywords: TS, sensitivity 2979ad8e2604SHong Zhang .seealso: 2980ad8e2604SHong Zhang @*/ 29815bf8c567SBarry Smith PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 2982ad8e2604SHong Zhang { 2983ad8e2604SHong Zhang PetscErrorCode ierr; 2984ad8e2604SHong Zhang 2985ad8e2604SHong Zhang PetscFunctionBegin; 2986ad8e2604SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2987ad8e2604SHong Zhang if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 2988ad8e2604SHong Zhang 2989ad8e2604SHong Zhang ts->rhsjacobianp = func; 2990ad8e2604SHong Zhang ts->rhsjacobianpctx = ctx; 2991ad8e2604SHong Zhang if(Amat) { 2992ad8e2604SHong Zhang ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 2993ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2994ad8e2604SHong Zhang ts->Jacp = Amat; 2995ad8e2604SHong Zhang } 2996ad8e2604SHong Zhang PetscFunctionReturn(0); 2997ad8e2604SHong Zhang } 2998ad8e2604SHong Zhang 29990cf82b59SBarry Smith /*@C 30005bf8c567SBarry Smith TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function. 3001ad8e2604SHong Zhang 3002ad8e2604SHong Zhang Collective on TS 3003ad8e2604SHong Zhang 3004ad8e2604SHong Zhang Input Parameters: 3005ad8e2604SHong Zhang . ts - The TS context obtained from TSCreate() 3006ad8e2604SHong Zhang 3007ad8e2604SHong Zhang Level: developer 3008ad8e2604SHong Zhang 3009ad8e2604SHong Zhang .keywords: TS, sensitivity 30105bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian() 3011ad8e2604SHong Zhang @*/ 30125bf8c567SBarry Smith PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat) 3013ad8e2604SHong Zhang { 3014ad8e2604SHong Zhang PetscErrorCode ierr; 3015ad8e2604SHong Zhang 3016ad8e2604SHong Zhang PetscFunctionBegin; 3017ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3018ad8e2604SHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 3019ad8e2604SHong Zhang PetscValidPointer(Amat,4); 3020ad8e2604SHong Zhang 3021ad8e2604SHong Zhang PetscStackPush("TS user JacobianP function for sensitivity analysis"); 3022ad8e2604SHong Zhang ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 3023ad8e2604SHong Zhang PetscStackPop; 3024c235aa8dSHong Zhang PetscFunctionReturn(0); 3025c235aa8dSHong Zhang } 3026c235aa8dSHong Zhang 30276fd0887fSHong Zhang /*@C 3028dfb21088SHong Zhang TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions 30296fd0887fSHong Zhang 30306fd0887fSHong Zhang Logically Collective on TS 30316fd0887fSHong Zhang 30326fd0887fSHong Zhang Input Parameters: 30336fd0887fSHong Zhang + ts - the TS context obtained from TSCreate() 3034abc2977eSBarry Smith . numcost - number of gradients to be computed, this is the number of cost functions 30350cf82b59SBarry Smith . rf - routine for evaluating the integrand function 3036b612ec54SBarry Smith . drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y 3037b612ec54SBarry Smith . drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p 3038feaa1ddbSSatish Balay . fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run 3039b612ec54SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 30406fd0887fSHong Zhang 30410cf82b59SBarry Smith Calling sequence of rf: 30420cf82b59SBarry Smith $ rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx); 30436fd0887fSHong Zhang 30446fd0887fSHong Zhang + t - current timestep 30450cf82b59SBarry Smith . y - input vector 30460cf82b59SBarry Smith . f - function result; one vector entry for each cost function 30476fd0887fSHong Zhang - ctx - [optional] user-defined function context 30486fd0887fSHong Zhang 3049b612ec54SBarry Smith Calling sequence of drdyf: 30500cf82b59SBarry Smith $ PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx); 3051b612ec54SBarry Smith 3052b612ec54SBarry Smith Calling sequence of drdpf: 30530cf82b59SBarry Smith $ PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx); 3054b612ec54SBarry Smith 3055b612ec54SBarry Smith Level: intermediate 30566fd0887fSHong Zhang 3057abc2977eSBarry Smith Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions 30580cf82b59SBarry Smith 30596fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 30606fd0887fSHong Zhang 3061dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients() 30626fd0887fSHong Zhang @*/ 3063dfb21088SHong Zhang PetscErrorCode TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*), 3064d8151eeaSBarry Smith PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*), 3065b1cb36f3SHong Zhang PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*), 3066b1cb36f3SHong Zhang PetscBool fwd,void *ctx) 30676fd0887fSHong Zhang { 30686fd0887fSHong Zhang PetscErrorCode ierr; 30696fd0887fSHong Zhang 30706fd0887fSHong Zhang PetscFunctionBegin; 30716fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3072dfb21088SHong 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()"); 3073c72ed514SHong Zhang if (!ts->numcost) ts->numcost=numcost; 30746fd0887fSHong Zhang 3075b1cb36f3SHong Zhang ts->costintegralfwd = fwd; /* Evaluate the cost integral in forward run if fwd is true */ 3076abc2977eSBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr); 30772c39e106SBarry Smith ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr); 30780cf82b59SBarry Smith ts->costintegrand = rf; 307905755b9cSHong Zhang ts->costintegrandctx = ctx; 3080b612ec54SBarry Smith ts->drdyfunction = drdyf; 3081b612ec54SBarry Smith ts->drdpfunction = drdpf; 30826fd0887fSHong Zhang PetscFunctionReturn(0); 30836fd0887fSHong Zhang } 30846fd0887fSHong Zhang 308536eaed60SHong Zhang /*@ 3086dfb21088SHong Zhang TSGetCostIntegral - Returns the values of the integral term in the cost functions. 308736eaed60SHong Zhang It is valid to call the routine after a backward run. 308836eaed60SHong Zhang 308936eaed60SHong Zhang Not Collective 309036eaed60SHong Zhang 309136eaed60SHong Zhang Input Parameter: 309236eaed60SHong Zhang . ts - the TS context obtained from TSCreate() 309336eaed60SHong Zhang 309436eaed60SHong Zhang Output Parameter: 30952c39e106SBarry Smith . v - the vector containing the integrals for each cost function 309636eaed60SHong Zhang 309736eaed60SHong Zhang Level: intermediate 309836eaed60SHong Zhang 3099dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 310036eaed60SHong Zhang 310136eaed60SHong Zhang .keywords: TS, sensitivity analysis 310236eaed60SHong Zhang @*/ 3103dfb21088SHong Zhang PetscErrorCode TSGetCostIntegral(TS ts,Vec *v) 310436eaed60SHong Zhang { 310536eaed60SHong Zhang PetscFunctionBegin; 310636eaed60SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 310736eaed60SHong Zhang PetscValidPointer(v,2); 31082c39e106SBarry Smith *v = ts->vec_costintegral; 310936eaed60SHong Zhang PetscFunctionReturn(0); 311036eaed60SHong Zhang } 311136eaed60SHong Zhang 31126fd0887fSHong Zhang /*@ 31132c39e106SBarry Smith TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions. 31146fd0887fSHong Zhang 31156fd0887fSHong Zhang Input Parameters: 31166fd0887fSHong Zhang + ts - the TS context 31176fd0887fSHong Zhang . t - current time 31180cf82b59SBarry Smith - y - state vector, i.e. current solution 31196fd0887fSHong Zhang 31206fd0887fSHong Zhang Output Parameter: 3121abc2977eSBarry Smith . q - vector of size numcost to hold the outputs 31226fd0887fSHong Zhang 31236fd0887fSHong Zhang Note: 31246fd0887fSHong Zhang Most users should not need to explicitly call this routine, as it 31256fd0887fSHong Zhang is used internally within the sensitivity analysis context. 31266fd0887fSHong Zhang 31276fd0887fSHong Zhang Level: developer 31286fd0887fSHong Zhang 31296fd0887fSHong Zhang .keywords: TS, compute 31306fd0887fSHong Zhang 3131dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 31326fd0887fSHong Zhang @*/ 31330cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q) 31346fd0887fSHong Zhang { 31356fd0887fSHong Zhang PetscErrorCode ierr; 31366fd0887fSHong Zhang 31376fd0887fSHong Zhang PetscFunctionBegin; 31386fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31390cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 31406fd0887fSHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,4); 31416fd0887fSHong Zhang 31420cf82b59SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 3143e4680132SHong Zhang if (ts->costintegrand) { 3144e4680132SHong Zhang PetscStackPush("TS user integrand in the cost function"); 31450cf82b59SBarry Smith ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr); 31466fd0887fSHong Zhang PetscStackPop; 31476fd0887fSHong Zhang } else { 31486fd0887fSHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 31496fd0887fSHong Zhang } 31506fd0887fSHong Zhang 31510cf82b59SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 31526fd0887fSHong Zhang PetscFunctionReturn(0); 31536fd0887fSHong Zhang } 31546fd0887fSHong Zhang 315505755b9cSHong Zhang /*@ 3156d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 315705755b9cSHong Zhang 315805755b9cSHong Zhang Collective on TS 315905755b9cSHong Zhang 316005755b9cSHong Zhang Input Parameters: 316105755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 316205755b9cSHong Zhang 316305755b9cSHong Zhang Notes: 3164d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 316505755b9cSHong Zhang so most users would not generally call this routine themselves. 316605755b9cSHong Zhang 316705755b9cSHong Zhang Level: developer 316805755b9cSHong Zhang 316905755b9cSHong Zhang .keywords: TS, sensitivity 3170d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction() 317105755b9cSHong Zhang @*/ 31720cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy) 317305755b9cSHong Zhang { 317405755b9cSHong Zhang PetscErrorCode ierr; 317505755b9cSHong Zhang 317605755b9cSHong Zhang PetscFunctionBegin; 317705755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31780cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 317905755b9cSHong Zhang 318005755b9cSHong Zhang PetscStackPush("TS user DRDY function for sensitivity analysis"); 31810cf82b59SBarry Smith ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr); 318205755b9cSHong Zhang PetscStackPop; 318305755b9cSHong Zhang PetscFunctionReturn(0); 318405755b9cSHong Zhang } 318505755b9cSHong Zhang 318605755b9cSHong Zhang /*@ 3187d4aa0a58SBarry Smith TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 318805755b9cSHong Zhang 318905755b9cSHong Zhang Collective on TS 319005755b9cSHong Zhang 319105755b9cSHong Zhang Input Parameters: 319205755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 319305755b9cSHong Zhang 319405755b9cSHong Zhang Notes: 319505755b9cSHong Zhang TSDRDPFunction() is typically used for sensitivity implementation, 319605755b9cSHong Zhang so most users would not generally call this routine themselves. 319705755b9cSHong Zhang 319805755b9cSHong Zhang Level: developer 319905755b9cSHong Zhang 320005755b9cSHong Zhang .keywords: TS, sensitivity 3201d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction() 320205755b9cSHong Zhang @*/ 32030cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp) 320405755b9cSHong Zhang { 320505755b9cSHong Zhang PetscErrorCode ierr; 320605755b9cSHong Zhang 320705755b9cSHong Zhang PetscFunctionBegin; 320805755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 32090cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 321005755b9cSHong Zhang 321105755b9cSHong Zhang PetscStackPush("TS user DRDP function for sensitivity analysis"); 32120cf82b59SBarry Smith ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr); 321305755b9cSHong Zhang PetscStackPop; 321405755b9cSHong Zhang PetscFunctionReturn(0); 321505755b9cSHong Zhang } 321605755b9cSHong Zhang 3217ac226902SBarry Smith /*@C 3218000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 32193f2090d5SJed Brown called once at the beginning of each time step. 3220000e7ae3SMatthew Knepley 32213f9fe445SBarry Smith Logically Collective on TS 3222000e7ae3SMatthew Knepley 3223000e7ae3SMatthew Knepley Input Parameters: 3224000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3225000e7ae3SMatthew Knepley - func - The function 3226000e7ae3SMatthew Knepley 3227000e7ae3SMatthew Knepley Calling sequence of func: 3228000e7ae3SMatthew Knepley . func (TS ts); 3229000e7ae3SMatthew Knepley 3230000e7ae3SMatthew Knepley Level: intermediate 3231000e7ae3SMatthew Knepley 3232000e7ae3SMatthew Knepley .keywords: TS, timestep 32339be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 3234000e7ae3SMatthew Knepley @*/ 32357087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3236000e7ae3SMatthew Knepley { 3237000e7ae3SMatthew Knepley PetscFunctionBegin; 32380700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3239ae60f76fSBarry Smith ts->prestep = func; 3240000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3241000e7ae3SMatthew Knepley } 3242000e7ae3SMatthew Knepley 324309ee8438SJed Brown /*@ 32443f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 32453f2090d5SJed Brown 32463f2090d5SJed Brown Collective on TS 32473f2090d5SJed Brown 32483f2090d5SJed Brown Input Parameters: 32493f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 32503f2090d5SJed Brown 32513f2090d5SJed Brown Notes: 32523f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 32533f2090d5SJed Brown so most users would not generally call this routine themselves. 32543f2090d5SJed Brown 32553f2090d5SJed Brown Level: developer 32563f2090d5SJed Brown 32573f2090d5SJed Brown .keywords: TS, timestep 32589be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 32593f2090d5SJed Brown @*/ 32607087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 32613f2090d5SJed Brown { 32623f2090d5SJed Brown PetscErrorCode ierr; 32633f2090d5SJed Brown 32643f2090d5SJed Brown PetscFunctionBegin; 32650700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3266ae60f76fSBarry Smith if (ts->prestep) { 3267ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 3268312ce896SJed Brown } 32693f2090d5SJed Brown PetscFunctionReturn(0); 32703f2090d5SJed Brown } 32713f2090d5SJed Brown 3272b8123daeSJed Brown /*@C 3273b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3274b8123daeSJed Brown called once at the beginning of each stage. 3275b8123daeSJed Brown 3276b8123daeSJed Brown Logically Collective on TS 3277b8123daeSJed Brown 3278b8123daeSJed Brown Input Parameters: 3279b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 3280b8123daeSJed Brown - func - The function 3281b8123daeSJed Brown 3282b8123daeSJed Brown Calling sequence of func: 3283b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 3284b8123daeSJed Brown 3285b8123daeSJed Brown Level: intermediate 3286b8123daeSJed Brown 3287b8123daeSJed Brown Note: 3288b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 3289b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 3290b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 3291b8123daeSJed Brown 3292b8123daeSJed Brown .keywords: TS, timestep 32939be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3294b8123daeSJed Brown @*/ 3295b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 3296b8123daeSJed Brown { 3297b8123daeSJed Brown PetscFunctionBegin; 3298b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3299ae60f76fSBarry Smith ts->prestage = func; 3300b8123daeSJed Brown PetscFunctionReturn(0); 3301b8123daeSJed Brown } 3302b8123daeSJed Brown 33039be3e283SDebojyoti Ghosh /*@C 33049be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 33059be3e283SDebojyoti Ghosh called once at the end of each stage. 33069be3e283SDebojyoti Ghosh 33079be3e283SDebojyoti Ghosh Logically Collective on TS 33089be3e283SDebojyoti Ghosh 33099be3e283SDebojyoti Ghosh Input Parameters: 33109be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 33119be3e283SDebojyoti Ghosh - func - The function 33129be3e283SDebojyoti Ghosh 33139be3e283SDebojyoti Ghosh Calling sequence of func: 33149be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 33159be3e283SDebojyoti Ghosh 33169be3e283SDebojyoti Ghosh Level: intermediate 33179be3e283SDebojyoti Ghosh 33189be3e283SDebojyoti Ghosh Note: 33199be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 33209be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 33219be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 33229be3e283SDebojyoti Ghosh 33239be3e283SDebojyoti Ghosh .keywords: TS, timestep 33249be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 33259be3e283SDebojyoti Ghosh @*/ 33269be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 33279be3e283SDebojyoti Ghosh { 33289be3e283SDebojyoti Ghosh PetscFunctionBegin; 33299be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 33309be3e283SDebojyoti Ghosh ts->poststage = func; 33319be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 33329be3e283SDebojyoti Ghosh } 33339be3e283SDebojyoti Ghosh 3334c688d042SShri Abhyankar /*@C 3335c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 3336c688d042SShri Abhyankar called once at the end of each step evaluation. 3337c688d042SShri Abhyankar 3338c688d042SShri Abhyankar Logically Collective on TS 3339c688d042SShri Abhyankar 3340c688d042SShri Abhyankar Input Parameters: 3341c688d042SShri Abhyankar + ts - The TS context obtained from TSCreate() 3342c688d042SShri Abhyankar - func - The function 3343c688d042SShri Abhyankar 3344c688d042SShri Abhyankar Calling sequence of func: 3345c688d042SShri Abhyankar . PetscErrorCode func(TS ts); 3346c688d042SShri Abhyankar 3347c688d042SShri Abhyankar Level: intermediate 3348c688d042SShri Abhyankar 3349c688d042SShri Abhyankar Note: 33501785ff2aSShri Abhyankar Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling 33511785ff2aSShri Abhyankar thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep() 3352e7e94ed4SShri Abhyankar may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step 3353e7e94ed4SShri Abhyankar solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step 3354e7e94ed4SShri Abhyankar with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime() 3355c688d042SShri Abhyankar 3356c688d042SShri Abhyankar .keywords: TS, timestep 3357c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3358c688d042SShri Abhyankar @*/ 3359c688d042SShri Abhyankar PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS)) 3360c688d042SShri Abhyankar { 3361c688d042SShri Abhyankar PetscFunctionBegin; 3362c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3363c688d042SShri Abhyankar ts->postevaluate = func; 3364c688d042SShri Abhyankar PetscFunctionReturn(0); 3365c688d042SShri Abhyankar } 3366c688d042SShri Abhyankar 3367b8123daeSJed Brown /*@ 3368b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 3369b8123daeSJed Brown 3370b8123daeSJed Brown Collective on TS 3371b8123daeSJed Brown 3372b8123daeSJed Brown Input Parameters: 3373b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 33749be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3375b8123daeSJed Brown 3376b8123daeSJed Brown Notes: 3377b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 3378b8123daeSJed Brown most users would not generally call this routine themselves. 3379b8123daeSJed Brown 3380b8123daeSJed Brown Level: developer 3381b8123daeSJed Brown 3382b8123daeSJed Brown .keywords: TS, timestep 33839be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 3384b8123daeSJed Brown @*/ 3385b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3386b8123daeSJed Brown { 3387b8123daeSJed Brown PetscErrorCode ierr; 3388b8123daeSJed Brown 3389b8123daeSJed Brown PetscFunctionBegin; 3390b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3391ae60f76fSBarry Smith if (ts->prestage) { 3392ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 3393b8123daeSJed Brown } 3394b8123daeSJed Brown PetscFunctionReturn(0); 3395b8123daeSJed Brown } 3396b8123daeSJed Brown 33979be3e283SDebojyoti Ghosh /*@ 33989be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 33999be3e283SDebojyoti Ghosh 34009be3e283SDebojyoti Ghosh Collective on TS 34019be3e283SDebojyoti Ghosh 34029be3e283SDebojyoti Ghosh Input Parameters: 34039be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 34049be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 34059be3e283SDebojyoti Ghosh stageindex - Stage number 34069be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 34079be3e283SDebojyoti Ghosh of stages) with the stage solutions 34089be3e283SDebojyoti Ghosh 34099be3e283SDebojyoti Ghosh Notes: 34109be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 34119be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 34129be3e283SDebojyoti Ghosh 34139be3e283SDebojyoti Ghosh Level: developer 34149be3e283SDebojyoti Ghosh 34159be3e283SDebojyoti Ghosh .keywords: TS, timestep 34169be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 34179be3e283SDebojyoti Ghosh @*/ 34189be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 34199be3e283SDebojyoti Ghosh { 34209be3e283SDebojyoti Ghosh PetscErrorCode ierr; 34219be3e283SDebojyoti Ghosh 34229be3e283SDebojyoti Ghosh PetscFunctionBegin; 34239be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 34244beae5d8SLisandro Dalcin if (ts->poststage) { 34259be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 34269be3e283SDebojyoti Ghosh } 34279be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 34289be3e283SDebojyoti Ghosh } 34299be3e283SDebojyoti Ghosh 3430c688d042SShri Abhyankar /*@ 3431c688d042SShri Abhyankar TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate() 3432c688d042SShri Abhyankar 3433c688d042SShri Abhyankar Collective on TS 3434c688d042SShri Abhyankar 3435c688d042SShri Abhyankar Input Parameters: 3436c688d042SShri Abhyankar . ts - The TS context obtained from TSCreate() 3437c688d042SShri Abhyankar 3438c688d042SShri Abhyankar Notes: 3439c688d042SShri Abhyankar TSPostEvaluate() is typically used within time stepping implementations, 3440c688d042SShri Abhyankar most users would not generally call this routine themselves. 3441c688d042SShri Abhyankar 3442c688d042SShri Abhyankar Level: developer 3443c688d042SShri Abhyankar 3444c688d042SShri Abhyankar .keywords: TS, timestep 3445c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep() 3446c688d042SShri Abhyankar @*/ 3447c688d042SShri Abhyankar PetscErrorCode TSPostEvaluate(TS ts) 3448c688d042SShri Abhyankar { 3449c688d042SShri Abhyankar PetscErrorCode ierr; 3450c688d042SShri Abhyankar 3451c688d042SShri Abhyankar PetscFunctionBegin; 3452c688d042SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3453c688d042SShri Abhyankar if (ts->postevaluate) { 3454c688d042SShri Abhyankar PetscStackCallStandard((*ts->postevaluate),(ts)); 3455c688d042SShri Abhyankar } 3456c688d042SShri Abhyankar PetscFunctionReturn(0); 3457c688d042SShri Abhyankar } 3458c688d042SShri Abhyankar 3459ac226902SBarry Smith /*@C 3460000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 34613f2090d5SJed Brown called once at the end of each time step. 3462000e7ae3SMatthew Knepley 34633f9fe445SBarry Smith Logically Collective on TS 3464000e7ae3SMatthew Knepley 3465000e7ae3SMatthew Knepley Input Parameters: 3466000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3467000e7ae3SMatthew Knepley - func - The function 3468000e7ae3SMatthew Knepley 3469000e7ae3SMatthew Knepley Calling sequence of func: 3470b8123daeSJed Brown $ func (TS ts); 3471000e7ae3SMatthew Knepley 34721785ff2aSShri Abhyankar Notes: 34731785ff2aSShri Abhyankar The function set by TSSetPostStep() is called after each successful step. The solution vector X 34741785ff2aSShri Abhyankar obtained by TSGetSolution() may be different than that computed at the step end if the event handler 34751785ff2aSShri Abhyankar locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead. 34761785ff2aSShri Abhyankar 3477000e7ae3SMatthew Knepley Level: intermediate 3478000e7ae3SMatthew Knepley 3479000e7ae3SMatthew Knepley .keywords: TS, timestep 34801785ff2aSShri Abhyankar .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 3481000e7ae3SMatthew Knepley @*/ 34827087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 3483000e7ae3SMatthew Knepley { 3484000e7ae3SMatthew Knepley PetscFunctionBegin; 34850700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3486ae60f76fSBarry Smith ts->poststep = func; 3487000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3488000e7ae3SMatthew Knepley } 3489000e7ae3SMatthew Knepley 349009ee8438SJed Brown /*@ 34913f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 34923f2090d5SJed Brown 34933f2090d5SJed Brown Collective on TS 34943f2090d5SJed Brown 34953f2090d5SJed Brown Input Parameters: 34963f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 34973f2090d5SJed Brown 34983f2090d5SJed Brown Notes: 34993f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 35003f2090d5SJed Brown so most users would not generally call this routine themselves. 35013f2090d5SJed Brown 35023f2090d5SJed Brown Level: developer 35033f2090d5SJed Brown 35043f2090d5SJed Brown .keywords: TS, timestep 35053f2090d5SJed Brown @*/ 35067087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 35073f2090d5SJed Brown { 35083f2090d5SJed Brown PetscErrorCode ierr; 35093f2090d5SJed Brown 35103f2090d5SJed Brown PetscFunctionBegin; 35110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3512ae60f76fSBarry Smith if (ts->poststep) { 3513ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 351472ac3e02SJed Brown } 35153f2090d5SJed Brown PetscFunctionReturn(0); 35163f2090d5SJed Brown } 35173f2090d5SJed Brown 3518d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 3519d763cef2SBarry Smith 3520d763cef2SBarry Smith /*@C 3521a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 3522d763cef2SBarry Smith timestep to display the iteration's progress. 3523d763cef2SBarry Smith 35243f9fe445SBarry Smith Logically Collective on TS 3525d763cef2SBarry Smith 3526d763cef2SBarry Smith Input Parameters: 3527d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3528e213d8f1SJed Brown . monitor - monitoring routine 3529329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 35300298fd71SBarry Smith monitor routine (use NULL if no context is desired) 3531b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 35320298fd71SBarry Smith (may be NULL) 3533d763cef2SBarry Smith 3534e213d8f1SJed Brown Calling sequence of monitor: 35350910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 3536d763cef2SBarry Smith 3537d763cef2SBarry Smith + ts - the TS context 353863e21af5SBarry 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) 35391f06c33eSBarry Smith . time - current time 35400910c330SBarry Smith . u - current iterate 3541d763cef2SBarry Smith - mctx - [optional] monitoring context 3542d763cef2SBarry Smith 3543d763cef2SBarry Smith Notes: 3544d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 3545d763cef2SBarry Smith already has been loaded. 3546d763cef2SBarry Smith 3547025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 3548025f1a04SBarry Smith 3549d763cef2SBarry Smith Level: intermediate 3550d763cef2SBarry Smith 3551d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3552d763cef2SBarry Smith 3553a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 3554d763cef2SBarry Smith @*/ 3555c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 3556d763cef2SBarry Smith { 355778064530SBarry Smith PetscErrorCode ierr; 355878064530SBarry Smith PetscInt i; 355978064530SBarry Smith PetscBool identical; 356078064530SBarry Smith 3561d763cef2SBarry Smith PetscFunctionBegin; 35620700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 356378064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 356478064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr); 356578064530SBarry Smith if (identical) PetscFunctionReturn(0); 356678064530SBarry Smith } 356717186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 3568d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 35698704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 3570d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 3571d763cef2SBarry Smith PetscFunctionReturn(0); 3572d763cef2SBarry Smith } 3573d763cef2SBarry Smith 3574d763cef2SBarry Smith /*@C 3575a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 3576d763cef2SBarry Smith 35773f9fe445SBarry Smith Logically Collective on TS 3578d763cef2SBarry Smith 3579d763cef2SBarry Smith Input Parameters: 3580d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3581d763cef2SBarry Smith 3582d763cef2SBarry Smith Notes: 3583d763cef2SBarry Smith There is no way to remove a single, specific monitor. 3584d763cef2SBarry Smith 3585d763cef2SBarry Smith Level: intermediate 3586d763cef2SBarry Smith 3587d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3588d763cef2SBarry Smith 3589a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 3590d763cef2SBarry Smith @*/ 35917087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 3592d763cef2SBarry Smith { 3593d952e501SBarry Smith PetscErrorCode ierr; 3594d952e501SBarry Smith PetscInt i; 3595d952e501SBarry Smith 3596d763cef2SBarry Smith PetscFunctionBegin; 35970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3598d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 35998704b422SBarry Smith if (ts->monitordestroy[i]) { 36008704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 3601d952e501SBarry Smith } 3602d952e501SBarry Smith } 3603d763cef2SBarry Smith ts->numbermonitors = 0; 3604d763cef2SBarry Smith PetscFunctionReturn(0); 3605d763cef2SBarry Smith } 3606d763cef2SBarry Smith 3607721cd6eeSBarry Smith /*@C 3608721cd6eeSBarry Smith TSMonitorDefault - The Default monitor, prints the timestep and time for each step 36095516499fSSatish Balay 36105516499fSSatish Balay Level: intermediate 361141251cbbSSatish Balay 36125516499fSSatish Balay .keywords: TS, set, monitor 36135516499fSSatish Balay 361463e21af5SBarry Smith .seealso: TSMonitorSet() 361541251cbbSSatish Balay @*/ 3616721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf) 3617d763cef2SBarry Smith { 3618dfbe8321SBarry Smith PetscErrorCode ierr; 3619721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 362041aca3d6SBarry Smith PetscBool iascii,ibinary; 3621d132466eSBarry Smith 3622d763cef2SBarry Smith PetscFunctionBegin; 36234d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 362441aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 362541aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr); 3626721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 362741aca3d6SBarry Smith if (iascii) { 3628649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 362963e21af5SBarry Smith if (step == -1){ /* this indicates it is an interpolated solution */ 363063e21af5SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr); 363163e21af5SBarry Smith } else { 36328392e04aSShri 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); 363363e21af5SBarry Smith } 3634649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 363541aca3d6SBarry Smith } else if (ibinary) { 363641aca3d6SBarry Smith PetscMPIInt rank; 363741aca3d6SBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 363841aca3d6SBarry Smith if (!rank) { 3639450a797fSBarry Smith PetscBool skipHeader; 3640450a797fSBarry Smith PetscInt classid = REAL_FILE_CLASSID; 3641450a797fSBarry Smith 3642450a797fSBarry Smith ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr); 3643450a797fSBarry Smith if (!skipHeader) { 3644450a797fSBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 3645450a797fSBarry Smith } 364641aca3d6SBarry Smith ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr); 364741aca3d6SBarry Smith } else { 364841aca3d6SBarry Smith ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr); 364941aca3d6SBarry Smith } 365041aca3d6SBarry Smith } 3651721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3652d763cef2SBarry Smith PetscFunctionReturn(0); 3653d763cef2SBarry Smith } 3654d763cef2SBarry Smith 36559110b2e7SHong Zhang /*@C 36569110b2e7SHong Zhang TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every 36579110b2e7SHong Zhang timestep to display the iteration's progress. 36589110b2e7SHong Zhang 36599110b2e7SHong Zhang Logically Collective on TS 36609110b2e7SHong Zhang 36619110b2e7SHong Zhang Input Parameters: 36629110b2e7SHong Zhang + ts - the TS context obtained from TSCreate() 36639110b2e7SHong Zhang . adjointmonitor - monitoring routine 36649110b2e7SHong Zhang . adjointmctx - [optional] user-defined context for private data for the 36659110b2e7SHong Zhang monitor routine (use NULL if no context is desired) 36669110b2e7SHong Zhang - adjointmonitordestroy - [optional] routine that frees monitor context 36679110b2e7SHong Zhang (may be NULL) 36689110b2e7SHong Zhang 36699110b2e7SHong Zhang Calling sequence of monitor: 36709110b2e7SHong Zhang $ int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx) 36719110b2e7SHong Zhang 36729110b2e7SHong Zhang + ts - the TS context 36739110b2e7SHong 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 36749110b2e7SHong Zhang been interpolated to) 36759110b2e7SHong Zhang . time - current time 36769110b2e7SHong Zhang . u - current iterate 36779110b2e7SHong Zhang . numcost - number of cost functionos 36789110b2e7SHong Zhang . lambda - sensitivities to initial conditions 36799110b2e7SHong Zhang . mu - sensitivities to parameters 36809110b2e7SHong Zhang - adjointmctx - [optional] adjoint monitoring context 36819110b2e7SHong Zhang 36829110b2e7SHong Zhang Notes: 36839110b2e7SHong Zhang This routine adds an additional monitor to the list of monitors that 36849110b2e7SHong Zhang already has been loaded. 36859110b2e7SHong Zhang 36869110b2e7SHong Zhang Fortran notes: Only a single monitor function can be set for each TS object 36879110b2e7SHong Zhang 36889110b2e7SHong Zhang Level: intermediate 36899110b2e7SHong Zhang 36909110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 36919110b2e7SHong Zhang 36929110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel() 36939110b2e7SHong Zhang @*/ 36949110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**)) 36959110b2e7SHong Zhang { 369678064530SBarry Smith PetscErrorCode ierr; 369778064530SBarry Smith PetscInt i; 369878064530SBarry Smith PetscBool identical; 369978064530SBarry Smith 37009110b2e7SHong Zhang PetscFunctionBegin; 37019110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 370278064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 370378064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);CHKERRQ(ierr); 370478064530SBarry Smith if (identical) PetscFunctionReturn(0); 370578064530SBarry Smith } 37069110b2e7SHong Zhang if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set"); 37079110b2e7SHong Zhang ts->adjointmonitor[ts->numberadjointmonitors] = adjointmonitor; 37089110b2e7SHong Zhang ts->adjointmonitordestroy[ts->numberadjointmonitors] = adjointmdestroy; 37099110b2e7SHong Zhang ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx; 37109110b2e7SHong Zhang PetscFunctionReturn(0); 37119110b2e7SHong Zhang } 37129110b2e7SHong Zhang 37139110b2e7SHong Zhang /*@C 37149110b2e7SHong Zhang TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object. 37159110b2e7SHong Zhang 37169110b2e7SHong Zhang Logically Collective on TS 37179110b2e7SHong Zhang 37189110b2e7SHong Zhang Input Parameters: 37199110b2e7SHong Zhang . ts - the TS context obtained from TSCreate() 37209110b2e7SHong Zhang 37219110b2e7SHong Zhang Notes: 37229110b2e7SHong Zhang There is no way to remove a single, specific monitor. 37239110b2e7SHong Zhang 37249110b2e7SHong Zhang Level: intermediate 37259110b2e7SHong Zhang 37269110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 37279110b2e7SHong Zhang 37289110b2e7SHong Zhang .seealso: TSAdjointMonitorSet() 37299110b2e7SHong Zhang @*/ 37309110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorCancel(TS ts) 37319110b2e7SHong Zhang { 37329110b2e7SHong Zhang PetscErrorCode ierr; 37339110b2e7SHong Zhang PetscInt i; 37349110b2e7SHong Zhang 37359110b2e7SHong Zhang PetscFunctionBegin; 37369110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 37379110b2e7SHong Zhang for (i=0; i<ts->numberadjointmonitors; i++) { 37389110b2e7SHong Zhang if (ts->adjointmonitordestroy[i]) { 37399110b2e7SHong Zhang ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 37409110b2e7SHong Zhang } 37419110b2e7SHong Zhang } 37429110b2e7SHong Zhang ts->numberadjointmonitors = 0; 37439110b2e7SHong Zhang PetscFunctionReturn(0); 37449110b2e7SHong Zhang } 37459110b2e7SHong Zhang 3746721cd6eeSBarry Smith /*@C 3747721cd6eeSBarry Smith TSAdjointMonitorDefault - the default monitor of adjoint computations 3748110eb670SHong Zhang 3749110eb670SHong Zhang Level: intermediate 3750110eb670SHong Zhang 3751110eb670SHong Zhang .keywords: TS, set, monitor 3752110eb670SHong Zhang 3753110eb670SHong Zhang .seealso: TSAdjointMonitorSet() 3754110eb670SHong Zhang @*/ 3755721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf) 3756110eb670SHong Zhang { 3757110eb670SHong Zhang PetscErrorCode ierr; 3758721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 3759110eb670SHong Zhang 3760110eb670SHong Zhang PetscFunctionBegin; 3761110eb670SHong Zhang PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3762721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 3763110eb670SHong Zhang ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3764110eb670SHong 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); 3765110eb670SHong Zhang ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3766721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3767110eb670SHong Zhang PetscFunctionReturn(0); 3768110eb670SHong Zhang } 3769110eb670SHong Zhang 3770cd652676SJed Brown /*@ 3771cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3772cd652676SJed Brown 3773cd652676SJed Brown Collective on TS 3774cd652676SJed Brown 3775cd652676SJed Brown Input Argument: 3776cd652676SJed Brown + ts - time stepping context 3777cd652676SJed Brown - t - time to interpolate to 3778cd652676SJed Brown 3779cd652676SJed Brown Output Argument: 37800910c330SBarry Smith . U - state at given time 3781cd652676SJed Brown 3782cd652676SJed Brown Level: intermediate 3783cd652676SJed Brown 3784cd652676SJed Brown Developer Notes: 3785cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3786cd652676SJed Brown 3787cd652676SJed Brown .keywords: TS, set 3788cd652676SJed Brown 3789874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve() 3790cd652676SJed Brown @*/ 37910910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3792cd652676SJed Brown { 3793cd652676SJed Brown PetscErrorCode ierr; 3794cd652676SJed Brown 3795cd652676SJed Brown PetscFunctionBegin; 3796cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3797b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3798be5899b3SLisandro 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); 3799ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 38000910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3801cd652676SJed Brown PetscFunctionReturn(0); 3802cd652676SJed Brown } 3803cd652676SJed Brown 3804d763cef2SBarry Smith /*@ 38056d9e5789SSean Farley TSStep - Steps one time step 3806d763cef2SBarry Smith 3807d763cef2SBarry Smith Collective on TS 3808d763cef2SBarry Smith 3809d763cef2SBarry Smith Input Parameter: 3810d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3811d763cef2SBarry Smith 381227829d71SBarry Smith Level: developer 3813d763cef2SBarry Smith 3814b8123daeSJed Brown Notes: 381527829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 381627829d71SBarry Smith 3817b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3818b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3819b8123daeSJed Brown 382025cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 382125cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 382225cb2221SBarry Smith 3823d763cef2SBarry Smith .keywords: TS, timestep, solve 3824d763cef2SBarry Smith 38259be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3826d763cef2SBarry Smith @*/ 3827193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3828d763cef2SBarry Smith { 3829dfbe8321SBarry Smith PetscErrorCode ierr; 3830fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3831be5899b3SLisandro Dalcin PetscReal ptime; 3832d763cef2SBarry Smith 3833d763cef2SBarry Smith PetscFunctionBegin; 38340700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3835fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3836fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3837fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3838fffbeea8SBarry Smith " type = {Preprint},\n" 3839fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3840fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3841302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3842fffbeea8SBarry Smith 3843d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 384468bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3845d405a339SMatthew Knepley 3846a6772fa2SLisandro 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()"); 3847be5899b3SLisandro 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"); 3848a6772fa2SLisandro Dalcin 3849be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 3850362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3851be5899b3SLisandro Dalcin ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev; 3852e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3853d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3854193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3855d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3856be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 3857be5899b3SLisandro Dalcin ts->steps++; ts->total_steps++; 3858be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 385928d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3860362cd11cSLisandro Dalcin 3861362cd11cSLisandro Dalcin if (ts->reason < 0) { 3862cef5090cSJed Brown if (ts->errorifstepfailed) { 386308c7845fSBarry 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]); 386408c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3865d2daff3dSHong Zhang } 386608c7845fSBarry Smith } else if (!ts->reason) { 386708c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 386808c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 386908c7845fSBarry Smith } 387008c7845fSBarry Smith PetscFunctionReturn(0); 387108c7845fSBarry Smith } 387208c7845fSBarry Smith 387308c7845fSBarry Smith /*@ 3874b957a604SHong Zhang TSAdjointStep - Steps one time step backward in the adjoint run 387508c7845fSBarry Smith 387608c7845fSBarry Smith Collective on TS 387708c7845fSBarry Smith 387808c7845fSBarry Smith Input Parameter: 387908c7845fSBarry Smith . ts - the TS context obtained from TSCreate() 388008c7845fSBarry Smith 38816a4d4014SLisandro Dalcin Level: intermediate 38826a4d4014SLisandro Dalcin 3883b957a604SHong Zhang .keywords: TS, adjoint, step 38846a4d4014SLisandro Dalcin 3885b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve() 38866a4d4014SLisandro Dalcin @*/ 388708c7845fSBarry Smith PetscErrorCode TSAdjointStep(TS ts) 38886a4d4014SLisandro Dalcin { 388908c7845fSBarry Smith DM dm; 38906a4d4014SLisandro Dalcin PetscErrorCode ierr; 38916a4d4014SLisandro Dalcin 38926a4d4014SLisandro Dalcin PetscFunctionBegin; 38934a2ae208SSatish Balay PetscValidHeaderSpecific(ts,TS_CLASSID,1); 389408c7845fSBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 389508c7845fSBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3896d763cef2SBarry Smith 3897685405a1SBarry Smith ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr); 3898d763cef2SBarry Smith 3899be5899b3SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3900be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime; 390142f2b339SBarry 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); 3902be5899b3SLisandro Dalcin ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 390342f2b339SBarry Smith ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr); 39048d0ad7a8SHong Zhang ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 3905be5899b3SLisandro Dalcin ts->steps++; ts->total_steps--; 3906362cd11cSLisandro Dalcin 3907362cd11cSLisandro Dalcin if (ts->reason < 0) { 3908cef5090cSJed Brown if (ts->errorifstepfailed) { 39096c4ed002SBarry 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]); 39106c4ed002SBarry 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]); 39116c4ed002SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3912cef5090cSJed Brown } 3913362cd11cSLisandro Dalcin } else if (!ts->reason) { 391473b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3915362cd11cSLisandro Dalcin } 3916d763cef2SBarry Smith PetscFunctionReturn(0); 3917d763cef2SBarry Smith } 3918d763cef2SBarry Smith 39197cbde773SLisandro Dalcin /*@ 39207cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 39217cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 39227cbde773SLisandro Dalcin 39237cbde773SLisandro Dalcin Collective on TS 39247cbde773SLisandro Dalcin 39257cbde773SLisandro Dalcin Input Arguments: 39267cbde773SLisandro Dalcin + ts - time stepping context 39277cbde773SLisandro Dalcin . wnormtype - norm type, either NORM_2 or NORM_INFINITY 39287cbde773SLisandro Dalcin - order - optional, desired order for the error evaluation or PETSC_DECIDE 39297cbde773SLisandro Dalcin 39307cbde773SLisandro Dalcin Output Arguments: 39317cbde773SLisandro Dalcin + order - optional, the actual order of the error evaluation 39327cbde773SLisandro Dalcin - wlte - the weighted local truncation error norm 39337cbde773SLisandro Dalcin 39347cbde773SLisandro Dalcin Level: advanced 39357cbde773SLisandro Dalcin 39367cbde773SLisandro Dalcin Notes: 39377cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 39387cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 39397cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 39407cbde773SLisandro Dalcin 39417cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm() 39427cbde773SLisandro Dalcin @*/ 39437cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte) 39447cbde773SLisandro Dalcin { 39457cbde773SLisandro Dalcin PetscErrorCode ierr; 39467cbde773SLisandro Dalcin 39477cbde773SLisandro Dalcin PetscFunctionBegin; 39487cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 39497cbde773SLisandro Dalcin PetscValidType(ts,1); 39507cbde773SLisandro Dalcin PetscValidLogicalCollectiveEnum(ts,wnormtype,4); 39517cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order,3); 39527cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts,*order,3); 39537cbde773SLisandro Dalcin PetscValidRealPointer(wlte,4); 39547cbde773SLisandro Dalcin if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 39557cbde773SLisandro Dalcin if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name); 39567cbde773SLisandro Dalcin ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr); 39577cbde773SLisandro Dalcin PetscFunctionReturn(0); 39587cbde773SLisandro Dalcin } 39597cbde773SLisandro Dalcin 396005175c85SJed Brown /*@ 396105175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 396205175c85SJed Brown 39631c3436cfSJed Brown Collective on TS 396405175c85SJed Brown 396505175c85SJed Brown Input Arguments: 39661c3436cfSJed Brown + ts - time stepping context 39671c3436cfSJed Brown . order - desired order of accuracy 39680298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 396905175c85SJed Brown 397005175c85SJed Brown Output Arguments: 39710910c330SBarry Smith . U - state at the end of the current step 397205175c85SJed Brown 397305175c85SJed Brown Level: advanced 397405175c85SJed Brown 3975108c343cSJed Brown Notes: 3976108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3977108c343cSJed 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. 3978108c343cSJed Brown 39791c3436cfSJed Brown .seealso: TSStep(), TSAdapt 398005175c85SJed Brown @*/ 39810910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 398205175c85SJed Brown { 398305175c85SJed Brown PetscErrorCode ierr; 398405175c85SJed Brown 398505175c85SJed Brown PetscFunctionBegin; 398605175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 398705175c85SJed Brown PetscValidType(ts,1); 39880910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3989ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 39900910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 399105175c85SJed Brown PetscFunctionReturn(0); 399205175c85SJed Brown } 399305175c85SJed Brown 3994b1cb36f3SHong Zhang /*@ 3995b1cb36f3SHong Zhang TSForwardCostIntegral - Evaluate the cost integral in the forward run. 3996b1cb36f3SHong Zhang 3997b1cb36f3SHong Zhang Collective on TS 3998b1cb36f3SHong Zhang 3999b1cb36f3SHong Zhang Input Arguments: 4000b1cb36f3SHong Zhang . ts - time stepping context 4001b1cb36f3SHong Zhang 4002b1cb36f3SHong Zhang Level: advanced 4003b1cb36f3SHong Zhang 4004b1cb36f3SHong Zhang Notes: 4005b1cb36f3SHong Zhang This function cannot be called until TSStep() has been completed. 4006b1cb36f3SHong Zhang 4007b1cb36f3SHong Zhang .seealso: TSSolve(), TSAdjointCostIntegral() 4008b1cb36f3SHong Zhang @*/ 4009b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts) 4010b1cb36f3SHong Zhang { 4011b1cb36f3SHong Zhang PetscErrorCode ierr; 4012b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4013b1cb36f3SHong 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); 4014b1cb36f3SHong Zhang ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr); 4015b1cb36f3SHong Zhang PetscFunctionReturn(0); 4016b1cb36f3SHong Zhang } 4017d67e68b7SBarry Smith 4018d763cef2SBarry Smith /*@ 4019d763cef2SBarry Smith TSSolve - Steps the requested number of timesteps. 4020d763cef2SBarry Smith 4021d763cef2SBarry Smith Collective on TS 4022d763cef2SBarry Smith 4023d763cef2SBarry Smith Input Parameter: 4024d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 402563e21af5SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used, 402663e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 40275a3a76d0SJed Brown 4028d763cef2SBarry Smith Level: beginner 4029d763cef2SBarry Smith 40305a3a76d0SJed Brown Notes: 40315a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 40325a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 40335a3a76d0SJed Brown stepped over the final time. 40345a3a76d0SJed Brown 4035d763cef2SBarry Smith .keywords: TS, timestep, solve 4036d763cef2SBarry Smith 403763e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime() 4038d763cef2SBarry Smith @*/ 4039cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 4040d763cef2SBarry Smith { 4041b06615a5SLisandro Dalcin Vec solution; 4042d763cef2SBarry Smith PetscErrorCode ierr; 4043d763cef2SBarry Smith 4044d763cef2SBarry Smith PetscFunctionBegin; 4045d763cef2SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4046f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 4047feed9e9dSBarry Smith 404849354f04SShri 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 */ 4049b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 40500910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 4051b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 4052b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 4053b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 40545a3a76d0SJed Brown } 40550910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 4056bbd56ea5SKarl Rupp } else if (u) { 40570910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 40585a3a76d0SJed Brown } 4059b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 406068bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 4061a6772fa2SLisandro Dalcin 4062a6772fa2SLisandro 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()"); 4063a6772fa2SLisandro 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"); 4064a6772fa2SLisandro Dalcin 4065e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 4066e7069c78SShri restarts the step after an event. Resetting these counters in such a case causes 4067e7069c78SShri TSTrajectory to incorrectly save the output files 4068e7069c78SShri */ 4069938c94caSShri Abhyankar 4070193ac0bcSJed Brown ts->steps = 0; 40715ef26d82SJed Brown ts->ksp_its = 0; 40725ef26d82SJed Brown ts->snes_its = 0; 4073c610991cSLisandro Dalcin ts->num_snes_failures = 0; 4074c610991cSLisandro Dalcin ts->reject = 0; 4075193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 4076193ac0bcSJed Brown 4077ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 4078f05ece33SBarry Smith 4079193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 4080193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 4081a6772fa2SLisandro Dalcin if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 4082cc708dedSBarry Smith ts->solvetime = ts->ptime; 4083a6772fa2SLisandro Dalcin solution = ts->vec_sol; 4084be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 4085db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 4086db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 4087e7069c78SShri 4088938c94caSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 40896427ac75SLisandro Dalcin ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 4090e7069c78SShri 409128d5b5d6SLisandro Dalcin ts->steprollback = PETSC_FALSE; 409228d5b5d6SLisandro Dalcin ts->steprestart = PETSC_TRUE; 40936427ac75SLisandro Dalcin 4094e1a7a14fSJed Brown while (!ts->reason) { 4095938c94caSShri Abhyankar ierr = TSMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 40969687d888SLisandro Dalcin if (!ts->steprollback) { 40979687d888SLisandro Dalcin ierr = TSPreStep(ts);CHKERRQ(ierr); 40989687d888SLisandro Dalcin } 4099193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 4100e783b05fSHong 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. */ 4101b1cb36f3SHong Zhang ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr); 4102b1cb36f3SHong Zhang } 410310b82f12SShri Abhyankar ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 4104e783b05fSHong 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. */ 4105e783b05fSHong Zhang if (!ts->steprollback) { 4106938c94caSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 41071eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 4108aeb4809dSShri Abhyankar } 4109193ac0bcSJed Brown } 4110938c94caSShri Abhyankar ierr = TSMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 41116427ac75SLisandro Dalcin 411249354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 41130910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 4114cc708dedSBarry Smith ts->solvetime = ts->max_time; 4115b06615a5SLisandro Dalcin solution = u; 411663e21af5SBarry Smith ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr); 41170574a7fbSJed Brown } else { 4118ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 4119cc708dedSBarry Smith ts->solvetime = ts->ptime; 4120b06615a5SLisandro Dalcin solution = ts->vec_sol; 41210574a7fbSJed Brown } 4122193ac0bcSJed Brown } 4123d2daff3dSHong Zhang 4124ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 412563e21af5SBarry Smith ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr); 412656f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 4127ef222394SBarry Smith if (ts->adjoint_solve) { 4128ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 41292b0a91c0SBarry Smith } 4130d763cef2SBarry Smith PetscFunctionReturn(0); 4131d763cef2SBarry Smith } 4132d763cef2SBarry Smith 4133b1cb36f3SHong Zhang /*@ 4134b1cb36f3SHong Zhang TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run. 4135b1cb36f3SHong Zhang 4136b1cb36f3SHong Zhang Collective on TS 4137b1cb36f3SHong Zhang 4138b1cb36f3SHong Zhang Input Arguments: 4139b1cb36f3SHong Zhang . ts - time stepping context 4140b1cb36f3SHong Zhang 4141b1cb36f3SHong Zhang Level: advanced 4142b1cb36f3SHong Zhang 4143b1cb36f3SHong Zhang Notes: 4144b1cb36f3SHong Zhang This function cannot be called until TSAdjointStep() has been completed. 4145b1cb36f3SHong Zhang 4146b1cb36f3SHong Zhang .seealso: TSAdjointSolve(), TSAdjointStep 4147b1cb36f3SHong Zhang @*/ 4148b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts) 4149b1cb36f3SHong Zhang { 4150b1cb36f3SHong Zhang PetscErrorCode ierr; 4151b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4152b1cb36f3SHong 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); 4153b1cb36f3SHong Zhang ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr); 4154b1cb36f3SHong Zhang PetscFunctionReturn(0); 4155b1cb36f3SHong Zhang } 4156b1cb36f3SHong Zhang 4157c88f2d44SBarry Smith /*@ 4158c88f2d44SBarry Smith TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 4159c88f2d44SBarry Smith 4160c88f2d44SBarry Smith Collective on TS 4161c88f2d44SBarry Smith 4162c88f2d44SBarry Smith Input Parameter: 41632c39e106SBarry Smith . ts - the TS context obtained from TSCreate() 4164c88f2d44SBarry Smith 4165e87fd094SBarry Smith Options Database: 4166e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions 4167e87fd094SBarry Smith 4168c88f2d44SBarry Smith Level: intermediate 4169c88f2d44SBarry Smith 4170c88f2d44SBarry Smith Notes: 4171c88f2d44SBarry Smith This must be called after a call to TSSolve() that solves the forward problem 4172c88f2d44SBarry Smith 417373b18844SBarry Smith By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time 417473b18844SBarry Smith 4175c88f2d44SBarry Smith .keywords: TS, timestep, solve 4176c88f2d44SBarry Smith 4177b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep() 4178c88f2d44SBarry Smith @*/ 41792c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts) 4180c88f2d44SBarry Smith { 4181c88f2d44SBarry Smith PetscErrorCode ierr; 4182c88f2d44SBarry Smith 4183c88f2d44SBarry Smith PetscFunctionBegin; 4184c88f2d44SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4185c88f2d44SBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 418668bece0bSHong Zhang 4187c88f2d44SBarry Smith /* reset time step and iteration counters */ 4188c88f2d44SBarry Smith ts->steps = 0; 4189c88f2d44SBarry Smith ts->ksp_its = 0; 4190c88f2d44SBarry Smith ts->snes_its = 0; 4191c88f2d44SBarry Smith ts->num_snes_failures = 0; 4192c88f2d44SBarry Smith ts->reject = 0; 4193c88f2d44SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 4194c88f2d44SBarry Smith 419573b18844SBarry Smith if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps; 4196c88f2d44SBarry Smith 419773b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 4198c88f2d44SBarry Smith while (!ts->reason) { 419955c52731SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 42009110b2e7SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 42016427ac75SLisandro Dalcin ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr); 4202616946c1SHong Zhang ierr = TSAdjointStep(ts);CHKERRQ(ierr); 4203b1cb36f3SHong Zhang if (ts->vec_costintegral && !ts->costintegralfwd) { 4204b1cb36f3SHong Zhang ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr); 4205b1cb36f3SHong Zhang } 4206c88f2d44SBarry Smith } 420726656371SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 420826656371SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 4209c88f2d44SBarry Smith ts->solvetime = ts->ptime; 4210e210cd0eSHong Zhang ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr); 4211685405a1SBarry Smith ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr); 4212c88f2d44SBarry Smith PetscFunctionReturn(0); 4213c88f2d44SBarry Smith } 4214c88f2d44SBarry Smith 42157db568b7SBarry Smith /*@C 4216228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 4217228d79bcSJed Brown 4218228d79bcSJed Brown Collective on TS 4219228d79bcSJed Brown 4220228d79bcSJed Brown Input Parameters: 4221228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 4222228d79bcSJed Brown . step - step number that has just completed 4223228d79bcSJed Brown . ptime - model time of the state 42240910c330SBarry Smith - u - state at the current model time 4225228d79bcSJed Brown 4226228d79bcSJed Brown Notes: 42277db568b7SBarry Smith TSMonitor() is typically used automatically within the time stepping implementations. 42287db568b7SBarry Smith Users would almost never call this routine directly. 4229228d79bcSJed Brown 423063e21af5SBarry Smith A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions 423163e21af5SBarry Smith 42327db568b7SBarry Smith Level: developer 4233228d79bcSJed Brown 4234228d79bcSJed Brown .keywords: TS, timestep 4235228d79bcSJed Brown @*/ 42360910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 4237d763cef2SBarry Smith { 4238d6152f81SLisandro Dalcin DM dm; 4239a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 4240d6152f81SLisandro Dalcin PetscErrorCode ierr; 4241d763cef2SBarry Smith 4242d763cef2SBarry Smith PetscFunctionBegin; 4243b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4244b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4245d6152f81SLisandro Dalcin 4246d6152f81SLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 4247d6152f81SLisandro Dalcin ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr); 4248d6152f81SLisandro Dalcin 42495edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 4250d763cef2SBarry Smith for (i=0; i<n; i++) { 42510910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 4252d763cef2SBarry Smith } 42535edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 4254d763cef2SBarry Smith PetscFunctionReturn(0); 4255d763cef2SBarry Smith } 4256d763cef2SBarry Smith 42577db568b7SBarry Smith /*@C 42589110b2e7SHong Zhang TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet() 42599110b2e7SHong Zhang 42609110b2e7SHong Zhang Collective on TS 42619110b2e7SHong Zhang 42629110b2e7SHong Zhang Input Parameters: 42639110b2e7SHong Zhang + ts - time stepping context obtained from TSCreate() 42649110b2e7SHong Zhang . step - step number that has just completed 42659110b2e7SHong Zhang . ptime - model time of the state 42669110b2e7SHong Zhang . u - state at the current model time 42679110b2e7SHong Zhang . numcost - number of cost functions (dimension of lambda or mu) 42689110b2e7SHong Zhang . lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 42699110b2e7SHong Zhang - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 42709110b2e7SHong Zhang 42719110b2e7SHong Zhang Notes: 42727db568b7SBarry Smith TSAdjointMonitor() is typically used automatically within the time stepping implementations. 42737db568b7SBarry Smith Users would almost never call this routine directly. 42749110b2e7SHong Zhang 42757db568b7SBarry Smith Level: developer 42769110b2e7SHong Zhang 42779110b2e7SHong Zhang .keywords: TS, timestep 42789110b2e7SHong Zhang @*/ 42799110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu) 42809110b2e7SHong Zhang { 42819110b2e7SHong Zhang PetscErrorCode ierr; 42829110b2e7SHong Zhang PetscInt i,n = ts->numberadjointmonitors; 42839110b2e7SHong Zhang 42849110b2e7SHong Zhang PetscFunctionBegin; 42859110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 42869110b2e7SHong Zhang PetscValidHeaderSpecific(u,VEC_CLASSID,4); 42879110b2e7SHong Zhang ierr = VecLockPush(u);CHKERRQ(ierr); 42889110b2e7SHong Zhang for (i=0; i<n; i++) { 42899110b2e7SHong Zhang ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 42909110b2e7SHong Zhang } 42919110b2e7SHong Zhang ierr = VecLockPop(u);CHKERRQ(ierr); 42929110b2e7SHong Zhang PetscFunctionReturn(0); 42939110b2e7SHong Zhang } 42949110b2e7SHong Zhang 4295d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 4296d763cef2SBarry Smith /*@C 42977db568b7SBarry Smith TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 4298a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 4299d763cef2SBarry Smith 4300d763cef2SBarry Smith Collective on TS 4301d763cef2SBarry Smith 4302d763cef2SBarry Smith Input Parameters: 4303d763cef2SBarry Smith + host - the X display to open, or null for the local machine 4304d763cef2SBarry Smith . label - the title to put in the title bar 43057c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 4306a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 4307a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 4308d763cef2SBarry Smith 4309d763cef2SBarry Smith Output Parameter: 43100b039ecaSBarry Smith . ctx - the context 4311d763cef2SBarry Smith 4312d763cef2SBarry Smith Options Database Key: 43134f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 43147db568b7SBarry Smith . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 43157db568b7SBarry Smith . -ts_monitor_lg_error - monitor the error 43167db568b7SBarry Smith . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 43177db568b7SBarry Smith . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 4318b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 4319d763cef2SBarry Smith 4320d763cef2SBarry Smith Notes: 4321a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 4322d763cef2SBarry Smith 43237db568b7SBarry Smith One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 43247db568b7SBarry Smith 43257db568b7SBarry 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 43267db568b7SBarry 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 43277db568b7SBarry Smith as the first argument. 43287db568b7SBarry Smith 43297db568b7SBarry Smith One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 43307db568b7SBarry Smith 43317db568b7SBarry Smith 4332d763cef2SBarry Smith Level: intermediate 4333d763cef2SBarry Smith 43347db568b7SBarry Smith .keywords: TS, monitor, line graph, residual 4335d763cef2SBarry Smith 43367db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 43377db568b7SBarry Smith TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 43387db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 43397db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 43407db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 43417c922b88SBarry Smith 4342d763cef2SBarry Smith @*/ 4343a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 4344d763cef2SBarry Smith { 43457f52daa2SLisandro Dalcin PetscDraw draw; 4346dfbe8321SBarry Smith PetscErrorCode ierr; 4347d763cef2SBarry Smith 4348d763cef2SBarry Smith PetscFunctionBegin; 4349b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 43507f52daa2SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 43517f52daa2SLisandro Dalcin ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 43527f52daa2SLisandro Dalcin ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 4353287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 43547f52daa2SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 4355a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 4356d763cef2SBarry Smith PetscFunctionReturn(0); 4357d763cef2SBarry Smith } 4358d763cef2SBarry Smith 4359b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 4360d763cef2SBarry Smith { 43610b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4362c32365f1SBarry Smith PetscReal x = ptime,y; 4363dfbe8321SBarry Smith PetscErrorCode ierr; 4364d763cef2SBarry Smith 4365d763cef2SBarry Smith PetscFunctionBegin; 436663e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */ 4367b06615a5SLisandro Dalcin if (!step) { 4368a9f9c1f6SBarry Smith PetscDrawAxis axis; 4369a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 43706934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr); 4371a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4372a9f9c1f6SBarry Smith } 4373c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 43740b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4375b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 43760b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 43776934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 43783923b477SBarry Smith } 4379d763cef2SBarry Smith PetscFunctionReturn(0); 4380d763cef2SBarry Smith } 4381d763cef2SBarry Smith 4382d763cef2SBarry Smith /*@C 4383a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 4384a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 4385d763cef2SBarry Smith 43860b039ecaSBarry Smith Collective on TSMonitorLGCtx 4387d763cef2SBarry Smith 4388d763cef2SBarry Smith Input Parameter: 43890b039ecaSBarry Smith . ctx - the monitor context 4390d763cef2SBarry Smith 4391d763cef2SBarry Smith Level: intermediate 4392d763cef2SBarry Smith 4393d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 4394d763cef2SBarry Smith 43954f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 4396d763cef2SBarry Smith @*/ 4397a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 4398d763cef2SBarry Smith { 4399dfbe8321SBarry Smith PetscErrorCode ierr; 4400d763cef2SBarry Smith 4401d763cef2SBarry Smith PetscFunctionBegin; 44027684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 44037684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 44047684fa3eSBarry Smith } 44050b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 440631152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 4407387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 4408387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 4409387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 44100b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 4411d763cef2SBarry Smith PetscFunctionReturn(0); 4412d763cef2SBarry Smith } 4413d763cef2SBarry Smith 4414d763cef2SBarry Smith /*@ 4415b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4416d763cef2SBarry Smith 4417d763cef2SBarry Smith Not Collective 4418d763cef2SBarry Smith 4419d763cef2SBarry Smith Input Parameter: 4420d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 4421d763cef2SBarry Smith 4422d763cef2SBarry Smith Output Parameter: 442363e21af5SBarry Smith . t - the current time. This time may not corresponds to the final time set with TSSetDuration(), use TSGetSolveTime(). 4424d763cef2SBarry Smith 4425d763cef2SBarry Smith Level: beginner 4426d763cef2SBarry Smith 4427b8123daeSJed Brown Note: 4428b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 44299be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 4430b8123daeSJed Brown 443163e21af5SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep(), TSGetSolveTime() 4432d763cef2SBarry Smith 4433d763cef2SBarry Smith .keywords: TS, get, time 4434d763cef2SBarry Smith @*/ 44357087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 4436d763cef2SBarry Smith { 4437d763cef2SBarry Smith PetscFunctionBegin; 44380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4439f7cf8827SBarry Smith PetscValidRealPointer(t,2); 4440d763cef2SBarry Smith *t = ts->ptime; 4441d763cef2SBarry Smith PetscFunctionReturn(0); 4442d763cef2SBarry Smith } 4443d763cef2SBarry Smith 4444e5e524a1SHong Zhang /*@ 4445e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4446e5e524a1SHong Zhang 4447e5e524a1SHong Zhang Not Collective 4448e5e524a1SHong Zhang 4449e5e524a1SHong Zhang Input Parameter: 4450e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 4451e5e524a1SHong Zhang 4452e5e524a1SHong Zhang Output Parameter: 4453e5e524a1SHong Zhang . t - the previous time 4454e5e524a1SHong Zhang 4455e5e524a1SHong Zhang Level: beginner 4456e5e524a1SHong Zhang 4457e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 4458e5e524a1SHong Zhang 4459e5e524a1SHong Zhang .keywords: TS, get, time 4460e5e524a1SHong Zhang @*/ 4461e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 4462e5e524a1SHong Zhang { 4463e5e524a1SHong Zhang PetscFunctionBegin; 4464e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4465e5e524a1SHong Zhang PetscValidRealPointer(t,2); 4466e5e524a1SHong Zhang *t = ts->ptime_prev; 4467e5e524a1SHong Zhang PetscFunctionReturn(0); 4468e5e524a1SHong Zhang } 4469e5e524a1SHong Zhang 44706a4d4014SLisandro Dalcin /*@ 44716a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 44726a4d4014SLisandro Dalcin 44733f9fe445SBarry Smith Logically Collective on TS 44746a4d4014SLisandro Dalcin 44756a4d4014SLisandro Dalcin Input Parameters: 44766a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 44776a4d4014SLisandro Dalcin - time - the time 44786a4d4014SLisandro Dalcin 44796a4d4014SLisandro Dalcin Level: intermediate 44806a4d4014SLisandro Dalcin 44816a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 44826a4d4014SLisandro Dalcin 44836a4d4014SLisandro Dalcin .keywords: TS, set, time 44846a4d4014SLisandro Dalcin @*/ 44857087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 44866a4d4014SLisandro Dalcin { 44876a4d4014SLisandro Dalcin PetscFunctionBegin; 44880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4489c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 44906a4d4014SLisandro Dalcin ts->ptime = t; 44916a4d4014SLisandro Dalcin PetscFunctionReturn(0); 44926a4d4014SLisandro Dalcin } 44936a4d4014SLisandro Dalcin 4494d763cef2SBarry Smith /*@C 4495d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4496d763cef2SBarry Smith TS options in the database. 4497d763cef2SBarry Smith 44983f9fe445SBarry Smith Logically Collective on TS 4499d763cef2SBarry Smith 4500d763cef2SBarry Smith Input Parameter: 4501d763cef2SBarry Smith + ts - The TS context 4502d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4503d763cef2SBarry Smith 4504d763cef2SBarry Smith Notes: 4505d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4506d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4507d763cef2SBarry Smith hyphen. 4508d763cef2SBarry Smith 4509d763cef2SBarry Smith Level: advanced 4510d763cef2SBarry Smith 4511d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 4512d763cef2SBarry Smith 4513d763cef2SBarry Smith .seealso: TSSetFromOptions() 4514d763cef2SBarry Smith 4515d763cef2SBarry Smith @*/ 45167087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 4517d763cef2SBarry Smith { 4518dfbe8321SBarry Smith PetscErrorCode ierr; 4519089b2837SJed Brown SNES snes; 4520d763cef2SBarry Smith 4521d763cef2SBarry Smith PetscFunctionBegin; 45220700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4523d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4524089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4525089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4526d763cef2SBarry Smith PetscFunctionReturn(0); 4527d763cef2SBarry Smith } 4528d763cef2SBarry Smith 4529d763cef2SBarry Smith 4530d763cef2SBarry Smith /*@C 4531d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4532d763cef2SBarry Smith TS options in the database. 4533d763cef2SBarry Smith 45343f9fe445SBarry Smith Logically Collective on TS 4535d763cef2SBarry Smith 4536d763cef2SBarry Smith Input Parameter: 4537d763cef2SBarry Smith + ts - The TS context 4538d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4539d763cef2SBarry Smith 4540d763cef2SBarry Smith Notes: 4541d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4542d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4543d763cef2SBarry Smith hyphen. 4544d763cef2SBarry Smith 4545d763cef2SBarry Smith Level: advanced 4546d763cef2SBarry Smith 4547d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 4548d763cef2SBarry Smith 4549d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 4550d763cef2SBarry Smith 4551d763cef2SBarry Smith @*/ 45527087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4553d763cef2SBarry Smith { 4554dfbe8321SBarry Smith PetscErrorCode ierr; 4555089b2837SJed Brown SNES snes; 4556d763cef2SBarry Smith 4557d763cef2SBarry Smith PetscFunctionBegin; 45580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4559d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4560089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4561089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4562d763cef2SBarry Smith PetscFunctionReturn(0); 4563d763cef2SBarry Smith } 4564d763cef2SBarry Smith 4565d763cef2SBarry Smith /*@C 4566d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4567d763cef2SBarry Smith TS options in the database. 4568d763cef2SBarry Smith 4569d763cef2SBarry Smith Not Collective 4570d763cef2SBarry Smith 4571d763cef2SBarry Smith Input Parameter: 4572d763cef2SBarry Smith . ts - The TS context 4573d763cef2SBarry Smith 4574d763cef2SBarry Smith Output Parameter: 4575d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4576d763cef2SBarry Smith 4577d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 4578d763cef2SBarry Smith sufficient length to hold the prefix. 4579d763cef2SBarry Smith 4580d763cef2SBarry Smith Level: intermediate 4581d763cef2SBarry Smith 4582d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 4583d763cef2SBarry Smith 4584d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 4585d763cef2SBarry Smith @*/ 45867087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4587d763cef2SBarry Smith { 4588dfbe8321SBarry Smith PetscErrorCode ierr; 4589d763cef2SBarry Smith 4590d763cef2SBarry Smith PetscFunctionBegin; 45910700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45924482741eSBarry Smith PetscValidPointer(prefix,2); 4593d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4594d763cef2SBarry Smith PetscFunctionReturn(0); 4595d763cef2SBarry Smith } 4596d763cef2SBarry Smith 4597d763cef2SBarry Smith /*@C 4598d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4599d763cef2SBarry Smith 4600d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4601d763cef2SBarry Smith 4602d763cef2SBarry Smith Input Parameter: 4603d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4604d763cef2SBarry Smith 4605d763cef2SBarry Smith Output Parameters: 4606e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4607e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4608e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4609e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4610d763cef2SBarry Smith 46110298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 4612d763cef2SBarry Smith 4613d763cef2SBarry Smith Level: intermediate 4614d763cef2SBarry Smith 461526d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 4616d763cef2SBarry Smith 4617d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 4618d763cef2SBarry Smith @*/ 4619e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4620d763cef2SBarry Smith { 4621089b2837SJed Brown PetscErrorCode ierr; 462224989b8cSPeter Brune DM dm; 4623089b2837SJed Brown 4624d763cef2SBarry Smith PetscFunctionBegin; 462523a57915SBarry Smith if (Amat || Pmat) { 462623a57915SBarry Smith SNES snes; 4627089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 462823a57915SBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4629e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 463023a57915SBarry Smith } 463124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 463224989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 4633d763cef2SBarry Smith PetscFunctionReturn(0); 4634d763cef2SBarry Smith } 4635d763cef2SBarry Smith 46362eca1d9cSJed Brown /*@C 46372eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 46382eca1d9cSJed Brown 46392eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 46402eca1d9cSJed Brown 46412eca1d9cSJed Brown Input Parameter: 46422eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 46432eca1d9cSJed Brown 46442eca1d9cSJed Brown Output Parameters: 4645e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4646e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 46472eca1d9cSJed Brown . f - The function to compute the matrices 46482eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 46492eca1d9cSJed Brown 46500298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 46512eca1d9cSJed Brown 46522eca1d9cSJed Brown Level: advanced 46532eca1d9cSJed Brown 46542eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 46552eca1d9cSJed Brown 46562eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 46572eca1d9cSJed Brown @*/ 4658e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 46592eca1d9cSJed Brown { 4660089b2837SJed Brown PetscErrorCode ierr; 466124989b8cSPeter Brune DM dm; 46620910c330SBarry Smith 46632eca1d9cSJed Brown PetscFunctionBegin; 4664c0aab802Sstefano_zampini if (Amat || Pmat) { 4665c0aab802Sstefano_zampini SNES snes; 4666089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4667f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4668e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 4669c0aab802Sstefano_zampini } 467024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 467124989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 46722eca1d9cSJed Brown PetscFunctionReturn(0); 46732eca1d9cSJed Brown } 46742eca1d9cSJed Brown 46756083293cSBarry Smith 46761713a123SBarry Smith /*@C 467783a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 46781713a123SBarry Smith VecView() for the solution at each timestep 46791713a123SBarry Smith 46801713a123SBarry Smith Collective on TS 46811713a123SBarry Smith 46821713a123SBarry Smith Input Parameters: 46831713a123SBarry Smith + ts - the TS context 46841713a123SBarry Smith . step - current time-step 4685142b95e3SSatish Balay . ptime - current time 46860298fd71SBarry Smith - dummy - either a viewer or NULL 46871713a123SBarry Smith 468899fdda47SBarry Smith Options Database: 468999fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 469099fdda47SBarry Smith 4691387f4636SBarry 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 469299fdda47SBarry Smith will look bad 469399fdda47SBarry Smith 46941713a123SBarry Smith Level: intermediate 46951713a123SBarry Smith 46961713a123SBarry Smith .keywords: TS, vector, monitor, view 46971713a123SBarry Smith 4698a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 46991713a123SBarry Smith @*/ 47000910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 47011713a123SBarry Smith { 4702dfbe8321SBarry Smith PetscErrorCode ierr; 470383a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4704473a3ab2SBarry Smith PetscDraw draw; 47051713a123SBarry Smith 47061713a123SBarry Smith PetscFunctionBegin; 47076083293cSBarry Smith if (!step && ictx->showinitial) { 47086083293cSBarry Smith if (!ictx->initialsolution) { 47090910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 47101713a123SBarry Smith } 47110910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 47126083293cSBarry Smith } 4713b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 47140dcf80beSBarry Smith 47156083293cSBarry Smith if (ictx->showinitial) { 47166083293cSBarry Smith PetscReal pause; 47176083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 47186083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 47196083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 47206083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 47216083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 47226083293cSBarry Smith } 47230910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 4724473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 472551fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 4726473a3ab2SBarry Smith char time[32]; 4727473a3ab2SBarry Smith 4728473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 472985b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4730473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4731473a3ab2SBarry Smith h = yl + .95*(yr - yl); 473251fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4733473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4734473a3ab2SBarry Smith } 4735473a3ab2SBarry Smith 47366083293cSBarry Smith if (ictx->showinitial) { 47376083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 47386083293cSBarry Smith } 47391713a123SBarry Smith PetscFunctionReturn(0); 47401713a123SBarry Smith } 47411713a123SBarry Smith 47429110b2e7SHong Zhang /*@C 47439110b2e7SHong Zhang TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling 47449110b2e7SHong Zhang VecView() for the sensitivities to initial states at each timestep 47459110b2e7SHong Zhang 47469110b2e7SHong Zhang Collective on TS 47479110b2e7SHong Zhang 47489110b2e7SHong Zhang Input Parameters: 47499110b2e7SHong Zhang + ts - the TS context 47509110b2e7SHong Zhang . step - current time-step 47519110b2e7SHong Zhang . ptime - current time 47529110b2e7SHong Zhang . u - current state 47539110b2e7SHong Zhang . numcost - number of cost functions 47549110b2e7SHong Zhang . lambda - sensitivities to initial conditions 47559110b2e7SHong Zhang . mu - sensitivities to parameters 47569110b2e7SHong Zhang - dummy - either a viewer or NULL 47579110b2e7SHong Zhang 47589110b2e7SHong Zhang Level: intermediate 47599110b2e7SHong Zhang 47609110b2e7SHong Zhang .keywords: TS, vector, adjoint, monitor, view 47619110b2e7SHong Zhang 47629110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView() 47639110b2e7SHong Zhang @*/ 47649110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 47659110b2e7SHong Zhang { 47669110b2e7SHong Zhang PetscErrorCode ierr; 47679110b2e7SHong Zhang TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 47689110b2e7SHong Zhang PetscDraw draw; 4769a0046e2cSSatish Balay PetscReal xl,yl,xr,yr,h; 4770a0046e2cSSatish Balay char time[32]; 47719110b2e7SHong Zhang 47729110b2e7SHong Zhang PetscFunctionBegin; 47739110b2e7SHong Zhang if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 47749110b2e7SHong Zhang 47759110b2e7SHong Zhang ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr); 47769110b2e7SHong Zhang ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 47779110b2e7SHong Zhang ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 47789110b2e7SHong Zhang ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 47799110b2e7SHong Zhang h = yl + .95*(yr - yl); 47809110b2e7SHong Zhang ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 47819110b2e7SHong Zhang ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 47829110b2e7SHong Zhang PetscFunctionReturn(0); 47839110b2e7SHong Zhang } 47849110b2e7SHong Zhang 47852d5ee99bSBarry Smith /*@C 47862d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 47872d5ee99bSBarry Smith 47882d5ee99bSBarry Smith Collective on TS 47892d5ee99bSBarry Smith 47902d5ee99bSBarry Smith Input Parameters: 47912d5ee99bSBarry Smith + ts - the TS context 47922d5ee99bSBarry Smith . step - current time-step 47932d5ee99bSBarry Smith . ptime - current time 47942d5ee99bSBarry Smith - dummy - either a viewer or NULL 47952d5ee99bSBarry Smith 47962d5ee99bSBarry Smith Level: intermediate 47972d5ee99bSBarry Smith 47982d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 47992d5ee99bSBarry Smith 48002d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 48012d5ee99bSBarry Smith @*/ 48022d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 48032d5ee99bSBarry Smith { 48042d5ee99bSBarry Smith PetscErrorCode ierr; 48052d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 48062d5ee99bSBarry Smith PetscDraw draw; 48076934998bSLisandro Dalcin PetscDrawAxis axis; 48082d5ee99bSBarry Smith PetscInt n; 48092d5ee99bSBarry Smith PetscMPIInt size; 48106934998bSLisandro Dalcin PetscReal U0,U1,xl,yl,xr,yr,h; 48112d5ee99bSBarry Smith char time[32]; 48122d5ee99bSBarry Smith const PetscScalar *U; 48132d5ee99bSBarry Smith 48142d5ee99bSBarry Smith PetscFunctionBegin; 48156934998bSLisandro Dalcin ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr); 48166934998bSLisandro Dalcin if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs"); 48172d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 48186934998bSLisandro Dalcin if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 48192d5ee99bSBarry Smith 48202d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 48216934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr); 48226934998bSLisandro Dalcin ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 48236934998bSLisandro Dalcin if (!step) { 48246934998bSLisandro Dalcin ierr = PetscDrawClear(draw);CHKERRQ(ierr); 48256934998bSLisandro Dalcin ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); 48266934998bSLisandro Dalcin } 48272d5ee99bSBarry Smith 48282d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 48296934998bSLisandro Dalcin U0 = PetscRealPart(U[0]); 48306934998bSLisandro Dalcin U1 = PetscRealPart(U[1]); 4831a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 48326934998bSLisandro Dalcin if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0); 48332d5ee99bSBarry Smith 48346934998bSLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 48356934998bSLisandro Dalcin ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr); 48362d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 48374b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 483885b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 48392d5ee99bSBarry Smith h = yl + .95*(yr - yl); 484051fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 48412d5ee99bSBarry Smith } 48426934998bSLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 48432d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 48446934998bSLisandro Dalcin ierr = PetscDrawSave(draw);CHKERRQ(ierr); 48452d5ee99bSBarry Smith PetscFunctionReturn(0); 48462d5ee99bSBarry Smith } 48472d5ee99bSBarry Smith 48481713a123SBarry Smith 48496083293cSBarry Smith /*@C 485083a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 48516083293cSBarry Smith 48526083293cSBarry Smith Collective on TS 48536083293cSBarry Smith 48546083293cSBarry Smith Input Parameters: 48556083293cSBarry Smith . ctx - the monitor context 48566083293cSBarry Smith 48576083293cSBarry Smith Level: intermediate 48586083293cSBarry Smith 48596083293cSBarry Smith .keywords: TS, vector, monitor, view 48606083293cSBarry Smith 486183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 48626083293cSBarry Smith @*/ 486383a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 48646083293cSBarry Smith { 48656083293cSBarry Smith PetscErrorCode ierr; 48666083293cSBarry Smith 48676083293cSBarry Smith PetscFunctionBegin; 486883a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 486983a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 487083a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 48716083293cSBarry Smith PetscFunctionReturn(0); 48726083293cSBarry Smith } 48736083293cSBarry Smith 48746083293cSBarry Smith /*@C 487583a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 48766083293cSBarry Smith 48776083293cSBarry Smith Collective on TS 48786083293cSBarry Smith 48796083293cSBarry Smith Input Parameter: 48806083293cSBarry Smith . ts - time-step context 48816083293cSBarry Smith 48826083293cSBarry Smith Output Patameter: 48836083293cSBarry Smith . ctx - the monitor context 48846083293cSBarry Smith 488599fdda47SBarry Smith Options Database: 488699fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 488799fdda47SBarry Smith 48886083293cSBarry Smith Level: intermediate 48896083293cSBarry Smith 48906083293cSBarry Smith .keywords: TS, vector, monitor, view 48916083293cSBarry Smith 489283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 48936083293cSBarry Smith @*/ 489483a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 48956083293cSBarry Smith { 48966083293cSBarry Smith PetscErrorCode ierr; 48976083293cSBarry Smith 48986083293cSBarry Smith PetscFunctionBegin; 4899b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 490083a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4901e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4902bbd56ea5SKarl Rupp 490383a4ac43SBarry Smith (*ctx)->howoften = howoften; 4904473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 4905c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4906473a3ab2SBarry Smith 4907473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 4908c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 49096083293cSBarry Smith PetscFunctionReturn(0); 49106083293cSBarry Smith } 49116083293cSBarry Smith 49123a471f94SBarry Smith /*@C 491383a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 49143a471f94SBarry Smith VecView() for the error at each timestep 49153a471f94SBarry Smith 49163a471f94SBarry Smith Collective on TS 49173a471f94SBarry Smith 49183a471f94SBarry Smith Input Parameters: 49193a471f94SBarry Smith + ts - the TS context 49203a471f94SBarry Smith . step - current time-step 49213a471f94SBarry Smith . ptime - current time 49220298fd71SBarry Smith - dummy - either a viewer or NULL 49233a471f94SBarry Smith 49243a471f94SBarry Smith Level: intermediate 49253a471f94SBarry Smith 49263a471f94SBarry Smith .keywords: TS, vector, monitor, view 49273a471f94SBarry Smith 49283a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 49293a471f94SBarry Smith @*/ 49300910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 49313a471f94SBarry Smith { 49323a471f94SBarry Smith PetscErrorCode ierr; 493383a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 493483a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 49353a471f94SBarry Smith Vec work; 49363a471f94SBarry Smith 49373a471f94SBarry Smith PetscFunctionBegin; 4938b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 49390910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 49403a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 49410910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 49423a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 49433a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 49443a471f94SBarry Smith PetscFunctionReturn(0); 49453a471f94SBarry Smith } 49463a471f94SBarry Smith 4947af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 49486c699258SBarry Smith /*@ 49492a808120SBarry Smith TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS 49506c699258SBarry Smith 49513f9fe445SBarry Smith Logically Collective on TS and DM 49526c699258SBarry Smith 49536c699258SBarry Smith Input Parameters: 49542a808120SBarry Smith + ts - the ODE integrator object 49552a808120SBarry Smith - dm - the dm, cannot be NULL 49566c699258SBarry Smith 49576c699258SBarry Smith Level: intermediate 49586c699258SBarry Smith 49596c699258SBarry Smith 49606c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 49616c699258SBarry Smith @*/ 49627087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 49636c699258SBarry Smith { 49646c699258SBarry Smith PetscErrorCode ierr; 4965089b2837SJed Brown SNES snes; 4966942e3340SBarry Smith DMTS tsdm; 49676c699258SBarry Smith 49686c699258SBarry Smith PetscFunctionBegin; 49690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 49702a808120SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 497170663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4972942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 49732a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4974942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4975942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 497624989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 497724989b8cSPeter Brune tsdm->originaldm = dm; 497824989b8cSPeter Brune } 497924989b8cSPeter Brune } 49806bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 498124989b8cSPeter Brune } 49826c699258SBarry Smith ts->dm = dm; 4983bbd56ea5SKarl Rupp 4984089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4985089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 49866c699258SBarry Smith PetscFunctionReturn(0); 49876c699258SBarry Smith } 49886c699258SBarry Smith 49896c699258SBarry Smith /*@ 49906c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 49916c699258SBarry Smith 49923f9fe445SBarry Smith Not Collective 49936c699258SBarry Smith 49946c699258SBarry Smith Input Parameter: 49956c699258SBarry Smith . ts - the preconditioner context 49966c699258SBarry Smith 49976c699258SBarry Smith Output Parameter: 49986c699258SBarry Smith . dm - the dm 49996c699258SBarry Smith 50006c699258SBarry Smith Level: intermediate 50016c699258SBarry Smith 50026c699258SBarry Smith 50036c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 50046c699258SBarry Smith @*/ 50057087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 50066c699258SBarry Smith { 5007496e6a7aSJed Brown PetscErrorCode ierr; 5008496e6a7aSJed Brown 50096c699258SBarry Smith PetscFunctionBegin; 50100700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5011496e6a7aSJed Brown if (!ts->dm) { 5012ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 5013496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 5014496e6a7aSJed Brown } 50156c699258SBarry Smith *dm = ts->dm; 50166c699258SBarry Smith PetscFunctionReturn(0); 50176c699258SBarry Smith } 50181713a123SBarry Smith 50190f5c6efeSJed Brown /*@ 50200f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 50210f5c6efeSJed Brown 50223f9fe445SBarry Smith Logically Collective on SNES 50230f5c6efeSJed Brown 50240f5c6efeSJed Brown Input Parameter: 5025d42a1c89SJed Brown + snes - nonlinear solver 50260910c330SBarry Smith . U - the current state at which to evaluate the residual 5027d42a1c89SJed Brown - ctx - user context, must be a TS 50280f5c6efeSJed Brown 50290f5c6efeSJed Brown Output Parameter: 50300f5c6efeSJed Brown . F - the nonlinear residual 50310f5c6efeSJed Brown 50320f5c6efeSJed Brown Notes: 50330f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 50340f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 50350f5c6efeSJed Brown 50360f5c6efeSJed Brown Level: advanced 50370f5c6efeSJed Brown 50380f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 50390f5c6efeSJed Brown @*/ 50400910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 50410f5c6efeSJed Brown { 50420f5c6efeSJed Brown TS ts = (TS)ctx; 50430f5c6efeSJed Brown PetscErrorCode ierr; 50440f5c6efeSJed Brown 50450f5c6efeSJed Brown PetscFunctionBegin; 50460f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 50470910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 50480f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 50490f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 50500910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 50510f5c6efeSJed Brown PetscFunctionReturn(0); 50520f5c6efeSJed Brown } 50530f5c6efeSJed Brown 50540f5c6efeSJed Brown /*@ 50550f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 50560f5c6efeSJed Brown 50570f5c6efeSJed Brown Collective on SNES 50580f5c6efeSJed Brown 50590f5c6efeSJed Brown Input Parameter: 50600f5c6efeSJed Brown + snes - nonlinear solver 50610910c330SBarry Smith . U - the current state at which to evaluate the residual 50620f5c6efeSJed Brown - ctx - user context, must be a TS 50630f5c6efeSJed Brown 50640f5c6efeSJed Brown Output Parameter: 50650f5c6efeSJed Brown + A - the Jacobian 50660f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 50670f5c6efeSJed Brown - flag - indicates any structure change in the matrix 50680f5c6efeSJed Brown 50690f5c6efeSJed Brown Notes: 50700f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 50710f5c6efeSJed Brown 50720f5c6efeSJed Brown Level: developer 50730f5c6efeSJed Brown 50740f5c6efeSJed Brown .seealso: SNESSetJacobian() 50750f5c6efeSJed Brown @*/ 5076d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 50770f5c6efeSJed Brown { 50780f5c6efeSJed Brown TS ts = (TS)ctx; 50790f5c6efeSJed Brown PetscErrorCode ierr; 50800f5c6efeSJed Brown 50810f5c6efeSJed Brown PetscFunctionBegin; 50820f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 50830910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 50840f5c6efeSJed Brown PetscValidPointer(A,3); 508594ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 50860f5c6efeSJed Brown PetscValidPointer(B,4); 508794ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 50880f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 5089d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 50900f5c6efeSJed Brown PetscFunctionReturn(0); 50910f5c6efeSJed Brown } 5092325fc9f4SBarry Smith 50930e4ef248SJed Brown /*@C 50949ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 50950e4ef248SJed Brown 50960e4ef248SJed Brown Collective on TS 50970e4ef248SJed Brown 50980e4ef248SJed Brown Input Arguments: 50990e4ef248SJed Brown + ts - time stepping context 51000e4ef248SJed Brown . t - time at which to evaluate 51010910c330SBarry Smith . U - state at which to evaluate 51020e4ef248SJed Brown - ctx - context 51030e4ef248SJed Brown 51040e4ef248SJed Brown Output Arguments: 51050e4ef248SJed Brown . F - right hand side 51060e4ef248SJed Brown 51070e4ef248SJed Brown Level: intermediate 51080e4ef248SJed Brown 51090e4ef248SJed Brown Notes: 51100e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 51110e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 51120e4ef248SJed Brown 51130e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 51140e4ef248SJed Brown @*/ 51150910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 51160e4ef248SJed Brown { 51170e4ef248SJed Brown PetscErrorCode ierr; 51180e4ef248SJed Brown Mat Arhs,Brhs; 51190e4ef248SJed Brown 51200e4ef248SJed Brown PetscFunctionBegin; 51210e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 5122d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 51230910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 51240e4ef248SJed Brown PetscFunctionReturn(0); 51250e4ef248SJed Brown } 51260e4ef248SJed Brown 51270e4ef248SJed Brown /*@C 51280e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 51290e4ef248SJed Brown 51300e4ef248SJed Brown Collective on TS 51310e4ef248SJed Brown 51320e4ef248SJed Brown Input Arguments: 51330e4ef248SJed Brown + ts - time stepping context 51340e4ef248SJed Brown . t - time at which to evaluate 51350910c330SBarry Smith . U - state at which to evaluate 51360e4ef248SJed Brown - ctx - context 51370e4ef248SJed Brown 51380e4ef248SJed Brown Output Arguments: 51390e4ef248SJed Brown + A - pointer to operator 51400e4ef248SJed Brown . B - pointer to preconditioning matrix 51410e4ef248SJed Brown - flg - matrix structure flag 51420e4ef248SJed Brown 51430e4ef248SJed Brown Level: intermediate 51440e4ef248SJed Brown 51450e4ef248SJed Brown Notes: 51460e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 51470e4ef248SJed Brown 51480e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 51490e4ef248SJed Brown @*/ 5150d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 51510e4ef248SJed Brown { 51520e4ef248SJed Brown PetscFunctionBegin; 51530e4ef248SJed Brown PetscFunctionReturn(0); 51540e4ef248SJed Brown } 51550e4ef248SJed Brown 51560026cea9SSean Farley /*@C 51570026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 51580026cea9SSean Farley 51590026cea9SSean Farley Collective on TS 51600026cea9SSean Farley 51610026cea9SSean Farley Input Arguments: 51620026cea9SSean Farley + ts - time stepping context 51630026cea9SSean Farley . t - time at which to evaluate 51640910c330SBarry Smith . U - state at which to evaluate 51650910c330SBarry Smith . Udot - time derivative of state vector 51660026cea9SSean Farley - ctx - context 51670026cea9SSean Farley 51680026cea9SSean Farley Output Arguments: 51690026cea9SSean Farley . F - left hand side 51700026cea9SSean Farley 51710026cea9SSean Farley Level: intermediate 51720026cea9SSean Farley 51730026cea9SSean Farley Notes: 51740910c330SBarry 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 51750026cea9SSean Farley user is required to write their own TSComputeIFunction. 51760026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 51770026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 51780026cea9SSean Farley 51799ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 51809ae8fd06SBarry Smith 51819ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 51820026cea9SSean Farley @*/ 51830910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 51840026cea9SSean Farley { 51850026cea9SSean Farley PetscErrorCode ierr; 51860026cea9SSean Farley Mat A,B; 51870026cea9SSean Farley 51880026cea9SSean Farley PetscFunctionBegin; 51890298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 5190d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 51910910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 51920026cea9SSean Farley PetscFunctionReturn(0); 51930026cea9SSean Farley } 51940026cea9SSean Farley 51950026cea9SSean Farley /*@C 5196b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 51970026cea9SSean Farley 51980026cea9SSean Farley Collective on TS 51990026cea9SSean Farley 52000026cea9SSean Farley Input Arguments: 52010026cea9SSean Farley + ts - time stepping context 52020026cea9SSean Farley . t - time at which to evaluate 52030910c330SBarry Smith . U - state at which to evaluate 52040910c330SBarry Smith . Udot - time derivative of state vector 52050026cea9SSean Farley . shift - shift to apply 52060026cea9SSean Farley - ctx - context 52070026cea9SSean Farley 52080026cea9SSean Farley Output Arguments: 52090026cea9SSean Farley + A - pointer to operator 52100026cea9SSean Farley . B - pointer to preconditioning matrix 52110026cea9SSean Farley - flg - matrix structure flag 52120026cea9SSean Farley 5213b41af12eSJed Brown Level: advanced 52140026cea9SSean Farley 52150026cea9SSean Farley Notes: 52160026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 52170026cea9SSean Farley 5218b41af12eSJed Brown It is only appropriate for problems of the form 5219b41af12eSJed Brown 5220b41af12eSJed Brown $ M Udot = F(U,t) 5221b41af12eSJed Brown 5222b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 5223b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 5224b41af12eSJed Brown an implicit operator of the form 5225b41af12eSJed Brown 5226b41af12eSJed Brown $ shift*M + J 5227b41af12eSJed Brown 5228b41af12eSJed 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 5229b41af12eSJed Brown a copy of M or reassemble it when requested. 5230b41af12eSJed Brown 52310026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 52320026cea9SSean Farley @*/ 5233d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 52340026cea9SSean Farley { 5235b41af12eSJed Brown PetscErrorCode ierr; 5236b41af12eSJed Brown 52370026cea9SSean Farley PetscFunctionBegin; 523894ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 5239b41af12eSJed Brown ts->ijacobian.shift = shift; 52400026cea9SSean Farley PetscFunctionReturn(0); 52410026cea9SSean Farley } 5242b41af12eSJed Brown 5243e817cc15SEmil Constantinescu /*@ 5244e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 5245e817cc15SEmil Constantinescu 5246e817cc15SEmil Constantinescu Not Collective 5247e817cc15SEmil Constantinescu 5248e817cc15SEmil Constantinescu Input Parameter: 5249e817cc15SEmil Constantinescu . ts - the TS context 5250e817cc15SEmil Constantinescu 5251e817cc15SEmil Constantinescu Output Parameter: 52524e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 5253e817cc15SEmil Constantinescu 5254e817cc15SEmil Constantinescu Level: beginner 5255e817cc15SEmil Constantinescu 5256e817cc15SEmil Constantinescu .keywords: TS, equation type 5257e817cc15SEmil Constantinescu 5258e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 5259e817cc15SEmil Constantinescu @*/ 5260e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 5261e817cc15SEmil Constantinescu { 5262e817cc15SEmil Constantinescu PetscFunctionBegin; 5263e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5264e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 5265e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 5266e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5267e817cc15SEmil Constantinescu } 5268e817cc15SEmil Constantinescu 5269e817cc15SEmil Constantinescu /*@ 5270e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 5271e817cc15SEmil Constantinescu 5272e817cc15SEmil Constantinescu Not Collective 5273e817cc15SEmil Constantinescu 5274e817cc15SEmil Constantinescu Input Parameter: 5275e817cc15SEmil Constantinescu + ts - the TS context 52761297b224SEmil Constantinescu - equation_type - see TSEquationType 5277e817cc15SEmil Constantinescu 5278e817cc15SEmil Constantinescu Level: advanced 5279e817cc15SEmil Constantinescu 5280e817cc15SEmil Constantinescu .keywords: TS, equation type 5281e817cc15SEmil Constantinescu 5282e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 5283e817cc15SEmil Constantinescu @*/ 5284e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 5285e817cc15SEmil Constantinescu { 5286e817cc15SEmil Constantinescu PetscFunctionBegin; 5287e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5288e817cc15SEmil Constantinescu ts->equation_type = equation_type; 5289e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5290e817cc15SEmil Constantinescu } 52910026cea9SSean Farley 52924af1b03aSJed Brown /*@ 52934af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 52944af1b03aSJed Brown 52954af1b03aSJed Brown Not Collective 52964af1b03aSJed Brown 52974af1b03aSJed Brown Input Parameter: 52984af1b03aSJed Brown . ts - the TS context 52994af1b03aSJed Brown 53004af1b03aSJed Brown Output Parameter: 53014af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 53024af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 53034af1b03aSJed Brown 5304487e0bb9SJed Brown Level: beginner 53054af1b03aSJed Brown 5306cd652676SJed Brown Notes: 5307cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 53084af1b03aSJed Brown 53094af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 53104af1b03aSJed Brown 53114af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 53124af1b03aSJed Brown @*/ 53134af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 53144af1b03aSJed Brown { 53154af1b03aSJed Brown PetscFunctionBegin; 53164af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53174af1b03aSJed Brown PetscValidPointer(reason,2); 53184af1b03aSJed Brown *reason = ts->reason; 53194af1b03aSJed Brown PetscFunctionReturn(0); 53204af1b03aSJed Brown } 53214af1b03aSJed Brown 5322d6ad946cSShri Abhyankar /*@ 5323d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 5324d6ad946cSShri Abhyankar 5325d6ad946cSShri Abhyankar Not Collective 5326d6ad946cSShri Abhyankar 5327d6ad946cSShri Abhyankar Input Parameter: 5328d6ad946cSShri Abhyankar + ts - the TS context 5329d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 5330d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 5331d6ad946cSShri Abhyankar 5332f5abba47SShri Abhyankar Level: advanced 5333d6ad946cSShri Abhyankar 5334d6ad946cSShri Abhyankar Notes: 5335d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 5336d6ad946cSShri Abhyankar 5337d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 5338d6ad946cSShri Abhyankar 5339d6ad946cSShri Abhyankar .seealso: TSConvergedReason 5340d6ad946cSShri Abhyankar @*/ 5341d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 5342d6ad946cSShri Abhyankar { 5343d6ad946cSShri Abhyankar PetscFunctionBegin; 5344d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5345d6ad946cSShri Abhyankar ts->reason = reason; 5346d6ad946cSShri Abhyankar PetscFunctionReturn(0); 5347d6ad946cSShri Abhyankar } 5348d6ad946cSShri Abhyankar 5349cc708dedSBarry Smith /*@ 5350cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 5351cc708dedSBarry Smith 5352cc708dedSBarry Smith Not Collective 5353cc708dedSBarry Smith 5354cc708dedSBarry Smith Input Parameter: 5355cc708dedSBarry Smith . ts - the TS context 5356cc708dedSBarry Smith 5357cc708dedSBarry Smith Output Parameter: 535863e21af5SBarry Smith . ftime - the final time. This time corresponds to the final time set with TSSetDuration() 5359cc708dedSBarry Smith 5360487e0bb9SJed Brown Level: beginner 5361cc708dedSBarry Smith 5362cc708dedSBarry Smith Notes: 5363cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 5364cc708dedSBarry Smith 5365cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 5366cc708dedSBarry Smith 5367cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 5368cc708dedSBarry Smith @*/ 5369cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 5370cc708dedSBarry Smith { 5371cc708dedSBarry Smith PetscFunctionBegin; 5372cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5373cc708dedSBarry Smith PetscValidPointer(ftime,2); 5374cc708dedSBarry Smith *ftime = ts->solvetime; 5375cc708dedSBarry Smith PetscFunctionReturn(0); 5376cc708dedSBarry Smith } 5377cc708dedSBarry Smith 53782c18e0fdSBarry Smith /*@ 53792c18e0fdSBarry Smith TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 53802c18e0fdSBarry Smith 53812c18e0fdSBarry Smith Not Collective 53822c18e0fdSBarry Smith 53832c18e0fdSBarry Smith Input Parameter: 53842c18e0fdSBarry Smith . ts - the TS context 53852c18e0fdSBarry Smith 53862c18e0fdSBarry Smith Output Parameter: 53872c18e0fdSBarry Smith . steps - the number of steps 53882c18e0fdSBarry Smith 53892c18e0fdSBarry Smith Level: beginner 53902c18e0fdSBarry Smith 53912c18e0fdSBarry Smith Notes: 53922c18e0fdSBarry Smith Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 53932c18e0fdSBarry Smith 53942c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test 53952c18e0fdSBarry Smith 53962c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 53972c18e0fdSBarry Smith @*/ 53982c18e0fdSBarry Smith PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 53992c18e0fdSBarry Smith { 54002c18e0fdSBarry Smith PetscFunctionBegin; 54012c18e0fdSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 54022c18e0fdSBarry Smith PetscValidPointer(steps,2); 54032c18e0fdSBarry Smith *steps = ts->total_steps; 54042c18e0fdSBarry Smith PetscFunctionReturn(0); 54052c18e0fdSBarry Smith } 54062c18e0fdSBarry Smith 54079f67acb7SJed Brown /*@ 54085ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 54099f67acb7SJed Brown used by the time integrator. 54109f67acb7SJed Brown 54119f67acb7SJed Brown Not Collective 54129f67acb7SJed Brown 54139f67acb7SJed Brown Input Parameter: 54149f67acb7SJed Brown . ts - TS context 54159f67acb7SJed Brown 54169f67acb7SJed Brown Output Parameter: 54179f67acb7SJed Brown . nits - number of nonlinear iterations 54189f67acb7SJed Brown 54199f67acb7SJed Brown Notes: 54209f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 54219f67acb7SJed Brown 54229f67acb7SJed Brown Level: intermediate 54239f67acb7SJed Brown 54249f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 54259f67acb7SJed Brown 54265ef26d82SJed Brown .seealso: TSGetKSPIterations() 54279f67acb7SJed Brown @*/ 54285ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 54299f67acb7SJed Brown { 54309f67acb7SJed Brown PetscFunctionBegin; 54319f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 54329f67acb7SJed Brown PetscValidIntPointer(nits,2); 54335ef26d82SJed Brown *nits = ts->snes_its; 54349f67acb7SJed Brown PetscFunctionReturn(0); 54359f67acb7SJed Brown } 54369f67acb7SJed Brown 54379f67acb7SJed Brown /*@ 54385ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 54399f67acb7SJed Brown used by the time integrator. 54409f67acb7SJed Brown 54419f67acb7SJed Brown Not Collective 54429f67acb7SJed Brown 54439f67acb7SJed Brown Input Parameter: 54449f67acb7SJed Brown . ts - TS context 54459f67acb7SJed Brown 54469f67acb7SJed Brown Output Parameter: 54479f67acb7SJed Brown . lits - number of linear iterations 54489f67acb7SJed Brown 54499f67acb7SJed Brown Notes: 54509f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 54519f67acb7SJed Brown 54529f67acb7SJed Brown Level: intermediate 54539f67acb7SJed Brown 54549f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 54559f67acb7SJed Brown 54565ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 54579f67acb7SJed Brown @*/ 54585ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 54599f67acb7SJed Brown { 54609f67acb7SJed Brown PetscFunctionBegin; 54619f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 54629f67acb7SJed Brown PetscValidIntPointer(lits,2); 54635ef26d82SJed Brown *lits = ts->ksp_its; 54649f67acb7SJed Brown PetscFunctionReturn(0); 54659f67acb7SJed Brown } 54669f67acb7SJed Brown 5467cef5090cSJed Brown /*@ 5468cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 5469cef5090cSJed Brown 5470cef5090cSJed Brown Not Collective 5471cef5090cSJed Brown 5472cef5090cSJed Brown Input Parameter: 5473cef5090cSJed Brown . ts - TS context 5474cef5090cSJed Brown 5475cef5090cSJed Brown Output Parameter: 5476cef5090cSJed Brown . rejects - number of steps rejected 5477cef5090cSJed Brown 5478cef5090cSJed Brown Notes: 5479cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5480cef5090cSJed Brown 5481cef5090cSJed Brown Level: intermediate 5482cef5090cSJed Brown 5483cef5090cSJed Brown .keywords: TS, get, number 5484cef5090cSJed Brown 54855ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 5486cef5090cSJed Brown @*/ 5487cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 5488cef5090cSJed Brown { 5489cef5090cSJed Brown PetscFunctionBegin; 5490cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5491cef5090cSJed Brown PetscValidIntPointer(rejects,2); 5492cef5090cSJed Brown *rejects = ts->reject; 5493cef5090cSJed Brown PetscFunctionReturn(0); 5494cef5090cSJed Brown } 5495cef5090cSJed Brown 5496cef5090cSJed Brown /*@ 5497cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 5498cef5090cSJed Brown 5499cef5090cSJed Brown Not Collective 5500cef5090cSJed Brown 5501cef5090cSJed Brown Input Parameter: 5502cef5090cSJed Brown . ts - TS context 5503cef5090cSJed Brown 5504cef5090cSJed Brown Output Parameter: 5505cef5090cSJed Brown . fails - number of failed nonlinear solves 5506cef5090cSJed Brown 5507cef5090cSJed Brown Notes: 5508cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5509cef5090cSJed Brown 5510cef5090cSJed Brown Level: intermediate 5511cef5090cSJed Brown 5512cef5090cSJed Brown .keywords: TS, get, number 5513cef5090cSJed Brown 55145ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 5515cef5090cSJed Brown @*/ 5516cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 5517cef5090cSJed Brown { 5518cef5090cSJed Brown PetscFunctionBegin; 5519cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5520cef5090cSJed Brown PetscValidIntPointer(fails,2); 5521cef5090cSJed Brown *fails = ts->num_snes_failures; 5522cef5090cSJed Brown PetscFunctionReturn(0); 5523cef5090cSJed Brown } 5524cef5090cSJed Brown 5525cef5090cSJed Brown /*@ 5526cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 5527cef5090cSJed Brown 5528cef5090cSJed Brown Not Collective 5529cef5090cSJed Brown 5530cef5090cSJed Brown Input Parameter: 5531cef5090cSJed Brown + ts - TS context 5532cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 5533cef5090cSJed Brown 5534cef5090cSJed Brown Notes: 5535cef5090cSJed Brown The counter is reset to zero for each step 5536cef5090cSJed Brown 5537cef5090cSJed Brown Options Database Key: 5538cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 5539cef5090cSJed Brown 5540cef5090cSJed Brown Level: intermediate 5541cef5090cSJed Brown 5542cef5090cSJed Brown .keywords: TS, set, maximum, number 5543cef5090cSJed Brown 55445ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5545cef5090cSJed Brown @*/ 5546cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 5547cef5090cSJed Brown { 5548cef5090cSJed Brown PetscFunctionBegin; 5549cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5550cef5090cSJed Brown ts->max_reject = rejects; 5551cef5090cSJed Brown PetscFunctionReturn(0); 5552cef5090cSJed Brown } 5553cef5090cSJed Brown 5554cef5090cSJed Brown /*@ 5555cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 5556cef5090cSJed Brown 5557cef5090cSJed Brown Not Collective 5558cef5090cSJed Brown 5559cef5090cSJed Brown Input Parameter: 5560cef5090cSJed Brown + ts - TS context 5561cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 5562cef5090cSJed Brown 5563cef5090cSJed Brown Notes: 5564cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 5565cef5090cSJed Brown 5566cef5090cSJed Brown Options Database Key: 5567cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 5568cef5090cSJed Brown 5569cef5090cSJed Brown Level: intermediate 5570cef5090cSJed Brown 5571cef5090cSJed Brown .keywords: TS, set, maximum, number 5572cef5090cSJed Brown 55735ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 5574cef5090cSJed Brown @*/ 5575cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 5576cef5090cSJed Brown { 5577cef5090cSJed Brown PetscFunctionBegin; 5578cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5579cef5090cSJed Brown ts->max_snes_failures = fails; 5580cef5090cSJed Brown PetscFunctionReturn(0); 5581cef5090cSJed Brown } 5582cef5090cSJed Brown 5583cef5090cSJed Brown /*@ 5584cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 5585cef5090cSJed Brown 5586cef5090cSJed Brown Not Collective 5587cef5090cSJed Brown 5588cef5090cSJed Brown Input Parameter: 5589cef5090cSJed Brown + ts - TS context 5590cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 5591cef5090cSJed Brown 5592cef5090cSJed Brown Options Database Key: 5593cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 5594cef5090cSJed Brown 5595cef5090cSJed Brown Level: intermediate 5596cef5090cSJed Brown 5597cef5090cSJed Brown .keywords: TS, set, error 5598cef5090cSJed Brown 55995ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5600cef5090cSJed Brown @*/ 5601cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 5602cef5090cSJed Brown { 5603cef5090cSJed Brown PetscFunctionBegin; 5604cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5605cef5090cSJed Brown ts->errorifstepfailed = err; 5606cef5090cSJed Brown PetscFunctionReturn(0); 5607cef5090cSJed Brown } 5608cef5090cSJed Brown 5609fb1732b5SBarry Smith /*@C 5610fde5950dSBarry 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 5611fb1732b5SBarry Smith 5612fb1732b5SBarry Smith Collective on TS 5613fb1732b5SBarry Smith 5614fb1732b5SBarry Smith Input Parameters: 5615fb1732b5SBarry Smith + ts - the TS context 5616fb1732b5SBarry Smith . step - current time-step 5617fb1732b5SBarry Smith . ptime - current time 56180910c330SBarry Smith . u - current state 5619721cd6eeSBarry Smith - vf - viewer and its format 5620fb1732b5SBarry Smith 5621fb1732b5SBarry Smith Level: intermediate 5622fb1732b5SBarry Smith 5623fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 5624fb1732b5SBarry Smith 5625fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5626fb1732b5SBarry Smith @*/ 5627721cd6eeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf) 5628fb1732b5SBarry Smith { 5629fb1732b5SBarry Smith PetscErrorCode ierr; 5630fb1732b5SBarry Smith 5631fb1732b5SBarry Smith PetscFunctionBegin; 5632721cd6eeSBarry Smith ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr); 5633721cd6eeSBarry Smith ierr = VecView(u,vf->viewer);CHKERRQ(ierr); 5634721cd6eeSBarry Smith ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr); 5635ed81e22dSJed Brown PetscFunctionReturn(0); 5636ed81e22dSJed Brown } 5637ed81e22dSJed Brown 5638ed81e22dSJed Brown /*@C 5639ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5640ed81e22dSJed Brown 5641ed81e22dSJed Brown Collective on TS 5642ed81e22dSJed Brown 5643ed81e22dSJed Brown Input Parameters: 5644ed81e22dSJed Brown + ts - the TS context 5645ed81e22dSJed Brown . step - current time-step 5646ed81e22dSJed Brown . ptime - current time 56470910c330SBarry Smith . u - current state 5648ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5649ed81e22dSJed Brown 5650ed81e22dSJed Brown Level: intermediate 5651ed81e22dSJed Brown 5652ed81e22dSJed Brown Notes: 5653ed81e22dSJed 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. 5654ed81e22dSJed Brown These are named according to the file name template. 5655ed81e22dSJed Brown 5656ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5657ed81e22dSJed Brown 5658ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5659ed81e22dSJed Brown 5660ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5661ed81e22dSJed Brown @*/ 56620910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5663ed81e22dSJed Brown { 5664ed81e22dSJed Brown PetscErrorCode ierr; 5665ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 5666ed81e22dSJed Brown PetscViewer viewer; 5667ed81e22dSJed Brown 5668ed81e22dSJed Brown PetscFunctionBegin; 566963e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 56708caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5671ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 56720910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 5673ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5674ed81e22dSJed Brown PetscFunctionReturn(0); 5675ed81e22dSJed Brown } 5676ed81e22dSJed Brown 5677ed81e22dSJed Brown /*@C 5678ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5679ed81e22dSJed Brown 5680ed81e22dSJed Brown Collective on TS 5681ed81e22dSJed Brown 5682ed81e22dSJed Brown Input Parameters: 5683ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5684ed81e22dSJed Brown 5685ed81e22dSJed Brown Level: intermediate 5686ed81e22dSJed Brown 5687ed81e22dSJed Brown Note: 5688ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5689ed81e22dSJed Brown 5690ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5691ed81e22dSJed Brown 5692ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5693ed81e22dSJed Brown @*/ 5694ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5695ed81e22dSJed Brown { 5696ed81e22dSJed Brown PetscErrorCode ierr; 5697ed81e22dSJed Brown 5698ed81e22dSJed Brown PetscFunctionBegin; 5699ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5700fb1732b5SBarry Smith PetscFunctionReturn(0); 5701fb1732b5SBarry Smith } 5702fb1732b5SBarry Smith 570384df9cb4SJed Brown /*@ 5704552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 570584df9cb4SJed Brown 5706ed81e22dSJed Brown Collective on TS if controller has not been created yet 570784df9cb4SJed Brown 570884df9cb4SJed Brown Input Arguments: 5709ed81e22dSJed Brown . ts - time stepping context 571084df9cb4SJed Brown 571184df9cb4SJed Brown Output Arguments: 5712ed81e22dSJed Brown . adapt - adaptive controller 571384df9cb4SJed Brown 571484df9cb4SJed Brown Level: intermediate 571584df9cb4SJed Brown 5716ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 571784df9cb4SJed Brown @*/ 5718552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 571984df9cb4SJed Brown { 572084df9cb4SJed Brown PetscErrorCode ierr; 572184df9cb4SJed Brown 572284df9cb4SJed Brown PetscFunctionBegin; 572384df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5724bec58848SLisandro Dalcin PetscValidPointer(adapt,2); 572584df9cb4SJed Brown if (!ts->adapt) { 5726ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 57273bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 57281c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 572984df9cb4SJed Brown } 5730bec58848SLisandro Dalcin *adapt = ts->adapt; 573184df9cb4SJed Brown PetscFunctionReturn(0); 573284df9cb4SJed Brown } 5733d6ebe24aSShri Abhyankar 57341c3436cfSJed Brown /*@ 57351c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 57361c3436cfSJed Brown 57371c3436cfSJed Brown Logically Collective 57381c3436cfSJed Brown 57391c3436cfSJed Brown Input Arguments: 57401c3436cfSJed Brown + ts - time integration context 57411c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 57420298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 57431c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 57440298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 57451c3436cfSJed Brown 5746a3cdaa26SBarry Smith Options Database keys: 5747a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5748a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5749a3cdaa26SBarry Smith 57503ff766beSShri Abhyankar Notes: 57513ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 57523ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 57533ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 57543ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 57553ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 57563ff766beSShri Abhyankar differential variables. 57573ff766beSShri Abhyankar 57581c3436cfSJed Brown Level: beginner 57591c3436cfSJed Brown 5760c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 57611c3436cfSJed Brown @*/ 57621c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 57631c3436cfSJed Brown { 57641c3436cfSJed Brown PetscErrorCode ierr; 57651c3436cfSJed Brown 57661c3436cfSJed Brown PetscFunctionBegin; 5767c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 57681c3436cfSJed Brown if (vatol) { 57691c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 57701c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 57711c3436cfSJed Brown ts->vatol = vatol; 57721c3436cfSJed Brown } 5773c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 57741c3436cfSJed Brown if (vrtol) { 57751c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 57761c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 57771c3436cfSJed Brown ts->vrtol = vrtol; 57781c3436cfSJed Brown } 57791c3436cfSJed Brown PetscFunctionReturn(0); 57801c3436cfSJed Brown } 57811c3436cfSJed Brown 5782c5033834SJed Brown /*@ 5783c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5784c5033834SJed Brown 5785c5033834SJed Brown Logically Collective 5786c5033834SJed Brown 5787c5033834SJed Brown Input Arguments: 5788c5033834SJed Brown . ts - time integration context 5789c5033834SJed Brown 5790c5033834SJed Brown Output Arguments: 57910298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 57920298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 57930298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 57940298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 5795c5033834SJed Brown 5796c5033834SJed Brown Level: beginner 5797c5033834SJed Brown 5798c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5799c5033834SJed Brown @*/ 5800c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5801c5033834SJed Brown { 5802c5033834SJed Brown PetscFunctionBegin; 5803c5033834SJed Brown if (atol) *atol = ts->atol; 5804c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5805c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5806c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 5807c5033834SJed Brown PetscFunctionReturn(0); 5808c5033834SJed Brown } 5809c5033834SJed Brown 58109c6b16b5SShri Abhyankar /*@ 5811a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 58129c6b16b5SShri Abhyankar 58139c6b16b5SShri Abhyankar Collective on TS 58149c6b16b5SShri Abhyankar 58159c6b16b5SShri Abhyankar Input Arguments: 58169c6b16b5SShri Abhyankar + ts - time stepping context 5817a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5818a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 58199c6b16b5SShri Abhyankar 58209c6b16b5SShri Abhyankar Output Arguments: 58217453f775SEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 58227453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 58237453f775SEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 58249c6b16b5SShri Abhyankar 58259c6b16b5SShri Abhyankar Level: developer 58269c6b16b5SShri Abhyankar 5827deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 58289c6b16b5SShri Abhyankar @*/ 58297453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 58309c6b16b5SShri Abhyankar { 58319c6b16b5SShri Abhyankar PetscErrorCode ierr; 58329c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 58337453f775SEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 58347453f775SEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 58359c6b16b5SShri Abhyankar const PetscScalar *u,*y; 58367453f775SEmil Constantinescu PetscReal sum,suma,sumr,gsum,gsuma,gsumr,diff; 58377453f775SEmil Constantinescu PetscReal tol,tola,tolr; 58387453f775SEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 58399c6b16b5SShri Abhyankar 58409c6b16b5SShri Abhyankar PetscFunctionBegin; 58419c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5842a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5843a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5844a4868fbcSLisandro Dalcin PetscValidType(U,2); 5845a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5846a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5847a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 58488a175baeSEmil Constantinescu PetscValidPointer(norma,5); 58498a175baeSEmil Constantinescu PetscValidPointer(normr,6); 5850a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 58519c6b16b5SShri Abhyankar 58529c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 58539c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 58549c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 58559c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 58569c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 58577453f775SEmil Constantinescu sum = 0.; n_loc = 0; 58587453f775SEmil Constantinescu suma = 0.; na_loc = 0; 58597453f775SEmil Constantinescu sumr = 0.; nr_loc = 0; 58609c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 58619c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 58629c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58639c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58649c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 58657453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 58667453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 58677453f775SEmil Constantinescu if(tola>0.){ 58687453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 58697453f775SEmil Constantinescu na_loc++; 58707453f775SEmil Constantinescu } 58717453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58727453f775SEmil Constantinescu if(tolr>0.){ 58737453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 58747453f775SEmil Constantinescu nr_loc++; 58757453f775SEmil Constantinescu } 58767453f775SEmil Constantinescu tol=tola+tolr; 58777453f775SEmil Constantinescu if(tol>0.){ 58787453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 58797453f775SEmil Constantinescu n_loc++; 58807453f775SEmil Constantinescu } 58819c6b16b5SShri Abhyankar } 58829c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58839c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58849c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 58859c6b16b5SShri Abhyankar const PetscScalar *atol; 58869c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58879c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 58887453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 58897453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 58907453f775SEmil Constantinescu if(tola>0.){ 58917453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 58927453f775SEmil Constantinescu na_loc++; 58937453f775SEmil Constantinescu } 58947453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58957453f775SEmil Constantinescu if(tolr>0.){ 58967453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 58977453f775SEmil Constantinescu nr_loc++; 58987453f775SEmil Constantinescu } 58997453f775SEmil Constantinescu tol=tola+tolr; 59007453f775SEmil Constantinescu if(tol>0.){ 59017453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 59027453f775SEmil Constantinescu n_loc++; 59037453f775SEmil Constantinescu } 59049c6b16b5SShri Abhyankar } 59059c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59069c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 59079c6b16b5SShri Abhyankar const PetscScalar *rtol; 59089c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59099c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 59107453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 59117453f775SEmil Constantinescu tola = ts->atol; 59127453f775SEmil Constantinescu if(tola>0.){ 59137453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 59147453f775SEmil Constantinescu na_loc++; 59157453f775SEmil Constantinescu } 59167453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59177453f775SEmil Constantinescu if(tolr>0.){ 59187453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 59197453f775SEmil Constantinescu nr_loc++; 59207453f775SEmil Constantinescu } 59217453f775SEmil Constantinescu tol=tola+tolr; 59227453f775SEmil Constantinescu if(tol>0.){ 59237453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 59247453f775SEmil Constantinescu n_loc++; 59257453f775SEmil Constantinescu } 59269c6b16b5SShri Abhyankar } 59279c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59289c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 59299c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 59307453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 59317453f775SEmil Constantinescu tola = ts->atol; 59327453f775SEmil Constantinescu if(tola>0.){ 59337453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 59347453f775SEmil Constantinescu na_loc++; 59357453f775SEmil Constantinescu } 59367453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59377453f775SEmil Constantinescu if(tolr>0.){ 59387453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 59397453f775SEmil Constantinescu nr_loc++; 59407453f775SEmil Constantinescu } 59417453f775SEmil Constantinescu tol=tola+tolr; 59427453f775SEmil Constantinescu if(tol>0.){ 59437453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 59447453f775SEmil Constantinescu n_loc++; 59457453f775SEmil Constantinescu } 59469c6b16b5SShri Abhyankar } 59479c6b16b5SShri Abhyankar } 59489c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 59499c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 59509c6b16b5SShri Abhyankar 59517453f775SEmil Constantinescu err_loc[0] = sum; 59527453f775SEmil Constantinescu err_loc[1] = suma; 59537453f775SEmil Constantinescu err_loc[2] = sumr; 59547453f775SEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 59557453f775SEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 59567453f775SEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 59577453f775SEmil Constantinescu 5958a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 59597453f775SEmil Constantinescu 59607453f775SEmil Constantinescu gsum = err_glb[0]; 59617453f775SEmil Constantinescu gsuma = err_glb[1]; 59627453f775SEmil Constantinescu gsumr = err_glb[2]; 59637453f775SEmil Constantinescu n_glb = err_glb[3]; 59647453f775SEmil Constantinescu na_glb = err_glb[4]; 59657453f775SEmil Constantinescu nr_glb = err_glb[5]; 59667453f775SEmil Constantinescu 5967b1316ef9SEmil Constantinescu *norm = 0.; 5968b1316ef9SEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 5969b1316ef9SEmil Constantinescu *norma = 0.; 5970b1316ef9SEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 5971b1316ef9SEmil Constantinescu *normr = 0.; 5972b1316ef9SEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 59739c6b16b5SShri Abhyankar 59749c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 59757453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 59767453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 59779c6b16b5SShri Abhyankar PetscFunctionReturn(0); 59789c6b16b5SShri Abhyankar } 59799c6b16b5SShri Abhyankar 59809c6b16b5SShri Abhyankar /*@ 5981a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 59829c6b16b5SShri Abhyankar 59839c6b16b5SShri Abhyankar Collective on TS 59849c6b16b5SShri Abhyankar 59859c6b16b5SShri Abhyankar Input Arguments: 59869c6b16b5SShri Abhyankar + ts - time stepping context 5987a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5988a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 59899c6b16b5SShri Abhyankar 59909c6b16b5SShri Abhyankar Output Arguments: 59917453f775SEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 59927453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 59937453f775SEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 59949c6b16b5SShri Abhyankar 59959c6b16b5SShri Abhyankar Level: developer 59969c6b16b5SShri Abhyankar 5997deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 59989c6b16b5SShri Abhyankar @*/ 59997453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 60009c6b16b5SShri Abhyankar { 60019c6b16b5SShri Abhyankar PetscErrorCode ierr; 60027453f775SEmil Constantinescu PetscInt i,n,N,rstart; 60039c6b16b5SShri Abhyankar const PetscScalar *u,*y; 60047453f775SEmil Constantinescu PetscReal max,gmax,maxa,gmaxa,maxr,gmaxr; 60057453f775SEmil Constantinescu PetscReal tol,tola,tolr,diff; 60067453f775SEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 60079c6b16b5SShri Abhyankar 60089c6b16b5SShri Abhyankar PetscFunctionBegin; 60099c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6010a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 6011a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 6012a4868fbcSLisandro Dalcin PetscValidType(U,2); 6013a4868fbcSLisandro Dalcin PetscValidType(Y,3); 6014a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 6015a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 60168a175baeSEmil Constantinescu PetscValidPointer(norma,5); 60178a175baeSEmil Constantinescu PetscValidPointer(normr,6); 6018a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 60199c6b16b5SShri Abhyankar 60209c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 60219c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 60229c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 60239c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 60249c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 60257453f775SEmil Constantinescu 60267453f775SEmil Constantinescu max=0.; 60277453f775SEmil Constantinescu maxa=0.; 60287453f775SEmil Constantinescu maxr=0.; 60297453f775SEmil Constantinescu 60307453f775SEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 60319c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 60329c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 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 = PetscRealPart(atol[i]); 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->vatol,&atol);CHKERRQ(ierr); 60519c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60529c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 60539c6b16b5SShri Abhyankar const PetscScalar *atol; 60549c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 60557453f775SEmil Constantinescu for (i=0; i<n; i++) { 60567453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 60577453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 60587453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60597453f775SEmil Constantinescu tol = tola+tolr; 60607453f775SEmil Constantinescu if(tola>0.){ 60617453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 60627453f775SEmil Constantinescu } 60637453f775SEmil Constantinescu if(tolr>0.){ 60647453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 60657453f775SEmil Constantinescu } 60667453f775SEmil Constantinescu if(tol>0.){ 60677453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 60687453f775SEmil Constantinescu } 60699c6b16b5SShri Abhyankar } 60709c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 60719c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 60729c6b16b5SShri Abhyankar const PetscScalar *rtol; 60739c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60747453f775SEmil Constantinescu 60757453f775SEmil Constantinescu for (i=0; i<n; i++) { 60767453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 60777453f775SEmil Constantinescu tola = ts->atol; 60787453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60797453f775SEmil Constantinescu tol = tola+tolr; 60807453f775SEmil Constantinescu if(tola>0.){ 60817453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 60827453f775SEmil Constantinescu } 60837453f775SEmil Constantinescu if(tolr>0.){ 60847453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 60857453f775SEmil Constantinescu } 60867453f775SEmil Constantinescu if(tol>0.){ 60877453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 60887453f775SEmil Constantinescu } 60899c6b16b5SShri Abhyankar } 60909c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60919c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 60927453f775SEmil Constantinescu 60937453f775SEmil Constantinescu for (i=0; i<n; i++) { 60947453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 60957453f775SEmil Constantinescu tola = ts->atol; 60967453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60977453f775SEmil Constantinescu tol = tola+tolr; 60987453f775SEmil Constantinescu if(tola>0.){ 60997453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 61007453f775SEmil Constantinescu } 61017453f775SEmil Constantinescu if(tolr>0.){ 61027453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 61037453f775SEmil Constantinescu } 61047453f775SEmil Constantinescu if(tol>0.){ 61057453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 61067453f775SEmil Constantinescu } 61079c6b16b5SShri Abhyankar } 61089c6b16b5SShri Abhyankar } 61099c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 61109c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 61117453f775SEmil Constantinescu err_loc[0] = max; 61127453f775SEmil Constantinescu err_loc[1] = maxa; 61137453f775SEmil Constantinescu err_loc[2] = maxr; 6114a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 61157453f775SEmil Constantinescu gmax = err_glb[0]; 61167453f775SEmil Constantinescu gmaxa = err_glb[1]; 61177453f775SEmil Constantinescu gmaxr = err_glb[2]; 61189c6b16b5SShri Abhyankar 61199c6b16b5SShri Abhyankar *norm = gmax; 61207453f775SEmil Constantinescu *norma = gmaxa; 61217453f775SEmil Constantinescu *normr = gmaxr; 61229c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 61237453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 61247453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 61259c6b16b5SShri Abhyankar PetscFunctionReturn(0); 61269c6b16b5SShri Abhyankar } 61279c6b16b5SShri Abhyankar 61281c3436cfSJed Brown /*@ 61298a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 61301c3436cfSJed Brown 61311c3436cfSJed Brown Collective on TS 61321c3436cfSJed Brown 61331c3436cfSJed Brown Input Arguments: 61341c3436cfSJed Brown + ts - time stepping context 6135a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 6136a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 6137a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 61387619abb3SShri 61391c3436cfSJed Brown Output Arguments: 61408a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 61418a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 61428a175baeSEmil Constantinescu . normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 6143a4868fbcSLisandro Dalcin 6144a4868fbcSLisandro Dalcin Options Database Keys: 6145a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 6146a4868fbcSLisandro Dalcin 61471c3436cfSJed Brown Level: developer 61481c3436cfSJed Brown 61498a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm 61501c3436cfSJed Brown @*/ 61517453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 61521c3436cfSJed Brown { 61538beabaa1SBarry Smith PetscErrorCode ierr; 61541c3436cfSJed Brown 61551c3436cfSJed Brown PetscFunctionBegin; 6156a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 61577453f775SEmil Constantinescu ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 6158a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 61597453f775SEmil Constantinescu ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 6160a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 61611c3436cfSJed Brown PetscFunctionReturn(0); 61621c3436cfSJed Brown } 61631c3436cfSJed Brown 61648a175baeSEmil Constantinescu 61658a175baeSEmil Constantinescu /*@ 61668a175baeSEmil Constantinescu TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances 61678a175baeSEmil Constantinescu 61688a175baeSEmil Constantinescu Collective on TS 61698a175baeSEmil Constantinescu 61708a175baeSEmil Constantinescu Input Arguments: 61718a175baeSEmil Constantinescu + ts - time stepping context 61728a175baeSEmil Constantinescu . E - error vector 61738a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 61748a175baeSEmil Constantinescu - Y - state vector, previous time step 61758a175baeSEmil Constantinescu 61768a175baeSEmil Constantinescu Output Arguments: 61778a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 61788a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 61798a175baeSEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 61808a175baeSEmil Constantinescu 61818a175baeSEmil Constantinescu Level: developer 61828a175baeSEmil Constantinescu 61838a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity() 61848a175baeSEmil Constantinescu @*/ 61858a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 61868a175baeSEmil Constantinescu { 61878a175baeSEmil Constantinescu PetscErrorCode ierr; 61888a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 61898a175baeSEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 61908a175baeSEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 61918a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 61928a175baeSEmil Constantinescu PetscReal err,sum,suma,sumr,gsum,gsuma,gsumr; 61938a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 61948a175baeSEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 61958a175baeSEmil Constantinescu 61968a175baeSEmil Constantinescu PetscFunctionBegin; 61978a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 61988a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 61998a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 62008a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 62018a175baeSEmil Constantinescu PetscValidType(E,2); 62028a175baeSEmil Constantinescu PetscValidType(U,3); 62038a175baeSEmil Constantinescu PetscValidType(Y,4); 62048a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 62058a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 62068a175baeSEmil Constantinescu PetscValidPointer(norm,5); 62078a175baeSEmil Constantinescu PetscValidPointer(norma,6); 62088a175baeSEmil Constantinescu PetscValidPointer(normr,7); 62098a175baeSEmil Constantinescu 62108a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 62118a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 62128a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 62138a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 62148a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 62158a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 62168a175baeSEmil Constantinescu sum = 0.; n_loc = 0; 62178a175baeSEmil Constantinescu suma = 0.; na_loc = 0; 62188a175baeSEmil Constantinescu sumr = 0.; nr_loc = 0; 62198a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { 62208a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 62218a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 62228a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 62238a175baeSEmil Constantinescu for (i=0; i<n; i++) { 62248a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 62258a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 62268a175baeSEmil Constantinescu if(tola>0.){ 62278a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 62288a175baeSEmil Constantinescu na_loc++; 62298a175baeSEmil Constantinescu } 62308a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 62318a175baeSEmil Constantinescu if(tolr>0.){ 62328a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 62338a175baeSEmil Constantinescu nr_loc++; 62348a175baeSEmil Constantinescu } 62358a175baeSEmil Constantinescu tol=tola+tolr; 62368a175baeSEmil Constantinescu if(tol>0.){ 62378a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 62388a175baeSEmil Constantinescu n_loc++; 62398a175baeSEmil Constantinescu } 62408a175baeSEmil Constantinescu } 62418a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 62428a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 62438a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 62448a175baeSEmil Constantinescu const PetscScalar *atol; 62458a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 62468a175baeSEmil Constantinescu for (i=0; i<n; i++) { 62478a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 62488a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 62498a175baeSEmil Constantinescu if(tola>0.){ 62508a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 62518a175baeSEmil Constantinescu na_loc++; 62528a175baeSEmil Constantinescu } 62538a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 62548a175baeSEmil Constantinescu if(tolr>0.){ 62558a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 62568a175baeSEmil Constantinescu nr_loc++; 62578a175baeSEmil Constantinescu } 62588a175baeSEmil Constantinescu tol=tola+tolr; 62598a175baeSEmil Constantinescu if(tol>0.){ 62608a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 62618a175baeSEmil Constantinescu n_loc++; 62628a175baeSEmil Constantinescu } 62638a175baeSEmil Constantinescu } 62648a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 62658a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 62668a175baeSEmil Constantinescu const PetscScalar *rtol; 62678a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 62688a175baeSEmil Constantinescu for (i=0; i<n; i++) { 62698a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 62708a175baeSEmil Constantinescu tola = ts->atol; 62718a175baeSEmil Constantinescu if(tola>0.){ 62728a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 62738a175baeSEmil Constantinescu na_loc++; 62748a175baeSEmil Constantinescu } 62758a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 62768a175baeSEmil Constantinescu if(tolr>0.){ 62778a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 62788a175baeSEmil Constantinescu nr_loc++; 62798a175baeSEmil Constantinescu } 62808a175baeSEmil Constantinescu tol=tola+tolr; 62818a175baeSEmil Constantinescu if(tol>0.){ 62828a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 62838a175baeSEmil Constantinescu n_loc++; 62848a175baeSEmil Constantinescu } 62858a175baeSEmil Constantinescu } 62868a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 62878a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 62888a175baeSEmil Constantinescu for (i=0; i<n; i++) { 62898a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 62908a175baeSEmil Constantinescu tola = ts->atol; 62918a175baeSEmil Constantinescu if(tola>0.){ 62928a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 62938a175baeSEmil Constantinescu na_loc++; 62948a175baeSEmil Constantinescu } 62958a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 62968a175baeSEmil Constantinescu if(tolr>0.){ 62978a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 62988a175baeSEmil Constantinescu nr_loc++; 62998a175baeSEmil Constantinescu } 63008a175baeSEmil Constantinescu tol=tola+tolr; 63018a175baeSEmil Constantinescu if(tol>0.){ 63028a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 63038a175baeSEmil Constantinescu n_loc++; 63048a175baeSEmil Constantinescu } 63058a175baeSEmil Constantinescu } 63068a175baeSEmil Constantinescu } 63078a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 63088a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 63098a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 63108a175baeSEmil Constantinescu 63118a175baeSEmil Constantinescu err_loc[0] = sum; 63128a175baeSEmil Constantinescu err_loc[1] = suma; 63138a175baeSEmil Constantinescu err_loc[2] = sumr; 63148a175baeSEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 63158a175baeSEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 63168a175baeSEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 63178a175baeSEmil Constantinescu 6318a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 63198a175baeSEmil Constantinescu 63208a175baeSEmil Constantinescu gsum = err_glb[0]; 63218a175baeSEmil Constantinescu gsuma = err_glb[1]; 63228a175baeSEmil Constantinescu gsumr = err_glb[2]; 63238a175baeSEmil Constantinescu n_glb = err_glb[3]; 63248a175baeSEmil Constantinescu na_glb = err_glb[4]; 63258a175baeSEmil Constantinescu nr_glb = err_glb[5]; 63268a175baeSEmil Constantinescu 63278a175baeSEmil Constantinescu *norm = 0.; 63288a175baeSEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 63298a175baeSEmil Constantinescu *norma = 0.; 63308a175baeSEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 63318a175baeSEmil Constantinescu *normr = 0.; 63328a175baeSEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 63338a175baeSEmil Constantinescu 63348a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 63358a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 63368a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 63378a175baeSEmil Constantinescu PetscFunctionReturn(0); 63388a175baeSEmil Constantinescu } 63398a175baeSEmil Constantinescu 63408a175baeSEmil Constantinescu /*@ 63418a175baeSEmil Constantinescu TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances 63428a175baeSEmil Constantinescu Collective on TS 63438a175baeSEmil Constantinescu 63448a175baeSEmil Constantinescu Input Arguments: 63458a175baeSEmil Constantinescu + ts - time stepping context 63468a175baeSEmil Constantinescu . E - error vector 63478a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 63488a175baeSEmil Constantinescu - Y - state vector, previous time step 63498a175baeSEmil Constantinescu 63508a175baeSEmil Constantinescu Output Arguments: 63518a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 63528a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 63538a175baeSEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 63548a175baeSEmil Constantinescu 63558a175baeSEmil Constantinescu Level: developer 63568a175baeSEmil Constantinescu 63578a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2() 63588a175baeSEmil Constantinescu @*/ 63598a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 63608a175baeSEmil Constantinescu { 63618a175baeSEmil Constantinescu PetscErrorCode ierr; 63628a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 63638a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 63648a175baeSEmil Constantinescu PetscReal err,max,gmax,maxa,gmaxa,maxr,gmaxr; 63658a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 63668a175baeSEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 63678a175baeSEmil Constantinescu 63688a175baeSEmil Constantinescu PetscFunctionBegin; 63698a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 63708a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 63718a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 63728a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 63738a175baeSEmil Constantinescu PetscValidType(E,2); 63748a175baeSEmil Constantinescu PetscValidType(U,3); 63758a175baeSEmil Constantinescu PetscValidType(Y,4); 63768a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 63778a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 63788a175baeSEmil Constantinescu PetscValidPointer(norm,5); 63798a175baeSEmil Constantinescu PetscValidPointer(norma,6); 63808a175baeSEmil Constantinescu PetscValidPointer(normr,7); 63818a175baeSEmil Constantinescu 63828a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 63838a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 63848a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 63858a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 63868a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 63878a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 63888a175baeSEmil Constantinescu 63898a175baeSEmil Constantinescu max=0.; 63908a175baeSEmil Constantinescu maxa=0.; 63918a175baeSEmil Constantinescu maxr=0.; 63928a175baeSEmil Constantinescu 63938a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 63948a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 63958a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 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 = PetscRealPart(atol[i]); 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->vatol,&atol);CHKERRQ(ierr); 64148a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 64158a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 64168a175baeSEmil Constantinescu const PetscScalar *atol; 64178a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 64188a175baeSEmil Constantinescu for (i=0; i<n; i++) { 64198a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 64208a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 64218a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 64228a175baeSEmil Constantinescu tol = tola+tolr; 64238a175baeSEmil Constantinescu if(tola>0.){ 64248a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 64258a175baeSEmil Constantinescu } 64268a175baeSEmil Constantinescu if(tolr>0.){ 64278a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 64288a175baeSEmil Constantinescu } 64298a175baeSEmil Constantinescu if(tol>0.){ 64308a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 64318a175baeSEmil Constantinescu } 64328a175baeSEmil Constantinescu } 64338a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 64348a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 64358a175baeSEmil Constantinescu const PetscScalar *rtol; 64368a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 64378a175baeSEmil Constantinescu 64388a175baeSEmil Constantinescu for (i=0; i<n; i++) { 64398a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 64408a175baeSEmil Constantinescu tola = ts->atol; 64418a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 64428a175baeSEmil Constantinescu tol = tola+tolr; 64438a175baeSEmil Constantinescu if(tola>0.){ 64448a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 64458a175baeSEmil Constantinescu } 64468a175baeSEmil Constantinescu if(tolr>0.){ 64478a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 64488a175baeSEmil Constantinescu } 64498a175baeSEmil Constantinescu if(tol>0.){ 64508a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 64518a175baeSEmil Constantinescu } 64528a175baeSEmil Constantinescu } 64538a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 64548a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 64558a175baeSEmil Constantinescu 64568a175baeSEmil Constantinescu for (i=0; i<n; i++) { 64578a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 64588a175baeSEmil Constantinescu tola = ts->atol; 64598a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 64608a175baeSEmil Constantinescu tol = tola+tolr; 64618a175baeSEmil Constantinescu if(tola>0.){ 64628a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 64638a175baeSEmil Constantinescu } 64648a175baeSEmil Constantinescu if(tolr>0.){ 64658a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 64668a175baeSEmil Constantinescu } 64678a175baeSEmil Constantinescu if(tol>0.){ 64688a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 64698a175baeSEmil Constantinescu } 64708a175baeSEmil Constantinescu } 64718a175baeSEmil Constantinescu } 64728a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 64738a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 64748a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 64758a175baeSEmil Constantinescu err_loc[0] = max; 64768a175baeSEmil Constantinescu err_loc[1] = maxa; 64778a175baeSEmil Constantinescu err_loc[2] = maxr; 6478a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 64798a175baeSEmil Constantinescu gmax = err_glb[0]; 64808a175baeSEmil Constantinescu gmaxa = err_glb[1]; 64818a175baeSEmil Constantinescu gmaxr = err_glb[2]; 64828a175baeSEmil Constantinescu 64838a175baeSEmil Constantinescu *norm = gmax; 64848a175baeSEmil Constantinescu *norma = gmaxa; 64858a175baeSEmil Constantinescu *normr = gmaxr; 64868a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 64878a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 64888a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 64898a175baeSEmil Constantinescu PetscFunctionReturn(0); 64908a175baeSEmil Constantinescu } 64918a175baeSEmil Constantinescu 64928a175baeSEmil Constantinescu /*@ 64938a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 64948a175baeSEmil Constantinescu 64958a175baeSEmil Constantinescu Collective on TS 64968a175baeSEmil Constantinescu 64978a175baeSEmil Constantinescu Input Arguments: 64988a175baeSEmil Constantinescu + ts - time stepping context 64998a175baeSEmil Constantinescu . E - error vector 65008a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 65018a175baeSEmil Constantinescu . Y - state vector, previous time step 65028a175baeSEmil Constantinescu - wnormtype - norm type, either NORM_2 or NORM_INFINITY 65038a175baeSEmil Constantinescu 65048a175baeSEmil Constantinescu Output Arguments: 65058a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 65068a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 65078a175baeSEmil Constantinescu . normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 65088a175baeSEmil Constantinescu 65098a175baeSEmil Constantinescu Options Database Keys: 65108a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 65118a175baeSEmil Constantinescu 65128a175baeSEmil Constantinescu Level: developer 65138a175baeSEmil Constantinescu 65148a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 65158a175baeSEmil Constantinescu @*/ 65168a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 65178a175baeSEmil Constantinescu { 65188a175baeSEmil Constantinescu PetscErrorCode ierr; 65198a175baeSEmil Constantinescu 65208a175baeSEmil Constantinescu PetscFunctionBegin; 65218a175baeSEmil Constantinescu if (wnormtype == NORM_2) { 65228a175baeSEmil Constantinescu ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 65238a175baeSEmil Constantinescu } else if(wnormtype == NORM_INFINITY) { 65248a175baeSEmil Constantinescu ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 65258a175baeSEmil Constantinescu } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 65268a175baeSEmil Constantinescu PetscFunctionReturn(0); 65278a175baeSEmil Constantinescu } 65288a175baeSEmil Constantinescu 65298a175baeSEmil Constantinescu 65308d59e960SJed Brown /*@ 65318d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 65328d59e960SJed Brown 65338d59e960SJed Brown Logically Collective on TS 65348d59e960SJed Brown 65358d59e960SJed Brown Input Arguments: 65368d59e960SJed Brown + ts - time stepping context 65378d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 65388d59e960SJed Brown 65398d59e960SJed Brown Note: 65408d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 65418d59e960SJed Brown 65428d59e960SJed Brown Level: intermediate 65438d59e960SJed Brown 65448d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 65458d59e960SJed Brown @*/ 65468d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 65478d59e960SJed Brown { 65488d59e960SJed Brown PetscFunctionBegin; 65498d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 65508d59e960SJed Brown ts->cfltime_local = cfltime; 65518d59e960SJed Brown ts->cfltime = -1.; 65528d59e960SJed Brown PetscFunctionReturn(0); 65538d59e960SJed Brown } 65548d59e960SJed Brown 65558d59e960SJed Brown /*@ 65568d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 65578d59e960SJed Brown 65588d59e960SJed Brown Collective on TS 65598d59e960SJed Brown 65608d59e960SJed Brown Input Arguments: 65618d59e960SJed Brown . ts - time stepping context 65628d59e960SJed Brown 65638d59e960SJed Brown Output Arguments: 65648d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 65658d59e960SJed Brown 65668d59e960SJed Brown Level: advanced 65678d59e960SJed Brown 65688d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 65698d59e960SJed Brown @*/ 65708d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 65718d59e960SJed Brown { 65728d59e960SJed Brown PetscErrorCode ierr; 65738d59e960SJed Brown 65748d59e960SJed Brown PetscFunctionBegin; 65758d59e960SJed Brown if (ts->cfltime < 0) { 6576b2566f29SBarry Smith ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 65778d59e960SJed Brown } 65788d59e960SJed Brown *cfltime = ts->cfltime; 65798d59e960SJed Brown PetscFunctionReturn(0); 65808d59e960SJed Brown } 65818d59e960SJed Brown 6582d6ebe24aSShri Abhyankar /*@ 6583d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 6584d6ebe24aSShri Abhyankar 6585d6ebe24aSShri Abhyankar Input Parameters: 6586d6ebe24aSShri Abhyankar . ts - the TS context. 6587d6ebe24aSShri Abhyankar . xl - lower bound. 6588d6ebe24aSShri Abhyankar . xu - upper bound. 6589d6ebe24aSShri Abhyankar 6590d6ebe24aSShri Abhyankar Notes: 6591d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 6592e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 6593d6ebe24aSShri Abhyankar 65942bd2b0e6SSatish Balay Level: advanced 65952bd2b0e6SSatish Balay 6596d6ebe24aSShri Abhyankar @*/ 6597d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 6598d6ebe24aSShri Abhyankar { 6599d6ebe24aSShri Abhyankar PetscErrorCode ierr; 6600d6ebe24aSShri Abhyankar SNES snes; 6601d6ebe24aSShri Abhyankar 6602d6ebe24aSShri Abhyankar PetscFunctionBegin; 6603d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 6604d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 6605d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 6606d6ebe24aSShri Abhyankar } 6607d6ebe24aSShri Abhyankar 6608325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 6609c6db04a5SJed Brown #include <mex.h> 6610325fc9f4SBarry Smith 6611325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 6612325fc9f4SBarry Smith 6613325fc9f4SBarry Smith /* 6614325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 6615325fc9f4SBarry Smith TSSetFunctionMatlab(). 6616325fc9f4SBarry Smith 6617325fc9f4SBarry Smith Collective on TS 6618325fc9f4SBarry Smith 6619325fc9f4SBarry Smith Input Parameters: 6620325fc9f4SBarry Smith + snes - the TS context 66210910c330SBarry Smith - u - input vector 6622325fc9f4SBarry Smith 6623325fc9f4SBarry Smith Output Parameter: 6624325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 6625325fc9f4SBarry Smith 6626325fc9f4SBarry Smith Notes: 6627325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 6628325fc9f4SBarry Smith implementations, so most users would not generally call this routine 6629325fc9f4SBarry Smith themselves. 6630325fc9f4SBarry Smith 6631325fc9f4SBarry Smith Level: developer 6632325fc9f4SBarry Smith 6633325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6634325fc9f4SBarry Smith 6635325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6636325fc9f4SBarry Smith */ 66370910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 6638325fc9f4SBarry Smith { 6639325fc9f4SBarry Smith PetscErrorCode ierr; 6640325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6641325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 6642325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 6643325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 6644325fc9f4SBarry Smith 6645325fc9f4SBarry Smith PetscFunctionBegin; 6646325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 66470910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 66480910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 6649325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 66500910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 6651325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 6652325fc9f4SBarry Smith 6653325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 66540910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 66550910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 66560910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 6657bbd56ea5SKarl Rupp 6658325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6659325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 6660325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6661325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6662325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 6663325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 6664325fc9f4SBarry Smith prhs[6] = sctx->ctx; 6665325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 6666325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6667325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6668325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6669325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6670325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6671325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6672325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6673325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6674325fc9f4SBarry Smith PetscFunctionReturn(0); 6675325fc9f4SBarry Smith } 6676325fc9f4SBarry Smith 6677325fc9f4SBarry Smith 6678325fc9f4SBarry Smith /* 6679325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 6680325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 6681e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 6682325fc9f4SBarry Smith 6683325fc9f4SBarry Smith Logically Collective on TS 6684325fc9f4SBarry Smith 6685325fc9f4SBarry Smith Input Parameters: 6686325fc9f4SBarry Smith + ts - the TS context 6687325fc9f4SBarry Smith - func - function evaluation routine 6688325fc9f4SBarry Smith 6689325fc9f4SBarry Smith Calling sequence of func: 66900910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 6691325fc9f4SBarry Smith 6692325fc9f4SBarry Smith Level: beginner 6693325fc9f4SBarry Smith 6694325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6695325fc9f4SBarry Smith 6696325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6697325fc9f4SBarry Smith */ 6698cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 6699325fc9f4SBarry Smith { 6700325fc9f4SBarry Smith PetscErrorCode ierr; 6701325fc9f4SBarry Smith TSMatlabContext *sctx; 6702325fc9f4SBarry Smith 6703325fc9f4SBarry Smith PetscFunctionBegin; 6704325fc9f4SBarry Smith /* currently sctx is memory bleed */ 670595dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6706325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6707325fc9f4SBarry Smith /* 6708325fc9f4SBarry Smith This should work, but it doesn't 6709325fc9f4SBarry Smith sctx->ctx = ctx; 6710325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6711325fc9f4SBarry Smith */ 6712325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6713bbd56ea5SKarl Rupp 67140298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 6715325fc9f4SBarry Smith PetscFunctionReturn(0); 6716325fc9f4SBarry Smith } 6717325fc9f4SBarry Smith 6718325fc9f4SBarry Smith /* 6719325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 6720325fc9f4SBarry Smith TSSetJacobianMatlab(). 6721325fc9f4SBarry Smith 6722325fc9f4SBarry Smith Collective on TS 6723325fc9f4SBarry Smith 6724325fc9f4SBarry Smith Input Parameters: 6725cdcf91faSSean Farley + ts - the TS context 67260910c330SBarry Smith . u - input vector 6727325fc9f4SBarry Smith . A, B - the matrices 6728325fc9f4SBarry Smith - ctx - user context 6729325fc9f4SBarry Smith 6730325fc9f4SBarry Smith Level: developer 6731325fc9f4SBarry Smith 6732325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6733325fc9f4SBarry Smith 6734325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6735325fc9f4SBarry Smith @*/ 6736f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 6737325fc9f4SBarry Smith { 6738325fc9f4SBarry Smith PetscErrorCode ierr; 6739325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6740325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 6741325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 6742325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 6743325fc9f4SBarry Smith 6744325fc9f4SBarry Smith PetscFunctionBegin; 6745cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 67460910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 6747325fc9f4SBarry Smith 67480910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 6749325fc9f4SBarry Smith 6750cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 67510910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 67520910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 67530910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 67540910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 6755bbd56ea5SKarl Rupp 6756325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6757325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 6758325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6759325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6760325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 6761325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 6762325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 6763325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 6764325fc9f4SBarry Smith prhs[8] = sctx->ctx; 6765325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 6766325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6767325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6768325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6769325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6770325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6771325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6772325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6773325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 6774325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 6775325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6776325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 6777325fc9f4SBarry Smith PetscFunctionReturn(0); 6778325fc9f4SBarry Smith } 6779325fc9f4SBarry Smith 6780325fc9f4SBarry Smith 6781325fc9f4SBarry Smith /* 6782325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 6783e3c5b3baSBarry 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 6784325fc9f4SBarry Smith 6785325fc9f4SBarry Smith Logically Collective on TS 6786325fc9f4SBarry Smith 6787325fc9f4SBarry Smith Input Parameters: 6788cdcf91faSSean Farley + ts - the TS context 6789325fc9f4SBarry Smith . A,B - Jacobian matrices 6790325fc9f4SBarry Smith . func - function evaluation routine 6791325fc9f4SBarry Smith - ctx - user context 6792325fc9f4SBarry Smith 6793325fc9f4SBarry Smith Calling sequence of func: 67940910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 6795325fc9f4SBarry Smith 6796325fc9f4SBarry Smith 6797325fc9f4SBarry Smith Level: developer 6798325fc9f4SBarry Smith 6799325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6800325fc9f4SBarry Smith 6801325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6802325fc9f4SBarry Smith */ 6803cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 6804325fc9f4SBarry Smith { 6805325fc9f4SBarry Smith PetscErrorCode ierr; 6806325fc9f4SBarry Smith TSMatlabContext *sctx; 6807325fc9f4SBarry Smith 6808325fc9f4SBarry Smith PetscFunctionBegin; 6809325fc9f4SBarry Smith /* currently sctx is memory bleed */ 681095dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6811325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6812325fc9f4SBarry Smith /* 6813325fc9f4SBarry Smith This should work, but it doesn't 6814325fc9f4SBarry Smith sctx->ctx = ctx; 6815325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6816325fc9f4SBarry Smith */ 6817325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6818bbd56ea5SKarl Rupp 6819cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 6820325fc9f4SBarry Smith PetscFunctionReturn(0); 6821325fc9f4SBarry Smith } 6822325fc9f4SBarry Smith 6823b5b1a830SBarry Smith /* 6824b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 6825b5b1a830SBarry Smith 6826b5b1a830SBarry Smith Collective on TS 6827b5b1a830SBarry Smith 6828b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6829b5b1a830SBarry Smith @*/ 68300910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 6831b5b1a830SBarry Smith { 6832b5b1a830SBarry Smith PetscErrorCode ierr; 6833b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6834a530c242SBarry Smith int nlhs = 1,nrhs = 6; 6835b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 6836b5b1a830SBarry Smith long long int lx = 0,ls = 0; 6837b5b1a830SBarry Smith 6838b5b1a830SBarry Smith PetscFunctionBegin; 6839cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 68400910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 6841b5b1a830SBarry Smith 6842cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 68430910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 6844bbd56ea5SKarl Rupp 6845b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6846b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 6847b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 6848b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 6849b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 6850b5b1a830SBarry Smith prhs[5] = sctx->ctx; 6851b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 6852b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6853b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 6854b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 6855b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 6856b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 6857b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 6858b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 6859b5b1a830SBarry Smith PetscFunctionReturn(0); 6860b5b1a830SBarry Smith } 6861b5b1a830SBarry Smith 6862b5b1a830SBarry Smith 6863b5b1a830SBarry Smith /* 6864b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 6865b5b1a830SBarry Smith 6866b5b1a830SBarry Smith Level: developer 6867b5b1a830SBarry Smith 6868b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 6869b5b1a830SBarry Smith 6870b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6871b5b1a830SBarry Smith */ 6872cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 6873b5b1a830SBarry Smith { 6874b5b1a830SBarry Smith PetscErrorCode ierr; 6875b5b1a830SBarry Smith TSMatlabContext *sctx; 6876b5b1a830SBarry Smith 6877b5b1a830SBarry Smith PetscFunctionBegin; 6878b5b1a830SBarry Smith /* currently sctx is memory bleed */ 687995dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6880b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6881b5b1a830SBarry Smith /* 6882b5b1a830SBarry Smith This should work, but it doesn't 6883b5b1a830SBarry Smith sctx->ctx = ctx; 6884b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6885b5b1a830SBarry Smith */ 6886b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6887bbd56ea5SKarl Rupp 68880298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 6889b5b1a830SBarry Smith PetscFunctionReturn(0); 6890b5b1a830SBarry Smith } 6891325fc9f4SBarry Smith #endif 6892b3603a34SBarry Smith 6893b3603a34SBarry Smith /*@C 68944f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 6895b3603a34SBarry Smith in a time based line graph 6896b3603a34SBarry Smith 6897b3603a34SBarry Smith Collective on TS 6898b3603a34SBarry Smith 6899b3603a34SBarry Smith Input Parameters: 6900b3603a34SBarry Smith + ts - the TS context 6901b3603a34SBarry Smith . step - current time-step 6902b3603a34SBarry Smith . ptime - current time 69037db568b7SBarry Smith . u - current solution 69047db568b7SBarry Smith - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 6905b3603a34SBarry Smith 6906b3d3934dSBarry Smith Options Database: 69079ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 6908b3d3934dSBarry Smith 6909b3603a34SBarry Smith Level: intermediate 6910b3603a34SBarry Smith 69116934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component solutions in a separate window 6912b3603a34SBarry Smith 6913b3603a34SBarry Smith .keywords: TS, vector, monitor, view 6914b3603a34SBarry Smith 69157db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 69167db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 69177db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 69187db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 6919b3603a34SBarry Smith @*/ 69207db568b7SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6921b3603a34SBarry Smith { 6922b3603a34SBarry Smith PetscErrorCode ierr; 69237db568b7SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 6924b3603a34SBarry Smith const PetscScalar *yy; 692580666b62SBarry Smith Vec v; 6926b3603a34SBarry Smith 6927b3603a34SBarry Smith PetscFunctionBegin; 692863e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 692958ff32f7SBarry Smith if (!step) { 6930a9f9c1f6SBarry Smith PetscDrawAxis axis; 69316934998bSLisandro Dalcin PetscInt dim; 6932a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6933a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 6934bab0a581SBarry Smith if (!ctx->names) { 6935bab0a581SBarry Smith PetscBool flg; 6936bab0a581SBarry Smith /* user provides names of variables to plot but no names has been set so assume names are integer values */ 6937bab0a581SBarry Smith ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr); 6938bab0a581SBarry Smith if (flg) { 6939bab0a581SBarry Smith PetscInt i,n; 6940bab0a581SBarry Smith char **names; 6941bab0a581SBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 6942bab0a581SBarry Smith ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr); 6943bab0a581SBarry Smith for (i=0; i<n; i++) { 6944bab0a581SBarry Smith ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr); 6945bab0a581SBarry Smith ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr); 6946bab0a581SBarry Smith } 6947bab0a581SBarry Smith names[n] = NULL; 6948bab0a581SBarry Smith ctx->names = names; 6949bab0a581SBarry Smith } 6950bab0a581SBarry Smith } 6951387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 6952387f4636SBarry Smith char **displaynames; 6953387f4636SBarry Smith PetscBool flg; 6954387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 695595dccacaSBarry Smith ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr); 6956387f4636SBarry Smith ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 6957c5929fdfSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 6958387f4636SBarry Smith if (flg) { 6959a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 6960387f4636SBarry Smith } 6961387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 6962387f4636SBarry Smith } 6963387f4636SBarry Smith if (ctx->displaynames) { 6964387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 6965387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 6966387f4636SBarry Smith } else if (ctx->names) { 69670910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 69680b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6969387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 6970b0bc92c6SBarry Smith } else { 6971b0bc92c6SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6972b0bc92c6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6973387f4636SBarry Smith } 69740b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 697558ff32f7SBarry Smith } 69766934998bSLisandro Dalcin 69776934998bSLisandro Dalcin if (!ctx->transform) v = u; 69786934998bSLisandro Dalcin else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);} 697980666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 69806934998bSLisandro Dalcin if (ctx->displaynames) { 69816934998bSLisandro Dalcin PetscInt i; 69826934998bSLisandro Dalcin for (i=0; i<ctx->ndisplayvariables; i++) 69836934998bSLisandro Dalcin ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]); 69846934998bSLisandro Dalcin ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 69856934998bSLisandro Dalcin } else { 6986e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6987e3efe391SJed Brown PetscInt i,n; 69886934998bSLisandro Dalcin PetscReal *yreal; 698980666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 6990785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6991e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 69920b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6993e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6994e3efe391SJed Brown #else 69950b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6996e3efe391SJed Brown #endif 699780666b62SBarry Smith } 69986934998bSLisandro Dalcin ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 69996934998bSLisandro Dalcin if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);} 70006934998bSLisandro Dalcin 7001b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 70020b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 70036934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 70043923b477SBarry Smith } 7005b3603a34SBarry Smith PetscFunctionReturn(0); 7006b3603a34SBarry Smith } 7007b3603a34SBarry Smith 7008387f4636SBarry Smith 7009b037adc7SBarry Smith /*@C 701031152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 7011b037adc7SBarry Smith 7012b037adc7SBarry Smith Collective on TS 7013b037adc7SBarry Smith 7014b037adc7SBarry Smith Input Parameters: 7015b037adc7SBarry Smith + ts - the TS context 7016b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 7017b037adc7SBarry Smith 7018b037adc7SBarry Smith Level: intermediate 7019b037adc7SBarry Smith 70207db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 70217db568b7SBarry Smith 7022b037adc7SBarry Smith .keywords: TS, vector, monitor, view 7023b037adc7SBarry Smith 7024a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 7025b037adc7SBarry Smith @*/ 702631152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 7027b037adc7SBarry Smith { 7028b037adc7SBarry Smith PetscErrorCode ierr; 7029b037adc7SBarry Smith PetscInt i; 7030b037adc7SBarry Smith 7031b037adc7SBarry Smith PetscFunctionBegin; 7032b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7033b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 70345537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 7035b3d3934dSBarry Smith break; 7036b3d3934dSBarry Smith } 7037b3d3934dSBarry Smith } 7038b3d3934dSBarry Smith PetscFunctionReturn(0); 7039b3d3934dSBarry Smith } 7040b3d3934dSBarry Smith 7041e673d494SBarry Smith /*@C 7042e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 7043e673d494SBarry Smith 7044e673d494SBarry Smith Collective on TS 7045e673d494SBarry Smith 7046e673d494SBarry Smith Input Parameters: 7047e673d494SBarry Smith + ts - the TS context 7048e673d494SBarry Smith - names - the names of the components, final string must be NULL 7049e673d494SBarry Smith 7050e673d494SBarry Smith Level: intermediate 7051e673d494SBarry Smith 7052e673d494SBarry Smith .keywords: TS, vector, monitor, view 7053e673d494SBarry Smith 7054a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 7055e673d494SBarry Smith @*/ 7056e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 7057e673d494SBarry Smith { 7058e673d494SBarry Smith PetscErrorCode ierr; 7059e673d494SBarry Smith 7060e673d494SBarry Smith PetscFunctionBegin; 7061e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 7062e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 7063e673d494SBarry Smith PetscFunctionReturn(0); 7064e673d494SBarry Smith } 7065e673d494SBarry Smith 7066b3d3934dSBarry Smith /*@C 7067b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 7068b3d3934dSBarry Smith 7069b3d3934dSBarry Smith Collective on TS 7070b3d3934dSBarry Smith 7071b3d3934dSBarry Smith Input Parameter: 7072b3d3934dSBarry Smith . ts - the TS context 7073b3d3934dSBarry Smith 7074b3d3934dSBarry Smith Output Parameter: 7075b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 7076b3d3934dSBarry Smith 7077b3d3934dSBarry Smith Level: intermediate 7078b3d3934dSBarry Smith 70797db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 70807db568b7SBarry Smith 7081b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7082b3d3934dSBarry Smith 7083b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 7084b3d3934dSBarry Smith @*/ 7085b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 7086b3d3934dSBarry Smith { 7087b3d3934dSBarry Smith PetscInt i; 7088b3d3934dSBarry Smith 7089b3d3934dSBarry Smith PetscFunctionBegin; 7090b3d3934dSBarry Smith *names = NULL; 7091b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7092b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 70935537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 7094b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 7095b3d3934dSBarry Smith break; 7096387f4636SBarry Smith } 7097387f4636SBarry Smith } 7098387f4636SBarry Smith PetscFunctionReturn(0); 7099387f4636SBarry Smith } 7100387f4636SBarry Smith 7101a66092f1SBarry Smith /*@C 7102a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 7103a66092f1SBarry Smith 7104a66092f1SBarry Smith Collective on TS 7105a66092f1SBarry Smith 7106a66092f1SBarry Smith Input Parameters: 7107a66092f1SBarry Smith + ctx - the TSMonitorLG context 7108a66092f1SBarry Smith . displaynames - the names of the components, final string must be NULL 7109a66092f1SBarry Smith 7110a66092f1SBarry Smith Level: intermediate 7111a66092f1SBarry Smith 7112a66092f1SBarry Smith .keywords: TS, vector, monitor, view 7113a66092f1SBarry Smith 7114a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 7115a66092f1SBarry Smith @*/ 7116a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 7117a66092f1SBarry Smith { 7118a66092f1SBarry Smith PetscInt j = 0,k; 7119a66092f1SBarry Smith PetscErrorCode ierr; 7120a66092f1SBarry Smith 7121a66092f1SBarry Smith PetscFunctionBegin; 7122a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 7123a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 7124a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 7125a66092f1SBarry Smith while (displaynames[j]) j++; 7126a66092f1SBarry Smith ctx->ndisplayvariables = j; 7127a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 7128a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 7129a66092f1SBarry Smith j = 0; 7130a66092f1SBarry Smith while (displaynames[j]) { 7131a66092f1SBarry Smith k = 0; 7132a66092f1SBarry Smith while (ctx->names[k]) { 7133a66092f1SBarry Smith PetscBool flg; 7134a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 7135a66092f1SBarry Smith if (flg) { 7136a66092f1SBarry Smith ctx->displayvariables[j] = k; 7137a66092f1SBarry Smith break; 7138a66092f1SBarry Smith } 7139a66092f1SBarry Smith k++; 7140a66092f1SBarry Smith } 7141a66092f1SBarry Smith j++; 7142a66092f1SBarry Smith } 7143a66092f1SBarry Smith PetscFunctionReturn(0); 7144a66092f1SBarry Smith } 7145a66092f1SBarry Smith 7146a66092f1SBarry Smith 7147387f4636SBarry Smith /*@C 7148387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 7149387f4636SBarry Smith 7150387f4636SBarry Smith Collective on TS 7151387f4636SBarry Smith 7152387f4636SBarry Smith Input Parameters: 7153387f4636SBarry Smith + ts - the TS context 7154387f4636SBarry Smith . displaynames - the names of the components, final string must be NULL 7155387f4636SBarry Smith 71567db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 71577db568b7SBarry Smith 7158387f4636SBarry Smith Level: intermediate 7159387f4636SBarry Smith 7160387f4636SBarry Smith .keywords: TS, vector, monitor, view 7161387f4636SBarry Smith 7162387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 7163387f4636SBarry Smith @*/ 7164387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 7165387f4636SBarry Smith { 7166a66092f1SBarry Smith PetscInt i; 7167387f4636SBarry Smith PetscErrorCode ierr; 7168387f4636SBarry Smith 7169387f4636SBarry Smith PetscFunctionBegin; 7170387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7171387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 71725537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 7173b3d3934dSBarry Smith break; 7174b037adc7SBarry Smith } 7175b037adc7SBarry Smith } 7176b037adc7SBarry Smith PetscFunctionReturn(0); 7177b037adc7SBarry Smith } 7178b037adc7SBarry Smith 717980666b62SBarry Smith /*@C 718080666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 718180666b62SBarry Smith 718280666b62SBarry Smith Collective on TS 718380666b62SBarry Smith 718480666b62SBarry Smith Input Parameters: 718580666b62SBarry Smith + ts - the TS context 718680666b62SBarry Smith . transform - the transform function 71877684fa3eSBarry Smith . destroy - function to destroy the optional context 718880666b62SBarry Smith - ctx - optional context used by transform function 718980666b62SBarry Smith 71907db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 71917db568b7SBarry Smith 719280666b62SBarry Smith Level: intermediate 719380666b62SBarry Smith 719480666b62SBarry Smith .keywords: TS, vector, monitor, view 719580666b62SBarry Smith 7196a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 719780666b62SBarry Smith @*/ 71987684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 719980666b62SBarry Smith { 720080666b62SBarry Smith PetscInt i; 7201a66092f1SBarry Smith PetscErrorCode ierr; 720280666b62SBarry Smith 720380666b62SBarry Smith PetscFunctionBegin; 720480666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 720580666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 72065537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 720780666b62SBarry Smith } 720880666b62SBarry Smith } 720980666b62SBarry Smith PetscFunctionReturn(0); 721080666b62SBarry Smith } 721180666b62SBarry Smith 7212e673d494SBarry Smith /*@C 7213e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 7214e673d494SBarry Smith 7215e673d494SBarry Smith Collective on TSLGCtx 7216e673d494SBarry Smith 7217e673d494SBarry Smith Input Parameters: 7218e673d494SBarry Smith + ts - the TS context 7219e673d494SBarry Smith . transform - the transform function 72207684fa3eSBarry Smith . destroy - function to destroy the optional context 7221e673d494SBarry Smith - ctx - optional context used by transform function 7222e673d494SBarry Smith 7223e673d494SBarry Smith Level: intermediate 7224e673d494SBarry Smith 7225e673d494SBarry Smith .keywords: TS, vector, monitor, view 7226e673d494SBarry Smith 7227a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 7228e673d494SBarry Smith @*/ 72297684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 7230e673d494SBarry Smith { 7231e673d494SBarry Smith PetscFunctionBegin; 7232e673d494SBarry Smith ctx->transform = transform; 72337684fa3eSBarry Smith ctx->transformdestroy = destroy; 7234e673d494SBarry Smith ctx->transformctx = tctx; 7235e673d494SBarry Smith PetscFunctionReturn(0); 7236e673d494SBarry Smith } 7237e673d494SBarry Smith 7238ef20d060SBarry Smith /*@C 72394f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 7240ef20d060SBarry Smith in a time based line graph 7241ef20d060SBarry Smith 7242ef20d060SBarry Smith Collective on TS 7243ef20d060SBarry Smith 7244ef20d060SBarry Smith Input Parameters: 7245ef20d060SBarry Smith + ts - the TS context 7246ef20d060SBarry Smith . step - current time-step 7247ef20d060SBarry Smith . ptime - current time 72487db568b7SBarry Smith . u - current solution 72497db568b7SBarry Smith - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 7250ef20d060SBarry Smith 7251ef20d060SBarry Smith Level: intermediate 7252ef20d060SBarry Smith 72536934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component errors in a separate window 7254abd5a294SJed Brown 7255abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 7256abd5a294SJed Brown 7257abd5a294SJed Brown Options Database Keys: 72584f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 7259ef20d060SBarry Smith 7260ef20d060SBarry Smith .keywords: TS, vector, monitor, view 7261ef20d060SBarry Smith 7262abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 7263ef20d060SBarry Smith @*/ 72640910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 7265ef20d060SBarry Smith { 7266ef20d060SBarry Smith PetscErrorCode ierr; 72670b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 7268ef20d060SBarry Smith const PetscScalar *yy; 7269ef20d060SBarry Smith Vec y; 7270ef20d060SBarry Smith 7271ef20d060SBarry Smith PetscFunctionBegin; 7272a9f9c1f6SBarry Smith if (!step) { 7273a9f9c1f6SBarry Smith PetscDrawAxis axis; 72746934998bSLisandro Dalcin PetscInt dim; 7275a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 72761ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 72770910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 7278a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 7279a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 7280a9f9c1f6SBarry Smith } 72810910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 7282ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 72830910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 7284ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 7285e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 7286e3efe391SJed Brown { 7287e3efe391SJed Brown PetscReal *yreal; 7288e3efe391SJed Brown PetscInt i,n; 7289e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 7290785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 7291e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 72920b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 7293e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 7294e3efe391SJed Brown } 7295e3efe391SJed Brown #else 72960b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 7297e3efe391SJed Brown #endif 7298ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 7299ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 7300b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 73010b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 73026934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 73033923b477SBarry Smith } 7304ef20d060SBarry Smith PetscFunctionReturn(0); 7305ef20d060SBarry Smith } 7306ef20d060SBarry Smith 7307201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 7308201da799SBarry Smith { 7309201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 7310201da799SBarry Smith PetscReal x = ptime,y; 7311201da799SBarry Smith PetscErrorCode ierr; 7312201da799SBarry Smith PetscInt its; 7313201da799SBarry Smith 7314201da799SBarry Smith PetscFunctionBegin; 731563e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 7316201da799SBarry Smith if (!n) { 7317201da799SBarry Smith PetscDrawAxis axis; 7318201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 7319201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 7320201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 7321201da799SBarry Smith ctx->snes_its = 0; 7322201da799SBarry Smith } 7323201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 7324201da799SBarry Smith y = its - ctx->snes_its; 7325201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 73263fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 7327201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 73286934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 7329201da799SBarry Smith } 7330201da799SBarry Smith ctx->snes_its = its; 7331201da799SBarry Smith PetscFunctionReturn(0); 7332201da799SBarry Smith } 7333201da799SBarry Smith 7334201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 7335201da799SBarry Smith { 7336201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 7337201da799SBarry Smith PetscReal x = ptime,y; 7338201da799SBarry Smith PetscErrorCode ierr; 7339201da799SBarry Smith PetscInt its; 7340201da799SBarry Smith 7341201da799SBarry Smith PetscFunctionBegin; 734263e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 7343201da799SBarry Smith if (!n) { 7344201da799SBarry Smith PetscDrawAxis axis; 7345201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 7346201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 7347201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 7348201da799SBarry Smith ctx->ksp_its = 0; 7349201da799SBarry Smith } 7350201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 7351201da799SBarry Smith y = its - ctx->ksp_its; 7352201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 735399fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 7354201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 73556934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 7356201da799SBarry Smith } 7357201da799SBarry Smith ctx->ksp_its = its; 7358201da799SBarry Smith PetscFunctionReturn(0); 7359201da799SBarry Smith } 7360f9c1d6abSBarry Smith 7361f9c1d6abSBarry Smith /*@ 7362f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 7363f9c1d6abSBarry Smith 7364f9c1d6abSBarry Smith Collective on TS and Vec 7365f9c1d6abSBarry Smith 7366f9c1d6abSBarry Smith Input Parameters: 7367f9c1d6abSBarry Smith + ts - the TS context 7368f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 7369f9c1d6abSBarry Smith 7370f9c1d6abSBarry Smith Output Parameters: 7371f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 7372f9c1d6abSBarry Smith 7373f9c1d6abSBarry Smith Level: developer 7374f9c1d6abSBarry Smith 7375f9c1d6abSBarry Smith .keywords: TS, compute 7376f9c1d6abSBarry Smith 7377f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 7378f9c1d6abSBarry Smith @*/ 7379f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 7380f9c1d6abSBarry Smith { 7381f9c1d6abSBarry Smith PetscErrorCode ierr; 7382f9c1d6abSBarry Smith 7383f9c1d6abSBarry Smith PetscFunctionBegin; 7384f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7385ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 7386f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 7387f9c1d6abSBarry Smith PetscFunctionReturn(0); 7388f9c1d6abSBarry Smith } 738924655328SShri 7390b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 7391b3d3934dSBarry Smith /*@C 7392b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 7393b3d3934dSBarry Smith 7394b3d3934dSBarry Smith Collective on TS 7395b3d3934dSBarry Smith 7396b3d3934dSBarry Smith Input Parameters: 7397b3d3934dSBarry Smith . ts - the ODE solver object 7398b3d3934dSBarry Smith 7399b3d3934dSBarry Smith Output Parameter: 7400b3d3934dSBarry Smith . ctx - the context 7401b3d3934dSBarry Smith 7402b3d3934dSBarry Smith Level: intermediate 7403b3d3934dSBarry Smith 7404b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso 7405b3d3934dSBarry Smith 7406b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 7407b3d3934dSBarry Smith 7408b3d3934dSBarry Smith @*/ 7409b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 7410b3d3934dSBarry Smith { 7411b3d3934dSBarry Smith PetscErrorCode ierr; 7412b3d3934dSBarry Smith 7413b3d3934dSBarry Smith PetscFunctionBegin; 7414a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 7415b3d3934dSBarry Smith PetscFunctionReturn(0); 7416b3d3934dSBarry Smith } 7417b3d3934dSBarry Smith 7418b3d3934dSBarry Smith /*@C 7419b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 7420b3d3934dSBarry Smith 7421b3d3934dSBarry Smith Collective on TS 7422b3d3934dSBarry Smith 7423b3d3934dSBarry Smith Input Parameters: 7424b3d3934dSBarry Smith + ts - the TS context 7425b3d3934dSBarry Smith . step - current time-step 7426b3d3934dSBarry Smith . ptime - current time 74277db568b7SBarry Smith . u - current solution 74287db568b7SBarry Smith - dctx - the envelope context 7429b3d3934dSBarry Smith 7430b3d3934dSBarry Smith Options Database: 7431b3d3934dSBarry Smith . -ts_monitor_envelope 7432b3d3934dSBarry Smith 7433b3d3934dSBarry Smith Level: intermediate 7434b3d3934dSBarry Smith 7435b3d3934dSBarry Smith Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 7436b3d3934dSBarry Smith 7437b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7438b3d3934dSBarry Smith 74397db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 7440b3d3934dSBarry Smith @*/ 74417db568b7SBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 7442b3d3934dSBarry Smith { 7443b3d3934dSBarry Smith PetscErrorCode ierr; 74447db568b7SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 7445b3d3934dSBarry Smith 7446b3d3934dSBarry Smith PetscFunctionBegin; 7447b3d3934dSBarry Smith if (!ctx->max) { 7448b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 7449b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 7450b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 7451b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 7452b3d3934dSBarry Smith } else { 7453b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 7454b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 7455b3d3934dSBarry Smith } 7456b3d3934dSBarry Smith PetscFunctionReturn(0); 7457b3d3934dSBarry Smith } 7458b3d3934dSBarry Smith 7459b3d3934dSBarry Smith 7460b3d3934dSBarry Smith /*@C 7461b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 7462b3d3934dSBarry Smith 7463b3d3934dSBarry Smith Collective on TS 7464b3d3934dSBarry Smith 7465b3d3934dSBarry Smith Input Parameter: 7466b3d3934dSBarry Smith . ts - the TS context 7467b3d3934dSBarry Smith 7468b3d3934dSBarry Smith Output Parameter: 7469b3d3934dSBarry Smith + max - the maximum values 7470b3d3934dSBarry Smith - min - the minimum values 7471b3d3934dSBarry Smith 74727db568b7SBarry Smith Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 74737db568b7SBarry Smith 7474b3d3934dSBarry Smith Level: intermediate 7475b3d3934dSBarry Smith 7476b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7477b3d3934dSBarry Smith 7478b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 7479b3d3934dSBarry Smith @*/ 7480b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 7481b3d3934dSBarry Smith { 7482b3d3934dSBarry Smith PetscInt i; 7483b3d3934dSBarry Smith 7484b3d3934dSBarry Smith PetscFunctionBegin; 7485b3d3934dSBarry Smith if (max) *max = NULL; 7486b3d3934dSBarry Smith if (min) *min = NULL; 7487b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7488b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 74895537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 7490b3d3934dSBarry Smith if (max) *max = ctx->max; 7491b3d3934dSBarry Smith if (min) *min = ctx->min; 7492b3d3934dSBarry Smith break; 7493b3d3934dSBarry Smith } 7494b3d3934dSBarry Smith } 7495b3d3934dSBarry Smith PetscFunctionReturn(0); 7496b3d3934dSBarry Smith } 7497b3d3934dSBarry Smith 7498b3d3934dSBarry Smith /*@C 7499b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 7500b3d3934dSBarry Smith 7501b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 7502b3d3934dSBarry Smith 7503b3d3934dSBarry Smith Input Parameter: 7504b3d3934dSBarry Smith . ctx - the monitor context 7505b3d3934dSBarry Smith 7506b3d3934dSBarry Smith Level: intermediate 7507b3d3934dSBarry Smith 7508b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy 7509b3d3934dSBarry Smith 75107db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 7511b3d3934dSBarry Smith @*/ 7512b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 7513b3d3934dSBarry Smith { 7514b3d3934dSBarry Smith PetscErrorCode ierr; 7515b3d3934dSBarry Smith 7516b3d3934dSBarry Smith PetscFunctionBegin; 7517b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 7518b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 7519b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 7520b3d3934dSBarry Smith PetscFunctionReturn(0); 7521b3d3934dSBarry Smith } 7522f2dee214SBarry Smith 752324655328SShri /*@ 752424655328SShri TSRollBack - Rolls back one time step 752524655328SShri 752624655328SShri Collective on TS 752724655328SShri 752824655328SShri Input Parameter: 752924655328SShri . ts - the TS context obtained from TSCreate() 753024655328SShri 753124655328SShri Level: advanced 753224655328SShri 753324655328SShri .keywords: TS, timestep, rollback 753424655328SShri 753524655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 753624655328SShri @*/ 753724655328SShri PetscErrorCode TSRollBack(TS ts) 753824655328SShri { 753924655328SShri PetscErrorCode ierr; 754024655328SShri 754124655328SShri PetscFunctionBegin; 754224655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7543b3de5cdeSLisandro Dalcin if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called"); 754424655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 754524655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 754624655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 754724655328SShri ts->ptime = ts->ptime_prev; 7548be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 7549be5899b3SLisandro Dalcin ts->steps--; ts->total_steps--; 755010b82f12SShri Abhyankar ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 7551b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 755224655328SShri PetscFunctionReturn(0); 755324655328SShri } 7554aeb4809dSShri Abhyankar 7555ff22ae23SHong Zhang /*@ 7556ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 7557ff22ae23SHong Zhang 7558ff22ae23SHong Zhang Input Parameter: 7559ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 7560ff22ae23SHong Zhang 7561ff22ae23SHong Zhang Level: advanced 7562ff22ae23SHong Zhang 7563ff22ae23SHong Zhang .keywords: TS, getstages 7564ff22ae23SHong Zhang 7565ff22ae23SHong Zhang .seealso: TSCreate() 7566ff22ae23SHong Zhang @*/ 7567ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns,Vec **Y) 7568ff22ae23SHong Zhang { 7569ff22ae23SHong Zhang PetscErrorCode ierr; 7570ff22ae23SHong Zhang 7571ff22ae23SHong Zhang PetscFunctionBegin; 7572ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7573ff22ae23SHong Zhang PetscValidPointer(ns,2); 7574ff22ae23SHong Zhang 7575ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 7576ff22ae23SHong Zhang else { 7577ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 7578ff22ae23SHong Zhang } 7579ff22ae23SHong Zhang PetscFunctionReturn(0); 7580ff22ae23SHong Zhang } 7581ff22ae23SHong Zhang 7582847ff0e1SMatthew G. Knepley /*@C 7583847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 7584847ff0e1SMatthew G. Knepley 7585847ff0e1SMatthew G. Knepley Collective on SNES 7586847ff0e1SMatthew G. Knepley 7587847ff0e1SMatthew G. Knepley Input Parameters: 7588847ff0e1SMatthew G. Knepley + ts - the TS context 7589847ff0e1SMatthew G. Knepley . t - current timestep 7590847ff0e1SMatthew G. Knepley . U - state vector 7591847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 7592847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 7593847ff0e1SMatthew G. Knepley - ctx - an optional user context 7594847ff0e1SMatthew G. Knepley 7595847ff0e1SMatthew G. Knepley Output Parameters: 7596847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 7597847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 7598847ff0e1SMatthew G. Knepley 7599847ff0e1SMatthew G. Knepley Level: intermediate 7600847ff0e1SMatthew G. Knepley 7601847ff0e1SMatthew G. Knepley Notes: 7602847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 7603847ff0e1SMatthew G. Knepley 7604847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 7605847ff0e1SMatthew G. Knepley 7606847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 7607847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 7608847ff0e1SMatthew G. Knepley 7609847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 7610847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 7611847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 7612847ff0e1SMatthew G. Knepley 7613847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse 7614847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 7615847ff0e1SMatthew G. Knepley @*/ 7616847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 7617847ff0e1SMatthew G. Knepley { 7618847ff0e1SMatthew G. Knepley SNES snes; 7619847ff0e1SMatthew G. Knepley MatFDColoring color; 7620847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 7621847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 7622847ff0e1SMatthew G. Knepley 7623847ff0e1SMatthew G. Knepley PetscFunctionBegin; 7624c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 7625847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 7626847ff0e1SMatthew G. Knepley if (!color) { 7627847ff0e1SMatthew G. Knepley DM dm; 7628847ff0e1SMatthew G. Knepley ISColoring iscoloring; 7629847ff0e1SMatthew G. Knepley 7630847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 7631847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 7632847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 7633847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 7634847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7635847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7636847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7637847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7638847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7639847ff0e1SMatthew G. Knepley } else { 7640847ff0e1SMatthew G. Knepley MatColoring mc; 7641847ff0e1SMatthew G. Knepley 7642847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 7643847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 7644847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 7645847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 7646847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 7647847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 7648847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7649847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7650847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7651847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7652847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7653847ff0e1SMatthew G. Knepley } 7654847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 7655847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 7656847ff0e1SMatthew G. Knepley } 7657847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 7658847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 7659847ff0e1SMatthew G. Knepley if (J != B) { 7660847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7661847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7662847ff0e1SMatthew G. Knepley } 7663847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 7664847ff0e1SMatthew G. Knepley } 766593b34091SDebojyoti Ghosh 7666cb9d8021SPierre Barbier de Reuille /*@ 7667cb9d8021SPierre Barbier de Reuille TSSetFunctionDomainError - Set the function testing if the current state vector is valid 7668cb9d8021SPierre Barbier de Reuille 7669cb9d8021SPierre Barbier de Reuille Input Parameters: 7670cb9d8021SPierre Barbier de Reuille ts - the TS context 7671cb9d8021SPierre Barbier de Reuille func - function called within TSFunctionDomainError 7672cb9d8021SPierre Barbier de Reuille 7673cb9d8021SPierre Barbier de Reuille Level: intermediate 7674cb9d8021SPierre Barbier de Reuille 7675cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain 7676cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError() 7677cb9d8021SPierre Barbier de Reuille @*/ 7678cb9d8021SPierre Barbier de Reuille 7679d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 7680cb9d8021SPierre Barbier de Reuille { 7681cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7682cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7683cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 7684cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7685cb9d8021SPierre Barbier de Reuille } 7686cb9d8021SPierre Barbier de Reuille 7687cb9d8021SPierre Barbier de Reuille /*@ 7688cb9d8021SPierre Barbier de Reuille TSFunctionDomainError - Check if the current state is valid 7689cb9d8021SPierre Barbier de Reuille 7690cb9d8021SPierre Barbier de Reuille Input Parameters: 7691cb9d8021SPierre Barbier de Reuille ts - the TS context 7692cb9d8021SPierre Barbier de Reuille stagetime - time of the simulation 7693d183316bSPierre Barbier de Reuille Y - state vector to check. 7694cb9d8021SPierre Barbier de Reuille 7695cb9d8021SPierre Barbier de Reuille Output Parameter: 7696cb9d8021SPierre Barbier de Reuille accept - Set to PETSC_FALSE if the current state vector is valid. 7697cb9d8021SPierre Barbier de Reuille 7698cb9d8021SPierre Barbier de Reuille Note: 7699cb9d8021SPierre Barbier de Reuille This function should be used to ensure the state is in a valid part of the space. 7700cb9d8021SPierre Barbier de Reuille For example, one can ensure here all values are positive. 770196a0c994SBarry Smith 770296a0c994SBarry Smith Level: advanced 7703cb9d8021SPierre Barbier de Reuille @*/ 7704d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 7705cb9d8021SPierre Barbier de Reuille { 7706cb9d8021SPierre Barbier de Reuille PetscErrorCode ierr; 7707cb9d8021SPierre Barbier de Reuille 7708cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7709cb9d8021SPierre Barbier de Reuille 7710cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7711cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 7712cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 7713d183316bSPierre Barbier de Reuille PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 7714cb9d8021SPierre Barbier de Reuille } 7715cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7716cb9d8021SPierre Barbier de Reuille } 77171ceb14c0SBarry Smith 771893b34091SDebojyoti Ghosh /*@C 7719e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 772093b34091SDebojyoti Ghosh 772193b34091SDebojyoti Ghosh Collective on MPI_Comm 772293b34091SDebojyoti Ghosh 772393b34091SDebojyoti Ghosh Input Parameter: 772493b34091SDebojyoti Ghosh . tsin - The input TS 772593b34091SDebojyoti Ghosh 772693b34091SDebojyoti Ghosh Output Parameter: 7727e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 772893b34091SDebojyoti Ghosh 77295eca1a21SEmil Constantinescu Notes: 77305eca1a21SEmil 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. 77315eca1a21SEmil Constantinescu 7732baa10174SEmil 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); 77335eca1a21SEmil Constantinescu 77345eca1a21SEmil Constantinescu Level: developer 773593b34091SDebojyoti Ghosh 7736e5168f73SEmil Constantinescu .keywords: TS, clone 7737e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 773893b34091SDebojyoti Ghosh @*/ 7739baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 774093b34091SDebojyoti Ghosh { 774193b34091SDebojyoti Ghosh TS t; 774293b34091SDebojyoti Ghosh PetscErrorCode ierr; 7743dc846ba4SSatish Balay SNES snes_start; 7744dc846ba4SSatish Balay DM dm; 7745dc846ba4SSatish Balay TSType type; 774693b34091SDebojyoti Ghosh 774793b34091SDebojyoti Ghosh PetscFunctionBegin; 774893b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 774993b34091SDebojyoti Ghosh *tsout = NULL; 775093b34091SDebojyoti Ghosh 77517a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 775293b34091SDebojyoti Ghosh 775393b34091SDebojyoti Ghosh /* General TS description */ 775493b34091SDebojyoti Ghosh t->numbermonitors = 0; 775593b34091SDebojyoti Ghosh t->setupcalled = 0; 775693b34091SDebojyoti Ghosh t->ksp_its = 0; 775793b34091SDebojyoti Ghosh t->snes_its = 0; 775893b34091SDebojyoti Ghosh t->nwork = 0; 775993b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 776093b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 776193b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 776293b34091SDebojyoti Ghosh 776334561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr); 776434561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr); 7765d15a3a53SEmil Constantinescu 776693b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr); 776793b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm);CHKERRQ(ierr); 776893b34091SDebojyoti Ghosh 776993b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 777051699248SLisandro Dalcin ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr); 777193b34091SDebojyoti Ghosh 7772e7069c78SShri t->trajectory = tsin->trajectory; 7773e7069c78SShri ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr); 7774e7069c78SShri 7775e7069c78SShri t->event = tsin->event; 77766b10a48eSSatish Balay if (t->event) t->event->refct++; 7777e7069c78SShri 777893b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 777993b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 7780e7069c78SShri t->ptime_prev = tsin->ptime_prev; 778193b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 778293b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 778393b34091SDebojyoti Ghosh t->steps = tsin->steps; 7784e7069c78SShri t->total_steps = tsin->total_steps; 778593b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 778693b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 778793b34091SDebojyoti Ghosh t->atol = tsin->atol; 778893b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 778993b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 779093b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 779193b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 779293b34091SDebojyoti Ghosh 779393b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type);CHKERRQ(ierr); 779493b34091SDebojyoti Ghosh ierr = TSSetType(t,type);CHKERRQ(ierr); 779593b34091SDebojyoti Ghosh 779693b34091SDebojyoti Ghosh t->vec_sol = NULL; 779793b34091SDebojyoti Ghosh 779893b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 779993b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 780093b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 780193b34091SDebojyoti Ghosh 780293b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 780393b34091SDebojyoti Ghosh 78040d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 78050d4fed19SBarry Smith PetscInt i; 78060d4fed19SBarry Smith ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr); 78070d4fed19SBarry Smith for (i=0; i<10; i++) { 78080d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 78090d4fed19SBarry Smith } 78100d4fed19SBarry Smith } 781193b34091SDebojyoti Ghosh *tsout = t; 781293b34091SDebojyoti Ghosh PetscFunctionReturn(0); 781393b34091SDebojyoti Ghosh } 7814