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: 12526d28e4eSEmil Constantinescu + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, 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) { 929214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 930d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 931e1244c69SJed Brown } 93294ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 933e1244c69SJed Brown ts->rhsjacobian.scale = -1; 934e1244c69SJed Brown ts->rhsjacobian.shift = shift; 93594ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 93694ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 93794ab13aaSBarry Smith if (A != B) { 93894ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 93994ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 940316643e7SJed Brown } 941e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 942e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 943e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 94494ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 94594ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 94694ab13aaSBarry Smith if (A != B) { 94794ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 94894ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 949214bc6a2SJed Brown } 950316643e7SJed Brown } 95194ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 95294ab13aaSBarry Smith if (A != B) { 95394ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 954316643e7SJed Brown } 955316643e7SJed Brown } 956316643e7SJed Brown } 95794ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 958316643e7SJed Brown PetscFunctionReturn(0); 959316643e7SJed Brown } 960316643e7SJed Brown 961316643e7SJed Brown #undef __FUNCT__ 9624a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 963d763cef2SBarry Smith /*@C 964d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 965b5abc632SBarry Smith where U_t = G(t,u). 966d763cef2SBarry Smith 9673f9fe445SBarry Smith Logically Collective on TS 968d763cef2SBarry Smith 969d763cef2SBarry Smith Input Parameters: 970d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 9710298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 972d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 973d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 9740298fd71SBarry Smith function evaluation routine (may be NULL) 975d763cef2SBarry Smith 976d763cef2SBarry Smith Calling sequence of func: 97787828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 978d763cef2SBarry Smith 979d763cef2SBarry Smith + t - current timestep 980d763cef2SBarry Smith . u - input vector 981d763cef2SBarry Smith . F - function vector 982d763cef2SBarry Smith - ctx - [optional] user-defined function context 983d763cef2SBarry Smith 984d763cef2SBarry Smith Level: beginner 985d763cef2SBarry Smith 9862bbac0d3SBarry Smith Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 9872bbac0d3SBarry Smith 988d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 989d763cef2SBarry Smith 990ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 991d763cef2SBarry Smith @*/ 992089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 993d763cef2SBarry Smith { 994089b2837SJed Brown PetscErrorCode ierr; 995089b2837SJed Brown SNES snes; 9960298fd71SBarry Smith Vec ralloc = NULL; 99724989b8cSPeter Brune DM dm; 998d763cef2SBarry Smith 999089b2837SJed Brown PetscFunctionBegin; 10000700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1001ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 100224989b8cSPeter Brune 100324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 100424989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 1005089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1006e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 1007e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 1008e856ceecSJed Brown r = ralloc; 1009e856ceecSJed Brown } 1010089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 1011e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1012d763cef2SBarry Smith PetscFunctionReturn(0); 1013d763cef2SBarry Smith } 1014d763cef2SBarry Smith 10154a2ae208SSatish Balay #undef __FUNCT__ 1016ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 1017ef20d060SBarry Smith /*@C 1018abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1019ef20d060SBarry Smith 1020ef20d060SBarry Smith Logically Collective on TS 1021ef20d060SBarry Smith 1022ef20d060SBarry Smith Input Parameters: 1023ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1024ef20d060SBarry Smith . f - routine for evaluating the solution 1025ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 10260298fd71SBarry Smith function evaluation routine (may be NULL) 1027ef20d060SBarry Smith 1028ef20d060SBarry Smith Calling sequence of func: 1029ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 1030ef20d060SBarry Smith 1031ef20d060SBarry Smith + t - current timestep 1032ef20d060SBarry Smith . u - output vector 1033ef20d060SBarry Smith - ctx - [optional] user-defined function context 1034ef20d060SBarry Smith 1035abd5a294SJed Brown Notes: 1036abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1037abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1038abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1039abd5a294SJed Brown 10404f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1041abd5a294SJed Brown 1042ef20d060SBarry Smith Level: beginner 1043ef20d060SBarry Smith 1044ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1045ef20d060SBarry Smith 10469b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 1047ef20d060SBarry Smith @*/ 1048ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1049ef20d060SBarry Smith { 1050ef20d060SBarry Smith PetscErrorCode ierr; 1051ef20d060SBarry Smith DM dm; 1052ef20d060SBarry Smith 1053ef20d060SBarry Smith PetscFunctionBegin; 1054ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1055ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1056ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 1057ef20d060SBarry Smith PetscFunctionReturn(0); 1058ef20d060SBarry Smith } 1059ef20d060SBarry Smith 1060ef20d060SBarry Smith #undef __FUNCT__ 10619b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction" 10629b7cd975SBarry Smith /*@C 10639b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 10649b7cd975SBarry Smith 10659b7cd975SBarry Smith Logically Collective on TS 10669b7cd975SBarry Smith 10679b7cd975SBarry Smith Input Parameters: 10689b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 10699b7cd975SBarry Smith . f - routine for evaluating the forcing function 10709b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 10710298fd71SBarry Smith function evaluation routine (may be NULL) 10729b7cd975SBarry Smith 10739b7cd975SBarry Smith Calling sequence of func: 10749b7cd975SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 10759b7cd975SBarry Smith 10769b7cd975SBarry Smith + t - current timestep 10779b7cd975SBarry Smith . u - output vector 10789b7cd975SBarry Smith - ctx - [optional] user-defined function context 10799b7cd975SBarry Smith 10809b7cd975SBarry Smith Notes: 10819b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 10829b7cd975SBarry Smith create closed-form solutions with a non-physical forcing term. 10839b7cd975SBarry Smith 10849b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 10859b7cd975SBarry Smith 10869b7cd975SBarry Smith Level: beginner 10879b7cd975SBarry Smith 10889b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 10899b7cd975SBarry Smith 10909b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 10919b7cd975SBarry Smith @*/ 1092d56366bfSLisandro Dalcin PetscErrorCode TSSetForcingFunction(TS ts,TSForcingFunction f,void *ctx) 10939b7cd975SBarry Smith { 10949b7cd975SBarry Smith PetscErrorCode ierr; 10959b7cd975SBarry Smith DM dm; 10969b7cd975SBarry Smith 10979b7cd975SBarry Smith PetscFunctionBegin; 10989b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10999b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 11009b7cd975SBarry Smith ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr); 11019b7cd975SBarry Smith PetscFunctionReturn(0); 11029b7cd975SBarry Smith } 11039b7cd975SBarry Smith 11049b7cd975SBarry Smith #undef __FUNCT__ 11054a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 1106d763cef2SBarry Smith /*@C 1107f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1108b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1109d763cef2SBarry Smith 11103f9fe445SBarry Smith Logically Collective on TS 1111d763cef2SBarry Smith 1112d763cef2SBarry Smith Input Parameters: 1113d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1114e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1115e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1116d763cef2SBarry Smith . f - the Jacobian evaluation routine 1117d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 11180298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1119d763cef2SBarry Smith 1120f7ab8db6SBarry Smith Calling sequence of f: 1121ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1122d763cef2SBarry Smith 1123d763cef2SBarry Smith + t - current timestep 1124d763cef2SBarry Smith . u - input vector 1125e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1126e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1127d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1128d763cef2SBarry Smith 11296cd88445SBarry Smith Notes: 11306cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 11316cd88445SBarry Smith 11326cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1133ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1134d763cef2SBarry Smith 1135d763cef2SBarry Smith Level: beginner 1136d763cef2SBarry Smith 1137d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1138d763cef2SBarry Smith 1139ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1140d763cef2SBarry Smith 1141d763cef2SBarry Smith @*/ 1142e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1143d763cef2SBarry Smith { 1144277b19d0SLisandro Dalcin PetscErrorCode ierr; 1145089b2837SJed Brown SNES snes; 114624989b8cSPeter Brune DM dm; 114724989b8cSPeter Brune TSIJacobian ijacobian; 1148277b19d0SLisandro Dalcin 1149d763cef2SBarry Smith PetscFunctionBegin; 11500700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1151e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1152e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1153e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1154e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1155d763cef2SBarry Smith 115624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 115724989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1158e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1159e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1160e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1161e1244c69SJed Brown } 11620298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1163089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11645f659677SPeter Brune if (!ijacobian) { 1165e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 11660e4ef248SJed Brown } 1167e5d3d808SBarry Smith if (Amat) { 1168e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 11690e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1170e5d3d808SBarry Smith ts->Arhs = Amat; 11710e4ef248SJed Brown } 1172e5d3d808SBarry Smith if (Pmat) { 1173e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 11740e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1175e5d3d808SBarry Smith ts->Brhs = Pmat; 11760e4ef248SJed Brown } 1177d763cef2SBarry Smith PetscFunctionReturn(0); 1178d763cef2SBarry Smith } 1179d763cef2SBarry Smith 1180316643e7SJed Brown 1181316643e7SJed Brown #undef __FUNCT__ 1182316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 1183316643e7SJed Brown /*@C 1184b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1185316643e7SJed Brown 11863f9fe445SBarry Smith Logically Collective on TS 1187316643e7SJed Brown 1188316643e7SJed Brown Input Parameters: 1189316643e7SJed Brown + ts - the TS context obtained from TSCreate() 11900298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1191316643e7SJed Brown . f - the function evaluation routine 11920298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1193316643e7SJed Brown 1194316643e7SJed Brown Calling sequence of f: 1195316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1196316643e7SJed Brown 1197316643e7SJed Brown + t - time at step/stage being solved 1198316643e7SJed Brown . u - state vector 1199316643e7SJed Brown . u_t - time derivative of state vector 1200316643e7SJed Brown . F - function vector 1201316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1202316643e7SJed Brown 1203316643e7SJed Brown Important: 12042bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1205316643e7SJed Brown 1206316643e7SJed Brown Level: beginner 1207316643e7SJed Brown 1208316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1209316643e7SJed Brown 1210d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1211316643e7SJed Brown @*/ 121251699248SLisandro Dalcin PetscErrorCode TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx) 1213316643e7SJed Brown { 1214089b2837SJed Brown PetscErrorCode ierr; 1215089b2837SJed Brown SNES snes; 121651699248SLisandro Dalcin Vec ralloc = NULL; 121724989b8cSPeter Brune DM dm; 1218316643e7SJed Brown 1219316643e7SJed Brown PetscFunctionBegin; 12200700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 122151699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 122224989b8cSPeter Brune 122324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 122424989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 122524989b8cSPeter Brune 1226089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 122751699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 122851699248SLisandro Dalcin ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 122951699248SLisandro Dalcin r = ralloc; 1230e856ceecSJed Brown } 123151699248SLisandro Dalcin ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 123251699248SLisandro Dalcin ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1233089b2837SJed Brown PetscFunctionReturn(0); 1234089b2837SJed Brown } 1235089b2837SJed Brown 1236089b2837SJed Brown #undef __FUNCT__ 1237089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 1238089b2837SJed Brown /*@C 1239089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1240089b2837SJed Brown 1241089b2837SJed Brown Not Collective 1242089b2837SJed Brown 1243089b2837SJed Brown Input Parameter: 1244089b2837SJed Brown . ts - the TS context 1245089b2837SJed Brown 1246089b2837SJed Brown Output Parameter: 12470298fd71SBarry Smith + r - vector to hold residual (or NULL) 12480298fd71SBarry Smith . func - the function to compute residual (or NULL) 12490298fd71SBarry Smith - ctx - the function context (or NULL) 1250089b2837SJed Brown 1251089b2837SJed Brown Level: advanced 1252089b2837SJed Brown 1253089b2837SJed Brown .keywords: TS, nonlinear, get, function 1254089b2837SJed Brown 1255089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1256089b2837SJed Brown @*/ 1257089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1258089b2837SJed Brown { 1259089b2837SJed Brown PetscErrorCode ierr; 1260089b2837SJed Brown SNES snes; 126124989b8cSPeter Brune DM dm; 1262089b2837SJed Brown 1263089b2837SJed Brown PetscFunctionBegin; 1264089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1265089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12660298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 126724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 126824989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1269089b2837SJed Brown PetscFunctionReturn(0); 1270089b2837SJed Brown } 1271089b2837SJed Brown 1272089b2837SJed Brown #undef __FUNCT__ 1273089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 1274089b2837SJed Brown /*@C 1275089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1276089b2837SJed Brown 1277089b2837SJed Brown Not Collective 1278089b2837SJed Brown 1279089b2837SJed Brown Input Parameter: 1280089b2837SJed Brown . ts - the TS context 1281089b2837SJed Brown 1282089b2837SJed Brown Output Parameter: 12830298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 12840298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 12850298fd71SBarry Smith - ctx - the function context (or NULL) 1286089b2837SJed Brown 1287089b2837SJed Brown Level: advanced 1288089b2837SJed Brown 1289089b2837SJed Brown .keywords: TS, nonlinear, get, function 1290089b2837SJed Brown 12912bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1292089b2837SJed Brown @*/ 1293089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1294089b2837SJed Brown { 1295089b2837SJed Brown PetscErrorCode ierr; 1296089b2837SJed Brown SNES snes; 129724989b8cSPeter Brune DM dm; 1298089b2837SJed Brown 1299089b2837SJed Brown PetscFunctionBegin; 1300089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1301089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13020298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 130324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 130424989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1305316643e7SJed Brown PetscFunctionReturn(0); 1306316643e7SJed Brown } 1307316643e7SJed Brown 1308316643e7SJed Brown #undef __FUNCT__ 1309316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 1310316643e7SJed Brown /*@C 1311a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1312ae8867d6SBarry Smith provided with TSSetIFunction(). 1313316643e7SJed Brown 13143f9fe445SBarry Smith Logically Collective on TS 1315316643e7SJed Brown 1316316643e7SJed Brown Input Parameters: 1317316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1318e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1319e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1320316643e7SJed Brown . f - the Jacobian evaluation routine 13210298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1322316643e7SJed Brown 1323316643e7SJed Brown Calling sequence of f: 1324ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1325316643e7SJed Brown 1326316643e7SJed Brown + t - time at step/stage being solved 13271b4a444bSJed Brown . U - state vector 13281b4a444bSJed Brown . U_t - time derivative of state vector 1329316643e7SJed Brown . a - shift 1330e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1331e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1332316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1333316643e7SJed Brown 1334316643e7SJed Brown Notes: 1335e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1336316643e7SJed Brown 1337895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1338895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1339895c21f2SBarry Smith 1340a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1341b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1342a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1343a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1344a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1345a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1346a4f0a591SBarry Smith 13476cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13486cd88445SBarry Smith 13496cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1350ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1351ca5f011dSBarry Smith 1352316643e7SJed Brown Level: beginner 1353316643e7SJed Brown 1354316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1355316643e7SJed Brown 1356ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1357316643e7SJed Brown 1358316643e7SJed Brown @*/ 1359e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1360316643e7SJed Brown { 1361316643e7SJed Brown PetscErrorCode ierr; 1362089b2837SJed Brown SNES snes; 136324989b8cSPeter Brune DM dm; 1364316643e7SJed Brown 1365316643e7SJed Brown PetscFunctionBegin; 13660700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1367e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1368e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1369e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1370e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 137124989b8cSPeter Brune 137224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 137324989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 137424989b8cSPeter Brune 1375089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1376e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1377316643e7SJed Brown PetscFunctionReturn(0); 1378316643e7SJed Brown } 1379316643e7SJed Brown 13804a2ae208SSatish Balay #undef __FUNCT__ 1381e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse" 1382e1244c69SJed Brown /*@ 1383e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1384e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1385e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1386e1244c69SJed Brown not been changed by the TS. 1387e1244c69SJed Brown 1388e1244c69SJed Brown Logically Collective 1389e1244c69SJed Brown 1390e1244c69SJed Brown Input Arguments: 1391e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1392e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1393e1244c69SJed Brown 1394e1244c69SJed Brown Level: intermediate 1395e1244c69SJed Brown 1396e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1397e1244c69SJed Brown @*/ 1398e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1399e1244c69SJed Brown { 1400e1244c69SJed Brown PetscFunctionBegin; 1401e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1402e1244c69SJed Brown PetscFunctionReturn(0); 1403e1244c69SJed Brown } 1404e1244c69SJed Brown 1405e1244c69SJed Brown #undef __FUNCT__ 1406efe9872eSLisandro Dalcin #define __FUNCT__ "TSSetI2Function" 1407efe9872eSLisandro Dalcin /*@C 1408efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1409efe9872eSLisandro Dalcin 1410efe9872eSLisandro Dalcin Logically Collective on TS 1411efe9872eSLisandro Dalcin 1412efe9872eSLisandro Dalcin Input Parameters: 1413efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1414efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1415efe9872eSLisandro Dalcin . fun - the function evaluation routine 1416efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1417efe9872eSLisandro Dalcin 1418efe9872eSLisandro Dalcin Calling sequence of fun: 1419efe9872eSLisandro Dalcin $ fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1420efe9872eSLisandro Dalcin 1421efe9872eSLisandro Dalcin + t - time at step/stage being solved 1422efe9872eSLisandro Dalcin . U - state vector 1423efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1424efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1425efe9872eSLisandro Dalcin . F - function vector 1426efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1427efe9872eSLisandro Dalcin 1428efe9872eSLisandro Dalcin Level: beginner 1429efe9872eSLisandro Dalcin 1430efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function 1431efe9872eSLisandro Dalcin 1432efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1433efe9872eSLisandro Dalcin @*/ 1434efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx) 1435efe9872eSLisandro Dalcin { 1436efe9872eSLisandro Dalcin DM dm; 1437efe9872eSLisandro Dalcin PetscErrorCode ierr; 1438efe9872eSLisandro Dalcin 1439efe9872eSLisandro Dalcin PetscFunctionBegin; 1440efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1441efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2); 1442efe9872eSLisandro Dalcin ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr); 1443efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1444efe9872eSLisandro Dalcin ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1445efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1446efe9872eSLisandro Dalcin } 1447efe9872eSLisandro Dalcin 1448efe9872eSLisandro Dalcin #undef __FUNCT__ 1449efe9872eSLisandro Dalcin #define __FUNCT__ "TSGetI2Function" 1450efe9872eSLisandro Dalcin /*@C 1451efe9872eSLisandro Dalcin TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1452efe9872eSLisandro Dalcin 1453efe9872eSLisandro Dalcin Not Collective 1454efe9872eSLisandro Dalcin 1455efe9872eSLisandro Dalcin Input Parameter: 1456efe9872eSLisandro Dalcin . ts - the TS context 1457efe9872eSLisandro Dalcin 1458efe9872eSLisandro Dalcin Output Parameter: 1459efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1460efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1461efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1462efe9872eSLisandro Dalcin 1463efe9872eSLisandro Dalcin Level: advanced 1464efe9872eSLisandro Dalcin 1465efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function 1466efe9872eSLisandro Dalcin 1467efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction() 1468efe9872eSLisandro Dalcin @*/ 1469efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx) 1470efe9872eSLisandro Dalcin { 1471efe9872eSLisandro Dalcin PetscErrorCode ierr; 1472efe9872eSLisandro Dalcin SNES snes; 1473efe9872eSLisandro Dalcin DM dm; 1474efe9872eSLisandro Dalcin 1475efe9872eSLisandro Dalcin PetscFunctionBegin; 1476efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1477efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1478efe9872eSLisandro Dalcin ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 1479efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1480efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1481efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1482efe9872eSLisandro Dalcin } 1483efe9872eSLisandro Dalcin 1484efe9872eSLisandro Dalcin #undef __FUNCT__ 1485efe9872eSLisandro Dalcin #define __FUNCT__ "TSSetI2Jacobian" 1486efe9872eSLisandro Dalcin /*@C 1487bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1488efe9872eSLisandro Dalcin where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function(). 1489efe9872eSLisandro Dalcin 1490efe9872eSLisandro Dalcin Logically Collective on TS 1491efe9872eSLisandro Dalcin 1492efe9872eSLisandro Dalcin Input Parameters: 1493efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1494efe9872eSLisandro Dalcin . J - Jacobian matrix 1495efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1496efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1497efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1498efe9872eSLisandro Dalcin 1499efe9872eSLisandro Dalcin Calling sequence of jac: 1500bc77d74cSLisandro Dalcin $ jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx); 1501efe9872eSLisandro Dalcin 1502efe9872eSLisandro Dalcin + t - time at step/stage being solved 1503efe9872eSLisandro Dalcin . U - state vector 1504efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1505efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1506efe9872eSLisandro Dalcin . v - shift for U_t 1507efe9872eSLisandro Dalcin . a - shift for U_tt 1508efe9872eSLisandro 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 1509efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1510efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1511efe9872eSLisandro Dalcin 1512efe9872eSLisandro Dalcin Notes: 1513efe9872eSLisandro Dalcin The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve. 1514efe9872eSLisandro Dalcin 1515efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1516efe9872eSLisandro 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. 1517efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1518bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1519efe9872eSLisandro Dalcin 1520efe9872eSLisandro Dalcin Level: beginner 1521efe9872eSLisandro Dalcin 1522efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian 1523efe9872eSLisandro Dalcin 1524efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1525efe9872eSLisandro Dalcin @*/ 1526efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx) 1527efe9872eSLisandro Dalcin { 1528efe9872eSLisandro Dalcin DM dm; 1529efe9872eSLisandro Dalcin PetscErrorCode ierr; 1530efe9872eSLisandro Dalcin 1531efe9872eSLisandro Dalcin PetscFunctionBegin; 1532efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1533efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2); 1534efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3); 1535efe9872eSLisandro Dalcin ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr); 1536efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1537efe9872eSLisandro Dalcin ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1538efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1539efe9872eSLisandro Dalcin } 1540efe9872eSLisandro Dalcin 1541efe9872eSLisandro Dalcin #undef __FUNCT__ 1542efe9872eSLisandro Dalcin #define __FUNCT__ "TSGetI2Jacobian" 1543efe9872eSLisandro Dalcin /*@C 1544efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1545efe9872eSLisandro Dalcin 1546efe9872eSLisandro Dalcin Not Collective, but parallel objects are returned if TS is parallel 1547efe9872eSLisandro Dalcin 1548efe9872eSLisandro Dalcin Input Parameter: 1549efe9872eSLisandro Dalcin . ts - The TS context obtained from TSCreate() 1550efe9872eSLisandro Dalcin 1551efe9872eSLisandro Dalcin Output Parameters: 1552efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1553efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1554efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1555efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1556efe9872eSLisandro Dalcin 1557efe9872eSLisandro Dalcin Notes: You can pass in NULL for any return argument you do not need. 1558efe9872eSLisandro Dalcin 1559efe9872eSLisandro Dalcin Level: advanced 1560efe9872eSLisandro Dalcin 1561efe9872eSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 1562efe9872eSLisandro Dalcin 1563efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian 1564efe9872eSLisandro Dalcin @*/ 1565efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx) 1566efe9872eSLisandro Dalcin { 1567efe9872eSLisandro Dalcin PetscErrorCode ierr; 1568efe9872eSLisandro Dalcin SNES snes; 1569efe9872eSLisandro Dalcin DM dm; 1570efe9872eSLisandro Dalcin 1571efe9872eSLisandro Dalcin PetscFunctionBegin; 1572efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1573efe9872eSLisandro Dalcin ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 1574efe9872eSLisandro Dalcin ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr); 1575efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1576efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1577efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1578efe9872eSLisandro Dalcin } 1579efe9872eSLisandro Dalcin 1580efe9872eSLisandro Dalcin #undef __FUNCT__ 1581efe9872eSLisandro Dalcin #define __FUNCT__ "TSComputeI2Function" 1582efe9872eSLisandro Dalcin /*@ 1583efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1584efe9872eSLisandro Dalcin 1585efe9872eSLisandro Dalcin Collective on TS and Vec 1586efe9872eSLisandro Dalcin 1587efe9872eSLisandro Dalcin Input Parameters: 1588efe9872eSLisandro Dalcin + ts - the TS context 1589efe9872eSLisandro Dalcin . t - current time 1590efe9872eSLisandro Dalcin . U - state vector 1591efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1592efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1593efe9872eSLisandro Dalcin 1594efe9872eSLisandro Dalcin Output Parameter: 1595efe9872eSLisandro Dalcin . F - the residual vector 1596efe9872eSLisandro Dalcin 1597efe9872eSLisandro Dalcin Note: 1598efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1599efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1600efe9872eSLisandro Dalcin 1601efe9872eSLisandro Dalcin Level: developer 1602efe9872eSLisandro Dalcin 1603efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector 1604efe9872eSLisandro Dalcin 1605efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1606efe9872eSLisandro Dalcin @*/ 1607efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F) 1608efe9872eSLisandro Dalcin { 1609efe9872eSLisandro Dalcin DM dm; 1610efe9872eSLisandro Dalcin TSI2Function I2Function; 1611efe9872eSLisandro Dalcin void *ctx; 1612efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1613efe9872eSLisandro Dalcin PetscErrorCode ierr; 1614efe9872eSLisandro Dalcin 1615efe9872eSLisandro Dalcin PetscFunctionBegin; 1616efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1617efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1618efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1619efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1620efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F,VEC_CLASSID,6); 1621efe9872eSLisandro Dalcin 1622efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1623efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr); 1624efe9872eSLisandro Dalcin ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 1625efe9872eSLisandro Dalcin 1626efe9872eSLisandro Dalcin if (!I2Function) { 1627efe9872eSLisandro Dalcin ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr); 1628efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1629efe9872eSLisandro Dalcin } 1630efe9872eSLisandro Dalcin 1631efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1632efe9872eSLisandro Dalcin 1633efe9872eSLisandro Dalcin PetscStackPush("TS user implicit function"); 1634efe9872eSLisandro Dalcin ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr); 1635efe9872eSLisandro Dalcin PetscStackPop; 1636efe9872eSLisandro Dalcin 1637efe9872eSLisandro Dalcin if (rhsfunction) { 1638efe9872eSLisandro Dalcin Vec Frhs; 1639efe9872eSLisandro Dalcin ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 1640efe9872eSLisandro Dalcin ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 1641efe9872eSLisandro Dalcin ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr); 1642efe9872eSLisandro Dalcin } 1643efe9872eSLisandro Dalcin 1644efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1645efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1646efe9872eSLisandro Dalcin } 1647efe9872eSLisandro Dalcin 1648efe9872eSLisandro Dalcin #undef __FUNCT__ 1649efe9872eSLisandro Dalcin #define __FUNCT__ "TSComputeI2Jacobian" 1650efe9872eSLisandro Dalcin /*@ 1651efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1652efe9872eSLisandro Dalcin 1653efe9872eSLisandro Dalcin Collective on TS and Vec 1654efe9872eSLisandro Dalcin 1655efe9872eSLisandro Dalcin Input Parameters: 1656efe9872eSLisandro Dalcin + ts - the TS context 1657efe9872eSLisandro Dalcin . t - current timestep 1658efe9872eSLisandro Dalcin . U - state vector 1659efe9872eSLisandro Dalcin . V - time derivative of state vector 1660efe9872eSLisandro Dalcin . A - second time derivative of state vector 1661efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1662efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1663efe9872eSLisandro Dalcin 1664efe9872eSLisandro Dalcin Output Parameters: 1665efe9872eSLisandro Dalcin + J - Jacobian matrix 1666efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1667efe9872eSLisandro Dalcin 1668efe9872eSLisandro Dalcin Notes: 1669efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1670efe9872eSLisandro Dalcin 1671efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1672efe9872eSLisandro Dalcin 1673efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1674efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1675efe9872eSLisandro Dalcin 1676efe9872eSLisandro Dalcin Level: developer 1677efe9872eSLisandro Dalcin 1678efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix 1679efe9872eSLisandro Dalcin 1680efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1681efe9872eSLisandro Dalcin @*/ 1682efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P) 1683efe9872eSLisandro Dalcin { 1684efe9872eSLisandro Dalcin DM dm; 1685efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1686efe9872eSLisandro Dalcin void *ctx; 1687efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1688efe9872eSLisandro Dalcin PetscErrorCode ierr; 1689efe9872eSLisandro Dalcin 1690efe9872eSLisandro Dalcin PetscFunctionBegin; 1691efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1692efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1693efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1694efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1695efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J,MAT_CLASSID,8); 1696efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P,MAT_CLASSID,9); 1697efe9872eSLisandro Dalcin 1698efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1699efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr); 1700efe9872eSLisandro Dalcin ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 1701efe9872eSLisandro Dalcin 1702efe9872eSLisandro Dalcin if (!I2Jacobian) { 1703efe9872eSLisandro Dalcin ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr); 1704efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1705efe9872eSLisandro Dalcin } 1706efe9872eSLisandro Dalcin 1707efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1708efe9872eSLisandro Dalcin 1709efe9872eSLisandro Dalcin PetscStackPush("TS user implicit Jacobian"); 1710efe9872eSLisandro Dalcin ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr); 1711efe9872eSLisandro Dalcin PetscStackPop; 1712efe9872eSLisandro Dalcin 1713efe9872eSLisandro Dalcin if (rhsjacobian) { 1714efe9872eSLisandro Dalcin Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 1715efe9872eSLisandro Dalcin ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr); 1716efe9872eSLisandro Dalcin ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr); 1717efe9872eSLisandro Dalcin ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr); 1718efe9872eSLisandro Dalcin if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);} 1719efe9872eSLisandro Dalcin } 1720efe9872eSLisandro Dalcin 1721efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1722efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1723efe9872eSLisandro Dalcin } 1724efe9872eSLisandro Dalcin 1725efe9872eSLisandro Dalcin #undef __FUNCT__ 1726efe9872eSLisandro Dalcin #define __FUNCT__ "TS2SetSolution" 1727efe9872eSLisandro Dalcin /*@ 1728efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1729efe9872eSLisandro Dalcin for use by the TS routines handling second order equations. 1730efe9872eSLisandro Dalcin 1731efe9872eSLisandro Dalcin Logically Collective on TS and Vec 1732efe9872eSLisandro Dalcin 1733efe9872eSLisandro Dalcin Input Parameters: 1734efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1735efe9872eSLisandro Dalcin . u - the solution vector 1736efe9872eSLisandro Dalcin - v - the time derivative vector 1737efe9872eSLisandro Dalcin 1738efe9872eSLisandro Dalcin Level: beginner 1739efe9872eSLisandro Dalcin 1740efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions 1741efe9872eSLisandro Dalcin @*/ 1742efe9872eSLisandro Dalcin PetscErrorCode TS2SetSolution(TS ts,Vec u,Vec v) 1743efe9872eSLisandro Dalcin { 1744efe9872eSLisandro Dalcin PetscErrorCode ierr; 1745efe9872eSLisandro Dalcin 1746efe9872eSLisandro Dalcin PetscFunctionBegin; 1747efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1748efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1749efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v,VEC_CLASSID,3); 1750efe9872eSLisandro Dalcin ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 1751efe9872eSLisandro Dalcin ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr); 1752efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 1753efe9872eSLisandro Dalcin ts->vec_dot = v; 1754efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1755efe9872eSLisandro Dalcin } 1756efe9872eSLisandro Dalcin 1757efe9872eSLisandro Dalcin #undef __FUNCT__ 1758efe9872eSLisandro Dalcin #define __FUNCT__ "TS2GetSolution" 1759efe9872eSLisandro Dalcin /*@ 1760efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 1761efe9872eSLisandro Dalcin for second order equations. It is valid to call this routine inside the function 1762efe9872eSLisandro Dalcin that you are evaluating in order to move to the new timestep. This vector not 1763efe9872eSLisandro Dalcin changed until the solution at the next timestep has been calculated. 1764efe9872eSLisandro Dalcin 1765efe9872eSLisandro Dalcin Not Collective, but Vec returned is parallel if TS is parallel 1766efe9872eSLisandro Dalcin 1767efe9872eSLisandro Dalcin Input Parameter: 1768efe9872eSLisandro Dalcin . ts - the TS context obtained from TSCreate() 1769efe9872eSLisandro Dalcin 1770efe9872eSLisandro Dalcin Output Parameter: 1771efe9872eSLisandro Dalcin + u - the vector containing the solution 1772efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1773efe9872eSLisandro Dalcin 1774efe9872eSLisandro Dalcin Level: intermediate 1775efe9872eSLisandro Dalcin 1776efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime() 1777efe9872eSLisandro Dalcin 1778efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution 1779efe9872eSLisandro Dalcin @*/ 1780efe9872eSLisandro Dalcin PetscErrorCode TS2GetSolution(TS ts,Vec *u,Vec *v) 1781efe9872eSLisandro Dalcin { 1782efe9872eSLisandro Dalcin PetscFunctionBegin; 1783efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1784efe9872eSLisandro Dalcin if (u) PetscValidPointer(u,2); 1785efe9872eSLisandro Dalcin if (v) PetscValidPointer(v,3); 1786efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1787efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 1788efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1789efe9872eSLisandro Dalcin } 1790efe9872eSLisandro Dalcin 1791efe9872eSLisandro Dalcin #undef __FUNCT__ 179255849f57SBarry Smith #define __FUNCT__ "TSLoad" 179355849f57SBarry Smith /*@C 179455849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 179555849f57SBarry Smith 179655849f57SBarry Smith Collective on PetscViewer 179755849f57SBarry Smith 179855849f57SBarry Smith Input Parameters: 179955849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 180055849f57SBarry Smith some related function before a call to TSLoad(). 180155849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 180255849f57SBarry Smith 180355849f57SBarry Smith Level: intermediate 180455849f57SBarry Smith 180555849f57SBarry Smith Notes: 180655849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 180755849f57SBarry Smith 180855849f57SBarry Smith Notes for advanced users: 180955849f57SBarry Smith Most users should not need to know the details of the binary storage 181055849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 181155849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 181255849f57SBarry Smith format is 181355849f57SBarry Smith .vb 181455849f57SBarry Smith has not yet been determined 181555849f57SBarry Smith .ve 181655849f57SBarry Smith 181755849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 181855849f57SBarry Smith @*/ 1819f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 182055849f57SBarry Smith { 182155849f57SBarry Smith PetscErrorCode ierr; 182255849f57SBarry Smith PetscBool isbinary; 182355849f57SBarry Smith PetscInt classid; 182455849f57SBarry Smith char type[256]; 18252d53ad75SBarry Smith DMTS sdm; 1826ad6bc421SBarry Smith DM dm; 182755849f57SBarry Smith 182855849f57SBarry Smith PetscFunctionBegin; 1829f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 183055849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 183155849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 183255849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 183355849f57SBarry Smith 1834060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1835ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1836060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1837f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1838f2c2a1b9SBarry Smith if (ts->ops->load) { 1839f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1840f2c2a1b9SBarry Smith } 1841ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1842ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1843ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1844f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1845f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 18462d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 18472d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 184855849f57SBarry Smith PetscFunctionReturn(0); 184955849f57SBarry Smith } 185055849f57SBarry Smith 18519804daf3SBarry Smith #include <petscdraw.h> 1852e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1853e04113cfSBarry Smith #include <petscviewersaws.h> 1854f05ece33SBarry Smith #endif 185555849f57SBarry Smith #undef __FUNCT__ 18564a2ae208SSatish Balay #define __FUNCT__ "TSView" 18577e2c5f70SBarry Smith /*@C 1858d763cef2SBarry Smith TSView - Prints the TS data structure. 1859d763cef2SBarry Smith 18604c49b128SBarry Smith Collective on TS 1861d763cef2SBarry Smith 1862d763cef2SBarry Smith Input Parameters: 1863d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1864d763cef2SBarry Smith - viewer - visualization context 1865d763cef2SBarry Smith 1866d763cef2SBarry Smith Options Database Key: 1867d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1868d763cef2SBarry Smith 1869d763cef2SBarry Smith Notes: 1870d763cef2SBarry Smith The available visualization contexts include 1871b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1872b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1873d763cef2SBarry Smith output where only the first processor opens 1874d763cef2SBarry Smith the file. All other processors send their 1875d763cef2SBarry Smith data to the first processor to print. 1876d763cef2SBarry Smith 1877d763cef2SBarry Smith The user can open an alternative visualization context with 1878b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1879d763cef2SBarry Smith 1880d763cef2SBarry Smith Level: beginner 1881d763cef2SBarry Smith 1882d763cef2SBarry Smith .keywords: TS, timestep, view 1883d763cef2SBarry Smith 1884b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1885d763cef2SBarry Smith @*/ 18867087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1887d763cef2SBarry Smith { 1888dfbe8321SBarry Smith PetscErrorCode ierr; 188919fd82e9SBarry Smith TSType type; 18902b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 18912d53ad75SBarry Smith DMTS sdm; 1892e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1893536b137fSBarry Smith PetscBool issaws; 1894f05ece33SBarry Smith #endif 1895d763cef2SBarry Smith 1896d763cef2SBarry Smith PetscFunctionBegin; 18970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18983050cee2SBarry Smith if (!viewer) { 1899ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 19003050cee2SBarry Smith } 19010700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1902c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1903fd16b177SBarry Smith 1904251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1905251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 190655849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 19072b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1908e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1909536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1910f05ece33SBarry Smith #endif 191132077d6dSBarry Smith if (iascii) { 1912dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 191377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 19147c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1915d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 19165ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1917c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1918d763cef2SBarry Smith } 19195ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1920193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 1921a0af407cSBarry Smith if (ts->vrtol) { 1922a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of relative error tolerances, ");CHKERRQ(ierr); 1923a0af407cSBarry Smith } else { 1924a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr); 1925a0af407cSBarry Smith } 1926a0af407cSBarry Smith if (ts->vatol) { 1927a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of absolute error tolerances\n");CHKERRQ(ierr); 1928a0af407cSBarry Smith } else { 1929a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr); 1930a0af407cSBarry Smith } 19312d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19322d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1933d52bd9f3SBarry Smith if (ts->ops->view) { 1934d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1935d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1936d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1937d52bd9f3SBarry Smith } 19380f5bd95cSBarry Smith } else if (isstring) { 1939a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1940b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 194155849f57SBarry Smith } else if (isbinary) { 194255849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 194355849f57SBarry Smith MPI_Comm comm; 194455849f57SBarry Smith PetscMPIInt rank; 194555849f57SBarry Smith char type[256]; 194655849f57SBarry Smith 194755849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 194855849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 194955849f57SBarry Smith if (!rank) { 195055849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 195155849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 195255849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 195355849f57SBarry Smith } 195455849f57SBarry Smith if (ts->ops->view) { 195555849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 195655849f57SBarry Smith } 1957f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1958f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 19592d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19602d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 19612b0a91c0SBarry Smith } else if (isdraw) { 19622b0a91c0SBarry Smith PetscDraw draw; 19632b0a91c0SBarry Smith char str[36]; 196489fd9fafSBarry Smith PetscReal x,y,bottom,h; 19652b0a91c0SBarry Smith 19662b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 19672b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 19682b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 19692b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 197051fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 197189fd9fafSBarry Smith bottom = y - h; 19722b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 19732b0a91c0SBarry Smith if (ts->ops->view) { 19742b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 19752b0a91c0SBarry Smith } 19762b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1977e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1978536b137fSBarry Smith } else if (issaws) { 1979d45a07a7SBarry Smith PetscMPIInt rank; 19802657e9d9SBarry Smith const char *name; 19812657e9d9SBarry Smith 19822657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1983d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1984d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1985d45a07a7SBarry Smith char dir[1024]; 1986d45a07a7SBarry Smith 1987e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1988a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 19892657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 19902657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 19912657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1992d763cef2SBarry Smith } 19930acecf5bSBarry Smith if (ts->ops->view) { 19940acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 19950acecf5bSBarry Smith } 1996f05ece33SBarry Smith #endif 1997f05ece33SBarry Smith } 1998f05ece33SBarry Smith 1999b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2000251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 2001b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2002d763cef2SBarry Smith PetscFunctionReturn(0); 2003d763cef2SBarry Smith } 2004d763cef2SBarry Smith 2005d763cef2SBarry Smith 20064a2ae208SSatish Balay #undef __FUNCT__ 20074a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 2008b07ff414SBarry Smith /*@ 2009d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2010d763cef2SBarry Smith the timesteppers. 2011d763cef2SBarry Smith 20123f9fe445SBarry Smith Logically Collective on TS 2013d763cef2SBarry Smith 2014d763cef2SBarry Smith Input Parameters: 2015d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2016d763cef2SBarry Smith - usrP - optional user context 2017d763cef2SBarry Smith 2018daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 2019daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2020daf670e6SBarry Smith 2021d763cef2SBarry Smith Level: intermediate 2022d763cef2SBarry Smith 2023d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 2024d763cef2SBarry Smith 2025d763cef2SBarry Smith .seealso: TSGetApplicationContext() 2026d763cef2SBarry Smith @*/ 20277087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 2028d763cef2SBarry Smith { 2029d763cef2SBarry Smith PetscFunctionBegin; 20300700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2031d763cef2SBarry Smith ts->user = usrP; 2032d763cef2SBarry Smith PetscFunctionReturn(0); 2033d763cef2SBarry Smith } 2034d763cef2SBarry Smith 20354a2ae208SSatish Balay #undef __FUNCT__ 20364a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 2037b07ff414SBarry Smith /*@ 2038d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2039d763cef2SBarry Smith timestepper. 2040d763cef2SBarry Smith 2041d763cef2SBarry Smith Not Collective 2042d763cef2SBarry Smith 2043d763cef2SBarry Smith Input Parameter: 2044d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2045d763cef2SBarry Smith 2046d763cef2SBarry Smith Output Parameter: 2047d763cef2SBarry Smith . usrP - user context 2048d763cef2SBarry Smith 2049daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 2050daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2051daf670e6SBarry Smith 2052d763cef2SBarry Smith Level: intermediate 2053d763cef2SBarry Smith 2054d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 2055d763cef2SBarry Smith 2056d763cef2SBarry Smith .seealso: TSSetApplicationContext() 2057d763cef2SBarry Smith @*/ 2058e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 2059d763cef2SBarry Smith { 2060d763cef2SBarry Smith PetscFunctionBegin; 20610700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2062e71120c6SJed Brown *(void**)usrP = ts->user; 2063d763cef2SBarry Smith PetscFunctionReturn(0); 2064d763cef2SBarry Smith } 2065d763cef2SBarry Smith 20664a2ae208SSatish Balay #undef __FUNCT__ 20674a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 2068d763cef2SBarry Smith /*@ 2069b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 2070d763cef2SBarry Smith 2071d763cef2SBarry Smith Not Collective 2072d763cef2SBarry Smith 2073d763cef2SBarry Smith Input Parameter: 2074d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2075d763cef2SBarry Smith 2076d763cef2SBarry Smith Output Parameter: 2077b8123daeSJed Brown . iter - number of steps completed so far 2078d763cef2SBarry Smith 2079d763cef2SBarry Smith Level: intermediate 2080d763cef2SBarry Smith 2081d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 20829be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 2083d763cef2SBarry Smith @*/ 20847087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 2085d763cef2SBarry Smith { 2086d763cef2SBarry Smith PetscFunctionBegin; 20870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20884482741eSBarry Smith PetscValidIntPointer(iter,2); 2089d763cef2SBarry Smith *iter = ts->steps; 2090d763cef2SBarry Smith PetscFunctionReturn(0); 2091d763cef2SBarry Smith } 2092d763cef2SBarry Smith 20934a2ae208SSatish Balay #undef __FUNCT__ 20944a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 2095d763cef2SBarry Smith /*@ 2096d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 2097d763cef2SBarry Smith as well as the initial time. 2098d763cef2SBarry Smith 20993f9fe445SBarry Smith Logically Collective on TS 2100d763cef2SBarry Smith 2101d763cef2SBarry Smith Input Parameters: 2102d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2103d763cef2SBarry Smith . initial_time - the initial time 2104d763cef2SBarry Smith - time_step - the size of the timestep 2105d763cef2SBarry Smith 2106d763cef2SBarry Smith Level: intermediate 2107d763cef2SBarry Smith 2108d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 2109d763cef2SBarry Smith 2110d763cef2SBarry Smith .keywords: TS, set, initial, timestep 2111d763cef2SBarry Smith @*/ 21127087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 2113d763cef2SBarry Smith { 2114e144a568SJed Brown PetscErrorCode ierr; 2115e144a568SJed Brown 2116d763cef2SBarry Smith PetscFunctionBegin; 21170700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2118e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 2119d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 2120d763cef2SBarry Smith PetscFunctionReturn(0); 2121d763cef2SBarry Smith } 2122d763cef2SBarry Smith 21234a2ae208SSatish Balay #undef __FUNCT__ 21244a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 2125d763cef2SBarry Smith /*@ 2126d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2127d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2128d763cef2SBarry Smith 21293f9fe445SBarry Smith Logically Collective on TS 2130d763cef2SBarry Smith 2131d763cef2SBarry Smith Input Parameters: 2132d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2133d763cef2SBarry Smith - time_step - the size of the timestep 2134d763cef2SBarry Smith 2135d763cef2SBarry Smith Level: intermediate 2136d763cef2SBarry Smith 2137d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2138d763cef2SBarry Smith 2139d763cef2SBarry Smith .keywords: TS, set, timestep 2140d763cef2SBarry Smith @*/ 21417087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 2142d763cef2SBarry Smith { 2143d763cef2SBarry Smith PetscFunctionBegin; 21440700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2145c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 2146d763cef2SBarry Smith ts->time_step = time_step; 2147d763cef2SBarry Smith PetscFunctionReturn(0); 2148d763cef2SBarry Smith } 2149d763cef2SBarry Smith 21504a2ae208SSatish Balay #undef __FUNCT__ 2151a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 2152a43b19c4SJed Brown /*@ 215349354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 215449354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 215549354f04SShri Abhyankar or just return at the final time TS computed. 2156a43b19c4SJed Brown 2157a43b19c4SJed Brown Logically Collective on TS 2158a43b19c4SJed Brown 2159a43b19c4SJed Brown Input Parameter: 2160a43b19c4SJed Brown + ts - the time-step context 216149354f04SShri Abhyankar - eftopt - exact final time option 2162a43b19c4SJed Brown 2163feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2164feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2165feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2166feed9e9dSBarry Smith 2167feed9e9dSBarry Smith Options Database: 2168feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2169feed9e9dSBarry Smith 2170ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 2171ee346746SBarry Smith then the final time you selected. 2172ee346746SBarry Smith 2173a43b19c4SJed Brown Level: beginner 2174a43b19c4SJed Brown 2175a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 2176a43b19c4SJed Brown @*/ 217749354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 2178a43b19c4SJed Brown { 2179a43b19c4SJed Brown PetscFunctionBegin; 2180a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 218149354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 218249354f04SShri Abhyankar ts->exact_final_time = eftopt; 2183a43b19c4SJed Brown PetscFunctionReturn(0); 2184a43b19c4SJed Brown } 2185a43b19c4SJed Brown 2186a43b19c4SJed Brown #undef __FUNCT__ 21874a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 2188d763cef2SBarry Smith /*@ 2189d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2190d763cef2SBarry Smith 2191d763cef2SBarry Smith Not Collective 2192d763cef2SBarry Smith 2193d763cef2SBarry Smith Input Parameter: 2194d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2195d763cef2SBarry Smith 2196d763cef2SBarry Smith Output Parameter: 2197d763cef2SBarry Smith . dt - the current timestep size 2198d763cef2SBarry Smith 2199d763cef2SBarry Smith Level: intermediate 2200d763cef2SBarry Smith 2201d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2202d763cef2SBarry Smith 2203d763cef2SBarry Smith .keywords: TS, get, timestep 2204d763cef2SBarry Smith @*/ 22057087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 2206d763cef2SBarry Smith { 2207d763cef2SBarry Smith PetscFunctionBegin; 22080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2209f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 2210d763cef2SBarry Smith *dt = ts->time_step; 2211d763cef2SBarry Smith PetscFunctionReturn(0); 2212d763cef2SBarry Smith } 2213d763cef2SBarry Smith 22144a2ae208SSatish Balay #undef __FUNCT__ 22154a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 2216d8e5e3e6SSatish Balay /*@ 2217d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2218d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2219d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2220d763cef2SBarry Smith the solution at the next timestep has been calculated. 2221d763cef2SBarry Smith 2222d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 2223d763cef2SBarry Smith 2224d763cef2SBarry Smith Input Parameter: 2225d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2226d763cef2SBarry Smith 2227d763cef2SBarry Smith Output Parameter: 2228d763cef2SBarry Smith . v - the vector containing the solution 2229d763cef2SBarry Smith 223063e21af5SBarry Smith Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested 223163e21af5SBarry Smith final time. It returns the solution at the next timestep. 223263e21af5SBarry Smith 2233d763cef2SBarry Smith Level: intermediate 2234d763cef2SBarry Smith 223563e21af5SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime() 2236d763cef2SBarry Smith 2237d763cef2SBarry Smith .keywords: TS, timestep, get, solution 2238d763cef2SBarry Smith @*/ 22397087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 2240d763cef2SBarry Smith { 2241d763cef2SBarry Smith PetscFunctionBegin; 22420700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22434482741eSBarry Smith PetscValidPointer(v,2); 22448737fe31SLisandro Dalcin *v = ts->vec_sol; 2245d763cef2SBarry Smith PetscFunctionReturn(0); 2246d763cef2SBarry Smith } 2247d763cef2SBarry Smith 2248c235aa8dSHong Zhang #undef __FUNCT__ 2249dfb21088SHong Zhang #define __FUNCT__ "TSGetCostGradients" 2250c235aa8dSHong Zhang /*@ 2251dfb21088SHong Zhang TSGetCostGradients - Returns the gradients from the TSAdjointSolve() 2252c235aa8dSHong Zhang 2253c235aa8dSHong Zhang Not Collective, but Vec returned is parallel if TS is parallel 2254c235aa8dSHong Zhang 2255c235aa8dSHong Zhang Input Parameter: 2256c235aa8dSHong Zhang . ts - the TS context obtained from TSCreate() 2257c235aa8dSHong Zhang 2258c235aa8dSHong Zhang Output Parameter: 2259abc2977eSBarry Smith + lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 2260abc2977eSBarry Smith - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 2261c235aa8dSHong Zhang 2262c235aa8dSHong Zhang Level: intermediate 2263c235aa8dSHong Zhang 2264c235aa8dSHong Zhang .seealso: TSGetTimeStep() 2265c235aa8dSHong Zhang 2266c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity 2267c235aa8dSHong Zhang @*/ 2268dfb21088SHong Zhang PetscErrorCode TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu) 2269c235aa8dSHong Zhang { 2270c235aa8dSHong Zhang PetscFunctionBegin; 2271c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2272abc2977eSBarry Smith if (numcost) *numcost = ts->numcost; 2273abc2977eSBarry Smith if (lambda) *lambda = ts->vecs_sensi; 2274abc2977eSBarry Smith if (mu) *mu = ts->vecs_sensip; 2275c235aa8dSHong Zhang PetscFunctionReturn(0); 2276c235aa8dSHong Zhang } 2277c235aa8dSHong Zhang 2278bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 22794a2ae208SSatish Balay #undef __FUNCT__ 2280bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 2281d8e5e3e6SSatish Balay /*@ 2282bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2283d763cef2SBarry Smith 2284bdad233fSMatthew Knepley Not collective 2285d763cef2SBarry Smith 2286bdad233fSMatthew Knepley Input Parameters: 2287bdad233fSMatthew Knepley + ts - The TS 2288bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2289d763cef2SBarry Smith .vb 22900910c330SBarry Smith U_t - A U = 0 (linear) 22910910c330SBarry Smith U_t - A(t) U = 0 (linear) 22920910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2293d763cef2SBarry Smith .ve 2294d763cef2SBarry Smith 2295d763cef2SBarry Smith Level: beginner 2296d763cef2SBarry Smith 2297bdad233fSMatthew Knepley .keywords: TS, problem type 2298bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2299d763cef2SBarry Smith @*/ 23007087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2301a7cc72afSBarry Smith { 23029e2a6581SJed Brown PetscErrorCode ierr; 23039e2a6581SJed Brown 2304d763cef2SBarry Smith PetscFunctionBegin; 23050700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2306bdad233fSMatthew Knepley ts->problem_type = type; 23079e2a6581SJed Brown if (type == TS_LINEAR) { 23089e2a6581SJed Brown SNES snes; 23099e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 23109e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 23119e2a6581SJed Brown } 2312d763cef2SBarry Smith PetscFunctionReturn(0); 2313d763cef2SBarry Smith } 2314d763cef2SBarry Smith 2315bdad233fSMatthew Knepley #undef __FUNCT__ 2316bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 2317bdad233fSMatthew Knepley /*@C 2318bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2319bdad233fSMatthew Knepley 2320bdad233fSMatthew Knepley Not collective 2321bdad233fSMatthew Knepley 2322bdad233fSMatthew Knepley Input Parameter: 2323bdad233fSMatthew Knepley . ts - The TS 2324bdad233fSMatthew Knepley 2325bdad233fSMatthew Knepley Output Parameter: 2326bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2327bdad233fSMatthew Knepley .vb 2328089b2837SJed Brown M U_t = A U 2329089b2837SJed Brown M(t) U_t = A(t) U 2330b5abc632SBarry Smith F(t,U,U_t) 2331bdad233fSMatthew Knepley .ve 2332bdad233fSMatthew Knepley 2333bdad233fSMatthew Knepley Level: beginner 2334bdad233fSMatthew Knepley 2335bdad233fSMatthew Knepley .keywords: TS, problem type 2336bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2337bdad233fSMatthew Knepley @*/ 23387087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2339a7cc72afSBarry Smith { 2340bdad233fSMatthew Knepley PetscFunctionBegin; 23410700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 23424482741eSBarry Smith PetscValidIntPointer(type,2); 2343bdad233fSMatthew Knepley *type = ts->problem_type; 2344bdad233fSMatthew Knepley PetscFunctionReturn(0); 2345bdad233fSMatthew Knepley } 2346d763cef2SBarry Smith 23474a2ae208SSatish Balay #undef __FUNCT__ 23484a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 2349d763cef2SBarry Smith /*@ 2350d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 2351d763cef2SBarry Smith of a timestepper. 2352d763cef2SBarry Smith 2353d763cef2SBarry Smith Collective on TS 2354d763cef2SBarry Smith 2355d763cef2SBarry Smith Input Parameter: 2356d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2357d763cef2SBarry Smith 2358d763cef2SBarry Smith Notes: 2359d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 2360d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 2361d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 2362d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 2363d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 2364d763cef2SBarry Smith 2365d763cef2SBarry Smith Level: advanced 2366d763cef2SBarry Smith 2367d763cef2SBarry Smith .keywords: TS, timestep, setup 2368d763cef2SBarry Smith 2369d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 2370d763cef2SBarry Smith @*/ 23717087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 2372d763cef2SBarry Smith { 2373dfbe8321SBarry Smith PetscErrorCode ierr; 23746c6b9e74SPeter Brune DM dm; 23756c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 2376d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 2377cd11d68dSLisandro Dalcin TSIFunction ifun; 23786c6b9e74SPeter Brune TSIJacobian ijac; 2379efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 23806c6b9e74SPeter Brune TSRHSJacobian rhsjac; 2381d763cef2SBarry Smith 2382d763cef2SBarry Smith PetscFunctionBegin; 23830700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2384277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 2385277b19d0SLisandro Dalcin 23862c18e0fdSBarry Smith ts->total_steps = 0; 23877adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 2388cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 2389cd11d68dSLisandro Dalcin ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr); 2390d763cef2SBarry Smith } 2391277b19d0SLisandro Dalcin 2392277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 2393277b19d0SLisandro Dalcin 2394e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 2395e1244c69SJed Brown Mat Amat,Pmat; 2396e1244c69SJed Brown SNES snes; 2397e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2398e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 2399e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2400e1244c69SJed Brown * have displaced the RHS matrix */ 2401e1244c69SJed Brown if (Amat == ts->Arhs) { 2402e1244c69SJed Brown ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 2403e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 2404e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 2405e1244c69SJed Brown } 2406e1244c69SJed Brown if (Pmat == ts->Brhs) { 2407e1244c69SJed Brown ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 2408e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 2409e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 2410e1244c69SJed Brown } 2411e1244c69SJed Brown } 2412277b19d0SLisandro Dalcin if (ts->ops->setup) { 2413000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 2414277b19d0SLisandro Dalcin } 2415277b19d0SLisandro Dalcin 2416a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 24176c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 24186c6b9e74SPeter Brune */ 24196c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 24200298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 24216c6b9e74SPeter Brune if (!func) { 24226c6b9e74SPeter Brune ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 24236c6b9e74SPeter Brune } 2424a6772fa2SLisandro 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. 24256c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 24266c6b9e74SPeter Brune */ 24270298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 24280298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 2429efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr); 24300298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 2431efe9872eSLisandro Dalcin if (!jac && (ijac || i2jac || rhsjac)) { 24326c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 24336c6b9e74SPeter Brune } 2434277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2435277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2436277b19d0SLisandro Dalcin } 2437277b19d0SLisandro Dalcin 2438277b19d0SLisandro Dalcin #undef __FUNCT__ 2439f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp" 2440f6a906c0SBarry Smith /*@ 2441f6a906c0SBarry Smith TSAdjointSetUp - Sets up the internal data structures for the later use 2442f6a906c0SBarry Smith of an adjoint solver 2443f6a906c0SBarry Smith 2444f6a906c0SBarry Smith Collective on TS 2445f6a906c0SBarry Smith 2446f6a906c0SBarry Smith Input Parameter: 2447f6a906c0SBarry Smith . ts - the TS context obtained from TSCreate() 2448f6a906c0SBarry Smith 2449f6a906c0SBarry Smith Level: advanced 2450f6a906c0SBarry Smith 2451f6a906c0SBarry Smith .keywords: TS, timestep, setup 2452f6a906c0SBarry Smith 2453947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients() 2454f6a906c0SBarry Smith @*/ 2455f6a906c0SBarry Smith PetscErrorCode TSAdjointSetUp(TS ts) 2456f6a906c0SBarry Smith { 2457f6a906c0SBarry Smith PetscErrorCode ierr; 2458f6a906c0SBarry Smith 2459f6a906c0SBarry Smith PetscFunctionBegin; 2460f6a906c0SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2461f6a906c0SBarry Smith if (ts->adjointsetupcalled) PetscFunctionReturn(0); 2462dfb21088SHong Zhang if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first"); 2463d8151eeaSBarry Smith 2464947abb85SHong Zhang if (ts->vec_costintegral) { /* if there is integral in the cost function*/ 2465d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2466d8151eeaSBarry Smith if (ts->vecs_sensip){ 2467d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2468d8151eeaSBarry Smith } 2469947abb85SHong Zhang } 2470d8151eeaSBarry Smith 247142f2b339SBarry Smith if (ts->ops->adjointsetup) { 247242f2b339SBarry Smith ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr); 2473f6a906c0SBarry Smith } 2474f6a906c0SBarry Smith ts->adjointsetupcalled = PETSC_TRUE; 2475f6a906c0SBarry Smith PetscFunctionReturn(0); 2476f6a906c0SBarry Smith } 2477f6a906c0SBarry Smith 2478f6a906c0SBarry Smith #undef __FUNCT__ 2479277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 2480277b19d0SLisandro Dalcin /*@ 2481277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2482277b19d0SLisandro Dalcin 2483277b19d0SLisandro Dalcin Collective on TS 2484277b19d0SLisandro Dalcin 2485277b19d0SLisandro Dalcin Input Parameter: 2486277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2487277b19d0SLisandro Dalcin 2488277b19d0SLisandro Dalcin Level: beginner 2489277b19d0SLisandro Dalcin 2490277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 2491277b19d0SLisandro Dalcin 2492277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2493277b19d0SLisandro Dalcin @*/ 2494277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2495277b19d0SLisandro Dalcin { 2496277b19d0SLisandro Dalcin PetscErrorCode ierr; 2497277b19d0SLisandro Dalcin 2498277b19d0SLisandro Dalcin PetscFunctionBegin; 2499277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2500b18ea86cSHong Zhang 2501277b19d0SLisandro Dalcin if (ts->ops->reset) { 2502277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2503277b19d0SLisandro Dalcin } 2504277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2505e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2506bbd56ea5SKarl Rupp 25074e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 25084e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2509214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 25106bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2511efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 2512e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2513e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 251438637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2515bbd56ea5SKarl Rupp 2516947abb85SHong Zhang if (ts->vec_costintegral) { 2517d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2518d8151eeaSBarry Smith if (ts->vecs_drdp){ 2519d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2520d8151eeaSBarry Smith } 2521947abb85SHong Zhang } 2522d8151eeaSBarry Smith ts->vecs_sensi = NULL; 2523d8151eeaSBarry Smith ts->vecs_sensip = NULL; 2524ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 25252c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 252636eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2527277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2528d763cef2SBarry Smith PetscFunctionReturn(0); 2529d763cef2SBarry Smith } 2530d763cef2SBarry Smith 25314a2ae208SSatish Balay #undef __FUNCT__ 25324a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 2533d8e5e3e6SSatish Balay /*@ 2534d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2535d763cef2SBarry Smith with TSCreate(). 2536d763cef2SBarry Smith 2537d763cef2SBarry Smith Collective on TS 2538d763cef2SBarry Smith 2539d763cef2SBarry Smith Input Parameter: 2540d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2541d763cef2SBarry Smith 2542d763cef2SBarry Smith Level: beginner 2543d763cef2SBarry Smith 2544d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2545d763cef2SBarry Smith 2546d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2547d763cef2SBarry Smith @*/ 25486bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2549d763cef2SBarry Smith { 25506849ba73SBarry Smith PetscErrorCode ierr; 2551d763cef2SBarry Smith 2552d763cef2SBarry Smith PetscFunctionBegin; 25536bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 25546bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 25556bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2556d763cef2SBarry Smith 25576bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2558277b19d0SLisandro Dalcin 2559e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2560e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 25616bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 25626d4c513bSLisandro Dalcin 2563bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2564bc952696SBarry Smith 256584df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 25666427ac75SLisandro Dalcin ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr); 25676427ac75SLisandro Dalcin 25686bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 25696bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 25706bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 25710dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 25726d4c513bSLisandro Dalcin 2573a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2574d763cef2SBarry Smith PetscFunctionReturn(0); 2575d763cef2SBarry Smith } 2576d763cef2SBarry Smith 25774a2ae208SSatish Balay #undef __FUNCT__ 25784a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 2579d8e5e3e6SSatish Balay /*@ 2580d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2581d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2582d763cef2SBarry Smith 2583d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2584d763cef2SBarry Smith 2585d763cef2SBarry Smith Input Parameter: 2586d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2587d763cef2SBarry Smith 2588d763cef2SBarry Smith Output Parameter: 2589d763cef2SBarry Smith . snes - the nonlinear solver context 2590d763cef2SBarry Smith 2591d763cef2SBarry Smith Notes: 2592d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2593d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 259494b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2595d763cef2SBarry Smith 2596d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 25970298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2598d763cef2SBarry Smith 2599d763cef2SBarry Smith Level: beginner 2600d763cef2SBarry Smith 2601d763cef2SBarry Smith .keywords: timestep, get, SNES 2602d763cef2SBarry Smith @*/ 26037087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2604d763cef2SBarry Smith { 2605d372ba47SLisandro Dalcin PetscErrorCode ierr; 2606d372ba47SLisandro Dalcin 2607d763cef2SBarry Smith PetscFunctionBegin; 26080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26094482741eSBarry Smith PetscValidPointer(snes,2); 2610d372ba47SLisandro Dalcin if (!ts->snes) { 2611ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 26120298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 26133bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2614d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2615496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 26169e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 26179e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 26189e2a6581SJed Brown } 2619d372ba47SLisandro Dalcin } 2620d763cef2SBarry Smith *snes = ts->snes; 2621d763cef2SBarry Smith PetscFunctionReturn(0); 2622d763cef2SBarry Smith } 2623d763cef2SBarry Smith 26244a2ae208SSatish Balay #undef __FUNCT__ 2625deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES" 2626deb2cd25SJed Brown /*@ 2627deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2628deb2cd25SJed Brown 2629deb2cd25SJed Brown Collective 2630deb2cd25SJed Brown 2631deb2cd25SJed Brown Input Parameter: 2632deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2633deb2cd25SJed Brown - snes - the nonlinear solver context 2634deb2cd25SJed Brown 2635deb2cd25SJed Brown Notes: 2636deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2637deb2cd25SJed Brown 2638deb2cd25SJed Brown Level: developer 2639deb2cd25SJed Brown 2640deb2cd25SJed Brown .keywords: timestep, set, SNES 2641deb2cd25SJed Brown @*/ 2642deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2643deb2cd25SJed Brown { 2644deb2cd25SJed Brown PetscErrorCode ierr; 2645d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2646deb2cd25SJed Brown 2647deb2cd25SJed Brown PetscFunctionBegin; 2648deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2649deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2650deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2651deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2652bbd56ea5SKarl Rupp 2653deb2cd25SJed Brown ts->snes = snes; 2654bbd56ea5SKarl Rupp 26550298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 26560298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2657740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 26580298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2659740132f1SEmil Constantinescu } 2660deb2cd25SJed Brown PetscFunctionReturn(0); 2661deb2cd25SJed Brown } 2662deb2cd25SJed Brown 2663deb2cd25SJed Brown #undef __FUNCT__ 266494b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 2665d8e5e3e6SSatish Balay /*@ 266694b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2667d763cef2SBarry Smith a TS (timestepper) context. 2668d763cef2SBarry Smith 266994b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2670d763cef2SBarry Smith 2671d763cef2SBarry Smith Input Parameter: 2672d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2673d763cef2SBarry Smith 2674d763cef2SBarry Smith Output Parameter: 267594b7f48cSBarry Smith . ksp - the nonlinear solver context 2676d763cef2SBarry Smith 2677d763cef2SBarry Smith Notes: 267894b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2679d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2680d763cef2SBarry Smith KSP and PC contexts as well. 2681d763cef2SBarry Smith 268294b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 26830298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2684d763cef2SBarry Smith 2685d763cef2SBarry Smith Level: beginner 2686d763cef2SBarry Smith 268794b7f48cSBarry Smith .keywords: timestep, get, KSP 2688d763cef2SBarry Smith @*/ 26897087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2690d763cef2SBarry Smith { 2691d372ba47SLisandro Dalcin PetscErrorCode ierr; 2692089b2837SJed Brown SNES snes; 2693d372ba47SLisandro Dalcin 2694d763cef2SBarry Smith PetscFunctionBegin; 26950700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26964482741eSBarry Smith PetscValidPointer(ksp,2); 269717186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2698e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2699089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2700089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2701d763cef2SBarry Smith PetscFunctionReturn(0); 2702d763cef2SBarry Smith } 2703d763cef2SBarry Smith 2704d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2705d763cef2SBarry Smith 27064a2ae208SSatish Balay #undef __FUNCT__ 2707adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 2708adb62b0dSMatthew Knepley /*@ 2709adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2710adb62b0dSMatthew Knepley maximum time for iteration. 2711adb62b0dSMatthew Knepley 27123f9fe445SBarry Smith Not Collective 2713adb62b0dSMatthew Knepley 2714adb62b0dSMatthew Knepley Input Parameters: 2715adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 27160298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 27170298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2718adb62b0dSMatthew Knepley 2719adb62b0dSMatthew Knepley Level: intermediate 2720adb62b0dSMatthew Knepley 2721adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2722adb62b0dSMatthew Knepley @*/ 27237087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2724adb62b0dSMatthew Knepley { 2725adb62b0dSMatthew Knepley PetscFunctionBegin; 27260700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2727abc0a331SBarry Smith if (maxsteps) { 27284482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2729adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2730adb62b0dSMatthew Knepley } 2731abc0a331SBarry Smith if (maxtime) { 27324482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2733adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2734adb62b0dSMatthew Knepley } 2735adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2736adb62b0dSMatthew Knepley } 2737adb62b0dSMatthew Knepley 2738adb62b0dSMatthew Knepley #undef __FUNCT__ 27394a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 2740d763cef2SBarry Smith /*@ 2741d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2742d763cef2SBarry Smith maximum time for iteration. 2743d763cef2SBarry Smith 27443f9fe445SBarry Smith Logically Collective on TS 2745d763cef2SBarry Smith 2746d763cef2SBarry Smith Input Parameters: 2747d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2748d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2749d763cef2SBarry Smith - maxtime - final time to iterate to 2750d763cef2SBarry Smith 2751d763cef2SBarry Smith Options Database Keys: 2752d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 27533bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2754d763cef2SBarry Smith 2755d763cef2SBarry Smith Notes: 2756d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2757d763cef2SBarry Smith 2758d763cef2SBarry Smith Level: intermediate 2759d763cef2SBarry Smith 2760d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2761a43b19c4SJed Brown 2762a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2763d763cef2SBarry Smith @*/ 27647087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2765d763cef2SBarry Smith { 2766d763cef2SBarry Smith PetscFunctionBegin; 27670700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2768c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2769c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 277039b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 277139b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2772d763cef2SBarry Smith PetscFunctionReturn(0); 2773d763cef2SBarry Smith } 2774d763cef2SBarry Smith 27754a2ae208SSatish Balay #undef __FUNCT__ 27764a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 2777d763cef2SBarry Smith /*@ 2778d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2779d763cef2SBarry Smith for use by the TS routines. 2780d763cef2SBarry Smith 27813f9fe445SBarry Smith Logically Collective on TS and Vec 2782d763cef2SBarry Smith 2783d763cef2SBarry Smith Input Parameters: 2784d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 27850910c330SBarry Smith - u - the solution vector 2786d763cef2SBarry Smith 2787d763cef2SBarry Smith Level: beginner 2788d763cef2SBarry Smith 2789d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2790d763cef2SBarry Smith @*/ 27910910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2792d763cef2SBarry Smith { 27938737fe31SLisandro Dalcin PetscErrorCode ierr; 2794496e6a7aSJed Brown DM dm; 27958737fe31SLisandro Dalcin 2796d763cef2SBarry Smith PetscFunctionBegin; 27970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27980910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 27990910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 28006bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 28010910c330SBarry Smith ts->vec_sol = u; 2802bbd56ea5SKarl Rupp 2803496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 28040910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2805d763cef2SBarry Smith PetscFunctionReturn(0); 2806d763cef2SBarry Smith } 2807d763cef2SBarry Smith 2808e74ef692SMatthew Knepley #undef __FUNCT__ 280973b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps" 281073b18844SBarry Smith /*@ 281173b18844SBarry Smith TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time 281273b18844SBarry Smith 281373b18844SBarry Smith Logically Collective on TS 281473b18844SBarry Smith 281573b18844SBarry Smith Input Parameters: 281673b18844SBarry Smith + ts - the TS context obtained from TSCreate() 281773b18844SBarry Smith . steps - number of steps to use 281873b18844SBarry Smith 281973b18844SBarry Smith Level: intermediate 282073b18844SBarry Smith 282173b18844SBarry Smith Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this 282273b18844SBarry Smith so as to integrate back to less than the original timestep 282373b18844SBarry Smith 282473b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations 282573b18844SBarry Smith 282673b18844SBarry Smith .seealso: TSSetExactFinalTime() 282773b18844SBarry Smith @*/ 282873b18844SBarry Smith PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps) 282973b18844SBarry Smith { 283073b18844SBarry Smith PetscFunctionBegin; 283173b18844SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 283273b18844SBarry Smith PetscValidLogicalCollectiveInt(ts,steps,2); 283373b18844SBarry Smith if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps"); 283473b18844SBarry 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"); 283573b18844SBarry Smith ts->adjoint_max_steps = steps; 283673b18844SBarry Smith PetscFunctionReturn(0); 283773b18844SBarry Smith } 283873b18844SBarry Smith 283973b18844SBarry Smith #undef __FUNCT__ 2840dfb21088SHong Zhang #define __FUNCT__ "TSSetCostGradients" 2841c235aa8dSHong Zhang /*@ 2842dfb21088SHong 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 28430cf82b59SBarry Smith for use by the TSAdjoint routines. 2844c235aa8dSHong Zhang 2845c235aa8dSHong Zhang Logically Collective on TS and Vec 2846c235aa8dSHong Zhang 2847c235aa8dSHong Zhang Input Parameters: 2848c235aa8dSHong Zhang + ts - the TS context obtained from TSCreate() 2849abc2977eSBarry 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 2850abc2977eSBarry Smith - mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters 2851c235aa8dSHong Zhang 2852c235aa8dSHong Zhang Level: beginner 2853c235aa8dSHong Zhang 2854abc2977eSBarry Smith Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime mu_i = df/dp|finaltime 28550cf82b59SBarry Smith 2856c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions 2857c235aa8dSHong Zhang @*/ 2858dfb21088SHong Zhang PetscErrorCode TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu) 2859c235aa8dSHong Zhang { 2860c235aa8dSHong Zhang PetscFunctionBegin; 2861c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2862abc2977eSBarry Smith PetscValidPointer(lambda,2); 2863abc2977eSBarry Smith ts->vecs_sensi = lambda; 2864abc2977eSBarry Smith ts->vecs_sensip = mu; 2865dfb21088SHong 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"); 2866abc2977eSBarry Smith ts->numcost = numcost; 2867ad8e2604SHong Zhang PetscFunctionReturn(0); 2868ad8e2604SHong Zhang } 2869ad8e2604SHong Zhang 2870ad8e2604SHong Zhang #undef __FUNCT__ 28715bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian" 2872ad8e2604SHong Zhang /*@C 28730cf82b59SBarry 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. 2874ad8e2604SHong Zhang 2875ad8e2604SHong Zhang Logically Collective on TS 2876ad8e2604SHong Zhang 2877ad8e2604SHong Zhang Input Parameters: 2878ad8e2604SHong Zhang + ts - The TS context obtained from TSCreate() 2879ad8e2604SHong Zhang - func - The function 2880ad8e2604SHong Zhang 2881ad8e2604SHong Zhang Calling sequence of func: 28820cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx); 288305755b9cSHong Zhang + t - current timestep 28840cf82b59SBarry Smith . y - input vector (current ODE solution) 288505755b9cSHong Zhang . A - output matrix 288605755b9cSHong Zhang - ctx - [optional] user-defined function context 2887ad8e2604SHong Zhang 2888ad8e2604SHong Zhang Level: intermediate 2889ad8e2604SHong Zhang 28900cf82b59SBarry 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 28910cf82b59SBarry Smith 2892ad8e2604SHong Zhang .keywords: TS, sensitivity 2893ad8e2604SHong Zhang .seealso: 2894ad8e2604SHong Zhang @*/ 28955bf8c567SBarry Smith PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 2896ad8e2604SHong Zhang { 2897ad8e2604SHong Zhang PetscErrorCode ierr; 2898ad8e2604SHong Zhang 2899ad8e2604SHong Zhang PetscFunctionBegin; 2900ad8e2604SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2901ad8e2604SHong Zhang if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 2902ad8e2604SHong Zhang 2903ad8e2604SHong Zhang ts->rhsjacobianp = func; 2904ad8e2604SHong Zhang ts->rhsjacobianpctx = ctx; 2905ad8e2604SHong Zhang if(Amat) { 2906ad8e2604SHong Zhang ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 2907ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2908ad8e2604SHong Zhang ts->Jacp = Amat; 2909ad8e2604SHong Zhang } 2910ad8e2604SHong Zhang PetscFunctionReturn(0); 2911ad8e2604SHong Zhang } 2912ad8e2604SHong Zhang 2913ad8e2604SHong Zhang #undef __FUNCT__ 29145bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian" 29150cf82b59SBarry Smith /*@C 29165bf8c567SBarry Smith TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function. 2917ad8e2604SHong Zhang 2918ad8e2604SHong Zhang Collective on TS 2919ad8e2604SHong Zhang 2920ad8e2604SHong Zhang Input Parameters: 2921ad8e2604SHong Zhang . ts - The TS context obtained from TSCreate() 2922ad8e2604SHong Zhang 2923ad8e2604SHong Zhang Level: developer 2924ad8e2604SHong Zhang 2925ad8e2604SHong Zhang .keywords: TS, sensitivity 29265bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian() 2927ad8e2604SHong Zhang @*/ 29285bf8c567SBarry Smith PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat) 2929ad8e2604SHong Zhang { 2930ad8e2604SHong Zhang PetscErrorCode ierr; 2931ad8e2604SHong Zhang 2932ad8e2604SHong Zhang PetscFunctionBegin; 2933ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2934ad8e2604SHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 2935ad8e2604SHong Zhang PetscValidPointer(Amat,4); 2936ad8e2604SHong Zhang 2937ad8e2604SHong Zhang PetscStackPush("TS user JacobianP function for sensitivity analysis"); 2938ad8e2604SHong Zhang ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 2939ad8e2604SHong Zhang PetscStackPop; 2940c235aa8dSHong Zhang PetscFunctionReturn(0); 2941c235aa8dSHong Zhang } 2942c235aa8dSHong Zhang 2943c235aa8dSHong Zhang #undef __FUNCT__ 2944dfb21088SHong Zhang #define __FUNCT__ "TSSetCostIntegrand" 29456fd0887fSHong Zhang /*@C 2946dfb21088SHong Zhang TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions 29476fd0887fSHong Zhang 29486fd0887fSHong Zhang Logically Collective on TS 29496fd0887fSHong Zhang 29506fd0887fSHong Zhang Input Parameters: 29516fd0887fSHong Zhang + ts - the TS context obtained from TSCreate() 2952abc2977eSBarry Smith . numcost - number of gradients to be computed, this is the number of cost functions 29530cf82b59SBarry Smith . rf - routine for evaluating the integrand function 2954b612ec54SBarry Smith . drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y 2955b612ec54SBarry Smith . drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p 2956feaa1ddbSSatish Balay . fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run 2957b612ec54SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 29586fd0887fSHong Zhang 29590cf82b59SBarry Smith Calling sequence of rf: 29600cf82b59SBarry Smith $ rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx); 29616fd0887fSHong Zhang 29626fd0887fSHong Zhang + t - current timestep 29630cf82b59SBarry Smith . y - input vector 29640cf82b59SBarry Smith . f - function result; one vector entry for each cost function 29656fd0887fSHong Zhang - ctx - [optional] user-defined function context 29666fd0887fSHong Zhang 2967b612ec54SBarry Smith Calling sequence of drdyf: 29680cf82b59SBarry Smith $ PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx); 2969b612ec54SBarry Smith 2970b612ec54SBarry Smith Calling sequence of drdpf: 29710cf82b59SBarry Smith $ PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx); 2972b612ec54SBarry Smith 2973b612ec54SBarry Smith Level: intermediate 29746fd0887fSHong Zhang 2975abc2977eSBarry Smith Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions 29760cf82b59SBarry Smith 29776fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 29786fd0887fSHong Zhang 2979dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients() 29806fd0887fSHong Zhang @*/ 2981dfb21088SHong Zhang PetscErrorCode TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*), 2982d8151eeaSBarry Smith PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*), 2983b1cb36f3SHong Zhang PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*), 2984b1cb36f3SHong Zhang PetscBool fwd,void *ctx) 29856fd0887fSHong Zhang { 29866fd0887fSHong Zhang PetscErrorCode ierr; 29876fd0887fSHong Zhang 29886fd0887fSHong Zhang PetscFunctionBegin; 29896fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2990dfb21088SHong 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()"); 2991c72ed514SHong Zhang if (!ts->numcost) ts->numcost=numcost; 29926fd0887fSHong Zhang 2993b1cb36f3SHong Zhang ts->costintegralfwd = fwd; /* Evaluate the cost integral in forward run if fwd is true */ 2994abc2977eSBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr); 29952c39e106SBarry Smith ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr); 29960cf82b59SBarry Smith ts->costintegrand = rf; 299705755b9cSHong Zhang ts->costintegrandctx = ctx; 2998b612ec54SBarry Smith ts->drdyfunction = drdyf; 2999b612ec54SBarry Smith ts->drdpfunction = drdpf; 30006fd0887fSHong Zhang PetscFunctionReturn(0); 30016fd0887fSHong Zhang } 30026fd0887fSHong Zhang 30036fd0887fSHong Zhang #undef __FUNCT__ 3004dfb21088SHong Zhang #define __FUNCT__ "TSGetCostIntegral" 300536eaed60SHong Zhang /*@ 3006dfb21088SHong Zhang TSGetCostIntegral - Returns the values of the integral term in the cost functions. 300736eaed60SHong Zhang It is valid to call the routine after a backward run. 300836eaed60SHong Zhang 300936eaed60SHong Zhang Not Collective 301036eaed60SHong Zhang 301136eaed60SHong Zhang Input Parameter: 301236eaed60SHong Zhang . ts - the TS context obtained from TSCreate() 301336eaed60SHong Zhang 301436eaed60SHong Zhang Output Parameter: 30152c39e106SBarry Smith . v - the vector containing the integrals for each cost function 301636eaed60SHong Zhang 301736eaed60SHong Zhang Level: intermediate 301836eaed60SHong Zhang 3019dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 302036eaed60SHong Zhang 302136eaed60SHong Zhang .keywords: TS, sensitivity analysis 302236eaed60SHong Zhang @*/ 3023dfb21088SHong Zhang PetscErrorCode TSGetCostIntegral(TS ts,Vec *v) 302436eaed60SHong Zhang { 302536eaed60SHong Zhang PetscFunctionBegin; 302636eaed60SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 302736eaed60SHong Zhang PetscValidPointer(v,2); 30282c39e106SBarry Smith *v = ts->vec_costintegral; 302936eaed60SHong Zhang PetscFunctionReturn(0); 303036eaed60SHong Zhang } 303136eaed60SHong Zhang 303236eaed60SHong Zhang #undef __FUNCT__ 3033d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand" 30346fd0887fSHong Zhang /*@ 30352c39e106SBarry Smith TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions. 30366fd0887fSHong Zhang 30376fd0887fSHong Zhang Input Parameters: 30386fd0887fSHong Zhang + ts - the TS context 30396fd0887fSHong Zhang . t - current time 30400cf82b59SBarry Smith - y - state vector, i.e. current solution 30416fd0887fSHong Zhang 30426fd0887fSHong Zhang Output Parameter: 3043abc2977eSBarry Smith . q - vector of size numcost to hold the outputs 30446fd0887fSHong Zhang 30456fd0887fSHong Zhang Note: 30466fd0887fSHong Zhang Most users should not need to explicitly call this routine, as it 30476fd0887fSHong Zhang is used internally within the sensitivity analysis context. 30486fd0887fSHong Zhang 30496fd0887fSHong Zhang Level: developer 30506fd0887fSHong Zhang 30516fd0887fSHong Zhang .keywords: TS, compute 30526fd0887fSHong Zhang 3053dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 30546fd0887fSHong Zhang @*/ 30550cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q) 30566fd0887fSHong Zhang { 30576fd0887fSHong Zhang PetscErrorCode ierr; 30586fd0887fSHong Zhang 30596fd0887fSHong Zhang PetscFunctionBegin; 30606fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30610cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 30626fd0887fSHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,4); 30636fd0887fSHong Zhang 30640cf82b59SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 3065e4680132SHong Zhang if (ts->costintegrand) { 3066e4680132SHong Zhang PetscStackPush("TS user integrand in the cost function"); 30670cf82b59SBarry Smith ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr); 30686fd0887fSHong Zhang PetscStackPop; 30696fd0887fSHong Zhang } else { 30706fd0887fSHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 30716fd0887fSHong Zhang } 30726fd0887fSHong Zhang 30730cf82b59SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 30746fd0887fSHong Zhang PetscFunctionReturn(0); 30756fd0887fSHong Zhang } 30766fd0887fSHong Zhang 30776fd0887fSHong Zhang #undef __FUNCT__ 3078d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction" 307905755b9cSHong Zhang /*@ 3080d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 308105755b9cSHong Zhang 308205755b9cSHong Zhang Collective on TS 308305755b9cSHong Zhang 308405755b9cSHong Zhang Input Parameters: 308505755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 308605755b9cSHong Zhang 308705755b9cSHong Zhang Notes: 3088d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 308905755b9cSHong Zhang so most users would not generally call this routine themselves. 309005755b9cSHong Zhang 309105755b9cSHong Zhang Level: developer 309205755b9cSHong Zhang 309305755b9cSHong Zhang .keywords: TS, sensitivity 3094d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction() 309505755b9cSHong Zhang @*/ 30960cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy) 309705755b9cSHong Zhang { 309805755b9cSHong Zhang PetscErrorCode ierr; 309905755b9cSHong Zhang 310005755b9cSHong Zhang PetscFunctionBegin; 310105755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31020cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 310305755b9cSHong Zhang 310405755b9cSHong Zhang PetscStackPush("TS user DRDY function for sensitivity analysis"); 31050cf82b59SBarry Smith ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr); 310605755b9cSHong Zhang PetscStackPop; 310705755b9cSHong Zhang PetscFunctionReturn(0); 310805755b9cSHong Zhang } 310905755b9cSHong Zhang 311005755b9cSHong Zhang #undef __FUNCT__ 3111d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction" 311205755b9cSHong Zhang /*@ 3113d4aa0a58SBarry Smith TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 311405755b9cSHong Zhang 311505755b9cSHong Zhang Collective on TS 311605755b9cSHong Zhang 311705755b9cSHong Zhang Input Parameters: 311805755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 311905755b9cSHong Zhang 312005755b9cSHong Zhang Notes: 312105755b9cSHong Zhang TSDRDPFunction() is typically used for sensitivity implementation, 312205755b9cSHong Zhang so most users would not generally call this routine themselves. 312305755b9cSHong Zhang 312405755b9cSHong Zhang Level: developer 312505755b9cSHong Zhang 312605755b9cSHong Zhang .keywords: TS, sensitivity 3127d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction() 312805755b9cSHong Zhang @*/ 31290cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp) 313005755b9cSHong Zhang { 313105755b9cSHong Zhang PetscErrorCode ierr; 313205755b9cSHong Zhang 313305755b9cSHong Zhang PetscFunctionBegin; 313405755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31350cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 313605755b9cSHong Zhang 313705755b9cSHong Zhang PetscStackPush("TS user DRDP function for sensitivity analysis"); 31380cf82b59SBarry Smith ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr); 313905755b9cSHong Zhang PetscStackPop; 314005755b9cSHong Zhang PetscFunctionReturn(0); 314105755b9cSHong Zhang } 314205755b9cSHong Zhang 314305755b9cSHong Zhang #undef __FUNCT__ 3144e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 3145ac226902SBarry Smith /*@C 3146000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 31473f2090d5SJed Brown called once at the beginning of each time step. 3148000e7ae3SMatthew Knepley 31493f9fe445SBarry Smith Logically Collective on TS 3150000e7ae3SMatthew Knepley 3151000e7ae3SMatthew Knepley Input Parameters: 3152000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3153000e7ae3SMatthew Knepley - func - The function 3154000e7ae3SMatthew Knepley 3155000e7ae3SMatthew Knepley Calling sequence of func: 3156000e7ae3SMatthew Knepley . func (TS ts); 3157000e7ae3SMatthew Knepley 3158000e7ae3SMatthew Knepley Level: intermediate 3159000e7ae3SMatthew Knepley 3160b8123daeSJed Brown Note: 3161b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 3162b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 3163b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 3164b8123daeSJed Brown 3165000e7ae3SMatthew Knepley .keywords: TS, timestep 31669be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 3167000e7ae3SMatthew Knepley @*/ 31687087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3169000e7ae3SMatthew Knepley { 3170000e7ae3SMatthew Knepley PetscFunctionBegin; 31710700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3172ae60f76fSBarry Smith ts->prestep = func; 3173000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3174000e7ae3SMatthew Knepley } 3175000e7ae3SMatthew Knepley 3176e74ef692SMatthew Knepley #undef __FUNCT__ 31773f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 317809ee8438SJed Brown /*@ 31793f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 31803f2090d5SJed Brown 31813f2090d5SJed Brown Collective on TS 31823f2090d5SJed Brown 31833f2090d5SJed Brown Input Parameters: 31843f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 31853f2090d5SJed Brown 31863f2090d5SJed Brown Notes: 31873f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 31883f2090d5SJed Brown so most users would not generally call this routine themselves. 31893f2090d5SJed Brown 31903f2090d5SJed Brown Level: developer 31913f2090d5SJed Brown 31923f2090d5SJed Brown .keywords: TS, timestep 31939be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 31943f2090d5SJed Brown @*/ 31957087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 31963f2090d5SJed Brown { 31973f2090d5SJed Brown PetscErrorCode ierr; 31983f2090d5SJed Brown 31993f2090d5SJed Brown PetscFunctionBegin; 32000700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3201ae60f76fSBarry Smith if (ts->prestep) { 3202ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 3203312ce896SJed Brown } 32043f2090d5SJed Brown PetscFunctionReturn(0); 32053f2090d5SJed Brown } 32063f2090d5SJed Brown 32073f2090d5SJed Brown #undef __FUNCT__ 3208b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 3209b8123daeSJed Brown /*@C 3210b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3211b8123daeSJed Brown called once at the beginning of each stage. 3212b8123daeSJed Brown 3213b8123daeSJed Brown Logically Collective on TS 3214b8123daeSJed Brown 3215b8123daeSJed Brown Input Parameters: 3216b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 3217b8123daeSJed Brown - func - The function 3218b8123daeSJed Brown 3219b8123daeSJed Brown Calling sequence of func: 3220b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 3221b8123daeSJed Brown 3222b8123daeSJed Brown Level: intermediate 3223b8123daeSJed Brown 3224b8123daeSJed Brown Note: 3225b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 3226b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 3227b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 3228b8123daeSJed Brown 3229b8123daeSJed Brown .keywords: TS, timestep 32309be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3231b8123daeSJed Brown @*/ 3232b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 3233b8123daeSJed Brown { 3234b8123daeSJed Brown PetscFunctionBegin; 3235b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3236ae60f76fSBarry Smith ts->prestage = func; 3237b8123daeSJed Brown PetscFunctionReturn(0); 3238b8123daeSJed Brown } 3239b8123daeSJed Brown 3240b8123daeSJed Brown #undef __FUNCT__ 32419be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage" 32429be3e283SDebojyoti Ghosh /*@C 32439be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 32449be3e283SDebojyoti Ghosh called once at the end of each stage. 32459be3e283SDebojyoti Ghosh 32469be3e283SDebojyoti Ghosh Logically Collective on TS 32479be3e283SDebojyoti Ghosh 32489be3e283SDebojyoti Ghosh Input Parameters: 32499be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 32509be3e283SDebojyoti Ghosh - func - The function 32519be3e283SDebojyoti Ghosh 32529be3e283SDebojyoti Ghosh Calling sequence of func: 32539be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 32549be3e283SDebojyoti Ghosh 32559be3e283SDebojyoti Ghosh Level: intermediate 32569be3e283SDebojyoti Ghosh 32579be3e283SDebojyoti Ghosh Note: 32589be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 32599be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 32609be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 32619be3e283SDebojyoti Ghosh 32629be3e283SDebojyoti Ghosh .keywords: TS, timestep 32639be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 32649be3e283SDebojyoti Ghosh @*/ 32659be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 32669be3e283SDebojyoti Ghosh { 32679be3e283SDebojyoti Ghosh PetscFunctionBegin; 32689be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 32699be3e283SDebojyoti Ghosh ts->poststage = func; 32709be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 32719be3e283SDebojyoti Ghosh } 32729be3e283SDebojyoti Ghosh 32739be3e283SDebojyoti Ghosh #undef __FUNCT__ 3274*c688d042SShri Abhyankar #define __FUNCT__ "TSSetPostEvaluate" 3275*c688d042SShri Abhyankar /*@C 3276*c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 3277*c688d042SShri Abhyankar called once at the end of each step evaluation. 3278*c688d042SShri Abhyankar 3279*c688d042SShri Abhyankar Logically Collective on TS 3280*c688d042SShri Abhyankar 3281*c688d042SShri Abhyankar Input Parameters: 3282*c688d042SShri Abhyankar + ts - The TS context obtained from TSCreate() 3283*c688d042SShri Abhyankar - func - The function 3284*c688d042SShri Abhyankar 3285*c688d042SShri Abhyankar Calling sequence of func: 3286*c688d042SShri Abhyankar . PetscErrorCode func(TS ts); 3287*c688d042SShri Abhyankar 3288*c688d042SShri Abhyankar Level: intermediate 3289*c688d042SShri Abhyankar 3290*c688d042SShri Abhyankar Note: 3291*c688d042SShri Abhyankar This is called after the next step solution is evaluated allowing to modify it, if need be. The solution can be obtained 3292*c688d042SShri Abhyankar with TSGetSolution(), the time step with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime() 3293*c688d042SShri Abhyankar 3294*c688d042SShri Abhyankar .keywords: TS, timestep 3295*c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3296*c688d042SShri Abhyankar @*/ 3297*c688d042SShri Abhyankar PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS)) 3298*c688d042SShri Abhyankar { 3299*c688d042SShri Abhyankar PetscFunctionBegin; 3300*c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3301*c688d042SShri Abhyankar ts->postevaluate = func; 3302*c688d042SShri Abhyankar PetscFunctionReturn(0); 3303*c688d042SShri Abhyankar } 3304*c688d042SShri Abhyankar 3305*c688d042SShri Abhyankar #undef __FUNCT__ 3306b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 3307b8123daeSJed Brown /*@ 3308b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 3309b8123daeSJed Brown 3310b8123daeSJed Brown Collective on TS 3311b8123daeSJed Brown 3312b8123daeSJed Brown Input Parameters: 3313b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 33149be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3315b8123daeSJed Brown 3316b8123daeSJed Brown Notes: 3317b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 3318b8123daeSJed Brown most users would not generally call this routine themselves. 3319b8123daeSJed Brown 3320b8123daeSJed Brown Level: developer 3321b8123daeSJed Brown 3322b8123daeSJed Brown .keywords: TS, timestep 33239be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 3324b8123daeSJed Brown @*/ 3325b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3326b8123daeSJed Brown { 3327b8123daeSJed Brown PetscErrorCode ierr; 3328b8123daeSJed Brown 3329b8123daeSJed Brown PetscFunctionBegin; 3330b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3331ae60f76fSBarry Smith if (ts->prestage) { 3332ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 3333b8123daeSJed Brown } 3334b8123daeSJed Brown PetscFunctionReturn(0); 3335b8123daeSJed Brown } 3336b8123daeSJed Brown 3337b8123daeSJed Brown #undef __FUNCT__ 33389be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage" 33399be3e283SDebojyoti Ghosh /*@ 33409be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 33419be3e283SDebojyoti Ghosh 33429be3e283SDebojyoti Ghosh Collective on TS 33439be3e283SDebojyoti Ghosh 33449be3e283SDebojyoti Ghosh Input Parameters: 33459be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 33469be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 33479be3e283SDebojyoti Ghosh stageindex - Stage number 33489be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 33499be3e283SDebojyoti Ghosh of stages) with the stage solutions 33509be3e283SDebojyoti Ghosh 33519be3e283SDebojyoti Ghosh Notes: 33529be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 33539be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 33549be3e283SDebojyoti Ghosh 33559be3e283SDebojyoti Ghosh Level: developer 33569be3e283SDebojyoti Ghosh 33579be3e283SDebojyoti Ghosh .keywords: TS, timestep 33589be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 33599be3e283SDebojyoti Ghosh @*/ 33609be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 33619be3e283SDebojyoti Ghosh { 33629be3e283SDebojyoti Ghosh PetscErrorCode ierr; 33639be3e283SDebojyoti Ghosh 33649be3e283SDebojyoti Ghosh PetscFunctionBegin; 33659be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 33664beae5d8SLisandro Dalcin if (ts->poststage) { 33679be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 33689be3e283SDebojyoti Ghosh } 33699be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 33709be3e283SDebojyoti Ghosh } 33719be3e283SDebojyoti Ghosh 33729be3e283SDebojyoti Ghosh #undef __FUNCT__ 3373*c688d042SShri Abhyankar #define __FUNCT__ "TSPostEvaluate" 3374*c688d042SShri Abhyankar /*@ 3375*c688d042SShri Abhyankar TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate() 3376*c688d042SShri Abhyankar 3377*c688d042SShri Abhyankar Collective on TS 3378*c688d042SShri Abhyankar 3379*c688d042SShri Abhyankar Input Parameters: 3380*c688d042SShri Abhyankar . ts - The TS context obtained from TSCreate() 3381*c688d042SShri Abhyankar 3382*c688d042SShri Abhyankar Notes: 3383*c688d042SShri Abhyankar TSPostEvaluate() is typically used within time stepping implementations, 3384*c688d042SShri Abhyankar most users would not generally call this routine themselves. 3385*c688d042SShri Abhyankar 3386*c688d042SShri Abhyankar Level: developer 3387*c688d042SShri Abhyankar 3388*c688d042SShri Abhyankar .keywords: TS, timestep 3389*c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep() 3390*c688d042SShri Abhyankar @*/ 3391*c688d042SShri Abhyankar PetscErrorCode TSPostEvaluate(TS ts) 3392*c688d042SShri Abhyankar { 3393*c688d042SShri Abhyankar PetscErrorCode ierr; 3394*c688d042SShri Abhyankar 3395*c688d042SShri Abhyankar PetscFunctionBegin; 3396*c688d042SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3397*c688d042SShri Abhyankar if (ts->postevaluate) { 3398*c688d042SShri Abhyankar PetscStackCallStandard((*ts->postevaluate),(ts)); 3399*c688d042SShri Abhyankar } 3400*c688d042SShri Abhyankar PetscFunctionReturn(0); 3401*c688d042SShri Abhyankar } 3402*c688d042SShri Abhyankar 3403*c688d042SShri Abhyankar #undef __FUNCT__ 3404e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 3405ac226902SBarry Smith /*@C 3406000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 34073f2090d5SJed Brown called once at the end of each time step. 3408000e7ae3SMatthew Knepley 34093f9fe445SBarry Smith Logically Collective on TS 3410000e7ae3SMatthew Knepley 3411000e7ae3SMatthew Knepley Input Parameters: 3412000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3413000e7ae3SMatthew Knepley - func - The function 3414000e7ae3SMatthew Knepley 3415000e7ae3SMatthew Knepley Calling sequence of func: 3416b8123daeSJed Brown $ func (TS ts); 3417000e7ae3SMatthew Knepley 3418000e7ae3SMatthew Knepley Level: intermediate 3419000e7ae3SMatthew Knepley 3420000e7ae3SMatthew Knepley .keywords: TS, timestep 3421b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 3422000e7ae3SMatthew Knepley @*/ 34237087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 3424000e7ae3SMatthew Knepley { 3425000e7ae3SMatthew Knepley PetscFunctionBegin; 34260700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3427ae60f76fSBarry Smith ts->poststep = func; 3428000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3429000e7ae3SMatthew Knepley } 3430000e7ae3SMatthew Knepley 3431e74ef692SMatthew Knepley #undef __FUNCT__ 34323f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 343309ee8438SJed Brown /*@ 34343f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 34353f2090d5SJed Brown 34363f2090d5SJed Brown Collective on TS 34373f2090d5SJed Brown 34383f2090d5SJed Brown Input Parameters: 34393f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 34403f2090d5SJed Brown 34413f2090d5SJed Brown Notes: 34423f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 34433f2090d5SJed Brown so most users would not generally call this routine themselves. 34443f2090d5SJed Brown 34453f2090d5SJed Brown Level: developer 34463f2090d5SJed Brown 34473f2090d5SJed Brown .keywords: TS, timestep 34483f2090d5SJed Brown @*/ 34497087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 34503f2090d5SJed Brown { 34513f2090d5SJed Brown PetscErrorCode ierr; 34523f2090d5SJed Brown 34533f2090d5SJed Brown PetscFunctionBegin; 34540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3455ae60f76fSBarry Smith if (ts->poststep) { 3456ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 345772ac3e02SJed Brown } 34583f2090d5SJed Brown PetscFunctionReturn(0); 34593f2090d5SJed Brown } 34603f2090d5SJed Brown 3461d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 3462d763cef2SBarry Smith 34634a2ae208SSatish Balay #undef __FUNCT__ 3464a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 3465d763cef2SBarry Smith /*@C 3466a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 3467d763cef2SBarry Smith timestep to display the iteration's progress. 3468d763cef2SBarry Smith 34693f9fe445SBarry Smith Logically Collective on TS 3470d763cef2SBarry Smith 3471d763cef2SBarry Smith Input Parameters: 3472d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3473e213d8f1SJed Brown . monitor - monitoring routine 3474329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 34750298fd71SBarry Smith monitor routine (use NULL if no context is desired) 3476b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 34770298fd71SBarry Smith (may be NULL) 3478d763cef2SBarry Smith 3479e213d8f1SJed Brown Calling sequence of monitor: 34800910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 3481d763cef2SBarry Smith 3482d763cef2SBarry Smith + ts - the TS context 348363e21af5SBarry 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) 34841f06c33eSBarry Smith . time - current time 34850910c330SBarry Smith . u - current iterate 3486d763cef2SBarry Smith - mctx - [optional] monitoring context 3487d763cef2SBarry Smith 3488d763cef2SBarry Smith Notes: 3489d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 3490d763cef2SBarry Smith already has been loaded. 3491d763cef2SBarry Smith 3492025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 3493025f1a04SBarry Smith 3494d763cef2SBarry Smith Level: intermediate 3495d763cef2SBarry Smith 3496d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3497d763cef2SBarry Smith 3498a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 3499d763cef2SBarry Smith @*/ 3500c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 3501d763cef2SBarry Smith { 350278064530SBarry Smith PetscErrorCode ierr; 350378064530SBarry Smith PetscInt i; 350478064530SBarry Smith PetscBool identical; 350578064530SBarry Smith 3506d763cef2SBarry Smith PetscFunctionBegin; 35070700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 350878064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 350978064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr); 351078064530SBarry Smith if (identical) PetscFunctionReturn(0); 351178064530SBarry Smith } 351217186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 3513d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 35148704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 3515d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 3516d763cef2SBarry Smith PetscFunctionReturn(0); 3517d763cef2SBarry Smith } 3518d763cef2SBarry Smith 35194a2ae208SSatish Balay #undef __FUNCT__ 3520a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 3521d763cef2SBarry Smith /*@C 3522a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 3523d763cef2SBarry Smith 35243f9fe445SBarry Smith Logically Collective on TS 3525d763cef2SBarry Smith 3526d763cef2SBarry Smith Input Parameters: 3527d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3528d763cef2SBarry Smith 3529d763cef2SBarry Smith Notes: 3530d763cef2SBarry Smith There is no way to remove a single, specific monitor. 3531d763cef2SBarry Smith 3532d763cef2SBarry Smith Level: intermediate 3533d763cef2SBarry Smith 3534d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3535d763cef2SBarry Smith 3536a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 3537d763cef2SBarry Smith @*/ 35387087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 3539d763cef2SBarry Smith { 3540d952e501SBarry Smith PetscErrorCode ierr; 3541d952e501SBarry Smith PetscInt i; 3542d952e501SBarry Smith 3543d763cef2SBarry Smith PetscFunctionBegin; 35440700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3545d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 35468704b422SBarry Smith if (ts->monitordestroy[i]) { 35478704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 3548d952e501SBarry Smith } 3549d952e501SBarry Smith } 3550d763cef2SBarry Smith ts->numbermonitors = 0; 3551d763cef2SBarry Smith PetscFunctionReturn(0); 3552d763cef2SBarry Smith } 3553d763cef2SBarry Smith 35544a2ae208SSatish Balay #undef __FUNCT__ 3555a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 3556721cd6eeSBarry Smith /*@C 3557721cd6eeSBarry Smith TSMonitorDefault - The Default monitor, prints the timestep and time for each step 35585516499fSSatish Balay 35595516499fSSatish Balay Level: intermediate 356041251cbbSSatish Balay 35615516499fSSatish Balay .keywords: TS, set, monitor 35625516499fSSatish Balay 356363e21af5SBarry Smith .seealso: TSMonitorSet() 356441251cbbSSatish Balay @*/ 3565721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf) 3566d763cef2SBarry Smith { 3567dfbe8321SBarry Smith PetscErrorCode ierr; 3568721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 356941aca3d6SBarry Smith PetscBool iascii,ibinary; 3570d132466eSBarry Smith 3571d763cef2SBarry Smith PetscFunctionBegin; 35724d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 357341aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 357441aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr); 3575721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 357641aca3d6SBarry Smith if (iascii) { 3577649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 357863e21af5SBarry Smith if (step == -1){ /* this indicates it is an interpolated solution */ 357963e21af5SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr); 358063e21af5SBarry Smith } else { 35818392e04aSShri 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); 358263e21af5SBarry Smith } 3583649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 358441aca3d6SBarry Smith } else if (ibinary) { 358541aca3d6SBarry Smith PetscMPIInt rank; 358641aca3d6SBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 358741aca3d6SBarry Smith if (!rank) { 3588450a797fSBarry Smith PetscBool skipHeader; 3589450a797fSBarry Smith PetscInt classid = REAL_FILE_CLASSID; 3590450a797fSBarry Smith 3591450a797fSBarry Smith ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr); 3592450a797fSBarry Smith if (!skipHeader) { 3593450a797fSBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 3594450a797fSBarry Smith } 359541aca3d6SBarry Smith ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr); 359641aca3d6SBarry Smith } else { 359741aca3d6SBarry Smith ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr); 359841aca3d6SBarry Smith } 359941aca3d6SBarry Smith } 3600721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3601d763cef2SBarry Smith PetscFunctionReturn(0); 3602d763cef2SBarry Smith } 3603d763cef2SBarry Smith 36044a2ae208SSatish Balay #undef __FUNCT__ 36059110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorSet" 36069110b2e7SHong Zhang /*@C 36079110b2e7SHong Zhang TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every 36089110b2e7SHong Zhang timestep to display the iteration's progress. 36099110b2e7SHong Zhang 36109110b2e7SHong Zhang Logically Collective on TS 36119110b2e7SHong Zhang 36129110b2e7SHong Zhang Input Parameters: 36139110b2e7SHong Zhang + ts - the TS context obtained from TSCreate() 36149110b2e7SHong Zhang . adjointmonitor - monitoring routine 36159110b2e7SHong Zhang . adjointmctx - [optional] user-defined context for private data for the 36169110b2e7SHong Zhang monitor routine (use NULL if no context is desired) 36179110b2e7SHong Zhang - adjointmonitordestroy - [optional] routine that frees monitor context 36189110b2e7SHong Zhang (may be NULL) 36199110b2e7SHong Zhang 36209110b2e7SHong Zhang Calling sequence of monitor: 36219110b2e7SHong Zhang $ int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx) 36229110b2e7SHong Zhang 36239110b2e7SHong Zhang + ts - the TS context 36249110b2e7SHong 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 36259110b2e7SHong Zhang been interpolated to) 36269110b2e7SHong Zhang . time - current time 36279110b2e7SHong Zhang . u - current iterate 36289110b2e7SHong Zhang . numcost - number of cost functionos 36299110b2e7SHong Zhang . lambda - sensitivities to initial conditions 36309110b2e7SHong Zhang . mu - sensitivities to parameters 36319110b2e7SHong Zhang - adjointmctx - [optional] adjoint monitoring context 36329110b2e7SHong Zhang 36339110b2e7SHong Zhang Notes: 36349110b2e7SHong Zhang This routine adds an additional monitor to the list of monitors that 36359110b2e7SHong Zhang already has been loaded. 36369110b2e7SHong Zhang 36379110b2e7SHong Zhang Fortran notes: Only a single monitor function can be set for each TS object 36389110b2e7SHong Zhang 36399110b2e7SHong Zhang Level: intermediate 36409110b2e7SHong Zhang 36419110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 36429110b2e7SHong Zhang 36439110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel() 36449110b2e7SHong Zhang @*/ 36459110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**)) 36469110b2e7SHong Zhang { 364778064530SBarry Smith PetscErrorCode ierr; 364878064530SBarry Smith PetscInt i; 364978064530SBarry Smith PetscBool identical; 365078064530SBarry Smith 36519110b2e7SHong Zhang PetscFunctionBegin; 36529110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 365378064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 365478064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);CHKERRQ(ierr); 365578064530SBarry Smith if (identical) PetscFunctionReturn(0); 365678064530SBarry Smith } 36579110b2e7SHong Zhang if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set"); 36589110b2e7SHong Zhang ts->adjointmonitor[ts->numberadjointmonitors] = adjointmonitor; 36599110b2e7SHong Zhang ts->adjointmonitordestroy[ts->numberadjointmonitors] = adjointmdestroy; 36609110b2e7SHong Zhang ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx; 36619110b2e7SHong Zhang PetscFunctionReturn(0); 36629110b2e7SHong Zhang } 36639110b2e7SHong Zhang 36649110b2e7SHong Zhang #undef __FUNCT__ 36659110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorCancel" 36669110b2e7SHong Zhang /*@C 36679110b2e7SHong Zhang TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object. 36689110b2e7SHong Zhang 36699110b2e7SHong Zhang Logically Collective on TS 36709110b2e7SHong Zhang 36719110b2e7SHong Zhang Input Parameters: 36729110b2e7SHong Zhang . ts - the TS context obtained from TSCreate() 36739110b2e7SHong Zhang 36749110b2e7SHong Zhang Notes: 36759110b2e7SHong Zhang There is no way to remove a single, specific monitor. 36769110b2e7SHong Zhang 36779110b2e7SHong Zhang Level: intermediate 36789110b2e7SHong Zhang 36799110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 36809110b2e7SHong Zhang 36819110b2e7SHong Zhang .seealso: TSAdjointMonitorSet() 36829110b2e7SHong Zhang @*/ 36839110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorCancel(TS ts) 36849110b2e7SHong Zhang { 36859110b2e7SHong Zhang PetscErrorCode ierr; 36869110b2e7SHong Zhang PetscInt i; 36879110b2e7SHong Zhang 36889110b2e7SHong Zhang PetscFunctionBegin; 36899110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36909110b2e7SHong Zhang for (i=0; i<ts->numberadjointmonitors; i++) { 36919110b2e7SHong Zhang if (ts->adjointmonitordestroy[i]) { 36929110b2e7SHong Zhang ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 36939110b2e7SHong Zhang } 36949110b2e7SHong Zhang } 36959110b2e7SHong Zhang ts->numberadjointmonitors = 0; 36969110b2e7SHong Zhang PetscFunctionReturn(0); 36979110b2e7SHong Zhang } 36989110b2e7SHong Zhang 36999110b2e7SHong Zhang #undef __FUNCT__ 3700110eb670SHong Zhang #define __FUNCT__ "TSAdjointMonitorDefault" 3701721cd6eeSBarry Smith /*@C 3702721cd6eeSBarry Smith TSAdjointMonitorDefault - the default monitor of adjoint computations 3703110eb670SHong Zhang 3704110eb670SHong Zhang Level: intermediate 3705110eb670SHong Zhang 3706110eb670SHong Zhang .keywords: TS, set, monitor 3707110eb670SHong Zhang 3708110eb670SHong Zhang .seealso: TSAdjointMonitorSet() 3709110eb670SHong Zhang @*/ 3710721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf) 3711110eb670SHong Zhang { 3712110eb670SHong Zhang PetscErrorCode ierr; 3713721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 3714110eb670SHong Zhang 3715110eb670SHong Zhang PetscFunctionBegin; 3716110eb670SHong Zhang PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3717721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 3718110eb670SHong Zhang ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3719110eb670SHong 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); 3720110eb670SHong Zhang ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3721721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3722110eb670SHong Zhang PetscFunctionReturn(0); 3723110eb670SHong Zhang } 3724110eb670SHong Zhang 3725110eb670SHong Zhang #undef __FUNCT__ 3726cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 3727cd652676SJed Brown /*@ 3728cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3729cd652676SJed Brown 3730cd652676SJed Brown Collective on TS 3731cd652676SJed Brown 3732cd652676SJed Brown Input Argument: 3733cd652676SJed Brown + ts - time stepping context 3734cd652676SJed Brown - t - time to interpolate to 3735cd652676SJed Brown 3736cd652676SJed Brown Output Argument: 37370910c330SBarry Smith . U - state at given time 3738cd652676SJed Brown 3739cd652676SJed Brown Level: intermediate 3740cd652676SJed Brown 3741cd652676SJed Brown Developer Notes: 3742cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3743cd652676SJed Brown 3744cd652676SJed Brown .keywords: TS, set 3745cd652676SJed Brown 3746874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve() 3747cd652676SJed Brown @*/ 37480910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3749cd652676SJed Brown { 3750cd652676SJed Brown PetscErrorCode ierr; 3751cd652676SJed Brown 3752cd652676SJed Brown PetscFunctionBegin; 3753cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3754b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3755be5899b3SLisandro 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); 3756ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 37570910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3758cd652676SJed Brown PetscFunctionReturn(0); 3759cd652676SJed Brown } 3760cd652676SJed Brown 3761cd652676SJed Brown #undef __FUNCT__ 37624a2ae208SSatish Balay #define __FUNCT__ "TSStep" 3763d763cef2SBarry Smith /*@ 37646d9e5789SSean Farley TSStep - Steps one time step 3765d763cef2SBarry Smith 3766d763cef2SBarry Smith Collective on TS 3767d763cef2SBarry Smith 3768d763cef2SBarry Smith Input Parameter: 3769d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3770d763cef2SBarry Smith 377127829d71SBarry Smith Level: developer 3772d763cef2SBarry Smith 3773b8123daeSJed Brown Notes: 377427829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 377527829d71SBarry Smith 3776b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3777b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3778b8123daeSJed Brown 377925cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 378025cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 378125cb2221SBarry Smith 3782d763cef2SBarry Smith .keywords: TS, timestep, solve 3783d763cef2SBarry Smith 37849be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3785d763cef2SBarry Smith @*/ 3786193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3787d763cef2SBarry Smith { 3788dfbe8321SBarry Smith PetscErrorCode ierr; 3789fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3790be5899b3SLisandro Dalcin PetscReal ptime; 3791d763cef2SBarry Smith 3792d763cef2SBarry Smith PetscFunctionBegin; 37930700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3794fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3795fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3796fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3797fffbeea8SBarry Smith " type = {Preprint},\n" 3798fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3799fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3800302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3801fffbeea8SBarry Smith 3802d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 380368bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3804d405a339SMatthew Knepley 3805a6772fa2SLisandro 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()"); 3806be5899b3SLisandro 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"); 3807a6772fa2SLisandro Dalcin 3808be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 3809362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3810be5899b3SLisandro Dalcin ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev; 3811e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3812d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3813193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3814d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3815be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 3816be5899b3SLisandro Dalcin ts->steps++; ts->total_steps++; 3817be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 381828d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3819362cd11cSLisandro Dalcin 3820362cd11cSLisandro Dalcin if (ts->reason < 0) { 3821cef5090cSJed Brown if (ts->errorifstepfailed) { 382208c7845fSBarry 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]); 382308c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3824d2daff3dSHong Zhang } 382508c7845fSBarry Smith } else if (!ts->reason) { 382608c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 382708c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 382808c7845fSBarry Smith } 382908c7845fSBarry Smith PetscFunctionReturn(0); 383008c7845fSBarry Smith } 383108c7845fSBarry Smith 383208c7845fSBarry Smith #undef __FUNCT__ 383308c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep" 383408c7845fSBarry Smith /*@ 3835b957a604SHong Zhang TSAdjointStep - Steps one time step backward in the adjoint run 383608c7845fSBarry Smith 383708c7845fSBarry Smith Collective on TS 383808c7845fSBarry Smith 383908c7845fSBarry Smith Input Parameter: 384008c7845fSBarry Smith . ts - the TS context obtained from TSCreate() 384108c7845fSBarry Smith 38426a4d4014SLisandro Dalcin Level: intermediate 38436a4d4014SLisandro Dalcin 3844b957a604SHong Zhang .keywords: TS, adjoint, step 38456a4d4014SLisandro Dalcin 3846b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve() 38476a4d4014SLisandro Dalcin @*/ 384808c7845fSBarry Smith PetscErrorCode TSAdjointStep(TS ts) 38496a4d4014SLisandro Dalcin { 385008c7845fSBarry Smith DM dm; 38516a4d4014SLisandro Dalcin PetscErrorCode ierr; 38526a4d4014SLisandro Dalcin 38536a4d4014SLisandro Dalcin PetscFunctionBegin; 38544a2ae208SSatish Balay PetscValidHeaderSpecific(ts,TS_CLASSID,1); 385508c7845fSBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 385608c7845fSBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3857d763cef2SBarry Smith 3858685405a1SBarry Smith ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr); 3859d763cef2SBarry Smith 3860be5899b3SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3861be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime; 386242f2b339SBarry 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); 3863be5899b3SLisandro Dalcin ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 386442f2b339SBarry Smith ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr); 38658d0ad7a8SHong Zhang ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 3866be5899b3SLisandro Dalcin ts->steps++; ts->total_steps--; 3867362cd11cSLisandro Dalcin 3868362cd11cSLisandro Dalcin if (ts->reason < 0) { 3869cef5090cSJed Brown if (ts->errorifstepfailed) { 38706c4ed002SBarry 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]); 38716c4ed002SBarry 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]); 38726c4ed002SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3873cef5090cSJed Brown } 3874362cd11cSLisandro Dalcin } else if (!ts->reason) { 387573b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3876362cd11cSLisandro Dalcin } 3877d763cef2SBarry Smith PetscFunctionReturn(0); 3878d763cef2SBarry Smith } 3879d763cef2SBarry Smith 3880d763cef2SBarry Smith #undef __FUNCT__ 38817cbde773SLisandro Dalcin #define __FUNCT__ "TSEvaluateWLTE" 38827cbde773SLisandro Dalcin /*@ 38837cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 38847cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 38857cbde773SLisandro Dalcin 38867cbde773SLisandro Dalcin Collective on TS 38877cbde773SLisandro Dalcin 38887cbde773SLisandro Dalcin Input Arguments: 38897cbde773SLisandro Dalcin + ts - time stepping context 38907cbde773SLisandro Dalcin . wnormtype - norm type, either NORM_2 or NORM_INFINITY 38917cbde773SLisandro Dalcin - order - optional, desired order for the error evaluation or PETSC_DECIDE 38927cbde773SLisandro Dalcin 38937cbde773SLisandro Dalcin Output Arguments: 38947cbde773SLisandro Dalcin + order - optional, the actual order of the error evaluation 38957cbde773SLisandro Dalcin - wlte - the weighted local truncation error norm 38967cbde773SLisandro Dalcin 38977cbde773SLisandro Dalcin Level: advanced 38987cbde773SLisandro Dalcin 38997cbde773SLisandro Dalcin Notes: 39007cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 39017cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 39027cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 39037cbde773SLisandro Dalcin 39047cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm() 39057cbde773SLisandro Dalcin @*/ 39067cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte) 39077cbde773SLisandro Dalcin { 39087cbde773SLisandro Dalcin PetscErrorCode ierr; 39097cbde773SLisandro Dalcin 39107cbde773SLisandro Dalcin PetscFunctionBegin; 39117cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 39127cbde773SLisandro Dalcin PetscValidType(ts,1); 39137cbde773SLisandro Dalcin PetscValidLogicalCollectiveEnum(ts,wnormtype,4); 39147cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order,3); 39157cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts,*order,3); 39167cbde773SLisandro Dalcin PetscValidRealPointer(wlte,4); 39177cbde773SLisandro Dalcin if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 39187cbde773SLisandro Dalcin if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name); 39197cbde773SLisandro Dalcin ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr); 39207cbde773SLisandro Dalcin PetscFunctionReturn(0); 39217cbde773SLisandro Dalcin } 39227cbde773SLisandro Dalcin 39237cbde773SLisandro Dalcin #undef __FUNCT__ 392405175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 392505175c85SJed Brown /*@ 392605175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 392705175c85SJed Brown 39281c3436cfSJed Brown Collective on TS 392905175c85SJed Brown 393005175c85SJed Brown Input Arguments: 39311c3436cfSJed Brown + ts - time stepping context 39321c3436cfSJed Brown . order - desired order of accuracy 39330298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 393405175c85SJed Brown 393505175c85SJed Brown Output Arguments: 39360910c330SBarry Smith . U - state at the end of the current step 393705175c85SJed Brown 393805175c85SJed Brown Level: advanced 393905175c85SJed Brown 3940108c343cSJed Brown Notes: 3941108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3942108c343cSJed 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. 3943108c343cSJed Brown 39441c3436cfSJed Brown .seealso: TSStep(), TSAdapt 394505175c85SJed Brown @*/ 39460910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 394705175c85SJed Brown { 394805175c85SJed Brown PetscErrorCode ierr; 394905175c85SJed Brown 395005175c85SJed Brown PetscFunctionBegin; 395105175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 395205175c85SJed Brown PetscValidType(ts,1); 39530910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3954ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 39550910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 395605175c85SJed Brown PetscFunctionReturn(0); 395705175c85SJed Brown } 395805175c85SJed Brown 3959b1cb36f3SHong Zhang #undef __FUNCT__ 3960b1cb36f3SHong Zhang #define __FUNCT__ "TSForwardCostIntegral" 3961b1cb36f3SHong Zhang /*@ 3962b1cb36f3SHong Zhang TSForwardCostIntegral - Evaluate the cost integral in the forward run. 3963b1cb36f3SHong Zhang 3964b1cb36f3SHong Zhang Collective on TS 3965b1cb36f3SHong Zhang 3966b1cb36f3SHong Zhang Input Arguments: 3967b1cb36f3SHong Zhang . ts - time stepping context 3968b1cb36f3SHong Zhang 3969b1cb36f3SHong Zhang Level: advanced 3970b1cb36f3SHong Zhang 3971b1cb36f3SHong Zhang Notes: 3972b1cb36f3SHong Zhang This function cannot be called until TSStep() has been completed. 3973b1cb36f3SHong Zhang 3974b1cb36f3SHong Zhang .seealso: TSSolve(), TSAdjointCostIntegral() 3975b1cb36f3SHong Zhang @*/ 3976b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts) 3977b1cb36f3SHong Zhang { 3978b1cb36f3SHong Zhang PetscErrorCode ierr; 3979b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3980b1cb36f3SHong 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); 3981b1cb36f3SHong Zhang ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr); 3982b1cb36f3SHong Zhang PetscFunctionReturn(0); 3983b1cb36f3SHong Zhang } 3984d67e68b7SBarry Smith 398505175c85SJed Brown #undef __FUNCT__ 3986d763cef2SBarry Smith #define __FUNCT__ "TSSolve" 3987d763cef2SBarry Smith /*@ 3988d763cef2SBarry Smith TSSolve - Steps the requested number of timesteps. 3989d763cef2SBarry Smith 3990d763cef2SBarry Smith Collective on TS 3991d763cef2SBarry Smith 3992d763cef2SBarry Smith Input Parameter: 3993d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 399463e21af5SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used, 399563e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 39965a3a76d0SJed Brown 3997d763cef2SBarry Smith Level: beginner 3998d763cef2SBarry Smith 39995a3a76d0SJed Brown Notes: 40005a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 40015a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 40025a3a76d0SJed Brown stepped over the final time. 40035a3a76d0SJed Brown 4004d763cef2SBarry Smith .keywords: TS, timestep, solve 4005d763cef2SBarry Smith 400663e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime() 4007d763cef2SBarry Smith @*/ 4008cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 4009d763cef2SBarry Smith { 4010b06615a5SLisandro Dalcin Vec solution; 4011d763cef2SBarry Smith PetscErrorCode ierr; 4012d763cef2SBarry Smith 4013d763cef2SBarry Smith PetscFunctionBegin; 4014d763cef2SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4015f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 4016feed9e9dSBarry Smith 401749354f04SShri 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 */ 4018b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 40190910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 4020b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 4021b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 4022b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 40235a3a76d0SJed Brown } 40240910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 4025bbd56ea5SKarl Rupp } else if (u) { 40260910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 40275a3a76d0SJed Brown } 4028b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 402968bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 4030a6772fa2SLisandro Dalcin 4031a6772fa2SLisandro 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()"); 4032a6772fa2SLisandro 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"); 4033a6772fa2SLisandro Dalcin 4034d763cef2SBarry Smith /* reset time step and iteration counters */ 4035193ac0bcSJed Brown ts->steps = 0; 40365ef26d82SJed Brown ts->ksp_its = 0; 40375ef26d82SJed Brown ts->snes_its = 0; 4038c610991cSLisandro Dalcin ts->num_snes_failures = 0; 4039c610991cSLisandro Dalcin ts->reject = 0; 4040193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 4041193ac0bcSJed Brown 4042ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 4043f05ece33SBarry Smith 4044193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 4045193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 4046a6772fa2SLisandro Dalcin if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 4047cc708dedSBarry Smith ts->solvetime = ts->ptime; 4048a6772fa2SLisandro Dalcin solution = ts->vec_sol; 4049be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 4050db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 4051db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 4052cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 40536427ac75SLisandro Dalcin ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 405428d5b5d6SLisandro Dalcin ts->steprollback = PETSC_FALSE; 405528d5b5d6SLisandro Dalcin ts->steprestart = PETSC_TRUE; 40566427ac75SLisandro Dalcin 4057e1a7a14fSJed Brown while (!ts->reason) { 4058193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 40599687d888SLisandro Dalcin if (!ts->steprollback) { 40609687d888SLisandro Dalcin ierr = TSPreStep(ts);CHKERRQ(ierr); 40619687d888SLisandro Dalcin } 4062193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 4063e783b05fSHong Zhang if (ts->vec_costintegral && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */ 4064b1cb36f3SHong Zhang ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr); 4065b1cb36f3SHong Zhang } 4066e783b05fSHong Zhang ierr = TSEventHandler(ts);CHKERRQ(ierr); /* The right-hand side may be changed due to event. Be careful with Any computation using the RHS information after this point. */ 4067e783b05fSHong Zhang if (!ts->steprollback) { 4068cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 40691eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 4070aeb4809dSShri Abhyankar } 4071193ac0bcSJed Brown } 407263e21af5SBarry Smith ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 40736427ac75SLisandro Dalcin 407449354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 40750910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 4076cc708dedSBarry Smith ts->solvetime = ts->max_time; 4077b06615a5SLisandro Dalcin solution = u; 407863e21af5SBarry Smith ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr); 40790574a7fbSJed Brown } else { 4080ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 4081cc708dedSBarry Smith ts->solvetime = ts->ptime; 4082b06615a5SLisandro Dalcin solution = ts->vec_sol; 40830574a7fbSJed Brown } 4084193ac0bcSJed Brown } 4085d2daff3dSHong Zhang 4086ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 408763e21af5SBarry Smith ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr); 408856f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 4089ef222394SBarry Smith if (ts->adjoint_solve) { 4090ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 40912b0a91c0SBarry Smith } 4092d763cef2SBarry Smith PetscFunctionReturn(0); 4093d763cef2SBarry Smith } 4094d763cef2SBarry Smith 4095d763cef2SBarry Smith #undef __FUNCT__ 4096b1cb36f3SHong Zhang #define __FUNCT__ "TSAdjointCostIntegral" 4097b1cb36f3SHong Zhang /*@ 4098b1cb36f3SHong Zhang TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run. 4099b1cb36f3SHong Zhang 4100b1cb36f3SHong Zhang Collective on TS 4101b1cb36f3SHong Zhang 4102b1cb36f3SHong Zhang Input Arguments: 4103b1cb36f3SHong Zhang . ts - time stepping context 4104b1cb36f3SHong Zhang 4105b1cb36f3SHong Zhang Level: advanced 4106b1cb36f3SHong Zhang 4107b1cb36f3SHong Zhang Notes: 4108b1cb36f3SHong Zhang This function cannot be called until TSAdjointStep() has been completed. 4109b1cb36f3SHong Zhang 4110b1cb36f3SHong Zhang .seealso: TSAdjointSolve(), TSAdjointStep 4111b1cb36f3SHong Zhang @*/ 4112b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts) 4113b1cb36f3SHong Zhang { 4114b1cb36f3SHong Zhang PetscErrorCode ierr; 4115b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4116b1cb36f3SHong 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); 4117b1cb36f3SHong Zhang ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr); 4118b1cb36f3SHong Zhang PetscFunctionReturn(0); 4119b1cb36f3SHong Zhang } 4120b1cb36f3SHong Zhang 4121b1cb36f3SHong Zhang #undef __FUNCT__ 4122c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve" 4123c88f2d44SBarry Smith /*@ 4124c88f2d44SBarry Smith TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 4125c88f2d44SBarry Smith 4126c88f2d44SBarry Smith Collective on TS 4127c88f2d44SBarry Smith 4128c88f2d44SBarry Smith Input Parameter: 41292c39e106SBarry Smith . ts - the TS context obtained from TSCreate() 4130c88f2d44SBarry Smith 4131e87fd094SBarry Smith Options Database: 4132e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions 4133e87fd094SBarry Smith 4134c88f2d44SBarry Smith Level: intermediate 4135c88f2d44SBarry Smith 4136c88f2d44SBarry Smith Notes: 4137c88f2d44SBarry Smith This must be called after a call to TSSolve() that solves the forward problem 4138c88f2d44SBarry Smith 413973b18844SBarry Smith By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time 414073b18844SBarry Smith 4141c88f2d44SBarry Smith .keywords: TS, timestep, solve 4142c88f2d44SBarry Smith 4143b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep() 4144c88f2d44SBarry Smith @*/ 41452c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts) 4146c88f2d44SBarry Smith { 4147c88f2d44SBarry Smith PetscErrorCode ierr; 4148c88f2d44SBarry Smith 4149c88f2d44SBarry Smith PetscFunctionBegin; 4150c88f2d44SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4151c88f2d44SBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 415268bece0bSHong Zhang 4153c88f2d44SBarry Smith /* reset time step and iteration counters */ 4154c88f2d44SBarry Smith ts->steps = 0; 4155c88f2d44SBarry Smith ts->ksp_its = 0; 4156c88f2d44SBarry Smith ts->snes_its = 0; 4157c88f2d44SBarry Smith ts->num_snes_failures = 0; 4158c88f2d44SBarry Smith ts->reject = 0; 4159c88f2d44SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 4160c88f2d44SBarry Smith 416173b18844SBarry Smith if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps; 4162c88f2d44SBarry Smith 416373b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 4164c88f2d44SBarry Smith while (!ts->reason) { 416555c52731SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 41669110b2e7SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 41676427ac75SLisandro Dalcin ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr); 4168616946c1SHong Zhang ierr = TSAdjointStep(ts);CHKERRQ(ierr); 4169b1cb36f3SHong Zhang if (ts->vec_costintegral && !ts->costintegralfwd) { 4170b1cb36f3SHong Zhang ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr); 4171b1cb36f3SHong Zhang } 4172c88f2d44SBarry Smith } 417326656371SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 417426656371SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 4175c88f2d44SBarry Smith ts->solvetime = ts->ptime; 4176e210cd0eSHong Zhang ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr); 4177685405a1SBarry Smith ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr); 4178c88f2d44SBarry Smith PetscFunctionReturn(0); 4179c88f2d44SBarry Smith } 4180c88f2d44SBarry Smith 4181c88f2d44SBarry Smith #undef __FUNCT__ 4182d763cef2SBarry Smith #define __FUNCT__ "TSMonitor" 41837db568b7SBarry Smith /*@C 4184228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 4185228d79bcSJed Brown 4186228d79bcSJed Brown Collective on TS 4187228d79bcSJed Brown 4188228d79bcSJed Brown Input Parameters: 4189228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 4190228d79bcSJed Brown . step - step number that has just completed 4191228d79bcSJed Brown . ptime - model time of the state 41920910c330SBarry Smith - u - state at the current model time 4193228d79bcSJed Brown 4194228d79bcSJed Brown Notes: 41957db568b7SBarry Smith TSMonitor() is typically used automatically within the time stepping implementations. 41967db568b7SBarry Smith Users would almost never call this routine directly. 4197228d79bcSJed Brown 419863e21af5SBarry Smith A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions 419963e21af5SBarry Smith 42007db568b7SBarry Smith Level: developer 4201228d79bcSJed Brown 4202228d79bcSJed Brown .keywords: TS, timestep 4203228d79bcSJed Brown @*/ 42040910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 4205d763cef2SBarry Smith { 4206d6152f81SLisandro Dalcin DM dm; 4207a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 4208d6152f81SLisandro Dalcin PetscErrorCode ierr; 4209d763cef2SBarry Smith 4210d763cef2SBarry Smith PetscFunctionBegin; 4211b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4212b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4213d6152f81SLisandro Dalcin 4214d6152f81SLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 4215d6152f81SLisandro Dalcin ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr); 4216d6152f81SLisandro Dalcin 42175edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 4218d763cef2SBarry Smith for (i=0; i<n; i++) { 42190910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 4220d763cef2SBarry Smith } 42215edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 4222d763cef2SBarry Smith PetscFunctionReturn(0); 4223d763cef2SBarry Smith } 4224d763cef2SBarry Smith 42259110b2e7SHong Zhang #undef __FUNCT__ 42269110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitor" 42277db568b7SBarry Smith /*@C 42289110b2e7SHong Zhang TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet() 42299110b2e7SHong Zhang 42309110b2e7SHong Zhang Collective on TS 42319110b2e7SHong Zhang 42329110b2e7SHong Zhang Input Parameters: 42339110b2e7SHong Zhang + ts - time stepping context obtained from TSCreate() 42349110b2e7SHong Zhang . step - step number that has just completed 42359110b2e7SHong Zhang . ptime - model time of the state 42369110b2e7SHong Zhang . u - state at the current model time 42379110b2e7SHong Zhang . numcost - number of cost functions (dimension of lambda or mu) 42389110b2e7SHong Zhang . lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 42399110b2e7SHong Zhang - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 42409110b2e7SHong Zhang 42419110b2e7SHong Zhang Notes: 42427db568b7SBarry Smith TSAdjointMonitor() is typically used automatically within the time stepping implementations. 42437db568b7SBarry Smith Users would almost never call this routine directly. 42449110b2e7SHong Zhang 42457db568b7SBarry Smith Level: developer 42469110b2e7SHong Zhang 42479110b2e7SHong Zhang .keywords: TS, timestep 42489110b2e7SHong Zhang @*/ 42499110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu) 42509110b2e7SHong Zhang { 42519110b2e7SHong Zhang PetscErrorCode ierr; 42529110b2e7SHong Zhang PetscInt i,n = ts->numberadjointmonitors; 42539110b2e7SHong Zhang 42549110b2e7SHong Zhang PetscFunctionBegin; 42559110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 42569110b2e7SHong Zhang PetscValidHeaderSpecific(u,VEC_CLASSID,4); 42579110b2e7SHong Zhang ierr = VecLockPush(u);CHKERRQ(ierr); 42589110b2e7SHong Zhang for (i=0; i<n; i++) { 42599110b2e7SHong Zhang ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 42609110b2e7SHong Zhang } 42619110b2e7SHong Zhang ierr = VecLockPop(u);CHKERRQ(ierr); 42629110b2e7SHong Zhang PetscFunctionReturn(0); 42639110b2e7SHong Zhang } 42649110b2e7SHong Zhang 4265d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 42664a2ae208SSatish Balay #undef __FUNCT__ 4267a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 4268d763cef2SBarry Smith /*@C 42697db568b7SBarry Smith TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 4270a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 4271d763cef2SBarry Smith 4272d763cef2SBarry Smith Collective on TS 4273d763cef2SBarry Smith 4274d763cef2SBarry Smith Input Parameters: 4275d763cef2SBarry Smith + host - the X display to open, or null for the local machine 4276d763cef2SBarry Smith . label - the title to put in the title bar 42777c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 4278a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 4279a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 4280d763cef2SBarry Smith 4281d763cef2SBarry Smith Output Parameter: 42820b039ecaSBarry Smith . ctx - the context 4283d763cef2SBarry Smith 4284d763cef2SBarry Smith Options Database Key: 42854f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 42867db568b7SBarry Smith . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 42877db568b7SBarry Smith . -ts_monitor_lg_error - monitor the error 42887db568b7SBarry Smith . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 42897db568b7SBarry Smith . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 4290b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 4291d763cef2SBarry Smith 4292d763cef2SBarry Smith Notes: 4293a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 4294d763cef2SBarry Smith 42957db568b7SBarry Smith One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 42967db568b7SBarry Smith 42977db568b7SBarry 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 42987db568b7SBarry 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 42997db568b7SBarry Smith as the first argument. 43007db568b7SBarry Smith 43017db568b7SBarry Smith One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 43027db568b7SBarry Smith 43037db568b7SBarry Smith 4304d763cef2SBarry Smith Level: intermediate 4305d763cef2SBarry Smith 43067db568b7SBarry Smith .keywords: TS, monitor, line graph, residual 4307d763cef2SBarry Smith 43087db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 43097db568b7SBarry Smith TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 43107db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 43117db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 43127db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 43137c922b88SBarry Smith 4314d763cef2SBarry Smith @*/ 4315a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 4316d763cef2SBarry Smith { 43177f52daa2SLisandro Dalcin PetscDraw draw; 4318dfbe8321SBarry Smith PetscErrorCode ierr; 4319d763cef2SBarry Smith 4320d763cef2SBarry Smith PetscFunctionBegin; 4321b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 43227f52daa2SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 43237f52daa2SLisandro Dalcin ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 43247f52daa2SLisandro Dalcin ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 4325287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 43267f52daa2SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 4327a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 4328d763cef2SBarry Smith PetscFunctionReturn(0); 4329d763cef2SBarry Smith } 4330d763cef2SBarry Smith 43314a2ae208SSatish Balay #undef __FUNCT__ 43324f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 4333b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 4334d763cef2SBarry Smith { 43350b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4336c32365f1SBarry Smith PetscReal x = ptime,y; 4337dfbe8321SBarry Smith PetscErrorCode ierr; 4338d763cef2SBarry Smith 4339d763cef2SBarry Smith PetscFunctionBegin; 434063e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */ 4341b06615a5SLisandro Dalcin if (!step) { 4342a9f9c1f6SBarry Smith PetscDrawAxis axis; 4343a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 43446934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr); 4345a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4346a9f9c1f6SBarry Smith } 4347c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 43480b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4349b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 43500b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 43516934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 43523923b477SBarry Smith } 4353d763cef2SBarry Smith PetscFunctionReturn(0); 4354d763cef2SBarry Smith } 4355d763cef2SBarry Smith 43564a2ae208SSatish Balay #undef __FUNCT__ 4357a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 4358d763cef2SBarry Smith /*@C 4359a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 4360a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 4361d763cef2SBarry Smith 43620b039ecaSBarry Smith Collective on TSMonitorLGCtx 4363d763cef2SBarry Smith 4364d763cef2SBarry Smith Input Parameter: 43650b039ecaSBarry Smith . ctx - the monitor context 4366d763cef2SBarry Smith 4367d763cef2SBarry Smith Level: intermediate 4368d763cef2SBarry Smith 4369d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 4370d763cef2SBarry Smith 43714f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 4372d763cef2SBarry Smith @*/ 4373a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 4374d763cef2SBarry Smith { 4375dfbe8321SBarry Smith PetscErrorCode ierr; 4376d763cef2SBarry Smith 4377d763cef2SBarry Smith PetscFunctionBegin; 43787684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 43797684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 43807684fa3eSBarry Smith } 43810b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 438231152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 4383387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 4384387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 4385387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 43860b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 4387d763cef2SBarry Smith PetscFunctionReturn(0); 4388d763cef2SBarry Smith } 4389d763cef2SBarry Smith 43904a2ae208SSatish Balay #undef __FUNCT__ 43914a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 4392d763cef2SBarry Smith /*@ 4393b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4394d763cef2SBarry Smith 4395d763cef2SBarry Smith Not Collective 4396d763cef2SBarry Smith 4397d763cef2SBarry Smith Input Parameter: 4398d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 4399d763cef2SBarry Smith 4400d763cef2SBarry Smith Output Parameter: 440163e21af5SBarry Smith . t - the current time. This time may not corresponds to the final time set with TSSetDuration(), use TSGetSolveTime(). 4402d763cef2SBarry Smith 4403d763cef2SBarry Smith Level: beginner 4404d763cef2SBarry Smith 4405b8123daeSJed Brown Note: 4406b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 44079be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 4408b8123daeSJed Brown 440963e21af5SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep(), TSGetSolveTime() 4410d763cef2SBarry Smith 4411d763cef2SBarry Smith .keywords: TS, get, time 4412d763cef2SBarry Smith @*/ 44137087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 4414d763cef2SBarry Smith { 4415d763cef2SBarry Smith PetscFunctionBegin; 44160700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4417f7cf8827SBarry Smith PetscValidRealPointer(t,2); 4418d763cef2SBarry Smith *t = ts->ptime; 4419d763cef2SBarry Smith PetscFunctionReturn(0); 4420d763cef2SBarry Smith } 4421d763cef2SBarry Smith 44224a2ae208SSatish Balay #undef __FUNCT__ 4423e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime" 4424e5e524a1SHong Zhang /*@ 4425e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4426e5e524a1SHong Zhang 4427e5e524a1SHong Zhang Not Collective 4428e5e524a1SHong Zhang 4429e5e524a1SHong Zhang Input Parameter: 4430e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 4431e5e524a1SHong Zhang 4432e5e524a1SHong Zhang Output Parameter: 4433e5e524a1SHong Zhang . t - the previous time 4434e5e524a1SHong Zhang 4435e5e524a1SHong Zhang Level: beginner 4436e5e524a1SHong Zhang 4437e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 4438e5e524a1SHong Zhang 4439e5e524a1SHong Zhang .keywords: TS, get, time 4440e5e524a1SHong Zhang @*/ 4441e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 4442e5e524a1SHong Zhang { 4443e5e524a1SHong Zhang PetscFunctionBegin; 4444e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4445e5e524a1SHong Zhang PetscValidRealPointer(t,2); 4446e5e524a1SHong Zhang *t = ts->ptime_prev; 4447e5e524a1SHong Zhang PetscFunctionReturn(0); 4448e5e524a1SHong Zhang } 4449e5e524a1SHong Zhang 4450e5e524a1SHong Zhang #undef __FUNCT__ 44516a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 44526a4d4014SLisandro Dalcin /*@ 44536a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 44546a4d4014SLisandro Dalcin 44553f9fe445SBarry Smith Logically Collective on TS 44566a4d4014SLisandro Dalcin 44576a4d4014SLisandro Dalcin Input Parameters: 44586a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 44596a4d4014SLisandro Dalcin - time - the time 44606a4d4014SLisandro Dalcin 44616a4d4014SLisandro Dalcin Level: intermediate 44626a4d4014SLisandro Dalcin 44636a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 44646a4d4014SLisandro Dalcin 44656a4d4014SLisandro Dalcin .keywords: TS, set, time 44666a4d4014SLisandro Dalcin @*/ 44677087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 44686a4d4014SLisandro Dalcin { 44696a4d4014SLisandro Dalcin PetscFunctionBegin; 44700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4471c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 44726a4d4014SLisandro Dalcin ts->ptime = t; 44736a4d4014SLisandro Dalcin PetscFunctionReturn(0); 44746a4d4014SLisandro Dalcin } 44756a4d4014SLisandro Dalcin 44766a4d4014SLisandro Dalcin #undef __FUNCT__ 44774a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 4478d763cef2SBarry Smith /*@C 4479d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4480d763cef2SBarry Smith TS options in the database. 4481d763cef2SBarry Smith 44823f9fe445SBarry Smith Logically Collective on TS 4483d763cef2SBarry Smith 4484d763cef2SBarry Smith Input Parameter: 4485d763cef2SBarry Smith + ts - The TS context 4486d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4487d763cef2SBarry Smith 4488d763cef2SBarry Smith Notes: 4489d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4490d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4491d763cef2SBarry Smith hyphen. 4492d763cef2SBarry Smith 4493d763cef2SBarry Smith Level: advanced 4494d763cef2SBarry Smith 4495d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 4496d763cef2SBarry Smith 4497d763cef2SBarry Smith .seealso: TSSetFromOptions() 4498d763cef2SBarry Smith 4499d763cef2SBarry Smith @*/ 45007087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 4501d763cef2SBarry Smith { 4502dfbe8321SBarry Smith PetscErrorCode ierr; 4503089b2837SJed Brown SNES snes; 4504d763cef2SBarry Smith 4505d763cef2SBarry Smith PetscFunctionBegin; 45060700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4507d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4508089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4509089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4510d763cef2SBarry Smith PetscFunctionReturn(0); 4511d763cef2SBarry Smith } 4512d763cef2SBarry Smith 4513d763cef2SBarry Smith 45144a2ae208SSatish Balay #undef __FUNCT__ 45154a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 4516d763cef2SBarry Smith /*@C 4517d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4518d763cef2SBarry Smith TS options in the database. 4519d763cef2SBarry Smith 45203f9fe445SBarry Smith Logically Collective on TS 4521d763cef2SBarry Smith 4522d763cef2SBarry Smith Input Parameter: 4523d763cef2SBarry Smith + ts - The TS context 4524d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4525d763cef2SBarry Smith 4526d763cef2SBarry Smith Notes: 4527d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4528d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4529d763cef2SBarry Smith hyphen. 4530d763cef2SBarry Smith 4531d763cef2SBarry Smith Level: advanced 4532d763cef2SBarry Smith 4533d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 4534d763cef2SBarry Smith 4535d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 4536d763cef2SBarry Smith 4537d763cef2SBarry Smith @*/ 45387087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4539d763cef2SBarry Smith { 4540dfbe8321SBarry Smith PetscErrorCode ierr; 4541089b2837SJed Brown SNES snes; 4542d763cef2SBarry Smith 4543d763cef2SBarry Smith PetscFunctionBegin; 45440700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4545d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4546089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4547089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4548d763cef2SBarry Smith PetscFunctionReturn(0); 4549d763cef2SBarry Smith } 4550d763cef2SBarry Smith 45514a2ae208SSatish Balay #undef __FUNCT__ 45524a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 4553d763cef2SBarry Smith /*@C 4554d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4555d763cef2SBarry Smith TS options in the database. 4556d763cef2SBarry Smith 4557d763cef2SBarry Smith Not Collective 4558d763cef2SBarry Smith 4559d763cef2SBarry Smith Input Parameter: 4560d763cef2SBarry Smith . ts - The TS context 4561d763cef2SBarry Smith 4562d763cef2SBarry Smith Output Parameter: 4563d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4564d763cef2SBarry Smith 4565d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 4566d763cef2SBarry Smith sufficient length to hold the prefix. 4567d763cef2SBarry Smith 4568d763cef2SBarry Smith Level: intermediate 4569d763cef2SBarry Smith 4570d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 4571d763cef2SBarry Smith 4572d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 4573d763cef2SBarry Smith @*/ 45747087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4575d763cef2SBarry Smith { 4576dfbe8321SBarry Smith PetscErrorCode ierr; 4577d763cef2SBarry Smith 4578d763cef2SBarry Smith PetscFunctionBegin; 45790700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45804482741eSBarry Smith PetscValidPointer(prefix,2); 4581d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4582d763cef2SBarry Smith PetscFunctionReturn(0); 4583d763cef2SBarry Smith } 4584d763cef2SBarry Smith 45854a2ae208SSatish Balay #undef __FUNCT__ 45864a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 4587d763cef2SBarry Smith /*@C 4588d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4589d763cef2SBarry Smith 4590d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4591d763cef2SBarry Smith 4592d763cef2SBarry Smith Input Parameter: 4593d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4594d763cef2SBarry Smith 4595d763cef2SBarry Smith Output Parameters: 4596e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4597e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4598e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4599e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4600d763cef2SBarry Smith 46010298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 4602d763cef2SBarry Smith 4603d763cef2SBarry Smith Level: intermediate 4604d763cef2SBarry Smith 460526d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 4606d763cef2SBarry Smith 4607d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 4608d763cef2SBarry Smith @*/ 4609e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4610d763cef2SBarry Smith { 4611089b2837SJed Brown PetscErrorCode ierr; 4612089b2837SJed Brown SNES snes; 461324989b8cSPeter Brune DM dm; 4614089b2837SJed Brown 4615d763cef2SBarry Smith PetscFunctionBegin; 4616089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4617e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 461824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 461924989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 4620d763cef2SBarry Smith PetscFunctionReturn(0); 4621d763cef2SBarry Smith } 4622d763cef2SBarry Smith 46231713a123SBarry Smith #undef __FUNCT__ 46242eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 46252eca1d9cSJed Brown /*@C 46262eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 46272eca1d9cSJed Brown 46282eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 46292eca1d9cSJed Brown 46302eca1d9cSJed Brown Input Parameter: 46312eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 46322eca1d9cSJed Brown 46332eca1d9cSJed Brown Output Parameters: 4634e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4635e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 46362eca1d9cSJed Brown . f - The function to compute the matrices 46372eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 46382eca1d9cSJed Brown 46390298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 46402eca1d9cSJed Brown 46412eca1d9cSJed Brown Level: advanced 46422eca1d9cSJed Brown 46432eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 46442eca1d9cSJed Brown 46452eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 46462eca1d9cSJed Brown @*/ 4647e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 46482eca1d9cSJed Brown { 4649089b2837SJed Brown PetscErrorCode ierr; 4650089b2837SJed Brown SNES snes; 465124989b8cSPeter Brune DM dm; 46520910c330SBarry Smith 46532eca1d9cSJed Brown PetscFunctionBegin; 4654089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4655f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4656e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 465724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 465824989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 46592eca1d9cSJed Brown PetscFunctionReturn(0); 46602eca1d9cSJed Brown } 46612eca1d9cSJed Brown 46626083293cSBarry Smith 46632eca1d9cSJed Brown #undef __FUNCT__ 466483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 46651713a123SBarry Smith /*@C 466683a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 46671713a123SBarry Smith VecView() for the solution at each timestep 46681713a123SBarry Smith 46691713a123SBarry Smith Collective on TS 46701713a123SBarry Smith 46711713a123SBarry Smith Input Parameters: 46721713a123SBarry Smith + ts - the TS context 46731713a123SBarry Smith . step - current time-step 4674142b95e3SSatish Balay . ptime - current time 46750298fd71SBarry Smith - dummy - either a viewer or NULL 46761713a123SBarry Smith 467799fdda47SBarry Smith Options Database: 467899fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 467999fdda47SBarry Smith 4680387f4636SBarry 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 468199fdda47SBarry Smith will look bad 468299fdda47SBarry Smith 46831713a123SBarry Smith Level: intermediate 46841713a123SBarry Smith 46851713a123SBarry Smith .keywords: TS, vector, monitor, view 46861713a123SBarry Smith 4687a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 46881713a123SBarry Smith @*/ 46890910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 46901713a123SBarry Smith { 4691dfbe8321SBarry Smith PetscErrorCode ierr; 469283a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4693473a3ab2SBarry Smith PetscDraw draw; 46941713a123SBarry Smith 46951713a123SBarry Smith PetscFunctionBegin; 46966083293cSBarry Smith if (!step && ictx->showinitial) { 46976083293cSBarry Smith if (!ictx->initialsolution) { 46980910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 46991713a123SBarry Smith } 47000910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 47016083293cSBarry Smith } 4702b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 47030dcf80beSBarry Smith 47046083293cSBarry Smith if (ictx->showinitial) { 47056083293cSBarry Smith PetscReal pause; 47066083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 47076083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 47086083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 47096083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 47106083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 47116083293cSBarry Smith } 47120910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 4713473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 471451fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 4715473a3ab2SBarry Smith char time[32]; 4716473a3ab2SBarry Smith 4717473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 471885b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4719473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4720473a3ab2SBarry Smith h = yl + .95*(yr - yl); 472151fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4722473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4723473a3ab2SBarry Smith } 4724473a3ab2SBarry Smith 47256083293cSBarry Smith if (ictx->showinitial) { 47266083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 47276083293cSBarry Smith } 47281713a123SBarry Smith PetscFunctionReturn(0); 47291713a123SBarry Smith } 47301713a123SBarry Smith 47312d5ee99bSBarry Smith #undef __FUNCT__ 47329110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorDrawSensi" 47339110b2e7SHong Zhang /*@C 47349110b2e7SHong Zhang TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling 47359110b2e7SHong Zhang VecView() for the sensitivities to initial states at each timestep 47369110b2e7SHong Zhang 47379110b2e7SHong Zhang Collective on TS 47389110b2e7SHong Zhang 47399110b2e7SHong Zhang Input Parameters: 47409110b2e7SHong Zhang + ts - the TS context 47419110b2e7SHong Zhang . step - current time-step 47429110b2e7SHong Zhang . ptime - current time 47439110b2e7SHong Zhang . u - current state 47449110b2e7SHong Zhang . numcost - number of cost functions 47459110b2e7SHong Zhang . lambda - sensitivities to initial conditions 47469110b2e7SHong Zhang . mu - sensitivities to parameters 47479110b2e7SHong Zhang - dummy - either a viewer or NULL 47489110b2e7SHong Zhang 47499110b2e7SHong Zhang Level: intermediate 47509110b2e7SHong Zhang 47519110b2e7SHong Zhang .keywords: TS, vector, adjoint, monitor, view 47529110b2e7SHong Zhang 47539110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView() 47549110b2e7SHong Zhang @*/ 47559110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 47569110b2e7SHong Zhang { 47579110b2e7SHong Zhang PetscErrorCode ierr; 47589110b2e7SHong Zhang TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 47599110b2e7SHong Zhang PetscDraw draw; 4760a0046e2cSSatish Balay PetscReal xl,yl,xr,yr,h; 4761a0046e2cSSatish Balay char time[32]; 47629110b2e7SHong Zhang 47639110b2e7SHong Zhang PetscFunctionBegin; 47649110b2e7SHong Zhang if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 47659110b2e7SHong Zhang 47669110b2e7SHong Zhang ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr); 47679110b2e7SHong Zhang ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 47689110b2e7SHong Zhang ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 47699110b2e7SHong Zhang ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 47709110b2e7SHong Zhang h = yl + .95*(yr - yl); 47719110b2e7SHong Zhang ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 47729110b2e7SHong Zhang ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 47739110b2e7SHong Zhang PetscFunctionReturn(0); 47749110b2e7SHong Zhang } 47759110b2e7SHong Zhang 47769110b2e7SHong Zhang #undef __FUNCT__ 47772d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase" 47782d5ee99bSBarry Smith /*@C 47792d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 47802d5ee99bSBarry Smith 47812d5ee99bSBarry Smith Collective on TS 47822d5ee99bSBarry Smith 47832d5ee99bSBarry Smith Input Parameters: 47842d5ee99bSBarry Smith + ts - the TS context 47852d5ee99bSBarry Smith . step - current time-step 47862d5ee99bSBarry Smith . ptime - current time 47872d5ee99bSBarry Smith - dummy - either a viewer or NULL 47882d5ee99bSBarry Smith 47892d5ee99bSBarry Smith Level: intermediate 47902d5ee99bSBarry Smith 47912d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 47922d5ee99bSBarry Smith 47932d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 47942d5ee99bSBarry Smith @*/ 47952d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 47962d5ee99bSBarry Smith { 47972d5ee99bSBarry Smith PetscErrorCode ierr; 47982d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 47992d5ee99bSBarry Smith PetscDraw draw; 48006934998bSLisandro Dalcin PetscDrawAxis axis; 48012d5ee99bSBarry Smith PetscInt n; 48022d5ee99bSBarry Smith PetscMPIInt size; 48036934998bSLisandro Dalcin PetscReal U0,U1,xl,yl,xr,yr,h; 48042d5ee99bSBarry Smith char time[32]; 48052d5ee99bSBarry Smith const PetscScalar *U; 48062d5ee99bSBarry Smith 48072d5ee99bSBarry Smith PetscFunctionBegin; 48086934998bSLisandro Dalcin ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr); 48096934998bSLisandro Dalcin if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs"); 48102d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 48116934998bSLisandro Dalcin if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 48122d5ee99bSBarry Smith 48132d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 48146934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr); 48156934998bSLisandro Dalcin ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 48166934998bSLisandro Dalcin if (!step) { 48176934998bSLisandro Dalcin ierr = PetscDrawClear(draw);CHKERRQ(ierr); 48186934998bSLisandro Dalcin ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); 48196934998bSLisandro Dalcin } 48202d5ee99bSBarry Smith 48212d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 48226934998bSLisandro Dalcin U0 = PetscRealPart(U[0]); 48236934998bSLisandro Dalcin U1 = PetscRealPart(U[1]); 4824a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 48256934998bSLisandro Dalcin if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0); 48262d5ee99bSBarry Smith 48276934998bSLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 48286934998bSLisandro Dalcin ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr); 48292d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 48304b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 483185b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 48322d5ee99bSBarry Smith h = yl + .95*(yr - yl); 483351fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 48342d5ee99bSBarry Smith } 48356934998bSLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 48362d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 48376934998bSLisandro Dalcin ierr = PetscDrawSave(draw);CHKERRQ(ierr); 48382d5ee99bSBarry Smith PetscFunctionReturn(0); 48392d5ee99bSBarry Smith } 48402d5ee99bSBarry Smith 48411713a123SBarry Smith 48426c699258SBarry Smith #undef __FUNCT__ 484383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 48446083293cSBarry Smith /*@C 484583a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 48466083293cSBarry Smith 48476083293cSBarry Smith Collective on TS 48486083293cSBarry Smith 48496083293cSBarry Smith Input Parameters: 48506083293cSBarry Smith . ctx - the monitor context 48516083293cSBarry Smith 48526083293cSBarry Smith Level: intermediate 48536083293cSBarry Smith 48546083293cSBarry Smith .keywords: TS, vector, monitor, view 48556083293cSBarry Smith 485683a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 48576083293cSBarry Smith @*/ 485883a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 48596083293cSBarry Smith { 48606083293cSBarry Smith PetscErrorCode ierr; 48616083293cSBarry Smith 48626083293cSBarry Smith PetscFunctionBegin; 486383a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 486483a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 486583a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 48666083293cSBarry Smith PetscFunctionReturn(0); 48676083293cSBarry Smith } 48686083293cSBarry Smith 48696083293cSBarry Smith #undef __FUNCT__ 487083a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 48716083293cSBarry Smith /*@C 487283a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 48736083293cSBarry Smith 48746083293cSBarry Smith Collective on TS 48756083293cSBarry Smith 48766083293cSBarry Smith Input Parameter: 48776083293cSBarry Smith . ts - time-step context 48786083293cSBarry Smith 48796083293cSBarry Smith Output Patameter: 48806083293cSBarry Smith . ctx - the monitor context 48816083293cSBarry Smith 488299fdda47SBarry Smith Options Database: 488399fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 488499fdda47SBarry Smith 48856083293cSBarry Smith Level: intermediate 48866083293cSBarry Smith 48876083293cSBarry Smith .keywords: TS, vector, monitor, view 48886083293cSBarry Smith 488983a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 48906083293cSBarry Smith @*/ 489183a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 48926083293cSBarry Smith { 48936083293cSBarry Smith PetscErrorCode ierr; 48946083293cSBarry Smith 48956083293cSBarry Smith PetscFunctionBegin; 4896b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 489783a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4898e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4899bbd56ea5SKarl Rupp 490083a4ac43SBarry Smith (*ctx)->howoften = howoften; 4901473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 4902c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4903473a3ab2SBarry Smith 4904473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 4905c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 49066083293cSBarry Smith PetscFunctionReturn(0); 49076083293cSBarry Smith } 49086083293cSBarry Smith 49096083293cSBarry Smith #undef __FUNCT__ 491083a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 49113a471f94SBarry Smith /*@C 491283a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 49133a471f94SBarry Smith VecView() for the error at each timestep 49143a471f94SBarry Smith 49153a471f94SBarry Smith Collective on TS 49163a471f94SBarry Smith 49173a471f94SBarry Smith Input Parameters: 49183a471f94SBarry Smith + ts - the TS context 49193a471f94SBarry Smith . step - current time-step 49203a471f94SBarry Smith . ptime - current time 49210298fd71SBarry Smith - dummy - either a viewer or NULL 49223a471f94SBarry Smith 49233a471f94SBarry Smith Level: intermediate 49243a471f94SBarry Smith 49253a471f94SBarry Smith .keywords: TS, vector, monitor, view 49263a471f94SBarry Smith 49273a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 49283a471f94SBarry Smith @*/ 49290910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 49303a471f94SBarry Smith { 49313a471f94SBarry Smith PetscErrorCode ierr; 493283a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 493383a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 49343a471f94SBarry Smith Vec work; 49353a471f94SBarry Smith 49363a471f94SBarry Smith PetscFunctionBegin; 4937b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 49380910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 49393a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 49400910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 49413a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 49423a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 49433a471f94SBarry Smith PetscFunctionReturn(0); 49443a471f94SBarry Smith } 49453a471f94SBarry Smith 4946af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 49473a471f94SBarry Smith #undef __FUNCT__ 49486c699258SBarry Smith #define __FUNCT__ "TSSetDM" 49496c699258SBarry Smith /*@ 49502a808120SBarry Smith TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS 49516c699258SBarry Smith 49523f9fe445SBarry Smith Logically Collective on TS and DM 49536c699258SBarry Smith 49546c699258SBarry Smith Input Parameters: 49552a808120SBarry Smith + ts - the ODE integrator object 49562a808120SBarry Smith - dm - the dm, cannot be NULL 49576c699258SBarry Smith 49586c699258SBarry Smith Level: intermediate 49596c699258SBarry Smith 49606c699258SBarry Smith 49616c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 49626c699258SBarry Smith @*/ 49637087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 49646c699258SBarry Smith { 49656c699258SBarry Smith PetscErrorCode ierr; 4966089b2837SJed Brown SNES snes; 4967942e3340SBarry Smith DMTS tsdm; 49686c699258SBarry Smith 49696c699258SBarry Smith PetscFunctionBegin; 49700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 49712a808120SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 497270663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4973942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 49742a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4975942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4976942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 497724989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 497824989b8cSPeter Brune tsdm->originaldm = dm; 497924989b8cSPeter Brune } 498024989b8cSPeter Brune } 49816bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 498224989b8cSPeter Brune } 49836c699258SBarry Smith ts->dm = dm; 4984bbd56ea5SKarl Rupp 4985089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4986089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 49876c699258SBarry Smith PetscFunctionReturn(0); 49886c699258SBarry Smith } 49896c699258SBarry Smith 49906c699258SBarry Smith #undef __FUNCT__ 49916c699258SBarry Smith #define __FUNCT__ "TSGetDM" 49926c699258SBarry Smith /*@ 49936c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 49946c699258SBarry Smith 49953f9fe445SBarry Smith Not Collective 49966c699258SBarry Smith 49976c699258SBarry Smith Input Parameter: 49986c699258SBarry Smith . ts - the preconditioner context 49996c699258SBarry Smith 50006c699258SBarry Smith Output Parameter: 50016c699258SBarry Smith . dm - the dm 50026c699258SBarry Smith 50036c699258SBarry Smith Level: intermediate 50046c699258SBarry Smith 50056c699258SBarry Smith 50066c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 50076c699258SBarry Smith @*/ 50087087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 50096c699258SBarry Smith { 5010496e6a7aSJed Brown PetscErrorCode ierr; 5011496e6a7aSJed Brown 50126c699258SBarry Smith PetscFunctionBegin; 50130700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5014496e6a7aSJed Brown if (!ts->dm) { 5015ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 5016496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 5017496e6a7aSJed Brown } 50186c699258SBarry Smith *dm = ts->dm; 50196c699258SBarry Smith PetscFunctionReturn(0); 50206c699258SBarry Smith } 50211713a123SBarry Smith 50220f5c6efeSJed Brown #undef __FUNCT__ 50230f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 50240f5c6efeSJed Brown /*@ 50250f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 50260f5c6efeSJed Brown 50273f9fe445SBarry Smith Logically Collective on SNES 50280f5c6efeSJed Brown 50290f5c6efeSJed Brown Input Parameter: 5030d42a1c89SJed Brown + snes - nonlinear solver 50310910c330SBarry Smith . U - the current state at which to evaluate the residual 5032d42a1c89SJed Brown - ctx - user context, must be a TS 50330f5c6efeSJed Brown 50340f5c6efeSJed Brown Output Parameter: 50350f5c6efeSJed Brown . F - the nonlinear residual 50360f5c6efeSJed Brown 50370f5c6efeSJed Brown Notes: 50380f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 50390f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 50400f5c6efeSJed Brown 50410f5c6efeSJed Brown Level: advanced 50420f5c6efeSJed Brown 50430f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 50440f5c6efeSJed Brown @*/ 50450910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 50460f5c6efeSJed Brown { 50470f5c6efeSJed Brown TS ts = (TS)ctx; 50480f5c6efeSJed Brown PetscErrorCode ierr; 50490f5c6efeSJed Brown 50500f5c6efeSJed Brown PetscFunctionBegin; 50510f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 50520910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 50530f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 50540f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 50550910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 50560f5c6efeSJed Brown PetscFunctionReturn(0); 50570f5c6efeSJed Brown } 50580f5c6efeSJed Brown 50590f5c6efeSJed Brown #undef __FUNCT__ 50600f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 50610f5c6efeSJed Brown /*@ 50620f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 50630f5c6efeSJed Brown 50640f5c6efeSJed Brown Collective on SNES 50650f5c6efeSJed Brown 50660f5c6efeSJed Brown Input Parameter: 50670f5c6efeSJed Brown + snes - nonlinear solver 50680910c330SBarry Smith . U - the current state at which to evaluate the residual 50690f5c6efeSJed Brown - ctx - user context, must be a TS 50700f5c6efeSJed Brown 50710f5c6efeSJed Brown Output Parameter: 50720f5c6efeSJed Brown + A - the Jacobian 50730f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 50740f5c6efeSJed Brown - flag - indicates any structure change in the matrix 50750f5c6efeSJed Brown 50760f5c6efeSJed Brown Notes: 50770f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 50780f5c6efeSJed Brown 50790f5c6efeSJed Brown Level: developer 50800f5c6efeSJed Brown 50810f5c6efeSJed Brown .seealso: SNESSetJacobian() 50820f5c6efeSJed Brown @*/ 5083d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 50840f5c6efeSJed Brown { 50850f5c6efeSJed Brown TS ts = (TS)ctx; 50860f5c6efeSJed Brown PetscErrorCode ierr; 50870f5c6efeSJed Brown 50880f5c6efeSJed Brown PetscFunctionBegin; 50890f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 50900910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 50910f5c6efeSJed Brown PetscValidPointer(A,3); 509294ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 50930f5c6efeSJed Brown PetscValidPointer(B,4); 509494ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 50950f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 5096d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 50970f5c6efeSJed Brown PetscFunctionReturn(0); 50980f5c6efeSJed Brown } 5099325fc9f4SBarry Smith 51000e4ef248SJed Brown #undef __FUNCT__ 51010e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 51020e4ef248SJed Brown /*@C 51039ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 51040e4ef248SJed Brown 51050e4ef248SJed Brown Collective on TS 51060e4ef248SJed Brown 51070e4ef248SJed Brown Input Arguments: 51080e4ef248SJed Brown + ts - time stepping context 51090e4ef248SJed Brown . t - time at which to evaluate 51100910c330SBarry Smith . U - state at which to evaluate 51110e4ef248SJed Brown - ctx - context 51120e4ef248SJed Brown 51130e4ef248SJed Brown Output Arguments: 51140e4ef248SJed Brown . F - right hand side 51150e4ef248SJed Brown 51160e4ef248SJed Brown Level: intermediate 51170e4ef248SJed Brown 51180e4ef248SJed Brown Notes: 51190e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 51200e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 51210e4ef248SJed Brown 51220e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 51230e4ef248SJed Brown @*/ 51240910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 51250e4ef248SJed Brown { 51260e4ef248SJed Brown PetscErrorCode ierr; 51270e4ef248SJed Brown Mat Arhs,Brhs; 51280e4ef248SJed Brown 51290e4ef248SJed Brown PetscFunctionBegin; 51300e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 5131d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 51320910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 51330e4ef248SJed Brown PetscFunctionReturn(0); 51340e4ef248SJed Brown } 51350e4ef248SJed Brown 51360e4ef248SJed Brown #undef __FUNCT__ 51370e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 51380e4ef248SJed Brown /*@C 51390e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 51400e4ef248SJed Brown 51410e4ef248SJed Brown Collective on TS 51420e4ef248SJed Brown 51430e4ef248SJed Brown Input Arguments: 51440e4ef248SJed Brown + ts - time stepping context 51450e4ef248SJed Brown . t - time at which to evaluate 51460910c330SBarry Smith . U - state at which to evaluate 51470e4ef248SJed Brown - ctx - context 51480e4ef248SJed Brown 51490e4ef248SJed Brown Output Arguments: 51500e4ef248SJed Brown + A - pointer to operator 51510e4ef248SJed Brown . B - pointer to preconditioning matrix 51520e4ef248SJed Brown - flg - matrix structure flag 51530e4ef248SJed Brown 51540e4ef248SJed Brown Level: intermediate 51550e4ef248SJed Brown 51560e4ef248SJed Brown Notes: 51570e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 51580e4ef248SJed Brown 51590e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 51600e4ef248SJed Brown @*/ 5161d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 51620e4ef248SJed Brown { 51630e4ef248SJed Brown PetscFunctionBegin; 51640e4ef248SJed Brown PetscFunctionReturn(0); 51650e4ef248SJed Brown } 51660e4ef248SJed Brown 51670026cea9SSean Farley #undef __FUNCT__ 51680026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 51690026cea9SSean Farley /*@C 51700026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 51710026cea9SSean Farley 51720026cea9SSean Farley Collective on TS 51730026cea9SSean Farley 51740026cea9SSean Farley Input Arguments: 51750026cea9SSean Farley + ts - time stepping context 51760026cea9SSean Farley . t - time at which to evaluate 51770910c330SBarry Smith . U - state at which to evaluate 51780910c330SBarry Smith . Udot - time derivative of state vector 51790026cea9SSean Farley - ctx - context 51800026cea9SSean Farley 51810026cea9SSean Farley Output Arguments: 51820026cea9SSean Farley . F - left hand side 51830026cea9SSean Farley 51840026cea9SSean Farley Level: intermediate 51850026cea9SSean Farley 51860026cea9SSean Farley Notes: 51870910c330SBarry 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 51880026cea9SSean Farley user is required to write their own TSComputeIFunction. 51890026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 51900026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 51910026cea9SSean Farley 51929ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 51939ae8fd06SBarry Smith 51949ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 51950026cea9SSean Farley @*/ 51960910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 51970026cea9SSean Farley { 51980026cea9SSean Farley PetscErrorCode ierr; 51990026cea9SSean Farley Mat A,B; 52000026cea9SSean Farley 52010026cea9SSean Farley PetscFunctionBegin; 52020298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 5203d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 52040910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 52050026cea9SSean Farley PetscFunctionReturn(0); 52060026cea9SSean Farley } 52070026cea9SSean Farley 52080026cea9SSean Farley #undef __FUNCT__ 52090026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 52100026cea9SSean Farley /*@C 5211b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 52120026cea9SSean Farley 52130026cea9SSean Farley Collective on TS 52140026cea9SSean Farley 52150026cea9SSean Farley Input Arguments: 52160026cea9SSean Farley + ts - time stepping context 52170026cea9SSean Farley . t - time at which to evaluate 52180910c330SBarry Smith . U - state at which to evaluate 52190910c330SBarry Smith . Udot - time derivative of state vector 52200026cea9SSean Farley . shift - shift to apply 52210026cea9SSean Farley - ctx - context 52220026cea9SSean Farley 52230026cea9SSean Farley Output Arguments: 52240026cea9SSean Farley + A - pointer to operator 52250026cea9SSean Farley . B - pointer to preconditioning matrix 52260026cea9SSean Farley - flg - matrix structure flag 52270026cea9SSean Farley 5228b41af12eSJed Brown Level: advanced 52290026cea9SSean Farley 52300026cea9SSean Farley Notes: 52310026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 52320026cea9SSean Farley 5233b41af12eSJed Brown It is only appropriate for problems of the form 5234b41af12eSJed Brown 5235b41af12eSJed Brown $ M Udot = F(U,t) 5236b41af12eSJed Brown 5237b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 5238b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 5239b41af12eSJed Brown an implicit operator of the form 5240b41af12eSJed Brown 5241b41af12eSJed Brown $ shift*M + J 5242b41af12eSJed Brown 5243b41af12eSJed 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 5244b41af12eSJed Brown a copy of M or reassemble it when requested. 5245b41af12eSJed Brown 52460026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 52470026cea9SSean Farley @*/ 5248d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 52490026cea9SSean Farley { 5250b41af12eSJed Brown PetscErrorCode ierr; 5251b41af12eSJed Brown 52520026cea9SSean Farley PetscFunctionBegin; 525394ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 5254b41af12eSJed Brown ts->ijacobian.shift = shift; 52550026cea9SSean Farley PetscFunctionReturn(0); 52560026cea9SSean Farley } 5257b41af12eSJed Brown 5258e817cc15SEmil Constantinescu #undef __FUNCT__ 5259e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType" 5260e817cc15SEmil Constantinescu /*@ 5261e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 5262e817cc15SEmil Constantinescu 5263e817cc15SEmil Constantinescu Not Collective 5264e817cc15SEmil Constantinescu 5265e817cc15SEmil Constantinescu Input Parameter: 5266e817cc15SEmil Constantinescu . ts - the TS context 5267e817cc15SEmil Constantinescu 5268e817cc15SEmil Constantinescu Output Parameter: 52694e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 5270e817cc15SEmil Constantinescu 5271e817cc15SEmil Constantinescu Level: beginner 5272e817cc15SEmil Constantinescu 5273e817cc15SEmil Constantinescu .keywords: TS, equation type 5274e817cc15SEmil Constantinescu 5275e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 5276e817cc15SEmil Constantinescu @*/ 5277e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 5278e817cc15SEmil Constantinescu { 5279e817cc15SEmil Constantinescu PetscFunctionBegin; 5280e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5281e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 5282e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 5283e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5284e817cc15SEmil Constantinescu } 5285e817cc15SEmil Constantinescu 5286e817cc15SEmil Constantinescu #undef __FUNCT__ 5287e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType" 5288e817cc15SEmil Constantinescu /*@ 5289e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 5290e817cc15SEmil Constantinescu 5291e817cc15SEmil Constantinescu Not Collective 5292e817cc15SEmil Constantinescu 5293e817cc15SEmil Constantinescu Input Parameter: 5294e817cc15SEmil Constantinescu + ts - the TS context 52951297b224SEmil Constantinescu - equation_type - see TSEquationType 5296e817cc15SEmil Constantinescu 5297e817cc15SEmil Constantinescu Level: advanced 5298e817cc15SEmil Constantinescu 5299e817cc15SEmil Constantinescu .keywords: TS, equation type 5300e817cc15SEmil Constantinescu 5301e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 5302e817cc15SEmil Constantinescu @*/ 5303e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 5304e817cc15SEmil Constantinescu { 5305e817cc15SEmil Constantinescu PetscFunctionBegin; 5306e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5307e817cc15SEmil Constantinescu ts->equation_type = equation_type; 5308e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5309e817cc15SEmil Constantinescu } 53100026cea9SSean Farley 53114af1b03aSJed Brown #undef __FUNCT__ 53124af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 53134af1b03aSJed Brown /*@ 53144af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 53154af1b03aSJed Brown 53164af1b03aSJed Brown Not Collective 53174af1b03aSJed Brown 53184af1b03aSJed Brown Input Parameter: 53194af1b03aSJed Brown . ts - the TS context 53204af1b03aSJed Brown 53214af1b03aSJed Brown Output Parameter: 53224af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 53234af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 53244af1b03aSJed Brown 5325487e0bb9SJed Brown Level: beginner 53264af1b03aSJed Brown 5327cd652676SJed Brown Notes: 5328cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 53294af1b03aSJed Brown 53304af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 53314af1b03aSJed Brown 53324af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 53334af1b03aSJed Brown @*/ 53344af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 53354af1b03aSJed Brown { 53364af1b03aSJed Brown PetscFunctionBegin; 53374af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53384af1b03aSJed Brown PetscValidPointer(reason,2); 53394af1b03aSJed Brown *reason = ts->reason; 53404af1b03aSJed Brown PetscFunctionReturn(0); 53414af1b03aSJed Brown } 53424af1b03aSJed Brown 5343fb1732b5SBarry Smith #undef __FUNCT__ 5344d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 5345d6ad946cSShri Abhyankar /*@ 5346d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 5347d6ad946cSShri Abhyankar 5348d6ad946cSShri Abhyankar Not Collective 5349d6ad946cSShri Abhyankar 5350d6ad946cSShri Abhyankar Input Parameter: 5351d6ad946cSShri Abhyankar + ts - the TS context 5352d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 5353d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 5354d6ad946cSShri Abhyankar 5355f5abba47SShri Abhyankar Level: advanced 5356d6ad946cSShri Abhyankar 5357d6ad946cSShri Abhyankar Notes: 5358d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 5359d6ad946cSShri Abhyankar 5360d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 5361d6ad946cSShri Abhyankar 5362d6ad946cSShri Abhyankar .seealso: TSConvergedReason 5363d6ad946cSShri Abhyankar @*/ 5364d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 5365d6ad946cSShri Abhyankar { 5366d6ad946cSShri Abhyankar PetscFunctionBegin; 5367d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5368d6ad946cSShri Abhyankar ts->reason = reason; 5369d6ad946cSShri Abhyankar PetscFunctionReturn(0); 5370d6ad946cSShri Abhyankar } 5371d6ad946cSShri Abhyankar 5372d6ad946cSShri Abhyankar #undef __FUNCT__ 5373cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 5374cc708dedSBarry Smith /*@ 5375cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 5376cc708dedSBarry Smith 5377cc708dedSBarry Smith Not Collective 5378cc708dedSBarry Smith 5379cc708dedSBarry Smith Input Parameter: 5380cc708dedSBarry Smith . ts - the TS context 5381cc708dedSBarry Smith 5382cc708dedSBarry Smith Output Parameter: 538363e21af5SBarry Smith . ftime - the final time. This time corresponds to the final time set with TSSetDuration() 5384cc708dedSBarry Smith 5385487e0bb9SJed Brown Level: beginner 5386cc708dedSBarry Smith 5387cc708dedSBarry Smith Notes: 5388cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 5389cc708dedSBarry Smith 5390cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 5391cc708dedSBarry Smith 5392cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 5393cc708dedSBarry Smith @*/ 5394cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 5395cc708dedSBarry Smith { 5396cc708dedSBarry Smith PetscFunctionBegin; 5397cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5398cc708dedSBarry Smith PetscValidPointer(ftime,2); 5399cc708dedSBarry Smith *ftime = ts->solvetime; 5400cc708dedSBarry Smith PetscFunctionReturn(0); 5401cc708dedSBarry Smith } 5402cc708dedSBarry Smith 5403cc708dedSBarry Smith #undef __FUNCT__ 54042c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps" 54052c18e0fdSBarry Smith /*@ 54062c18e0fdSBarry Smith TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 54072c18e0fdSBarry Smith 54082c18e0fdSBarry Smith Not Collective 54092c18e0fdSBarry Smith 54102c18e0fdSBarry Smith Input Parameter: 54112c18e0fdSBarry Smith . ts - the TS context 54122c18e0fdSBarry Smith 54132c18e0fdSBarry Smith Output Parameter: 54142c18e0fdSBarry Smith . steps - the number of steps 54152c18e0fdSBarry Smith 54162c18e0fdSBarry Smith Level: beginner 54172c18e0fdSBarry Smith 54182c18e0fdSBarry Smith Notes: 54192c18e0fdSBarry Smith Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 54202c18e0fdSBarry Smith 54212c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test 54222c18e0fdSBarry Smith 54232c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 54242c18e0fdSBarry Smith @*/ 54252c18e0fdSBarry Smith PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 54262c18e0fdSBarry Smith { 54272c18e0fdSBarry Smith PetscFunctionBegin; 54282c18e0fdSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 54292c18e0fdSBarry Smith PetscValidPointer(steps,2); 54302c18e0fdSBarry Smith *steps = ts->total_steps; 54312c18e0fdSBarry Smith PetscFunctionReturn(0); 54322c18e0fdSBarry Smith } 54332c18e0fdSBarry Smith 54342c18e0fdSBarry Smith #undef __FUNCT__ 54355ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 54369f67acb7SJed Brown /*@ 54375ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 54389f67acb7SJed Brown used by the time integrator. 54399f67acb7SJed Brown 54409f67acb7SJed Brown Not Collective 54419f67acb7SJed Brown 54429f67acb7SJed Brown Input Parameter: 54439f67acb7SJed Brown . ts - TS context 54449f67acb7SJed Brown 54459f67acb7SJed Brown Output Parameter: 54469f67acb7SJed Brown . nits - number of nonlinear iterations 54479f67acb7SJed Brown 54489f67acb7SJed Brown Notes: 54499f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 54509f67acb7SJed Brown 54519f67acb7SJed Brown Level: intermediate 54529f67acb7SJed Brown 54539f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 54549f67acb7SJed Brown 54555ef26d82SJed Brown .seealso: TSGetKSPIterations() 54569f67acb7SJed Brown @*/ 54575ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 54589f67acb7SJed Brown { 54599f67acb7SJed Brown PetscFunctionBegin; 54609f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 54619f67acb7SJed Brown PetscValidIntPointer(nits,2); 54625ef26d82SJed Brown *nits = ts->snes_its; 54639f67acb7SJed Brown PetscFunctionReturn(0); 54649f67acb7SJed Brown } 54659f67acb7SJed Brown 54669f67acb7SJed Brown #undef __FUNCT__ 54675ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 54689f67acb7SJed Brown /*@ 54695ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 54709f67acb7SJed Brown used by the time integrator. 54719f67acb7SJed Brown 54729f67acb7SJed Brown Not Collective 54739f67acb7SJed Brown 54749f67acb7SJed Brown Input Parameter: 54759f67acb7SJed Brown . ts - TS context 54769f67acb7SJed Brown 54779f67acb7SJed Brown Output Parameter: 54789f67acb7SJed Brown . lits - number of linear iterations 54799f67acb7SJed Brown 54809f67acb7SJed Brown Notes: 54819f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 54829f67acb7SJed Brown 54839f67acb7SJed Brown Level: intermediate 54849f67acb7SJed Brown 54859f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 54869f67acb7SJed Brown 54875ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 54889f67acb7SJed Brown @*/ 54895ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 54909f67acb7SJed Brown { 54919f67acb7SJed Brown PetscFunctionBegin; 54929f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 54939f67acb7SJed Brown PetscValidIntPointer(lits,2); 54945ef26d82SJed Brown *lits = ts->ksp_its; 54959f67acb7SJed Brown PetscFunctionReturn(0); 54969f67acb7SJed Brown } 54979f67acb7SJed Brown 54989f67acb7SJed Brown #undef __FUNCT__ 5499cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 5500cef5090cSJed Brown /*@ 5501cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 5502cef5090cSJed Brown 5503cef5090cSJed Brown Not Collective 5504cef5090cSJed Brown 5505cef5090cSJed Brown Input Parameter: 5506cef5090cSJed Brown . ts - TS context 5507cef5090cSJed Brown 5508cef5090cSJed Brown Output Parameter: 5509cef5090cSJed Brown . rejects - number of steps rejected 5510cef5090cSJed Brown 5511cef5090cSJed Brown Notes: 5512cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5513cef5090cSJed Brown 5514cef5090cSJed Brown Level: intermediate 5515cef5090cSJed Brown 5516cef5090cSJed Brown .keywords: TS, get, number 5517cef5090cSJed Brown 55185ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 5519cef5090cSJed Brown @*/ 5520cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 5521cef5090cSJed Brown { 5522cef5090cSJed Brown PetscFunctionBegin; 5523cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5524cef5090cSJed Brown PetscValidIntPointer(rejects,2); 5525cef5090cSJed Brown *rejects = ts->reject; 5526cef5090cSJed Brown PetscFunctionReturn(0); 5527cef5090cSJed Brown } 5528cef5090cSJed Brown 5529cef5090cSJed Brown #undef __FUNCT__ 5530cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 5531cef5090cSJed Brown /*@ 5532cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 5533cef5090cSJed Brown 5534cef5090cSJed Brown Not Collective 5535cef5090cSJed Brown 5536cef5090cSJed Brown Input Parameter: 5537cef5090cSJed Brown . ts - TS context 5538cef5090cSJed Brown 5539cef5090cSJed Brown Output Parameter: 5540cef5090cSJed Brown . fails - number of failed nonlinear solves 5541cef5090cSJed Brown 5542cef5090cSJed Brown Notes: 5543cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5544cef5090cSJed Brown 5545cef5090cSJed Brown Level: intermediate 5546cef5090cSJed Brown 5547cef5090cSJed Brown .keywords: TS, get, number 5548cef5090cSJed Brown 55495ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 5550cef5090cSJed Brown @*/ 5551cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 5552cef5090cSJed Brown { 5553cef5090cSJed Brown PetscFunctionBegin; 5554cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5555cef5090cSJed Brown PetscValidIntPointer(fails,2); 5556cef5090cSJed Brown *fails = ts->num_snes_failures; 5557cef5090cSJed Brown PetscFunctionReturn(0); 5558cef5090cSJed Brown } 5559cef5090cSJed Brown 5560cef5090cSJed Brown #undef __FUNCT__ 5561cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 5562cef5090cSJed Brown /*@ 5563cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 5564cef5090cSJed Brown 5565cef5090cSJed Brown Not Collective 5566cef5090cSJed Brown 5567cef5090cSJed Brown Input Parameter: 5568cef5090cSJed Brown + ts - TS context 5569cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 5570cef5090cSJed Brown 5571cef5090cSJed Brown Notes: 5572cef5090cSJed Brown The counter is reset to zero for each step 5573cef5090cSJed Brown 5574cef5090cSJed Brown Options Database Key: 5575cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 5576cef5090cSJed Brown 5577cef5090cSJed Brown Level: intermediate 5578cef5090cSJed Brown 5579cef5090cSJed Brown .keywords: TS, set, maximum, number 5580cef5090cSJed Brown 55815ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5582cef5090cSJed Brown @*/ 5583cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 5584cef5090cSJed Brown { 5585cef5090cSJed Brown PetscFunctionBegin; 5586cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5587cef5090cSJed Brown ts->max_reject = rejects; 5588cef5090cSJed Brown PetscFunctionReturn(0); 5589cef5090cSJed Brown } 5590cef5090cSJed Brown 5591cef5090cSJed Brown #undef __FUNCT__ 5592cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 5593cef5090cSJed Brown /*@ 5594cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 5595cef5090cSJed Brown 5596cef5090cSJed Brown Not Collective 5597cef5090cSJed Brown 5598cef5090cSJed Brown Input Parameter: 5599cef5090cSJed Brown + ts - TS context 5600cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 5601cef5090cSJed Brown 5602cef5090cSJed Brown Notes: 5603cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 5604cef5090cSJed Brown 5605cef5090cSJed Brown Options Database Key: 5606cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 5607cef5090cSJed Brown 5608cef5090cSJed Brown Level: intermediate 5609cef5090cSJed Brown 5610cef5090cSJed Brown .keywords: TS, set, maximum, number 5611cef5090cSJed Brown 56125ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 5613cef5090cSJed Brown @*/ 5614cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 5615cef5090cSJed Brown { 5616cef5090cSJed Brown PetscFunctionBegin; 5617cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5618cef5090cSJed Brown ts->max_snes_failures = fails; 5619cef5090cSJed Brown PetscFunctionReturn(0); 5620cef5090cSJed Brown } 5621cef5090cSJed Brown 5622cef5090cSJed Brown #undef __FUNCT__ 56234e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails" 5624cef5090cSJed Brown /*@ 5625cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 5626cef5090cSJed Brown 5627cef5090cSJed Brown Not Collective 5628cef5090cSJed Brown 5629cef5090cSJed Brown Input Parameter: 5630cef5090cSJed Brown + ts - TS context 5631cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 5632cef5090cSJed Brown 5633cef5090cSJed Brown Options Database Key: 5634cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 5635cef5090cSJed Brown 5636cef5090cSJed Brown Level: intermediate 5637cef5090cSJed Brown 5638cef5090cSJed Brown .keywords: TS, set, error 5639cef5090cSJed Brown 56405ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5641cef5090cSJed Brown @*/ 5642cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 5643cef5090cSJed Brown { 5644cef5090cSJed Brown PetscFunctionBegin; 5645cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5646cef5090cSJed Brown ts->errorifstepfailed = err; 5647cef5090cSJed Brown PetscFunctionReturn(0); 5648cef5090cSJed Brown } 5649cef5090cSJed Brown 5650cef5090cSJed Brown #undef __FUNCT__ 5651fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSolution" 5652fb1732b5SBarry Smith /*@C 5653fde5950dSBarry 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 5654fb1732b5SBarry Smith 5655fb1732b5SBarry Smith Collective on TS 5656fb1732b5SBarry Smith 5657fb1732b5SBarry Smith Input Parameters: 5658fb1732b5SBarry Smith + ts - the TS context 5659fb1732b5SBarry Smith . step - current time-step 5660fb1732b5SBarry Smith . ptime - current time 56610910c330SBarry Smith . u - current state 5662721cd6eeSBarry Smith - vf - viewer and its format 5663fb1732b5SBarry Smith 5664fb1732b5SBarry Smith Level: intermediate 5665fb1732b5SBarry Smith 5666fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 5667fb1732b5SBarry Smith 5668fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5669fb1732b5SBarry Smith @*/ 5670721cd6eeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf) 5671fb1732b5SBarry Smith { 5672fb1732b5SBarry Smith PetscErrorCode ierr; 5673fb1732b5SBarry Smith 5674fb1732b5SBarry Smith PetscFunctionBegin; 5675721cd6eeSBarry Smith ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr); 5676721cd6eeSBarry Smith ierr = VecView(u,vf->viewer);CHKERRQ(ierr); 5677721cd6eeSBarry Smith ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr); 5678ed81e22dSJed Brown PetscFunctionReturn(0); 5679ed81e22dSJed Brown } 5680ed81e22dSJed Brown 5681ed81e22dSJed Brown #undef __FUNCT__ 5682ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 5683ed81e22dSJed Brown /*@C 5684ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5685ed81e22dSJed Brown 5686ed81e22dSJed Brown Collective on TS 5687ed81e22dSJed Brown 5688ed81e22dSJed Brown Input Parameters: 5689ed81e22dSJed Brown + ts - the TS context 5690ed81e22dSJed Brown . step - current time-step 5691ed81e22dSJed Brown . ptime - current time 56920910c330SBarry Smith . u - current state 5693ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5694ed81e22dSJed Brown 5695ed81e22dSJed Brown Level: intermediate 5696ed81e22dSJed Brown 5697ed81e22dSJed Brown Notes: 5698ed81e22dSJed 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. 5699ed81e22dSJed Brown These are named according to the file name template. 5700ed81e22dSJed Brown 5701ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5702ed81e22dSJed Brown 5703ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5704ed81e22dSJed Brown 5705ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5706ed81e22dSJed Brown @*/ 57070910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5708ed81e22dSJed Brown { 5709ed81e22dSJed Brown PetscErrorCode ierr; 5710ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 5711ed81e22dSJed Brown PetscViewer viewer; 5712ed81e22dSJed Brown 5713ed81e22dSJed Brown PetscFunctionBegin; 571463e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 57158caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5716ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 57170910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 5718ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5719ed81e22dSJed Brown PetscFunctionReturn(0); 5720ed81e22dSJed Brown } 5721ed81e22dSJed Brown 5722ed81e22dSJed Brown #undef __FUNCT__ 5723ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 5724ed81e22dSJed Brown /*@C 5725ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5726ed81e22dSJed Brown 5727ed81e22dSJed Brown Collective on TS 5728ed81e22dSJed Brown 5729ed81e22dSJed Brown Input Parameters: 5730ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5731ed81e22dSJed Brown 5732ed81e22dSJed Brown Level: intermediate 5733ed81e22dSJed Brown 5734ed81e22dSJed Brown Note: 5735ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5736ed81e22dSJed Brown 5737ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5738ed81e22dSJed Brown 5739ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5740ed81e22dSJed Brown @*/ 5741ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5742ed81e22dSJed Brown { 5743ed81e22dSJed Brown PetscErrorCode ierr; 5744ed81e22dSJed Brown 5745ed81e22dSJed Brown PetscFunctionBegin; 5746ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5747fb1732b5SBarry Smith PetscFunctionReturn(0); 5748fb1732b5SBarry Smith } 5749fb1732b5SBarry Smith 575084df9cb4SJed Brown #undef __FUNCT__ 5751552698daSJed Brown #define __FUNCT__ "TSGetAdapt" 575284df9cb4SJed Brown /*@ 5753552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 575484df9cb4SJed Brown 5755ed81e22dSJed Brown Collective on TS if controller has not been created yet 575684df9cb4SJed Brown 575784df9cb4SJed Brown Input Arguments: 5758ed81e22dSJed Brown . ts - time stepping context 575984df9cb4SJed Brown 576084df9cb4SJed Brown Output Arguments: 5761ed81e22dSJed Brown . adapt - adaptive controller 576284df9cb4SJed Brown 576384df9cb4SJed Brown Level: intermediate 576484df9cb4SJed Brown 5765ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 576684df9cb4SJed Brown @*/ 5767552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 576884df9cb4SJed Brown { 576984df9cb4SJed Brown PetscErrorCode ierr; 577084df9cb4SJed Brown 577184df9cb4SJed Brown PetscFunctionBegin; 577284df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5773bec58848SLisandro Dalcin PetscValidPointer(adapt,2); 577484df9cb4SJed Brown if (!ts->adapt) { 5775ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 57763bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 57771c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 577884df9cb4SJed Brown } 5779bec58848SLisandro Dalcin *adapt = ts->adapt; 578084df9cb4SJed Brown PetscFunctionReturn(0); 578184df9cb4SJed Brown } 5782d6ebe24aSShri Abhyankar 5783d6ebe24aSShri Abhyankar #undef __FUNCT__ 57841c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 57851c3436cfSJed Brown /*@ 57861c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 57871c3436cfSJed Brown 57881c3436cfSJed Brown Logically Collective 57891c3436cfSJed Brown 57901c3436cfSJed Brown Input Arguments: 57911c3436cfSJed Brown + ts - time integration context 57921c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 57930298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 57941c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 57950298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 57961c3436cfSJed Brown 5797a3cdaa26SBarry Smith Options Database keys: 5798a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5799a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5800a3cdaa26SBarry Smith 58013ff766beSShri Abhyankar Notes: 58023ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 58033ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 58043ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 58053ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 58063ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 58073ff766beSShri Abhyankar differential variables. 58083ff766beSShri Abhyankar 58091c3436cfSJed Brown Level: beginner 58101c3436cfSJed Brown 5811c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 58121c3436cfSJed Brown @*/ 58131c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 58141c3436cfSJed Brown { 58151c3436cfSJed Brown PetscErrorCode ierr; 58161c3436cfSJed Brown 58171c3436cfSJed Brown PetscFunctionBegin; 5818c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 58191c3436cfSJed Brown if (vatol) { 58201c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 58211c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 58221c3436cfSJed Brown ts->vatol = vatol; 58231c3436cfSJed Brown } 5824c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 58251c3436cfSJed Brown if (vrtol) { 58261c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 58271c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 58281c3436cfSJed Brown ts->vrtol = vrtol; 58291c3436cfSJed Brown } 58301c3436cfSJed Brown PetscFunctionReturn(0); 58311c3436cfSJed Brown } 58321c3436cfSJed Brown 58331c3436cfSJed Brown #undef __FUNCT__ 5834c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 5835c5033834SJed Brown /*@ 5836c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5837c5033834SJed Brown 5838c5033834SJed Brown Logically Collective 5839c5033834SJed Brown 5840c5033834SJed Brown Input Arguments: 5841c5033834SJed Brown . ts - time integration context 5842c5033834SJed Brown 5843c5033834SJed Brown Output Arguments: 58440298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 58450298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 58460298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 58470298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 5848c5033834SJed Brown 5849c5033834SJed Brown Level: beginner 5850c5033834SJed Brown 5851c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5852c5033834SJed Brown @*/ 5853c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5854c5033834SJed Brown { 5855c5033834SJed Brown PetscFunctionBegin; 5856c5033834SJed Brown if (atol) *atol = ts->atol; 5857c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5858c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5859c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 5860c5033834SJed Brown PetscFunctionReturn(0); 5861c5033834SJed Brown } 5862c5033834SJed Brown 5863c5033834SJed Brown #undef __FUNCT__ 58649c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2" 58659c6b16b5SShri Abhyankar /*@ 5866a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 58679c6b16b5SShri Abhyankar 58689c6b16b5SShri Abhyankar Collective on TS 58699c6b16b5SShri Abhyankar 58709c6b16b5SShri Abhyankar Input Arguments: 58719c6b16b5SShri Abhyankar + ts - time stepping context 5872a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5873a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 58749c6b16b5SShri Abhyankar 58759c6b16b5SShri Abhyankar Output Arguments: 58769c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 58779c6b16b5SShri Abhyankar 58789c6b16b5SShri Abhyankar Level: developer 58799c6b16b5SShri Abhyankar 5880deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 58819c6b16b5SShri Abhyankar @*/ 5882a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm) 58839c6b16b5SShri Abhyankar { 58849c6b16b5SShri Abhyankar PetscErrorCode ierr; 58859c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 58869c6b16b5SShri Abhyankar const PetscScalar *u,*y; 58879c6b16b5SShri Abhyankar PetscReal sum,gsum; 58889c6b16b5SShri Abhyankar PetscReal tol; 58899c6b16b5SShri Abhyankar 58909c6b16b5SShri Abhyankar PetscFunctionBegin; 58919c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5892a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5893a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5894a4868fbcSLisandro Dalcin PetscValidType(U,2); 5895a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5896a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5897a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5898a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 58999c6b16b5SShri Abhyankar 59009c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 59019c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 59029c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 59039c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 59049c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 59059c6b16b5SShri Abhyankar sum = 0.; 59069c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 59079c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 59089c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59099c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59109c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 59119c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59129c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 59139c6b16b5SShri Abhyankar } 59149c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59159c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59169c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 59179c6b16b5SShri Abhyankar const PetscScalar *atol; 59189c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59199c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 59209c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59219c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 59229c6b16b5SShri Abhyankar } 59239c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59249c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 59259c6b16b5SShri Abhyankar const PetscScalar *rtol; 59269c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59279c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 59289c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59299c6b16b5SShri Abhyankar sum += PetscSqr(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 for (i=0; i<n; i++) { 59349c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59359c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 59369c6b16b5SShri Abhyankar } 59379c6b16b5SShri Abhyankar } 59389c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 59399c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 59409c6b16b5SShri Abhyankar 5941b2566f29SBarry Smith ierr = MPIU_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 59429c6b16b5SShri Abhyankar *norm = PetscSqrtReal(gsum / N); 59439c6b16b5SShri Abhyankar 59449c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 59459c6b16b5SShri Abhyankar PetscFunctionReturn(0); 59469c6b16b5SShri Abhyankar } 59479c6b16b5SShri Abhyankar 59489c6b16b5SShri Abhyankar #undef __FUNCT__ 59499c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity" 59509c6b16b5SShri Abhyankar /*@ 5951a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 59529c6b16b5SShri Abhyankar 59539c6b16b5SShri Abhyankar Collective on TS 59549c6b16b5SShri Abhyankar 59559c6b16b5SShri Abhyankar Input Arguments: 59569c6b16b5SShri Abhyankar + ts - time stepping context 5957a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5958a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 59599c6b16b5SShri Abhyankar 59609c6b16b5SShri Abhyankar Output Arguments: 59619c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 59629c6b16b5SShri Abhyankar 59639c6b16b5SShri Abhyankar Level: developer 59649c6b16b5SShri Abhyankar 5965deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 59669c6b16b5SShri Abhyankar @*/ 5967a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm) 59689c6b16b5SShri Abhyankar { 59699c6b16b5SShri Abhyankar PetscErrorCode ierr; 59709c6b16b5SShri Abhyankar PetscInt i,n,N,rstart,k; 59719c6b16b5SShri Abhyankar const PetscScalar *u,*y; 59729c6b16b5SShri Abhyankar PetscReal max,gmax; 59739c6b16b5SShri Abhyankar PetscReal tol; 59749c6b16b5SShri Abhyankar 59759c6b16b5SShri Abhyankar PetscFunctionBegin; 59769c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5977a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5978a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5979a4868fbcSLisandro Dalcin PetscValidType(U,2); 5980a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5981a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5982a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5983a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 59849c6b16b5SShri Abhyankar 59859c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 59869c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 59879c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 59889c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 59899c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 59909c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 59919c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 59929c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59939c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59949c6b16b5SShri Abhyankar k = 0; 59959c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 59969c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 59979c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 59989c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59999c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 60009c6b16b5SShri Abhyankar } 60019c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 60029c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60039c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 60049c6b16b5SShri Abhyankar const PetscScalar *atol; 60059c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 60069c6b16b5SShri Abhyankar k = 0; 60079c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 60089c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 60099c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 60109c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60119c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 60129c6b16b5SShri Abhyankar } 60139c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 60149c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 60159c6b16b5SShri Abhyankar const PetscScalar *rtol; 60169c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60179c6b16b5SShri Abhyankar k = 0; 60189c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 60199c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 60209c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 60219c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60229c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 60239c6b16b5SShri Abhyankar } 60249c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60259c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 60269c6b16b5SShri Abhyankar k = 0; 60279c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 60289c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 60299c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 60309c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60319c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 60329c6b16b5SShri Abhyankar } 60339c6b16b5SShri Abhyankar } 60349c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 60359c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 60369c6b16b5SShri Abhyankar 6037b2566f29SBarry Smith ierr = MPIU_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 60389c6b16b5SShri Abhyankar *norm = gmax; 60399c6b16b5SShri Abhyankar 60409c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 60419c6b16b5SShri Abhyankar PetscFunctionReturn(0); 60429c6b16b5SShri Abhyankar } 60439c6b16b5SShri Abhyankar 60449c6b16b5SShri Abhyankar #undef __FUNCT__ 60457619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm" 60461c3436cfSJed Brown /*@ 6047a4868fbcSLisandro Dalcin TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors 60481c3436cfSJed Brown 60491c3436cfSJed Brown Collective on TS 60501c3436cfSJed Brown 60511c3436cfSJed Brown Input Arguments: 60521c3436cfSJed Brown + ts - time stepping context 6053a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 6054a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 6055a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 60567619abb3SShri 60571c3436cfSJed Brown Output Arguments: 60581c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 60591c3436cfSJed Brown 6060a4868fbcSLisandro Dalcin 6061a4868fbcSLisandro Dalcin Options Database Keys: 6062a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 6063a4868fbcSLisandro Dalcin 60641c3436cfSJed Brown Level: developer 60651c3436cfSJed Brown 6066deea92deSShri .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 60671c3436cfSJed Brown @*/ 6068a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm) 60691c3436cfSJed Brown { 60708beabaa1SBarry Smith PetscErrorCode ierr; 60711c3436cfSJed Brown 60721c3436cfSJed Brown PetscFunctionBegin; 6073a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 6074a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNorm2(ts,U,Y,norm);CHKERRQ(ierr); 6075a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 6076a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm);CHKERRQ(ierr); 6077a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 60781c3436cfSJed Brown PetscFunctionReturn(0); 60791c3436cfSJed Brown } 60801c3436cfSJed Brown 60811c3436cfSJed Brown #undef __FUNCT__ 60828d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 60838d59e960SJed Brown /*@ 60848d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 60858d59e960SJed Brown 60868d59e960SJed Brown Logically Collective on TS 60878d59e960SJed Brown 60888d59e960SJed Brown Input Arguments: 60898d59e960SJed Brown + ts - time stepping context 60908d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 60918d59e960SJed Brown 60928d59e960SJed Brown Note: 60938d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 60948d59e960SJed Brown 60958d59e960SJed Brown Level: intermediate 60968d59e960SJed Brown 60978d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 60988d59e960SJed Brown @*/ 60998d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 61008d59e960SJed Brown { 61018d59e960SJed Brown PetscFunctionBegin; 61028d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 61038d59e960SJed Brown ts->cfltime_local = cfltime; 61048d59e960SJed Brown ts->cfltime = -1.; 61058d59e960SJed Brown PetscFunctionReturn(0); 61068d59e960SJed Brown } 61078d59e960SJed Brown 61088d59e960SJed Brown #undef __FUNCT__ 61098d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 61108d59e960SJed Brown /*@ 61118d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 61128d59e960SJed Brown 61138d59e960SJed Brown Collective on TS 61148d59e960SJed Brown 61158d59e960SJed Brown Input Arguments: 61168d59e960SJed Brown . ts - time stepping context 61178d59e960SJed Brown 61188d59e960SJed Brown Output Arguments: 61198d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 61208d59e960SJed Brown 61218d59e960SJed Brown Level: advanced 61228d59e960SJed Brown 61238d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 61248d59e960SJed Brown @*/ 61258d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 61268d59e960SJed Brown { 61278d59e960SJed Brown PetscErrorCode ierr; 61288d59e960SJed Brown 61298d59e960SJed Brown PetscFunctionBegin; 61308d59e960SJed Brown if (ts->cfltime < 0) { 6131b2566f29SBarry Smith ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 61328d59e960SJed Brown } 61338d59e960SJed Brown *cfltime = ts->cfltime; 61348d59e960SJed Brown PetscFunctionReturn(0); 61358d59e960SJed Brown } 61368d59e960SJed Brown 61378d59e960SJed Brown #undef __FUNCT__ 6138d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 6139d6ebe24aSShri Abhyankar /*@ 6140d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 6141d6ebe24aSShri Abhyankar 6142d6ebe24aSShri Abhyankar Input Parameters: 6143d6ebe24aSShri Abhyankar . ts - the TS context. 6144d6ebe24aSShri Abhyankar . xl - lower bound. 6145d6ebe24aSShri Abhyankar . xu - upper bound. 6146d6ebe24aSShri Abhyankar 6147d6ebe24aSShri Abhyankar Notes: 6148d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 6149e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 6150d6ebe24aSShri Abhyankar 61512bd2b0e6SSatish Balay Level: advanced 61522bd2b0e6SSatish Balay 6153d6ebe24aSShri Abhyankar @*/ 6154d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 6155d6ebe24aSShri Abhyankar { 6156d6ebe24aSShri Abhyankar PetscErrorCode ierr; 6157d6ebe24aSShri Abhyankar SNES snes; 6158d6ebe24aSShri Abhyankar 6159d6ebe24aSShri Abhyankar PetscFunctionBegin; 6160d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 6161d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 6162d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 6163d6ebe24aSShri Abhyankar } 6164d6ebe24aSShri Abhyankar 6165325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 6166c6db04a5SJed Brown #include <mex.h> 6167325fc9f4SBarry Smith 6168325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 6169325fc9f4SBarry Smith 6170325fc9f4SBarry Smith #undef __FUNCT__ 6171325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 6172325fc9f4SBarry Smith /* 6173325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 6174325fc9f4SBarry Smith TSSetFunctionMatlab(). 6175325fc9f4SBarry Smith 6176325fc9f4SBarry Smith Collective on TS 6177325fc9f4SBarry Smith 6178325fc9f4SBarry Smith Input Parameters: 6179325fc9f4SBarry Smith + snes - the TS context 61800910c330SBarry Smith - u - input vector 6181325fc9f4SBarry Smith 6182325fc9f4SBarry Smith Output Parameter: 6183325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 6184325fc9f4SBarry Smith 6185325fc9f4SBarry Smith Notes: 6186325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 6187325fc9f4SBarry Smith implementations, so most users would not generally call this routine 6188325fc9f4SBarry Smith themselves. 6189325fc9f4SBarry Smith 6190325fc9f4SBarry Smith Level: developer 6191325fc9f4SBarry Smith 6192325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6193325fc9f4SBarry Smith 6194325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6195325fc9f4SBarry Smith */ 61960910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 6197325fc9f4SBarry Smith { 6198325fc9f4SBarry Smith PetscErrorCode ierr; 6199325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6200325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 6201325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 6202325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 6203325fc9f4SBarry Smith 6204325fc9f4SBarry Smith PetscFunctionBegin; 6205325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 62060910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 62070910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 6208325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 62090910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 6210325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 6211325fc9f4SBarry Smith 6212325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 62130910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 62140910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 62150910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 6216bbd56ea5SKarl Rupp 6217325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6218325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 6219325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6220325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6221325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 6222325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 6223325fc9f4SBarry Smith prhs[6] = sctx->ctx; 6224325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 6225325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6226325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6227325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6228325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6229325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6230325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6231325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6232325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6233325fc9f4SBarry Smith PetscFunctionReturn(0); 6234325fc9f4SBarry Smith } 6235325fc9f4SBarry Smith 6236325fc9f4SBarry Smith 6237325fc9f4SBarry Smith #undef __FUNCT__ 6238325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 6239325fc9f4SBarry Smith /* 6240325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 6241325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 6242e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 6243325fc9f4SBarry Smith 6244325fc9f4SBarry Smith Logically Collective on TS 6245325fc9f4SBarry Smith 6246325fc9f4SBarry Smith Input Parameters: 6247325fc9f4SBarry Smith + ts - the TS context 6248325fc9f4SBarry Smith - func - function evaluation routine 6249325fc9f4SBarry Smith 6250325fc9f4SBarry Smith Calling sequence of func: 62510910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 6252325fc9f4SBarry Smith 6253325fc9f4SBarry Smith Level: beginner 6254325fc9f4SBarry Smith 6255325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6256325fc9f4SBarry Smith 6257325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6258325fc9f4SBarry Smith */ 6259cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 6260325fc9f4SBarry Smith { 6261325fc9f4SBarry Smith PetscErrorCode ierr; 6262325fc9f4SBarry Smith TSMatlabContext *sctx; 6263325fc9f4SBarry Smith 6264325fc9f4SBarry Smith PetscFunctionBegin; 6265325fc9f4SBarry Smith /* currently sctx is memory bleed */ 626695dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6267325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6268325fc9f4SBarry Smith /* 6269325fc9f4SBarry Smith This should work, but it doesn't 6270325fc9f4SBarry Smith sctx->ctx = ctx; 6271325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6272325fc9f4SBarry Smith */ 6273325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6274bbd56ea5SKarl Rupp 62750298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 6276325fc9f4SBarry Smith PetscFunctionReturn(0); 6277325fc9f4SBarry Smith } 6278325fc9f4SBarry Smith 6279325fc9f4SBarry Smith #undef __FUNCT__ 6280325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 6281325fc9f4SBarry Smith /* 6282325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 6283325fc9f4SBarry Smith TSSetJacobianMatlab(). 6284325fc9f4SBarry Smith 6285325fc9f4SBarry Smith Collective on TS 6286325fc9f4SBarry Smith 6287325fc9f4SBarry Smith Input Parameters: 6288cdcf91faSSean Farley + ts - the TS context 62890910c330SBarry Smith . u - input vector 6290325fc9f4SBarry Smith . A, B - the matrices 6291325fc9f4SBarry Smith - ctx - user context 6292325fc9f4SBarry Smith 6293325fc9f4SBarry Smith Level: developer 6294325fc9f4SBarry Smith 6295325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6296325fc9f4SBarry Smith 6297325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6298325fc9f4SBarry Smith @*/ 6299f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 6300325fc9f4SBarry Smith { 6301325fc9f4SBarry Smith PetscErrorCode ierr; 6302325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6303325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 6304325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 6305325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 6306325fc9f4SBarry Smith 6307325fc9f4SBarry Smith PetscFunctionBegin; 6308cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 63090910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 6310325fc9f4SBarry Smith 63110910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 6312325fc9f4SBarry Smith 6313cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 63140910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 63150910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 63160910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 63170910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 6318bbd56ea5SKarl Rupp 6319325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6320325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 6321325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6322325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6323325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 6324325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 6325325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 6326325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 6327325fc9f4SBarry Smith prhs[8] = sctx->ctx; 6328325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 6329325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6330325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6331325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6332325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6333325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6334325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6335325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6336325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 6337325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 6338325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6339325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 6340325fc9f4SBarry Smith PetscFunctionReturn(0); 6341325fc9f4SBarry Smith } 6342325fc9f4SBarry Smith 6343325fc9f4SBarry Smith 6344325fc9f4SBarry Smith #undef __FUNCT__ 6345325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 6346325fc9f4SBarry Smith /* 6347325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 6348e3c5b3baSBarry 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 6349325fc9f4SBarry Smith 6350325fc9f4SBarry Smith Logically Collective on TS 6351325fc9f4SBarry Smith 6352325fc9f4SBarry Smith Input Parameters: 6353cdcf91faSSean Farley + ts - the TS context 6354325fc9f4SBarry Smith . A,B - Jacobian matrices 6355325fc9f4SBarry Smith . func - function evaluation routine 6356325fc9f4SBarry Smith - ctx - user context 6357325fc9f4SBarry Smith 6358325fc9f4SBarry Smith Calling sequence of func: 63590910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 6360325fc9f4SBarry Smith 6361325fc9f4SBarry Smith 6362325fc9f4SBarry Smith Level: developer 6363325fc9f4SBarry Smith 6364325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6365325fc9f4SBarry Smith 6366325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6367325fc9f4SBarry Smith */ 6368cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 6369325fc9f4SBarry Smith { 6370325fc9f4SBarry Smith PetscErrorCode ierr; 6371325fc9f4SBarry Smith TSMatlabContext *sctx; 6372325fc9f4SBarry Smith 6373325fc9f4SBarry Smith PetscFunctionBegin; 6374325fc9f4SBarry Smith /* currently sctx is memory bleed */ 637595dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6376325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6377325fc9f4SBarry Smith /* 6378325fc9f4SBarry Smith This should work, but it doesn't 6379325fc9f4SBarry Smith sctx->ctx = ctx; 6380325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6381325fc9f4SBarry Smith */ 6382325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6383bbd56ea5SKarl Rupp 6384cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 6385325fc9f4SBarry Smith PetscFunctionReturn(0); 6386325fc9f4SBarry Smith } 6387325fc9f4SBarry Smith 6388b5b1a830SBarry Smith #undef __FUNCT__ 6389b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 6390b5b1a830SBarry Smith /* 6391b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 6392b5b1a830SBarry Smith 6393b5b1a830SBarry Smith Collective on TS 6394b5b1a830SBarry Smith 6395b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6396b5b1a830SBarry Smith @*/ 63970910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 6398b5b1a830SBarry Smith { 6399b5b1a830SBarry Smith PetscErrorCode ierr; 6400b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6401a530c242SBarry Smith int nlhs = 1,nrhs = 6; 6402b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 6403b5b1a830SBarry Smith long long int lx = 0,ls = 0; 6404b5b1a830SBarry Smith 6405b5b1a830SBarry Smith PetscFunctionBegin; 6406cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 64070910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 6408b5b1a830SBarry Smith 6409cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 64100910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 6411bbd56ea5SKarl Rupp 6412b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6413b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 6414b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 6415b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 6416b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 6417b5b1a830SBarry Smith prhs[5] = sctx->ctx; 6418b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 6419b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6420b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 6421b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 6422b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 6423b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 6424b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 6425b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 6426b5b1a830SBarry Smith PetscFunctionReturn(0); 6427b5b1a830SBarry Smith } 6428b5b1a830SBarry Smith 6429b5b1a830SBarry Smith 6430b5b1a830SBarry Smith #undef __FUNCT__ 6431b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 6432b5b1a830SBarry Smith /* 6433b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 6434b5b1a830SBarry Smith 6435b5b1a830SBarry Smith Level: developer 6436b5b1a830SBarry Smith 6437b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 6438b5b1a830SBarry Smith 6439b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6440b5b1a830SBarry Smith */ 6441cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 6442b5b1a830SBarry Smith { 6443b5b1a830SBarry Smith PetscErrorCode ierr; 6444b5b1a830SBarry Smith TSMatlabContext *sctx; 6445b5b1a830SBarry Smith 6446b5b1a830SBarry Smith PetscFunctionBegin; 6447b5b1a830SBarry Smith /* currently sctx is memory bleed */ 644895dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6449b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6450b5b1a830SBarry Smith /* 6451b5b1a830SBarry Smith This should work, but it doesn't 6452b5b1a830SBarry Smith sctx->ctx = ctx; 6453b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6454b5b1a830SBarry Smith */ 6455b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6456bbd56ea5SKarl Rupp 64570298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 6458b5b1a830SBarry Smith PetscFunctionReturn(0); 6459b5b1a830SBarry Smith } 6460325fc9f4SBarry Smith #endif 6461b3603a34SBarry Smith 6462b3603a34SBarry Smith #undef __FUNCT__ 64634f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 6464b3603a34SBarry Smith /*@C 64654f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 6466b3603a34SBarry Smith in a time based line graph 6467b3603a34SBarry Smith 6468b3603a34SBarry Smith Collective on TS 6469b3603a34SBarry Smith 6470b3603a34SBarry Smith Input Parameters: 6471b3603a34SBarry Smith + ts - the TS context 6472b3603a34SBarry Smith . step - current time-step 6473b3603a34SBarry Smith . ptime - current time 64747db568b7SBarry Smith . u - current solution 64757db568b7SBarry Smith - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 6476b3603a34SBarry Smith 6477b3d3934dSBarry Smith Options Database: 64789ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 6479b3d3934dSBarry Smith 6480b3603a34SBarry Smith Level: intermediate 6481b3603a34SBarry Smith 64826934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component solutions in a separate window 6483b3603a34SBarry Smith 6484b3603a34SBarry Smith .keywords: TS, vector, monitor, view 6485b3603a34SBarry Smith 64867db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 64877db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 64887db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 64897db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 6490b3603a34SBarry Smith @*/ 64917db568b7SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6492b3603a34SBarry Smith { 6493b3603a34SBarry Smith PetscErrorCode ierr; 64947db568b7SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 6495b3603a34SBarry Smith const PetscScalar *yy; 649680666b62SBarry Smith Vec v; 6497b3603a34SBarry Smith 6498b3603a34SBarry Smith PetscFunctionBegin; 649963e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 650058ff32f7SBarry Smith if (!step) { 6501a9f9c1f6SBarry Smith PetscDrawAxis axis; 65026934998bSLisandro Dalcin PetscInt dim; 6503a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6504a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 6505bab0a581SBarry Smith if (!ctx->names) { 6506bab0a581SBarry Smith PetscBool flg; 6507bab0a581SBarry Smith /* user provides names of variables to plot but no names has been set so assume names are integer values */ 6508bab0a581SBarry Smith ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr); 6509bab0a581SBarry Smith if (flg) { 6510bab0a581SBarry Smith PetscInt i,n; 6511bab0a581SBarry Smith char **names; 6512bab0a581SBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 6513bab0a581SBarry Smith ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr); 6514bab0a581SBarry Smith for (i=0; i<n; i++) { 6515bab0a581SBarry Smith ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr); 6516bab0a581SBarry Smith ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr); 6517bab0a581SBarry Smith } 6518bab0a581SBarry Smith names[n] = NULL; 6519bab0a581SBarry Smith ctx->names = names; 6520bab0a581SBarry Smith } 6521bab0a581SBarry Smith } 6522387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 6523387f4636SBarry Smith char **displaynames; 6524387f4636SBarry Smith PetscBool flg; 6525387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 652695dccacaSBarry Smith ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr); 6527387f4636SBarry Smith ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 6528c5929fdfSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 6529387f4636SBarry Smith if (flg) { 6530a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 6531387f4636SBarry Smith } 6532387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 6533387f4636SBarry Smith } 6534387f4636SBarry Smith if (ctx->displaynames) { 6535387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 6536387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 6537387f4636SBarry Smith } else if (ctx->names) { 65380910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 65390b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6540387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 6541b0bc92c6SBarry Smith } else { 6542b0bc92c6SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6543b0bc92c6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6544387f4636SBarry Smith } 65450b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 654658ff32f7SBarry Smith } 65476934998bSLisandro Dalcin 65486934998bSLisandro Dalcin if (!ctx->transform) v = u; 65496934998bSLisandro Dalcin else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);} 655080666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 65516934998bSLisandro Dalcin if (ctx->displaynames) { 65526934998bSLisandro Dalcin PetscInt i; 65536934998bSLisandro Dalcin for (i=0; i<ctx->ndisplayvariables; i++) 65546934998bSLisandro Dalcin ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]); 65556934998bSLisandro Dalcin ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 65566934998bSLisandro Dalcin } else { 6557e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6558e3efe391SJed Brown PetscInt i,n; 65596934998bSLisandro Dalcin PetscReal *yreal; 656080666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 6561785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6562e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 65630b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6564e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6565e3efe391SJed Brown #else 65660b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6567e3efe391SJed Brown #endif 656880666b62SBarry Smith } 65696934998bSLisandro Dalcin ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 65706934998bSLisandro Dalcin if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);} 65716934998bSLisandro Dalcin 6572b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 65730b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 65746934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 65753923b477SBarry Smith } 6576b3603a34SBarry Smith PetscFunctionReturn(0); 6577b3603a34SBarry Smith } 6578b3603a34SBarry Smith 6579387f4636SBarry Smith 6580b3603a34SBarry Smith #undef __FUNCT__ 658131152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames" 6582b037adc7SBarry Smith /*@C 658331152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6584b037adc7SBarry Smith 6585b037adc7SBarry Smith Collective on TS 6586b037adc7SBarry Smith 6587b037adc7SBarry Smith Input Parameters: 6588b037adc7SBarry Smith + ts - the TS context 6589b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 6590b037adc7SBarry Smith 6591b037adc7SBarry Smith Level: intermediate 6592b037adc7SBarry Smith 65937db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 65947db568b7SBarry Smith 6595b037adc7SBarry Smith .keywords: TS, vector, monitor, view 6596b037adc7SBarry Smith 6597a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 6598b037adc7SBarry Smith @*/ 659931152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 6600b037adc7SBarry Smith { 6601b037adc7SBarry Smith PetscErrorCode ierr; 6602b037adc7SBarry Smith PetscInt i; 6603b037adc7SBarry Smith 6604b037adc7SBarry Smith PetscFunctionBegin; 6605b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6606b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 66075537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 6608b3d3934dSBarry Smith break; 6609b3d3934dSBarry Smith } 6610b3d3934dSBarry Smith } 6611b3d3934dSBarry Smith PetscFunctionReturn(0); 6612b3d3934dSBarry Smith } 6613b3d3934dSBarry Smith 6614b3d3934dSBarry Smith #undef __FUNCT__ 6615e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames" 6616e673d494SBarry Smith /*@C 6617e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6618e673d494SBarry Smith 6619e673d494SBarry Smith Collective on TS 6620e673d494SBarry Smith 6621e673d494SBarry Smith Input Parameters: 6622e673d494SBarry Smith + ts - the TS context 6623e673d494SBarry Smith - names - the names of the components, final string must be NULL 6624e673d494SBarry Smith 6625e673d494SBarry Smith Level: intermediate 6626e673d494SBarry Smith 6627e673d494SBarry Smith .keywords: TS, vector, monitor, view 6628e673d494SBarry Smith 6629a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 6630e673d494SBarry Smith @*/ 6631e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 6632e673d494SBarry Smith { 6633e673d494SBarry Smith PetscErrorCode ierr; 6634e673d494SBarry Smith 6635e673d494SBarry Smith PetscFunctionBegin; 6636e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 6637e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 6638e673d494SBarry Smith PetscFunctionReturn(0); 6639e673d494SBarry Smith } 6640e673d494SBarry Smith 6641e673d494SBarry Smith #undef __FUNCT__ 6642b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames" 6643b3d3934dSBarry Smith /*@C 6644b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 6645b3d3934dSBarry Smith 6646b3d3934dSBarry Smith Collective on TS 6647b3d3934dSBarry Smith 6648b3d3934dSBarry Smith Input Parameter: 6649b3d3934dSBarry Smith . ts - the TS context 6650b3d3934dSBarry Smith 6651b3d3934dSBarry Smith Output Parameter: 6652b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 6653b3d3934dSBarry Smith 6654b3d3934dSBarry Smith Level: intermediate 6655b3d3934dSBarry Smith 66567db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 66577db568b7SBarry Smith 6658b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6659b3d3934dSBarry Smith 6660b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6661b3d3934dSBarry Smith @*/ 6662b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 6663b3d3934dSBarry Smith { 6664b3d3934dSBarry Smith PetscInt i; 6665b3d3934dSBarry Smith 6666b3d3934dSBarry Smith PetscFunctionBegin; 6667b3d3934dSBarry Smith *names = NULL; 6668b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6669b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 66705537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 6671b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 6672b3d3934dSBarry Smith break; 6673387f4636SBarry Smith } 6674387f4636SBarry Smith } 6675387f4636SBarry Smith PetscFunctionReturn(0); 6676387f4636SBarry Smith } 6677387f4636SBarry Smith 6678387f4636SBarry Smith #undef __FUNCT__ 6679a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables" 6680a66092f1SBarry Smith /*@C 6681a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 6682a66092f1SBarry Smith 6683a66092f1SBarry Smith Collective on TS 6684a66092f1SBarry Smith 6685a66092f1SBarry Smith Input Parameters: 6686a66092f1SBarry Smith + ctx - the TSMonitorLG context 6687a66092f1SBarry Smith . displaynames - the names of the components, final string must be NULL 6688a66092f1SBarry Smith 6689a66092f1SBarry Smith Level: intermediate 6690a66092f1SBarry Smith 6691a66092f1SBarry Smith .keywords: TS, vector, monitor, view 6692a66092f1SBarry Smith 6693a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6694a66092f1SBarry Smith @*/ 6695a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 6696a66092f1SBarry Smith { 6697a66092f1SBarry Smith PetscInt j = 0,k; 6698a66092f1SBarry Smith PetscErrorCode ierr; 6699a66092f1SBarry Smith 6700a66092f1SBarry Smith PetscFunctionBegin; 6701a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 6702a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 6703a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 6704a66092f1SBarry Smith while (displaynames[j]) j++; 6705a66092f1SBarry Smith ctx->ndisplayvariables = j; 6706a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 6707a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 6708a66092f1SBarry Smith j = 0; 6709a66092f1SBarry Smith while (displaynames[j]) { 6710a66092f1SBarry Smith k = 0; 6711a66092f1SBarry Smith while (ctx->names[k]) { 6712a66092f1SBarry Smith PetscBool flg; 6713a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 6714a66092f1SBarry Smith if (flg) { 6715a66092f1SBarry Smith ctx->displayvariables[j] = k; 6716a66092f1SBarry Smith break; 6717a66092f1SBarry Smith } 6718a66092f1SBarry Smith k++; 6719a66092f1SBarry Smith } 6720a66092f1SBarry Smith j++; 6721a66092f1SBarry Smith } 6722a66092f1SBarry Smith PetscFunctionReturn(0); 6723a66092f1SBarry Smith } 6724a66092f1SBarry Smith 6725a66092f1SBarry Smith 6726a66092f1SBarry Smith #undef __FUNCT__ 6727387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables" 6728387f4636SBarry Smith /*@C 6729387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 6730387f4636SBarry Smith 6731387f4636SBarry Smith Collective on TS 6732387f4636SBarry Smith 6733387f4636SBarry Smith Input Parameters: 6734387f4636SBarry Smith + ts - the TS context 6735387f4636SBarry Smith . displaynames - the names of the components, final string must be NULL 6736387f4636SBarry Smith 67377db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 67387db568b7SBarry Smith 6739387f4636SBarry Smith Level: intermediate 6740387f4636SBarry Smith 6741387f4636SBarry Smith .keywords: TS, vector, monitor, view 6742387f4636SBarry Smith 6743387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6744387f4636SBarry Smith @*/ 6745387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 6746387f4636SBarry Smith { 6747a66092f1SBarry Smith PetscInt i; 6748387f4636SBarry Smith PetscErrorCode ierr; 6749387f4636SBarry Smith 6750387f4636SBarry Smith PetscFunctionBegin; 6751387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6752387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 67535537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 6754b3d3934dSBarry Smith break; 6755b037adc7SBarry Smith } 6756b037adc7SBarry Smith } 6757b037adc7SBarry Smith PetscFunctionReturn(0); 6758b037adc7SBarry Smith } 6759b037adc7SBarry Smith 6760b037adc7SBarry Smith #undef __FUNCT__ 676180666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform" 676280666b62SBarry Smith /*@C 676380666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 676480666b62SBarry Smith 676580666b62SBarry Smith Collective on TS 676680666b62SBarry Smith 676780666b62SBarry Smith Input Parameters: 676880666b62SBarry Smith + ts - the TS context 676980666b62SBarry Smith . transform - the transform function 67707684fa3eSBarry Smith . destroy - function to destroy the optional context 677180666b62SBarry Smith - ctx - optional context used by transform function 677280666b62SBarry Smith 67737db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 67747db568b7SBarry Smith 677580666b62SBarry Smith Level: intermediate 677680666b62SBarry Smith 677780666b62SBarry Smith .keywords: TS, vector, monitor, view 677880666b62SBarry Smith 6779a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 678080666b62SBarry Smith @*/ 67817684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 678280666b62SBarry Smith { 678380666b62SBarry Smith PetscInt i; 6784a66092f1SBarry Smith PetscErrorCode ierr; 678580666b62SBarry Smith 678680666b62SBarry Smith PetscFunctionBegin; 678780666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 678880666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 67895537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 679080666b62SBarry Smith } 679180666b62SBarry Smith } 679280666b62SBarry Smith PetscFunctionReturn(0); 679380666b62SBarry Smith } 679480666b62SBarry Smith 679580666b62SBarry Smith #undef __FUNCT__ 6796e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform" 6797e673d494SBarry Smith /*@C 6798e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 6799e673d494SBarry Smith 6800e673d494SBarry Smith Collective on TSLGCtx 6801e673d494SBarry Smith 6802e673d494SBarry Smith Input Parameters: 6803e673d494SBarry Smith + ts - the TS context 6804e673d494SBarry Smith . transform - the transform function 68057684fa3eSBarry Smith . destroy - function to destroy the optional context 6806e673d494SBarry Smith - ctx - optional context used by transform function 6807e673d494SBarry Smith 6808e673d494SBarry Smith Level: intermediate 6809e673d494SBarry Smith 6810e673d494SBarry Smith .keywords: TS, vector, monitor, view 6811e673d494SBarry Smith 6812a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 6813e673d494SBarry Smith @*/ 68147684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 6815e673d494SBarry Smith { 6816e673d494SBarry Smith PetscFunctionBegin; 6817e673d494SBarry Smith ctx->transform = transform; 68187684fa3eSBarry Smith ctx->transformdestroy = destroy; 6819e673d494SBarry Smith ctx->transformctx = tctx; 6820e673d494SBarry Smith PetscFunctionReturn(0); 6821e673d494SBarry Smith } 6822e673d494SBarry Smith 6823b3603a34SBarry Smith #undef __FUNCT__ 68244f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 6825ef20d060SBarry Smith /*@C 68264f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 6827ef20d060SBarry Smith in a time based line graph 6828ef20d060SBarry Smith 6829ef20d060SBarry Smith Collective on TS 6830ef20d060SBarry Smith 6831ef20d060SBarry Smith Input Parameters: 6832ef20d060SBarry Smith + ts - the TS context 6833ef20d060SBarry Smith . step - current time-step 6834ef20d060SBarry Smith . ptime - current time 68357db568b7SBarry Smith . u - current solution 68367db568b7SBarry Smith - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 6837ef20d060SBarry Smith 6838ef20d060SBarry Smith Level: intermediate 6839ef20d060SBarry Smith 68406934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component errors in a separate window 6841abd5a294SJed Brown 6842abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 6843abd5a294SJed Brown 6844abd5a294SJed Brown Options Database Keys: 68454f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 6846ef20d060SBarry Smith 6847ef20d060SBarry Smith .keywords: TS, vector, monitor, view 6848ef20d060SBarry Smith 6849abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 6850ef20d060SBarry Smith @*/ 68510910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 6852ef20d060SBarry Smith { 6853ef20d060SBarry Smith PetscErrorCode ierr; 68540b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 6855ef20d060SBarry Smith const PetscScalar *yy; 6856ef20d060SBarry Smith Vec y; 6857ef20d060SBarry Smith 6858ef20d060SBarry Smith PetscFunctionBegin; 6859a9f9c1f6SBarry Smith if (!step) { 6860a9f9c1f6SBarry Smith PetscDrawAxis axis; 68616934998bSLisandro Dalcin PetscInt dim; 6862a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 68631ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 68640910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6865a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6866a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6867a9f9c1f6SBarry Smith } 68680910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 6869ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 68700910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 6871ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 6872e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6873e3efe391SJed Brown { 6874e3efe391SJed Brown PetscReal *yreal; 6875e3efe391SJed Brown PetscInt i,n; 6876e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 6877785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6878e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 68790b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6880e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6881e3efe391SJed Brown } 6882e3efe391SJed Brown #else 68830b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6884e3efe391SJed Brown #endif 6885ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 6886ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 6887b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 68880b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 68896934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 68903923b477SBarry Smith } 6891ef20d060SBarry Smith PetscFunctionReturn(0); 6892ef20d060SBarry Smith } 6893ef20d060SBarry Smith 6894201da799SBarry Smith #undef __FUNCT__ 6895201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 6896201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6897201da799SBarry Smith { 6898201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6899201da799SBarry Smith PetscReal x = ptime,y; 6900201da799SBarry Smith PetscErrorCode ierr; 6901201da799SBarry Smith PetscInt its; 6902201da799SBarry Smith 6903201da799SBarry Smith PetscFunctionBegin; 690463e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 6905201da799SBarry Smith if (!n) { 6906201da799SBarry Smith PetscDrawAxis axis; 6907201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6908201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 6909201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6910201da799SBarry Smith ctx->snes_its = 0; 6911201da799SBarry Smith } 6912201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 6913201da799SBarry Smith y = its - ctx->snes_its; 6914201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 69153fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6916201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 69176934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 6918201da799SBarry Smith } 6919201da799SBarry Smith ctx->snes_its = its; 6920201da799SBarry Smith PetscFunctionReturn(0); 6921201da799SBarry Smith } 6922201da799SBarry Smith 6923201da799SBarry Smith #undef __FUNCT__ 6924201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 6925201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6926201da799SBarry Smith { 6927201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6928201da799SBarry Smith PetscReal x = ptime,y; 6929201da799SBarry Smith PetscErrorCode ierr; 6930201da799SBarry Smith PetscInt its; 6931201da799SBarry Smith 6932201da799SBarry Smith PetscFunctionBegin; 693363e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 6934201da799SBarry Smith if (!n) { 6935201da799SBarry Smith PetscDrawAxis axis; 6936201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6937201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 6938201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6939201da799SBarry Smith ctx->ksp_its = 0; 6940201da799SBarry Smith } 6941201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 6942201da799SBarry Smith y = its - ctx->ksp_its; 6943201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 694499fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6945201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 69466934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 6947201da799SBarry Smith } 6948201da799SBarry Smith ctx->ksp_its = its; 6949201da799SBarry Smith PetscFunctionReturn(0); 6950201da799SBarry Smith } 6951f9c1d6abSBarry Smith 6952f9c1d6abSBarry Smith #undef __FUNCT__ 6953f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 6954f9c1d6abSBarry Smith /*@ 6955f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 6956f9c1d6abSBarry Smith 6957f9c1d6abSBarry Smith Collective on TS and Vec 6958f9c1d6abSBarry Smith 6959f9c1d6abSBarry Smith Input Parameters: 6960f9c1d6abSBarry Smith + ts - the TS context 6961f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 6962f9c1d6abSBarry Smith 6963f9c1d6abSBarry Smith Output Parameters: 6964f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 6965f9c1d6abSBarry Smith 6966f9c1d6abSBarry Smith Level: developer 6967f9c1d6abSBarry Smith 6968f9c1d6abSBarry Smith .keywords: TS, compute 6969f9c1d6abSBarry Smith 6970f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 6971f9c1d6abSBarry Smith @*/ 6972f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 6973f9c1d6abSBarry Smith { 6974f9c1d6abSBarry Smith PetscErrorCode ierr; 6975f9c1d6abSBarry Smith 6976f9c1d6abSBarry Smith PetscFunctionBegin; 6977f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6978ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 6979f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 6980f9c1d6abSBarry Smith PetscFunctionReturn(0); 6981f9c1d6abSBarry Smith } 698224655328SShri 6983b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 6984b3d3934dSBarry Smith #undef __FUNCT__ 6985b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate" 6986b3d3934dSBarry Smith /*@C 6987b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 6988b3d3934dSBarry Smith 6989b3d3934dSBarry Smith Collective on TS 6990b3d3934dSBarry Smith 6991b3d3934dSBarry Smith Input Parameters: 6992b3d3934dSBarry Smith . ts - the ODE solver object 6993b3d3934dSBarry Smith 6994b3d3934dSBarry Smith Output Parameter: 6995b3d3934dSBarry Smith . ctx - the context 6996b3d3934dSBarry Smith 6997b3d3934dSBarry Smith Level: intermediate 6998b3d3934dSBarry Smith 6999b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso 7000b3d3934dSBarry Smith 7001b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 7002b3d3934dSBarry Smith 7003b3d3934dSBarry Smith @*/ 7004b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 7005b3d3934dSBarry Smith { 7006b3d3934dSBarry Smith PetscErrorCode ierr; 7007b3d3934dSBarry Smith 7008b3d3934dSBarry Smith PetscFunctionBegin; 7009a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 7010b3d3934dSBarry Smith PetscFunctionReturn(0); 7011b3d3934dSBarry Smith } 7012b3d3934dSBarry Smith 7013b3d3934dSBarry Smith #undef __FUNCT__ 7014b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope" 7015b3d3934dSBarry Smith /*@C 7016b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 7017b3d3934dSBarry Smith 7018b3d3934dSBarry Smith Collective on TS 7019b3d3934dSBarry Smith 7020b3d3934dSBarry Smith Input Parameters: 7021b3d3934dSBarry Smith + ts - the TS context 7022b3d3934dSBarry Smith . step - current time-step 7023b3d3934dSBarry Smith . ptime - current time 70247db568b7SBarry Smith . u - current solution 70257db568b7SBarry Smith - dctx - the envelope context 7026b3d3934dSBarry Smith 7027b3d3934dSBarry Smith Options Database: 7028b3d3934dSBarry Smith . -ts_monitor_envelope 7029b3d3934dSBarry Smith 7030b3d3934dSBarry Smith Level: intermediate 7031b3d3934dSBarry Smith 7032b3d3934dSBarry Smith Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 7033b3d3934dSBarry Smith 7034b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7035b3d3934dSBarry Smith 70367db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 7037b3d3934dSBarry Smith @*/ 70387db568b7SBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 7039b3d3934dSBarry Smith { 7040b3d3934dSBarry Smith PetscErrorCode ierr; 70417db568b7SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 7042b3d3934dSBarry Smith 7043b3d3934dSBarry Smith PetscFunctionBegin; 7044b3d3934dSBarry Smith if (!ctx->max) { 7045b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 7046b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 7047b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 7048b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 7049b3d3934dSBarry Smith } else { 7050b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 7051b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 7052b3d3934dSBarry Smith } 7053b3d3934dSBarry Smith PetscFunctionReturn(0); 7054b3d3934dSBarry Smith } 7055b3d3934dSBarry Smith 7056b3d3934dSBarry Smith 7057b3d3934dSBarry Smith #undef __FUNCT__ 7058b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds" 7059b3d3934dSBarry Smith /*@C 7060b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 7061b3d3934dSBarry Smith 7062b3d3934dSBarry Smith Collective on TS 7063b3d3934dSBarry Smith 7064b3d3934dSBarry Smith Input Parameter: 7065b3d3934dSBarry Smith . ts - the TS context 7066b3d3934dSBarry Smith 7067b3d3934dSBarry Smith Output Parameter: 7068b3d3934dSBarry Smith + max - the maximum values 7069b3d3934dSBarry Smith - min - the minimum values 7070b3d3934dSBarry Smith 70717db568b7SBarry Smith Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 70727db568b7SBarry Smith 7073b3d3934dSBarry Smith Level: intermediate 7074b3d3934dSBarry Smith 7075b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7076b3d3934dSBarry Smith 7077b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 7078b3d3934dSBarry Smith @*/ 7079b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 7080b3d3934dSBarry Smith { 7081b3d3934dSBarry Smith PetscInt i; 7082b3d3934dSBarry Smith 7083b3d3934dSBarry Smith PetscFunctionBegin; 7084b3d3934dSBarry Smith if (max) *max = NULL; 7085b3d3934dSBarry Smith if (min) *min = NULL; 7086b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7087b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 70885537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 7089b3d3934dSBarry Smith if (max) *max = ctx->max; 7090b3d3934dSBarry Smith if (min) *min = ctx->min; 7091b3d3934dSBarry Smith break; 7092b3d3934dSBarry Smith } 7093b3d3934dSBarry Smith } 7094b3d3934dSBarry Smith PetscFunctionReturn(0); 7095b3d3934dSBarry Smith } 7096b3d3934dSBarry Smith 7097b3d3934dSBarry Smith #undef __FUNCT__ 7098b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy" 7099b3d3934dSBarry Smith /*@C 7100b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 7101b3d3934dSBarry Smith 7102b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 7103b3d3934dSBarry Smith 7104b3d3934dSBarry Smith Input Parameter: 7105b3d3934dSBarry Smith . ctx - the monitor context 7106b3d3934dSBarry Smith 7107b3d3934dSBarry Smith Level: intermediate 7108b3d3934dSBarry Smith 7109b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy 7110b3d3934dSBarry Smith 71117db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 7112b3d3934dSBarry Smith @*/ 7113b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 7114b3d3934dSBarry Smith { 7115b3d3934dSBarry Smith PetscErrorCode ierr; 7116b3d3934dSBarry Smith 7117b3d3934dSBarry Smith PetscFunctionBegin; 7118b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 7119b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 7120b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 7121b3d3934dSBarry Smith PetscFunctionReturn(0); 7122b3d3934dSBarry Smith } 7123f2dee214SBarry Smith 712424655328SShri #undef __FUNCT__ 712524655328SShri #define __FUNCT__ "TSRollBack" 712624655328SShri /*@ 712724655328SShri TSRollBack - Rolls back one time step 712824655328SShri 712924655328SShri Collective on TS 713024655328SShri 713124655328SShri Input Parameter: 713224655328SShri . ts - the TS context obtained from TSCreate() 713324655328SShri 713424655328SShri Level: advanced 713524655328SShri 713624655328SShri .keywords: TS, timestep, rollback 713724655328SShri 713824655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 713924655328SShri @*/ 714024655328SShri PetscErrorCode TSRollBack(TS ts) 714124655328SShri { 714224655328SShri PetscErrorCode ierr; 714324655328SShri 714424655328SShri PetscFunctionBegin; 714524655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7146b3de5cdeSLisandro Dalcin if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called"); 714724655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 714824655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 714924655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 715024655328SShri ts->ptime = ts->ptime_prev; 7151be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 7152be5899b3SLisandro Dalcin ts->steps--; ts->total_steps--; 7153b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 715424655328SShri PetscFunctionReturn(0); 715524655328SShri } 7156aeb4809dSShri Abhyankar 7157ff22ae23SHong Zhang #undef __FUNCT__ 7158ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages" 7159ff22ae23SHong Zhang /*@ 7160ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 7161ff22ae23SHong Zhang 7162ff22ae23SHong Zhang Input Parameter: 7163ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 7164ff22ae23SHong Zhang 7165ff22ae23SHong Zhang Level: advanced 7166ff22ae23SHong Zhang 7167ff22ae23SHong Zhang .keywords: TS, getstages 7168ff22ae23SHong Zhang 7169ff22ae23SHong Zhang .seealso: TSCreate() 7170ff22ae23SHong Zhang @*/ 7171ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns,Vec **Y) 7172ff22ae23SHong Zhang { 7173ff22ae23SHong Zhang PetscErrorCode ierr; 7174ff22ae23SHong Zhang 7175ff22ae23SHong Zhang PetscFunctionBegin; 7176ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7177ff22ae23SHong Zhang PetscValidPointer(ns,2); 7178ff22ae23SHong Zhang 7179ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 7180ff22ae23SHong Zhang else { 7181ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 7182ff22ae23SHong Zhang } 7183ff22ae23SHong Zhang PetscFunctionReturn(0); 7184ff22ae23SHong Zhang } 7185ff22ae23SHong Zhang 7186847ff0e1SMatthew G. Knepley #undef __FUNCT__ 7187847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor" 7188847ff0e1SMatthew G. Knepley /*@C 7189847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 7190847ff0e1SMatthew G. Knepley 7191847ff0e1SMatthew G. Knepley Collective on SNES 7192847ff0e1SMatthew G. Knepley 7193847ff0e1SMatthew G. Knepley Input Parameters: 7194847ff0e1SMatthew G. Knepley + ts - the TS context 7195847ff0e1SMatthew G. Knepley . t - current timestep 7196847ff0e1SMatthew G. Knepley . U - state vector 7197847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 7198847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 7199847ff0e1SMatthew G. Knepley - ctx - an optional user context 7200847ff0e1SMatthew G. Knepley 7201847ff0e1SMatthew G. Knepley Output Parameters: 7202847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 7203847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 7204847ff0e1SMatthew G. Knepley 7205847ff0e1SMatthew G. Knepley Level: intermediate 7206847ff0e1SMatthew G. Knepley 7207847ff0e1SMatthew G. Knepley Notes: 7208847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 7209847ff0e1SMatthew G. Knepley 7210847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 7211847ff0e1SMatthew G. Knepley 7212847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 7213847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 7214847ff0e1SMatthew G. Knepley 7215847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 7216847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 7217847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 7218847ff0e1SMatthew G. Knepley 7219847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse 7220847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 7221847ff0e1SMatthew G. Knepley @*/ 7222847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 7223847ff0e1SMatthew G. Knepley { 7224847ff0e1SMatthew G. Knepley SNES snes; 7225847ff0e1SMatthew G. Knepley MatFDColoring color; 7226847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 7227847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 7228847ff0e1SMatthew G. Knepley 7229847ff0e1SMatthew G. Knepley PetscFunctionBegin; 7230c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 7231847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 7232847ff0e1SMatthew G. Knepley if (!color) { 7233847ff0e1SMatthew G. Knepley DM dm; 7234847ff0e1SMatthew G. Knepley ISColoring iscoloring; 7235847ff0e1SMatthew G. Knepley 7236847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 7237847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 7238847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 7239847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 7240847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7241847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7242847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7243847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7244847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7245847ff0e1SMatthew G. Knepley } else { 7246847ff0e1SMatthew G. Knepley MatColoring mc; 7247847ff0e1SMatthew G. Knepley 7248847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 7249847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 7250847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 7251847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 7252847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 7253847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 7254847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7255847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7256847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7257847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7258847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7259847ff0e1SMatthew G. Knepley } 7260847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 7261847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 7262847ff0e1SMatthew G. Knepley } 7263847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 7264847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 7265847ff0e1SMatthew G. Knepley if (J != B) { 7266847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7267847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7268847ff0e1SMatthew G. Knepley } 7269847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 7270847ff0e1SMatthew G. Knepley } 727193b34091SDebojyoti Ghosh 727293b34091SDebojyoti Ghosh #undef __FUNCT__ 7273cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSSetFunctionDomainError" 7274cb9d8021SPierre Barbier de Reuille /*@ 7275cb9d8021SPierre Barbier de Reuille TSSetFunctionDomainError - Set the function testing if the current state vector is valid 7276cb9d8021SPierre Barbier de Reuille 7277cb9d8021SPierre Barbier de Reuille Input Parameters: 7278cb9d8021SPierre Barbier de Reuille ts - the TS context 7279cb9d8021SPierre Barbier de Reuille func - function called within TSFunctionDomainError 7280cb9d8021SPierre Barbier de Reuille 7281cb9d8021SPierre Barbier de Reuille Level: intermediate 7282cb9d8021SPierre Barbier de Reuille 7283cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain 7284cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError() 7285cb9d8021SPierre Barbier de Reuille @*/ 7286cb9d8021SPierre Barbier de Reuille 7287d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 7288cb9d8021SPierre Barbier de Reuille { 7289cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7290cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7291cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 7292cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7293cb9d8021SPierre Barbier de Reuille } 7294cb9d8021SPierre Barbier de Reuille 7295cb9d8021SPierre Barbier de Reuille #undef __FUNCT__ 7296cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSFunctionDomainError" 7297cb9d8021SPierre Barbier de Reuille /*@ 7298cb9d8021SPierre Barbier de Reuille TSFunctionDomainError - Check if the current state is valid 7299cb9d8021SPierre Barbier de Reuille 7300cb9d8021SPierre Barbier de Reuille Input Parameters: 7301cb9d8021SPierre Barbier de Reuille ts - the TS context 7302cb9d8021SPierre Barbier de Reuille stagetime - time of the simulation 7303d183316bSPierre Barbier de Reuille Y - state vector to check. 7304cb9d8021SPierre Barbier de Reuille 7305cb9d8021SPierre Barbier de Reuille Output Parameter: 7306cb9d8021SPierre Barbier de Reuille accept - Set to PETSC_FALSE if the current state vector is valid. 7307cb9d8021SPierre Barbier de Reuille 7308cb9d8021SPierre Barbier de Reuille Note: 7309cb9d8021SPierre Barbier de Reuille This function should be used to ensure the state is in a valid part of the space. 7310cb9d8021SPierre Barbier de Reuille For example, one can ensure here all values are positive. 731196a0c994SBarry Smith 731296a0c994SBarry Smith Level: advanced 7313cb9d8021SPierre Barbier de Reuille @*/ 7314d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 7315cb9d8021SPierre Barbier de Reuille { 7316cb9d8021SPierre Barbier de Reuille PetscErrorCode ierr; 7317cb9d8021SPierre Barbier de Reuille 7318cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7319cb9d8021SPierre Barbier de Reuille 7320cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7321cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 7322cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 7323d183316bSPierre Barbier de Reuille PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 7324cb9d8021SPierre Barbier de Reuille } 7325cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7326cb9d8021SPierre Barbier de Reuille } 73271ceb14c0SBarry Smith 73281ceb14c0SBarry Smith #undef __FUNCT__ 7329e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone" 733093b34091SDebojyoti Ghosh /*@C 7331e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 733293b34091SDebojyoti Ghosh 733393b34091SDebojyoti Ghosh Collective on MPI_Comm 733493b34091SDebojyoti Ghosh 733593b34091SDebojyoti Ghosh Input Parameter: 733693b34091SDebojyoti Ghosh . tsin - The input TS 733793b34091SDebojyoti Ghosh 733893b34091SDebojyoti Ghosh Output Parameter: 7339e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 734093b34091SDebojyoti Ghosh 73415eca1a21SEmil Constantinescu Notes: 73425eca1a21SEmil 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. 73435eca1a21SEmil Constantinescu 7344baa10174SEmil 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); 73455eca1a21SEmil Constantinescu 73465eca1a21SEmil Constantinescu Level: developer 734793b34091SDebojyoti Ghosh 7348e5168f73SEmil Constantinescu .keywords: TS, clone 7349e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 735093b34091SDebojyoti Ghosh @*/ 7351baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 735293b34091SDebojyoti Ghosh { 735393b34091SDebojyoti Ghosh TS t; 735493b34091SDebojyoti Ghosh PetscErrorCode ierr; 7355dc846ba4SSatish Balay SNES snes_start; 7356dc846ba4SSatish Balay DM dm; 7357dc846ba4SSatish Balay TSType type; 735893b34091SDebojyoti Ghosh 735993b34091SDebojyoti Ghosh PetscFunctionBegin; 736093b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 736193b34091SDebojyoti Ghosh *tsout = NULL; 736293b34091SDebojyoti Ghosh 73637a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 736493b34091SDebojyoti Ghosh 736593b34091SDebojyoti Ghosh /* General TS description */ 736693b34091SDebojyoti Ghosh t->numbermonitors = 0; 736793b34091SDebojyoti Ghosh t->setupcalled = 0; 736893b34091SDebojyoti Ghosh t->ksp_its = 0; 736993b34091SDebojyoti Ghosh t->snes_its = 0; 737093b34091SDebojyoti Ghosh t->nwork = 0; 737193b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 737293b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 737393b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 737493b34091SDebojyoti Ghosh 737534561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr); 737634561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr); 7377d15a3a53SEmil Constantinescu 737893b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr); 737993b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm);CHKERRQ(ierr); 738093b34091SDebojyoti Ghosh 738193b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 738251699248SLisandro Dalcin ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr); 738393b34091SDebojyoti Ghosh 738493b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 738593b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 738693b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 738793b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 738893b34091SDebojyoti Ghosh t->steps = tsin->steps; 738993b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 739093b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 739193b34091SDebojyoti Ghosh t->atol = tsin->atol; 739293b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 739393b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 739493b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 739593b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 739693b34091SDebojyoti Ghosh 739793b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type);CHKERRQ(ierr); 739893b34091SDebojyoti Ghosh ierr = TSSetType(t,type);CHKERRQ(ierr); 739993b34091SDebojyoti Ghosh 740093b34091SDebojyoti Ghosh t->vec_sol = NULL; 740193b34091SDebojyoti Ghosh 740293b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 740393b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 740493b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 740593b34091SDebojyoti Ghosh 740693b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 740793b34091SDebojyoti Ghosh 74080d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 74090d4fed19SBarry Smith PetscInt i; 74100d4fed19SBarry Smith ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr); 74110d4fed19SBarry Smith for (i=0; i<10; i++) { 74120d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 74130d4fed19SBarry Smith } 74140d4fed19SBarry Smith } 741593b34091SDebojyoti Ghosh *tsout = t; 741693b34091SDebojyoti Ghosh PetscFunctionReturn(0); 741793b34091SDebojyoti Ghosh } 7418