163dd3a1aSKris Buschelman 2af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 3496e6a7aSJed Brown #include <petscdmshell.h> 41e25c274SJed Brown #include <petscdmda.h> 52d5ee99bSBarry Smith #include <petscviewer.h> 62d5ee99bSBarry Smith #include <petscdraw.h> 7d763cef2SBarry Smith 8d5ba7fb7SMatthew Knepley /* Logging support */ 9d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 108d0ad7a8SHong Zhang PetscLogEvent TS_AdjointStep, TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 11d405a339SMatthew Knepley 12feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1349354f04SShri Abhyankar 142d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx { 152d5ee99bSBarry Smith PetscViewer viewer; 162d5ee99bSBarry Smith Vec initialsolution; 172d5ee99bSBarry Smith PetscBool showinitial; 182d5ee99bSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 192d5ee99bSBarry Smith PetscBool showtimestepandtime; 202d5ee99bSBarry Smith }; 212d5ee99bSBarry Smith 22fde5950dSBarry Smith /*@C 23fde5950dSBarry Smith TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 24fde5950dSBarry Smith 25fde5950dSBarry Smith Collective on TS 26fde5950dSBarry Smith 27fde5950dSBarry Smith Input Parameters: 28fde5950dSBarry Smith + ts - TS object you wish to monitor 29fde5950dSBarry Smith . name - the monitor type one is seeking 30fde5950dSBarry Smith . help - message indicating what monitoring is done 31fde5950dSBarry Smith . manual - manual page for the monitor 32fde5950dSBarry Smith . monitor - the monitor function 33fde5950dSBarry Smith - monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects 34fde5950dSBarry Smith 35fde5950dSBarry Smith Level: developer 36fde5950dSBarry Smith 37fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 38fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 39fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 40fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 41fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 42fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 43fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 44fde5950dSBarry Smith @*/ 45721cd6eeSBarry Smith PetscErrorCode TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*)) 46fde5950dSBarry Smith { 47fde5950dSBarry Smith PetscErrorCode ierr; 48fde5950dSBarry Smith PetscViewer viewer; 49fde5950dSBarry Smith PetscViewerFormat format; 50fde5950dSBarry Smith PetscBool flg; 51fde5950dSBarry Smith 52fde5950dSBarry Smith PetscFunctionBegin; 53fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 54fde5950dSBarry Smith if (flg) { 55721cd6eeSBarry Smith PetscViewerAndFormat *vf; 56721cd6eeSBarry Smith ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr); 57721cd6eeSBarry Smith ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 58fde5950dSBarry Smith if (monitorsetup) { 59721cd6eeSBarry Smith ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr); 60fde5950dSBarry Smith } 61721cd6eeSBarry Smith ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr); 62fde5950dSBarry Smith } 63fde5950dSBarry Smith PetscFunctionReturn(0); 64fde5950dSBarry Smith } 65fde5950dSBarry Smith 66fde5950dSBarry Smith /*@C 67e875c4f7SBarry Smith TSAdjointMonitorSensi - monitors the first lambda sensitivity 68287c2655SBarry Smith 69287c2655SBarry Smith Level: intermediate 70287c2655SBarry Smith 71287c2655SBarry Smith .keywords: TS, set, monitor 72287c2655SBarry Smith 73287c2655SBarry Smith .seealso: TSAdjointMonitorSet() 74287c2655SBarry Smith @*/ 75f4c7b390SBarry Smith PetscErrorCode TSAdjointMonitorSensi(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf) 76287c2655SBarry Smith { 77287c2655SBarry Smith PetscErrorCode ierr; 78287c2655SBarry Smith PetscViewer viewer = vf->viewer; 79287c2655SBarry Smith 80287c2655SBarry Smith PetscFunctionBegin; 81287c2655SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 82287c2655SBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 83287c2655SBarry Smith ierr = VecView(lambda[0],viewer);CHKERRQ(ierr); 84287c2655SBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 85287c2655SBarry Smith PetscFunctionReturn(0); 86287c2655SBarry Smith } 87287c2655SBarry Smith 88287c2655SBarry Smith /*@C 89fde5950dSBarry Smith TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 90fde5950dSBarry Smith 91fde5950dSBarry Smith Collective on TS 92fde5950dSBarry Smith 93fde5950dSBarry Smith Input Parameters: 94fde5950dSBarry Smith + ts - TS object you wish to monitor 95fde5950dSBarry Smith . name - the monitor type one is seeking 96fde5950dSBarry Smith . help - message indicating what monitoring is done 97fde5950dSBarry Smith . manual - manual page for the monitor 98fde5950dSBarry Smith . monitor - the monitor function 99fde5950dSBarry 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 100fde5950dSBarry Smith 101fde5950dSBarry Smith Level: developer 102fde5950dSBarry Smith 103fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 104fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 105fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 106fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 107fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 108fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 109fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 110fde5950dSBarry Smith @*/ 111721cd6eeSBarry 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*)) 112fde5950dSBarry Smith { 113fde5950dSBarry Smith PetscErrorCode ierr; 114fde5950dSBarry Smith PetscViewer viewer; 115fde5950dSBarry Smith PetscViewerFormat format; 116fde5950dSBarry Smith PetscBool flg; 117fde5950dSBarry Smith 118fde5950dSBarry Smith PetscFunctionBegin; 119fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 120fde5950dSBarry Smith if (flg) { 121721cd6eeSBarry Smith PetscViewerAndFormat *vf; 122721cd6eeSBarry Smith ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr); 123721cd6eeSBarry Smith ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 124fde5950dSBarry Smith if (monitorsetup) { 125721cd6eeSBarry Smith ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr); 126fde5950dSBarry Smith } 127721cd6eeSBarry Smith ierr = TSAdjointMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr); 128fde5950dSBarry Smith } 129fde5950dSBarry Smith PetscFunctionReturn(0); 130fde5950dSBarry Smith } 131fde5950dSBarry Smith 1322ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type) 1332ffb9264SLisandro Dalcin { 1342ffb9264SLisandro Dalcin PetscErrorCode ierr; 1352ffb9264SLisandro Dalcin 1362ffb9264SLisandro Dalcin PetscFunctionBegin; 137b92453a8SLisandro Dalcin PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1); 138b92453a8SLisandro Dalcin PetscValidCharPointer(default_type,2); 1392ffb9264SLisandro Dalcin if (!((PetscObject)adapt)->type_name) { 1402ffb9264SLisandro Dalcin ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr); 1412ffb9264SLisandro Dalcin } 1422ffb9264SLisandro Dalcin PetscFunctionReturn(0); 1432ffb9264SLisandro Dalcin } 1442ffb9264SLisandro Dalcin 145bdad233fSMatthew Knepley /*@ 146bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 147bdad233fSMatthew Knepley 148bdad233fSMatthew Knepley Collective on TS 149bdad233fSMatthew Knepley 150bdad233fSMatthew Knepley Input Parameter: 151bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 152bdad233fSMatthew Knepley 153bdad233fSMatthew Knepley Options Database Keys: 154b6a60446SDebojyoti Ghosh + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE 155ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 1563e4cdcaaSBarry Smith . -ts_max_steps <maxsteps> - maximum number of time-steps to take 1573e4cdcaaSBarry Smith . -ts_final_time <time> - maximum time to compute to 1583e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 159a3cdaa26SBarry 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 160a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 161a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 162a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 163a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 164a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 165ef222394SBarry Smith . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 166847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 167bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 168de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 169de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 1706934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 171de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 172de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 173de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 174de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 1753e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 1763e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 177fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 178e4160dc7SJulian Andrej . -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu) 179110eb670SHong Zhang . -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 180110eb670SHong Zhang . -ts_adjoint_monitor - print information at each adjoint time step 18153ea634cSHong Zhang - -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically 18253ea634cSHong Zhang 18391b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 184bdad233fSMatthew Knepley 185bdad233fSMatthew Knepley Level: beginner 186bdad233fSMatthew Knepley 187bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 188bdad233fSMatthew Knepley 189a313700dSBarry Smith .seealso: TSGetType() 190bdad233fSMatthew Knepley @*/ 1917087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 192bdad233fSMatthew Knepley { 193bc952696SBarry Smith PetscBool opt,flg,tflg; 194dfbe8321SBarry Smith PetscErrorCode ierr; 195eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 19631748224SBarry Smith PetscReal time_step; 19749354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 198d1212d36SBarry Smith char dir[16]; 199cd11d68dSLisandro Dalcin TSIFunction ifun; 2006991f827SBarry Smith const char *defaultType; 2016991f827SBarry Smith char typeName[256]; 202bdad233fSMatthew Knepley 203bdad233fSMatthew Knepley PetscFunctionBegin; 2040700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2056991f827SBarry Smith 2066991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 207cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 208cd11d68dSLisandro Dalcin 209cd11d68dSLisandro Dalcin ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 210cd11d68dSLisandro Dalcin if (((PetscObject)ts)->type_name) 211cd11d68dSLisandro Dalcin defaultType = ((PetscObject)ts)->type_name; 212cd11d68dSLisandro Dalcin else 213cd11d68dSLisandro Dalcin defaultType = ifun ? TSBEULER : TSEULER; 2146991f827SBarry Smith ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr); 2156991f827SBarry Smith if (opt) { 2166991f827SBarry Smith ierr = TSSetType(ts,typeName);CHKERRQ(ierr); 2176991f827SBarry Smith } else { 2186991f827SBarry Smith ierr = TSSetType(ts,defaultType);CHKERRQ(ierr); 2196991f827SBarry Smith } 220bdad233fSMatthew Knepley 221bdad233fSMatthew Knepley /* Handle generic TS options */ 2220298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 2230298fd71SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 2240298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 22531748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 226cd11d68dSLisandro Dalcin if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);} 22749354f04SShri 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); 22849354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 2290298fd71SBarry 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); 2300298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 2310298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 2320298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 2330298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 234bdad233fSMatthew Knepley 23556f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 23656f85f32SBarry Smith { 23756f85f32SBarry Smith PetscBool set; 23856f85f32SBarry Smith flg = PETSC_FALSE; 23956f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 24056f85f32SBarry Smith if (set) { 24156f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 24256f85f32SBarry Smith } 24356f85f32SBarry Smith } 24456f85f32SBarry Smith #endif 24556f85f32SBarry Smith 246bdad233fSMatthew Knepley /* Monitor options */ 247cd11d68dSLisandro Dalcin ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr); 248fde5950dSBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr); 249fde5950dSBarry Smith ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr); 250e875c4f7SBarry Smith ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor_sensi","Monitor sensitivity in the adjoint computation","TSAdjointMonitorSensi",TSAdjointMonitorSensi,NULL);CHKERRQ(ierr); 251fde5950dSBarry Smith 2525180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2535180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 2545180491cSLisandro Dalcin 2554f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 256b3603a34SBarry Smith if (opt) { 2570b039ecaSBarry Smith TSMonitorLGCtx ctx; 2583923b477SBarry Smith PetscInt howoften = 1; 259b3603a34SBarry Smith 2600298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 2616ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2624f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 263bdad233fSMatthew Knepley } 2646ba87a44SLisandro Dalcin 2654f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 266ef20d060SBarry Smith if (opt) { 2670b039ecaSBarry Smith TSMonitorLGCtx ctx; 2683923b477SBarry Smith PetscInt howoften = 1; 269ef20d060SBarry Smith 2700298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 2716ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2724f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 273ef20d060SBarry Smith } 2746934998bSLisandro Dalcin 2756934998bSLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 2766934998bSLisandro Dalcin if (opt) { 2776934998bSLisandro Dalcin TSMonitorLGCtx ctx; 2786934998bSLisandro Dalcin PetscInt howoften = 1; 2796934998bSLisandro Dalcin 2806934998bSLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 2816ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2826934998bSLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 2836934998bSLisandro Dalcin } 284201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 285201da799SBarry Smith if (opt) { 286201da799SBarry Smith TSMonitorLGCtx ctx; 287201da799SBarry Smith PetscInt howoften = 1; 288201da799SBarry Smith 2890298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2906ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 291201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 292201da799SBarry Smith } 293201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 294201da799SBarry Smith if (opt) { 295201da799SBarry Smith TSMonitorLGCtx ctx; 296201da799SBarry Smith PetscInt howoften = 1; 297201da799SBarry Smith 2980298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2996ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 300201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 301201da799SBarry Smith } 3028189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 3038189c53fSBarry Smith if (opt) { 3048189c53fSBarry Smith TSMonitorSPEigCtx ctx; 3058189c53fSBarry Smith PetscInt howoften = 1; 3068189c53fSBarry Smith 3070298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 3086934998bSLisandro Dalcin ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 3098189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 3108189c53fSBarry Smith } 311ef20d060SBarry Smith opt = PETSC_FALSE; 3120dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 313a7cc72afSBarry Smith if (opt) { 31483a4ac43SBarry Smith TSMonitorDrawCtx ctx; 31583a4ac43SBarry Smith PetscInt howoften = 1; 316a80ad3e0SBarry Smith 3170298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 3186934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 31983a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 320bdad233fSMatthew Knepley } 321fb1732b5SBarry Smith opt = PETSC_FALSE; 3229110b2e7SHong Zhang ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr); 3239110b2e7SHong Zhang if (opt) { 3249110b2e7SHong Zhang TSMonitorDrawCtx ctx; 3259110b2e7SHong Zhang PetscInt howoften = 1; 3269110b2e7SHong Zhang 3279110b2e7SHong Zhang ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr); 3286934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 3299110b2e7SHong Zhang ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3309110b2e7SHong Zhang } 3319110b2e7SHong Zhang opt = PETSC_FALSE; 3322d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 3332d5ee99bSBarry Smith if (opt) { 3342d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 3352d5ee99bSBarry Smith PetscReal bounds[4]; 3362d5ee99bSBarry Smith PetscInt n = 4; 3372d5ee99bSBarry Smith PetscDraw draw; 3386934998bSLisandro Dalcin PetscDrawAxis axis; 3392d5ee99bSBarry Smith 3402d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 3412d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 3426934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr); 3432d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 3446934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr); 3456934998bSLisandro Dalcin ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 3466934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 3472d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3482d5ee99bSBarry Smith } 3492d5ee99bSBarry Smith opt = PETSC_FALSE; 3500dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 3513a471f94SBarry Smith if (opt) { 35283a4ac43SBarry Smith TSMonitorDrawCtx ctx; 35383a4ac43SBarry Smith PetscInt howoften = 1; 3543a471f94SBarry Smith 3550298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 3566934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 35783a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3583a471f94SBarry Smith } 359fde5950dSBarry Smith 360ed81e22dSJed Brown opt = PETSC_FALSE; 36191b97e58SBarry 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); 362ed81e22dSJed Brown if (flg) { 363ed81e22dSJed Brown const char *ptr,*ptr2; 364ed81e22dSJed Brown char *filetemplate; 365ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 366ed81e22dSJed Brown /* Do some cursory validation of the input. */ 367ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 368ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 369ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 370ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 371ce94432eSBarry 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"); 372ed81e22dSJed Brown if (ptr2) break; 373ed81e22dSJed Brown } 374ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 375ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 376ed81e22dSJed Brown } 377bdad233fSMatthew Knepley 378d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 379d1212d36SBarry Smith if (flg) { 380d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 381d1212d36SBarry Smith int ray = 0; 382d1212d36SBarry Smith DMDADirection ddir; 383d1212d36SBarry Smith DM da; 384d1212d36SBarry Smith PetscMPIInt rank; 385d1212d36SBarry Smith 386ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 387d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 388d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 389ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 390d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 391d1212d36SBarry Smith 392d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 393b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 394d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 395d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 396ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 397d1212d36SBarry Smith if (!rank) { 398d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 399d1212d36SBarry Smith } 40051b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 401d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 402d1212d36SBarry Smith } 40351b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 40451b4a12fSMatthew G. Knepley if (flg) { 40551b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 40651b4a12fSMatthew G. Knepley int ray = 0; 40751b4a12fSMatthew G. Knepley DMDADirection ddir; 40851b4a12fSMatthew G. Knepley DM da; 40951b4a12fSMatthew G. Knepley PetscInt howoften = 1; 410d1212d36SBarry Smith 41151b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 41251b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 41351b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 41451b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 41551b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 4161c3436cfSJed Brown 41751b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 418b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 41951b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 42051b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 42151b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 42251b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 42351b4a12fSMatthew G. Knepley } 424a7a1495cSBarry Smith 425b3d3934dSBarry Smith ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 426b3d3934dSBarry Smith if (opt) { 427b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 428b3d3934dSBarry Smith 429b3d3934dSBarry Smith ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 430b3d3934dSBarry Smith ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 431b3d3934dSBarry Smith } 432b3d3934dSBarry Smith 433847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 434847ff0e1SMatthew G. Knepley ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr); 435847ff0e1SMatthew G. Knepley if (flg) { 436847ff0e1SMatthew G. Knepley DM dm; 437847ff0e1SMatthew G. Knepley DMTS tdm; 438847ff0e1SMatthew G. Knepley 439847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 440847ff0e1SMatthew G. Knepley ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr); 441847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 442847ff0e1SMatthew G. Knepley ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr); 443847ff0e1SMatthew G. Knepley ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 444847ff0e1SMatthew G. Knepley } 445847ff0e1SMatthew G. Knepley 446d763cef2SBarry Smith /* Handle specific TS options */ 447d763cef2SBarry Smith if (ts->ops->setfromoptions) { 4486991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 449d763cef2SBarry Smith } 450fbc52257SHong Zhang 451a7bdc993SLisandro Dalcin /* Handle TSAdapt options */ 452a7bdc993SLisandro Dalcin ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 453a7bdc993SLisandro Dalcin ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr); 454a7bdc993SLisandro Dalcin ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr); 455a7bdc993SLisandro Dalcin 45668bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 4574f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 45868bece0bSHong Zhang ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 45968bece0bSHong Zhang if (tflg) { 46068bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 46168bece0bSHong Zhang } 4624f122a70SLisandro Dalcin tflg = ts->adjoint_solve ? PETSC_TRUE : PETSC_FALSE; 46368bece0bSHong Zhang ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr); 46468bece0bSHong Zhang if (flg) { 46568bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 46668bece0bSHong Zhang ts->adjoint_solve = tflg; 46768bece0bSHong Zhang } 468d763cef2SBarry Smith 469d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 4700633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr); 471fbc52257SHong Zhang ierr = PetscOptionsEnd();CHKERRQ(ierr); 472d763cef2SBarry Smith 4734f122a70SLisandro Dalcin if (ts->trajectory) { 4744f122a70SLisandro Dalcin ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 475d763cef2SBarry Smith } 47668bece0bSHong Zhang 4774f122a70SLisandro Dalcin ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr); 4784f122a70SLisandro Dalcin if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);} 4794f122a70SLisandro Dalcin ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr); 480d763cef2SBarry Smith PetscFunctionReturn(0); 481d763cef2SBarry Smith } 482d763cef2SBarry Smith 483d2daff3dSHong Zhang /*@ 48478fbdcc8SBarry Smith TSGetTrajectory - Gets the trajectory from a TS if it exists 48578fbdcc8SBarry Smith 48678fbdcc8SBarry Smith Collective on TS 48778fbdcc8SBarry Smith 48878fbdcc8SBarry Smith Input Parameters: 48978fbdcc8SBarry Smith . ts - the TS context obtained from TSCreate() 49078fbdcc8SBarry Smith 49178fbdcc8SBarry Smith Output Parameters; 49278fbdcc8SBarry Smith . tr - the TSTrajectory object, if it exists 49378fbdcc8SBarry Smith 49478fbdcc8SBarry Smith Note: This routine should be called after all TS options have been set 49578fbdcc8SBarry Smith 49678fbdcc8SBarry Smith Level: advanced 49778fbdcc8SBarry Smith 49878fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate() 49978fbdcc8SBarry Smith 50078fbdcc8SBarry Smith .keywords: TS, set, checkpoint, 50178fbdcc8SBarry Smith @*/ 50278fbdcc8SBarry Smith PetscErrorCode TSGetTrajectory(TS ts,TSTrajectory *tr) 50378fbdcc8SBarry Smith { 50478fbdcc8SBarry Smith PetscFunctionBegin; 50578fbdcc8SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 50678fbdcc8SBarry Smith *tr = ts->trajectory; 50778fbdcc8SBarry Smith PetscFunctionReturn(0); 50878fbdcc8SBarry Smith } 50978fbdcc8SBarry Smith 51078fbdcc8SBarry Smith /*@ 511bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 512d2daff3dSHong Zhang 513d2daff3dSHong Zhang Collective on TS 514d2daff3dSHong Zhang 515d2daff3dSHong Zhang Input Parameters: 516bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 517bc952696SBarry Smith 51878fbdcc8SBarry Smith Options Database: 51978fbdcc8SBarry Smith + -ts_save_trajectory - saves the trajectory to a file 52078fbdcc8SBarry Smith - -ts_trajectory_type type 52178fbdcc8SBarry Smith 52268bece0bSHong Zhang Note: This routine should be called after all TS options have been set 523d2daff3dSHong Zhang 52478fbdcc8SBarry Smith The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/bin/PetscBinaryIOTrajectory.py and 52578fbdcc8SBarry Smith MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m 52678fbdcc8SBarry Smith 527d2daff3dSHong Zhang Level: intermediate 528d2daff3dSHong Zhang 52978fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectoryType, TSSetTrajectoryType() 530d2daff3dSHong Zhang 531bc952696SBarry Smith .keywords: TS, set, checkpoint, 532d2daff3dSHong Zhang @*/ 533bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 534d2daff3dSHong Zhang { 535bc952696SBarry Smith PetscErrorCode ierr; 536bc952696SBarry Smith 537d2daff3dSHong Zhang PetscFunctionBegin; 538d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 539bc952696SBarry Smith if (!ts->trajectory) { 540bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 541972caf09SHong Zhang ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 542bc952696SBarry Smith } 543d2daff3dSHong Zhang PetscFunctionReturn(0); 544d2daff3dSHong Zhang } 545d2daff3dSHong Zhang 546a7a1495cSBarry Smith /*@ 547a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 548a7a1495cSBarry Smith set with TSSetRHSJacobian(). 549a7a1495cSBarry Smith 550a7a1495cSBarry Smith Collective on TS and Vec 551a7a1495cSBarry Smith 552a7a1495cSBarry Smith Input Parameters: 553316643e7SJed Brown + ts - the TS context 554a7a1495cSBarry Smith . t - current timestep 5550910c330SBarry Smith - U - input vector 556a7a1495cSBarry Smith 557a7a1495cSBarry Smith Output Parameters: 558a7a1495cSBarry Smith + A - Jacobian matrix 559a7a1495cSBarry Smith . B - optional preconditioning matrix 560a7a1495cSBarry Smith - flag - flag indicating matrix structure 561a7a1495cSBarry Smith 562a7a1495cSBarry Smith Notes: 563a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 564a7a1495cSBarry Smith is used internally within the nonlinear solvers. 565a7a1495cSBarry Smith 56694b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 567a7a1495cSBarry Smith flag parameter. 568a7a1495cSBarry Smith 569a7a1495cSBarry Smith Level: developer 570a7a1495cSBarry Smith 571a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 572a7a1495cSBarry Smith 57394b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 574a7a1495cSBarry Smith @*/ 575d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 576a7a1495cSBarry Smith { 577dfbe8321SBarry Smith PetscErrorCode ierr; 578270bf2e7SJed Brown PetscObjectState Ustate; 5796c1e1eecSBarry Smith PetscObjectId Uid; 58024989b8cSPeter Brune DM dm; 581942e3340SBarry Smith DMTS tsdm; 58224989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 58324989b8cSPeter Brune void *ctx; 58424989b8cSPeter Brune TSIJacobian ijacobianfunc; 585b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 586a7a1495cSBarry Smith 587a7a1495cSBarry Smith PetscFunctionBegin; 5880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5890910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5900910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 59124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 592942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 59324989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 5940298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 595b2df71adSDebojyoti Ghosh ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 59659e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 5976c1e1eecSBarry Smith ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr); 5986c1e1eecSBarry Smith if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 5990e4ef248SJed Brown PetscFunctionReturn(0); 600f8ede8e7SJed Brown } 601d90be118SSean Farley 602ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 603d90be118SSean Farley 604e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 60594ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 60694ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 60794ab13aaSBarry Smith if (A != B) { 60894ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 60994ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 610e1244c69SJed Brown } 611e1244c69SJed Brown ts->rhsjacobian.shift = 0; 612e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 613e1244c69SJed Brown } 614e1244c69SJed Brown 61524989b8cSPeter Brune if (rhsjacobianfunc) { 6166cd88445SBarry Smith PetscBool missing; 61794ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 618a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 619d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 620a7a1495cSBarry Smith PetscStackPop; 62194ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 6226cd88445SBarry Smith if (A) { 6236cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 6246cd88445SBarry 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"); 6256cd88445SBarry Smith } 6266cd88445SBarry Smith if (B && B != A) { 6276cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 6286cd88445SBarry 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"); 6296cd88445SBarry Smith } 630ef66eb69SBarry Smith } else { 63194ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 63294ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 633ef66eb69SBarry Smith } 6340e4ef248SJed Brown ts->rhsjacobian.time = t; 6356c1e1eecSBarry Smith ierr = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid); 63659e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 637a7a1495cSBarry Smith PetscFunctionReturn(0); 638a7a1495cSBarry Smith } 639a7a1495cSBarry Smith 640316643e7SJed Brown /*@ 641d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 642d763cef2SBarry Smith 643316643e7SJed Brown Collective on TS and Vec 644316643e7SJed Brown 645316643e7SJed Brown Input Parameters: 646316643e7SJed Brown + ts - the TS context 647316643e7SJed Brown . t - current time 6480910c330SBarry Smith - U - state vector 649316643e7SJed Brown 650316643e7SJed Brown Output Parameter: 651316643e7SJed Brown . y - right hand side 652316643e7SJed Brown 653316643e7SJed Brown Note: 654316643e7SJed Brown Most users should not need to explicitly call this routine, as it 655316643e7SJed Brown is used internally within the nonlinear solvers. 656316643e7SJed Brown 657316643e7SJed Brown Level: developer 658316643e7SJed Brown 659316643e7SJed Brown .keywords: TS, compute 660316643e7SJed Brown 661316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 662316643e7SJed Brown @*/ 6630910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 664d763cef2SBarry Smith { 665dfbe8321SBarry Smith PetscErrorCode ierr; 66624989b8cSPeter Brune TSRHSFunction rhsfunction; 66724989b8cSPeter Brune TSIFunction ifunction; 66824989b8cSPeter Brune void *ctx; 66924989b8cSPeter Brune DM dm; 67024989b8cSPeter Brune 671d763cef2SBarry Smith PetscFunctionBegin; 6720700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6730910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6740700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 67524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 67624989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 6770298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 678d763cef2SBarry Smith 679ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 680d763cef2SBarry Smith 6810910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 68224989b8cSPeter Brune if (rhsfunction) { 683d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 6840910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 685d763cef2SBarry Smith PetscStackPop; 686214bc6a2SJed Brown } else { 687214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 688b2cd27e8SJed Brown } 68944a41b28SSean Farley 6900910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 691d763cef2SBarry Smith PetscFunctionReturn(0); 692d763cef2SBarry Smith } 693d763cef2SBarry Smith 694ef20d060SBarry Smith /*@ 695ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 696ef20d060SBarry Smith 697ef20d060SBarry Smith Collective on TS and Vec 698ef20d060SBarry Smith 699ef20d060SBarry Smith Input Parameters: 700ef20d060SBarry Smith + ts - the TS context 701ef20d060SBarry Smith - t - current time 702ef20d060SBarry Smith 703ef20d060SBarry Smith Output Parameter: 7040910c330SBarry Smith . U - the solution 705ef20d060SBarry Smith 706ef20d060SBarry Smith Note: 707ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 708ef20d060SBarry Smith is used internally within the nonlinear solvers. 709ef20d060SBarry Smith 710ef20d060SBarry Smith Level: developer 711ef20d060SBarry Smith 712ef20d060SBarry Smith .keywords: TS, compute 713ef20d060SBarry Smith 714abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 715ef20d060SBarry Smith @*/ 7160910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 717ef20d060SBarry Smith { 718ef20d060SBarry Smith PetscErrorCode ierr; 719ef20d060SBarry Smith TSSolutionFunction solutionfunction; 720ef20d060SBarry Smith void *ctx; 721ef20d060SBarry Smith DM dm; 722ef20d060SBarry Smith 723ef20d060SBarry Smith PetscFunctionBegin; 724ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7250910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 726ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 727ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 728ef20d060SBarry Smith 729ef20d060SBarry Smith if (solutionfunction) { 7309b7cd975SBarry Smith PetscStackPush("TS user solution function"); 7310910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 732ef20d060SBarry Smith PetscStackPop; 733ef20d060SBarry Smith } 734ef20d060SBarry Smith PetscFunctionReturn(0); 735ef20d060SBarry Smith } 7369b7cd975SBarry Smith /*@ 7379b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 7389b7cd975SBarry Smith 7399b7cd975SBarry Smith Collective on TS and Vec 7409b7cd975SBarry Smith 7419b7cd975SBarry Smith Input Parameters: 7429b7cd975SBarry Smith + ts - the TS context 7439b7cd975SBarry Smith - t - current time 7449b7cd975SBarry Smith 7459b7cd975SBarry Smith Output Parameter: 7469b7cd975SBarry Smith . U - the function value 7479b7cd975SBarry Smith 7489b7cd975SBarry Smith Note: 7499b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 7509b7cd975SBarry Smith is used internally within the nonlinear solvers. 7519b7cd975SBarry Smith 7529b7cd975SBarry Smith Level: developer 7539b7cd975SBarry Smith 7549b7cd975SBarry Smith .keywords: TS, compute 7559b7cd975SBarry Smith 7569b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 7579b7cd975SBarry Smith @*/ 7589b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 7599b7cd975SBarry Smith { 7609b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 7619b7cd975SBarry Smith void *ctx; 7629b7cd975SBarry Smith DM dm; 7639b7cd975SBarry Smith 7649b7cd975SBarry Smith PetscFunctionBegin; 7659b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7669b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7679b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 7689b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 7699b7cd975SBarry Smith 7709b7cd975SBarry Smith if (forcing) { 7719b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 7729b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 7739b7cd975SBarry Smith PetscStackPop; 7749b7cd975SBarry Smith } 7759b7cd975SBarry Smith PetscFunctionReturn(0); 7769b7cd975SBarry Smith } 777ef20d060SBarry Smith 778214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 779214bc6a2SJed Brown { 7802dd45cf8SJed Brown Vec F; 781214bc6a2SJed Brown PetscErrorCode ierr; 782214bc6a2SJed Brown 783214bc6a2SJed Brown PetscFunctionBegin; 7840298fd71SBarry Smith *Frhs = NULL; 7850298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 786214bc6a2SJed Brown if (!ts->Frhs) { 7872dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 788214bc6a2SJed Brown } 789214bc6a2SJed Brown *Frhs = ts->Frhs; 790214bc6a2SJed Brown PetscFunctionReturn(0); 791214bc6a2SJed Brown } 792214bc6a2SJed Brown 793214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 794214bc6a2SJed Brown { 795214bc6a2SJed Brown Mat A,B; 7962dd45cf8SJed Brown PetscErrorCode ierr; 79741a1d4d2SBarry Smith TSIJacobian ijacobian; 798214bc6a2SJed Brown 799214bc6a2SJed Brown PetscFunctionBegin; 800c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 801c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 80241a1d4d2SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr); 803214bc6a2SJed Brown if (Arhs) { 804214bc6a2SJed Brown if (!ts->Arhs) { 80541a1d4d2SBarry Smith if (ijacobian) { 806214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 80741a1d4d2SBarry Smith } else { 80841a1d4d2SBarry Smith ts->Arhs = A; 80941a1d4d2SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 81041a1d4d2SBarry Smith } 8113565c898SBarry Smith } else { 8123565c898SBarry Smith PetscBool flg; 8133565c898SBarry Smith ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr); 8143565c898SBarry Smith /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */ 8153565c898SBarry Smith if (flg && !ijacobian && ts->Arhs == ts->Brhs){ 8163565c898SBarry Smith ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr); 8173565c898SBarry Smith ts->Arhs = A; 8183565c898SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 8193565c898SBarry Smith } 820214bc6a2SJed Brown } 821214bc6a2SJed Brown *Arhs = ts->Arhs; 822214bc6a2SJed Brown } 823214bc6a2SJed Brown if (Brhs) { 824214bc6a2SJed Brown if (!ts->Brhs) { 825bdb70873SJed Brown if (A != B) { 82641a1d4d2SBarry Smith if (ijacobian) { 827214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 828bdb70873SJed Brown } else { 82941a1d4d2SBarry Smith ts->Brhs = B; 83041a1d4d2SBarry Smith ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 83141a1d4d2SBarry Smith } 83241a1d4d2SBarry Smith } else { 833bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 83451699248SLisandro Dalcin ts->Brhs = ts->Arhs; 835bdb70873SJed Brown } 836214bc6a2SJed Brown } 837214bc6a2SJed Brown *Brhs = ts->Brhs; 838214bc6a2SJed Brown } 839214bc6a2SJed Brown PetscFunctionReturn(0); 840214bc6a2SJed Brown } 841214bc6a2SJed Brown 842316643e7SJed Brown /*@ 8430910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 844316643e7SJed Brown 845316643e7SJed Brown Collective on TS and Vec 846316643e7SJed Brown 847316643e7SJed Brown Input Parameters: 848316643e7SJed Brown + ts - the TS context 849316643e7SJed Brown . t - current time 8500910c330SBarry Smith . U - state vector 8510910c330SBarry Smith . Udot - time derivative of state vector 852214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 853316643e7SJed Brown 854316643e7SJed Brown Output Parameter: 855316643e7SJed Brown . Y - right hand side 856316643e7SJed Brown 857316643e7SJed Brown Note: 858316643e7SJed Brown Most users should not need to explicitly call this routine, as it 859316643e7SJed Brown is used internally within the nonlinear solvers. 860316643e7SJed Brown 861316643e7SJed Brown If the user did did not write their equations in implicit form, this 862316643e7SJed Brown function recasts them in implicit form. 863316643e7SJed Brown 864316643e7SJed Brown Level: developer 865316643e7SJed Brown 866316643e7SJed Brown .keywords: TS, compute 867316643e7SJed Brown 868316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 869316643e7SJed Brown @*/ 8700910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 871316643e7SJed Brown { 872316643e7SJed Brown PetscErrorCode ierr; 87324989b8cSPeter Brune TSIFunction ifunction; 87424989b8cSPeter Brune TSRHSFunction rhsfunction; 87524989b8cSPeter Brune void *ctx; 87624989b8cSPeter Brune DM dm; 877316643e7SJed Brown 878316643e7SJed Brown PetscFunctionBegin; 8790700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8800910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8810910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 8820700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 883316643e7SJed Brown 88424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 88524989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 8860298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 88724989b8cSPeter Brune 888ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 889d90be118SSean Farley 8900910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 89124989b8cSPeter Brune if (ifunction) { 892316643e7SJed Brown PetscStackPush("TS user implicit function"); 8930910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 894316643e7SJed Brown PetscStackPop; 895214bc6a2SJed Brown } 896214bc6a2SJed Brown if (imex) { 89724989b8cSPeter Brune if (!ifunction) { 8980910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 8992dd45cf8SJed Brown } 90024989b8cSPeter Brune } else if (rhsfunction) { 90124989b8cSPeter Brune if (ifunction) { 902214bc6a2SJed Brown Vec Frhs; 903214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 9040910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 905214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 9062dd45cf8SJed Brown } else { 9070910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 9080910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 909316643e7SJed Brown } 9104a6899ffSJed Brown } 9110910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 912316643e7SJed Brown PetscFunctionReturn(0); 913316643e7SJed Brown } 914316643e7SJed Brown 915316643e7SJed Brown /*@ 916316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 917316643e7SJed Brown 918316643e7SJed Brown Collective on TS and Vec 919316643e7SJed Brown 920316643e7SJed Brown Input 921316643e7SJed Brown Input Parameters: 922316643e7SJed Brown + ts - the TS context 923316643e7SJed Brown . t - current timestep 9240910c330SBarry Smith . U - state vector 9250910c330SBarry Smith . Udot - time derivative of state vector 926214bc6a2SJed Brown . shift - shift to apply, see note below 927214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 928316643e7SJed Brown 929316643e7SJed Brown Output Parameters: 930316643e7SJed Brown + A - Jacobian matrix 9313565c898SBarry Smith - B - matrix from which the preconditioner is constructed; often the same as A 932316643e7SJed Brown 933316643e7SJed Brown Notes: 9340910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 935316643e7SJed Brown 9360910c330SBarry Smith dF/dU + shift*dF/dUdot 937316643e7SJed Brown 938316643e7SJed Brown Most users should not need to explicitly call this routine, as it 939316643e7SJed Brown is used internally within the nonlinear solvers. 940316643e7SJed Brown 941316643e7SJed Brown Level: developer 942316643e7SJed Brown 943316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 944316643e7SJed Brown 945316643e7SJed Brown .seealso: TSSetIJacobian() 94663495f91SJed Brown @*/ 947d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 948316643e7SJed Brown { 949316643e7SJed Brown PetscErrorCode ierr; 95024989b8cSPeter Brune TSIJacobian ijacobian; 95124989b8cSPeter Brune TSRHSJacobian rhsjacobian; 95224989b8cSPeter Brune DM dm; 95324989b8cSPeter Brune void *ctx; 954316643e7SJed Brown 955316643e7SJed Brown PetscFunctionBegin; 9560700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9570910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 9580910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 959316643e7SJed Brown PetscValidPointer(A,6); 96094ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 961316643e7SJed Brown PetscValidPointer(B,7); 96294ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 96324989b8cSPeter Brune 96424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 96524989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 9660298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 96724989b8cSPeter Brune 968ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 969316643e7SJed Brown 97094ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 97124989b8cSPeter Brune if (ijacobian) { 9726cd88445SBarry Smith PetscBool missing; 973316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 974d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 975316643e7SJed Brown PetscStackPop; 9766cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 9776cd88445SBarry 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"); 9783565c898SBarry Smith if (B != A) { 9796cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 9806cd88445SBarry 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"); 9816cd88445SBarry Smith } 9824a6899ffSJed Brown } 983214bc6a2SJed Brown if (imex) { 984b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 9854c26be97Sstefano_zampini PetscBool assembled; 98694ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 9874c26be97Sstefano_zampini ierr = MatAssembled(A,&assembled);CHKERRQ(ierr); 9884c26be97Sstefano_zampini if (!assembled) { 9894c26be97Sstefano_zampini ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9904c26be97Sstefano_zampini ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9914c26be97Sstefano_zampini } 99294ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 99394ab13aaSBarry Smith if (A != B) { 99494ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 9954c26be97Sstefano_zampini ierr = MatAssembled(B,&assembled);CHKERRQ(ierr); 9964c26be97Sstefano_zampini if (!assembled) { 9974c26be97Sstefano_zampini ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9984c26be97Sstefano_zampini ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9994c26be97Sstefano_zampini } 100094ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 1001214bc6a2SJed Brown } 1002214bc6a2SJed Brown } 1003214bc6a2SJed Brown } else { 1004e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 1005e1244c69SJed Brown if (rhsjacobian) { 1006214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 1007d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 1008e1244c69SJed Brown } 100994ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 10103565c898SBarry Smith PetscBool flg; 1011e1244c69SJed Brown ts->rhsjacobian.scale = -1; 1012e1244c69SJed Brown ts->rhsjacobian.shift = shift; 10133565c898SBarry Smith ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr); 10143565c898SBarry Smith /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */ 10153565c898SBarry Smith if (!flg) { 101694ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 101794ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 10183565c898SBarry Smith } 101994ab13aaSBarry Smith if (A != B) { 102094ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 102194ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 1022316643e7SJed Brown } 1023e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 1024e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 1025e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 102694ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 102794ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 102894ab13aaSBarry Smith if (A != B) { 102994ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 103094ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 1031214bc6a2SJed Brown } 1032316643e7SJed Brown } 103394ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 103494ab13aaSBarry Smith if (A != B) { 103594ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 1036316643e7SJed Brown } 1037316643e7SJed Brown } 1038316643e7SJed Brown } 103994ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 1040316643e7SJed Brown PetscFunctionReturn(0); 1041316643e7SJed Brown } 1042316643e7SJed Brown 1043d763cef2SBarry Smith /*@C 1044d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 1045b5abc632SBarry Smith where U_t = G(t,u). 1046d763cef2SBarry Smith 10473f9fe445SBarry Smith Logically Collective on TS 1048d763cef2SBarry Smith 1049d763cef2SBarry Smith Input Parameters: 1050d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 10510298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 1052d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 1053d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 10540298fd71SBarry Smith function evaluation routine (may be NULL) 1055d763cef2SBarry Smith 1056d763cef2SBarry Smith Calling sequence of func: 105787828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 1058d763cef2SBarry Smith 1059d763cef2SBarry Smith + t - current timestep 1060d763cef2SBarry Smith . u - input vector 1061d763cef2SBarry Smith . F - function vector 1062d763cef2SBarry Smith - ctx - [optional] user-defined function context 1063d763cef2SBarry Smith 1064d763cef2SBarry Smith Level: beginner 1065d763cef2SBarry Smith 10662bbac0d3SBarry Smith Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 10672bbac0d3SBarry Smith 1068d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1069d763cef2SBarry Smith 1070ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 1071d763cef2SBarry Smith @*/ 1072089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 1073d763cef2SBarry Smith { 1074089b2837SJed Brown PetscErrorCode ierr; 1075089b2837SJed Brown SNES snes; 10760298fd71SBarry Smith Vec ralloc = NULL; 107724989b8cSPeter Brune DM dm; 1078d763cef2SBarry Smith 1079089b2837SJed Brown PetscFunctionBegin; 10800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1081ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 108224989b8cSPeter Brune 108324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 108424989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 1085089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1086e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 1087e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 1088e856ceecSJed Brown r = ralloc; 1089e856ceecSJed Brown } 1090089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 1091e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1092d763cef2SBarry Smith PetscFunctionReturn(0); 1093d763cef2SBarry Smith } 1094d763cef2SBarry Smith 1095ef20d060SBarry Smith /*@C 1096abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1097ef20d060SBarry Smith 1098ef20d060SBarry Smith Logically Collective on TS 1099ef20d060SBarry Smith 1100ef20d060SBarry Smith Input Parameters: 1101ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1102ef20d060SBarry Smith . f - routine for evaluating the solution 1103ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 11040298fd71SBarry Smith function evaluation routine (may be NULL) 1105ef20d060SBarry Smith 1106ef20d060SBarry Smith Calling sequence of func: 1107ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 1108ef20d060SBarry Smith 1109ef20d060SBarry Smith + t - current timestep 1110ef20d060SBarry Smith . u - output vector 1111ef20d060SBarry Smith - ctx - [optional] user-defined function context 1112ef20d060SBarry Smith 1113abd5a294SJed Brown Notes: 1114abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1115abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1116abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1117abd5a294SJed Brown 11184f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1119abd5a294SJed Brown 1120ef20d060SBarry Smith Level: beginner 1121ef20d060SBarry Smith 1122ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1123ef20d060SBarry Smith 11249b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 1125ef20d060SBarry Smith @*/ 1126ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1127ef20d060SBarry Smith { 1128ef20d060SBarry Smith PetscErrorCode ierr; 1129ef20d060SBarry Smith DM dm; 1130ef20d060SBarry Smith 1131ef20d060SBarry Smith PetscFunctionBegin; 1132ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1133ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1134ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 1135ef20d060SBarry Smith PetscFunctionReturn(0); 1136ef20d060SBarry Smith } 1137ef20d060SBarry Smith 11389b7cd975SBarry Smith /*@C 11399b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 11409b7cd975SBarry Smith 11419b7cd975SBarry Smith Logically Collective on TS 11429b7cd975SBarry Smith 11439b7cd975SBarry Smith Input Parameters: 11449b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 1145e162b725SBarry Smith . func - routine for evaluating the forcing function 11469b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 11470298fd71SBarry Smith function evaluation routine (may be NULL) 11489b7cd975SBarry Smith 11499b7cd975SBarry Smith Calling sequence of func: 1150e162b725SBarry Smith $ func (TS ts,PetscReal t,Vec f,void *ctx); 11519b7cd975SBarry Smith 11529b7cd975SBarry Smith + t - current timestep 1153e162b725SBarry Smith . f - output vector 11549b7cd975SBarry Smith - ctx - [optional] user-defined function context 11559b7cd975SBarry Smith 11569b7cd975SBarry Smith Notes: 11579b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1158e162b725SBarry Smith create closed-form solutions with a non-physical forcing term. It allows you to use the Method of Manufactored Solution without directly editing the 1159e162b725SBarry Smith definition of the problem you are solving and hence possibly introducing bugs. 1160e162b725SBarry Smith 1161e162b725SBarry Smith This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0 1162e162b725SBarry Smith 1163e162b725SBarry Smith This forcing function does not depend on the solution to the equations, it can only depend on spatial location, time, and possibly parameters, the 1164e162b725SBarry Smith parameters can be passed in the ctx variable. 11659b7cd975SBarry Smith 11669b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 11679b7cd975SBarry Smith 11689b7cd975SBarry Smith Level: beginner 11699b7cd975SBarry Smith 11709b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 11719b7cd975SBarry Smith 11729b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 11739b7cd975SBarry Smith @*/ 1174e162b725SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx) 11759b7cd975SBarry Smith { 11769b7cd975SBarry Smith PetscErrorCode ierr; 11779b7cd975SBarry Smith DM dm; 11789b7cd975SBarry Smith 11799b7cd975SBarry Smith PetscFunctionBegin; 11809b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11819b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1182e162b725SBarry Smith ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr); 11839b7cd975SBarry Smith PetscFunctionReturn(0); 11849b7cd975SBarry Smith } 11859b7cd975SBarry Smith 1186d763cef2SBarry Smith /*@C 1187f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1188b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1189d763cef2SBarry Smith 11903f9fe445SBarry Smith Logically Collective on TS 1191d763cef2SBarry Smith 1192d763cef2SBarry Smith Input Parameters: 1193d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1194e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1195e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1196d763cef2SBarry Smith . f - the Jacobian evaluation routine 1197d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 11980298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1199d763cef2SBarry Smith 1200f7ab8db6SBarry Smith Calling sequence of f: 1201ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1202d763cef2SBarry Smith 1203d763cef2SBarry Smith + t - current timestep 1204d763cef2SBarry Smith . u - input vector 1205e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1206e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1207d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1208d763cef2SBarry Smith 12096cd88445SBarry Smith Notes: 12106cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 12116cd88445SBarry Smith 12126cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1213ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1214d763cef2SBarry Smith 1215d763cef2SBarry Smith Level: beginner 1216d763cef2SBarry Smith 1217d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1218d763cef2SBarry Smith 1219ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1220d763cef2SBarry Smith 1221d763cef2SBarry Smith @*/ 1222e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1223d763cef2SBarry Smith { 1224277b19d0SLisandro Dalcin PetscErrorCode ierr; 1225089b2837SJed Brown SNES snes; 122624989b8cSPeter Brune DM dm; 122724989b8cSPeter Brune TSIJacobian ijacobian; 1228277b19d0SLisandro Dalcin 1229d763cef2SBarry Smith PetscFunctionBegin; 12300700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1231e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1232e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1233e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1234e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1235d763cef2SBarry Smith 123624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 123724989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1238e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1239e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1240e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1241e1244c69SJed Brown } 12420298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1243089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12445f659677SPeter Brune if (!ijacobian) { 1245e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 12460e4ef248SJed Brown } 1247e5d3d808SBarry Smith if (Amat) { 1248e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 12490e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1250e5d3d808SBarry Smith ts->Arhs = Amat; 12510e4ef248SJed Brown } 1252e5d3d808SBarry Smith if (Pmat) { 1253e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 12540e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1255e5d3d808SBarry Smith ts->Brhs = Pmat; 12560e4ef248SJed Brown } 1257d763cef2SBarry Smith PetscFunctionReturn(0); 1258d763cef2SBarry Smith } 1259d763cef2SBarry Smith 1260316643e7SJed Brown 1261316643e7SJed Brown /*@C 1262b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1263316643e7SJed Brown 12643f9fe445SBarry Smith Logically Collective on TS 1265316643e7SJed Brown 1266316643e7SJed Brown Input Parameters: 1267316643e7SJed Brown + ts - the TS context obtained from TSCreate() 12680298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1269316643e7SJed Brown . f - the function evaluation routine 12700298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1271316643e7SJed Brown 1272316643e7SJed Brown Calling sequence of f: 1273316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1274316643e7SJed Brown 1275316643e7SJed Brown + t - time at step/stage being solved 1276316643e7SJed Brown . u - state vector 1277316643e7SJed Brown . u_t - time derivative of state vector 1278316643e7SJed Brown . F - function vector 1279316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1280316643e7SJed Brown 1281316643e7SJed Brown Important: 12822bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1283316643e7SJed Brown 1284316643e7SJed Brown Level: beginner 1285316643e7SJed Brown 1286316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1287316643e7SJed Brown 1288d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1289316643e7SJed Brown @*/ 129051699248SLisandro Dalcin PetscErrorCode TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx) 1291316643e7SJed Brown { 1292089b2837SJed Brown PetscErrorCode ierr; 1293089b2837SJed Brown SNES snes; 129451699248SLisandro Dalcin Vec ralloc = NULL; 129524989b8cSPeter Brune DM dm; 1296316643e7SJed Brown 1297316643e7SJed Brown PetscFunctionBegin; 12980700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 129951699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 130024989b8cSPeter Brune 130124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 130224989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 130324989b8cSPeter Brune 1304089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 130551699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 130651699248SLisandro Dalcin ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 130751699248SLisandro Dalcin r = ralloc; 1308e856ceecSJed Brown } 130951699248SLisandro Dalcin ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 131051699248SLisandro Dalcin ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1311089b2837SJed Brown PetscFunctionReturn(0); 1312089b2837SJed Brown } 1313089b2837SJed Brown 1314089b2837SJed Brown /*@C 1315089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1316089b2837SJed Brown 1317089b2837SJed Brown Not Collective 1318089b2837SJed Brown 1319089b2837SJed Brown Input Parameter: 1320089b2837SJed Brown . ts - the TS context 1321089b2837SJed Brown 1322089b2837SJed Brown Output Parameter: 13230298fd71SBarry Smith + r - vector to hold residual (or NULL) 13240298fd71SBarry Smith . func - the function to compute residual (or NULL) 13250298fd71SBarry Smith - ctx - the function context (or NULL) 1326089b2837SJed Brown 1327089b2837SJed Brown Level: advanced 1328089b2837SJed Brown 1329089b2837SJed Brown .keywords: TS, nonlinear, get, function 1330089b2837SJed Brown 1331089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1332089b2837SJed Brown @*/ 1333089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1334089b2837SJed Brown { 1335089b2837SJed Brown PetscErrorCode ierr; 1336089b2837SJed Brown SNES snes; 133724989b8cSPeter Brune DM dm; 1338089b2837SJed Brown 1339089b2837SJed Brown PetscFunctionBegin; 1340089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1341089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13420298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 134324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 134424989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1345089b2837SJed Brown PetscFunctionReturn(0); 1346089b2837SJed Brown } 1347089b2837SJed Brown 1348089b2837SJed Brown /*@C 1349089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1350089b2837SJed Brown 1351089b2837SJed Brown Not Collective 1352089b2837SJed Brown 1353089b2837SJed Brown Input Parameter: 1354089b2837SJed Brown . ts - the TS context 1355089b2837SJed Brown 1356089b2837SJed Brown Output Parameter: 13570298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 13580298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 13590298fd71SBarry Smith - ctx - the function context (or NULL) 1360089b2837SJed Brown 1361089b2837SJed Brown Level: advanced 1362089b2837SJed Brown 1363089b2837SJed Brown .keywords: TS, nonlinear, get, function 1364089b2837SJed Brown 13652bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1366089b2837SJed Brown @*/ 1367089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1368089b2837SJed Brown { 1369089b2837SJed Brown PetscErrorCode ierr; 1370089b2837SJed Brown SNES snes; 137124989b8cSPeter Brune DM dm; 1372089b2837SJed Brown 1373089b2837SJed Brown PetscFunctionBegin; 1374089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1375089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13760298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 137724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 137824989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1379316643e7SJed Brown PetscFunctionReturn(0); 1380316643e7SJed Brown } 1381316643e7SJed Brown 1382316643e7SJed Brown /*@C 1383a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1384ae8867d6SBarry Smith provided with TSSetIFunction(). 1385316643e7SJed Brown 13863f9fe445SBarry Smith Logically Collective on TS 1387316643e7SJed Brown 1388316643e7SJed Brown Input Parameters: 1389316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1390e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1391e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1392316643e7SJed Brown . f - the Jacobian evaluation routine 13930298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1394316643e7SJed Brown 1395316643e7SJed Brown Calling sequence of f: 1396ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1397316643e7SJed Brown 1398316643e7SJed Brown + t - time at step/stage being solved 13991b4a444bSJed Brown . U - state vector 14001b4a444bSJed Brown . U_t - time derivative of state vector 1401316643e7SJed Brown . a - shift 1402e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1403e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1404316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1405316643e7SJed Brown 1406316643e7SJed Brown Notes: 1407e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1408316643e7SJed Brown 1409895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1410895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1411895c21f2SBarry Smith 1412a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1413b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1414a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1415a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1416a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1417a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1418a4f0a591SBarry Smith 14196cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 14206cd88445SBarry Smith 14216cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1422ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1423ca5f011dSBarry Smith 1424316643e7SJed Brown Level: beginner 1425316643e7SJed Brown 1426316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1427316643e7SJed Brown 1428ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1429316643e7SJed Brown 1430316643e7SJed Brown @*/ 1431e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1432316643e7SJed Brown { 1433316643e7SJed Brown PetscErrorCode ierr; 1434089b2837SJed Brown SNES snes; 143524989b8cSPeter Brune DM dm; 1436316643e7SJed Brown 1437316643e7SJed Brown PetscFunctionBegin; 14380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1439e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1440e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1441e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1442e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 144324989b8cSPeter Brune 144424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 144524989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 144624989b8cSPeter Brune 1447089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1448e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1449316643e7SJed Brown PetscFunctionReturn(0); 1450316643e7SJed Brown } 1451316643e7SJed Brown 1452e1244c69SJed Brown /*@ 1453e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1454e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1455e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1456e1244c69SJed Brown not been changed by the TS. 1457e1244c69SJed Brown 1458e1244c69SJed Brown Logically Collective 1459e1244c69SJed Brown 1460e1244c69SJed Brown Input Arguments: 1461e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1462e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1463e1244c69SJed Brown 1464e1244c69SJed Brown Level: intermediate 1465e1244c69SJed Brown 1466e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1467e1244c69SJed Brown @*/ 1468e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1469e1244c69SJed Brown { 1470e1244c69SJed Brown PetscFunctionBegin; 1471e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1472e1244c69SJed Brown PetscFunctionReturn(0); 1473e1244c69SJed Brown } 1474e1244c69SJed Brown 1475efe9872eSLisandro Dalcin /*@C 1476efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1477efe9872eSLisandro Dalcin 1478efe9872eSLisandro Dalcin Logically Collective on TS 1479efe9872eSLisandro Dalcin 1480efe9872eSLisandro Dalcin Input Parameters: 1481efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1482efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1483efe9872eSLisandro Dalcin . fun - the function evaluation routine 1484efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1485efe9872eSLisandro Dalcin 1486efe9872eSLisandro Dalcin Calling sequence of fun: 1487efe9872eSLisandro Dalcin $ fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1488efe9872eSLisandro Dalcin 1489efe9872eSLisandro Dalcin + t - time at step/stage being solved 1490efe9872eSLisandro Dalcin . U - state vector 1491efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1492efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1493efe9872eSLisandro Dalcin . F - function vector 1494efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1495efe9872eSLisandro Dalcin 1496efe9872eSLisandro Dalcin Level: beginner 1497efe9872eSLisandro Dalcin 1498efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function 1499efe9872eSLisandro Dalcin 1500efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1501efe9872eSLisandro Dalcin @*/ 1502efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx) 1503efe9872eSLisandro Dalcin { 1504efe9872eSLisandro Dalcin DM dm; 1505efe9872eSLisandro Dalcin PetscErrorCode ierr; 1506efe9872eSLisandro Dalcin 1507efe9872eSLisandro Dalcin PetscFunctionBegin; 1508efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1509efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2); 1510efe9872eSLisandro Dalcin ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr); 1511efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1512efe9872eSLisandro Dalcin ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1513efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1514efe9872eSLisandro Dalcin } 1515efe9872eSLisandro Dalcin 1516efe9872eSLisandro Dalcin /*@C 1517efe9872eSLisandro Dalcin TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1518efe9872eSLisandro Dalcin 1519efe9872eSLisandro Dalcin Not Collective 1520efe9872eSLisandro Dalcin 1521efe9872eSLisandro Dalcin Input Parameter: 1522efe9872eSLisandro Dalcin . ts - the TS context 1523efe9872eSLisandro Dalcin 1524efe9872eSLisandro Dalcin Output Parameter: 1525efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1526efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1527efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1528efe9872eSLisandro Dalcin 1529efe9872eSLisandro Dalcin Level: advanced 1530efe9872eSLisandro Dalcin 1531efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function 1532efe9872eSLisandro Dalcin 1533efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction() 1534efe9872eSLisandro Dalcin @*/ 1535efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx) 1536efe9872eSLisandro Dalcin { 1537efe9872eSLisandro Dalcin PetscErrorCode ierr; 1538efe9872eSLisandro Dalcin SNES snes; 1539efe9872eSLisandro Dalcin DM dm; 1540efe9872eSLisandro Dalcin 1541efe9872eSLisandro Dalcin PetscFunctionBegin; 1542efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1543efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1544efe9872eSLisandro Dalcin ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 1545efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1546efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1547efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1548efe9872eSLisandro Dalcin } 1549efe9872eSLisandro Dalcin 1550efe9872eSLisandro Dalcin /*@C 1551bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1552efe9872eSLisandro Dalcin where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function(). 1553efe9872eSLisandro Dalcin 1554efe9872eSLisandro Dalcin Logically Collective on TS 1555efe9872eSLisandro Dalcin 1556efe9872eSLisandro Dalcin Input Parameters: 1557efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1558efe9872eSLisandro Dalcin . J - Jacobian matrix 1559efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1560efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1561efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1562efe9872eSLisandro Dalcin 1563efe9872eSLisandro Dalcin Calling sequence of jac: 1564bc77d74cSLisandro Dalcin $ jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx); 1565efe9872eSLisandro Dalcin 1566efe9872eSLisandro Dalcin + t - time at step/stage being solved 1567efe9872eSLisandro Dalcin . U - state vector 1568efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1569efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1570efe9872eSLisandro Dalcin . v - shift for U_t 1571efe9872eSLisandro Dalcin . a - shift for U_tt 1572efe9872eSLisandro 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 1573efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1574efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1575efe9872eSLisandro Dalcin 1576efe9872eSLisandro Dalcin Notes: 1577efe9872eSLisandro Dalcin The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve. 1578efe9872eSLisandro Dalcin 1579efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1580efe9872eSLisandro 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. 1581efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1582bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1583efe9872eSLisandro Dalcin 1584efe9872eSLisandro Dalcin Level: beginner 1585efe9872eSLisandro Dalcin 1586efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian 1587efe9872eSLisandro Dalcin 1588efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1589efe9872eSLisandro Dalcin @*/ 1590efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx) 1591efe9872eSLisandro Dalcin { 1592efe9872eSLisandro Dalcin DM dm; 1593efe9872eSLisandro Dalcin PetscErrorCode ierr; 1594efe9872eSLisandro Dalcin 1595efe9872eSLisandro Dalcin PetscFunctionBegin; 1596efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1597efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2); 1598efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3); 1599efe9872eSLisandro Dalcin ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr); 1600efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1601efe9872eSLisandro Dalcin ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1602efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1603efe9872eSLisandro Dalcin } 1604efe9872eSLisandro Dalcin 1605efe9872eSLisandro Dalcin /*@C 1606efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1607efe9872eSLisandro Dalcin 1608efe9872eSLisandro Dalcin Not Collective, but parallel objects are returned if TS is parallel 1609efe9872eSLisandro Dalcin 1610efe9872eSLisandro Dalcin Input Parameter: 1611efe9872eSLisandro Dalcin . ts - The TS context obtained from TSCreate() 1612efe9872eSLisandro Dalcin 1613efe9872eSLisandro Dalcin Output Parameters: 1614efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1615efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1616efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1617efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1618efe9872eSLisandro Dalcin 1619efe9872eSLisandro Dalcin Notes: You can pass in NULL for any return argument you do not need. 1620efe9872eSLisandro Dalcin 1621efe9872eSLisandro Dalcin Level: advanced 1622efe9872eSLisandro Dalcin 1623efe9872eSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 1624efe9872eSLisandro Dalcin 1625efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian 1626efe9872eSLisandro Dalcin @*/ 1627efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx) 1628efe9872eSLisandro Dalcin { 1629efe9872eSLisandro Dalcin PetscErrorCode ierr; 1630efe9872eSLisandro Dalcin SNES snes; 1631efe9872eSLisandro Dalcin DM dm; 1632efe9872eSLisandro Dalcin 1633efe9872eSLisandro Dalcin PetscFunctionBegin; 1634efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1635efe9872eSLisandro Dalcin ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 1636efe9872eSLisandro Dalcin ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr); 1637efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1638efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1639efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1640efe9872eSLisandro Dalcin } 1641efe9872eSLisandro Dalcin 1642efe9872eSLisandro Dalcin /*@ 1643efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1644efe9872eSLisandro Dalcin 1645efe9872eSLisandro Dalcin Collective on TS and Vec 1646efe9872eSLisandro Dalcin 1647efe9872eSLisandro Dalcin Input Parameters: 1648efe9872eSLisandro Dalcin + ts - the TS context 1649efe9872eSLisandro Dalcin . t - current time 1650efe9872eSLisandro Dalcin . U - state vector 1651efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1652efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1653efe9872eSLisandro Dalcin 1654efe9872eSLisandro Dalcin Output Parameter: 1655efe9872eSLisandro Dalcin . F - the residual vector 1656efe9872eSLisandro Dalcin 1657efe9872eSLisandro Dalcin Note: 1658efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1659efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1660efe9872eSLisandro Dalcin 1661efe9872eSLisandro Dalcin Level: developer 1662efe9872eSLisandro Dalcin 1663efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector 1664efe9872eSLisandro Dalcin 1665efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1666efe9872eSLisandro Dalcin @*/ 1667efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F) 1668efe9872eSLisandro Dalcin { 1669efe9872eSLisandro Dalcin DM dm; 1670efe9872eSLisandro Dalcin TSI2Function I2Function; 1671efe9872eSLisandro Dalcin void *ctx; 1672efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1673efe9872eSLisandro Dalcin PetscErrorCode ierr; 1674efe9872eSLisandro Dalcin 1675efe9872eSLisandro Dalcin PetscFunctionBegin; 1676efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1677efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1678efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1679efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1680efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F,VEC_CLASSID,6); 1681efe9872eSLisandro Dalcin 1682efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1683efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr); 1684efe9872eSLisandro Dalcin ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 1685efe9872eSLisandro Dalcin 1686efe9872eSLisandro Dalcin if (!I2Function) { 1687efe9872eSLisandro Dalcin ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr); 1688efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1689efe9872eSLisandro Dalcin } 1690efe9872eSLisandro Dalcin 1691efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1692efe9872eSLisandro Dalcin 1693efe9872eSLisandro Dalcin PetscStackPush("TS user implicit function"); 1694efe9872eSLisandro Dalcin ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr); 1695efe9872eSLisandro Dalcin PetscStackPop; 1696efe9872eSLisandro Dalcin 1697efe9872eSLisandro Dalcin if (rhsfunction) { 1698efe9872eSLisandro Dalcin Vec Frhs; 1699efe9872eSLisandro Dalcin ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 1700efe9872eSLisandro Dalcin ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 1701efe9872eSLisandro Dalcin ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr); 1702efe9872eSLisandro Dalcin } 1703efe9872eSLisandro Dalcin 1704efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1705efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1706efe9872eSLisandro Dalcin } 1707efe9872eSLisandro Dalcin 1708efe9872eSLisandro Dalcin /*@ 1709efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1710efe9872eSLisandro Dalcin 1711efe9872eSLisandro Dalcin Collective on TS and Vec 1712efe9872eSLisandro Dalcin 1713efe9872eSLisandro Dalcin Input Parameters: 1714efe9872eSLisandro Dalcin + ts - the TS context 1715efe9872eSLisandro Dalcin . t - current timestep 1716efe9872eSLisandro Dalcin . U - state vector 1717efe9872eSLisandro Dalcin . V - time derivative of state vector 1718efe9872eSLisandro Dalcin . A - second time derivative of state vector 1719efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1720efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1721efe9872eSLisandro Dalcin 1722efe9872eSLisandro Dalcin Output Parameters: 1723efe9872eSLisandro Dalcin + J - Jacobian matrix 1724efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1725efe9872eSLisandro Dalcin 1726efe9872eSLisandro Dalcin Notes: 1727efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1728efe9872eSLisandro Dalcin 1729efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1730efe9872eSLisandro Dalcin 1731efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1732efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1733efe9872eSLisandro Dalcin 1734efe9872eSLisandro Dalcin Level: developer 1735efe9872eSLisandro Dalcin 1736efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix 1737efe9872eSLisandro Dalcin 1738efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1739efe9872eSLisandro Dalcin @*/ 1740efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P) 1741efe9872eSLisandro Dalcin { 1742efe9872eSLisandro Dalcin DM dm; 1743efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1744efe9872eSLisandro Dalcin void *ctx; 1745efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1746efe9872eSLisandro Dalcin PetscErrorCode ierr; 1747efe9872eSLisandro Dalcin 1748efe9872eSLisandro Dalcin PetscFunctionBegin; 1749efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1750efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1751efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1752efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1753efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J,MAT_CLASSID,8); 1754efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P,MAT_CLASSID,9); 1755efe9872eSLisandro Dalcin 1756efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1757efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr); 1758efe9872eSLisandro Dalcin ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 1759efe9872eSLisandro Dalcin 1760efe9872eSLisandro Dalcin if (!I2Jacobian) { 1761efe9872eSLisandro Dalcin ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr); 1762efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1763efe9872eSLisandro Dalcin } 1764efe9872eSLisandro Dalcin 1765efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1766efe9872eSLisandro Dalcin 1767efe9872eSLisandro Dalcin PetscStackPush("TS user implicit Jacobian"); 1768efe9872eSLisandro Dalcin ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr); 1769efe9872eSLisandro Dalcin PetscStackPop; 1770efe9872eSLisandro Dalcin 1771efe9872eSLisandro Dalcin if (rhsjacobian) { 1772efe9872eSLisandro Dalcin Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 1773efe9872eSLisandro Dalcin ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr); 1774efe9872eSLisandro Dalcin ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr); 1775efe9872eSLisandro Dalcin ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr); 1776efe9872eSLisandro Dalcin if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);} 1777efe9872eSLisandro Dalcin } 1778efe9872eSLisandro Dalcin 1779efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1780efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1781efe9872eSLisandro Dalcin } 1782efe9872eSLisandro Dalcin 1783efe9872eSLisandro Dalcin /*@ 1784efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1785efe9872eSLisandro Dalcin for use by the TS routines handling second order equations. 1786efe9872eSLisandro Dalcin 1787efe9872eSLisandro Dalcin Logically Collective on TS and Vec 1788efe9872eSLisandro Dalcin 1789efe9872eSLisandro Dalcin Input Parameters: 1790efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1791efe9872eSLisandro Dalcin . u - the solution vector 1792efe9872eSLisandro Dalcin - v - the time derivative vector 1793efe9872eSLisandro Dalcin 1794efe9872eSLisandro Dalcin Level: beginner 1795efe9872eSLisandro Dalcin 1796efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions 1797efe9872eSLisandro Dalcin @*/ 1798efe9872eSLisandro Dalcin PetscErrorCode TS2SetSolution(TS ts,Vec u,Vec v) 1799efe9872eSLisandro Dalcin { 1800efe9872eSLisandro Dalcin PetscErrorCode ierr; 1801efe9872eSLisandro Dalcin 1802efe9872eSLisandro Dalcin PetscFunctionBegin; 1803efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1804efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1805efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v,VEC_CLASSID,3); 1806efe9872eSLisandro Dalcin ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 1807efe9872eSLisandro Dalcin ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr); 1808efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 1809efe9872eSLisandro Dalcin ts->vec_dot = v; 1810efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1811efe9872eSLisandro Dalcin } 1812efe9872eSLisandro Dalcin 1813efe9872eSLisandro Dalcin /*@ 1814efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 1815efe9872eSLisandro Dalcin for second order equations. It is valid to call this routine inside the function 1816efe9872eSLisandro Dalcin that you are evaluating in order to move to the new timestep. This vector not 1817efe9872eSLisandro Dalcin changed until the solution at the next timestep has been calculated. 1818efe9872eSLisandro Dalcin 1819efe9872eSLisandro Dalcin Not Collective, but Vec returned is parallel if TS is parallel 1820efe9872eSLisandro Dalcin 1821efe9872eSLisandro Dalcin Input Parameter: 1822efe9872eSLisandro Dalcin . ts - the TS context obtained from TSCreate() 1823efe9872eSLisandro Dalcin 1824efe9872eSLisandro Dalcin Output Parameter: 1825efe9872eSLisandro Dalcin + u - the vector containing the solution 1826efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1827efe9872eSLisandro Dalcin 1828efe9872eSLisandro Dalcin Level: intermediate 1829efe9872eSLisandro Dalcin 1830efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime() 1831efe9872eSLisandro Dalcin 1832efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution 1833efe9872eSLisandro Dalcin @*/ 1834efe9872eSLisandro Dalcin PetscErrorCode TS2GetSolution(TS ts,Vec *u,Vec *v) 1835efe9872eSLisandro Dalcin { 1836efe9872eSLisandro Dalcin PetscFunctionBegin; 1837efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1838efe9872eSLisandro Dalcin if (u) PetscValidPointer(u,2); 1839efe9872eSLisandro Dalcin if (v) PetscValidPointer(v,3); 1840efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1841efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 1842efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1843efe9872eSLisandro Dalcin } 1844efe9872eSLisandro Dalcin 184555849f57SBarry Smith /*@C 184655849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 184755849f57SBarry Smith 184855849f57SBarry Smith Collective on PetscViewer 184955849f57SBarry Smith 185055849f57SBarry Smith Input Parameters: 185155849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 185255849f57SBarry Smith some related function before a call to TSLoad(). 185355849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 185455849f57SBarry Smith 185555849f57SBarry Smith Level: intermediate 185655849f57SBarry Smith 185755849f57SBarry Smith Notes: 185855849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 185955849f57SBarry Smith 186055849f57SBarry Smith Notes for advanced users: 186155849f57SBarry Smith Most users should not need to know the details of the binary storage 186255849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 186355849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 186455849f57SBarry Smith format is 186555849f57SBarry Smith .vb 186655849f57SBarry Smith has not yet been determined 186755849f57SBarry Smith .ve 186855849f57SBarry Smith 186955849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 187055849f57SBarry Smith @*/ 1871f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 187255849f57SBarry Smith { 187355849f57SBarry Smith PetscErrorCode ierr; 187455849f57SBarry Smith PetscBool isbinary; 187555849f57SBarry Smith PetscInt classid; 187655849f57SBarry Smith char type[256]; 18772d53ad75SBarry Smith DMTS sdm; 1878ad6bc421SBarry Smith DM dm; 187955849f57SBarry Smith 188055849f57SBarry Smith PetscFunctionBegin; 1881f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 188255849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 188355849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 188455849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 188555849f57SBarry Smith 1886060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1887ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1888060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1889f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1890f2c2a1b9SBarry Smith if (ts->ops->load) { 1891f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1892f2c2a1b9SBarry Smith } 1893ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1894ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1895ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1896f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1897f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 18982d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 18992d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 190055849f57SBarry Smith PetscFunctionReturn(0); 190155849f57SBarry Smith } 190255849f57SBarry Smith 19039804daf3SBarry Smith #include <petscdraw.h> 1904e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1905e04113cfSBarry Smith #include <petscviewersaws.h> 1906f05ece33SBarry Smith #endif 19077e2c5f70SBarry Smith /*@C 1908d763cef2SBarry Smith TSView - Prints the TS data structure. 1909d763cef2SBarry Smith 19104c49b128SBarry Smith Collective on TS 1911d763cef2SBarry Smith 1912d763cef2SBarry Smith Input Parameters: 1913d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1914d763cef2SBarry Smith - viewer - visualization context 1915d763cef2SBarry Smith 1916d763cef2SBarry Smith Options Database Key: 1917d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1918d763cef2SBarry Smith 1919d763cef2SBarry Smith Notes: 1920d763cef2SBarry Smith The available visualization contexts include 1921b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1922b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1923d763cef2SBarry Smith output where only the first processor opens 1924d763cef2SBarry Smith the file. All other processors send their 1925d763cef2SBarry Smith data to the first processor to print. 1926d763cef2SBarry Smith 1927d763cef2SBarry Smith The user can open an alternative visualization context with 1928b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1929d763cef2SBarry Smith 1930d763cef2SBarry Smith Level: beginner 1931d763cef2SBarry Smith 1932d763cef2SBarry Smith .keywords: TS, timestep, view 1933d763cef2SBarry Smith 1934b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1935d763cef2SBarry Smith @*/ 19367087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1937d763cef2SBarry Smith { 1938dfbe8321SBarry Smith PetscErrorCode ierr; 193919fd82e9SBarry Smith TSType type; 19402b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 19412d53ad75SBarry Smith DMTS sdm; 1942e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1943536b137fSBarry Smith PetscBool issaws; 1944f05ece33SBarry Smith #endif 1945d763cef2SBarry Smith 1946d763cef2SBarry Smith PetscFunctionBegin; 19470700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 19483050cee2SBarry Smith if (!viewer) { 1949ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 19503050cee2SBarry Smith } 19510700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1952c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1953fd16b177SBarry Smith 1954251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1955251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 195655849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 19572b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1958e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1959536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1960f05ece33SBarry Smith #endif 196132077d6dSBarry Smith if (iascii) { 1962dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 1963efd4aadfSBarry Smith if (ts->ops->view) { 1964efd4aadfSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1965efd4aadfSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1966efd4aadfSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1967efd4aadfSBarry Smith } 196877431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 19697c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1970efd4aadfSBarry Smith if (ts->usessnes) { 1971efd4aadfSBarry Smith PetscBool lin; 1972d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 19735ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1974d763cef2SBarry Smith } 19755ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1976efd4aadfSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)ts->snes,SNESKSPONLY,&lin);CHKERRQ(ierr); 1977efd4aadfSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr); 1978efd4aadfSBarry Smith } 1979193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 1980a0af407cSBarry Smith if (ts->vrtol) { 1981a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of relative error tolerances, ");CHKERRQ(ierr); 1982a0af407cSBarry Smith } else { 1983a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr); 1984a0af407cSBarry Smith } 1985a0af407cSBarry Smith if (ts->vatol) { 1986a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of absolute error tolerances\n");CHKERRQ(ierr); 1987a0af407cSBarry Smith } else { 1988a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr); 1989a0af407cSBarry Smith } 1990efd4aadfSBarry Smith ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr); 1991efd4aadfSBarry Smith if (ts->snes && ts->usessnes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 19922d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19932d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 19940f5bd95cSBarry Smith } else if (isstring) { 1995a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1996b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 199755849f57SBarry Smith } else if (isbinary) { 199855849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 199955849f57SBarry Smith MPI_Comm comm; 200055849f57SBarry Smith PetscMPIInt rank; 200155849f57SBarry Smith char type[256]; 200255849f57SBarry Smith 200355849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 200455849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 200555849f57SBarry Smith if (!rank) { 200655849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 200755849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 200855849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 200955849f57SBarry Smith } 201055849f57SBarry Smith if (ts->ops->view) { 201155849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 201255849f57SBarry Smith } 2013efd4aadfSBarry Smith if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);} 2014f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 2015f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 20162d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 20172d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 20182b0a91c0SBarry Smith } else if (isdraw) { 20192b0a91c0SBarry Smith PetscDraw draw; 20202b0a91c0SBarry Smith char str[36]; 202189fd9fafSBarry Smith PetscReal x,y,bottom,h; 20222b0a91c0SBarry Smith 20232b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 20242b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 20252b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 20262b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 202751fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 202889fd9fafSBarry Smith bottom = y - h; 20292b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 20302b0a91c0SBarry Smith if (ts->ops->view) { 20312b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 20322b0a91c0SBarry Smith } 2033efd4aadfSBarry Smith if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);} 2034efd4aadfSBarry Smith if (ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 20352b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 2036e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 2037536b137fSBarry Smith } else if (issaws) { 2038d45a07a7SBarry Smith PetscMPIInt rank; 20392657e9d9SBarry Smith const char *name; 20402657e9d9SBarry Smith 20412657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 2042d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 2043d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 2044d45a07a7SBarry Smith char dir[1024]; 2045d45a07a7SBarry Smith 2046e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 2047a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 20482657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 20492657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 20502657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 2051d763cef2SBarry Smith } 20520acecf5bSBarry Smith if (ts->ops->view) { 20530acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 20540acecf5bSBarry Smith } 2055f05ece33SBarry Smith #endif 2056f05ece33SBarry Smith } 2057f05ece33SBarry Smith 2058b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2059251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 2060b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2061d763cef2SBarry Smith PetscFunctionReturn(0); 2062d763cef2SBarry Smith } 2063d763cef2SBarry Smith 2064d763cef2SBarry Smith 2065b07ff414SBarry Smith /*@ 2066d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2067d763cef2SBarry Smith the timesteppers. 2068d763cef2SBarry Smith 20693f9fe445SBarry Smith Logically Collective on TS 2070d763cef2SBarry Smith 2071d763cef2SBarry Smith Input Parameters: 2072d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2073d763cef2SBarry Smith - usrP - optional user context 2074d763cef2SBarry Smith 2075daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 2076daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2077daf670e6SBarry Smith 2078d763cef2SBarry Smith Level: intermediate 2079d763cef2SBarry Smith 2080d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 2081d763cef2SBarry Smith 2082d763cef2SBarry Smith .seealso: TSGetApplicationContext() 2083d763cef2SBarry Smith @*/ 20847087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 2085d763cef2SBarry Smith { 2086d763cef2SBarry Smith PetscFunctionBegin; 20870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2088d763cef2SBarry Smith ts->user = usrP; 2089d763cef2SBarry Smith PetscFunctionReturn(0); 2090d763cef2SBarry Smith } 2091d763cef2SBarry Smith 2092b07ff414SBarry Smith /*@ 2093d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2094d763cef2SBarry Smith timestepper. 2095d763cef2SBarry Smith 2096d763cef2SBarry Smith Not Collective 2097d763cef2SBarry Smith 2098d763cef2SBarry Smith Input Parameter: 2099d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2100d763cef2SBarry Smith 2101d763cef2SBarry Smith Output Parameter: 2102d763cef2SBarry Smith . usrP - user context 2103d763cef2SBarry Smith 2104daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 2105daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2106daf670e6SBarry Smith 2107d763cef2SBarry Smith Level: intermediate 2108d763cef2SBarry Smith 2109d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 2110d763cef2SBarry Smith 2111d763cef2SBarry Smith .seealso: TSSetApplicationContext() 2112d763cef2SBarry Smith @*/ 2113e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 2114d763cef2SBarry Smith { 2115d763cef2SBarry Smith PetscFunctionBegin; 21160700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2117e71120c6SJed Brown *(void**)usrP = ts->user; 2118d763cef2SBarry Smith PetscFunctionReturn(0); 2119d763cef2SBarry Smith } 2120d763cef2SBarry Smith 2121d763cef2SBarry Smith /*@ 2122b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 2123d763cef2SBarry Smith 2124d763cef2SBarry Smith Not Collective 2125d763cef2SBarry Smith 2126d763cef2SBarry Smith Input Parameter: 2127d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2128d763cef2SBarry Smith 2129d763cef2SBarry Smith Output Parameter: 2130b8123daeSJed Brown . iter - number of steps completed so far 2131d763cef2SBarry Smith 2132d763cef2SBarry Smith Level: intermediate 2133d763cef2SBarry Smith 2134d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 21359be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 2136d763cef2SBarry Smith @*/ 21377087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 2138d763cef2SBarry Smith { 2139d763cef2SBarry Smith PetscFunctionBegin; 21400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 21414482741eSBarry Smith PetscValidIntPointer(iter,2); 2142d763cef2SBarry Smith *iter = ts->steps; 2143d763cef2SBarry Smith PetscFunctionReturn(0); 2144d763cef2SBarry Smith } 2145d763cef2SBarry Smith 2146d763cef2SBarry Smith /*@ 2147d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 2148d763cef2SBarry Smith as well as the initial time. 2149d763cef2SBarry Smith 21503f9fe445SBarry Smith Logically Collective on TS 2151d763cef2SBarry Smith 2152d763cef2SBarry Smith Input Parameters: 2153d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2154d763cef2SBarry Smith . initial_time - the initial time 2155d763cef2SBarry Smith - time_step - the size of the timestep 2156d763cef2SBarry Smith 2157d763cef2SBarry Smith Level: intermediate 2158d763cef2SBarry Smith 2159d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 2160d763cef2SBarry Smith 2161d763cef2SBarry Smith .keywords: TS, set, initial, timestep 2162d763cef2SBarry Smith @*/ 21637087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 2164d763cef2SBarry Smith { 2165e144a568SJed Brown PetscErrorCode ierr; 2166e144a568SJed Brown 2167d763cef2SBarry Smith PetscFunctionBegin; 21680700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2169e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 2170d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 2171d763cef2SBarry Smith PetscFunctionReturn(0); 2172d763cef2SBarry Smith } 2173d763cef2SBarry Smith 2174d763cef2SBarry Smith /*@ 2175d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2176d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2177d763cef2SBarry Smith 21783f9fe445SBarry Smith Logically Collective on TS 2179d763cef2SBarry Smith 2180d763cef2SBarry Smith Input Parameters: 2181d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2182d763cef2SBarry Smith - time_step - the size of the timestep 2183d763cef2SBarry Smith 2184d763cef2SBarry Smith Level: intermediate 2185d763cef2SBarry Smith 2186d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2187d763cef2SBarry Smith 2188d763cef2SBarry Smith .keywords: TS, set, timestep 2189d763cef2SBarry Smith @*/ 21907087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 2191d763cef2SBarry Smith { 2192d763cef2SBarry Smith PetscFunctionBegin; 21930700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2194c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 2195d763cef2SBarry Smith ts->time_step = time_step; 2196d763cef2SBarry Smith PetscFunctionReturn(0); 2197d763cef2SBarry Smith } 2198d763cef2SBarry Smith 2199a43b19c4SJed Brown /*@ 220049354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 220149354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 220249354f04SShri Abhyankar or just return at the final time TS computed. 2203a43b19c4SJed Brown 2204a43b19c4SJed Brown Logically Collective on TS 2205a43b19c4SJed Brown 2206a43b19c4SJed Brown Input Parameter: 2207a43b19c4SJed Brown + ts - the time-step context 220849354f04SShri Abhyankar - eftopt - exact final time option 2209a43b19c4SJed Brown 2210feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2211feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2212feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2213feed9e9dSBarry Smith 2214feed9e9dSBarry Smith Options Database: 2215feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2216feed9e9dSBarry Smith 2217ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 2218ee346746SBarry Smith then the final time you selected. 2219ee346746SBarry Smith 2220a43b19c4SJed Brown Level: beginner 2221a43b19c4SJed Brown 2222a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 2223a43b19c4SJed Brown @*/ 222449354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 2225a43b19c4SJed Brown { 2226a43b19c4SJed Brown PetscFunctionBegin; 2227a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 222849354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 222949354f04SShri Abhyankar ts->exact_final_time = eftopt; 2230a43b19c4SJed Brown PetscFunctionReturn(0); 2231a43b19c4SJed Brown } 2232a43b19c4SJed Brown 2233d763cef2SBarry Smith /*@ 2234d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2235d763cef2SBarry Smith 2236d763cef2SBarry Smith Not Collective 2237d763cef2SBarry Smith 2238d763cef2SBarry Smith Input Parameter: 2239d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2240d763cef2SBarry Smith 2241d763cef2SBarry Smith Output Parameter: 2242d763cef2SBarry Smith . dt - the current timestep size 2243d763cef2SBarry Smith 2244d763cef2SBarry Smith Level: intermediate 2245d763cef2SBarry Smith 2246d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2247d763cef2SBarry Smith 2248d763cef2SBarry Smith .keywords: TS, get, timestep 2249d763cef2SBarry Smith @*/ 22507087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 2251d763cef2SBarry Smith { 2252d763cef2SBarry Smith PetscFunctionBegin; 22530700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2254f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 2255d763cef2SBarry Smith *dt = ts->time_step; 2256d763cef2SBarry Smith PetscFunctionReturn(0); 2257d763cef2SBarry Smith } 2258d763cef2SBarry Smith 2259d8e5e3e6SSatish Balay /*@ 2260d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2261d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2262d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2263d763cef2SBarry Smith the solution at the next timestep has been calculated. 2264d763cef2SBarry Smith 2265d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 2266d763cef2SBarry Smith 2267d763cef2SBarry Smith Input Parameter: 2268d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2269d763cef2SBarry Smith 2270d763cef2SBarry Smith Output Parameter: 2271d763cef2SBarry Smith . v - the vector containing the solution 2272d763cef2SBarry Smith 227363e21af5SBarry Smith Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested 227463e21af5SBarry Smith final time. It returns the solution at the next timestep. 227563e21af5SBarry Smith 2276d763cef2SBarry Smith Level: intermediate 2277d763cef2SBarry Smith 2278b2bf4f3aSDebojyoti Ghosh .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents() 2279d763cef2SBarry Smith 2280d763cef2SBarry Smith .keywords: TS, timestep, get, solution 2281d763cef2SBarry Smith @*/ 22827087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 2283d763cef2SBarry Smith { 2284d763cef2SBarry Smith PetscFunctionBegin; 22850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22864482741eSBarry Smith PetscValidPointer(v,2); 22878737fe31SLisandro Dalcin *v = ts->vec_sol; 2288d763cef2SBarry Smith PetscFunctionReturn(0); 2289d763cef2SBarry Smith } 2290d763cef2SBarry Smith 229103fe5f5eSDebojyoti Ghosh /*@ 2292b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 229303fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2294b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 229503fe5f5eSDebojyoti Ghosh structure as the solution vector. 229603fe5f5eSDebojyoti Ghosh 229703fe5f5eSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 229803fe5f5eSDebojyoti Ghosh 229903fe5f5eSDebojyoti Ghosh Parameters : 230003fe5f5eSDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 2301b2bf4f3aSDebojyoti Ghosh . n - If v is PETSC_NULL, then the number of solution components is 2302b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 230303fe5f5eSDebojyoti Ghosh returned in v. 2304b2bf4f3aSDebojyoti Ghosh . v - the vector containing the n-th solution component 230503fe5f5eSDebojyoti Ghosh (may be PETSC_NULL to use this function to find out 2306b2bf4f3aSDebojyoti Ghosh the number of solutions components). 230703fe5f5eSDebojyoti Ghosh 23084cdd57e5SDebojyoti Ghosh Level: advanced 230903fe5f5eSDebojyoti Ghosh 231003fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution() 231103fe5f5eSDebojyoti Ghosh 231203fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution 231303fe5f5eSDebojyoti Ghosh @*/ 2314b2bf4f3aSDebojyoti Ghosh PetscErrorCode TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v) 231503fe5f5eSDebojyoti Ghosh { 231603fe5f5eSDebojyoti Ghosh PetscErrorCode ierr; 231703fe5f5eSDebojyoti Ghosh 231803fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 231903fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2320b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 232103fe5f5eSDebojyoti Ghosh else { 2322b2bf4f3aSDebojyoti Ghosh ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr); 232303fe5f5eSDebojyoti Ghosh } 232403fe5f5eSDebojyoti Ghosh PetscFunctionReturn(0); 232503fe5f5eSDebojyoti Ghosh } 232603fe5f5eSDebojyoti Ghosh 23274cdd57e5SDebojyoti Ghosh /*@ 23284cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 23294cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 23304cdd57e5SDebojyoti Ghosh 23314cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 23324cdd57e5SDebojyoti Ghosh 23334cdd57e5SDebojyoti Ghosh Parameters : 23344cdd57e5SDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 23354cdd57e5SDebojyoti Ghosh . v - the vector containing the auxiliary solution 23364cdd57e5SDebojyoti Ghosh 23374cdd57e5SDebojyoti Ghosh Level: intermediate 23384cdd57e5SDebojyoti Ghosh 23394cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution() 23404cdd57e5SDebojyoti Ghosh 23414cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution 23424cdd57e5SDebojyoti Ghosh @*/ 23434cdd57e5SDebojyoti Ghosh PetscErrorCode TSGetAuxSolution(TS ts,Vec *v) 23444cdd57e5SDebojyoti Ghosh { 23454cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 23464cdd57e5SDebojyoti Ghosh 23474cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23484cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2349f6356ec7SDebojyoti Ghosh if (ts->ops->getauxsolution) { 23504cdd57e5SDebojyoti Ghosh ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr); 2351f6356ec7SDebojyoti Ghosh } else { 2352f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v); CHKERRQ(ierr); 2353f6356ec7SDebojyoti Ghosh } 23544cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 23554cdd57e5SDebojyoti Ghosh } 23564cdd57e5SDebojyoti Ghosh 23574cdd57e5SDebojyoti Ghosh /*@ 23584cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 23594cdd57e5SDebojyoti Ghosh TSType has an error estimation functionality. 23604cdd57e5SDebojyoti Ghosh 23614cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 23624cdd57e5SDebojyoti Ghosh 23639657682dSDebojyoti Ghosh Note: MUST call after TSSetUp() 23649657682dSDebojyoti Ghosh 23654cdd57e5SDebojyoti Ghosh Parameters : 23664cdd57e5SDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 2367657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 23684cdd57e5SDebojyoti Ghosh . v - the vector containing the error (same size as the solution). 23694cdd57e5SDebojyoti Ghosh 23704cdd57e5SDebojyoti Ghosh Level: intermediate 23714cdd57e5SDebojyoti Ghosh 237257df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError() 23734cdd57e5SDebojyoti Ghosh 23744cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error 23754cdd57e5SDebojyoti Ghosh @*/ 23760a01e1b2SEmil Constantinescu PetscErrorCode TSGetTimeError(TS ts,PetscInt n,Vec *v) 23774cdd57e5SDebojyoti Ghosh { 23784cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 23794cdd57e5SDebojyoti Ghosh 23804cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23814cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2382f6356ec7SDebojyoti Ghosh if (ts->ops->gettimeerror) { 23830a01e1b2SEmil Constantinescu ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr); 2384f6356ec7SDebojyoti Ghosh } else { 2385f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v);CHKERRQ(ierr); 2386f6356ec7SDebojyoti Ghosh } 23874cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 23884cdd57e5SDebojyoti Ghosh } 23894cdd57e5SDebojyoti Ghosh 239057df6a1bSDebojyoti Ghosh /*@ 239157df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 239257df6a1bSDebojyoti Ghosh TSType has an error estimation functionality. This can be used 239357df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 239457df6a1bSDebojyoti Ghosh 239557df6a1bSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 239657df6a1bSDebojyoti Ghosh 239757df6a1bSDebojyoti Ghosh Parameters : 239857df6a1bSDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 239957df6a1bSDebojyoti Ghosh . v - the vector containing the error (same size as the solution). 240057df6a1bSDebojyoti Ghosh 240157df6a1bSDebojyoti Ghosh Level: intermediate 240257df6a1bSDebojyoti Ghosh 240357df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError) 240457df6a1bSDebojyoti Ghosh 240557df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error 240657df6a1bSDebojyoti Ghosh @*/ 240757df6a1bSDebojyoti Ghosh PetscErrorCode TSSetTimeError(TS ts,Vec v) 240857df6a1bSDebojyoti Ghosh { 240957df6a1bSDebojyoti Ghosh PetscErrorCode ierr; 241057df6a1bSDebojyoti Ghosh 241157df6a1bSDebojyoti Ghosh PetscFunctionBegin; 241257df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 24139657682dSDebojyoti Ghosh if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first"); 241457df6a1bSDebojyoti Ghosh if (ts->ops->settimeerror) { 241557df6a1bSDebojyoti Ghosh ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr); 241657df6a1bSDebojyoti Ghosh } 241757df6a1bSDebojyoti Ghosh PetscFunctionReturn(0); 241857df6a1bSDebojyoti Ghosh } 241957df6a1bSDebojyoti Ghosh 2420c235aa8dSHong Zhang /*@ 2421dfb21088SHong Zhang TSGetCostGradients - Returns the gradients from the TSAdjointSolve() 2422c235aa8dSHong Zhang 2423c235aa8dSHong Zhang Not Collective, but Vec returned is parallel if TS is parallel 2424c235aa8dSHong Zhang 2425c235aa8dSHong Zhang Input Parameter: 2426c235aa8dSHong Zhang . ts - the TS context obtained from TSCreate() 2427c235aa8dSHong Zhang 2428c235aa8dSHong Zhang Output Parameter: 2429abc2977eSBarry Smith + lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 2430abc2977eSBarry Smith - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 2431c235aa8dSHong Zhang 2432c235aa8dSHong Zhang Level: intermediate 2433c235aa8dSHong Zhang 2434c235aa8dSHong Zhang .seealso: TSGetTimeStep() 2435c235aa8dSHong Zhang 2436c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity 2437c235aa8dSHong Zhang @*/ 2438dfb21088SHong Zhang PetscErrorCode TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu) 2439c235aa8dSHong Zhang { 2440c235aa8dSHong Zhang PetscFunctionBegin; 2441c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2442abc2977eSBarry Smith if (numcost) *numcost = ts->numcost; 2443abc2977eSBarry Smith if (lambda) *lambda = ts->vecs_sensi; 2444abc2977eSBarry Smith if (mu) *mu = ts->vecs_sensip; 2445c235aa8dSHong Zhang PetscFunctionReturn(0); 2446c235aa8dSHong Zhang } 2447c235aa8dSHong Zhang 2448bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2449d8e5e3e6SSatish Balay /*@ 2450bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2451d763cef2SBarry Smith 2452bdad233fSMatthew Knepley Not collective 2453d763cef2SBarry Smith 2454bdad233fSMatthew Knepley Input Parameters: 2455bdad233fSMatthew Knepley + ts - The TS 2456bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2457d763cef2SBarry Smith .vb 24580910c330SBarry Smith U_t - A U = 0 (linear) 24590910c330SBarry Smith U_t - A(t) U = 0 (linear) 24600910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2461d763cef2SBarry Smith .ve 2462d763cef2SBarry Smith 2463d763cef2SBarry Smith Level: beginner 2464d763cef2SBarry Smith 2465bdad233fSMatthew Knepley .keywords: TS, problem type 2466bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2467d763cef2SBarry Smith @*/ 24687087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2469a7cc72afSBarry Smith { 24709e2a6581SJed Brown PetscErrorCode ierr; 24719e2a6581SJed Brown 2472d763cef2SBarry Smith PetscFunctionBegin; 24730700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2474bdad233fSMatthew Knepley ts->problem_type = type; 24759e2a6581SJed Brown if (type == TS_LINEAR) { 24769e2a6581SJed Brown SNES snes; 24779e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 24789e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 24799e2a6581SJed Brown } 2480d763cef2SBarry Smith PetscFunctionReturn(0); 2481d763cef2SBarry Smith } 2482d763cef2SBarry Smith 2483bdad233fSMatthew Knepley /*@C 2484bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2485bdad233fSMatthew Knepley 2486bdad233fSMatthew Knepley Not collective 2487bdad233fSMatthew Knepley 2488bdad233fSMatthew Knepley Input Parameter: 2489bdad233fSMatthew Knepley . ts - The TS 2490bdad233fSMatthew Knepley 2491bdad233fSMatthew Knepley Output Parameter: 2492bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2493bdad233fSMatthew Knepley .vb 2494089b2837SJed Brown M U_t = A U 2495089b2837SJed Brown M(t) U_t = A(t) U 2496b5abc632SBarry Smith F(t,U,U_t) 2497bdad233fSMatthew Knepley .ve 2498bdad233fSMatthew Knepley 2499bdad233fSMatthew Knepley Level: beginner 2500bdad233fSMatthew Knepley 2501bdad233fSMatthew Knepley .keywords: TS, problem type 2502bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2503bdad233fSMatthew Knepley @*/ 25047087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2505a7cc72afSBarry Smith { 2506bdad233fSMatthew Knepley PetscFunctionBegin; 25070700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 25084482741eSBarry Smith PetscValidIntPointer(type,2); 2509bdad233fSMatthew Knepley *type = ts->problem_type; 2510bdad233fSMatthew Knepley PetscFunctionReturn(0); 2511bdad233fSMatthew Knepley } 2512d763cef2SBarry Smith 2513d763cef2SBarry Smith /*@ 2514d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 2515d763cef2SBarry Smith of a timestepper. 2516d763cef2SBarry Smith 2517d763cef2SBarry Smith Collective on TS 2518d763cef2SBarry Smith 2519d763cef2SBarry Smith Input Parameter: 2520d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2521d763cef2SBarry Smith 2522d763cef2SBarry Smith Notes: 2523d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 2524d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 2525d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 2526d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 2527d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 2528d763cef2SBarry Smith 2529d763cef2SBarry Smith Level: advanced 2530d763cef2SBarry Smith 2531d763cef2SBarry Smith .keywords: TS, timestep, setup 2532d763cef2SBarry Smith 2533d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 2534d763cef2SBarry Smith @*/ 25357087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 2536d763cef2SBarry Smith { 2537dfbe8321SBarry Smith PetscErrorCode ierr; 25386c6b9e74SPeter Brune DM dm; 25396c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 2540d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 2541cd11d68dSLisandro Dalcin TSIFunction ifun; 25426c6b9e74SPeter Brune TSIJacobian ijac; 2543efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 25446c6b9e74SPeter Brune TSRHSJacobian rhsjac; 25452ffb9264SLisandro Dalcin PetscBool isnone; 2546d763cef2SBarry Smith 2547d763cef2SBarry Smith PetscFunctionBegin; 25480700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2549277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 2550277b19d0SLisandro Dalcin 25517adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 2552cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 2553cd11d68dSLisandro Dalcin ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr); 2554d763cef2SBarry Smith } 2555277b19d0SLisandro Dalcin 2556277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 2557277b19d0SLisandro Dalcin 2558e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 2559e1244c69SJed Brown Mat Amat,Pmat; 2560e1244c69SJed Brown SNES snes; 2561e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2562e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 2563e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2564e1244c69SJed Brown * have displaced the RHS matrix */ 2565e1244c69SJed Brown if (Amat == ts->Arhs) { 2566*abc0d4abSBarry Smith /* we need to copy the values of the matrix because for the constant Jacobian case the user will never set the numerical values in this new location */ 2567*abc0d4abSBarry Smith ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr); 2568e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 2569e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 2570e1244c69SJed Brown } 2571e1244c69SJed Brown if (Pmat == ts->Brhs) { 2572*abc0d4abSBarry Smith ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 2573e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 2574e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 2575e1244c69SJed Brown } 2576e1244c69SJed Brown } 25772ffb9264SLisandro Dalcin 25782ffb9264SLisandro Dalcin ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 25792ffb9264SLisandro Dalcin ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr); 25802ffb9264SLisandro Dalcin 2581277b19d0SLisandro Dalcin if (ts->ops->setup) { 2582000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 2583277b19d0SLisandro Dalcin } 2584277b19d0SLisandro Dalcin 25852ffb9264SLisandro Dalcin /* Attempt to check/preset a default value for the exact final time option */ 25862ffb9264SLisandro Dalcin ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr); 25872ffb9264SLisandro Dalcin if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) 25882ffb9264SLisandro Dalcin ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP; 25892ffb9264SLisandro Dalcin 2590a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 25916c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 25926c6b9e74SPeter Brune */ 25936c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 25940298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 25956c6b9e74SPeter Brune if (!func) { 25966c6b9e74SPeter Brune ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 25976c6b9e74SPeter Brune } 2598a6772fa2SLisandro 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. 25996c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 26006c6b9e74SPeter Brune */ 26010298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 26020298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 2603efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr); 26040298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 2605efe9872eSLisandro Dalcin if (!jac && (ijac || i2jac || rhsjac)) { 26066c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 26076c6b9e74SPeter Brune } 2608c0517034SDebojyoti Ghosh 2609c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 2610c0517034SDebojyoti Ghosh if (ts->ops->startingmethod) { 2611c0517034SDebojyoti Ghosh ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr); 2612c0517034SDebojyoti Ghosh } 2613c0517034SDebojyoti Ghosh 2614277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2615277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2616277b19d0SLisandro Dalcin } 2617277b19d0SLisandro Dalcin 2618f6a906c0SBarry Smith /*@ 2619f6a906c0SBarry Smith TSAdjointSetUp - Sets up the internal data structures for the later use 2620f6a906c0SBarry Smith of an adjoint solver 2621f6a906c0SBarry Smith 2622f6a906c0SBarry Smith Collective on TS 2623f6a906c0SBarry Smith 2624f6a906c0SBarry Smith Input Parameter: 2625f6a906c0SBarry Smith . ts - the TS context obtained from TSCreate() 2626f6a906c0SBarry Smith 2627f6a906c0SBarry Smith Level: advanced 2628f6a906c0SBarry Smith 2629f6a906c0SBarry Smith .keywords: TS, timestep, setup 2630f6a906c0SBarry Smith 2631947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients() 2632f6a906c0SBarry Smith @*/ 2633f6a906c0SBarry Smith PetscErrorCode TSAdjointSetUp(TS ts) 2634f6a906c0SBarry Smith { 2635f6a906c0SBarry Smith PetscErrorCode ierr; 2636f6a906c0SBarry Smith 2637f6a906c0SBarry Smith PetscFunctionBegin; 2638f6a906c0SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2639f6a906c0SBarry Smith if (ts->adjointsetupcalled) PetscFunctionReturn(0); 264023706b9aSHong Zhang if (!ts->vecs_sensi) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first"); 2641c5767687SHong Zhang if (ts->vecs_sensip && !ts->Jacp) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"Must call TSAdjointSetRHSJacobian() first"); 2642d8151eeaSBarry Smith 2643947abb85SHong Zhang if (ts->vec_costintegral) { /* if there is integral in the cost function */ 2644d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2645d8151eeaSBarry Smith if (ts->vecs_sensip){ 2646d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2647d8151eeaSBarry Smith } 2648947abb85SHong Zhang } 2649d8151eeaSBarry Smith 265042f2b339SBarry Smith if (ts->ops->adjointsetup) { 265142f2b339SBarry Smith ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr); 2652f6a906c0SBarry Smith } 2653f6a906c0SBarry Smith ts->adjointsetupcalled = PETSC_TRUE; 2654f6a906c0SBarry Smith PetscFunctionReturn(0); 2655f6a906c0SBarry Smith } 2656f6a906c0SBarry Smith 2657277b19d0SLisandro Dalcin /*@ 2658277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2659277b19d0SLisandro Dalcin 2660277b19d0SLisandro Dalcin Collective on TS 2661277b19d0SLisandro Dalcin 2662277b19d0SLisandro Dalcin Input Parameter: 2663277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2664277b19d0SLisandro Dalcin 2665277b19d0SLisandro Dalcin Level: beginner 2666277b19d0SLisandro Dalcin 2667277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 2668277b19d0SLisandro Dalcin 2669277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2670277b19d0SLisandro Dalcin @*/ 2671277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2672277b19d0SLisandro Dalcin { 2673277b19d0SLisandro Dalcin PetscErrorCode ierr; 2674277b19d0SLisandro Dalcin 2675277b19d0SLisandro Dalcin PetscFunctionBegin; 2676277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2677b18ea86cSHong Zhang 2678277b19d0SLisandro Dalcin if (ts->ops->reset) { 2679277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2680277b19d0SLisandro Dalcin } 2681277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2682e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2683bbd56ea5SKarl Rupp 26844e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 26854e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2686214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 26876bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2688efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 2689e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2690e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 269138637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2692bbd56ea5SKarl Rupp 2693947abb85SHong Zhang if (ts->vec_costintegral) { 2694d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2695d8151eeaSBarry Smith if (ts->vecs_drdp){ 2696d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2697d8151eeaSBarry Smith } 2698947abb85SHong Zhang } 2699d8151eeaSBarry Smith ts->vecs_sensi = NULL; 2700d8151eeaSBarry Smith ts->vecs_sensip = NULL; 2701ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 27022c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 270336eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2704277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2705d763cef2SBarry Smith PetscFunctionReturn(0); 2706d763cef2SBarry Smith } 2707d763cef2SBarry Smith 2708d8e5e3e6SSatish Balay /*@ 2709d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2710d763cef2SBarry Smith with TSCreate(). 2711d763cef2SBarry Smith 2712d763cef2SBarry Smith Collective on TS 2713d763cef2SBarry Smith 2714d763cef2SBarry Smith Input Parameter: 2715d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2716d763cef2SBarry Smith 2717d763cef2SBarry Smith Level: beginner 2718d763cef2SBarry Smith 2719d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2720d763cef2SBarry Smith 2721d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2722d763cef2SBarry Smith @*/ 27236bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2724d763cef2SBarry Smith { 27256849ba73SBarry Smith PetscErrorCode ierr; 2726d763cef2SBarry Smith 2727d763cef2SBarry Smith PetscFunctionBegin; 27286bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 27296bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 27306bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2731d763cef2SBarry Smith 27326bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2733277b19d0SLisandro Dalcin 2734e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2735e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 27366bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 27376d4c513bSLisandro Dalcin 2738bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2739bc952696SBarry Smith 274084df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 27416427ac75SLisandro Dalcin ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr); 27426427ac75SLisandro Dalcin 27436bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 27446bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 27456bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 27460dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 27476d4c513bSLisandro Dalcin 2748a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2749d763cef2SBarry Smith PetscFunctionReturn(0); 2750d763cef2SBarry Smith } 2751d763cef2SBarry Smith 2752d8e5e3e6SSatish Balay /*@ 2753d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2754d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2755d763cef2SBarry Smith 2756d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2757d763cef2SBarry Smith 2758d763cef2SBarry Smith Input Parameter: 2759d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2760d763cef2SBarry Smith 2761d763cef2SBarry Smith Output Parameter: 2762d763cef2SBarry Smith . snes - the nonlinear solver context 2763d763cef2SBarry Smith 2764d763cef2SBarry Smith Notes: 2765d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2766d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 276794b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2768d763cef2SBarry Smith 2769d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 27700298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2771d763cef2SBarry Smith 2772d763cef2SBarry Smith Level: beginner 2773d763cef2SBarry Smith 2774d763cef2SBarry Smith .keywords: timestep, get, SNES 2775d763cef2SBarry Smith @*/ 27767087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2777d763cef2SBarry Smith { 2778d372ba47SLisandro Dalcin PetscErrorCode ierr; 2779d372ba47SLisandro Dalcin 2780d763cef2SBarry Smith PetscFunctionBegin; 27810700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27824482741eSBarry Smith PetscValidPointer(snes,2); 2783d372ba47SLisandro Dalcin if (!ts->snes) { 2784ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 27850298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 27863bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2787d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2788496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 27899e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 27909e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 27919e2a6581SJed Brown } 2792d372ba47SLisandro Dalcin } 2793d763cef2SBarry Smith *snes = ts->snes; 2794d763cef2SBarry Smith PetscFunctionReturn(0); 2795d763cef2SBarry Smith } 2796d763cef2SBarry Smith 2797deb2cd25SJed Brown /*@ 2798deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2799deb2cd25SJed Brown 2800deb2cd25SJed Brown Collective 2801deb2cd25SJed Brown 2802deb2cd25SJed Brown Input Parameter: 2803deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2804deb2cd25SJed Brown - snes - the nonlinear solver context 2805deb2cd25SJed Brown 2806deb2cd25SJed Brown Notes: 2807deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2808deb2cd25SJed Brown 2809deb2cd25SJed Brown Level: developer 2810deb2cd25SJed Brown 2811deb2cd25SJed Brown .keywords: timestep, set, SNES 2812deb2cd25SJed Brown @*/ 2813deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2814deb2cd25SJed Brown { 2815deb2cd25SJed Brown PetscErrorCode ierr; 2816d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2817deb2cd25SJed Brown 2818deb2cd25SJed Brown PetscFunctionBegin; 2819deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2820deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2821deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2822deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2823bbd56ea5SKarl Rupp 2824deb2cd25SJed Brown ts->snes = snes; 2825bbd56ea5SKarl Rupp 28260298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 28270298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2828740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 28290298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2830740132f1SEmil Constantinescu } 2831deb2cd25SJed Brown PetscFunctionReturn(0); 2832deb2cd25SJed Brown } 2833deb2cd25SJed Brown 2834d8e5e3e6SSatish Balay /*@ 283594b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2836d763cef2SBarry Smith a TS (timestepper) context. 2837d763cef2SBarry Smith 283894b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2839d763cef2SBarry Smith 2840d763cef2SBarry Smith Input Parameter: 2841d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2842d763cef2SBarry Smith 2843d763cef2SBarry Smith Output Parameter: 284494b7f48cSBarry Smith . ksp - the nonlinear solver context 2845d763cef2SBarry Smith 2846d763cef2SBarry Smith Notes: 284794b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2848d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2849d763cef2SBarry Smith KSP and PC contexts as well. 2850d763cef2SBarry Smith 285194b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 28520298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2853d763cef2SBarry Smith 2854d763cef2SBarry Smith Level: beginner 2855d763cef2SBarry Smith 285694b7f48cSBarry Smith .keywords: timestep, get, KSP 2857d763cef2SBarry Smith @*/ 28587087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2859d763cef2SBarry Smith { 2860d372ba47SLisandro Dalcin PetscErrorCode ierr; 2861089b2837SJed Brown SNES snes; 2862d372ba47SLisandro Dalcin 2863d763cef2SBarry Smith PetscFunctionBegin; 28640700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28654482741eSBarry Smith PetscValidPointer(ksp,2); 286617186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2867e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2868089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2869089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2870d763cef2SBarry Smith PetscFunctionReturn(0); 2871d763cef2SBarry Smith } 2872d763cef2SBarry Smith 2873d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2874d763cef2SBarry Smith 2875adb62b0dSMatthew Knepley /*@ 2876adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2877adb62b0dSMatthew Knepley maximum time for iteration. 2878adb62b0dSMatthew Knepley 28793f9fe445SBarry Smith Not Collective 2880adb62b0dSMatthew Knepley 2881adb62b0dSMatthew Knepley Input Parameters: 2882adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 28830298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 28840298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2885adb62b0dSMatthew Knepley 2886adb62b0dSMatthew Knepley Level: intermediate 2887adb62b0dSMatthew Knepley 2888adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2889adb62b0dSMatthew Knepley @*/ 28907087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2891adb62b0dSMatthew Knepley { 2892adb62b0dSMatthew Knepley PetscFunctionBegin; 28930700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2894abc0a331SBarry Smith if (maxsteps) { 28954482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2896adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2897adb62b0dSMatthew Knepley } 2898abc0a331SBarry Smith if (maxtime) { 28994482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2900adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2901adb62b0dSMatthew Knepley } 2902adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2903adb62b0dSMatthew Knepley } 2904adb62b0dSMatthew Knepley 2905d763cef2SBarry Smith /*@ 2906d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2907d763cef2SBarry Smith maximum time for iteration. 2908d763cef2SBarry Smith 29093f9fe445SBarry Smith Logically Collective on TS 2910d763cef2SBarry Smith 2911d763cef2SBarry Smith Input Parameters: 2912d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2913d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2914d763cef2SBarry Smith - maxtime - final time to iterate to 2915d763cef2SBarry Smith 2916d763cef2SBarry Smith Options Database Keys: 2917d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 29183bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2919d763cef2SBarry Smith 2920d763cef2SBarry Smith Notes: 2921d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2922d763cef2SBarry Smith 2923d763cef2SBarry Smith Level: intermediate 2924d763cef2SBarry Smith 2925d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2926a43b19c4SJed Brown 2927a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2928d763cef2SBarry Smith @*/ 29297087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2930d763cef2SBarry Smith { 2931d763cef2SBarry Smith PetscFunctionBegin; 29320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2933c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2934c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 293539b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 293639b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2937d763cef2SBarry Smith PetscFunctionReturn(0); 2938d763cef2SBarry Smith } 2939d763cef2SBarry Smith 2940d763cef2SBarry Smith /*@ 2941d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2942d763cef2SBarry Smith for use by the TS routines. 2943d763cef2SBarry Smith 29443f9fe445SBarry Smith Logically Collective on TS and Vec 2945d763cef2SBarry Smith 2946d763cef2SBarry Smith Input Parameters: 2947d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 29480910c330SBarry Smith - u - the solution vector 2949d763cef2SBarry Smith 2950d763cef2SBarry Smith Level: beginner 2951d763cef2SBarry Smith 2952d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2953d763cef2SBarry Smith @*/ 29540910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2955d763cef2SBarry Smith { 29568737fe31SLisandro Dalcin PetscErrorCode ierr; 2957496e6a7aSJed Brown DM dm; 29588737fe31SLisandro Dalcin 2959d763cef2SBarry Smith PetscFunctionBegin; 29600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 29610910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 29620910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 29636bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 29640910c330SBarry Smith ts->vec_sol = u; 2965bbd56ea5SKarl Rupp 2966496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 29670910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2968d763cef2SBarry Smith PetscFunctionReturn(0); 2969d763cef2SBarry Smith } 2970d763cef2SBarry Smith 297173b18844SBarry Smith /*@ 297273b18844SBarry Smith TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time 297373b18844SBarry Smith 297473b18844SBarry Smith Logically Collective on TS 297573b18844SBarry Smith 297673b18844SBarry Smith Input Parameters: 297773b18844SBarry Smith + ts - the TS context obtained from TSCreate() 297873b18844SBarry Smith . steps - number of steps to use 297973b18844SBarry Smith 298073b18844SBarry Smith Level: intermediate 298173b18844SBarry Smith 298273b18844SBarry Smith Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this 298373b18844SBarry Smith so as to integrate back to less than the original timestep 298473b18844SBarry Smith 298573b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations 298673b18844SBarry Smith 298773b18844SBarry Smith .seealso: TSSetExactFinalTime() 298873b18844SBarry Smith @*/ 298973b18844SBarry Smith PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps) 299073b18844SBarry Smith { 299173b18844SBarry Smith PetscFunctionBegin; 299273b18844SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 299373b18844SBarry Smith PetscValidLogicalCollectiveInt(ts,steps,2); 299473b18844SBarry Smith if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps"); 299573b18844SBarry 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"); 299673b18844SBarry Smith ts->adjoint_max_steps = steps; 299773b18844SBarry Smith PetscFunctionReturn(0); 299873b18844SBarry Smith } 299973b18844SBarry Smith 3000c235aa8dSHong Zhang /*@ 3001dfb21088SHong 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 30020cf82b59SBarry Smith for use by the TSAdjoint routines. 3003c235aa8dSHong Zhang 3004c235aa8dSHong Zhang Logically Collective on TS and Vec 3005c235aa8dSHong Zhang 3006c235aa8dSHong Zhang Input Parameters: 3007c235aa8dSHong Zhang + ts - the TS context obtained from TSCreate() 3008abc2977eSBarry 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 3009abc2977eSBarry Smith - mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters 3010c235aa8dSHong Zhang 3011c235aa8dSHong Zhang Level: beginner 3012c235aa8dSHong Zhang 3013abc2977eSBarry Smith Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime mu_i = df/dp|finaltime 30140cf82b59SBarry Smith 3015c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions 3016c235aa8dSHong Zhang @*/ 3017dfb21088SHong Zhang PetscErrorCode TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu) 3018c235aa8dSHong Zhang { 3019c235aa8dSHong Zhang PetscFunctionBegin; 3020c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3021abc2977eSBarry Smith PetscValidPointer(lambda,2); 3022abc2977eSBarry Smith ts->vecs_sensi = lambda; 3023abc2977eSBarry Smith ts->vecs_sensip = mu; 3024dfb21088SHong 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"); 3025abc2977eSBarry Smith ts->numcost = numcost; 3026ad8e2604SHong Zhang PetscFunctionReturn(0); 3027ad8e2604SHong Zhang } 3028ad8e2604SHong Zhang 3029ad8e2604SHong Zhang /*@C 30300cf82b59SBarry 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. 3031ad8e2604SHong Zhang 3032ad8e2604SHong Zhang Logically Collective on TS 3033ad8e2604SHong Zhang 3034ad8e2604SHong Zhang Input Parameters: 3035ad8e2604SHong Zhang + ts - The TS context obtained from TSCreate() 3036ad8e2604SHong Zhang - func - The function 3037ad8e2604SHong Zhang 3038ad8e2604SHong Zhang Calling sequence of func: 30390cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx); 304005755b9cSHong Zhang + t - current timestep 30410cf82b59SBarry Smith . y - input vector (current ODE solution) 304205755b9cSHong Zhang . A - output matrix 304305755b9cSHong Zhang - ctx - [optional] user-defined function context 3044ad8e2604SHong Zhang 3045ad8e2604SHong Zhang Level: intermediate 3046ad8e2604SHong Zhang 30470cf82b59SBarry 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 30480cf82b59SBarry Smith 3049ad8e2604SHong Zhang .keywords: TS, sensitivity 3050ad8e2604SHong Zhang .seealso: 3051ad8e2604SHong Zhang @*/ 30525bf8c567SBarry Smith PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 3053ad8e2604SHong Zhang { 3054ad8e2604SHong Zhang PetscErrorCode ierr; 3055ad8e2604SHong Zhang 3056ad8e2604SHong Zhang PetscFunctionBegin; 3057ad8e2604SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 305823706b9aSHong Zhang PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 3059ad8e2604SHong Zhang 3060ad8e2604SHong Zhang ts->rhsjacobianp = func; 3061ad8e2604SHong Zhang ts->rhsjacobianpctx = ctx; 3062ad8e2604SHong Zhang if(Amat) { 3063ad8e2604SHong Zhang ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 3064ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 3065ad8e2604SHong Zhang ts->Jacp = Amat; 3066ad8e2604SHong Zhang } 3067ad8e2604SHong Zhang PetscFunctionReturn(0); 3068ad8e2604SHong Zhang } 3069ad8e2604SHong Zhang 30700cf82b59SBarry Smith /*@C 30715bf8c567SBarry Smith TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function. 3072ad8e2604SHong Zhang 3073ad8e2604SHong Zhang Collective on TS 3074ad8e2604SHong Zhang 3075ad8e2604SHong Zhang Input Parameters: 3076ad8e2604SHong Zhang . ts - The TS context obtained from TSCreate() 3077ad8e2604SHong Zhang 3078ad8e2604SHong Zhang Level: developer 3079ad8e2604SHong Zhang 3080ad8e2604SHong Zhang .keywords: TS, sensitivity 30815bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian() 3082ad8e2604SHong Zhang @*/ 30835bf8c567SBarry Smith PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat) 3084ad8e2604SHong Zhang { 3085ad8e2604SHong Zhang PetscErrorCode ierr; 3086ad8e2604SHong Zhang 3087ad8e2604SHong Zhang PetscFunctionBegin; 3088ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3089ad8e2604SHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 3090ad8e2604SHong Zhang PetscValidPointer(Amat,4); 3091ad8e2604SHong Zhang 3092ad8e2604SHong Zhang PetscStackPush("TS user JacobianP function for sensitivity analysis"); 3093ad8e2604SHong Zhang ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 3094ad8e2604SHong Zhang PetscStackPop; 3095c235aa8dSHong Zhang PetscFunctionReturn(0); 3096c235aa8dSHong Zhang } 3097c235aa8dSHong Zhang 30986fd0887fSHong Zhang /*@C 3099dfb21088SHong Zhang TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions 31006fd0887fSHong Zhang 31016fd0887fSHong Zhang Logically Collective on TS 31026fd0887fSHong Zhang 31036fd0887fSHong Zhang Input Parameters: 31046fd0887fSHong Zhang + ts - the TS context obtained from TSCreate() 3105abc2977eSBarry Smith . numcost - number of gradients to be computed, this is the number of cost functions 31063d1d12c6SHong Zhang . costintegral - vector that stores the integral values 31070cf82b59SBarry Smith . rf - routine for evaluating the integrand function 3108b612ec54SBarry Smith . drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y 3109b612ec54SBarry Smith . drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p 3110feaa1ddbSSatish Balay . fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run 3111b612ec54SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 31126fd0887fSHong Zhang 31130cf82b59SBarry Smith Calling sequence of rf: 31143d1d12c6SHong Zhang $ PetscErrorCode rf(TS ts,PetscReal t,Vec y,Vec f,void *ctx); 31156fd0887fSHong Zhang 3116b612ec54SBarry Smith Calling sequence of drdyf: 31170cf82b59SBarry Smith $ PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx); 3118b612ec54SBarry Smith 3119b612ec54SBarry Smith Calling sequence of drdpf: 31200cf82b59SBarry Smith $ PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx); 3121b612ec54SBarry Smith 3122b612ec54SBarry Smith Level: intermediate 31236fd0887fSHong Zhang 3124abc2977eSBarry Smith Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions 31250cf82b59SBarry Smith 31266fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 31276fd0887fSHong Zhang 3128dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients() 31296fd0887fSHong Zhang @*/ 31303d1d12c6SHong Zhang PetscErrorCode TSSetCostIntegrand(TS ts,PetscInt numcost,Vec costintegral,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*), 3131d8151eeaSBarry Smith PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*), 3132b1cb36f3SHong Zhang PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*), 3133b1cb36f3SHong Zhang PetscBool fwd,void *ctx) 31346fd0887fSHong Zhang { 31356fd0887fSHong Zhang PetscErrorCode ierr; 31366fd0887fSHong Zhang 31376fd0887fSHong Zhang PetscFunctionBegin; 31386fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31393d1d12c6SHong Zhang if (costintegral) PetscValidHeaderSpecific(costintegral,VEC_CLASSID,3); 3140dfb21088SHong 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()"); 3141c72ed514SHong Zhang if (!ts->numcost) ts->numcost=numcost; 31426fd0887fSHong Zhang 31433d1d12c6SHong Zhang if (costintegral) { 31443d1d12c6SHong Zhang ierr = PetscObjectReference((PetscObject)costintegral);CHKERRQ(ierr); 31453d1d12c6SHong Zhang ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 31463d1d12c6SHong Zhang ts->vec_costintegral = costintegral; 31473d1d12c6SHong Zhang } else { ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr); /* works for serial only */ } 31483d1d12c6SHong Zhang 3149b1cb36f3SHong Zhang ts->costintegralfwd = fwd; /* Evaluate the cost integral in forward run if fwd is true */ 31502c39e106SBarry Smith ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr); 31510cf82b59SBarry Smith ts->costintegrand = rf; 315205755b9cSHong Zhang ts->costintegrandctx = ctx; 3153b612ec54SBarry Smith ts->drdyfunction = drdyf; 3154b612ec54SBarry Smith ts->drdpfunction = drdpf; 31556fd0887fSHong Zhang PetscFunctionReturn(0); 31566fd0887fSHong Zhang } 31576fd0887fSHong Zhang 315836eaed60SHong Zhang /*@ 3159dfb21088SHong Zhang TSGetCostIntegral - Returns the values of the integral term in the cost functions. 316036eaed60SHong Zhang It is valid to call the routine after a backward run. 316136eaed60SHong Zhang 316236eaed60SHong Zhang Not Collective 316336eaed60SHong Zhang 316436eaed60SHong Zhang Input Parameter: 316536eaed60SHong Zhang . ts - the TS context obtained from TSCreate() 316636eaed60SHong Zhang 316736eaed60SHong Zhang Output Parameter: 31682c39e106SBarry Smith . v - the vector containing the integrals for each cost function 316936eaed60SHong Zhang 317036eaed60SHong Zhang Level: intermediate 317136eaed60SHong Zhang 3172dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 317336eaed60SHong Zhang 317436eaed60SHong Zhang .keywords: TS, sensitivity analysis 317536eaed60SHong Zhang @*/ 3176dfb21088SHong Zhang PetscErrorCode TSGetCostIntegral(TS ts,Vec *v) 317736eaed60SHong Zhang { 317836eaed60SHong Zhang PetscFunctionBegin; 317936eaed60SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 318036eaed60SHong Zhang PetscValidPointer(v,2); 31812c39e106SBarry Smith *v = ts->vec_costintegral; 318236eaed60SHong Zhang PetscFunctionReturn(0); 318336eaed60SHong Zhang } 318436eaed60SHong Zhang 31856fd0887fSHong Zhang /*@ 31862c39e106SBarry Smith TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions. 31876fd0887fSHong Zhang 31886fd0887fSHong Zhang Input Parameters: 31896fd0887fSHong Zhang + ts - the TS context 31906fd0887fSHong Zhang . t - current time 31910cf82b59SBarry Smith - y - state vector, i.e. current solution 31926fd0887fSHong Zhang 31936fd0887fSHong Zhang Output Parameter: 3194abc2977eSBarry Smith . q - vector of size numcost to hold the outputs 31956fd0887fSHong Zhang 31966fd0887fSHong Zhang Note: 31976fd0887fSHong Zhang Most users should not need to explicitly call this routine, as it 31986fd0887fSHong Zhang is used internally within the sensitivity analysis context. 31996fd0887fSHong Zhang 32006fd0887fSHong Zhang Level: developer 32016fd0887fSHong Zhang 32026fd0887fSHong Zhang .keywords: TS, compute 32036fd0887fSHong Zhang 3204dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 32056fd0887fSHong Zhang @*/ 32060cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q) 32076fd0887fSHong Zhang { 32086fd0887fSHong Zhang PetscErrorCode ierr; 32096fd0887fSHong Zhang 32106fd0887fSHong Zhang PetscFunctionBegin; 32116fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 32120cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 32136fd0887fSHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,4); 32146fd0887fSHong Zhang 32150cf82b59SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 3216e4680132SHong Zhang if (ts->costintegrand) { 3217e4680132SHong Zhang PetscStackPush("TS user integrand in the cost function"); 32180cf82b59SBarry Smith ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr); 32196fd0887fSHong Zhang PetscStackPop; 32206fd0887fSHong Zhang } else { 32216fd0887fSHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 32226fd0887fSHong Zhang } 32236fd0887fSHong Zhang 32240cf82b59SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 32256fd0887fSHong Zhang PetscFunctionReturn(0); 32266fd0887fSHong Zhang } 32276fd0887fSHong Zhang 322805755b9cSHong Zhang /*@ 3229d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 323005755b9cSHong Zhang 323105755b9cSHong Zhang Collective on TS 323205755b9cSHong Zhang 323305755b9cSHong Zhang Input Parameters: 323405755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 323505755b9cSHong Zhang 323605755b9cSHong Zhang Notes: 3237d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 323805755b9cSHong Zhang so most users would not generally call this routine themselves. 323905755b9cSHong Zhang 324005755b9cSHong Zhang Level: developer 324105755b9cSHong Zhang 324205755b9cSHong Zhang .keywords: TS, sensitivity 3243d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction() 324405755b9cSHong Zhang @*/ 32450cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy) 324605755b9cSHong Zhang { 324705755b9cSHong Zhang PetscErrorCode ierr; 324805755b9cSHong Zhang 324905755b9cSHong Zhang PetscFunctionBegin; 325005755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 32510cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 325205755b9cSHong Zhang 325305755b9cSHong Zhang PetscStackPush("TS user DRDY function for sensitivity analysis"); 32540cf82b59SBarry Smith ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr); 325505755b9cSHong Zhang PetscStackPop; 325605755b9cSHong Zhang PetscFunctionReturn(0); 325705755b9cSHong Zhang } 325805755b9cSHong Zhang 325905755b9cSHong Zhang /*@ 3260d4aa0a58SBarry Smith TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 326105755b9cSHong Zhang 326205755b9cSHong Zhang Collective on TS 326305755b9cSHong Zhang 326405755b9cSHong Zhang Input Parameters: 326505755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 326605755b9cSHong Zhang 326705755b9cSHong Zhang Notes: 326805755b9cSHong Zhang TSDRDPFunction() is typically used for sensitivity implementation, 326905755b9cSHong Zhang so most users would not generally call this routine themselves. 327005755b9cSHong Zhang 327105755b9cSHong Zhang Level: developer 327205755b9cSHong Zhang 327305755b9cSHong Zhang .keywords: TS, sensitivity 3274d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction() 327505755b9cSHong Zhang @*/ 32760cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp) 327705755b9cSHong Zhang { 327805755b9cSHong Zhang PetscErrorCode ierr; 327905755b9cSHong Zhang 328005755b9cSHong Zhang PetscFunctionBegin; 328105755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 32820cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 328305755b9cSHong Zhang 328405755b9cSHong Zhang PetscStackPush("TS user DRDP function for sensitivity analysis"); 32850cf82b59SBarry Smith ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr); 328605755b9cSHong Zhang PetscStackPop; 328705755b9cSHong Zhang PetscFunctionReturn(0); 328805755b9cSHong Zhang } 328905755b9cSHong Zhang 3290ac226902SBarry Smith /*@C 3291000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 32923f2090d5SJed Brown called once at the beginning of each time step. 3293000e7ae3SMatthew Knepley 32943f9fe445SBarry Smith Logically Collective on TS 3295000e7ae3SMatthew Knepley 3296000e7ae3SMatthew Knepley Input Parameters: 3297000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3298000e7ae3SMatthew Knepley - func - The function 3299000e7ae3SMatthew Knepley 3300000e7ae3SMatthew Knepley Calling sequence of func: 3301000e7ae3SMatthew Knepley . func (TS ts); 3302000e7ae3SMatthew Knepley 3303000e7ae3SMatthew Knepley Level: intermediate 3304000e7ae3SMatthew Knepley 3305000e7ae3SMatthew Knepley .keywords: TS, timestep 33069be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 3307000e7ae3SMatthew Knepley @*/ 33087087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3309000e7ae3SMatthew Knepley { 3310000e7ae3SMatthew Knepley PetscFunctionBegin; 33110700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3312ae60f76fSBarry Smith ts->prestep = func; 3313000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3314000e7ae3SMatthew Knepley } 3315000e7ae3SMatthew Knepley 331609ee8438SJed Brown /*@ 33173f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 33183f2090d5SJed Brown 33193f2090d5SJed Brown Collective on TS 33203f2090d5SJed Brown 33213f2090d5SJed Brown Input Parameters: 33223f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 33233f2090d5SJed Brown 33243f2090d5SJed Brown Notes: 33253f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 33263f2090d5SJed Brown so most users would not generally call this routine themselves. 33273f2090d5SJed Brown 33283f2090d5SJed Brown Level: developer 33293f2090d5SJed Brown 33303f2090d5SJed Brown .keywords: TS, timestep 33319be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 33323f2090d5SJed Brown @*/ 33337087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 33343f2090d5SJed Brown { 33353f2090d5SJed Brown PetscErrorCode ierr; 33363f2090d5SJed Brown 33373f2090d5SJed Brown PetscFunctionBegin; 33380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3339ae60f76fSBarry Smith if (ts->prestep) { 3340ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 3341312ce896SJed Brown } 33423f2090d5SJed Brown PetscFunctionReturn(0); 33433f2090d5SJed Brown } 33443f2090d5SJed Brown 3345b8123daeSJed Brown /*@C 3346b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3347b8123daeSJed Brown called once at the beginning of each stage. 3348b8123daeSJed Brown 3349b8123daeSJed Brown Logically Collective on TS 3350b8123daeSJed Brown 3351b8123daeSJed Brown Input Parameters: 3352b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 3353b8123daeSJed Brown - func - The function 3354b8123daeSJed Brown 3355b8123daeSJed Brown Calling sequence of func: 3356b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 3357b8123daeSJed Brown 3358b8123daeSJed Brown Level: intermediate 3359b8123daeSJed Brown 3360b8123daeSJed Brown Note: 3361b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 3362b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 3363b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 3364b8123daeSJed Brown 3365b8123daeSJed Brown .keywords: TS, timestep 33669be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3367b8123daeSJed Brown @*/ 3368b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 3369b8123daeSJed Brown { 3370b8123daeSJed Brown PetscFunctionBegin; 3371b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3372ae60f76fSBarry Smith ts->prestage = func; 3373b8123daeSJed Brown PetscFunctionReturn(0); 3374b8123daeSJed Brown } 3375b8123daeSJed Brown 33769be3e283SDebojyoti Ghosh /*@C 33779be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 33789be3e283SDebojyoti Ghosh called once at the end of each stage. 33799be3e283SDebojyoti Ghosh 33809be3e283SDebojyoti Ghosh Logically Collective on TS 33819be3e283SDebojyoti Ghosh 33829be3e283SDebojyoti Ghosh Input Parameters: 33839be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 33849be3e283SDebojyoti Ghosh - func - The function 33859be3e283SDebojyoti Ghosh 33869be3e283SDebojyoti Ghosh Calling sequence of func: 33879be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 33889be3e283SDebojyoti Ghosh 33899be3e283SDebojyoti Ghosh Level: intermediate 33909be3e283SDebojyoti Ghosh 33919be3e283SDebojyoti Ghosh Note: 33929be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 33939be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 33949be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 33959be3e283SDebojyoti Ghosh 33969be3e283SDebojyoti Ghosh .keywords: TS, timestep 33979be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 33989be3e283SDebojyoti Ghosh @*/ 33999be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 34009be3e283SDebojyoti Ghosh { 34019be3e283SDebojyoti Ghosh PetscFunctionBegin; 34029be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 34039be3e283SDebojyoti Ghosh ts->poststage = func; 34049be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 34059be3e283SDebojyoti Ghosh } 34069be3e283SDebojyoti Ghosh 3407c688d042SShri Abhyankar /*@C 3408c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 3409c688d042SShri Abhyankar called once at the end of each step evaluation. 3410c688d042SShri Abhyankar 3411c688d042SShri Abhyankar Logically Collective on TS 3412c688d042SShri Abhyankar 3413c688d042SShri Abhyankar Input Parameters: 3414c688d042SShri Abhyankar + ts - The TS context obtained from TSCreate() 3415c688d042SShri Abhyankar - func - The function 3416c688d042SShri Abhyankar 3417c688d042SShri Abhyankar Calling sequence of func: 3418c688d042SShri Abhyankar . PetscErrorCode func(TS ts); 3419c688d042SShri Abhyankar 3420c688d042SShri Abhyankar Level: intermediate 3421c688d042SShri Abhyankar 3422c688d042SShri Abhyankar Note: 34231785ff2aSShri Abhyankar Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling 34241785ff2aSShri Abhyankar thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep() 3425e7e94ed4SShri Abhyankar may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step 3426e7e94ed4SShri Abhyankar solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step 3427e7e94ed4SShri Abhyankar with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime() 3428c688d042SShri Abhyankar 3429c688d042SShri Abhyankar .keywords: TS, timestep 3430c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3431c688d042SShri Abhyankar @*/ 3432c688d042SShri Abhyankar PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS)) 3433c688d042SShri Abhyankar { 3434c688d042SShri Abhyankar PetscFunctionBegin; 3435c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3436c688d042SShri Abhyankar ts->postevaluate = func; 3437c688d042SShri Abhyankar PetscFunctionReturn(0); 3438c688d042SShri Abhyankar } 3439c688d042SShri Abhyankar 3440b8123daeSJed Brown /*@ 3441b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 3442b8123daeSJed Brown 3443b8123daeSJed Brown Collective on TS 3444b8123daeSJed Brown 3445b8123daeSJed Brown Input Parameters: 3446b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 34479be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3448b8123daeSJed Brown 3449b8123daeSJed Brown Notes: 3450b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 3451b8123daeSJed Brown most users would not generally call this routine themselves. 3452b8123daeSJed Brown 3453b8123daeSJed Brown Level: developer 3454b8123daeSJed Brown 3455b8123daeSJed Brown .keywords: TS, timestep 34569be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 3457b8123daeSJed Brown @*/ 3458b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3459b8123daeSJed Brown { 3460b8123daeSJed Brown PetscErrorCode ierr; 3461b8123daeSJed Brown 3462b8123daeSJed Brown PetscFunctionBegin; 3463b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3464ae60f76fSBarry Smith if (ts->prestage) { 3465ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 3466b8123daeSJed Brown } 3467b8123daeSJed Brown PetscFunctionReturn(0); 3468b8123daeSJed Brown } 3469b8123daeSJed Brown 34709be3e283SDebojyoti Ghosh /*@ 34719be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 34729be3e283SDebojyoti Ghosh 34739be3e283SDebojyoti Ghosh Collective on TS 34749be3e283SDebojyoti Ghosh 34759be3e283SDebojyoti Ghosh Input Parameters: 34769be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 34779be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 34789be3e283SDebojyoti Ghosh stageindex - Stage number 34799be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 34809be3e283SDebojyoti Ghosh of stages) with the stage solutions 34819be3e283SDebojyoti Ghosh 34829be3e283SDebojyoti Ghosh Notes: 34839be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 34849be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 34859be3e283SDebojyoti Ghosh 34869be3e283SDebojyoti Ghosh Level: developer 34879be3e283SDebojyoti Ghosh 34889be3e283SDebojyoti Ghosh .keywords: TS, timestep 34899be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 34909be3e283SDebojyoti Ghosh @*/ 34919be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 34929be3e283SDebojyoti Ghosh { 34939be3e283SDebojyoti Ghosh PetscErrorCode ierr; 34949be3e283SDebojyoti Ghosh 34959be3e283SDebojyoti Ghosh PetscFunctionBegin; 34969be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 34974beae5d8SLisandro Dalcin if (ts->poststage) { 34989be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 34999be3e283SDebojyoti Ghosh } 35009be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 35019be3e283SDebojyoti Ghosh } 35029be3e283SDebojyoti Ghosh 3503c688d042SShri Abhyankar /*@ 3504c688d042SShri Abhyankar TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate() 3505c688d042SShri Abhyankar 3506c688d042SShri Abhyankar Collective on TS 3507c688d042SShri Abhyankar 3508c688d042SShri Abhyankar Input Parameters: 3509c688d042SShri Abhyankar . ts - The TS context obtained from TSCreate() 3510c688d042SShri Abhyankar 3511c688d042SShri Abhyankar Notes: 3512c688d042SShri Abhyankar TSPostEvaluate() is typically used within time stepping implementations, 3513c688d042SShri Abhyankar most users would not generally call this routine themselves. 3514c688d042SShri Abhyankar 3515c688d042SShri Abhyankar Level: developer 3516c688d042SShri Abhyankar 3517c688d042SShri Abhyankar .keywords: TS, timestep 3518c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep() 3519c688d042SShri Abhyankar @*/ 3520c688d042SShri Abhyankar PetscErrorCode TSPostEvaluate(TS ts) 3521c688d042SShri Abhyankar { 3522c688d042SShri Abhyankar PetscErrorCode ierr; 3523c688d042SShri Abhyankar 3524c688d042SShri Abhyankar PetscFunctionBegin; 3525c688d042SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3526c688d042SShri Abhyankar if (ts->postevaluate) { 3527c688d042SShri Abhyankar PetscStackCallStandard((*ts->postevaluate),(ts)); 3528c688d042SShri Abhyankar } 3529c688d042SShri Abhyankar PetscFunctionReturn(0); 3530c688d042SShri Abhyankar } 3531c688d042SShri Abhyankar 3532ac226902SBarry Smith /*@C 3533000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 35343f2090d5SJed Brown called once at the end of each time step. 3535000e7ae3SMatthew Knepley 35363f9fe445SBarry Smith Logically Collective on TS 3537000e7ae3SMatthew Knepley 3538000e7ae3SMatthew Knepley Input Parameters: 3539000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3540000e7ae3SMatthew Knepley - func - The function 3541000e7ae3SMatthew Knepley 3542000e7ae3SMatthew Knepley Calling sequence of func: 3543b8123daeSJed Brown $ func (TS ts); 3544000e7ae3SMatthew Knepley 35451785ff2aSShri Abhyankar Notes: 35461785ff2aSShri Abhyankar The function set by TSSetPostStep() is called after each successful step. The solution vector X 35471785ff2aSShri Abhyankar obtained by TSGetSolution() may be different than that computed at the step end if the event handler 35481785ff2aSShri Abhyankar locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead. 35491785ff2aSShri Abhyankar 3550000e7ae3SMatthew Knepley Level: intermediate 3551000e7ae3SMatthew Knepley 3552000e7ae3SMatthew Knepley .keywords: TS, timestep 35531785ff2aSShri Abhyankar .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 3554000e7ae3SMatthew Knepley @*/ 35557087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 3556000e7ae3SMatthew Knepley { 3557000e7ae3SMatthew Knepley PetscFunctionBegin; 35580700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3559ae60f76fSBarry Smith ts->poststep = func; 3560000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3561000e7ae3SMatthew Knepley } 3562000e7ae3SMatthew Knepley 356309ee8438SJed Brown /*@ 35643f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 35653f2090d5SJed Brown 35663f2090d5SJed Brown Collective on TS 35673f2090d5SJed Brown 35683f2090d5SJed Brown Input Parameters: 35693f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 35703f2090d5SJed Brown 35713f2090d5SJed Brown Notes: 35723f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 35733f2090d5SJed Brown so most users would not generally call this routine themselves. 35743f2090d5SJed Brown 35753f2090d5SJed Brown Level: developer 35763f2090d5SJed Brown 35773f2090d5SJed Brown .keywords: TS, timestep 35783f2090d5SJed Brown @*/ 35797087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 35803f2090d5SJed Brown { 35813f2090d5SJed Brown PetscErrorCode ierr; 35823f2090d5SJed Brown 35833f2090d5SJed Brown PetscFunctionBegin; 35840700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3585ae60f76fSBarry Smith if (ts->poststep) { 3586ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 358772ac3e02SJed Brown } 35883f2090d5SJed Brown PetscFunctionReturn(0); 35893f2090d5SJed Brown } 35903f2090d5SJed Brown 3591d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 3592d763cef2SBarry Smith 3593d763cef2SBarry Smith /*@C 3594a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 3595d763cef2SBarry Smith timestep to display the iteration's progress. 3596d763cef2SBarry Smith 35973f9fe445SBarry Smith Logically Collective on TS 3598d763cef2SBarry Smith 3599d763cef2SBarry Smith Input Parameters: 3600d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3601e213d8f1SJed Brown . monitor - monitoring routine 3602329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 36030298fd71SBarry Smith monitor routine (use NULL if no context is desired) 3604b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 36050298fd71SBarry Smith (may be NULL) 3606d763cef2SBarry Smith 3607e213d8f1SJed Brown Calling sequence of monitor: 36080910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 3609d763cef2SBarry Smith 3610d763cef2SBarry Smith + ts - the TS context 361163e21af5SBarry 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) 36121f06c33eSBarry Smith . time - current time 36130910c330SBarry Smith . u - current iterate 3614d763cef2SBarry Smith - mctx - [optional] monitoring context 3615d763cef2SBarry Smith 3616d763cef2SBarry Smith Notes: 3617d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 3618d763cef2SBarry Smith already has been loaded. 3619d763cef2SBarry Smith 3620025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 3621025f1a04SBarry Smith 3622d763cef2SBarry Smith Level: intermediate 3623d763cef2SBarry Smith 3624d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3625d763cef2SBarry Smith 3626a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 3627d763cef2SBarry Smith @*/ 3628c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 3629d763cef2SBarry Smith { 363078064530SBarry Smith PetscErrorCode ierr; 363178064530SBarry Smith PetscInt i; 363278064530SBarry Smith PetscBool identical; 363378064530SBarry Smith 3634d763cef2SBarry Smith PetscFunctionBegin; 36350700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 363678064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 363778064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr); 363878064530SBarry Smith if (identical) PetscFunctionReturn(0); 363978064530SBarry Smith } 364017186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 3641d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 36428704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 3643d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 3644d763cef2SBarry Smith PetscFunctionReturn(0); 3645d763cef2SBarry Smith } 3646d763cef2SBarry Smith 3647d763cef2SBarry Smith /*@C 3648a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 3649d763cef2SBarry Smith 36503f9fe445SBarry Smith Logically Collective on TS 3651d763cef2SBarry Smith 3652d763cef2SBarry Smith Input Parameters: 3653d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3654d763cef2SBarry Smith 3655d763cef2SBarry Smith Notes: 3656d763cef2SBarry Smith There is no way to remove a single, specific monitor. 3657d763cef2SBarry Smith 3658d763cef2SBarry Smith Level: intermediate 3659d763cef2SBarry Smith 3660d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3661d763cef2SBarry Smith 3662a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 3663d763cef2SBarry Smith @*/ 36647087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 3665d763cef2SBarry Smith { 3666d952e501SBarry Smith PetscErrorCode ierr; 3667d952e501SBarry Smith PetscInt i; 3668d952e501SBarry Smith 3669d763cef2SBarry Smith PetscFunctionBegin; 36700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3671d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 36728704b422SBarry Smith if (ts->monitordestroy[i]) { 36738704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 3674d952e501SBarry Smith } 3675d952e501SBarry Smith } 3676d763cef2SBarry Smith ts->numbermonitors = 0; 3677d763cef2SBarry Smith PetscFunctionReturn(0); 3678d763cef2SBarry Smith } 3679d763cef2SBarry Smith 3680721cd6eeSBarry Smith /*@C 3681721cd6eeSBarry Smith TSMonitorDefault - The Default monitor, prints the timestep and time for each step 36825516499fSSatish Balay 36835516499fSSatish Balay Level: intermediate 368441251cbbSSatish Balay 36855516499fSSatish Balay .keywords: TS, set, monitor 36865516499fSSatish Balay 368763e21af5SBarry Smith .seealso: TSMonitorSet() 368841251cbbSSatish Balay @*/ 3689721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf) 3690d763cef2SBarry Smith { 3691dfbe8321SBarry Smith PetscErrorCode ierr; 3692721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 369341aca3d6SBarry Smith PetscBool iascii,ibinary; 3694d132466eSBarry Smith 3695d763cef2SBarry Smith PetscFunctionBegin; 36964d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 369741aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 369841aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr); 3699721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 370041aca3d6SBarry Smith if (iascii) { 3701649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 370263e21af5SBarry Smith if (step == -1){ /* this indicates it is an interpolated solution */ 370363e21af5SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr); 370463e21af5SBarry Smith } else { 37058392e04aSShri 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); 370663e21af5SBarry Smith } 3707649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 370841aca3d6SBarry Smith } else if (ibinary) { 370941aca3d6SBarry Smith PetscMPIInt rank; 371041aca3d6SBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 371141aca3d6SBarry Smith if (!rank) { 3712450a797fSBarry Smith PetscBool skipHeader; 3713450a797fSBarry Smith PetscInt classid = REAL_FILE_CLASSID; 3714450a797fSBarry Smith 3715450a797fSBarry Smith ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr); 3716450a797fSBarry Smith if (!skipHeader) { 3717450a797fSBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 3718450a797fSBarry Smith } 371941aca3d6SBarry Smith ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr); 372041aca3d6SBarry Smith } else { 372141aca3d6SBarry Smith ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr); 372241aca3d6SBarry Smith } 372341aca3d6SBarry Smith } 3724721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3725d763cef2SBarry Smith PetscFunctionReturn(0); 3726d763cef2SBarry Smith } 3727d763cef2SBarry Smith 37289110b2e7SHong Zhang /*@C 37299110b2e7SHong Zhang TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every 37309110b2e7SHong Zhang timestep to display the iteration's progress. 37319110b2e7SHong Zhang 37329110b2e7SHong Zhang Logically Collective on TS 37339110b2e7SHong Zhang 37349110b2e7SHong Zhang Input Parameters: 37359110b2e7SHong Zhang + ts - the TS context obtained from TSCreate() 37369110b2e7SHong Zhang . adjointmonitor - monitoring routine 37379110b2e7SHong Zhang . adjointmctx - [optional] user-defined context for private data for the 37389110b2e7SHong Zhang monitor routine (use NULL if no context is desired) 37399110b2e7SHong Zhang - adjointmonitordestroy - [optional] routine that frees monitor context 37409110b2e7SHong Zhang (may be NULL) 37419110b2e7SHong Zhang 37429110b2e7SHong Zhang Calling sequence of monitor: 37439110b2e7SHong Zhang $ int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx) 37449110b2e7SHong Zhang 37459110b2e7SHong Zhang + ts - the TS context 37469110b2e7SHong 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 37479110b2e7SHong Zhang been interpolated to) 37489110b2e7SHong Zhang . time - current time 37499110b2e7SHong Zhang . u - current iterate 37509110b2e7SHong Zhang . numcost - number of cost functionos 37519110b2e7SHong Zhang . lambda - sensitivities to initial conditions 37529110b2e7SHong Zhang . mu - sensitivities to parameters 37539110b2e7SHong Zhang - adjointmctx - [optional] adjoint monitoring context 37549110b2e7SHong Zhang 37559110b2e7SHong Zhang Notes: 37569110b2e7SHong Zhang This routine adds an additional monitor to the list of monitors that 37579110b2e7SHong Zhang already has been loaded. 37589110b2e7SHong Zhang 37599110b2e7SHong Zhang Fortran notes: Only a single monitor function can be set for each TS object 37609110b2e7SHong Zhang 37619110b2e7SHong Zhang Level: intermediate 37629110b2e7SHong Zhang 37639110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 37649110b2e7SHong Zhang 37659110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel() 37669110b2e7SHong Zhang @*/ 37679110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**)) 37689110b2e7SHong Zhang { 376978064530SBarry Smith PetscErrorCode ierr; 377078064530SBarry Smith PetscInt i; 377178064530SBarry Smith PetscBool identical; 377278064530SBarry Smith 37739110b2e7SHong Zhang PetscFunctionBegin; 37749110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 377578064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 377678064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);CHKERRQ(ierr); 377778064530SBarry Smith if (identical) PetscFunctionReturn(0); 377878064530SBarry Smith } 37799110b2e7SHong Zhang if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set"); 37809110b2e7SHong Zhang ts->adjointmonitor[ts->numberadjointmonitors] = adjointmonitor; 37819110b2e7SHong Zhang ts->adjointmonitordestroy[ts->numberadjointmonitors] = adjointmdestroy; 37829110b2e7SHong Zhang ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx; 37839110b2e7SHong Zhang PetscFunctionReturn(0); 37849110b2e7SHong Zhang } 37859110b2e7SHong Zhang 37869110b2e7SHong Zhang /*@C 37879110b2e7SHong Zhang TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object. 37889110b2e7SHong Zhang 37899110b2e7SHong Zhang Logically Collective on TS 37909110b2e7SHong Zhang 37919110b2e7SHong Zhang Input Parameters: 37929110b2e7SHong Zhang . ts - the TS context obtained from TSCreate() 37939110b2e7SHong Zhang 37949110b2e7SHong Zhang Notes: 37959110b2e7SHong Zhang There is no way to remove a single, specific monitor. 37969110b2e7SHong Zhang 37979110b2e7SHong Zhang Level: intermediate 37989110b2e7SHong Zhang 37999110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 38009110b2e7SHong Zhang 38019110b2e7SHong Zhang .seealso: TSAdjointMonitorSet() 38029110b2e7SHong Zhang @*/ 38039110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorCancel(TS ts) 38049110b2e7SHong Zhang { 38059110b2e7SHong Zhang PetscErrorCode ierr; 38069110b2e7SHong Zhang PetscInt i; 38079110b2e7SHong Zhang 38089110b2e7SHong Zhang PetscFunctionBegin; 38099110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 38109110b2e7SHong Zhang for (i=0; i<ts->numberadjointmonitors; i++) { 38119110b2e7SHong Zhang if (ts->adjointmonitordestroy[i]) { 38129110b2e7SHong Zhang ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 38139110b2e7SHong Zhang } 38149110b2e7SHong Zhang } 38159110b2e7SHong Zhang ts->numberadjointmonitors = 0; 38169110b2e7SHong Zhang PetscFunctionReturn(0); 38179110b2e7SHong Zhang } 38189110b2e7SHong Zhang 3819721cd6eeSBarry Smith /*@C 3820721cd6eeSBarry Smith TSAdjointMonitorDefault - the default monitor of adjoint computations 3821110eb670SHong Zhang 3822110eb670SHong Zhang Level: intermediate 3823110eb670SHong Zhang 3824110eb670SHong Zhang .keywords: TS, set, monitor 3825110eb670SHong Zhang 3826110eb670SHong Zhang .seealso: TSAdjointMonitorSet() 3827110eb670SHong Zhang @*/ 3828721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf) 3829110eb670SHong Zhang { 3830110eb670SHong Zhang PetscErrorCode ierr; 3831721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 3832110eb670SHong Zhang 3833110eb670SHong Zhang PetscFunctionBegin; 3834110eb670SHong Zhang PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3835721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 3836110eb670SHong Zhang ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3837110eb670SHong 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); 3838110eb670SHong Zhang ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3839721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3840110eb670SHong Zhang PetscFunctionReturn(0); 3841110eb670SHong Zhang } 3842110eb670SHong Zhang 3843cd652676SJed Brown /*@ 3844cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3845cd652676SJed Brown 3846cd652676SJed Brown Collective on TS 3847cd652676SJed Brown 3848cd652676SJed Brown Input Argument: 3849cd652676SJed Brown + ts - time stepping context 3850cd652676SJed Brown - t - time to interpolate to 3851cd652676SJed Brown 3852cd652676SJed Brown Output Argument: 38530910c330SBarry Smith . U - state at given time 3854cd652676SJed Brown 3855cd652676SJed Brown Level: intermediate 3856cd652676SJed Brown 3857cd652676SJed Brown Developer Notes: 3858cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3859cd652676SJed Brown 3860cd652676SJed Brown .keywords: TS, set 3861cd652676SJed Brown 3862874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve() 3863cd652676SJed Brown @*/ 38640910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3865cd652676SJed Brown { 3866cd652676SJed Brown PetscErrorCode ierr; 3867cd652676SJed Brown 3868cd652676SJed Brown PetscFunctionBegin; 3869cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3870b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3871be5899b3SLisandro 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); 3872ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 38730910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3874cd652676SJed Brown PetscFunctionReturn(0); 3875cd652676SJed Brown } 3876cd652676SJed Brown 3877d763cef2SBarry Smith /*@ 38786d9e5789SSean Farley TSStep - Steps one time step 3879d763cef2SBarry Smith 3880d763cef2SBarry Smith Collective on TS 3881d763cef2SBarry Smith 3882d763cef2SBarry Smith Input Parameter: 3883d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3884d763cef2SBarry Smith 388527829d71SBarry Smith Level: developer 3886d763cef2SBarry Smith 3887b8123daeSJed Brown Notes: 388827829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 388927829d71SBarry Smith 3890b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3891b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3892b8123daeSJed Brown 389325cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 389425cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 389525cb2221SBarry Smith 3896d763cef2SBarry Smith .keywords: TS, timestep, solve 3897d763cef2SBarry Smith 38989be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3899d763cef2SBarry Smith @*/ 3900193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3901d763cef2SBarry Smith { 3902dfbe8321SBarry Smith PetscErrorCode ierr; 3903fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3904be5899b3SLisandro Dalcin PetscReal ptime; 3905d763cef2SBarry Smith 3906d763cef2SBarry Smith PetscFunctionBegin; 39070700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3908fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3909fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3910fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3911fffbeea8SBarry Smith " type = {Preprint},\n" 3912fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3913fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3914302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3915fffbeea8SBarry Smith 3916d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 391768bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3918d405a339SMatthew Knepley 3919a6772fa2SLisandro 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()"); 3920be5899b3SLisandro 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"); 3921a6772fa2SLisandro Dalcin 3922be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 3923362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3924be5899b3SLisandro Dalcin ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev; 3925e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3926d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3927193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3928d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3929be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 3930be5899b3SLisandro Dalcin ts->steps++; ts->total_steps++; 3931be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 393228d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3933362cd11cSLisandro Dalcin 3934362cd11cSLisandro Dalcin if (ts->reason < 0) { 3935cef5090cSJed Brown if (ts->errorifstepfailed) { 393608c7845fSBarry 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]); 393708c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3938d2daff3dSHong Zhang } 393908c7845fSBarry Smith } else if (!ts->reason) { 394008c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 394108c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 394208c7845fSBarry Smith } 394308c7845fSBarry Smith PetscFunctionReturn(0); 394408c7845fSBarry Smith } 394508c7845fSBarry Smith 394608c7845fSBarry Smith /*@ 3947b957a604SHong Zhang TSAdjointStep - Steps one time step backward in the adjoint run 394808c7845fSBarry Smith 394908c7845fSBarry Smith Collective on TS 395008c7845fSBarry Smith 395108c7845fSBarry Smith Input Parameter: 395208c7845fSBarry Smith . ts - the TS context obtained from TSCreate() 395308c7845fSBarry Smith 39546a4d4014SLisandro Dalcin Level: intermediate 39556a4d4014SLisandro Dalcin 3956b957a604SHong Zhang .keywords: TS, adjoint, step 39576a4d4014SLisandro Dalcin 3958b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve() 39596a4d4014SLisandro Dalcin @*/ 396008c7845fSBarry Smith PetscErrorCode TSAdjointStep(TS ts) 39616a4d4014SLisandro Dalcin { 396208c7845fSBarry Smith DM dm; 39636a4d4014SLisandro Dalcin PetscErrorCode ierr; 39646a4d4014SLisandro Dalcin 39656a4d4014SLisandro Dalcin PetscFunctionBegin; 39664a2ae208SSatish Balay PetscValidHeaderSpecific(ts,TS_CLASSID,1); 396708c7845fSBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 396808c7845fSBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3969d763cef2SBarry Smith 3970685405a1SBarry Smith ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr); 3971d763cef2SBarry Smith 3972be5899b3SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3973be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime; 397442f2b339SBarry 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); 3975be5899b3SLisandro Dalcin ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 397642f2b339SBarry Smith ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr); 39778d0ad7a8SHong Zhang ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 3978be5899b3SLisandro Dalcin ts->steps++; ts->total_steps--; 3979362cd11cSLisandro Dalcin 3980362cd11cSLisandro Dalcin if (ts->reason < 0) { 3981cef5090cSJed Brown if (ts->errorifstepfailed) { 39826c4ed002SBarry 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]); 39836c4ed002SBarry 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]); 39846c4ed002SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3985cef5090cSJed Brown } 3986362cd11cSLisandro Dalcin } else if (!ts->reason) { 398773b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3988362cd11cSLisandro Dalcin } 3989d763cef2SBarry Smith PetscFunctionReturn(0); 3990d763cef2SBarry Smith } 3991d763cef2SBarry Smith 39927cbde773SLisandro Dalcin /*@ 39937cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 39947cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 39957cbde773SLisandro Dalcin 39967cbde773SLisandro Dalcin Collective on TS 39977cbde773SLisandro Dalcin 39987cbde773SLisandro Dalcin Input Arguments: 39997cbde773SLisandro Dalcin + ts - time stepping context 40007cbde773SLisandro Dalcin . wnormtype - norm type, either NORM_2 or NORM_INFINITY 40017cbde773SLisandro Dalcin - order - optional, desired order for the error evaluation or PETSC_DECIDE 40027cbde773SLisandro Dalcin 40037cbde773SLisandro Dalcin Output Arguments: 40047cbde773SLisandro Dalcin + order - optional, the actual order of the error evaluation 40057cbde773SLisandro Dalcin - wlte - the weighted local truncation error norm 40067cbde773SLisandro Dalcin 40077cbde773SLisandro Dalcin Level: advanced 40087cbde773SLisandro Dalcin 40097cbde773SLisandro Dalcin Notes: 40107cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 40117cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 40127cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 40137cbde773SLisandro Dalcin 40147cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm() 40157cbde773SLisandro Dalcin @*/ 40167cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte) 40177cbde773SLisandro Dalcin { 40187cbde773SLisandro Dalcin PetscErrorCode ierr; 40197cbde773SLisandro Dalcin 40207cbde773SLisandro Dalcin PetscFunctionBegin; 40217cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 40227cbde773SLisandro Dalcin PetscValidType(ts,1); 40237cbde773SLisandro Dalcin PetscValidLogicalCollectiveEnum(ts,wnormtype,4); 40247cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order,3); 40257cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts,*order,3); 40267cbde773SLisandro Dalcin PetscValidRealPointer(wlte,4); 40277cbde773SLisandro Dalcin if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 40287cbde773SLisandro Dalcin if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name); 40297cbde773SLisandro Dalcin ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr); 40307cbde773SLisandro Dalcin PetscFunctionReturn(0); 40317cbde773SLisandro Dalcin } 40327cbde773SLisandro Dalcin 403305175c85SJed Brown /*@ 403405175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 403505175c85SJed Brown 40361c3436cfSJed Brown Collective on TS 403705175c85SJed Brown 403805175c85SJed Brown Input Arguments: 40391c3436cfSJed Brown + ts - time stepping context 40401c3436cfSJed Brown . order - desired order of accuracy 40410298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 404205175c85SJed Brown 404305175c85SJed Brown Output Arguments: 40440910c330SBarry Smith . U - state at the end of the current step 404505175c85SJed Brown 404605175c85SJed Brown Level: advanced 404705175c85SJed Brown 4048108c343cSJed Brown Notes: 4049108c343cSJed Brown This function cannot be called until all stages have been evaluated. 4050108c343cSJed 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. 4051108c343cSJed Brown 40521c3436cfSJed Brown .seealso: TSStep(), TSAdapt 405305175c85SJed Brown @*/ 40540910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 405505175c85SJed Brown { 405605175c85SJed Brown PetscErrorCode ierr; 405705175c85SJed Brown 405805175c85SJed Brown PetscFunctionBegin; 405905175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 406005175c85SJed Brown PetscValidType(ts,1); 40610910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 4062ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 40630910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 406405175c85SJed Brown PetscFunctionReturn(0); 406505175c85SJed Brown } 406605175c85SJed Brown 4067b1cb36f3SHong Zhang /*@ 4068b1cb36f3SHong Zhang TSForwardCostIntegral - Evaluate the cost integral in the forward run. 4069b1cb36f3SHong Zhang 4070b1cb36f3SHong Zhang Collective on TS 4071b1cb36f3SHong Zhang 4072b1cb36f3SHong Zhang Input Arguments: 4073b1cb36f3SHong Zhang . ts - time stepping context 4074b1cb36f3SHong Zhang 4075b1cb36f3SHong Zhang Level: advanced 4076b1cb36f3SHong Zhang 4077b1cb36f3SHong Zhang Notes: 4078b1cb36f3SHong Zhang This function cannot be called until TSStep() has been completed. 4079b1cb36f3SHong Zhang 4080b1cb36f3SHong Zhang .seealso: TSSolve(), TSAdjointCostIntegral() 4081b1cb36f3SHong Zhang @*/ 4082b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts) 4083b1cb36f3SHong Zhang { 4084b1cb36f3SHong Zhang PetscErrorCode ierr; 4085b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4086b1cb36f3SHong 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); 4087b1cb36f3SHong Zhang ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr); 4088b1cb36f3SHong Zhang PetscFunctionReturn(0); 4089b1cb36f3SHong Zhang } 4090d67e68b7SBarry Smith 4091d763cef2SBarry Smith /*@ 4092d763cef2SBarry Smith TSSolve - Steps the requested number of timesteps. 4093d763cef2SBarry Smith 4094d763cef2SBarry Smith Collective on TS 4095d763cef2SBarry Smith 4096d763cef2SBarry Smith Input Parameter: 4097d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 409863e21af5SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used, 409963e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 41005a3a76d0SJed Brown 4101d763cef2SBarry Smith Level: beginner 4102d763cef2SBarry Smith 41035a3a76d0SJed Brown Notes: 41045a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 41055a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 41065a3a76d0SJed Brown stepped over the final time. 41075a3a76d0SJed Brown 4108d763cef2SBarry Smith .keywords: TS, timestep, solve 4109d763cef2SBarry Smith 411063e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime() 4111d763cef2SBarry Smith @*/ 4112cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 4113d763cef2SBarry Smith { 4114b06615a5SLisandro Dalcin Vec solution; 4115d763cef2SBarry Smith PetscErrorCode ierr; 4116d763cef2SBarry Smith 4117d763cef2SBarry Smith PetscFunctionBegin; 4118d763cef2SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4119f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 4120feed9e9dSBarry Smith 412149354f04SShri 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 */ 4122b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 41230910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 4124b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 4125b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 4126b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 41275a3a76d0SJed Brown } 41280910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 4129bbd56ea5SKarl Rupp } else if (u) { 41300910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 41315a3a76d0SJed Brown } 4132b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 413368bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 4134a6772fa2SLisandro Dalcin 4135a6772fa2SLisandro 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()"); 4136a6772fa2SLisandro 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"); 4137a6772fa2SLisandro Dalcin 4138e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 4139e7069c78SShri restarts the step after an event. Resetting these counters in such a case causes 4140e7069c78SShri TSTrajectory to incorrectly save the output files 4141e7069c78SShri */ 4142938c94caSShri Abhyankar 4143193ac0bcSJed Brown ts->steps = 0; 41445ef26d82SJed Brown ts->ksp_its = 0; 41455ef26d82SJed Brown ts->snes_its = 0; 4146c610991cSLisandro Dalcin ts->num_snes_failures = 0; 4147c610991cSLisandro Dalcin ts->reject = 0; 4148193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 4149193ac0bcSJed Brown 4150ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 4151f05ece33SBarry Smith 4152193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 4153193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 4154a6772fa2SLisandro Dalcin if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 4155cc708dedSBarry Smith ts->solvetime = ts->ptime; 4156a6772fa2SLisandro Dalcin solution = ts->vec_sol; 4157be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 4158db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 4159db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 4160e7069c78SShri 4161938c94caSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 41626427ac75SLisandro Dalcin ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 4163e7069c78SShri 416428d5b5d6SLisandro Dalcin ts->steprollback = PETSC_FALSE; 416528d5b5d6SLisandro Dalcin ts->steprestart = PETSC_TRUE; 41666427ac75SLisandro Dalcin 4167e1a7a14fSJed Brown while (!ts->reason) { 4168938c94caSShri Abhyankar ierr = TSMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 41699687d888SLisandro Dalcin if (!ts->steprollback) { 41709687d888SLisandro Dalcin ierr = TSPreStep(ts);CHKERRQ(ierr); 41719687d888SLisandro Dalcin } 4172193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 4173e783b05fSHong 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. */ 4174b1cb36f3SHong Zhang ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr); 4175b1cb36f3SHong Zhang } 417610b82f12SShri Abhyankar ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 4177e783b05fSHong 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. */ 4178e783b05fSHong Zhang if (!ts->steprollback) { 4179938c94caSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 41801eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 4181aeb4809dSShri Abhyankar } 4182193ac0bcSJed Brown } 4183938c94caSShri Abhyankar ierr = TSMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 41846427ac75SLisandro Dalcin 418549354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 41860910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 4187cc708dedSBarry Smith ts->solvetime = ts->max_time; 4188b06615a5SLisandro Dalcin solution = u; 418963e21af5SBarry Smith ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr); 41900574a7fbSJed Brown } else { 4191ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 4192cc708dedSBarry Smith ts->solvetime = ts->ptime; 4193b06615a5SLisandro Dalcin solution = ts->vec_sol; 41940574a7fbSJed Brown } 4195193ac0bcSJed Brown } 4196d2daff3dSHong Zhang 4197ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 419863e21af5SBarry Smith ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr); 419956f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 4200ef222394SBarry Smith if (ts->adjoint_solve) { 4201ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 42022b0a91c0SBarry Smith } 4203d763cef2SBarry Smith PetscFunctionReturn(0); 4204d763cef2SBarry Smith } 4205d763cef2SBarry Smith 4206b1cb36f3SHong Zhang /*@ 4207b1cb36f3SHong Zhang TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run. 4208b1cb36f3SHong Zhang 4209b1cb36f3SHong Zhang Collective on TS 4210b1cb36f3SHong Zhang 4211b1cb36f3SHong Zhang Input Arguments: 4212b1cb36f3SHong Zhang . ts - time stepping context 4213b1cb36f3SHong Zhang 4214b1cb36f3SHong Zhang Level: advanced 4215b1cb36f3SHong Zhang 4216b1cb36f3SHong Zhang Notes: 4217b1cb36f3SHong Zhang This function cannot be called until TSAdjointStep() has been completed. 4218b1cb36f3SHong Zhang 4219b1cb36f3SHong Zhang .seealso: TSAdjointSolve(), TSAdjointStep 4220b1cb36f3SHong Zhang @*/ 4221b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts) 4222b1cb36f3SHong Zhang { 4223b1cb36f3SHong Zhang PetscErrorCode ierr; 4224b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4225b1cb36f3SHong 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); 4226b1cb36f3SHong Zhang ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr); 4227b1cb36f3SHong Zhang PetscFunctionReturn(0); 4228b1cb36f3SHong Zhang } 4229b1cb36f3SHong Zhang 4230c88f2d44SBarry Smith /*@ 4231c88f2d44SBarry Smith TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 4232c88f2d44SBarry Smith 4233c88f2d44SBarry Smith Collective on TS 4234c88f2d44SBarry Smith 4235c88f2d44SBarry Smith Input Parameter: 42362c39e106SBarry Smith . ts - the TS context obtained from TSCreate() 4237c88f2d44SBarry Smith 4238e87fd094SBarry Smith Options Database: 4239e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions 4240e87fd094SBarry Smith 4241c88f2d44SBarry Smith Level: intermediate 4242c88f2d44SBarry Smith 4243c88f2d44SBarry Smith Notes: 4244c88f2d44SBarry Smith This must be called after a call to TSSolve() that solves the forward problem 4245c88f2d44SBarry Smith 424673b18844SBarry Smith By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time 424773b18844SBarry Smith 4248c88f2d44SBarry Smith .keywords: TS, timestep, solve 4249c88f2d44SBarry Smith 4250b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep() 4251c88f2d44SBarry Smith @*/ 42522c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts) 4253c88f2d44SBarry Smith { 4254c88f2d44SBarry Smith PetscErrorCode ierr; 4255c88f2d44SBarry Smith 4256c88f2d44SBarry Smith PetscFunctionBegin; 4257c88f2d44SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4258c88f2d44SBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 425968bece0bSHong Zhang 4260c88f2d44SBarry Smith /* reset time step and iteration counters */ 4261c88f2d44SBarry Smith ts->steps = 0; 4262c88f2d44SBarry Smith ts->ksp_its = 0; 4263c88f2d44SBarry Smith ts->snes_its = 0; 4264c88f2d44SBarry Smith ts->num_snes_failures = 0; 4265c88f2d44SBarry Smith ts->reject = 0; 4266c88f2d44SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 4267c88f2d44SBarry Smith 426873b18844SBarry Smith if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps; 4269c88f2d44SBarry Smith 427073b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 4271c88f2d44SBarry Smith while (!ts->reason) { 427255c52731SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 42739110b2e7SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 42746427ac75SLisandro Dalcin ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr); 4275616946c1SHong Zhang ierr = TSAdjointStep(ts);CHKERRQ(ierr); 4276b1cb36f3SHong Zhang if (ts->vec_costintegral && !ts->costintegralfwd) { 4277b1cb36f3SHong Zhang ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr); 4278b1cb36f3SHong Zhang } 4279c88f2d44SBarry Smith } 428026656371SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 428126656371SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 4282c88f2d44SBarry Smith ts->solvetime = ts->ptime; 4283e210cd0eSHong Zhang ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr); 4284685405a1SBarry Smith ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr); 4285c88f2d44SBarry Smith PetscFunctionReturn(0); 4286c88f2d44SBarry Smith } 4287c88f2d44SBarry Smith 42887db568b7SBarry Smith /*@C 4289228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 4290228d79bcSJed Brown 4291228d79bcSJed Brown Collective on TS 4292228d79bcSJed Brown 4293228d79bcSJed Brown Input Parameters: 4294228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 4295228d79bcSJed Brown . step - step number that has just completed 4296228d79bcSJed Brown . ptime - model time of the state 42970910c330SBarry Smith - u - state at the current model time 4298228d79bcSJed Brown 4299228d79bcSJed Brown Notes: 43007db568b7SBarry Smith TSMonitor() is typically used automatically within the time stepping implementations. 43017db568b7SBarry Smith Users would almost never call this routine directly. 4302228d79bcSJed Brown 430363e21af5SBarry Smith A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions 430463e21af5SBarry Smith 43057db568b7SBarry Smith Level: developer 4306228d79bcSJed Brown 4307228d79bcSJed Brown .keywords: TS, timestep 4308228d79bcSJed Brown @*/ 43090910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 4310d763cef2SBarry Smith { 4311d6152f81SLisandro Dalcin DM dm; 4312a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 4313d6152f81SLisandro Dalcin PetscErrorCode ierr; 4314d763cef2SBarry Smith 4315d763cef2SBarry Smith PetscFunctionBegin; 4316b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4317b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4318d6152f81SLisandro Dalcin 4319d6152f81SLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 4320d6152f81SLisandro Dalcin ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr); 4321d6152f81SLisandro Dalcin 43225edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 4323d763cef2SBarry Smith for (i=0; i<n; i++) { 43240910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 4325d763cef2SBarry Smith } 43265edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 4327d763cef2SBarry Smith PetscFunctionReturn(0); 4328d763cef2SBarry Smith } 4329d763cef2SBarry Smith 43307db568b7SBarry Smith /*@C 43319110b2e7SHong Zhang TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet() 43329110b2e7SHong Zhang 43339110b2e7SHong Zhang Collective on TS 43349110b2e7SHong Zhang 43359110b2e7SHong Zhang Input Parameters: 43369110b2e7SHong Zhang + ts - time stepping context obtained from TSCreate() 43379110b2e7SHong Zhang . step - step number that has just completed 43389110b2e7SHong Zhang . ptime - model time of the state 43399110b2e7SHong Zhang . u - state at the current model time 43409110b2e7SHong Zhang . numcost - number of cost functions (dimension of lambda or mu) 43419110b2e7SHong Zhang . lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 43429110b2e7SHong Zhang - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 43439110b2e7SHong Zhang 43449110b2e7SHong Zhang Notes: 43457db568b7SBarry Smith TSAdjointMonitor() is typically used automatically within the time stepping implementations. 43467db568b7SBarry Smith Users would almost never call this routine directly. 43479110b2e7SHong Zhang 43487db568b7SBarry Smith Level: developer 43499110b2e7SHong Zhang 43509110b2e7SHong Zhang .keywords: TS, timestep 43519110b2e7SHong Zhang @*/ 43529110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu) 43539110b2e7SHong Zhang { 43549110b2e7SHong Zhang PetscErrorCode ierr; 43559110b2e7SHong Zhang PetscInt i,n = ts->numberadjointmonitors; 43569110b2e7SHong Zhang 43579110b2e7SHong Zhang PetscFunctionBegin; 43589110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 43599110b2e7SHong Zhang PetscValidHeaderSpecific(u,VEC_CLASSID,4); 43609110b2e7SHong Zhang ierr = VecLockPush(u);CHKERRQ(ierr); 43619110b2e7SHong Zhang for (i=0; i<n; i++) { 43629110b2e7SHong Zhang ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 43639110b2e7SHong Zhang } 43649110b2e7SHong Zhang ierr = VecLockPop(u);CHKERRQ(ierr); 43659110b2e7SHong Zhang PetscFunctionReturn(0); 43669110b2e7SHong Zhang } 43679110b2e7SHong Zhang 4368d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 4369d763cef2SBarry Smith /*@C 43707db568b7SBarry Smith TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 4371a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 4372d763cef2SBarry Smith 4373d763cef2SBarry Smith Collective on TS 4374d763cef2SBarry Smith 4375d763cef2SBarry Smith Input Parameters: 4376d763cef2SBarry Smith + host - the X display to open, or null for the local machine 4377d763cef2SBarry Smith . label - the title to put in the title bar 43787c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 4379a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 4380a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 4381d763cef2SBarry Smith 4382d763cef2SBarry Smith Output Parameter: 43830b039ecaSBarry Smith . ctx - the context 4384d763cef2SBarry Smith 4385d763cef2SBarry Smith Options Database Key: 43864f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 43877db568b7SBarry Smith . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 43887db568b7SBarry Smith . -ts_monitor_lg_error - monitor the error 43897db568b7SBarry Smith . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 43907db568b7SBarry Smith . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 4391b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 4392d763cef2SBarry Smith 4393d763cef2SBarry Smith Notes: 4394a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 4395d763cef2SBarry Smith 43967db568b7SBarry Smith One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 43977db568b7SBarry Smith 43987db568b7SBarry 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 43997db568b7SBarry 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 44007db568b7SBarry Smith as the first argument. 44017db568b7SBarry Smith 44027db568b7SBarry Smith One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 44037db568b7SBarry Smith 44047db568b7SBarry Smith 4405d763cef2SBarry Smith Level: intermediate 4406d763cef2SBarry Smith 44077db568b7SBarry Smith .keywords: TS, monitor, line graph, residual 4408d763cef2SBarry Smith 44097db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 44107db568b7SBarry Smith TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 44117db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 44127db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 44137db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 44147c922b88SBarry Smith 4415d763cef2SBarry Smith @*/ 4416a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 4417d763cef2SBarry Smith { 44187f52daa2SLisandro Dalcin PetscDraw draw; 4419dfbe8321SBarry Smith PetscErrorCode ierr; 4420d763cef2SBarry Smith 4421d763cef2SBarry Smith PetscFunctionBegin; 4422b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 44237f52daa2SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 44247f52daa2SLisandro Dalcin ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 44257f52daa2SLisandro Dalcin ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 4426287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 44277f52daa2SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 4428a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 4429d763cef2SBarry Smith PetscFunctionReturn(0); 4430d763cef2SBarry Smith } 4431d763cef2SBarry Smith 4432b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 4433d763cef2SBarry Smith { 44340b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4435c32365f1SBarry Smith PetscReal x = ptime,y; 4436dfbe8321SBarry Smith PetscErrorCode ierr; 4437d763cef2SBarry Smith 4438d763cef2SBarry Smith PetscFunctionBegin; 443963e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */ 4440b06615a5SLisandro Dalcin if (!step) { 4441a9f9c1f6SBarry Smith PetscDrawAxis axis; 4442a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 44436934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr); 4444a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4445a9f9c1f6SBarry Smith } 4446c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 444708763aebSBarry Smith y = PetscLog10Real(y); 44480b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4449b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 44500b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 44516934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 44523923b477SBarry Smith } 4453d763cef2SBarry Smith PetscFunctionReturn(0); 4454d763cef2SBarry Smith } 4455d763cef2SBarry Smith 4456d763cef2SBarry Smith /*@C 4457a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 4458a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 4459d763cef2SBarry Smith 44600b039ecaSBarry Smith Collective on TSMonitorLGCtx 4461d763cef2SBarry Smith 4462d763cef2SBarry Smith Input Parameter: 44630b039ecaSBarry Smith . ctx - the monitor context 4464d763cef2SBarry Smith 4465d763cef2SBarry Smith Level: intermediate 4466d763cef2SBarry Smith 4467d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 4468d763cef2SBarry Smith 44694f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 4470d763cef2SBarry Smith @*/ 4471a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 4472d763cef2SBarry Smith { 4473dfbe8321SBarry Smith PetscErrorCode ierr; 4474d763cef2SBarry Smith 4475d763cef2SBarry Smith PetscFunctionBegin; 44767684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 44777684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 44787684fa3eSBarry Smith } 44790b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 448031152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 4481387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 4482387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 4483387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 44840b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 4485d763cef2SBarry Smith PetscFunctionReturn(0); 4486d763cef2SBarry Smith } 4487d763cef2SBarry Smith 4488d763cef2SBarry Smith /*@ 4489b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4490d763cef2SBarry Smith 4491d763cef2SBarry Smith Not Collective 4492d763cef2SBarry Smith 4493d763cef2SBarry Smith Input Parameter: 4494d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 4495d763cef2SBarry Smith 4496d763cef2SBarry Smith Output Parameter: 449763e21af5SBarry Smith . t - the current time. This time may not corresponds to the final time set with TSSetDuration(), use TSGetSolveTime(). 4498d763cef2SBarry Smith 4499d763cef2SBarry Smith Level: beginner 4500d763cef2SBarry Smith 4501b8123daeSJed Brown Note: 4502b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 45039be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 4504b8123daeSJed Brown 450563e21af5SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep(), TSGetSolveTime() 4506d763cef2SBarry Smith 4507d763cef2SBarry Smith .keywords: TS, get, time 4508d763cef2SBarry Smith @*/ 45097087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 4510d763cef2SBarry Smith { 4511d763cef2SBarry Smith PetscFunctionBegin; 45120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4513f7cf8827SBarry Smith PetscValidRealPointer(t,2); 4514d763cef2SBarry Smith *t = ts->ptime; 4515d763cef2SBarry Smith PetscFunctionReturn(0); 4516d763cef2SBarry Smith } 4517d763cef2SBarry Smith 4518e5e524a1SHong Zhang /*@ 4519e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4520e5e524a1SHong Zhang 4521e5e524a1SHong Zhang Not Collective 4522e5e524a1SHong Zhang 4523e5e524a1SHong Zhang Input Parameter: 4524e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 4525e5e524a1SHong Zhang 4526e5e524a1SHong Zhang Output Parameter: 4527e5e524a1SHong Zhang . t - the previous time 4528e5e524a1SHong Zhang 4529e5e524a1SHong Zhang Level: beginner 4530e5e524a1SHong Zhang 4531e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 4532e5e524a1SHong Zhang 4533e5e524a1SHong Zhang .keywords: TS, get, time 4534e5e524a1SHong Zhang @*/ 4535e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 4536e5e524a1SHong Zhang { 4537e5e524a1SHong Zhang PetscFunctionBegin; 4538e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4539e5e524a1SHong Zhang PetscValidRealPointer(t,2); 4540e5e524a1SHong Zhang *t = ts->ptime_prev; 4541e5e524a1SHong Zhang PetscFunctionReturn(0); 4542e5e524a1SHong Zhang } 4543e5e524a1SHong Zhang 45446a4d4014SLisandro Dalcin /*@ 45456a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 45466a4d4014SLisandro Dalcin 45473f9fe445SBarry Smith Logically Collective on TS 45486a4d4014SLisandro Dalcin 45496a4d4014SLisandro Dalcin Input Parameters: 45506a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 45516a4d4014SLisandro Dalcin - time - the time 45526a4d4014SLisandro Dalcin 45536a4d4014SLisandro Dalcin Level: intermediate 45546a4d4014SLisandro Dalcin 45556a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 45566a4d4014SLisandro Dalcin 45576a4d4014SLisandro Dalcin .keywords: TS, set, time 45586a4d4014SLisandro Dalcin @*/ 45597087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 45606a4d4014SLisandro Dalcin { 45616a4d4014SLisandro Dalcin PetscFunctionBegin; 45620700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4563c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 45646a4d4014SLisandro Dalcin ts->ptime = t; 45656a4d4014SLisandro Dalcin PetscFunctionReturn(0); 45666a4d4014SLisandro Dalcin } 45676a4d4014SLisandro Dalcin 4568d763cef2SBarry Smith /*@C 4569d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4570d763cef2SBarry Smith TS options in the database. 4571d763cef2SBarry Smith 45723f9fe445SBarry Smith Logically Collective on TS 4573d763cef2SBarry Smith 4574d763cef2SBarry Smith Input Parameter: 4575d763cef2SBarry Smith + ts - The TS context 4576d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4577d763cef2SBarry Smith 4578d763cef2SBarry Smith Notes: 4579d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4580d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4581d763cef2SBarry Smith hyphen. 4582d763cef2SBarry Smith 4583d763cef2SBarry Smith Level: advanced 4584d763cef2SBarry Smith 4585d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 4586d763cef2SBarry Smith 4587d763cef2SBarry Smith .seealso: TSSetFromOptions() 4588d763cef2SBarry Smith 4589d763cef2SBarry Smith @*/ 45907087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 4591d763cef2SBarry Smith { 4592dfbe8321SBarry Smith PetscErrorCode ierr; 4593089b2837SJed Brown SNES snes; 4594d763cef2SBarry Smith 4595d763cef2SBarry Smith PetscFunctionBegin; 45960700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4597d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4598089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4599089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4600d763cef2SBarry Smith PetscFunctionReturn(0); 4601d763cef2SBarry Smith } 4602d763cef2SBarry Smith 4603d763cef2SBarry Smith 4604d763cef2SBarry Smith /*@C 4605d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4606d763cef2SBarry Smith TS options in the database. 4607d763cef2SBarry Smith 46083f9fe445SBarry Smith Logically Collective on TS 4609d763cef2SBarry Smith 4610d763cef2SBarry Smith Input Parameter: 4611d763cef2SBarry Smith + ts - The TS context 4612d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4613d763cef2SBarry Smith 4614d763cef2SBarry Smith Notes: 4615d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4616d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4617d763cef2SBarry Smith hyphen. 4618d763cef2SBarry Smith 4619d763cef2SBarry Smith Level: advanced 4620d763cef2SBarry Smith 4621d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 4622d763cef2SBarry Smith 4623d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 4624d763cef2SBarry Smith 4625d763cef2SBarry Smith @*/ 46267087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4627d763cef2SBarry Smith { 4628dfbe8321SBarry Smith PetscErrorCode ierr; 4629089b2837SJed Brown SNES snes; 4630d763cef2SBarry Smith 4631d763cef2SBarry Smith PetscFunctionBegin; 46320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4633d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4634089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4635089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4636d763cef2SBarry Smith PetscFunctionReturn(0); 4637d763cef2SBarry Smith } 4638d763cef2SBarry Smith 4639d763cef2SBarry Smith /*@C 4640d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4641d763cef2SBarry Smith TS options in the database. 4642d763cef2SBarry Smith 4643d763cef2SBarry Smith Not Collective 4644d763cef2SBarry Smith 4645d763cef2SBarry Smith Input Parameter: 4646d763cef2SBarry Smith . ts - The TS context 4647d763cef2SBarry Smith 4648d763cef2SBarry Smith Output Parameter: 4649d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4650d763cef2SBarry Smith 4651d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 4652d763cef2SBarry Smith sufficient length to hold the prefix. 4653d763cef2SBarry Smith 4654d763cef2SBarry Smith Level: intermediate 4655d763cef2SBarry Smith 4656d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 4657d763cef2SBarry Smith 4658d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 4659d763cef2SBarry Smith @*/ 46607087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4661d763cef2SBarry Smith { 4662dfbe8321SBarry Smith PetscErrorCode ierr; 4663d763cef2SBarry Smith 4664d763cef2SBarry Smith PetscFunctionBegin; 46650700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 46664482741eSBarry Smith PetscValidPointer(prefix,2); 4667d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4668d763cef2SBarry Smith PetscFunctionReturn(0); 4669d763cef2SBarry Smith } 4670d763cef2SBarry Smith 4671d763cef2SBarry Smith /*@C 4672d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4673d763cef2SBarry Smith 4674d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4675d763cef2SBarry Smith 4676d763cef2SBarry Smith Input Parameter: 4677d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4678d763cef2SBarry Smith 4679d763cef2SBarry Smith Output Parameters: 4680e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4681e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4682e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4683e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4684d763cef2SBarry Smith 46850298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 4686d763cef2SBarry Smith 4687d763cef2SBarry Smith Level: intermediate 4688d763cef2SBarry Smith 468926d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 4690d763cef2SBarry Smith 4691d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 4692d763cef2SBarry Smith @*/ 4693e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4694d763cef2SBarry Smith { 4695089b2837SJed Brown PetscErrorCode ierr; 469624989b8cSPeter Brune DM dm; 4697089b2837SJed Brown 4698d763cef2SBarry Smith PetscFunctionBegin; 469923a57915SBarry Smith if (Amat || Pmat) { 470023a57915SBarry Smith SNES snes; 4701089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 470223a57915SBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4703e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 470423a57915SBarry Smith } 470524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 470624989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 4707d763cef2SBarry Smith PetscFunctionReturn(0); 4708d763cef2SBarry Smith } 4709d763cef2SBarry Smith 47102eca1d9cSJed Brown /*@C 47112eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 47122eca1d9cSJed Brown 47132eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 47142eca1d9cSJed Brown 47152eca1d9cSJed Brown Input Parameter: 47162eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 47172eca1d9cSJed Brown 47182eca1d9cSJed Brown Output Parameters: 4719e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4720e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 47212eca1d9cSJed Brown . f - The function to compute the matrices 47222eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 47232eca1d9cSJed Brown 47240298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 47252eca1d9cSJed Brown 47262eca1d9cSJed Brown Level: advanced 47272eca1d9cSJed Brown 47282eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 47292eca1d9cSJed Brown 47302eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 47312eca1d9cSJed Brown @*/ 4732e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 47332eca1d9cSJed Brown { 4734089b2837SJed Brown PetscErrorCode ierr; 473524989b8cSPeter Brune DM dm; 47360910c330SBarry Smith 47372eca1d9cSJed Brown PetscFunctionBegin; 4738c0aab802Sstefano_zampini if (Amat || Pmat) { 4739c0aab802Sstefano_zampini SNES snes; 4740089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4741f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4742e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 4743c0aab802Sstefano_zampini } 474424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 474524989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 47462eca1d9cSJed Brown PetscFunctionReturn(0); 47472eca1d9cSJed Brown } 47482eca1d9cSJed Brown 47496083293cSBarry Smith 47501713a123SBarry Smith /*@C 475183a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 47521713a123SBarry Smith VecView() for the solution at each timestep 47531713a123SBarry Smith 47541713a123SBarry Smith Collective on TS 47551713a123SBarry Smith 47561713a123SBarry Smith Input Parameters: 47571713a123SBarry Smith + ts - the TS context 47581713a123SBarry Smith . step - current time-step 4759142b95e3SSatish Balay . ptime - current time 47600298fd71SBarry Smith - dummy - either a viewer or NULL 47611713a123SBarry Smith 476299fdda47SBarry Smith Options Database: 476399fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 476499fdda47SBarry Smith 4765387f4636SBarry 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 476699fdda47SBarry Smith will look bad 476799fdda47SBarry Smith 47681713a123SBarry Smith Level: intermediate 47691713a123SBarry Smith 47701713a123SBarry Smith .keywords: TS, vector, monitor, view 47711713a123SBarry Smith 4772a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 47731713a123SBarry Smith @*/ 47740910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 47751713a123SBarry Smith { 4776dfbe8321SBarry Smith PetscErrorCode ierr; 477783a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4778473a3ab2SBarry Smith PetscDraw draw; 47791713a123SBarry Smith 47801713a123SBarry Smith PetscFunctionBegin; 47816083293cSBarry Smith if (!step && ictx->showinitial) { 47826083293cSBarry Smith if (!ictx->initialsolution) { 47830910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 47841713a123SBarry Smith } 47850910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 47866083293cSBarry Smith } 4787b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 47880dcf80beSBarry Smith 47896083293cSBarry Smith if (ictx->showinitial) { 47906083293cSBarry Smith PetscReal pause; 47916083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 47926083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 47936083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 47946083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 47956083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 47966083293cSBarry Smith } 47970910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 4798473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 479951fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 4800473a3ab2SBarry Smith char time[32]; 4801473a3ab2SBarry Smith 4802473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 480385b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4804473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4805473a3ab2SBarry Smith h = yl + .95*(yr - yl); 480651fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4807473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4808473a3ab2SBarry Smith } 4809473a3ab2SBarry Smith 48106083293cSBarry Smith if (ictx->showinitial) { 48116083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 48126083293cSBarry Smith } 48131713a123SBarry Smith PetscFunctionReturn(0); 48141713a123SBarry Smith } 48151713a123SBarry Smith 48169110b2e7SHong Zhang /*@C 48179110b2e7SHong Zhang TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling 48189110b2e7SHong Zhang VecView() for the sensitivities to initial states at each timestep 48199110b2e7SHong Zhang 48209110b2e7SHong Zhang Collective on TS 48219110b2e7SHong Zhang 48229110b2e7SHong Zhang Input Parameters: 48239110b2e7SHong Zhang + ts - the TS context 48249110b2e7SHong Zhang . step - current time-step 48259110b2e7SHong Zhang . ptime - current time 48269110b2e7SHong Zhang . u - current state 48279110b2e7SHong Zhang . numcost - number of cost functions 48289110b2e7SHong Zhang . lambda - sensitivities to initial conditions 48299110b2e7SHong Zhang . mu - sensitivities to parameters 48309110b2e7SHong Zhang - dummy - either a viewer or NULL 48319110b2e7SHong Zhang 48329110b2e7SHong Zhang Level: intermediate 48339110b2e7SHong Zhang 48349110b2e7SHong Zhang .keywords: TS, vector, adjoint, monitor, view 48359110b2e7SHong Zhang 48369110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView() 48379110b2e7SHong Zhang @*/ 48389110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 48399110b2e7SHong Zhang { 48409110b2e7SHong Zhang PetscErrorCode ierr; 48419110b2e7SHong Zhang TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 48429110b2e7SHong Zhang PetscDraw draw; 4843a0046e2cSSatish Balay PetscReal xl,yl,xr,yr,h; 4844a0046e2cSSatish Balay char time[32]; 48459110b2e7SHong Zhang 48469110b2e7SHong Zhang PetscFunctionBegin; 48479110b2e7SHong Zhang if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 48489110b2e7SHong Zhang 48499110b2e7SHong Zhang ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr); 48509110b2e7SHong Zhang ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 48519110b2e7SHong Zhang ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 48529110b2e7SHong Zhang ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 48539110b2e7SHong Zhang h = yl + .95*(yr - yl); 48549110b2e7SHong Zhang ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 48559110b2e7SHong Zhang ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 48569110b2e7SHong Zhang PetscFunctionReturn(0); 48579110b2e7SHong Zhang } 48589110b2e7SHong Zhang 48592d5ee99bSBarry Smith /*@C 48602d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 48612d5ee99bSBarry Smith 48622d5ee99bSBarry Smith Collective on TS 48632d5ee99bSBarry Smith 48642d5ee99bSBarry Smith Input Parameters: 48652d5ee99bSBarry Smith + ts - the TS context 48662d5ee99bSBarry Smith . step - current time-step 48672d5ee99bSBarry Smith . ptime - current time 48682d5ee99bSBarry Smith - dummy - either a viewer or NULL 48692d5ee99bSBarry Smith 48702d5ee99bSBarry Smith Level: intermediate 48712d5ee99bSBarry Smith 48722d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 48732d5ee99bSBarry Smith 48742d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 48752d5ee99bSBarry Smith @*/ 48762d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 48772d5ee99bSBarry Smith { 48782d5ee99bSBarry Smith PetscErrorCode ierr; 48792d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 48802d5ee99bSBarry Smith PetscDraw draw; 48816934998bSLisandro Dalcin PetscDrawAxis axis; 48822d5ee99bSBarry Smith PetscInt n; 48832d5ee99bSBarry Smith PetscMPIInt size; 48846934998bSLisandro Dalcin PetscReal U0,U1,xl,yl,xr,yr,h; 48852d5ee99bSBarry Smith char time[32]; 48862d5ee99bSBarry Smith const PetscScalar *U; 48872d5ee99bSBarry Smith 48882d5ee99bSBarry Smith PetscFunctionBegin; 48896934998bSLisandro Dalcin ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr); 48906934998bSLisandro Dalcin if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs"); 48912d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 48926934998bSLisandro Dalcin if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 48932d5ee99bSBarry Smith 48942d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 48956934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr); 48966934998bSLisandro Dalcin ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 48976934998bSLisandro Dalcin if (!step) { 48986934998bSLisandro Dalcin ierr = PetscDrawClear(draw);CHKERRQ(ierr); 48996934998bSLisandro Dalcin ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); 49006934998bSLisandro Dalcin } 49012d5ee99bSBarry Smith 49022d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 49036934998bSLisandro Dalcin U0 = PetscRealPart(U[0]); 49046934998bSLisandro Dalcin U1 = PetscRealPart(U[1]); 4905a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 49066934998bSLisandro Dalcin if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0); 49072d5ee99bSBarry Smith 49086934998bSLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 49096934998bSLisandro Dalcin ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr); 49102d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 49114b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 491285b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 49132d5ee99bSBarry Smith h = yl + .95*(yr - yl); 491451fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 49152d5ee99bSBarry Smith } 49166934998bSLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 49172d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 491856d62c8fSLisandro Dalcin ierr = PetscDrawPause(draw);CHKERRQ(ierr); 49196934998bSLisandro Dalcin ierr = PetscDrawSave(draw);CHKERRQ(ierr); 49202d5ee99bSBarry Smith PetscFunctionReturn(0); 49212d5ee99bSBarry Smith } 49222d5ee99bSBarry Smith 49231713a123SBarry Smith 49246083293cSBarry Smith /*@C 492583a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 49266083293cSBarry Smith 49276083293cSBarry Smith Collective on TS 49286083293cSBarry Smith 49296083293cSBarry Smith Input Parameters: 49306083293cSBarry Smith . ctx - the monitor context 49316083293cSBarry Smith 49326083293cSBarry Smith Level: intermediate 49336083293cSBarry Smith 49346083293cSBarry Smith .keywords: TS, vector, monitor, view 49356083293cSBarry Smith 493683a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 49376083293cSBarry Smith @*/ 493883a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 49396083293cSBarry Smith { 49406083293cSBarry Smith PetscErrorCode ierr; 49416083293cSBarry Smith 49426083293cSBarry Smith PetscFunctionBegin; 494383a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 494483a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 494583a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 49466083293cSBarry Smith PetscFunctionReturn(0); 49476083293cSBarry Smith } 49486083293cSBarry Smith 49496083293cSBarry Smith /*@C 495083a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 49516083293cSBarry Smith 49526083293cSBarry Smith Collective on TS 49536083293cSBarry Smith 49546083293cSBarry Smith Input Parameter: 49556083293cSBarry Smith . ts - time-step context 49566083293cSBarry Smith 49576083293cSBarry Smith Output Patameter: 49586083293cSBarry Smith . ctx - the monitor context 49596083293cSBarry Smith 496099fdda47SBarry Smith Options Database: 496199fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 496299fdda47SBarry Smith 49636083293cSBarry Smith Level: intermediate 49646083293cSBarry Smith 49656083293cSBarry Smith .keywords: TS, vector, monitor, view 49666083293cSBarry Smith 496783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 49686083293cSBarry Smith @*/ 496983a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 49706083293cSBarry Smith { 49716083293cSBarry Smith PetscErrorCode ierr; 49726083293cSBarry Smith 49736083293cSBarry Smith PetscFunctionBegin; 4974b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 497583a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4976e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4977bbd56ea5SKarl Rupp 497883a4ac43SBarry Smith (*ctx)->howoften = howoften; 4979473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 4980c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4981473a3ab2SBarry Smith 4982473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 4983c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 49846083293cSBarry Smith PetscFunctionReturn(0); 49856083293cSBarry Smith } 49866083293cSBarry Smith 49873a471f94SBarry Smith /*@C 498883a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 49893a471f94SBarry Smith VecView() for the error at each timestep 49903a471f94SBarry Smith 49913a471f94SBarry Smith Collective on TS 49923a471f94SBarry Smith 49933a471f94SBarry Smith Input Parameters: 49943a471f94SBarry Smith + ts - the TS context 49953a471f94SBarry Smith . step - current time-step 49963a471f94SBarry Smith . ptime - current time 49970298fd71SBarry Smith - dummy - either a viewer or NULL 49983a471f94SBarry Smith 49993a471f94SBarry Smith Level: intermediate 50003a471f94SBarry Smith 50013a471f94SBarry Smith .keywords: TS, vector, monitor, view 50023a471f94SBarry Smith 50033a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 50043a471f94SBarry Smith @*/ 50050910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 50063a471f94SBarry Smith { 50073a471f94SBarry Smith PetscErrorCode ierr; 500883a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 500983a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 50103a471f94SBarry Smith Vec work; 50113a471f94SBarry Smith 50123a471f94SBarry Smith PetscFunctionBegin; 5013b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 50140910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 50153a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 50160910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 50173a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 50183a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 50193a471f94SBarry Smith PetscFunctionReturn(0); 50203a471f94SBarry Smith } 50213a471f94SBarry Smith 5022af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 50236c699258SBarry Smith /*@ 50242a808120SBarry Smith TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS 50256c699258SBarry Smith 50263f9fe445SBarry Smith Logically Collective on TS and DM 50276c699258SBarry Smith 50286c699258SBarry Smith Input Parameters: 50292a808120SBarry Smith + ts - the ODE integrator object 50302a808120SBarry Smith - dm - the dm, cannot be NULL 50316c699258SBarry Smith 50326c699258SBarry Smith Level: intermediate 50336c699258SBarry Smith 50346c699258SBarry Smith 50356c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 50366c699258SBarry Smith @*/ 50377087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 50386c699258SBarry Smith { 50396c699258SBarry Smith PetscErrorCode ierr; 5040089b2837SJed Brown SNES snes; 5041942e3340SBarry Smith DMTS tsdm; 50426c699258SBarry Smith 50436c699258SBarry Smith PetscFunctionBegin; 50440700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 50452a808120SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 504670663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 5047942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 50482a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 5049942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 5050942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 505124989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 505224989b8cSPeter Brune tsdm->originaldm = dm; 505324989b8cSPeter Brune } 505424989b8cSPeter Brune } 50556bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 505624989b8cSPeter Brune } 50576c699258SBarry Smith ts->dm = dm; 5058bbd56ea5SKarl Rupp 5059089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 5060089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 50616c699258SBarry Smith PetscFunctionReturn(0); 50626c699258SBarry Smith } 50636c699258SBarry Smith 50646c699258SBarry Smith /*@ 50656c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 50666c699258SBarry Smith 50673f9fe445SBarry Smith Not Collective 50686c699258SBarry Smith 50696c699258SBarry Smith Input Parameter: 50706c699258SBarry Smith . ts - the preconditioner context 50716c699258SBarry Smith 50726c699258SBarry Smith Output Parameter: 50736c699258SBarry Smith . dm - the dm 50746c699258SBarry Smith 50756c699258SBarry Smith Level: intermediate 50766c699258SBarry Smith 50776c699258SBarry Smith 50786c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 50796c699258SBarry Smith @*/ 50807087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 50816c699258SBarry Smith { 5082496e6a7aSJed Brown PetscErrorCode ierr; 5083496e6a7aSJed Brown 50846c699258SBarry Smith PetscFunctionBegin; 50850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5086496e6a7aSJed Brown if (!ts->dm) { 5087ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 5088496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 5089496e6a7aSJed Brown } 50906c699258SBarry Smith *dm = ts->dm; 50916c699258SBarry Smith PetscFunctionReturn(0); 50926c699258SBarry Smith } 50931713a123SBarry Smith 50940f5c6efeSJed Brown /*@ 50950f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 50960f5c6efeSJed Brown 50973f9fe445SBarry Smith Logically Collective on SNES 50980f5c6efeSJed Brown 50990f5c6efeSJed Brown Input Parameter: 5100d42a1c89SJed Brown + snes - nonlinear solver 51010910c330SBarry Smith . U - the current state at which to evaluate the residual 5102d42a1c89SJed Brown - ctx - user context, must be a TS 51030f5c6efeSJed Brown 51040f5c6efeSJed Brown Output Parameter: 51050f5c6efeSJed Brown . F - the nonlinear residual 51060f5c6efeSJed Brown 51070f5c6efeSJed Brown Notes: 51080f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 51090f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 51100f5c6efeSJed Brown 51110f5c6efeSJed Brown Level: advanced 51120f5c6efeSJed Brown 51130f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 51140f5c6efeSJed Brown @*/ 51150910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 51160f5c6efeSJed Brown { 51170f5c6efeSJed Brown TS ts = (TS)ctx; 51180f5c6efeSJed Brown PetscErrorCode ierr; 51190f5c6efeSJed Brown 51200f5c6efeSJed Brown PetscFunctionBegin; 51210f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 51220910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 51230f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 51240f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 51250910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 51260f5c6efeSJed Brown PetscFunctionReturn(0); 51270f5c6efeSJed Brown } 51280f5c6efeSJed Brown 51290f5c6efeSJed Brown /*@ 51300f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 51310f5c6efeSJed Brown 51320f5c6efeSJed Brown Collective on SNES 51330f5c6efeSJed Brown 51340f5c6efeSJed Brown Input Parameter: 51350f5c6efeSJed Brown + snes - nonlinear solver 51360910c330SBarry Smith . U - the current state at which to evaluate the residual 51370f5c6efeSJed Brown - ctx - user context, must be a TS 51380f5c6efeSJed Brown 51390f5c6efeSJed Brown Output Parameter: 51400f5c6efeSJed Brown + A - the Jacobian 51410f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 51420f5c6efeSJed Brown - flag - indicates any structure change in the matrix 51430f5c6efeSJed Brown 51440f5c6efeSJed Brown Notes: 51450f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 51460f5c6efeSJed Brown 51470f5c6efeSJed Brown Level: developer 51480f5c6efeSJed Brown 51490f5c6efeSJed Brown .seealso: SNESSetJacobian() 51500f5c6efeSJed Brown @*/ 5151d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 51520f5c6efeSJed Brown { 51530f5c6efeSJed Brown TS ts = (TS)ctx; 51540f5c6efeSJed Brown PetscErrorCode ierr; 51550f5c6efeSJed Brown 51560f5c6efeSJed Brown PetscFunctionBegin; 51570f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 51580910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 51590f5c6efeSJed Brown PetscValidPointer(A,3); 516094ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 51610f5c6efeSJed Brown PetscValidPointer(B,4); 516294ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 51630f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 5164d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 51650f5c6efeSJed Brown PetscFunctionReturn(0); 51660f5c6efeSJed Brown } 5167325fc9f4SBarry Smith 51680e4ef248SJed Brown /*@C 51699ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 51700e4ef248SJed Brown 51710e4ef248SJed Brown Collective on TS 51720e4ef248SJed Brown 51730e4ef248SJed Brown Input Arguments: 51740e4ef248SJed Brown + ts - time stepping context 51750e4ef248SJed Brown . t - time at which to evaluate 51760910c330SBarry Smith . U - state at which to evaluate 51770e4ef248SJed Brown - ctx - context 51780e4ef248SJed Brown 51790e4ef248SJed Brown Output Arguments: 51800e4ef248SJed Brown . F - right hand side 51810e4ef248SJed Brown 51820e4ef248SJed Brown Level: intermediate 51830e4ef248SJed Brown 51840e4ef248SJed Brown Notes: 51850e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 51860e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 51870e4ef248SJed Brown 51880e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 51890e4ef248SJed Brown @*/ 51900910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 51910e4ef248SJed Brown { 51920e4ef248SJed Brown PetscErrorCode ierr; 51930e4ef248SJed Brown Mat Arhs,Brhs; 51940e4ef248SJed Brown 51950e4ef248SJed Brown PetscFunctionBegin; 51960e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 5197d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 51980910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 51990e4ef248SJed Brown PetscFunctionReturn(0); 52000e4ef248SJed Brown } 52010e4ef248SJed Brown 52020e4ef248SJed Brown /*@C 52030e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 52040e4ef248SJed Brown 52050e4ef248SJed Brown Collective on TS 52060e4ef248SJed Brown 52070e4ef248SJed Brown Input Arguments: 52080e4ef248SJed Brown + ts - time stepping context 52090e4ef248SJed Brown . t - time at which to evaluate 52100910c330SBarry Smith . U - state at which to evaluate 52110e4ef248SJed Brown - ctx - context 52120e4ef248SJed Brown 52130e4ef248SJed Brown Output Arguments: 52140e4ef248SJed Brown + A - pointer to operator 52150e4ef248SJed Brown . B - pointer to preconditioning matrix 52160e4ef248SJed Brown - flg - matrix structure flag 52170e4ef248SJed Brown 52180e4ef248SJed Brown Level: intermediate 52190e4ef248SJed Brown 52200e4ef248SJed Brown Notes: 52210e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 52220e4ef248SJed Brown 52230e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 52240e4ef248SJed Brown @*/ 5225d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 52260e4ef248SJed Brown { 52270e4ef248SJed Brown PetscFunctionBegin; 52280e4ef248SJed Brown PetscFunctionReturn(0); 52290e4ef248SJed Brown } 52300e4ef248SJed Brown 52310026cea9SSean Farley /*@C 52320026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 52330026cea9SSean Farley 52340026cea9SSean Farley Collective on TS 52350026cea9SSean Farley 52360026cea9SSean Farley Input Arguments: 52370026cea9SSean Farley + ts - time stepping context 52380026cea9SSean Farley . t - time at which to evaluate 52390910c330SBarry Smith . U - state at which to evaluate 52400910c330SBarry Smith . Udot - time derivative of state vector 52410026cea9SSean Farley - ctx - context 52420026cea9SSean Farley 52430026cea9SSean Farley Output Arguments: 52440026cea9SSean Farley . F - left hand side 52450026cea9SSean Farley 52460026cea9SSean Farley Level: intermediate 52470026cea9SSean Farley 52480026cea9SSean Farley Notes: 52490910c330SBarry 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 52500026cea9SSean Farley user is required to write their own TSComputeIFunction. 52510026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 52520026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 52530026cea9SSean Farley 52549ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 52559ae8fd06SBarry Smith 52569ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 52570026cea9SSean Farley @*/ 52580910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 52590026cea9SSean Farley { 52600026cea9SSean Farley PetscErrorCode ierr; 52610026cea9SSean Farley Mat A,B; 52620026cea9SSean Farley 52630026cea9SSean Farley PetscFunctionBegin; 52640298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 5265d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 52660910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 52670026cea9SSean Farley PetscFunctionReturn(0); 52680026cea9SSean Farley } 52690026cea9SSean Farley 52700026cea9SSean Farley /*@C 5271b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 52720026cea9SSean Farley 52730026cea9SSean Farley Collective on TS 52740026cea9SSean Farley 52750026cea9SSean Farley Input Arguments: 52760026cea9SSean Farley + ts - time stepping context 52770026cea9SSean Farley . t - time at which to evaluate 52780910c330SBarry Smith . U - state at which to evaluate 52790910c330SBarry Smith . Udot - time derivative of state vector 52800026cea9SSean Farley . shift - shift to apply 52810026cea9SSean Farley - ctx - context 52820026cea9SSean Farley 52830026cea9SSean Farley Output Arguments: 52840026cea9SSean Farley + A - pointer to operator 52850026cea9SSean Farley . B - pointer to preconditioning matrix 52860026cea9SSean Farley - flg - matrix structure flag 52870026cea9SSean Farley 5288b41af12eSJed Brown Level: advanced 52890026cea9SSean Farley 52900026cea9SSean Farley Notes: 52910026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 52920026cea9SSean Farley 5293b41af12eSJed Brown It is only appropriate for problems of the form 5294b41af12eSJed Brown 5295b41af12eSJed Brown $ M Udot = F(U,t) 5296b41af12eSJed Brown 5297b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 5298b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 5299b41af12eSJed Brown an implicit operator of the form 5300b41af12eSJed Brown 5301b41af12eSJed Brown $ shift*M + J 5302b41af12eSJed Brown 5303b41af12eSJed 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 5304b41af12eSJed Brown a copy of M or reassemble it when requested. 5305b41af12eSJed Brown 53060026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 53070026cea9SSean Farley @*/ 5308d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 53090026cea9SSean Farley { 5310b41af12eSJed Brown PetscErrorCode ierr; 5311b41af12eSJed Brown 53120026cea9SSean Farley PetscFunctionBegin; 531394ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 5314b41af12eSJed Brown ts->ijacobian.shift = shift; 53150026cea9SSean Farley PetscFunctionReturn(0); 53160026cea9SSean Farley } 5317b41af12eSJed Brown 5318e817cc15SEmil Constantinescu /*@ 5319e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 5320e817cc15SEmil Constantinescu 5321e817cc15SEmil Constantinescu Not Collective 5322e817cc15SEmil Constantinescu 5323e817cc15SEmil Constantinescu Input Parameter: 5324e817cc15SEmil Constantinescu . ts - the TS context 5325e817cc15SEmil Constantinescu 5326e817cc15SEmil Constantinescu Output Parameter: 53274e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 5328e817cc15SEmil Constantinescu 5329e817cc15SEmil Constantinescu Level: beginner 5330e817cc15SEmil Constantinescu 5331e817cc15SEmil Constantinescu .keywords: TS, equation type 5332e817cc15SEmil Constantinescu 5333e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 5334e817cc15SEmil Constantinescu @*/ 5335e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 5336e817cc15SEmil Constantinescu { 5337e817cc15SEmil Constantinescu PetscFunctionBegin; 5338e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5339e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 5340e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 5341e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5342e817cc15SEmil Constantinescu } 5343e817cc15SEmil Constantinescu 5344e817cc15SEmil Constantinescu /*@ 5345e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 5346e817cc15SEmil Constantinescu 5347e817cc15SEmil Constantinescu Not Collective 5348e817cc15SEmil Constantinescu 5349e817cc15SEmil Constantinescu Input Parameter: 5350e817cc15SEmil Constantinescu + ts - the TS context 53511297b224SEmil Constantinescu - equation_type - see TSEquationType 5352e817cc15SEmil Constantinescu 5353e817cc15SEmil Constantinescu Level: advanced 5354e817cc15SEmil Constantinescu 5355e817cc15SEmil Constantinescu .keywords: TS, equation type 5356e817cc15SEmil Constantinescu 5357e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 5358e817cc15SEmil Constantinescu @*/ 5359e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 5360e817cc15SEmil Constantinescu { 5361e817cc15SEmil Constantinescu PetscFunctionBegin; 5362e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5363e817cc15SEmil Constantinescu ts->equation_type = equation_type; 5364e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5365e817cc15SEmil Constantinescu } 53660026cea9SSean Farley 53674af1b03aSJed Brown /*@ 53684af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 53694af1b03aSJed Brown 53704af1b03aSJed Brown Not Collective 53714af1b03aSJed Brown 53724af1b03aSJed Brown Input Parameter: 53734af1b03aSJed Brown . ts - the TS context 53744af1b03aSJed Brown 53754af1b03aSJed Brown Output Parameter: 53764af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 53774af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 53784af1b03aSJed Brown 5379487e0bb9SJed Brown Level: beginner 53804af1b03aSJed Brown 5381cd652676SJed Brown Notes: 5382cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 53834af1b03aSJed Brown 53844af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 53854af1b03aSJed Brown 53864af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 53874af1b03aSJed Brown @*/ 53884af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 53894af1b03aSJed Brown { 53904af1b03aSJed Brown PetscFunctionBegin; 53914af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53924af1b03aSJed Brown PetscValidPointer(reason,2); 53934af1b03aSJed Brown *reason = ts->reason; 53944af1b03aSJed Brown PetscFunctionReturn(0); 53954af1b03aSJed Brown } 53964af1b03aSJed Brown 5397d6ad946cSShri Abhyankar /*@ 5398d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 5399d6ad946cSShri Abhyankar 5400d6ad946cSShri Abhyankar Not Collective 5401d6ad946cSShri Abhyankar 5402d6ad946cSShri Abhyankar Input Parameter: 5403d6ad946cSShri Abhyankar + ts - the TS context 5404d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 5405d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 5406d6ad946cSShri Abhyankar 5407f5abba47SShri Abhyankar Level: advanced 5408d6ad946cSShri Abhyankar 5409d6ad946cSShri Abhyankar Notes: 5410d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 5411d6ad946cSShri Abhyankar 5412d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 5413d6ad946cSShri Abhyankar 5414d6ad946cSShri Abhyankar .seealso: TSConvergedReason 5415d6ad946cSShri Abhyankar @*/ 5416d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 5417d6ad946cSShri Abhyankar { 5418d6ad946cSShri Abhyankar PetscFunctionBegin; 5419d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5420d6ad946cSShri Abhyankar ts->reason = reason; 5421d6ad946cSShri Abhyankar PetscFunctionReturn(0); 5422d6ad946cSShri Abhyankar } 5423d6ad946cSShri Abhyankar 5424cc708dedSBarry Smith /*@ 5425cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 5426cc708dedSBarry Smith 5427cc708dedSBarry Smith Not Collective 5428cc708dedSBarry Smith 5429cc708dedSBarry Smith Input Parameter: 5430cc708dedSBarry Smith . ts - the TS context 5431cc708dedSBarry Smith 5432cc708dedSBarry Smith Output Parameter: 543363e21af5SBarry Smith . ftime - the final time. This time corresponds to the final time set with TSSetDuration() 5434cc708dedSBarry Smith 5435487e0bb9SJed Brown Level: beginner 5436cc708dedSBarry Smith 5437cc708dedSBarry Smith Notes: 5438cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 5439cc708dedSBarry Smith 5440cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 5441cc708dedSBarry Smith 5442cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 5443cc708dedSBarry Smith @*/ 5444cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 5445cc708dedSBarry Smith { 5446cc708dedSBarry Smith PetscFunctionBegin; 5447cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5448cc708dedSBarry Smith PetscValidPointer(ftime,2); 5449cc708dedSBarry Smith *ftime = ts->solvetime; 5450cc708dedSBarry Smith PetscFunctionReturn(0); 5451cc708dedSBarry Smith } 5452cc708dedSBarry Smith 54532c18e0fdSBarry Smith /*@ 54542c18e0fdSBarry Smith TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 54552c18e0fdSBarry Smith 54562c18e0fdSBarry Smith Not Collective 54572c18e0fdSBarry Smith 54582c18e0fdSBarry Smith Input Parameter: 54592c18e0fdSBarry Smith . ts - the TS context 54602c18e0fdSBarry Smith 54612c18e0fdSBarry Smith Output Parameter: 54622c18e0fdSBarry Smith . steps - the number of steps 54632c18e0fdSBarry Smith 54642c18e0fdSBarry Smith Level: beginner 54652c18e0fdSBarry Smith 54662c18e0fdSBarry Smith Notes: 54672c18e0fdSBarry Smith Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 54682c18e0fdSBarry Smith 54692c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test 54702c18e0fdSBarry Smith 54712c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 54722c18e0fdSBarry Smith @*/ 54732c18e0fdSBarry Smith PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 54742c18e0fdSBarry Smith { 54752c18e0fdSBarry Smith PetscFunctionBegin; 54762c18e0fdSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 54772c18e0fdSBarry Smith PetscValidPointer(steps,2); 54782c18e0fdSBarry Smith *steps = ts->total_steps; 54792c18e0fdSBarry Smith PetscFunctionReturn(0); 54802c18e0fdSBarry Smith } 54812c18e0fdSBarry Smith 54829f67acb7SJed Brown /*@ 54835ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 54849f67acb7SJed Brown used by the time integrator. 54859f67acb7SJed Brown 54869f67acb7SJed Brown Not Collective 54879f67acb7SJed Brown 54889f67acb7SJed Brown Input Parameter: 54899f67acb7SJed Brown . ts - TS context 54909f67acb7SJed Brown 54919f67acb7SJed Brown Output Parameter: 54929f67acb7SJed Brown . nits - number of nonlinear iterations 54939f67acb7SJed Brown 54949f67acb7SJed Brown Notes: 54959f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 54969f67acb7SJed Brown 54979f67acb7SJed Brown Level: intermediate 54989f67acb7SJed Brown 54999f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 55009f67acb7SJed Brown 55015ef26d82SJed Brown .seealso: TSGetKSPIterations() 55029f67acb7SJed Brown @*/ 55035ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 55049f67acb7SJed Brown { 55059f67acb7SJed Brown PetscFunctionBegin; 55069f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 55079f67acb7SJed Brown PetscValidIntPointer(nits,2); 55085ef26d82SJed Brown *nits = ts->snes_its; 55099f67acb7SJed Brown PetscFunctionReturn(0); 55109f67acb7SJed Brown } 55119f67acb7SJed Brown 55129f67acb7SJed Brown /*@ 55135ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 55149f67acb7SJed Brown used by the time integrator. 55159f67acb7SJed Brown 55169f67acb7SJed Brown Not Collective 55179f67acb7SJed Brown 55189f67acb7SJed Brown Input Parameter: 55199f67acb7SJed Brown . ts - TS context 55209f67acb7SJed Brown 55219f67acb7SJed Brown Output Parameter: 55229f67acb7SJed Brown . lits - number of linear iterations 55239f67acb7SJed Brown 55249f67acb7SJed Brown Notes: 55259f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 55269f67acb7SJed Brown 55279f67acb7SJed Brown Level: intermediate 55289f67acb7SJed Brown 55299f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 55309f67acb7SJed Brown 55315ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 55329f67acb7SJed Brown @*/ 55335ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 55349f67acb7SJed Brown { 55359f67acb7SJed Brown PetscFunctionBegin; 55369f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 55379f67acb7SJed Brown PetscValidIntPointer(lits,2); 55385ef26d82SJed Brown *lits = ts->ksp_its; 55399f67acb7SJed Brown PetscFunctionReturn(0); 55409f67acb7SJed Brown } 55419f67acb7SJed Brown 5542cef5090cSJed Brown /*@ 5543cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 5544cef5090cSJed Brown 5545cef5090cSJed Brown Not Collective 5546cef5090cSJed Brown 5547cef5090cSJed Brown Input Parameter: 5548cef5090cSJed Brown . ts - TS context 5549cef5090cSJed Brown 5550cef5090cSJed Brown Output Parameter: 5551cef5090cSJed Brown . rejects - number of steps rejected 5552cef5090cSJed Brown 5553cef5090cSJed Brown Notes: 5554cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5555cef5090cSJed Brown 5556cef5090cSJed Brown Level: intermediate 5557cef5090cSJed Brown 5558cef5090cSJed Brown .keywords: TS, get, number 5559cef5090cSJed Brown 55605ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 5561cef5090cSJed Brown @*/ 5562cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 5563cef5090cSJed Brown { 5564cef5090cSJed Brown PetscFunctionBegin; 5565cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5566cef5090cSJed Brown PetscValidIntPointer(rejects,2); 5567cef5090cSJed Brown *rejects = ts->reject; 5568cef5090cSJed Brown PetscFunctionReturn(0); 5569cef5090cSJed Brown } 5570cef5090cSJed Brown 5571cef5090cSJed Brown /*@ 5572cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 5573cef5090cSJed Brown 5574cef5090cSJed Brown Not Collective 5575cef5090cSJed Brown 5576cef5090cSJed Brown Input Parameter: 5577cef5090cSJed Brown . ts - TS context 5578cef5090cSJed Brown 5579cef5090cSJed Brown Output Parameter: 5580cef5090cSJed Brown . fails - number of failed nonlinear solves 5581cef5090cSJed Brown 5582cef5090cSJed Brown Notes: 5583cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5584cef5090cSJed Brown 5585cef5090cSJed Brown Level: intermediate 5586cef5090cSJed Brown 5587cef5090cSJed Brown .keywords: TS, get, number 5588cef5090cSJed Brown 55895ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 5590cef5090cSJed Brown @*/ 5591cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 5592cef5090cSJed Brown { 5593cef5090cSJed Brown PetscFunctionBegin; 5594cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5595cef5090cSJed Brown PetscValidIntPointer(fails,2); 5596cef5090cSJed Brown *fails = ts->num_snes_failures; 5597cef5090cSJed Brown PetscFunctionReturn(0); 5598cef5090cSJed Brown } 5599cef5090cSJed Brown 5600cef5090cSJed Brown /*@ 5601cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 5602cef5090cSJed Brown 5603cef5090cSJed Brown Not Collective 5604cef5090cSJed Brown 5605cef5090cSJed Brown Input Parameter: 5606cef5090cSJed Brown + ts - TS context 5607cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 5608cef5090cSJed Brown 5609cef5090cSJed Brown Notes: 5610cef5090cSJed Brown The counter is reset to zero for each step 5611cef5090cSJed Brown 5612cef5090cSJed Brown Options Database Key: 5613cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 5614cef5090cSJed Brown 5615cef5090cSJed Brown Level: intermediate 5616cef5090cSJed Brown 5617cef5090cSJed Brown .keywords: TS, set, maximum, number 5618cef5090cSJed Brown 56195ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5620cef5090cSJed Brown @*/ 5621cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 5622cef5090cSJed Brown { 5623cef5090cSJed Brown PetscFunctionBegin; 5624cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5625cef5090cSJed Brown ts->max_reject = rejects; 5626cef5090cSJed Brown PetscFunctionReturn(0); 5627cef5090cSJed Brown } 5628cef5090cSJed Brown 5629cef5090cSJed Brown /*@ 5630cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 5631cef5090cSJed Brown 5632cef5090cSJed Brown Not Collective 5633cef5090cSJed Brown 5634cef5090cSJed Brown Input Parameter: 5635cef5090cSJed Brown + ts - TS context 5636cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 5637cef5090cSJed Brown 5638cef5090cSJed Brown Notes: 5639cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 5640cef5090cSJed Brown 5641cef5090cSJed Brown Options Database Key: 5642cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 5643cef5090cSJed Brown 5644cef5090cSJed Brown Level: intermediate 5645cef5090cSJed Brown 5646cef5090cSJed Brown .keywords: TS, set, maximum, number 5647cef5090cSJed Brown 56485ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 5649cef5090cSJed Brown @*/ 5650cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 5651cef5090cSJed Brown { 5652cef5090cSJed Brown PetscFunctionBegin; 5653cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5654cef5090cSJed Brown ts->max_snes_failures = fails; 5655cef5090cSJed Brown PetscFunctionReturn(0); 5656cef5090cSJed Brown } 5657cef5090cSJed Brown 5658cef5090cSJed Brown /*@ 5659cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 5660cef5090cSJed Brown 5661cef5090cSJed Brown Not Collective 5662cef5090cSJed Brown 5663cef5090cSJed Brown Input Parameter: 5664cef5090cSJed Brown + ts - TS context 5665cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 5666cef5090cSJed Brown 5667cef5090cSJed Brown Options Database Key: 5668cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 5669cef5090cSJed Brown 5670cef5090cSJed Brown Level: intermediate 5671cef5090cSJed Brown 5672cef5090cSJed Brown .keywords: TS, set, error 5673cef5090cSJed Brown 56745ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5675cef5090cSJed Brown @*/ 5676cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 5677cef5090cSJed Brown { 5678cef5090cSJed Brown PetscFunctionBegin; 5679cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5680cef5090cSJed Brown ts->errorifstepfailed = err; 5681cef5090cSJed Brown PetscFunctionReturn(0); 5682cef5090cSJed Brown } 5683cef5090cSJed Brown 5684fb1732b5SBarry Smith /*@C 5685fde5950dSBarry 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 5686fb1732b5SBarry Smith 5687fb1732b5SBarry Smith Collective on TS 5688fb1732b5SBarry Smith 5689fb1732b5SBarry Smith Input Parameters: 5690fb1732b5SBarry Smith + ts - the TS context 5691fb1732b5SBarry Smith . step - current time-step 5692fb1732b5SBarry Smith . ptime - current time 56930910c330SBarry Smith . u - current state 5694721cd6eeSBarry Smith - vf - viewer and its format 5695fb1732b5SBarry Smith 5696fb1732b5SBarry Smith Level: intermediate 5697fb1732b5SBarry Smith 5698fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 5699fb1732b5SBarry Smith 5700fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5701fb1732b5SBarry Smith @*/ 5702721cd6eeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf) 5703fb1732b5SBarry Smith { 5704fb1732b5SBarry Smith PetscErrorCode ierr; 5705fb1732b5SBarry Smith 5706fb1732b5SBarry Smith PetscFunctionBegin; 5707721cd6eeSBarry Smith ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr); 5708721cd6eeSBarry Smith ierr = VecView(u,vf->viewer);CHKERRQ(ierr); 5709721cd6eeSBarry Smith ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr); 5710ed81e22dSJed Brown PetscFunctionReturn(0); 5711ed81e22dSJed Brown } 5712ed81e22dSJed Brown 5713ed81e22dSJed Brown /*@C 5714ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5715ed81e22dSJed Brown 5716ed81e22dSJed Brown Collective on TS 5717ed81e22dSJed Brown 5718ed81e22dSJed Brown Input Parameters: 5719ed81e22dSJed Brown + ts - the TS context 5720ed81e22dSJed Brown . step - current time-step 5721ed81e22dSJed Brown . ptime - current time 57220910c330SBarry Smith . u - current state 5723ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5724ed81e22dSJed Brown 5725ed81e22dSJed Brown Level: intermediate 5726ed81e22dSJed Brown 5727ed81e22dSJed Brown Notes: 5728ed81e22dSJed 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. 5729ed81e22dSJed Brown These are named according to the file name template. 5730ed81e22dSJed Brown 5731ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5732ed81e22dSJed Brown 5733ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5734ed81e22dSJed Brown 5735ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5736ed81e22dSJed Brown @*/ 57370910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5738ed81e22dSJed Brown { 5739ed81e22dSJed Brown PetscErrorCode ierr; 5740ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 5741ed81e22dSJed Brown PetscViewer viewer; 5742ed81e22dSJed Brown 5743ed81e22dSJed Brown PetscFunctionBegin; 574463e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 57458caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5746ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 57470910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 5748ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5749ed81e22dSJed Brown PetscFunctionReturn(0); 5750ed81e22dSJed Brown } 5751ed81e22dSJed Brown 5752ed81e22dSJed Brown /*@C 5753ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5754ed81e22dSJed Brown 5755ed81e22dSJed Brown Collective on TS 5756ed81e22dSJed Brown 5757ed81e22dSJed Brown Input Parameters: 5758ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5759ed81e22dSJed Brown 5760ed81e22dSJed Brown Level: intermediate 5761ed81e22dSJed Brown 5762ed81e22dSJed Brown Note: 5763ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5764ed81e22dSJed Brown 5765ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5766ed81e22dSJed Brown 5767ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5768ed81e22dSJed Brown @*/ 5769ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5770ed81e22dSJed Brown { 5771ed81e22dSJed Brown PetscErrorCode ierr; 5772ed81e22dSJed Brown 5773ed81e22dSJed Brown PetscFunctionBegin; 5774ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5775fb1732b5SBarry Smith PetscFunctionReturn(0); 5776fb1732b5SBarry Smith } 5777fb1732b5SBarry Smith 577884df9cb4SJed Brown /*@ 5779552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 578084df9cb4SJed Brown 5781ed81e22dSJed Brown Collective on TS if controller has not been created yet 578284df9cb4SJed Brown 578384df9cb4SJed Brown Input Arguments: 5784ed81e22dSJed Brown . ts - time stepping context 578584df9cb4SJed Brown 578684df9cb4SJed Brown Output Arguments: 5787ed81e22dSJed Brown . adapt - adaptive controller 578884df9cb4SJed Brown 578984df9cb4SJed Brown Level: intermediate 579084df9cb4SJed Brown 5791ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 579284df9cb4SJed Brown @*/ 5793552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 579484df9cb4SJed Brown { 579584df9cb4SJed Brown PetscErrorCode ierr; 579684df9cb4SJed Brown 579784df9cb4SJed Brown PetscFunctionBegin; 579884df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5799bec58848SLisandro Dalcin PetscValidPointer(adapt,2); 580084df9cb4SJed Brown if (!ts->adapt) { 5801ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 58023bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 58031c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 580484df9cb4SJed Brown } 5805bec58848SLisandro Dalcin *adapt = ts->adapt; 580684df9cb4SJed Brown PetscFunctionReturn(0); 580784df9cb4SJed Brown } 5808d6ebe24aSShri Abhyankar 58091c3436cfSJed Brown /*@ 58101c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 58111c3436cfSJed Brown 58121c3436cfSJed Brown Logically Collective 58131c3436cfSJed Brown 58141c3436cfSJed Brown Input Arguments: 58151c3436cfSJed Brown + ts - time integration context 58161c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 58170298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 58181c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 58190298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 58201c3436cfSJed Brown 5821a3cdaa26SBarry Smith Options Database keys: 5822a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5823a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5824a3cdaa26SBarry Smith 58253ff766beSShri Abhyankar Notes: 58263ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 58273ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 58283ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 58293ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 58303ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 58313ff766beSShri Abhyankar differential variables. 58323ff766beSShri Abhyankar 58331c3436cfSJed Brown Level: beginner 58341c3436cfSJed Brown 5835c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 58361c3436cfSJed Brown @*/ 58371c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 58381c3436cfSJed Brown { 58391c3436cfSJed Brown PetscErrorCode ierr; 58401c3436cfSJed Brown 58411c3436cfSJed Brown PetscFunctionBegin; 5842c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 58431c3436cfSJed Brown if (vatol) { 58441c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 58451c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 58461c3436cfSJed Brown ts->vatol = vatol; 58471c3436cfSJed Brown } 5848c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 58491c3436cfSJed Brown if (vrtol) { 58501c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 58511c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 58521c3436cfSJed Brown ts->vrtol = vrtol; 58531c3436cfSJed Brown } 58541c3436cfSJed Brown PetscFunctionReturn(0); 58551c3436cfSJed Brown } 58561c3436cfSJed Brown 5857c5033834SJed Brown /*@ 5858c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5859c5033834SJed Brown 5860c5033834SJed Brown Logically Collective 5861c5033834SJed Brown 5862c5033834SJed Brown Input Arguments: 5863c5033834SJed Brown . ts - time integration context 5864c5033834SJed Brown 5865c5033834SJed Brown Output Arguments: 58660298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 58670298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 58680298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 58690298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 5870c5033834SJed Brown 5871c5033834SJed Brown Level: beginner 5872c5033834SJed Brown 5873c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5874c5033834SJed Brown @*/ 5875c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5876c5033834SJed Brown { 5877c5033834SJed Brown PetscFunctionBegin; 5878c5033834SJed Brown if (atol) *atol = ts->atol; 5879c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5880c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5881c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 5882c5033834SJed Brown PetscFunctionReturn(0); 5883c5033834SJed Brown } 5884c5033834SJed Brown 58859c6b16b5SShri Abhyankar /*@ 5886a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 58879c6b16b5SShri Abhyankar 58889c6b16b5SShri Abhyankar Collective on TS 58899c6b16b5SShri Abhyankar 58909c6b16b5SShri Abhyankar Input Arguments: 58919c6b16b5SShri Abhyankar + ts - time stepping context 5892a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5893a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 58949c6b16b5SShri Abhyankar 58959c6b16b5SShri Abhyankar Output Arguments: 58967453f775SEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 58977453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 58987453f775SEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 58999c6b16b5SShri Abhyankar 59009c6b16b5SShri Abhyankar Level: developer 59019c6b16b5SShri Abhyankar 5902deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 59039c6b16b5SShri Abhyankar @*/ 59047453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 59059c6b16b5SShri Abhyankar { 59069c6b16b5SShri Abhyankar PetscErrorCode ierr; 59079c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 59087453f775SEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 59097453f775SEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 59109c6b16b5SShri Abhyankar const PetscScalar *u,*y; 59117453f775SEmil Constantinescu PetscReal sum,suma,sumr,gsum,gsuma,gsumr,diff; 59127453f775SEmil Constantinescu PetscReal tol,tola,tolr; 59137453f775SEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 59149c6b16b5SShri Abhyankar 59159c6b16b5SShri Abhyankar PetscFunctionBegin; 59169c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5917a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5918a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5919a4868fbcSLisandro Dalcin PetscValidType(U,2); 5920a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5921a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5922a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 59238a175baeSEmil Constantinescu PetscValidPointer(norma,5); 59248a175baeSEmil Constantinescu PetscValidPointer(normr,6); 5925a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 59269c6b16b5SShri Abhyankar 59279c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 59289c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 59299c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 59309c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 59319c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 59327453f775SEmil Constantinescu sum = 0.; n_loc = 0; 59337453f775SEmil Constantinescu suma = 0.; na_loc = 0; 59347453f775SEmil Constantinescu sumr = 0.; nr_loc = 0; 59359c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 59369c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 59379c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59389c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59399c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 59407453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 59417453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 59427453f775SEmil Constantinescu if(tola>0.){ 59437453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 59447453f775SEmil Constantinescu na_loc++; 59457453f775SEmil Constantinescu } 59467453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59477453f775SEmil Constantinescu if(tolr>0.){ 59487453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 59497453f775SEmil Constantinescu nr_loc++; 59507453f775SEmil Constantinescu } 59517453f775SEmil Constantinescu tol=tola+tolr; 59527453f775SEmil Constantinescu if(tol>0.){ 59537453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 59547453f775SEmil Constantinescu n_loc++; 59557453f775SEmil Constantinescu } 59569c6b16b5SShri Abhyankar } 59579c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59589c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59599c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 59609c6b16b5SShri Abhyankar const PetscScalar *atol; 59619c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59629c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 59637453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 59647453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 59657453f775SEmil Constantinescu if(tola>0.){ 59667453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 59677453f775SEmil Constantinescu na_loc++; 59687453f775SEmil Constantinescu } 59697453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59707453f775SEmil Constantinescu if(tolr>0.){ 59717453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 59727453f775SEmil Constantinescu nr_loc++; 59737453f775SEmil Constantinescu } 59747453f775SEmil Constantinescu tol=tola+tolr; 59757453f775SEmil Constantinescu if(tol>0.){ 59767453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 59777453f775SEmil Constantinescu n_loc++; 59787453f775SEmil Constantinescu } 59799c6b16b5SShri Abhyankar } 59809c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59819c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 59829c6b16b5SShri Abhyankar const PetscScalar *rtol; 59839c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59849c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 59857453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 59867453f775SEmil Constantinescu tola = ts->atol; 59877453f775SEmil Constantinescu if(tola>0.){ 59887453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 59897453f775SEmil Constantinescu na_loc++; 59907453f775SEmil Constantinescu } 59917453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59927453f775SEmil Constantinescu if(tolr>0.){ 59937453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 59947453f775SEmil Constantinescu nr_loc++; 59957453f775SEmil Constantinescu } 59967453f775SEmil Constantinescu tol=tola+tolr; 59977453f775SEmil Constantinescu if(tol>0.){ 59987453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 59997453f775SEmil Constantinescu n_loc++; 60007453f775SEmil Constantinescu } 60019c6b16b5SShri Abhyankar } 60029c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60039c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 60049c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 60057453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 60067453f775SEmil Constantinescu tola = ts->atol; 60077453f775SEmil Constantinescu if(tola>0.){ 60087453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 60097453f775SEmil Constantinescu na_loc++; 60107453f775SEmil Constantinescu } 60117453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60127453f775SEmil Constantinescu if(tolr>0.){ 60137453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 60147453f775SEmil Constantinescu nr_loc++; 60157453f775SEmil Constantinescu } 60167453f775SEmil Constantinescu tol=tola+tolr; 60177453f775SEmil Constantinescu if(tol>0.){ 60187453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 60197453f775SEmil Constantinescu n_loc++; 60207453f775SEmil Constantinescu } 60219c6b16b5SShri Abhyankar } 60229c6b16b5SShri Abhyankar } 60239c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 60249c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 60259c6b16b5SShri Abhyankar 60267453f775SEmil Constantinescu err_loc[0] = sum; 60277453f775SEmil Constantinescu err_loc[1] = suma; 60287453f775SEmil Constantinescu err_loc[2] = sumr; 60297453f775SEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 60307453f775SEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 60317453f775SEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 60327453f775SEmil Constantinescu 6033a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 60347453f775SEmil Constantinescu 60357453f775SEmil Constantinescu gsum = err_glb[0]; 60367453f775SEmil Constantinescu gsuma = err_glb[1]; 60377453f775SEmil Constantinescu gsumr = err_glb[2]; 60387453f775SEmil Constantinescu n_glb = err_glb[3]; 60397453f775SEmil Constantinescu na_glb = err_glb[4]; 60407453f775SEmil Constantinescu nr_glb = err_glb[5]; 60417453f775SEmil Constantinescu 6042b1316ef9SEmil Constantinescu *norm = 0.; 6043b1316ef9SEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 6044b1316ef9SEmil Constantinescu *norma = 0.; 6045b1316ef9SEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 6046b1316ef9SEmil Constantinescu *normr = 0.; 6047b1316ef9SEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 60489c6b16b5SShri Abhyankar 60499c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 60507453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 60517453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 60529c6b16b5SShri Abhyankar PetscFunctionReturn(0); 60539c6b16b5SShri Abhyankar } 60549c6b16b5SShri Abhyankar 60559c6b16b5SShri Abhyankar /*@ 6056a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 60579c6b16b5SShri Abhyankar 60589c6b16b5SShri Abhyankar Collective on TS 60599c6b16b5SShri Abhyankar 60609c6b16b5SShri Abhyankar Input Arguments: 60619c6b16b5SShri Abhyankar + ts - time stepping context 6062a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 6063a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 60649c6b16b5SShri Abhyankar 60659c6b16b5SShri Abhyankar Output Arguments: 60667453f775SEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 60677453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 60687453f775SEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 60699c6b16b5SShri Abhyankar 60709c6b16b5SShri Abhyankar Level: developer 60719c6b16b5SShri Abhyankar 6072deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 60739c6b16b5SShri Abhyankar @*/ 60747453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 60759c6b16b5SShri Abhyankar { 60769c6b16b5SShri Abhyankar PetscErrorCode ierr; 60777453f775SEmil Constantinescu PetscInt i,n,N,rstart; 60789c6b16b5SShri Abhyankar const PetscScalar *u,*y; 60797453f775SEmil Constantinescu PetscReal max,gmax,maxa,gmaxa,maxr,gmaxr; 60807453f775SEmil Constantinescu PetscReal tol,tola,tolr,diff; 60817453f775SEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 60829c6b16b5SShri Abhyankar 60839c6b16b5SShri Abhyankar PetscFunctionBegin; 60849c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6085a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 6086a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 6087a4868fbcSLisandro Dalcin PetscValidType(U,2); 6088a4868fbcSLisandro Dalcin PetscValidType(Y,3); 6089a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 6090a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 60918a175baeSEmil Constantinescu PetscValidPointer(norma,5); 60928a175baeSEmil Constantinescu PetscValidPointer(normr,6); 6093a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 60949c6b16b5SShri Abhyankar 60959c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 60969c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 60979c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 60989c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 60999c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 61007453f775SEmil Constantinescu 61017453f775SEmil Constantinescu max=0.; 61027453f775SEmil Constantinescu maxa=0.; 61037453f775SEmil Constantinescu maxr=0.; 61047453f775SEmil Constantinescu 61057453f775SEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 61069c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 61079c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61089c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61097453f775SEmil Constantinescu 61107453f775SEmil Constantinescu for (i=0; i<n; i++) { 61117453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 61127453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 61137453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61147453f775SEmil Constantinescu tol = tola+tolr; 61157453f775SEmil Constantinescu if(tola>0.){ 61167453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 61177453f775SEmil Constantinescu } 61187453f775SEmil Constantinescu if(tolr>0.){ 61197453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 61207453f775SEmil Constantinescu } 61217453f775SEmil Constantinescu if(tol>0.){ 61227453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 61237453f775SEmil Constantinescu } 61249c6b16b5SShri Abhyankar } 61259c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61269c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61279c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 61289c6b16b5SShri Abhyankar const PetscScalar *atol; 61299c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61307453f775SEmil Constantinescu for (i=0; i<n; i++) { 61317453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 61327453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 61337453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61347453f775SEmil Constantinescu tol = tola+tolr; 61357453f775SEmil Constantinescu if(tola>0.){ 61367453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 61377453f775SEmil Constantinescu } 61387453f775SEmil Constantinescu if(tolr>0.){ 61397453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 61407453f775SEmil Constantinescu } 61417453f775SEmil Constantinescu if(tol>0.){ 61427453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 61437453f775SEmil Constantinescu } 61449c6b16b5SShri Abhyankar } 61459c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61469c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 61479c6b16b5SShri Abhyankar const PetscScalar *rtol; 61489c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61497453f775SEmil Constantinescu 61507453f775SEmil Constantinescu for (i=0; i<n; i++) { 61517453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 61527453f775SEmil Constantinescu tola = ts->atol; 61537453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61547453f775SEmil Constantinescu tol = tola+tolr; 61557453f775SEmil Constantinescu if(tola>0.){ 61567453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 61577453f775SEmil Constantinescu } 61587453f775SEmil Constantinescu if(tolr>0.){ 61597453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 61607453f775SEmil Constantinescu } 61617453f775SEmil Constantinescu if(tol>0.){ 61627453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 61637453f775SEmil Constantinescu } 61649c6b16b5SShri Abhyankar } 61659c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61669c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 61677453f775SEmil Constantinescu 61687453f775SEmil Constantinescu for (i=0; i<n; i++) { 61697453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 61707453f775SEmil Constantinescu tola = ts->atol; 61717453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61727453f775SEmil Constantinescu tol = tola+tolr; 61737453f775SEmil Constantinescu if(tola>0.){ 61747453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 61757453f775SEmil Constantinescu } 61767453f775SEmil Constantinescu if(tolr>0.){ 61777453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 61787453f775SEmil Constantinescu } 61797453f775SEmil Constantinescu if(tol>0.){ 61807453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 61817453f775SEmil Constantinescu } 61829c6b16b5SShri Abhyankar } 61839c6b16b5SShri Abhyankar } 61849c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 61859c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 61867453f775SEmil Constantinescu err_loc[0] = max; 61877453f775SEmil Constantinescu err_loc[1] = maxa; 61887453f775SEmil Constantinescu err_loc[2] = maxr; 6189a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 61907453f775SEmil Constantinescu gmax = err_glb[0]; 61917453f775SEmil Constantinescu gmaxa = err_glb[1]; 61927453f775SEmil Constantinescu gmaxr = err_glb[2]; 61939c6b16b5SShri Abhyankar 61949c6b16b5SShri Abhyankar *norm = gmax; 61957453f775SEmil Constantinescu *norma = gmaxa; 61967453f775SEmil Constantinescu *normr = gmaxr; 61979c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 61987453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 61997453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 62009c6b16b5SShri Abhyankar PetscFunctionReturn(0); 62019c6b16b5SShri Abhyankar } 62029c6b16b5SShri Abhyankar 62031c3436cfSJed Brown /*@ 62048a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 62051c3436cfSJed Brown 62061c3436cfSJed Brown Collective on TS 62071c3436cfSJed Brown 62081c3436cfSJed Brown Input Arguments: 62091c3436cfSJed Brown + ts - time stepping context 6210a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 6211a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 6212a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 62137619abb3SShri 62141c3436cfSJed Brown Output Arguments: 62158a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 62168a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 62178a175baeSEmil Constantinescu . normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 6218a4868fbcSLisandro Dalcin 6219a4868fbcSLisandro Dalcin Options Database Keys: 6220a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 6221a4868fbcSLisandro Dalcin 62221c3436cfSJed Brown Level: developer 62231c3436cfSJed Brown 62248a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm 62251c3436cfSJed Brown @*/ 62267453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 62271c3436cfSJed Brown { 62288beabaa1SBarry Smith PetscErrorCode ierr; 62291c3436cfSJed Brown 62301c3436cfSJed Brown PetscFunctionBegin; 6231a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 62327453f775SEmil Constantinescu ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 6233a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 62347453f775SEmil Constantinescu ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 6235a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 62361c3436cfSJed Brown PetscFunctionReturn(0); 62371c3436cfSJed Brown } 62381c3436cfSJed Brown 62398a175baeSEmil Constantinescu 62408a175baeSEmil Constantinescu /*@ 62418a175baeSEmil Constantinescu TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances 62428a175baeSEmil Constantinescu 62438a175baeSEmil Constantinescu Collective on TS 62448a175baeSEmil Constantinescu 62458a175baeSEmil Constantinescu Input Arguments: 62468a175baeSEmil Constantinescu + ts - time stepping context 62478a175baeSEmil Constantinescu . E - error vector 62488a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 62498a175baeSEmil Constantinescu - Y - state vector, previous time step 62508a175baeSEmil Constantinescu 62518a175baeSEmil Constantinescu Output Arguments: 62528a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 62538a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 62548a175baeSEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 62558a175baeSEmil Constantinescu 62568a175baeSEmil Constantinescu Level: developer 62578a175baeSEmil Constantinescu 62588a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity() 62598a175baeSEmil Constantinescu @*/ 62608a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 62618a175baeSEmil Constantinescu { 62628a175baeSEmil Constantinescu PetscErrorCode ierr; 62638a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 62648a175baeSEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 62658a175baeSEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 62668a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 62678a175baeSEmil Constantinescu PetscReal err,sum,suma,sumr,gsum,gsuma,gsumr; 62688a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 62698a175baeSEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 62708a175baeSEmil Constantinescu 62718a175baeSEmil Constantinescu PetscFunctionBegin; 62728a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 62738a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 62748a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 62758a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 62768a175baeSEmil Constantinescu PetscValidType(E,2); 62778a175baeSEmil Constantinescu PetscValidType(U,3); 62788a175baeSEmil Constantinescu PetscValidType(Y,4); 62798a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 62808a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 62818a175baeSEmil Constantinescu PetscValidPointer(norm,5); 62828a175baeSEmil Constantinescu PetscValidPointer(norma,6); 62838a175baeSEmil Constantinescu PetscValidPointer(normr,7); 62848a175baeSEmil Constantinescu 62858a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 62868a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 62878a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 62888a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 62898a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 62908a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 62918a175baeSEmil Constantinescu sum = 0.; n_loc = 0; 62928a175baeSEmil Constantinescu suma = 0.; na_loc = 0; 62938a175baeSEmil Constantinescu sumr = 0.; nr_loc = 0; 62948a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { 62958a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 62968a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 62978a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 62988a175baeSEmil Constantinescu for (i=0; i<n; i++) { 62998a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 63008a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 63018a175baeSEmil Constantinescu if(tola>0.){ 63028a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 63038a175baeSEmil Constantinescu na_loc++; 63048a175baeSEmil Constantinescu } 63058a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 63068a175baeSEmil Constantinescu if(tolr>0.){ 63078a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 63088a175baeSEmil Constantinescu nr_loc++; 63098a175baeSEmil Constantinescu } 63108a175baeSEmil Constantinescu tol=tola+tolr; 63118a175baeSEmil Constantinescu if(tol>0.){ 63128a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 63138a175baeSEmil Constantinescu n_loc++; 63148a175baeSEmil Constantinescu } 63158a175baeSEmil Constantinescu } 63168a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 63178a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 63188a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 63198a175baeSEmil Constantinescu const PetscScalar *atol; 63208a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 63218a175baeSEmil Constantinescu for (i=0; i<n; i++) { 63228a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 63238a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 63248a175baeSEmil Constantinescu if(tola>0.){ 63258a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 63268a175baeSEmil Constantinescu na_loc++; 63278a175baeSEmil Constantinescu } 63288a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 63298a175baeSEmil Constantinescu if(tolr>0.){ 63308a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 63318a175baeSEmil Constantinescu nr_loc++; 63328a175baeSEmil Constantinescu } 63338a175baeSEmil Constantinescu tol=tola+tolr; 63348a175baeSEmil Constantinescu if(tol>0.){ 63358a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 63368a175baeSEmil Constantinescu n_loc++; 63378a175baeSEmil Constantinescu } 63388a175baeSEmil Constantinescu } 63398a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 63408a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 63418a175baeSEmil Constantinescu const PetscScalar *rtol; 63428a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 63438a175baeSEmil Constantinescu for (i=0; i<n; i++) { 63448a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 63458a175baeSEmil Constantinescu tola = ts->atol; 63468a175baeSEmil Constantinescu if(tola>0.){ 63478a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 63488a175baeSEmil Constantinescu na_loc++; 63498a175baeSEmil Constantinescu } 63508a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 63518a175baeSEmil Constantinescu if(tolr>0.){ 63528a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 63538a175baeSEmil Constantinescu nr_loc++; 63548a175baeSEmil Constantinescu } 63558a175baeSEmil Constantinescu tol=tola+tolr; 63568a175baeSEmil Constantinescu if(tol>0.){ 63578a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 63588a175baeSEmil Constantinescu n_loc++; 63598a175baeSEmil Constantinescu } 63608a175baeSEmil Constantinescu } 63618a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 63628a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 63638a175baeSEmil Constantinescu for (i=0; i<n; i++) { 63648a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 63658a175baeSEmil Constantinescu tola = ts->atol; 63668a175baeSEmil Constantinescu if(tola>0.){ 63678a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 63688a175baeSEmil Constantinescu na_loc++; 63698a175baeSEmil Constantinescu } 63708a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 63718a175baeSEmil Constantinescu if(tolr>0.){ 63728a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 63738a175baeSEmil Constantinescu nr_loc++; 63748a175baeSEmil Constantinescu } 63758a175baeSEmil Constantinescu tol=tola+tolr; 63768a175baeSEmil Constantinescu if(tol>0.){ 63778a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 63788a175baeSEmil Constantinescu n_loc++; 63798a175baeSEmil Constantinescu } 63808a175baeSEmil Constantinescu } 63818a175baeSEmil Constantinescu } 63828a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 63838a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 63848a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 63858a175baeSEmil Constantinescu 63868a175baeSEmil Constantinescu err_loc[0] = sum; 63878a175baeSEmil Constantinescu err_loc[1] = suma; 63888a175baeSEmil Constantinescu err_loc[2] = sumr; 63898a175baeSEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 63908a175baeSEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 63918a175baeSEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 63928a175baeSEmil Constantinescu 6393a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 63948a175baeSEmil Constantinescu 63958a175baeSEmil Constantinescu gsum = err_glb[0]; 63968a175baeSEmil Constantinescu gsuma = err_glb[1]; 63978a175baeSEmil Constantinescu gsumr = err_glb[2]; 63988a175baeSEmil Constantinescu n_glb = err_glb[3]; 63998a175baeSEmil Constantinescu na_glb = err_glb[4]; 64008a175baeSEmil Constantinescu nr_glb = err_glb[5]; 64018a175baeSEmil Constantinescu 64028a175baeSEmil Constantinescu *norm = 0.; 64038a175baeSEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 64048a175baeSEmil Constantinescu *norma = 0.; 64058a175baeSEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 64068a175baeSEmil Constantinescu *normr = 0.; 64078a175baeSEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 64088a175baeSEmil Constantinescu 64098a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 64108a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 64118a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 64128a175baeSEmil Constantinescu PetscFunctionReturn(0); 64138a175baeSEmil Constantinescu } 64148a175baeSEmil Constantinescu 64158a175baeSEmil Constantinescu /*@ 64168a175baeSEmil Constantinescu TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances 64178a175baeSEmil Constantinescu Collective on TS 64188a175baeSEmil Constantinescu 64198a175baeSEmil Constantinescu Input Arguments: 64208a175baeSEmil Constantinescu + ts - time stepping context 64218a175baeSEmil Constantinescu . E - error vector 64228a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 64238a175baeSEmil Constantinescu - Y - state vector, previous time step 64248a175baeSEmil Constantinescu 64258a175baeSEmil Constantinescu Output Arguments: 64268a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 64278a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 64288a175baeSEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 64298a175baeSEmil Constantinescu 64308a175baeSEmil Constantinescu Level: developer 64318a175baeSEmil Constantinescu 64328a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2() 64338a175baeSEmil Constantinescu @*/ 64348a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 64358a175baeSEmil Constantinescu { 64368a175baeSEmil Constantinescu PetscErrorCode ierr; 64378a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 64388a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 64398a175baeSEmil Constantinescu PetscReal err,max,gmax,maxa,gmaxa,maxr,gmaxr; 64408a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 64418a175baeSEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 64428a175baeSEmil Constantinescu 64438a175baeSEmil Constantinescu PetscFunctionBegin; 64448a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 64458a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 64468a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 64478a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 64488a175baeSEmil Constantinescu PetscValidType(E,2); 64498a175baeSEmil Constantinescu PetscValidType(U,3); 64508a175baeSEmil Constantinescu PetscValidType(Y,4); 64518a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 64528a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 64538a175baeSEmil Constantinescu PetscValidPointer(norm,5); 64548a175baeSEmil Constantinescu PetscValidPointer(norma,6); 64558a175baeSEmil Constantinescu PetscValidPointer(normr,7); 64568a175baeSEmil Constantinescu 64578a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 64588a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 64598a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 64608a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 64618a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 64628a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 64638a175baeSEmil Constantinescu 64648a175baeSEmil Constantinescu max=0.; 64658a175baeSEmil Constantinescu maxa=0.; 64668a175baeSEmil Constantinescu maxr=0.; 64678a175baeSEmil Constantinescu 64688a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 64698a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 64708a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 64718a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 64728a175baeSEmil Constantinescu 64738a175baeSEmil Constantinescu for (i=0; i<n; i++) { 64748a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 64758a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 64768a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 64778a175baeSEmil Constantinescu tol = tola+tolr; 64788a175baeSEmil Constantinescu if(tola>0.){ 64798a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 64808a175baeSEmil Constantinescu } 64818a175baeSEmil Constantinescu if(tolr>0.){ 64828a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 64838a175baeSEmil Constantinescu } 64848a175baeSEmil Constantinescu if(tol>0.){ 64858a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 64868a175baeSEmil Constantinescu } 64878a175baeSEmil Constantinescu } 64888a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 64898a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 64908a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 64918a175baeSEmil Constantinescu const PetscScalar *atol; 64928a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 64938a175baeSEmil Constantinescu for (i=0; i<n; i++) { 64948a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 64958a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 64968a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 64978a175baeSEmil Constantinescu tol = tola+tolr; 64988a175baeSEmil Constantinescu if(tola>0.){ 64998a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 65008a175baeSEmil Constantinescu } 65018a175baeSEmil Constantinescu if(tolr>0.){ 65028a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 65038a175baeSEmil Constantinescu } 65048a175baeSEmil Constantinescu if(tol>0.){ 65058a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 65068a175baeSEmil Constantinescu } 65078a175baeSEmil Constantinescu } 65088a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 65098a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 65108a175baeSEmil Constantinescu const PetscScalar *rtol; 65118a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 65128a175baeSEmil Constantinescu 65138a175baeSEmil Constantinescu for (i=0; i<n; i++) { 65148a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 65158a175baeSEmil Constantinescu tola = ts->atol; 65168a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 65178a175baeSEmil Constantinescu tol = tola+tolr; 65188a175baeSEmil Constantinescu if(tola>0.){ 65198a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 65208a175baeSEmil Constantinescu } 65218a175baeSEmil Constantinescu if(tolr>0.){ 65228a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 65238a175baeSEmil Constantinescu } 65248a175baeSEmil Constantinescu if(tol>0.){ 65258a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 65268a175baeSEmil Constantinescu } 65278a175baeSEmil Constantinescu } 65288a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 65298a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 65308a175baeSEmil Constantinescu 65318a175baeSEmil Constantinescu for (i=0; i<n; i++) { 65328a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 65338a175baeSEmil Constantinescu tola = ts->atol; 65348a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 65358a175baeSEmil Constantinescu tol = tola+tolr; 65368a175baeSEmil Constantinescu if(tola>0.){ 65378a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 65388a175baeSEmil Constantinescu } 65398a175baeSEmil Constantinescu if(tolr>0.){ 65408a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 65418a175baeSEmil Constantinescu } 65428a175baeSEmil Constantinescu if(tol>0.){ 65438a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 65448a175baeSEmil Constantinescu } 65458a175baeSEmil Constantinescu } 65468a175baeSEmil Constantinescu } 65478a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 65488a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 65498a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 65508a175baeSEmil Constantinescu err_loc[0] = max; 65518a175baeSEmil Constantinescu err_loc[1] = maxa; 65528a175baeSEmil Constantinescu err_loc[2] = maxr; 6553a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 65548a175baeSEmil Constantinescu gmax = err_glb[0]; 65558a175baeSEmil Constantinescu gmaxa = err_glb[1]; 65568a175baeSEmil Constantinescu gmaxr = err_glb[2]; 65578a175baeSEmil Constantinescu 65588a175baeSEmil Constantinescu *norm = gmax; 65598a175baeSEmil Constantinescu *norma = gmaxa; 65608a175baeSEmil Constantinescu *normr = gmaxr; 65618a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 65628a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 65638a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 65648a175baeSEmil Constantinescu PetscFunctionReturn(0); 65658a175baeSEmil Constantinescu } 65668a175baeSEmil Constantinescu 65678a175baeSEmil Constantinescu /*@ 65688a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 65698a175baeSEmil Constantinescu 65708a175baeSEmil Constantinescu Collective on TS 65718a175baeSEmil Constantinescu 65728a175baeSEmil Constantinescu Input Arguments: 65738a175baeSEmil Constantinescu + ts - time stepping context 65748a175baeSEmil Constantinescu . E - error vector 65758a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 65768a175baeSEmil Constantinescu . Y - state vector, previous time step 65778a175baeSEmil Constantinescu - wnormtype - norm type, either NORM_2 or NORM_INFINITY 65788a175baeSEmil Constantinescu 65798a175baeSEmil Constantinescu Output Arguments: 65808a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 65818a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 65828a175baeSEmil Constantinescu . normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 65838a175baeSEmil Constantinescu 65848a175baeSEmil Constantinescu Options Database Keys: 65858a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 65868a175baeSEmil Constantinescu 65878a175baeSEmil Constantinescu Level: developer 65888a175baeSEmil Constantinescu 65898a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 65908a175baeSEmil Constantinescu @*/ 65918a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 65928a175baeSEmil Constantinescu { 65938a175baeSEmil Constantinescu PetscErrorCode ierr; 65948a175baeSEmil Constantinescu 65958a175baeSEmil Constantinescu PetscFunctionBegin; 65968a175baeSEmil Constantinescu if (wnormtype == NORM_2) { 65978a175baeSEmil Constantinescu ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 65988a175baeSEmil Constantinescu } else if(wnormtype == NORM_INFINITY) { 65998a175baeSEmil Constantinescu ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 66008a175baeSEmil Constantinescu } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 66018a175baeSEmil Constantinescu PetscFunctionReturn(0); 66028a175baeSEmil Constantinescu } 66038a175baeSEmil Constantinescu 66048a175baeSEmil Constantinescu 66058d59e960SJed Brown /*@ 66068d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 66078d59e960SJed Brown 66088d59e960SJed Brown Logically Collective on TS 66098d59e960SJed Brown 66108d59e960SJed Brown Input Arguments: 66118d59e960SJed Brown + ts - time stepping context 66128d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 66138d59e960SJed Brown 66148d59e960SJed Brown Note: 66158d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 66168d59e960SJed Brown 66178d59e960SJed Brown Level: intermediate 66188d59e960SJed Brown 66198d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 66208d59e960SJed Brown @*/ 66218d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 66228d59e960SJed Brown { 66238d59e960SJed Brown PetscFunctionBegin; 66248d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 66258d59e960SJed Brown ts->cfltime_local = cfltime; 66268d59e960SJed Brown ts->cfltime = -1.; 66278d59e960SJed Brown PetscFunctionReturn(0); 66288d59e960SJed Brown } 66298d59e960SJed Brown 66308d59e960SJed Brown /*@ 66318d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 66328d59e960SJed Brown 66338d59e960SJed Brown Collective on TS 66348d59e960SJed Brown 66358d59e960SJed Brown Input Arguments: 66368d59e960SJed Brown . ts - time stepping context 66378d59e960SJed Brown 66388d59e960SJed Brown Output Arguments: 66398d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 66408d59e960SJed Brown 66418d59e960SJed Brown Level: advanced 66428d59e960SJed Brown 66438d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 66448d59e960SJed Brown @*/ 66458d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 66468d59e960SJed Brown { 66478d59e960SJed Brown PetscErrorCode ierr; 66488d59e960SJed Brown 66498d59e960SJed Brown PetscFunctionBegin; 66508d59e960SJed Brown if (ts->cfltime < 0) { 6651b2566f29SBarry Smith ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 66528d59e960SJed Brown } 66538d59e960SJed Brown *cfltime = ts->cfltime; 66548d59e960SJed Brown PetscFunctionReturn(0); 66558d59e960SJed Brown } 66568d59e960SJed Brown 6657d6ebe24aSShri Abhyankar /*@ 6658d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 6659d6ebe24aSShri Abhyankar 6660d6ebe24aSShri Abhyankar Input Parameters: 6661d6ebe24aSShri Abhyankar . ts - the TS context. 6662d6ebe24aSShri Abhyankar . xl - lower bound. 6663d6ebe24aSShri Abhyankar . xu - upper bound. 6664d6ebe24aSShri Abhyankar 6665d6ebe24aSShri Abhyankar Notes: 6666d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 6667e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 6668d6ebe24aSShri Abhyankar 66692bd2b0e6SSatish Balay Level: advanced 66702bd2b0e6SSatish Balay 6671d6ebe24aSShri Abhyankar @*/ 6672d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 6673d6ebe24aSShri Abhyankar { 6674d6ebe24aSShri Abhyankar PetscErrorCode ierr; 6675d6ebe24aSShri Abhyankar SNES snes; 6676d6ebe24aSShri Abhyankar 6677d6ebe24aSShri Abhyankar PetscFunctionBegin; 6678d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 6679d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 6680d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 6681d6ebe24aSShri Abhyankar } 6682d6ebe24aSShri Abhyankar 6683325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 6684c6db04a5SJed Brown #include <mex.h> 6685325fc9f4SBarry Smith 6686325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 6687325fc9f4SBarry Smith 6688325fc9f4SBarry Smith /* 6689325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 6690325fc9f4SBarry Smith TSSetFunctionMatlab(). 6691325fc9f4SBarry Smith 6692325fc9f4SBarry Smith Collective on TS 6693325fc9f4SBarry Smith 6694325fc9f4SBarry Smith Input Parameters: 6695325fc9f4SBarry Smith + snes - the TS context 66960910c330SBarry Smith - u - input vector 6697325fc9f4SBarry Smith 6698325fc9f4SBarry Smith Output Parameter: 6699325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 6700325fc9f4SBarry Smith 6701325fc9f4SBarry Smith Notes: 6702325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 6703325fc9f4SBarry Smith implementations, so most users would not generally call this routine 6704325fc9f4SBarry Smith themselves. 6705325fc9f4SBarry Smith 6706325fc9f4SBarry Smith Level: developer 6707325fc9f4SBarry Smith 6708325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6709325fc9f4SBarry Smith 6710325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6711325fc9f4SBarry Smith */ 67120910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 6713325fc9f4SBarry Smith { 6714325fc9f4SBarry Smith PetscErrorCode ierr; 6715325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6716325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 6717325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 6718325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 6719325fc9f4SBarry Smith 6720325fc9f4SBarry Smith PetscFunctionBegin; 6721325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 67220910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 67230910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 6724325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 67250910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 6726325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 6727325fc9f4SBarry Smith 6728325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 67290910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 67300910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 67310910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 6732bbd56ea5SKarl Rupp 6733325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6734325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 6735325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6736325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6737325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 6738325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 6739325fc9f4SBarry Smith prhs[6] = sctx->ctx; 6740325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 6741325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6742325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6743325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6744325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6745325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6746325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6747325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6748325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6749325fc9f4SBarry Smith PetscFunctionReturn(0); 6750325fc9f4SBarry Smith } 6751325fc9f4SBarry Smith 6752325fc9f4SBarry Smith 6753325fc9f4SBarry Smith /* 6754325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 6755325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 6756e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 6757325fc9f4SBarry Smith 6758325fc9f4SBarry Smith Logically Collective on TS 6759325fc9f4SBarry Smith 6760325fc9f4SBarry Smith Input Parameters: 6761325fc9f4SBarry Smith + ts - the TS context 6762325fc9f4SBarry Smith - func - function evaluation routine 6763325fc9f4SBarry Smith 6764325fc9f4SBarry Smith Calling sequence of func: 67650910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 6766325fc9f4SBarry Smith 6767325fc9f4SBarry Smith Level: beginner 6768325fc9f4SBarry Smith 6769325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6770325fc9f4SBarry Smith 6771325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6772325fc9f4SBarry Smith */ 6773cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 6774325fc9f4SBarry Smith { 6775325fc9f4SBarry Smith PetscErrorCode ierr; 6776325fc9f4SBarry Smith TSMatlabContext *sctx; 6777325fc9f4SBarry Smith 6778325fc9f4SBarry Smith PetscFunctionBegin; 6779325fc9f4SBarry Smith /* currently sctx is memory bleed */ 678095dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6781325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6782325fc9f4SBarry Smith /* 6783325fc9f4SBarry Smith This should work, but it doesn't 6784325fc9f4SBarry Smith sctx->ctx = ctx; 6785325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6786325fc9f4SBarry Smith */ 6787325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6788bbd56ea5SKarl Rupp 67890298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 6790325fc9f4SBarry Smith PetscFunctionReturn(0); 6791325fc9f4SBarry Smith } 6792325fc9f4SBarry Smith 6793325fc9f4SBarry Smith /* 6794325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 6795325fc9f4SBarry Smith TSSetJacobianMatlab(). 6796325fc9f4SBarry Smith 6797325fc9f4SBarry Smith Collective on TS 6798325fc9f4SBarry Smith 6799325fc9f4SBarry Smith Input Parameters: 6800cdcf91faSSean Farley + ts - the TS context 68010910c330SBarry Smith . u - input vector 6802325fc9f4SBarry Smith . A, B - the matrices 6803325fc9f4SBarry Smith - ctx - user context 6804325fc9f4SBarry Smith 6805325fc9f4SBarry Smith Level: developer 6806325fc9f4SBarry Smith 6807325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6808325fc9f4SBarry Smith 6809325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6810325fc9f4SBarry Smith @*/ 6811f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 6812325fc9f4SBarry Smith { 6813325fc9f4SBarry Smith PetscErrorCode ierr; 6814325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6815325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 6816325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 6817325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 6818325fc9f4SBarry Smith 6819325fc9f4SBarry Smith PetscFunctionBegin; 6820cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 68210910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 6822325fc9f4SBarry Smith 68230910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 6824325fc9f4SBarry Smith 6825cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 68260910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 68270910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 68280910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 68290910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 6830bbd56ea5SKarl Rupp 6831325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6832325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 6833325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6834325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6835325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 6836325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 6837325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 6838325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 6839325fc9f4SBarry Smith prhs[8] = sctx->ctx; 6840325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 6841325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6842325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6843325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6844325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6845325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6846325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6847325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6848325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 6849325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 6850325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6851325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 6852325fc9f4SBarry Smith PetscFunctionReturn(0); 6853325fc9f4SBarry Smith } 6854325fc9f4SBarry Smith 6855325fc9f4SBarry Smith 6856325fc9f4SBarry Smith /* 6857325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 6858e3c5b3baSBarry 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 6859325fc9f4SBarry Smith 6860325fc9f4SBarry Smith Logically Collective on TS 6861325fc9f4SBarry Smith 6862325fc9f4SBarry Smith Input Parameters: 6863cdcf91faSSean Farley + ts - the TS context 6864325fc9f4SBarry Smith . A,B - Jacobian matrices 6865325fc9f4SBarry Smith . func - function evaluation routine 6866325fc9f4SBarry Smith - ctx - user context 6867325fc9f4SBarry Smith 6868325fc9f4SBarry Smith Calling sequence of func: 68690910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 6870325fc9f4SBarry Smith 6871325fc9f4SBarry Smith 6872325fc9f4SBarry Smith Level: developer 6873325fc9f4SBarry Smith 6874325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6875325fc9f4SBarry Smith 6876325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6877325fc9f4SBarry Smith */ 6878cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 6879325fc9f4SBarry Smith { 6880325fc9f4SBarry Smith PetscErrorCode ierr; 6881325fc9f4SBarry Smith TSMatlabContext *sctx; 6882325fc9f4SBarry Smith 6883325fc9f4SBarry Smith PetscFunctionBegin; 6884325fc9f4SBarry Smith /* currently sctx is memory bleed */ 688595dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6886325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6887325fc9f4SBarry Smith /* 6888325fc9f4SBarry Smith This should work, but it doesn't 6889325fc9f4SBarry Smith sctx->ctx = ctx; 6890325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6891325fc9f4SBarry Smith */ 6892325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6893bbd56ea5SKarl Rupp 6894cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 6895325fc9f4SBarry Smith PetscFunctionReturn(0); 6896325fc9f4SBarry Smith } 6897325fc9f4SBarry Smith 6898b5b1a830SBarry Smith /* 6899b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 6900b5b1a830SBarry Smith 6901b5b1a830SBarry Smith Collective on TS 6902b5b1a830SBarry Smith 6903b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6904b5b1a830SBarry Smith @*/ 69050910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 6906b5b1a830SBarry Smith { 6907b5b1a830SBarry Smith PetscErrorCode ierr; 6908b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6909a530c242SBarry Smith int nlhs = 1,nrhs = 6; 6910b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 6911b5b1a830SBarry Smith long long int lx = 0,ls = 0; 6912b5b1a830SBarry Smith 6913b5b1a830SBarry Smith PetscFunctionBegin; 6914cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 69150910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 6916b5b1a830SBarry Smith 6917cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 69180910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 6919bbd56ea5SKarl Rupp 6920b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6921b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 6922b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 6923b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 6924b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 6925b5b1a830SBarry Smith prhs[5] = sctx->ctx; 6926b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 6927b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6928b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 6929b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 6930b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 6931b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 6932b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 6933b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 6934b5b1a830SBarry Smith PetscFunctionReturn(0); 6935b5b1a830SBarry Smith } 6936b5b1a830SBarry Smith 6937b5b1a830SBarry Smith 6938b5b1a830SBarry Smith /* 6939b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 6940b5b1a830SBarry Smith 6941b5b1a830SBarry Smith Level: developer 6942b5b1a830SBarry Smith 6943b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 6944b5b1a830SBarry Smith 6945b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6946b5b1a830SBarry Smith */ 6947cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 6948b5b1a830SBarry Smith { 6949b5b1a830SBarry Smith PetscErrorCode ierr; 6950b5b1a830SBarry Smith TSMatlabContext *sctx; 6951b5b1a830SBarry Smith 6952b5b1a830SBarry Smith PetscFunctionBegin; 6953b5b1a830SBarry Smith /* currently sctx is memory bleed */ 695495dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6955b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6956b5b1a830SBarry Smith /* 6957b5b1a830SBarry Smith This should work, but it doesn't 6958b5b1a830SBarry Smith sctx->ctx = ctx; 6959b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6960b5b1a830SBarry Smith */ 6961b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6962bbd56ea5SKarl Rupp 69630298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 6964b5b1a830SBarry Smith PetscFunctionReturn(0); 6965b5b1a830SBarry Smith } 6966325fc9f4SBarry Smith #endif 6967b3603a34SBarry Smith 6968b3603a34SBarry Smith /*@C 69694f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 6970b3603a34SBarry Smith in a time based line graph 6971b3603a34SBarry Smith 6972b3603a34SBarry Smith Collective on TS 6973b3603a34SBarry Smith 6974b3603a34SBarry Smith Input Parameters: 6975b3603a34SBarry Smith + ts - the TS context 6976b3603a34SBarry Smith . step - current time-step 6977b3603a34SBarry Smith . ptime - current time 69787db568b7SBarry Smith . u - current solution 69797db568b7SBarry Smith - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 6980b3603a34SBarry Smith 6981b3d3934dSBarry Smith Options Database: 69829ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 6983b3d3934dSBarry Smith 6984b3603a34SBarry Smith Level: intermediate 6985b3603a34SBarry Smith 69866934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component solutions in a separate window 6987b3603a34SBarry Smith 6988b3603a34SBarry Smith .keywords: TS, vector, monitor, view 6989b3603a34SBarry Smith 69907db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 69917db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 69927db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 69937db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 6994b3603a34SBarry Smith @*/ 69957db568b7SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6996b3603a34SBarry Smith { 6997b3603a34SBarry Smith PetscErrorCode ierr; 69987db568b7SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 6999b3603a34SBarry Smith const PetscScalar *yy; 700080666b62SBarry Smith Vec v; 7001b3603a34SBarry Smith 7002b3603a34SBarry Smith PetscFunctionBegin; 700363e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 700458ff32f7SBarry Smith if (!step) { 7005a9f9c1f6SBarry Smith PetscDrawAxis axis; 70066934998bSLisandro Dalcin PetscInt dim; 7007a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 7008a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 7009bab0a581SBarry Smith if (!ctx->names) { 7010bab0a581SBarry Smith PetscBool flg; 7011bab0a581SBarry Smith /* user provides names of variables to plot but no names has been set so assume names are integer values */ 7012bab0a581SBarry Smith ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr); 7013bab0a581SBarry Smith if (flg) { 7014bab0a581SBarry Smith PetscInt i,n; 7015bab0a581SBarry Smith char **names; 7016bab0a581SBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 7017bab0a581SBarry Smith ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr); 7018bab0a581SBarry Smith for (i=0; i<n; i++) { 7019bab0a581SBarry Smith ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr); 7020bab0a581SBarry Smith ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr); 7021bab0a581SBarry Smith } 7022bab0a581SBarry Smith names[n] = NULL; 7023bab0a581SBarry Smith ctx->names = names; 7024bab0a581SBarry Smith } 7025bab0a581SBarry Smith } 7026387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 7027387f4636SBarry Smith char **displaynames; 7028387f4636SBarry Smith PetscBool flg; 7029387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 703095dccacaSBarry Smith ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr); 7031387f4636SBarry Smith ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 7032c5929fdfSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 7033387f4636SBarry Smith if (flg) { 7034a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 7035387f4636SBarry Smith } 7036387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 7037387f4636SBarry Smith } 7038387f4636SBarry Smith if (ctx->displaynames) { 7039387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 7040387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 7041387f4636SBarry Smith } else if (ctx->names) { 70420910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 70430b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 7044387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 7045b0bc92c6SBarry Smith } else { 7046b0bc92c6SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 7047b0bc92c6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 7048387f4636SBarry Smith } 70490b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 705058ff32f7SBarry Smith } 70516934998bSLisandro Dalcin 70526934998bSLisandro Dalcin if (!ctx->transform) v = u; 70536934998bSLisandro Dalcin else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);} 705480666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 70556934998bSLisandro Dalcin if (ctx->displaynames) { 70566934998bSLisandro Dalcin PetscInt i; 70576934998bSLisandro Dalcin for (i=0; i<ctx->ndisplayvariables; i++) 70586934998bSLisandro Dalcin ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]); 70596934998bSLisandro Dalcin ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 70606934998bSLisandro Dalcin } else { 7061e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 7062e3efe391SJed Brown PetscInt i,n; 70636934998bSLisandro Dalcin PetscReal *yreal; 706480666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 7065785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 7066e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 70670b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 7068e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 7069e3efe391SJed Brown #else 70700b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 7071e3efe391SJed Brown #endif 707280666b62SBarry Smith } 70736934998bSLisandro Dalcin ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 70746934998bSLisandro Dalcin if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);} 70756934998bSLisandro Dalcin 7076b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 70770b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 70786934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 70793923b477SBarry Smith } 7080b3603a34SBarry Smith PetscFunctionReturn(0); 7081b3603a34SBarry Smith } 7082b3603a34SBarry Smith 7083387f4636SBarry Smith 7084b037adc7SBarry Smith /*@C 708531152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 7086b037adc7SBarry Smith 7087b037adc7SBarry Smith Collective on TS 7088b037adc7SBarry Smith 7089b037adc7SBarry Smith Input Parameters: 7090b037adc7SBarry Smith + ts - the TS context 7091b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 7092b037adc7SBarry Smith 7093b037adc7SBarry Smith Level: intermediate 7094b037adc7SBarry Smith 70957db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 70967db568b7SBarry Smith 7097b037adc7SBarry Smith .keywords: TS, vector, monitor, view 7098b037adc7SBarry Smith 7099a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 7100b037adc7SBarry Smith @*/ 710131152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 7102b037adc7SBarry Smith { 7103b037adc7SBarry Smith PetscErrorCode ierr; 7104b037adc7SBarry Smith PetscInt i; 7105b037adc7SBarry Smith 7106b037adc7SBarry Smith PetscFunctionBegin; 7107b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7108b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 71095537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 7110b3d3934dSBarry Smith break; 7111b3d3934dSBarry Smith } 7112b3d3934dSBarry Smith } 7113b3d3934dSBarry Smith PetscFunctionReturn(0); 7114b3d3934dSBarry Smith } 7115b3d3934dSBarry Smith 7116e673d494SBarry Smith /*@C 7117e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 7118e673d494SBarry Smith 7119e673d494SBarry Smith Collective on TS 7120e673d494SBarry Smith 7121e673d494SBarry Smith Input Parameters: 7122e673d494SBarry Smith + ts - the TS context 7123e673d494SBarry Smith - names - the names of the components, final string must be NULL 7124e673d494SBarry Smith 7125e673d494SBarry Smith Level: intermediate 7126e673d494SBarry Smith 7127e673d494SBarry Smith .keywords: TS, vector, monitor, view 7128e673d494SBarry Smith 7129a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 7130e673d494SBarry Smith @*/ 7131e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 7132e673d494SBarry Smith { 7133e673d494SBarry Smith PetscErrorCode ierr; 7134e673d494SBarry Smith 7135e673d494SBarry Smith PetscFunctionBegin; 7136e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 7137e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 7138e673d494SBarry Smith PetscFunctionReturn(0); 7139e673d494SBarry Smith } 7140e673d494SBarry Smith 7141b3d3934dSBarry Smith /*@C 7142b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 7143b3d3934dSBarry Smith 7144b3d3934dSBarry Smith Collective on TS 7145b3d3934dSBarry Smith 7146b3d3934dSBarry Smith Input Parameter: 7147b3d3934dSBarry Smith . ts - the TS context 7148b3d3934dSBarry Smith 7149b3d3934dSBarry Smith Output Parameter: 7150b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 7151b3d3934dSBarry Smith 7152b3d3934dSBarry Smith Level: intermediate 7153b3d3934dSBarry Smith 71547db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 71557db568b7SBarry Smith 7156b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7157b3d3934dSBarry Smith 7158b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 7159b3d3934dSBarry Smith @*/ 7160b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 7161b3d3934dSBarry Smith { 7162b3d3934dSBarry Smith PetscInt i; 7163b3d3934dSBarry Smith 7164b3d3934dSBarry Smith PetscFunctionBegin; 7165b3d3934dSBarry Smith *names = NULL; 7166b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7167b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 71685537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 7169b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 7170b3d3934dSBarry Smith break; 7171387f4636SBarry Smith } 7172387f4636SBarry Smith } 7173387f4636SBarry Smith PetscFunctionReturn(0); 7174387f4636SBarry Smith } 7175387f4636SBarry Smith 7176a66092f1SBarry Smith /*@C 7177a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 7178a66092f1SBarry Smith 7179a66092f1SBarry Smith Collective on TS 7180a66092f1SBarry Smith 7181a66092f1SBarry Smith Input Parameters: 7182a66092f1SBarry Smith + ctx - the TSMonitorLG context 7183a66092f1SBarry Smith . displaynames - the names of the components, final string must be NULL 7184a66092f1SBarry Smith 7185a66092f1SBarry Smith Level: intermediate 7186a66092f1SBarry Smith 7187a66092f1SBarry Smith .keywords: TS, vector, monitor, view 7188a66092f1SBarry Smith 7189a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 7190a66092f1SBarry Smith @*/ 7191a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 7192a66092f1SBarry Smith { 7193a66092f1SBarry Smith PetscInt j = 0,k; 7194a66092f1SBarry Smith PetscErrorCode ierr; 7195a66092f1SBarry Smith 7196a66092f1SBarry Smith PetscFunctionBegin; 7197a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 7198a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 7199a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 7200a66092f1SBarry Smith while (displaynames[j]) j++; 7201a66092f1SBarry Smith ctx->ndisplayvariables = j; 7202a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 7203a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 7204a66092f1SBarry Smith j = 0; 7205a66092f1SBarry Smith while (displaynames[j]) { 7206a66092f1SBarry Smith k = 0; 7207a66092f1SBarry Smith while (ctx->names[k]) { 7208a66092f1SBarry Smith PetscBool flg; 7209a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 7210a66092f1SBarry Smith if (flg) { 7211a66092f1SBarry Smith ctx->displayvariables[j] = k; 7212a66092f1SBarry Smith break; 7213a66092f1SBarry Smith } 7214a66092f1SBarry Smith k++; 7215a66092f1SBarry Smith } 7216a66092f1SBarry Smith j++; 7217a66092f1SBarry Smith } 7218a66092f1SBarry Smith PetscFunctionReturn(0); 7219a66092f1SBarry Smith } 7220a66092f1SBarry Smith 7221a66092f1SBarry Smith 7222387f4636SBarry Smith /*@C 7223387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 7224387f4636SBarry Smith 7225387f4636SBarry Smith Collective on TS 7226387f4636SBarry Smith 7227387f4636SBarry Smith Input Parameters: 7228387f4636SBarry Smith + ts - the TS context 7229387f4636SBarry Smith . displaynames - the names of the components, final string must be NULL 7230387f4636SBarry Smith 72317db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 72327db568b7SBarry Smith 7233387f4636SBarry Smith Level: intermediate 7234387f4636SBarry Smith 7235387f4636SBarry Smith .keywords: TS, vector, monitor, view 7236387f4636SBarry Smith 7237387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 7238387f4636SBarry Smith @*/ 7239387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 7240387f4636SBarry Smith { 7241a66092f1SBarry Smith PetscInt i; 7242387f4636SBarry Smith PetscErrorCode ierr; 7243387f4636SBarry Smith 7244387f4636SBarry Smith PetscFunctionBegin; 7245387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7246387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 72475537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 7248b3d3934dSBarry Smith break; 7249b037adc7SBarry Smith } 7250b037adc7SBarry Smith } 7251b037adc7SBarry Smith PetscFunctionReturn(0); 7252b037adc7SBarry Smith } 7253b037adc7SBarry Smith 725480666b62SBarry Smith /*@C 725580666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 725680666b62SBarry Smith 725780666b62SBarry Smith Collective on TS 725880666b62SBarry Smith 725980666b62SBarry Smith Input Parameters: 726080666b62SBarry Smith + ts - the TS context 726180666b62SBarry Smith . transform - the transform function 72627684fa3eSBarry Smith . destroy - function to destroy the optional context 726380666b62SBarry Smith - ctx - optional context used by transform function 726480666b62SBarry Smith 72657db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 72667db568b7SBarry Smith 726780666b62SBarry Smith Level: intermediate 726880666b62SBarry Smith 726980666b62SBarry Smith .keywords: TS, vector, monitor, view 727080666b62SBarry Smith 7271a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 727280666b62SBarry Smith @*/ 72737684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 727480666b62SBarry Smith { 727580666b62SBarry Smith PetscInt i; 7276a66092f1SBarry Smith PetscErrorCode ierr; 727780666b62SBarry Smith 727880666b62SBarry Smith PetscFunctionBegin; 727980666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 728080666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 72815537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 728280666b62SBarry Smith } 728380666b62SBarry Smith } 728480666b62SBarry Smith PetscFunctionReturn(0); 728580666b62SBarry Smith } 728680666b62SBarry Smith 7287e673d494SBarry Smith /*@C 7288e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 7289e673d494SBarry Smith 7290e673d494SBarry Smith Collective on TSLGCtx 7291e673d494SBarry Smith 7292e673d494SBarry Smith Input Parameters: 7293e673d494SBarry Smith + ts - the TS context 7294e673d494SBarry Smith . transform - the transform function 72957684fa3eSBarry Smith . destroy - function to destroy the optional context 7296e673d494SBarry Smith - ctx - optional context used by transform function 7297e673d494SBarry Smith 7298e673d494SBarry Smith Level: intermediate 7299e673d494SBarry Smith 7300e673d494SBarry Smith .keywords: TS, vector, monitor, view 7301e673d494SBarry Smith 7302a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 7303e673d494SBarry Smith @*/ 73047684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 7305e673d494SBarry Smith { 7306e673d494SBarry Smith PetscFunctionBegin; 7307e673d494SBarry Smith ctx->transform = transform; 73087684fa3eSBarry Smith ctx->transformdestroy = destroy; 7309e673d494SBarry Smith ctx->transformctx = tctx; 7310e673d494SBarry Smith PetscFunctionReturn(0); 7311e673d494SBarry Smith } 7312e673d494SBarry Smith 7313ef20d060SBarry Smith /*@C 73144f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 7315ef20d060SBarry Smith in a time based line graph 7316ef20d060SBarry Smith 7317ef20d060SBarry Smith Collective on TS 7318ef20d060SBarry Smith 7319ef20d060SBarry Smith Input Parameters: 7320ef20d060SBarry Smith + ts - the TS context 7321ef20d060SBarry Smith . step - current time-step 7322ef20d060SBarry Smith . ptime - current time 73237db568b7SBarry Smith . u - current solution 73247db568b7SBarry Smith - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 7325ef20d060SBarry Smith 7326ef20d060SBarry Smith Level: intermediate 7327ef20d060SBarry Smith 73286934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component errors in a separate window 7329abd5a294SJed Brown 7330abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 7331abd5a294SJed Brown 7332abd5a294SJed Brown Options Database Keys: 73334f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 7334ef20d060SBarry Smith 7335ef20d060SBarry Smith .keywords: TS, vector, monitor, view 7336ef20d060SBarry Smith 7337abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 7338ef20d060SBarry Smith @*/ 73390910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 7340ef20d060SBarry Smith { 7341ef20d060SBarry Smith PetscErrorCode ierr; 73420b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 7343ef20d060SBarry Smith const PetscScalar *yy; 7344ef20d060SBarry Smith Vec y; 7345ef20d060SBarry Smith 7346ef20d060SBarry Smith PetscFunctionBegin; 7347a9f9c1f6SBarry Smith if (!step) { 7348a9f9c1f6SBarry Smith PetscDrawAxis axis; 73496934998bSLisandro Dalcin PetscInt dim; 7350a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 73511ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 73520910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 7353a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 7354a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 7355a9f9c1f6SBarry Smith } 73560910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 7357ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 73580910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 7359ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 7360e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 7361e3efe391SJed Brown { 7362e3efe391SJed Brown PetscReal *yreal; 7363e3efe391SJed Brown PetscInt i,n; 7364e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 7365785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 7366e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 73670b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 7368e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 7369e3efe391SJed Brown } 7370e3efe391SJed Brown #else 73710b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 7372e3efe391SJed Brown #endif 7373ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 7374ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 7375b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 73760b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 73776934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 73783923b477SBarry Smith } 7379ef20d060SBarry Smith PetscFunctionReturn(0); 7380ef20d060SBarry Smith } 7381ef20d060SBarry Smith 7382201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 7383201da799SBarry Smith { 7384201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 7385201da799SBarry Smith PetscReal x = ptime,y; 7386201da799SBarry Smith PetscErrorCode ierr; 7387201da799SBarry Smith PetscInt its; 7388201da799SBarry Smith 7389201da799SBarry Smith PetscFunctionBegin; 739063e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 7391201da799SBarry Smith if (!n) { 7392201da799SBarry Smith PetscDrawAxis axis; 7393201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 7394201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 7395201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 7396201da799SBarry Smith ctx->snes_its = 0; 7397201da799SBarry Smith } 7398201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 7399201da799SBarry Smith y = its - ctx->snes_its; 7400201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 74013fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 7402201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 74036934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 7404201da799SBarry Smith } 7405201da799SBarry Smith ctx->snes_its = its; 7406201da799SBarry Smith PetscFunctionReturn(0); 7407201da799SBarry Smith } 7408201da799SBarry Smith 7409201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 7410201da799SBarry Smith { 7411201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 7412201da799SBarry Smith PetscReal x = ptime,y; 7413201da799SBarry Smith PetscErrorCode ierr; 7414201da799SBarry Smith PetscInt its; 7415201da799SBarry Smith 7416201da799SBarry Smith PetscFunctionBegin; 741763e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 7418201da799SBarry Smith if (!n) { 7419201da799SBarry Smith PetscDrawAxis axis; 7420201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 7421201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 7422201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 7423201da799SBarry Smith ctx->ksp_its = 0; 7424201da799SBarry Smith } 7425201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 7426201da799SBarry Smith y = its - ctx->ksp_its; 7427201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 742899fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 7429201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 74306934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 7431201da799SBarry Smith } 7432201da799SBarry Smith ctx->ksp_its = its; 7433201da799SBarry Smith PetscFunctionReturn(0); 7434201da799SBarry Smith } 7435f9c1d6abSBarry Smith 7436f9c1d6abSBarry Smith /*@ 7437f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 7438f9c1d6abSBarry Smith 7439f9c1d6abSBarry Smith Collective on TS and Vec 7440f9c1d6abSBarry Smith 7441f9c1d6abSBarry Smith Input Parameters: 7442f9c1d6abSBarry Smith + ts - the TS context 7443f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 7444f9c1d6abSBarry Smith 7445f9c1d6abSBarry Smith Output Parameters: 7446f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 7447f9c1d6abSBarry Smith 7448f9c1d6abSBarry Smith Level: developer 7449f9c1d6abSBarry Smith 7450f9c1d6abSBarry Smith .keywords: TS, compute 7451f9c1d6abSBarry Smith 7452f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 7453f9c1d6abSBarry Smith @*/ 7454f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 7455f9c1d6abSBarry Smith { 7456f9c1d6abSBarry Smith PetscErrorCode ierr; 7457f9c1d6abSBarry Smith 7458f9c1d6abSBarry Smith PetscFunctionBegin; 7459f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7460ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 7461f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 7462f9c1d6abSBarry Smith PetscFunctionReturn(0); 7463f9c1d6abSBarry Smith } 746424655328SShri 7465b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 7466b3d3934dSBarry Smith /*@C 7467b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 7468b3d3934dSBarry Smith 7469b3d3934dSBarry Smith Collective on TS 7470b3d3934dSBarry Smith 7471b3d3934dSBarry Smith Input Parameters: 7472b3d3934dSBarry Smith . ts - the ODE solver object 7473b3d3934dSBarry Smith 7474b3d3934dSBarry Smith Output Parameter: 7475b3d3934dSBarry Smith . ctx - the context 7476b3d3934dSBarry Smith 7477b3d3934dSBarry Smith Level: intermediate 7478b3d3934dSBarry Smith 7479b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso 7480b3d3934dSBarry Smith 7481b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 7482b3d3934dSBarry Smith 7483b3d3934dSBarry Smith @*/ 7484b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 7485b3d3934dSBarry Smith { 7486b3d3934dSBarry Smith PetscErrorCode ierr; 7487b3d3934dSBarry Smith 7488b3d3934dSBarry Smith PetscFunctionBegin; 7489a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 7490b3d3934dSBarry Smith PetscFunctionReturn(0); 7491b3d3934dSBarry Smith } 7492b3d3934dSBarry Smith 7493b3d3934dSBarry Smith /*@C 7494b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 7495b3d3934dSBarry Smith 7496b3d3934dSBarry Smith Collective on TS 7497b3d3934dSBarry Smith 7498b3d3934dSBarry Smith Input Parameters: 7499b3d3934dSBarry Smith + ts - the TS context 7500b3d3934dSBarry Smith . step - current time-step 7501b3d3934dSBarry Smith . ptime - current time 75027db568b7SBarry Smith . u - current solution 75037db568b7SBarry Smith - dctx - the envelope context 7504b3d3934dSBarry Smith 7505b3d3934dSBarry Smith Options Database: 7506b3d3934dSBarry Smith . -ts_monitor_envelope 7507b3d3934dSBarry Smith 7508b3d3934dSBarry Smith Level: intermediate 7509b3d3934dSBarry Smith 7510b3d3934dSBarry Smith Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 7511b3d3934dSBarry Smith 7512b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7513b3d3934dSBarry Smith 75147db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 7515b3d3934dSBarry Smith @*/ 75167db568b7SBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 7517b3d3934dSBarry Smith { 7518b3d3934dSBarry Smith PetscErrorCode ierr; 75197db568b7SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 7520b3d3934dSBarry Smith 7521b3d3934dSBarry Smith PetscFunctionBegin; 7522b3d3934dSBarry Smith if (!ctx->max) { 7523b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 7524b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 7525b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 7526b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 7527b3d3934dSBarry Smith } else { 7528b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 7529b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 7530b3d3934dSBarry Smith } 7531b3d3934dSBarry Smith PetscFunctionReturn(0); 7532b3d3934dSBarry Smith } 7533b3d3934dSBarry Smith 7534b3d3934dSBarry Smith 7535b3d3934dSBarry Smith /*@C 7536b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 7537b3d3934dSBarry Smith 7538b3d3934dSBarry Smith Collective on TS 7539b3d3934dSBarry Smith 7540b3d3934dSBarry Smith Input Parameter: 7541b3d3934dSBarry Smith . ts - the TS context 7542b3d3934dSBarry Smith 7543b3d3934dSBarry Smith Output Parameter: 7544b3d3934dSBarry Smith + max - the maximum values 7545b3d3934dSBarry Smith - min - the minimum values 7546b3d3934dSBarry Smith 75477db568b7SBarry Smith Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 75487db568b7SBarry Smith 7549b3d3934dSBarry Smith Level: intermediate 7550b3d3934dSBarry Smith 7551b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7552b3d3934dSBarry Smith 7553b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 7554b3d3934dSBarry Smith @*/ 7555b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 7556b3d3934dSBarry Smith { 7557b3d3934dSBarry Smith PetscInt i; 7558b3d3934dSBarry Smith 7559b3d3934dSBarry Smith PetscFunctionBegin; 7560b3d3934dSBarry Smith if (max) *max = NULL; 7561b3d3934dSBarry Smith if (min) *min = NULL; 7562b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7563b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 75645537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 7565b3d3934dSBarry Smith if (max) *max = ctx->max; 7566b3d3934dSBarry Smith if (min) *min = ctx->min; 7567b3d3934dSBarry Smith break; 7568b3d3934dSBarry Smith } 7569b3d3934dSBarry Smith } 7570b3d3934dSBarry Smith PetscFunctionReturn(0); 7571b3d3934dSBarry Smith } 7572b3d3934dSBarry Smith 7573b3d3934dSBarry Smith /*@C 7574b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 7575b3d3934dSBarry Smith 7576b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 7577b3d3934dSBarry Smith 7578b3d3934dSBarry Smith Input Parameter: 7579b3d3934dSBarry Smith . ctx - the monitor context 7580b3d3934dSBarry Smith 7581b3d3934dSBarry Smith Level: intermediate 7582b3d3934dSBarry Smith 7583b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy 7584b3d3934dSBarry Smith 75857db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 7586b3d3934dSBarry Smith @*/ 7587b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 7588b3d3934dSBarry Smith { 7589b3d3934dSBarry Smith PetscErrorCode ierr; 7590b3d3934dSBarry Smith 7591b3d3934dSBarry Smith PetscFunctionBegin; 7592b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 7593b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 7594b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 7595b3d3934dSBarry Smith PetscFunctionReturn(0); 7596b3d3934dSBarry Smith } 7597f2dee214SBarry Smith 759824655328SShri /*@ 759924655328SShri TSRollBack - Rolls back one time step 760024655328SShri 760124655328SShri Collective on TS 760224655328SShri 760324655328SShri Input Parameter: 760424655328SShri . ts - the TS context obtained from TSCreate() 760524655328SShri 760624655328SShri Level: advanced 760724655328SShri 760824655328SShri .keywords: TS, timestep, rollback 760924655328SShri 761024655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 761124655328SShri @*/ 761224655328SShri PetscErrorCode TSRollBack(TS ts) 761324655328SShri { 761424655328SShri PetscErrorCode ierr; 761524655328SShri 761624655328SShri PetscFunctionBegin; 761724655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7618b3de5cdeSLisandro Dalcin if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called"); 761924655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 762024655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 762124655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 762224655328SShri ts->ptime = ts->ptime_prev; 7623be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 7624be5899b3SLisandro Dalcin ts->steps--; ts->total_steps--; 762510b82f12SShri Abhyankar ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 7626b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 762724655328SShri PetscFunctionReturn(0); 762824655328SShri } 7629aeb4809dSShri Abhyankar 7630ff22ae23SHong Zhang /*@ 7631ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 7632ff22ae23SHong Zhang 7633ff22ae23SHong Zhang Input Parameter: 7634ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 7635ff22ae23SHong Zhang 7636ff22ae23SHong Zhang Level: advanced 7637ff22ae23SHong Zhang 7638ff22ae23SHong Zhang .keywords: TS, getstages 7639ff22ae23SHong Zhang 7640ff22ae23SHong Zhang .seealso: TSCreate() 7641ff22ae23SHong Zhang @*/ 7642ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns,Vec **Y) 7643ff22ae23SHong Zhang { 7644ff22ae23SHong Zhang PetscErrorCode ierr; 7645ff22ae23SHong Zhang 7646ff22ae23SHong Zhang PetscFunctionBegin; 7647ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7648ff22ae23SHong Zhang PetscValidPointer(ns,2); 7649ff22ae23SHong Zhang 7650ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 7651ff22ae23SHong Zhang else { 7652ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 7653ff22ae23SHong Zhang } 7654ff22ae23SHong Zhang PetscFunctionReturn(0); 7655ff22ae23SHong Zhang } 7656ff22ae23SHong Zhang 7657847ff0e1SMatthew G. Knepley /*@C 7658847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 7659847ff0e1SMatthew G. Knepley 7660847ff0e1SMatthew G. Knepley Collective on SNES 7661847ff0e1SMatthew G. Knepley 7662847ff0e1SMatthew G. Knepley Input Parameters: 7663847ff0e1SMatthew G. Knepley + ts - the TS context 7664847ff0e1SMatthew G. Knepley . t - current timestep 7665847ff0e1SMatthew G. Knepley . U - state vector 7666847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 7667847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 7668847ff0e1SMatthew G. Knepley - ctx - an optional user context 7669847ff0e1SMatthew G. Knepley 7670847ff0e1SMatthew G. Knepley Output Parameters: 7671847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 7672847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 7673847ff0e1SMatthew G. Knepley 7674847ff0e1SMatthew G. Knepley Level: intermediate 7675847ff0e1SMatthew G. Knepley 7676847ff0e1SMatthew G. Knepley Notes: 7677847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 7678847ff0e1SMatthew G. Knepley 7679847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 7680847ff0e1SMatthew G. Knepley 7681847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 7682847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 7683847ff0e1SMatthew G. Knepley 7684847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 7685847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 7686847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 7687847ff0e1SMatthew G. Knepley 7688847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse 7689847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 7690847ff0e1SMatthew G. Knepley @*/ 7691847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 7692847ff0e1SMatthew G. Knepley { 7693847ff0e1SMatthew G. Knepley SNES snes; 7694847ff0e1SMatthew G. Knepley MatFDColoring color; 7695847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 7696847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 7697847ff0e1SMatthew G. Knepley 7698847ff0e1SMatthew G. Knepley PetscFunctionBegin; 7699c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 7700847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 7701847ff0e1SMatthew G. Knepley if (!color) { 7702847ff0e1SMatthew G. Knepley DM dm; 7703847ff0e1SMatthew G. Knepley ISColoring iscoloring; 7704847ff0e1SMatthew G. Knepley 7705847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 7706847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 7707847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 7708847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 7709847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7710847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7711847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7712847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7713847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7714847ff0e1SMatthew G. Knepley } else { 7715847ff0e1SMatthew G. Knepley MatColoring mc; 7716847ff0e1SMatthew G. Knepley 7717847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 7718847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 7719847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 7720847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 7721847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 7722847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 7723847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7724847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7725847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7726847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7727847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7728847ff0e1SMatthew G. Knepley } 7729847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 7730847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 7731847ff0e1SMatthew G. Knepley } 7732847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 7733847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 7734847ff0e1SMatthew G. Knepley if (J != B) { 7735847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7736847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7737847ff0e1SMatthew G. Knepley } 7738847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 7739847ff0e1SMatthew G. Knepley } 774093b34091SDebojyoti Ghosh 7741cb9d8021SPierre Barbier de Reuille /*@ 7742cb9d8021SPierre Barbier de Reuille TSSetFunctionDomainError - Set the function testing if the current state vector is valid 7743cb9d8021SPierre Barbier de Reuille 7744cb9d8021SPierre Barbier de Reuille Input Parameters: 7745cb9d8021SPierre Barbier de Reuille ts - the TS context 7746cb9d8021SPierre Barbier de Reuille func - function called within TSFunctionDomainError 7747cb9d8021SPierre Barbier de Reuille 7748cb9d8021SPierre Barbier de Reuille Level: intermediate 7749cb9d8021SPierre Barbier de Reuille 7750cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain 7751cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError() 7752cb9d8021SPierre Barbier de Reuille @*/ 7753cb9d8021SPierre Barbier de Reuille 7754d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 7755cb9d8021SPierre Barbier de Reuille { 7756cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7757cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7758cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 7759cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7760cb9d8021SPierre Barbier de Reuille } 7761cb9d8021SPierre Barbier de Reuille 7762cb9d8021SPierre Barbier de Reuille /*@ 7763cb9d8021SPierre Barbier de Reuille TSFunctionDomainError - Check if the current state is valid 7764cb9d8021SPierre Barbier de Reuille 7765cb9d8021SPierre Barbier de Reuille Input Parameters: 7766cb9d8021SPierre Barbier de Reuille ts - the TS context 7767cb9d8021SPierre Barbier de Reuille stagetime - time of the simulation 7768d183316bSPierre Barbier de Reuille Y - state vector to check. 7769cb9d8021SPierre Barbier de Reuille 7770cb9d8021SPierre Barbier de Reuille Output Parameter: 7771cb9d8021SPierre Barbier de Reuille accept - Set to PETSC_FALSE if the current state vector is valid. 7772cb9d8021SPierre Barbier de Reuille 7773cb9d8021SPierre Barbier de Reuille Note: 7774cb9d8021SPierre Barbier de Reuille This function should be used to ensure the state is in a valid part of the space. 7775cb9d8021SPierre Barbier de Reuille For example, one can ensure here all values are positive. 777696a0c994SBarry Smith 777796a0c994SBarry Smith Level: advanced 7778cb9d8021SPierre Barbier de Reuille @*/ 7779d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 7780cb9d8021SPierre Barbier de Reuille { 7781cb9d8021SPierre Barbier de Reuille PetscErrorCode ierr; 7782cb9d8021SPierre Barbier de Reuille 7783cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7784cb9d8021SPierre Barbier de Reuille 7785cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7786cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 7787cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 7788d183316bSPierre Barbier de Reuille PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 7789cb9d8021SPierre Barbier de Reuille } 7790cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7791cb9d8021SPierre Barbier de Reuille } 77921ceb14c0SBarry Smith 779393b34091SDebojyoti Ghosh /*@C 7794e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 779593b34091SDebojyoti Ghosh 779693b34091SDebojyoti Ghosh Collective on MPI_Comm 779793b34091SDebojyoti Ghosh 779893b34091SDebojyoti Ghosh Input Parameter: 779993b34091SDebojyoti Ghosh . tsin - The input TS 780093b34091SDebojyoti Ghosh 780193b34091SDebojyoti Ghosh Output Parameter: 7802e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 780393b34091SDebojyoti Ghosh 78045eca1a21SEmil Constantinescu Notes: 78055eca1a21SEmil 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. 78065eca1a21SEmil Constantinescu 7807baa10174SEmil 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); 78085eca1a21SEmil Constantinescu 78095eca1a21SEmil Constantinescu Level: developer 781093b34091SDebojyoti Ghosh 7811e5168f73SEmil Constantinescu .keywords: TS, clone 7812e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 781393b34091SDebojyoti Ghosh @*/ 7814baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 781593b34091SDebojyoti Ghosh { 781693b34091SDebojyoti Ghosh TS t; 781793b34091SDebojyoti Ghosh PetscErrorCode ierr; 7818dc846ba4SSatish Balay SNES snes_start; 7819dc846ba4SSatish Balay DM dm; 7820dc846ba4SSatish Balay TSType type; 782193b34091SDebojyoti Ghosh 782293b34091SDebojyoti Ghosh PetscFunctionBegin; 782393b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 782493b34091SDebojyoti Ghosh *tsout = NULL; 782593b34091SDebojyoti Ghosh 78267a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 782793b34091SDebojyoti Ghosh 782893b34091SDebojyoti Ghosh /* General TS description */ 782993b34091SDebojyoti Ghosh t->numbermonitors = 0; 783093b34091SDebojyoti Ghosh t->setupcalled = 0; 783193b34091SDebojyoti Ghosh t->ksp_its = 0; 783293b34091SDebojyoti Ghosh t->snes_its = 0; 783393b34091SDebojyoti Ghosh t->nwork = 0; 783493b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 783593b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 783693b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 783793b34091SDebojyoti Ghosh 783834561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr); 783934561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr); 7840d15a3a53SEmil Constantinescu 784193b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr); 784293b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm);CHKERRQ(ierr); 784393b34091SDebojyoti Ghosh 784493b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 784551699248SLisandro Dalcin ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr); 784693b34091SDebojyoti Ghosh 7847e7069c78SShri t->trajectory = tsin->trajectory; 7848e7069c78SShri ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr); 7849e7069c78SShri 7850e7069c78SShri t->event = tsin->event; 78516b10a48eSSatish Balay if (t->event) t->event->refct++; 7852e7069c78SShri 785393b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 785493b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 7855e7069c78SShri t->ptime_prev = tsin->ptime_prev; 785693b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 785793b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 785893b34091SDebojyoti Ghosh t->steps = tsin->steps; 7859e7069c78SShri t->total_steps = tsin->total_steps; 786093b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 786193b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 786293b34091SDebojyoti Ghosh t->atol = tsin->atol; 786393b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 786493b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 786593b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 786693b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 786793b34091SDebojyoti Ghosh 786893b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type);CHKERRQ(ierr); 786993b34091SDebojyoti Ghosh ierr = TSSetType(t,type);CHKERRQ(ierr); 787093b34091SDebojyoti Ghosh 787193b34091SDebojyoti Ghosh t->vec_sol = NULL; 787293b34091SDebojyoti Ghosh 787393b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 787493b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 787593b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 787693b34091SDebojyoti Ghosh 787793b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 787893b34091SDebojyoti Ghosh 78790d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 78800d4fed19SBarry Smith PetscInt i; 78810d4fed19SBarry Smith ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr); 78820d4fed19SBarry Smith for (i=0; i<10; i++) { 78830d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 78840d4fed19SBarry Smith } 78850d4fed19SBarry Smith } 788693b34091SDebojyoti Ghosh *tsout = t; 788793b34091SDebojyoti Ghosh PetscFunctionReturn(0); 788893b34091SDebojyoti Ghosh } 7889