163dd3a1aSKris Buschelman 2af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 3496e6a7aSJed Brown #include <petscdmshell.h> 41e25c274SJed Brown #include <petscdmda.h> 52d5ee99bSBarry Smith #include <petscviewer.h> 62d5ee99bSBarry Smith #include <petscdraw.h> 7d763cef2SBarry Smith 8d5ba7fb7SMatthew Knepley /* Logging support */ 9d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 108d0ad7a8SHong Zhang PetscLogEvent TS_AdjointStep, TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 11d405a339SMatthew Knepley 12feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1349354f04SShri Abhyankar 142d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx { 152d5ee99bSBarry Smith PetscViewer viewer; 162d5ee99bSBarry Smith Vec initialsolution; 172d5ee99bSBarry Smith PetscBool showinitial; 182d5ee99bSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 192d5ee99bSBarry Smith PetscBool showtimestepandtime; 202d5ee99bSBarry Smith }; 212d5ee99bSBarry Smith 22bdad233fSMatthew Knepley #undef __FUNCT__ 23fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSetFromOptions" 24fde5950dSBarry Smith /*@C 25fde5950dSBarry Smith TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 26fde5950dSBarry Smith 27fde5950dSBarry Smith Collective on TS 28fde5950dSBarry Smith 29fde5950dSBarry Smith Input Parameters: 30fde5950dSBarry Smith + ts - TS object you wish to monitor 31fde5950dSBarry Smith . name - the monitor type one is seeking 32fde5950dSBarry Smith . help - message indicating what monitoring is done 33fde5950dSBarry Smith . manual - manual page for the monitor 34fde5950dSBarry Smith . monitor - the monitor function 35fde5950dSBarry Smith - monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects 36fde5950dSBarry Smith 37fde5950dSBarry Smith Level: developer 38fde5950dSBarry Smith 39fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 40fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 41fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 42fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 43fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 44fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 45fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 46fde5950dSBarry Smith @*/ 47721cd6eeSBarry Smith PetscErrorCode TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*)) 48fde5950dSBarry Smith { 49fde5950dSBarry Smith PetscErrorCode ierr; 50fde5950dSBarry Smith PetscViewer viewer; 51fde5950dSBarry Smith PetscViewerFormat format; 52fde5950dSBarry Smith PetscBool flg; 53fde5950dSBarry Smith 54fde5950dSBarry Smith PetscFunctionBegin; 55fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 56fde5950dSBarry Smith if (flg) { 57721cd6eeSBarry Smith PetscViewerAndFormat *vf; 58721cd6eeSBarry Smith ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr); 59721cd6eeSBarry Smith ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 60fde5950dSBarry Smith if (monitorsetup) { 61721cd6eeSBarry Smith ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr); 62fde5950dSBarry Smith } 63721cd6eeSBarry Smith ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr); 64fde5950dSBarry Smith } 65fde5950dSBarry Smith PetscFunctionReturn(0); 66fde5950dSBarry Smith } 67fde5950dSBarry Smith 68fde5950dSBarry Smith #undef __FUNCT__ 69fde5950dSBarry Smith #define __FUNCT__ "TSAdjointMonitorSetFromOptions" 70fde5950dSBarry Smith /*@C 71fde5950dSBarry Smith TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 72fde5950dSBarry Smith 73fde5950dSBarry Smith Collective on TS 74fde5950dSBarry Smith 75fde5950dSBarry Smith Input Parameters: 76fde5950dSBarry Smith + ts - TS object you wish to monitor 77fde5950dSBarry Smith . name - the monitor type one is seeking 78fde5950dSBarry Smith . help - message indicating what monitoring is done 79fde5950dSBarry Smith . manual - manual page for the monitor 80fde5950dSBarry Smith . monitor - the monitor function 81fde5950dSBarry Smith - monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects 82fde5950dSBarry Smith 83fde5950dSBarry Smith Level: developer 84fde5950dSBarry Smith 85fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 86fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 87fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 88fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 89fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 90fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 91fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 92fde5950dSBarry Smith @*/ 93721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*)) 94fde5950dSBarry Smith { 95fde5950dSBarry Smith PetscErrorCode ierr; 96fde5950dSBarry Smith PetscViewer viewer; 97fde5950dSBarry Smith PetscViewerFormat format; 98fde5950dSBarry Smith PetscBool flg; 99fde5950dSBarry Smith 100fde5950dSBarry Smith PetscFunctionBegin; 101fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 102fde5950dSBarry Smith if (flg) { 103721cd6eeSBarry Smith PetscViewerAndFormat *vf; 104721cd6eeSBarry Smith ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr); 105721cd6eeSBarry Smith ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 106fde5950dSBarry Smith if (monitorsetup) { 107721cd6eeSBarry Smith ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr); 108fde5950dSBarry Smith } 109721cd6eeSBarry Smith ierr = TSAdjointMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr); 110fde5950dSBarry Smith } 111fde5950dSBarry Smith PetscFunctionReturn(0); 112fde5950dSBarry Smith } 113fde5950dSBarry Smith 114fde5950dSBarry Smith #undef __FUNCT__ 115bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions" 116bdad233fSMatthew Knepley /*@ 117bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 118bdad233fSMatthew Knepley 119bdad233fSMatthew Knepley Collective on TS 120bdad233fSMatthew Knepley 121bdad233fSMatthew Knepley Input Parameter: 122bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 123bdad233fSMatthew Knepley 124bdad233fSMatthew Knepley Options Database Keys: 125cd11d68dSLisandro Dalcin + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGL, TSSSP 126ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 1273e4cdcaaSBarry Smith . -ts_max_steps <maxsteps> - maximum number of time-steps to take 1283e4cdcaaSBarry Smith . -ts_final_time <time> - maximum time to compute to 1293e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 130a3cdaa26SBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> whether to stop at the exact given final time and how to compute the solution at that ti,e 131a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 132a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 133a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 134a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 135a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 136ef222394SBarry Smith . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 137847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 138bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 139de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 140de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 1416934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 142de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 143de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 144de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 145de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 1463e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 1473e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 148fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 149b3d3934dSBarry Smith . -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 150110eb670SHong Zhang . -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 151110eb670SHong Zhang . -ts_adjoint_monitor - print information at each adjoint time step 15253ea634cSHong Zhang - -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically 15353ea634cSHong Zhang 15491b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 155bdad233fSMatthew Knepley 156bdad233fSMatthew Knepley Level: beginner 157bdad233fSMatthew Knepley 158bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 159bdad233fSMatthew Knepley 160a313700dSBarry Smith .seealso: TSGetType() 161bdad233fSMatthew Knepley @*/ 1627087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 163bdad233fSMatthew Knepley { 164bc952696SBarry Smith PetscBool opt,flg,tflg; 165dfbe8321SBarry Smith PetscErrorCode ierr; 166eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 16731748224SBarry Smith PetscReal time_step; 16849354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 169d1212d36SBarry Smith char dir[16]; 170cd11d68dSLisandro Dalcin TSIFunction ifun; 1716991f827SBarry Smith const char *defaultType; 1726991f827SBarry Smith char typeName[256]; 173bdad233fSMatthew Knepley 174bdad233fSMatthew Knepley PetscFunctionBegin; 1750700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1766991f827SBarry Smith 1776991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 178cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 179cd11d68dSLisandro Dalcin 180cd11d68dSLisandro Dalcin ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 181cd11d68dSLisandro Dalcin if (((PetscObject)ts)->type_name) 182cd11d68dSLisandro Dalcin defaultType = ((PetscObject)ts)->type_name; 183cd11d68dSLisandro Dalcin else 184cd11d68dSLisandro Dalcin defaultType = ifun ? TSBEULER : TSEULER; 1856991f827SBarry Smith ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr); 1866991f827SBarry Smith if (opt) { 1876991f827SBarry Smith ierr = TSSetType(ts,typeName);CHKERRQ(ierr); 1886991f827SBarry Smith } else { 1896991f827SBarry Smith ierr = TSSetType(ts,defaultType);CHKERRQ(ierr); 1906991f827SBarry Smith } 191bdad233fSMatthew Knepley 192bdad233fSMatthew Knepley /* Handle generic TS options */ 1930298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1940298fd71SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 1950298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 19631748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 197cd11d68dSLisandro Dalcin if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);} 19849354f04SShri Abhyankar ierr = PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);CHKERRQ(ierr); 19949354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 2000298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL);CHKERRQ(ierr); 2010298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 2020298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 2030298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 2040298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 205bdad233fSMatthew Knepley 20656f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 20756f85f32SBarry Smith { 20856f85f32SBarry Smith PetscBool set; 20956f85f32SBarry Smith flg = PETSC_FALSE; 21056f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 21156f85f32SBarry Smith if (set) { 21256f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 21356f85f32SBarry Smith } 21456f85f32SBarry Smith } 21556f85f32SBarry Smith #endif 21656f85f32SBarry Smith 217bdad233fSMatthew Knepley /* Monitor options */ 218cd11d68dSLisandro Dalcin ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr); 219fde5950dSBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr); 220fde5950dSBarry Smith ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr); 221fde5950dSBarry Smith 2225180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2235180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 2245180491cSLisandro Dalcin 2254f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 226b3603a34SBarry Smith if (opt) { 2270b039ecaSBarry Smith TSMonitorLGCtx ctx; 2283923b477SBarry Smith PetscInt howoften = 1; 229b3603a34SBarry Smith 2300298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 2316ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2324f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 233bdad233fSMatthew Knepley } 2346ba87a44SLisandro Dalcin 2354f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 236ef20d060SBarry Smith if (opt) { 2370b039ecaSBarry Smith TSMonitorLGCtx ctx; 2383923b477SBarry Smith PetscInt howoften = 1; 239ef20d060SBarry Smith 2400298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 2416ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2424f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 243ef20d060SBarry Smith } 2446934998bSLisandro Dalcin 2456934998bSLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 2466934998bSLisandro Dalcin if (opt) { 2476934998bSLisandro Dalcin TSMonitorLGCtx ctx; 2486934998bSLisandro Dalcin PetscInt howoften = 1; 2496934998bSLisandro Dalcin 2506934998bSLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 2516ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2526934998bSLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 2536934998bSLisandro Dalcin } 254201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 255201da799SBarry Smith if (opt) { 256201da799SBarry Smith TSMonitorLGCtx ctx; 257201da799SBarry Smith PetscInt howoften = 1; 258201da799SBarry Smith 2590298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2606ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 261201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 262201da799SBarry Smith } 263201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 264201da799SBarry Smith if (opt) { 265201da799SBarry Smith TSMonitorLGCtx ctx; 266201da799SBarry Smith PetscInt howoften = 1; 267201da799SBarry Smith 2680298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2696ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 270201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 271201da799SBarry Smith } 2728189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 2738189c53fSBarry Smith if (opt) { 2748189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2758189c53fSBarry Smith PetscInt howoften = 1; 2768189c53fSBarry Smith 2770298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 2786934998bSLisandro Dalcin ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 2798189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 2808189c53fSBarry Smith } 281ef20d060SBarry Smith opt = PETSC_FALSE; 2820dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 283a7cc72afSBarry Smith if (opt) { 28483a4ac43SBarry Smith TSMonitorDrawCtx ctx; 28583a4ac43SBarry Smith PetscInt howoften = 1; 286a80ad3e0SBarry Smith 2870298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 2886934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 28983a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 290bdad233fSMatthew Knepley } 291fb1732b5SBarry Smith opt = PETSC_FALSE; 2929110b2e7SHong Zhang ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr); 2939110b2e7SHong Zhang if (opt) { 2949110b2e7SHong Zhang TSMonitorDrawCtx ctx; 2959110b2e7SHong Zhang PetscInt howoften = 1; 2969110b2e7SHong Zhang 2979110b2e7SHong Zhang ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr); 2986934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 2999110b2e7SHong Zhang ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3009110b2e7SHong Zhang } 3019110b2e7SHong Zhang opt = PETSC_FALSE; 3022d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 3032d5ee99bSBarry Smith if (opt) { 3042d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 3052d5ee99bSBarry Smith PetscReal bounds[4]; 3062d5ee99bSBarry Smith PetscInt n = 4; 3072d5ee99bSBarry Smith PetscDraw draw; 3086934998bSLisandro Dalcin PetscDrawAxis axis; 3092d5ee99bSBarry Smith 3102d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 3112d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 3126934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr); 3132d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 3146934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr); 3156934998bSLisandro Dalcin ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 3166934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 3172d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3182d5ee99bSBarry Smith } 3192d5ee99bSBarry Smith opt = PETSC_FALSE; 3200dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 3213a471f94SBarry Smith if (opt) { 32283a4ac43SBarry Smith TSMonitorDrawCtx ctx; 32383a4ac43SBarry Smith PetscInt howoften = 1; 3243a471f94SBarry Smith 3250298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 3266934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 32783a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3283a471f94SBarry Smith } 329fde5950dSBarry Smith 330ed81e22dSJed Brown opt = PETSC_FALSE; 33191b97e58SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 332ed81e22dSJed Brown if (flg) { 333ed81e22dSJed Brown const char *ptr,*ptr2; 334ed81e22dSJed Brown char *filetemplate; 335ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 336ed81e22dSJed Brown /* Do some cursory validation of the input. */ 337ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 338ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 339ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 340ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 341ce94432eSBarry Smith if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts"); 342ed81e22dSJed Brown if (ptr2) break; 343ed81e22dSJed Brown } 344ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 345ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 346ed81e22dSJed Brown } 347bdad233fSMatthew Knepley 348d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 349d1212d36SBarry Smith if (flg) { 350d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 351d1212d36SBarry Smith int ray = 0; 352d1212d36SBarry Smith DMDADirection ddir; 353d1212d36SBarry Smith DM da; 354d1212d36SBarry Smith PetscMPIInt rank; 355d1212d36SBarry Smith 356ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 357d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 358d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 359ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 360d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 361d1212d36SBarry Smith 362d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 363b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 364d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 365d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 366ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 367d1212d36SBarry Smith if (!rank) { 368d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 369d1212d36SBarry Smith } 37051b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 371d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 372d1212d36SBarry Smith } 37351b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 37451b4a12fSMatthew G. Knepley if (flg) { 37551b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 37651b4a12fSMatthew G. Knepley int ray = 0; 37751b4a12fSMatthew G. Knepley DMDADirection ddir; 37851b4a12fSMatthew G. Knepley DM da; 37951b4a12fSMatthew G. Knepley PetscInt howoften = 1; 380d1212d36SBarry Smith 38151b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 38251b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 38351b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 38451b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 38551b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 3861c3436cfSJed Brown 38751b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 388b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 38951b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 39051b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 39151b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 39251b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 39351b4a12fSMatthew G. Knepley } 394a7a1495cSBarry Smith 395b3d3934dSBarry Smith ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 396b3d3934dSBarry Smith if (opt) { 397b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 398b3d3934dSBarry Smith 399b3d3934dSBarry Smith ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 400b3d3934dSBarry Smith ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 401b3d3934dSBarry Smith } 402b3d3934dSBarry Smith 403847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 404847ff0e1SMatthew G. Knepley ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr); 405847ff0e1SMatthew G. Knepley if (flg) { 406847ff0e1SMatthew G. Knepley DM dm; 407847ff0e1SMatthew G. Knepley DMTS tdm; 408847ff0e1SMatthew G. Knepley 409847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 410847ff0e1SMatthew G. Knepley ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr); 411847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 412847ff0e1SMatthew G. Knepley ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr); 413847ff0e1SMatthew G. Knepley ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 414847ff0e1SMatthew G. Knepley } 415847ff0e1SMatthew G. Knepley 416ee346746SBarry Smith if (ts->adapt) { 417ee346746SBarry Smith ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr); 418ee346746SBarry Smith } 419d763cef2SBarry Smith 420d763cef2SBarry Smith /* Handle specific TS options */ 421d763cef2SBarry Smith if (ts->ops->setfromoptions) { 4226991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 423d763cef2SBarry Smith } 424fbc52257SHong Zhang 42568bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 4264f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 42768bece0bSHong Zhang ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 42868bece0bSHong Zhang if (tflg) { 42968bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 43068bece0bSHong Zhang } 4314f122a70SLisandro Dalcin tflg = ts->adjoint_solve ? PETSC_TRUE : PETSC_FALSE; 43268bece0bSHong Zhang ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr); 43368bece0bSHong Zhang if (flg) { 43468bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 43568bece0bSHong Zhang ts->adjoint_solve = tflg; 43668bece0bSHong Zhang } 437d763cef2SBarry Smith 438d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 4390633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr); 440fbc52257SHong Zhang ierr = PetscOptionsEnd();CHKERRQ(ierr); 441d763cef2SBarry Smith 4424f122a70SLisandro Dalcin if (ts->trajectory) { 4434f122a70SLisandro Dalcin ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 444d763cef2SBarry Smith } 44568bece0bSHong Zhang 4464f122a70SLisandro Dalcin ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr); 4474f122a70SLisandro Dalcin if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);} 4484f122a70SLisandro Dalcin ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr); 449d763cef2SBarry Smith PetscFunctionReturn(0); 450d763cef2SBarry Smith } 451d763cef2SBarry Smith 452d763cef2SBarry Smith #undef __FUNCT__ 453bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory" 454d2daff3dSHong Zhang /*@ 455bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 456d2daff3dSHong Zhang 457d2daff3dSHong Zhang Collective on TS 458d2daff3dSHong Zhang 459d2daff3dSHong Zhang Input Parameters: 460bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 461bc952696SBarry Smith 46268bece0bSHong Zhang Note: This routine should be called after all TS options have been set 463d2daff3dSHong Zhang 464d2daff3dSHong Zhang Level: intermediate 465d2daff3dSHong Zhang 466bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve() 467d2daff3dSHong Zhang 468bc952696SBarry Smith .keywords: TS, set, checkpoint, 469d2daff3dSHong Zhang @*/ 470bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 471d2daff3dSHong Zhang { 472bc952696SBarry Smith PetscErrorCode ierr; 473bc952696SBarry Smith 474d2daff3dSHong Zhang PetscFunctionBegin; 475d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 476bc952696SBarry Smith if (!ts->trajectory) { 477bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 478972caf09SHong Zhang ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 479bc952696SBarry Smith } 480d2daff3dSHong Zhang PetscFunctionReturn(0); 481d2daff3dSHong Zhang } 482d2daff3dSHong Zhang 483a7a1495cSBarry Smith #undef __FUNCT__ 484a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian" 485a7a1495cSBarry Smith /*@ 486a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 487a7a1495cSBarry Smith set with TSSetRHSJacobian(). 488a7a1495cSBarry Smith 489a7a1495cSBarry Smith Collective on TS and Vec 490a7a1495cSBarry Smith 491a7a1495cSBarry Smith Input Parameters: 492316643e7SJed Brown + ts - the TS context 493a7a1495cSBarry Smith . t - current timestep 4940910c330SBarry Smith - U - input vector 495a7a1495cSBarry Smith 496a7a1495cSBarry Smith Output Parameters: 497a7a1495cSBarry Smith + A - Jacobian matrix 498a7a1495cSBarry Smith . B - optional preconditioning matrix 499a7a1495cSBarry Smith - flag - flag indicating matrix structure 500a7a1495cSBarry Smith 501a7a1495cSBarry Smith Notes: 502a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 503a7a1495cSBarry Smith is used internally within the nonlinear solvers. 504a7a1495cSBarry Smith 50594b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 506a7a1495cSBarry Smith flag parameter. 507a7a1495cSBarry Smith 508a7a1495cSBarry Smith Level: developer 509a7a1495cSBarry Smith 510a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 511a7a1495cSBarry Smith 51294b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 513a7a1495cSBarry Smith @*/ 514d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 515a7a1495cSBarry Smith { 516dfbe8321SBarry Smith PetscErrorCode ierr; 517270bf2e7SJed Brown PetscObjectState Ustate; 51824989b8cSPeter Brune DM dm; 519942e3340SBarry Smith DMTS tsdm; 52024989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 52124989b8cSPeter Brune void *ctx; 52224989b8cSPeter Brune TSIJacobian ijacobianfunc; 523b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 524a7a1495cSBarry Smith 525a7a1495cSBarry Smith PetscFunctionBegin; 5260700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5270910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5280910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 52924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 530942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 53124989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 5320298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 533b2df71adSDebojyoti Ghosh ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 53459e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 535b2df71adSDebojyoti Ghosh if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 5360e4ef248SJed Brown PetscFunctionReturn(0); 537f8ede8e7SJed Brown } 538d90be118SSean Farley 539ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 540d90be118SSean Farley 541e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 54294ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 54394ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 54494ab13aaSBarry Smith if (A != B) { 54594ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 54694ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 547e1244c69SJed Brown } 548e1244c69SJed Brown ts->rhsjacobian.shift = 0; 549e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 550e1244c69SJed Brown } 551e1244c69SJed Brown 55224989b8cSPeter Brune if (rhsjacobianfunc) { 5536cd88445SBarry Smith PetscBool missing; 55494ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 555a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 556d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 557a7a1495cSBarry Smith PetscStackPop; 55894ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 5596cd88445SBarry Smith if (A) { 5606cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 5616cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 5626cd88445SBarry Smith } 5636cd88445SBarry Smith if (B && B != A) { 5646cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 5656cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 5666cd88445SBarry Smith } 567ef66eb69SBarry Smith } else { 56894ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 56994ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 570ef66eb69SBarry Smith } 5710e4ef248SJed Brown ts->rhsjacobian.time = t; 5720910c330SBarry Smith ts->rhsjacobian.X = U; 57359e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 574a7a1495cSBarry Smith PetscFunctionReturn(0); 575a7a1495cSBarry Smith } 576a7a1495cSBarry Smith 5774a2ae208SSatish Balay #undef __FUNCT__ 5784a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 579316643e7SJed Brown /*@ 580d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 581d763cef2SBarry Smith 582316643e7SJed Brown Collective on TS and Vec 583316643e7SJed Brown 584316643e7SJed Brown Input Parameters: 585316643e7SJed Brown + ts - the TS context 586316643e7SJed Brown . t - current time 5870910c330SBarry Smith - U - state vector 588316643e7SJed Brown 589316643e7SJed Brown Output Parameter: 590316643e7SJed Brown . y - right hand side 591316643e7SJed Brown 592316643e7SJed Brown Note: 593316643e7SJed Brown Most users should not need to explicitly call this routine, as it 594316643e7SJed Brown is used internally within the nonlinear solvers. 595316643e7SJed Brown 596316643e7SJed Brown Level: developer 597316643e7SJed Brown 598316643e7SJed Brown .keywords: TS, compute 599316643e7SJed Brown 600316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 601316643e7SJed Brown @*/ 6020910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 603d763cef2SBarry Smith { 604dfbe8321SBarry Smith PetscErrorCode ierr; 60524989b8cSPeter Brune TSRHSFunction rhsfunction; 60624989b8cSPeter Brune TSIFunction ifunction; 60724989b8cSPeter Brune void *ctx; 60824989b8cSPeter Brune DM dm; 60924989b8cSPeter Brune 610d763cef2SBarry Smith PetscFunctionBegin; 6110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6120910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6130700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 61424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 61524989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 6160298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 617d763cef2SBarry Smith 618ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 619d763cef2SBarry Smith 6200910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 62124989b8cSPeter Brune if (rhsfunction) { 622d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 6230910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 624d763cef2SBarry Smith PetscStackPop; 625214bc6a2SJed Brown } else { 626214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 627b2cd27e8SJed Brown } 62844a41b28SSean Farley 6290910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 630d763cef2SBarry Smith PetscFunctionReturn(0); 631d763cef2SBarry Smith } 632d763cef2SBarry Smith 6334a2ae208SSatish Balay #undef __FUNCT__ 634ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 635ef20d060SBarry Smith /*@ 636ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 637ef20d060SBarry Smith 638ef20d060SBarry Smith Collective on TS and Vec 639ef20d060SBarry Smith 640ef20d060SBarry Smith Input Parameters: 641ef20d060SBarry Smith + ts - the TS context 642ef20d060SBarry Smith - t - current time 643ef20d060SBarry Smith 644ef20d060SBarry Smith Output Parameter: 6450910c330SBarry Smith . U - the solution 646ef20d060SBarry Smith 647ef20d060SBarry Smith Note: 648ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 649ef20d060SBarry Smith is used internally within the nonlinear solvers. 650ef20d060SBarry Smith 651ef20d060SBarry Smith Level: developer 652ef20d060SBarry Smith 653ef20d060SBarry Smith .keywords: TS, compute 654ef20d060SBarry Smith 655abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 656ef20d060SBarry Smith @*/ 6570910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 658ef20d060SBarry Smith { 659ef20d060SBarry Smith PetscErrorCode ierr; 660ef20d060SBarry Smith TSSolutionFunction solutionfunction; 661ef20d060SBarry Smith void *ctx; 662ef20d060SBarry Smith DM dm; 663ef20d060SBarry Smith 664ef20d060SBarry Smith PetscFunctionBegin; 665ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6660910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 667ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 668ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 669ef20d060SBarry Smith 670ef20d060SBarry Smith if (solutionfunction) { 6719b7cd975SBarry Smith PetscStackPush("TS user solution function"); 6720910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 673ef20d060SBarry Smith PetscStackPop; 674ef20d060SBarry Smith } 675ef20d060SBarry Smith PetscFunctionReturn(0); 676ef20d060SBarry Smith } 6779b7cd975SBarry Smith #undef __FUNCT__ 6789b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction" 6799b7cd975SBarry Smith /*@ 6809b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6819b7cd975SBarry Smith 6829b7cd975SBarry Smith Collective on TS and Vec 6839b7cd975SBarry Smith 6849b7cd975SBarry Smith Input Parameters: 6859b7cd975SBarry Smith + ts - the TS context 6869b7cd975SBarry Smith - t - current time 6879b7cd975SBarry Smith 6889b7cd975SBarry Smith Output Parameter: 6899b7cd975SBarry Smith . U - the function value 6909b7cd975SBarry Smith 6919b7cd975SBarry Smith Note: 6929b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 6939b7cd975SBarry Smith is used internally within the nonlinear solvers. 6949b7cd975SBarry Smith 6959b7cd975SBarry Smith Level: developer 6969b7cd975SBarry Smith 6979b7cd975SBarry Smith .keywords: TS, compute 6989b7cd975SBarry Smith 6999b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 7009b7cd975SBarry Smith @*/ 7019b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 7029b7cd975SBarry Smith { 7039b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 7049b7cd975SBarry Smith void *ctx; 7059b7cd975SBarry Smith DM dm; 7069b7cd975SBarry Smith 7079b7cd975SBarry Smith PetscFunctionBegin; 7089b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7099b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7109b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 7119b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 7129b7cd975SBarry Smith 7139b7cd975SBarry Smith if (forcing) { 7149b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 7159b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 7169b7cd975SBarry Smith PetscStackPop; 7179b7cd975SBarry Smith } 7189b7cd975SBarry Smith PetscFunctionReturn(0); 7199b7cd975SBarry Smith } 720ef20d060SBarry Smith 721ef20d060SBarry Smith #undef __FUNCT__ 722214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 723214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 724214bc6a2SJed Brown { 7252dd45cf8SJed Brown Vec F; 726214bc6a2SJed Brown PetscErrorCode ierr; 727214bc6a2SJed Brown 728214bc6a2SJed Brown PetscFunctionBegin; 7290298fd71SBarry Smith *Frhs = NULL; 7300298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 731214bc6a2SJed Brown if (!ts->Frhs) { 7322dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 733214bc6a2SJed Brown } 734214bc6a2SJed Brown *Frhs = ts->Frhs; 735214bc6a2SJed Brown PetscFunctionReturn(0); 736214bc6a2SJed Brown } 737214bc6a2SJed Brown 738214bc6a2SJed Brown #undef __FUNCT__ 739214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 740214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 741214bc6a2SJed Brown { 742214bc6a2SJed Brown Mat A,B; 7432dd45cf8SJed Brown PetscErrorCode ierr; 744214bc6a2SJed Brown 745214bc6a2SJed Brown PetscFunctionBegin; 746c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 747c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 7480298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 749214bc6a2SJed Brown if (Arhs) { 750214bc6a2SJed Brown if (!ts->Arhs) { 751214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 752214bc6a2SJed Brown } 753214bc6a2SJed Brown *Arhs = ts->Arhs; 754214bc6a2SJed Brown } 755214bc6a2SJed Brown if (Brhs) { 756214bc6a2SJed Brown if (!ts->Brhs) { 757bdb70873SJed Brown if (A != B) { 758214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 759bdb70873SJed Brown } else { 760bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 76151699248SLisandro Dalcin ts->Brhs = ts->Arhs; 762bdb70873SJed Brown } 763214bc6a2SJed Brown } 764214bc6a2SJed Brown *Brhs = ts->Brhs; 765214bc6a2SJed Brown } 766214bc6a2SJed Brown PetscFunctionReturn(0); 767214bc6a2SJed Brown } 768214bc6a2SJed Brown 769214bc6a2SJed Brown #undef __FUNCT__ 770316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 771316643e7SJed Brown /*@ 7720910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 773316643e7SJed Brown 774316643e7SJed Brown Collective on TS and Vec 775316643e7SJed Brown 776316643e7SJed Brown Input Parameters: 777316643e7SJed Brown + ts - the TS context 778316643e7SJed Brown . t - current time 7790910c330SBarry Smith . U - state vector 7800910c330SBarry Smith . Udot - time derivative of state vector 781214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 782316643e7SJed Brown 783316643e7SJed Brown Output Parameter: 784316643e7SJed Brown . Y - right hand side 785316643e7SJed Brown 786316643e7SJed Brown Note: 787316643e7SJed Brown Most users should not need to explicitly call this routine, as it 788316643e7SJed Brown is used internally within the nonlinear solvers. 789316643e7SJed Brown 790316643e7SJed Brown If the user did did not write their equations in implicit form, this 791316643e7SJed Brown function recasts them in implicit form. 792316643e7SJed Brown 793316643e7SJed Brown Level: developer 794316643e7SJed Brown 795316643e7SJed Brown .keywords: TS, compute 796316643e7SJed Brown 797316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 798316643e7SJed Brown @*/ 7990910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 800316643e7SJed Brown { 801316643e7SJed Brown PetscErrorCode ierr; 80224989b8cSPeter Brune TSIFunction ifunction; 80324989b8cSPeter Brune TSRHSFunction rhsfunction; 80424989b8cSPeter Brune void *ctx; 80524989b8cSPeter Brune DM dm; 806316643e7SJed Brown 807316643e7SJed Brown PetscFunctionBegin; 8080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8090910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8100910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 8110700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 812316643e7SJed Brown 81324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 81424989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 8150298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 81624989b8cSPeter Brune 817ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 818d90be118SSean Farley 8190910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 82024989b8cSPeter Brune if (ifunction) { 821316643e7SJed Brown PetscStackPush("TS user implicit function"); 8220910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 823316643e7SJed Brown PetscStackPop; 824214bc6a2SJed Brown } 825214bc6a2SJed Brown if (imex) { 82624989b8cSPeter Brune if (!ifunction) { 8270910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 8282dd45cf8SJed Brown } 82924989b8cSPeter Brune } else if (rhsfunction) { 83024989b8cSPeter Brune if (ifunction) { 831214bc6a2SJed Brown Vec Frhs; 832214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 8330910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 834214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 8352dd45cf8SJed Brown } else { 8360910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 8370910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 838316643e7SJed Brown } 8394a6899ffSJed Brown } 8400910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 841316643e7SJed Brown PetscFunctionReturn(0); 842316643e7SJed Brown } 843316643e7SJed Brown 844316643e7SJed Brown #undef __FUNCT__ 845316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 846316643e7SJed Brown /*@ 847316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 848316643e7SJed Brown 849316643e7SJed Brown Collective on TS and Vec 850316643e7SJed Brown 851316643e7SJed Brown Input 852316643e7SJed Brown Input Parameters: 853316643e7SJed Brown + ts - the TS context 854316643e7SJed Brown . t - current timestep 8550910c330SBarry Smith . U - state vector 8560910c330SBarry Smith . Udot - time derivative of state vector 857214bc6a2SJed Brown . shift - shift to apply, see note below 858214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 859316643e7SJed Brown 860316643e7SJed Brown Output Parameters: 861316643e7SJed Brown + A - Jacobian matrix 862316643e7SJed Brown . B - optional preconditioning matrix 863316643e7SJed Brown - flag - flag indicating matrix structure 864316643e7SJed Brown 865316643e7SJed Brown Notes: 8660910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 867316643e7SJed Brown 8680910c330SBarry Smith dF/dU + shift*dF/dUdot 869316643e7SJed Brown 870316643e7SJed Brown Most users should not need to explicitly call this routine, as it 871316643e7SJed Brown is used internally within the nonlinear solvers. 872316643e7SJed Brown 873316643e7SJed Brown Level: developer 874316643e7SJed Brown 875316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 876316643e7SJed Brown 877316643e7SJed Brown .seealso: TSSetIJacobian() 87863495f91SJed Brown @*/ 879d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 880316643e7SJed Brown { 881316643e7SJed Brown PetscErrorCode ierr; 88224989b8cSPeter Brune TSIJacobian ijacobian; 88324989b8cSPeter Brune TSRHSJacobian rhsjacobian; 88424989b8cSPeter Brune DM dm; 88524989b8cSPeter Brune void *ctx; 886316643e7SJed Brown 887316643e7SJed Brown PetscFunctionBegin; 8880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8890910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8900910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 891316643e7SJed Brown PetscValidPointer(A,6); 89294ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 893316643e7SJed Brown PetscValidPointer(B,7); 89494ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 89524989b8cSPeter Brune 89624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 89724989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 8980298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 89924989b8cSPeter Brune 900ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 901316643e7SJed Brown 90294ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 90324989b8cSPeter Brune if (ijacobian) { 9046cd88445SBarry Smith PetscBool missing; 905316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 906d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 907316643e7SJed Brown PetscStackPop; 9086cd88445SBarry Smith if (A) { 9096cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 9106cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 9116cd88445SBarry Smith } 9126cd88445SBarry Smith if (B && B != A) { 9136cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 9146cd88445SBarry Smith if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 9156cd88445SBarry Smith } 9164a6899ffSJed Brown } 917214bc6a2SJed Brown if (imex) { 918b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 9194c26be97Sstefano_zampini PetscBool assembled; 92094ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 9214c26be97Sstefano_zampini ierr = MatAssembled(A,&assembled);CHKERRQ(ierr); 9224c26be97Sstefano_zampini if (!assembled) { 9234c26be97Sstefano_zampini ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9244c26be97Sstefano_zampini ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9254c26be97Sstefano_zampini } 92694ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 92794ab13aaSBarry Smith if (A != B) { 92894ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 9294c26be97Sstefano_zampini ierr = MatAssembled(B,&assembled);CHKERRQ(ierr); 9304c26be97Sstefano_zampini if (!assembled) { 9314c26be97Sstefano_zampini ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9324c26be97Sstefano_zampini ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9334c26be97Sstefano_zampini } 93494ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 935214bc6a2SJed Brown } 936214bc6a2SJed Brown } 937214bc6a2SJed Brown } else { 938e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 939e1244c69SJed Brown if (rhsjacobian) { 940214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 941d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 942e1244c69SJed Brown } 94394ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 944e1244c69SJed Brown ts->rhsjacobian.scale = -1; 945e1244c69SJed Brown ts->rhsjacobian.shift = shift; 94694ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 94794ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 94894ab13aaSBarry Smith if (A != B) { 94994ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 95094ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 951316643e7SJed Brown } 952e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 953e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 954e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 95594ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 95694ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 95794ab13aaSBarry Smith if (A != B) { 95894ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 95994ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 960214bc6a2SJed Brown } 961316643e7SJed Brown } 96294ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 96394ab13aaSBarry Smith if (A != B) { 96494ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 965316643e7SJed Brown } 966316643e7SJed Brown } 967316643e7SJed Brown } 96894ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 969316643e7SJed Brown PetscFunctionReturn(0); 970316643e7SJed Brown } 971316643e7SJed Brown 972316643e7SJed Brown #undef __FUNCT__ 9734a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 974d763cef2SBarry Smith /*@C 975d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 976b5abc632SBarry Smith where U_t = G(t,u). 977d763cef2SBarry Smith 9783f9fe445SBarry Smith Logically Collective on TS 979d763cef2SBarry Smith 980d763cef2SBarry Smith Input Parameters: 981d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 9820298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 983d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 984d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 9850298fd71SBarry Smith function evaluation routine (may be NULL) 986d763cef2SBarry Smith 987d763cef2SBarry Smith Calling sequence of func: 98887828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 989d763cef2SBarry Smith 990d763cef2SBarry Smith + t - current timestep 991d763cef2SBarry Smith . u - input vector 992d763cef2SBarry Smith . F - function vector 993d763cef2SBarry Smith - ctx - [optional] user-defined function context 994d763cef2SBarry Smith 995d763cef2SBarry Smith Level: beginner 996d763cef2SBarry Smith 9972bbac0d3SBarry Smith Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 9982bbac0d3SBarry Smith 999d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1000d763cef2SBarry Smith 1001ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 1002d763cef2SBarry Smith @*/ 1003089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 1004d763cef2SBarry Smith { 1005089b2837SJed Brown PetscErrorCode ierr; 1006089b2837SJed Brown SNES snes; 10070298fd71SBarry Smith Vec ralloc = NULL; 100824989b8cSPeter Brune DM dm; 1009d763cef2SBarry Smith 1010089b2837SJed Brown PetscFunctionBegin; 10110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1012ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 101324989b8cSPeter Brune 101424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 101524989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 1016089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1017e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 1018e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 1019e856ceecSJed Brown r = ralloc; 1020e856ceecSJed Brown } 1021089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 1022e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1023d763cef2SBarry Smith PetscFunctionReturn(0); 1024d763cef2SBarry Smith } 1025d763cef2SBarry Smith 10264a2ae208SSatish Balay #undef __FUNCT__ 1027ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 1028ef20d060SBarry Smith /*@C 1029abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1030ef20d060SBarry Smith 1031ef20d060SBarry Smith Logically Collective on TS 1032ef20d060SBarry Smith 1033ef20d060SBarry Smith Input Parameters: 1034ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1035ef20d060SBarry Smith . f - routine for evaluating the solution 1036ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 10370298fd71SBarry Smith function evaluation routine (may be NULL) 1038ef20d060SBarry Smith 1039ef20d060SBarry Smith Calling sequence of func: 1040ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 1041ef20d060SBarry Smith 1042ef20d060SBarry Smith + t - current timestep 1043ef20d060SBarry Smith . u - output vector 1044ef20d060SBarry Smith - ctx - [optional] user-defined function context 1045ef20d060SBarry Smith 1046abd5a294SJed Brown Notes: 1047abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1048abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1049abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1050abd5a294SJed Brown 10514f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1052abd5a294SJed Brown 1053ef20d060SBarry Smith Level: beginner 1054ef20d060SBarry Smith 1055ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1056ef20d060SBarry Smith 10579b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 1058ef20d060SBarry Smith @*/ 1059ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1060ef20d060SBarry Smith { 1061ef20d060SBarry Smith PetscErrorCode ierr; 1062ef20d060SBarry Smith DM dm; 1063ef20d060SBarry Smith 1064ef20d060SBarry Smith PetscFunctionBegin; 1065ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1066ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1067ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 1068ef20d060SBarry Smith PetscFunctionReturn(0); 1069ef20d060SBarry Smith } 1070ef20d060SBarry Smith 1071ef20d060SBarry Smith #undef __FUNCT__ 10729b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction" 10739b7cd975SBarry Smith /*@C 10749b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 10759b7cd975SBarry Smith 10769b7cd975SBarry Smith Logically Collective on TS 10779b7cd975SBarry Smith 10789b7cd975SBarry Smith Input Parameters: 10799b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 10809b7cd975SBarry Smith . f - routine for evaluating the forcing function 10819b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 10820298fd71SBarry Smith function evaluation routine (may be NULL) 10839b7cd975SBarry Smith 10849b7cd975SBarry Smith Calling sequence of func: 10859b7cd975SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 10869b7cd975SBarry Smith 10879b7cd975SBarry Smith + t - current timestep 10889b7cd975SBarry Smith . u - output vector 10899b7cd975SBarry Smith - ctx - [optional] user-defined function context 10909b7cd975SBarry Smith 10919b7cd975SBarry Smith Notes: 10929b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 10939b7cd975SBarry Smith create closed-form solutions with a non-physical forcing term. 10949b7cd975SBarry Smith 10959b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 10969b7cd975SBarry Smith 10979b7cd975SBarry Smith Level: beginner 10989b7cd975SBarry Smith 10999b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 11009b7cd975SBarry Smith 11019b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 11029b7cd975SBarry Smith @*/ 1103d56366bfSLisandro Dalcin PetscErrorCode TSSetForcingFunction(TS ts,TSForcingFunction f,void *ctx) 11049b7cd975SBarry Smith { 11059b7cd975SBarry Smith PetscErrorCode ierr; 11069b7cd975SBarry Smith DM dm; 11079b7cd975SBarry Smith 11089b7cd975SBarry Smith PetscFunctionBegin; 11099b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11109b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 11119b7cd975SBarry Smith ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr); 11129b7cd975SBarry Smith PetscFunctionReturn(0); 11139b7cd975SBarry Smith } 11149b7cd975SBarry Smith 11159b7cd975SBarry Smith #undef __FUNCT__ 11164a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 1117d763cef2SBarry Smith /*@C 1118f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1119b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1120d763cef2SBarry Smith 11213f9fe445SBarry Smith Logically Collective on TS 1122d763cef2SBarry Smith 1123d763cef2SBarry Smith Input Parameters: 1124d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1125e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1126e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1127d763cef2SBarry Smith . f - the Jacobian evaluation routine 1128d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 11290298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1130d763cef2SBarry Smith 1131f7ab8db6SBarry Smith Calling sequence of f: 1132ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1133d763cef2SBarry Smith 1134d763cef2SBarry Smith + t - current timestep 1135d763cef2SBarry Smith . u - input vector 1136e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1137e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1138d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1139d763cef2SBarry Smith 11406cd88445SBarry Smith Notes: 11416cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 11426cd88445SBarry Smith 11436cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1144ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1145d763cef2SBarry Smith 1146d763cef2SBarry Smith Level: beginner 1147d763cef2SBarry Smith 1148d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1149d763cef2SBarry Smith 1150ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1151d763cef2SBarry Smith 1152d763cef2SBarry Smith @*/ 1153e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1154d763cef2SBarry Smith { 1155277b19d0SLisandro Dalcin PetscErrorCode ierr; 1156089b2837SJed Brown SNES snes; 115724989b8cSPeter Brune DM dm; 115824989b8cSPeter Brune TSIJacobian ijacobian; 1159277b19d0SLisandro Dalcin 1160d763cef2SBarry Smith PetscFunctionBegin; 11610700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1162e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1163e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1164e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1165e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1166d763cef2SBarry Smith 116724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 116824989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1169e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1170e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1171e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1172e1244c69SJed Brown } 11730298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1174089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11755f659677SPeter Brune if (!ijacobian) { 1176e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 11770e4ef248SJed Brown } 1178e5d3d808SBarry Smith if (Amat) { 1179e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 11800e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1181e5d3d808SBarry Smith ts->Arhs = Amat; 11820e4ef248SJed Brown } 1183e5d3d808SBarry Smith if (Pmat) { 1184e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 11850e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1186e5d3d808SBarry Smith ts->Brhs = Pmat; 11870e4ef248SJed Brown } 1188d763cef2SBarry Smith PetscFunctionReturn(0); 1189d763cef2SBarry Smith } 1190d763cef2SBarry Smith 1191316643e7SJed Brown 1192316643e7SJed Brown #undef __FUNCT__ 1193316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 1194316643e7SJed Brown /*@C 1195b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1196316643e7SJed Brown 11973f9fe445SBarry Smith Logically Collective on TS 1198316643e7SJed Brown 1199316643e7SJed Brown Input Parameters: 1200316643e7SJed Brown + ts - the TS context obtained from TSCreate() 12010298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1202316643e7SJed Brown . f - the function evaluation routine 12030298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1204316643e7SJed Brown 1205316643e7SJed Brown Calling sequence of f: 1206316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1207316643e7SJed Brown 1208316643e7SJed Brown + t - time at step/stage being solved 1209316643e7SJed Brown . u - state vector 1210316643e7SJed Brown . u_t - time derivative of state vector 1211316643e7SJed Brown . F - function vector 1212316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1213316643e7SJed Brown 1214316643e7SJed Brown Important: 12152bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1216316643e7SJed Brown 1217316643e7SJed Brown Level: beginner 1218316643e7SJed Brown 1219316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1220316643e7SJed Brown 1221d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1222316643e7SJed Brown @*/ 122351699248SLisandro Dalcin PetscErrorCode TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx) 1224316643e7SJed Brown { 1225089b2837SJed Brown PetscErrorCode ierr; 1226089b2837SJed Brown SNES snes; 122751699248SLisandro Dalcin Vec ralloc = NULL; 122824989b8cSPeter Brune DM dm; 1229316643e7SJed Brown 1230316643e7SJed Brown PetscFunctionBegin; 12310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 123251699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 123324989b8cSPeter Brune 123424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 123524989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 123624989b8cSPeter Brune 1237089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 123851699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 123951699248SLisandro Dalcin ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 124051699248SLisandro Dalcin r = ralloc; 1241e856ceecSJed Brown } 124251699248SLisandro Dalcin ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 124351699248SLisandro Dalcin ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1244089b2837SJed Brown PetscFunctionReturn(0); 1245089b2837SJed Brown } 1246089b2837SJed Brown 1247089b2837SJed Brown #undef __FUNCT__ 1248089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 1249089b2837SJed Brown /*@C 1250089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1251089b2837SJed Brown 1252089b2837SJed Brown Not Collective 1253089b2837SJed Brown 1254089b2837SJed Brown Input Parameter: 1255089b2837SJed Brown . ts - the TS context 1256089b2837SJed Brown 1257089b2837SJed Brown Output Parameter: 12580298fd71SBarry Smith + r - vector to hold residual (or NULL) 12590298fd71SBarry Smith . func - the function to compute residual (or NULL) 12600298fd71SBarry Smith - ctx - the function context (or NULL) 1261089b2837SJed Brown 1262089b2837SJed Brown Level: advanced 1263089b2837SJed Brown 1264089b2837SJed Brown .keywords: TS, nonlinear, get, function 1265089b2837SJed Brown 1266089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1267089b2837SJed Brown @*/ 1268089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1269089b2837SJed Brown { 1270089b2837SJed Brown PetscErrorCode ierr; 1271089b2837SJed Brown SNES snes; 127224989b8cSPeter Brune DM dm; 1273089b2837SJed Brown 1274089b2837SJed Brown PetscFunctionBegin; 1275089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1276089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12770298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 127824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 127924989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1280089b2837SJed Brown PetscFunctionReturn(0); 1281089b2837SJed Brown } 1282089b2837SJed Brown 1283089b2837SJed Brown #undef __FUNCT__ 1284089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 1285089b2837SJed Brown /*@C 1286089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1287089b2837SJed Brown 1288089b2837SJed Brown Not Collective 1289089b2837SJed Brown 1290089b2837SJed Brown Input Parameter: 1291089b2837SJed Brown . ts - the TS context 1292089b2837SJed Brown 1293089b2837SJed Brown Output Parameter: 12940298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 12950298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 12960298fd71SBarry Smith - ctx - the function context (or NULL) 1297089b2837SJed Brown 1298089b2837SJed Brown Level: advanced 1299089b2837SJed Brown 1300089b2837SJed Brown .keywords: TS, nonlinear, get, function 1301089b2837SJed Brown 13022bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1303089b2837SJed Brown @*/ 1304089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1305089b2837SJed Brown { 1306089b2837SJed Brown PetscErrorCode ierr; 1307089b2837SJed Brown SNES snes; 130824989b8cSPeter Brune DM dm; 1309089b2837SJed Brown 1310089b2837SJed Brown PetscFunctionBegin; 1311089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1312089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13130298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 131424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 131524989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1316316643e7SJed Brown PetscFunctionReturn(0); 1317316643e7SJed Brown } 1318316643e7SJed Brown 1319316643e7SJed Brown #undef __FUNCT__ 1320316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 1321316643e7SJed Brown /*@C 1322a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1323ae8867d6SBarry Smith provided with TSSetIFunction(). 1324316643e7SJed Brown 13253f9fe445SBarry Smith Logically Collective on TS 1326316643e7SJed Brown 1327316643e7SJed Brown Input Parameters: 1328316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1329e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1330e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1331316643e7SJed Brown . f - the Jacobian evaluation routine 13320298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1333316643e7SJed Brown 1334316643e7SJed Brown Calling sequence of f: 1335ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1336316643e7SJed Brown 1337316643e7SJed Brown + t - time at step/stage being solved 13381b4a444bSJed Brown . U - state vector 13391b4a444bSJed Brown . U_t - time derivative of state vector 1340316643e7SJed Brown . a - shift 1341e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1342e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1343316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1344316643e7SJed Brown 1345316643e7SJed Brown Notes: 1346e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1347316643e7SJed Brown 1348895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1349895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1350895c21f2SBarry Smith 1351a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1352b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1353a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1354a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1355a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1356a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1357a4f0a591SBarry Smith 13586cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13596cd88445SBarry Smith 13606cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1361ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1362ca5f011dSBarry Smith 1363316643e7SJed Brown Level: beginner 1364316643e7SJed Brown 1365316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1366316643e7SJed Brown 1367ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1368316643e7SJed Brown 1369316643e7SJed Brown @*/ 1370e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1371316643e7SJed Brown { 1372316643e7SJed Brown PetscErrorCode ierr; 1373089b2837SJed Brown SNES snes; 137424989b8cSPeter Brune DM dm; 1375316643e7SJed Brown 1376316643e7SJed Brown PetscFunctionBegin; 13770700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1378e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1379e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1380e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1381e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 138224989b8cSPeter Brune 138324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 138424989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 138524989b8cSPeter Brune 1386089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1387e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1388316643e7SJed Brown PetscFunctionReturn(0); 1389316643e7SJed Brown } 1390316643e7SJed Brown 13914a2ae208SSatish Balay #undef __FUNCT__ 1392e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse" 1393e1244c69SJed Brown /*@ 1394e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1395e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1396e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1397e1244c69SJed Brown not been changed by the TS. 1398e1244c69SJed Brown 1399e1244c69SJed Brown Logically Collective 1400e1244c69SJed Brown 1401e1244c69SJed Brown Input Arguments: 1402e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1403e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1404e1244c69SJed Brown 1405e1244c69SJed Brown Level: intermediate 1406e1244c69SJed Brown 1407e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1408e1244c69SJed Brown @*/ 1409e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1410e1244c69SJed Brown { 1411e1244c69SJed Brown PetscFunctionBegin; 1412e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1413e1244c69SJed Brown PetscFunctionReturn(0); 1414e1244c69SJed Brown } 1415e1244c69SJed Brown 1416e1244c69SJed Brown #undef __FUNCT__ 1417efe9872eSLisandro Dalcin #define __FUNCT__ "TSSetI2Function" 1418efe9872eSLisandro Dalcin /*@C 1419efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1420efe9872eSLisandro Dalcin 1421efe9872eSLisandro Dalcin Logically Collective on TS 1422efe9872eSLisandro Dalcin 1423efe9872eSLisandro Dalcin Input Parameters: 1424efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1425efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1426efe9872eSLisandro Dalcin . fun - the function evaluation routine 1427efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1428efe9872eSLisandro Dalcin 1429efe9872eSLisandro Dalcin Calling sequence of fun: 1430efe9872eSLisandro Dalcin $ fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1431efe9872eSLisandro Dalcin 1432efe9872eSLisandro Dalcin + t - time at step/stage being solved 1433efe9872eSLisandro Dalcin . U - state vector 1434efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1435efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1436efe9872eSLisandro Dalcin . F - function vector 1437efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1438efe9872eSLisandro Dalcin 1439efe9872eSLisandro Dalcin Level: beginner 1440efe9872eSLisandro Dalcin 1441efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function 1442efe9872eSLisandro Dalcin 1443efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1444efe9872eSLisandro Dalcin @*/ 1445efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx) 1446efe9872eSLisandro Dalcin { 1447efe9872eSLisandro Dalcin DM dm; 1448efe9872eSLisandro Dalcin PetscErrorCode ierr; 1449efe9872eSLisandro Dalcin 1450efe9872eSLisandro Dalcin PetscFunctionBegin; 1451efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1452efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2); 1453efe9872eSLisandro Dalcin ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr); 1454efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1455efe9872eSLisandro Dalcin ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1456efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1457efe9872eSLisandro Dalcin } 1458efe9872eSLisandro Dalcin 1459efe9872eSLisandro Dalcin #undef __FUNCT__ 1460efe9872eSLisandro Dalcin #define __FUNCT__ "TSGetI2Function" 1461efe9872eSLisandro Dalcin /*@C 1462efe9872eSLisandro Dalcin TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1463efe9872eSLisandro Dalcin 1464efe9872eSLisandro Dalcin Not Collective 1465efe9872eSLisandro Dalcin 1466efe9872eSLisandro Dalcin Input Parameter: 1467efe9872eSLisandro Dalcin . ts - the TS context 1468efe9872eSLisandro Dalcin 1469efe9872eSLisandro Dalcin Output Parameter: 1470efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1471efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1472efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1473efe9872eSLisandro Dalcin 1474efe9872eSLisandro Dalcin Level: advanced 1475efe9872eSLisandro Dalcin 1476efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function 1477efe9872eSLisandro Dalcin 1478efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction() 1479efe9872eSLisandro Dalcin @*/ 1480efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx) 1481efe9872eSLisandro Dalcin { 1482efe9872eSLisandro Dalcin PetscErrorCode ierr; 1483efe9872eSLisandro Dalcin SNES snes; 1484efe9872eSLisandro Dalcin DM dm; 1485efe9872eSLisandro Dalcin 1486efe9872eSLisandro Dalcin PetscFunctionBegin; 1487efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1488efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1489efe9872eSLisandro Dalcin ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 1490efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1491efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1492efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1493efe9872eSLisandro Dalcin } 1494efe9872eSLisandro Dalcin 1495efe9872eSLisandro Dalcin #undef __FUNCT__ 1496efe9872eSLisandro Dalcin #define __FUNCT__ "TSSetI2Jacobian" 1497efe9872eSLisandro Dalcin /*@C 1498bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1499efe9872eSLisandro Dalcin where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function(). 1500efe9872eSLisandro Dalcin 1501efe9872eSLisandro Dalcin Logically Collective on TS 1502efe9872eSLisandro Dalcin 1503efe9872eSLisandro Dalcin Input Parameters: 1504efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1505efe9872eSLisandro Dalcin . J - Jacobian matrix 1506efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1507efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1508efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1509efe9872eSLisandro Dalcin 1510efe9872eSLisandro Dalcin Calling sequence of jac: 1511bc77d74cSLisandro Dalcin $ jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx); 1512efe9872eSLisandro Dalcin 1513efe9872eSLisandro Dalcin + t - time at step/stage being solved 1514efe9872eSLisandro Dalcin . U - state vector 1515efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1516efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1517efe9872eSLisandro Dalcin . v - shift for U_t 1518efe9872eSLisandro Dalcin . a - shift for U_tt 1519efe9872eSLisandro 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 1520efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1521efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1522efe9872eSLisandro Dalcin 1523efe9872eSLisandro Dalcin Notes: 1524efe9872eSLisandro Dalcin The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve. 1525efe9872eSLisandro Dalcin 1526efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1527efe9872eSLisandro 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. 1528efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1529bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1530efe9872eSLisandro Dalcin 1531efe9872eSLisandro Dalcin Level: beginner 1532efe9872eSLisandro Dalcin 1533efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian 1534efe9872eSLisandro Dalcin 1535efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1536efe9872eSLisandro Dalcin @*/ 1537efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx) 1538efe9872eSLisandro Dalcin { 1539efe9872eSLisandro Dalcin DM dm; 1540efe9872eSLisandro Dalcin PetscErrorCode ierr; 1541efe9872eSLisandro Dalcin 1542efe9872eSLisandro Dalcin PetscFunctionBegin; 1543efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1544efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2); 1545efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3); 1546efe9872eSLisandro Dalcin ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr); 1547efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1548efe9872eSLisandro Dalcin ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1549efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1550efe9872eSLisandro Dalcin } 1551efe9872eSLisandro Dalcin 1552efe9872eSLisandro Dalcin #undef __FUNCT__ 1553efe9872eSLisandro Dalcin #define __FUNCT__ "TSGetI2Jacobian" 1554efe9872eSLisandro Dalcin /*@C 1555efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1556efe9872eSLisandro Dalcin 1557efe9872eSLisandro Dalcin Not Collective, but parallel objects are returned if TS is parallel 1558efe9872eSLisandro Dalcin 1559efe9872eSLisandro Dalcin Input Parameter: 1560efe9872eSLisandro Dalcin . ts - The TS context obtained from TSCreate() 1561efe9872eSLisandro Dalcin 1562efe9872eSLisandro Dalcin Output Parameters: 1563efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1564efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1565efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1566efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1567efe9872eSLisandro Dalcin 1568efe9872eSLisandro Dalcin Notes: You can pass in NULL for any return argument you do not need. 1569efe9872eSLisandro Dalcin 1570efe9872eSLisandro Dalcin Level: advanced 1571efe9872eSLisandro Dalcin 1572efe9872eSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 1573efe9872eSLisandro Dalcin 1574efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian 1575efe9872eSLisandro Dalcin @*/ 1576efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx) 1577efe9872eSLisandro Dalcin { 1578efe9872eSLisandro Dalcin PetscErrorCode ierr; 1579efe9872eSLisandro Dalcin SNES snes; 1580efe9872eSLisandro Dalcin DM dm; 1581efe9872eSLisandro Dalcin 1582efe9872eSLisandro Dalcin PetscFunctionBegin; 1583efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1584efe9872eSLisandro Dalcin ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 1585efe9872eSLisandro Dalcin ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr); 1586efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1587efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1588efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1589efe9872eSLisandro Dalcin } 1590efe9872eSLisandro Dalcin 1591efe9872eSLisandro Dalcin #undef __FUNCT__ 1592efe9872eSLisandro Dalcin #define __FUNCT__ "TSComputeI2Function" 1593efe9872eSLisandro Dalcin /*@ 1594efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1595efe9872eSLisandro Dalcin 1596efe9872eSLisandro Dalcin Collective on TS and Vec 1597efe9872eSLisandro Dalcin 1598efe9872eSLisandro Dalcin Input Parameters: 1599efe9872eSLisandro Dalcin + ts - the TS context 1600efe9872eSLisandro Dalcin . t - current time 1601efe9872eSLisandro Dalcin . U - state vector 1602efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1603efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1604efe9872eSLisandro Dalcin 1605efe9872eSLisandro Dalcin Output Parameter: 1606efe9872eSLisandro Dalcin . F - the residual vector 1607efe9872eSLisandro Dalcin 1608efe9872eSLisandro Dalcin Note: 1609efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1610efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1611efe9872eSLisandro Dalcin 1612efe9872eSLisandro Dalcin Level: developer 1613efe9872eSLisandro Dalcin 1614efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector 1615efe9872eSLisandro Dalcin 1616efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1617efe9872eSLisandro Dalcin @*/ 1618efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F) 1619efe9872eSLisandro Dalcin { 1620efe9872eSLisandro Dalcin DM dm; 1621efe9872eSLisandro Dalcin TSI2Function I2Function; 1622efe9872eSLisandro Dalcin void *ctx; 1623efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1624efe9872eSLisandro Dalcin PetscErrorCode ierr; 1625efe9872eSLisandro Dalcin 1626efe9872eSLisandro Dalcin PetscFunctionBegin; 1627efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1628efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1629efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1630efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1631efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F,VEC_CLASSID,6); 1632efe9872eSLisandro Dalcin 1633efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1634efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr); 1635efe9872eSLisandro Dalcin ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 1636efe9872eSLisandro Dalcin 1637efe9872eSLisandro Dalcin if (!I2Function) { 1638efe9872eSLisandro Dalcin ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr); 1639efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1640efe9872eSLisandro Dalcin } 1641efe9872eSLisandro Dalcin 1642efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1643efe9872eSLisandro Dalcin 1644efe9872eSLisandro Dalcin PetscStackPush("TS user implicit function"); 1645efe9872eSLisandro Dalcin ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr); 1646efe9872eSLisandro Dalcin PetscStackPop; 1647efe9872eSLisandro Dalcin 1648efe9872eSLisandro Dalcin if (rhsfunction) { 1649efe9872eSLisandro Dalcin Vec Frhs; 1650efe9872eSLisandro Dalcin ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 1651efe9872eSLisandro Dalcin ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 1652efe9872eSLisandro Dalcin ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr); 1653efe9872eSLisandro Dalcin } 1654efe9872eSLisandro Dalcin 1655efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1656efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1657efe9872eSLisandro Dalcin } 1658efe9872eSLisandro Dalcin 1659efe9872eSLisandro Dalcin #undef __FUNCT__ 1660efe9872eSLisandro Dalcin #define __FUNCT__ "TSComputeI2Jacobian" 1661efe9872eSLisandro Dalcin /*@ 1662efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1663efe9872eSLisandro Dalcin 1664efe9872eSLisandro Dalcin Collective on TS and Vec 1665efe9872eSLisandro Dalcin 1666efe9872eSLisandro Dalcin Input Parameters: 1667efe9872eSLisandro Dalcin + ts - the TS context 1668efe9872eSLisandro Dalcin . t - current timestep 1669efe9872eSLisandro Dalcin . U - state vector 1670efe9872eSLisandro Dalcin . V - time derivative of state vector 1671efe9872eSLisandro Dalcin . A - second time derivative of state vector 1672efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1673efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1674efe9872eSLisandro Dalcin 1675efe9872eSLisandro Dalcin Output Parameters: 1676efe9872eSLisandro Dalcin + J - Jacobian matrix 1677efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1678efe9872eSLisandro Dalcin 1679efe9872eSLisandro Dalcin Notes: 1680efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1681efe9872eSLisandro Dalcin 1682efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1683efe9872eSLisandro Dalcin 1684efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1685efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1686efe9872eSLisandro Dalcin 1687efe9872eSLisandro Dalcin Level: developer 1688efe9872eSLisandro Dalcin 1689efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix 1690efe9872eSLisandro Dalcin 1691efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1692efe9872eSLisandro Dalcin @*/ 1693efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P) 1694efe9872eSLisandro Dalcin { 1695efe9872eSLisandro Dalcin DM dm; 1696efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1697efe9872eSLisandro Dalcin void *ctx; 1698efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1699efe9872eSLisandro Dalcin PetscErrorCode ierr; 1700efe9872eSLisandro Dalcin 1701efe9872eSLisandro Dalcin PetscFunctionBegin; 1702efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1703efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1704efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1705efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1706efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J,MAT_CLASSID,8); 1707efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P,MAT_CLASSID,9); 1708efe9872eSLisandro Dalcin 1709efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1710efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr); 1711efe9872eSLisandro Dalcin ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 1712efe9872eSLisandro Dalcin 1713efe9872eSLisandro Dalcin if (!I2Jacobian) { 1714efe9872eSLisandro Dalcin ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr); 1715efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1716efe9872eSLisandro Dalcin } 1717efe9872eSLisandro Dalcin 1718efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1719efe9872eSLisandro Dalcin 1720efe9872eSLisandro Dalcin PetscStackPush("TS user implicit Jacobian"); 1721efe9872eSLisandro Dalcin ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr); 1722efe9872eSLisandro Dalcin PetscStackPop; 1723efe9872eSLisandro Dalcin 1724efe9872eSLisandro Dalcin if (rhsjacobian) { 1725efe9872eSLisandro Dalcin Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 1726efe9872eSLisandro Dalcin ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr); 1727efe9872eSLisandro Dalcin ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr); 1728efe9872eSLisandro Dalcin ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr); 1729efe9872eSLisandro Dalcin if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);} 1730efe9872eSLisandro Dalcin } 1731efe9872eSLisandro Dalcin 1732efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1733efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1734efe9872eSLisandro Dalcin } 1735efe9872eSLisandro Dalcin 1736efe9872eSLisandro Dalcin #undef __FUNCT__ 1737efe9872eSLisandro Dalcin #define __FUNCT__ "TS2SetSolution" 1738efe9872eSLisandro Dalcin /*@ 1739efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1740efe9872eSLisandro Dalcin for use by the TS routines handling second order equations. 1741efe9872eSLisandro Dalcin 1742efe9872eSLisandro Dalcin Logically Collective on TS and Vec 1743efe9872eSLisandro Dalcin 1744efe9872eSLisandro Dalcin Input Parameters: 1745efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1746efe9872eSLisandro Dalcin . u - the solution vector 1747efe9872eSLisandro Dalcin - v - the time derivative vector 1748efe9872eSLisandro Dalcin 1749efe9872eSLisandro Dalcin Level: beginner 1750efe9872eSLisandro Dalcin 1751efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions 1752efe9872eSLisandro Dalcin @*/ 1753efe9872eSLisandro Dalcin PetscErrorCode TS2SetSolution(TS ts,Vec u,Vec v) 1754efe9872eSLisandro Dalcin { 1755efe9872eSLisandro Dalcin PetscErrorCode ierr; 1756efe9872eSLisandro Dalcin 1757efe9872eSLisandro Dalcin PetscFunctionBegin; 1758efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1759efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1760efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v,VEC_CLASSID,3); 1761efe9872eSLisandro Dalcin ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 1762efe9872eSLisandro Dalcin ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr); 1763efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 1764efe9872eSLisandro Dalcin ts->vec_dot = v; 1765efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1766efe9872eSLisandro Dalcin } 1767efe9872eSLisandro Dalcin 1768efe9872eSLisandro Dalcin #undef __FUNCT__ 1769efe9872eSLisandro Dalcin #define __FUNCT__ "TS2GetSolution" 1770efe9872eSLisandro Dalcin /*@ 1771efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 1772efe9872eSLisandro Dalcin for second order equations. It is valid to call this routine inside the function 1773efe9872eSLisandro Dalcin that you are evaluating in order to move to the new timestep. This vector not 1774efe9872eSLisandro Dalcin changed until the solution at the next timestep has been calculated. 1775efe9872eSLisandro Dalcin 1776efe9872eSLisandro Dalcin Not Collective, but Vec returned is parallel if TS is parallel 1777efe9872eSLisandro Dalcin 1778efe9872eSLisandro Dalcin Input Parameter: 1779efe9872eSLisandro Dalcin . ts - the TS context obtained from TSCreate() 1780efe9872eSLisandro Dalcin 1781efe9872eSLisandro Dalcin Output Parameter: 1782efe9872eSLisandro Dalcin + u - the vector containing the solution 1783efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1784efe9872eSLisandro Dalcin 1785efe9872eSLisandro Dalcin Level: intermediate 1786efe9872eSLisandro Dalcin 1787efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime() 1788efe9872eSLisandro Dalcin 1789efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution 1790efe9872eSLisandro Dalcin @*/ 1791efe9872eSLisandro Dalcin PetscErrorCode TS2GetSolution(TS ts,Vec *u,Vec *v) 1792efe9872eSLisandro Dalcin { 1793efe9872eSLisandro Dalcin PetscFunctionBegin; 1794efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1795efe9872eSLisandro Dalcin if (u) PetscValidPointer(u,2); 1796efe9872eSLisandro Dalcin if (v) PetscValidPointer(v,3); 1797efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1798efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 1799efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1800efe9872eSLisandro Dalcin } 1801efe9872eSLisandro Dalcin 1802efe9872eSLisandro Dalcin #undef __FUNCT__ 180355849f57SBarry Smith #define __FUNCT__ "TSLoad" 180455849f57SBarry Smith /*@C 180555849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 180655849f57SBarry Smith 180755849f57SBarry Smith Collective on PetscViewer 180855849f57SBarry Smith 180955849f57SBarry Smith Input Parameters: 181055849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 181155849f57SBarry Smith some related function before a call to TSLoad(). 181255849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 181355849f57SBarry Smith 181455849f57SBarry Smith Level: intermediate 181555849f57SBarry Smith 181655849f57SBarry Smith Notes: 181755849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 181855849f57SBarry Smith 181955849f57SBarry Smith Notes for advanced users: 182055849f57SBarry Smith Most users should not need to know the details of the binary storage 182155849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 182255849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 182355849f57SBarry Smith format is 182455849f57SBarry Smith .vb 182555849f57SBarry Smith has not yet been determined 182655849f57SBarry Smith .ve 182755849f57SBarry Smith 182855849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 182955849f57SBarry Smith @*/ 1830f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 183155849f57SBarry Smith { 183255849f57SBarry Smith PetscErrorCode ierr; 183355849f57SBarry Smith PetscBool isbinary; 183455849f57SBarry Smith PetscInt classid; 183555849f57SBarry Smith char type[256]; 18362d53ad75SBarry Smith DMTS sdm; 1837ad6bc421SBarry Smith DM dm; 183855849f57SBarry Smith 183955849f57SBarry Smith PetscFunctionBegin; 1840f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 184155849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 184255849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 184355849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 184455849f57SBarry Smith 1845060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1846ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1847060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1848f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1849f2c2a1b9SBarry Smith if (ts->ops->load) { 1850f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1851f2c2a1b9SBarry Smith } 1852ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1853ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1854ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1855f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1856f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 18572d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 18582d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 185955849f57SBarry Smith PetscFunctionReturn(0); 186055849f57SBarry Smith } 186155849f57SBarry Smith 18629804daf3SBarry Smith #include <petscdraw.h> 1863e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1864e04113cfSBarry Smith #include <petscviewersaws.h> 1865f05ece33SBarry Smith #endif 186655849f57SBarry Smith #undef __FUNCT__ 18674a2ae208SSatish Balay #define __FUNCT__ "TSView" 18687e2c5f70SBarry Smith /*@C 1869d763cef2SBarry Smith TSView - Prints the TS data structure. 1870d763cef2SBarry Smith 18714c49b128SBarry Smith Collective on TS 1872d763cef2SBarry Smith 1873d763cef2SBarry Smith Input Parameters: 1874d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1875d763cef2SBarry Smith - viewer - visualization context 1876d763cef2SBarry Smith 1877d763cef2SBarry Smith Options Database Key: 1878d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1879d763cef2SBarry Smith 1880d763cef2SBarry Smith Notes: 1881d763cef2SBarry Smith The available visualization contexts include 1882b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1883b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1884d763cef2SBarry Smith output where only the first processor opens 1885d763cef2SBarry Smith the file. All other processors send their 1886d763cef2SBarry Smith data to the first processor to print. 1887d763cef2SBarry Smith 1888d763cef2SBarry Smith The user can open an alternative visualization context with 1889b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1890d763cef2SBarry Smith 1891d763cef2SBarry Smith Level: beginner 1892d763cef2SBarry Smith 1893d763cef2SBarry Smith .keywords: TS, timestep, view 1894d763cef2SBarry Smith 1895b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1896d763cef2SBarry Smith @*/ 18977087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1898d763cef2SBarry Smith { 1899dfbe8321SBarry Smith PetscErrorCode ierr; 190019fd82e9SBarry Smith TSType type; 19012b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 19022d53ad75SBarry Smith DMTS sdm; 1903e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1904536b137fSBarry Smith PetscBool issaws; 1905f05ece33SBarry Smith #endif 1906d763cef2SBarry Smith 1907d763cef2SBarry Smith PetscFunctionBegin; 19080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 19093050cee2SBarry Smith if (!viewer) { 1910ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 19113050cee2SBarry Smith } 19120700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1913c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1914fd16b177SBarry Smith 1915251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1916251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 191755849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 19182b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1919e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1920536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1921f05ece33SBarry Smith #endif 192232077d6dSBarry Smith if (iascii) { 1923dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 192477431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 19257c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1926d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 19275ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1928c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1929d763cef2SBarry Smith } 19305ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1931193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 19322d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19332d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1934d52bd9f3SBarry Smith if (ts->ops->view) { 1935d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1936d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1937d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1938d52bd9f3SBarry Smith } 19390f5bd95cSBarry Smith } else if (isstring) { 1940a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1941b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 194255849f57SBarry Smith } else if (isbinary) { 194355849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 194455849f57SBarry Smith MPI_Comm comm; 194555849f57SBarry Smith PetscMPIInt rank; 194655849f57SBarry Smith char type[256]; 194755849f57SBarry Smith 194855849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 194955849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 195055849f57SBarry Smith if (!rank) { 195155849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 195255849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 195355849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 195455849f57SBarry Smith } 195555849f57SBarry Smith if (ts->ops->view) { 195655849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 195755849f57SBarry Smith } 1958f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1959f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 19602d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19612d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 19622b0a91c0SBarry Smith } else if (isdraw) { 19632b0a91c0SBarry Smith PetscDraw draw; 19642b0a91c0SBarry Smith char str[36]; 196589fd9fafSBarry Smith PetscReal x,y,bottom,h; 19662b0a91c0SBarry Smith 19672b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 19682b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 19692b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 19702b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 197151fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 197289fd9fafSBarry Smith bottom = y - h; 19732b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 19742b0a91c0SBarry Smith if (ts->ops->view) { 19752b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 19762b0a91c0SBarry Smith } 19772b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1978e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1979536b137fSBarry Smith } else if (issaws) { 1980d45a07a7SBarry Smith PetscMPIInt rank; 19812657e9d9SBarry Smith const char *name; 19822657e9d9SBarry Smith 19832657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1984d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1985d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1986d45a07a7SBarry Smith char dir[1024]; 1987d45a07a7SBarry Smith 1988e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1989a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 19902657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 19912657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 19922657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1993d763cef2SBarry Smith } 19940acecf5bSBarry Smith if (ts->ops->view) { 19950acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 19960acecf5bSBarry Smith } 1997f05ece33SBarry Smith #endif 1998f05ece33SBarry Smith } 1999f05ece33SBarry Smith 2000b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2001251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 2002b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2003d763cef2SBarry Smith PetscFunctionReturn(0); 2004d763cef2SBarry Smith } 2005d763cef2SBarry Smith 2006d763cef2SBarry Smith 20074a2ae208SSatish Balay #undef __FUNCT__ 20084a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 2009b07ff414SBarry Smith /*@ 2010d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2011d763cef2SBarry Smith the timesteppers. 2012d763cef2SBarry Smith 20133f9fe445SBarry Smith Logically Collective on TS 2014d763cef2SBarry Smith 2015d763cef2SBarry Smith Input Parameters: 2016d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2017d763cef2SBarry Smith - usrP - optional user context 2018d763cef2SBarry Smith 2019daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 2020daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2021daf670e6SBarry Smith 2022d763cef2SBarry Smith Level: intermediate 2023d763cef2SBarry Smith 2024d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 2025d763cef2SBarry Smith 2026d763cef2SBarry Smith .seealso: TSGetApplicationContext() 2027d763cef2SBarry Smith @*/ 20287087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 2029d763cef2SBarry Smith { 2030d763cef2SBarry Smith PetscFunctionBegin; 20310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2032d763cef2SBarry Smith ts->user = usrP; 2033d763cef2SBarry Smith PetscFunctionReturn(0); 2034d763cef2SBarry Smith } 2035d763cef2SBarry Smith 20364a2ae208SSatish Balay #undef __FUNCT__ 20374a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 2038b07ff414SBarry Smith /*@ 2039d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2040d763cef2SBarry Smith timestepper. 2041d763cef2SBarry Smith 2042d763cef2SBarry Smith Not Collective 2043d763cef2SBarry Smith 2044d763cef2SBarry Smith Input Parameter: 2045d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2046d763cef2SBarry Smith 2047d763cef2SBarry Smith Output Parameter: 2048d763cef2SBarry Smith . usrP - user context 2049d763cef2SBarry Smith 2050daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 2051daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2052daf670e6SBarry Smith 2053d763cef2SBarry Smith Level: intermediate 2054d763cef2SBarry Smith 2055d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 2056d763cef2SBarry Smith 2057d763cef2SBarry Smith .seealso: TSSetApplicationContext() 2058d763cef2SBarry Smith @*/ 2059e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 2060d763cef2SBarry Smith { 2061d763cef2SBarry Smith PetscFunctionBegin; 20620700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2063e71120c6SJed Brown *(void**)usrP = ts->user; 2064d763cef2SBarry Smith PetscFunctionReturn(0); 2065d763cef2SBarry Smith } 2066d763cef2SBarry Smith 20674a2ae208SSatish Balay #undef __FUNCT__ 20684a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 2069d763cef2SBarry Smith /*@ 2070b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 2071d763cef2SBarry Smith 2072d763cef2SBarry Smith Not Collective 2073d763cef2SBarry Smith 2074d763cef2SBarry Smith Input Parameter: 2075d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2076d763cef2SBarry Smith 2077d763cef2SBarry Smith Output Parameter: 2078b8123daeSJed Brown . iter - number of steps completed so far 2079d763cef2SBarry Smith 2080d763cef2SBarry Smith Level: intermediate 2081d763cef2SBarry Smith 2082d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 20839be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 2084d763cef2SBarry Smith @*/ 20857087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 2086d763cef2SBarry Smith { 2087d763cef2SBarry Smith PetscFunctionBegin; 20880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20894482741eSBarry Smith PetscValidIntPointer(iter,2); 2090d763cef2SBarry Smith *iter = ts->steps; 2091d763cef2SBarry Smith PetscFunctionReturn(0); 2092d763cef2SBarry Smith } 2093d763cef2SBarry Smith 20944a2ae208SSatish Balay #undef __FUNCT__ 20954a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 2096d763cef2SBarry Smith /*@ 2097d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 2098d763cef2SBarry Smith as well as the initial time. 2099d763cef2SBarry Smith 21003f9fe445SBarry Smith Logically Collective on TS 2101d763cef2SBarry Smith 2102d763cef2SBarry Smith Input Parameters: 2103d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2104d763cef2SBarry Smith . initial_time - the initial time 2105d763cef2SBarry Smith - time_step - the size of the timestep 2106d763cef2SBarry Smith 2107d763cef2SBarry Smith Level: intermediate 2108d763cef2SBarry Smith 2109d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 2110d763cef2SBarry Smith 2111d763cef2SBarry Smith .keywords: TS, set, initial, timestep 2112d763cef2SBarry Smith @*/ 21137087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 2114d763cef2SBarry Smith { 2115e144a568SJed Brown PetscErrorCode ierr; 2116e144a568SJed Brown 2117d763cef2SBarry Smith PetscFunctionBegin; 21180700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2119e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 2120d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 2121d763cef2SBarry Smith PetscFunctionReturn(0); 2122d763cef2SBarry Smith } 2123d763cef2SBarry Smith 21244a2ae208SSatish Balay #undef __FUNCT__ 21254a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 2126d763cef2SBarry Smith /*@ 2127d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2128d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2129d763cef2SBarry Smith 21303f9fe445SBarry Smith Logically Collective on TS 2131d763cef2SBarry Smith 2132d763cef2SBarry Smith Input Parameters: 2133d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2134d763cef2SBarry Smith - time_step - the size of the timestep 2135d763cef2SBarry Smith 2136d763cef2SBarry Smith Level: intermediate 2137d763cef2SBarry Smith 2138d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2139d763cef2SBarry Smith 2140d763cef2SBarry Smith .keywords: TS, set, timestep 2141d763cef2SBarry Smith @*/ 21427087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 2143d763cef2SBarry Smith { 2144d763cef2SBarry Smith PetscFunctionBegin; 21450700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2146c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 2147d763cef2SBarry Smith ts->time_step = time_step; 2148d763cef2SBarry Smith PetscFunctionReturn(0); 2149d763cef2SBarry Smith } 2150d763cef2SBarry Smith 21514a2ae208SSatish Balay #undef __FUNCT__ 2152a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 2153a43b19c4SJed Brown /*@ 215449354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 215549354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 215649354f04SShri Abhyankar or just return at the final time TS computed. 2157a43b19c4SJed Brown 2158a43b19c4SJed Brown Logically Collective on TS 2159a43b19c4SJed Brown 2160a43b19c4SJed Brown Input Parameter: 2161a43b19c4SJed Brown + ts - the time-step context 216249354f04SShri Abhyankar - eftopt - exact final time option 2163a43b19c4SJed Brown 2164feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2165feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2166feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2167feed9e9dSBarry Smith 2168feed9e9dSBarry Smith Options Database: 2169feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2170feed9e9dSBarry Smith 2171ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 2172ee346746SBarry Smith then the final time you selected. 2173ee346746SBarry Smith 2174a43b19c4SJed Brown Level: beginner 2175a43b19c4SJed Brown 2176a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 2177a43b19c4SJed Brown @*/ 217849354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 2179a43b19c4SJed Brown { 2180a43b19c4SJed Brown PetscFunctionBegin; 2181a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 218249354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 218349354f04SShri Abhyankar ts->exact_final_time = eftopt; 2184a43b19c4SJed Brown PetscFunctionReturn(0); 2185a43b19c4SJed Brown } 2186a43b19c4SJed Brown 2187a43b19c4SJed Brown #undef __FUNCT__ 21884a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 2189d763cef2SBarry Smith /*@ 2190d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2191d763cef2SBarry Smith 2192d763cef2SBarry Smith Not Collective 2193d763cef2SBarry Smith 2194d763cef2SBarry Smith Input Parameter: 2195d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2196d763cef2SBarry Smith 2197d763cef2SBarry Smith Output Parameter: 2198d763cef2SBarry Smith . dt - the current timestep size 2199d763cef2SBarry Smith 2200d763cef2SBarry Smith Level: intermediate 2201d763cef2SBarry Smith 2202d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2203d763cef2SBarry Smith 2204d763cef2SBarry Smith .keywords: TS, get, timestep 2205d763cef2SBarry Smith @*/ 22067087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 2207d763cef2SBarry Smith { 2208d763cef2SBarry Smith PetscFunctionBegin; 22090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2210f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 2211d763cef2SBarry Smith *dt = ts->time_step; 2212d763cef2SBarry Smith PetscFunctionReturn(0); 2213d763cef2SBarry Smith } 2214d763cef2SBarry Smith 22154a2ae208SSatish Balay #undef __FUNCT__ 22164a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 2217d8e5e3e6SSatish Balay /*@ 2218d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2219d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2220d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2221d763cef2SBarry Smith the solution at the next timestep has been calculated. 2222d763cef2SBarry Smith 2223d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 2224d763cef2SBarry Smith 2225d763cef2SBarry Smith Input Parameter: 2226d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2227d763cef2SBarry Smith 2228d763cef2SBarry Smith Output Parameter: 2229d763cef2SBarry Smith . v - the vector containing the solution 2230d763cef2SBarry Smith 223163e21af5SBarry Smith Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested 223263e21af5SBarry Smith final time. It returns the solution at the next timestep. 223363e21af5SBarry Smith 2234d763cef2SBarry Smith Level: intermediate 2235d763cef2SBarry Smith 223663e21af5SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime() 2237d763cef2SBarry Smith 2238d763cef2SBarry Smith .keywords: TS, timestep, get, solution 2239d763cef2SBarry Smith @*/ 22407087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 2241d763cef2SBarry Smith { 2242d763cef2SBarry Smith PetscFunctionBegin; 22430700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22444482741eSBarry Smith PetscValidPointer(v,2); 22458737fe31SLisandro Dalcin *v = ts->vec_sol; 2246d763cef2SBarry Smith PetscFunctionReturn(0); 2247d763cef2SBarry Smith } 2248d763cef2SBarry Smith 2249c235aa8dSHong Zhang #undef __FUNCT__ 2250dfb21088SHong Zhang #define __FUNCT__ "TSGetCostGradients" 2251c235aa8dSHong Zhang /*@ 2252dfb21088SHong Zhang TSGetCostGradients - Returns the gradients from the TSAdjointSolve() 2253c235aa8dSHong Zhang 2254c235aa8dSHong Zhang Not Collective, but Vec returned is parallel if TS is parallel 2255c235aa8dSHong Zhang 2256c235aa8dSHong Zhang Input Parameter: 2257c235aa8dSHong Zhang . ts - the TS context obtained from TSCreate() 2258c235aa8dSHong Zhang 2259c235aa8dSHong Zhang Output Parameter: 2260abc2977eSBarry Smith + lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 2261abc2977eSBarry Smith - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 2262c235aa8dSHong Zhang 2263c235aa8dSHong Zhang Level: intermediate 2264c235aa8dSHong Zhang 2265c235aa8dSHong Zhang .seealso: TSGetTimeStep() 2266c235aa8dSHong Zhang 2267c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity 2268c235aa8dSHong Zhang @*/ 2269dfb21088SHong Zhang PetscErrorCode TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu) 2270c235aa8dSHong Zhang { 2271c235aa8dSHong Zhang PetscFunctionBegin; 2272c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2273abc2977eSBarry Smith if (numcost) *numcost = ts->numcost; 2274abc2977eSBarry Smith if (lambda) *lambda = ts->vecs_sensi; 2275abc2977eSBarry Smith if (mu) *mu = ts->vecs_sensip; 2276c235aa8dSHong Zhang PetscFunctionReturn(0); 2277c235aa8dSHong Zhang } 2278c235aa8dSHong Zhang 2279bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 22804a2ae208SSatish Balay #undef __FUNCT__ 2281bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 2282d8e5e3e6SSatish Balay /*@ 2283bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2284d763cef2SBarry Smith 2285bdad233fSMatthew Knepley Not collective 2286d763cef2SBarry Smith 2287bdad233fSMatthew Knepley Input Parameters: 2288bdad233fSMatthew Knepley + ts - The TS 2289bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2290d763cef2SBarry Smith .vb 22910910c330SBarry Smith U_t - A U = 0 (linear) 22920910c330SBarry Smith U_t - A(t) U = 0 (linear) 22930910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2294d763cef2SBarry Smith .ve 2295d763cef2SBarry Smith 2296d763cef2SBarry Smith Level: beginner 2297d763cef2SBarry Smith 2298bdad233fSMatthew Knepley .keywords: TS, problem type 2299bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2300d763cef2SBarry Smith @*/ 23017087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2302a7cc72afSBarry Smith { 23039e2a6581SJed Brown PetscErrorCode ierr; 23049e2a6581SJed Brown 2305d763cef2SBarry Smith PetscFunctionBegin; 23060700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2307bdad233fSMatthew Knepley ts->problem_type = type; 23089e2a6581SJed Brown if (type == TS_LINEAR) { 23099e2a6581SJed Brown SNES snes; 23109e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 23119e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 23129e2a6581SJed Brown } 2313d763cef2SBarry Smith PetscFunctionReturn(0); 2314d763cef2SBarry Smith } 2315d763cef2SBarry Smith 2316bdad233fSMatthew Knepley #undef __FUNCT__ 2317bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 2318bdad233fSMatthew Knepley /*@C 2319bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2320bdad233fSMatthew Knepley 2321bdad233fSMatthew Knepley Not collective 2322bdad233fSMatthew Knepley 2323bdad233fSMatthew Knepley Input Parameter: 2324bdad233fSMatthew Knepley . ts - The TS 2325bdad233fSMatthew Knepley 2326bdad233fSMatthew Knepley Output Parameter: 2327bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2328bdad233fSMatthew Knepley .vb 2329089b2837SJed Brown M U_t = A U 2330089b2837SJed Brown M(t) U_t = A(t) U 2331b5abc632SBarry Smith F(t,U,U_t) 2332bdad233fSMatthew Knepley .ve 2333bdad233fSMatthew Knepley 2334bdad233fSMatthew Knepley Level: beginner 2335bdad233fSMatthew Knepley 2336bdad233fSMatthew Knepley .keywords: TS, problem type 2337bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2338bdad233fSMatthew Knepley @*/ 23397087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2340a7cc72afSBarry Smith { 2341bdad233fSMatthew Knepley PetscFunctionBegin; 23420700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 23434482741eSBarry Smith PetscValidIntPointer(type,2); 2344bdad233fSMatthew Knepley *type = ts->problem_type; 2345bdad233fSMatthew Knepley PetscFunctionReturn(0); 2346bdad233fSMatthew Knepley } 2347d763cef2SBarry Smith 23484a2ae208SSatish Balay #undef __FUNCT__ 23494a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 2350d763cef2SBarry Smith /*@ 2351d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 2352d763cef2SBarry Smith of a timestepper. 2353d763cef2SBarry Smith 2354d763cef2SBarry Smith Collective on TS 2355d763cef2SBarry Smith 2356d763cef2SBarry Smith Input Parameter: 2357d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2358d763cef2SBarry Smith 2359d763cef2SBarry Smith Notes: 2360d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 2361d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 2362d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 2363d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 2364d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 2365d763cef2SBarry Smith 2366d763cef2SBarry Smith Level: advanced 2367d763cef2SBarry Smith 2368d763cef2SBarry Smith .keywords: TS, timestep, setup 2369d763cef2SBarry Smith 2370d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 2371d763cef2SBarry Smith @*/ 23727087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 2373d763cef2SBarry Smith { 2374dfbe8321SBarry Smith PetscErrorCode ierr; 23756c6b9e74SPeter Brune DM dm; 23766c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 2377d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 2378cd11d68dSLisandro Dalcin TSIFunction ifun; 23796c6b9e74SPeter Brune TSIJacobian ijac; 2380efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 23816c6b9e74SPeter Brune TSRHSJacobian rhsjac; 2382d763cef2SBarry Smith 2383d763cef2SBarry Smith PetscFunctionBegin; 23840700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2385277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 2386277b19d0SLisandro Dalcin 23872c18e0fdSBarry Smith ts->total_steps = 0; 23887adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 2389cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 2390cd11d68dSLisandro Dalcin ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr); 2391d763cef2SBarry Smith } 2392277b19d0SLisandro Dalcin 2393277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 2394277b19d0SLisandro Dalcin 2395e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 2396e1244c69SJed Brown Mat Amat,Pmat; 2397e1244c69SJed Brown SNES snes; 2398e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2399e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 2400e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2401e1244c69SJed Brown * have displaced the RHS matrix */ 2402e1244c69SJed Brown if (Amat == ts->Arhs) { 2403e1244c69SJed Brown ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 2404e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 2405e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 2406e1244c69SJed Brown } 2407e1244c69SJed Brown if (Pmat == ts->Brhs) { 2408e1244c69SJed Brown ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 2409e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 2410e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 2411e1244c69SJed Brown } 2412e1244c69SJed Brown } 2413277b19d0SLisandro Dalcin if (ts->ops->setup) { 2414000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 2415277b19d0SLisandro Dalcin } 2416277b19d0SLisandro Dalcin 2417a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 24186c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 24196c6b9e74SPeter Brune */ 24206c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 24210298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 24226c6b9e74SPeter Brune if (!func) { 24236c6b9e74SPeter Brune ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 24246c6b9e74SPeter Brune } 2425a6772fa2SLisandro 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. 24266c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 24276c6b9e74SPeter Brune */ 24280298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 24290298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 2430efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr); 24310298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 2432efe9872eSLisandro Dalcin if (!jac && (ijac || i2jac || rhsjac)) { 24336c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 24346c6b9e74SPeter Brune } 2435277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2436277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2437277b19d0SLisandro Dalcin } 2438277b19d0SLisandro Dalcin 2439277b19d0SLisandro Dalcin #undef __FUNCT__ 2440f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp" 2441f6a906c0SBarry Smith /*@ 2442f6a906c0SBarry Smith TSAdjointSetUp - Sets up the internal data structures for the later use 2443f6a906c0SBarry Smith of an adjoint solver 2444f6a906c0SBarry Smith 2445f6a906c0SBarry Smith Collective on TS 2446f6a906c0SBarry Smith 2447f6a906c0SBarry Smith Input Parameter: 2448f6a906c0SBarry Smith . ts - the TS context obtained from TSCreate() 2449f6a906c0SBarry Smith 2450f6a906c0SBarry Smith Level: advanced 2451f6a906c0SBarry Smith 2452f6a906c0SBarry Smith .keywords: TS, timestep, setup 2453f6a906c0SBarry Smith 2454947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients() 2455f6a906c0SBarry Smith @*/ 2456f6a906c0SBarry Smith PetscErrorCode TSAdjointSetUp(TS ts) 2457f6a906c0SBarry Smith { 2458f6a906c0SBarry Smith PetscErrorCode ierr; 2459f6a906c0SBarry Smith 2460f6a906c0SBarry Smith PetscFunctionBegin; 2461f6a906c0SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2462f6a906c0SBarry Smith if (ts->adjointsetupcalled) PetscFunctionReturn(0); 2463dfb21088SHong Zhang if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first"); 2464d8151eeaSBarry Smith 2465947abb85SHong Zhang if (ts->vec_costintegral) { /* if there is integral in the cost function*/ 2466d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2467d8151eeaSBarry Smith if (ts->vecs_sensip){ 2468d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2469d8151eeaSBarry Smith } 2470947abb85SHong Zhang } 2471d8151eeaSBarry Smith 247242f2b339SBarry Smith if (ts->ops->adjointsetup) { 247342f2b339SBarry Smith ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr); 2474f6a906c0SBarry Smith } 2475f6a906c0SBarry Smith ts->adjointsetupcalled = PETSC_TRUE; 2476f6a906c0SBarry Smith PetscFunctionReturn(0); 2477f6a906c0SBarry Smith } 2478f6a906c0SBarry Smith 2479f6a906c0SBarry Smith #undef __FUNCT__ 2480277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 2481277b19d0SLisandro Dalcin /*@ 2482277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2483277b19d0SLisandro Dalcin 2484277b19d0SLisandro Dalcin Collective on TS 2485277b19d0SLisandro Dalcin 2486277b19d0SLisandro Dalcin Input Parameter: 2487277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2488277b19d0SLisandro Dalcin 2489277b19d0SLisandro Dalcin Level: beginner 2490277b19d0SLisandro Dalcin 2491277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 2492277b19d0SLisandro Dalcin 2493277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2494277b19d0SLisandro Dalcin @*/ 2495277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2496277b19d0SLisandro Dalcin { 2497277b19d0SLisandro Dalcin PetscErrorCode ierr; 2498277b19d0SLisandro Dalcin 2499277b19d0SLisandro Dalcin PetscFunctionBegin; 2500277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2501b18ea86cSHong Zhang 2502277b19d0SLisandro Dalcin if (ts->ops->reset) { 2503277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2504277b19d0SLisandro Dalcin } 2505277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2506e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2507bbd56ea5SKarl Rupp 25084e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 25094e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2510214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 25116bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2512efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 2513e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2514e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 251538637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2516bbd56ea5SKarl Rupp 2517947abb85SHong Zhang if (ts->vec_costintegral) { 2518d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2519d8151eeaSBarry Smith if (ts->vecs_drdp){ 2520d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2521d8151eeaSBarry Smith } 2522947abb85SHong Zhang } 2523d8151eeaSBarry Smith ts->vecs_sensi = NULL; 2524d8151eeaSBarry Smith ts->vecs_sensip = NULL; 2525ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 25262c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 252736eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2528277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2529d763cef2SBarry Smith PetscFunctionReturn(0); 2530d763cef2SBarry Smith } 2531d763cef2SBarry Smith 25324a2ae208SSatish Balay #undef __FUNCT__ 25334a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 2534d8e5e3e6SSatish Balay /*@ 2535d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2536d763cef2SBarry Smith with TSCreate(). 2537d763cef2SBarry Smith 2538d763cef2SBarry Smith Collective on TS 2539d763cef2SBarry Smith 2540d763cef2SBarry Smith Input Parameter: 2541d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2542d763cef2SBarry Smith 2543d763cef2SBarry Smith Level: beginner 2544d763cef2SBarry Smith 2545d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2546d763cef2SBarry Smith 2547d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2548d763cef2SBarry Smith @*/ 25496bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2550d763cef2SBarry Smith { 25516849ba73SBarry Smith PetscErrorCode ierr; 2552d763cef2SBarry Smith 2553d763cef2SBarry Smith PetscFunctionBegin; 25546bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 25556bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 25566bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2557d763cef2SBarry Smith 25586bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2559277b19d0SLisandro Dalcin 2560e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2561e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 25626bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 25636d4c513bSLisandro Dalcin 2564bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2565bc952696SBarry Smith 256684df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 25676427ac75SLisandro Dalcin ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr); 25686427ac75SLisandro Dalcin 25696bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 25706bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 25716bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 25720dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 25736d4c513bSLisandro Dalcin 2574a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2575d763cef2SBarry Smith PetscFunctionReturn(0); 2576d763cef2SBarry Smith } 2577d763cef2SBarry Smith 25784a2ae208SSatish Balay #undef __FUNCT__ 25794a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 2580d8e5e3e6SSatish Balay /*@ 2581d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2582d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2583d763cef2SBarry Smith 2584d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2585d763cef2SBarry Smith 2586d763cef2SBarry Smith Input Parameter: 2587d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2588d763cef2SBarry Smith 2589d763cef2SBarry Smith Output Parameter: 2590d763cef2SBarry Smith . snes - the nonlinear solver context 2591d763cef2SBarry Smith 2592d763cef2SBarry Smith Notes: 2593d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2594d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 259594b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2596d763cef2SBarry Smith 2597d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 25980298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2599d763cef2SBarry Smith 2600d763cef2SBarry Smith Level: beginner 2601d763cef2SBarry Smith 2602d763cef2SBarry Smith .keywords: timestep, get, SNES 2603d763cef2SBarry Smith @*/ 26047087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2605d763cef2SBarry Smith { 2606d372ba47SLisandro Dalcin PetscErrorCode ierr; 2607d372ba47SLisandro Dalcin 2608d763cef2SBarry Smith PetscFunctionBegin; 26090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26104482741eSBarry Smith PetscValidPointer(snes,2); 2611d372ba47SLisandro Dalcin if (!ts->snes) { 2612ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 26130298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 26143bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2615d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2616496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 26179e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 26189e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 26199e2a6581SJed Brown } 2620d372ba47SLisandro Dalcin } 2621d763cef2SBarry Smith *snes = ts->snes; 2622d763cef2SBarry Smith PetscFunctionReturn(0); 2623d763cef2SBarry Smith } 2624d763cef2SBarry Smith 26254a2ae208SSatish Balay #undef __FUNCT__ 2626deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES" 2627deb2cd25SJed Brown /*@ 2628deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2629deb2cd25SJed Brown 2630deb2cd25SJed Brown Collective 2631deb2cd25SJed Brown 2632deb2cd25SJed Brown Input Parameter: 2633deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2634deb2cd25SJed Brown - snes - the nonlinear solver context 2635deb2cd25SJed Brown 2636deb2cd25SJed Brown Notes: 2637deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2638deb2cd25SJed Brown 2639deb2cd25SJed Brown Level: developer 2640deb2cd25SJed Brown 2641deb2cd25SJed Brown .keywords: timestep, set, SNES 2642deb2cd25SJed Brown @*/ 2643deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2644deb2cd25SJed Brown { 2645deb2cd25SJed Brown PetscErrorCode ierr; 2646d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2647deb2cd25SJed Brown 2648deb2cd25SJed Brown PetscFunctionBegin; 2649deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2650deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2651deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2652deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2653bbd56ea5SKarl Rupp 2654deb2cd25SJed Brown ts->snes = snes; 2655bbd56ea5SKarl Rupp 26560298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 26570298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2658740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 26590298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2660740132f1SEmil Constantinescu } 2661deb2cd25SJed Brown PetscFunctionReturn(0); 2662deb2cd25SJed Brown } 2663deb2cd25SJed Brown 2664deb2cd25SJed Brown #undef __FUNCT__ 266594b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 2666d8e5e3e6SSatish Balay /*@ 266794b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2668d763cef2SBarry Smith a TS (timestepper) context. 2669d763cef2SBarry Smith 267094b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2671d763cef2SBarry Smith 2672d763cef2SBarry Smith Input Parameter: 2673d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2674d763cef2SBarry Smith 2675d763cef2SBarry Smith Output Parameter: 267694b7f48cSBarry Smith . ksp - the nonlinear solver context 2677d763cef2SBarry Smith 2678d763cef2SBarry Smith Notes: 267994b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2680d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2681d763cef2SBarry Smith KSP and PC contexts as well. 2682d763cef2SBarry Smith 268394b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 26840298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2685d763cef2SBarry Smith 2686d763cef2SBarry Smith Level: beginner 2687d763cef2SBarry Smith 268894b7f48cSBarry Smith .keywords: timestep, get, KSP 2689d763cef2SBarry Smith @*/ 26907087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2691d763cef2SBarry Smith { 2692d372ba47SLisandro Dalcin PetscErrorCode ierr; 2693089b2837SJed Brown SNES snes; 2694d372ba47SLisandro Dalcin 2695d763cef2SBarry Smith PetscFunctionBegin; 26960700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26974482741eSBarry Smith PetscValidPointer(ksp,2); 269817186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2699e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2700089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2701089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2702d763cef2SBarry Smith PetscFunctionReturn(0); 2703d763cef2SBarry Smith } 2704d763cef2SBarry Smith 2705d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2706d763cef2SBarry Smith 27074a2ae208SSatish Balay #undef __FUNCT__ 2708adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 2709adb62b0dSMatthew Knepley /*@ 2710adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2711adb62b0dSMatthew Knepley maximum time for iteration. 2712adb62b0dSMatthew Knepley 27133f9fe445SBarry Smith Not Collective 2714adb62b0dSMatthew Knepley 2715adb62b0dSMatthew Knepley Input Parameters: 2716adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 27170298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 27180298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2719adb62b0dSMatthew Knepley 2720adb62b0dSMatthew Knepley Level: intermediate 2721adb62b0dSMatthew Knepley 2722adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2723adb62b0dSMatthew Knepley @*/ 27247087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2725adb62b0dSMatthew Knepley { 2726adb62b0dSMatthew Knepley PetscFunctionBegin; 27270700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2728abc0a331SBarry Smith if (maxsteps) { 27294482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2730adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2731adb62b0dSMatthew Knepley } 2732abc0a331SBarry Smith if (maxtime) { 27334482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2734adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2735adb62b0dSMatthew Knepley } 2736adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2737adb62b0dSMatthew Knepley } 2738adb62b0dSMatthew Knepley 2739adb62b0dSMatthew Knepley #undef __FUNCT__ 27404a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 2741d763cef2SBarry Smith /*@ 2742d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2743d763cef2SBarry Smith maximum time for iteration. 2744d763cef2SBarry Smith 27453f9fe445SBarry Smith Logically Collective on TS 2746d763cef2SBarry Smith 2747d763cef2SBarry Smith Input Parameters: 2748d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2749d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2750d763cef2SBarry Smith - maxtime - final time to iterate to 2751d763cef2SBarry Smith 2752d763cef2SBarry Smith Options Database Keys: 2753d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 27543bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2755d763cef2SBarry Smith 2756d763cef2SBarry Smith Notes: 2757d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2758d763cef2SBarry Smith 2759d763cef2SBarry Smith Level: intermediate 2760d763cef2SBarry Smith 2761d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2762a43b19c4SJed Brown 2763a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2764d763cef2SBarry Smith @*/ 27657087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2766d763cef2SBarry Smith { 2767d763cef2SBarry Smith PetscFunctionBegin; 27680700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2769c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2770c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 277139b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 277239b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2773d763cef2SBarry Smith PetscFunctionReturn(0); 2774d763cef2SBarry Smith } 2775d763cef2SBarry Smith 27764a2ae208SSatish Balay #undef __FUNCT__ 27774a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 2778d763cef2SBarry Smith /*@ 2779d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2780d763cef2SBarry Smith for use by the TS routines. 2781d763cef2SBarry Smith 27823f9fe445SBarry Smith Logically Collective on TS and Vec 2783d763cef2SBarry Smith 2784d763cef2SBarry Smith Input Parameters: 2785d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 27860910c330SBarry Smith - u - the solution vector 2787d763cef2SBarry Smith 2788d763cef2SBarry Smith Level: beginner 2789d763cef2SBarry Smith 2790d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2791d763cef2SBarry Smith @*/ 27920910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2793d763cef2SBarry Smith { 27948737fe31SLisandro Dalcin PetscErrorCode ierr; 2795496e6a7aSJed Brown DM dm; 27968737fe31SLisandro Dalcin 2797d763cef2SBarry Smith PetscFunctionBegin; 27980700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27990910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 28000910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 28016bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 28020910c330SBarry Smith ts->vec_sol = u; 2803bbd56ea5SKarl Rupp 2804496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 28050910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2806d763cef2SBarry Smith PetscFunctionReturn(0); 2807d763cef2SBarry Smith } 2808d763cef2SBarry Smith 2809e74ef692SMatthew Knepley #undef __FUNCT__ 281073b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps" 281173b18844SBarry Smith /*@ 281273b18844SBarry Smith TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time 281373b18844SBarry Smith 281473b18844SBarry Smith Logically Collective on TS 281573b18844SBarry Smith 281673b18844SBarry Smith Input Parameters: 281773b18844SBarry Smith + ts - the TS context obtained from TSCreate() 281873b18844SBarry Smith . steps - number of steps to use 281973b18844SBarry Smith 282073b18844SBarry Smith Level: intermediate 282173b18844SBarry Smith 282273b18844SBarry Smith Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this 282373b18844SBarry Smith so as to integrate back to less than the original timestep 282473b18844SBarry Smith 282573b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations 282673b18844SBarry Smith 282773b18844SBarry Smith .seealso: TSSetExactFinalTime() 282873b18844SBarry Smith @*/ 282973b18844SBarry Smith PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps) 283073b18844SBarry Smith { 283173b18844SBarry Smith PetscFunctionBegin; 283273b18844SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 283373b18844SBarry Smith PetscValidLogicalCollectiveInt(ts,steps,2); 283473b18844SBarry Smith if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps"); 283573b18844SBarry 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"); 283673b18844SBarry Smith ts->adjoint_max_steps = steps; 283773b18844SBarry Smith PetscFunctionReturn(0); 283873b18844SBarry Smith } 283973b18844SBarry Smith 284073b18844SBarry Smith #undef __FUNCT__ 2841dfb21088SHong Zhang #define __FUNCT__ "TSSetCostGradients" 2842c235aa8dSHong Zhang /*@ 2843dfb21088SHong 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 28440cf82b59SBarry Smith for use by the TSAdjoint routines. 2845c235aa8dSHong Zhang 2846c235aa8dSHong Zhang Logically Collective on TS and Vec 2847c235aa8dSHong Zhang 2848c235aa8dSHong Zhang Input Parameters: 2849c235aa8dSHong Zhang + ts - the TS context obtained from TSCreate() 2850abc2977eSBarry 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 2851abc2977eSBarry Smith - mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters 2852c235aa8dSHong Zhang 2853c235aa8dSHong Zhang Level: beginner 2854c235aa8dSHong Zhang 2855abc2977eSBarry Smith Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime mu_i = df/dp|finaltime 28560cf82b59SBarry Smith 2857c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions 2858c235aa8dSHong Zhang @*/ 2859dfb21088SHong Zhang PetscErrorCode TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu) 2860c235aa8dSHong Zhang { 2861c235aa8dSHong Zhang PetscFunctionBegin; 2862c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2863abc2977eSBarry Smith PetscValidPointer(lambda,2); 2864abc2977eSBarry Smith ts->vecs_sensi = lambda; 2865abc2977eSBarry Smith ts->vecs_sensip = mu; 2866dfb21088SHong 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"); 2867abc2977eSBarry Smith ts->numcost = numcost; 2868ad8e2604SHong Zhang PetscFunctionReturn(0); 2869ad8e2604SHong Zhang } 2870ad8e2604SHong Zhang 2871ad8e2604SHong Zhang #undef __FUNCT__ 28725bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian" 2873ad8e2604SHong Zhang /*@C 28740cf82b59SBarry 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. 2875ad8e2604SHong Zhang 2876ad8e2604SHong Zhang Logically Collective on TS 2877ad8e2604SHong Zhang 2878ad8e2604SHong Zhang Input Parameters: 2879ad8e2604SHong Zhang + ts - The TS context obtained from TSCreate() 2880ad8e2604SHong Zhang - func - The function 2881ad8e2604SHong Zhang 2882ad8e2604SHong Zhang Calling sequence of func: 28830cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx); 288405755b9cSHong Zhang + t - current timestep 28850cf82b59SBarry Smith . y - input vector (current ODE solution) 288605755b9cSHong Zhang . A - output matrix 288705755b9cSHong Zhang - ctx - [optional] user-defined function context 2888ad8e2604SHong Zhang 2889ad8e2604SHong Zhang Level: intermediate 2890ad8e2604SHong Zhang 28910cf82b59SBarry 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 28920cf82b59SBarry Smith 2893ad8e2604SHong Zhang .keywords: TS, sensitivity 2894ad8e2604SHong Zhang .seealso: 2895ad8e2604SHong Zhang @*/ 28965bf8c567SBarry Smith PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 2897ad8e2604SHong Zhang { 2898ad8e2604SHong Zhang PetscErrorCode ierr; 2899ad8e2604SHong Zhang 2900ad8e2604SHong Zhang PetscFunctionBegin; 2901ad8e2604SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2902ad8e2604SHong Zhang if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 2903ad8e2604SHong Zhang 2904ad8e2604SHong Zhang ts->rhsjacobianp = func; 2905ad8e2604SHong Zhang ts->rhsjacobianpctx = ctx; 2906ad8e2604SHong Zhang if(Amat) { 2907ad8e2604SHong Zhang ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 2908ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2909ad8e2604SHong Zhang ts->Jacp = Amat; 2910ad8e2604SHong Zhang } 2911ad8e2604SHong Zhang PetscFunctionReturn(0); 2912ad8e2604SHong Zhang } 2913ad8e2604SHong Zhang 2914ad8e2604SHong Zhang #undef __FUNCT__ 29155bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian" 29160cf82b59SBarry Smith /*@C 29175bf8c567SBarry Smith TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function. 2918ad8e2604SHong Zhang 2919ad8e2604SHong Zhang Collective on TS 2920ad8e2604SHong Zhang 2921ad8e2604SHong Zhang Input Parameters: 2922ad8e2604SHong Zhang . ts - The TS context obtained from TSCreate() 2923ad8e2604SHong Zhang 2924ad8e2604SHong Zhang Level: developer 2925ad8e2604SHong Zhang 2926ad8e2604SHong Zhang .keywords: TS, sensitivity 29275bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian() 2928ad8e2604SHong Zhang @*/ 29295bf8c567SBarry Smith PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat) 2930ad8e2604SHong Zhang { 2931ad8e2604SHong Zhang PetscErrorCode ierr; 2932ad8e2604SHong Zhang 2933ad8e2604SHong Zhang PetscFunctionBegin; 2934ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2935ad8e2604SHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 2936ad8e2604SHong Zhang PetscValidPointer(Amat,4); 2937ad8e2604SHong Zhang 2938ad8e2604SHong Zhang PetscStackPush("TS user JacobianP function for sensitivity analysis"); 2939ad8e2604SHong Zhang ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 2940ad8e2604SHong Zhang PetscStackPop; 2941c235aa8dSHong Zhang PetscFunctionReturn(0); 2942c235aa8dSHong Zhang } 2943c235aa8dSHong Zhang 2944c235aa8dSHong Zhang #undef __FUNCT__ 2945dfb21088SHong Zhang #define __FUNCT__ "TSSetCostIntegrand" 29466fd0887fSHong Zhang /*@C 2947dfb21088SHong Zhang TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions 29486fd0887fSHong Zhang 29496fd0887fSHong Zhang Logically Collective on TS 29506fd0887fSHong Zhang 29516fd0887fSHong Zhang Input Parameters: 29526fd0887fSHong Zhang + ts - the TS context obtained from TSCreate() 2953abc2977eSBarry Smith . numcost - number of gradients to be computed, this is the number of cost functions 29540cf82b59SBarry Smith . rf - routine for evaluating the integrand function 2955b612ec54SBarry Smith . drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y 2956b612ec54SBarry Smith . drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p 2957b1cb36f3SHong Zhang . fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run 2958b612ec54SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 29596fd0887fSHong Zhang 29600cf82b59SBarry Smith Calling sequence of rf: 29610cf82b59SBarry Smith $ rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx); 29626fd0887fSHong Zhang 29636fd0887fSHong Zhang + t - current timestep 29640cf82b59SBarry Smith . y - input vector 29650cf82b59SBarry Smith . f - function result; one vector entry for each cost function 29666fd0887fSHong Zhang - ctx - [optional] user-defined function context 29676fd0887fSHong Zhang 2968b612ec54SBarry Smith Calling sequence of drdyf: 29690cf82b59SBarry Smith $ PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx); 2970b612ec54SBarry Smith 2971b612ec54SBarry Smith Calling sequence of drdpf: 29720cf82b59SBarry Smith $ PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx); 2973b612ec54SBarry Smith 2974b612ec54SBarry Smith Level: intermediate 29756fd0887fSHong Zhang 2976abc2977eSBarry Smith Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions 29770cf82b59SBarry Smith 29786fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 29796fd0887fSHong Zhang 2980dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients() 29816fd0887fSHong Zhang @*/ 2982dfb21088SHong Zhang PetscErrorCode TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*), 2983d8151eeaSBarry Smith PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*), 2984b1cb36f3SHong Zhang PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*), 2985b1cb36f3SHong Zhang PetscBool fwd,void *ctx) 29866fd0887fSHong Zhang { 29876fd0887fSHong Zhang PetscErrorCode ierr; 29886fd0887fSHong Zhang 29896fd0887fSHong Zhang PetscFunctionBegin; 29906fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2991dfb21088SHong 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()"); 2992c72ed514SHong Zhang if (!ts->numcost) ts->numcost=numcost; 29936fd0887fSHong Zhang 2994b1cb36f3SHong Zhang ts->costintegralfwd = fwd; /* Evaluate the cost integral in forward run if fwd is true */ 2995abc2977eSBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr); 29962c39e106SBarry Smith ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr); 29970cf82b59SBarry Smith ts->costintegrand = rf; 299805755b9cSHong Zhang ts->costintegrandctx = ctx; 2999b612ec54SBarry Smith ts->drdyfunction = drdyf; 3000b612ec54SBarry Smith ts->drdpfunction = drdpf; 30016fd0887fSHong Zhang PetscFunctionReturn(0); 30026fd0887fSHong Zhang } 30036fd0887fSHong Zhang 30046fd0887fSHong Zhang #undef __FUNCT__ 3005dfb21088SHong Zhang #define __FUNCT__ "TSGetCostIntegral" 300636eaed60SHong Zhang /*@ 3007dfb21088SHong Zhang TSGetCostIntegral - Returns the values of the integral term in the cost functions. 300836eaed60SHong Zhang It is valid to call the routine after a backward run. 300936eaed60SHong Zhang 301036eaed60SHong Zhang Not Collective 301136eaed60SHong Zhang 301236eaed60SHong Zhang Input Parameter: 301336eaed60SHong Zhang . ts - the TS context obtained from TSCreate() 301436eaed60SHong Zhang 301536eaed60SHong Zhang Output Parameter: 30162c39e106SBarry Smith . v - the vector containing the integrals for each cost function 301736eaed60SHong Zhang 301836eaed60SHong Zhang Level: intermediate 301936eaed60SHong Zhang 3020dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 302136eaed60SHong Zhang 302236eaed60SHong Zhang .keywords: TS, sensitivity analysis 302336eaed60SHong Zhang @*/ 3024dfb21088SHong Zhang PetscErrorCode TSGetCostIntegral(TS ts,Vec *v) 302536eaed60SHong Zhang { 302636eaed60SHong Zhang PetscFunctionBegin; 302736eaed60SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 302836eaed60SHong Zhang PetscValidPointer(v,2); 30292c39e106SBarry Smith *v = ts->vec_costintegral; 303036eaed60SHong Zhang PetscFunctionReturn(0); 303136eaed60SHong Zhang } 303236eaed60SHong Zhang 303336eaed60SHong Zhang #undef __FUNCT__ 3034d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand" 30356fd0887fSHong Zhang /*@ 30362c39e106SBarry Smith TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions. 30376fd0887fSHong Zhang 30386fd0887fSHong Zhang Input Parameters: 30396fd0887fSHong Zhang + ts - the TS context 30406fd0887fSHong Zhang . t - current time 30410cf82b59SBarry Smith - y - state vector, i.e. current solution 30426fd0887fSHong Zhang 30436fd0887fSHong Zhang Output Parameter: 3044abc2977eSBarry Smith . q - vector of size numcost to hold the outputs 30456fd0887fSHong Zhang 30466fd0887fSHong Zhang Note: 30476fd0887fSHong Zhang Most users should not need to explicitly call this routine, as it 30486fd0887fSHong Zhang is used internally within the sensitivity analysis context. 30496fd0887fSHong Zhang 30506fd0887fSHong Zhang Level: developer 30516fd0887fSHong Zhang 30526fd0887fSHong Zhang .keywords: TS, compute 30536fd0887fSHong Zhang 3054dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 30556fd0887fSHong Zhang @*/ 30560cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q) 30576fd0887fSHong Zhang { 30586fd0887fSHong Zhang PetscErrorCode ierr; 30596fd0887fSHong Zhang 30606fd0887fSHong Zhang PetscFunctionBegin; 30616fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30620cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 30636fd0887fSHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,4); 30646fd0887fSHong Zhang 30650cf82b59SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 3066e4680132SHong Zhang if (ts->costintegrand) { 3067e4680132SHong Zhang PetscStackPush("TS user integrand in the cost function"); 30680cf82b59SBarry Smith ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr); 30696fd0887fSHong Zhang PetscStackPop; 30706fd0887fSHong Zhang } else { 30716fd0887fSHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 30726fd0887fSHong Zhang } 30736fd0887fSHong Zhang 30740cf82b59SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 30756fd0887fSHong Zhang PetscFunctionReturn(0); 30766fd0887fSHong Zhang } 30776fd0887fSHong Zhang 30786fd0887fSHong Zhang #undef __FUNCT__ 3079d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction" 308005755b9cSHong Zhang /*@ 3081d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 308205755b9cSHong Zhang 308305755b9cSHong Zhang Collective on TS 308405755b9cSHong Zhang 308505755b9cSHong Zhang Input Parameters: 308605755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 308705755b9cSHong Zhang 308805755b9cSHong Zhang Notes: 3089d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 309005755b9cSHong Zhang so most users would not generally call this routine themselves. 309105755b9cSHong Zhang 309205755b9cSHong Zhang Level: developer 309305755b9cSHong Zhang 309405755b9cSHong Zhang .keywords: TS, sensitivity 3095d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction() 309605755b9cSHong Zhang @*/ 30970cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy) 309805755b9cSHong Zhang { 309905755b9cSHong Zhang PetscErrorCode ierr; 310005755b9cSHong Zhang 310105755b9cSHong Zhang PetscFunctionBegin; 310205755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31030cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 310405755b9cSHong Zhang 310505755b9cSHong Zhang PetscStackPush("TS user DRDY function for sensitivity analysis"); 31060cf82b59SBarry Smith ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr); 310705755b9cSHong Zhang PetscStackPop; 310805755b9cSHong Zhang PetscFunctionReturn(0); 310905755b9cSHong Zhang } 311005755b9cSHong Zhang 311105755b9cSHong Zhang #undef __FUNCT__ 3112d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction" 311305755b9cSHong Zhang /*@ 3114d4aa0a58SBarry Smith TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 311505755b9cSHong Zhang 311605755b9cSHong Zhang Collective on TS 311705755b9cSHong Zhang 311805755b9cSHong Zhang Input Parameters: 311905755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 312005755b9cSHong Zhang 312105755b9cSHong Zhang Notes: 312205755b9cSHong Zhang TSDRDPFunction() is typically used for sensitivity implementation, 312305755b9cSHong Zhang so most users would not generally call this routine themselves. 312405755b9cSHong Zhang 312505755b9cSHong Zhang Level: developer 312605755b9cSHong Zhang 312705755b9cSHong Zhang .keywords: TS, sensitivity 3128d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction() 312905755b9cSHong Zhang @*/ 31300cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp) 313105755b9cSHong Zhang { 313205755b9cSHong Zhang PetscErrorCode ierr; 313305755b9cSHong Zhang 313405755b9cSHong Zhang PetscFunctionBegin; 313505755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31360cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 313705755b9cSHong Zhang 313805755b9cSHong Zhang PetscStackPush("TS user DRDP function for sensitivity analysis"); 31390cf82b59SBarry Smith ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr); 314005755b9cSHong Zhang PetscStackPop; 314105755b9cSHong Zhang PetscFunctionReturn(0); 314205755b9cSHong Zhang } 314305755b9cSHong Zhang 314405755b9cSHong Zhang #undef __FUNCT__ 3145e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 3146ac226902SBarry Smith /*@C 3147000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 31483f2090d5SJed Brown called once at the beginning of each time step. 3149000e7ae3SMatthew Knepley 31503f9fe445SBarry Smith Logically Collective on TS 3151000e7ae3SMatthew Knepley 3152000e7ae3SMatthew Knepley Input Parameters: 3153000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3154000e7ae3SMatthew Knepley - func - The function 3155000e7ae3SMatthew Knepley 3156000e7ae3SMatthew Knepley Calling sequence of func: 3157000e7ae3SMatthew Knepley . func (TS ts); 3158000e7ae3SMatthew Knepley 3159000e7ae3SMatthew Knepley Level: intermediate 3160000e7ae3SMatthew Knepley 3161000e7ae3SMatthew Knepley .keywords: TS, timestep 31629be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 3163000e7ae3SMatthew Knepley @*/ 31647087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3165000e7ae3SMatthew Knepley { 3166000e7ae3SMatthew Knepley PetscFunctionBegin; 31670700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3168ae60f76fSBarry Smith ts->prestep = func; 3169000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3170000e7ae3SMatthew Knepley } 3171000e7ae3SMatthew Knepley 3172e74ef692SMatthew Knepley #undef __FUNCT__ 31733f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 317409ee8438SJed Brown /*@ 31753f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 31763f2090d5SJed Brown 31773f2090d5SJed Brown Collective on TS 31783f2090d5SJed Brown 31793f2090d5SJed Brown Input Parameters: 31803f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 31813f2090d5SJed Brown 31823f2090d5SJed Brown Notes: 31833f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 31843f2090d5SJed Brown so most users would not generally call this routine themselves. 31853f2090d5SJed Brown 31863f2090d5SJed Brown Level: developer 31873f2090d5SJed Brown 31883f2090d5SJed Brown .keywords: TS, timestep 31899be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 31903f2090d5SJed Brown @*/ 31917087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 31923f2090d5SJed Brown { 31933f2090d5SJed Brown PetscErrorCode ierr; 31943f2090d5SJed Brown 31953f2090d5SJed Brown PetscFunctionBegin; 31960700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3197ae60f76fSBarry Smith if (ts->prestep) { 3198*5efd42a4SStefano Zampini Vec U; 3199*5efd42a4SStefano Zampini PetscObjectState sprev,spost; 3200*5efd42a4SStefano Zampini 3201*5efd42a4SStefano Zampini ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 3202*5efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3203ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 3204*5efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3205*5efd42a4SStefano Zampini if (sprev != spost) ts->steprestart = PETSC_TRUE; 3206312ce896SJed Brown } 32073f2090d5SJed Brown PetscFunctionReturn(0); 32083f2090d5SJed Brown } 32093f2090d5SJed Brown 32103f2090d5SJed Brown #undef __FUNCT__ 3211b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 3212b8123daeSJed Brown /*@C 3213b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3214b8123daeSJed Brown called once at the beginning of each stage. 3215b8123daeSJed Brown 3216b8123daeSJed Brown Logically Collective on TS 3217b8123daeSJed Brown 3218b8123daeSJed Brown Input Parameters: 3219b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 3220b8123daeSJed Brown - func - The function 3221b8123daeSJed Brown 3222b8123daeSJed Brown Calling sequence of func: 3223b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 3224b8123daeSJed Brown 3225b8123daeSJed Brown Level: intermediate 3226b8123daeSJed Brown 3227b8123daeSJed Brown Note: 3228b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 3229b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 3230b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 3231b8123daeSJed Brown 3232b8123daeSJed Brown .keywords: TS, timestep 32339be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3234b8123daeSJed Brown @*/ 3235b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 3236b8123daeSJed Brown { 3237b8123daeSJed Brown PetscFunctionBegin; 3238b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3239ae60f76fSBarry Smith ts->prestage = func; 3240b8123daeSJed Brown PetscFunctionReturn(0); 3241b8123daeSJed Brown } 3242b8123daeSJed Brown 3243b8123daeSJed Brown #undef __FUNCT__ 32449be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage" 32459be3e283SDebojyoti Ghosh /*@C 32469be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 32479be3e283SDebojyoti Ghosh called once at the end of each stage. 32489be3e283SDebojyoti Ghosh 32499be3e283SDebojyoti Ghosh Logically Collective on TS 32509be3e283SDebojyoti Ghosh 32519be3e283SDebojyoti Ghosh Input Parameters: 32529be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 32539be3e283SDebojyoti Ghosh - func - The function 32549be3e283SDebojyoti Ghosh 32559be3e283SDebojyoti Ghosh Calling sequence of func: 32569be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 32579be3e283SDebojyoti Ghosh 32589be3e283SDebojyoti Ghosh Level: intermediate 32599be3e283SDebojyoti Ghosh 32609be3e283SDebojyoti Ghosh Note: 32619be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 32629be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 32639be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 32649be3e283SDebojyoti Ghosh 32659be3e283SDebojyoti Ghosh .keywords: TS, timestep 32669be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 32679be3e283SDebojyoti Ghosh @*/ 32689be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 32699be3e283SDebojyoti Ghosh { 32709be3e283SDebojyoti Ghosh PetscFunctionBegin; 32719be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 32729be3e283SDebojyoti Ghosh ts->poststage = func; 32739be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 32749be3e283SDebojyoti Ghosh } 32759be3e283SDebojyoti Ghosh 32769be3e283SDebojyoti Ghosh #undef __FUNCT__ 3277b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 3278b8123daeSJed Brown /*@ 3279b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 3280b8123daeSJed Brown 3281b8123daeSJed Brown Collective on TS 3282b8123daeSJed Brown 3283b8123daeSJed Brown Input Parameters: 3284b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 32859be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3286b8123daeSJed Brown 3287b8123daeSJed Brown Notes: 3288b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 3289b8123daeSJed Brown most users would not generally call this routine themselves. 3290b8123daeSJed Brown 3291b8123daeSJed Brown Level: developer 3292b8123daeSJed Brown 3293b8123daeSJed Brown .keywords: TS, timestep 32949be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 3295b8123daeSJed Brown @*/ 3296b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3297b8123daeSJed Brown { 3298b8123daeSJed Brown PetscErrorCode ierr; 3299b8123daeSJed Brown 3300b8123daeSJed Brown PetscFunctionBegin; 3301b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3302ae60f76fSBarry Smith if (ts->prestage) { 3303ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 3304b8123daeSJed Brown } 3305b8123daeSJed Brown PetscFunctionReturn(0); 3306b8123daeSJed Brown } 3307b8123daeSJed Brown 3308b8123daeSJed Brown #undef __FUNCT__ 33099be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage" 33109be3e283SDebojyoti Ghosh /*@ 33119be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 33129be3e283SDebojyoti Ghosh 33139be3e283SDebojyoti Ghosh Collective on TS 33149be3e283SDebojyoti Ghosh 33159be3e283SDebojyoti Ghosh Input Parameters: 33169be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 33179be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 33189be3e283SDebojyoti Ghosh stageindex - Stage number 33199be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 33209be3e283SDebojyoti Ghosh of stages) with the stage solutions 33219be3e283SDebojyoti Ghosh 33229be3e283SDebojyoti Ghosh Notes: 33239be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 33249be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 33259be3e283SDebojyoti Ghosh 33269be3e283SDebojyoti Ghosh Level: developer 33279be3e283SDebojyoti Ghosh 33289be3e283SDebojyoti Ghosh .keywords: TS, timestep 33299be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 33309be3e283SDebojyoti Ghosh @*/ 33319be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 33329be3e283SDebojyoti Ghosh { 33339be3e283SDebojyoti Ghosh PetscErrorCode ierr; 33349be3e283SDebojyoti Ghosh 33359be3e283SDebojyoti Ghosh PetscFunctionBegin; 33369be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 33374beae5d8SLisandro Dalcin if (ts->poststage) { 33389be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 33399be3e283SDebojyoti Ghosh } 33409be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 33419be3e283SDebojyoti Ghosh } 33429be3e283SDebojyoti Ghosh 33439be3e283SDebojyoti Ghosh #undef __FUNCT__ 3344e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 3345ac226902SBarry Smith /*@C 3346000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 33473f2090d5SJed Brown called once at the end of each time step. 3348000e7ae3SMatthew Knepley 33493f9fe445SBarry Smith Logically Collective on TS 3350000e7ae3SMatthew Knepley 3351000e7ae3SMatthew Knepley Input Parameters: 3352000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3353000e7ae3SMatthew Knepley - func - The function 3354000e7ae3SMatthew Knepley 3355000e7ae3SMatthew Knepley Calling sequence of func: 3356b8123daeSJed Brown $ func (TS ts); 3357000e7ae3SMatthew Knepley 3358000e7ae3SMatthew Knepley Level: intermediate 3359000e7ae3SMatthew Knepley 3360000e7ae3SMatthew Knepley .keywords: TS, timestep 3361b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 3362000e7ae3SMatthew Knepley @*/ 33637087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 3364000e7ae3SMatthew Knepley { 3365000e7ae3SMatthew Knepley PetscFunctionBegin; 33660700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3367ae60f76fSBarry Smith ts->poststep = func; 3368000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3369000e7ae3SMatthew Knepley } 3370000e7ae3SMatthew Knepley 3371e74ef692SMatthew Knepley #undef __FUNCT__ 33723f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 337309ee8438SJed Brown /*@ 33743f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 33753f2090d5SJed Brown 33763f2090d5SJed Brown Collective on TS 33773f2090d5SJed Brown 33783f2090d5SJed Brown Input Parameters: 33793f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 33803f2090d5SJed Brown 33813f2090d5SJed Brown Notes: 33823f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 33833f2090d5SJed Brown so most users would not generally call this routine themselves. 33843f2090d5SJed Brown 33853f2090d5SJed Brown Level: developer 33863f2090d5SJed Brown 33873f2090d5SJed Brown .keywords: TS, timestep 33883f2090d5SJed Brown @*/ 33897087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 33903f2090d5SJed Brown { 33913f2090d5SJed Brown PetscErrorCode ierr; 33923f2090d5SJed Brown 33933f2090d5SJed Brown PetscFunctionBegin; 33940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3395ae60f76fSBarry Smith if (ts->poststep) { 3396*5efd42a4SStefano Zampini Vec U; 3397*5efd42a4SStefano Zampini PetscObjectState sprev,spost; 3398*5efd42a4SStefano Zampini 3399*5efd42a4SStefano Zampini ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 3400*5efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3401ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 3402*5efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3403*5efd42a4SStefano Zampini if (sprev != spost) ts->steprestart = PETSC_TRUE; 340472ac3e02SJed Brown } 34053f2090d5SJed Brown PetscFunctionReturn(0); 34063f2090d5SJed Brown } 34073f2090d5SJed Brown 3408d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 3409d763cef2SBarry Smith 34104a2ae208SSatish Balay #undef __FUNCT__ 3411a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 3412d763cef2SBarry Smith /*@C 3413a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 3414d763cef2SBarry Smith timestep to display the iteration's progress. 3415d763cef2SBarry Smith 34163f9fe445SBarry Smith Logically Collective on TS 3417d763cef2SBarry Smith 3418d763cef2SBarry Smith Input Parameters: 3419d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3420e213d8f1SJed Brown . monitor - monitoring routine 3421329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 34220298fd71SBarry Smith monitor routine (use NULL if no context is desired) 3423b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 34240298fd71SBarry Smith (may be NULL) 3425d763cef2SBarry Smith 3426e213d8f1SJed Brown Calling sequence of monitor: 34270910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 3428d763cef2SBarry Smith 3429d763cef2SBarry Smith + ts - the TS context 343063e21af5SBarry 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) 34311f06c33eSBarry Smith . time - current time 34320910c330SBarry Smith . u - current iterate 3433d763cef2SBarry Smith - mctx - [optional] monitoring context 3434d763cef2SBarry Smith 3435d763cef2SBarry Smith Notes: 3436d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 3437d763cef2SBarry Smith already has been loaded. 3438d763cef2SBarry Smith 3439025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 3440025f1a04SBarry Smith 3441d763cef2SBarry Smith Level: intermediate 3442d763cef2SBarry Smith 3443d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3444d763cef2SBarry Smith 3445a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 3446d763cef2SBarry Smith @*/ 3447c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 3448d763cef2SBarry Smith { 344978064530SBarry Smith PetscErrorCode ierr; 345078064530SBarry Smith PetscInt i; 345178064530SBarry Smith PetscBool identical; 345278064530SBarry Smith 3453d763cef2SBarry Smith PetscFunctionBegin; 34540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 345578064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 345678064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr); 345778064530SBarry Smith if (identical) PetscFunctionReturn(0); 345878064530SBarry Smith } 345917186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 3460d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 34618704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 3462d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 3463d763cef2SBarry Smith PetscFunctionReturn(0); 3464d763cef2SBarry Smith } 3465d763cef2SBarry Smith 34664a2ae208SSatish Balay #undef __FUNCT__ 3467a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 3468d763cef2SBarry Smith /*@C 3469a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 3470d763cef2SBarry Smith 34713f9fe445SBarry Smith Logically Collective on TS 3472d763cef2SBarry Smith 3473d763cef2SBarry Smith Input Parameters: 3474d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3475d763cef2SBarry Smith 3476d763cef2SBarry Smith Notes: 3477d763cef2SBarry Smith There is no way to remove a single, specific monitor. 3478d763cef2SBarry Smith 3479d763cef2SBarry Smith Level: intermediate 3480d763cef2SBarry Smith 3481d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3482d763cef2SBarry Smith 3483a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 3484d763cef2SBarry Smith @*/ 34857087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 3486d763cef2SBarry Smith { 3487d952e501SBarry Smith PetscErrorCode ierr; 3488d952e501SBarry Smith PetscInt i; 3489d952e501SBarry Smith 3490d763cef2SBarry Smith PetscFunctionBegin; 34910700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3492d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 34938704b422SBarry Smith if (ts->monitordestroy[i]) { 34948704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 3495d952e501SBarry Smith } 3496d952e501SBarry Smith } 3497d763cef2SBarry Smith ts->numbermonitors = 0; 3498d763cef2SBarry Smith PetscFunctionReturn(0); 3499d763cef2SBarry Smith } 3500d763cef2SBarry Smith 35014a2ae208SSatish Balay #undef __FUNCT__ 3502a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 3503721cd6eeSBarry Smith /*@C 3504721cd6eeSBarry Smith TSMonitorDefault - The Default monitor, prints the timestep and time for each step 35055516499fSSatish Balay 35065516499fSSatish Balay Level: intermediate 350741251cbbSSatish Balay 35085516499fSSatish Balay .keywords: TS, set, monitor 35095516499fSSatish Balay 351063e21af5SBarry Smith .seealso: TSMonitorSet() 351141251cbbSSatish Balay @*/ 3512721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf) 3513d763cef2SBarry Smith { 3514dfbe8321SBarry Smith PetscErrorCode ierr; 3515721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 351641aca3d6SBarry Smith PetscBool iascii,ibinary; 3517d132466eSBarry Smith 3518d763cef2SBarry Smith PetscFunctionBegin; 35194d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 352041aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 352141aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr); 3522721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 352341aca3d6SBarry Smith if (iascii) { 3524649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 352563e21af5SBarry Smith if (step == -1){ /* this indicates it is an interpolated solution */ 352663e21af5SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr); 352763e21af5SBarry Smith } else { 35288392e04aSShri 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); 352963e21af5SBarry Smith } 3530649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 353141aca3d6SBarry Smith } else if (ibinary) { 353241aca3d6SBarry Smith PetscMPIInt rank; 353341aca3d6SBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 353441aca3d6SBarry Smith if (!rank) { 353541aca3d6SBarry Smith ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr); 353641aca3d6SBarry Smith } else { 353741aca3d6SBarry Smith ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr); 353841aca3d6SBarry Smith } 353941aca3d6SBarry Smith } 3540721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3541d763cef2SBarry Smith PetscFunctionReturn(0); 3542d763cef2SBarry Smith } 3543d763cef2SBarry Smith 35444a2ae208SSatish Balay #undef __FUNCT__ 35459110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorSet" 35469110b2e7SHong Zhang /*@C 35479110b2e7SHong Zhang TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every 35489110b2e7SHong Zhang timestep to display the iteration's progress. 35499110b2e7SHong Zhang 35509110b2e7SHong Zhang Logically Collective on TS 35519110b2e7SHong Zhang 35529110b2e7SHong Zhang Input Parameters: 35539110b2e7SHong Zhang + ts - the TS context obtained from TSCreate() 35549110b2e7SHong Zhang . adjointmonitor - monitoring routine 35559110b2e7SHong Zhang . adjointmctx - [optional] user-defined context for private data for the 35569110b2e7SHong Zhang monitor routine (use NULL if no context is desired) 35579110b2e7SHong Zhang - adjointmonitordestroy - [optional] routine that frees monitor context 35589110b2e7SHong Zhang (may be NULL) 35599110b2e7SHong Zhang 35609110b2e7SHong Zhang Calling sequence of monitor: 35619110b2e7SHong Zhang $ int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx) 35629110b2e7SHong Zhang 35639110b2e7SHong Zhang + ts - the TS context 35649110b2e7SHong 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 35659110b2e7SHong Zhang been interpolated to) 35669110b2e7SHong Zhang . time - current time 35679110b2e7SHong Zhang . u - current iterate 35689110b2e7SHong Zhang . numcost - number of cost functionos 35699110b2e7SHong Zhang . lambda - sensitivities to initial conditions 35709110b2e7SHong Zhang . mu - sensitivities to parameters 35719110b2e7SHong Zhang - adjointmctx - [optional] adjoint monitoring context 35729110b2e7SHong Zhang 35739110b2e7SHong Zhang Notes: 35749110b2e7SHong Zhang This routine adds an additional monitor to the list of monitors that 35759110b2e7SHong Zhang already has been loaded. 35769110b2e7SHong Zhang 35779110b2e7SHong Zhang Fortran notes: Only a single monitor function can be set for each TS object 35789110b2e7SHong Zhang 35799110b2e7SHong Zhang Level: intermediate 35809110b2e7SHong Zhang 35819110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 35829110b2e7SHong Zhang 35839110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel() 35849110b2e7SHong Zhang @*/ 35859110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**)) 35869110b2e7SHong Zhang { 358778064530SBarry Smith PetscErrorCode ierr; 358878064530SBarry Smith PetscInt i; 358978064530SBarry Smith PetscBool identical; 359078064530SBarry Smith 35919110b2e7SHong Zhang PetscFunctionBegin; 35929110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 359378064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 359478064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);CHKERRQ(ierr); 359578064530SBarry Smith if (identical) PetscFunctionReturn(0); 359678064530SBarry Smith } 35979110b2e7SHong Zhang if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set"); 35989110b2e7SHong Zhang ts->adjointmonitor[ts->numberadjointmonitors] = adjointmonitor; 35999110b2e7SHong Zhang ts->adjointmonitordestroy[ts->numberadjointmonitors] = adjointmdestroy; 36009110b2e7SHong Zhang ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx; 36019110b2e7SHong Zhang PetscFunctionReturn(0); 36029110b2e7SHong Zhang } 36039110b2e7SHong Zhang 36049110b2e7SHong Zhang #undef __FUNCT__ 36059110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorCancel" 36069110b2e7SHong Zhang /*@C 36079110b2e7SHong Zhang TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object. 36089110b2e7SHong Zhang 36099110b2e7SHong Zhang Logically Collective on TS 36109110b2e7SHong Zhang 36119110b2e7SHong Zhang Input Parameters: 36129110b2e7SHong Zhang . ts - the TS context obtained from TSCreate() 36139110b2e7SHong Zhang 36149110b2e7SHong Zhang Notes: 36159110b2e7SHong Zhang There is no way to remove a single, specific monitor. 36169110b2e7SHong Zhang 36179110b2e7SHong Zhang Level: intermediate 36189110b2e7SHong Zhang 36199110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 36209110b2e7SHong Zhang 36219110b2e7SHong Zhang .seealso: TSAdjointMonitorSet() 36229110b2e7SHong Zhang @*/ 36239110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorCancel(TS ts) 36249110b2e7SHong Zhang { 36259110b2e7SHong Zhang PetscErrorCode ierr; 36269110b2e7SHong Zhang PetscInt i; 36279110b2e7SHong Zhang 36289110b2e7SHong Zhang PetscFunctionBegin; 36299110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36309110b2e7SHong Zhang for (i=0; i<ts->numberadjointmonitors; i++) { 36319110b2e7SHong Zhang if (ts->adjointmonitordestroy[i]) { 36329110b2e7SHong Zhang ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 36339110b2e7SHong Zhang } 36349110b2e7SHong Zhang } 36359110b2e7SHong Zhang ts->numberadjointmonitors = 0; 36369110b2e7SHong Zhang PetscFunctionReturn(0); 36379110b2e7SHong Zhang } 36389110b2e7SHong Zhang 36399110b2e7SHong Zhang #undef __FUNCT__ 3640110eb670SHong Zhang #define __FUNCT__ "TSAdjointMonitorDefault" 3641721cd6eeSBarry Smith /*@C 3642721cd6eeSBarry Smith TSAdjointMonitorDefault - the default monitor of adjoint computations 3643110eb670SHong Zhang 3644110eb670SHong Zhang Level: intermediate 3645110eb670SHong Zhang 3646110eb670SHong Zhang .keywords: TS, set, monitor 3647110eb670SHong Zhang 3648110eb670SHong Zhang .seealso: TSAdjointMonitorSet() 3649110eb670SHong Zhang @*/ 3650721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf) 3651110eb670SHong Zhang { 3652110eb670SHong Zhang PetscErrorCode ierr; 3653721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 3654110eb670SHong Zhang 3655110eb670SHong Zhang PetscFunctionBegin; 3656110eb670SHong Zhang PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3657721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 3658110eb670SHong Zhang ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3659110eb670SHong 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); 3660110eb670SHong Zhang ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3661721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3662110eb670SHong Zhang PetscFunctionReturn(0); 3663110eb670SHong Zhang } 3664110eb670SHong Zhang 3665110eb670SHong Zhang #undef __FUNCT__ 3666cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 3667cd652676SJed Brown /*@ 3668cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3669cd652676SJed Brown 3670cd652676SJed Brown Collective on TS 3671cd652676SJed Brown 3672cd652676SJed Brown Input Argument: 3673cd652676SJed Brown + ts - time stepping context 3674cd652676SJed Brown - t - time to interpolate to 3675cd652676SJed Brown 3676cd652676SJed Brown Output Argument: 36770910c330SBarry Smith . U - state at given time 3678cd652676SJed Brown 3679cd652676SJed Brown Level: intermediate 3680cd652676SJed Brown 3681cd652676SJed Brown Developer Notes: 3682cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3683cd652676SJed Brown 3684cd652676SJed Brown .keywords: TS, set 3685cd652676SJed Brown 3686874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve() 3687cd652676SJed Brown @*/ 36880910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3689cd652676SJed Brown { 3690cd652676SJed Brown PetscErrorCode ierr; 3691cd652676SJed Brown 3692cd652676SJed Brown PetscFunctionBegin; 3693cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3694b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3695be5899b3SLisandro 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); 3696ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 36970910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3698cd652676SJed Brown PetscFunctionReturn(0); 3699cd652676SJed Brown } 3700cd652676SJed Brown 3701cd652676SJed Brown #undef __FUNCT__ 37024a2ae208SSatish Balay #define __FUNCT__ "TSStep" 3703d763cef2SBarry Smith /*@ 37046d9e5789SSean Farley TSStep - Steps one time step 3705d763cef2SBarry Smith 3706d763cef2SBarry Smith Collective on TS 3707d763cef2SBarry Smith 3708d763cef2SBarry Smith Input Parameter: 3709d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3710d763cef2SBarry Smith 371127829d71SBarry Smith Level: developer 3712d763cef2SBarry Smith 3713b8123daeSJed Brown Notes: 371427829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 371527829d71SBarry Smith 3716b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3717b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3718b8123daeSJed Brown 371925cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 372025cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 372125cb2221SBarry Smith 3722d763cef2SBarry Smith .keywords: TS, timestep, solve 3723d763cef2SBarry Smith 37249be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3725d763cef2SBarry Smith @*/ 3726193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3727d763cef2SBarry Smith { 3728dfbe8321SBarry Smith PetscErrorCode ierr; 3729fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3730be5899b3SLisandro Dalcin PetscReal ptime; 3731d763cef2SBarry Smith 3732d763cef2SBarry Smith PetscFunctionBegin; 37330700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3734fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3735fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3736fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3737fffbeea8SBarry Smith " type = {Preprint},\n" 3738fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3739fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3740302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3741fffbeea8SBarry Smith 3742d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 374368bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3744d405a339SMatthew Knepley 3745a6772fa2SLisandro 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()"); 3746be5899b3SLisandro 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"); 3747a6772fa2SLisandro Dalcin 3748be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 3749362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3750be5899b3SLisandro Dalcin ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev; 3751e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3752d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3753193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3754d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3755be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 3756be5899b3SLisandro Dalcin ts->steps++; ts->total_steps++; 3757be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 375828d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3759362cd11cSLisandro Dalcin 3760362cd11cSLisandro Dalcin if (ts->reason < 0) { 3761cef5090cSJed Brown if (ts->errorifstepfailed) { 376208c7845fSBarry 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]); 376308c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3764d2daff3dSHong Zhang } 376508c7845fSBarry Smith } else if (!ts->reason) { 376608c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 376708c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 376808c7845fSBarry Smith } 376908c7845fSBarry Smith PetscFunctionReturn(0); 377008c7845fSBarry Smith } 377108c7845fSBarry Smith 377208c7845fSBarry Smith #undef __FUNCT__ 377308c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep" 377408c7845fSBarry Smith /*@ 3775b957a604SHong Zhang TSAdjointStep - Steps one time step backward in the adjoint run 377608c7845fSBarry Smith 377708c7845fSBarry Smith Collective on TS 377808c7845fSBarry Smith 377908c7845fSBarry Smith Input Parameter: 378008c7845fSBarry Smith . ts - the TS context obtained from TSCreate() 378108c7845fSBarry Smith 37826a4d4014SLisandro Dalcin Level: intermediate 37836a4d4014SLisandro Dalcin 3784b957a604SHong Zhang .keywords: TS, adjoint, step 37856a4d4014SLisandro Dalcin 3786b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve() 37876a4d4014SLisandro Dalcin @*/ 378808c7845fSBarry Smith PetscErrorCode TSAdjointStep(TS ts) 37896a4d4014SLisandro Dalcin { 379008c7845fSBarry Smith DM dm; 37916a4d4014SLisandro Dalcin PetscErrorCode ierr; 37926a4d4014SLisandro Dalcin 37936a4d4014SLisandro Dalcin PetscFunctionBegin; 37944a2ae208SSatish Balay PetscValidHeaderSpecific(ts,TS_CLASSID,1); 379508c7845fSBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 379608c7845fSBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3797d763cef2SBarry Smith 3798685405a1SBarry Smith ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr); 3799d763cef2SBarry Smith 3800be5899b3SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3801be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime; 380242f2b339SBarry 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); 3803be5899b3SLisandro Dalcin ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 380442f2b339SBarry Smith ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr); 38058d0ad7a8SHong Zhang ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 3806be5899b3SLisandro Dalcin ts->steps++; ts->total_steps--; 3807362cd11cSLisandro Dalcin 3808362cd11cSLisandro Dalcin if (ts->reason < 0) { 3809cef5090cSJed Brown if (ts->errorifstepfailed) { 38106c4ed002SBarry 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]); 38116c4ed002SBarry 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]); 38126c4ed002SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3813cef5090cSJed Brown } 3814362cd11cSLisandro Dalcin } else if (!ts->reason) { 381573b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3816362cd11cSLisandro Dalcin } 3817d763cef2SBarry Smith PetscFunctionReturn(0); 3818d763cef2SBarry Smith } 3819d763cef2SBarry Smith 3820d763cef2SBarry Smith #undef __FUNCT__ 38217cbde773SLisandro Dalcin #define __FUNCT__ "TSEvaluateWLTE" 38227cbde773SLisandro Dalcin /*@ 38237cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 38247cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 38257cbde773SLisandro Dalcin 38267cbde773SLisandro Dalcin Collective on TS 38277cbde773SLisandro Dalcin 38287cbde773SLisandro Dalcin Input Arguments: 38297cbde773SLisandro Dalcin + ts - time stepping context 38307cbde773SLisandro Dalcin . wnormtype - norm type, either NORM_2 or NORM_INFINITY 38317cbde773SLisandro Dalcin - order - optional, desired order for the error evaluation or PETSC_DECIDE 38327cbde773SLisandro Dalcin 38337cbde773SLisandro Dalcin Output Arguments: 38347cbde773SLisandro Dalcin + order - optional, the actual order of the error evaluation 38357cbde773SLisandro Dalcin - wlte - the weighted local truncation error norm 38367cbde773SLisandro Dalcin 38377cbde773SLisandro Dalcin Level: advanced 38387cbde773SLisandro Dalcin 38397cbde773SLisandro Dalcin Notes: 38407cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 38417cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 38427cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 38437cbde773SLisandro Dalcin 38447cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm() 38457cbde773SLisandro Dalcin @*/ 38467cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte) 38477cbde773SLisandro Dalcin { 38487cbde773SLisandro Dalcin PetscErrorCode ierr; 38497cbde773SLisandro Dalcin 38507cbde773SLisandro Dalcin PetscFunctionBegin; 38517cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 38527cbde773SLisandro Dalcin PetscValidType(ts,1); 38537cbde773SLisandro Dalcin PetscValidLogicalCollectiveEnum(ts,wnormtype,4); 38547cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order,3); 38557cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts,*order,3); 38567cbde773SLisandro Dalcin PetscValidRealPointer(wlte,4); 38577cbde773SLisandro Dalcin if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 38587cbde773SLisandro Dalcin if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name); 38597cbde773SLisandro Dalcin ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr); 38607cbde773SLisandro Dalcin PetscFunctionReturn(0); 38617cbde773SLisandro Dalcin } 38627cbde773SLisandro Dalcin 38637cbde773SLisandro Dalcin #undef __FUNCT__ 386405175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 386505175c85SJed Brown /*@ 386605175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 386705175c85SJed Brown 38681c3436cfSJed Brown Collective on TS 386905175c85SJed Brown 387005175c85SJed Brown Input Arguments: 38711c3436cfSJed Brown + ts - time stepping context 38721c3436cfSJed Brown . order - desired order of accuracy 38730298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 387405175c85SJed Brown 387505175c85SJed Brown Output Arguments: 38760910c330SBarry Smith . U - state at the end of the current step 387705175c85SJed Brown 387805175c85SJed Brown Level: advanced 387905175c85SJed Brown 3880108c343cSJed Brown Notes: 3881108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3882108c343cSJed 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. 3883108c343cSJed Brown 38841c3436cfSJed Brown .seealso: TSStep(), TSAdapt 388505175c85SJed Brown @*/ 38860910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 388705175c85SJed Brown { 388805175c85SJed Brown PetscErrorCode ierr; 388905175c85SJed Brown 389005175c85SJed Brown PetscFunctionBegin; 389105175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 389205175c85SJed Brown PetscValidType(ts,1); 38930910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3894ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 38950910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 389605175c85SJed Brown PetscFunctionReturn(0); 389705175c85SJed Brown } 389805175c85SJed Brown 3899b1cb36f3SHong Zhang #undef __FUNCT__ 3900b1cb36f3SHong Zhang #define __FUNCT__ "TSForwardCostIntegral" 3901b1cb36f3SHong Zhang /*@ 3902b1cb36f3SHong Zhang TSForwardCostIntegral - Evaluate the cost integral in the forward run. 3903b1cb36f3SHong Zhang 3904b1cb36f3SHong Zhang Collective on TS 3905b1cb36f3SHong Zhang 3906b1cb36f3SHong Zhang Input Arguments: 3907b1cb36f3SHong Zhang . ts - time stepping context 3908b1cb36f3SHong Zhang 3909b1cb36f3SHong Zhang Level: advanced 3910b1cb36f3SHong Zhang 3911b1cb36f3SHong Zhang Notes: 3912b1cb36f3SHong Zhang This function cannot be called until TSStep() has been completed. 3913b1cb36f3SHong Zhang 3914b1cb36f3SHong Zhang .seealso: TSSolve(), TSAdjointCostIntegral() 3915b1cb36f3SHong Zhang @*/ 3916b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts) 3917b1cb36f3SHong Zhang { 3918b1cb36f3SHong Zhang PetscErrorCode ierr; 3919b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3920b1cb36f3SHong 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); 3921b1cb36f3SHong Zhang ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr); 3922b1cb36f3SHong Zhang PetscFunctionReturn(0); 3923b1cb36f3SHong Zhang } 3924d67e68b7SBarry Smith 392505175c85SJed Brown #undef __FUNCT__ 3926d763cef2SBarry Smith #define __FUNCT__ "TSSolve" 3927d763cef2SBarry Smith /*@ 3928d763cef2SBarry Smith TSSolve - Steps the requested number of timesteps. 3929d763cef2SBarry Smith 3930d763cef2SBarry Smith Collective on TS 3931d763cef2SBarry Smith 3932d763cef2SBarry Smith Input Parameter: 3933d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 393463e21af5SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used, 393563e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 39365a3a76d0SJed Brown 3937d763cef2SBarry Smith Level: beginner 3938d763cef2SBarry Smith 39395a3a76d0SJed Brown Notes: 39405a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 39415a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 39425a3a76d0SJed Brown stepped over the final time. 39435a3a76d0SJed Brown 3944d763cef2SBarry Smith .keywords: TS, timestep, solve 3945d763cef2SBarry Smith 394663e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime() 3947d763cef2SBarry Smith @*/ 3948cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 3949d763cef2SBarry Smith { 3950b06615a5SLisandro Dalcin Vec solution; 3951d763cef2SBarry Smith PetscErrorCode ierr; 3952d763cef2SBarry Smith 3953d763cef2SBarry Smith PetscFunctionBegin; 3954d763cef2SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3955f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 3956feed9e9dSBarry Smith 395749354f04SShri 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 */ 3958b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 39590910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 3960b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3961b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3962b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 39635a3a76d0SJed Brown } 39640910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3965bbd56ea5SKarl Rupp } else if (u) { 39660910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 39675a3a76d0SJed Brown } 3968b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 396968bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3970a6772fa2SLisandro Dalcin 3971a6772fa2SLisandro 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()"); 3972a6772fa2SLisandro 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"); 3973a6772fa2SLisandro Dalcin 3974d763cef2SBarry Smith /* reset time step and iteration counters */ 3975193ac0bcSJed Brown ts->steps = 0; 39765ef26d82SJed Brown ts->ksp_its = 0; 39775ef26d82SJed Brown ts->snes_its = 0; 3978c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3979c610991cSLisandro Dalcin ts->reject = 0; 3980193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3981193ac0bcSJed Brown 3982ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3983f05ece33SBarry Smith 3984193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3985193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 3986a6772fa2SLisandro Dalcin if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3987cc708dedSBarry Smith ts->solvetime = ts->ptime; 3988a6772fa2SLisandro Dalcin solution = ts->vec_sol; 3989be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 3990db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3991db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3992cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39936427ac75SLisandro Dalcin ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 399428d5b5d6SLisandro Dalcin ts->steprollback = PETSC_FALSE; 399528d5b5d6SLisandro Dalcin ts->steprestart = PETSC_TRUE; 39966427ac75SLisandro Dalcin 3997e1a7a14fSJed Brown while (!ts->reason) { 3998193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39999687d888SLisandro Dalcin if (!ts->steprollback) { 40009687d888SLisandro Dalcin ierr = TSPreStep(ts);CHKERRQ(ierr); 40019687d888SLisandro Dalcin } 4002193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 4003e783b05fSHong 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. */ 4004b1cb36f3SHong Zhang ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr); 4005b1cb36f3SHong Zhang } 4006e783b05fSHong 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. */ 4007e783b05fSHong Zhang if (!ts->steprollback) { 4008cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 40091eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 4010aeb4809dSShri Abhyankar } 4011193ac0bcSJed Brown } 401263e21af5SBarry Smith ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 40136427ac75SLisandro Dalcin 401449354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 40150910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 4016cc708dedSBarry Smith ts->solvetime = ts->max_time; 4017b06615a5SLisandro Dalcin solution = u; 401863e21af5SBarry Smith ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr); 40190574a7fbSJed Brown } else { 4020ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 4021cc708dedSBarry Smith ts->solvetime = ts->ptime; 4022b06615a5SLisandro Dalcin solution = ts->vec_sol; 40230574a7fbSJed Brown } 4024193ac0bcSJed Brown } 4025d2daff3dSHong Zhang 4026ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 402763e21af5SBarry Smith ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr); 402856f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 4029ef222394SBarry Smith if (ts->adjoint_solve) { 4030ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 40312b0a91c0SBarry Smith } 4032d763cef2SBarry Smith PetscFunctionReturn(0); 4033d763cef2SBarry Smith } 4034d763cef2SBarry Smith 4035d763cef2SBarry Smith #undef __FUNCT__ 4036b1cb36f3SHong Zhang #define __FUNCT__ "TSAdjointCostIntegral" 4037b1cb36f3SHong Zhang /*@ 4038b1cb36f3SHong Zhang TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run. 4039b1cb36f3SHong Zhang 4040b1cb36f3SHong Zhang Collective on TS 4041b1cb36f3SHong Zhang 4042b1cb36f3SHong Zhang Input Arguments: 4043b1cb36f3SHong Zhang . ts - time stepping context 4044b1cb36f3SHong Zhang 4045b1cb36f3SHong Zhang Level: advanced 4046b1cb36f3SHong Zhang 4047b1cb36f3SHong Zhang Notes: 4048b1cb36f3SHong Zhang This function cannot be called until TSAdjointStep() has been completed. 4049b1cb36f3SHong Zhang 4050b1cb36f3SHong Zhang .seealso: TSAdjointSolve(), TSAdjointStep 4051b1cb36f3SHong Zhang @*/ 4052b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts) 4053b1cb36f3SHong Zhang { 4054b1cb36f3SHong Zhang PetscErrorCode ierr; 4055b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4056b1cb36f3SHong 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); 4057b1cb36f3SHong Zhang ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr); 4058b1cb36f3SHong Zhang PetscFunctionReturn(0); 4059b1cb36f3SHong Zhang } 4060b1cb36f3SHong Zhang 4061b1cb36f3SHong Zhang #undef __FUNCT__ 4062c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve" 4063c88f2d44SBarry Smith /*@ 4064c88f2d44SBarry Smith TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 4065c88f2d44SBarry Smith 4066c88f2d44SBarry Smith Collective on TS 4067c88f2d44SBarry Smith 4068c88f2d44SBarry Smith Input Parameter: 40692c39e106SBarry Smith . ts - the TS context obtained from TSCreate() 4070c88f2d44SBarry Smith 4071e87fd094SBarry Smith Options Database: 4072e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions 4073e87fd094SBarry Smith 4074c88f2d44SBarry Smith Level: intermediate 4075c88f2d44SBarry Smith 4076c88f2d44SBarry Smith Notes: 4077c88f2d44SBarry Smith This must be called after a call to TSSolve() that solves the forward problem 4078c88f2d44SBarry Smith 407973b18844SBarry Smith By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time 408073b18844SBarry Smith 4081c88f2d44SBarry Smith .keywords: TS, timestep, solve 4082c88f2d44SBarry Smith 4083b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep() 4084c88f2d44SBarry Smith @*/ 40852c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts) 4086c88f2d44SBarry Smith { 4087c88f2d44SBarry Smith PetscErrorCode ierr; 4088c88f2d44SBarry Smith 4089c88f2d44SBarry Smith PetscFunctionBegin; 4090c88f2d44SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4091c88f2d44SBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 409268bece0bSHong Zhang 4093c88f2d44SBarry Smith /* reset time step and iteration counters */ 4094c88f2d44SBarry Smith ts->steps = 0; 4095c88f2d44SBarry Smith ts->ksp_its = 0; 4096c88f2d44SBarry Smith ts->snes_its = 0; 4097c88f2d44SBarry Smith ts->num_snes_failures = 0; 4098c88f2d44SBarry Smith ts->reject = 0; 4099c88f2d44SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 4100c88f2d44SBarry Smith 410173b18844SBarry Smith if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps; 4102c88f2d44SBarry Smith 410373b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 4104c88f2d44SBarry Smith while (!ts->reason) { 410555c52731SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 41069110b2e7SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 41076427ac75SLisandro Dalcin ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr); 4108616946c1SHong Zhang ierr = TSAdjointStep(ts);CHKERRQ(ierr); 4109b1cb36f3SHong Zhang if (ts->vec_costintegral && !ts->costintegralfwd) { 4110b1cb36f3SHong Zhang ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr); 4111b1cb36f3SHong Zhang } 4112c88f2d44SBarry Smith } 411326656371SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 411426656371SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 4115c88f2d44SBarry Smith ts->solvetime = ts->ptime; 4116e210cd0eSHong Zhang ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr); 4117685405a1SBarry Smith ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr); 4118c88f2d44SBarry Smith PetscFunctionReturn(0); 4119c88f2d44SBarry Smith } 4120c88f2d44SBarry Smith 4121c88f2d44SBarry Smith #undef __FUNCT__ 4122d763cef2SBarry Smith #define __FUNCT__ "TSMonitor" 41237db568b7SBarry Smith /*@C 4124228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 4125228d79bcSJed Brown 4126228d79bcSJed Brown Collective on TS 4127228d79bcSJed Brown 4128228d79bcSJed Brown Input Parameters: 4129228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 4130228d79bcSJed Brown . step - step number that has just completed 4131228d79bcSJed Brown . ptime - model time of the state 41320910c330SBarry Smith - u - state at the current model time 4133228d79bcSJed Brown 4134228d79bcSJed Brown Notes: 41357db568b7SBarry Smith TSMonitor() is typically used automatically within the time stepping implementations. 41367db568b7SBarry Smith Users would almost never call this routine directly. 4137228d79bcSJed Brown 413863e21af5SBarry Smith A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions 413963e21af5SBarry Smith 41407db568b7SBarry Smith Level: developer 4141228d79bcSJed Brown 4142228d79bcSJed Brown .keywords: TS, timestep 4143228d79bcSJed Brown @*/ 41440910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 4145d763cef2SBarry Smith { 4146d6152f81SLisandro Dalcin DM dm; 4147a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 4148d6152f81SLisandro Dalcin PetscErrorCode ierr; 4149d763cef2SBarry Smith 4150d763cef2SBarry Smith PetscFunctionBegin; 4151b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4152b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4153d6152f81SLisandro Dalcin 4154d6152f81SLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 4155d6152f81SLisandro Dalcin ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr); 4156d6152f81SLisandro Dalcin 41575edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 4158d763cef2SBarry Smith for (i=0; i<n; i++) { 41590910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 4160d763cef2SBarry Smith } 41615edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 4162d763cef2SBarry Smith PetscFunctionReturn(0); 4163d763cef2SBarry Smith } 4164d763cef2SBarry Smith 41659110b2e7SHong Zhang #undef __FUNCT__ 41669110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitor" 41677db568b7SBarry Smith /*@C 41689110b2e7SHong Zhang TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet() 41699110b2e7SHong Zhang 41709110b2e7SHong Zhang Collective on TS 41719110b2e7SHong Zhang 41729110b2e7SHong Zhang Input Parameters: 41739110b2e7SHong Zhang + ts - time stepping context obtained from TSCreate() 41749110b2e7SHong Zhang . step - step number that has just completed 41759110b2e7SHong Zhang . ptime - model time of the state 41769110b2e7SHong Zhang . u - state at the current model time 41779110b2e7SHong Zhang . numcost - number of cost functions (dimension of lambda or mu) 41789110b2e7SHong Zhang . lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 41799110b2e7SHong Zhang - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 41809110b2e7SHong Zhang 41819110b2e7SHong Zhang Notes: 41827db568b7SBarry Smith TSAdjointMonitor() is typically used automatically within the time stepping implementations. 41837db568b7SBarry Smith Users would almost never call this routine directly. 41849110b2e7SHong Zhang 41857db568b7SBarry Smith Level: developer 41869110b2e7SHong Zhang 41879110b2e7SHong Zhang .keywords: TS, timestep 41889110b2e7SHong Zhang @*/ 41899110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu) 41909110b2e7SHong Zhang { 41919110b2e7SHong Zhang PetscErrorCode ierr; 41929110b2e7SHong Zhang PetscInt i,n = ts->numberadjointmonitors; 41939110b2e7SHong Zhang 41949110b2e7SHong Zhang PetscFunctionBegin; 41959110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 41969110b2e7SHong Zhang PetscValidHeaderSpecific(u,VEC_CLASSID,4); 41979110b2e7SHong Zhang ierr = VecLockPush(u);CHKERRQ(ierr); 41989110b2e7SHong Zhang for (i=0; i<n; i++) { 41999110b2e7SHong Zhang ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 42009110b2e7SHong Zhang } 42019110b2e7SHong Zhang ierr = VecLockPop(u);CHKERRQ(ierr); 42029110b2e7SHong Zhang PetscFunctionReturn(0); 42039110b2e7SHong Zhang } 42049110b2e7SHong Zhang 4205d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 42064a2ae208SSatish Balay #undef __FUNCT__ 4207a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 4208d763cef2SBarry Smith /*@C 42097db568b7SBarry Smith TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 4210a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 4211d763cef2SBarry Smith 4212d763cef2SBarry Smith Collective on TS 4213d763cef2SBarry Smith 4214d763cef2SBarry Smith Input Parameters: 4215d763cef2SBarry Smith + host - the X display to open, or null for the local machine 4216d763cef2SBarry Smith . label - the title to put in the title bar 42177c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 4218a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 4219a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 4220d763cef2SBarry Smith 4221d763cef2SBarry Smith Output Parameter: 42220b039ecaSBarry Smith . ctx - the context 4223d763cef2SBarry Smith 4224d763cef2SBarry Smith Options Database Key: 42254f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 42267db568b7SBarry Smith . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 42277db568b7SBarry Smith . -ts_monitor_lg_error - monitor the error 42287db568b7SBarry Smith . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 42297db568b7SBarry Smith . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 4230b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 4231d763cef2SBarry Smith 4232d763cef2SBarry Smith Notes: 4233a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 4234d763cef2SBarry Smith 42357db568b7SBarry Smith One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 42367db568b7SBarry Smith 42377db568b7SBarry 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 42387db568b7SBarry 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 42397db568b7SBarry Smith as the first argument. 42407db568b7SBarry Smith 42417db568b7SBarry Smith One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 42427db568b7SBarry Smith 42437db568b7SBarry Smith 4244d763cef2SBarry Smith Level: intermediate 4245d763cef2SBarry Smith 42467db568b7SBarry Smith .keywords: TS, monitor, line graph, residual 4247d763cef2SBarry Smith 42487db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 42497db568b7SBarry Smith TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 42507db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 42517db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 42527db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 42537c922b88SBarry Smith 4254d763cef2SBarry Smith @*/ 4255a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 4256d763cef2SBarry Smith { 42577f52daa2SLisandro Dalcin PetscDraw draw; 4258dfbe8321SBarry Smith PetscErrorCode ierr; 4259d763cef2SBarry Smith 4260d763cef2SBarry Smith PetscFunctionBegin; 4261b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 42627f52daa2SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 42637f52daa2SLisandro Dalcin ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 42647f52daa2SLisandro Dalcin ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 4265287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 42667f52daa2SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 4267a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 4268d763cef2SBarry Smith PetscFunctionReturn(0); 4269d763cef2SBarry Smith } 4270d763cef2SBarry Smith 42714a2ae208SSatish Balay #undef __FUNCT__ 42724f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 4273b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 4274d763cef2SBarry Smith { 42750b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4276c32365f1SBarry Smith PetscReal x = ptime,y; 4277dfbe8321SBarry Smith PetscErrorCode ierr; 4278d763cef2SBarry Smith 4279d763cef2SBarry Smith PetscFunctionBegin; 428063e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */ 4281b06615a5SLisandro Dalcin if (!step) { 4282a9f9c1f6SBarry Smith PetscDrawAxis axis; 4283a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 42846934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr); 4285a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4286a9f9c1f6SBarry Smith } 4287c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 42880b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4289b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 42900b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 42916934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 42923923b477SBarry Smith } 4293d763cef2SBarry Smith PetscFunctionReturn(0); 4294d763cef2SBarry Smith } 4295d763cef2SBarry Smith 42964a2ae208SSatish Balay #undef __FUNCT__ 4297a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 4298d763cef2SBarry Smith /*@C 4299a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 4300a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 4301d763cef2SBarry Smith 43020b039ecaSBarry Smith Collective on TSMonitorLGCtx 4303d763cef2SBarry Smith 4304d763cef2SBarry Smith Input Parameter: 43050b039ecaSBarry Smith . ctx - the monitor context 4306d763cef2SBarry Smith 4307d763cef2SBarry Smith Level: intermediate 4308d763cef2SBarry Smith 4309d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 4310d763cef2SBarry Smith 43114f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 4312d763cef2SBarry Smith @*/ 4313a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 4314d763cef2SBarry Smith { 4315dfbe8321SBarry Smith PetscErrorCode ierr; 4316d763cef2SBarry Smith 4317d763cef2SBarry Smith PetscFunctionBegin; 43187684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 43197684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 43207684fa3eSBarry Smith } 43210b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 432231152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 4323387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 4324387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 4325387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 43260b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 4327d763cef2SBarry Smith PetscFunctionReturn(0); 4328d763cef2SBarry Smith } 4329d763cef2SBarry Smith 43304a2ae208SSatish Balay #undef __FUNCT__ 43314a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 4332d763cef2SBarry Smith /*@ 4333b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4334d763cef2SBarry Smith 4335d763cef2SBarry Smith Not Collective 4336d763cef2SBarry Smith 4337d763cef2SBarry Smith Input Parameter: 4338d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 4339d763cef2SBarry Smith 4340d763cef2SBarry Smith Output Parameter: 434163e21af5SBarry Smith . t - the current time. This time may not corresponds to the final time set with TSSetDuration(), use TSGetSolveTime(). 4342d763cef2SBarry Smith 4343d763cef2SBarry Smith Level: beginner 4344d763cef2SBarry Smith 4345b8123daeSJed Brown Note: 4346b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 43479be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 4348b8123daeSJed Brown 434963e21af5SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep(), TSGetSolveTime() 4350d763cef2SBarry Smith 4351d763cef2SBarry Smith .keywords: TS, get, time 4352d763cef2SBarry Smith @*/ 43537087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 4354d763cef2SBarry Smith { 4355d763cef2SBarry Smith PetscFunctionBegin; 43560700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4357f7cf8827SBarry Smith PetscValidRealPointer(t,2); 4358d763cef2SBarry Smith *t = ts->ptime; 4359d763cef2SBarry Smith PetscFunctionReturn(0); 4360d763cef2SBarry Smith } 4361d763cef2SBarry Smith 43624a2ae208SSatish Balay #undef __FUNCT__ 4363e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime" 4364e5e524a1SHong Zhang /*@ 4365e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4366e5e524a1SHong Zhang 4367e5e524a1SHong Zhang Not Collective 4368e5e524a1SHong Zhang 4369e5e524a1SHong Zhang Input Parameter: 4370e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 4371e5e524a1SHong Zhang 4372e5e524a1SHong Zhang Output Parameter: 4373e5e524a1SHong Zhang . t - the previous time 4374e5e524a1SHong Zhang 4375e5e524a1SHong Zhang Level: beginner 4376e5e524a1SHong Zhang 4377e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 4378e5e524a1SHong Zhang 4379e5e524a1SHong Zhang .keywords: TS, get, time 4380e5e524a1SHong Zhang @*/ 4381e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 4382e5e524a1SHong Zhang { 4383e5e524a1SHong Zhang PetscFunctionBegin; 4384e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4385e5e524a1SHong Zhang PetscValidRealPointer(t,2); 4386e5e524a1SHong Zhang *t = ts->ptime_prev; 4387e5e524a1SHong Zhang PetscFunctionReturn(0); 4388e5e524a1SHong Zhang } 4389e5e524a1SHong Zhang 4390e5e524a1SHong Zhang #undef __FUNCT__ 43916a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 43926a4d4014SLisandro Dalcin /*@ 43936a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 43946a4d4014SLisandro Dalcin 43953f9fe445SBarry Smith Logically Collective on TS 43966a4d4014SLisandro Dalcin 43976a4d4014SLisandro Dalcin Input Parameters: 43986a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 43996a4d4014SLisandro Dalcin - time - the time 44006a4d4014SLisandro Dalcin 44016a4d4014SLisandro Dalcin Level: intermediate 44026a4d4014SLisandro Dalcin 44036a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 44046a4d4014SLisandro Dalcin 44056a4d4014SLisandro Dalcin .keywords: TS, set, time 44066a4d4014SLisandro Dalcin @*/ 44077087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 44086a4d4014SLisandro Dalcin { 44096a4d4014SLisandro Dalcin PetscFunctionBegin; 44100700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4411c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 44126a4d4014SLisandro Dalcin ts->ptime = t; 44136a4d4014SLisandro Dalcin PetscFunctionReturn(0); 44146a4d4014SLisandro Dalcin } 44156a4d4014SLisandro Dalcin 44166a4d4014SLisandro Dalcin #undef __FUNCT__ 44174a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 4418d763cef2SBarry Smith /*@C 4419d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4420d763cef2SBarry Smith TS options in the database. 4421d763cef2SBarry Smith 44223f9fe445SBarry Smith Logically Collective on TS 4423d763cef2SBarry Smith 4424d763cef2SBarry Smith Input Parameter: 4425d763cef2SBarry Smith + ts - The TS context 4426d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4427d763cef2SBarry Smith 4428d763cef2SBarry Smith Notes: 4429d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4430d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4431d763cef2SBarry Smith hyphen. 4432d763cef2SBarry Smith 4433d763cef2SBarry Smith Level: advanced 4434d763cef2SBarry Smith 4435d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 4436d763cef2SBarry Smith 4437d763cef2SBarry Smith .seealso: TSSetFromOptions() 4438d763cef2SBarry Smith 4439d763cef2SBarry Smith @*/ 44407087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 4441d763cef2SBarry Smith { 4442dfbe8321SBarry Smith PetscErrorCode ierr; 4443089b2837SJed Brown SNES snes; 4444d763cef2SBarry Smith 4445d763cef2SBarry Smith PetscFunctionBegin; 44460700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4447d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4448089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4449089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4450d763cef2SBarry Smith PetscFunctionReturn(0); 4451d763cef2SBarry Smith } 4452d763cef2SBarry Smith 4453d763cef2SBarry Smith 44544a2ae208SSatish Balay #undef __FUNCT__ 44554a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 4456d763cef2SBarry Smith /*@C 4457d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4458d763cef2SBarry Smith TS options in the database. 4459d763cef2SBarry Smith 44603f9fe445SBarry Smith Logically Collective on TS 4461d763cef2SBarry Smith 4462d763cef2SBarry Smith Input Parameter: 4463d763cef2SBarry Smith + ts - The TS context 4464d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4465d763cef2SBarry Smith 4466d763cef2SBarry Smith Notes: 4467d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4468d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4469d763cef2SBarry Smith hyphen. 4470d763cef2SBarry Smith 4471d763cef2SBarry Smith Level: advanced 4472d763cef2SBarry Smith 4473d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 4474d763cef2SBarry Smith 4475d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 4476d763cef2SBarry Smith 4477d763cef2SBarry Smith @*/ 44787087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4479d763cef2SBarry Smith { 4480dfbe8321SBarry Smith PetscErrorCode ierr; 4481089b2837SJed Brown SNES snes; 4482d763cef2SBarry Smith 4483d763cef2SBarry Smith PetscFunctionBegin; 44840700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4485d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4486089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4487089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4488d763cef2SBarry Smith PetscFunctionReturn(0); 4489d763cef2SBarry Smith } 4490d763cef2SBarry Smith 44914a2ae208SSatish Balay #undef __FUNCT__ 44924a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 4493d763cef2SBarry Smith /*@C 4494d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4495d763cef2SBarry Smith TS options in the database. 4496d763cef2SBarry Smith 4497d763cef2SBarry Smith Not Collective 4498d763cef2SBarry Smith 4499d763cef2SBarry Smith Input Parameter: 4500d763cef2SBarry Smith . ts - The TS context 4501d763cef2SBarry Smith 4502d763cef2SBarry Smith Output Parameter: 4503d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4504d763cef2SBarry Smith 4505d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 4506d763cef2SBarry Smith sufficient length to hold the prefix. 4507d763cef2SBarry Smith 4508d763cef2SBarry Smith Level: intermediate 4509d763cef2SBarry Smith 4510d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 4511d763cef2SBarry Smith 4512d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 4513d763cef2SBarry Smith @*/ 45147087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4515d763cef2SBarry Smith { 4516dfbe8321SBarry Smith PetscErrorCode ierr; 4517d763cef2SBarry Smith 4518d763cef2SBarry Smith PetscFunctionBegin; 45190700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45204482741eSBarry Smith PetscValidPointer(prefix,2); 4521d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4522d763cef2SBarry Smith PetscFunctionReturn(0); 4523d763cef2SBarry Smith } 4524d763cef2SBarry Smith 45254a2ae208SSatish Balay #undef __FUNCT__ 45264a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 4527d763cef2SBarry Smith /*@C 4528d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4529d763cef2SBarry Smith 4530d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4531d763cef2SBarry Smith 4532d763cef2SBarry Smith Input Parameter: 4533d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4534d763cef2SBarry Smith 4535d763cef2SBarry Smith Output Parameters: 4536e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4537e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4538e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4539e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4540d763cef2SBarry Smith 45410298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 4542d763cef2SBarry Smith 4543d763cef2SBarry Smith Level: intermediate 4544d763cef2SBarry Smith 454526d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 4546d763cef2SBarry Smith 4547d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 4548d763cef2SBarry Smith @*/ 4549e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4550d763cef2SBarry Smith { 4551089b2837SJed Brown PetscErrorCode ierr; 4552089b2837SJed Brown SNES snes; 455324989b8cSPeter Brune DM dm; 4554089b2837SJed Brown 4555d763cef2SBarry Smith PetscFunctionBegin; 4556089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4557e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 455824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 455924989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 4560d763cef2SBarry Smith PetscFunctionReturn(0); 4561d763cef2SBarry Smith } 4562d763cef2SBarry Smith 45631713a123SBarry Smith #undef __FUNCT__ 45642eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 45652eca1d9cSJed Brown /*@C 45662eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 45672eca1d9cSJed Brown 45682eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 45692eca1d9cSJed Brown 45702eca1d9cSJed Brown Input Parameter: 45712eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 45722eca1d9cSJed Brown 45732eca1d9cSJed Brown Output Parameters: 4574e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4575e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 45762eca1d9cSJed Brown . f - The function to compute the matrices 45772eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 45782eca1d9cSJed Brown 45790298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 45802eca1d9cSJed Brown 45812eca1d9cSJed Brown Level: advanced 45822eca1d9cSJed Brown 45832eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 45842eca1d9cSJed Brown 45852eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 45862eca1d9cSJed Brown @*/ 4587e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 45882eca1d9cSJed Brown { 4589089b2837SJed Brown PetscErrorCode ierr; 459024989b8cSPeter Brune DM dm; 45910910c330SBarry Smith 45922eca1d9cSJed Brown PetscFunctionBegin; 4593c0aab802Sstefano_zampini if (Amat || Pmat) { 4594c0aab802Sstefano_zampini SNES snes; 4595089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4596f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4597e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 4598c0aab802Sstefano_zampini } 459924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 460024989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 46012eca1d9cSJed Brown PetscFunctionReturn(0); 46022eca1d9cSJed Brown } 46032eca1d9cSJed Brown 46046083293cSBarry Smith 46052eca1d9cSJed Brown #undef __FUNCT__ 460683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 46071713a123SBarry Smith /*@C 460883a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 46091713a123SBarry Smith VecView() for the solution at each timestep 46101713a123SBarry Smith 46111713a123SBarry Smith Collective on TS 46121713a123SBarry Smith 46131713a123SBarry Smith Input Parameters: 46141713a123SBarry Smith + ts - the TS context 46151713a123SBarry Smith . step - current time-step 4616142b95e3SSatish Balay . ptime - current time 46170298fd71SBarry Smith - dummy - either a viewer or NULL 46181713a123SBarry Smith 461999fdda47SBarry Smith Options Database: 462099fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 462199fdda47SBarry Smith 4622387f4636SBarry 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 462399fdda47SBarry Smith will look bad 462499fdda47SBarry Smith 46251713a123SBarry Smith Level: intermediate 46261713a123SBarry Smith 46271713a123SBarry Smith .keywords: TS, vector, monitor, view 46281713a123SBarry Smith 4629a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 46301713a123SBarry Smith @*/ 46310910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 46321713a123SBarry Smith { 4633dfbe8321SBarry Smith PetscErrorCode ierr; 463483a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4635473a3ab2SBarry Smith PetscDraw draw; 46361713a123SBarry Smith 46371713a123SBarry Smith PetscFunctionBegin; 46386083293cSBarry Smith if (!step && ictx->showinitial) { 46396083293cSBarry Smith if (!ictx->initialsolution) { 46400910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 46411713a123SBarry Smith } 46420910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 46436083293cSBarry Smith } 4644b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 46450dcf80beSBarry Smith 46466083293cSBarry Smith if (ictx->showinitial) { 46476083293cSBarry Smith PetscReal pause; 46486083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 46496083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 46506083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 46516083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 46526083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 46536083293cSBarry Smith } 46540910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 4655473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 465651fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 4657473a3ab2SBarry Smith char time[32]; 4658473a3ab2SBarry Smith 4659473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 466085b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4661473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4662473a3ab2SBarry Smith h = yl + .95*(yr - yl); 466351fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4664473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4665473a3ab2SBarry Smith } 4666473a3ab2SBarry Smith 46676083293cSBarry Smith if (ictx->showinitial) { 46686083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 46696083293cSBarry Smith } 46701713a123SBarry Smith PetscFunctionReturn(0); 46711713a123SBarry Smith } 46721713a123SBarry Smith 46732d5ee99bSBarry Smith #undef __FUNCT__ 46749110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorDrawSensi" 46759110b2e7SHong Zhang /*@C 46769110b2e7SHong Zhang TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling 46779110b2e7SHong Zhang VecView() for the sensitivities to initial states at each timestep 46789110b2e7SHong Zhang 46799110b2e7SHong Zhang Collective on TS 46809110b2e7SHong Zhang 46819110b2e7SHong Zhang Input Parameters: 46829110b2e7SHong Zhang + ts - the TS context 46839110b2e7SHong Zhang . step - current time-step 46849110b2e7SHong Zhang . ptime - current time 46859110b2e7SHong Zhang . u - current state 46869110b2e7SHong Zhang . numcost - number of cost functions 46879110b2e7SHong Zhang . lambda - sensitivities to initial conditions 46889110b2e7SHong Zhang . mu - sensitivities to parameters 46899110b2e7SHong Zhang - dummy - either a viewer or NULL 46909110b2e7SHong Zhang 46919110b2e7SHong Zhang Level: intermediate 46929110b2e7SHong Zhang 46939110b2e7SHong Zhang .keywords: TS, vector, adjoint, monitor, view 46949110b2e7SHong Zhang 46959110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView() 46969110b2e7SHong Zhang @*/ 46979110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 46989110b2e7SHong Zhang { 46999110b2e7SHong Zhang PetscErrorCode ierr; 47009110b2e7SHong Zhang TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 47019110b2e7SHong Zhang PetscDraw draw; 4702a0046e2cSSatish Balay PetscReal xl,yl,xr,yr,h; 4703a0046e2cSSatish Balay char time[32]; 47049110b2e7SHong Zhang 47059110b2e7SHong Zhang PetscFunctionBegin; 47069110b2e7SHong Zhang if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 47079110b2e7SHong Zhang 47089110b2e7SHong Zhang ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr); 47099110b2e7SHong Zhang ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 47109110b2e7SHong Zhang ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 47119110b2e7SHong Zhang ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 47129110b2e7SHong Zhang h = yl + .95*(yr - yl); 47139110b2e7SHong Zhang ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 47149110b2e7SHong Zhang ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 47159110b2e7SHong Zhang PetscFunctionReturn(0); 47169110b2e7SHong Zhang } 47179110b2e7SHong Zhang 47189110b2e7SHong Zhang #undef __FUNCT__ 47192d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase" 47202d5ee99bSBarry Smith /*@C 47212d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 47222d5ee99bSBarry Smith 47232d5ee99bSBarry Smith Collective on TS 47242d5ee99bSBarry Smith 47252d5ee99bSBarry Smith Input Parameters: 47262d5ee99bSBarry Smith + ts - the TS context 47272d5ee99bSBarry Smith . step - current time-step 47282d5ee99bSBarry Smith . ptime - current time 47292d5ee99bSBarry Smith - dummy - either a viewer or NULL 47302d5ee99bSBarry Smith 47312d5ee99bSBarry Smith Level: intermediate 47322d5ee99bSBarry Smith 47332d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 47342d5ee99bSBarry Smith 47352d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 47362d5ee99bSBarry Smith @*/ 47372d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 47382d5ee99bSBarry Smith { 47392d5ee99bSBarry Smith PetscErrorCode ierr; 47402d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 47412d5ee99bSBarry Smith PetscDraw draw; 47426934998bSLisandro Dalcin PetscDrawAxis axis; 47432d5ee99bSBarry Smith PetscInt n; 47442d5ee99bSBarry Smith PetscMPIInt size; 47456934998bSLisandro Dalcin PetscReal U0,U1,xl,yl,xr,yr,h; 47462d5ee99bSBarry Smith char time[32]; 47472d5ee99bSBarry Smith const PetscScalar *U; 47482d5ee99bSBarry Smith 47492d5ee99bSBarry Smith PetscFunctionBegin; 47506934998bSLisandro Dalcin ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr); 47516934998bSLisandro Dalcin if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs"); 47522d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 47536934998bSLisandro Dalcin if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 47542d5ee99bSBarry Smith 47552d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 47566934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr); 47576934998bSLisandro Dalcin ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 47586934998bSLisandro Dalcin if (!step) { 47596934998bSLisandro Dalcin ierr = PetscDrawClear(draw);CHKERRQ(ierr); 47606934998bSLisandro Dalcin ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); 47616934998bSLisandro Dalcin } 47622d5ee99bSBarry Smith 47632d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 47646934998bSLisandro Dalcin U0 = PetscRealPart(U[0]); 47656934998bSLisandro Dalcin U1 = PetscRealPart(U[1]); 4766a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 47676934998bSLisandro Dalcin if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0); 47682d5ee99bSBarry Smith 47696934998bSLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 47706934998bSLisandro Dalcin ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr); 47712d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 47724b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 477385b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 47742d5ee99bSBarry Smith h = yl + .95*(yr - yl); 477551fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 47762d5ee99bSBarry Smith } 47776934998bSLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 47782d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 47796934998bSLisandro Dalcin ierr = PetscDrawSave(draw);CHKERRQ(ierr); 47802d5ee99bSBarry Smith PetscFunctionReturn(0); 47812d5ee99bSBarry Smith } 47822d5ee99bSBarry Smith 47831713a123SBarry Smith 47846c699258SBarry Smith #undef __FUNCT__ 478583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 47866083293cSBarry Smith /*@C 478783a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 47886083293cSBarry Smith 47896083293cSBarry Smith Collective on TS 47906083293cSBarry Smith 47916083293cSBarry Smith Input Parameters: 47926083293cSBarry Smith . ctx - the monitor context 47936083293cSBarry Smith 47946083293cSBarry Smith Level: intermediate 47956083293cSBarry Smith 47966083293cSBarry Smith .keywords: TS, vector, monitor, view 47976083293cSBarry Smith 479883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 47996083293cSBarry Smith @*/ 480083a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 48016083293cSBarry Smith { 48026083293cSBarry Smith PetscErrorCode ierr; 48036083293cSBarry Smith 48046083293cSBarry Smith PetscFunctionBegin; 480583a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 480683a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 480783a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 48086083293cSBarry Smith PetscFunctionReturn(0); 48096083293cSBarry Smith } 48106083293cSBarry Smith 48116083293cSBarry Smith #undef __FUNCT__ 481283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 48136083293cSBarry Smith /*@C 481483a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 48156083293cSBarry Smith 48166083293cSBarry Smith Collective on TS 48176083293cSBarry Smith 48186083293cSBarry Smith Input Parameter: 48196083293cSBarry Smith . ts - time-step context 48206083293cSBarry Smith 48216083293cSBarry Smith Output Patameter: 48226083293cSBarry Smith . ctx - the monitor context 48236083293cSBarry Smith 482499fdda47SBarry Smith Options Database: 482599fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 482699fdda47SBarry Smith 48276083293cSBarry Smith Level: intermediate 48286083293cSBarry Smith 48296083293cSBarry Smith .keywords: TS, vector, monitor, view 48306083293cSBarry Smith 483183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 48326083293cSBarry Smith @*/ 483383a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 48346083293cSBarry Smith { 48356083293cSBarry Smith PetscErrorCode ierr; 48366083293cSBarry Smith 48376083293cSBarry Smith PetscFunctionBegin; 4838b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 483983a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4840e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4841bbd56ea5SKarl Rupp 484283a4ac43SBarry Smith (*ctx)->howoften = howoften; 4843473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 4844c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4845473a3ab2SBarry Smith 4846473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 4847c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 48486083293cSBarry Smith PetscFunctionReturn(0); 48496083293cSBarry Smith } 48506083293cSBarry Smith 48516083293cSBarry Smith #undef __FUNCT__ 485283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 48533a471f94SBarry Smith /*@C 485483a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 48553a471f94SBarry Smith VecView() for the error at each timestep 48563a471f94SBarry Smith 48573a471f94SBarry Smith Collective on TS 48583a471f94SBarry Smith 48593a471f94SBarry Smith Input Parameters: 48603a471f94SBarry Smith + ts - the TS context 48613a471f94SBarry Smith . step - current time-step 48623a471f94SBarry Smith . ptime - current time 48630298fd71SBarry Smith - dummy - either a viewer or NULL 48643a471f94SBarry Smith 48653a471f94SBarry Smith Level: intermediate 48663a471f94SBarry Smith 48673a471f94SBarry Smith .keywords: TS, vector, monitor, view 48683a471f94SBarry Smith 48693a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 48703a471f94SBarry Smith @*/ 48710910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 48723a471f94SBarry Smith { 48733a471f94SBarry Smith PetscErrorCode ierr; 487483a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 487583a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 48763a471f94SBarry Smith Vec work; 48773a471f94SBarry Smith 48783a471f94SBarry Smith PetscFunctionBegin; 4879b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 48800910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 48813a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 48820910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 48833a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 48843a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 48853a471f94SBarry Smith PetscFunctionReturn(0); 48863a471f94SBarry Smith } 48873a471f94SBarry Smith 4888af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 48893a471f94SBarry Smith #undef __FUNCT__ 48906c699258SBarry Smith #define __FUNCT__ "TSSetDM" 48916c699258SBarry Smith /*@ 48926c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 48936c699258SBarry Smith 48943f9fe445SBarry Smith Logically Collective on TS and DM 48956c699258SBarry Smith 48966c699258SBarry Smith Input Parameters: 48976c699258SBarry Smith + ts - the preconditioner context 48986c699258SBarry Smith - dm - the dm 48996c699258SBarry Smith 49006c699258SBarry Smith Level: intermediate 49016c699258SBarry Smith 49026c699258SBarry Smith 49036c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 49046c699258SBarry Smith @*/ 49057087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 49066c699258SBarry Smith { 49076c699258SBarry Smith PetscErrorCode ierr; 4908089b2837SJed Brown SNES snes; 4909942e3340SBarry Smith DMTS tsdm; 49106c699258SBarry Smith 49116c699258SBarry Smith PetscFunctionBegin; 49120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 491370663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4914942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 49152a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4916942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4917942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 491824989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 491924989b8cSPeter Brune tsdm->originaldm = dm; 492024989b8cSPeter Brune } 492124989b8cSPeter Brune } 49226bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 492324989b8cSPeter Brune } 49246c699258SBarry Smith ts->dm = dm; 4925bbd56ea5SKarl Rupp 4926089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4927089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 49286c699258SBarry Smith PetscFunctionReturn(0); 49296c699258SBarry Smith } 49306c699258SBarry Smith 49316c699258SBarry Smith #undef __FUNCT__ 49326c699258SBarry Smith #define __FUNCT__ "TSGetDM" 49336c699258SBarry Smith /*@ 49346c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 49356c699258SBarry Smith 49363f9fe445SBarry Smith Not Collective 49376c699258SBarry Smith 49386c699258SBarry Smith Input Parameter: 49396c699258SBarry Smith . ts - the preconditioner context 49406c699258SBarry Smith 49416c699258SBarry Smith Output Parameter: 49426c699258SBarry Smith . dm - the dm 49436c699258SBarry Smith 49446c699258SBarry Smith Level: intermediate 49456c699258SBarry Smith 49466c699258SBarry Smith 49476c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 49486c699258SBarry Smith @*/ 49497087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 49506c699258SBarry Smith { 4951496e6a7aSJed Brown PetscErrorCode ierr; 4952496e6a7aSJed Brown 49536c699258SBarry Smith PetscFunctionBegin; 49540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4955496e6a7aSJed Brown if (!ts->dm) { 4956ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4957496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4958496e6a7aSJed Brown } 49596c699258SBarry Smith *dm = ts->dm; 49606c699258SBarry Smith PetscFunctionReturn(0); 49616c699258SBarry Smith } 49621713a123SBarry Smith 49630f5c6efeSJed Brown #undef __FUNCT__ 49640f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 49650f5c6efeSJed Brown /*@ 49660f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 49670f5c6efeSJed Brown 49683f9fe445SBarry Smith Logically Collective on SNES 49690f5c6efeSJed Brown 49700f5c6efeSJed Brown Input Parameter: 4971d42a1c89SJed Brown + snes - nonlinear solver 49720910c330SBarry Smith . U - the current state at which to evaluate the residual 4973d42a1c89SJed Brown - ctx - user context, must be a TS 49740f5c6efeSJed Brown 49750f5c6efeSJed Brown Output Parameter: 49760f5c6efeSJed Brown . F - the nonlinear residual 49770f5c6efeSJed Brown 49780f5c6efeSJed Brown Notes: 49790f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 49800f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 49810f5c6efeSJed Brown 49820f5c6efeSJed Brown Level: advanced 49830f5c6efeSJed Brown 49840f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 49850f5c6efeSJed Brown @*/ 49860910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 49870f5c6efeSJed Brown { 49880f5c6efeSJed Brown TS ts = (TS)ctx; 49890f5c6efeSJed Brown PetscErrorCode ierr; 49900f5c6efeSJed Brown 49910f5c6efeSJed Brown PetscFunctionBegin; 49920f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 49930910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 49940f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 49950f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 49960910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 49970f5c6efeSJed Brown PetscFunctionReturn(0); 49980f5c6efeSJed Brown } 49990f5c6efeSJed Brown 50000f5c6efeSJed Brown #undef __FUNCT__ 50010f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 50020f5c6efeSJed Brown /*@ 50030f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 50040f5c6efeSJed Brown 50050f5c6efeSJed Brown Collective on SNES 50060f5c6efeSJed Brown 50070f5c6efeSJed Brown Input Parameter: 50080f5c6efeSJed Brown + snes - nonlinear solver 50090910c330SBarry Smith . U - the current state at which to evaluate the residual 50100f5c6efeSJed Brown - ctx - user context, must be a TS 50110f5c6efeSJed Brown 50120f5c6efeSJed Brown Output Parameter: 50130f5c6efeSJed Brown + A - the Jacobian 50140f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 50150f5c6efeSJed Brown - flag - indicates any structure change in the matrix 50160f5c6efeSJed Brown 50170f5c6efeSJed Brown Notes: 50180f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 50190f5c6efeSJed Brown 50200f5c6efeSJed Brown Level: developer 50210f5c6efeSJed Brown 50220f5c6efeSJed Brown .seealso: SNESSetJacobian() 50230f5c6efeSJed Brown @*/ 5024d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 50250f5c6efeSJed Brown { 50260f5c6efeSJed Brown TS ts = (TS)ctx; 50270f5c6efeSJed Brown PetscErrorCode ierr; 50280f5c6efeSJed Brown 50290f5c6efeSJed Brown PetscFunctionBegin; 50300f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 50310910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 50320f5c6efeSJed Brown PetscValidPointer(A,3); 503394ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 50340f5c6efeSJed Brown PetscValidPointer(B,4); 503594ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 50360f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 5037d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 50380f5c6efeSJed Brown PetscFunctionReturn(0); 50390f5c6efeSJed Brown } 5040325fc9f4SBarry Smith 50410e4ef248SJed Brown #undef __FUNCT__ 50420e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 50430e4ef248SJed Brown /*@C 50449ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 50450e4ef248SJed Brown 50460e4ef248SJed Brown Collective on TS 50470e4ef248SJed Brown 50480e4ef248SJed Brown Input Arguments: 50490e4ef248SJed Brown + ts - time stepping context 50500e4ef248SJed Brown . t - time at which to evaluate 50510910c330SBarry Smith . U - state at which to evaluate 50520e4ef248SJed Brown - ctx - context 50530e4ef248SJed Brown 50540e4ef248SJed Brown Output Arguments: 50550e4ef248SJed Brown . F - right hand side 50560e4ef248SJed Brown 50570e4ef248SJed Brown Level: intermediate 50580e4ef248SJed Brown 50590e4ef248SJed Brown Notes: 50600e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 50610e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 50620e4ef248SJed Brown 50630e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 50640e4ef248SJed Brown @*/ 50650910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 50660e4ef248SJed Brown { 50670e4ef248SJed Brown PetscErrorCode ierr; 50680e4ef248SJed Brown Mat Arhs,Brhs; 50690e4ef248SJed Brown 50700e4ef248SJed Brown PetscFunctionBegin; 50710e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 5072d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 50730910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 50740e4ef248SJed Brown PetscFunctionReturn(0); 50750e4ef248SJed Brown } 50760e4ef248SJed Brown 50770e4ef248SJed Brown #undef __FUNCT__ 50780e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 50790e4ef248SJed Brown /*@C 50800e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 50810e4ef248SJed Brown 50820e4ef248SJed Brown Collective on TS 50830e4ef248SJed Brown 50840e4ef248SJed Brown Input Arguments: 50850e4ef248SJed Brown + ts - time stepping context 50860e4ef248SJed Brown . t - time at which to evaluate 50870910c330SBarry Smith . U - state at which to evaluate 50880e4ef248SJed Brown - ctx - context 50890e4ef248SJed Brown 50900e4ef248SJed Brown Output Arguments: 50910e4ef248SJed Brown + A - pointer to operator 50920e4ef248SJed Brown . B - pointer to preconditioning matrix 50930e4ef248SJed Brown - flg - matrix structure flag 50940e4ef248SJed Brown 50950e4ef248SJed Brown Level: intermediate 50960e4ef248SJed Brown 50970e4ef248SJed Brown Notes: 50980e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 50990e4ef248SJed Brown 51000e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 51010e4ef248SJed Brown @*/ 5102d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 51030e4ef248SJed Brown { 51040e4ef248SJed Brown PetscFunctionBegin; 51050e4ef248SJed Brown PetscFunctionReturn(0); 51060e4ef248SJed Brown } 51070e4ef248SJed Brown 51080026cea9SSean Farley #undef __FUNCT__ 51090026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 51100026cea9SSean Farley /*@C 51110026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 51120026cea9SSean Farley 51130026cea9SSean Farley Collective on TS 51140026cea9SSean Farley 51150026cea9SSean Farley Input Arguments: 51160026cea9SSean Farley + ts - time stepping context 51170026cea9SSean Farley . t - time at which to evaluate 51180910c330SBarry Smith . U - state at which to evaluate 51190910c330SBarry Smith . Udot - time derivative of state vector 51200026cea9SSean Farley - ctx - context 51210026cea9SSean Farley 51220026cea9SSean Farley Output Arguments: 51230026cea9SSean Farley . F - left hand side 51240026cea9SSean Farley 51250026cea9SSean Farley Level: intermediate 51260026cea9SSean Farley 51270026cea9SSean Farley Notes: 51280910c330SBarry 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 51290026cea9SSean Farley user is required to write their own TSComputeIFunction. 51300026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 51310026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 51320026cea9SSean Farley 51339ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 51349ae8fd06SBarry Smith 51359ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 51360026cea9SSean Farley @*/ 51370910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 51380026cea9SSean Farley { 51390026cea9SSean Farley PetscErrorCode ierr; 51400026cea9SSean Farley Mat A,B; 51410026cea9SSean Farley 51420026cea9SSean Farley PetscFunctionBegin; 51430298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 5144d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 51450910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 51460026cea9SSean Farley PetscFunctionReturn(0); 51470026cea9SSean Farley } 51480026cea9SSean Farley 51490026cea9SSean Farley #undef __FUNCT__ 51500026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 51510026cea9SSean Farley /*@C 5152b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 51530026cea9SSean Farley 51540026cea9SSean Farley Collective on TS 51550026cea9SSean Farley 51560026cea9SSean Farley Input Arguments: 51570026cea9SSean Farley + ts - time stepping context 51580026cea9SSean Farley . t - time at which to evaluate 51590910c330SBarry Smith . U - state at which to evaluate 51600910c330SBarry Smith . Udot - time derivative of state vector 51610026cea9SSean Farley . shift - shift to apply 51620026cea9SSean Farley - ctx - context 51630026cea9SSean Farley 51640026cea9SSean Farley Output Arguments: 51650026cea9SSean Farley + A - pointer to operator 51660026cea9SSean Farley . B - pointer to preconditioning matrix 51670026cea9SSean Farley - flg - matrix structure flag 51680026cea9SSean Farley 5169b41af12eSJed Brown Level: advanced 51700026cea9SSean Farley 51710026cea9SSean Farley Notes: 51720026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 51730026cea9SSean Farley 5174b41af12eSJed Brown It is only appropriate for problems of the form 5175b41af12eSJed Brown 5176b41af12eSJed Brown $ M Udot = F(U,t) 5177b41af12eSJed Brown 5178b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 5179b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 5180b41af12eSJed Brown an implicit operator of the form 5181b41af12eSJed Brown 5182b41af12eSJed Brown $ shift*M + J 5183b41af12eSJed Brown 5184b41af12eSJed 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 5185b41af12eSJed Brown a copy of M or reassemble it when requested. 5186b41af12eSJed Brown 51870026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 51880026cea9SSean Farley @*/ 5189d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 51900026cea9SSean Farley { 5191b41af12eSJed Brown PetscErrorCode ierr; 5192b41af12eSJed Brown 51930026cea9SSean Farley PetscFunctionBegin; 519494ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 5195b41af12eSJed Brown ts->ijacobian.shift = shift; 51960026cea9SSean Farley PetscFunctionReturn(0); 51970026cea9SSean Farley } 5198b41af12eSJed Brown 5199e817cc15SEmil Constantinescu #undef __FUNCT__ 5200e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType" 5201e817cc15SEmil Constantinescu /*@ 5202e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 5203e817cc15SEmil Constantinescu 5204e817cc15SEmil Constantinescu Not Collective 5205e817cc15SEmil Constantinescu 5206e817cc15SEmil Constantinescu Input Parameter: 5207e817cc15SEmil Constantinescu . ts - the TS context 5208e817cc15SEmil Constantinescu 5209e817cc15SEmil Constantinescu Output Parameter: 52104e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 5211e817cc15SEmil Constantinescu 5212e817cc15SEmil Constantinescu Level: beginner 5213e817cc15SEmil Constantinescu 5214e817cc15SEmil Constantinescu .keywords: TS, equation type 5215e817cc15SEmil Constantinescu 5216e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 5217e817cc15SEmil Constantinescu @*/ 5218e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 5219e817cc15SEmil Constantinescu { 5220e817cc15SEmil Constantinescu PetscFunctionBegin; 5221e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5222e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 5223e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 5224e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5225e817cc15SEmil Constantinescu } 5226e817cc15SEmil Constantinescu 5227e817cc15SEmil Constantinescu #undef __FUNCT__ 5228e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType" 5229e817cc15SEmil Constantinescu /*@ 5230e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 5231e817cc15SEmil Constantinescu 5232e817cc15SEmil Constantinescu Not Collective 5233e817cc15SEmil Constantinescu 5234e817cc15SEmil Constantinescu Input Parameter: 5235e817cc15SEmil Constantinescu + ts - the TS context 52361297b224SEmil Constantinescu - equation_type - see TSEquationType 5237e817cc15SEmil Constantinescu 5238e817cc15SEmil Constantinescu Level: advanced 5239e817cc15SEmil Constantinescu 5240e817cc15SEmil Constantinescu .keywords: TS, equation type 5241e817cc15SEmil Constantinescu 5242e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 5243e817cc15SEmil Constantinescu @*/ 5244e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 5245e817cc15SEmil Constantinescu { 5246e817cc15SEmil Constantinescu PetscFunctionBegin; 5247e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5248e817cc15SEmil Constantinescu ts->equation_type = equation_type; 5249e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5250e817cc15SEmil Constantinescu } 52510026cea9SSean Farley 52524af1b03aSJed Brown #undef __FUNCT__ 52534af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 52544af1b03aSJed Brown /*@ 52554af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 52564af1b03aSJed Brown 52574af1b03aSJed Brown Not Collective 52584af1b03aSJed Brown 52594af1b03aSJed Brown Input Parameter: 52604af1b03aSJed Brown . ts - the TS context 52614af1b03aSJed Brown 52624af1b03aSJed Brown Output Parameter: 52634af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 52644af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 52654af1b03aSJed Brown 5266487e0bb9SJed Brown Level: beginner 52674af1b03aSJed Brown 5268cd652676SJed Brown Notes: 5269cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 52704af1b03aSJed Brown 52714af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 52724af1b03aSJed Brown 52734af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 52744af1b03aSJed Brown @*/ 52754af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 52764af1b03aSJed Brown { 52774af1b03aSJed Brown PetscFunctionBegin; 52784af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 52794af1b03aSJed Brown PetscValidPointer(reason,2); 52804af1b03aSJed Brown *reason = ts->reason; 52814af1b03aSJed Brown PetscFunctionReturn(0); 52824af1b03aSJed Brown } 52834af1b03aSJed Brown 5284fb1732b5SBarry Smith #undef __FUNCT__ 5285d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 5286d6ad946cSShri Abhyankar /*@ 5287d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 5288d6ad946cSShri Abhyankar 5289d6ad946cSShri Abhyankar Not Collective 5290d6ad946cSShri Abhyankar 5291d6ad946cSShri Abhyankar Input Parameter: 5292d6ad946cSShri Abhyankar + ts - the TS context 5293d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 5294d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 5295d6ad946cSShri Abhyankar 5296f5abba47SShri Abhyankar Level: advanced 5297d6ad946cSShri Abhyankar 5298d6ad946cSShri Abhyankar Notes: 5299d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 5300d6ad946cSShri Abhyankar 5301d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 5302d6ad946cSShri Abhyankar 5303d6ad946cSShri Abhyankar .seealso: TSConvergedReason 5304d6ad946cSShri Abhyankar @*/ 5305d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 5306d6ad946cSShri Abhyankar { 5307d6ad946cSShri Abhyankar PetscFunctionBegin; 5308d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5309d6ad946cSShri Abhyankar ts->reason = reason; 5310d6ad946cSShri Abhyankar PetscFunctionReturn(0); 5311d6ad946cSShri Abhyankar } 5312d6ad946cSShri Abhyankar 5313d6ad946cSShri Abhyankar #undef __FUNCT__ 5314cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 5315cc708dedSBarry Smith /*@ 5316cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 5317cc708dedSBarry Smith 5318cc708dedSBarry Smith Not Collective 5319cc708dedSBarry Smith 5320cc708dedSBarry Smith Input Parameter: 5321cc708dedSBarry Smith . ts - the TS context 5322cc708dedSBarry Smith 5323cc708dedSBarry Smith Output Parameter: 532463e21af5SBarry Smith . ftime - the final time. This time corresponds to the final time set with TSSetDuration() 5325cc708dedSBarry Smith 5326487e0bb9SJed Brown Level: beginner 5327cc708dedSBarry Smith 5328cc708dedSBarry Smith Notes: 5329cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 5330cc708dedSBarry Smith 5331cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 5332cc708dedSBarry Smith 5333cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 5334cc708dedSBarry Smith @*/ 5335cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 5336cc708dedSBarry Smith { 5337cc708dedSBarry Smith PetscFunctionBegin; 5338cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5339cc708dedSBarry Smith PetscValidPointer(ftime,2); 5340cc708dedSBarry Smith *ftime = ts->solvetime; 5341cc708dedSBarry Smith PetscFunctionReturn(0); 5342cc708dedSBarry Smith } 5343cc708dedSBarry Smith 5344cc708dedSBarry Smith #undef __FUNCT__ 53452c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps" 53462c18e0fdSBarry Smith /*@ 53472c18e0fdSBarry Smith TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 53482c18e0fdSBarry Smith 53492c18e0fdSBarry Smith Not Collective 53502c18e0fdSBarry Smith 53512c18e0fdSBarry Smith Input Parameter: 53522c18e0fdSBarry Smith . ts - the TS context 53532c18e0fdSBarry Smith 53542c18e0fdSBarry Smith Output Parameter: 53552c18e0fdSBarry Smith . steps - the number of steps 53562c18e0fdSBarry Smith 53572c18e0fdSBarry Smith Level: beginner 53582c18e0fdSBarry Smith 53592c18e0fdSBarry Smith Notes: 53602c18e0fdSBarry Smith Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 53612c18e0fdSBarry Smith 53622c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test 53632c18e0fdSBarry Smith 53642c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 53652c18e0fdSBarry Smith @*/ 53662c18e0fdSBarry Smith PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 53672c18e0fdSBarry Smith { 53682c18e0fdSBarry Smith PetscFunctionBegin; 53692c18e0fdSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53702c18e0fdSBarry Smith PetscValidPointer(steps,2); 53712c18e0fdSBarry Smith *steps = ts->total_steps; 53722c18e0fdSBarry Smith PetscFunctionReturn(0); 53732c18e0fdSBarry Smith } 53742c18e0fdSBarry Smith 53752c18e0fdSBarry Smith #undef __FUNCT__ 53765ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 53779f67acb7SJed Brown /*@ 53785ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 53799f67acb7SJed Brown used by the time integrator. 53809f67acb7SJed Brown 53819f67acb7SJed Brown Not Collective 53829f67acb7SJed Brown 53839f67acb7SJed Brown Input Parameter: 53849f67acb7SJed Brown . ts - TS context 53859f67acb7SJed Brown 53869f67acb7SJed Brown Output Parameter: 53879f67acb7SJed Brown . nits - number of nonlinear iterations 53889f67acb7SJed Brown 53899f67acb7SJed Brown Notes: 53909f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 53919f67acb7SJed Brown 53929f67acb7SJed Brown Level: intermediate 53939f67acb7SJed Brown 53949f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 53959f67acb7SJed Brown 53965ef26d82SJed Brown .seealso: TSGetKSPIterations() 53979f67acb7SJed Brown @*/ 53985ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 53999f67acb7SJed Brown { 54009f67acb7SJed Brown PetscFunctionBegin; 54019f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 54029f67acb7SJed Brown PetscValidIntPointer(nits,2); 54035ef26d82SJed Brown *nits = ts->snes_its; 54049f67acb7SJed Brown PetscFunctionReturn(0); 54059f67acb7SJed Brown } 54069f67acb7SJed Brown 54079f67acb7SJed Brown #undef __FUNCT__ 54085ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 54099f67acb7SJed Brown /*@ 54105ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 54119f67acb7SJed Brown used by the time integrator. 54129f67acb7SJed Brown 54139f67acb7SJed Brown Not Collective 54149f67acb7SJed Brown 54159f67acb7SJed Brown Input Parameter: 54169f67acb7SJed Brown . ts - TS context 54179f67acb7SJed Brown 54189f67acb7SJed Brown Output Parameter: 54199f67acb7SJed Brown . lits - number of linear iterations 54209f67acb7SJed Brown 54219f67acb7SJed Brown Notes: 54229f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 54239f67acb7SJed Brown 54249f67acb7SJed Brown Level: intermediate 54259f67acb7SJed Brown 54269f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 54279f67acb7SJed Brown 54285ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 54299f67acb7SJed Brown @*/ 54305ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 54319f67acb7SJed Brown { 54329f67acb7SJed Brown PetscFunctionBegin; 54339f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 54349f67acb7SJed Brown PetscValidIntPointer(lits,2); 54355ef26d82SJed Brown *lits = ts->ksp_its; 54369f67acb7SJed Brown PetscFunctionReturn(0); 54379f67acb7SJed Brown } 54389f67acb7SJed Brown 54399f67acb7SJed Brown #undef __FUNCT__ 5440cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 5441cef5090cSJed Brown /*@ 5442cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 5443cef5090cSJed Brown 5444cef5090cSJed Brown Not Collective 5445cef5090cSJed Brown 5446cef5090cSJed Brown Input Parameter: 5447cef5090cSJed Brown . ts - TS context 5448cef5090cSJed Brown 5449cef5090cSJed Brown Output Parameter: 5450cef5090cSJed Brown . rejects - number of steps rejected 5451cef5090cSJed Brown 5452cef5090cSJed Brown Notes: 5453cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5454cef5090cSJed Brown 5455cef5090cSJed Brown Level: intermediate 5456cef5090cSJed Brown 5457cef5090cSJed Brown .keywords: TS, get, number 5458cef5090cSJed Brown 54595ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 5460cef5090cSJed Brown @*/ 5461cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 5462cef5090cSJed Brown { 5463cef5090cSJed Brown PetscFunctionBegin; 5464cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5465cef5090cSJed Brown PetscValidIntPointer(rejects,2); 5466cef5090cSJed Brown *rejects = ts->reject; 5467cef5090cSJed Brown PetscFunctionReturn(0); 5468cef5090cSJed Brown } 5469cef5090cSJed Brown 5470cef5090cSJed Brown #undef __FUNCT__ 5471cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 5472cef5090cSJed Brown /*@ 5473cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 5474cef5090cSJed Brown 5475cef5090cSJed Brown Not Collective 5476cef5090cSJed Brown 5477cef5090cSJed Brown Input Parameter: 5478cef5090cSJed Brown . ts - TS context 5479cef5090cSJed Brown 5480cef5090cSJed Brown Output Parameter: 5481cef5090cSJed Brown . fails - number of failed nonlinear solves 5482cef5090cSJed Brown 5483cef5090cSJed Brown Notes: 5484cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5485cef5090cSJed Brown 5486cef5090cSJed Brown Level: intermediate 5487cef5090cSJed Brown 5488cef5090cSJed Brown .keywords: TS, get, number 5489cef5090cSJed Brown 54905ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 5491cef5090cSJed Brown @*/ 5492cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 5493cef5090cSJed Brown { 5494cef5090cSJed Brown PetscFunctionBegin; 5495cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5496cef5090cSJed Brown PetscValidIntPointer(fails,2); 5497cef5090cSJed Brown *fails = ts->num_snes_failures; 5498cef5090cSJed Brown PetscFunctionReturn(0); 5499cef5090cSJed Brown } 5500cef5090cSJed Brown 5501cef5090cSJed Brown #undef __FUNCT__ 5502cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 5503cef5090cSJed Brown /*@ 5504cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 5505cef5090cSJed Brown 5506cef5090cSJed Brown Not Collective 5507cef5090cSJed Brown 5508cef5090cSJed Brown Input Parameter: 5509cef5090cSJed Brown + ts - TS context 5510cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 5511cef5090cSJed Brown 5512cef5090cSJed Brown Notes: 5513cef5090cSJed Brown The counter is reset to zero for each step 5514cef5090cSJed Brown 5515cef5090cSJed Brown Options Database Key: 5516cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 5517cef5090cSJed Brown 5518cef5090cSJed Brown Level: intermediate 5519cef5090cSJed Brown 5520cef5090cSJed Brown .keywords: TS, set, maximum, number 5521cef5090cSJed Brown 55225ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5523cef5090cSJed Brown @*/ 5524cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 5525cef5090cSJed Brown { 5526cef5090cSJed Brown PetscFunctionBegin; 5527cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5528cef5090cSJed Brown ts->max_reject = rejects; 5529cef5090cSJed Brown PetscFunctionReturn(0); 5530cef5090cSJed Brown } 5531cef5090cSJed Brown 5532cef5090cSJed Brown #undef __FUNCT__ 5533cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 5534cef5090cSJed Brown /*@ 5535cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 5536cef5090cSJed Brown 5537cef5090cSJed Brown Not Collective 5538cef5090cSJed Brown 5539cef5090cSJed Brown Input Parameter: 5540cef5090cSJed Brown + ts - TS context 5541cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 5542cef5090cSJed Brown 5543cef5090cSJed Brown Notes: 5544cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 5545cef5090cSJed Brown 5546cef5090cSJed Brown Options Database Key: 5547cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 5548cef5090cSJed Brown 5549cef5090cSJed Brown Level: intermediate 5550cef5090cSJed Brown 5551cef5090cSJed Brown .keywords: TS, set, maximum, number 5552cef5090cSJed Brown 55535ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 5554cef5090cSJed Brown @*/ 5555cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 5556cef5090cSJed Brown { 5557cef5090cSJed Brown PetscFunctionBegin; 5558cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5559cef5090cSJed Brown ts->max_snes_failures = fails; 5560cef5090cSJed Brown PetscFunctionReturn(0); 5561cef5090cSJed Brown } 5562cef5090cSJed Brown 5563cef5090cSJed Brown #undef __FUNCT__ 55644e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails" 5565cef5090cSJed Brown /*@ 5566cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 5567cef5090cSJed Brown 5568cef5090cSJed Brown Not Collective 5569cef5090cSJed Brown 5570cef5090cSJed Brown Input Parameter: 5571cef5090cSJed Brown + ts - TS context 5572cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 5573cef5090cSJed Brown 5574cef5090cSJed Brown Options Database Key: 5575cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 5576cef5090cSJed Brown 5577cef5090cSJed Brown Level: intermediate 5578cef5090cSJed Brown 5579cef5090cSJed Brown .keywords: TS, set, error 5580cef5090cSJed Brown 55815ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5582cef5090cSJed Brown @*/ 5583cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 5584cef5090cSJed Brown { 5585cef5090cSJed Brown PetscFunctionBegin; 5586cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5587cef5090cSJed Brown ts->errorifstepfailed = err; 5588cef5090cSJed Brown PetscFunctionReturn(0); 5589cef5090cSJed Brown } 5590cef5090cSJed Brown 5591cef5090cSJed Brown #undef __FUNCT__ 5592fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSolution" 5593fb1732b5SBarry Smith /*@C 5594fde5950dSBarry 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 5595fb1732b5SBarry Smith 5596fb1732b5SBarry Smith Collective on TS 5597fb1732b5SBarry Smith 5598fb1732b5SBarry Smith Input Parameters: 5599fb1732b5SBarry Smith + ts - the TS context 5600fb1732b5SBarry Smith . step - current time-step 5601fb1732b5SBarry Smith . ptime - current time 56020910c330SBarry Smith . u - current state 5603721cd6eeSBarry Smith - vf - viewer and its format 5604fb1732b5SBarry Smith 5605fb1732b5SBarry Smith Level: intermediate 5606fb1732b5SBarry Smith 5607fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 5608fb1732b5SBarry Smith 5609fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5610fb1732b5SBarry Smith @*/ 5611721cd6eeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf) 5612fb1732b5SBarry Smith { 5613fb1732b5SBarry Smith PetscErrorCode ierr; 5614fb1732b5SBarry Smith 5615fb1732b5SBarry Smith PetscFunctionBegin; 5616721cd6eeSBarry Smith ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr); 5617721cd6eeSBarry Smith ierr = VecView(u,vf->viewer);CHKERRQ(ierr); 5618721cd6eeSBarry Smith ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr); 5619ed81e22dSJed Brown PetscFunctionReturn(0); 5620ed81e22dSJed Brown } 5621ed81e22dSJed Brown 5622ed81e22dSJed Brown #undef __FUNCT__ 5623ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 5624ed81e22dSJed Brown /*@C 5625ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5626ed81e22dSJed Brown 5627ed81e22dSJed Brown Collective on TS 5628ed81e22dSJed Brown 5629ed81e22dSJed Brown Input Parameters: 5630ed81e22dSJed Brown + ts - the TS context 5631ed81e22dSJed Brown . step - current time-step 5632ed81e22dSJed Brown . ptime - current time 56330910c330SBarry Smith . u - current state 5634ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5635ed81e22dSJed Brown 5636ed81e22dSJed Brown Level: intermediate 5637ed81e22dSJed Brown 5638ed81e22dSJed Brown Notes: 5639ed81e22dSJed 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. 5640ed81e22dSJed Brown These are named according to the file name template. 5641ed81e22dSJed Brown 5642ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5643ed81e22dSJed Brown 5644ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5645ed81e22dSJed Brown 5646ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5647ed81e22dSJed Brown @*/ 56480910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5649ed81e22dSJed Brown { 5650ed81e22dSJed Brown PetscErrorCode ierr; 5651ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 5652ed81e22dSJed Brown PetscViewer viewer; 5653ed81e22dSJed Brown 5654ed81e22dSJed Brown PetscFunctionBegin; 565563e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 56568caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5657ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 56580910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 5659ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5660ed81e22dSJed Brown PetscFunctionReturn(0); 5661ed81e22dSJed Brown } 5662ed81e22dSJed Brown 5663ed81e22dSJed Brown #undef __FUNCT__ 5664ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 5665ed81e22dSJed Brown /*@C 5666ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5667ed81e22dSJed Brown 5668ed81e22dSJed Brown Collective on TS 5669ed81e22dSJed Brown 5670ed81e22dSJed Brown Input Parameters: 5671ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5672ed81e22dSJed Brown 5673ed81e22dSJed Brown Level: intermediate 5674ed81e22dSJed Brown 5675ed81e22dSJed Brown Note: 5676ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5677ed81e22dSJed Brown 5678ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5679ed81e22dSJed Brown 5680ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5681ed81e22dSJed Brown @*/ 5682ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5683ed81e22dSJed Brown { 5684ed81e22dSJed Brown PetscErrorCode ierr; 5685ed81e22dSJed Brown 5686ed81e22dSJed Brown PetscFunctionBegin; 5687ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5688fb1732b5SBarry Smith PetscFunctionReturn(0); 5689fb1732b5SBarry Smith } 5690fb1732b5SBarry Smith 569184df9cb4SJed Brown #undef __FUNCT__ 5692552698daSJed Brown #define __FUNCT__ "TSGetAdapt" 569384df9cb4SJed Brown /*@ 5694552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 569584df9cb4SJed Brown 5696ed81e22dSJed Brown Collective on TS if controller has not been created yet 569784df9cb4SJed Brown 569884df9cb4SJed Brown Input Arguments: 5699ed81e22dSJed Brown . ts - time stepping context 570084df9cb4SJed Brown 570184df9cb4SJed Brown Output Arguments: 5702ed81e22dSJed Brown . adapt - adaptive controller 570384df9cb4SJed Brown 570484df9cb4SJed Brown Level: intermediate 570584df9cb4SJed Brown 5706ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 570784df9cb4SJed Brown @*/ 5708552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 570984df9cb4SJed Brown { 571084df9cb4SJed Brown PetscErrorCode ierr; 571184df9cb4SJed Brown 571284df9cb4SJed Brown PetscFunctionBegin; 571384df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5714bec58848SLisandro Dalcin PetscValidPointer(adapt,2); 571584df9cb4SJed Brown if (!ts->adapt) { 5716ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 57173bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 57181c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 571984df9cb4SJed Brown } 5720bec58848SLisandro Dalcin *adapt = ts->adapt; 572184df9cb4SJed Brown PetscFunctionReturn(0); 572284df9cb4SJed Brown } 5723d6ebe24aSShri Abhyankar 5724d6ebe24aSShri Abhyankar #undef __FUNCT__ 57251c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 57261c3436cfSJed Brown /*@ 57271c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 57281c3436cfSJed Brown 57291c3436cfSJed Brown Logically Collective 57301c3436cfSJed Brown 57311c3436cfSJed Brown Input Arguments: 57321c3436cfSJed Brown + ts - time integration context 57331c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 57340298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 57351c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 57360298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 57371c3436cfSJed Brown 5738a3cdaa26SBarry Smith Options Database keys: 5739a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5740a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5741a3cdaa26SBarry Smith 57423ff766beSShri Abhyankar Notes: 57433ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 57443ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 57453ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 57463ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 57473ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 57483ff766beSShri Abhyankar differential variables. 57493ff766beSShri Abhyankar 57501c3436cfSJed Brown Level: beginner 57511c3436cfSJed Brown 5752c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 57531c3436cfSJed Brown @*/ 57541c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 57551c3436cfSJed Brown { 57561c3436cfSJed Brown PetscErrorCode ierr; 57571c3436cfSJed Brown 57581c3436cfSJed Brown PetscFunctionBegin; 5759c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 57601c3436cfSJed Brown if (vatol) { 57611c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 57621c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 57631c3436cfSJed Brown ts->vatol = vatol; 57641c3436cfSJed Brown } 5765c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 57661c3436cfSJed Brown if (vrtol) { 57671c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 57681c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 57691c3436cfSJed Brown ts->vrtol = vrtol; 57701c3436cfSJed Brown } 57711c3436cfSJed Brown PetscFunctionReturn(0); 57721c3436cfSJed Brown } 57731c3436cfSJed Brown 57741c3436cfSJed Brown #undef __FUNCT__ 5775c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 5776c5033834SJed Brown /*@ 5777c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5778c5033834SJed Brown 5779c5033834SJed Brown Logically Collective 5780c5033834SJed Brown 5781c5033834SJed Brown Input Arguments: 5782c5033834SJed Brown . ts - time integration context 5783c5033834SJed Brown 5784c5033834SJed Brown Output Arguments: 57850298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 57860298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 57870298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 57880298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 5789c5033834SJed Brown 5790c5033834SJed Brown Level: beginner 5791c5033834SJed Brown 5792c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5793c5033834SJed Brown @*/ 5794c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5795c5033834SJed Brown { 5796c5033834SJed Brown PetscFunctionBegin; 5797c5033834SJed Brown if (atol) *atol = ts->atol; 5798c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5799c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5800c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 5801c5033834SJed Brown PetscFunctionReturn(0); 5802c5033834SJed Brown } 5803c5033834SJed Brown 5804c5033834SJed Brown #undef __FUNCT__ 58059c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2" 58069c6b16b5SShri Abhyankar /*@ 5807a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 58089c6b16b5SShri Abhyankar 58099c6b16b5SShri Abhyankar Collective on TS 58109c6b16b5SShri Abhyankar 58119c6b16b5SShri Abhyankar Input Arguments: 58129c6b16b5SShri Abhyankar + ts - time stepping context 5813a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5814a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 58159c6b16b5SShri Abhyankar 58169c6b16b5SShri Abhyankar Output Arguments: 58179c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 58189c6b16b5SShri Abhyankar 58199c6b16b5SShri Abhyankar Level: developer 58209c6b16b5SShri Abhyankar 5821deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 58229c6b16b5SShri Abhyankar @*/ 5823a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm) 58249c6b16b5SShri Abhyankar { 58259c6b16b5SShri Abhyankar PetscErrorCode ierr; 58269c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 58279c6b16b5SShri Abhyankar const PetscScalar *u,*y; 58289c6b16b5SShri Abhyankar PetscReal sum,gsum; 58299c6b16b5SShri Abhyankar PetscReal tol; 58309c6b16b5SShri Abhyankar 58319c6b16b5SShri Abhyankar PetscFunctionBegin; 58329c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5833a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5834a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5835a4868fbcSLisandro Dalcin PetscValidType(U,2); 5836a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5837a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5838a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5839a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 58409c6b16b5SShri Abhyankar 58419c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 58429c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 58439c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 58449c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 58459c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 58469c6b16b5SShri Abhyankar sum = 0.; 58479c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 58489c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 58499c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58509c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58519c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 58529c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58539c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 58549c6b16b5SShri Abhyankar } 58559c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58569c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58579c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 58589c6b16b5SShri Abhyankar const PetscScalar *atol; 58599c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58609c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 58619c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58629c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 58639c6b16b5SShri Abhyankar } 58649c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58659c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 58669c6b16b5SShri Abhyankar const PetscScalar *rtol; 58679c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58689c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 58699c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58709c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 58719c6b16b5SShri Abhyankar } 58729c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58739c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 58749c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 58759c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58769c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 58779c6b16b5SShri Abhyankar } 58789c6b16b5SShri Abhyankar } 58799c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 58809c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 58819c6b16b5SShri Abhyankar 5882b2566f29SBarry Smith ierr = MPIU_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 58839c6b16b5SShri Abhyankar *norm = PetscSqrtReal(gsum / N); 58849c6b16b5SShri Abhyankar 58859c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 58869c6b16b5SShri Abhyankar PetscFunctionReturn(0); 58879c6b16b5SShri Abhyankar } 58889c6b16b5SShri Abhyankar 58899c6b16b5SShri Abhyankar #undef __FUNCT__ 58909c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity" 58919c6b16b5SShri Abhyankar /*@ 5892a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 58939c6b16b5SShri Abhyankar 58949c6b16b5SShri Abhyankar Collective on TS 58959c6b16b5SShri Abhyankar 58969c6b16b5SShri Abhyankar Input Arguments: 58979c6b16b5SShri Abhyankar + ts - time stepping context 5898a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5899a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 59009c6b16b5SShri Abhyankar 59019c6b16b5SShri Abhyankar Output Arguments: 59029c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 59039c6b16b5SShri Abhyankar 59049c6b16b5SShri Abhyankar Level: developer 59059c6b16b5SShri Abhyankar 5906deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 59079c6b16b5SShri Abhyankar @*/ 5908a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm) 59099c6b16b5SShri Abhyankar { 59109c6b16b5SShri Abhyankar PetscErrorCode ierr; 59119c6b16b5SShri Abhyankar PetscInt i,n,N,rstart,k; 59129c6b16b5SShri Abhyankar const PetscScalar *u,*y; 59139c6b16b5SShri Abhyankar PetscReal max,gmax; 59149c6b16b5SShri Abhyankar PetscReal tol; 59159c6b16b5SShri Abhyankar 59169c6b16b5SShri Abhyankar PetscFunctionBegin; 59179c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5918a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5919a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5920a4868fbcSLisandro Dalcin PetscValidType(U,2); 5921a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5922a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5923a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5924a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 59259c6b16b5SShri Abhyankar 59269c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 59279c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 59289c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 59299c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 59309c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 59319c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 59329c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 59339c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59349c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59359c6b16b5SShri Abhyankar k = 0; 59369c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 59379c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 59389c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 59399c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59409c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 59419c6b16b5SShri Abhyankar } 59429c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59439c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59449c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 59459c6b16b5SShri Abhyankar const PetscScalar *atol; 59469c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59479c6b16b5SShri Abhyankar k = 0; 59489c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 59499c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 59509c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 59519c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59529c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 59539c6b16b5SShri Abhyankar } 59549c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59559c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 59569c6b16b5SShri Abhyankar const PetscScalar *rtol; 59579c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59589c6b16b5SShri Abhyankar k = 0; 59599c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 59609c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 59619c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 59629c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59639c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 59649c6b16b5SShri Abhyankar } 59659c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59669c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 59679c6b16b5SShri Abhyankar k = 0; 59689c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 59699c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 59709c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 59719c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59729c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 59739c6b16b5SShri Abhyankar } 59749c6b16b5SShri Abhyankar } 59759c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 59769c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 59779c6b16b5SShri Abhyankar 5978b2566f29SBarry Smith ierr = MPIU_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 59799c6b16b5SShri Abhyankar *norm = gmax; 59809c6b16b5SShri Abhyankar 59819c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 59829c6b16b5SShri Abhyankar PetscFunctionReturn(0); 59839c6b16b5SShri Abhyankar } 59849c6b16b5SShri Abhyankar 59859c6b16b5SShri Abhyankar #undef __FUNCT__ 59867619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm" 59871c3436cfSJed Brown /*@ 5988a4868fbcSLisandro Dalcin TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors 59891c3436cfSJed Brown 59901c3436cfSJed Brown Collective on TS 59911c3436cfSJed Brown 59921c3436cfSJed Brown Input Arguments: 59931c3436cfSJed Brown + ts - time stepping context 5994a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5995a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5996a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 59977619abb3SShri 59981c3436cfSJed Brown Output Arguments: 59991c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 60001c3436cfSJed Brown 6001a4868fbcSLisandro Dalcin 6002a4868fbcSLisandro Dalcin Options Database Keys: 6003a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 6004a4868fbcSLisandro Dalcin 60051c3436cfSJed Brown Level: developer 60061c3436cfSJed Brown 6007deea92deSShri .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 60081c3436cfSJed Brown @*/ 6009a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm) 60101c3436cfSJed Brown { 60118beabaa1SBarry Smith PetscErrorCode ierr; 60121c3436cfSJed Brown 60131c3436cfSJed Brown PetscFunctionBegin; 6014a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 6015a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNorm2(ts,U,Y,norm);CHKERRQ(ierr); 6016a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 6017a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm);CHKERRQ(ierr); 6018a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 60191c3436cfSJed Brown PetscFunctionReturn(0); 60201c3436cfSJed Brown } 60211c3436cfSJed Brown 60221c3436cfSJed Brown #undef __FUNCT__ 60238d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 60248d59e960SJed Brown /*@ 60258d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 60268d59e960SJed Brown 60278d59e960SJed Brown Logically Collective on TS 60288d59e960SJed Brown 60298d59e960SJed Brown Input Arguments: 60308d59e960SJed Brown + ts - time stepping context 60318d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 60328d59e960SJed Brown 60338d59e960SJed Brown Note: 60348d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 60358d59e960SJed Brown 60368d59e960SJed Brown Level: intermediate 60378d59e960SJed Brown 60388d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 60398d59e960SJed Brown @*/ 60408d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 60418d59e960SJed Brown { 60428d59e960SJed Brown PetscFunctionBegin; 60438d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 60448d59e960SJed Brown ts->cfltime_local = cfltime; 60458d59e960SJed Brown ts->cfltime = -1.; 60468d59e960SJed Brown PetscFunctionReturn(0); 60478d59e960SJed Brown } 60488d59e960SJed Brown 60498d59e960SJed Brown #undef __FUNCT__ 60508d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 60518d59e960SJed Brown /*@ 60528d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 60538d59e960SJed Brown 60548d59e960SJed Brown Collective on TS 60558d59e960SJed Brown 60568d59e960SJed Brown Input Arguments: 60578d59e960SJed Brown . ts - time stepping context 60588d59e960SJed Brown 60598d59e960SJed Brown Output Arguments: 60608d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 60618d59e960SJed Brown 60628d59e960SJed Brown Level: advanced 60638d59e960SJed Brown 60648d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 60658d59e960SJed Brown @*/ 60668d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 60678d59e960SJed Brown { 60688d59e960SJed Brown PetscErrorCode ierr; 60698d59e960SJed Brown 60708d59e960SJed Brown PetscFunctionBegin; 60718d59e960SJed Brown if (ts->cfltime < 0) { 6072b2566f29SBarry Smith ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 60738d59e960SJed Brown } 60748d59e960SJed Brown *cfltime = ts->cfltime; 60758d59e960SJed Brown PetscFunctionReturn(0); 60768d59e960SJed Brown } 60778d59e960SJed Brown 60788d59e960SJed Brown #undef __FUNCT__ 6079d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 6080d6ebe24aSShri Abhyankar /*@ 6081d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 6082d6ebe24aSShri Abhyankar 6083d6ebe24aSShri Abhyankar Input Parameters: 6084d6ebe24aSShri Abhyankar . ts - the TS context. 6085d6ebe24aSShri Abhyankar . xl - lower bound. 6086d6ebe24aSShri Abhyankar . xu - upper bound. 6087d6ebe24aSShri Abhyankar 6088d6ebe24aSShri Abhyankar Notes: 6089d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 6090e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 6091d6ebe24aSShri Abhyankar 60922bd2b0e6SSatish Balay Level: advanced 60932bd2b0e6SSatish Balay 6094d6ebe24aSShri Abhyankar @*/ 6095d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 6096d6ebe24aSShri Abhyankar { 6097d6ebe24aSShri Abhyankar PetscErrorCode ierr; 6098d6ebe24aSShri Abhyankar SNES snes; 6099d6ebe24aSShri Abhyankar 6100d6ebe24aSShri Abhyankar PetscFunctionBegin; 6101d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 6102d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 6103d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 6104d6ebe24aSShri Abhyankar } 6105d6ebe24aSShri Abhyankar 6106325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 6107c6db04a5SJed Brown #include <mex.h> 6108325fc9f4SBarry Smith 6109325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 6110325fc9f4SBarry Smith 6111325fc9f4SBarry Smith #undef __FUNCT__ 6112325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 6113325fc9f4SBarry Smith /* 6114325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 6115325fc9f4SBarry Smith TSSetFunctionMatlab(). 6116325fc9f4SBarry Smith 6117325fc9f4SBarry Smith Collective on TS 6118325fc9f4SBarry Smith 6119325fc9f4SBarry Smith Input Parameters: 6120325fc9f4SBarry Smith + snes - the TS context 61210910c330SBarry Smith - u - input vector 6122325fc9f4SBarry Smith 6123325fc9f4SBarry Smith Output Parameter: 6124325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 6125325fc9f4SBarry Smith 6126325fc9f4SBarry Smith Notes: 6127325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 6128325fc9f4SBarry Smith implementations, so most users would not generally call this routine 6129325fc9f4SBarry Smith themselves. 6130325fc9f4SBarry Smith 6131325fc9f4SBarry Smith Level: developer 6132325fc9f4SBarry Smith 6133325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6134325fc9f4SBarry Smith 6135325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6136325fc9f4SBarry Smith */ 61370910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 6138325fc9f4SBarry Smith { 6139325fc9f4SBarry Smith PetscErrorCode ierr; 6140325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6141325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 6142325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 6143325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 6144325fc9f4SBarry Smith 6145325fc9f4SBarry Smith PetscFunctionBegin; 6146325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 61470910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 61480910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 6149325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 61500910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 6151325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 6152325fc9f4SBarry Smith 6153325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 61540910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 61550910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 61560910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 6157bbd56ea5SKarl Rupp 6158325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6159325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 6160325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6161325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6162325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 6163325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 6164325fc9f4SBarry Smith prhs[6] = sctx->ctx; 6165325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 6166325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6167325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6168325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6169325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6170325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6171325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6172325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6173325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6174325fc9f4SBarry Smith PetscFunctionReturn(0); 6175325fc9f4SBarry Smith } 6176325fc9f4SBarry Smith 6177325fc9f4SBarry Smith 6178325fc9f4SBarry Smith #undef __FUNCT__ 6179325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 6180325fc9f4SBarry Smith /* 6181325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 6182325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 6183e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 6184325fc9f4SBarry Smith 6185325fc9f4SBarry Smith Logically Collective on TS 6186325fc9f4SBarry Smith 6187325fc9f4SBarry Smith Input Parameters: 6188325fc9f4SBarry Smith + ts - the TS context 6189325fc9f4SBarry Smith - func - function evaluation routine 6190325fc9f4SBarry Smith 6191325fc9f4SBarry Smith Calling sequence of func: 61920910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 6193325fc9f4SBarry Smith 6194325fc9f4SBarry Smith Level: beginner 6195325fc9f4SBarry Smith 6196325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6197325fc9f4SBarry Smith 6198325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6199325fc9f4SBarry Smith */ 6200cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 6201325fc9f4SBarry Smith { 6202325fc9f4SBarry Smith PetscErrorCode ierr; 6203325fc9f4SBarry Smith TSMatlabContext *sctx; 6204325fc9f4SBarry Smith 6205325fc9f4SBarry Smith PetscFunctionBegin; 6206325fc9f4SBarry Smith /* currently sctx is memory bleed */ 6207325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 6208325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6209325fc9f4SBarry Smith /* 6210325fc9f4SBarry Smith This should work, but it doesn't 6211325fc9f4SBarry Smith sctx->ctx = ctx; 6212325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6213325fc9f4SBarry Smith */ 6214325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6215bbd56ea5SKarl Rupp 62160298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 6217325fc9f4SBarry Smith PetscFunctionReturn(0); 6218325fc9f4SBarry Smith } 6219325fc9f4SBarry Smith 6220325fc9f4SBarry Smith #undef __FUNCT__ 6221325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 6222325fc9f4SBarry Smith /* 6223325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 6224325fc9f4SBarry Smith TSSetJacobianMatlab(). 6225325fc9f4SBarry Smith 6226325fc9f4SBarry Smith Collective on TS 6227325fc9f4SBarry Smith 6228325fc9f4SBarry Smith Input Parameters: 6229cdcf91faSSean Farley + ts - the TS context 62300910c330SBarry Smith . u - input vector 6231325fc9f4SBarry Smith . A, B - the matrices 6232325fc9f4SBarry Smith - ctx - user context 6233325fc9f4SBarry Smith 6234325fc9f4SBarry Smith Level: developer 6235325fc9f4SBarry Smith 6236325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6237325fc9f4SBarry Smith 6238325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6239325fc9f4SBarry Smith @*/ 6240f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 6241325fc9f4SBarry Smith { 6242325fc9f4SBarry Smith PetscErrorCode ierr; 6243325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6244325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 6245325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 6246325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 6247325fc9f4SBarry Smith 6248325fc9f4SBarry Smith PetscFunctionBegin; 6249cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 62500910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 6251325fc9f4SBarry Smith 62520910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 6253325fc9f4SBarry Smith 6254cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 62550910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 62560910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 62570910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 62580910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 6259bbd56ea5SKarl Rupp 6260325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6261325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 6262325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6263325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6264325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 6265325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 6266325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 6267325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 6268325fc9f4SBarry Smith prhs[8] = sctx->ctx; 6269325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 6270325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6271325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6272325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6273325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6274325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6275325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6276325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6277325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 6278325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 6279325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6280325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 6281325fc9f4SBarry Smith PetscFunctionReturn(0); 6282325fc9f4SBarry Smith } 6283325fc9f4SBarry Smith 6284325fc9f4SBarry Smith 6285325fc9f4SBarry Smith #undef __FUNCT__ 6286325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 6287325fc9f4SBarry Smith /* 6288325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 6289e3c5b3baSBarry 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 6290325fc9f4SBarry Smith 6291325fc9f4SBarry Smith Logically Collective on TS 6292325fc9f4SBarry Smith 6293325fc9f4SBarry Smith Input Parameters: 6294cdcf91faSSean Farley + ts - the TS context 6295325fc9f4SBarry Smith . A,B - Jacobian matrices 6296325fc9f4SBarry Smith . func - function evaluation routine 6297325fc9f4SBarry Smith - ctx - user context 6298325fc9f4SBarry Smith 6299325fc9f4SBarry Smith Calling sequence of func: 63000910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 6301325fc9f4SBarry Smith 6302325fc9f4SBarry Smith 6303325fc9f4SBarry Smith Level: developer 6304325fc9f4SBarry Smith 6305325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6306325fc9f4SBarry Smith 6307325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6308325fc9f4SBarry Smith */ 6309cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 6310325fc9f4SBarry Smith { 6311325fc9f4SBarry Smith PetscErrorCode ierr; 6312325fc9f4SBarry Smith TSMatlabContext *sctx; 6313325fc9f4SBarry Smith 6314325fc9f4SBarry Smith PetscFunctionBegin; 6315325fc9f4SBarry Smith /* currently sctx is memory bleed */ 6316325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 6317325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6318325fc9f4SBarry Smith /* 6319325fc9f4SBarry Smith This should work, but it doesn't 6320325fc9f4SBarry Smith sctx->ctx = ctx; 6321325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6322325fc9f4SBarry Smith */ 6323325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6324bbd56ea5SKarl Rupp 6325cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 6326325fc9f4SBarry Smith PetscFunctionReturn(0); 6327325fc9f4SBarry Smith } 6328325fc9f4SBarry Smith 6329b5b1a830SBarry Smith #undef __FUNCT__ 6330b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 6331b5b1a830SBarry Smith /* 6332b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 6333b5b1a830SBarry Smith 6334b5b1a830SBarry Smith Collective on TS 6335b5b1a830SBarry Smith 6336b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6337b5b1a830SBarry Smith @*/ 63380910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 6339b5b1a830SBarry Smith { 6340b5b1a830SBarry Smith PetscErrorCode ierr; 6341b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6342a530c242SBarry Smith int nlhs = 1,nrhs = 6; 6343b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 6344b5b1a830SBarry Smith long long int lx = 0,ls = 0; 6345b5b1a830SBarry Smith 6346b5b1a830SBarry Smith PetscFunctionBegin; 6347cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 63480910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 6349b5b1a830SBarry Smith 6350cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 63510910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 6352bbd56ea5SKarl Rupp 6353b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6354b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 6355b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 6356b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 6357b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 6358b5b1a830SBarry Smith prhs[5] = sctx->ctx; 6359b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 6360b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6361b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 6362b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 6363b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 6364b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 6365b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 6366b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 6367b5b1a830SBarry Smith PetscFunctionReturn(0); 6368b5b1a830SBarry Smith } 6369b5b1a830SBarry Smith 6370b5b1a830SBarry Smith 6371b5b1a830SBarry Smith #undef __FUNCT__ 6372b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 6373b5b1a830SBarry Smith /* 6374b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 6375b5b1a830SBarry Smith 6376b5b1a830SBarry Smith Level: developer 6377b5b1a830SBarry Smith 6378b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 6379b5b1a830SBarry Smith 6380b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6381b5b1a830SBarry Smith */ 6382cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 6383b5b1a830SBarry Smith { 6384b5b1a830SBarry Smith PetscErrorCode ierr; 6385b5b1a830SBarry Smith TSMatlabContext *sctx; 6386b5b1a830SBarry Smith 6387b5b1a830SBarry Smith PetscFunctionBegin; 6388b5b1a830SBarry Smith /* currently sctx is memory bleed */ 6389b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 6390b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6391b5b1a830SBarry Smith /* 6392b5b1a830SBarry Smith This should work, but it doesn't 6393b5b1a830SBarry Smith sctx->ctx = ctx; 6394b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6395b5b1a830SBarry Smith */ 6396b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6397bbd56ea5SKarl Rupp 63980298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 6399b5b1a830SBarry Smith PetscFunctionReturn(0); 6400b5b1a830SBarry Smith } 6401325fc9f4SBarry Smith #endif 6402b3603a34SBarry Smith 6403b3603a34SBarry Smith #undef __FUNCT__ 64044f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 6405b3603a34SBarry Smith /*@C 64064f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 6407b3603a34SBarry Smith in a time based line graph 6408b3603a34SBarry Smith 6409b3603a34SBarry Smith Collective on TS 6410b3603a34SBarry Smith 6411b3603a34SBarry Smith Input Parameters: 6412b3603a34SBarry Smith + ts - the TS context 6413b3603a34SBarry Smith . step - current time-step 6414b3603a34SBarry Smith . ptime - current time 64157db568b7SBarry Smith . u - current solution 64167db568b7SBarry Smith - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 6417b3603a34SBarry Smith 6418b3d3934dSBarry Smith Options Database: 64199ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 6420b3d3934dSBarry Smith 6421b3603a34SBarry Smith Level: intermediate 6422b3603a34SBarry Smith 64236934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component solutions in a separate window 6424b3603a34SBarry Smith 6425b3603a34SBarry Smith .keywords: TS, vector, monitor, view 6426b3603a34SBarry Smith 64277db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 64287db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 64297db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 64307db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 6431b3603a34SBarry Smith @*/ 64327db568b7SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6433b3603a34SBarry Smith { 6434b3603a34SBarry Smith PetscErrorCode ierr; 64357db568b7SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 6436b3603a34SBarry Smith const PetscScalar *yy; 643780666b62SBarry Smith Vec v; 6438b3603a34SBarry Smith 6439b3603a34SBarry Smith PetscFunctionBegin; 644063e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 644158ff32f7SBarry Smith if (!step) { 6442a9f9c1f6SBarry Smith PetscDrawAxis axis; 64436934998bSLisandro Dalcin PetscInt dim; 6444a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6445a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 6446387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 6447387f4636SBarry Smith char **displaynames; 6448387f4636SBarry Smith PetscBool flg; 6449387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6450387f4636SBarry Smith ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr); 6451387f4636SBarry Smith ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 6452c5929fdfSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 6453387f4636SBarry Smith if (flg) { 6454a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 6455387f4636SBarry Smith } 6456387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 6457387f4636SBarry Smith } 6458387f4636SBarry Smith if (ctx->displaynames) { 6459387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 6460387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 6461387f4636SBarry Smith } else if (ctx->names) { 64620910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 64630b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6464387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 6465b0bc92c6SBarry Smith } else { 6466b0bc92c6SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6467b0bc92c6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6468387f4636SBarry Smith } 64690b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 647058ff32f7SBarry Smith } 64716934998bSLisandro Dalcin 64726934998bSLisandro Dalcin if (!ctx->transform) v = u; 64736934998bSLisandro Dalcin else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);} 647480666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 64756934998bSLisandro Dalcin if (ctx->displaynames) { 64766934998bSLisandro Dalcin PetscInt i; 64776934998bSLisandro Dalcin for (i=0; i<ctx->ndisplayvariables; i++) 64786934998bSLisandro Dalcin ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]); 64796934998bSLisandro Dalcin ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 64806934998bSLisandro Dalcin } else { 6481e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6482e3efe391SJed Brown PetscInt i,n; 64836934998bSLisandro Dalcin PetscReal *yreal; 648480666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 6485785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6486e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 64870b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6488e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6489e3efe391SJed Brown #else 64900b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6491e3efe391SJed Brown #endif 649280666b62SBarry Smith } 64936934998bSLisandro Dalcin ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 64946934998bSLisandro Dalcin if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);} 64956934998bSLisandro Dalcin 6496b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 64970b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 64986934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 64993923b477SBarry Smith } 6500b3603a34SBarry Smith PetscFunctionReturn(0); 6501b3603a34SBarry Smith } 6502b3603a34SBarry Smith 6503387f4636SBarry Smith 6504b3603a34SBarry Smith #undef __FUNCT__ 650531152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames" 6506b037adc7SBarry Smith /*@C 650731152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6508b037adc7SBarry Smith 6509b037adc7SBarry Smith Collective on TS 6510b037adc7SBarry Smith 6511b037adc7SBarry Smith Input Parameters: 6512b037adc7SBarry Smith + ts - the TS context 6513b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 6514b037adc7SBarry Smith 6515b037adc7SBarry Smith Level: intermediate 6516b037adc7SBarry Smith 65177db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 65187db568b7SBarry Smith 6519b037adc7SBarry Smith .keywords: TS, vector, monitor, view 6520b037adc7SBarry Smith 6521a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 6522b037adc7SBarry Smith @*/ 652331152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 6524b037adc7SBarry Smith { 6525b037adc7SBarry Smith PetscErrorCode ierr; 6526b037adc7SBarry Smith PetscInt i; 6527b037adc7SBarry Smith 6528b037adc7SBarry Smith PetscFunctionBegin; 6529b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6530b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 65315537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 6532b3d3934dSBarry Smith break; 6533b3d3934dSBarry Smith } 6534b3d3934dSBarry Smith } 6535b3d3934dSBarry Smith PetscFunctionReturn(0); 6536b3d3934dSBarry Smith } 6537b3d3934dSBarry Smith 6538b3d3934dSBarry Smith #undef __FUNCT__ 6539e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames" 6540e673d494SBarry Smith /*@C 6541e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6542e673d494SBarry Smith 6543e673d494SBarry Smith Collective on TS 6544e673d494SBarry Smith 6545e673d494SBarry Smith Input Parameters: 6546e673d494SBarry Smith + ts - the TS context 6547e673d494SBarry Smith - names - the names of the components, final string must be NULL 6548e673d494SBarry Smith 6549e673d494SBarry Smith Level: intermediate 6550e673d494SBarry Smith 6551e673d494SBarry Smith .keywords: TS, vector, monitor, view 6552e673d494SBarry Smith 6553a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 6554e673d494SBarry Smith @*/ 6555e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 6556e673d494SBarry Smith { 6557e673d494SBarry Smith PetscErrorCode ierr; 6558e673d494SBarry Smith 6559e673d494SBarry Smith PetscFunctionBegin; 6560e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 6561e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 6562e673d494SBarry Smith PetscFunctionReturn(0); 6563e673d494SBarry Smith } 6564e673d494SBarry Smith 6565e673d494SBarry Smith #undef __FUNCT__ 6566b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames" 6567b3d3934dSBarry Smith /*@C 6568b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 6569b3d3934dSBarry Smith 6570b3d3934dSBarry Smith Collective on TS 6571b3d3934dSBarry Smith 6572b3d3934dSBarry Smith Input Parameter: 6573b3d3934dSBarry Smith . ts - the TS context 6574b3d3934dSBarry Smith 6575b3d3934dSBarry Smith Output Parameter: 6576b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 6577b3d3934dSBarry Smith 6578b3d3934dSBarry Smith Level: intermediate 6579b3d3934dSBarry Smith 65807db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 65817db568b7SBarry Smith 6582b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6583b3d3934dSBarry Smith 6584b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6585b3d3934dSBarry Smith @*/ 6586b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 6587b3d3934dSBarry Smith { 6588b3d3934dSBarry Smith PetscInt i; 6589b3d3934dSBarry Smith 6590b3d3934dSBarry Smith PetscFunctionBegin; 6591b3d3934dSBarry Smith *names = NULL; 6592b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6593b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 65945537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 6595b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 6596b3d3934dSBarry Smith break; 6597387f4636SBarry Smith } 6598387f4636SBarry Smith } 6599387f4636SBarry Smith PetscFunctionReturn(0); 6600387f4636SBarry Smith } 6601387f4636SBarry Smith 6602387f4636SBarry Smith #undef __FUNCT__ 6603a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables" 6604a66092f1SBarry Smith /*@C 6605a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 6606a66092f1SBarry Smith 6607a66092f1SBarry Smith Collective on TS 6608a66092f1SBarry Smith 6609a66092f1SBarry Smith Input Parameters: 6610a66092f1SBarry Smith + ctx - the TSMonitorLG context 6611a66092f1SBarry Smith . displaynames - the names of the components, final string must be NULL 6612a66092f1SBarry Smith 6613a66092f1SBarry Smith Level: intermediate 6614a66092f1SBarry Smith 6615a66092f1SBarry Smith .keywords: TS, vector, monitor, view 6616a66092f1SBarry Smith 6617a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6618a66092f1SBarry Smith @*/ 6619a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 6620a66092f1SBarry Smith { 6621a66092f1SBarry Smith PetscInt j = 0,k; 6622a66092f1SBarry Smith PetscErrorCode ierr; 6623a66092f1SBarry Smith 6624a66092f1SBarry Smith PetscFunctionBegin; 6625a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 6626a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 6627a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 6628a66092f1SBarry Smith while (displaynames[j]) j++; 6629a66092f1SBarry Smith ctx->ndisplayvariables = j; 6630a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 6631a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 6632a66092f1SBarry Smith j = 0; 6633a66092f1SBarry Smith while (displaynames[j]) { 6634a66092f1SBarry Smith k = 0; 6635a66092f1SBarry Smith while (ctx->names[k]) { 6636a66092f1SBarry Smith PetscBool flg; 6637a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 6638a66092f1SBarry Smith if (flg) { 6639a66092f1SBarry Smith ctx->displayvariables[j] = k; 6640a66092f1SBarry Smith break; 6641a66092f1SBarry Smith } 6642a66092f1SBarry Smith k++; 6643a66092f1SBarry Smith } 6644a66092f1SBarry Smith j++; 6645a66092f1SBarry Smith } 6646a66092f1SBarry Smith PetscFunctionReturn(0); 6647a66092f1SBarry Smith } 6648a66092f1SBarry Smith 6649a66092f1SBarry Smith 6650a66092f1SBarry Smith #undef __FUNCT__ 6651387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables" 6652387f4636SBarry Smith /*@C 6653387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 6654387f4636SBarry Smith 6655387f4636SBarry Smith Collective on TS 6656387f4636SBarry Smith 6657387f4636SBarry Smith Input Parameters: 6658387f4636SBarry Smith + ts - the TS context 6659387f4636SBarry Smith . displaynames - the names of the components, final string must be NULL 6660387f4636SBarry Smith 66617db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 66627db568b7SBarry Smith 6663387f4636SBarry Smith Level: intermediate 6664387f4636SBarry Smith 6665387f4636SBarry Smith .keywords: TS, vector, monitor, view 6666387f4636SBarry Smith 6667387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6668387f4636SBarry Smith @*/ 6669387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 6670387f4636SBarry Smith { 6671a66092f1SBarry Smith PetscInt i; 6672387f4636SBarry Smith PetscErrorCode ierr; 6673387f4636SBarry Smith 6674387f4636SBarry Smith PetscFunctionBegin; 6675387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6676387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 66775537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 6678b3d3934dSBarry Smith break; 6679b037adc7SBarry Smith } 6680b037adc7SBarry Smith } 6681b037adc7SBarry Smith PetscFunctionReturn(0); 6682b037adc7SBarry Smith } 6683b037adc7SBarry Smith 6684b037adc7SBarry Smith #undef __FUNCT__ 668580666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform" 668680666b62SBarry Smith /*@C 668780666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 668880666b62SBarry Smith 668980666b62SBarry Smith Collective on TS 669080666b62SBarry Smith 669180666b62SBarry Smith Input Parameters: 669280666b62SBarry Smith + ts - the TS context 669380666b62SBarry Smith . transform - the transform function 66947684fa3eSBarry Smith . destroy - function to destroy the optional context 669580666b62SBarry Smith - ctx - optional context used by transform function 669680666b62SBarry Smith 66977db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 66987db568b7SBarry Smith 669980666b62SBarry Smith Level: intermediate 670080666b62SBarry Smith 670180666b62SBarry Smith .keywords: TS, vector, monitor, view 670280666b62SBarry Smith 6703a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 670480666b62SBarry Smith @*/ 67057684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 670680666b62SBarry Smith { 670780666b62SBarry Smith PetscInt i; 6708a66092f1SBarry Smith PetscErrorCode ierr; 670980666b62SBarry Smith 671080666b62SBarry Smith PetscFunctionBegin; 671180666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 671280666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 67135537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 671480666b62SBarry Smith } 671580666b62SBarry Smith } 671680666b62SBarry Smith PetscFunctionReturn(0); 671780666b62SBarry Smith } 671880666b62SBarry Smith 671980666b62SBarry Smith #undef __FUNCT__ 6720e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform" 6721e673d494SBarry Smith /*@C 6722e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 6723e673d494SBarry Smith 6724e673d494SBarry Smith Collective on TSLGCtx 6725e673d494SBarry Smith 6726e673d494SBarry Smith Input Parameters: 6727e673d494SBarry Smith + ts - the TS context 6728e673d494SBarry Smith . transform - the transform function 67297684fa3eSBarry Smith . destroy - function to destroy the optional context 6730e673d494SBarry Smith - ctx - optional context used by transform function 6731e673d494SBarry Smith 6732e673d494SBarry Smith Level: intermediate 6733e673d494SBarry Smith 6734e673d494SBarry Smith .keywords: TS, vector, monitor, view 6735e673d494SBarry Smith 6736a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 6737e673d494SBarry Smith @*/ 67387684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 6739e673d494SBarry Smith { 6740e673d494SBarry Smith PetscFunctionBegin; 6741e673d494SBarry Smith ctx->transform = transform; 67427684fa3eSBarry Smith ctx->transformdestroy = destroy; 6743e673d494SBarry Smith ctx->transformctx = tctx; 6744e673d494SBarry Smith PetscFunctionReturn(0); 6745e673d494SBarry Smith } 6746e673d494SBarry Smith 6747b3603a34SBarry Smith #undef __FUNCT__ 67484f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 6749ef20d060SBarry Smith /*@C 67504f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 6751ef20d060SBarry Smith in a time based line graph 6752ef20d060SBarry Smith 6753ef20d060SBarry Smith Collective on TS 6754ef20d060SBarry Smith 6755ef20d060SBarry Smith Input Parameters: 6756ef20d060SBarry Smith + ts - the TS context 6757ef20d060SBarry Smith . step - current time-step 6758ef20d060SBarry Smith . ptime - current time 67597db568b7SBarry Smith . u - current solution 67607db568b7SBarry Smith - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 6761ef20d060SBarry Smith 6762ef20d060SBarry Smith Level: intermediate 6763ef20d060SBarry Smith 67646934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component errors in a separate window 6765abd5a294SJed Brown 6766abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 6767abd5a294SJed Brown 6768abd5a294SJed Brown Options Database Keys: 67694f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 6770ef20d060SBarry Smith 6771ef20d060SBarry Smith .keywords: TS, vector, monitor, view 6772ef20d060SBarry Smith 6773abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 6774ef20d060SBarry Smith @*/ 67750910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 6776ef20d060SBarry Smith { 6777ef20d060SBarry Smith PetscErrorCode ierr; 67780b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 6779ef20d060SBarry Smith const PetscScalar *yy; 6780ef20d060SBarry Smith Vec y; 6781ef20d060SBarry Smith 6782ef20d060SBarry Smith PetscFunctionBegin; 6783a9f9c1f6SBarry Smith if (!step) { 6784a9f9c1f6SBarry Smith PetscDrawAxis axis; 67856934998bSLisandro Dalcin PetscInt dim; 6786a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 67871ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 67880910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6789a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6790a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6791a9f9c1f6SBarry Smith } 67920910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 6793ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 67940910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 6795ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 6796e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6797e3efe391SJed Brown { 6798e3efe391SJed Brown PetscReal *yreal; 6799e3efe391SJed Brown PetscInt i,n; 6800e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 6801785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6802e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 68030b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6804e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6805e3efe391SJed Brown } 6806e3efe391SJed Brown #else 68070b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6808e3efe391SJed Brown #endif 6809ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 6810ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 6811b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 68120b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 68136934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 68143923b477SBarry Smith } 6815ef20d060SBarry Smith PetscFunctionReturn(0); 6816ef20d060SBarry Smith } 6817ef20d060SBarry Smith 6818201da799SBarry Smith #undef __FUNCT__ 6819201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 6820201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6821201da799SBarry Smith { 6822201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6823201da799SBarry Smith PetscReal x = ptime,y; 6824201da799SBarry Smith PetscErrorCode ierr; 6825201da799SBarry Smith PetscInt its; 6826201da799SBarry Smith 6827201da799SBarry Smith PetscFunctionBegin; 682863e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 6829201da799SBarry Smith if (!n) { 6830201da799SBarry Smith PetscDrawAxis axis; 6831201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6832201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 6833201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6834201da799SBarry Smith ctx->snes_its = 0; 6835201da799SBarry Smith } 6836201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 6837201da799SBarry Smith y = its - ctx->snes_its; 6838201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 68393fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6840201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 68416934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 6842201da799SBarry Smith } 6843201da799SBarry Smith ctx->snes_its = its; 6844201da799SBarry Smith PetscFunctionReturn(0); 6845201da799SBarry Smith } 6846201da799SBarry Smith 6847201da799SBarry Smith #undef __FUNCT__ 6848201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 6849201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6850201da799SBarry Smith { 6851201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6852201da799SBarry Smith PetscReal x = ptime,y; 6853201da799SBarry Smith PetscErrorCode ierr; 6854201da799SBarry Smith PetscInt its; 6855201da799SBarry Smith 6856201da799SBarry Smith PetscFunctionBegin; 685763e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 6858201da799SBarry Smith if (!n) { 6859201da799SBarry Smith PetscDrawAxis axis; 6860201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6861201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 6862201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6863201da799SBarry Smith ctx->ksp_its = 0; 6864201da799SBarry Smith } 6865201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 6866201da799SBarry Smith y = its - ctx->ksp_its; 6867201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 686899fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6869201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 68706934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 6871201da799SBarry Smith } 6872201da799SBarry Smith ctx->ksp_its = its; 6873201da799SBarry Smith PetscFunctionReturn(0); 6874201da799SBarry Smith } 6875f9c1d6abSBarry Smith 6876f9c1d6abSBarry Smith #undef __FUNCT__ 6877f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 6878f9c1d6abSBarry Smith /*@ 6879f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 6880f9c1d6abSBarry Smith 6881f9c1d6abSBarry Smith Collective on TS and Vec 6882f9c1d6abSBarry Smith 6883f9c1d6abSBarry Smith Input Parameters: 6884f9c1d6abSBarry Smith + ts - the TS context 6885f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 6886f9c1d6abSBarry Smith 6887f9c1d6abSBarry Smith Output Parameters: 6888f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 6889f9c1d6abSBarry Smith 6890f9c1d6abSBarry Smith Level: developer 6891f9c1d6abSBarry Smith 6892f9c1d6abSBarry Smith .keywords: TS, compute 6893f9c1d6abSBarry Smith 6894f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 6895f9c1d6abSBarry Smith @*/ 6896f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 6897f9c1d6abSBarry Smith { 6898f9c1d6abSBarry Smith PetscErrorCode ierr; 6899f9c1d6abSBarry Smith 6900f9c1d6abSBarry Smith PetscFunctionBegin; 6901f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6902ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 6903f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 6904f9c1d6abSBarry Smith PetscFunctionReturn(0); 6905f9c1d6abSBarry Smith } 690624655328SShri 6907b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 6908b3d3934dSBarry Smith #undef __FUNCT__ 6909b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate" 6910b3d3934dSBarry Smith /*@C 6911b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 6912b3d3934dSBarry Smith 6913b3d3934dSBarry Smith Collective on TS 6914b3d3934dSBarry Smith 6915b3d3934dSBarry Smith Input Parameters: 6916b3d3934dSBarry Smith . ts - the ODE solver object 6917b3d3934dSBarry Smith 6918b3d3934dSBarry Smith Output Parameter: 6919b3d3934dSBarry Smith . ctx - the context 6920b3d3934dSBarry Smith 6921b3d3934dSBarry Smith Level: intermediate 6922b3d3934dSBarry Smith 6923b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso 6924b3d3934dSBarry Smith 6925b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 6926b3d3934dSBarry Smith 6927b3d3934dSBarry Smith @*/ 6928b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 6929b3d3934dSBarry Smith { 6930b3d3934dSBarry Smith PetscErrorCode ierr; 6931b3d3934dSBarry Smith 6932b3d3934dSBarry Smith PetscFunctionBegin; 6933a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 6934b3d3934dSBarry Smith PetscFunctionReturn(0); 6935b3d3934dSBarry Smith } 6936b3d3934dSBarry Smith 6937b3d3934dSBarry Smith #undef __FUNCT__ 6938b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope" 6939b3d3934dSBarry Smith /*@C 6940b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 6941b3d3934dSBarry Smith 6942b3d3934dSBarry Smith Collective on TS 6943b3d3934dSBarry Smith 6944b3d3934dSBarry Smith Input Parameters: 6945b3d3934dSBarry Smith + ts - the TS context 6946b3d3934dSBarry Smith . step - current time-step 6947b3d3934dSBarry Smith . ptime - current time 69487db568b7SBarry Smith . u - current solution 69497db568b7SBarry Smith - dctx - the envelope context 6950b3d3934dSBarry Smith 6951b3d3934dSBarry Smith Options Database: 6952b3d3934dSBarry Smith . -ts_monitor_envelope 6953b3d3934dSBarry Smith 6954b3d3934dSBarry Smith Level: intermediate 6955b3d3934dSBarry Smith 6956b3d3934dSBarry Smith Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 6957b3d3934dSBarry Smith 6958b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6959b3d3934dSBarry Smith 69607db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 6961b3d3934dSBarry Smith @*/ 69627db568b7SBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6963b3d3934dSBarry Smith { 6964b3d3934dSBarry Smith PetscErrorCode ierr; 69657db568b7SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 6966b3d3934dSBarry Smith 6967b3d3934dSBarry Smith PetscFunctionBegin; 6968b3d3934dSBarry Smith if (!ctx->max) { 6969b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 6970b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 6971b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 6972b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 6973b3d3934dSBarry Smith } else { 6974b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 6975b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 6976b3d3934dSBarry Smith } 6977b3d3934dSBarry Smith PetscFunctionReturn(0); 6978b3d3934dSBarry Smith } 6979b3d3934dSBarry Smith 6980b3d3934dSBarry Smith 6981b3d3934dSBarry Smith #undef __FUNCT__ 6982b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds" 6983b3d3934dSBarry Smith /*@C 6984b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 6985b3d3934dSBarry Smith 6986b3d3934dSBarry Smith Collective on TS 6987b3d3934dSBarry Smith 6988b3d3934dSBarry Smith Input Parameter: 6989b3d3934dSBarry Smith . ts - the TS context 6990b3d3934dSBarry Smith 6991b3d3934dSBarry Smith Output Parameter: 6992b3d3934dSBarry Smith + max - the maximum values 6993b3d3934dSBarry Smith - min - the minimum values 6994b3d3934dSBarry Smith 69957db568b7SBarry Smith Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 69967db568b7SBarry Smith 6997b3d3934dSBarry Smith Level: intermediate 6998b3d3934dSBarry Smith 6999b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7000b3d3934dSBarry Smith 7001b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 7002b3d3934dSBarry Smith @*/ 7003b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 7004b3d3934dSBarry Smith { 7005b3d3934dSBarry Smith PetscInt i; 7006b3d3934dSBarry Smith 7007b3d3934dSBarry Smith PetscFunctionBegin; 7008b3d3934dSBarry Smith if (max) *max = NULL; 7009b3d3934dSBarry Smith if (min) *min = NULL; 7010b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7011b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 70125537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 7013b3d3934dSBarry Smith if (max) *max = ctx->max; 7014b3d3934dSBarry Smith if (min) *min = ctx->min; 7015b3d3934dSBarry Smith break; 7016b3d3934dSBarry Smith } 7017b3d3934dSBarry Smith } 7018b3d3934dSBarry Smith PetscFunctionReturn(0); 7019b3d3934dSBarry Smith } 7020b3d3934dSBarry Smith 7021b3d3934dSBarry Smith #undef __FUNCT__ 7022b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy" 7023b3d3934dSBarry Smith /*@C 7024b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 7025b3d3934dSBarry Smith 7026b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 7027b3d3934dSBarry Smith 7028b3d3934dSBarry Smith Input Parameter: 7029b3d3934dSBarry Smith . ctx - the monitor context 7030b3d3934dSBarry Smith 7031b3d3934dSBarry Smith Level: intermediate 7032b3d3934dSBarry Smith 7033b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy 7034b3d3934dSBarry Smith 70357db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 7036b3d3934dSBarry Smith @*/ 7037b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 7038b3d3934dSBarry Smith { 7039b3d3934dSBarry Smith PetscErrorCode ierr; 7040b3d3934dSBarry Smith 7041b3d3934dSBarry Smith PetscFunctionBegin; 7042b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 7043b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 7044b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 7045b3d3934dSBarry Smith PetscFunctionReturn(0); 7046b3d3934dSBarry Smith } 7047f2dee214SBarry Smith 704824655328SShri #undef __FUNCT__ 704924655328SShri #define __FUNCT__ "TSRollBack" 705024655328SShri /*@ 705124655328SShri TSRollBack - Rolls back one time step 705224655328SShri 705324655328SShri Collective on TS 705424655328SShri 705524655328SShri Input Parameter: 705624655328SShri . ts - the TS context obtained from TSCreate() 705724655328SShri 705824655328SShri Level: advanced 705924655328SShri 706024655328SShri .keywords: TS, timestep, rollback 706124655328SShri 706224655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 706324655328SShri @*/ 706424655328SShri PetscErrorCode TSRollBack(TS ts) 706524655328SShri { 706624655328SShri PetscErrorCode ierr; 706724655328SShri 706824655328SShri PetscFunctionBegin; 706924655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7070b3de5cdeSLisandro Dalcin if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called"); 707124655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 707224655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 707324655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 707424655328SShri ts->ptime = ts->ptime_prev; 7075be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 7076be5899b3SLisandro Dalcin ts->steps--; ts->total_steps--; 7077b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 707824655328SShri PetscFunctionReturn(0); 707924655328SShri } 7080aeb4809dSShri Abhyankar 7081ff22ae23SHong Zhang #undef __FUNCT__ 7082ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages" 7083ff22ae23SHong Zhang /*@ 7084ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 7085ff22ae23SHong Zhang 7086ff22ae23SHong Zhang Input Parameter: 7087ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 7088ff22ae23SHong Zhang 7089ff22ae23SHong Zhang Level: advanced 7090ff22ae23SHong Zhang 7091ff22ae23SHong Zhang .keywords: TS, getstages 7092ff22ae23SHong Zhang 7093ff22ae23SHong Zhang .seealso: TSCreate() 7094ff22ae23SHong Zhang @*/ 7095ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns,Vec **Y) 7096ff22ae23SHong Zhang { 7097ff22ae23SHong Zhang PetscErrorCode ierr; 7098ff22ae23SHong Zhang 7099ff22ae23SHong Zhang PetscFunctionBegin; 7100ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7101ff22ae23SHong Zhang PetscValidPointer(ns,2); 7102ff22ae23SHong Zhang 7103ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 7104ff22ae23SHong Zhang else { 7105ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 7106ff22ae23SHong Zhang } 7107ff22ae23SHong Zhang PetscFunctionReturn(0); 7108ff22ae23SHong Zhang } 7109ff22ae23SHong Zhang 7110847ff0e1SMatthew G. Knepley #undef __FUNCT__ 7111847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor" 7112847ff0e1SMatthew G. Knepley /*@C 7113847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 7114847ff0e1SMatthew G. Knepley 7115847ff0e1SMatthew G. Knepley Collective on SNES 7116847ff0e1SMatthew G. Knepley 7117847ff0e1SMatthew G. Knepley Input Parameters: 7118847ff0e1SMatthew G. Knepley + ts - the TS context 7119847ff0e1SMatthew G. Knepley . t - current timestep 7120847ff0e1SMatthew G. Knepley . U - state vector 7121847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 7122847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 7123847ff0e1SMatthew G. Knepley - ctx - an optional user context 7124847ff0e1SMatthew G. Knepley 7125847ff0e1SMatthew G. Knepley Output Parameters: 7126847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 7127847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 7128847ff0e1SMatthew G. Knepley 7129847ff0e1SMatthew G. Knepley Level: intermediate 7130847ff0e1SMatthew G. Knepley 7131847ff0e1SMatthew G. Knepley Notes: 7132847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 7133847ff0e1SMatthew G. Knepley 7134847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 7135847ff0e1SMatthew G. Knepley 7136847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 7137847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 7138847ff0e1SMatthew G. Knepley 7139847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 7140847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 7141847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 7142847ff0e1SMatthew G. Knepley 7143847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse 7144847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 7145847ff0e1SMatthew G. Knepley @*/ 7146847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 7147847ff0e1SMatthew G. Knepley { 7148847ff0e1SMatthew G. Knepley SNES snes; 7149847ff0e1SMatthew G. Knepley MatFDColoring color; 7150847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 7151847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 7152847ff0e1SMatthew G. Knepley 7153847ff0e1SMatthew G. Knepley PetscFunctionBegin; 7154c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 7155847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 7156847ff0e1SMatthew G. Knepley if (!color) { 7157847ff0e1SMatthew G. Knepley DM dm; 7158847ff0e1SMatthew G. Knepley ISColoring iscoloring; 7159847ff0e1SMatthew G. Knepley 7160847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 7161847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 7162847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 7163847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 7164847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7165847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7166847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7167847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7168847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7169847ff0e1SMatthew G. Knepley } else { 7170847ff0e1SMatthew G. Knepley MatColoring mc; 7171847ff0e1SMatthew G. Knepley 7172847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 7173847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 7174847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 7175847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 7176847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 7177847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 7178847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7179847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7180847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7181847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7182847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7183847ff0e1SMatthew G. Knepley } 7184847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 7185847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 7186847ff0e1SMatthew G. Knepley } 7187847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 7188847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 7189847ff0e1SMatthew G. Knepley if (J != B) { 7190847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7191847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7192847ff0e1SMatthew G. Knepley } 7193847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 7194847ff0e1SMatthew G. Knepley } 719593b34091SDebojyoti Ghosh 719693b34091SDebojyoti Ghosh #undef __FUNCT__ 7197cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSSetFunctionDomainError" 7198cb9d8021SPierre Barbier de Reuille /*@ 7199cb9d8021SPierre Barbier de Reuille TSSetFunctionDomainError - Set the function testing if the current state vector is valid 7200cb9d8021SPierre Barbier de Reuille 7201cb9d8021SPierre Barbier de Reuille Input Parameters: 7202cb9d8021SPierre Barbier de Reuille ts - the TS context 7203cb9d8021SPierre Barbier de Reuille func - function called within TSFunctionDomainError 7204cb9d8021SPierre Barbier de Reuille 7205cb9d8021SPierre Barbier de Reuille Level: intermediate 7206cb9d8021SPierre Barbier de Reuille 7207cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain 7208cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError() 7209cb9d8021SPierre Barbier de Reuille @*/ 7210cb9d8021SPierre Barbier de Reuille 7211d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 7212cb9d8021SPierre Barbier de Reuille { 7213cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7214cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7215cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 7216cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7217cb9d8021SPierre Barbier de Reuille } 7218cb9d8021SPierre Barbier de Reuille 7219cb9d8021SPierre Barbier de Reuille #undef __FUNCT__ 7220cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSFunctionDomainError" 7221cb9d8021SPierre Barbier de Reuille /*@ 7222cb9d8021SPierre Barbier de Reuille TSFunctionDomainError - Check if the current state is valid 7223cb9d8021SPierre Barbier de Reuille 7224cb9d8021SPierre Barbier de Reuille Input Parameters: 7225cb9d8021SPierre Barbier de Reuille ts - the TS context 7226cb9d8021SPierre Barbier de Reuille stagetime - time of the simulation 7227d183316bSPierre Barbier de Reuille Y - state vector to check. 7228cb9d8021SPierre Barbier de Reuille 7229cb9d8021SPierre Barbier de Reuille Output Parameter: 7230cb9d8021SPierre Barbier de Reuille accept - Set to PETSC_FALSE if the current state vector is valid. 7231cb9d8021SPierre Barbier de Reuille 7232cb9d8021SPierre Barbier de Reuille Note: 7233cb9d8021SPierre Barbier de Reuille This function should be used to ensure the state is in a valid part of the space. 7234cb9d8021SPierre Barbier de Reuille For example, one can ensure here all values are positive. 723596a0c994SBarry Smith 723696a0c994SBarry Smith Level: advanced 7237cb9d8021SPierre Barbier de Reuille @*/ 7238d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 7239cb9d8021SPierre Barbier de Reuille { 7240cb9d8021SPierre Barbier de Reuille PetscErrorCode ierr; 7241cb9d8021SPierre Barbier de Reuille 7242cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7243cb9d8021SPierre Barbier de Reuille 7244cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7245cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 7246cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 7247d183316bSPierre Barbier de Reuille PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 7248cb9d8021SPierre Barbier de Reuille } 7249cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7250cb9d8021SPierre Barbier de Reuille } 72511ceb14c0SBarry Smith 72521ceb14c0SBarry Smith #undef __FUNCT__ 7253e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone" 725493b34091SDebojyoti Ghosh /*@C 7255e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 725693b34091SDebojyoti Ghosh 725793b34091SDebojyoti Ghosh Collective on MPI_Comm 725893b34091SDebojyoti Ghosh 725993b34091SDebojyoti Ghosh Input Parameter: 726093b34091SDebojyoti Ghosh . tsin - The input TS 726193b34091SDebojyoti Ghosh 726293b34091SDebojyoti Ghosh Output Parameter: 7263e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 726493b34091SDebojyoti Ghosh 72655eca1a21SEmil Constantinescu Notes: 72665eca1a21SEmil 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. 72675eca1a21SEmil Constantinescu 7268baa10174SEmil 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); 72695eca1a21SEmil Constantinescu 72705eca1a21SEmil Constantinescu Level: developer 727193b34091SDebojyoti Ghosh 7272e5168f73SEmil Constantinescu .keywords: TS, clone 7273e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 727493b34091SDebojyoti Ghosh @*/ 7275baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 727693b34091SDebojyoti Ghosh { 727793b34091SDebojyoti Ghosh TS t; 727893b34091SDebojyoti Ghosh PetscErrorCode ierr; 7279dc846ba4SSatish Balay SNES snes_start; 7280dc846ba4SSatish Balay DM dm; 7281dc846ba4SSatish Balay TSType type; 728293b34091SDebojyoti Ghosh 728393b34091SDebojyoti Ghosh PetscFunctionBegin; 728493b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 728593b34091SDebojyoti Ghosh *tsout = NULL; 728693b34091SDebojyoti Ghosh 72877a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 728893b34091SDebojyoti Ghosh 728993b34091SDebojyoti Ghosh /* General TS description */ 729093b34091SDebojyoti Ghosh t->numbermonitors = 0; 729193b34091SDebojyoti Ghosh t->setupcalled = 0; 729293b34091SDebojyoti Ghosh t->ksp_its = 0; 729393b34091SDebojyoti Ghosh t->snes_its = 0; 729493b34091SDebojyoti Ghosh t->nwork = 0; 729593b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 729693b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 729793b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 729893b34091SDebojyoti Ghosh 729934561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr); 730034561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr); 7301d15a3a53SEmil Constantinescu 730293b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr); 730393b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm);CHKERRQ(ierr); 730493b34091SDebojyoti Ghosh 730593b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 730651699248SLisandro Dalcin ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr); 730793b34091SDebojyoti Ghosh 730893b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 730993b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 731093b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 731193b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 731293b34091SDebojyoti Ghosh t->steps = tsin->steps; 731393b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 731493b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 731593b34091SDebojyoti Ghosh t->atol = tsin->atol; 731693b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 731793b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 731893b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 731993b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 732093b34091SDebojyoti Ghosh 732193b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type);CHKERRQ(ierr); 732293b34091SDebojyoti Ghosh ierr = TSSetType(t,type);CHKERRQ(ierr); 732393b34091SDebojyoti Ghosh 732493b34091SDebojyoti Ghosh t->vec_sol = NULL; 732593b34091SDebojyoti Ghosh 732693b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 732793b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 732893b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 732993b34091SDebojyoti Ghosh 733093b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 733193b34091SDebojyoti Ghosh 73320d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 73330d4fed19SBarry Smith PetscInt i; 73340d4fed19SBarry Smith ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr); 73350d4fed19SBarry Smith for (i=0; i<10; i++) { 73360d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 73370d4fed19SBarry Smith } 73380d4fed19SBarry Smith } 733993b34091SDebojyoti Ghosh *tsout = t; 734093b34091SDebojyoti Ghosh PetscFunctionReturn(0); 734193b34091SDebojyoti Ghosh } 7342