163dd3a1aSKris Buschelman 2af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 3496e6a7aSJed Brown #include <petscdmshell.h> 41e25c274SJed Brown #include <petscdmda.h> 52d5ee99bSBarry Smith #include <petscviewer.h> 62d5ee99bSBarry Smith #include <petscdraw.h> 7d763cef2SBarry Smith 8d5ba7fb7SMatthew Knepley /* Logging support */ 9d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 108d0ad7a8SHong Zhang PetscLogEvent TS_AdjointStep, TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 11d405a339SMatthew Knepley 12feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1349354f04SShri Abhyankar 142d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx { 152d5ee99bSBarry Smith PetscViewer viewer; 162d5ee99bSBarry Smith Vec initialsolution; 172d5ee99bSBarry Smith PetscBool showinitial; 182d5ee99bSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 192d5ee99bSBarry Smith PetscBool showtimestepandtime; 202d5ee99bSBarry Smith }; 212d5ee99bSBarry Smith 22bdad233fSMatthew Knepley #undef __FUNCT__ 23fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSetFromOptions" 24fde5950dSBarry Smith /*@C 25fde5950dSBarry Smith TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 26fde5950dSBarry Smith 27fde5950dSBarry Smith Collective on TS 28fde5950dSBarry Smith 29fde5950dSBarry Smith Input Parameters: 30fde5950dSBarry Smith + ts - TS object you wish to monitor 31fde5950dSBarry Smith . name - the monitor type one is seeking 32fde5950dSBarry Smith . help - message indicating what monitoring is done 33fde5950dSBarry Smith . manual - manual page for the monitor 34fde5950dSBarry Smith . monitor - the monitor function 35fde5950dSBarry Smith - monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects 36fde5950dSBarry Smith 37fde5950dSBarry Smith Level: developer 38fde5950dSBarry Smith 39fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 40fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 41fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 42fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 43fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 44fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 45fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 46fde5950dSBarry Smith @*/ 47721cd6eeSBarry 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*)) 48fde5950dSBarry Smith { 49fde5950dSBarry Smith PetscErrorCode ierr; 50fde5950dSBarry Smith PetscViewer viewer; 51fde5950dSBarry Smith PetscViewerFormat format; 52fde5950dSBarry Smith PetscBool flg; 53fde5950dSBarry Smith 54fde5950dSBarry Smith PetscFunctionBegin; 55fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 56fde5950dSBarry Smith if (flg) { 57721cd6eeSBarry Smith PetscViewerAndFormat *vf; 58721cd6eeSBarry Smith ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr); 59721cd6eeSBarry Smith ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 60fde5950dSBarry Smith if (monitorsetup) { 61721cd6eeSBarry Smith ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr); 62fde5950dSBarry Smith } 63721cd6eeSBarry Smith ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr); 64fde5950dSBarry Smith } 65fde5950dSBarry Smith PetscFunctionReturn(0); 66fde5950dSBarry Smith } 67fde5950dSBarry Smith 68fde5950dSBarry Smith #undef __FUNCT__ 69fde5950dSBarry Smith #define __FUNCT__ "TSAdjointMonitorSetFromOptions" 70fde5950dSBarry Smith /*@C 71fde5950dSBarry Smith TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 72fde5950dSBarry Smith 73fde5950dSBarry Smith Collective on TS 74fde5950dSBarry Smith 75fde5950dSBarry Smith Input Parameters: 76fde5950dSBarry Smith + ts - TS object you wish to monitor 77fde5950dSBarry Smith . name - the monitor type one is seeking 78fde5950dSBarry Smith . help - message indicating what monitoring is done 79fde5950dSBarry Smith . manual - manual page for the monitor 80fde5950dSBarry Smith . monitor - the monitor function 81fde5950dSBarry 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 82fde5950dSBarry Smith 83fde5950dSBarry Smith Level: developer 84fde5950dSBarry Smith 85fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 86fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 87fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 88fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 89fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 90fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 91fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 92fde5950dSBarry Smith @*/ 93721cd6eeSBarry 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*)) 94fde5950dSBarry Smith { 95fde5950dSBarry Smith PetscErrorCode ierr; 96fde5950dSBarry Smith PetscViewer viewer; 97fde5950dSBarry Smith PetscViewerFormat format; 98fde5950dSBarry Smith PetscBool flg; 99fde5950dSBarry Smith 100fde5950dSBarry Smith PetscFunctionBegin; 101fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 102fde5950dSBarry Smith if (flg) { 103721cd6eeSBarry Smith PetscViewerAndFormat *vf; 104721cd6eeSBarry Smith ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr); 105721cd6eeSBarry Smith ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 106fde5950dSBarry Smith if (monitorsetup) { 107721cd6eeSBarry Smith ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr); 108fde5950dSBarry Smith } 109721cd6eeSBarry Smith ierr = TSAdjointMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr); 110fde5950dSBarry Smith } 111fde5950dSBarry Smith PetscFunctionReturn(0); 112fde5950dSBarry Smith } 113fde5950dSBarry Smith 114fde5950dSBarry Smith #undef __FUNCT__ 115bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions" 116bdad233fSMatthew Knepley /*@ 117bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 118bdad233fSMatthew Knepley 119bdad233fSMatthew Knepley Collective on TS 120bdad233fSMatthew Knepley 121bdad233fSMatthew Knepley Input Parameter: 122bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 123bdad233fSMatthew Knepley 124bdad233fSMatthew Knepley Options Database Keys: 125cd11d68dSLisandro Dalcin + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGL, TSSSP 126ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 1273e4cdcaaSBarry Smith . -ts_max_steps <maxsteps> - maximum number of time-steps to take 1283e4cdcaaSBarry Smith . -ts_final_time <time> - maximum time to compute to 1293e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 130a3cdaa26SBarry 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 131a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 132a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 133a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 134a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 135a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 136ef222394SBarry Smith . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 137847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 138bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 139de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 140de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 1416934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 142de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 143de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 144de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 145de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 1463e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 1473e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 148fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 149b3d3934dSBarry Smith . -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 150110eb670SHong Zhang . -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 151110eb670SHong Zhang . -ts_adjoint_monitor - print information at each adjoint time step 15253ea634cSHong Zhang - -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically 15353ea634cSHong Zhang 15491b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 155bdad233fSMatthew Knepley 156bdad233fSMatthew Knepley Level: beginner 157bdad233fSMatthew Knepley 158bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 159bdad233fSMatthew Knepley 160a313700dSBarry Smith .seealso: TSGetType() 161bdad233fSMatthew Knepley @*/ 1627087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 163bdad233fSMatthew Knepley { 164bc952696SBarry Smith PetscBool opt,flg,tflg; 165dfbe8321SBarry Smith PetscErrorCode ierr; 166eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 16731748224SBarry Smith PetscReal time_step; 16849354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 169d1212d36SBarry Smith char dir[16]; 170cd11d68dSLisandro Dalcin TSIFunction ifun; 1716991f827SBarry Smith const char *defaultType; 1726991f827SBarry Smith char typeName[256]; 173bdad233fSMatthew Knepley 174bdad233fSMatthew Knepley PetscFunctionBegin; 1750700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1766991f827SBarry Smith 1776991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 178cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 179cd11d68dSLisandro Dalcin 180cd11d68dSLisandro Dalcin ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 181cd11d68dSLisandro Dalcin if (((PetscObject)ts)->type_name) 182cd11d68dSLisandro Dalcin defaultType = ((PetscObject)ts)->type_name; 183cd11d68dSLisandro Dalcin else 184cd11d68dSLisandro Dalcin defaultType = ifun ? TSBEULER : TSEULER; 1856991f827SBarry Smith ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr); 1866991f827SBarry Smith if (opt) { 1876991f827SBarry Smith ierr = TSSetType(ts,typeName);CHKERRQ(ierr); 1886991f827SBarry Smith } else { 1896991f827SBarry Smith ierr = TSSetType(ts,defaultType);CHKERRQ(ierr); 1906991f827SBarry Smith } 191bdad233fSMatthew Knepley 192bdad233fSMatthew Knepley /* Handle generic TS options */ 1930298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1940298fd71SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 1950298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 19631748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 197cd11d68dSLisandro Dalcin if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);} 19849354f04SShri 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); 19949354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 2000298fd71SBarry 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); 2010298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 2020298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 2030298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 2040298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 205bdad233fSMatthew Knepley 20656f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 20756f85f32SBarry Smith { 20856f85f32SBarry Smith PetscBool set; 20956f85f32SBarry Smith flg = PETSC_FALSE; 21056f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 21156f85f32SBarry Smith if (set) { 21256f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 21356f85f32SBarry Smith } 21456f85f32SBarry Smith } 21556f85f32SBarry Smith #endif 21656f85f32SBarry Smith 217bdad233fSMatthew Knepley /* Monitor options */ 218cd11d68dSLisandro Dalcin ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr); 219fde5950dSBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr); 220fde5950dSBarry Smith ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr); 221fde5950dSBarry Smith 2225180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2235180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 2245180491cSLisandro Dalcin 2254f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 226b3603a34SBarry Smith if (opt) { 2270b039ecaSBarry Smith TSMonitorLGCtx ctx; 2283923b477SBarry Smith PetscInt howoften = 1; 229b3603a34SBarry Smith 2300298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 2316ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2324f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 233bdad233fSMatthew Knepley } 2346ba87a44SLisandro Dalcin 2354f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 236ef20d060SBarry Smith if (opt) { 2370b039ecaSBarry Smith TSMonitorLGCtx ctx; 2383923b477SBarry Smith PetscInt howoften = 1; 239ef20d060SBarry Smith 2400298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 2416ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2424f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 243ef20d060SBarry Smith } 2446934998bSLisandro Dalcin 2456934998bSLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 2466934998bSLisandro Dalcin if (opt) { 2476934998bSLisandro Dalcin TSMonitorLGCtx ctx; 2486934998bSLisandro Dalcin PetscInt howoften = 1; 2496934998bSLisandro Dalcin 2506934998bSLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 2516ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2526934998bSLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 2536934998bSLisandro Dalcin } 254201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 255201da799SBarry Smith if (opt) { 256201da799SBarry Smith TSMonitorLGCtx ctx; 257201da799SBarry Smith PetscInt howoften = 1; 258201da799SBarry Smith 2590298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2606ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 261201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 262201da799SBarry Smith } 263201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 264201da799SBarry Smith if (opt) { 265201da799SBarry Smith TSMonitorLGCtx ctx; 266201da799SBarry Smith PetscInt howoften = 1; 267201da799SBarry Smith 2680298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2696ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 270201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 271201da799SBarry Smith } 2728189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 2738189c53fSBarry Smith if (opt) { 2748189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2758189c53fSBarry Smith PetscInt howoften = 1; 2768189c53fSBarry Smith 2770298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 2786934998bSLisandro Dalcin ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 2798189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 2808189c53fSBarry Smith } 281ef20d060SBarry Smith opt = PETSC_FALSE; 2820dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 283a7cc72afSBarry Smith if (opt) { 28483a4ac43SBarry Smith TSMonitorDrawCtx ctx; 28583a4ac43SBarry Smith PetscInt howoften = 1; 286a80ad3e0SBarry Smith 2870298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 2886934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 28983a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 290bdad233fSMatthew Knepley } 291fb1732b5SBarry Smith opt = PETSC_FALSE; 2929110b2e7SHong Zhang ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr); 2939110b2e7SHong Zhang if (opt) { 2949110b2e7SHong Zhang TSMonitorDrawCtx ctx; 2959110b2e7SHong Zhang PetscInt howoften = 1; 2969110b2e7SHong Zhang 2979110b2e7SHong Zhang ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr); 2986934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 2999110b2e7SHong Zhang ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3009110b2e7SHong Zhang } 3019110b2e7SHong Zhang opt = PETSC_FALSE; 3022d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 3032d5ee99bSBarry Smith if (opt) { 3042d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 3052d5ee99bSBarry Smith PetscReal bounds[4]; 3062d5ee99bSBarry Smith PetscInt n = 4; 3072d5ee99bSBarry Smith PetscDraw draw; 3086934998bSLisandro Dalcin PetscDrawAxis axis; 3092d5ee99bSBarry Smith 3102d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 3112d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 3126934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr); 3132d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 3146934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr); 3156934998bSLisandro Dalcin ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 3166934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 3172d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3182d5ee99bSBarry Smith } 3192d5ee99bSBarry Smith opt = PETSC_FALSE; 3200dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 3213a471f94SBarry Smith if (opt) { 32283a4ac43SBarry Smith TSMonitorDrawCtx ctx; 32383a4ac43SBarry Smith PetscInt howoften = 1; 3243a471f94SBarry Smith 3250298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 3266934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 32783a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3283a471f94SBarry Smith } 329fde5950dSBarry Smith 330ed81e22dSJed Brown opt = PETSC_FALSE; 33191b97e58SBarry 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); 332ed81e22dSJed Brown if (flg) { 333ed81e22dSJed Brown const char *ptr,*ptr2; 334ed81e22dSJed Brown char *filetemplate; 335ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 336ed81e22dSJed Brown /* Do some cursory validation of the input. */ 337ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 338ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 339ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 340ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 341ce94432eSBarry 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"); 342ed81e22dSJed Brown if (ptr2) break; 343ed81e22dSJed Brown } 344ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 345ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 346ed81e22dSJed Brown } 347bdad233fSMatthew Knepley 348d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 349d1212d36SBarry Smith if (flg) { 350d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 351d1212d36SBarry Smith int ray = 0; 352d1212d36SBarry Smith DMDADirection ddir; 353d1212d36SBarry Smith DM da; 354d1212d36SBarry Smith PetscMPIInt rank; 355d1212d36SBarry Smith 356ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 357d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 358d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 359ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 360d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 361d1212d36SBarry Smith 362d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 363b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 364d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 365d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 366ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 367d1212d36SBarry Smith if (!rank) { 368d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 369d1212d36SBarry Smith } 37051b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 371d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 372d1212d36SBarry Smith } 37351b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 37451b4a12fSMatthew G. Knepley if (flg) { 37551b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 37651b4a12fSMatthew G. Knepley int ray = 0; 37751b4a12fSMatthew G. Knepley DMDADirection ddir; 37851b4a12fSMatthew G. Knepley DM da; 37951b4a12fSMatthew G. Knepley PetscInt howoften = 1; 380d1212d36SBarry Smith 38151b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 38251b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 38351b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 38451b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 38551b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 3861c3436cfSJed Brown 38751b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 388b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 38951b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 39051b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 39151b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 39251b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 39351b4a12fSMatthew G. Knepley } 394a7a1495cSBarry Smith 395b3d3934dSBarry Smith ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 396b3d3934dSBarry Smith if (opt) { 397b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 398b3d3934dSBarry Smith 399b3d3934dSBarry Smith ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 400b3d3934dSBarry Smith ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 401b3d3934dSBarry Smith } 402b3d3934dSBarry Smith 403847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 404847ff0e1SMatthew G. Knepley ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr); 405847ff0e1SMatthew G. Knepley if (flg) { 406847ff0e1SMatthew G. Knepley DM dm; 407847ff0e1SMatthew G. Knepley DMTS tdm; 408847ff0e1SMatthew G. Knepley 409847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 410847ff0e1SMatthew G. Knepley ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr); 411847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 412847ff0e1SMatthew G. Knepley ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr); 413847ff0e1SMatthew G. Knepley ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 414847ff0e1SMatthew G. Knepley } 415847ff0e1SMatthew G. Knepley 416ee346746SBarry Smith if (ts->adapt) { 417ee346746SBarry Smith ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr); 418ee346746SBarry Smith } 419d763cef2SBarry Smith 420d763cef2SBarry Smith /* Handle specific TS options */ 421d763cef2SBarry Smith if (ts->ops->setfromoptions) { 4226991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 423d763cef2SBarry Smith } 424fbc52257SHong Zhang 42568bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 4264f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 42768bece0bSHong Zhang ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 42868bece0bSHong Zhang if (tflg) { 42968bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 43068bece0bSHong Zhang } 4314f122a70SLisandro Dalcin tflg = ts->adjoint_solve ? PETSC_TRUE : PETSC_FALSE; 43268bece0bSHong Zhang ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr); 43368bece0bSHong Zhang if (flg) { 43468bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 43568bece0bSHong Zhang ts->adjoint_solve = tflg; 43668bece0bSHong Zhang } 437d763cef2SBarry Smith 438d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 4390633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr); 440fbc52257SHong Zhang ierr = PetscOptionsEnd();CHKERRQ(ierr); 441d763cef2SBarry Smith 4424f122a70SLisandro Dalcin if (ts->trajectory) { 4434f122a70SLisandro Dalcin ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 444d763cef2SBarry Smith } 44568bece0bSHong Zhang 4464f122a70SLisandro Dalcin ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr); 4474f122a70SLisandro Dalcin if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);} 4484f122a70SLisandro Dalcin ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr); 449d763cef2SBarry Smith PetscFunctionReturn(0); 450d763cef2SBarry Smith } 451d763cef2SBarry Smith 452d763cef2SBarry Smith #undef __FUNCT__ 453bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory" 454d2daff3dSHong Zhang /*@ 455bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 456d2daff3dSHong Zhang 457d2daff3dSHong Zhang Collective on TS 458d2daff3dSHong Zhang 459d2daff3dSHong Zhang Input Parameters: 460bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 461bc952696SBarry Smith 46268bece0bSHong Zhang Note: This routine should be called after all TS options have been set 463d2daff3dSHong Zhang 464d2daff3dSHong Zhang Level: intermediate 465d2daff3dSHong Zhang 466bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve() 467d2daff3dSHong Zhang 468bc952696SBarry Smith .keywords: TS, set, checkpoint, 469d2daff3dSHong Zhang @*/ 470bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 471d2daff3dSHong Zhang { 472bc952696SBarry Smith PetscErrorCode ierr; 473bc952696SBarry Smith 474d2daff3dSHong Zhang PetscFunctionBegin; 475d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 476bc952696SBarry Smith if (!ts->trajectory) { 477bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 478972caf09SHong Zhang ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 479bc952696SBarry Smith } 480d2daff3dSHong Zhang PetscFunctionReturn(0); 481d2daff3dSHong Zhang } 482d2daff3dSHong Zhang 483a7a1495cSBarry Smith #undef __FUNCT__ 484a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian" 485a7a1495cSBarry Smith /*@ 486a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 487a7a1495cSBarry Smith set with TSSetRHSJacobian(). 488a7a1495cSBarry Smith 489a7a1495cSBarry Smith Collective on TS and Vec 490a7a1495cSBarry Smith 491a7a1495cSBarry Smith Input Parameters: 492316643e7SJed Brown + ts - the TS context 493a7a1495cSBarry Smith . t - current timestep 4940910c330SBarry Smith - U - input vector 495a7a1495cSBarry Smith 496a7a1495cSBarry Smith Output Parameters: 497a7a1495cSBarry Smith + A - Jacobian matrix 498a7a1495cSBarry Smith . B - optional preconditioning matrix 499a7a1495cSBarry Smith - flag - flag indicating matrix structure 500a7a1495cSBarry Smith 501a7a1495cSBarry Smith Notes: 502a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 503a7a1495cSBarry Smith is used internally within the nonlinear solvers. 504a7a1495cSBarry Smith 50594b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 506a7a1495cSBarry Smith flag parameter. 507a7a1495cSBarry Smith 508a7a1495cSBarry Smith Level: developer 509a7a1495cSBarry Smith 510a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 511a7a1495cSBarry Smith 51294b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 513a7a1495cSBarry Smith @*/ 514d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 515a7a1495cSBarry Smith { 516dfbe8321SBarry Smith PetscErrorCode ierr; 517270bf2e7SJed Brown PetscObjectState Ustate; 51824989b8cSPeter Brune DM dm; 519942e3340SBarry Smith DMTS tsdm; 52024989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 52124989b8cSPeter Brune void *ctx; 52224989b8cSPeter Brune TSIJacobian ijacobianfunc; 523b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 524a7a1495cSBarry Smith 525a7a1495cSBarry Smith PetscFunctionBegin; 5260700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5270910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5280910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 52924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 530942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 53124989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 5320298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 533b2df71adSDebojyoti Ghosh ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 53459e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 535b2df71adSDebojyoti Ghosh if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 5360e4ef248SJed Brown PetscFunctionReturn(0); 537f8ede8e7SJed Brown } 538d90be118SSean Farley 539ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 540d90be118SSean Farley 541e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 54294ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 54394ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 54494ab13aaSBarry Smith if (A != B) { 54594ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 54694ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 547e1244c69SJed Brown } 548e1244c69SJed Brown ts->rhsjacobian.shift = 0; 549e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 550e1244c69SJed Brown } 551e1244c69SJed Brown 55224989b8cSPeter Brune if (rhsjacobianfunc) { 5536cd88445SBarry Smith PetscBool missing; 55494ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 555a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 556d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 557a7a1495cSBarry Smith PetscStackPop; 55894ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 5596cd88445SBarry Smith if (A) { 5606cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 5616cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 5626cd88445SBarry Smith } 5636cd88445SBarry Smith if (B && B != A) { 5646cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 5656cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 5666cd88445SBarry Smith } 567ef66eb69SBarry Smith } else { 56894ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 56994ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 570ef66eb69SBarry Smith } 5710e4ef248SJed Brown ts->rhsjacobian.time = t; 5720910c330SBarry Smith ts->rhsjacobian.X = U; 57359e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 574a7a1495cSBarry Smith PetscFunctionReturn(0); 575a7a1495cSBarry Smith } 576a7a1495cSBarry Smith 5774a2ae208SSatish Balay #undef __FUNCT__ 5784a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 579316643e7SJed Brown /*@ 580d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 581d763cef2SBarry Smith 582316643e7SJed Brown Collective on TS and Vec 583316643e7SJed Brown 584316643e7SJed Brown Input Parameters: 585316643e7SJed Brown + ts - the TS context 586316643e7SJed Brown . t - current time 5870910c330SBarry Smith - U - state vector 588316643e7SJed Brown 589316643e7SJed Brown Output Parameter: 590316643e7SJed Brown . y - right hand side 591316643e7SJed Brown 592316643e7SJed Brown Note: 593316643e7SJed Brown Most users should not need to explicitly call this routine, as it 594316643e7SJed Brown is used internally within the nonlinear solvers. 595316643e7SJed Brown 596316643e7SJed Brown Level: developer 597316643e7SJed Brown 598316643e7SJed Brown .keywords: TS, compute 599316643e7SJed Brown 600316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 601316643e7SJed Brown @*/ 6020910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 603d763cef2SBarry Smith { 604dfbe8321SBarry Smith PetscErrorCode ierr; 60524989b8cSPeter Brune TSRHSFunction rhsfunction; 60624989b8cSPeter Brune TSIFunction ifunction; 60724989b8cSPeter Brune void *ctx; 60824989b8cSPeter Brune DM dm; 60924989b8cSPeter Brune 610d763cef2SBarry Smith PetscFunctionBegin; 6110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6120910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6130700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 61424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 61524989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 6160298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 617d763cef2SBarry Smith 618ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 619d763cef2SBarry Smith 6200910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 62124989b8cSPeter Brune if (rhsfunction) { 622d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 6230910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 624d763cef2SBarry Smith PetscStackPop; 625214bc6a2SJed Brown } else { 626214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 627b2cd27e8SJed Brown } 62844a41b28SSean Farley 6290910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 630d763cef2SBarry Smith PetscFunctionReturn(0); 631d763cef2SBarry Smith } 632d763cef2SBarry Smith 6334a2ae208SSatish Balay #undef __FUNCT__ 634ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 635ef20d060SBarry Smith /*@ 636ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 637ef20d060SBarry Smith 638ef20d060SBarry Smith Collective on TS and Vec 639ef20d060SBarry Smith 640ef20d060SBarry Smith Input Parameters: 641ef20d060SBarry Smith + ts - the TS context 642ef20d060SBarry Smith - t - current time 643ef20d060SBarry Smith 644ef20d060SBarry Smith Output Parameter: 6450910c330SBarry Smith . U - the solution 646ef20d060SBarry Smith 647ef20d060SBarry Smith Note: 648ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 649ef20d060SBarry Smith is used internally within the nonlinear solvers. 650ef20d060SBarry Smith 651ef20d060SBarry Smith Level: developer 652ef20d060SBarry Smith 653ef20d060SBarry Smith .keywords: TS, compute 654ef20d060SBarry Smith 655abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 656ef20d060SBarry Smith @*/ 6570910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 658ef20d060SBarry Smith { 659ef20d060SBarry Smith PetscErrorCode ierr; 660ef20d060SBarry Smith TSSolutionFunction solutionfunction; 661ef20d060SBarry Smith void *ctx; 662ef20d060SBarry Smith DM dm; 663ef20d060SBarry Smith 664ef20d060SBarry Smith PetscFunctionBegin; 665ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6660910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 667ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 668ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 669ef20d060SBarry Smith 670ef20d060SBarry Smith if (solutionfunction) { 6719b7cd975SBarry Smith PetscStackPush("TS user solution function"); 6720910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 673ef20d060SBarry Smith PetscStackPop; 674ef20d060SBarry Smith } 675ef20d060SBarry Smith PetscFunctionReturn(0); 676ef20d060SBarry Smith } 6779b7cd975SBarry Smith #undef __FUNCT__ 6789b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction" 6799b7cd975SBarry Smith /*@ 6809b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6819b7cd975SBarry Smith 6829b7cd975SBarry Smith Collective on TS and Vec 6839b7cd975SBarry Smith 6849b7cd975SBarry Smith Input Parameters: 6859b7cd975SBarry Smith + ts - the TS context 6869b7cd975SBarry Smith - t - current time 6879b7cd975SBarry Smith 6889b7cd975SBarry Smith Output Parameter: 6899b7cd975SBarry Smith . U - the function value 6909b7cd975SBarry Smith 6919b7cd975SBarry Smith Note: 6929b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 6939b7cd975SBarry Smith is used internally within the nonlinear solvers. 6949b7cd975SBarry Smith 6959b7cd975SBarry Smith Level: developer 6969b7cd975SBarry Smith 6979b7cd975SBarry Smith .keywords: TS, compute 6989b7cd975SBarry Smith 6999b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 7009b7cd975SBarry Smith @*/ 7019b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 7029b7cd975SBarry Smith { 7039b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 7049b7cd975SBarry Smith void *ctx; 7059b7cd975SBarry Smith DM dm; 7069b7cd975SBarry Smith 7079b7cd975SBarry Smith PetscFunctionBegin; 7089b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7099b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7109b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 7119b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 7129b7cd975SBarry Smith 7139b7cd975SBarry Smith if (forcing) { 7149b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 7159b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 7169b7cd975SBarry Smith PetscStackPop; 7179b7cd975SBarry Smith } 7189b7cd975SBarry Smith PetscFunctionReturn(0); 7199b7cd975SBarry Smith } 720ef20d060SBarry Smith 721ef20d060SBarry Smith #undef __FUNCT__ 722214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 723214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 724214bc6a2SJed Brown { 7252dd45cf8SJed Brown Vec F; 726214bc6a2SJed Brown PetscErrorCode ierr; 727214bc6a2SJed Brown 728214bc6a2SJed Brown PetscFunctionBegin; 7290298fd71SBarry Smith *Frhs = NULL; 7300298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 731214bc6a2SJed Brown if (!ts->Frhs) { 7322dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 733214bc6a2SJed Brown } 734214bc6a2SJed Brown *Frhs = ts->Frhs; 735214bc6a2SJed Brown PetscFunctionReturn(0); 736214bc6a2SJed Brown } 737214bc6a2SJed Brown 738214bc6a2SJed Brown #undef __FUNCT__ 739214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 740214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 741214bc6a2SJed Brown { 742214bc6a2SJed Brown Mat A,B; 7432dd45cf8SJed Brown PetscErrorCode ierr; 744214bc6a2SJed Brown 745214bc6a2SJed Brown PetscFunctionBegin; 746c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 747c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 7480298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 749214bc6a2SJed Brown if (Arhs) { 750214bc6a2SJed Brown if (!ts->Arhs) { 751214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 752214bc6a2SJed Brown } 753214bc6a2SJed Brown *Arhs = ts->Arhs; 754214bc6a2SJed Brown } 755214bc6a2SJed Brown if (Brhs) { 756214bc6a2SJed Brown if (!ts->Brhs) { 757bdb70873SJed Brown if (A != B) { 758214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 759bdb70873SJed Brown } else { 760bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 76151699248SLisandro Dalcin ts->Brhs = ts->Arhs; 762bdb70873SJed Brown } 763214bc6a2SJed Brown } 764214bc6a2SJed Brown *Brhs = ts->Brhs; 765214bc6a2SJed Brown } 766214bc6a2SJed Brown PetscFunctionReturn(0); 767214bc6a2SJed Brown } 768214bc6a2SJed Brown 769214bc6a2SJed Brown #undef __FUNCT__ 770316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 771316643e7SJed Brown /*@ 7720910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 773316643e7SJed Brown 774316643e7SJed Brown Collective on TS and Vec 775316643e7SJed Brown 776316643e7SJed Brown Input Parameters: 777316643e7SJed Brown + ts - the TS context 778316643e7SJed Brown . t - current time 7790910c330SBarry Smith . U - state vector 7800910c330SBarry Smith . Udot - time derivative of state vector 781214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 782316643e7SJed Brown 783316643e7SJed Brown Output Parameter: 784316643e7SJed Brown . Y - right hand side 785316643e7SJed Brown 786316643e7SJed Brown Note: 787316643e7SJed Brown Most users should not need to explicitly call this routine, as it 788316643e7SJed Brown is used internally within the nonlinear solvers. 789316643e7SJed Brown 790316643e7SJed Brown If the user did did not write their equations in implicit form, this 791316643e7SJed Brown function recasts them in implicit form. 792316643e7SJed Brown 793316643e7SJed Brown Level: developer 794316643e7SJed Brown 795316643e7SJed Brown .keywords: TS, compute 796316643e7SJed Brown 797316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 798316643e7SJed Brown @*/ 7990910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 800316643e7SJed Brown { 801316643e7SJed Brown PetscErrorCode ierr; 80224989b8cSPeter Brune TSIFunction ifunction; 80324989b8cSPeter Brune TSRHSFunction rhsfunction; 80424989b8cSPeter Brune void *ctx; 80524989b8cSPeter Brune DM dm; 806316643e7SJed Brown 807316643e7SJed Brown PetscFunctionBegin; 8080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8090910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8100910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 8110700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 812316643e7SJed Brown 81324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 81424989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 8150298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 81624989b8cSPeter Brune 817ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 818d90be118SSean Farley 8190910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 82024989b8cSPeter Brune if (ifunction) { 821316643e7SJed Brown PetscStackPush("TS user implicit function"); 8220910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 823316643e7SJed Brown PetscStackPop; 824214bc6a2SJed Brown } 825214bc6a2SJed Brown if (imex) { 82624989b8cSPeter Brune if (!ifunction) { 8270910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 8282dd45cf8SJed Brown } 82924989b8cSPeter Brune } else if (rhsfunction) { 83024989b8cSPeter Brune if (ifunction) { 831214bc6a2SJed Brown Vec Frhs; 832214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 8330910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 834214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 8352dd45cf8SJed Brown } else { 8360910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 8370910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 838316643e7SJed Brown } 8394a6899ffSJed Brown } 8400910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 841316643e7SJed Brown PetscFunctionReturn(0); 842316643e7SJed Brown } 843316643e7SJed Brown 844316643e7SJed Brown #undef __FUNCT__ 845316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 846316643e7SJed Brown /*@ 847316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 848316643e7SJed Brown 849316643e7SJed Brown Collective on TS and Vec 850316643e7SJed Brown 851316643e7SJed Brown Input 852316643e7SJed Brown Input Parameters: 853316643e7SJed Brown + ts - the TS context 854316643e7SJed Brown . t - current timestep 8550910c330SBarry Smith . U - state vector 8560910c330SBarry Smith . Udot - time derivative of state vector 857214bc6a2SJed Brown . shift - shift to apply, see note below 858214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 859316643e7SJed Brown 860316643e7SJed Brown Output Parameters: 861316643e7SJed Brown + A - Jacobian matrix 862316643e7SJed Brown . B - optional preconditioning matrix 863316643e7SJed Brown - flag - flag indicating matrix structure 864316643e7SJed Brown 865316643e7SJed Brown Notes: 8660910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 867316643e7SJed Brown 8680910c330SBarry Smith dF/dU + shift*dF/dUdot 869316643e7SJed Brown 870316643e7SJed Brown Most users should not need to explicitly call this routine, as it 871316643e7SJed Brown is used internally within the nonlinear solvers. 872316643e7SJed Brown 873316643e7SJed Brown Level: developer 874316643e7SJed Brown 875316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 876316643e7SJed Brown 877316643e7SJed Brown .seealso: TSSetIJacobian() 87863495f91SJed Brown @*/ 879d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 880316643e7SJed Brown { 881316643e7SJed Brown PetscErrorCode ierr; 88224989b8cSPeter Brune TSIJacobian ijacobian; 88324989b8cSPeter Brune TSRHSJacobian rhsjacobian; 88424989b8cSPeter Brune DM dm; 88524989b8cSPeter Brune void *ctx; 886316643e7SJed Brown 887316643e7SJed Brown PetscFunctionBegin; 8880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8890910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8900910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 891316643e7SJed Brown PetscValidPointer(A,6); 89294ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 893316643e7SJed Brown PetscValidPointer(B,7); 89494ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 89524989b8cSPeter Brune 89624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 89724989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 8980298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 89924989b8cSPeter Brune 900ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 901316643e7SJed Brown 90294ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 90324989b8cSPeter Brune if (ijacobian) { 9046cd88445SBarry Smith PetscBool missing; 905316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 906d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 907316643e7SJed Brown PetscStackPop; 9086cd88445SBarry Smith if (A) { 9096cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 9106cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 9116cd88445SBarry Smith } 9126cd88445SBarry Smith if (B && B != A) { 9136cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 9146cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 9156cd88445SBarry Smith } 9164a6899ffSJed Brown } 917214bc6a2SJed Brown if (imex) { 918b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 91994ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 92094ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 92194ab13aaSBarry Smith if (A != B) { 92294ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 92394ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 924214bc6a2SJed Brown } 925214bc6a2SJed Brown } 926214bc6a2SJed Brown } else { 927e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 928e1244c69SJed Brown if (rhsjacobian) { 929bd373395SBarry Smith if (ijacobian) { 930214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 931bd373395SBarry Smith } else { 932bd373395SBarry Smith ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr); 933214bc6a2SJed Brown } 934d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 935e1244c69SJed Brown } 93694ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 937e1244c69SJed Brown ts->rhsjacobian.scale = -1; 938e1244c69SJed Brown ts->rhsjacobian.shift = shift; 93994ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 94094ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 94194ab13aaSBarry Smith if (A != B) { 94294ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 94394ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 944316643e7SJed Brown } 945e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 946e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 947e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 94894ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 94994ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 95094ab13aaSBarry Smith if (A != B) { 95194ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 95294ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 953214bc6a2SJed Brown } 954316643e7SJed Brown } 95594ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 95694ab13aaSBarry Smith if (A != B) { 95794ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 958316643e7SJed Brown } 959316643e7SJed Brown } 960316643e7SJed Brown } 96194ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 962316643e7SJed Brown PetscFunctionReturn(0); 963316643e7SJed Brown } 964316643e7SJed Brown 965316643e7SJed Brown #undef __FUNCT__ 9664a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 967d763cef2SBarry Smith /*@C 968d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 969b5abc632SBarry Smith where U_t = G(t,u). 970d763cef2SBarry Smith 9713f9fe445SBarry Smith Logically Collective on TS 972d763cef2SBarry Smith 973d763cef2SBarry Smith Input Parameters: 974d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 9750298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 976d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 977d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 9780298fd71SBarry Smith function evaluation routine (may be NULL) 979d763cef2SBarry Smith 980d763cef2SBarry Smith Calling sequence of func: 98187828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 982d763cef2SBarry Smith 983d763cef2SBarry Smith + t - current timestep 984d763cef2SBarry Smith . u - input vector 985d763cef2SBarry Smith . F - function vector 986d763cef2SBarry Smith - ctx - [optional] user-defined function context 987d763cef2SBarry Smith 988d763cef2SBarry Smith Level: beginner 989d763cef2SBarry Smith 9902bbac0d3SBarry Smith Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 9912bbac0d3SBarry Smith 992d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 993d763cef2SBarry Smith 994ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 995d763cef2SBarry Smith @*/ 996089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 997d763cef2SBarry Smith { 998089b2837SJed Brown PetscErrorCode ierr; 999089b2837SJed Brown SNES snes; 10000298fd71SBarry Smith Vec ralloc = NULL; 100124989b8cSPeter Brune DM dm; 1002d763cef2SBarry Smith 1003089b2837SJed Brown PetscFunctionBegin; 10040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1005ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 100624989b8cSPeter Brune 100724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 100824989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 1009089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1010e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 1011e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 1012e856ceecSJed Brown r = ralloc; 1013e856ceecSJed Brown } 1014089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 1015e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1016d763cef2SBarry Smith PetscFunctionReturn(0); 1017d763cef2SBarry Smith } 1018d763cef2SBarry Smith 10194a2ae208SSatish Balay #undef __FUNCT__ 1020ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 1021ef20d060SBarry Smith /*@C 1022abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1023ef20d060SBarry Smith 1024ef20d060SBarry Smith Logically Collective on TS 1025ef20d060SBarry Smith 1026ef20d060SBarry Smith Input Parameters: 1027ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1028ef20d060SBarry Smith . f - routine for evaluating the solution 1029ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 10300298fd71SBarry Smith function evaluation routine (may be NULL) 1031ef20d060SBarry Smith 1032ef20d060SBarry Smith Calling sequence of func: 1033ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 1034ef20d060SBarry Smith 1035ef20d060SBarry Smith + t - current timestep 1036ef20d060SBarry Smith . u - output vector 1037ef20d060SBarry Smith - ctx - [optional] user-defined function context 1038ef20d060SBarry Smith 1039abd5a294SJed Brown Notes: 1040abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1041abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1042abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1043abd5a294SJed Brown 10444f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1045abd5a294SJed Brown 1046ef20d060SBarry Smith Level: beginner 1047ef20d060SBarry Smith 1048ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1049ef20d060SBarry Smith 10509b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 1051ef20d060SBarry Smith @*/ 1052ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1053ef20d060SBarry Smith { 1054ef20d060SBarry Smith PetscErrorCode ierr; 1055ef20d060SBarry Smith DM dm; 1056ef20d060SBarry Smith 1057ef20d060SBarry Smith PetscFunctionBegin; 1058ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1059ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1060ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 1061ef20d060SBarry Smith PetscFunctionReturn(0); 1062ef20d060SBarry Smith } 1063ef20d060SBarry Smith 1064ef20d060SBarry Smith #undef __FUNCT__ 10659b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction" 10669b7cd975SBarry Smith /*@C 10679b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 10689b7cd975SBarry Smith 10699b7cd975SBarry Smith Logically Collective on TS 10709b7cd975SBarry Smith 10719b7cd975SBarry Smith Input Parameters: 10729b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 10739b7cd975SBarry Smith . f - routine for evaluating the forcing function 10749b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 10750298fd71SBarry Smith function evaluation routine (may be NULL) 10769b7cd975SBarry Smith 10779b7cd975SBarry Smith Calling sequence of func: 10789b7cd975SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 10799b7cd975SBarry Smith 10809b7cd975SBarry Smith + t - current timestep 10819b7cd975SBarry Smith . u - output vector 10829b7cd975SBarry Smith - ctx - [optional] user-defined function context 10839b7cd975SBarry Smith 10849b7cd975SBarry Smith Notes: 10859b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 10869b7cd975SBarry Smith create closed-form solutions with a non-physical forcing term. 10879b7cd975SBarry Smith 10889b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 10899b7cd975SBarry Smith 10909b7cd975SBarry Smith Level: beginner 10919b7cd975SBarry Smith 10929b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 10939b7cd975SBarry Smith 10949b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 10959b7cd975SBarry Smith @*/ 1096d56366bfSLisandro Dalcin PetscErrorCode TSSetForcingFunction(TS ts,TSForcingFunction f,void *ctx) 10979b7cd975SBarry Smith { 10989b7cd975SBarry Smith PetscErrorCode ierr; 10999b7cd975SBarry Smith DM dm; 11009b7cd975SBarry Smith 11019b7cd975SBarry Smith PetscFunctionBegin; 11029b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11039b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 11049b7cd975SBarry Smith ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr); 11059b7cd975SBarry Smith PetscFunctionReturn(0); 11069b7cd975SBarry Smith } 11079b7cd975SBarry Smith 11089b7cd975SBarry Smith #undef __FUNCT__ 11094a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 1110d763cef2SBarry Smith /*@C 1111f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1112b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1113d763cef2SBarry Smith 11143f9fe445SBarry Smith Logically Collective on TS 1115d763cef2SBarry Smith 1116d763cef2SBarry Smith Input Parameters: 1117d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1118e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1119e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1120d763cef2SBarry Smith . f - the Jacobian evaluation routine 1121d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 11220298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1123d763cef2SBarry Smith 1124f7ab8db6SBarry Smith Calling sequence of f: 1125ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1126d763cef2SBarry Smith 1127d763cef2SBarry Smith + t - current timestep 1128d763cef2SBarry Smith . u - input vector 1129e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1130e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1131d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1132d763cef2SBarry Smith 11336cd88445SBarry Smith Notes: 11346cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 11356cd88445SBarry Smith 11366cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1137ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1138d763cef2SBarry Smith 1139d763cef2SBarry Smith Level: beginner 1140d763cef2SBarry Smith 1141d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1142d763cef2SBarry Smith 1143ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1144d763cef2SBarry Smith 1145d763cef2SBarry Smith @*/ 1146e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1147d763cef2SBarry Smith { 1148277b19d0SLisandro Dalcin PetscErrorCode ierr; 1149089b2837SJed Brown SNES snes; 115024989b8cSPeter Brune DM dm; 115124989b8cSPeter Brune TSIJacobian ijacobian; 1152277b19d0SLisandro Dalcin 1153d763cef2SBarry Smith PetscFunctionBegin; 11540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1155e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1156e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1157e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1158e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1159d763cef2SBarry Smith 116024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 116124989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1162e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1163e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1164e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1165e1244c69SJed Brown } 11660298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1167089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11685f659677SPeter Brune if (!ijacobian) { 1169e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 11700e4ef248SJed Brown } 1171e5d3d808SBarry Smith if (Amat) { 1172e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 11730e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1174e5d3d808SBarry Smith ts->Arhs = Amat; 11750e4ef248SJed Brown } 1176e5d3d808SBarry Smith if (Pmat) { 1177e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 11780e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1179e5d3d808SBarry Smith ts->Brhs = Pmat; 11800e4ef248SJed Brown } 1181d763cef2SBarry Smith PetscFunctionReturn(0); 1182d763cef2SBarry Smith } 1183d763cef2SBarry Smith 1184316643e7SJed Brown 1185316643e7SJed Brown #undef __FUNCT__ 1186316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 1187316643e7SJed Brown /*@C 1188b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1189316643e7SJed Brown 11903f9fe445SBarry Smith Logically Collective on TS 1191316643e7SJed Brown 1192316643e7SJed Brown Input Parameters: 1193316643e7SJed Brown + ts - the TS context obtained from TSCreate() 11940298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1195316643e7SJed Brown . f - the function evaluation routine 11960298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1197316643e7SJed Brown 1198316643e7SJed Brown Calling sequence of f: 1199316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1200316643e7SJed Brown 1201316643e7SJed Brown + t - time at step/stage being solved 1202316643e7SJed Brown . u - state vector 1203316643e7SJed Brown . u_t - time derivative of state vector 1204316643e7SJed Brown . F - function vector 1205316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1206316643e7SJed Brown 1207316643e7SJed Brown Important: 12082bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1209316643e7SJed Brown 1210316643e7SJed Brown Level: beginner 1211316643e7SJed Brown 1212316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1213316643e7SJed Brown 1214d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1215316643e7SJed Brown @*/ 121651699248SLisandro Dalcin PetscErrorCode TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx) 1217316643e7SJed Brown { 1218089b2837SJed Brown PetscErrorCode ierr; 1219089b2837SJed Brown SNES snes; 122051699248SLisandro Dalcin Vec ralloc = NULL; 122124989b8cSPeter Brune DM dm; 1222316643e7SJed Brown 1223316643e7SJed Brown PetscFunctionBegin; 12240700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 122551699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 122624989b8cSPeter Brune 122724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 122824989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 122924989b8cSPeter Brune 1230089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 123151699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 123251699248SLisandro Dalcin ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 123351699248SLisandro Dalcin r = ralloc; 1234e856ceecSJed Brown } 123551699248SLisandro Dalcin ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 123651699248SLisandro Dalcin ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1237089b2837SJed Brown PetscFunctionReturn(0); 1238089b2837SJed Brown } 1239089b2837SJed Brown 1240089b2837SJed Brown #undef __FUNCT__ 1241089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 1242089b2837SJed Brown /*@C 1243089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1244089b2837SJed Brown 1245089b2837SJed Brown Not Collective 1246089b2837SJed Brown 1247089b2837SJed Brown Input Parameter: 1248089b2837SJed Brown . ts - the TS context 1249089b2837SJed Brown 1250089b2837SJed Brown Output Parameter: 12510298fd71SBarry Smith + r - vector to hold residual (or NULL) 12520298fd71SBarry Smith . func - the function to compute residual (or NULL) 12530298fd71SBarry Smith - ctx - the function context (or NULL) 1254089b2837SJed Brown 1255089b2837SJed Brown Level: advanced 1256089b2837SJed Brown 1257089b2837SJed Brown .keywords: TS, nonlinear, get, function 1258089b2837SJed Brown 1259089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1260089b2837SJed Brown @*/ 1261089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1262089b2837SJed Brown { 1263089b2837SJed Brown PetscErrorCode ierr; 1264089b2837SJed Brown SNES snes; 126524989b8cSPeter Brune DM dm; 1266089b2837SJed Brown 1267089b2837SJed Brown PetscFunctionBegin; 1268089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1269089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12700298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 127124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 127224989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1273089b2837SJed Brown PetscFunctionReturn(0); 1274089b2837SJed Brown } 1275089b2837SJed Brown 1276089b2837SJed Brown #undef __FUNCT__ 1277089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 1278089b2837SJed Brown /*@C 1279089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1280089b2837SJed Brown 1281089b2837SJed Brown Not Collective 1282089b2837SJed Brown 1283089b2837SJed Brown Input Parameter: 1284089b2837SJed Brown . ts - the TS context 1285089b2837SJed Brown 1286089b2837SJed Brown Output Parameter: 12870298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 12880298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 12890298fd71SBarry Smith - ctx - the function context (or NULL) 1290089b2837SJed Brown 1291089b2837SJed Brown Level: advanced 1292089b2837SJed Brown 1293089b2837SJed Brown .keywords: TS, nonlinear, get, function 1294089b2837SJed Brown 12952bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1296089b2837SJed Brown @*/ 1297089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1298089b2837SJed Brown { 1299089b2837SJed Brown PetscErrorCode ierr; 1300089b2837SJed Brown SNES snes; 130124989b8cSPeter Brune DM dm; 1302089b2837SJed Brown 1303089b2837SJed Brown PetscFunctionBegin; 1304089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1305089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13060298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 130724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 130824989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1309316643e7SJed Brown PetscFunctionReturn(0); 1310316643e7SJed Brown } 1311316643e7SJed Brown 1312316643e7SJed Brown #undef __FUNCT__ 1313316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 1314316643e7SJed Brown /*@C 1315a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1316ae8867d6SBarry Smith provided with TSSetIFunction(). 1317316643e7SJed Brown 13183f9fe445SBarry Smith Logically Collective on TS 1319316643e7SJed Brown 1320316643e7SJed Brown Input Parameters: 1321316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1322e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1323e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1324316643e7SJed Brown . f - the Jacobian evaluation routine 13250298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1326316643e7SJed Brown 1327316643e7SJed Brown Calling sequence of f: 1328ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1329316643e7SJed Brown 1330316643e7SJed Brown + t - time at step/stage being solved 13311b4a444bSJed Brown . U - state vector 13321b4a444bSJed Brown . U_t - time derivative of state vector 1333316643e7SJed Brown . a - shift 1334e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1335e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1336316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1337316643e7SJed Brown 1338316643e7SJed Brown Notes: 1339e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1340316643e7SJed Brown 1341895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1342895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1343895c21f2SBarry Smith 1344a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1345b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1346a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1347a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1348a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1349a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1350a4f0a591SBarry Smith 13516cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13526cd88445SBarry Smith 13536cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1354ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1355ca5f011dSBarry Smith 1356316643e7SJed Brown Level: beginner 1357316643e7SJed Brown 1358316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1359316643e7SJed Brown 1360ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1361316643e7SJed Brown 1362316643e7SJed Brown @*/ 1363e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1364316643e7SJed Brown { 1365316643e7SJed Brown PetscErrorCode ierr; 1366089b2837SJed Brown SNES snes; 136724989b8cSPeter Brune DM dm; 1368316643e7SJed Brown 1369316643e7SJed Brown PetscFunctionBegin; 13700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1371e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1372e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1373e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1374e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 137524989b8cSPeter Brune 137624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 137724989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 137824989b8cSPeter Brune 1379089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1380e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1381316643e7SJed Brown PetscFunctionReturn(0); 1382316643e7SJed Brown } 1383316643e7SJed Brown 13844a2ae208SSatish Balay #undef __FUNCT__ 1385e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse" 1386e1244c69SJed Brown /*@ 1387e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1388e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1389e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1390e1244c69SJed Brown not been changed by the TS. 1391e1244c69SJed Brown 1392e1244c69SJed Brown Logically Collective 1393e1244c69SJed Brown 1394e1244c69SJed Brown Input Arguments: 1395e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1396e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1397e1244c69SJed Brown 1398e1244c69SJed Brown Level: intermediate 1399e1244c69SJed Brown 1400e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1401e1244c69SJed Brown @*/ 1402e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1403e1244c69SJed Brown { 1404e1244c69SJed Brown PetscFunctionBegin; 1405e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1406e1244c69SJed Brown PetscFunctionReturn(0); 1407e1244c69SJed Brown } 1408e1244c69SJed Brown 1409e1244c69SJed Brown #undef __FUNCT__ 1410efe9872eSLisandro Dalcin #define __FUNCT__ "TSSetI2Function" 1411efe9872eSLisandro Dalcin /*@C 1412efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1413efe9872eSLisandro Dalcin 1414efe9872eSLisandro Dalcin Logically Collective on TS 1415efe9872eSLisandro Dalcin 1416efe9872eSLisandro Dalcin Input Parameters: 1417efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1418efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1419efe9872eSLisandro Dalcin . fun - the function evaluation routine 1420efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1421efe9872eSLisandro Dalcin 1422efe9872eSLisandro Dalcin Calling sequence of fun: 1423efe9872eSLisandro Dalcin $ fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1424efe9872eSLisandro Dalcin 1425efe9872eSLisandro Dalcin + t - time at step/stage being solved 1426efe9872eSLisandro Dalcin . U - state vector 1427efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1428efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1429efe9872eSLisandro Dalcin . F - function vector 1430efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1431efe9872eSLisandro Dalcin 1432efe9872eSLisandro Dalcin Level: beginner 1433efe9872eSLisandro Dalcin 1434efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function 1435efe9872eSLisandro Dalcin 1436efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1437efe9872eSLisandro Dalcin @*/ 1438efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx) 1439efe9872eSLisandro Dalcin { 1440efe9872eSLisandro Dalcin DM dm; 1441efe9872eSLisandro Dalcin PetscErrorCode ierr; 1442efe9872eSLisandro Dalcin 1443efe9872eSLisandro Dalcin PetscFunctionBegin; 1444efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1445efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2); 1446efe9872eSLisandro Dalcin ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr); 1447efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1448efe9872eSLisandro Dalcin ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1449efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1450efe9872eSLisandro Dalcin } 1451efe9872eSLisandro Dalcin 1452efe9872eSLisandro Dalcin #undef __FUNCT__ 1453efe9872eSLisandro Dalcin #define __FUNCT__ "TSGetI2Function" 1454efe9872eSLisandro Dalcin /*@C 1455efe9872eSLisandro Dalcin TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1456efe9872eSLisandro Dalcin 1457efe9872eSLisandro Dalcin Not Collective 1458efe9872eSLisandro Dalcin 1459efe9872eSLisandro Dalcin Input Parameter: 1460efe9872eSLisandro Dalcin . ts - the TS context 1461efe9872eSLisandro Dalcin 1462efe9872eSLisandro Dalcin Output Parameter: 1463efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1464efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1465efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1466efe9872eSLisandro Dalcin 1467efe9872eSLisandro Dalcin Level: advanced 1468efe9872eSLisandro Dalcin 1469efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function 1470efe9872eSLisandro Dalcin 1471efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction() 1472efe9872eSLisandro Dalcin @*/ 1473efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx) 1474efe9872eSLisandro Dalcin { 1475efe9872eSLisandro Dalcin PetscErrorCode ierr; 1476efe9872eSLisandro Dalcin SNES snes; 1477efe9872eSLisandro Dalcin DM dm; 1478efe9872eSLisandro Dalcin 1479efe9872eSLisandro Dalcin PetscFunctionBegin; 1480efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1481efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1482efe9872eSLisandro Dalcin ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 1483efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1484efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1485efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1486efe9872eSLisandro Dalcin } 1487efe9872eSLisandro Dalcin 1488efe9872eSLisandro Dalcin #undef __FUNCT__ 1489efe9872eSLisandro Dalcin #define __FUNCT__ "TSSetI2Jacobian" 1490efe9872eSLisandro Dalcin /*@C 1491*bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1492efe9872eSLisandro Dalcin where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function(). 1493efe9872eSLisandro Dalcin 1494efe9872eSLisandro Dalcin Logically Collective on TS 1495efe9872eSLisandro Dalcin 1496efe9872eSLisandro Dalcin Input Parameters: 1497efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1498efe9872eSLisandro Dalcin . J - Jacobian matrix 1499efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1500efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1501efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1502efe9872eSLisandro Dalcin 1503efe9872eSLisandro Dalcin Calling sequence of jac: 1504*bc77d74cSLisandro Dalcin $ jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx); 1505efe9872eSLisandro Dalcin 1506efe9872eSLisandro Dalcin + t - time at step/stage being solved 1507efe9872eSLisandro Dalcin . U - state vector 1508efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1509efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1510efe9872eSLisandro Dalcin . v - shift for U_t 1511efe9872eSLisandro Dalcin . a - shift for U_tt 1512efe9872eSLisandro 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 1513efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1514efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1515efe9872eSLisandro Dalcin 1516efe9872eSLisandro Dalcin Notes: 1517efe9872eSLisandro Dalcin The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve. 1518efe9872eSLisandro Dalcin 1519efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1520efe9872eSLisandro 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. 1521efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1522*bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1523efe9872eSLisandro Dalcin 1524efe9872eSLisandro Dalcin Level: beginner 1525efe9872eSLisandro Dalcin 1526efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian 1527efe9872eSLisandro Dalcin 1528efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1529efe9872eSLisandro Dalcin @*/ 1530efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx) 1531efe9872eSLisandro Dalcin { 1532efe9872eSLisandro Dalcin DM dm; 1533efe9872eSLisandro Dalcin PetscErrorCode ierr; 1534efe9872eSLisandro Dalcin 1535efe9872eSLisandro Dalcin PetscFunctionBegin; 1536efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1537efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2); 1538efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3); 1539efe9872eSLisandro Dalcin ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr); 1540efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1541efe9872eSLisandro Dalcin ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1542efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1543efe9872eSLisandro Dalcin } 1544efe9872eSLisandro Dalcin 1545efe9872eSLisandro Dalcin #undef __FUNCT__ 1546efe9872eSLisandro Dalcin #define __FUNCT__ "TSGetI2Jacobian" 1547efe9872eSLisandro Dalcin /*@C 1548efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1549efe9872eSLisandro Dalcin 1550efe9872eSLisandro Dalcin Not Collective, but parallel objects are returned if TS is parallel 1551efe9872eSLisandro Dalcin 1552efe9872eSLisandro Dalcin Input Parameter: 1553efe9872eSLisandro Dalcin . ts - The TS context obtained from TSCreate() 1554efe9872eSLisandro Dalcin 1555efe9872eSLisandro Dalcin Output Parameters: 1556efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1557efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1558efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1559efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1560efe9872eSLisandro Dalcin 1561efe9872eSLisandro Dalcin Notes: You can pass in NULL for any return argument you do not need. 1562efe9872eSLisandro Dalcin 1563efe9872eSLisandro Dalcin Level: advanced 1564efe9872eSLisandro Dalcin 1565efe9872eSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 1566efe9872eSLisandro Dalcin 1567efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian 1568efe9872eSLisandro Dalcin @*/ 1569efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx) 1570efe9872eSLisandro Dalcin { 1571efe9872eSLisandro Dalcin PetscErrorCode ierr; 1572efe9872eSLisandro Dalcin SNES snes; 1573efe9872eSLisandro Dalcin DM dm; 1574efe9872eSLisandro Dalcin 1575efe9872eSLisandro Dalcin PetscFunctionBegin; 1576efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1577efe9872eSLisandro Dalcin ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 1578efe9872eSLisandro Dalcin ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr); 1579efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1580efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1581efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1582efe9872eSLisandro Dalcin } 1583efe9872eSLisandro Dalcin 1584efe9872eSLisandro Dalcin #undef __FUNCT__ 1585efe9872eSLisandro Dalcin #define __FUNCT__ "TSComputeI2Function" 1586efe9872eSLisandro Dalcin /*@ 1587efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1588efe9872eSLisandro Dalcin 1589efe9872eSLisandro Dalcin Collective on TS and Vec 1590efe9872eSLisandro Dalcin 1591efe9872eSLisandro Dalcin Input Parameters: 1592efe9872eSLisandro Dalcin + ts - the TS context 1593efe9872eSLisandro Dalcin . t - current time 1594efe9872eSLisandro Dalcin . U - state vector 1595efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1596efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1597efe9872eSLisandro Dalcin 1598efe9872eSLisandro Dalcin Output Parameter: 1599efe9872eSLisandro Dalcin . F - the residual vector 1600efe9872eSLisandro Dalcin 1601efe9872eSLisandro Dalcin Note: 1602efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1603efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1604efe9872eSLisandro Dalcin 1605efe9872eSLisandro Dalcin Level: developer 1606efe9872eSLisandro Dalcin 1607efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector 1608efe9872eSLisandro Dalcin 1609efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1610efe9872eSLisandro Dalcin @*/ 1611efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F) 1612efe9872eSLisandro Dalcin { 1613efe9872eSLisandro Dalcin DM dm; 1614efe9872eSLisandro Dalcin TSI2Function I2Function; 1615efe9872eSLisandro Dalcin void *ctx; 1616efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1617efe9872eSLisandro Dalcin PetscErrorCode ierr; 1618efe9872eSLisandro Dalcin 1619efe9872eSLisandro Dalcin PetscFunctionBegin; 1620efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1621efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1622efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1623efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1624efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F,VEC_CLASSID,6); 1625efe9872eSLisandro Dalcin 1626efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1627efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr); 1628efe9872eSLisandro Dalcin ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 1629efe9872eSLisandro Dalcin 1630efe9872eSLisandro Dalcin if (!I2Function) { 1631efe9872eSLisandro Dalcin ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr); 1632efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1633efe9872eSLisandro Dalcin } 1634efe9872eSLisandro Dalcin 1635efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1636efe9872eSLisandro Dalcin 1637efe9872eSLisandro Dalcin PetscStackPush("TS user implicit function"); 1638efe9872eSLisandro Dalcin ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr); 1639efe9872eSLisandro Dalcin PetscStackPop; 1640efe9872eSLisandro Dalcin 1641efe9872eSLisandro Dalcin if (rhsfunction) { 1642efe9872eSLisandro Dalcin Vec Frhs; 1643efe9872eSLisandro Dalcin ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 1644efe9872eSLisandro Dalcin ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 1645efe9872eSLisandro Dalcin ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr); 1646efe9872eSLisandro Dalcin } 1647efe9872eSLisandro Dalcin 1648efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1649efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1650efe9872eSLisandro Dalcin } 1651efe9872eSLisandro Dalcin 1652efe9872eSLisandro Dalcin #undef __FUNCT__ 1653efe9872eSLisandro Dalcin #define __FUNCT__ "TSComputeI2Jacobian" 1654efe9872eSLisandro Dalcin /*@ 1655efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1656efe9872eSLisandro Dalcin 1657efe9872eSLisandro Dalcin Collective on TS and Vec 1658efe9872eSLisandro Dalcin 1659efe9872eSLisandro Dalcin Input Parameters: 1660efe9872eSLisandro Dalcin + ts - the TS context 1661efe9872eSLisandro Dalcin . t - current timestep 1662efe9872eSLisandro Dalcin . U - state vector 1663efe9872eSLisandro Dalcin . V - time derivative of state vector 1664efe9872eSLisandro Dalcin . A - second time derivative of state vector 1665efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1666efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1667efe9872eSLisandro Dalcin 1668efe9872eSLisandro Dalcin Output Parameters: 1669efe9872eSLisandro Dalcin + J - Jacobian matrix 1670efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1671efe9872eSLisandro Dalcin 1672efe9872eSLisandro Dalcin Notes: 1673efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1674efe9872eSLisandro Dalcin 1675efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1676efe9872eSLisandro Dalcin 1677efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1678efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1679efe9872eSLisandro Dalcin 1680efe9872eSLisandro Dalcin Level: developer 1681efe9872eSLisandro Dalcin 1682efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix 1683efe9872eSLisandro Dalcin 1684efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1685efe9872eSLisandro Dalcin @*/ 1686efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P) 1687efe9872eSLisandro Dalcin { 1688efe9872eSLisandro Dalcin DM dm; 1689efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1690efe9872eSLisandro Dalcin void *ctx; 1691efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1692efe9872eSLisandro Dalcin PetscErrorCode ierr; 1693efe9872eSLisandro Dalcin 1694efe9872eSLisandro Dalcin PetscFunctionBegin; 1695efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1696efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1697efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1698efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1699efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J,MAT_CLASSID,8); 1700efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P,MAT_CLASSID,9); 1701efe9872eSLisandro Dalcin 1702efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1703efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr); 1704efe9872eSLisandro Dalcin ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 1705efe9872eSLisandro Dalcin 1706efe9872eSLisandro Dalcin if (!I2Jacobian) { 1707efe9872eSLisandro Dalcin ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr); 1708efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1709efe9872eSLisandro Dalcin } 1710efe9872eSLisandro Dalcin 1711efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1712efe9872eSLisandro Dalcin 1713efe9872eSLisandro Dalcin PetscStackPush("TS user implicit Jacobian"); 1714efe9872eSLisandro Dalcin ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr); 1715efe9872eSLisandro Dalcin PetscStackPop; 1716efe9872eSLisandro Dalcin 1717efe9872eSLisandro Dalcin if (rhsjacobian) { 1718efe9872eSLisandro Dalcin Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 1719efe9872eSLisandro Dalcin ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr); 1720efe9872eSLisandro Dalcin ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr); 1721efe9872eSLisandro Dalcin ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr); 1722efe9872eSLisandro Dalcin if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);} 1723efe9872eSLisandro Dalcin } 1724efe9872eSLisandro Dalcin 1725efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1726efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1727efe9872eSLisandro Dalcin } 1728efe9872eSLisandro Dalcin 1729efe9872eSLisandro Dalcin #undef __FUNCT__ 1730efe9872eSLisandro Dalcin #define __FUNCT__ "TS2SetSolution" 1731efe9872eSLisandro Dalcin /*@ 1732efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1733efe9872eSLisandro Dalcin for use by the TS routines handling second order equations. 1734efe9872eSLisandro Dalcin 1735efe9872eSLisandro Dalcin Logically Collective on TS and Vec 1736efe9872eSLisandro Dalcin 1737efe9872eSLisandro Dalcin Input Parameters: 1738efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1739efe9872eSLisandro Dalcin . u - the solution vector 1740efe9872eSLisandro Dalcin - v - the time derivative vector 1741efe9872eSLisandro Dalcin 1742efe9872eSLisandro Dalcin Level: beginner 1743efe9872eSLisandro Dalcin 1744efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions 1745efe9872eSLisandro Dalcin @*/ 1746efe9872eSLisandro Dalcin PetscErrorCode TS2SetSolution(TS ts,Vec u,Vec v) 1747efe9872eSLisandro Dalcin { 1748efe9872eSLisandro Dalcin PetscErrorCode ierr; 1749efe9872eSLisandro Dalcin 1750efe9872eSLisandro Dalcin PetscFunctionBegin; 1751efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1752efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1753efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v,VEC_CLASSID,3); 1754efe9872eSLisandro Dalcin ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 1755efe9872eSLisandro Dalcin ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr); 1756efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 1757efe9872eSLisandro Dalcin ts->vec_dot = v; 1758efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1759efe9872eSLisandro Dalcin } 1760efe9872eSLisandro Dalcin 1761efe9872eSLisandro Dalcin #undef __FUNCT__ 1762efe9872eSLisandro Dalcin #define __FUNCT__ "TS2GetSolution" 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 1795efe9872eSLisandro Dalcin #undef __FUNCT__ 179655849f57SBarry Smith #define __FUNCT__ "TSLoad" 179755849f57SBarry Smith /*@C 179855849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 179955849f57SBarry Smith 180055849f57SBarry Smith Collective on PetscViewer 180155849f57SBarry Smith 180255849f57SBarry Smith Input Parameters: 180355849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 180455849f57SBarry Smith some related function before a call to TSLoad(). 180555849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 180655849f57SBarry Smith 180755849f57SBarry Smith Level: intermediate 180855849f57SBarry Smith 180955849f57SBarry Smith Notes: 181055849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 181155849f57SBarry Smith 181255849f57SBarry Smith Notes for advanced users: 181355849f57SBarry Smith Most users should not need to know the details of the binary storage 181455849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 181555849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 181655849f57SBarry Smith format is 181755849f57SBarry Smith .vb 181855849f57SBarry Smith has not yet been determined 181955849f57SBarry Smith .ve 182055849f57SBarry Smith 182155849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 182255849f57SBarry Smith @*/ 1823f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 182455849f57SBarry Smith { 182555849f57SBarry Smith PetscErrorCode ierr; 182655849f57SBarry Smith PetscBool isbinary; 182755849f57SBarry Smith PetscInt classid; 182855849f57SBarry Smith char type[256]; 18292d53ad75SBarry Smith DMTS sdm; 1830ad6bc421SBarry Smith DM dm; 183155849f57SBarry Smith 183255849f57SBarry Smith PetscFunctionBegin; 1833f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 183455849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 183555849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 183655849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 183755849f57SBarry Smith 1838060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1839ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1840060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1841f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1842f2c2a1b9SBarry Smith if (ts->ops->load) { 1843f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1844f2c2a1b9SBarry Smith } 1845ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1846ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1847ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1848f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1849f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 18502d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 18512d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 185255849f57SBarry Smith PetscFunctionReturn(0); 185355849f57SBarry Smith } 185455849f57SBarry Smith 18559804daf3SBarry Smith #include <petscdraw.h> 1856e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1857e04113cfSBarry Smith #include <petscviewersaws.h> 1858f05ece33SBarry Smith #endif 185955849f57SBarry Smith #undef __FUNCT__ 18604a2ae208SSatish Balay #define __FUNCT__ "TSView" 18617e2c5f70SBarry Smith /*@C 1862d763cef2SBarry Smith TSView - Prints the TS data structure. 1863d763cef2SBarry Smith 18644c49b128SBarry Smith Collective on TS 1865d763cef2SBarry Smith 1866d763cef2SBarry Smith Input Parameters: 1867d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1868d763cef2SBarry Smith - viewer - visualization context 1869d763cef2SBarry Smith 1870d763cef2SBarry Smith Options Database Key: 1871d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1872d763cef2SBarry Smith 1873d763cef2SBarry Smith Notes: 1874d763cef2SBarry Smith The available visualization contexts include 1875b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1876b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1877d763cef2SBarry Smith output where only the first processor opens 1878d763cef2SBarry Smith the file. All other processors send their 1879d763cef2SBarry Smith data to the first processor to print. 1880d763cef2SBarry Smith 1881d763cef2SBarry Smith The user can open an alternative visualization context with 1882b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1883d763cef2SBarry Smith 1884d763cef2SBarry Smith Level: beginner 1885d763cef2SBarry Smith 1886d763cef2SBarry Smith .keywords: TS, timestep, view 1887d763cef2SBarry Smith 1888b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1889d763cef2SBarry Smith @*/ 18907087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1891d763cef2SBarry Smith { 1892dfbe8321SBarry Smith PetscErrorCode ierr; 189319fd82e9SBarry Smith TSType type; 18942b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 18952d53ad75SBarry Smith DMTS sdm; 1896e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1897536b137fSBarry Smith PetscBool issaws; 1898f05ece33SBarry Smith #endif 1899d763cef2SBarry Smith 1900d763cef2SBarry Smith PetscFunctionBegin; 19010700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 19023050cee2SBarry Smith if (!viewer) { 1903ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 19043050cee2SBarry Smith } 19050700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1906c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1907fd16b177SBarry Smith 1908251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1909251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 191055849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 19112b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1912e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1913536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1914f05ece33SBarry Smith #endif 191532077d6dSBarry Smith if (iascii) { 1916dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 191777431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 19187c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1919d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 19205ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1921c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1922d763cef2SBarry Smith } 19235ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1924193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 19252d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19262d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1927d52bd9f3SBarry Smith if (ts->ops->view) { 1928d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1929d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1930d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1931d52bd9f3SBarry Smith } 19320f5bd95cSBarry Smith } else if (isstring) { 1933a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1934b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 193555849f57SBarry Smith } else if (isbinary) { 193655849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 193755849f57SBarry Smith MPI_Comm comm; 193855849f57SBarry Smith PetscMPIInt rank; 193955849f57SBarry Smith char type[256]; 194055849f57SBarry Smith 194155849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 194255849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 194355849f57SBarry Smith if (!rank) { 194455849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 194555849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 194655849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 194755849f57SBarry Smith } 194855849f57SBarry Smith if (ts->ops->view) { 194955849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 195055849f57SBarry Smith } 1951f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1952f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 19532d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19542d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 19552b0a91c0SBarry Smith } else if (isdraw) { 19562b0a91c0SBarry Smith PetscDraw draw; 19572b0a91c0SBarry Smith char str[36]; 195889fd9fafSBarry Smith PetscReal x,y,bottom,h; 19592b0a91c0SBarry Smith 19602b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 19612b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 19622b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 19632b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 196451fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 196589fd9fafSBarry Smith bottom = y - h; 19662b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 19672b0a91c0SBarry Smith if (ts->ops->view) { 19682b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 19692b0a91c0SBarry Smith } 19702b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1971e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1972536b137fSBarry Smith } else if (issaws) { 1973d45a07a7SBarry Smith PetscMPIInt rank; 19742657e9d9SBarry Smith const char *name; 19752657e9d9SBarry Smith 19762657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1977d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1978d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1979d45a07a7SBarry Smith char dir[1024]; 1980d45a07a7SBarry Smith 1981e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1982a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 19832657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 19842657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 19852657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1986d763cef2SBarry Smith } 19870acecf5bSBarry Smith if (ts->ops->view) { 19880acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 19890acecf5bSBarry Smith } 1990f05ece33SBarry Smith #endif 1991f05ece33SBarry Smith } 1992f05ece33SBarry Smith 1993b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1994251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1995b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1996d763cef2SBarry Smith PetscFunctionReturn(0); 1997d763cef2SBarry Smith } 1998d763cef2SBarry Smith 1999d763cef2SBarry Smith 20004a2ae208SSatish Balay #undef __FUNCT__ 20014a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 2002b07ff414SBarry Smith /*@ 2003d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2004d763cef2SBarry Smith the timesteppers. 2005d763cef2SBarry Smith 20063f9fe445SBarry Smith Logically Collective on TS 2007d763cef2SBarry Smith 2008d763cef2SBarry Smith Input Parameters: 2009d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2010d763cef2SBarry Smith - usrP - optional user context 2011d763cef2SBarry Smith 2012daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 2013daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2014daf670e6SBarry Smith 2015d763cef2SBarry Smith Level: intermediate 2016d763cef2SBarry Smith 2017d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 2018d763cef2SBarry Smith 2019d763cef2SBarry Smith .seealso: TSGetApplicationContext() 2020d763cef2SBarry Smith @*/ 20217087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 2022d763cef2SBarry Smith { 2023d763cef2SBarry Smith PetscFunctionBegin; 20240700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2025d763cef2SBarry Smith ts->user = usrP; 2026d763cef2SBarry Smith PetscFunctionReturn(0); 2027d763cef2SBarry Smith } 2028d763cef2SBarry Smith 20294a2ae208SSatish Balay #undef __FUNCT__ 20304a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 2031b07ff414SBarry Smith /*@ 2032d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2033d763cef2SBarry Smith timestepper. 2034d763cef2SBarry Smith 2035d763cef2SBarry Smith Not Collective 2036d763cef2SBarry Smith 2037d763cef2SBarry Smith Input Parameter: 2038d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2039d763cef2SBarry Smith 2040d763cef2SBarry Smith Output Parameter: 2041d763cef2SBarry Smith . usrP - user context 2042d763cef2SBarry Smith 2043daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 2044daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2045daf670e6SBarry Smith 2046d763cef2SBarry Smith Level: intermediate 2047d763cef2SBarry Smith 2048d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 2049d763cef2SBarry Smith 2050d763cef2SBarry Smith .seealso: TSSetApplicationContext() 2051d763cef2SBarry Smith @*/ 2052e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 2053d763cef2SBarry Smith { 2054d763cef2SBarry Smith PetscFunctionBegin; 20550700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2056e71120c6SJed Brown *(void**)usrP = ts->user; 2057d763cef2SBarry Smith PetscFunctionReturn(0); 2058d763cef2SBarry Smith } 2059d763cef2SBarry Smith 20604a2ae208SSatish Balay #undef __FUNCT__ 20614a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 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 20874a2ae208SSatish Balay #undef __FUNCT__ 20884a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 2089d763cef2SBarry Smith /*@ 2090d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 2091d763cef2SBarry Smith as well as the initial time. 2092d763cef2SBarry Smith 20933f9fe445SBarry Smith Logically Collective on TS 2094d763cef2SBarry Smith 2095d763cef2SBarry Smith Input Parameters: 2096d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2097d763cef2SBarry Smith . initial_time - the initial time 2098d763cef2SBarry Smith - time_step - the size of the timestep 2099d763cef2SBarry Smith 2100d763cef2SBarry Smith Level: intermediate 2101d763cef2SBarry Smith 2102d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 2103d763cef2SBarry Smith 2104d763cef2SBarry Smith .keywords: TS, set, initial, timestep 2105d763cef2SBarry Smith @*/ 21067087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 2107d763cef2SBarry Smith { 2108e144a568SJed Brown PetscErrorCode ierr; 2109e144a568SJed Brown 2110d763cef2SBarry Smith PetscFunctionBegin; 21110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2112e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 2113d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 2114d763cef2SBarry Smith PetscFunctionReturn(0); 2115d763cef2SBarry Smith } 2116d763cef2SBarry Smith 21174a2ae208SSatish Balay #undef __FUNCT__ 21184a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 2119d763cef2SBarry Smith /*@ 2120d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2121d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2122d763cef2SBarry Smith 21233f9fe445SBarry Smith Logically Collective on TS 2124d763cef2SBarry Smith 2125d763cef2SBarry Smith Input Parameters: 2126d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2127d763cef2SBarry Smith - time_step - the size of the timestep 2128d763cef2SBarry Smith 2129d763cef2SBarry Smith Level: intermediate 2130d763cef2SBarry Smith 2131d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2132d763cef2SBarry Smith 2133d763cef2SBarry Smith .keywords: TS, set, timestep 2134d763cef2SBarry Smith @*/ 21357087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 2136d763cef2SBarry Smith { 2137d763cef2SBarry Smith PetscFunctionBegin; 21380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2139c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 2140d763cef2SBarry Smith ts->time_step = time_step; 2141d763cef2SBarry Smith PetscFunctionReturn(0); 2142d763cef2SBarry Smith } 2143d763cef2SBarry Smith 21444a2ae208SSatish Balay #undef __FUNCT__ 2145a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 2146a43b19c4SJed Brown /*@ 214749354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 214849354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 214949354f04SShri Abhyankar or just return at the final time TS computed. 2150a43b19c4SJed Brown 2151a43b19c4SJed Brown Logically Collective on TS 2152a43b19c4SJed Brown 2153a43b19c4SJed Brown Input Parameter: 2154a43b19c4SJed Brown + ts - the time-step context 215549354f04SShri Abhyankar - eftopt - exact final time option 2156a43b19c4SJed Brown 2157feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2158feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2159feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2160feed9e9dSBarry Smith 2161feed9e9dSBarry Smith Options Database: 2162feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2163feed9e9dSBarry Smith 2164ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 2165ee346746SBarry Smith then the final time you selected. 2166ee346746SBarry Smith 2167a43b19c4SJed Brown Level: beginner 2168a43b19c4SJed Brown 2169a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 2170a43b19c4SJed Brown @*/ 217149354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 2172a43b19c4SJed Brown { 2173a43b19c4SJed Brown PetscFunctionBegin; 2174a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 217549354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 217649354f04SShri Abhyankar ts->exact_final_time = eftopt; 2177a43b19c4SJed Brown PetscFunctionReturn(0); 2178a43b19c4SJed Brown } 2179a43b19c4SJed Brown 2180a43b19c4SJed Brown #undef __FUNCT__ 21814a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 2182d763cef2SBarry Smith /*@ 2183d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2184d763cef2SBarry Smith 2185d763cef2SBarry Smith Not Collective 2186d763cef2SBarry Smith 2187d763cef2SBarry Smith Input Parameter: 2188d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2189d763cef2SBarry Smith 2190d763cef2SBarry Smith Output Parameter: 2191d763cef2SBarry Smith . dt - the current timestep size 2192d763cef2SBarry Smith 2193d763cef2SBarry Smith Level: intermediate 2194d763cef2SBarry Smith 2195d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2196d763cef2SBarry Smith 2197d763cef2SBarry Smith .keywords: TS, get, timestep 2198d763cef2SBarry Smith @*/ 21997087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 2200d763cef2SBarry Smith { 2201d763cef2SBarry Smith PetscFunctionBegin; 22020700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2203f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 2204d763cef2SBarry Smith *dt = ts->time_step; 2205d763cef2SBarry Smith PetscFunctionReturn(0); 2206d763cef2SBarry Smith } 2207d763cef2SBarry Smith 22084a2ae208SSatish Balay #undef __FUNCT__ 22094a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 2210d8e5e3e6SSatish Balay /*@ 2211d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2212d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2213d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2214d763cef2SBarry Smith the solution at the next timestep has been calculated. 2215d763cef2SBarry Smith 2216d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 2217d763cef2SBarry Smith 2218d763cef2SBarry Smith Input Parameter: 2219d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2220d763cef2SBarry Smith 2221d763cef2SBarry Smith Output Parameter: 2222d763cef2SBarry Smith . v - the vector containing the solution 2223d763cef2SBarry Smith 222463e21af5SBarry Smith Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested 222563e21af5SBarry Smith final time. It returns the solution at the next timestep. 222663e21af5SBarry Smith 2227d763cef2SBarry Smith Level: intermediate 2228d763cef2SBarry Smith 222963e21af5SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime() 2230d763cef2SBarry Smith 2231d763cef2SBarry Smith .keywords: TS, timestep, get, solution 2232d763cef2SBarry Smith @*/ 22337087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 2234d763cef2SBarry Smith { 2235d763cef2SBarry Smith PetscFunctionBegin; 22360700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22374482741eSBarry Smith PetscValidPointer(v,2); 22388737fe31SLisandro Dalcin *v = ts->vec_sol; 2239d763cef2SBarry Smith PetscFunctionReturn(0); 2240d763cef2SBarry Smith } 2241d763cef2SBarry Smith 2242c235aa8dSHong Zhang #undef __FUNCT__ 2243dfb21088SHong Zhang #define __FUNCT__ "TSGetCostGradients" 2244c235aa8dSHong Zhang /*@ 2245dfb21088SHong Zhang TSGetCostGradients - Returns the gradients from the TSAdjointSolve() 2246c235aa8dSHong Zhang 2247c235aa8dSHong Zhang Not Collective, but Vec returned is parallel if TS is parallel 2248c235aa8dSHong Zhang 2249c235aa8dSHong Zhang Input Parameter: 2250c235aa8dSHong Zhang . ts - the TS context obtained from TSCreate() 2251c235aa8dSHong Zhang 2252c235aa8dSHong Zhang Output Parameter: 2253abc2977eSBarry Smith + lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 2254abc2977eSBarry Smith - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 2255c235aa8dSHong Zhang 2256c235aa8dSHong Zhang Level: intermediate 2257c235aa8dSHong Zhang 2258c235aa8dSHong Zhang .seealso: TSGetTimeStep() 2259c235aa8dSHong Zhang 2260c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity 2261c235aa8dSHong Zhang @*/ 2262dfb21088SHong Zhang PetscErrorCode TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu) 2263c235aa8dSHong Zhang { 2264c235aa8dSHong Zhang PetscFunctionBegin; 2265c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2266abc2977eSBarry Smith if (numcost) *numcost = ts->numcost; 2267abc2977eSBarry Smith if (lambda) *lambda = ts->vecs_sensi; 2268abc2977eSBarry Smith if (mu) *mu = ts->vecs_sensip; 2269c235aa8dSHong Zhang PetscFunctionReturn(0); 2270c235aa8dSHong Zhang } 2271c235aa8dSHong Zhang 2272bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 22734a2ae208SSatish Balay #undef __FUNCT__ 2274bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 2275d8e5e3e6SSatish Balay /*@ 2276bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2277d763cef2SBarry Smith 2278bdad233fSMatthew Knepley Not collective 2279d763cef2SBarry Smith 2280bdad233fSMatthew Knepley Input Parameters: 2281bdad233fSMatthew Knepley + ts - The TS 2282bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2283d763cef2SBarry Smith .vb 22840910c330SBarry Smith U_t - A U = 0 (linear) 22850910c330SBarry Smith U_t - A(t) U = 0 (linear) 22860910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2287d763cef2SBarry Smith .ve 2288d763cef2SBarry Smith 2289d763cef2SBarry Smith Level: beginner 2290d763cef2SBarry Smith 2291bdad233fSMatthew Knepley .keywords: TS, problem type 2292bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2293d763cef2SBarry Smith @*/ 22947087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2295a7cc72afSBarry Smith { 22969e2a6581SJed Brown PetscErrorCode ierr; 22979e2a6581SJed Brown 2298d763cef2SBarry Smith PetscFunctionBegin; 22990700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2300bdad233fSMatthew Knepley ts->problem_type = type; 23019e2a6581SJed Brown if (type == TS_LINEAR) { 23029e2a6581SJed Brown SNES snes; 23039e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 23049e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 23059e2a6581SJed Brown } 2306d763cef2SBarry Smith PetscFunctionReturn(0); 2307d763cef2SBarry Smith } 2308d763cef2SBarry Smith 2309bdad233fSMatthew Knepley #undef __FUNCT__ 2310bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 2311bdad233fSMatthew Knepley /*@C 2312bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2313bdad233fSMatthew Knepley 2314bdad233fSMatthew Knepley Not collective 2315bdad233fSMatthew Knepley 2316bdad233fSMatthew Knepley Input Parameter: 2317bdad233fSMatthew Knepley . ts - The TS 2318bdad233fSMatthew Knepley 2319bdad233fSMatthew Knepley Output Parameter: 2320bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2321bdad233fSMatthew Knepley .vb 2322089b2837SJed Brown M U_t = A U 2323089b2837SJed Brown M(t) U_t = A(t) U 2324b5abc632SBarry Smith F(t,U,U_t) 2325bdad233fSMatthew Knepley .ve 2326bdad233fSMatthew Knepley 2327bdad233fSMatthew Knepley Level: beginner 2328bdad233fSMatthew Knepley 2329bdad233fSMatthew Knepley .keywords: TS, problem type 2330bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2331bdad233fSMatthew Knepley @*/ 23327087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2333a7cc72afSBarry Smith { 2334bdad233fSMatthew Knepley PetscFunctionBegin; 23350700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 23364482741eSBarry Smith PetscValidIntPointer(type,2); 2337bdad233fSMatthew Knepley *type = ts->problem_type; 2338bdad233fSMatthew Knepley PetscFunctionReturn(0); 2339bdad233fSMatthew Knepley } 2340d763cef2SBarry Smith 23414a2ae208SSatish Balay #undef __FUNCT__ 23424a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 2343d763cef2SBarry Smith /*@ 2344d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 2345d763cef2SBarry Smith of a timestepper. 2346d763cef2SBarry Smith 2347d763cef2SBarry Smith Collective on TS 2348d763cef2SBarry Smith 2349d763cef2SBarry Smith Input Parameter: 2350d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2351d763cef2SBarry Smith 2352d763cef2SBarry Smith Notes: 2353d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 2354d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 2355d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 2356d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 2357d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 2358d763cef2SBarry Smith 2359d763cef2SBarry Smith Level: advanced 2360d763cef2SBarry Smith 2361d763cef2SBarry Smith .keywords: TS, timestep, setup 2362d763cef2SBarry Smith 2363d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 2364d763cef2SBarry Smith @*/ 23657087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 2366d763cef2SBarry Smith { 2367dfbe8321SBarry Smith PetscErrorCode ierr; 23686c6b9e74SPeter Brune DM dm; 23696c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 2370d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 2371cd11d68dSLisandro Dalcin TSIFunction ifun; 23726c6b9e74SPeter Brune TSIJacobian ijac; 2373efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 23746c6b9e74SPeter Brune TSRHSJacobian rhsjac; 2375d763cef2SBarry Smith 2376d763cef2SBarry Smith PetscFunctionBegin; 23770700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2378277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 2379277b19d0SLisandro Dalcin 23802c18e0fdSBarry Smith ts->total_steps = 0; 23817adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 2382cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 2383cd11d68dSLisandro Dalcin ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr); 2384d763cef2SBarry Smith } 2385277b19d0SLisandro Dalcin 2386277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 2387277b19d0SLisandro Dalcin 2388e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 2389e1244c69SJed Brown Mat Amat,Pmat; 2390e1244c69SJed Brown SNES snes; 2391e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2392e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 2393e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2394e1244c69SJed Brown * have displaced the RHS matrix */ 2395e1244c69SJed Brown if (Amat == ts->Arhs) { 2396e1244c69SJed Brown ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 2397e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 2398e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 2399e1244c69SJed Brown } 2400e1244c69SJed Brown if (Pmat == ts->Brhs) { 2401e1244c69SJed Brown ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 2402e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 2403e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 2404e1244c69SJed Brown } 2405e1244c69SJed Brown } 2406277b19d0SLisandro Dalcin if (ts->ops->setup) { 2407000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 2408277b19d0SLisandro Dalcin } 2409277b19d0SLisandro Dalcin 2410a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 24116c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 24126c6b9e74SPeter Brune */ 24136c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 24140298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 24156c6b9e74SPeter Brune if (!func) { 24166c6b9e74SPeter Brune ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 24176c6b9e74SPeter Brune } 2418a6772fa2SLisandro 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. 24196c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 24206c6b9e74SPeter Brune */ 24210298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 24220298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 2423efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr); 24240298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 2425efe9872eSLisandro Dalcin if (!jac && (ijac || i2jac || rhsjac)) { 24266c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 24276c6b9e74SPeter Brune } 2428277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2429277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2430277b19d0SLisandro Dalcin } 2431277b19d0SLisandro Dalcin 2432277b19d0SLisandro Dalcin #undef __FUNCT__ 2433f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp" 2434f6a906c0SBarry Smith /*@ 2435f6a906c0SBarry Smith TSAdjointSetUp - Sets up the internal data structures for the later use 2436f6a906c0SBarry Smith of an adjoint solver 2437f6a906c0SBarry Smith 2438f6a906c0SBarry Smith Collective on TS 2439f6a906c0SBarry Smith 2440f6a906c0SBarry Smith Input Parameter: 2441f6a906c0SBarry Smith . ts - the TS context obtained from TSCreate() 2442f6a906c0SBarry Smith 2443f6a906c0SBarry Smith Level: advanced 2444f6a906c0SBarry Smith 2445f6a906c0SBarry Smith .keywords: TS, timestep, setup 2446f6a906c0SBarry Smith 2447947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients() 2448f6a906c0SBarry Smith @*/ 2449f6a906c0SBarry Smith PetscErrorCode TSAdjointSetUp(TS ts) 2450f6a906c0SBarry Smith { 2451f6a906c0SBarry Smith PetscErrorCode ierr; 2452f6a906c0SBarry Smith 2453f6a906c0SBarry Smith PetscFunctionBegin; 2454f6a906c0SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2455f6a906c0SBarry Smith if (ts->adjointsetupcalled) PetscFunctionReturn(0); 2456dfb21088SHong Zhang if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first"); 2457d8151eeaSBarry Smith 2458947abb85SHong Zhang if (ts->vec_costintegral) { /* if there is integral in the cost function*/ 2459d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2460d8151eeaSBarry Smith if (ts->vecs_sensip){ 2461d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2462d8151eeaSBarry Smith } 2463947abb85SHong Zhang } 2464d8151eeaSBarry Smith 246542f2b339SBarry Smith if (ts->ops->adjointsetup) { 246642f2b339SBarry Smith ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr); 2467f6a906c0SBarry Smith } 2468f6a906c0SBarry Smith ts->adjointsetupcalled = PETSC_TRUE; 2469f6a906c0SBarry Smith PetscFunctionReturn(0); 2470f6a906c0SBarry Smith } 2471f6a906c0SBarry Smith 2472f6a906c0SBarry Smith #undef __FUNCT__ 2473277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 2474277b19d0SLisandro Dalcin /*@ 2475277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2476277b19d0SLisandro Dalcin 2477277b19d0SLisandro Dalcin Collective on TS 2478277b19d0SLisandro Dalcin 2479277b19d0SLisandro Dalcin Input Parameter: 2480277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2481277b19d0SLisandro Dalcin 2482277b19d0SLisandro Dalcin Level: beginner 2483277b19d0SLisandro Dalcin 2484277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 2485277b19d0SLisandro Dalcin 2486277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2487277b19d0SLisandro Dalcin @*/ 2488277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2489277b19d0SLisandro Dalcin { 2490277b19d0SLisandro Dalcin PetscErrorCode ierr; 2491277b19d0SLisandro Dalcin 2492277b19d0SLisandro Dalcin PetscFunctionBegin; 2493277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2494b18ea86cSHong Zhang 2495277b19d0SLisandro Dalcin if (ts->ops->reset) { 2496277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2497277b19d0SLisandro Dalcin } 2498277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2499e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2500bbd56ea5SKarl Rupp 25014e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 25024e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2503214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 25046bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2505efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 2506e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2507e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 250838637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2509bbd56ea5SKarl Rupp 2510947abb85SHong Zhang if (ts->vec_costintegral) { 2511d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2512d8151eeaSBarry Smith if (ts->vecs_drdp){ 2513d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2514d8151eeaSBarry Smith } 2515947abb85SHong Zhang } 2516d8151eeaSBarry Smith ts->vecs_sensi = NULL; 2517d8151eeaSBarry Smith ts->vecs_sensip = NULL; 2518ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 25192c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 252036eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2521277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2522d763cef2SBarry Smith PetscFunctionReturn(0); 2523d763cef2SBarry Smith } 2524d763cef2SBarry Smith 25254a2ae208SSatish Balay #undef __FUNCT__ 25264a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 2527d8e5e3e6SSatish Balay /*@ 2528d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2529d763cef2SBarry Smith with TSCreate(). 2530d763cef2SBarry Smith 2531d763cef2SBarry Smith Collective on TS 2532d763cef2SBarry Smith 2533d763cef2SBarry Smith Input Parameter: 2534d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2535d763cef2SBarry Smith 2536d763cef2SBarry Smith Level: beginner 2537d763cef2SBarry Smith 2538d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2539d763cef2SBarry Smith 2540d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2541d763cef2SBarry Smith @*/ 25426bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2543d763cef2SBarry Smith { 25446849ba73SBarry Smith PetscErrorCode ierr; 2545d763cef2SBarry Smith 2546d763cef2SBarry Smith PetscFunctionBegin; 25476bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 25486bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 25496bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2550d763cef2SBarry Smith 25516bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2552277b19d0SLisandro Dalcin 2553e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2554e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 25556bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 25566d4c513bSLisandro Dalcin 2557bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2558bc952696SBarry Smith 255984df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 25606427ac75SLisandro Dalcin ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr); 25616427ac75SLisandro Dalcin 25626bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 25636bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 25646bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 25650dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 25666d4c513bSLisandro Dalcin 2567a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2568d763cef2SBarry Smith PetscFunctionReturn(0); 2569d763cef2SBarry Smith } 2570d763cef2SBarry Smith 25714a2ae208SSatish Balay #undef __FUNCT__ 25724a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 2573d8e5e3e6SSatish Balay /*@ 2574d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2575d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2576d763cef2SBarry Smith 2577d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2578d763cef2SBarry Smith 2579d763cef2SBarry Smith Input Parameter: 2580d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2581d763cef2SBarry Smith 2582d763cef2SBarry Smith Output Parameter: 2583d763cef2SBarry Smith . snes - the nonlinear solver context 2584d763cef2SBarry Smith 2585d763cef2SBarry Smith Notes: 2586d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2587d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 258894b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2589d763cef2SBarry Smith 2590d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 25910298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2592d763cef2SBarry Smith 2593d763cef2SBarry Smith Level: beginner 2594d763cef2SBarry Smith 2595d763cef2SBarry Smith .keywords: timestep, get, SNES 2596d763cef2SBarry Smith @*/ 25977087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2598d763cef2SBarry Smith { 2599d372ba47SLisandro Dalcin PetscErrorCode ierr; 2600d372ba47SLisandro Dalcin 2601d763cef2SBarry Smith PetscFunctionBegin; 26020700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26034482741eSBarry Smith PetscValidPointer(snes,2); 2604d372ba47SLisandro Dalcin if (!ts->snes) { 2605ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 26060298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 26073bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2608d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2609496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 26109e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 26119e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 26129e2a6581SJed Brown } 2613d372ba47SLisandro Dalcin } 2614d763cef2SBarry Smith *snes = ts->snes; 2615d763cef2SBarry Smith PetscFunctionReturn(0); 2616d763cef2SBarry Smith } 2617d763cef2SBarry Smith 26184a2ae208SSatish Balay #undef __FUNCT__ 2619deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES" 2620deb2cd25SJed Brown /*@ 2621deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2622deb2cd25SJed Brown 2623deb2cd25SJed Brown Collective 2624deb2cd25SJed Brown 2625deb2cd25SJed Brown Input Parameter: 2626deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2627deb2cd25SJed Brown - snes - the nonlinear solver context 2628deb2cd25SJed Brown 2629deb2cd25SJed Brown Notes: 2630deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2631deb2cd25SJed Brown 2632deb2cd25SJed Brown Level: developer 2633deb2cd25SJed Brown 2634deb2cd25SJed Brown .keywords: timestep, set, SNES 2635deb2cd25SJed Brown @*/ 2636deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2637deb2cd25SJed Brown { 2638deb2cd25SJed Brown PetscErrorCode ierr; 2639d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2640deb2cd25SJed Brown 2641deb2cd25SJed Brown PetscFunctionBegin; 2642deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2643deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2644deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2645deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2646bbd56ea5SKarl Rupp 2647deb2cd25SJed Brown ts->snes = snes; 2648bbd56ea5SKarl Rupp 26490298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 26500298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2651740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 26520298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2653740132f1SEmil Constantinescu } 2654deb2cd25SJed Brown PetscFunctionReturn(0); 2655deb2cd25SJed Brown } 2656deb2cd25SJed Brown 2657deb2cd25SJed Brown #undef __FUNCT__ 265894b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 2659d8e5e3e6SSatish Balay /*@ 266094b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2661d763cef2SBarry Smith a TS (timestepper) context. 2662d763cef2SBarry Smith 266394b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2664d763cef2SBarry Smith 2665d763cef2SBarry Smith Input Parameter: 2666d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2667d763cef2SBarry Smith 2668d763cef2SBarry Smith Output Parameter: 266994b7f48cSBarry Smith . ksp - the nonlinear solver context 2670d763cef2SBarry Smith 2671d763cef2SBarry Smith Notes: 267294b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2673d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2674d763cef2SBarry Smith KSP and PC contexts as well. 2675d763cef2SBarry Smith 267694b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 26770298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2678d763cef2SBarry Smith 2679d763cef2SBarry Smith Level: beginner 2680d763cef2SBarry Smith 268194b7f48cSBarry Smith .keywords: timestep, get, KSP 2682d763cef2SBarry Smith @*/ 26837087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2684d763cef2SBarry Smith { 2685d372ba47SLisandro Dalcin PetscErrorCode ierr; 2686089b2837SJed Brown SNES snes; 2687d372ba47SLisandro Dalcin 2688d763cef2SBarry Smith PetscFunctionBegin; 26890700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26904482741eSBarry Smith PetscValidPointer(ksp,2); 269117186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2692e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2693089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2694089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2695d763cef2SBarry Smith PetscFunctionReturn(0); 2696d763cef2SBarry Smith } 2697d763cef2SBarry Smith 2698d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2699d763cef2SBarry Smith 27004a2ae208SSatish Balay #undef __FUNCT__ 2701adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 2702adb62b0dSMatthew Knepley /*@ 2703adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2704adb62b0dSMatthew Knepley maximum time for iteration. 2705adb62b0dSMatthew Knepley 27063f9fe445SBarry Smith Not Collective 2707adb62b0dSMatthew Knepley 2708adb62b0dSMatthew Knepley Input Parameters: 2709adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 27100298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 27110298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2712adb62b0dSMatthew Knepley 2713adb62b0dSMatthew Knepley Level: intermediate 2714adb62b0dSMatthew Knepley 2715adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2716adb62b0dSMatthew Knepley @*/ 27177087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2718adb62b0dSMatthew Knepley { 2719adb62b0dSMatthew Knepley PetscFunctionBegin; 27200700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2721abc0a331SBarry Smith if (maxsteps) { 27224482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2723adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2724adb62b0dSMatthew Knepley } 2725abc0a331SBarry Smith if (maxtime) { 27264482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2727adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2728adb62b0dSMatthew Knepley } 2729adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2730adb62b0dSMatthew Knepley } 2731adb62b0dSMatthew Knepley 2732adb62b0dSMatthew Knepley #undef __FUNCT__ 27334a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 2734d763cef2SBarry Smith /*@ 2735d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2736d763cef2SBarry Smith maximum time for iteration. 2737d763cef2SBarry Smith 27383f9fe445SBarry Smith Logically Collective on TS 2739d763cef2SBarry Smith 2740d763cef2SBarry Smith Input Parameters: 2741d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2742d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2743d763cef2SBarry Smith - maxtime - final time to iterate to 2744d763cef2SBarry Smith 2745d763cef2SBarry Smith Options Database Keys: 2746d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 27473bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2748d763cef2SBarry Smith 2749d763cef2SBarry Smith Notes: 2750d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2751d763cef2SBarry Smith 2752d763cef2SBarry Smith Level: intermediate 2753d763cef2SBarry Smith 2754d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2755a43b19c4SJed Brown 2756a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2757d763cef2SBarry Smith @*/ 27587087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2759d763cef2SBarry Smith { 2760d763cef2SBarry Smith PetscFunctionBegin; 27610700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2762c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2763c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 276439b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 276539b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2766d763cef2SBarry Smith PetscFunctionReturn(0); 2767d763cef2SBarry Smith } 2768d763cef2SBarry Smith 27694a2ae208SSatish Balay #undef __FUNCT__ 27704a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 2771d763cef2SBarry Smith /*@ 2772d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2773d763cef2SBarry Smith for use by the TS routines. 2774d763cef2SBarry Smith 27753f9fe445SBarry Smith Logically Collective on TS and Vec 2776d763cef2SBarry Smith 2777d763cef2SBarry Smith Input Parameters: 2778d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 27790910c330SBarry Smith - u - the solution vector 2780d763cef2SBarry Smith 2781d763cef2SBarry Smith Level: beginner 2782d763cef2SBarry Smith 2783d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2784d763cef2SBarry Smith @*/ 27850910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2786d763cef2SBarry Smith { 27878737fe31SLisandro Dalcin PetscErrorCode ierr; 2788496e6a7aSJed Brown DM dm; 27898737fe31SLisandro Dalcin 2790d763cef2SBarry Smith PetscFunctionBegin; 27910700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27920910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 27930910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 27946bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 27950910c330SBarry Smith ts->vec_sol = u; 2796bbd56ea5SKarl Rupp 2797496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 27980910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2799d763cef2SBarry Smith PetscFunctionReturn(0); 2800d763cef2SBarry Smith } 2801d763cef2SBarry Smith 2802e74ef692SMatthew Knepley #undef __FUNCT__ 280373b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps" 280473b18844SBarry Smith /*@ 280573b18844SBarry Smith TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time 280673b18844SBarry Smith 280773b18844SBarry Smith Logically Collective on TS 280873b18844SBarry Smith 280973b18844SBarry Smith Input Parameters: 281073b18844SBarry Smith + ts - the TS context obtained from TSCreate() 281173b18844SBarry Smith . steps - number of steps to use 281273b18844SBarry Smith 281373b18844SBarry Smith Level: intermediate 281473b18844SBarry Smith 281573b18844SBarry Smith Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this 281673b18844SBarry Smith so as to integrate back to less than the original timestep 281773b18844SBarry Smith 281873b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations 281973b18844SBarry Smith 282073b18844SBarry Smith .seealso: TSSetExactFinalTime() 282173b18844SBarry Smith @*/ 282273b18844SBarry Smith PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps) 282373b18844SBarry Smith { 282473b18844SBarry Smith PetscFunctionBegin; 282573b18844SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 282673b18844SBarry Smith PetscValidLogicalCollectiveInt(ts,steps,2); 282773b18844SBarry Smith if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps"); 282873b18844SBarry 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"); 282973b18844SBarry Smith ts->adjoint_max_steps = steps; 283073b18844SBarry Smith PetscFunctionReturn(0); 283173b18844SBarry Smith } 283273b18844SBarry Smith 283373b18844SBarry Smith #undef __FUNCT__ 2834dfb21088SHong Zhang #define __FUNCT__ "TSSetCostGradients" 2835c235aa8dSHong Zhang /*@ 2836dfb21088SHong 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 28370cf82b59SBarry Smith for use by the TSAdjoint routines. 2838c235aa8dSHong Zhang 2839c235aa8dSHong Zhang Logically Collective on TS and Vec 2840c235aa8dSHong Zhang 2841c235aa8dSHong Zhang Input Parameters: 2842c235aa8dSHong Zhang + ts - the TS context obtained from TSCreate() 2843abc2977eSBarry 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 2844abc2977eSBarry Smith - mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters 2845c235aa8dSHong Zhang 2846c235aa8dSHong Zhang Level: beginner 2847c235aa8dSHong Zhang 2848abc2977eSBarry Smith Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime mu_i = df/dp|finaltime 28490cf82b59SBarry Smith 2850c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions 2851c235aa8dSHong Zhang @*/ 2852dfb21088SHong Zhang PetscErrorCode TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu) 2853c235aa8dSHong Zhang { 2854c235aa8dSHong Zhang PetscFunctionBegin; 2855c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2856abc2977eSBarry Smith PetscValidPointer(lambda,2); 2857abc2977eSBarry Smith ts->vecs_sensi = lambda; 2858abc2977eSBarry Smith ts->vecs_sensip = mu; 2859dfb21088SHong 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"); 2860abc2977eSBarry Smith ts->numcost = numcost; 2861ad8e2604SHong Zhang PetscFunctionReturn(0); 2862ad8e2604SHong Zhang } 2863ad8e2604SHong Zhang 2864ad8e2604SHong Zhang #undef __FUNCT__ 28655bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian" 2866ad8e2604SHong Zhang /*@C 28670cf82b59SBarry 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. 2868ad8e2604SHong Zhang 2869ad8e2604SHong Zhang Logically Collective on TS 2870ad8e2604SHong Zhang 2871ad8e2604SHong Zhang Input Parameters: 2872ad8e2604SHong Zhang + ts - The TS context obtained from TSCreate() 2873ad8e2604SHong Zhang - func - The function 2874ad8e2604SHong Zhang 2875ad8e2604SHong Zhang Calling sequence of func: 28760cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx); 287705755b9cSHong Zhang + t - current timestep 28780cf82b59SBarry Smith . y - input vector (current ODE solution) 287905755b9cSHong Zhang . A - output matrix 288005755b9cSHong Zhang - ctx - [optional] user-defined function context 2881ad8e2604SHong Zhang 2882ad8e2604SHong Zhang Level: intermediate 2883ad8e2604SHong Zhang 28840cf82b59SBarry 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 28850cf82b59SBarry Smith 2886ad8e2604SHong Zhang .keywords: TS, sensitivity 2887ad8e2604SHong Zhang .seealso: 2888ad8e2604SHong Zhang @*/ 28895bf8c567SBarry Smith PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 2890ad8e2604SHong Zhang { 2891ad8e2604SHong Zhang PetscErrorCode ierr; 2892ad8e2604SHong Zhang 2893ad8e2604SHong Zhang PetscFunctionBegin; 2894ad8e2604SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2895ad8e2604SHong Zhang if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 2896ad8e2604SHong Zhang 2897ad8e2604SHong Zhang ts->rhsjacobianp = func; 2898ad8e2604SHong Zhang ts->rhsjacobianpctx = ctx; 2899ad8e2604SHong Zhang if(Amat) { 2900ad8e2604SHong Zhang ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 2901ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2902ad8e2604SHong Zhang ts->Jacp = Amat; 2903ad8e2604SHong Zhang } 2904ad8e2604SHong Zhang PetscFunctionReturn(0); 2905ad8e2604SHong Zhang } 2906ad8e2604SHong Zhang 2907ad8e2604SHong Zhang #undef __FUNCT__ 29085bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian" 29090cf82b59SBarry Smith /*@C 29105bf8c567SBarry Smith TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function. 2911ad8e2604SHong Zhang 2912ad8e2604SHong Zhang Collective on TS 2913ad8e2604SHong Zhang 2914ad8e2604SHong Zhang Input Parameters: 2915ad8e2604SHong Zhang . ts - The TS context obtained from TSCreate() 2916ad8e2604SHong Zhang 2917ad8e2604SHong Zhang Level: developer 2918ad8e2604SHong Zhang 2919ad8e2604SHong Zhang .keywords: TS, sensitivity 29205bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian() 2921ad8e2604SHong Zhang @*/ 29225bf8c567SBarry Smith PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat) 2923ad8e2604SHong Zhang { 2924ad8e2604SHong Zhang PetscErrorCode ierr; 2925ad8e2604SHong Zhang 2926ad8e2604SHong Zhang PetscFunctionBegin; 2927ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2928ad8e2604SHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 2929ad8e2604SHong Zhang PetscValidPointer(Amat,4); 2930ad8e2604SHong Zhang 2931ad8e2604SHong Zhang PetscStackPush("TS user JacobianP function for sensitivity analysis"); 2932ad8e2604SHong Zhang ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 2933ad8e2604SHong Zhang PetscStackPop; 2934c235aa8dSHong Zhang PetscFunctionReturn(0); 2935c235aa8dSHong Zhang } 2936c235aa8dSHong Zhang 2937c235aa8dSHong Zhang #undef __FUNCT__ 2938dfb21088SHong Zhang #define __FUNCT__ "TSSetCostIntegrand" 29396fd0887fSHong Zhang /*@C 2940dfb21088SHong Zhang TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions 29416fd0887fSHong Zhang 29426fd0887fSHong Zhang Logically Collective on TS 29436fd0887fSHong Zhang 29446fd0887fSHong Zhang Input Parameters: 29456fd0887fSHong Zhang + ts - the TS context obtained from TSCreate() 2946abc2977eSBarry Smith . numcost - number of gradients to be computed, this is the number of cost functions 29470cf82b59SBarry Smith . rf - routine for evaluating the integrand function 2948b612ec54SBarry Smith . drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y 2949b612ec54SBarry Smith . drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p 2950b1cb36f3SHong Zhang . fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run 2951b612ec54SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 29526fd0887fSHong Zhang 29530cf82b59SBarry Smith Calling sequence of rf: 29540cf82b59SBarry Smith $ rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx); 29556fd0887fSHong Zhang 29566fd0887fSHong Zhang + t - current timestep 29570cf82b59SBarry Smith . y - input vector 29580cf82b59SBarry Smith . f - function result; one vector entry for each cost function 29596fd0887fSHong Zhang - ctx - [optional] user-defined function context 29606fd0887fSHong Zhang 2961b612ec54SBarry Smith Calling sequence of drdyf: 29620cf82b59SBarry Smith $ PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx); 2963b612ec54SBarry Smith 2964b612ec54SBarry Smith Calling sequence of drdpf: 29650cf82b59SBarry Smith $ PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx); 2966b612ec54SBarry Smith 2967b612ec54SBarry Smith Level: intermediate 29686fd0887fSHong Zhang 2969abc2977eSBarry Smith Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions 29700cf82b59SBarry Smith 29716fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 29726fd0887fSHong Zhang 2973dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients() 29746fd0887fSHong Zhang @*/ 2975dfb21088SHong Zhang PetscErrorCode TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*), 2976d8151eeaSBarry Smith PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*), 2977b1cb36f3SHong Zhang PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*), 2978b1cb36f3SHong Zhang PetscBool fwd,void *ctx) 29796fd0887fSHong Zhang { 29806fd0887fSHong Zhang PetscErrorCode ierr; 29816fd0887fSHong Zhang 29826fd0887fSHong Zhang PetscFunctionBegin; 29836fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2984dfb21088SHong 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()"); 2985c72ed514SHong Zhang if (!ts->numcost) ts->numcost=numcost; 29866fd0887fSHong Zhang 2987b1cb36f3SHong Zhang ts->costintegralfwd = fwd; /* Evaluate the cost integral in forward run if fwd is true */ 2988abc2977eSBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr); 29892c39e106SBarry Smith ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr); 29900cf82b59SBarry Smith ts->costintegrand = rf; 299105755b9cSHong Zhang ts->costintegrandctx = ctx; 2992b612ec54SBarry Smith ts->drdyfunction = drdyf; 2993b612ec54SBarry Smith ts->drdpfunction = drdpf; 29946fd0887fSHong Zhang PetscFunctionReturn(0); 29956fd0887fSHong Zhang } 29966fd0887fSHong Zhang 29976fd0887fSHong Zhang #undef __FUNCT__ 2998dfb21088SHong Zhang #define __FUNCT__ "TSGetCostIntegral" 299936eaed60SHong Zhang /*@ 3000dfb21088SHong Zhang TSGetCostIntegral - Returns the values of the integral term in the cost functions. 300136eaed60SHong Zhang It is valid to call the routine after a backward run. 300236eaed60SHong Zhang 300336eaed60SHong Zhang Not Collective 300436eaed60SHong Zhang 300536eaed60SHong Zhang Input Parameter: 300636eaed60SHong Zhang . ts - the TS context obtained from TSCreate() 300736eaed60SHong Zhang 300836eaed60SHong Zhang Output Parameter: 30092c39e106SBarry Smith . v - the vector containing the integrals for each cost function 301036eaed60SHong Zhang 301136eaed60SHong Zhang Level: intermediate 301236eaed60SHong Zhang 3013dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 301436eaed60SHong Zhang 301536eaed60SHong Zhang .keywords: TS, sensitivity analysis 301636eaed60SHong Zhang @*/ 3017dfb21088SHong Zhang PetscErrorCode TSGetCostIntegral(TS ts,Vec *v) 301836eaed60SHong Zhang { 301936eaed60SHong Zhang PetscFunctionBegin; 302036eaed60SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 302136eaed60SHong Zhang PetscValidPointer(v,2); 30222c39e106SBarry Smith *v = ts->vec_costintegral; 302336eaed60SHong Zhang PetscFunctionReturn(0); 302436eaed60SHong Zhang } 302536eaed60SHong Zhang 302636eaed60SHong Zhang #undef __FUNCT__ 3027d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand" 30286fd0887fSHong Zhang /*@ 30292c39e106SBarry Smith TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions. 30306fd0887fSHong Zhang 30316fd0887fSHong Zhang Input Parameters: 30326fd0887fSHong Zhang + ts - the TS context 30336fd0887fSHong Zhang . t - current time 30340cf82b59SBarry Smith - y - state vector, i.e. current solution 30356fd0887fSHong Zhang 30366fd0887fSHong Zhang Output Parameter: 3037abc2977eSBarry Smith . q - vector of size numcost to hold the outputs 30386fd0887fSHong Zhang 30396fd0887fSHong Zhang Note: 30406fd0887fSHong Zhang Most users should not need to explicitly call this routine, as it 30416fd0887fSHong Zhang is used internally within the sensitivity analysis context. 30426fd0887fSHong Zhang 30436fd0887fSHong Zhang Level: developer 30446fd0887fSHong Zhang 30456fd0887fSHong Zhang .keywords: TS, compute 30466fd0887fSHong Zhang 3047dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 30486fd0887fSHong Zhang @*/ 30490cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q) 30506fd0887fSHong Zhang { 30516fd0887fSHong Zhang PetscErrorCode ierr; 30526fd0887fSHong Zhang 30536fd0887fSHong Zhang PetscFunctionBegin; 30546fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30550cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 30566fd0887fSHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,4); 30576fd0887fSHong Zhang 30580cf82b59SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 3059e4680132SHong Zhang if (ts->costintegrand) { 3060e4680132SHong Zhang PetscStackPush("TS user integrand in the cost function"); 30610cf82b59SBarry Smith ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr); 30626fd0887fSHong Zhang PetscStackPop; 30636fd0887fSHong Zhang } else { 30646fd0887fSHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 30656fd0887fSHong Zhang } 30666fd0887fSHong Zhang 30670cf82b59SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 30686fd0887fSHong Zhang PetscFunctionReturn(0); 30696fd0887fSHong Zhang } 30706fd0887fSHong Zhang 30716fd0887fSHong Zhang #undef __FUNCT__ 3072d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction" 307305755b9cSHong Zhang /*@ 3074d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 307505755b9cSHong Zhang 307605755b9cSHong Zhang Collective on TS 307705755b9cSHong Zhang 307805755b9cSHong Zhang Input Parameters: 307905755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 308005755b9cSHong Zhang 308105755b9cSHong Zhang Notes: 3082d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 308305755b9cSHong Zhang so most users would not generally call this routine themselves. 308405755b9cSHong Zhang 308505755b9cSHong Zhang Level: developer 308605755b9cSHong Zhang 308705755b9cSHong Zhang .keywords: TS, sensitivity 3088d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction() 308905755b9cSHong Zhang @*/ 30900cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy) 309105755b9cSHong Zhang { 309205755b9cSHong Zhang PetscErrorCode ierr; 309305755b9cSHong Zhang 309405755b9cSHong Zhang PetscFunctionBegin; 309505755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30960cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 309705755b9cSHong Zhang 309805755b9cSHong Zhang PetscStackPush("TS user DRDY function for sensitivity analysis"); 30990cf82b59SBarry Smith ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr); 310005755b9cSHong Zhang PetscStackPop; 310105755b9cSHong Zhang PetscFunctionReturn(0); 310205755b9cSHong Zhang } 310305755b9cSHong Zhang 310405755b9cSHong Zhang #undef __FUNCT__ 3105d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction" 310605755b9cSHong Zhang /*@ 3107d4aa0a58SBarry Smith TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 310805755b9cSHong Zhang 310905755b9cSHong Zhang Collective on TS 311005755b9cSHong Zhang 311105755b9cSHong Zhang Input Parameters: 311205755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 311305755b9cSHong Zhang 311405755b9cSHong Zhang Notes: 311505755b9cSHong Zhang TSDRDPFunction() is typically used for sensitivity implementation, 311605755b9cSHong Zhang so most users would not generally call this routine themselves. 311705755b9cSHong Zhang 311805755b9cSHong Zhang Level: developer 311905755b9cSHong Zhang 312005755b9cSHong Zhang .keywords: TS, sensitivity 3121d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction() 312205755b9cSHong Zhang @*/ 31230cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp) 312405755b9cSHong Zhang { 312505755b9cSHong Zhang PetscErrorCode ierr; 312605755b9cSHong Zhang 312705755b9cSHong Zhang PetscFunctionBegin; 312805755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31290cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 313005755b9cSHong Zhang 313105755b9cSHong Zhang PetscStackPush("TS user DRDP function for sensitivity analysis"); 31320cf82b59SBarry Smith ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr); 313305755b9cSHong Zhang PetscStackPop; 313405755b9cSHong Zhang PetscFunctionReturn(0); 313505755b9cSHong Zhang } 313605755b9cSHong Zhang 313705755b9cSHong Zhang #undef __FUNCT__ 3138e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 3139ac226902SBarry Smith /*@C 3140000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 31413f2090d5SJed Brown called once at the beginning of each time step. 3142000e7ae3SMatthew Knepley 31433f9fe445SBarry Smith Logically Collective on TS 3144000e7ae3SMatthew Knepley 3145000e7ae3SMatthew Knepley Input Parameters: 3146000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3147000e7ae3SMatthew Knepley - func - The function 3148000e7ae3SMatthew Knepley 3149000e7ae3SMatthew Knepley Calling sequence of func: 3150000e7ae3SMatthew Knepley . func (TS ts); 3151000e7ae3SMatthew Knepley 3152000e7ae3SMatthew Knepley Level: intermediate 3153000e7ae3SMatthew Knepley 3154b8123daeSJed Brown Note: 3155b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 3156b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 3157b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 3158b8123daeSJed Brown 3159000e7ae3SMatthew Knepley .keywords: TS, timestep 31609be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 3161000e7ae3SMatthew Knepley @*/ 31627087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3163000e7ae3SMatthew Knepley { 3164000e7ae3SMatthew Knepley PetscFunctionBegin; 31650700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3166ae60f76fSBarry Smith ts->prestep = func; 3167000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3168000e7ae3SMatthew Knepley } 3169000e7ae3SMatthew Knepley 3170e74ef692SMatthew Knepley #undef __FUNCT__ 31713f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 317209ee8438SJed Brown /*@ 31733f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 31743f2090d5SJed Brown 31753f2090d5SJed Brown Collective on TS 31763f2090d5SJed Brown 31773f2090d5SJed Brown Input Parameters: 31783f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 31793f2090d5SJed Brown 31803f2090d5SJed Brown Notes: 31813f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 31823f2090d5SJed Brown so most users would not generally call this routine themselves. 31833f2090d5SJed Brown 31843f2090d5SJed Brown Level: developer 31853f2090d5SJed Brown 31863f2090d5SJed Brown .keywords: TS, timestep 31879be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 31883f2090d5SJed Brown @*/ 31897087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 31903f2090d5SJed Brown { 31913f2090d5SJed Brown PetscErrorCode ierr; 31923f2090d5SJed Brown 31933f2090d5SJed Brown PetscFunctionBegin; 31940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3195ae60f76fSBarry Smith if (ts->prestep) { 3196ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 3197312ce896SJed Brown } 31983f2090d5SJed Brown PetscFunctionReturn(0); 31993f2090d5SJed Brown } 32003f2090d5SJed Brown 32013f2090d5SJed Brown #undef __FUNCT__ 3202b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 3203b8123daeSJed Brown /*@C 3204b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3205b8123daeSJed Brown called once at the beginning of each stage. 3206b8123daeSJed Brown 3207b8123daeSJed Brown Logically Collective on TS 3208b8123daeSJed Brown 3209b8123daeSJed Brown Input Parameters: 3210b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 3211b8123daeSJed Brown - func - The function 3212b8123daeSJed Brown 3213b8123daeSJed Brown Calling sequence of func: 3214b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 3215b8123daeSJed Brown 3216b8123daeSJed Brown Level: intermediate 3217b8123daeSJed Brown 3218b8123daeSJed Brown Note: 3219b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 3220b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 3221b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 3222b8123daeSJed Brown 3223b8123daeSJed Brown .keywords: TS, timestep 32249be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3225b8123daeSJed Brown @*/ 3226b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 3227b8123daeSJed Brown { 3228b8123daeSJed Brown PetscFunctionBegin; 3229b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3230ae60f76fSBarry Smith ts->prestage = func; 3231b8123daeSJed Brown PetscFunctionReturn(0); 3232b8123daeSJed Brown } 3233b8123daeSJed Brown 3234b8123daeSJed Brown #undef __FUNCT__ 32359be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage" 32369be3e283SDebojyoti Ghosh /*@C 32379be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 32389be3e283SDebojyoti Ghosh called once at the end of each stage. 32399be3e283SDebojyoti Ghosh 32409be3e283SDebojyoti Ghosh Logically Collective on TS 32419be3e283SDebojyoti Ghosh 32429be3e283SDebojyoti Ghosh Input Parameters: 32439be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 32449be3e283SDebojyoti Ghosh - func - The function 32459be3e283SDebojyoti Ghosh 32469be3e283SDebojyoti Ghosh Calling sequence of func: 32479be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 32489be3e283SDebojyoti Ghosh 32499be3e283SDebojyoti Ghosh Level: intermediate 32509be3e283SDebojyoti Ghosh 32519be3e283SDebojyoti Ghosh Note: 32529be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 32539be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 32549be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 32559be3e283SDebojyoti Ghosh 32569be3e283SDebojyoti Ghosh .keywords: TS, timestep 32579be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 32589be3e283SDebojyoti Ghosh @*/ 32599be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 32609be3e283SDebojyoti Ghosh { 32619be3e283SDebojyoti Ghosh PetscFunctionBegin; 32629be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 32639be3e283SDebojyoti Ghosh ts->poststage = func; 32649be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 32659be3e283SDebojyoti Ghosh } 32669be3e283SDebojyoti Ghosh 32679be3e283SDebojyoti Ghosh #undef __FUNCT__ 3268b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 3269b8123daeSJed Brown /*@ 3270b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 3271b8123daeSJed Brown 3272b8123daeSJed Brown Collective on TS 3273b8123daeSJed Brown 3274b8123daeSJed Brown Input Parameters: 3275b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 32769be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3277b8123daeSJed Brown 3278b8123daeSJed Brown Notes: 3279b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 3280b8123daeSJed Brown most users would not generally call this routine themselves. 3281b8123daeSJed Brown 3282b8123daeSJed Brown Level: developer 3283b8123daeSJed Brown 3284b8123daeSJed Brown .keywords: TS, timestep 32859be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 3286b8123daeSJed Brown @*/ 3287b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3288b8123daeSJed Brown { 3289b8123daeSJed Brown PetscErrorCode ierr; 3290b8123daeSJed Brown 3291b8123daeSJed Brown PetscFunctionBegin; 3292b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3293ae60f76fSBarry Smith if (ts->prestage) { 3294ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 3295b8123daeSJed Brown } 3296b8123daeSJed Brown PetscFunctionReturn(0); 3297b8123daeSJed Brown } 3298b8123daeSJed Brown 3299b8123daeSJed Brown #undef __FUNCT__ 33009be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage" 33019be3e283SDebojyoti Ghosh /*@ 33029be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 33039be3e283SDebojyoti Ghosh 33049be3e283SDebojyoti Ghosh Collective on TS 33059be3e283SDebojyoti Ghosh 33069be3e283SDebojyoti Ghosh Input Parameters: 33079be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 33089be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 33099be3e283SDebojyoti Ghosh stageindex - Stage number 33109be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 33119be3e283SDebojyoti Ghosh of stages) with the stage solutions 33129be3e283SDebojyoti Ghosh 33139be3e283SDebojyoti Ghosh Notes: 33149be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 33159be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 33169be3e283SDebojyoti Ghosh 33179be3e283SDebojyoti Ghosh Level: developer 33189be3e283SDebojyoti Ghosh 33199be3e283SDebojyoti Ghosh .keywords: TS, timestep 33209be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 33219be3e283SDebojyoti Ghosh @*/ 33229be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 33239be3e283SDebojyoti Ghosh { 33249be3e283SDebojyoti Ghosh PetscErrorCode ierr; 33259be3e283SDebojyoti Ghosh 33269be3e283SDebojyoti Ghosh PetscFunctionBegin; 33279be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 33284beae5d8SLisandro Dalcin if (ts->poststage) { 33299be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 33309be3e283SDebojyoti Ghosh } 33319be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 33329be3e283SDebojyoti Ghosh } 33339be3e283SDebojyoti Ghosh 33349be3e283SDebojyoti Ghosh #undef __FUNCT__ 3335e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 3336ac226902SBarry Smith /*@C 3337000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 33383f2090d5SJed Brown called once at the end of each time step. 3339000e7ae3SMatthew Knepley 33403f9fe445SBarry Smith Logically Collective on TS 3341000e7ae3SMatthew Knepley 3342000e7ae3SMatthew Knepley Input Parameters: 3343000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3344000e7ae3SMatthew Knepley - func - The function 3345000e7ae3SMatthew Knepley 3346000e7ae3SMatthew Knepley Calling sequence of func: 3347b8123daeSJed Brown $ func (TS ts); 3348000e7ae3SMatthew Knepley 3349000e7ae3SMatthew Knepley Level: intermediate 3350000e7ae3SMatthew Knepley 3351000e7ae3SMatthew Knepley .keywords: TS, timestep 3352b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 3353000e7ae3SMatthew Knepley @*/ 33547087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 3355000e7ae3SMatthew Knepley { 3356000e7ae3SMatthew Knepley PetscFunctionBegin; 33570700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3358ae60f76fSBarry Smith ts->poststep = func; 3359000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3360000e7ae3SMatthew Knepley } 3361000e7ae3SMatthew Knepley 3362e74ef692SMatthew Knepley #undef __FUNCT__ 33633f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 336409ee8438SJed Brown /*@ 33653f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 33663f2090d5SJed Brown 33673f2090d5SJed Brown Collective on TS 33683f2090d5SJed Brown 33693f2090d5SJed Brown Input Parameters: 33703f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 33713f2090d5SJed Brown 33723f2090d5SJed Brown Notes: 33733f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 33743f2090d5SJed Brown so most users would not generally call this routine themselves. 33753f2090d5SJed Brown 33763f2090d5SJed Brown Level: developer 33773f2090d5SJed Brown 33783f2090d5SJed Brown .keywords: TS, timestep 33793f2090d5SJed Brown @*/ 33807087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 33813f2090d5SJed Brown { 33823f2090d5SJed Brown PetscErrorCode ierr; 33833f2090d5SJed Brown 33843f2090d5SJed Brown PetscFunctionBegin; 33850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3386ae60f76fSBarry Smith if (ts->poststep) { 3387ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 338872ac3e02SJed Brown } 33893f2090d5SJed Brown PetscFunctionReturn(0); 33903f2090d5SJed Brown } 33913f2090d5SJed Brown 3392d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 3393d763cef2SBarry Smith 33944a2ae208SSatish Balay #undef __FUNCT__ 3395a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 3396d763cef2SBarry Smith /*@C 3397a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 3398d763cef2SBarry Smith timestep to display the iteration's progress. 3399d763cef2SBarry Smith 34003f9fe445SBarry Smith Logically Collective on TS 3401d763cef2SBarry Smith 3402d763cef2SBarry Smith Input Parameters: 3403d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3404e213d8f1SJed Brown . monitor - monitoring routine 3405329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 34060298fd71SBarry Smith monitor routine (use NULL if no context is desired) 3407b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 34080298fd71SBarry Smith (may be NULL) 3409d763cef2SBarry Smith 3410e213d8f1SJed Brown Calling sequence of monitor: 34110910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 3412d763cef2SBarry Smith 3413d763cef2SBarry Smith + ts - the TS context 341463e21af5SBarry 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) 34151f06c33eSBarry Smith . time - current time 34160910c330SBarry Smith . u - current iterate 3417d763cef2SBarry Smith - mctx - [optional] monitoring context 3418d763cef2SBarry Smith 3419d763cef2SBarry Smith Notes: 3420d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 3421d763cef2SBarry Smith already has been loaded. 3422d763cef2SBarry Smith 3423025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 3424025f1a04SBarry Smith 3425d763cef2SBarry Smith Level: intermediate 3426d763cef2SBarry Smith 3427d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3428d763cef2SBarry Smith 3429a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 3430d763cef2SBarry Smith @*/ 3431c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 3432d763cef2SBarry Smith { 3433d763cef2SBarry Smith PetscFunctionBegin; 34340700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 343517186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 3436d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 34378704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 3438d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 3439d763cef2SBarry Smith PetscFunctionReturn(0); 3440d763cef2SBarry Smith } 3441d763cef2SBarry Smith 34424a2ae208SSatish Balay #undef __FUNCT__ 3443a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 3444d763cef2SBarry Smith /*@C 3445a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 3446d763cef2SBarry Smith 34473f9fe445SBarry Smith Logically Collective on TS 3448d763cef2SBarry Smith 3449d763cef2SBarry Smith Input Parameters: 3450d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3451d763cef2SBarry Smith 3452d763cef2SBarry Smith Notes: 3453d763cef2SBarry Smith There is no way to remove a single, specific monitor. 3454d763cef2SBarry Smith 3455d763cef2SBarry Smith Level: intermediate 3456d763cef2SBarry Smith 3457d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3458d763cef2SBarry Smith 3459a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 3460d763cef2SBarry Smith @*/ 34617087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 3462d763cef2SBarry Smith { 3463d952e501SBarry Smith PetscErrorCode ierr; 3464d952e501SBarry Smith PetscInt i; 3465d952e501SBarry Smith 3466d763cef2SBarry Smith PetscFunctionBegin; 34670700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3468d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 34698704b422SBarry Smith if (ts->monitordestroy[i]) { 34708704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 3471d952e501SBarry Smith } 3472d952e501SBarry Smith } 3473d763cef2SBarry Smith ts->numbermonitors = 0; 3474d763cef2SBarry Smith PetscFunctionReturn(0); 3475d763cef2SBarry Smith } 3476d763cef2SBarry Smith 34774a2ae208SSatish Balay #undef __FUNCT__ 3478a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 3479721cd6eeSBarry Smith /*@C 3480721cd6eeSBarry Smith TSMonitorDefault - The Default monitor, prints the timestep and time for each step 34815516499fSSatish Balay 34825516499fSSatish Balay Level: intermediate 348341251cbbSSatish Balay 34845516499fSSatish Balay .keywords: TS, set, monitor 34855516499fSSatish Balay 348663e21af5SBarry Smith .seealso: TSMonitorSet() 348741251cbbSSatish Balay @*/ 3488721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf) 3489d763cef2SBarry Smith { 3490dfbe8321SBarry Smith PetscErrorCode ierr; 3491721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 349241aca3d6SBarry Smith PetscBool iascii,ibinary; 3493d132466eSBarry Smith 3494d763cef2SBarry Smith PetscFunctionBegin; 34954d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 349641aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 349741aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr); 3498721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 349941aca3d6SBarry Smith if (iascii) { 3500649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 350163e21af5SBarry Smith if (step == -1){ /* this indicates it is an interpolated solution */ 350263e21af5SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr); 350363e21af5SBarry Smith } else { 35048392e04aSShri 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); 350563e21af5SBarry Smith } 3506649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 350741aca3d6SBarry Smith } else if (ibinary) { 350841aca3d6SBarry Smith PetscMPIInt rank; 350941aca3d6SBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 351041aca3d6SBarry Smith if (!rank) { 351141aca3d6SBarry Smith ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr); 351241aca3d6SBarry Smith } else { 351341aca3d6SBarry Smith ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr); 351441aca3d6SBarry Smith } 351541aca3d6SBarry Smith } 3516721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3517d763cef2SBarry Smith PetscFunctionReturn(0); 3518d763cef2SBarry Smith } 3519d763cef2SBarry Smith 35204a2ae208SSatish Balay #undef __FUNCT__ 35219110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorSet" 35229110b2e7SHong Zhang /*@C 35239110b2e7SHong Zhang TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every 35249110b2e7SHong Zhang timestep to display the iteration's progress. 35259110b2e7SHong Zhang 35269110b2e7SHong Zhang Logically Collective on TS 35279110b2e7SHong Zhang 35289110b2e7SHong Zhang Input Parameters: 35299110b2e7SHong Zhang + ts - the TS context obtained from TSCreate() 35309110b2e7SHong Zhang . adjointmonitor - monitoring routine 35319110b2e7SHong Zhang . adjointmctx - [optional] user-defined context for private data for the 35329110b2e7SHong Zhang monitor routine (use NULL if no context is desired) 35339110b2e7SHong Zhang - adjointmonitordestroy - [optional] routine that frees monitor context 35349110b2e7SHong Zhang (may be NULL) 35359110b2e7SHong Zhang 35369110b2e7SHong Zhang Calling sequence of monitor: 35379110b2e7SHong Zhang $ int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx) 35389110b2e7SHong Zhang 35399110b2e7SHong Zhang + ts - the TS context 35409110b2e7SHong 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 35419110b2e7SHong Zhang been interpolated to) 35429110b2e7SHong Zhang . time - current time 35439110b2e7SHong Zhang . u - current iterate 35449110b2e7SHong Zhang . numcost - number of cost functionos 35459110b2e7SHong Zhang . lambda - sensitivities to initial conditions 35469110b2e7SHong Zhang . mu - sensitivities to parameters 35479110b2e7SHong Zhang - adjointmctx - [optional] adjoint monitoring context 35489110b2e7SHong Zhang 35499110b2e7SHong Zhang Notes: 35509110b2e7SHong Zhang This routine adds an additional monitor to the list of monitors that 35519110b2e7SHong Zhang already has been loaded. 35529110b2e7SHong Zhang 35539110b2e7SHong Zhang Fortran notes: Only a single monitor function can be set for each TS object 35549110b2e7SHong Zhang 35559110b2e7SHong Zhang Level: intermediate 35569110b2e7SHong Zhang 35579110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 35589110b2e7SHong Zhang 35599110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel() 35609110b2e7SHong Zhang @*/ 35619110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**)) 35629110b2e7SHong Zhang { 35639110b2e7SHong Zhang PetscFunctionBegin; 35649110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 35659110b2e7SHong Zhang if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set"); 35669110b2e7SHong Zhang ts->adjointmonitor[ts->numberadjointmonitors] = adjointmonitor; 35679110b2e7SHong Zhang ts->adjointmonitordestroy[ts->numberadjointmonitors] = adjointmdestroy; 35689110b2e7SHong Zhang ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx; 35699110b2e7SHong Zhang PetscFunctionReturn(0); 35709110b2e7SHong Zhang } 35719110b2e7SHong Zhang 35729110b2e7SHong Zhang #undef __FUNCT__ 35739110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorCancel" 35749110b2e7SHong Zhang /*@C 35759110b2e7SHong Zhang TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object. 35769110b2e7SHong Zhang 35779110b2e7SHong Zhang Logically Collective on TS 35789110b2e7SHong Zhang 35799110b2e7SHong Zhang Input Parameters: 35809110b2e7SHong Zhang . ts - the TS context obtained from TSCreate() 35819110b2e7SHong Zhang 35829110b2e7SHong Zhang Notes: 35839110b2e7SHong Zhang There is no way to remove a single, specific monitor. 35849110b2e7SHong Zhang 35859110b2e7SHong Zhang Level: intermediate 35869110b2e7SHong Zhang 35879110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 35889110b2e7SHong Zhang 35899110b2e7SHong Zhang .seealso: TSAdjointMonitorSet() 35909110b2e7SHong Zhang @*/ 35919110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorCancel(TS ts) 35929110b2e7SHong Zhang { 35939110b2e7SHong Zhang PetscErrorCode ierr; 35949110b2e7SHong Zhang PetscInt i; 35959110b2e7SHong Zhang 35969110b2e7SHong Zhang PetscFunctionBegin; 35979110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 35989110b2e7SHong Zhang for (i=0; i<ts->numberadjointmonitors; i++) { 35999110b2e7SHong Zhang if (ts->adjointmonitordestroy[i]) { 36009110b2e7SHong Zhang ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 36019110b2e7SHong Zhang } 36029110b2e7SHong Zhang } 36039110b2e7SHong Zhang ts->numberadjointmonitors = 0; 36049110b2e7SHong Zhang PetscFunctionReturn(0); 36059110b2e7SHong Zhang } 36069110b2e7SHong Zhang 36079110b2e7SHong Zhang #undef __FUNCT__ 3608110eb670SHong Zhang #define __FUNCT__ "TSAdjointMonitorDefault" 3609721cd6eeSBarry Smith /*@C 3610721cd6eeSBarry Smith TSAdjointMonitorDefault - the default monitor of adjoint computations 3611110eb670SHong Zhang 3612110eb670SHong Zhang Level: intermediate 3613110eb670SHong Zhang 3614110eb670SHong Zhang .keywords: TS, set, monitor 3615110eb670SHong Zhang 3616110eb670SHong Zhang .seealso: TSAdjointMonitorSet() 3617110eb670SHong Zhang @*/ 3618721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf) 3619110eb670SHong Zhang { 3620110eb670SHong Zhang PetscErrorCode ierr; 3621721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 3622110eb670SHong Zhang 3623110eb670SHong Zhang PetscFunctionBegin; 3624110eb670SHong Zhang PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3625721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 3626110eb670SHong Zhang ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3627110eb670SHong 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); 3628110eb670SHong Zhang ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3629721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3630110eb670SHong Zhang PetscFunctionReturn(0); 3631110eb670SHong Zhang } 3632110eb670SHong Zhang 3633110eb670SHong Zhang #undef __FUNCT__ 3634cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 3635cd652676SJed Brown /*@ 3636cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3637cd652676SJed Brown 3638cd652676SJed Brown Collective on TS 3639cd652676SJed Brown 3640cd652676SJed Brown Input Argument: 3641cd652676SJed Brown + ts - time stepping context 3642cd652676SJed Brown - t - time to interpolate to 3643cd652676SJed Brown 3644cd652676SJed Brown Output Argument: 36450910c330SBarry Smith . U - state at given time 3646cd652676SJed Brown 3647cd652676SJed Brown Level: intermediate 3648cd652676SJed Brown 3649cd652676SJed Brown Developer Notes: 3650cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3651cd652676SJed Brown 3652cd652676SJed Brown .keywords: TS, set 3653cd652676SJed Brown 3654874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve() 3655cd652676SJed Brown @*/ 36560910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3657cd652676SJed Brown { 3658cd652676SJed Brown PetscErrorCode ierr; 3659cd652676SJed Brown 3660cd652676SJed Brown PetscFunctionBegin; 3661cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3662b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3663be5899b3SLisandro 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); 3664ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 36650910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3666cd652676SJed Brown PetscFunctionReturn(0); 3667cd652676SJed Brown } 3668cd652676SJed Brown 3669cd652676SJed Brown #undef __FUNCT__ 36704a2ae208SSatish Balay #define __FUNCT__ "TSStep" 3671d763cef2SBarry Smith /*@ 36726d9e5789SSean Farley TSStep - Steps one time step 3673d763cef2SBarry Smith 3674d763cef2SBarry Smith Collective on TS 3675d763cef2SBarry Smith 3676d763cef2SBarry Smith Input Parameter: 3677d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3678d763cef2SBarry Smith 367927829d71SBarry Smith Level: developer 3680d763cef2SBarry Smith 3681b8123daeSJed Brown Notes: 368227829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 368327829d71SBarry Smith 3684b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3685b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3686b8123daeSJed Brown 368725cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 368825cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 368925cb2221SBarry Smith 3690d763cef2SBarry Smith .keywords: TS, timestep, solve 3691d763cef2SBarry Smith 36929be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3693d763cef2SBarry Smith @*/ 3694193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3695d763cef2SBarry Smith { 3696dfbe8321SBarry Smith PetscErrorCode ierr; 3697fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3698be5899b3SLisandro Dalcin PetscReal ptime; 3699d763cef2SBarry Smith 3700d763cef2SBarry Smith PetscFunctionBegin; 37010700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3702fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3703fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3704fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3705fffbeea8SBarry Smith " type = {Preprint},\n" 3706fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3707fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3708302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3709fffbeea8SBarry Smith 3710d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 371168bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3712d405a339SMatthew Knepley 3713a6772fa2SLisandro 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()"); 3714be5899b3SLisandro 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"); 3715a6772fa2SLisandro Dalcin 3716be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 3717362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3718be5899b3SLisandro Dalcin ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev; 3719e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3720d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3721193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3722d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3723be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 3724be5899b3SLisandro Dalcin ts->steps++; ts->total_steps++; 3725be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 372628d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3727362cd11cSLisandro Dalcin 3728362cd11cSLisandro Dalcin if (ts->reason < 0) { 3729cef5090cSJed Brown if (ts->errorifstepfailed) { 373008c7845fSBarry 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]); 373108c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3732d2daff3dSHong Zhang } 373308c7845fSBarry Smith } else if (!ts->reason) { 373408c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 373508c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 373608c7845fSBarry Smith } 373708c7845fSBarry Smith PetscFunctionReturn(0); 373808c7845fSBarry Smith } 373908c7845fSBarry Smith 374008c7845fSBarry Smith #undef __FUNCT__ 374108c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep" 374208c7845fSBarry Smith /*@ 3743b957a604SHong Zhang TSAdjointStep - Steps one time step backward in the adjoint run 374408c7845fSBarry Smith 374508c7845fSBarry Smith Collective on TS 374608c7845fSBarry Smith 374708c7845fSBarry Smith Input Parameter: 374808c7845fSBarry Smith . ts - the TS context obtained from TSCreate() 374908c7845fSBarry Smith 37506a4d4014SLisandro Dalcin Level: intermediate 37516a4d4014SLisandro Dalcin 3752b957a604SHong Zhang .keywords: TS, adjoint, step 37536a4d4014SLisandro Dalcin 3754b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve() 37556a4d4014SLisandro Dalcin @*/ 375608c7845fSBarry Smith PetscErrorCode TSAdjointStep(TS ts) 37576a4d4014SLisandro Dalcin { 375808c7845fSBarry Smith DM dm; 37596a4d4014SLisandro Dalcin PetscErrorCode ierr; 37606a4d4014SLisandro Dalcin 37616a4d4014SLisandro Dalcin PetscFunctionBegin; 37624a2ae208SSatish Balay PetscValidHeaderSpecific(ts,TS_CLASSID,1); 376308c7845fSBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 376408c7845fSBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3765d763cef2SBarry Smith 3766685405a1SBarry Smith ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr); 3767d763cef2SBarry Smith 3768be5899b3SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3769be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime; 377042f2b339SBarry 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); 3771be5899b3SLisandro Dalcin ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 377242f2b339SBarry Smith ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr); 37738d0ad7a8SHong Zhang ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 3774be5899b3SLisandro Dalcin ts->steps++; ts->total_steps--; 3775362cd11cSLisandro Dalcin 3776362cd11cSLisandro Dalcin if (ts->reason < 0) { 3777cef5090cSJed Brown if (ts->errorifstepfailed) { 37786c4ed002SBarry 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]); 37796c4ed002SBarry 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]); 37806c4ed002SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3781cef5090cSJed Brown } 3782362cd11cSLisandro Dalcin } else if (!ts->reason) { 378373b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3784362cd11cSLisandro Dalcin } 3785d763cef2SBarry Smith PetscFunctionReturn(0); 3786d763cef2SBarry Smith } 3787d763cef2SBarry Smith 3788d763cef2SBarry Smith #undef __FUNCT__ 37897cbde773SLisandro Dalcin #define __FUNCT__ "TSEvaluateWLTE" 37907cbde773SLisandro Dalcin /*@ 37917cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 37927cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 37937cbde773SLisandro Dalcin 37947cbde773SLisandro Dalcin Collective on TS 37957cbde773SLisandro Dalcin 37967cbde773SLisandro Dalcin Input Arguments: 37977cbde773SLisandro Dalcin + ts - time stepping context 37987cbde773SLisandro Dalcin . wnormtype - norm type, either NORM_2 or NORM_INFINITY 37997cbde773SLisandro Dalcin - order - optional, desired order for the error evaluation or PETSC_DECIDE 38007cbde773SLisandro Dalcin 38017cbde773SLisandro Dalcin Output Arguments: 38027cbde773SLisandro Dalcin + order - optional, the actual order of the error evaluation 38037cbde773SLisandro Dalcin - wlte - the weighted local truncation error norm 38047cbde773SLisandro Dalcin 38057cbde773SLisandro Dalcin Level: advanced 38067cbde773SLisandro Dalcin 38077cbde773SLisandro Dalcin Notes: 38087cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 38097cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 38107cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 38117cbde773SLisandro Dalcin 38127cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm() 38137cbde773SLisandro Dalcin @*/ 38147cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte) 38157cbde773SLisandro Dalcin { 38167cbde773SLisandro Dalcin PetscErrorCode ierr; 38177cbde773SLisandro Dalcin 38187cbde773SLisandro Dalcin PetscFunctionBegin; 38197cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 38207cbde773SLisandro Dalcin PetscValidType(ts,1); 38217cbde773SLisandro Dalcin PetscValidLogicalCollectiveEnum(ts,wnormtype,4); 38227cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order,3); 38237cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts,*order,3); 38247cbde773SLisandro Dalcin PetscValidRealPointer(wlte,4); 38257cbde773SLisandro Dalcin if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 38267cbde773SLisandro Dalcin if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name); 38277cbde773SLisandro Dalcin ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr); 38287cbde773SLisandro Dalcin PetscFunctionReturn(0); 38297cbde773SLisandro Dalcin } 38307cbde773SLisandro Dalcin 38317cbde773SLisandro Dalcin #undef __FUNCT__ 383205175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 383305175c85SJed Brown /*@ 383405175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 383505175c85SJed Brown 38361c3436cfSJed Brown Collective on TS 383705175c85SJed Brown 383805175c85SJed Brown Input Arguments: 38391c3436cfSJed Brown + ts - time stepping context 38401c3436cfSJed Brown . order - desired order of accuracy 38410298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 384205175c85SJed Brown 384305175c85SJed Brown Output Arguments: 38440910c330SBarry Smith . U - state at the end of the current step 384505175c85SJed Brown 384605175c85SJed Brown Level: advanced 384705175c85SJed Brown 3848108c343cSJed Brown Notes: 3849108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3850108c343cSJed 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. 3851108c343cSJed Brown 38521c3436cfSJed Brown .seealso: TSStep(), TSAdapt 385305175c85SJed Brown @*/ 38540910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 385505175c85SJed Brown { 385605175c85SJed Brown PetscErrorCode ierr; 385705175c85SJed Brown 385805175c85SJed Brown PetscFunctionBegin; 385905175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 386005175c85SJed Brown PetscValidType(ts,1); 38610910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3862ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 38630910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 386405175c85SJed Brown PetscFunctionReturn(0); 386505175c85SJed Brown } 386605175c85SJed Brown 3867b1cb36f3SHong Zhang #undef __FUNCT__ 3868b1cb36f3SHong Zhang #define __FUNCT__ "TSForwardCostIntegral" 3869b1cb36f3SHong Zhang /*@ 3870b1cb36f3SHong Zhang TSForwardCostIntegral - Evaluate the cost integral in the forward run. 3871b1cb36f3SHong Zhang 3872b1cb36f3SHong Zhang Collective on TS 3873b1cb36f3SHong Zhang 3874b1cb36f3SHong Zhang Input Arguments: 3875b1cb36f3SHong Zhang . ts - time stepping context 3876b1cb36f3SHong Zhang 3877b1cb36f3SHong Zhang Level: advanced 3878b1cb36f3SHong Zhang 3879b1cb36f3SHong Zhang Notes: 3880b1cb36f3SHong Zhang This function cannot be called until TSStep() has been completed. 3881b1cb36f3SHong Zhang 3882b1cb36f3SHong Zhang .seealso: TSSolve(), TSAdjointCostIntegral() 3883b1cb36f3SHong Zhang @*/ 3884b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts) 3885b1cb36f3SHong Zhang { 3886b1cb36f3SHong Zhang PetscErrorCode ierr; 3887b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3888b1cb36f3SHong 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); 3889b1cb36f3SHong Zhang ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr); 3890b1cb36f3SHong Zhang PetscFunctionReturn(0); 3891b1cb36f3SHong Zhang } 3892d67e68b7SBarry Smith 389305175c85SJed Brown #undef __FUNCT__ 3894d763cef2SBarry Smith #define __FUNCT__ "TSSolve" 3895d763cef2SBarry Smith /*@ 3896d763cef2SBarry Smith TSSolve - Steps the requested number of timesteps. 3897d763cef2SBarry Smith 3898d763cef2SBarry Smith Collective on TS 3899d763cef2SBarry Smith 3900d763cef2SBarry Smith Input Parameter: 3901d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 390263e21af5SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used, 390363e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 39045a3a76d0SJed Brown 3905d763cef2SBarry Smith Level: beginner 3906d763cef2SBarry Smith 39075a3a76d0SJed Brown Notes: 39085a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 39095a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 39105a3a76d0SJed Brown stepped over the final time. 39115a3a76d0SJed Brown 3912d763cef2SBarry Smith .keywords: TS, timestep, solve 3913d763cef2SBarry Smith 391463e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime() 3915d763cef2SBarry Smith @*/ 3916cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 3917d763cef2SBarry Smith { 3918b06615a5SLisandro Dalcin Vec solution; 3919d763cef2SBarry Smith PetscErrorCode ierr; 3920d763cef2SBarry Smith 3921d763cef2SBarry Smith PetscFunctionBegin; 3922d763cef2SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3923f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 3924feed9e9dSBarry Smith 392549354f04SShri 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 */ 3926b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 39270910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 3928b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3929b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3930b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 39315a3a76d0SJed Brown } 39320910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3933bbd56ea5SKarl Rupp } else if (u) { 39340910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 39355a3a76d0SJed Brown } 3936b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 393768bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3938a6772fa2SLisandro Dalcin 3939a6772fa2SLisandro 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()"); 3940a6772fa2SLisandro 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"); 3941a6772fa2SLisandro Dalcin 3942d763cef2SBarry Smith /* reset time step and iteration counters */ 3943193ac0bcSJed Brown ts->steps = 0; 39445ef26d82SJed Brown ts->ksp_its = 0; 39455ef26d82SJed Brown ts->snes_its = 0; 3946c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3947c610991cSLisandro Dalcin ts->reject = 0; 3948193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3949193ac0bcSJed Brown 3950ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3951f05ece33SBarry Smith 3952193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3953193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 3954a6772fa2SLisandro Dalcin if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3955cc708dedSBarry Smith ts->solvetime = ts->ptime; 3956a6772fa2SLisandro Dalcin solution = ts->vec_sol; 3957be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 3958db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3959db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3960cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39616427ac75SLisandro Dalcin ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 396228d5b5d6SLisandro Dalcin ts->steprollback = PETSC_FALSE; 396328d5b5d6SLisandro Dalcin ts->steprestart = PETSC_TRUE; 39646427ac75SLisandro Dalcin 3965e1a7a14fSJed Brown while (!ts->reason) { 3966193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39679687d888SLisandro Dalcin if (!ts->steprollback) { 39689687d888SLisandro Dalcin ierr = TSPreStep(ts);CHKERRQ(ierr); 39699687d888SLisandro Dalcin } 3970193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 3971d61f013cSLisandro Dalcin ierr = TSEventHandler(ts);CHKERRQ(ierr); 3972d61f013cSLisandro Dalcin if (!ts->steprollback) { 39739687d888SLisandro Dalcin if (ts->vec_costintegral && ts->costintegralfwd) { 3974b1cb36f3SHong Zhang ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr); 3975b1cb36f3SHong Zhang } 3976cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39771eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 3978aeb4809dSShri Abhyankar } 3979193ac0bcSJed Brown } 398063e21af5SBarry Smith ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39816427ac75SLisandro Dalcin 398249354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 39830910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 3984cc708dedSBarry Smith ts->solvetime = ts->max_time; 3985b06615a5SLisandro Dalcin solution = u; 398663e21af5SBarry Smith ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr); 39870574a7fbSJed Brown } else { 3988ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3989cc708dedSBarry Smith ts->solvetime = ts->ptime; 3990b06615a5SLisandro Dalcin solution = ts->vec_sol; 39910574a7fbSJed Brown } 3992193ac0bcSJed Brown } 3993d2daff3dSHong Zhang 3994ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 399563e21af5SBarry Smith ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr); 399656f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 3997ef222394SBarry Smith if (ts->adjoint_solve) { 3998ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 39992b0a91c0SBarry Smith } 4000d763cef2SBarry Smith PetscFunctionReturn(0); 4001d763cef2SBarry Smith } 4002d763cef2SBarry Smith 4003d763cef2SBarry Smith #undef __FUNCT__ 4004b1cb36f3SHong Zhang #define __FUNCT__ "TSAdjointCostIntegral" 4005b1cb36f3SHong Zhang /*@ 4006b1cb36f3SHong Zhang TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run. 4007b1cb36f3SHong Zhang 4008b1cb36f3SHong Zhang Collective on TS 4009b1cb36f3SHong Zhang 4010b1cb36f3SHong Zhang Input Arguments: 4011b1cb36f3SHong Zhang . ts - time stepping context 4012b1cb36f3SHong Zhang 4013b1cb36f3SHong Zhang Level: advanced 4014b1cb36f3SHong Zhang 4015b1cb36f3SHong Zhang Notes: 4016b1cb36f3SHong Zhang This function cannot be called until TSAdjointStep() has been completed. 4017b1cb36f3SHong Zhang 4018b1cb36f3SHong Zhang .seealso: TSAdjointSolve(), TSAdjointStep 4019b1cb36f3SHong Zhang @*/ 4020b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts) 4021b1cb36f3SHong Zhang { 4022b1cb36f3SHong Zhang PetscErrorCode ierr; 4023b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4024b1cb36f3SHong 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); 4025b1cb36f3SHong Zhang ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr); 4026b1cb36f3SHong Zhang PetscFunctionReturn(0); 4027b1cb36f3SHong Zhang } 4028b1cb36f3SHong Zhang 4029b1cb36f3SHong Zhang #undef __FUNCT__ 4030c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve" 4031c88f2d44SBarry Smith /*@ 4032c88f2d44SBarry Smith TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 4033c88f2d44SBarry Smith 4034c88f2d44SBarry Smith Collective on TS 4035c88f2d44SBarry Smith 4036c88f2d44SBarry Smith Input Parameter: 40372c39e106SBarry Smith . ts - the TS context obtained from TSCreate() 4038c88f2d44SBarry Smith 4039e87fd094SBarry Smith Options Database: 4040e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions 4041e87fd094SBarry Smith 4042c88f2d44SBarry Smith Level: intermediate 4043c88f2d44SBarry Smith 4044c88f2d44SBarry Smith Notes: 4045c88f2d44SBarry Smith This must be called after a call to TSSolve() that solves the forward problem 4046c88f2d44SBarry Smith 404773b18844SBarry Smith By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time 404873b18844SBarry Smith 4049c88f2d44SBarry Smith .keywords: TS, timestep, solve 4050c88f2d44SBarry Smith 4051b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep() 4052c88f2d44SBarry Smith @*/ 40532c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts) 4054c88f2d44SBarry Smith { 4055c88f2d44SBarry Smith PetscErrorCode ierr; 4056c88f2d44SBarry Smith 4057c88f2d44SBarry Smith PetscFunctionBegin; 4058c88f2d44SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4059c88f2d44SBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 406068bece0bSHong Zhang 4061c88f2d44SBarry Smith /* reset time step and iteration counters */ 4062c88f2d44SBarry Smith ts->steps = 0; 4063c88f2d44SBarry Smith ts->ksp_its = 0; 4064c88f2d44SBarry Smith ts->snes_its = 0; 4065c88f2d44SBarry Smith ts->num_snes_failures = 0; 4066c88f2d44SBarry Smith ts->reject = 0; 4067c88f2d44SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 4068c88f2d44SBarry Smith 406973b18844SBarry Smith if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps; 4070c88f2d44SBarry Smith 407173b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 4072c88f2d44SBarry Smith while (!ts->reason) { 407355c52731SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 40749110b2e7SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 40756427ac75SLisandro Dalcin ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr); 4076616946c1SHong Zhang ierr = TSAdjointStep(ts);CHKERRQ(ierr); 4077b1cb36f3SHong Zhang if (ts->vec_costintegral && !ts->costintegralfwd) { 4078b1cb36f3SHong Zhang ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr); 4079b1cb36f3SHong Zhang } 4080c88f2d44SBarry Smith } 408126656371SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 408226656371SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 4083c88f2d44SBarry Smith ts->solvetime = ts->ptime; 4084e210cd0eSHong Zhang ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr); 4085685405a1SBarry Smith ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr); 4086c88f2d44SBarry Smith PetscFunctionReturn(0); 4087c88f2d44SBarry Smith } 4088c88f2d44SBarry Smith 4089c88f2d44SBarry Smith #undef __FUNCT__ 4090d763cef2SBarry Smith #define __FUNCT__ "TSMonitor" 40917db568b7SBarry Smith /*@C 4092228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 4093228d79bcSJed Brown 4094228d79bcSJed Brown Collective on TS 4095228d79bcSJed Brown 4096228d79bcSJed Brown Input Parameters: 4097228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 4098228d79bcSJed Brown . step - step number that has just completed 4099228d79bcSJed Brown . ptime - model time of the state 41000910c330SBarry Smith - u - state at the current model time 4101228d79bcSJed Brown 4102228d79bcSJed Brown Notes: 41037db568b7SBarry Smith TSMonitor() is typically used automatically within the time stepping implementations. 41047db568b7SBarry Smith Users would almost never call this routine directly. 4105228d79bcSJed Brown 410663e21af5SBarry Smith A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions 410763e21af5SBarry Smith 41087db568b7SBarry Smith Level: developer 4109228d79bcSJed Brown 4110228d79bcSJed Brown .keywords: TS, timestep 4111228d79bcSJed Brown @*/ 41120910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 4113d763cef2SBarry Smith { 4114d6152f81SLisandro Dalcin DM dm; 4115a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 4116d6152f81SLisandro Dalcin PetscErrorCode ierr; 4117d763cef2SBarry Smith 4118d763cef2SBarry Smith PetscFunctionBegin; 4119b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4120b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4121d6152f81SLisandro Dalcin 4122d6152f81SLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 4123d6152f81SLisandro Dalcin ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr); 4124d6152f81SLisandro Dalcin 41255edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 4126d763cef2SBarry Smith for (i=0; i<n; i++) { 41270910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 4128d763cef2SBarry Smith } 41295edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 4130d763cef2SBarry Smith PetscFunctionReturn(0); 4131d763cef2SBarry Smith } 4132d763cef2SBarry Smith 41339110b2e7SHong Zhang #undef __FUNCT__ 41349110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitor" 41357db568b7SBarry Smith /*@C 41369110b2e7SHong Zhang TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet() 41379110b2e7SHong Zhang 41389110b2e7SHong Zhang Collective on TS 41399110b2e7SHong Zhang 41409110b2e7SHong Zhang Input Parameters: 41419110b2e7SHong Zhang + ts - time stepping context obtained from TSCreate() 41429110b2e7SHong Zhang . step - step number that has just completed 41439110b2e7SHong Zhang . ptime - model time of the state 41449110b2e7SHong Zhang . u - state at the current model time 41459110b2e7SHong Zhang . numcost - number of cost functions (dimension of lambda or mu) 41469110b2e7SHong Zhang . lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 41479110b2e7SHong Zhang - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 41489110b2e7SHong Zhang 41499110b2e7SHong Zhang Notes: 41507db568b7SBarry Smith TSAdjointMonitor() is typically used automatically within the time stepping implementations. 41517db568b7SBarry Smith Users would almost never call this routine directly. 41529110b2e7SHong Zhang 41537db568b7SBarry Smith Level: developer 41549110b2e7SHong Zhang 41559110b2e7SHong Zhang .keywords: TS, timestep 41569110b2e7SHong Zhang @*/ 41579110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu) 41589110b2e7SHong Zhang { 41599110b2e7SHong Zhang PetscErrorCode ierr; 41609110b2e7SHong Zhang PetscInt i,n = ts->numberadjointmonitors; 41619110b2e7SHong Zhang 41629110b2e7SHong Zhang PetscFunctionBegin; 41639110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 41649110b2e7SHong Zhang PetscValidHeaderSpecific(u,VEC_CLASSID,4); 41659110b2e7SHong Zhang ierr = VecLockPush(u);CHKERRQ(ierr); 41669110b2e7SHong Zhang for (i=0; i<n; i++) { 41679110b2e7SHong Zhang ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 41689110b2e7SHong Zhang } 41699110b2e7SHong Zhang ierr = VecLockPop(u);CHKERRQ(ierr); 41709110b2e7SHong Zhang PetscFunctionReturn(0); 41719110b2e7SHong Zhang } 41729110b2e7SHong Zhang 4173d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 41744a2ae208SSatish Balay #undef __FUNCT__ 4175a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 4176d763cef2SBarry Smith /*@C 41777db568b7SBarry Smith TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 4178a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 4179d763cef2SBarry Smith 4180d763cef2SBarry Smith Collective on TS 4181d763cef2SBarry Smith 4182d763cef2SBarry Smith Input Parameters: 4183d763cef2SBarry Smith + host - the X display to open, or null for the local machine 4184d763cef2SBarry Smith . label - the title to put in the title bar 41857c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 4186a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 4187a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 4188d763cef2SBarry Smith 4189d763cef2SBarry Smith Output Parameter: 41900b039ecaSBarry Smith . ctx - the context 4191d763cef2SBarry Smith 4192d763cef2SBarry Smith Options Database Key: 41934f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 41947db568b7SBarry Smith . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 41957db568b7SBarry Smith . -ts_monitor_lg_error - monitor the error 41967db568b7SBarry Smith . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 41977db568b7SBarry Smith . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 4198b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 4199d763cef2SBarry Smith 4200d763cef2SBarry Smith Notes: 4201a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 4202d763cef2SBarry Smith 42037db568b7SBarry Smith One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 42047db568b7SBarry Smith 42057db568b7SBarry 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 42067db568b7SBarry 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 42077db568b7SBarry Smith as the first argument. 42087db568b7SBarry Smith 42097db568b7SBarry Smith One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 42107db568b7SBarry Smith 42117db568b7SBarry Smith 4212d763cef2SBarry Smith Level: intermediate 4213d763cef2SBarry Smith 42147db568b7SBarry Smith .keywords: TS, monitor, line graph, residual 4215d763cef2SBarry Smith 42167db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 42177db568b7SBarry Smith TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 42187db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 42197db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 42207db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 42217c922b88SBarry Smith 4222d763cef2SBarry Smith @*/ 4223a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 4224d763cef2SBarry Smith { 42257f52daa2SLisandro Dalcin PetscDraw draw; 4226dfbe8321SBarry Smith PetscErrorCode ierr; 4227d763cef2SBarry Smith 4228d763cef2SBarry Smith PetscFunctionBegin; 4229b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 42307f52daa2SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 42317f52daa2SLisandro Dalcin ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 42327f52daa2SLisandro Dalcin ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 4233287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 42347f52daa2SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 4235a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 4236d763cef2SBarry Smith PetscFunctionReturn(0); 4237d763cef2SBarry Smith } 4238d763cef2SBarry Smith 42394a2ae208SSatish Balay #undef __FUNCT__ 42404f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 4241b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 4242d763cef2SBarry Smith { 42430b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4244c32365f1SBarry Smith PetscReal x = ptime,y; 4245dfbe8321SBarry Smith PetscErrorCode ierr; 4246d763cef2SBarry Smith 4247d763cef2SBarry Smith PetscFunctionBegin; 424863e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */ 4249b06615a5SLisandro Dalcin if (!step) { 4250a9f9c1f6SBarry Smith PetscDrawAxis axis; 4251a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 42526934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr); 4253a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4254a9f9c1f6SBarry Smith } 4255c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 42560b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4257b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 42580b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 42596934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 42603923b477SBarry Smith } 4261d763cef2SBarry Smith PetscFunctionReturn(0); 4262d763cef2SBarry Smith } 4263d763cef2SBarry Smith 42644a2ae208SSatish Balay #undef __FUNCT__ 4265a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 4266d763cef2SBarry Smith /*@C 4267a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 4268a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 4269d763cef2SBarry Smith 42700b039ecaSBarry Smith Collective on TSMonitorLGCtx 4271d763cef2SBarry Smith 4272d763cef2SBarry Smith Input Parameter: 42730b039ecaSBarry Smith . ctx - the monitor context 4274d763cef2SBarry Smith 4275d763cef2SBarry Smith Level: intermediate 4276d763cef2SBarry Smith 4277d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 4278d763cef2SBarry Smith 42794f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 4280d763cef2SBarry Smith @*/ 4281a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 4282d763cef2SBarry Smith { 4283dfbe8321SBarry Smith PetscErrorCode ierr; 4284d763cef2SBarry Smith 4285d763cef2SBarry Smith PetscFunctionBegin; 42867684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 42877684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 42887684fa3eSBarry Smith } 42890b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 429031152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 4291387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 4292387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 4293387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 42940b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 4295d763cef2SBarry Smith PetscFunctionReturn(0); 4296d763cef2SBarry Smith } 4297d763cef2SBarry Smith 42984a2ae208SSatish Balay #undef __FUNCT__ 42994a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 4300d763cef2SBarry Smith /*@ 4301b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4302d763cef2SBarry Smith 4303d763cef2SBarry Smith Not Collective 4304d763cef2SBarry Smith 4305d763cef2SBarry Smith Input Parameter: 4306d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 4307d763cef2SBarry Smith 4308d763cef2SBarry Smith Output Parameter: 430963e21af5SBarry Smith . t - the current time. This time may not corresponds to the final time set with TSSetDuration(), use TSGetSolveTime(). 4310d763cef2SBarry Smith 4311d763cef2SBarry Smith Level: beginner 4312d763cef2SBarry Smith 4313b8123daeSJed Brown Note: 4314b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 43159be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 4316b8123daeSJed Brown 431763e21af5SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep(), TSGetSolveTime() 4318d763cef2SBarry Smith 4319d763cef2SBarry Smith .keywords: TS, get, time 4320d763cef2SBarry Smith @*/ 43217087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 4322d763cef2SBarry Smith { 4323d763cef2SBarry Smith PetscFunctionBegin; 43240700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4325f7cf8827SBarry Smith PetscValidRealPointer(t,2); 4326d763cef2SBarry Smith *t = ts->ptime; 4327d763cef2SBarry Smith PetscFunctionReturn(0); 4328d763cef2SBarry Smith } 4329d763cef2SBarry Smith 43304a2ae208SSatish Balay #undef __FUNCT__ 4331e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime" 4332e5e524a1SHong Zhang /*@ 4333e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4334e5e524a1SHong Zhang 4335e5e524a1SHong Zhang Not Collective 4336e5e524a1SHong Zhang 4337e5e524a1SHong Zhang Input Parameter: 4338e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 4339e5e524a1SHong Zhang 4340e5e524a1SHong Zhang Output Parameter: 4341e5e524a1SHong Zhang . t - the previous time 4342e5e524a1SHong Zhang 4343e5e524a1SHong Zhang Level: beginner 4344e5e524a1SHong Zhang 4345e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 4346e5e524a1SHong Zhang 4347e5e524a1SHong Zhang .keywords: TS, get, time 4348e5e524a1SHong Zhang @*/ 4349e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 4350e5e524a1SHong Zhang { 4351e5e524a1SHong Zhang PetscFunctionBegin; 4352e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4353e5e524a1SHong Zhang PetscValidRealPointer(t,2); 4354e5e524a1SHong Zhang *t = ts->ptime_prev; 4355e5e524a1SHong Zhang PetscFunctionReturn(0); 4356e5e524a1SHong Zhang } 4357e5e524a1SHong Zhang 4358e5e524a1SHong Zhang #undef __FUNCT__ 43596a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 43606a4d4014SLisandro Dalcin /*@ 43616a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 43626a4d4014SLisandro Dalcin 43633f9fe445SBarry Smith Logically Collective on TS 43646a4d4014SLisandro Dalcin 43656a4d4014SLisandro Dalcin Input Parameters: 43666a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 43676a4d4014SLisandro Dalcin - time - the time 43686a4d4014SLisandro Dalcin 43696a4d4014SLisandro Dalcin Level: intermediate 43706a4d4014SLisandro Dalcin 43716a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 43726a4d4014SLisandro Dalcin 43736a4d4014SLisandro Dalcin .keywords: TS, set, time 43746a4d4014SLisandro Dalcin @*/ 43757087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 43766a4d4014SLisandro Dalcin { 43776a4d4014SLisandro Dalcin PetscFunctionBegin; 43780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4379c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 43806a4d4014SLisandro Dalcin ts->ptime = t; 43816a4d4014SLisandro Dalcin PetscFunctionReturn(0); 43826a4d4014SLisandro Dalcin } 43836a4d4014SLisandro Dalcin 43846a4d4014SLisandro Dalcin #undef __FUNCT__ 43854a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 4386d763cef2SBarry Smith /*@C 4387d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4388d763cef2SBarry Smith TS options in the database. 4389d763cef2SBarry Smith 43903f9fe445SBarry Smith Logically Collective on TS 4391d763cef2SBarry Smith 4392d763cef2SBarry Smith Input Parameter: 4393d763cef2SBarry Smith + ts - The TS context 4394d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4395d763cef2SBarry Smith 4396d763cef2SBarry Smith Notes: 4397d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4398d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4399d763cef2SBarry Smith hyphen. 4400d763cef2SBarry Smith 4401d763cef2SBarry Smith Level: advanced 4402d763cef2SBarry Smith 4403d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 4404d763cef2SBarry Smith 4405d763cef2SBarry Smith .seealso: TSSetFromOptions() 4406d763cef2SBarry Smith 4407d763cef2SBarry Smith @*/ 44087087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 4409d763cef2SBarry Smith { 4410dfbe8321SBarry Smith PetscErrorCode ierr; 4411089b2837SJed Brown SNES snes; 4412d763cef2SBarry Smith 4413d763cef2SBarry Smith PetscFunctionBegin; 44140700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4415d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4416089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4417089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4418d763cef2SBarry Smith PetscFunctionReturn(0); 4419d763cef2SBarry Smith } 4420d763cef2SBarry Smith 4421d763cef2SBarry Smith 44224a2ae208SSatish Balay #undef __FUNCT__ 44234a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 4424d763cef2SBarry Smith /*@C 4425d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4426d763cef2SBarry Smith TS options in the database. 4427d763cef2SBarry Smith 44283f9fe445SBarry Smith Logically Collective on TS 4429d763cef2SBarry Smith 4430d763cef2SBarry Smith Input Parameter: 4431d763cef2SBarry Smith + ts - The TS context 4432d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4433d763cef2SBarry Smith 4434d763cef2SBarry Smith Notes: 4435d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4436d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4437d763cef2SBarry Smith hyphen. 4438d763cef2SBarry Smith 4439d763cef2SBarry Smith Level: advanced 4440d763cef2SBarry Smith 4441d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 4442d763cef2SBarry Smith 4443d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 4444d763cef2SBarry Smith 4445d763cef2SBarry Smith @*/ 44467087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4447d763cef2SBarry Smith { 4448dfbe8321SBarry Smith PetscErrorCode ierr; 4449089b2837SJed Brown SNES snes; 4450d763cef2SBarry Smith 4451d763cef2SBarry Smith PetscFunctionBegin; 44520700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4453d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4454089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4455089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4456d763cef2SBarry Smith PetscFunctionReturn(0); 4457d763cef2SBarry Smith } 4458d763cef2SBarry Smith 44594a2ae208SSatish Balay #undef __FUNCT__ 44604a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 4461d763cef2SBarry Smith /*@C 4462d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4463d763cef2SBarry Smith TS options in the database. 4464d763cef2SBarry Smith 4465d763cef2SBarry Smith Not Collective 4466d763cef2SBarry Smith 4467d763cef2SBarry Smith Input Parameter: 4468d763cef2SBarry Smith . ts - The TS context 4469d763cef2SBarry Smith 4470d763cef2SBarry Smith Output Parameter: 4471d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4472d763cef2SBarry Smith 4473d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 4474d763cef2SBarry Smith sufficient length to hold the prefix. 4475d763cef2SBarry Smith 4476d763cef2SBarry Smith Level: intermediate 4477d763cef2SBarry Smith 4478d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 4479d763cef2SBarry Smith 4480d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 4481d763cef2SBarry Smith @*/ 44827087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4483d763cef2SBarry Smith { 4484dfbe8321SBarry Smith PetscErrorCode ierr; 4485d763cef2SBarry Smith 4486d763cef2SBarry Smith PetscFunctionBegin; 44870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 44884482741eSBarry Smith PetscValidPointer(prefix,2); 4489d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4490d763cef2SBarry Smith PetscFunctionReturn(0); 4491d763cef2SBarry Smith } 4492d763cef2SBarry Smith 44934a2ae208SSatish Balay #undef __FUNCT__ 44944a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 4495d763cef2SBarry Smith /*@C 4496d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4497d763cef2SBarry Smith 4498d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4499d763cef2SBarry Smith 4500d763cef2SBarry Smith Input Parameter: 4501d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4502d763cef2SBarry Smith 4503d763cef2SBarry Smith Output Parameters: 4504e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4505e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4506e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4507e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4508d763cef2SBarry Smith 45090298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 4510d763cef2SBarry Smith 4511d763cef2SBarry Smith Level: intermediate 4512d763cef2SBarry Smith 451326d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 4514d763cef2SBarry Smith 4515d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 4516d763cef2SBarry Smith @*/ 4517e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4518d763cef2SBarry Smith { 4519089b2837SJed Brown PetscErrorCode ierr; 4520089b2837SJed Brown SNES snes; 452124989b8cSPeter Brune DM dm; 4522089b2837SJed Brown 4523d763cef2SBarry Smith PetscFunctionBegin; 4524089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4525e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 452624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 452724989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 4528d763cef2SBarry Smith PetscFunctionReturn(0); 4529d763cef2SBarry Smith } 4530d763cef2SBarry Smith 45311713a123SBarry Smith #undef __FUNCT__ 45322eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 45332eca1d9cSJed Brown /*@C 45342eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 45352eca1d9cSJed Brown 45362eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 45372eca1d9cSJed Brown 45382eca1d9cSJed Brown Input Parameter: 45392eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 45402eca1d9cSJed Brown 45412eca1d9cSJed Brown Output Parameters: 4542e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4543e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 45442eca1d9cSJed Brown . f - The function to compute the matrices 45452eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 45462eca1d9cSJed Brown 45470298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 45482eca1d9cSJed Brown 45492eca1d9cSJed Brown Level: advanced 45502eca1d9cSJed Brown 45512eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 45522eca1d9cSJed Brown 45532eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 45542eca1d9cSJed Brown @*/ 4555e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 45562eca1d9cSJed Brown { 4557089b2837SJed Brown PetscErrorCode ierr; 4558089b2837SJed Brown SNES snes; 455924989b8cSPeter Brune DM dm; 45600910c330SBarry Smith 45612eca1d9cSJed Brown PetscFunctionBegin; 4562089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4563f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4564e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 456524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 456624989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 45672eca1d9cSJed Brown PetscFunctionReturn(0); 45682eca1d9cSJed Brown } 45692eca1d9cSJed Brown 45706083293cSBarry Smith 45712eca1d9cSJed Brown #undef __FUNCT__ 457283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 45731713a123SBarry Smith /*@C 457483a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 45751713a123SBarry Smith VecView() for the solution at each timestep 45761713a123SBarry Smith 45771713a123SBarry Smith Collective on TS 45781713a123SBarry Smith 45791713a123SBarry Smith Input Parameters: 45801713a123SBarry Smith + ts - the TS context 45811713a123SBarry Smith . step - current time-step 4582142b95e3SSatish Balay . ptime - current time 45830298fd71SBarry Smith - dummy - either a viewer or NULL 45841713a123SBarry Smith 458599fdda47SBarry Smith Options Database: 458699fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 458799fdda47SBarry Smith 4588387f4636SBarry 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 458999fdda47SBarry Smith will look bad 459099fdda47SBarry Smith 45911713a123SBarry Smith Level: intermediate 45921713a123SBarry Smith 45931713a123SBarry Smith .keywords: TS, vector, monitor, view 45941713a123SBarry Smith 4595a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 45961713a123SBarry Smith @*/ 45970910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 45981713a123SBarry Smith { 4599dfbe8321SBarry Smith PetscErrorCode ierr; 460083a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4601473a3ab2SBarry Smith PetscDraw draw; 46021713a123SBarry Smith 46031713a123SBarry Smith PetscFunctionBegin; 46046083293cSBarry Smith if (!step && ictx->showinitial) { 46056083293cSBarry Smith if (!ictx->initialsolution) { 46060910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 46071713a123SBarry Smith } 46080910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 46096083293cSBarry Smith } 4610b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 46110dcf80beSBarry Smith 46126083293cSBarry Smith if (ictx->showinitial) { 46136083293cSBarry Smith PetscReal pause; 46146083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 46156083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 46166083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 46176083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 46186083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 46196083293cSBarry Smith } 46200910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 4621473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 462251fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 4623473a3ab2SBarry Smith char time[32]; 4624473a3ab2SBarry Smith 4625473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 462685b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4627473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4628473a3ab2SBarry Smith h = yl + .95*(yr - yl); 462951fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4630473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4631473a3ab2SBarry Smith } 4632473a3ab2SBarry Smith 46336083293cSBarry Smith if (ictx->showinitial) { 46346083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 46356083293cSBarry Smith } 46361713a123SBarry Smith PetscFunctionReturn(0); 46371713a123SBarry Smith } 46381713a123SBarry Smith 46392d5ee99bSBarry Smith #undef __FUNCT__ 46409110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorDrawSensi" 46419110b2e7SHong Zhang /*@C 46429110b2e7SHong Zhang TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling 46439110b2e7SHong Zhang VecView() for the sensitivities to initial states at each timestep 46449110b2e7SHong Zhang 46459110b2e7SHong Zhang Collective on TS 46469110b2e7SHong Zhang 46479110b2e7SHong Zhang Input Parameters: 46489110b2e7SHong Zhang + ts - the TS context 46499110b2e7SHong Zhang . step - current time-step 46509110b2e7SHong Zhang . ptime - current time 46519110b2e7SHong Zhang . u - current state 46529110b2e7SHong Zhang . numcost - number of cost functions 46539110b2e7SHong Zhang . lambda - sensitivities to initial conditions 46549110b2e7SHong Zhang . mu - sensitivities to parameters 46559110b2e7SHong Zhang - dummy - either a viewer or NULL 46569110b2e7SHong Zhang 46579110b2e7SHong Zhang Level: intermediate 46589110b2e7SHong Zhang 46599110b2e7SHong Zhang .keywords: TS, vector, adjoint, monitor, view 46609110b2e7SHong Zhang 46619110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView() 46629110b2e7SHong Zhang @*/ 46639110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 46649110b2e7SHong Zhang { 46659110b2e7SHong Zhang PetscErrorCode ierr; 46669110b2e7SHong Zhang TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 46679110b2e7SHong Zhang PetscDraw draw; 4668a0046e2cSSatish Balay PetscReal xl,yl,xr,yr,h; 4669a0046e2cSSatish Balay char time[32]; 46709110b2e7SHong Zhang 46719110b2e7SHong Zhang PetscFunctionBegin; 46729110b2e7SHong Zhang if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 46739110b2e7SHong Zhang 46749110b2e7SHong Zhang ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr); 46759110b2e7SHong Zhang ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 46769110b2e7SHong Zhang ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 46779110b2e7SHong Zhang ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 46789110b2e7SHong Zhang h = yl + .95*(yr - yl); 46799110b2e7SHong Zhang ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 46809110b2e7SHong Zhang ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 46819110b2e7SHong Zhang PetscFunctionReturn(0); 46829110b2e7SHong Zhang } 46839110b2e7SHong Zhang 46849110b2e7SHong Zhang #undef __FUNCT__ 46852d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase" 46862d5ee99bSBarry Smith /*@C 46872d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 46882d5ee99bSBarry Smith 46892d5ee99bSBarry Smith Collective on TS 46902d5ee99bSBarry Smith 46912d5ee99bSBarry Smith Input Parameters: 46922d5ee99bSBarry Smith + ts - the TS context 46932d5ee99bSBarry Smith . step - current time-step 46942d5ee99bSBarry Smith . ptime - current time 46952d5ee99bSBarry Smith - dummy - either a viewer or NULL 46962d5ee99bSBarry Smith 46972d5ee99bSBarry Smith Level: intermediate 46982d5ee99bSBarry Smith 46992d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 47002d5ee99bSBarry Smith 47012d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 47022d5ee99bSBarry Smith @*/ 47032d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 47042d5ee99bSBarry Smith { 47052d5ee99bSBarry Smith PetscErrorCode ierr; 47062d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 47072d5ee99bSBarry Smith PetscDraw draw; 47086934998bSLisandro Dalcin PetscDrawAxis axis; 47092d5ee99bSBarry Smith PetscInt n; 47102d5ee99bSBarry Smith PetscMPIInt size; 47116934998bSLisandro Dalcin PetscReal U0,U1,xl,yl,xr,yr,h; 47122d5ee99bSBarry Smith char time[32]; 47132d5ee99bSBarry Smith const PetscScalar *U; 47142d5ee99bSBarry Smith 47152d5ee99bSBarry Smith PetscFunctionBegin; 47166934998bSLisandro Dalcin ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr); 47176934998bSLisandro Dalcin if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs"); 47182d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 47196934998bSLisandro Dalcin if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 47202d5ee99bSBarry Smith 47212d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 47226934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr); 47236934998bSLisandro Dalcin ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 47246934998bSLisandro Dalcin if (!step) { 47256934998bSLisandro Dalcin ierr = PetscDrawClear(draw);CHKERRQ(ierr); 47266934998bSLisandro Dalcin ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); 47276934998bSLisandro Dalcin } 47282d5ee99bSBarry Smith 47292d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 47306934998bSLisandro Dalcin U0 = PetscRealPart(U[0]); 47316934998bSLisandro Dalcin U1 = PetscRealPart(U[1]); 4732a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 47336934998bSLisandro Dalcin if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0); 47342d5ee99bSBarry Smith 47356934998bSLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 47366934998bSLisandro Dalcin ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr); 47372d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 47384b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 473985b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 47402d5ee99bSBarry Smith h = yl + .95*(yr - yl); 474151fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 47422d5ee99bSBarry Smith } 47436934998bSLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 47442d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 47456934998bSLisandro Dalcin ierr = PetscDrawSave(draw);CHKERRQ(ierr); 47462d5ee99bSBarry Smith PetscFunctionReturn(0); 47472d5ee99bSBarry Smith } 47482d5ee99bSBarry Smith 47491713a123SBarry Smith 47506c699258SBarry Smith #undef __FUNCT__ 475183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 47526083293cSBarry Smith /*@C 475383a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 47546083293cSBarry Smith 47556083293cSBarry Smith Collective on TS 47566083293cSBarry Smith 47576083293cSBarry Smith Input Parameters: 47586083293cSBarry Smith . ctx - the monitor context 47596083293cSBarry Smith 47606083293cSBarry Smith Level: intermediate 47616083293cSBarry Smith 47626083293cSBarry Smith .keywords: TS, vector, monitor, view 47636083293cSBarry Smith 476483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 47656083293cSBarry Smith @*/ 476683a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 47676083293cSBarry Smith { 47686083293cSBarry Smith PetscErrorCode ierr; 47696083293cSBarry Smith 47706083293cSBarry Smith PetscFunctionBegin; 477183a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 477283a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 477383a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 47746083293cSBarry Smith PetscFunctionReturn(0); 47756083293cSBarry Smith } 47766083293cSBarry Smith 47776083293cSBarry Smith #undef __FUNCT__ 477883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 47796083293cSBarry Smith /*@C 478083a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 47816083293cSBarry Smith 47826083293cSBarry Smith Collective on TS 47836083293cSBarry Smith 47846083293cSBarry Smith Input Parameter: 47856083293cSBarry Smith . ts - time-step context 47866083293cSBarry Smith 47876083293cSBarry Smith Output Patameter: 47886083293cSBarry Smith . ctx - the monitor context 47896083293cSBarry Smith 479099fdda47SBarry Smith Options Database: 479199fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 479299fdda47SBarry Smith 47936083293cSBarry Smith Level: intermediate 47946083293cSBarry Smith 47956083293cSBarry Smith .keywords: TS, vector, monitor, view 47966083293cSBarry Smith 479783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 47986083293cSBarry Smith @*/ 479983a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 48006083293cSBarry Smith { 48016083293cSBarry Smith PetscErrorCode ierr; 48026083293cSBarry Smith 48036083293cSBarry Smith PetscFunctionBegin; 4804b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 480583a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4806e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4807bbd56ea5SKarl Rupp 480883a4ac43SBarry Smith (*ctx)->howoften = howoften; 4809473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 4810c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4811473a3ab2SBarry Smith 4812473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 4813c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 48146083293cSBarry Smith PetscFunctionReturn(0); 48156083293cSBarry Smith } 48166083293cSBarry Smith 48176083293cSBarry Smith #undef __FUNCT__ 481883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 48193a471f94SBarry Smith /*@C 482083a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 48213a471f94SBarry Smith VecView() for the error at each timestep 48223a471f94SBarry Smith 48233a471f94SBarry Smith Collective on TS 48243a471f94SBarry Smith 48253a471f94SBarry Smith Input Parameters: 48263a471f94SBarry Smith + ts - the TS context 48273a471f94SBarry Smith . step - current time-step 48283a471f94SBarry Smith . ptime - current time 48290298fd71SBarry Smith - dummy - either a viewer or NULL 48303a471f94SBarry Smith 48313a471f94SBarry Smith Level: intermediate 48323a471f94SBarry Smith 48333a471f94SBarry Smith .keywords: TS, vector, monitor, view 48343a471f94SBarry Smith 48353a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 48363a471f94SBarry Smith @*/ 48370910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 48383a471f94SBarry Smith { 48393a471f94SBarry Smith PetscErrorCode ierr; 484083a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 484183a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 48423a471f94SBarry Smith Vec work; 48433a471f94SBarry Smith 48443a471f94SBarry Smith PetscFunctionBegin; 4845b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 48460910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 48473a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 48480910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 48493a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 48503a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 48513a471f94SBarry Smith PetscFunctionReturn(0); 48523a471f94SBarry Smith } 48533a471f94SBarry Smith 4854af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 48553a471f94SBarry Smith #undef __FUNCT__ 48566c699258SBarry Smith #define __FUNCT__ "TSSetDM" 48576c699258SBarry Smith /*@ 48586c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 48596c699258SBarry Smith 48603f9fe445SBarry Smith Logically Collective on TS and DM 48616c699258SBarry Smith 48626c699258SBarry Smith Input Parameters: 48636c699258SBarry Smith + ts - the preconditioner context 48646c699258SBarry Smith - dm - the dm 48656c699258SBarry Smith 48666c699258SBarry Smith Level: intermediate 48676c699258SBarry Smith 48686c699258SBarry Smith 48696c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 48706c699258SBarry Smith @*/ 48717087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 48726c699258SBarry Smith { 48736c699258SBarry Smith PetscErrorCode ierr; 4874089b2837SJed Brown SNES snes; 4875942e3340SBarry Smith DMTS tsdm; 48766c699258SBarry Smith 48776c699258SBarry Smith PetscFunctionBegin; 48780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 487970663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4880942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 48812a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4882942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4883942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 488424989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 488524989b8cSPeter Brune tsdm->originaldm = dm; 488624989b8cSPeter Brune } 488724989b8cSPeter Brune } 48886bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 488924989b8cSPeter Brune } 48906c699258SBarry Smith ts->dm = dm; 4891bbd56ea5SKarl Rupp 4892089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4893089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 48946c699258SBarry Smith PetscFunctionReturn(0); 48956c699258SBarry Smith } 48966c699258SBarry Smith 48976c699258SBarry Smith #undef __FUNCT__ 48986c699258SBarry Smith #define __FUNCT__ "TSGetDM" 48996c699258SBarry Smith /*@ 49006c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 49016c699258SBarry Smith 49023f9fe445SBarry Smith Not Collective 49036c699258SBarry Smith 49046c699258SBarry Smith Input Parameter: 49056c699258SBarry Smith . ts - the preconditioner context 49066c699258SBarry Smith 49076c699258SBarry Smith Output Parameter: 49086c699258SBarry Smith . dm - the dm 49096c699258SBarry Smith 49106c699258SBarry Smith Level: intermediate 49116c699258SBarry Smith 49126c699258SBarry Smith 49136c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 49146c699258SBarry Smith @*/ 49157087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 49166c699258SBarry Smith { 4917496e6a7aSJed Brown PetscErrorCode ierr; 4918496e6a7aSJed Brown 49196c699258SBarry Smith PetscFunctionBegin; 49200700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4921496e6a7aSJed Brown if (!ts->dm) { 4922ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4923496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4924496e6a7aSJed Brown } 49256c699258SBarry Smith *dm = ts->dm; 49266c699258SBarry Smith PetscFunctionReturn(0); 49276c699258SBarry Smith } 49281713a123SBarry Smith 49290f5c6efeSJed Brown #undef __FUNCT__ 49300f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 49310f5c6efeSJed Brown /*@ 49320f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 49330f5c6efeSJed Brown 49343f9fe445SBarry Smith Logically Collective on SNES 49350f5c6efeSJed Brown 49360f5c6efeSJed Brown Input Parameter: 4937d42a1c89SJed Brown + snes - nonlinear solver 49380910c330SBarry Smith . U - the current state at which to evaluate the residual 4939d42a1c89SJed Brown - ctx - user context, must be a TS 49400f5c6efeSJed Brown 49410f5c6efeSJed Brown Output Parameter: 49420f5c6efeSJed Brown . F - the nonlinear residual 49430f5c6efeSJed Brown 49440f5c6efeSJed Brown Notes: 49450f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 49460f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 49470f5c6efeSJed Brown 49480f5c6efeSJed Brown Level: advanced 49490f5c6efeSJed Brown 49500f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 49510f5c6efeSJed Brown @*/ 49520910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 49530f5c6efeSJed Brown { 49540f5c6efeSJed Brown TS ts = (TS)ctx; 49550f5c6efeSJed Brown PetscErrorCode ierr; 49560f5c6efeSJed Brown 49570f5c6efeSJed Brown PetscFunctionBegin; 49580f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 49590910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 49600f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 49610f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 49620910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 49630f5c6efeSJed Brown PetscFunctionReturn(0); 49640f5c6efeSJed Brown } 49650f5c6efeSJed Brown 49660f5c6efeSJed Brown #undef __FUNCT__ 49670f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 49680f5c6efeSJed Brown /*@ 49690f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 49700f5c6efeSJed Brown 49710f5c6efeSJed Brown Collective on SNES 49720f5c6efeSJed Brown 49730f5c6efeSJed Brown Input Parameter: 49740f5c6efeSJed Brown + snes - nonlinear solver 49750910c330SBarry Smith . U - the current state at which to evaluate the residual 49760f5c6efeSJed Brown - ctx - user context, must be a TS 49770f5c6efeSJed Brown 49780f5c6efeSJed Brown Output Parameter: 49790f5c6efeSJed Brown + A - the Jacobian 49800f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 49810f5c6efeSJed Brown - flag - indicates any structure change in the matrix 49820f5c6efeSJed Brown 49830f5c6efeSJed Brown Notes: 49840f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 49850f5c6efeSJed Brown 49860f5c6efeSJed Brown Level: developer 49870f5c6efeSJed Brown 49880f5c6efeSJed Brown .seealso: SNESSetJacobian() 49890f5c6efeSJed Brown @*/ 4990d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 49910f5c6efeSJed Brown { 49920f5c6efeSJed Brown TS ts = (TS)ctx; 49930f5c6efeSJed Brown PetscErrorCode ierr; 49940f5c6efeSJed Brown 49950f5c6efeSJed Brown PetscFunctionBegin; 49960f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 49970910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 49980f5c6efeSJed Brown PetscValidPointer(A,3); 499994ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 50000f5c6efeSJed Brown PetscValidPointer(B,4); 500194ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 50020f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 5003d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 50040f5c6efeSJed Brown PetscFunctionReturn(0); 50050f5c6efeSJed Brown } 5006325fc9f4SBarry Smith 50070e4ef248SJed Brown #undef __FUNCT__ 50080e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 50090e4ef248SJed Brown /*@C 50109ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 50110e4ef248SJed Brown 50120e4ef248SJed Brown Collective on TS 50130e4ef248SJed Brown 50140e4ef248SJed Brown Input Arguments: 50150e4ef248SJed Brown + ts - time stepping context 50160e4ef248SJed Brown . t - time at which to evaluate 50170910c330SBarry Smith . U - state at which to evaluate 50180e4ef248SJed Brown - ctx - context 50190e4ef248SJed Brown 50200e4ef248SJed Brown Output Arguments: 50210e4ef248SJed Brown . F - right hand side 50220e4ef248SJed Brown 50230e4ef248SJed Brown Level: intermediate 50240e4ef248SJed Brown 50250e4ef248SJed Brown Notes: 50260e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 50270e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 50280e4ef248SJed Brown 50290e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 50300e4ef248SJed Brown @*/ 50310910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 50320e4ef248SJed Brown { 50330e4ef248SJed Brown PetscErrorCode ierr; 50340e4ef248SJed Brown Mat Arhs,Brhs; 50350e4ef248SJed Brown 50360e4ef248SJed Brown PetscFunctionBegin; 50370e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 5038d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 50390910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 50400e4ef248SJed Brown PetscFunctionReturn(0); 50410e4ef248SJed Brown } 50420e4ef248SJed Brown 50430e4ef248SJed Brown #undef __FUNCT__ 50440e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 50450e4ef248SJed Brown /*@C 50460e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 50470e4ef248SJed Brown 50480e4ef248SJed Brown Collective on TS 50490e4ef248SJed Brown 50500e4ef248SJed Brown Input Arguments: 50510e4ef248SJed Brown + ts - time stepping context 50520e4ef248SJed Brown . t - time at which to evaluate 50530910c330SBarry Smith . U - state at which to evaluate 50540e4ef248SJed Brown - ctx - context 50550e4ef248SJed Brown 50560e4ef248SJed Brown Output Arguments: 50570e4ef248SJed Brown + A - pointer to operator 50580e4ef248SJed Brown . B - pointer to preconditioning matrix 50590e4ef248SJed Brown - flg - matrix structure flag 50600e4ef248SJed Brown 50610e4ef248SJed Brown Level: intermediate 50620e4ef248SJed Brown 50630e4ef248SJed Brown Notes: 50640e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 50650e4ef248SJed Brown 50660e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 50670e4ef248SJed Brown @*/ 5068d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 50690e4ef248SJed Brown { 50700e4ef248SJed Brown PetscFunctionBegin; 50710e4ef248SJed Brown PetscFunctionReturn(0); 50720e4ef248SJed Brown } 50730e4ef248SJed Brown 50740026cea9SSean Farley #undef __FUNCT__ 50750026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 50760026cea9SSean Farley /*@C 50770026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 50780026cea9SSean Farley 50790026cea9SSean Farley Collective on TS 50800026cea9SSean Farley 50810026cea9SSean Farley Input Arguments: 50820026cea9SSean Farley + ts - time stepping context 50830026cea9SSean Farley . t - time at which to evaluate 50840910c330SBarry Smith . U - state at which to evaluate 50850910c330SBarry Smith . Udot - time derivative of state vector 50860026cea9SSean Farley - ctx - context 50870026cea9SSean Farley 50880026cea9SSean Farley Output Arguments: 50890026cea9SSean Farley . F - left hand side 50900026cea9SSean Farley 50910026cea9SSean Farley Level: intermediate 50920026cea9SSean Farley 50930026cea9SSean Farley Notes: 50940910c330SBarry 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 50950026cea9SSean Farley user is required to write their own TSComputeIFunction. 50960026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 50970026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 50980026cea9SSean Farley 50999ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 51009ae8fd06SBarry Smith 51019ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 51020026cea9SSean Farley @*/ 51030910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 51040026cea9SSean Farley { 51050026cea9SSean Farley PetscErrorCode ierr; 51060026cea9SSean Farley Mat A,B; 51070026cea9SSean Farley 51080026cea9SSean Farley PetscFunctionBegin; 51090298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 5110d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 51110910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 51120026cea9SSean Farley PetscFunctionReturn(0); 51130026cea9SSean Farley } 51140026cea9SSean Farley 51150026cea9SSean Farley #undef __FUNCT__ 51160026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 51170026cea9SSean Farley /*@C 5118b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 51190026cea9SSean Farley 51200026cea9SSean Farley Collective on TS 51210026cea9SSean Farley 51220026cea9SSean Farley Input Arguments: 51230026cea9SSean Farley + ts - time stepping context 51240026cea9SSean Farley . t - time at which to evaluate 51250910c330SBarry Smith . U - state at which to evaluate 51260910c330SBarry Smith . Udot - time derivative of state vector 51270026cea9SSean Farley . shift - shift to apply 51280026cea9SSean Farley - ctx - context 51290026cea9SSean Farley 51300026cea9SSean Farley Output Arguments: 51310026cea9SSean Farley + A - pointer to operator 51320026cea9SSean Farley . B - pointer to preconditioning matrix 51330026cea9SSean Farley - flg - matrix structure flag 51340026cea9SSean Farley 5135b41af12eSJed Brown Level: advanced 51360026cea9SSean Farley 51370026cea9SSean Farley Notes: 51380026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 51390026cea9SSean Farley 5140b41af12eSJed Brown It is only appropriate for problems of the form 5141b41af12eSJed Brown 5142b41af12eSJed Brown $ M Udot = F(U,t) 5143b41af12eSJed Brown 5144b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 5145b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 5146b41af12eSJed Brown an implicit operator of the form 5147b41af12eSJed Brown 5148b41af12eSJed Brown $ shift*M + J 5149b41af12eSJed Brown 5150b41af12eSJed 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 5151b41af12eSJed Brown a copy of M or reassemble it when requested. 5152b41af12eSJed Brown 51530026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 51540026cea9SSean Farley @*/ 5155d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 51560026cea9SSean Farley { 5157b41af12eSJed Brown PetscErrorCode ierr; 5158b41af12eSJed Brown 51590026cea9SSean Farley PetscFunctionBegin; 516094ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 5161b41af12eSJed Brown ts->ijacobian.shift = shift; 51620026cea9SSean Farley PetscFunctionReturn(0); 51630026cea9SSean Farley } 5164b41af12eSJed Brown 5165e817cc15SEmil Constantinescu #undef __FUNCT__ 5166e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType" 5167e817cc15SEmil Constantinescu /*@ 5168e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 5169e817cc15SEmil Constantinescu 5170e817cc15SEmil Constantinescu Not Collective 5171e817cc15SEmil Constantinescu 5172e817cc15SEmil Constantinescu Input Parameter: 5173e817cc15SEmil Constantinescu . ts - the TS context 5174e817cc15SEmil Constantinescu 5175e817cc15SEmil Constantinescu Output Parameter: 51764e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 5177e817cc15SEmil Constantinescu 5178e817cc15SEmil Constantinescu Level: beginner 5179e817cc15SEmil Constantinescu 5180e817cc15SEmil Constantinescu .keywords: TS, equation type 5181e817cc15SEmil Constantinescu 5182e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 5183e817cc15SEmil Constantinescu @*/ 5184e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 5185e817cc15SEmil Constantinescu { 5186e817cc15SEmil Constantinescu PetscFunctionBegin; 5187e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5188e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 5189e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 5190e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5191e817cc15SEmil Constantinescu } 5192e817cc15SEmil Constantinescu 5193e817cc15SEmil Constantinescu #undef __FUNCT__ 5194e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType" 5195e817cc15SEmil Constantinescu /*@ 5196e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 5197e817cc15SEmil Constantinescu 5198e817cc15SEmil Constantinescu Not Collective 5199e817cc15SEmil Constantinescu 5200e817cc15SEmil Constantinescu Input Parameter: 5201e817cc15SEmil Constantinescu + ts - the TS context 52021297b224SEmil Constantinescu - equation_type - see TSEquationType 5203e817cc15SEmil Constantinescu 5204e817cc15SEmil Constantinescu Level: advanced 5205e817cc15SEmil Constantinescu 5206e817cc15SEmil Constantinescu .keywords: TS, equation type 5207e817cc15SEmil Constantinescu 5208e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 5209e817cc15SEmil Constantinescu @*/ 5210e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 5211e817cc15SEmil Constantinescu { 5212e817cc15SEmil Constantinescu PetscFunctionBegin; 5213e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5214e817cc15SEmil Constantinescu ts->equation_type = equation_type; 5215e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5216e817cc15SEmil Constantinescu } 52170026cea9SSean Farley 52184af1b03aSJed Brown #undef __FUNCT__ 52194af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 52204af1b03aSJed Brown /*@ 52214af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 52224af1b03aSJed Brown 52234af1b03aSJed Brown Not Collective 52244af1b03aSJed Brown 52254af1b03aSJed Brown Input Parameter: 52264af1b03aSJed Brown . ts - the TS context 52274af1b03aSJed Brown 52284af1b03aSJed Brown Output Parameter: 52294af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 52304af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 52314af1b03aSJed Brown 5232487e0bb9SJed Brown Level: beginner 52334af1b03aSJed Brown 5234cd652676SJed Brown Notes: 5235cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 52364af1b03aSJed Brown 52374af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 52384af1b03aSJed Brown 52394af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 52404af1b03aSJed Brown @*/ 52414af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 52424af1b03aSJed Brown { 52434af1b03aSJed Brown PetscFunctionBegin; 52444af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 52454af1b03aSJed Brown PetscValidPointer(reason,2); 52464af1b03aSJed Brown *reason = ts->reason; 52474af1b03aSJed Brown PetscFunctionReturn(0); 52484af1b03aSJed Brown } 52494af1b03aSJed Brown 5250fb1732b5SBarry Smith #undef __FUNCT__ 5251d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 5252d6ad946cSShri Abhyankar /*@ 5253d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 5254d6ad946cSShri Abhyankar 5255d6ad946cSShri Abhyankar Not Collective 5256d6ad946cSShri Abhyankar 5257d6ad946cSShri Abhyankar Input Parameter: 5258d6ad946cSShri Abhyankar + ts - the TS context 5259d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 5260d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 5261d6ad946cSShri Abhyankar 5262f5abba47SShri Abhyankar Level: advanced 5263d6ad946cSShri Abhyankar 5264d6ad946cSShri Abhyankar Notes: 5265d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 5266d6ad946cSShri Abhyankar 5267d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 5268d6ad946cSShri Abhyankar 5269d6ad946cSShri Abhyankar .seealso: TSConvergedReason 5270d6ad946cSShri Abhyankar @*/ 5271d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 5272d6ad946cSShri Abhyankar { 5273d6ad946cSShri Abhyankar PetscFunctionBegin; 5274d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5275d6ad946cSShri Abhyankar ts->reason = reason; 5276d6ad946cSShri Abhyankar PetscFunctionReturn(0); 5277d6ad946cSShri Abhyankar } 5278d6ad946cSShri Abhyankar 5279d6ad946cSShri Abhyankar #undef __FUNCT__ 5280cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 5281cc708dedSBarry Smith /*@ 5282cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 5283cc708dedSBarry Smith 5284cc708dedSBarry Smith Not Collective 5285cc708dedSBarry Smith 5286cc708dedSBarry Smith Input Parameter: 5287cc708dedSBarry Smith . ts - the TS context 5288cc708dedSBarry Smith 5289cc708dedSBarry Smith Output Parameter: 529063e21af5SBarry Smith . ftime - the final time. This time corresponds to the final time set with TSSetDuration() 5291cc708dedSBarry Smith 5292487e0bb9SJed Brown Level: beginner 5293cc708dedSBarry Smith 5294cc708dedSBarry Smith Notes: 5295cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 5296cc708dedSBarry Smith 5297cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 5298cc708dedSBarry Smith 5299cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 5300cc708dedSBarry Smith @*/ 5301cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 5302cc708dedSBarry Smith { 5303cc708dedSBarry Smith PetscFunctionBegin; 5304cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5305cc708dedSBarry Smith PetscValidPointer(ftime,2); 5306cc708dedSBarry Smith *ftime = ts->solvetime; 5307cc708dedSBarry Smith PetscFunctionReturn(0); 5308cc708dedSBarry Smith } 5309cc708dedSBarry Smith 5310cc708dedSBarry Smith #undef __FUNCT__ 53112c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps" 53122c18e0fdSBarry Smith /*@ 53132c18e0fdSBarry Smith TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 53142c18e0fdSBarry Smith 53152c18e0fdSBarry Smith Not Collective 53162c18e0fdSBarry Smith 53172c18e0fdSBarry Smith Input Parameter: 53182c18e0fdSBarry Smith . ts - the TS context 53192c18e0fdSBarry Smith 53202c18e0fdSBarry Smith Output Parameter: 53212c18e0fdSBarry Smith . steps - the number of steps 53222c18e0fdSBarry Smith 53232c18e0fdSBarry Smith Level: beginner 53242c18e0fdSBarry Smith 53252c18e0fdSBarry Smith Notes: 53262c18e0fdSBarry Smith Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 53272c18e0fdSBarry Smith 53282c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test 53292c18e0fdSBarry Smith 53302c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 53312c18e0fdSBarry Smith @*/ 53322c18e0fdSBarry Smith PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 53332c18e0fdSBarry Smith { 53342c18e0fdSBarry Smith PetscFunctionBegin; 53352c18e0fdSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53362c18e0fdSBarry Smith PetscValidPointer(steps,2); 53372c18e0fdSBarry Smith *steps = ts->total_steps; 53382c18e0fdSBarry Smith PetscFunctionReturn(0); 53392c18e0fdSBarry Smith } 53402c18e0fdSBarry Smith 53412c18e0fdSBarry Smith #undef __FUNCT__ 53425ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 53439f67acb7SJed Brown /*@ 53445ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 53459f67acb7SJed Brown used by the time integrator. 53469f67acb7SJed Brown 53479f67acb7SJed Brown Not Collective 53489f67acb7SJed Brown 53499f67acb7SJed Brown Input Parameter: 53509f67acb7SJed Brown . ts - TS context 53519f67acb7SJed Brown 53529f67acb7SJed Brown Output Parameter: 53539f67acb7SJed Brown . nits - number of nonlinear iterations 53549f67acb7SJed Brown 53559f67acb7SJed Brown Notes: 53569f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 53579f67acb7SJed Brown 53589f67acb7SJed Brown Level: intermediate 53599f67acb7SJed Brown 53609f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 53619f67acb7SJed Brown 53625ef26d82SJed Brown .seealso: TSGetKSPIterations() 53639f67acb7SJed Brown @*/ 53645ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 53659f67acb7SJed Brown { 53669f67acb7SJed Brown PetscFunctionBegin; 53679f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53689f67acb7SJed Brown PetscValidIntPointer(nits,2); 53695ef26d82SJed Brown *nits = ts->snes_its; 53709f67acb7SJed Brown PetscFunctionReturn(0); 53719f67acb7SJed Brown } 53729f67acb7SJed Brown 53739f67acb7SJed Brown #undef __FUNCT__ 53745ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 53759f67acb7SJed Brown /*@ 53765ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 53779f67acb7SJed Brown used by the time integrator. 53789f67acb7SJed Brown 53799f67acb7SJed Brown Not Collective 53809f67acb7SJed Brown 53819f67acb7SJed Brown Input Parameter: 53829f67acb7SJed Brown . ts - TS context 53839f67acb7SJed Brown 53849f67acb7SJed Brown Output Parameter: 53859f67acb7SJed Brown . lits - number of linear iterations 53869f67acb7SJed Brown 53879f67acb7SJed Brown Notes: 53889f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 53899f67acb7SJed Brown 53909f67acb7SJed Brown Level: intermediate 53919f67acb7SJed Brown 53929f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 53939f67acb7SJed Brown 53945ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 53959f67acb7SJed Brown @*/ 53965ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 53979f67acb7SJed Brown { 53989f67acb7SJed Brown PetscFunctionBegin; 53999f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 54009f67acb7SJed Brown PetscValidIntPointer(lits,2); 54015ef26d82SJed Brown *lits = ts->ksp_its; 54029f67acb7SJed Brown PetscFunctionReturn(0); 54039f67acb7SJed Brown } 54049f67acb7SJed Brown 54059f67acb7SJed Brown #undef __FUNCT__ 5406cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 5407cef5090cSJed Brown /*@ 5408cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 5409cef5090cSJed Brown 5410cef5090cSJed Brown Not Collective 5411cef5090cSJed Brown 5412cef5090cSJed Brown Input Parameter: 5413cef5090cSJed Brown . ts - TS context 5414cef5090cSJed Brown 5415cef5090cSJed Brown Output Parameter: 5416cef5090cSJed Brown . rejects - number of steps rejected 5417cef5090cSJed Brown 5418cef5090cSJed Brown Notes: 5419cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5420cef5090cSJed Brown 5421cef5090cSJed Brown Level: intermediate 5422cef5090cSJed Brown 5423cef5090cSJed Brown .keywords: TS, get, number 5424cef5090cSJed Brown 54255ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 5426cef5090cSJed Brown @*/ 5427cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 5428cef5090cSJed Brown { 5429cef5090cSJed Brown PetscFunctionBegin; 5430cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5431cef5090cSJed Brown PetscValidIntPointer(rejects,2); 5432cef5090cSJed Brown *rejects = ts->reject; 5433cef5090cSJed Brown PetscFunctionReturn(0); 5434cef5090cSJed Brown } 5435cef5090cSJed Brown 5436cef5090cSJed Brown #undef __FUNCT__ 5437cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 5438cef5090cSJed Brown /*@ 5439cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 5440cef5090cSJed Brown 5441cef5090cSJed Brown Not Collective 5442cef5090cSJed Brown 5443cef5090cSJed Brown Input Parameter: 5444cef5090cSJed Brown . ts - TS context 5445cef5090cSJed Brown 5446cef5090cSJed Brown Output Parameter: 5447cef5090cSJed Brown . fails - number of failed nonlinear solves 5448cef5090cSJed Brown 5449cef5090cSJed Brown Notes: 5450cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5451cef5090cSJed Brown 5452cef5090cSJed Brown Level: intermediate 5453cef5090cSJed Brown 5454cef5090cSJed Brown .keywords: TS, get, number 5455cef5090cSJed Brown 54565ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 5457cef5090cSJed Brown @*/ 5458cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 5459cef5090cSJed Brown { 5460cef5090cSJed Brown PetscFunctionBegin; 5461cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5462cef5090cSJed Brown PetscValidIntPointer(fails,2); 5463cef5090cSJed Brown *fails = ts->num_snes_failures; 5464cef5090cSJed Brown PetscFunctionReturn(0); 5465cef5090cSJed Brown } 5466cef5090cSJed Brown 5467cef5090cSJed Brown #undef __FUNCT__ 5468cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 5469cef5090cSJed Brown /*@ 5470cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 5471cef5090cSJed Brown 5472cef5090cSJed Brown Not Collective 5473cef5090cSJed Brown 5474cef5090cSJed Brown Input Parameter: 5475cef5090cSJed Brown + ts - TS context 5476cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 5477cef5090cSJed Brown 5478cef5090cSJed Brown Notes: 5479cef5090cSJed Brown The counter is reset to zero for each step 5480cef5090cSJed Brown 5481cef5090cSJed Brown Options Database Key: 5482cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 5483cef5090cSJed Brown 5484cef5090cSJed Brown Level: intermediate 5485cef5090cSJed Brown 5486cef5090cSJed Brown .keywords: TS, set, maximum, number 5487cef5090cSJed Brown 54885ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5489cef5090cSJed Brown @*/ 5490cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 5491cef5090cSJed Brown { 5492cef5090cSJed Brown PetscFunctionBegin; 5493cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5494cef5090cSJed Brown ts->max_reject = rejects; 5495cef5090cSJed Brown PetscFunctionReturn(0); 5496cef5090cSJed Brown } 5497cef5090cSJed Brown 5498cef5090cSJed Brown #undef __FUNCT__ 5499cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 5500cef5090cSJed Brown /*@ 5501cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 5502cef5090cSJed Brown 5503cef5090cSJed Brown Not Collective 5504cef5090cSJed Brown 5505cef5090cSJed Brown Input Parameter: 5506cef5090cSJed Brown + ts - TS context 5507cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 5508cef5090cSJed Brown 5509cef5090cSJed Brown Notes: 5510cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 5511cef5090cSJed Brown 5512cef5090cSJed Brown Options Database Key: 5513cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 5514cef5090cSJed Brown 5515cef5090cSJed Brown Level: intermediate 5516cef5090cSJed Brown 5517cef5090cSJed Brown .keywords: TS, set, maximum, number 5518cef5090cSJed Brown 55195ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 5520cef5090cSJed Brown @*/ 5521cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 5522cef5090cSJed Brown { 5523cef5090cSJed Brown PetscFunctionBegin; 5524cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5525cef5090cSJed Brown ts->max_snes_failures = fails; 5526cef5090cSJed Brown PetscFunctionReturn(0); 5527cef5090cSJed Brown } 5528cef5090cSJed Brown 5529cef5090cSJed Brown #undef __FUNCT__ 55304e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails" 5531cef5090cSJed Brown /*@ 5532cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 5533cef5090cSJed Brown 5534cef5090cSJed Brown Not Collective 5535cef5090cSJed Brown 5536cef5090cSJed Brown Input Parameter: 5537cef5090cSJed Brown + ts - TS context 5538cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 5539cef5090cSJed Brown 5540cef5090cSJed Brown Options Database Key: 5541cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 5542cef5090cSJed Brown 5543cef5090cSJed Brown Level: intermediate 5544cef5090cSJed Brown 5545cef5090cSJed Brown .keywords: TS, set, error 5546cef5090cSJed Brown 55475ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5548cef5090cSJed Brown @*/ 5549cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 5550cef5090cSJed Brown { 5551cef5090cSJed Brown PetscFunctionBegin; 5552cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5553cef5090cSJed Brown ts->errorifstepfailed = err; 5554cef5090cSJed Brown PetscFunctionReturn(0); 5555cef5090cSJed Brown } 5556cef5090cSJed Brown 5557cef5090cSJed Brown #undef __FUNCT__ 5558fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSolution" 5559fb1732b5SBarry Smith /*@C 5560fde5950dSBarry 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 5561fb1732b5SBarry Smith 5562fb1732b5SBarry Smith Collective on TS 5563fb1732b5SBarry Smith 5564fb1732b5SBarry Smith Input Parameters: 5565fb1732b5SBarry Smith + ts - the TS context 5566fb1732b5SBarry Smith . step - current time-step 5567fb1732b5SBarry Smith . ptime - current time 55680910c330SBarry Smith . u - current state 5569721cd6eeSBarry Smith - vf - viewer and its format 5570fb1732b5SBarry Smith 5571fb1732b5SBarry Smith Level: intermediate 5572fb1732b5SBarry Smith 5573fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 5574fb1732b5SBarry Smith 5575fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5576fb1732b5SBarry Smith @*/ 5577721cd6eeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf) 5578fb1732b5SBarry Smith { 5579fb1732b5SBarry Smith PetscErrorCode ierr; 5580fb1732b5SBarry Smith 5581fb1732b5SBarry Smith PetscFunctionBegin; 5582721cd6eeSBarry Smith ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr); 5583721cd6eeSBarry Smith ierr = VecView(u,vf->viewer);CHKERRQ(ierr); 5584721cd6eeSBarry Smith ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr); 5585ed81e22dSJed Brown PetscFunctionReturn(0); 5586ed81e22dSJed Brown } 5587ed81e22dSJed Brown 5588ed81e22dSJed Brown #undef __FUNCT__ 5589ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 5590ed81e22dSJed Brown /*@C 5591ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5592ed81e22dSJed Brown 5593ed81e22dSJed Brown Collective on TS 5594ed81e22dSJed Brown 5595ed81e22dSJed Brown Input Parameters: 5596ed81e22dSJed Brown + ts - the TS context 5597ed81e22dSJed Brown . step - current time-step 5598ed81e22dSJed Brown . ptime - current time 55990910c330SBarry Smith . u - current state 5600ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5601ed81e22dSJed Brown 5602ed81e22dSJed Brown Level: intermediate 5603ed81e22dSJed Brown 5604ed81e22dSJed Brown Notes: 5605ed81e22dSJed 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. 5606ed81e22dSJed Brown These are named according to the file name template. 5607ed81e22dSJed Brown 5608ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5609ed81e22dSJed Brown 5610ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5611ed81e22dSJed Brown 5612ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5613ed81e22dSJed Brown @*/ 56140910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5615ed81e22dSJed Brown { 5616ed81e22dSJed Brown PetscErrorCode ierr; 5617ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 5618ed81e22dSJed Brown PetscViewer viewer; 5619ed81e22dSJed Brown 5620ed81e22dSJed Brown PetscFunctionBegin; 562163e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 56228caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5623ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 56240910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 5625ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5626ed81e22dSJed Brown PetscFunctionReturn(0); 5627ed81e22dSJed Brown } 5628ed81e22dSJed Brown 5629ed81e22dSJed Brown #undef __FUNCT__ 5630ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 5631ed81e22dSJed Brown /*@C 5632ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5633ed81e22dSJed Brown 5634ed81e22dSJed Brown Collective on TS 5635ed81e22dSJed Brown 5636ed81e22dSJed Brown Input Parameters: 5637ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5638ed81e22dSJed Brown 5639ed81e22dSJed Brown Level: intermediate 5640ed81e22dSJed Brown 5641ed81e22dSJed Brown Note: 5642ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5643ed81e22dSJed Brown 5644ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5645ed81e22dSJed Brown 5646ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5647ed81e22dSJed Brown @*/ 5648ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5649ed81e22dSJed Brown { 5650ed81e22dSJed Brown PetscErrorCode ierr; 5651ed81e22dSJed Brown 5652ed81e22dSJed Brown PetscFunctionBegin; 5653ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5654fb1732b5SBarry Smith PetscFunctionReturn(0); 5655fb1732b5SBarry Smith } 5656fb1732b5SBarry Smith 565784df9cb4SJed Brown #undef __FUNCT__ 5658552698daSJed Brown #define __FUNCT__ "TSGetAdapt" 565984df9cb4SJed Brown /*@ 5660552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 566184df9cb4SJed Brown 5662ed81e22dSJed Brown Collective on TS if controller has not been created yet 566384df9cb4SJed Brown 566484df9cb4SJed Brown Input Arguments: 5665ed81e22dSJed Brown . ts - time stepping context 566684df9cb4SJed Brown 566784df9cb4SJed Brown Output Arguments: 5668ed81e22dSJed Brown . adapt - adaptive controller 566984df9cb4SJed Brown 567084df9cb4SJed Brown Level: intermediate 567184df9cb4SJed Brown 5672ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 567384df9cb4SJed Brown @*/ 5674552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 567584df9cb4SJed Brown { 567684df9cb4SJed Brown PetscErrorCode ierr; 567784df9cb4SJed Brown 567884df9cb4SJed Brown PetscFunctionBegin; 567984df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5680bec58848SLisandro Dalcin PetscValidPointer(adapt,2); 568184df9cb4SJed Brown if (!ts->adapt) { 5682ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 56833bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 56841c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 568584df9cb4SJed Brown } 5686bec58848SLisandro Dalcin *adapt = ts->adapt; 568784df9cb4SJed Brown PetscFunctionReturn(0); 568884df9cb4SJed Brown } 5689d6ebe24aSShri Abhyankar 5690d6ebe24aSShri Abhyankar #undef __FUNCT__ 56911c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 56921c3436cfSJed Brown /*@ 56931c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 56941c3436cfSJed Brown 56951c3436cfSJed Brown Logically Collective 56961c3436cfSJed Brown 56971c3436cfSJed Brown Input Arguments: 56981c3436cfSJed Brown + ts - time integration context 56991c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 57000298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 57011c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 57020298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 57031c3436cfSJed Brown 5704a3cdaa26SBarry Smith Options Database keys: 5705a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5706a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5707a3cdaa26SBarry Smith 57083ff766beSShri Abhyankar Notes: 57093ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 57103ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 57113ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 57123ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 57133ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 57143ff766beSShri Abhyankar differential variables. 57153ff766beSShri Abhyankar 57161c3436cfSJed Brown Level: beginner 57171c3436cfSJed Brown 5718c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 57191c3436cfSJed Brown @*/ 57201c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 57211c3436cfSJed Brown { 57221c3436cfSJed Brown PetscErrorCode ierr; 57231c3436cfSJed Brown 57241c3436cfSJed Brown PetscFunctionBegin; 5725c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 57261c3436cfSJed Brown if (vatol) { 57271c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 57281c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 57291c3436cfSJed Brown ts->vatol = vatol; 57301c3436cfSJed Brown } 5731c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 57321c3436cfSJed Brown if (vrtol) { 57331c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 57341c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 57351c3436cfSJed Brown ts->vrtol = vrtol; 57361c3436cfSJed Brown } 57371c3436cfSJed Brown PetscFunctionReturn(0); 57381c3436cfSJed Brown } 57391c3436cfSJed Brown 57401c3436cfSJed Brown #undef __FUNCT__ 5741c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 5742c5033834SJed Brown /*@ 5743c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5744c5033834SJed Brown 5745c5033834SJed Brown Logically Collective 5746c5033834SJed Brown 5747c5033834SJed Brown Input Arguments: 5748c5033834SJed Brown . ts - time integration context 5749c5033834SJed Brown 5750c5033834SJed Brown Output Arguments: 57510298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 57520298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 57530298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 57540298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 5755c5033834SJed Brown 5756c5033834SJed Brown Level: beginner 5757c5033834SJed Brown 5758c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5759c5033834SJed Brown @*/ 5760c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5761c5033834SJed Brown { 5762c5033834SJed Brown PetscFunctionBegin; 5763c5033834SJed Brown if (atol) *atol = ts->atol; 5764c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5765c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5766c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 5767c5033834SJed Brown PetscFunctionReturn(0); 5768c5033834SJed Brown } 5769c5033834SJed Brown 5770c5033834SJed Brown #undef __FUNCT__ 57719c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2" 57729c6b16b5SShri Abhyankar /*@ 5773a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 57749c6b16b5SShri Abhyankar 57759c6b16b5SShri Abhyankar Collective on TS 57769c6b16b5SShri Abhyankar 57779c6b16b5SShri Abhyankar Input Arguments: 57789c6b16b5SShri Abhyankar + ts - time stepping context 5779a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5780a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 57819c6b16b5SShri Abhyankar 57829c6b16b5SShri Abhyankar Output Arguments: 57839c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 57849c6b16b5SShri Abhyankar 57859c6b16b5SShri Abhyankar Level: developer 57869c6b16b5SShri Abhyankar 5787deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 57889c6b16b5SShri Abhyankar @*/ 5789a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm) 57909c6b16b5SShri Abhyankar { 57919c6b16b5SShri Abhyankar PetscErrorCode ierr; 57929c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 57939c6b16b5SShri Abhyankar const PetscScalar *u,*y; 57949c6b16b5SShri Abhyankar PetscReal sum,gsum; 57959c6b16b5SShri Abhyankar PetscReal tol; 57969c6b16b5SShri Abhyankar 57979c6b16b5SShri Abhyankar PetscFunctionBegin; 57989c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5799a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5800a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5801a4868fbcSLisandro Dalcin PetscValidType(U,2); 5802a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5803a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5804a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5805a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 58069c6b16b5SShri Abhyankar 58079c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 58089c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 58099c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 58109c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 58119c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 58129c6b16b5SShri Abhyankar sum = 0.; 58139c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 58149c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 58159c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58169c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58179c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 58189c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58199c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 58209c6b16b5SShri Abhyankar } 58219c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58229c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58239c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 58249c6b16b5SShri Abhyankar const PetscScalar *atol; 58259c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58269c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 58279c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58289c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 58299c6b16b5SShri Abhyankar } 58309c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58319c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 58329c6b16b5SShri Abhyankar const PetscScalar *rtol; 58339c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58349c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 58359c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58369c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 58379c6b16b5SShri Abhyankar } 58389c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58399c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 58409c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 58419c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58429c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 58439c6b16b5SShri Abhyankar } 58449c6b16b5SShri Abhyankar } 58459c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 58469c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 58479c6b16b5SShri Abhyankar 5848b2566f29SBarry Smith ierr = MPIU_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 58499c6b16b5SShri Abhyankar *norm = PetscSqrtReal(gsum / N); 58509c6b16b5SShri Abhyankar 58519c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 58529c6b16b5SShri Abhyankar PetscFunctionReturn(0); 58539c6b16b5SShri Abhyankar } 58549c6b16b5SShri Abhyankar 58559c6b16b5SShri Abhyankar #undef __FUNCT__ 58569c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity" 58579c6b16b5SShri Abhyankar /*@ 5858a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 58599c6b16b5SShri Abhyankar 58609c6b16b5SShri Abhyankar Collective on TS 58619c6b16b5SShri Abhyankar 58629c6b16b5SShri Abhyankar Input Arguments: 58639c6b16b5SShri Abhyankar + ts - time stepping context 5864a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5865a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 58669c6b16b5SShri Abhyankar 58679c6b16b5SShri Abhyankar Output Arguments: 58689c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 58699c6b16b5SShri Abhyankar 58709c6b16b5SShri Abhyankar Level: developer 58719c6b16b5SShri Abhyankar 5872deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 58739c6b16b5SShri Abhyankar @*/ 5874a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm) 58759c6b16b5SShri Abhyankar { 58769c6b16b5SShri Abhyankar PetscErrorCode ierr; 58779c6b16b5SShri Abhyankar PetscInt i,n,N,rstart,k; 58789c6b16b5SShri Abhyankar const PetscScalar *u,*y; 58799c6b16b5SShri Abhyankar PetscReal max,gmax; 58809c6b16b5SShri Abhyankar PetscReal tol; 58819c6b16b5SShri Abhyankar 58829c6b16b5SShri Abhyankar PetscFunctionBegin; 58839c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5884a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5885a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5886a4868fbcSLisandro Dalcin PetscValidType(U,2); 5887a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5888a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5889a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5890a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 58919c6b16b5SShri Abhyankar 58929c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 58939c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 58949c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 58959c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 58969c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 58979c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 58989c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 58999c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59009c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59019c6b16b5SShri Abhyankar k = 0; 59029c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 59039c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 59049c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 59059c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59069c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 59079c6b16b5SShri Abhyankar } 59089c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59099c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59109c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 59119c6b16b5SShri Abhyankar const PetscScalar *atol; 59129c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59139c6b16b5SShri Abhyankar k = 0; 59149c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 59159c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 59169c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 59179c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59189c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 59199c6b16b5SShri Abhyankar } 59209c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59219c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 59229c6b16b5SShri Abhyankar const PetscScalar *rtol; 59239c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59249c6b16b5SShri Abhyankar k = 0; 59259c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 59269c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 59279c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 59289c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59299c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 59309c6b16b5SShri Abhyankar } 59319c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59329c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 59339c6b16b5SShri Abhyankar k = 0; 59349c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 59359c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 59369c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 59379c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59389c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 59399c6b16b5SShri Abhyankar } 59409c6b16b5SShri Abhyankar } 59419c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 59429c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 59439c6b16b5SShri Abhyankar 5944b2566f29SBarry Smith ierr = MPIU_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 59459c6b16b5SShri Abhyankar *norm = gmax; 59469c6b16b5SShri Abhyankar 59479c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 59489c6b16b5SShri Abhyankar PetscFunctionReturn(0); 59499c6b16b5SShri Abhyankar } 59509c6b16b5SShri Abhyankar 59519c6b16b5SShri Abhyankar #undef __FUNCT__ 59527619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm" 59531c3436cfSJed Brown /*@ 5954a4868fbcSLisandro Dalcin TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors 59551c3436cfSJed Brown 59561c3436cfSJed Brown Collective on TS 59571c3436cfSJed Brown 59581c3436cfSJed Brown Input Arguments: 59591c3436cfSJed Brown + ts - time stepping context 5960a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5961a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5962a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 59637619abb3SShri 59641c3436cfSJed Brown Output Arguments: 59651c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 59661c3436cfSJed Brown 5967a4868fbcSLisandro Dalcin 5968a4868fbcSLisandro Dalcin Options Database Keys: 5969a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5970a4868fbcSLisandro Dalcin 59711c3436cfSJed Brown Level: developer 59721c3436cfSJed Brown 5973deea92deSShri .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 59741c3436cfSJed Brown @*/ 5975a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm) 59761c3436cfSJed Brown { 59778beabaa1SBarry Smith PetscErrorCode ierr; 59781c3436cfSJed Brown 59791c3436cfSJed Brown PetscFunctionBegin; 5980a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 5981a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNorm2(ts,U,Y,norm);CHKERRQ(ierr); 5982a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 5983a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm);CHKERRQ(ierr); 5984a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 59851c3436cfSJed Brown PetscFunctionReturn(0); 59861c3436cfSJed Brown } 59871c3436cfSJed Brown 59881c3436cfSJed Brown #undef __FUNCT__ 59898d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 59908d59e960SJed Brown /*@ 59918d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 59928d59e960SJed Brown 59938d59e960SJed Brown Logically Collective on TS 59948d59e960SJed Brown 59958d59e960SJed Brown Input Arguments: 59968d59e960SJed Brown + ts - time stepping context 59978d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 59988d59e960SJed Brown 59998d59e960SJed Brown Note: 60008d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 60018d59e960SJed Brown 60028d59e960SJed Brown Level: intermediate 60038d59e960SJed Brown 60048d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 60058d59e960SJed Brown @*/ 60068d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 60078d59e960SJed Brown { 60088d59e960SJed Brown PetscFunctionBegin; 60098d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 60108d59e960SJed Brown ts->cfltime_local = cfltime; 60118d59e960SJed Brown ts->cfltime = -1.; 60128d59e960SJed Brown PetscFunctionReturn(0); 60138d59e960SJed Brown } 60148d59e960SJed Brown 60158d59e960SJed Brown #undef __FUNCT__ 60168d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 60178d59e960SJed Brown /*@ 60188d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 60198d59e960SJed Brown 60208d59e960SJed Brown Collective on TS 60218d59e960SJed Brown 60228d59e960SJed Brown Input Arguments: 60238d59e960SJed Brown . ts - time stepping context 60248d59e960SJed Brown 60258d59e960SJed Brown Output Arguments: 60268d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 60278d59e960SJed Brown 60288d59e960SJed Brown Level: advanced 60298d59e960SJed Brown 60308d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 60318d59e960SJed Brown @*/ 60328d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 60338d59e960SJed Brown { 60348d59e960SJed Brown PetscErrorCode ierr; 60358d59e960SJed Brown 60368d59e960SJed Brown PetscFunctionBegin; 60378d59e960SJed Brown if (ts->cfltime < 0) { 6038b2566f29SBarry Smith ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 60398d59e960SJed Brown } 60408d59e960SJed Brown *cfltime = ts->cfltime; 60418d59e960SJed Brown PetscFunctionReturn(0); 60428d59e960SJed Brown } 60438d59e960SJed Brown 60448d59e960SJed Brown #undef __FUNCT__ 6045d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 6046d6ebe24aSShri Abhyankar /*@ 6047d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 6048d6ebe24aSShri Abhyankar 6049d6ebe24aSShri Abhyankar Input Parameters: 6050d6ebe24aSShri Abhyankar . ts - the TS context. 6051d6ebe24aSShri Abhyankar . xl - lower bound. 6052d6ebe24aSShri Abhyankar . xu - upper bound. 6053d6ebe24aSShri Abhyankar 6054d6ebe24aSShri Abhyankar Notes: 6055d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 6056e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 6057d6ebe24aSShri Abhyankar 60582bd2b0e6SSatish Balay Level: advanced 60592bd2b0e6SSatish Balay 6060d6ebe24aSShri Abhyankar @*/ 6061d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 6062d6ebe24aSShri Abhyankar { 6063d6ebe24aSShri Abhyankar PetscErrorCode ierr; 6064d6ebe24aSShri Abhyankar SNES snes; 6065d6ebe24aSShri Abhyankar 6066d6ebe24aSShri Abhyankar PetscFunctionBegin; 6067d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 6068d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 6069d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 6070d6ebe24aSShri Abhyankar } 6071d6ebe24aSShri Abhyankar 6072325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 6073c6db04a5SJed Brown #include <mex.h> 6074325fc9f4SBarry Smith 6075325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 6076325fc9f4SBarry Smith 6077325fc9f4SBarry Smith #undef __FUNCT__ 6078325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 6079325fc9f4SBarry Smith /* 6080325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 6081325fc9f4SBarry Smith TSSetFunctionMatlab(). 6082325fc9f4SBarry Smith 6083325fc9f4SBarry Smith Collective on TS 6084325fc9f4SBarry Smith 6085325fc9f4SBarry Smith Input Parameters: 6086325fc9f4SBarry Smith + snes - the TS context 60870910c330SBarry Smith - u - input vector 6088325fc9f4SBarry Smith 6089325fc9f4SBarry Smith Output Parameter: 6090325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 6091325fc9f4SBarry Smith 6092325fc9f4SBarry Smith Notes: 6093325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 6094325fc9f4SBarry Smith implementations, so most users would not generally call this routine 6095325fc9f4SBarry Smith themselves. 6096325fc9f4SBarry Smith 6097325fc9f4SBarry Smith Level: developer 6098325fc9f4SBarry Smith 6099325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6100325fc9f4SBarry Smith 6101325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6102325fc9f4SBarry Smith */ 61030910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 6104325fc9f4SBarry Smith { 6105325fc9f4SBarry Smith PetscErrorCode ierr; 6106325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6107325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 6108325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 6109325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 6110325fc9f4SBarry Smith 6111325fc9f4SBarry Smith PetscFunctionBegin; 6112325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 61130910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 61140910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 6115325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 61160910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 6117325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 6118325fc9f4SBarry Smith 6119325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 61200910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 61210910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 61220910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 6123bbd56ea5SKarl Rupp 6124325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6125325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 6126325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6127325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6128325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 6129325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 6130325fc9f4SBarry Smith prhs[6] = sctx->ctx; 6131325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 6132325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6133325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6134325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6135325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6136325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6137325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6138325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6139325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6140325fc9f4SBarry Smith PetscFunctionReturn(0); 6141325fc9f4SBarry Smith } 6142325fc9f4SBarry Smith 6143325fc9f4SBarry Smith 6144325fc9f4SBarry Smith #undef __FUNCT__ 6145325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 6146325fc9f4SBarry Smith /* 6147325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 6148325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 6149e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 6150325fc9f4SBarry Smith 6151325fc9f4SBarry Smith Logically Collective on TS 6152325fc9f4SBarry Smith 6153325fc9f4SBarry Smith Input Parameters: 6154325fc9f4SBarry Smith + ts - the TS context 6155325fc9f4SBarry Smith - func - function evaluation routine 6156325fc9f4SBarry Smith 6157325fc9f4SBarry Smith Calling sequence of func: 61580910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 6159325fc9f4SBarry Smith 6160325fc9f4SBarry Smith Level: beginner 6161325fc9f4SBarry Smith 6162325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6163325fc9f4SBarry Smith 6164325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6165325fc9f4SBarry Smith */ 6166cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 6167325fc9f4SBarry Smith { 6168325fc9f4SBarry Smith PetscErrorCode ierr; 6169325fc9f4SBarry Smith TSMatlabContext *sctx; 6170325fc9f4SBarry Smith 6171325fc9f4SBarry Smith PetscFunctionBegin; 6172325fc9f4SBarry Smith /* currently sctx is memory bleed */ 6173325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 6174325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6175325fc9f4SBarry Smith /* 6176325fc9f4SBarry Smith This should work, but it doesn't 6177325fc9f4SBarry Smith sctx->ctx = ctx; 6178325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6179325fc9f4SBarry Smith */ 6180325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6181bbd56ea5SKarl Rupp 61820298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 6183325fc9f4SBarry Smith PetscFunctionReturn(0); 6184325fc9f4SBarry Smith } 6185325fc9f4SBarry Smith 6186325fc9f4SBarry Smith #undef __FUNCT__ 6187325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 6188325fc9f4SBarry Smith /* 6189325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 6190325fc9f4SBarry Smith TSSetJacobianMatlab(). 6191325fc9f4SBarry Smith 6192325fc9f4SBarry Smith Collective on TS 6193325fc9f4SBarry Smith 6194325fc9f4SBarry Smith Input Parameters: 6195cdcf91faSSean Farley + ts - the TS context 61960910c330SBarry Smith . u - input vector 6197325fc9f4SBarry Smith . A, B - the matrices 6198325fc9f4SBarry Smith - ctx - user context 6199325fc9f4SBarry Smith 6200325fc9f4SBarry Smith Level: developer 6201325fc9f4SBarry Smith 6202325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6203325fc9f4SBarry Smith 6204325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6205325fc9f4SBarry Smith @*/ 6206f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 6207325fc9f4SBarry Smith { 6208325fc9f4SBarry Smith PetscErrorCode ierr; 6209325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6210325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 6211325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 6212325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 6213325fc9f4SBarry Smith 6214325fc9f4SBarry Smith PetscFunctionBegin; 6215cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 62160910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 6217325fc9f4SBarry Smith 62180910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 6219325fc9f4SBarry Smith 6220cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 62210910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 62220910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 62230910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 62240910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 6225bbd56ea5SKarl Rupp 6226325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6227325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 6228325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6229325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6230325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 6231325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 6232325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 6233325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 6234325fc9f4SBarry Smith prhs[8] = sctx->ctx; 6235325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 6236325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6237325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6238325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6239325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6240325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6241325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6242325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6243325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 6244325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 6245325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6246325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 6247325fc9f4SBarry Smith PetscFunctionReturn(0); 6248325fc9f4SBarry Smith } 6249325fc9f4SBarry Smith 6250325fc9f4SBarry Smith 6251325fc9f4SBarry Smith #undef __FUNCT__ 6252325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 6253325fc9f4SBarry Smith /* 6254325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 6255e3c5b3baSBarry 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 6256325fc9f4SBarry Smith 6257325fc9f4SBarry Smith Logically Collective on TS 6258325fc9f4SBarry Smith 6259325fc9f4SBarry Smith Input Parameters: 6260cdcf91faSSean Farley + ts - the TS context 6261325fc9f4SBarry Smith . A,B - Jacobian matrices 6262325fc9f4SBarry Smith . func - function evaluation routine 6263325fc9f4SBarry Smith - ctx - user context 6264325fc9f4SBarry Smith 6265325fc9f4SBarry Smith Calling sequence of func: 62660910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 6267325fc9f4SBarry Smith 6268325fc9f4SBarry Smith 6269325fc9f4SBarry Smith Level: developer 6270325fc9f4SBarry Smith 6271325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6272325fc9f4SBarry Smith 6273325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6274325fc9f4SBarry Smith */ 6275cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 6276325fc9f4SBarry Smith { 6277325fc9f4SBarry Smith PetscErrorCode ierr; 6278325fc9f4SBarry Smith TSMatlabContext *sctx; 6279325fc9f4SBarry Smith 6280325fc9f4SBarry Smith PetscFunctionBegin; 6281325fc9f4SBarry Smith /* currently sctx is memory bleed */ 6282325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 6283325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6284325fc9f4SBarry Smith /* 6285325fc9f4SBarry Smith This should work, but it doesn't 6286325fc9f4SBarry Smith sctx->ctx = ctx; 6287325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6288325fc9f4SBarry Smith */ 6289325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6290bbd56ea5SKarl Rupp 6291cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 6292325fc9f4SBarry Smith PetscFunctionReturn(0); 6293325fc9f4SBarry Smith } 6294325fc9f4SBarry Smith 6295b5b1a830SBarry Smith #undef __FUNCT__ 6296b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 6297b5b1a830SBarry Smith /* 6298b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 6299b5b1a830SBarry Smith 6300b5b1a830SBarry Smith Collective on TS 6301b5b1a830SBarry Smith 6302b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6303b5b1a830SBarry Smith @*/ 63040910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 6305b5b1a830SBarry Smith { 6306b5b1a830SBarry Smith PetscErrorCode ierr; 6307b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6308a530c242SBarry Smith int nlhs = 1,nrhs = 6; 6309b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 6310b5b1a830SBarry Smith long long int lx = 0,ls = 0; 6311b5b1a830SBarry Smith 6312b5b1a830SBarry Smith PetscFunctionBegin; 6313cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 63140910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 6315b5b1a830SBarry Smith 6316cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 63170910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 6318bbd56ea5SKarl Rupp 6319b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6320b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 6321b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 6322b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 6323b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 6324b5b1a830SBarry Smith prhs[5] = sctx->ctx; 6325b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 6326b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6327b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 6328b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 6329b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 6330b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 6331b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 6332b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 6333b5b1a830SBarry Smith PetscFunctionReturn(0); 6334b5b1a830SBarry Smith } 6335b5b1a830SBarry Smith 6336b5b1a830SBarry Smith 6337b5b1a830SBarry Smith #undef __FUNCT__ 6338b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 6339b5b1a830SBarry Smith /* 6340b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 6341b5b1a830SBarry Smith 6342b5b1a830SBarry Smith Level: developer 6343b5b1a830SBarry Smith 6344b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 6345b5b1a830SBarry Smith 6346b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6347b5b1a830SBarry Smith */ 6348cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 6349b5b1a830SBarry Smith { 6350b5b1a830SBarry Smith PetscErrorCode ierr; 6351b5b1a830SBarry Smith TSMatlabContext *sctx; 6352b5b1a830SBarry Smith 6353b5b1a830SBarry Smith PetscFunctionBegin; 6354b5b1a830SBarry Smith /* currently sctx is memory bleed */ 6355b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 6356b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6357b5b1a830SBarry Smith /* 6358b5b1a830SBarry Smith This should work, but it doesn't 6359b5b1a830SBarry Smith sctx->ctx = ctx; 6360b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6361b5b1a830SBarry Smith */ 6362b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6363bbd56ea5SKarl Rupp 63640298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 6365b5b1a830SBarry Smith PetscFunctionReturn(0); 6366b5b1a830SBarry Smith } 6367325fc9f4SBarry Smith #endif 6368b3603a34SBarry Smith 6369b3603a34SBarry Smith #undef __FUNCT__ 63704f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 6371b3603a34SBarry Smith /*@C 63724f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 6373b3603a34SBarry Smith in a time based line graph 6374b3603a34SBarry Smith 6375b3603a34SBarry Smith Collective on TS 6376b3603a34SBarry Smith 6377b3603a34SBarry Smith Input Parameters: 6378b3603a34SBarry Smith + ts - the TS context 6379b3603a34SBarry Smith . step - current time-step 6380b3603a34SBarry Smith . ptime - current time 63817db568b7SBarry Smith . u - current solution 63827db568b7SBarry Smith - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 6383b3603a34SBarry Smith 6384b3d3934dSBarry Smith Options Database: 63859ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 6386b3d3934dSBarry Smith 6387b3603a34SBarry Smith Level: intermediate 6388b3603a34SBarry Smith 63896934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component solutions in a separate window 6390b3603a34SBarry Smith 6391b3603a34SBarry Smith .keywords: TS, vector, monitor, view 6392b3603a34SBarry Smith 63937db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 63947db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 63957db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 63967db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 6397b3603a34SBarry Smith @*/ 63987db568b7SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6399b3603a34SBarry Smith { 6400b3603a34SBarry Smith PetscErrorCode ierr; 64017db568b7SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 6402b3603a34SBarry Smith const PetscScalar *yy; 640380666b62SBarry Smith Vec v; 6404b3603a34SBarry Smith 6405b3603a34SBarry Smith PetscFunctionBegin; 640663e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 640758ff32f7SBarry Smith if (!step) { 6408a9f9c1f6SBarry Smith PetscDrawAxis axis; 64096934998bSLisandro Dalcin PetscInt dim; 6410a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6411a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 6412387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 6413387f4636SBarry Smith char **displaynames; 6414387f4636SBarry Smith PetscBool flg; 6415387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6416387f4636SBarry Smith ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr); 6417387f4636SBarry Smith ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 6418c5929fdfSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 6419387f4636SBarry Smith if (flg) { 6420a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 6421387f4636SBarry Smith } 6422387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 6423387f4636SBarry Smith } 6424387f4636SBarry Smith if (ctx->displaynames) { 6425387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 6426387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 6427387f4636SBarry Smith } else if (ctx->names) { 64280910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 64290b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6430387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 6431b0bc92c6SBarry Smith } else { 6432b0bc92c6SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6433b0bc92c6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6434387f4636SBarry Smith } 64350b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 643658ff32f7SBarry Smith } 64376934998bSLisandro Dalcin 64386934998bSLisandro Dalcin if (!ctx->transform) v = u; 64396934998bSLisandro Dalcin else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);} 644080666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 64416934998bSLisandro Dalcin if (ctx->displaynames) { 64426934998bSLisandro Dalcin PetscInt i; 64436934998bSLisandro Dalcin for (i=0; i<ctx->ndisplayvariables; i++) 64446934998bSLisandro Dalcin ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]); 64456934998bSLisandro Dalcin ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 64466934998bSLisandro Dalcin } else { 6447e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6448e3efe391SJed Brown PetscInt i,n; 64496934998bSLisandro Dalcin PetscReal *yreal; 645080666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 6451785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6452e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 64530b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6454e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6455e3efe391SJed Brown #else 64560b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6457e3efe391SJed Brown #endif 645880666b62SBarry Smith } 64596934998bSLisandro Dalcin ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 64606934998bSLisandro Dalcin if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);} 64616934998bSLisandro Dalcin 6462b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 64630b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 64646934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 64653923b477SBarry Smith } 6466b3603a34SBarry Smith PetscFunctionReturn(0); 6467b3603a34SBarry Smith } 6468b3603a34SBarry Smith 6469387f4636SBarry Smith 6470b3603a34SBarry Smith #undef __FUNCT__ 647131152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames" 6472b037adc7SBarry Smith /*@C 647331152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6474b037adc7SBarry Smith 6475b037adc7SBarry Smith Collective on TS 6476b037adc7SBarry Smith 6477b037adc7SBarry Smith Input Parameters: 6478b037adc7SBarry Smith + ts - the TS context 6479b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 6480b037adc7SBarry Smith 6481b037adc7SBarry Smith Level: intermediate 6482b037adc7SBarry Smith 64837db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 64847db568b7SBarry Smith 6485b037adc7SBarry Smith .keywords: TS, vector, monitor, view 6486b037adc7SBarry Smith 6487a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 6488b037adc7SBarry Smith @*/ 648931152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 6490b037adc7SBarry Smith { 6491b037adc7SBarry Smith PetscErrorCode ierr; 6492b037adc7SBarry Smith PetscInt i; 6493b037adc7SBarry Smith 6494b037adc7SBarry Smith PetscFunctionBegin; 6495b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6496b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 64975537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 6498b3d3934dSBarry Smith break; 6499b3d3934dSBarry Smith } 6500b3d3934dSBarry Smith } 6501b3d3934dSBarry Smith PetscFunctionReturn(0); 6502b3d3934dSBarry Smith } 6503b3d3934dSBarry Smith 6504b3d3934dSBarry Smith #undef __FUNCT__ 6505e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames" 6506e673d494SBarry Smith /*@C 6507e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6508e673d494SBarry Smith 6509e673d494SBarry Smith Collective on TS 6510e673d494SBarry Smith 6511e673d494SBarry Smith Input Parameters: 6512e673d494SBarry Smith + ts - the TS context 6513e673d494SBarry Smith - names - the names of the components, final string must be NULL 6514e673d494SBarry Smith 6515e673d494SBarry Smith Level: intermediate 6516e673d494SBarry Smith 6517e673d494SBarry Smith .keywords: TS, vector, monitor, view 6518e673d494SBarry Smith 6519a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 6520e673d494SBarry Smith @*/ 6521e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 6522e673d494SBarry Smith { 6523e673d494SBarry Smith PetscErrorCode ierr; 6524e673d494SBarry Smith 6525e673d494SBarry Smith PetscFunctionBegin; 6526e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 6527e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 6528e673d494SBarry Smith PetscFunctionReturn(0); 6529e673d494SBarry Smith } 6530e673d494SBarry Smith 6531e673d494SBarry Smith #undef __FUNCT__ 6532b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames" 6533b3d3934dSBarry Smith /*@C 6534b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 6535b3d3934dSBarry Smith 6536b3d3934dSBarry Smith Collective on TS 6537b3d3934dSBarry Smith 6538b3d3934dSBarry Smith Input Parameter: 6539b3d3934dSBarry Smith . ts - the TS context 6540b3d3934dSBarry Smith 6541b3d3934dSBarry Smith Output Parameter: 6542b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 6543b3d3934dSBarry Smith 6544b3d3934dSBarry Smith Level: intermediate 6545b3d3934dSBarry Smith 65467db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 65477db568b7SBarry Smith 6548b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6549b3d3934dSBarry Smith 6550b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6551b3d3934dSBarry Smith @*/ 6552b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 6553b3d3934dSBarry Smith { 6554b3d3934dSBarry Smith PetscInt i; 6555b3d3934dSBarry Smith 6556b3d3934dSBarry Smith PetscFunctionBegin; 6557b3d3934dSBarry Smith *names = NULL; 6558b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6559b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 65605537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 6561b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 6562b3d3934dSBarry Smith break; 6563387f4636SBarry Smith } 6564387f4636SBarry Smith } 6565387f4636SBarry Smith PetscFunctionReturn(0); 6566387f4636SBarry Smith } 6567387f4636SBarry Smith 6568387f4636SBarry Smith #undef __FUNCT__ 6569a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables" 6570a66092f1SBarry Smith /*@C 6571a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 6572a66092f1SBarry Smith 6573a66092f1SBarry Smith Collective on TS 6574a66092f1SBarry Smith 6575a66092f1SBarry Smith Input Parameters: 6576a66092f1SBarry Smith + ctx - the TSMonitorLG context 6577a66092f1SBarry Smith . displaynames - the names of the components, final string must be NULL 6578a66092f1SBarry Smith 6579a66092f1SBarry Smith Level: intermediate 6580a66092f1SBarry Smith 6581a66092f1SBarry Smith .keywords: TS, vector, monitor, view 6582a66092f1SBarry Smith 6583a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6584a66092f1SBarry Smith @*/ 6585a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 6586a66092f1SBarry Smith { 6587a66092f1SBarry Smith PetscInt j = 0,k; 6588a66092f1SBarry Smith PetscErrorCode ierr; 6589a66092f1SBarry Smith 6590a66092f1SBarry Smith PetscFunctionBegin; 6591a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 6592a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 6593a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 6594a66092f1SBarry Smith while (displaynames[j]) j++; 6595a66092f1SBarry Smith ctx->ndisplayvariables = j; 6596a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 6597a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 6598a66092f1SBarry Smith j = 0; 6599a66092f1SBarry Smith while (displaynames[j]) { 6600a66092f1SBarry Smith k = 0; 6601a66092f1SBarry Smith while (ctx->names[k]) { 6602a66092f1SBarry Smith PetscBool flg; 6603a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 6604a66092f1SBarry Smith if (flg) { 6605a66092f1SBarry Smith ctx->displayvariables[j] = k; 6606a66092f1SBarry Smith break; 6607a66092f1SBarry Smith } 6608a66092f1SBarry Smith k++; 6609a66092f1SBarry Smith } 6610a66092f1SBarry Smith j++; 6611a66092f1SBarry Smith } 6612a66092f1SBarry Smith PetscFunctionReturn(0); 6613a66092f1SBarry Smith } 6614a66092f1SBarry Smith 6615a66092f1SBarry Smith 6616a66092f1SBarry Smith #undef __FUNCT__ 6617387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables" 6618387f4636SBarry Smith /*@C 6619387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 6620387f4636SBarry Smith 6621387f4636SBarry Smith Collective on TS 6622387f4636SBarry Smith 6623387f4636SBarry Smith Input Parameters: 6624387f4636SBarry Smith + ts - the TS context 6625387f4636SBarry Smith . displaynames - the names of the components, final string must be NULL 6626387f4636SBarry Smith 66277db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 66287db568b7SBarry Smith 6629387f4636SBarry Smith Level: intermediate 6630387f4636SBarry Smith 6631387f4636SBarry Smith .keywords: TS, vector, monitor, view 6632387f4636SBarry Smith 6633387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6634387f4636SBarry Smith @*/ 6635387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 6636387f4636SBarry Smith { 6637a66092f1SBarry Smith PetscInt i; 6638387f4636SBarry Smith PetscErrorCode ierr; 6639387f4636SBarry Smith 6640387f4636SBarry Smith PetscFunctionBegin; 6641387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6642387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 66435537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 6644b3d3934dSBarry Smith break; 6645b037adc7SBarry Smith } 6646b037adc7SBarry Smith } 6647b037adc7SBarry Smith PetscFunctionReturn(0); 6648b037adc7SBarry Smith } 6649b037adc7SBarry Smith 6650b037adc7SBarry Smith #undef __FUNCT__ 665180666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform" 665280666b62SBarry Smith /*@C 665380666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 665480666b62SBarry Smith 665580666b62SBarry Smith Collective on TS 665680666b62SBarry Smith 665780666b62SBarry Smith Input Parameters: 665880666b62SBarry Smith + ts - the TS context 665980666b62SBarry Smith . transform - the transform function 66607684fa3eSBarry Smith . destroy - function to destroy the optional context 666180666b62SBarry Smith - ctx - optional context used by transform function 666280666b62SBarry Smith 66637db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 66647db568b7SBarry Smith 666580666b62SBarry Smith Level: intermediate 666680666b62SBarry Smith 666780666b62SBarry Smith .keywords: TS, vector, monitor, view 666880666b62SBarry Smith 6669a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 667080666b62SBarry Smith @*/ 66717684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 667280666b62SBarry Smith { 667380666b62SBarry Smith PetscInt i; 6674a66092f1SBarry Smith PetscErrorCode ierr; 667580666b62SBarry Smith 667680666b62SBarry Smith PetscFunctionBegin; 667780666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 667880666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 66795537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 668080666b62SBarry Smith } 668180666b62SBarry Smith } 668280666b62SBarry Smith PetscFunctionReturn(0); 668380666b62SBarry Smith } 668480666b62SBarry Smith 668580666b62SBarry Smith #undef __FUNCT__ 6686e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform" 6687e673d494SBarry Smith /*@C 6688e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 6689e673d494SBarry Smith 6690e673d494SBarry Smith Collective on TSLGCtx 6691e673d494SBarry Smith 6692e673d494SBarry Smith Input Parameters: 6693e673d494SBarry Smith + ts - the TS context 6694e673d494SBarry Smith . transform - the transform function 66957684fa3eSBarry Smith . destroy - function to destroy the optional context 6696e673d494SBarry Smith - ctx - optional context used by transform function 6697e673d494SBarry Smith 6698e673d494SBarry Smith Level: intermediate 6699e673d494SBarry Smith 6700e673d494SBarry Smith .keywords: TS, vector, monitor, view 6701e673d494SBarry Smith 6702a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 6703e673d494SBarry Smith @*/ 67047684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 6705e673d494SBarry Smith { 6706e673d494SBarry Smith PetscFunctionBegin; 6707e673d494SBarry Smith ctx->transform = transform; 67087684fa3eSBarry Smith ctx->transformdestroy = destroy; 6709e673d494SBarry Smith ctx->transformctx = tctx; 6710e673d494SBarry Smith PetscFunctionReturn(0); 6711e673d494SBarry Smith } 6712e673d494SBarry Smith 6713b3603a34SBarry Smith #undef __FUNCT__ 67144f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 6715ef20d060SBarry Smith /*@C 67164f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 6717ef20d060SBarry Smith in a time based line graph 6718ef20d060SBarry Smith 6719ef20d060SBarry Smith Collective on TS 6720ef20d060SBarry Smith 6721ef20d060SBarry Smith Input Parameters: 6722ef20d060SBarry Smith + ts - the TS context 6723ef20d060SBarry Smith . step - current time-step 6724ef20d060SBarry Smith . ptime - current time 67257db568b7SBarry Smith . u - current solution 67267db568b7SBarry Smith - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 6727ef20d060SBarry Smith 6728ef20d060SBarry Smith Level: intermediate 6729ef20d060SBarry Smith 67306934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component errors in a separate window 6731abd5a294SJed Brown 6732abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 6733abd5a294SJed Brown 6734abd5a294SJed Brown Options Database Keys: 67354f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 6736ef20d060SBarry Smith 6737ef20d060SBarry Smith .keywords: TS, vector, monitor, view 6738ef20d060SBarry Smith 6739abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 6740ef20d060SBarry Smith @*/ 67410910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 6742ef20d060SBarry Smith { 6743ef20d060SBarry Smith PetscErrorCode ierr; 67440b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 6745ef20d060SBarry Smith const PetscScalar *yy; 6746ef20d060SBarry Smith Vec y; 6747ef20d060SBarry Smith 6748ef20d060SBarry Smith PetscFunctionBegin; 6749a9f9c1f6SBarry Smith if (!step) { 6750a9f9c1f6SBarry Smith PetscDrawAxis axis; 67516934998bSLisandro Dalcin PetscInt dim; 6752a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 67531ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 67540910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6755a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6756a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6757a9f9c1f6SBarry Smith } 67580910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 6759ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 67600910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 6761ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 6762e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6763e3efe391SJed Brown { 6764e3efe391SJed Brown PetscReal *yreal; 6765e3efe391SJed Brown PetscInt i,n; 6766e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 6767785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6768e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 67690b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6770e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6771e3efe391SJed Brown } 6772e3efe391SJed Brown #else 67730b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6774e3efe391SJed Brown #endif 6775ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 6776ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 6777b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 67780b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 67796934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 67803923b477SBarry Smith } 6781ef20d060SBarry Smith PetscFunctionReturn(0); 6782ef20d060SBarry Smith } 6783ef20d060SBarry Smith 6784201da799SBarry Smith #undef __FUNCT__ 6785201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 6786201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6787201da799SBarry Smith { 6788201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6789201da799SBarry Smith PetscReal x = ptime,y; 6790201da799SBarry Smith PetscErrorCode ierr; 6791201da799SBarry Smith PetscInt its; 6792201da799SBarry Smith 6793201da799SBarry Smith PetscFunctionBegin; 679463e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 6795201da799SBarry Smith if (!n) { 6796201da799SBarry Smith PetscDrawAxis axis; 6797201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6798201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 6799201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6800201da799SBarry Smith ctx->snes_its = 0; 6801201da799SBarry Smith } 6802201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 6803201da799SBarry Smith y = its - ctx->snes_its; 6804201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 68053fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6806201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 68076934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 6808201da799SBarry Smith } 6809201da799SBarry Smith ctx->snes_its = its; 6810201da799SBarry Smith PetscFunctionReturn(0); 6811201da799SBarry Smith } 6812201da799SBarry Smith 6813201da799SBarry Smith #undef __FUNCT__ 6814201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 6815201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6816201da799SBarry Smith { 6817201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6818201da799SBarry Smith PetscReal x = ptime,y; 6819201da799SBarry Smith PetscErrorCode ierr; 6820201da799SBarry Smith PetscInt its; 6821201da799SBarry Smith 6822201da799SBarry Smith PetscFunctionBegin; 682363e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 6824201da799SBarry Smith if (!n) { 6825201da799SBarry Smith PetscDrawAxis axis; 6826201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6827201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 6828201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6829201da799SBarry Smith ctx->ksp_its = 0; 6830201da799SBarry Smith } 6831201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 6832201da799SBarry Smith y = its - ctx->ksp_its; 6833201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 683499fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6835201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 68366934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 6837201da799SBarry Smith } 6838201da799SBarry Smith ctx->ksp_its = its; 6839201da799SBarry Smith PetscFunctionReturn(0); 6840201da799SBarry Smith } 6841f9c1d6abSBarry Smith 6842f9c1d6abSBarry Smith #undef __FUNCT__ 6843f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 6844f9c1d6abSBarry Smith /*@ 6845f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 6846f9c1d6abSBarry Smith 6847f9c1d6abSBarry Smith Collective on TS and Vec 6848f9c1d6abSBarry Smith 6849f9c1d6abSBarry Smith Input Parameters: 6850f9c1d6abSBarry Smith + ts - the TS context 6851f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 6852f9c1d6abSBarry Smith 6853f9c1d6abSBarry Smith Output Parameters: 6854f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 6855f9c1d6abSBarry Smith 6856f9c1d6abSBarry Smith Level: developer 6857f9c1d6abSBarry Smith 6858f9c1d6abSBarry Smith .keywords: TS, compute 6859f9c1d6abSBarry Smith 6860f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 6861f9c1d6abSBarry Smith @*/ 6862f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 6863f9c1d6abSBarry Smith { 6864f9c1d6abSBarry Smith PetscErrorCode ierr; 6865f9c1d6abSBarry Smith 6866f9c1d6abSBarry Smith PetscFunctionBegin; 6867f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6868ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 6869f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 6870f9c1d6abSBarry Smith PetscFunctionReturn(0); 6871f9c1d6abSBarry Smith } 687224655328SShri 6873b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 6874b3d3934dSBarry Smith #undef __FUNCT__ 6875b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate" 6876b3d3934dSBarry Smith /*@C 6877b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 6878b3d3934dSBarry Smith 6879b3d3934dSBarry Smith Collective on TS 6880b3d3934dSBarry Smith 6881b3d3934dSBarry Smith Input Parameters: 6882b3d3934dSBarry Smith . ts - the ODE solver object 6883b3d3934dSBarry Smith 6884b3d3934dSBarry Smith Output Parameter: 6885b3d3934dSBarry Smith . ctx - the context 6886b3d3934dSBarry Smith 6887b3d3934dSBarry Smith Level: intermediate 6888b3d3934dSBarry Smith 6889b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso 6890b3d3934dSBarry Smith 6891b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 6892b3d3934dSBarry Smith 6893b3d3934dSBarry Smith @*/ 6894b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 6895b3d3934dSBarry Smith { 6896b3d3934dSBarry Smith PetscErrorCode ierr; 6897b3d3934dSBarry Smith 6898b3d3934dSBarry Smith PetscFunctionBegin; 6899a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 6900b3d3934dSBarry Smith PetscFunctionReturn(0); 6901b3d3934dSBarry Smith } 6902b3d3934dSBarry Smith 6903b3d3934dSBarry Smith #undef __FUNCT__ 6904b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope" 6905b3d3934dSBarry Smith /*@C 6906b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 6907b3d3934dSBarry Smith 6908b3d3934dSBarry Smith Collective on TS 6909b3d3934dSBarry Smith 6910b3d3934dSBarry Smith Input Parameters: 6911b3d3934dSBarry Smith + ts - the TS context 6912b3d3934dSBarry Smith . step - current time-step 6913b3d3934dSBarry Smith . ptime - current time 69147db568b7SBarry Smith . u - current solution 69157db568b7SBarry Smith - dctx - the envelope context 6916b3d3934dSBarry Smith 6917b3d3934dSBarry Smith Options Database: 6918b3d3934dSBarry Smith . -ts_monitor_envelope 6919b3d3934dSBarry Smith 6920b3d3934dSBarry Smith Level: intermediate 6921b3d3934dSBarry Smith 6922b3d3934dSBarry Smith Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 6923b3d3934dSBarry Smith 6924b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6925b3d3934dSBarry Smith 69267db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 6927b3d3934dSBarry Smith @*/ 69287db568b7SBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6929b3d3934dSBarry Smith { 6930b3d3934dSBarry Smith PetscErrorCode ierr; 69317db568b7SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 6932b3d3934dSBarry Smith 6933b3d3934dSBarry Smith PetscFunctionBegin; 6934b3d3934dSBarry Smith if (!ctx->max) { 6935b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 6936b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 6937b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 6938b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 6939b3d3934dSBarry Smith } else { 6940b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 6941b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 6942b3d3934dSBarry Smith } 6943b3d3934dSBarry Smith PetscFunctionReturn(0); 6944b3d3934dSBarry Smith } 6945b3d3934dSBarry Smith 6946b3d3934dSBarry Smith 6947b3d3934dSBarry Smith #undef __FUNCT__ 6948b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds" 6949b3d3934dSBarry Smith /*@C 6950b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 6951b3d3934dSBarry Smith 6952b3d3934dSBarry Smith Collective on TS 6953b3d3934dSBarry Smith 6954b3d3934dSBarry Smith Input Parameter: 6955b3d3934dSBarry Smith . ts - the TS context 6956b3d3934dSBarry Smith 6957b3d3934dSBarry Smith Output Parameter: 6958b3d3934dSBarry Smith + max - the maximum values 6959b3d3934dSBarry Smith - min - the minimum values 6960b3d3934dSBarry Smith 69617db568b7SBarry Smith Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 69627db568b7SBarry Smith 6963b3d3934dSBarry Smith Level: intermediate 6964b3d3934dSBarry Smith 6965b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6966b3d3934dSBarry Smith 6967b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6968b3d3934dSBarry Smith @*/ 6969b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 6970b3d3934dSBarry Smith { 6971b3d3934dSBarry Smith PetscInt i; 6972b3d3934dSBarry Smith 6973b3d3934dSBarry Smith PetscFunctionBegin; 6974b3d3934dSBarry Smith if (max) *max = NULL; 6975b3d3934dSBarry Smith if (min) *min = NULL; 6976b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6977b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 69785537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 6979b3d3934dSBarry Smith if (max) *max = ctx->max; 6980b3d3934dSBarry Smith if (min) *min = ctx->min; 6981b3d3934dSBarry Smith break; 6982b3d3934dSBarry Smith } 6983b3d3934dSBarry Smith } 6984b3d3934dSBarry Smith PetscFunctionReturn(0); 6985b3d3934dSBarry Smith } 6986b3d3934dSBarry Smith 6987b3d3934dSBarry Smith #undef __FUNCT__ 6988b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy" 6989b3d3934dSBarry Smith /*@C 6990b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 6991b3d3934dSBarry Smith 6992b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 6993b3d3934dSBarry Smith 6994b3d3934dSBarry Smith Input Parameter: 6995b3d3934dSBarry Smith . ctx - the monitor context 6996b3d3934dSBarry Smith 6997b3d3934dSBarry Smith Level: intermediate 6998b3d3934dSBarry Smith 6999b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy 7000b3d3934dSBarry Smith 70017db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 7002b3d3934dSBarry Smith @*/ 7003b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 7004b3d3934dSBarry Smith { 7005b3d3934dSBarry Smith PetscErrorCode ierr; 7006b3d3934dSBarry Smith 7007b3d3934dSBarry Smith PetscFunctionBegin; 7008b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 7009b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 7010b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 7011b3d3934dSBarry Smith PetscFunctionReturn(0); 7012b3d3934dSBarry Smith } 7013f2dee214SBarry Smith 701424655328SShri #undef __FUNCT__ 701524655328SShri #define __FUNCT__ "TSRollBack" 701624655328SShri /*@ 701724655328SShri TSRollBack - Rolls back one time step 701824655328SShri 701924655328SShri Collective on TS 702024655328SShri 702124655328SShri Input Parameter: 702224655328SShri . ts - the TS context obtained from TSCreate() 702324655328SShri 702424655328SShri Level: advanced 702524655328SShri 702624655328SShri .keywords: TS, timestep, rollback 702724655328SShri 702824655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 702924655328SShri @*/ 703024655328SShri PetscErrorCode TSRollBack(TS ts) 703124655328SShri { 703224655328SShri PetscErrorCode ierr; 703324655328SShri 703424655328SShri PetscFunctionBegin; 703524655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7036b3de5cdeSLisandro Dalcin if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called"); 703724655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 703824655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 703924655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 704024655328SShri ts->ptime = ts->ptime_prev; 7041be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 7042be5899b3SLisandro Dalcin ts->steps--; ts->total_steps--; 7043b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 704424655328SShri PetscFunctionReturn(0); 704524655328SShri } 7046aeb4809dSShri Abhyankar 7047ff22ae23SHong Zhang #undef __FUNCT__ 7048ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages" 7049ff22ae23SHong Zhang /*@ 7050ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 7051ff22ae23SHong Zhang 7052ff22ae23SHong Zhang Input Parameter: 7053ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 7054ff22ae23SHong Zhang 7055ff22ae23SHong Zhang Level: advanced 7056ff22ae23SHong Zhang 7057ff22ae23SHong Zhang .keywords: TS, getstages 7058ff22ae23SHong Zhang 7059ff22ae23SHong Zhang .seealso: TSCreate() 7060ff22ae23SHong Zhang @*/ 7061ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns,Vec **Y) 7062ff22ae23SHong Zhang { 7063ff22ae23SHong Zhang PetscErrorCode ierr; 7064ff22ae23SHong Zhang 7065ff22ae23SHong Zhang PetscFunctionBegin; 7066ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7067ff22ae23SHong Zhang PetscValidPointer(ns,2); 7068ff22ae23SHong Zhang 7069ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 7070ff22ae23SHong Zhang else { 7071ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 7072ff22ae23SHong Zhang } 7073ff22ae23SHong Zhang PetscFunctionReturn(0); 7074ff22ae23SHong Zhang } 7075ff22ae23SHong Zhang 7076847ff0e1SMatthew G. Knepley #undef __FUNCT__ 7077847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor" 7078847ff0e1SMatthew G. Knepley /*@C 7079847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 7080847ff0e1SMatthew G. Knepley 7081847ff0e1SMatthew G. Knepley Collective on SNES 7082847ff0e1SMatthew G. Knepley 7083847ff0e1SMatthew G. Knepley Input Parameters: 7084847ff0e1SMatthew G. Knepley + ts - the TS context 7085847ff0e1SMatthew G. Knepley . t - current timestep 7086847ff0e1SMatthew G. Knepley . U - state vector 7087847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 7088847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 7089847ff0e1SMatthew G. Knepley - ctx - an optional user context 7090847ff0e1SMatthew G. Knepley 7091847ff0e1SMatthew G. Knepley Output Parameters: 7092847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 7093847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 7094847ff0e1SMatthew G. Knepley 7095847ff0e1SMatthew G. Knepley Level: intermediate 7096847ff0e1SMatthew G. Knepley 7097847ff0e1SMatthew G. Knepley Notes: 7098847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 7099847ff0e1SMatthew G. Knepley 7100847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 7101847ff0e1SMatthew G. Knepley 7102847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 7103847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 7104847ff0e1SMatthew G. Knepley 7105847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 7106847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 7107847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 7108847ff0e1SMatthew G. Knepley 7109847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse 7110847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 7111847ff0e1SMatthew G. Knepley @*/ 7112847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 7113847ff0e1SMatthew G. Knepley { 7114847ff0e1SMatthew G. Knepley SNES snes; 7115847ff0e1SMatthew G. Knepley MatFDColoring color; 7116847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 7117847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 7118847ff0e1SMatthew G. Knepley 7119847ff0e1SMatthew G. Knepley PetscFunctionBegin; 7120c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 7121847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 7122847ff0e1SMatthew G. Knepley if (!color) { 7123847ff0e1SMatthew G. Knepley DM dm; 7124847ff0e1SMatthew G. Knepley ISColoring iscoloring; 7125847ff0e1SMatthew G. Knepley 7126847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 7127847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 7128847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 7129847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 7130847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7131847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7132847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7133847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7134847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7135847ff0e1SMatthew G. Knepley } else { 7136847ff0e1SMatthew G. Knepley MatColoring mc; 7137847ff0e1SMatthew G. Knepley 7138847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 7139847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 7140847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 7141847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 7142847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 7143847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 7144847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7145847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7146847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7147847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7148847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7149847ff0e1SMatthew G. Knepley } 7150847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 7151847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 7152847ff0e1SMatthew G. Knepley } 7153847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 7154847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 7155847ff0e1SMatthew G. Knepley if (J != B) { 7156847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7157847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7158847ff0e1SMatthew G. Knepley } 7159847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 7160847ff0e1SMatthew G. Knepley } 716193b34091SDebojyoti Ghosh 716293b34091SDebojyoti Ghosh #undef __FUNCT__ 7163cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSSetFunctionDomainError" 7164cb9d8021SPierre Barbier de Reuille /*@ 7165cb9d8021SPierre Barbier de Reuille TSSetFunctionDomainError - Set the function testing if the current state vector is valid 7166cb9d8021SPierre Barbier de Reuille 7167cb9d8021SPierre Barbier de Reuille Input Parameters: 7168cb9d8021SPierre Barbier de Reuille ts - the TS context 7169cb9d8021SPierre Barbier de Reuille func - function called within TSFunctionDomainError 7170cb9d8021SPierre Barbier de Reuille 7171cb9d8021SPierre Barbier de Reuille Level: intermediate 7172cb9d8021SPierre Barbier de Reuille 7173cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain 7174cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError() 7175cb9d8021SPierre Barbier de Reuille @*/ 7176cb9d8021SPierre Barbier de Reuille 7177d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 7178cb9d8021SPierre Barbier de Reuille { 7179cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7180cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7181cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 7182cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7183cb9d8021SPierre Barbier de Reuille } 7184cb9d8021SPierre Barbier de Reuille 7185cb9d8021SPierre Barbier de Reuille #undef __FUNCT__ 7186cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSFunctionDomainError" 7187cb9d8021SPierre Barbier de Reuille /*@ 7188cb9d8021SPierre Barbier de Reuille TSFunctionDomainError - Check if the current state is valid 7189cb9d8021SPierre Barbier de Reuille 7190cb9d8021SPierre Barbier de Reuille Input Parameters: 7191cb9d8021SPierre Barbier de Reuille ts - the TS context 7192cb9d8021SPierre Barbier de Reuille stagetime - time of the simulation 7193d183316bSPierre Barbier de Reuille Y - state vector to check. 7194cb9d8021SPierre Barbier de Reuille 7195cb9d8021SPierre Barbier de Reuille Output Parameter: 7196cb9d8021SPierre Barbier de Reuille accept - Set to PETSC_FALSE if the current state vector is valid. 7197cb9d8021SPierre Barbier de Reuille 7198cb9d8021SPierre Barbier de Reuille Note: 7199cb9d8021SPierre Barbier de Reuille This function should be used to ensure the state is in a valid part of the space. 7200cb9d8021SPierre Barbier de Reuille For example, one can ensure here all values are positive. 720196a0c994SBarry Smith 720296a0c994SBarry Smith Level: advanced 7203cb9d8021SPierre Barbier de Reuille @*/ 7204d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 7205cb9d8021SPierre Barbier de Reuille { 7206cb9d8021SPierre Barbier de Reuille PetscErrorCode ierr; 7207cb9d8021SPierre Barbier de Reuille 7208cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7209cb9d8021SPierre Barbier de Reuille 7210cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7211cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 7212cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 7213d183316bSPierre Barbier de Reuille PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 7214cb9d8021SPierre Barbier de Reuille } 7215cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7216cb9d8021SPierre Barbier de Reuille } 72171ceb14c0SBarry Smith 72181ceb14c0SBarry Smith #undef __FUNCT__ 7219e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone" 722093b34091SDebojyoti Ghosh /*@C 7221e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 722293b34091SDebojyoti Ghosh 722393b34091SDebojyoti Ghosh Collective on MPI_Comm 722493b34091SDebojyoti Ghosh 722593b34091SDebojyoti Ghosh Input Parameter: 722693b34091SDebojyoti Ghosh . tsin - The input TS 722793b34091SDebojyoti Ghosh 722893b34091SDebojyoti Ghosh Output Parameter: 7229e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 723093b34091SDebojyoti Ghosh 72315eca1a21SEmil Constantinescu Notes: 72325eca1a21SEmil 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. 72335eca1a21SEmil Constantinescu 7234baa10174SEmil 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); 72355eca1a21SEmil Constantinescu 72365eca1a21SEmil Constantinescu Level: developer 723793b34091SDebojyoti Ghosh 7238e5168f73SEmil Constantinescu .keywords: TS, clone 7239e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 724093b34091SDebojyoti Ghosh @*/ 7241baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 724293b34091SDebojyoti Ghosh { 724393b34091SDebojyoti Ghosh TS t; 724493b34091SDebojyoti Ghosh PetscErrorCode ierr; 7245dc846ba4SSatish Balay SNES snes_start; 7246dc846ba4SSatish Balay DM dm; 7247dc846ba4SSatish Balay TSType type; 724893b34091SDebojyoti Ghosh 724993b34091SDebojyoti Ghosh PetscFunctionBegin; 725093b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 725193b34091SDebojyoti Ghosh *tsout = NULL; 725293b34091SDebojyoti Ghosh 72537a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 725493b34091SDebojyoti Ghosh 725593b34091SDebojyoti Ghosh /* General TS description */ 725693b34091SDebojyoti Ghosh t->numbermonitors = 0; 725793b34091SDebojyoti Ghosh t->setupcalled = 0; 725893b34091SDebojyoti Ghosh t->ksp_its = 0; 725993b34091SDebojyoti Ghosh t->snes_its = 0; 726093b34091SDebojyoti Ghosh t->nwork = 0; 726193b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 726293b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 726393b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 726493b34091SDebojyoti Ghosh 726534561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr); 726634561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr); 7267d15a3a53SEmil Constantinescu 726893b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr); 726993b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm);CHKERRQ(ierr); 727093b34091SDebojyoti Ghosh 727193b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 727251699248SLisandro Dalcin ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr); 727393b34091SDebojyoti Ghosh 727493b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 727593b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 727693b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 727793b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 727893b34091SDebojyoti Ghosh t->steps = tsin->steps; 727993b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 728093b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 728193b34091SDebojyoti Ghosh t->atol = tsin->atol; 728293b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 728393b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 728493b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 728593b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 728693b34091SDebojyoti Ghosh 728793b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type);CHKERRQ(ierr); 728893b34091SDebojyoti Ghosh ierr = TSSetType(t,type);CHKERRQ(ierr); 728993b34091SDebojyoti Ghosh 729093b34091SDebojyoti Ghosh t->vec_sol = NULL; 729193b34091SDebojyoti Ghosh 729293b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 729393b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 729493b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 729593b34091SDebojyoti Ghosh 729693b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 729793b34091SDebojyoti Ghosh 72980d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 72990d4fed19SBarry Smith PetscInt i; 73000d4fed19SBarry Smith ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr); 73010d4fed19SBarry Smith for (i=0; i<10; i++) { 73020d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 73030d4fed19SBarry Smith } 73040d4fed19SBarry Smith } 730593b34091SDebojyoti Ghosh *tsout = t; 730693b34091SDebojyoti Ghosh PetscFunctionReturn(0); 730793b34091SDebojyoti Ghosh } 7308