1af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 2496e6a7aSJed Brown #include <petscdmshell.h> 31e25c274SJed Brown #include <petscdmda.h> 42d5ee99bSBarry Smith #include <petscviewer.h> 52d5ee99bSBarry Smith #include <petscdraw.h> 6d763cef2SBarry Smith 7d5ba7fb7SMatthew Knepley /* Logging support */ 8d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 98d0ad7a8SHong Zhang PetscLogEvent TS_AdjointStep, TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 10d405a339SMatthew Knepley 11feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1249354f04SShri Abhyankar 132d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx { 142d5ee99bSBarry Smith PetscViewer viewer; 152d5ee99bSBarry Smith Vec initialsolution; 162d5ee99bSBarry Smith PetscBool showinitial; 172d5ee99bSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 182d5ee99bSBarry Smith PetscBool showtimestepandtime; 192d5ee99bSBarry Smith }; 202d5ee99bSBarry Smith 21fde5950dSBarry Smith /*@C 22fde5950dSBarry Smith TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 23fde5950dSBarry Smith 24fde5950dSBarry Smith Collective on TS 25fde5950dSBarry Smith 26fde5950dSBarry Smith Input Parameters: 27fde5950dSBarry Smith + ts - TS object you wish to monitor 28fde5950dSBarry Smith . name - the monitor type one is seeking 29fde5950dSBarry Smith . help - message indicating what monitoring is done 30fde5950dSBarry Smith . manual - manual page for the monitor 31fde5950dSBarry Smith . monitor - the monitor function 32fde5950dSBarry 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 33fde5950dSBarry Smith 34fde5950dSBarry Smith Level: developer 35fde5950dSBarry Smith 36fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 37fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 38fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 39fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 40fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 41fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 42fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 43fde5950dSBarry Smith @*/ 44721cd6eeSBarry 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*)) 45fde5950dSBarry Smith { 46fde5950dSBarry Smith PetscErrorCode ierr; 47fde5950dSBarry Smith PetscViewer viewer; 48fde5950dSBarry Smith PetscViewerFormat format; 49fde5950dSBarry Smith PetscBool flg; 50fde5950dSBarry Smith 51fde5950dSBarry Smith PetscFunctionBegin; 52fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 53fde5950dSBarry Smith if (flg) { 54721cd6eeSBarry Smith PetscViewerAndFormat *vf; 55721cd6eeSBarry Smith ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr); 56721cd6eeSBarry Smith ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 57fde5950dSBarry Smith if (monitorsetup) { 58721cd6eeSBarry Smith ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr); 59fde5950dSBarry Smith } 60721cd6eeSBarry Smith ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr); 61fde5950dSBarry Smith } 62fde5950dSBarry Smith PetscFunctionReturn(0); 63fde5950dSBarry Smith } 64fde5950dSBarry Smith 65fde5950dSBarry Smith /*@C 66e875c4f7SBarry Smith TSAdjointMonitorSensi - monitors the first lambda sensitivity 67287c2655SBarry Smith 68287c2655SBarry Smith Level: intermediate 69287c2655SBarry Smith 70287c2655SBarry Smith .keywords: TS, set, monitor 71287c2655SBarry Smith 72287c2655SBarry Smith .seealso: TSAdjointMonitorSet() 73287c2655SBarry Smith @*/ 74f4c7b390SBarry Smith PetscErrorCode TSAdjointMonitorSensi(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf) 75287c2655SBarry Smith { 76287c2655SBarry Smith PetscErrorCode ierr; 77287c2655SBarry Smith PetscViewer viewer = vf->viewer; 78287c2655SBarry Smith 79287c2655SBarry Smith PetscFunctionBegin; 80287c2655SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 81287c2655SBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 82287c2655SBarry Smith ierr = VecView(lambda[0],viewer);CHKERRQ(ierr); 83287c2655SBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 84287c2655SBarry Smith PetscFunctionReturn(0); 85287c2655SBarry Smith } 86287c2655SBarry Smith 87287c2655SBarry Smith /*@C 88fde5950dSBarry Smith TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 89fde5950dSBarry Smith 90fde5950dSBarry Smith Collective on TS 91fde5950dSBarry Smith 92fde5950dSBarry Smith Input Parameters: 93fde5950dSBarry Smith + ts - TS object you wish to monitor 94fde5950dSBarry Smith . name - the monitor type one is seeking 95fde5950dSBarry Smith . help - message indicating what monitoring is done 96fde5950dSBarry Smith . manual - manual page for the monitor 97fde5950dSBarry Smith . monitor - the monitor function 98fde5950dSBarry 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 99fde5950dSBarry Smith 100fde5950dSBarry Smith Level: developer 101fde5950dSBarry Smith 102fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 103fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 104fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 105fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 106fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 107fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 108fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 109fde5950dSBarry Smith @*/ 110721cd6eeSBarry 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*)) 111fde5950dSBarry Smith { 112fde5950dSBarry Smith PetscErrorCode ierr; 113fde5950dSBarry Smith PetscViewer viewer; 114fde5950dSBarry Smith PetscViewerFormat format; 115fde5950dSBarry Smith PetscBool flg; 116fde5950dSBarry Smith 117fde5950dSBarry Smith PetscFunctionBegin; 118fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 119fde5950dSBarry Smith if (flg) { 120721cd6eeSBarry Smith PetscViewerAndFormat *vf; 121721cd6eeSBarry Smith ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr); 122721cd6eeSBarry Smith ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 123fde5950dSBarry Smith if (monitorsetup) { 124721cd6eeSBarry Smith ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr); 125fde5950dSBarry Smith } 126721cd6eeSBarry Smith ierr = TSAdjointMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr); 127fde5950dSBarry Smith } 128fde5950dSBarry Smith PetscFunctionReturn(0); 129fde5950dSBarry Smith } 130fde5950dSBarry Smith 1312ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type) 1322ffb9264SLisandro Dalcin { 1332ffb9264SLisandro Dalcin PetscErrorCode ierr; 1342ffb9264SLisandro Dalcin 1352ffb9264SLisandro Dalcin PetscFunctionBegin; 136b92453a8SLisandro Dalcin PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1); 137b92453a8SLisandro Dalcin PetscValidCharPointer(default_type,2); 1382ffb9264SLisandro Dalcin if (!((PetscObject)adapt)->type_name) { 1392ffb9264SLisandro Dalcin ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr); 1402ffb9264SLisandro Dalcin } 1412ffb9264SLisandro Dalcin PetscFunctionReturn(0); 1422ffb9264SLisandro Dalcin } 1432ffb9264SLisandro Dalcin 144bdad233fSMatthew Knepley /*@ 145bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 146bdad233fSMatthew Knepley 147bdad233fSMatthew Knepley Collective on TS 148bdad233fSMatthew Knepley 149bdad233fSMatthew Knepley Input Parameter: 150bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 151bdad233fSMatthew Knepley 152bdad233fSMatthew Knepley Options Database Keys: 153b6a60446SDebojyoti Ghosh + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE 154ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 155ef85077eSLisandro Dalcin . -ts_max_time <time> - maximum time to compute to 156ef85077eSLisandro Dalcin . -ts_max_steps <steps> - maximum number of time-steps to take 157ef85077eSLisandro Dalcin . -ts_init_time <time> - initial time to start computation 158ef85077eSLisandro Dalcin . -ts_final_time <time> - final time to compute to 1593e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 160a3cdaa26SBarry 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 161a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 162a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 163a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 164a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 165a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 166ef222394SBarry Smith . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 167847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 168bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 169de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 170de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 1716934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 1728b668821SLisandro Dalcin . -ts_monitor_lg_timestep_log - Monitor log timestep size graphically 173de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 174de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 175de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 176de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 1773e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 1783e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 179fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 180e4160dc7SJulian Andrej . -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu) 181110eb670SHong Zhang . -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 182110eb670SHong Zhang . -ts_adjoint_monitor - print information at each adjoint time step 18353ea634cSHong Zhang - -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically 18453ea634cSHong Zhang 18591b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 186bdad233fSMatthew Knepley 187bdad233fSMatthew Knepley Level: beginner 188bdad233fSMatthew Knepley 189bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 190bdad233fSMatthew Knepley 191a313700dSBarry Smith .seealso: TSGetType() 192bdad233fSMatthew Knepley @*/ 1937087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 194bdad233fSMatthew Knepley { 195bc952696SBarry Smith PetscBool opt,flg,tflg; 196dfbe8321SBarry Smith PetscErrorCode ierr; 197eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 19831748224SBarry Smith PetscReal time_step; 19949354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 200d1212d36SBarry Smith char dir[16]; 201cd11d68dSLisandro Dalcin TSIFunction ifun; 2026991f827SBarry Smith const char *defaultType; 2036991f827SBarry Smith char typeName[256]; 204bdad233fSMatthew Knepley 205bdad233fSMatthew Knepley PetscFunctionBegin; 2060700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2076991f827SBarry Smith 2086991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 209cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 210cd11d68dSLisandro Dalcin 211cd11d68dSLisandro Dalcin ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 212cd11d68dSLisandro Dalcin if (((PetscObject)ts)->type_name) 213cd11d68dSLisandro Dalcin defaultType = ((PetscObject)ts)->type_name; 214cd11d68dSLisandro Dalcin else 215cd11d68dSLisandro Dalcin defaultType = ifun ? TSBEULER : TSEULER; 2166991f827SBarry Smith ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr); 2176991f827SBarry Smith if (opt) { 2186991f827SBarry Smith ierr = TSSetType(ts,typeName);CHKERRQ(ierr); 2196991f827SBarry Smith } else { 2206991f827SBarry Smith ierr = TSSetType(ts,defaultType);CHKERRQ(ierr); 2216991f827SBarry Smith } 222bdad233fSMatthew Knepley 223bdad233fSMatthew Knepley /* Handle generic TS options */ 224ef85077eSLisandro Dalcin ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 22519eac22cSLisandro Dalcin ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 2260298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 227ef85077eSLisandro Dalcin ierr = PetscOptionsReal("-ts_final_time","Final time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 22831748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 229cd11d68dSLisandro Dalcin if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);} 23049354f04SShri 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); 23149354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 2320298fd71SBarry 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); 2330298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 2340298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 2350298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 2360298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 237bdad233fSMatthew Knepley 23856f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 23956f85f32SBarry Smith { 24056f85f32SBarry Smith PetscBool set; 24156f85f32SBarry Smith flg = PETSC_FALSE; 24256f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 24356f85f32SBarry Smith if (set) { 24456f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 24556f85f32SBarry Smith } 24656f85f32SBarry Smith } 24756f85f32SBarry Smith #endif 24856f85f32SBarry Smith 249bdad233fSMatthew Knepley /* Monitor options */ 250cd11d68dSLisandro Dalcin ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr); 251fde5950dSBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr); 252fde5950dSBarry Smith ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr); 253e875c4f7SBarry Smith ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor_sensi","Monitor sensitivity in the adjoint computation","TSAdjointMonitorSensi",TSAdjointMonitorSensi,NULL);CHKERRQ(ierr); 254fde5950dSBarry Smith 2555180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2565180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 2575180491cSLisandro Dalcin 2584f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 259b3603a34SBarry Smith if (opt) { 2600b039ecaSBarry Smith TSMonitorLGCtx ctx; 2613923b477SBarry Smith PetscInt howoften = 1; 262b3603a34SBarry Smith 2630298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 2646ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2654f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 266bdad233fSMatthew Knepley } 2676ba87a44SLisandro Dalcin 2684f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 269ef20d060SBarry Smith if (opt) { 2700b039ecaSBarry Smith TSMonitorLGCtx ctx; 2713923b477SBarry Smith PetscInt howoften = 1; 272ef20d060SBarry Smith 2730298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 2746ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2754f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 276ef20d060SBarry Smith } 2776934998bSLisandro Dalcin 2786934998bSLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 2796934998bSLisandro Dalcin if (opt) { 2806934998bSLisandro Dalcin TSMonitorLGCtx ctx; 2816934998bSLisandro Dalcin PetscInt howoften = 1; 2826934998bSLisandro Dalcin 2836934998bSLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 2846ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2856934998bSLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 2866934998bSLisandro Dalcin } 2878b668821SLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 2888b668821SLisandro Dalcin if (opt) { 2898b668821SLisandro Dalcin TSMonitorLGCtx ctx; 2908b668821SLisandro Dalcin PetscInt howoften = 1; 2918b668821SLisandro Dalcin 2928b668821SLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 2938b668821SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2948b668821SLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 2958b668821SLisandro Dalcin ctx->semilogy = PETSC_TRUE; 2968b668821SLisandro Dalcin } 2978b668821SLisandro Dalcin 298201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 299201da799SBarry Smith if (opt) { 300201da799SBarry Smith TSMonitorLGCtx ctx; 301201da799SBarry Smith PetscInt howoften = 1; 302201da799SBarry Smith 3030298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 3046ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 305201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 306201da799SBarry Smith } 307201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 308201da799SBarry Smith if (opt) { 309201da799SBarry Smith TSMonitorLGCtx ctx; 310201da799SBarry Smith PetscInt howoften = 1; 311201da799SBarry Smith 3120298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 3136ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 314201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 315201da799SBarry Smith } 3168189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 3178189c53fSBarry Smith if (opt) { 3188189c53fSBarry Smith TSMonitorSPEigCtx ctx; 3198189c53fSBarry Smith PetscInt howoften = 1; 3208189c53fSBarry Smith 3210298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 3226934998bSLisandro Dalcin ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 3238189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 3248189c53fSBarry Smith } 325ef20d060SBarry Smith opt = PETSC_FALSE; 3260dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 327a7cc72afSBarry Smith if (opt) { 32883a4ac43SBarry Smith TSMonitorDrawCtx ctx; 32983a4ac43SBarry Smith PetscInt howoften = 1; 330a80ad3e0SBarry Smith 3310298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 3326934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 33383a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 334bdad233fSMatthew Knepley } 335fb1732b5SBarry Smith opt = PETSC_FALSE; 3369110b2e7SHong Zhang ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr); 3379110b2e7SHong Zhang if (opt) { 3389110b2e7SHong Zhang TSMonitorDrawCtx ctx; 3399110b2e7SHong Zhang PetscInt howoften = 1; 3409110b2e7SHong Zhang 3419110b2e7SHong Zhang ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr); 3426934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 3439110b2e7SHong Zhang ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3449110b2e7SHong Zhang } 3459110b2e7SHong Zhang opt = PETSC_FALSE; 3462d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 3472d5ee99bSBarry Smith if (opt) { 3482d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 3492d5ee99bSBarry Smith PetscReal bounds[4]; 3502d5ee99bSBarry Smith PetscInt n = 4; 3512d5ee99bSBarry Smith PetscDraw draw; 3526934998bSLisandro Dalcin PetscDrawAxis axis; 3532d5ee99bSBarry Smith 3542d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 3552d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 3566934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr); 3572d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 3586934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr); 3596934998bSLisandro Dalcin ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 3606934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 3612d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3622d5ee99bSBarry Smith } 3632d5ee99bSBarry Smith opt = PETSC_FALSE; 3640dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 3653a471f94SBarry Smith if (opt) { 36683a4ac43SBarry Smith TSMonitorDrawCtx ctx; 36783a4ac43SBarry Smith PetscInt howoften = 1; 3683a471f94SBarry Smith 3690298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 3706934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 37183a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3723a471f94SBarry Smith } 373fde5950dSBarry Smith 374ed81e22dSJed Brown opt = PETSC_FALSE; 37591b97e58SBarry 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); 376ed81e22dSJed Brown if (flg) { 377ed81e22dSJed Brown const char *ptr,*ptr2; 378ed81e22dSJed Brown char *filetemplate; 379ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 380ed81e22dSJed Brown /* Do some cursory validation of the input. */ 381ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 382ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 383ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 384ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 385ce94432eSBarry 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"); 386ed81e22dSJed Brown if (ptr2) break; 387ed81e22dSJed Brown } 388ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 389ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 390ed81e22dSJed Brown } 391bdad233fSMatthew Knepley 392d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 393d1212d36SBarry Smith if (flg) { 394d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 395d1212d36SBarry Smith int ray = 0; 396d1212d36SBarry Smith DMDADirection ddir; 397d1212d36SBarry Smith DM da; 398d1212d36SBarry Smith PetscMPIInt rank; 399d1212d36SBarry Smith 400ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 401d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 402d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 403ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 404d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 405d1212d36SBarry Smith 406d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 407b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 408d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 409d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 410ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 411d1212d36SBarry Smith if (!rank) { 412d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 413d1212d36SBarry Smith } 41451b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 415d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 416d1212d36SBarry Smith } 41751b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 41851b4a12fSMatthew G. Knepley if (flg) { 41951b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 42051b4a12fSMatthew G. Knepley int ray = 0; 42151b4a12fSMatthew G. Knepley DMDADirection ddir; 42251b4a12fSMatthew G. Knepley DM da; 42351b4a12fSMatthew G. Knepley PetscInt howoften = 1; 424d1212d36SBarry Smith 42551b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 42651b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 42751b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 42851b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 42951b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 4301c3436cfSJed Brown 43151b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 432b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 43351b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 43451b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 43551b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 43651b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 43751b4a12fSMatthew G. Knepley } 438a7a1495cSBarry Smith 439b3d3934dSBarry Smith ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 440b3d3934dSBarry Smith if (opt) { 441b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 442b3d3934dSBarry Smith 443b3d3934dSBarry Smith ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 444b3d3934dSBarry Smith ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 445b3d3934dSBarry Smith } 446b3d3934dSBarry Smith 447847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 448847ff0e1SMatthew G. Knepley ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr); 449847ff0e1SMatthew G. Knepley if (flg) { 450847ff0e1SMatthew G. Knepley DM dm; 451847ff0e1SMatthew G. Knepley DMTS tdm; 452847ff0e1SMatthew G. Knepley 453847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 454847ff0e1SMatthew G. Knepley ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr); 455847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 456847ff0e1SMatthew G. Knepley ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr); 457847ff0e1SMatthew G. Knepley ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 458847ff0e1SMatthew G. Knepley } 459847ff0e1SMatthew G. Knepley 460d763cef2SBarry Smith /* Handle specific TS options */ 461d763cef2SBarry Smith if (ts->ops->setfromoptions) { 4626991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 463d763cef2SBarry Smith } 464fbc52257SHong Zhang 465a7bdc993SLisandro Dalcin /* Handle TSAdapt options */ 466a7bdc993SLisandro Dalcin ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 467a7bdc993SLisandro Dalcin ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr); 468a7bdc993SLisandro Dalcin ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr); 469a7bdc993SLisandro Dalcin 47068bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 4714f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 47268bece0bSHong Zhang ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 47368bece0bSHong Zhang if (tflg) { 47468bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 47568bece0bSHong Zhang } 4764f122a70SLisandro Dalcin tflg = ts->adjoint_solve ? PETSC_TRUE : PETSC_FALSE; 47768bece0bSHong Zhang ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr); 47868bece0bSHong Zhang if (flg) { 47968bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 48068bece0bSHong Zhang ts->adjoint_solve = tflg; 48168bece0bSHong Zhang } 482d763cef2SBarry Smith 483d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 4840633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr); 485fbc52257SHong Zhang ierr = PetscOptionsEnd();CHKERRQ(ierr); 486d763cef2SBarry Smith 4874f122a70SLisandro Dalcin if (ts->trajectory) { 4884f122a70SLisandro Dalcin ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 489d763cef2SBarry Smith } 49068bece0bSHong Zhang 4914f122a70SLisandro Dalcin ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr); 4924f122a70SLisandro Dalcin if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);} 4934f122a70SLisandro Dalcin ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr); 494d763cef2SBarry Smith PetscFunctionReturn(0); 495d763cef2SBarry Smith } 496d763cef2SBarry Smith 497d2daff3dSHong Zhang /*@ 49878fbdcc8SBarry Smith TSGetTrajectory - Gets the trajectory from a TS if it exists 49978fbdcc8SBarry Smith 50078fbdcc8SBarry Smith Collective on TS 50178fbdcc8SBarry Smith 50278fbdcc8SBarry Smith Input Parameters: 50378fbdcc8SBarry Smith . ts - the TS context obtained from TSCreate() 50478fbdcc8SBarry Smith 50578fbdcc8SBarry Smith Output Parameters; 50678fbdcc8SBarry Smith . tr - the TSTrajectory object, if it exists 50778fbdcc8SBarry Smith 50878fbdcc8SBarry Smith Note: This routine should be called after all TS options have been set 50978fbdcc8SBarry Smith 51078fbdcc8SBarry Smith Level: advanced 51178fbdcc8SBarry Smith 51278fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate() 51378fbdcc8SBarry Smith 51478fbdcc8SBarry Smith .keywords: TS, set, checkpoint, 51578fbdcc8SBarry Smith @*/ 51678fbdcc8SBarry Smith PetscErrorCode TSGetTrajectory(TS ts,TSTrajectory *tr) 51778fbdcc8SBarry Smith { 51878fbdcc8SBarry Smith PetscFunctionBegin; 51978fbdcc8SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 52078fbdcc8SBarry Smith *tr = ts->trajectory; 52178fbdcc8SBarry Smith PetscFunctionReturn(0); 52278fbdcc8SBarry Smith } 52378fbdcc8SBarry Smith 52478fbdcc8SBarry Smith /*@ 525bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 526d2daff3dSHong Zhang 527d2daff3dSHong Zhang Collective on TS 528d2daff3dSHong Zhang 529d2daff3dSHong Zhang Input Parameters: 530bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 531bc952696SBarry Smith 53278fbdcc8SBarry Smith Options Database: 53378fbdcc8SBarry Smith + -ts_save_trajectory - saves the trajectory to a file 53478fbdcc8SBarry Smith - -ts_trajectory_type type 53578fbdcc8SBarry Smith 53668bece0bSHong Zhang Note: This routine should be called after all TS options have been set 537d2daff3dSHong Zhang 53878fbdcc8SBarry Smith The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/bin/PetscBinaryIOTrajectory.py and 53978fbdcc8SBarry Smith MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m 54078fbdcc8SBarry Smith 541d2daff3dSHong Zhang Level: intermediate 542d2daff3dSHong Zhang 54378fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectoryType, TSSetTrajectoryType() 544d2daff3dSHong Zhang 545bc952696SBarry Smith .keywords: TS, set, checkpoint, 546d2daff3dSHong Zhang @*/ 547bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 548d2daff3dSHong Zhang { 549bc952696SBarry Smith PetscErrorCode ierr; 550bc952696SBarry Smith 551d2daff3dSHong Zhang PetscFunctionBegin; 552d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 553bc952696SBarry Smith if (!ts->trajectory) { 554bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 555972caf09SHong Zhang ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 556bc952696SBarry Smith } 557d2daff3dSHong Zhang PetscFunctionReturn(0); 558d2daff3dSHong Zhang } 559d2daff3dSHong Zhang 560a7a1495cSBarry Smith /*@ 561a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 562a7a1495cSBarry Smith set with TSSetRHSJacobian(). 563a7a1495cSBarry Smith 564a7a1495cSBarry Smith Collective on TS and Vec 565a7a1495cSBarry Smith 566a7a1495cSBarry Smith Input Parameters: 567316643e7SJed Brown + ts - the TS context 568a7a1495cSBarry Smith . t - current timestep 5690910c330SBarry Smith - U - input vector 570a7a1495cSBarry Smith 571a7a1495cSBarry Smith Output Parameters: 572a7a1495cSBarry Smith + A - Jacobian matrix 573a7a1495cSBarry Smith . B - optional preconditioning matrix 574a7a1495cSBarry Smith - flag - flag indicating matrix structure 575a7a1495cSBarry Smith 576a7a1495cSBarry Smith Notes: 577a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 578a7a1495cSBarry Smith is used internally within the nonlinear solvers. 579a7a1495cSBarry Smith 58094b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 581a7a1495cSBarry Smith flag parameter. 582a7a1495cSBarry Smith 583a7a1495cSBarry Smith Level: developer 584a7a1495cSBarry Smith 585a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 586a7a1495cSBarry Smith 58794b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 588a7a1495cSBarry Smith @*/ 589d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 590a7a1495cSBarry Smith { 591dfbe8321SBarry Smith PetscErrorCode ierr; 592270bf2e7SJed Brown PetscObjectState Ustate; 5936c1e1eecSBarry Smith PetscObjectId Uid; 59424989b8cSPeter Brune DM dm; 595942e3340SBarry Smith DMTS tsdm; 59624989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 59724989b8cSPeter Brune void *ctx; 59824989b8cSPeter Brune TSIJacobian ijacobianfunc; 599b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 600a7a1495cSBarry Smith 601a7a1495cSBarry Smith PetscFunctionBegin; 6020700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6030910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6040910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 60524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 606942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 60724989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 6080298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 609b2df71adSDebojyoti Ghosh ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 61059e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 6116c1e1eecSBarry Smith ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr); 6126c1e1eecSBarry Smith if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 6130e4ef248SJed Brown PetscFunctionReturn(0); 614f8ede8e7SJed Brown } 615d90be118SSean Farley 616ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 617d90be118SSean Farley 618e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 61994ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 62094ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 62194ab13aaSBarry Smith if (A != B) { 62294ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 62394ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 624e1244c69SJed Brown } 625e1244c69SJed Brown ts->rhsjacobian.shift = 0; 626e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 627e1244c69SJed Brown } 628e1244c69SJed Brown 62924989b8cSPeter Brune if (rhsjacobianfunc) { 6306cd88445SBarry Smith PetscBool missing; 63194ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 632a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 633d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 634a7a1495cSBarry Smith PetscStackPop; 63594ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 6366cd88445SBarry Smith if (A) { 6376cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 6386cd88445SBarry 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"); 6396cd88445SBarry Smith } 6406cd88445SBarry Smith if (B && B != A) { 6416cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 6426cd88445SBarry 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"); 6436cd88445SBarry Smith } 644ef66eb69SBarry Smith } else { 64594ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 64694ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 647ef66eb69SBarry Smith } 6480e4ef248SJed Brown ts->rhsjacobian.time = t; 6497f79407eSBarry Smith ierr = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr); 65059e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 651a7a1495cSBarry Smith PetscFunctionReturn(0); 652a7a1495cSBarry Smith } 653a7a1495cSBarry Smith 654316643e7SJed Brown /*@ 655d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 656d763cef2SBarry Smith 657316643e7SJed Brown Collective on TS and Vec 658316643e7SJed Brown 659316643e7SJed Brown Input Parameters: 660316643e7SJed Brown + ts - the TS context 661316643e7SJed Brown . t - current time 6620910c330SBarry Smith - U - state vector 663316643e7SJed Brown 664316643e7SJed Brown Output Parameter: 665316643e7SJed Brown . y - right hand side 666316643e7SJed Brown 667316643e7SJed Brown Note: 668316643e7SJed Brown Most users should not need to explicitly call this routine, as it 669316643e7SJed Brown is used internally within the nonlinear solvers. 670316643e7SJed Brown 671316643e7SJed Brown Level: developer 672316643e7SJed Brown 673316643e7SJed Brown .keywords: TS, compute 674316643e7SJed Brown 675316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 676316643e7SJed Brown @*/ 6770910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 678d763cef2SBarry Smith { 679dfbe8321SBarry Smith PetscErrorCode ierr; 68024989b8cSPeter Brune TSRHSFunction rhsfunction; 68124989b8cSPeter Brune TSIFunction ifunction; 68224989b8cSPeter Brune void *ctx; 68324989b8cSPeter Brune DM dm; 68424989b8cSPeter Brune 685d763cef2SBarry Smith PetscFunctionBegin; 6860700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6870910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6880700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 68924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 69024989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 6910298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 692d763cef2SBarry Smith 693ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 694d763cef2SBarry Smith 6950910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 69624989b8cSPeter Brune if (rhsfunction) { 697d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 6980910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 699d763cef2SBarry Smith PetscStackPop; 700214bc6a2SJed Brown } else { 701214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 702b2cd27e8SJed Brown } 70344a41b28SSean Farley 7040910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 705d763cef2SBarry Smith PetscFunctionReturn(0); 706d763cef2SBarry Smith } 707d763cef2SBarry Smith 708ef20d060SBarry Smith /*@ 709ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 710ef20d060SBarry Smith 711ef20d060SBarry Smith Collective on TS and Vec 712ef20d060SBarry Smith 713ef20d060SBarry Smith Input Parameters: 714ef20d060SBarry Smith + ts - the TS context 715ef20d060SBarry Smith - t - current time 716ef20d060SBarry Smith 717ef20d060SBarry Smith Output Parameter: 7180910c330SBarry Smith . U - the solution 719ef20d060SBarry Smith 720ef20d060SBarry Smith Note: 721ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 722ef20d060SBarry Smith is used internally within the nonlinear solvers. 723ef20d060SBarry Smith 724ef20d060SBarry Smith Level: developer 725ef20d060SBarry Smith 726ef20d060SBarry Smith .keywords: TS, compute 727ef20d060SBarry Smith 728abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 729ef20d060SBarry Smith @*/ 7300910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 731ef20d060SBarry Smith { 732ef20d060SBarry Smith PetscErrorCode ierr; 733ef20d060SBarry Smith TSSolutionFunction solutionfunction; 734ef20d060SBarry Smith void *ctx; 735ef20d060SBarry Smith DM dm; 736ef20d060SBarry Smith 737ef20d060SBarry Smith PetscFunctionBegin; 738ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7390910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 740ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 741ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 742ef20d060SBarry Smith 743ef20d060SBarry Smith if (solutionfunction) { 7449b7cd975SBarry Smith PetscStackPush("TS user solution function"); 7450910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 746ef20d060SBarry Smith PetscStackPop; 747ef20d060SBarry Smith } 748ef20d060SBarry Smith PetscFunctionReturn(0); 749ef20d060SBarry Smith } 7509b7cd975SBarry Smith /*@ 7519b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 7529b7cd975SBarry Smith 7539b7cd975SBarry Smith Collective on TS and Vec 7549b7cd975SBarry Smith 7559b7cd975SBarry Smith Input Parameters: 7569b7cd975SBarry Smith + ts - the TS context 7579b7cd975SBarry Smith - t - current time 7589b7cd975SBarry Smith 7599b7cd975SBarry Smith Output Parameter: 7609b7cd975SBarry Smith . U - the function value 7619b7cd975SBarry Smith 7629b7cd975SBarry Smith Note: 7639b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 7649b7cd975SBarry Smith is used internally within the nonlinear solvers. 7659b7cd975SBarry Smith 7669b7cd975SBarry Smith Level: developer 7679b7cd975SBarry Smith 7689b7cd975SBarry Smith .keywords: TS, compute 7699b7cd975SBarry Smith 7709b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 7719b7cd975SBarry Smith @*/ 7729b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 7739b7cd975SBarry Smith { 7749b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 7759b7cd975SBarry Smith void *ctx; 7769b7cd975SBarry Smith DM dm; 7779b7cd975SBarry Smith 7789b7cd975SBarry Smith PetscFunctionBegin; 7799b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7809b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7819b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 7829b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 7839b7cd975SBarry Smith 7849b7cd975SBarry Smith if (forcing) { 7859b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 7869b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 7879b7cd975SBarry Smith PetscStackPop; 7889b7cd975SBarry Smith } 7899b7cd975SBarry Smith PetscFunctionReturn(0); 7909b7cd975SBarry Smith } 791ef20d060SBarry Smith 792214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 793214bc6a2SJed Brown { 7942dd45cf8SJed Brown Vec F; 795214bc6a2SJed Brown PetscErrorCode ierr; 796214bc6a2SJed Brown 797214bc6a2SJed Brown PetscFunctionBegin; 7980298fd71SBarry Smith *Frhs = NULL; 7990298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 800214bc6a2SJed Brown if (!ts->Frhs) { 8012dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 802214bc6a2SJed Brown } 803214bc6a2SJed Brown *Frhs = ts->Frhs; 804214bc6a2SJed Brown PetscFunctionReturn(0); 805214bc6a2SJed Brown } 806214bc6a2SJed Brown 807214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 808214bc6a2SJed Brown { 809214bc6a2SJed Brown Mat A,B; 8102dd45cf8SJed Brown PetscErrorCode ierr; 81141a1d4d2SBarry Smith TSIJacobian ijacobian; 812214bc6a2SJed Brown 813214bc6a2SJed Brown PetscFunctionBegin; 814c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 815c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 81641a1d4d2SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr); 817214bc6a2SJed Brown if (Arhs) { 818214bc6a2SJed Brown if (!ts->Arhs) { 81941a1d4d2SBarry Smith if (ijacobian) { 820214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 82141a1d4d2SBarry Smith } else { 82241a1d4d2SBarry Smith ts->Arhs = A; 82341a1d4d2SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 82441a1d4d2SBarry Smith } 8253565c898SBarry Smith } else { 8263565c898SBarry Smith PetscBool flg; 8273565c898SBarry Smith ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr); 8283565c898SBarry Smith /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */ 8293565c898SBarry Smith if (flg && !ijacobian && ts->Arhs == ts->Brhs){ 8303565c898SBarry Smith ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr); 8313565c898SBarry Smith ts->Arhs = A; 8323565c898SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 8333565c898SBarry Smith } 834214bc6a2SJed Brown } 835214bc6a2SJed Brown *Arhs = ts->Arhs; 836214bc6a2SJed Brown } 837214bc6a2SJed Brown if (Brhs) { 838214bc6a2SJed Brown if (!ts->Brhs) { 839bdb70873SJed Brown if (A != B) { 84041a1d4d2SBarry Smith if (ijacobian) { 841214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 842bdb70873SJed Brown } else { 84341a1d4d2SBarry Smith ts->Brhs = B; 84441a1d4d2SBarry Smith ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 84541a1d4d2SBarry Smith } 84641a1d4d2SBarry Smith } else { 847bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 84851699248SLisandro Dalcin ts->Brhs = ts->Arhs; 849bdb70873SJed Brown } 850214bc6a2SJed Brown } 851214bc6a2SJed Brown *Brhs = ts->Brhs; 852214bc6a2SJed Brown } 853214bc6a2SJed Brown PetscFunctionReturn(0); 854214bc6a2SJed Brown } 855214bc6a2SJed Brown 856316643e7SJed Brown /*@ 8570910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 858316643e7SJed Brown 859316643e7SJed Brown Collective on TS and Vec 860316643e7SJed Brown 861316643e7SJed Brown Input Parameters: 862316643e7SJed Brown + ts - the TS context 863316643e7SJed Brown . t - current time 8640910c330SBarry Smith . U - state vector 8650910c330SBarry Smith . Udot - time derivative of state vector 866214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 867316643e7SJed Brown 868316643e7SJed Brown Output Parameter: 869316643e7SJed Brown . Y - right hand side 870316643e7SJed Brown 871316643e7SJed Brown Note: 872316643e7SJed Brown Most users should not need to explicitly call this routine, as it 873316643e7SJed Brown is used internally within the nonlinear solvers. 874316643e7SJed Brown 875316643e7SJed Brown If the user did did not write their equations in implicit form, this 876316643e7SJed Brown function recasts them in implicit form. 877316643e7SJed Brown 878316643e7SJed Brown Level: developer 879316643e7SJed Brown 880316643e7SJed Brown .keywords: TS, compute 881316643e7SJed Brown 882316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 883316643e7SJed Brown @*/ 8840910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 885316643e7SJed Brown { 886316643e7SJed Brown PetscErrorCode ierr; 88724989b8cSPeter Brune TSIFunction ifunction; 88824989b8cSPeter Brune TSRHSFunction rhsfunction; 88924989b8cSPeter Brune void *ctx; 89024989b8cSPeter Brune DM dm; 891316643e7SJed Brown 892316643e7SJed Brown PetscFunctionBegin; 8930700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8940910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8950910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 8960700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 897316643e7SJed Brown 89824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 89924989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 9000298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 90124989b8cSPeter Brune 902ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 903d90be118SSean Farley 9040910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 90524989b8cSPeter Brune if (ifunction) { 906316643e7SJed Brown PetscStackPush("TS user implicit function"); 9070910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 908316643e7SJed Brown PetscStackPop; 909214bc6a2SJed Brown } 910214bc6a2SJed Brown if (imex) { 91124989b8cSPeter Brune if (!ifunction) { 9120910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 9132dd45cf8SJed Brown } 91424989b8cSPeter Brune } else if (rhsfunction) { 91524989b8cSPeter Brune if (ifunction) { 916214bc6a2SJed Brown Vec Frhs; 917214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 9180910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 919214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 9202dd45cf8SJed Brown } else { 9210910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 9220910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 923316643e7SJed Brown } 9244a6899ffSJed Brown } 9250910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 926316643e7SJed Brown PetscFunctionReturn(0); 927316643e7SJed Brown } 928316643e7SJed Brown 929316643e7SJed Brown /*@ 930316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 931316643e7SJed Brown 932316643e7SJed Brown Collective on TS and Vec 933316643e7SJed Brown 934316643e7SJed Brown Input 935316643e7SJed Brown Input Parameters: 936316643e7SJed Brown + ts - the TS context 937316643e7SJed Brown . t - current timestep 9380910c330SBarry Smith . U - state vector 9390910c330SBarry Smith . Udot - time derivative of state vector 940214bc6a2SJed Brown . shift - shift to apply, see note below 941214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 942316643e7SJed Brown 943316643e7SJed Brown Output Parameters: 944316643e7SJed Brown + A - Jacobian matrix 9453565c898SBarry Smith - B - matrix from which the preconditioner is constructed; often the same as A 946316643e7SJed Brown 947316643e7SJed Brown Notes: 9480910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 949316643e7SJed Brown 9500910c330SBarry Smith dF/dU + shift*dF/dUdot 951316643e7SJed Brown 952316643e7SJed Brown Most users should not need to explicitly call this routine, as it 953316643e7SJed Brown is used internally within the nonlinear solvers. 954316643e7SJed Brown 955316643e7SJed Brown Level: developer 956316643e7SJed Brown 957316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 958316643e7SJed Brown 959316643e7SJed Brown .seealso: TSSetIJacobian() 96063495f91SJed Brown @*/ 961d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 962316643e7SJed Brown { 963316643e7SJed Brown PetscErrorCode ierr; 96424989b8cSPeter Brune TSIJacobian ijacobian; 96524989b8cSPeter Brune TSRHSJacobian rhsjacobian; 96624989b8cSPeter Brune DM dm; 96724989b8cSPeter Brune void *ctx; 968316643e7SJed Brown 969316643e7SJed Brown PetscFunctionBegin; 9700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9710910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 9720910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 973316643e7SJed Brown PetscValidPointer(A,6); 97494ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 975316643e7SJed Brown PetscValidPointer(B,7); 97694ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 97724989b8cSPeter Brune 97824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 97924989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 9800298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 98124989b8cSPeter Brune 982ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 983316643e7SJed Brown 98494ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 98524989b8cSPeter Brune if (ijacobian) { 9866cd88445SBarry Smith PetscBool missing; 987316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 988d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 989316643e7SJed Brown PetscStackPop; 9906cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 9916cd88445SBarry 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"); 9923565c898SBarry Smith if (B != A) { 9936cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 9946cd88445SBarry 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"); 9956cd88445SBarry Smith } 9964a6899ffSJed Brown } 997214bc6a2SJed Brown if (imex) { 998b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 9994c26be97Sstefano_zampini PetscBool assembled; 100094ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 10014c26be97Sstefano_zampini ierr = MatAssembled(A,&assembled);CHKERRQ(ierr); 10024c26be97Sstefano_zampini if (!assembled) { 10034c26be97Sstefano_zampini ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10044c26be97Sstefano_zampini ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10054c26be97Sstefano_zampini } 100694ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 100794ab13aaSBarry Smith if (A != B) { 100894ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 10094c26be97Sstefano_zampini ierr = MatAssembled(B,&assembled);CHKERRQ(ierr); 10104c26be97Sstefano_zampini if (!assembled) { 10114c26be97Sstefano_zampini ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10124c26be97Sstefano_zampini ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10134c26be97Sstefano_zampini } 101494ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 1015214bc6a2SJed Brown } 1016214bc6a2SJed Brown } 1017214bc6a2SJed Brown } else { 1018e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 1019e1244c69SJed Brown if (rhsjacobian) { 1020214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 1021d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 1022e1244c69SJed Brown } 102394ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 10243565c898SBarry Smith PetscBool flg; 1025e1244c69SJed Brown ts->rhsjacobian.scale = -1; 1026e1244c69SJed Brown ts->rhsjacobian.shift = shift; 10273565c898SBarry Smith ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr); 10283565c898SBarry Smith /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */ 10293565c898SBarry Smith if (!flg) { 103094ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 103194ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 10323565c898SBarry Smith } 103394ab13aaSBarry Smith if (A != B) { 103494ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 103594ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 1036316643e7SJed Brown } 1037e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 1038e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 1039e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 104094ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 104194ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 104294ab13aaSBarry Smith if (A != B) { 104394ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 104494ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 1045214bc6a2SJed Brown } 1046316643e7SJed Brown } 104794ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 104894ab13aaSBarry Smith if (A != B) { 104994ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 1050316643e7SJed Brown } 1051316643e7SJed Brown } 1052316643e7SJed Brown } 105394ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 1054316643e7SJed Brown PetscFunctionReturn(0); 1055316643e7SJed Brown } 1056316643e7SJed Brown 1057d763cef2SBarry Smith /*@C 1058d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 1059b5abc632SBarry Smith where U_t = G(t,u). 1060d763cef2SBarry Smith 10613f9fe445SBarry Smith Logically Collective on TS 1062d763cef2SBarry Smith 1063d763cef2SBarry Smith Input Parameters: 1064d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 10650298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 1066d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 1067d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 10680298fd71SBarry Smith function evaluation routine (may be NULL) 1069d763cef2SBarry Smith 1070d763cef2SBarry Smith Calling sequence of func: 107187828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 1072d763cef2SBarry Smith 1073d763cef2SBarry Smith + t - current timestep 1074d763cef2SBarry Smith . u - input vector 1075d763cef2SBarry Smith . F - function vector 1076d763cef2SBarry Smith - ctx - [optional] user-defined function context 1077d763cef2SBarry Smith 1078d763cef2SBarry Smith Level: beginner 1079d763cef2SBarry Smith 10802bbac0d3SBarry Smith Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 10812bbac0d3SBarry Smith 1082d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1083d763cef2SBarry Smith 1084ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 1085d763cef2SBarry Smith @*/ 1086089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 1087d763cef2SBarry Smith { 1088089b2837SJed Brown PetscErrorCode ierr; 1089089b2837SJed Brown SNES snes; 10900298fd71SBarry Smith Vec ralloc = NULL; 109124989b8cSPeter Brune DM dm; 1092d763cef2SBarry Smith 1093089b2837SJed Brown PetscFunctionBegin; 10940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1095ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 109624989b8cSPeter Brune 109724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 109824989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 1099089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1100e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 1101e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 1102e856ceecSJed Brown r = ralloc; 1103e856ceecSJed Brown } 1104089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 1105e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1106d763cef2SBarry Smith PetscFunctionReturn(0); 1107d763cef2SBarry Smith } 1108d763cef2SBarry Smith 1109ef20d060SBarry Smith /*@C 1110abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1111ef20d060SBarry Smith 1112ef20d060SBarry Smith Logically Collective on TS 1113ef20d060SBarry Smith 1114ef20d060SBarry Smith Input Parameters: 1115ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1116ef20d060SBarry Smith . f - routine for evaluating the solution 1117ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 11180298fd71SBarry Smith function evaluation routine (may be NULL) 1119ef20d060SBarry Smith 1120ef20d060SBarry Smith Calling sequence of func: 1121ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 1122ef20d060SBarry Smith 1123ef20d060SBarry Smith + t - current timestep 1124ef20d060SBarry Smith . u - output vector 1125ef20d060SBarry Smith - ctx - [optional] user-defined function context 1126ef20d060SBarry Smith 1127abd5a294SJed Brown Notes: 1128abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1129abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1130abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1131abd5a294SJed Brown 11324f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1133abd5a294SJed Brown 1134ef20d060SBarry Smith Level: beginner 1135ef20d060SBarry Smith 1136ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1137ef20d060SBarry Smith 11389b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 1139ef20d060SBarry Smith @*/ 1140ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1141ef20d060SBarry Smith { 1142ef20d060SBarry Smith PetscErrorCode ierr; 1143ef20d060SBarry Smith DM dm; 1144ef20d060SBarry Smith 1145ef20d060SBarry Smith PetscFunctionBegin; 1146ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1147ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1148ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 1149ef20d060SBarry Smith PetscFunctionReturn(0); 1150ef20d060SBarry Smith } 1151ef20d060SBarry Smith 11529b7cd975SBarry Smith /*@C 11539b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 11549b7cd975SBarry Smith 11559b7cd975SBarry Smith Logically Collective on TS 11569b7cd975SBarry Smith 11579b7cd975SBarry Smith Input Parameters: 11589b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 1159e162b725SBarry Smith . func - routine for evaluating the forcing function 11609b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 11610298fd71SBarry Smith function evaluation routine (may be NULL) 11629b7cd975SBarry Smith 11639b7cd975SBarry Smith Calling sequence of func: 1164e162b725SBarry Smith $ func (TS ts,PetscReal t,Vec f,void *ctx); 11659b7cd975SBarry Smith 11669b7cd975SBarry Smith + t - current timestep 1167e162b725SBarry Smith . f - output vector 11689b7cd975SBarry Smith - ctx - [optional] user-defined function context 11699b7cd975SBarry Smith 11709b7cd975SBarry Smith Notes: 11719b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1172e162b725SBarry 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 1173e162b725SBarry Smith definition of the problem you are solving and hence possibly introducing bugs. 1174e162b725SBarry Smith 1175e162b725SBarry Smith This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0 1176e162b725SBarry Smith 1177e162b725SBarry 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 1178e162b725SBarry Smith parameters can be passed in the ctx variable. 11799b7cd975SBarry Smith 11809b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 11819b7cd975SBarry Smith 11829b7cd975SBarry Smith Level: beginner 11839b7cd975SBarry Smith 11849b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 11859b7cd975SBarry Smith 11869b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 11879b7cd975SBarry Smith @*/ 1188e162b725SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx) 11899b7cd975SBarry Smith { 11909b7cd975SBarry Smith PetscErrorCode ierr; 11919b7cd975SBarry Smith DM dm; 11929b7cd975SBarry Smith 11939b7cd975SBarry Smith PetscFunctionBegin; 11949b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11959b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1196e162b725SBarry Smith ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr); 11979b7cd975SBarry Smith PetscFunctionReturn(0); 11989b7cd975SBarry Smith } 11999b7cd975SBarry Smith 1200d763cef2SBarry Smith /*@C 1201f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1202b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1203d763cef2SBarry Smith 12043f9fe445SBarry Smith Logically Collective on TS 1205d763cef2SBarry Smith 1206d763cef2SBarry Smith Input Parameters: 1207d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1208e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1209e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1210d763cef2SBarry Smith . f - the Jacobian evaluation routine 1211d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 12120298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1213d763cef2SBarry Smith 1214f7ab8db6SBarry Smith Calling sequence of f: 1215ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1216d763cef2SBarry Smith 1217d763cef2SBarry Smith + t - current timestep 1218d763cef2SBarry Smith . u - input vector 1219e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1220e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1221d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1222d763cef2SBarry Smith 12236cd88445SBarry Smith Notes: 12246cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 12256cd88445SBarry Smith 12266cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1227ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1228d763cef2SBarry Smith 1229d763cef2SBarry Smith Level: beginner 1230d763cef2SBarry Smith 1231d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1232d763cef2SBarry Smith 1233ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1234d763cef2SBarry Smith 1235d763cef2SBarry Smith @*/ 1236e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1237d763cef2SBarry Smith { 1238277b19d0SLisandro Dalcin PetscErrorCode ierr; 1239089b2837SJed Brown SNES snes; 124024989b8cSPeter Brune DM dm; 124124989b8cSPeter Brune TSIJacobian ijacobian; 1242277b19d0SLisandro Dalcin 1243d763cef2SBarry Smith PetscFunctionBegin; 12440700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1245e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1246e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1247e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1248e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1249d763cef2SBarry Smith 125024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 125124989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1252e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1253e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1254e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1255e1244c69SJed Brown } 12560298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1257089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12585f659677SPeter Brune if (!ijacobian) { 1259e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 12600e4ef248SJed Brown } 1261e5d3d808SBarry Smith if (Amat) { 1262e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 12630e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1264e5d3d808SBarry Smith ts->Arhs = Amat; 12650e4ef248SJed Brown } 1266e5d3d808SBarry Smith if (Pmat) { 1267e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 12680e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1269e5d3d808SBarry Smith ts->Brhs = Pmat; 12700e4ef248SJed Brown } 1271d763cef2SBarry Smith PetscFunctionReturn(0); 1272d763cef2SBarry Smith } 1273d763cef2SBarry Smith 1274316643e7SJed Brown /*@C 1275b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1276316643e7SJed Brown 12773f9fe445SBarry Smith Logically Collective on TS 1278316643e7SJed Brown 1279316643e7SJed Brown Input Parameters: 1280316643e7SJed Brown + ts - the TS context obtained from TSCreate() 12810298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1282316643e7SJed Brown . f - the function evaluation routine 12830298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1284316643e7SJed Brown 1285316643e7SJed Brown Calling sequence of f: 1286316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1287316643e7SJed Brown 1288316643e7SJed Brown + t - time at step/stage being solved 1289316643e7SJed Brown . u - state vector 1290316643e7SJed Brown . u_t - time derivative of state vector 1291316643e7SJed Brown . F - function vector 1292316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1293316643e7SJed Brown 1294316643e7SJed Brown Important: 12952bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1296316643e7SJed Brown 1297316643e7SJed Brown Level: beginner 1298316643e7SJed Brown 1299316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1300316643e7SJed Brown 1301d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1302316643e7SJed Brown @*/ 130351699248SLisandro Dalcin PetscErrorCode TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx) 1304316643e7SJed Brown { 1305089b2837SJed Brown PetscErrorCode ierr; 1306089b2837SJed Brown SNES snes; 130751699248SLisandro Dalcin Vec ralloc = NULL; 130824989b8cSPeter Brune DM dm; 1309316643e7SJed Brown 1310316643e7SJed Brown PetscFunctionBegin; 13110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 131251699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 131324989b8cSPeter Brune 131424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 131524989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 131624989b8cSPeter Brune 1317089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 131851699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 131951699248SLisandro Dalcin ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 132051699248SLisandro Dalcin r = ralloc; 1321e856ceecSJed Brown } 132251699248SLisandro Dalcin ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 132351699248SLisandro Dalcin ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1324089b2837SJed Brown PetscFunctionReturn(0); 1325089b2837SJed Brown } 1326089b2837SJed Brown 1327089b2837SJed Brown /*@C 1328089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1329089b2837SJed Brown 1330089b2837SJed Brown Not Collective 1331089b2837SJed Brown 1332089b2837SJed Brown Input Parameter: 1333089b2837SJed Brown . ts - the TS context 1334089b2837SJed Brown 1335089b2837SJed Brown Output Parameter: 13360298fd71SBarry Smith + r - vector to hold residual (or NULL) 13370298fd71SBarry Smith . func - the function to compute residual (or NULL) 13380298fd71SBarry Smith - ctx - the function context (or NULL) 1339089b2837SJed Brown 1340089b2837SJed Brown Level: advanced 1341089b2837SJed Brown 1342089b2837SJed Brown .keywords: TS, nonlinear, get, function 1343089b2837SJed Brown 1344089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1345089b2837SJed Brown @*/ 1346089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1347089b2837SJed Brown { 1348089b2837SJed Brown PetscErrorCode ierr; 1349089b2837SJed Brown SNES snes; 135024989b8cSPeter Brune DM dm; 1351089b2837SJed Brown 1352089b2837SJed Brown PetscFunctionBegin; 1353089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1354089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13550298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 135624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 135724989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1358089b2837SJed Brown PetscFunctionReturn(0); 1359089b2837SJed Brown } 1360089b2837SJed Brown 1361089b2837SJed Brown /*@C 1362089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1363089b2837SJed Brown 1364089b2837SJed Brown Not Collective 1365089b2837SJed Brown 1366089b2837SJed Brown Input Parameter: 1367089b2837SJed Brown . ts - the TS context 1368089b2837SJed Brown 1369089b2837SJed Brown Output Parameter: 13700298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 13710298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 13720298fd71SBarry Smith - ctx - the function context (or NULL) 1373089b2837SJed Brown 1374089b2837SJed Brown Level: advanced 1375089b2837SJed Brown 1376089b2837SJed Brown .keywords: TS, nonlinear, get, function 1377089b2837SJed Brown 13782bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1379089b2837SJed Brown @*/ 1380089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1381089b2837SJed Brown { 1382089b2837SJed Brown PetscErrorCode ierr; 1383089b2837SJed Brown SNES snes; 138424989b8cSPeter Brune DM dm; 1385089b2837SJed Brown 1386089b2837SJed Brown PetscFunctionBegin; 1387089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1388089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13890298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 139024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 139124989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1392316643e7SJed Brown PetscFunctionReturn(0); 1393316643e7SJed Brown } 1394316643e7SJed Brown 1395316643e7SJed Brown /*@C 1396a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1397ae8867d6SBarry Smith provided with TSSetIFunction(). 1398316643e7SJed Brown 13993f9fe445SBarry Smith Logically Collective on TS 1400316643e7SJed Brown 1401316643e7SJed Brown Input Parameters: 1402316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1403e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1404e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1405316643e7SJed Brown . f - the Jacobian evaluation routine 14060298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1407316643e7SJed Brown 1408316643e7SJed Brown Calling sequence of f: 1409ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1410316643e7SJed Brown 1411316643e7SJed Brown + t - time at step/stage being solved 14121b4a444bSJed Brown . U - state vector 14131b4a444bSJed Brown . U_t - time derivative of state vector 1414316643e7SJed Brown . a - shift 1415e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1416e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1417316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1418316643e7SJed Brown 1419316643e7SJed Brown Notes: 1420e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1421316643e7SJed Brown 1422895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1423895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1424895c21f2SBarry Smith 1425a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1426b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1427a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1428a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1429a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1430a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1431a4f0a591SBarry Smith 14326cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 14336cd88445SBarry Smith 14346cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1435ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1436ca5f011dSBarry Smith 1437316643e7SJed Brown Level: beginner 1438316643e7SJed Brown 1439316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1440316643e7SJed Brown 1441ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1442316643e7SJed Brown 1443316643e7SJed Brown @*/ 1444e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1445316643e7SJed Brown { 1446316643e7SJed Brown PetscErrorCode ierr; 1447089b2837SJed Brown SNES snes; 144824989b8cSPeter Brune DM dm; 1449316643e7SJed Brown 1450316643e7SJed Brown PetscFunctionBegin; 14510700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1452e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1453e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1454e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1455e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 145624989b8cSPeter Brune 145724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 145824989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 145924989b8cSPeter Brune 1460089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1461e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1462316643e7SJed Brown PetscFunctionReturn(0); 1463316643e7SJed Brown } 1464316643e7SJed Brown 1465e1244c69SJed Brown /*@ 1466e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1467e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1468e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1469e1244c69SJed Brown not been changed by the TS. 1470e1244c69SJed Brown 1471e1244c69SJed Brown Logically Collective 1472e1244c69SJed Brown 1473e1244c69SJed Brown Input Arguments: 1474e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1475e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1476e1244c69SJed Brown 1477e1244c69SJed Brown Level: intermediate 1478e1244c69SJed Brown 1479e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1480e1244c69SJed Brown @*/ 1481e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1482e1244c69SJed Brown { 1483e1244c69SJed Brown PetscFunctionBegin; 1484e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1485e1244c69SJed Brown PetscFunctionReturn(0); 1486e1244c69SJed Brown } 1487e1244c69SJed Brown 1488efe9872eSLisandro Dalcin /*@C 1489efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1490efe9872eSLisandro Dalcin 1491efe9872eSLisandro Dalcin Logically Collective on TS 1492efe9872eSLisandro Dalcin 1493efe9872eSLisandro Dalcin Input Parameters: 1494efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1495efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1496efe9872eSLisandro Dalcin . fun - the function evaluation routine 1497efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1498efe9872eSLisandro Dalcin 1499efe9872eSLisandro Dalcin Calling sequence of fun: 1500efe9872eSLisandro Dalcin $ fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1501efe9872eSLisandro Dalcin 1502efe9872eSLisandro Dalcin + t - time at step/stage being solved 1503efe9872eSLisandro Dalcin . U - state vector 1504efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1505efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1506efe9872eSLisandro Dalcin . F - function vector 1507efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1508efe9872eSLisandro Dalcin 1509efe9872eSLisandro Dalcin Level: beginner 1510efe9872eSLisandro Dalcin 1511efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function 1512efe9872eSLisandro Dalcin 1513efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1514efe9872eSLisandro Dalcin @*/ 1515efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx) 1516efe9872eSLisandro Dalcin { 1517efe9872eSLisandro Dalcin DM dm; 1518efe9872eSLisandro Dalcin PetscErrorCode ierr; 1519efe9872eSLisandro Dalcin 1520efe9872eSLisandro Dalcin PetscFunctionBegin; 1521efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1522efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2); 1523efe9872eSLisandro Dalcin ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr); 1524efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1525efe9872eSLisandro Dalcin ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1526efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1527efe9872eSLisandro Dalcin } 1528efe9872eSLisandro Dalcin 1529efe9872eSLisandro Dalcin /*@C 1530efe9872eSLisandro Dalcin TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1531efe9872eSLisandro Dalcin 1532efe9872eSLisandro Dalcin Not Collective 1533efe9872eSLisandro Dalcin 1534efe9872eSLisandro Dalcin Input Parameter: 1535efe9872eSLisandro Dalcin . ts - the TS context 1536efe9872eSLisandro Dalcin 1537efe9872eSLisandro Dalcin Output Parameter: 1538efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1539efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1540efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1541efe9872eSLisandro Dalcin 1542efe9872eSLisandro Dalcin Level: advanced 1543efe9872eSLisandro Dalcin 1544efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function 1545efe9872eSLisandro Dalcin 1546efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction() 1547efe9872eSLisandro Dalcin @*/ 1548efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx) 1549efe9872eSLisandro Dalcin { 1550efe9872eSLisandro Dalcin PetscErrorCode ierr; 1551efe9872eSLisandro Dalcin SNES snes; 1552efe9872eSLisandro Dalcin DM dm; 1553efe9872eSLisandro Dalcin 1554efe9872eSLisandro Dalcin PetscFunctionBegin; 1555efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1556efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1557efe9872eSLisandro Dalcin ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 1558efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1559efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1560efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1561efe9872eSLisandro Dalcin } 1562efe9872eSLisandro Dalcin 1563efe9872eSLisandro Dalcin /*@C 1564bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1565efe9872eSLisandro Dalcin where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function(). 1566efe9872eSLisandro Dalcin 1567efe9872eSLisandro Dalcin Logically Collective on TS 1568efe9872eSLisandro Dalcin 1569efe9872eSLisandro Dalcin Input Parameters: 1570efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1571efe9872eSLisandro Dalcin . J - Jacobian matrix 1572efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1573efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1574efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1575efe9872eSLisandro Dalcin 1576efe9872eSLisandro Dalcin Calling sequence of jac: 1577bc77d74cSLisandro Dalcin $ jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx); 1578efe9872eSLisandro Dalcin 1579efe9872eSLisandro Dalcin + t - time at step/stage being solved 1580efe9872eSLisandro Dalcin . U - state vector 1581efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1582efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1583efe9872eSLisandro Dalcin . v - shift for U_t 1584efe9872eSLisandro Dalcin . a - shift for U_tt 1585efe9872eSLisandro 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 1586efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1587efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1588efe9872eSLisandro Dalcin 1589efe9872eSLisandro Dalcin Notes: 1590efe9872eSLisandro Dalcin The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve. 1591efe9872eSLisandro Dalcin 1592efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1593efe9872eSLisandro 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. 1594efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1595bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1596efe9872eSLisandro Dalcin 1597efe9872eSLisandro Dalcin Level: beginner 1598efe9872eSLisandro Dalcin 1599efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian 1600efe9872eSLisandro Dalcin 1601efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1602efe9872eSLisandro Dalcin @*/ 1603efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx) 1604efe9872eSLisandro Dalcin { 1605efe9872eSLisandro Dalcin DM dm; 1606efe9872eSLisandro Dalcin PetscErrorCode ierr; 1607efe9872eSLisandro Dalcin 1608efe9872eSLisandro Dalcin PetscFunctionBegin; 1609efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1610efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2); 1611efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3); 1612efe9872eSLisandro Dalcin ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr); 1613efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1614efe9872eSLisandro Dalcin ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1615efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1616efe9872eSLisandro Dalcin } 1617efe9872eSLisandro Dalcin 1618efe9872eSLisandro Dalcin /*@C 1619efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1620efe9872eSLisandro Dalcin 1621efe9872eSLisandro Dalcin Not Collective, but parallel objects are returned if TS is parallel 1622efe9872eSLisandro Dalcin 1623efe9872eSLisandro Dalcin Input Parameter: 1624efe9872eSLisandro Dalcin . ts - The TS context obtained from TSCreate() 1625efe9872eSLisandro Dalcin 1626efe9872eSLisandro Dalcin Output Parameters: 1627efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1628efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1629efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1630efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1631efe9872eSLisandro Dalcin 1632efe9872eSLisandro Dalcin Notes: You can pass in NULL for any return argument you do not need. 1633efe9872eSLisandro Dalcin 1634efe9872eSLisandro Dalcin Level: advanced 1635efe9872eSLisandro Dalcin 163680275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 1637efe9872eSLisandro Dalcin 1638efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian 1639efe9872eSLisandro Dalcin @*/ 1640efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx) 1641efe9872eSLisandro Dalcin { 1642efe9872eSLisandro Dalcin PetscErrorCode ierr; 1643efe9872eSLisandro Dalcin SNES snes; 1644efe9872eSLisandro Dalcin DM dm; 1645efe9872eSLisandro Dalcin 1646efe9872eSLisandro Dalcin PetscFunctionBegin; 1647efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1648efe9872eSLisandro Dalcin ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 1649efe9872eSLisandro Dalcin ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr); 1650efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1651efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1652efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1653efe9872eSLisandro Dalcin } 1654efe9872eSLisandro Dalcin 1655efe9872eSLisandro Dalcin /*@ 1656efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1657efe9872eSLisandro Dalcin 1658efe9872eSLisandro Dalcin Collective on TS and Vec 1659efe9872eSLisandro Dalcin 1660efe9872eSLisandro Dalcin Input Parameters: 1661efe9872eSLisandro Dalcin + ts - the TS context 1662efe9872eSLisandro Dalcin . t - current time 1663efe9872eSLisandro Dalcin . U - state vector 1664efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1665efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1666efe9872eSLisandro Dalcin 1667efe9872eSLisandro Dalcin Output Parameter: 1668efe9872eSLisandro Dalcin . F - the residual vector 1669efe9872eSLisandro Dalcin 1670efe9872eSLisandro Dalcin Note: 1671efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1672efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1673efe9872eSLisandro Dalcin 1674efe9872eSLisandro Dalcin Level: developer 1675efe9872eSLisandro Dalcin 1676efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector 1677efe9872eSLisandro Dalcin 1678efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1679efe9872eSLisandro Dalcin @*/ 1680efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F) 1681efe9872eSLisandro Dalcin { 1682efe9872eSLisandro Dalcin DM dm; 1683efe9872eSLisandro Dalcin TSI2Function I2Function; 1684efe9872eSLisandro Dalcin void *ctx; 1685efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1686efe9872eSLisandro Dalcin PetscErrorCode ierr; 1687efe9872eSLisandro Dalcin 1688efe9872eSLisandro Dalcin PetscFunctionBegin; 1689efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1690efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1691efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1692efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1693efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F,VEC_CLASSID,6); 1694efe9872eSLisandro Dalcin 1695efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1696efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr); 1697efe9872eSLisandro Dalcin ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 1698efe9872eSLisandro Dalcin 1699efe9872eSLisandro Dalcin if (!I2Function) { 1700efe9872eSLisandro Dalcin ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr); 1701efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1702efe9872eSLisandro Dalcin } 1703efe9872eSLisandro Dalcin 1704efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1705efe9872eSLisandro Dalcin 1706efe9872eSLisandro Dalcin PetscStackPush("TS user implicit function"); 1707efe9872eSLisandro Dalcin ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr); 1708efe9872eSLisandro Dalcin PetscStackPop; 1709efe9872eSLisandro Dalcin 1710efe9872eSLisandro Dalcin if (rhsfunction) { 1711efe9872eSLisandro Dalcin Vec Frhs; 1712efe9872eSLisandro Dalcin ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 1713efe9872eSLisandro Dalcin ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 1714efe9872eSLisandro Dalcin ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr); 1715efe9872eSLisandro Dalcin } 1716efe9872eSLisandro Dalcin 1717efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1718efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1719efe9872eSLisandro Dalcin } 1720efe9872eSLisandro Dalcin 1721efe9872eSLisandro Dalcin /*@ 1722efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1723efe9872eSLisandro Dalcin 1724efe9872eSLisandro Dalcin Collective on TS and Vec 1725efe9872eSLisandro Dalcin 1726efe9872eSLisandro Dalcin Input Parameters: 1727efe9872eSLisandro Dalcin + ts - the TS context 1728efe9872eSLisandro Dalcin . t - current timestep 1729efe9872eSLisandro Dalcin . U - state vector 1730efe9872eSLisandro Dalcin . V - time derivative of state vector 1731efe9872eSLisandro Dalcin . A - second time derivative of state vector 1732efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1733efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1734efe9872eSLisandro Dalcin 1735efe9872eSLisandro Dalcin Output Parameters: 1736efe9872eSLisandro Dalcin + J - Jacobian matrix 1737efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1738efe9872eSLisandro Dalcin 1739efe9872eSLisandro Dalcin Notes: 1740efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1741efe9872eSLisandro Dalcin 1742efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1743efe9872eSLisandro Dalcin 1744efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1745efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1746efe9872eSLisandro Dalcin 1747efe9872eSLisandro Dalcin Level: developer 1748efe9872eSLisandro Dalcin 1749efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix 1750efe9872eSLisandro Dalcin 1751efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1752efe9872eSLisandro Dalcin @*/ 1753efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P) 1754efe9872eSLisandro Dalcin { 1755efe9872eSLisandro Dalcin DM dm; 1756efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1757efe9872eSLisandro Dalcin void *ctx; 1758efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1759efe9872eSLisandro Dalcin PetscErrorCode ierr; 1760efe9872eSLisandro Dalcin 1761efe9872eSLisandro Dalcin PetscFunctionBegin; 1762efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1763efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1764efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1765efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1766efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J,MAT_CLASSID,8); 1767efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P,MAT_CLASSID,9); 1768efe9872eSLisandro Dalcin 1769efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1770efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr); 1771efe9872eSLisandro Dalcin ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 1772efe9872eSLisandro Dalcin 1773efe9872eSLisandro Dalcin if (!I2Jacobian) { 1774efe9872eSLisandro Dalcin ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr); 1775efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1776efe9872eSLisandro Dalcin } 1777efe9872eSLisandro Dalcin 1778efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1779efe9872eSLisandro Dalcin 1780efe9872eSLisandro Dalcin PetscStackPush("TS user implicit Jacobian"); 1781efe9872eSLisandro Dalcin ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr); 1782efe9872eSLisandro Dalcin PetscStackPop; 1783efe9872eSLisandro Dalcin 1784efe9872eSLisandro Dalcin if (rhsjacobian) { 1785efe9872eSLisandro Dalcin Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 1786efe9872eSLisandro Dalcin ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr); 1787efe9872eSLisandro Dalcin ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr); 1788efe9872eSLisandro Dalcin ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr); 1789efe9872eSLisandro Dalcin if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);} 1790efe9872eSLisandro Dalcin } 1791efe9872eSLisandro Dalcin 1792efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1793efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1794efe9872eSLisandro Dalcin } 1795efe9872eSLisandro Dalcin 1796efe9872eSLisandro Dalcin /*@ 1797efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1798efe9872eSLisandro Dalcin for use by the TS routines handling second order equations. 1799efe9872eSLisandro Dalcin 1800efe9872eSLisandro Dalcin Logically Collective on TS and Vec 1801efe9872eSLisandro Dalcin 1802efe9872eSLisandro Dalcin Input Parameters: 1803efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1804efe9872eSLisandro Dalcin . u - the solution vector 1805efe9872eSLisandro Dalcin - v - the time derivative vector 1806efe9872eSLisandro Dalcin 1807efe9872eSLisandro Dalcin Level: beginner 1808efe9872eSLisandro Dalcin 1809efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions 1810efe9872eSLisandro Dalcin @*/ 1811efe9872eSLisandro Dalcin PetscErrorCode TS2SetSolution(TS ts,Vec u,Vec v) 1812efe9872eSLisandro Dalcin { 1813efe9872eSLisandro Dalcin PetscErrorCode ierr; 1814efe9872eSLisandro Dalcin 1815efe9872eSLisandro Dalcin PetscFunctionBegin; 1816efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1817efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1818efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v,VEC_CLASSID,3); 1819efe9872eSLisandro Dalcin ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 1820efe9872eSLisandro Dalcin ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr); 1821efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 1822efe9872eSLisandro Dalcin ts->vec_dot = v; 1823efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1824efe9872eSLisandro Dalcin } 1825efe9872eSLisandro Dalcin 1826efe9872eSLisandro Dalcin /*@ 1827efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 1828efe9872eSLisandro Dalcin for second order equations. It is valid to call this routine inside the function 1829efe9872eSLisandro Dalcin that you are evaluating in order to move to the new timestep. This vector not 1830efe9872eSLisandro Dalcin changed until the solution at the next timestep has been calculated. 1831efe9872eSLisandro Dalcin 1832efe9872eSLisandro Dalcin Not Collective, but Vec returned is parallel if TS is parallel 1833efe9872eSLisandro Dalcin 1834efe9872eSLisandro Dalcin Input Parameter: 1835efe9872eSLisandro Dalcin . ts - the TS context obtained from TSCreate() 1836efe9872eSLisandro Dalcin 1837efe9872eSLisandro Dalcin Output Parameter: 1838efe9872eSLisandro Dalcin + u - the vector containing the solution 1839efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1840efe9872eSLisandro Dalcin 1841efe9872eSLisandro Dalcin Level: intermediate 1842efe9872eSLisandro Dalcin 1843efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime() 1844efe9872eSLisandro Dalcin 1845efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution 1846efe9872eSLisandro Dalcin @*/ 1847efe9872eSLisandro Dalcin PetscErrorCode TS2GetSolution(TS ts,Vec *u,Vec *v) 1848efe9872eSLisandro Dalcin { 1849efe9872eSLisandro Dalcin PetscFunctionBegin; 1850efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1851efe9872eSLisandro Dalcin if (u) PetscValidPointer(u,2); 1852efe9872eSLisandro Dalcin if (v) PetscValidPointer(v,3); 1853efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1854efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 1855efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1856efe9872eSLisandro Dalcin } 1857efe9872eSLisandro Dalcin 185855849f57SBarry Smith /*@C 185955849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 186055849f57SBarry Smith 186155849f57SBarry Smith Collective on PetscViewer 186255849f57SBarry Smith 186355849f57SBarry Smith Input Parameters: 186455849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 186555849f57SBarry Smith some related function before a call to TSLoad(). 186655849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 186755849f57SBarry Smith 186855849f57SBarry Smith Level: intermediate 186955849f57SBarry Smith 187055849f57SBarry Smith Notes: 187155849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 187255849f57SBarry Smith 187355849f57SBarry Smith Notes for advanced users: 187455849f57SBarry Smith Most users should not need to know the details of the binary storage 187555849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 187655849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 187755849f57SBarry Smith format is 187855849f57SBarry Smith .vb 187955849f57SBarry Smith has not yet been determined 188055849f57SBarry Smith .ve 188155849f57SBarry Smith 188255849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 188355849f57SBarry Smith @*/ 1884f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 188555849f57SBarry Smith { 188655849f57SBarry Smith PetscErrorCode ierr; 188755849f57SBarry Smith PetscBool isbinary; 188855849f57SBarry Smith PetscInt classid; 188955849f57SBarry Smith char type[256]; 18902d53ad75SBarry Smith DMTS sdm; 1891ad6bc421SBarry Smith DM dm; 189255849f57SBarry Smith 189355849f57SBarry Smith PetscFunctionBegin; 1894f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 189555849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 189655849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 189755849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 189855849f57SBarry Smith 1899060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1900ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1901060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1902f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1903f2c2a1b9SBarry Smith if (ts->ops->load) { 1904f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1905f2c2a1b9SBarry Smith } 1906ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1907ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1908ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1909f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1910f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 19112d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19122d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 191355849f57SBarry Smith PetscFunctionReturn(0); 191455849f57SBarry Smith } 191555849f57SBarry Smith 19169804daf3SBarry Smith #include <petscdraw.h> 1917e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1918e04113cfSBarry Smith #include <petscviewersaws.h> 1919f05ece33SBarry Smith #endif 19207e2c5f70SBarry Smith /*@C 1921d763cef2SBarry Smith TSView - Prints the TS data structure. 1922d763cef2SBarry Smith 19234c49b128SBarry Smith Collective on TS 1924d763cef2SBarry Smith 1925d763cef2SBarry Smith Input Parameters: 1926d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1927d763cef2SBarry Smith - viewer - visualization context 1928d763cef2SBarry Smith 1929d763cef2SBarry Smith Options Database Key: 1930d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1931d763cef2SBarry Smith 1932d763cef2SBarry Smith Notes: 1933d763cef2SBarry Smith The available visualization contexts include 1934b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1935b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1936d763cef2SBarry Smith output where only the first processor opens 1937d763cef2SBarry Smith the file. All other processors send their 1938d763cef2SBarry Smith data to the first processor to print. 1939d763cef2SBarry Smith 1940d763cef2SBarry Smith The user can open an alternative visualization context with 1941b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1942d763cef2SBarry Smith 1943d763cef2SBarry Smith Level: beginner 1944d763cef2SBarry Smith 1945d763cef2SBarry Smith .keywords: TS, timestep, view 1946d763cef2SBarry Smith 1947b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1948d763cef2SBarry Smith @*/ 19497087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1950d763cef2SBarry Smith { 1951dfbe8321SBarry Smith PetscErrorCode ierr; 195219fd82e9SBarry Smith TSType type; 19532b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 19542d53ad75SBarry Smith DMTS sdm; 1955e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1956536b137fSBarry Smith PetscBool issaws; 1957f05ece33SBarry Smith #endif 1958d763cef2SBarry Smith 1959d763cef2SBarry Smith PetscFunctionBegin; 19600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 19613050cee2SBarry Smith if (!viewer) { 1962ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 19633050cee2SBarry Smith } 19640700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1965c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1966fd16b177SBarry Smith 1967251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1968251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 196955849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 19702b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1971e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1972536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1973f05ece33SBarry Smith #endif 197432077d6dSBarry Smith if (iascii) { 1975dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 1976efd4aadfSBarry Smith if (ts->ops->view) { 1977efd4aadfSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1978efd4aadfSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1979efd4aadfSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1980efd4aadfSBarry Smith } 1981ef85077eSLisandro Dalcin if (ts->max_steps < PETSC_MAX_INT) { 198277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 1983ef85077eSLisandro Dalcin } 1984ef85077eSLisandro Dalcin if (ts->max_time < PETSC_MAX_REAL) { 19857c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1986ef85077eSLisandro Dalcin } 1987efd4aadfSBarry Smith if (ts->usessnes) { 1988efd4aadfSBarry Smith PetscBool lin; 1989d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 19905ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1991d763cef2SBarry Smith } 19925ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1993efd4aadfSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)ts->snes,SNESKSPONLY,&lin);CHKERRQ(ierr); 1994efd4aadfSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr); 1995efd4aadfSBarry Smith } 1996193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 1997a0af407cSBarry Smith if (ts->vrtol) { 1998a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of relative error tolerances, ");CHKERRQ(ierr); 1999a0af407cSBarry Smith } else { 2000a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr); 2001a0af407cSBarry Smith } 2002a0af407cSBarry Smith if (ts->vatol) { 2003a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of absolute error tolerances\n");CHKERRQ(ierr); 2004a0af407cSBarry Smith } else { 2005a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr); 2006a0af407cSBarry Smith } 2007efd4aadfSBarry Smith ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr); 2008efd4aadfSBarry Smith if (ts->snes && ts->usessnes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 20092d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 20102d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 20110f5bd95cSBarry Smith } else if (isstring) { 2012a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 2013b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 201455849f57SBarry Smith } else if (isbinary) { 201555849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 201655849f57SBarry Smith MPI_Comm comm; 201755849f57SBarry Smith PetscMPIInt rank; 201855849f57SBarry Smith char type[256]; 201955849f57SBarry Smith 202055849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 202155849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 202255849f57SBarry Smith if (!rank) { 202355849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 202455849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 202555849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 202655849f57SBarry Smith } 202755849f57SBarry Smith if (ts->ops->view) { 202855849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 202955849f57SBarry Smith } 2030efd4aadfSBarry Smith if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);} 2031f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 2032f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 20332d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 20342d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 20352b0a91c0SBarry Smith } else if (isdraw) { 20362b0a91c0SBarry Smith PetscDraw draw; 20372b0a91c0SBarry Smith char str[36]; 203889fd9fafSBarry Smith PetscReal x,y,bottom,h; 20392b0a91c0SBarry Smith 20402b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 20412b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 20422b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 20432b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 204451fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 204589fd9fafSBarry Smith bottom = y - h; 20462b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 20472b0a91c0SBarry Smith if (ts->ops->view) { 20482b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 20492b0a91c0SBarry Smith } 2050efd4aadfSBarry Smith if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);} 2051efd4aadfSBarry Smith if (ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 20522b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 2053e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 2054536b137fSBarry Smith } else if (issaws) { 2055d45a07a7SBarry Smith PetscMPIInt rank; 20562657e9d9SBarry Smith const char *name; 20572657e9d9SBarry Smith 20582657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 2059d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 2060d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 2061d45a07a7SBarry Smith char dir[1024]; 2062d45a07a7SBarry Smith 2063e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 2064a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 20652657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 20662657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 20672657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 2068d763cef2SBarry Smith } 20690acecf5bSBarry Smith if (ts->ops->view) { 20700acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 20710acecf5bSBarry Smith } 2072f05ece33SBarry Smith #endif 2073f05ece33SBarry Smith } 2074f05ece33SBarry Smith 2075b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2076251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 2077b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2078d763cef2SBarry Smith PetscFunctionReturn(0); 2079d763cef2SBarry Smith } 2080d763cef2SBarry Smith 2081b07ff414SBarry Smith /*@ 2082d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2083d763cef2SBarry Smith the timesteppers. 2084d763cef2SBarry Smith 20853f9fe445SBarry Smith Logically Collective on TS 2086d763cef2SBarry Smith 2087d763cef2SBarry Smith Input Parameters: 2088d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2089d763cef2SBarry Smith - usrP - optional user context 2090d763cef2SBarry Smith 2091daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 2092daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2093daf670e6SBarry Smith 2094d763cef2SBarry Smith Level: intermediate 2095d763cef2SBarry Smith 2096d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 2097d763cef2SBarry Smith 2098d763cef2SBarry Smith .seealso: TSGetApplicationContext() 2099d763cef2SBarry Smith @*/ 21007087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 2101d763cef2SBarry Smith { 2102d763cef2SBarry Smith PetscFunctionBegin; 21030700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2104d763cef2SBarry Smith ts->user = usrP; 2105d763cef2SBarry Smith PetscFunctionReturn(0); 2106d763cef2SBarry Smith } 2107d763cef2SBarry Smith 2108b07ff414SBarry Smith /*@ 2109d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2110d763cef2SBarry Smith timestepper. 2111d763cef2SBarry Smith 2112d763cef2SBarry Smith Not Collective 2113d763cef2SBarry Smith 2114d763cef2SBarry Smith Input Parameter: 2115d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2116d763cef2SBarry Smith 2117d763cef2SBarry Smith Output Parameter: 2118d763cef2SBarry Smith . usrP - user context 2119d763cef2SBarry Smith 2120daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 2121daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2122daf670e6SBarry Smith 2123d763cef2SBarry Smith Level: intermediate 2124d763cef2SBarry Smith 2125d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 2126d763cef2SBarry Smith 2127d763cef2SBarry Smith .seealso: TSSetApplicationContext() 2128d763cef2SBarry Smith @*/ 2129e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 2130d763cef2SBarry Smith { 2131d763cef2SBarry Smith PetscFunctionBegin; 21320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2133e71120c6SJed Brown *(void**)usrP = ts->user; 2134d763cef2SBarry Smith PetscFunctionReturn(0); 2135d763cef2SBarry Smith } 2136d763cef2SBarry Smith 2137d763cef2SBarry Smith /*@ 213880275a0aSLisandro Dalcin TSGetStepNumber - Gets the number of steps completed. 2139d763cef2SBarry Smith 2140d763cef2SBarry Smith Not Collective 2141d763cef2SBarry Smith 2142d763cef2SBarry Smith Input Parameter: 2143d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2144d763cef2SBarry Smith 2145d763cef2SBarry Smith Output Parameter: 214680275a0aSLisandro Dalcin . steps - number of steps completed so far 2147d763cef2SBarry Smith 2148d763cef2SBarry Smith Level: intermediate 2149d763cef2SBarry Smith 2150d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 21519be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 2152d763cef2SBarry Smith @*/ 215380275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps) 2154d763cef2SBarry Smith { 2155d763cef2SBarry Smith PetscFunctionBegin; 21560700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 215780275a0aSLisandro Dalcin PetscValidIntPointer(steps,2); 215880275a0aSLisandro Dalcin *steps = ts->steps; 215980275a0aSLisandro Dalcin PetscFunctionReturn(0); 216080275a0aSLisandro Dalcin } 216180275a0aSLisandro Dalcin 216280275a0aSLisandro Dalcin /*@ 216380275a0aSLisandro Dalcin TSSetStepNumber - Sets the number of steps completed. 216480275a0aSLisandro Dalcin 216580275a0aSLisandro Dalcin Logically Collective on TS 216680275a0aSLisandro Dalcin 216780275a0aSLisandro Dalcin Input Parameters: 216880275a0aSLisandro Dalcin + ts - the TS context 216980275a0aSLisandro Dalcin - steps - number of steps completed so far 217080275a0aSLisandro Dalcin 217180275a0aSLisandro Dalcin Notes: 217280275a0aSLisandro Dalcin For most uses of the TS solvers the user need not explicitly call 217380275a0aSLisandro Dalcin TSSetStepNumber(), as the step counter is appropriately updated in 217480275a0aSLisandro Dalcin TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to 217580275a0aSLisandro Dalcin reinitialize timestepping by setting the step counter to zero (and time 217680275a0aSLisandro Dalcin to the initial time) to solve a similar problem with different initial 217780275a0aSLisandro Dalcin conditions or parameters. Other possible use case is to continue 217880275a0aSLisandro Dalcin timestepping from a previously interrupted run in such a way that TS 217980275a0aSLisandro Dalcin monitors will be called with a initial nonzero step counter. 218080275a0aSLisandro Dalcin 218180275a0aSLisandro Dalcin Level: advanced 218280275a0aSLisandro Dalcin 218380275a0aSLisandro Dalcin .keywords: TS, timestep, set, iteration, number 218480275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution() 218580275a0aSLisandro Dalcin @*/ 218680275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps) 218780275a0aSLisandro Dalcin { 218880275a0aSLisandro Dalcin PetscFunctionBegin; 218980275a0aSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 219080275a0aSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,steps,2); 219180275a0aSLisandro Dalcin if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative"); 219280275a0aSLisandro Dalcin ts->steps = steps; 2193d763cef2SBarry Smith PetscFunctionReturn(0); 2194d763cef2SBarry Smith } 2195d763cef2SBarry Smith 2196d763cef2SBarry Smith /*@ 2197d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2198d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2199d763cef2SBarry Smith 22003f9fe445SBarry Smith Logically Collective on TS 2201d763cef2SBarry Smith 2202d763cef2SBarry Smith Input Parameters: 2203d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2204d763cef2SBarry Smith - time_step - the size of the timestep 2205d763cef2SBarry Smith 2206d763cef2SBarry Smith Level: intermediate 2207d763cef2SBarry Smith 2208aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime() 2209d763cef2SBarry Smith 2210d763cef2SBarry Smith .keywords: TS, set, timestep 2211d763cef2SBarry Smith @*/ 22127087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 2213d763cef2SBarry Smith { 2214d763cef2SBarry Smith PetscFunctionBegin; 22150700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2216c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 2217d763cef2SBarry Smith ts->time_step = time_step; 2218d763cef2SBarry Smith PetscFunctionReturn(0); 2219d763cef2SBarry Smith } 2220d763cef2SBarry Smith 2221a43b19c4SJed Brown /*@ 222249354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 222349354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 222449354f04SShri Abhyankar or just return at the final time TS computed. 2225a43b19c4SJed Brown 2226a43b19c4SJed Brown Logically Collective on TS 2227a43b19c4SJed Brown 2228a43b19c4SJed Brown Input Parameter: 2229a43b19c4SJed Brown + ts - the time-step context 223049354f04SShri Abhyankar - eftopt - exact final time option 2231a43b19c4SJed Brown 2232feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2233feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2234feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2235feed9e9dSBarry Smith 2236feed9e9dSBarry Smith Options Database: 2237feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2238feed9e9dSBarry Smith 2239ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 2240ee346746SBarry Smith then the final time you selected. 2241ee346746SBarry Smith 2242a43b19c4SJed Brown Level: beginner 2243a43b19c4SJed Brown 2244f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime() 2245a43b19c4SJed Brown @*/ 224649354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 2247a43b19c4SJed Brown { 2248a43b19c4SJed Brown PetscFunctionBegin; 2249a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 225049354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 225149354f04SShri Abhyankar ts->exact_final_time = eftopt; 2252a43b19c4SJed Brown PetscFunctionReturn(0); 2253a43b19c4SJed Brown } 2254a43b19c4SJed Brown 2255d763cef2SBarry Smith /*@ 2256f6953c82SLisandro Dalcin TSGetExactFinalTime - Gets the exact final time option. 2257f6953c82SLisandro Dalcin 2258f6953c82SLisandro Dalcin Not Collective 2259f6953c82SLisandro Dalcin 2260f6953c82SLisandro Dalcin Input Parameter: 2261f6953c82SLisandro Dalcin . ts - the TS context 2262f6953c82SLisandro Dalcin 2263f6953c82SLisandro Dalcin Output Parameter: 2264f6953c82SLisandro Dalcin . eftopt - exact final time option 2265f6953c82SLisandro Dalcin 2266f6953c82SLisandro Dalcin Level: beginner 2267f6953c82SLisandro Dalcin 2268f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime() 2269f6953c82SLisandro Dalcin @*/ 2270f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt) 2271f6953c82SLisandro Dalcin { 2272f6953c82SLisandro Dalcin PetscFunctionBegin; 2273f6953c82SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2274f6953c82SLisandro Dalcin PetscValidPointer(eftopt,2); 2275f6953c82SLisandro Dalcin *eftopt = ts->exact_final_time; 2276f6953c82SLisandro Dalcin PetscFunctionReturn(0); 2277f6953c82SLisandro Dalcin } 2278f6953c82SLisandro Dalcin 2279f6953c82SLisandro Dalcin /*@ 2280d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2281d763cef2SBarry Smith 2282d763cef2SBarry Smith Not Collective 2283d763cef2SBarry Smith 2284d763cef2SBarry Smith Input Parameter: 2285d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2286d763cef2SBarry Smith 2287d763cef2SBarry Smith Output Parameter: 2288d763cef2SBarry Smith . dt - the current timestep size 2289d763cef2SBarry Smith 2290d763cef2SBarry Smith Level: intermediate 2291d763cef2SBarry Smith 2292aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime() 2293d763cef2SBarry Smith 2294d763cef2SBarry Smith .keywords: TS, get, timestep 2295d763cef2SBarry Smith @*/ 22967087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 2297d763cef2SBarry Smith { 2298d763cef2SBarry Smith PetscFunctionBegin; 22990700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2300f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 2301d763cef2SBarry Smith *dt = ts->time_step; 2302d763cef2SBarry Smith PetscFunctionReturn(0); 2303d763cef2SBarry Smith } 2304d763cef2SBarry Smith 2305d8e5e3e6SSatish Balay /*@ 2306d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2307d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2308d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2309d763cef2SBarry Smith the solution at the next timestep has been calculated. 2310d763cef2SBarry Smith 2311d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 2312d763cef2SBarry Smith 2313d763cef2SBarry Smith Input Parameter: 2314d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2315d763cef2SBarry Smith 2316d763cef2SBarry Smith Output Parameter: 2317d763cef2SBarry Smith . v - the vector containing the solution 2318d763cef2SBarry Smith 231963e21af5SBarry Smith Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested 232063e21af5SBarry Smith final time. It returns the solution at the next timestep. 232163e21af5SBarry Smith 2322d763cef2SBarry Smith Level: intermediate 2323d763cef2SBarry Smith 2324b2bf4f3aSDebojyoti Ghosh .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents() 2325d763cef2SBarry Smith 2326d763cef2SBarry Smith .keywords: TS, timestep, get, solution 2327d763cef2SBarry Smith @*/ 23287087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 2329d763cef2SBarry Smith { 2330d763cef2SBarry Smith PetscFunctionBegin; 23310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 23324482741eSBarry Smith PetscValidPointer(v,2); 23338737fe31SLisandro Dalcin *v = ts->vec_sol; 2334d763cef2SBarry Smith PetscFunctionReturn(0); 2335d763cef2SBarry Smith } 2336d763cef2SBarry Smith 233703fe5f5eSDebojyoti Ghosh /*@ 2338b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 233903fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2340b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 234103fe5f5eSDebojyoti Ghosh structure as the solution vector. 234203fe5f5eSDebojyoti Ghosh 234303fe5f5eSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 234403fe5f5eSDebojyoti Ghosh 234503fe5f5eSDebojyoti Ghosh Parameters : 234603fe5f5eSDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 2347b2bf4f3aSDebojyoti Ghosh . n - If v is PETSC_NULL, then the number of solution components is 2348b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 234903fe5f5eSDebojyoti Ghosh returned in v. 2350b2bf4f3aSDebojyoti Ghosh . v - the vector containing the n-th solution component 235103fe5f5eSDebojyoti Ghosh (may be PETSC_NULL to use this function to find out 2352b2bf4f3aSDebojyoti Ghosh the number of solutions components). 235303fe5f5eSDebojyoti Ghosh 23544cdd57e5SDebojyoti Ghosh Level: advanced 235503fe5f5eSDebojyoti Ghosh 235603fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution() 235703fe5f5eSDebojyoti Ghosh 235803fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution 235903fe5f5eSDebojyoti Ghosh @*/ 2360b2bf4f3aSDebojyoti Ghosh PetscErrorCode TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v) 236103fe5f5eSDebojyoti Ghosh { 236203fe5f5eSDebojyoti Ghosh PetscErrorCode ierr; 236303fe5f5eSDebojyoti Ghosh 236403fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 236503fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2366b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 236703fe5f5eSDebojyoti Ghosh else { 2368b2bf4f3aSDebojyoti Ghosh ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr); 236903fe5f5eSDebojyoti Ghosh } 237003fe5f5eSDebojyoti Ghosh PetscFunctionReturn(0); 237103fe5f5eSDebojyoti Ghosh } 237203fe5f5eSDebojyoti Ghosh 23734cdd57e5SDebojyoti Ghosh /*@ 23744cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 23754cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 23764cdd57e5SDebojyoti Ghosh 23774cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 23784cdd57e5SDebojyoti Ghosh 23794cdd57e5SDebojyoti Ghosh Parameters : 23804cdd57e5SDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 23814cdd57e5SDebojyoti Ghosh . v - the vector containing the auxiliary solution 23824cdd57e5SDebojyoti Ghosh 23834cdd57e5SDebojyoti Ghosh Level: intermediate 23844cdd57e5SDebojyoti Ghosh 23854cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution() 23864cdd57e5SDebojyoti Ghosh 23874cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution 23884cdd57e5SDebojyoti Ghosh @*/ 23894cdd57e5SDebojyoti Ghosh PetscErrorCode TSGetAuxSolution(TS ts,Vec *v) 23904cdd57e5SDebojyoti Ghosh { 23914cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 23924cdd57e5SDebojyoti Ghosh 23934cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23944cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2395f6356ec7SDebojyoti Ghosh if (ts->ops->getauxsolution) { 23964cdd57e5SDebojyoti Ghosh ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr); 2397f6356ec7SDebojyoti Ghosh } else { 2398f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v); CHKERRQ(ierr); 2399f6356ec7SDebojyoti Ghosh } 24004cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 24014cdd57e5SDebojyoti Ghosh } 24024cdd57e5SDebojyoti Ghosh 24034cdd57e5SDebojyoti Ghosh /*@ 24044cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 24054cdd57e5SDebojyoti Ghosh TSType has an error estimation functionality. 24064cdd57e5SDebojyoti Ghosh 24074cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 24084cdd57e5SDebojyoti Ghosh 24099657682dSDebojyoti Ghosh Note: MUST call after TSSetUp() 24109657682dSDebojyoti Ghosh 24114cdd57e5SDebojyoti Ghosh Parameters : 24124cdd57e5SDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 2413657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 24144cdd57e5SDebojyoti Ghosh . v - the vector containing the error (same size as the solution). 24154cdd57e5SDebojyoti Ghosh 24164cdd57e5SDebojyoti Ghosh Level: intermediate 24174cdd57e5SDebojyoti Ghosh 241857df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError() 24194cdd57e5SDebojyoti Ghosh 24204cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error 24214cdd57e5SDebojyoti Ghosh @*/ 24220a01e1b2SEmil Constantinescu PetscErrorCode TSGetTimeError(TS ts,PetscInt n,Vec *v) 24234cdd57e5SDebojyoti Ghosh { 24244cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 24254cdd57e5SDebojyoti Ghosh 24264cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 24274cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2428f6356ec7SDebojyoti Ghosh if (ts->ops->gettimeerror) { 24290a01e1b2SEmil Constantinescu ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr); 2430f6356ec7SDebojyoti Ghosh } else { 2431f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v);CHKERRQ(ierr); 2432f6356ec7SDebojyoti Ghosh } 24334cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 24344cdd57e5SDebojyoti Ghosh } 24354cdd57e5SDebojyoti Ghosh 243657df6a1bSDebojyoti Ghosh /*@ 243757df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 243857df6a1bSDebojyoti Ghosh TSType has an error estimation functionality. This can be used 243957df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 244057df6a1bSDebojyoti Ghosh 244157df6a1bSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 244257df6a1bSDebojyoti Ghosh 244357df6a1bSDebojyoti Ghosh Parameters : 244457df6a1bSDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 244557df6a1bSDebojyoti Ghosh . v - the vector containing the error (same size as the solution). 244657df6a1bSDebojyoti Ghosh 244757df6a1bSDebojyoti Ghosh Level: intermediate 244857df6a1bSDebojyoti Ghosh 244957df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError) 245057df6a1bSDebojyoti Ghosh 245157df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error 245257df6a1bSDebojyoti Ghosh @*/ 245357df6a1bSDebojyoti Ghosh PetscErrorCode TSSetTimeError(TS ts,Vec v) 245457df6a1bSDebojyoti Ghosh { 245557df6a1bSDebojyoti Ghosh PetscErrorCode ierr; 245657df6a1bSDebojyoti Ghosh 245757df6a1bSDebojyoti Ghosh PetscFunctionBegin; 245857df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 24599657682dSDebojyoti Ghosh if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first"); 246057df6a1bSDebojyoti Ghosh if (ts->ops->settimeerror) { 246157df6a1bSDebojyoti Ghosh ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr); 246257df6a1bSDebojyoti Ghosh } 246357df6a1bSDebojyoti Ghosh PetscFunctionReturn(0); 246457df6a1bSDebojyoti Ghosh } 246557df6a1bSDebojyoti Ghosh 2466c235aa8dSHong Zhang /*@ 2467dfb21088SHong Zhang TSGetCostGradients - Returns the gradients from the TSAdjointSolve() 2468c235aa8dSHong Zhang 2469c235aa8dSHong Zhang Not Collective, but Vec returned is parallel if TS is parallel 2470c235aa8dSHong Zhang 2471c235aa8dSHong Zhang Input Parameter: 2472c235aa8dSHong Zhang . ts - the TS context obtained from TSCreate() 2473c235aa8dSHong Zhang 2474c235aa8dSHong Zhang Output Parameter: 2475abc2977eSBarry Smith + lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 2476abc2977eSBarry Smith - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 2477c235aa8dSHong Zhang 2478c235aa8dSHong Zhang Level: intermediate 2479c235aa8dSHong Zhang 2480c235aa8dSHong Zhang .seealso: TSGetTimeStep() 2481c235aa8dSHong Zhang 2482c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity 2483c235aa8dSHong Zhang @*/ 2484dfb21088SHong Zhang PetscErrorCode TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu) 2485c235aa8dSHong Zhang { 2486c235aa8dSHong Zhang PetscFunctionBegin; 2487c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2488abc2977eSBarry Smith if (numcost) *numcost = ts->numcost; 2489abc2977eSBarry Smith if (lambda) *lambda = ts->vecs_sensi; 2490abc2977eSBarry Smith if (mu) *mu = ts->vecs_sensip; 2491c235aa8dSHong Zhang PetscFunctionReturn(0); 2492c235aa8dSHong Zhang } 2493c235aa8dSHong Zhang 2494bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2495d8e5e3e6SSatish Balay /*@ 2496bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2497d763cef2SBarry Smith 2498bdad233fSMatthew Knepley Not collective 2499d763cef2SBarry Smith 2500bdad233fSMatthew Knepley Input Parameters: 2501bdad233fSMatthew Knepley + ts - The TS 2502bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2503d763cef2SBarry Smith .vb 25040910c330SBarry Smith U_t - A U = 0 (linear) 25050910c330SBarry Smith U_t - A(t) U = 0 (linear) 25060910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2507d763cef2SBarry Smith .ve 2508d763cef2SBarry Smith 2509d763cef2SBarry Smith Level: beginner 2510d763cef2SBarry Smith 2511bdad233fSMatthew Knepley .keywords: TS, problem type 2512bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2513d763cef2SBarry Smith @*/ 25147087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2515a7cc72afSBarry Smith { 25169e2a6581SJed Brown PetscErrorCode ierr; 25179e2a6581SJed Brown 2518d763cef2SBarry Smith PetscFunctionBegin; 25190700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2520bdad233fSMatthew Knepley ts->problem_type = type; 25219e2a6581SJed Brown if (type == TS_LINEAR) { 25229e2a6581SJed Brown SNES snes; 25239e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 25249e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 25259e2a6581SJed Brown } 2526d763cef2SBarry Smith PetscFunctionReturn(0); 2527d763cef2SBarry Smith } 2528d763cef2SBarry Smith 2529bdad233fSMatthew Knepley /*@C 2530bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2531bdad233fSMatthew Knepley 2532bdad233fSMatthew Knepley Not collective 2533bdad233fSMatthew Knepley 2534bdad233fSMatthew Knepley Input Parameter: 2535bdad233fSMatthew Knepley . ts - The TS 2536bdad233fSMatthew Knepley 2537bdad233fSMatthew Knepley Output Parameter: 2538bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2539bdad233fSMatthew Knepley .vb 2540089b2837SJed Brown M U_t = A U 2541089b2837SJed Brown M(t) U_t = A(t) U 2542b5abc632SBarry Smith F(t,U,U_t) 2543bdad233fSMatthew Knepley .ve 2544bdad233fSMatthew Knepley 2545bdad233fSMatthew Knepley Level: beginner 2546bdad233fSMatthew Knepley 2547bdad233fSMatthew Knepley .keywords: TS, problem type 2548bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2549bdad233fSMatthew Knepley @*/ 25507087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2551a7cc72afSBarry Smith { 2552bdad233fSMatthew Knepley PetscFunctionBegin; 25530700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 25544482741eSBarry Smith PetscValidIntPointer(type,2); 2555bdad233fSMatthew Knepley *type = ts->problem_type; 2556bdad233fSMatthew Knepley PetscFunctionReturn(0); 2557bdad233fSMatthew Knepley } 2558d763cef2SBarry Smith 2559d763cef2SBarry Smith /*@ 2560d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 2561d763cef2SBarry Smith of a timestepper. 2562d763cef2SBarry Smith 2563d763cef2SBarry Smith Collective on TS 2564d763cef2SBarry Smith 2565d763cef2SBarry Smith Input Parameter: 2566d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2567d763cef2SBarry Smith 2568d763cef2SBarry Smith Notes: 2569d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 2570d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 2571141bd67dSStefano Zampini the call to TSStep() or TSSolve(). However, if one wishes to control this 2572d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 2573141bd67dSStefano Zampini and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve(). 2574d763cef2SBarry Smith 2575d763cef2SBarry Smith Level: advanced 2576d763cef2SBarry Smith 2577d763cef2SBarry Smith .keywords: TS, timestep, setup 2578d763cef2SBarry Smith 2579141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve() 2580d763cef2SBarry Smith @*/ 25817087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 2582d763cef2SBarry Smith { 2583dfbe8321SBarry Smith PetscErrorCode ierr; 25846c6b9e74SPeter Brune DM dm; 25856c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 2586d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 2587cd11d68dSLisandro Dalcin TSIFunction ifun; 25886c6b9e74SPeter Brune TSIJacobian ijac; 2589efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 25906c6b9e74SPeter Brune TSRHSJacobian rhsjac; 25912ffb9264SLisandro Dalcin PetscBool isnone; 2592d763cef2SBarry Smith 2593d763cef2SBarry Smith PetscFunctionBegin; 25940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2595277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 2596277b19d0SLisandro Dalcin 25977adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 2598cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 2599cd11d68dSLisandro Dalcin ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr); 2600d763cef2SBarry Smith } 2601277b19d0SLisandro Dalcin 2602277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 2603277b19d0SLisandro Dalcin 2604e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 2605e1244c69SJed Brown Mat Amat,Pmat; 2606e1244c69SJed Brown SNES snes; 2607e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2608e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 2609e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2610e1244c69SJed Brown * have displaced the RHS matrix */ 2611e1244c69SJed Brown if (Amat == ts->Arhs) { 2612abc0d4abSBarry 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 */ 2613abc0d4abSBarry Smith ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr); 2614e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 2615e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 2616e1244c69SJed Brown } 2617e1244c69SJed Brown if (Pmat == ts->Brhs) { 2618abc0d4abSBarry Smith ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 2619e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 2620e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 2621e1244c69SJed Brown } 2622e1244c69SJed Brown } 26232ffb9264SLisandro Dalcin 26242ffb9264SLisandro Dalcin ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 26252ffb9264SLisandro Dalcin ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr); 26262ffb9264SLisandro Dalcin 2627277b19d0SLisandro Dalcin if (ts->ops->setup) { 2628000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 2629277b19d0SLisandro Dalcin } 2630277b19d0SLisandro Dalcin 26312ffb9264SLisandro Dalcin /* Attempt to check/preset a default value for the exact final time option */ 26322ffb9264SLisandro Dalcin ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr); 26332ffb9264SLisandro Dalcin if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) 26342ffb9264SLisandro Dalcin ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP; 26352ffb9264SLisandro Dalcin 2636a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 26376c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 26386c6b9e74SPeter Brune */ 26396c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 26400298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 26416c6b9e74SPeter Brune if (!func) { 26426c6b9e74SPeter Brune ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 26436c6b9e74SPeter Brune } 2644a6772fa2SLisandro 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. 26456c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 26466c6b9e74SPeter Brune */ 26470298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 26480298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 2649efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr); 26500298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 2651efe9872eSLisandro Dalcin if (!jac && (ijac || i2jac || rhsjac)) { 26526c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 26536c6b9e74SPeter Brune } 2654c0517034SDebojyoti Ghosh 2655c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 2656c0517034SDebojyoti Ghosh if (ts->ops->startingmethod) { 2657c0517034SDebojyoti Ghosh ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr); 2658c0517034SDebojyoti Ghosh } 2659c0517034SDebojyoti Ghosh 2660277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2661277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2662277b19d0SLisandro Dalcin } 2663277b19d0SLisandro Dalcin 2664f6a906c0SBarry Smith /*@ 2665f6a906c0SBarry Smith TSAdjointSetUp - Sets up the internal data structures for the later use 2666f6a906c0SBarry Smith of an adjoint solver 2667f6a906c0SBarry Smith 2668f6a906c0SBarry Smith Collective on TS 2669f6a906c0SBarry Smith 2670f6a906c0SBarry Smith Input Parameter: 2671f6a906c0SBarry Smith . ts - the TS context obtained from TSCreate() 2672f6a906c0SBarry Smith 2673f6a906c0SBarry Smith Level: advanced 2674f6a906c0SBarry Smith 2675f6a906c0SBarry Smith .keywords: TS, timestep, setup 2676f6a906c0SBarry Smith 2677947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients() 2678f6a906c0SBarry Smith @*/ 2679f6a906c0SBarry Smith PetscErrorCode TSAdjointSetUp(TS ts) 2680f6a906c0SBarry Smith { 2681f6a906c0SBarry Smith PetscErrorCode ierr; 2682f6a906c0SBarry Smith 2683f6a906c0SBarry Smith PetscFunctionBegin; 2684f6a906c0SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2685f6a906c0SBarry Smith if (ts->adjointsetupcalled) PetscFunctionReturn(0); 268623706b9aSHong Zhang if (!ts->vecs_sensi) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first"); 2687c5767687SHong Zhang if (ts->vecs_sensip && !ts->Jacp) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"Must call TSAdjointSetRHSJacobian() first"); 2688d8151eeaSBarry Smith 2689947abb85SHong Zhang if (ts->vec_costintegral) { /* if there is integral in the cost function */ 2690d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2691d8151eeaSBarry Smith if (ts->vecs_sensip){ 2692d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2693d8151eeaSBarry Smith } 2694947abb85SHong Zhang } 2695d8151eeaSBarry Smith 269642f2b339SBarry Smith if (ts->ops->adjointsetup) { 269742f2b339SBarry Smith ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr); 2698f6a906c0SBarry Smith } 2699f6a906c0SBarry Smith ts->adjointsetupcalled = PETSC_TRUE; 2700f6a906c0SBarry Smith PetscFunctionReturn(0); 2701f6a906c0SBarry Smith } 2702f6a906c0SBarry Smith 2703277b19d0SLisandro Dalcin /*@ 2704277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2705277b19d0SLisandro Dalcin 2706277b19d0SLisandro Dalcin Collective on TS 2707277b19d0SLisandro Dalcin 2708277b19d0SLisandro Dalcin Input Parameter: 2709277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2710277b19d0SLisandro Dalcin 2711277b19d0SLisandro Dalcin Level: beginner 2712277b19d0SLisandro Dalcin 2713277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 2714277b19d0SLisandro Dalcin 2715277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2716277b19d0SLisandro Dalcin @*/ 2717277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2718277b19d0SLisandro Dalcin { 2719277b19d0SLisandro Dalcin PetscErrorCode ierr; 2720277b19d0SLisandro Dalcin 2721277b19d0SLisandro Dalcin PetscFunctionBegin; 2722277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2723b18ea86cSHong Zhang 2724277b19d0SLisandro Dalcin if (ts->ops->reset) { 2725277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2726277b19d0SLisandro Dalcin } 2727277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2728e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2729bbd56ea5SKarl Rupp 27304e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 27314e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2732214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 27336bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2734efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 2735e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2736e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 273738637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2738bbd56ea5SKarl Rupp 2739d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2740d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2741715f1b00SHong Zhang 2742ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 27432c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 274436eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2745715f1b00SHong Zhang 2746022c081aSHong Zhang ierr = PetscFree(ts->vecs_fwdsensipacked);CHKERRQ(ierr); 2747022c081aSHong Zhang 2748277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2749d763cef2SBarry Smith PetscFunctionReturn(0); 2750d763cef2SBarry Smith } 2751d763cef2SBarry Smith 2752d8e5e3e6SSatish Balay /*@ 2753d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2754d763cef2SBarry Smith with TSCreate(). 2755d763cef2SBarry Smith 2756d763cef2SBarry Smith Collective on TS 2757d763cef2SBarry Smith 2758d763cef2SBarry Smith Input Parameter: 2759d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2760d763cef2SBarry Smith 2761d763cef2SBarry Smith Level: beginner 2762d763cef2SBarry Smith 2763d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2764d763cef2SBarry Smith 2765d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2766d763cef2SBarry Smith @*/ 27676bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2768d763cef2SBarry Smith { 27696849ba73SBarry Smith PetscErrorCode ierr; 2770d763cef2SBarry Smith 2771d763cef2SBarry Smith PetscFunctionBegin; 27726bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 27736bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 27746bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2775d763cef2SBarry Smith 27766bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2777277b19d0SLisandro Dalcin 2778e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2779e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 27806bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 27816d4c513bSLisandro Dalcin 2782bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2783bc952696SBarry Smith 278484df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 27856427ac75SLisandro Dalcin ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr); 27866427ac75SLisandro Dalcin 27876bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 27886bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 27896bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 27900dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 27916d4c513bSLisandro Dalcin 2792a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2793d763cef2SBarry Smith PetscFunctionReturn(0); 2794d763cef2SBarry Smith } 2795d763cef2SBarry Smith 2796d8e5e3e6SSatish Balay /*@ 2797d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2798d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2799d763cef2SBarry Smith 2800d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2801d763cef2SBarry Smith 2802d763cef2SBarry Smith Input Parameter: 2803d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2804d763cef2SBarry Smith 2805d763cef2SBarry Smith Output Parameter: 2806d763cef2SBarry Smith . snes - the nonlinear solver context 2807d763cef2SBarry Smith 2808d763cef2SBarry Smith Notes: 2809d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2810d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 281194b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2812d763cef2SBarry Smith 2813d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 28140298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2815d763cef2SBarry Smith 2816d763cef2SBarry Smith Level: beginner 2817d763cef2SBarry Smith 2818d763cef2SBarry Smith .keywords: timestep, get, SNES 2819d763cef2SBarry Smith @*/ 28207087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2821d763cef2SBarry Smith { 2822d372ba47SLisandro Dalcin PetscErrorCode ierr; 2823d372ba47SLisandro Dalcin 2824d763cef2SBarry Smith PetscFunctionBegin; 28250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28264482741eSBarry Smith PetscValidPointer(snes,2); 2827d372ba47SLisandro Dalcin if (!ts->snes) { 2828ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 28290298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 28303bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2831d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2832496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 28339e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 28349e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 28359e2a6581SJed Brown } 2836d372ba47SLisandro Dalcin } 2837d763cef2SBarry Smith *snes = ts->snes; 2838d763cef2SBarry Smith PetscFunctionReturn(0); 2839d763cef2SBarry Smith } 2840d763cef2SBarry Smith 2841deb2cd25SJed Brown /*@ 2842deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2843deb2cd25SJed Brown 2844deb2cd25SJed Brown Collective 2845deb2cd25SJed Brown 2846deb2cd25SJed Brown Input Parameter: 2847deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2848deb2cd25SJed Brown - snes - the nonlinear solver context 2849deb2cd25SJed Brown 2850deb2cd25SJed Brown Notes: 2851deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2852deb2cd25SJed Brown 2853deb2cd25SJed Brown Level: developer 2854deb2cd25SJed Brown 2855deb2cd25SJed Brown .keywords: timestep, set, SNES 2856deb2cd25SJed Brown @*/ 2857deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2858deb2cd25SJed Brown { 2859deb2cd25SJed Brown PetscErrorCode ierr; 2860d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2861deb2cd25SJed Brown 2862deb2cd25SJed Brown PetscFunctionBegin; 2863deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2864deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2865deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2866deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2867bbd56ea5SKarl Rupp 2868deb2cd25SJed Brown ts->snes = snes; 2869bbd56ea5SKarl Rupp 28700298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 28710298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2872740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 28730298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2874740132f1SEmil Constantinescu } 2875deb2cd25SJed Brown PetscFunctionReturn(0); 2876deb2cd25SJed Brown } 2877deb2cd25SJed Brown 2878d8e5e3e6SSatish Balay /*@ 287994b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2880d763cef2SBarry Smith a TS (timestepper) context. 2881d763cef2SBarry Smith 288294b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2883d763cef2SBarry Smith 2884d763cef2SBarry Smith Input Parameter: 2885d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2886d763cef2SBarry Smith 2887d763cef2SBarry Smith Output Parameter: 288894b7f48cSBarry Smith . ksp - the nonlinear solver context 2889d763cef2SBarry Smith 2890d763cef2SBarry Smith Notes: 289194b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2892d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2893d763cef2SBarry Smith KSP and PC contexts as well. 2894d763cef2SBarry Smith 289594b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 28960298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2897d763cef2SBarry Smith 2898d763cef2SBarry Smith Level: beginner 2899d763cef2SBarry Smith 290094b7f48cSBarry Smith .keywords: timestep, get, KSP 2901d763cef2SBarry Smith @*/ 29027087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2903d763cef2SBarry Smith { 2904d372ba47SLisandro Dalcin PetscErrorCode ierr; 2905089b2837SJed Brown SNES snes; 2906d372ba47SLisandro Dalcin 2907d763cef2SBarry Smith PetscFunctionBegin; 29080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 29094482741eSBarry Smith PetscValidPointer(ksp,2); 291017186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2911e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2912089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2913089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2914d763cef2SBarry Smith PetscFunctionReturn(0); 2915d763cef2SBarry Smith } 2916d763cef2SBarry Smith 2917d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2918d763cef2SBarry Smith 2919adb62b0dSMatthew Knepley /*@ 2920618ce8baSLisandro Dalcin TSSetMaxSteps - Sets the maximum number of steps to use. 2921618ce8baSLisandro Dalcin 2922618ce8baSLisandro Dalcin Logically Collective on TS 2923618ce8baSLisandro Dalcin 2924618ce8baSLisandro Dalcin Input Parameters: 2925618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 2926618ce8baSLisandro Dalcin - maxsteps - maximum number of steps to use 2927618ce8baSLisandro Dalcin 2928618ce8baSLisandro Dalcin Options Database Keys: 2929618ce8baSLisandro Dalcin . -ts_max_steps <maxsteps> - Sets maxsteps 2930618ce8baSLisandro Dalcin 2931618ce8baSLisandro Dalcin Notes: 2932618ce8baSLisandro Dalcin The default maximum number of steps is 5000 2933618ce8baSLisandro Dalcin 2934618ce8baSLisandro Dalcin Level: intermediate 2935618ce8baSLisandro Dalcin 2936618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, steps 2937618ce8baSLisandro Dalcin 2938618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime() 2939618ce8baSLisandro Dalcin @*/ 2940618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps) 2941618ce8baSLisandro Dalcin { 2942618ce8baSLisandro Dalcin PetscFunctionBegin; 2943618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2944618ce8baSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2945618ce8baSLisandro Dalcin if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative"); 2946618ce8baSLisandro Dalcin ts->max_steps = maxsteps; 2947618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2948618ce8baSLisandro Dalcin } 2949618ce8baSLisandro Dalcin 2950618ce8baSLisandro Dalcin /*@ 2951618ce8baSLisandro Dalcin TSGetMaxSteps - Gets the maximum number of steps to use. 2952618ce8baSLisandro Dalcin 2953618ce8baSLisandro Dalcin Not Collective 2954618ce8baSLisandro Dalcin 2955618ce8baSLisandro Dalcin Input Parameters: 2956618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 2957618ce8baSLisandro Dalcin 2958618ce8baSLisandro Dalcin Output Parameter: 2959618ce8baSLisandro Dalcin . maxsteps - maximum number of steps to use 2960618ce8baSLisandro Dalcin 2961618ce8baSLisandro Dalcin Level: advanced 2962618ce8baSLisandro Dalcin 2963618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, steps 2964618ce8baSLisandro Dalcin 2965618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime() 2966618ce8baSLisandro Dalcin @*/ 2967618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps) 2968618ce8baSLisandro Dalcin { 2969618ce8baSLisandro Dalcin PetscFunctionBegin; 2970618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2971618ce8baSLisandro Dalcin PetscValidIntPointer(maxsteps,2); 2972618ce8baSLisandro Dalcin *maxsteps = ts->max_steps; 2973618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2974618ce8baSLisandro Dalcin } 2975618ce8baSLisandro Dalcin 2976618ce8baSLisandro Dalcin /*@ 2977618ce8baSLisandro Dalcin TSSetMaxTime - Sets the maximum (or final) time for timestepping. 2978618ce8baSLisandro Dalcin 2979618ce8baSLisandro Dalcin Logically Collective on TS 2980618ce8baSLisandro Dalcin 2981618ce8baSLisandro Dalcin Input Parameters: 2982618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 2983618ce8baSLisandro Dalcin - maxtime - final time to step to 2984618ce8baSLisandro Dalcin 2985618ce8baSLisandro Dalcin Options Database Keys: 2986ef85077eSLisandro Dalcin . -ts_max_time <maxtime> - Sets maxtime 2987618ce8baSLisandro Dalcin 2988618ce8baSLisandro Dalcin Notes: 2989618ce8baSLisandro Dalcin The default maximum time is 5.0 2990618ce8baSLisandro Dalcin 2991618ce8baSLisandro Dalcin Level: intermediate 2992618ce8baSLisandro Dalcin 2993618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, time 2994618ce8baSLisandro Dalcin 2995618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime() 2996618ce8baSLisandro Dalcin @*/ 2997618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime) 2998618ce8baSLisandro Dalcin { 2999618ce8baSLisandro Dalcin PetscFunctionBegin; 3000618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3001618ce8baSLisandro Dalcin PetscValidLogicalCollectiveReal(ts,maxtime,2); 3002618ce8baSLisandro Dalcin ts->max_time = maxtime; 3003618ce8baSLisandro Dalcin PetscFunctionReturn(0); 3004618ce8baSLisandro Dalcin } 3005618ce8baSLisandro Dalcin 3006618ce8baSLisandro Dalcin /*@ 3007618ce8baSLisandro Dalcin TSGetMaxTime - Gets the maximum (or final) time for timestepping. 3008618ce8baSLisandro Dalcin 3009618ce8baSLisandro Dalcin Not Collective 3010618ce8baSLisandro Dalcin 3011618ce8baSLisandro Dalcin Input Parameters: 3012618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 3013618ce8baSLisandro Dalcin 3014618ce8baSLisandro Dalcin Output Parameter: 3015618ce8baSLisandro Dalcin . maxtime - final time to step to 3016618ce8baSLisandro Dalcin 3017618ce8baSLisandro Dalcin Level: advanced 3018618ce8baSLisandro Dalcin 3019618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, time 3020618ce8baSLisandro Dalcin 3021618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps() 3022618ce8baSLisandro Dalcin @*/ 3023618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime) 3024618ce8baSLisandro Dalcin { 3025618ce8baSLisandro Dalcin PetscFunctionBegin; 3026618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3027618ce8baSLisandro Dalcin PetscValidRealPointer(maxtime,2); 3028618ce8baSLisandro Dalcin *maxtime = ts->max_time; 3029618ce8baSLisandro Dalcin PetscFunctionReturn(0); 3030618ce8baSLisandro Dalcin } 3031618ce8baSLisandro Dalcin 3032618ce8baSLisandro Dalcin /*@ 3033aaa6c58dSLisandro Dalcin TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep(). 3034edc382c3SSatish Balay 3035edc382c3SSatish Balay Level: deprecated 3036edc382c3SSatish Balay 3037aaa6c58dSLisandro Dalcin @*/ 3038aaa6c58dSLisandro Dalcin PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 3039aaa6c58dSLisandro Dalcin { 3040aaa6c58dSLisandro Dalcin PetscErrorCode ierr; 3041aaa6c58dSLisandro Dalcin PetscFunctionBegin; 3042aaa6c58dSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3043aaa6c58dSLisandro Dalcin ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 3044aaa6c58dSLisandro Dalcin ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 3045aaa6c58dSLisandro Dalcin PetscFunctionReturn(0); 3046aaa6c58dSLisandro Dalcin } 3047aaa6c58dSLisandro Dalcin 3048aaa6c58dSLisandro Dalcin /*@ 304919eac22cSLisandro Dalcin TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime(). 3050edc382c3SSatish Balay 3051edc382c3SSatish Balay Level: deprecated 3052edc382c3SSatish Balay 3053adb62b0dSMatthew Knepley @*/ 30547087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 3055adb62b0dSMatthew Knepley { 3056adb62b0dSMatthew Knepley PetscFunctionBegin; 30570700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3058abc0a331SBarry Smith if (maxsteps) { 30594482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 3060adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 3061adb62b0dSMatthew Knepley } 3062abc0a331SBarry Smith if (maxtime) { 30634482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 3064adb62b0dSMatthew Knepley *maxtime = ts->max_time; 3065adb62b0dSMatthew Knepley } 3066adb62b0dSMatthew Knepley PetscFunctionReturn(0); 3067adb62b0dSMatthew Knepley } 3068adb62b0dSMatthew Knepley 3069d763cef2SBarry Smith /*@ 307019eac22cSLisandro Dalcin TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime(). 3071edc382c3SSatish Balay 3072edc382c3SSatish Balay Level: deprecated 3073edc382c3SSatish Balay 3074d763cef2SBarry Smith @*/ 30757087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 3076d763cef2SBarry Smith { 3077d763cef2SBarry Smith PetscFunctionBegin; 30780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3079c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 3080c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 308139b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 308239b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 3083d763cef2SBarry Smith PetscFunctionReturn(0); 3084d763cef2SBarry Smith } 3085d763cef2SBarry Smith 3086d763cef2SBarry Smith /*@ 30875c5f5948SLisandro Dalcin TSGetTimeStepNumber - Deprecated, use TSGetStepNumber(). 3088edc382c3SSatish Balay 3089edc382c3SSatish Balay Level: deprecated 3090edc382c3SSatish Balay 30915c5f5948SLisandro Dalcin @*/ 3092e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 30935c5f5948SLisandro Dalcin 309419eac22cSLisandro Dalcin /*@ 30954f4e0956SLisandro Dalcin TSGetTotalSteps - Deprecated, use TSGetStepNumber(). 3096edc382c3SSatish Balay 3097edc382c3SSatish Balay Level: deprecated 3098edc382c3SSatish Balay 30994f4e0956SLisandro Dalcin @*/ 31004f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 31014f4e0956SLisandro Dalcin 31024f4e0956SLisandro Dalcin /*@ 3103d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 3104d763cef2SBarry Smith for use by the TS routines. 3105d763cef2SBarry Smith 31063f9fe445SBarry Smith Logically Collective on TS and Vec 3107d763cef2SBarry Smith 3108d763cef2SBarry Smith Input Parameters: 3109d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 31100910c330SBarry Smith - u - the solution vector 3111d763cef2SBarry Smith 3112d763cef2SBarry Smith Level: beginner 3113d763cef2SBarry Smith 3114715f1b00SHong Zhang .keywords: TS, timestep, set, solution, initial values 3115d763cef2SBarry Smith @*/ 31160910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 3117d763cef2SBarry Smith { 31188737fe31SLisandro Dalcin PetscErrorCode ierr; 3119496e6a7aSJed Brown DM dm; 31208737fe31SLisandro Dalcin 3121d763cef2SBarry Smith PetscFunctionBegin; 31220700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31230910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 31240910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 31256bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 31260910c330SBarry Smith ts->vec_sol = u; 3127bbd56ea5SKarl Rupp 3128496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 31290910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 3130d763cef2SBarry Smith PetscFunctionReturn(0); 3131d763cef2SBarry Smith } 3132d763cef2SBarry Smith 313373b18844SBarry Smith /*@ 313473b18844SBarry Smith TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time 313573b18844SBarry Smith 313673b18844SBarry Smith Logically Collective on TS 313773b18844SBarry Smith 313873b18844SBarry Smith Input Parameters: 313973b18844SBarry Smith + ts - the TS context obtained from TSCreate() 314073b18844SBarry Smith . steps - number of steps to use 314173b18844SBarry Smith 314273b18844SBarry Smith Level: intermediate 314373b18844SBarry Smith 314473b18844SBarry Smith Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this 314573b18844SBarry Smith so as to integrate back to less than the original timestep 314673b18844SBarry Smith 314773b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations 314873b18844SBarry Smith 314973b18844SBarry Smith .seealso: TSSetExactFinalTime() 315073b18844SBarry Smith @*/ 315173b18844SBarry Smith PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps) 315273b18844SBarry Smith { 315373b18844SBarry Smith PetscFunctionBegin; 315473b18844SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 315573b18844SBarry Smith PetscValidLogicalCollectiveInt(ts,steps,2); 315673b18844SBarry Smith if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps"); 31572808aa04SLisandro Dalcin if (steps > ts->steps) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back more than the total number of forward steps"); 315873b18844SBarry Smith ts->adjoint_max_steps = steps; 315973b18844SBarry Smith PetscFunctionReturn(0); 316073b18844SBarry Smith } 316173b18844SBarry Smith 3162c235aa8dSHong Zhang /*@ 3163715f1b00SHong Zhang TSSetCostGradients - Sets the initial value of the gradients of the cost function w.r.t. initial values and w.r.t. the problem parameters 31640cf82b59SBarry Smith for use by the TSAdjoint routines. 3165c235aa8dSHong Zhang 3166c235aa8dSHong Zhang Logically Collective on TS and Vec 3167c235aa8dSHong Zhang 3168c235aa8dSHong Zhang Input Parameters: 3169c235aa8dSHong Zhang + ts - the TS context obtained from TSCreate() 3170abc2977eSBarry 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 3171abc2977eSBarry Smith - mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters 3172c235aa8dSHong Zhang 3173c235aa8dSHong Zhang Level: beginner 3174c235aa8dSHong Zhang 3175abc2977eSBarry Smith Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime mu_i = df/dp|finaltime 31760cf82b59SBarry Smith 3177715f1b00SHong Zhang .keywords: TS, timestep, set, sensitivity, initial values 3178c235aa8dSHong Zhang @*/ 3179dfb21088SHong Zhang PetscErrorCode TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu) 3180c235aa8dSHong Zhang { 3181c235aa8dSHong Zhang PetscFunctionBegin; 3182c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3183abc2977eSBarry Smith PetscValidPointer(lambda,2); 3184abc2977eSBarry Smith ts->vecs_sensi = lambda; 3185abc2977eSBarry Smith ts->vecs_sensip = mu; 3186dfb21088SHong 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"); 3187abc2977eSBarry Smith ts->numcost = numcost; 3188ad8e2604SHong Zhang PetscFunctionReturn(0); 3189ad8e2604SHong Zhang } 3190ad8e2604SHong Zhang 3191ad8e2604SHong Zhang /*@C 31920cf82b59SBarry 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. 3193ad8e2604SHong Zhang 3194ad8e2604SHong Zhang Logically Collective on TS 3195ad8e2604SHong Zhang 3196ad8e2604SHong Zhang Input Parameters: 3197ad8e2604SHong Zhang + ts - The TS context obtained from TSCreate() 3198ad8e2604SHong Zhang - func - The function 3199ad8e2604SHong Zhang 3200ad8e2604SHong Zhang Calling sequence of func: 32010cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx); 320205755b9cSHong Zhang + t - current timestep 32030cf82b59SBarry Smith . y - input vector (current ODE solution) 320405755b9cSHong Zhang . A - output matrix 320505755b9cSHong Zhang - ctx - [optional] user-defined function context 3206ad8e2604SHong Zhang 3207ad8e2604SHong Zhang Level: intermediate 3208ad8e2604SHong Zhang 32090cf82b59SBarry 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 32100cf82b59SBarry Smith 3211ad8e2604SHong Zhang .keywords: TS, sensitivity 3212ad8e2604SHong Zhang .seealso: 3213ad8e2604SHong Zhang @*/ 32145bf8c567SBarry Smith PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 3215ad8e2604SHong Zhang { 3216ad8e2604SHong Zhang PetscErrorCode ierr; 3217ad8e2604SHong Zhang 3218ad8e2604SHong Zhang PetscFunctionBegin; 3219ad8e2604SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 322023706b9aSHong Zhang PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 3221ad8e2604SHong Zhang 3222ad8e2604SHong Zhang ts->rhsjacobianp = func; 3223ad8e2604SHong Zhang ts->rhsjacobianpctx = ctx; 3224ad8e2604SHong Zhang if(Amat) { 3225ad8e2604SHong Zhang ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 3226ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 3227ad8e2604SHong Zhang ts->Jacp = Amat; 3228ad8e2604SHong Zhang } 3229ad8e2604SHong Zhang PetscFunctionReturn(0); 3230ad8e2604SHong Zhang } 3231ad8e2604SHong Zhang 32320cf82b59SBarry Smith /*@C 32335bf8c567SBarry Smith TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function. 3234ad8e2604SHong Zhang 3235ad8e2604SHong Zhang Collective on TS 3236ad8e2604SHong Zhang 3237ad8e2604SHong Zhang Input Parameters: 3238ad8e2604SHong Zhang . ts - The TS context obtained from TSCreate() 3239ad8e2604SHong Zhang 3240ad8e2604SHong Zhang Level: developer 3241ad8e2604SHong Zhang 3242ad8e2604SHong Zhang .keywords: TS, sensitivity 32435bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian() 3244ad8e2604SHong Zhang @*/ 32455bf8c567SBarry Smith PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat) 3246ad8e2604SHong Zhang { 3247ad8e2604SHong Zhang PetscErrorCode ierr; 3248ad8e2604SHong Zhang 3249ad8e2604SHong Zhang PetscFunctionBegin; 3250ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3251ad8e2604SHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 3252ad8e2604SHong Zhang PetscValidPointer(Amat,4); 3253ad8e2604SHong Zhang 3254ad8e2604SHong Zhang PetscStackPush("TS user JacobianP function for sensitivity analysis"); 3255ad8e2604SHong Zhang ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 3256ad8e2604SHong Zhang PetscStackPop; 3257c235aa8dSHong Zhang PetscFunctionReturn(0); 3258c235aa8dSHong Zhang } 3259c235aa8dSHong Zhang 32606fd0887fSHong Zhang /*@C 3261dfb21088SHong Zhang TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions 32626fd0887fSHong Zhang 32636fd0887fSHong Zhang Logically Collective on TS 32646fd0887fSHong Zhang 32656fd0887fSHong Zhang Input Parameters: 32666fd0887fSHong Zhang + ts - the TS context obtained from TSCreate() 3267abc2977eSBarry Smith . numcost - number of gradients to be computed, this is the number of cost functions 32683d1d12c6SHong Zhang . costintegral - vector that stores the integral values 32690cf82b59SBarry Smith . rf - routine for evaluating the integrand function 3270b612ec54SBarry Smith . drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y 3271b612ec54SBarry Smith . drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p 3272feaa1ddbSSatish Balay . fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run 3273b612ec54SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 32746fd0887fSHong Zhang 32750cf82b59SBarry Smith Calling sequence of rf: 32763d1d12c6SHong Zhang $ PetscErrorCode rf(TS ts,PetscReal t,Vec y,Vec f,void *ctx); 32776fd0887fSHong Zhang 3278b612ec54SBarry Smith Calling sequence of drdyf: 32790cf82b59SBarry Smith $ PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx); 3280b612ec54SBarry Smith 3281b612ec54SBarry Smith Calling sequence of drdpf: 32820cf82b59SBarry Smith $ PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx); 3283b612ec54SBarry Smith 3284b612ec54SBarry Smith Level: intermediate 32856fd0887fSHong Zhang 3286715f1b00SHong Zhang Notes: For optimization there is usually a single cost function (numcost = 1). For sensitivities there may be multiple cost functions 32870cf82b59SBarry Smith 32886fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 32896fd0887fSHong Zhang 3290dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients() 32916fd0887fSHong Zhang @*/ 32923d1d12c6SHong Zhang PetscErrorCode TSSetCostIntegrand(TS ts,PetscInt numcost,Vec costintegral,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*), 3293d8151eeaSBarry Smith PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*), 3294b1cb36f3SHong Zhang PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*), 3295b1cb36f3SHong Zhang PetscBool fwd,void *ctx) 32966fd0887fSHong Zhang { 32976fd0887fSHong Zhang PetscErrorCode ierr; 32986fd0887fSHong Zhang 32996fd0887fSHong Zhang PetscFunctionBegin; 33006fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 33013d1d12c6SHong Zhang if (costintegral) PetscValidHeaderSpecific(costintegral,VEC_CLASSID,3); 3302715f1b00SHong 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() or TSForwardSetIntegralGradients()"); 3303c72ed514SHong Zhang if (!ts->numcost) ts->numcost=numcost; 33046fd0887fSHong Zhang 33053d1d12c6SHong Zhang if (costintegral) { 33063d1d12c6SHong Zhang ierr = PetscObjectReference((PetscObject)costintegral);CHKERRQ(ierr); 33073d1d12c6SHong Zhang ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 33083d1d12c6SHong Zhang ts->vec_costintegral = costintegral; 3309afe7b317SHong Zhang } else { 3310afe7b317SHong Zhang if (!ts->vec_costintegral) { /* Create a seq vec if user does not provide one */ 3311afe7b317SHong Zhang ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr); 3312afe7b317SHong Zhang } else { 3313afe7b317SHong Zhang ierr = VecSet(ts->vec_costintegral,0.0);CHKERRQ(ierr); 3314afe7b317SHong Zhang } 3315afe7b317SHong Zhang } 3316afe7b317SHong Zhang if (!ts->vec_costintegrand) { 33172c39e106SBarry Smith ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr); 3318afe7b317SHong Zhang } else { 3319afe7b317SHong Zhang ierr = VecSet(ts->vec_costintegrand,0.0);CHKERRQ(ierr); 3320afe7b317SHong Zhang } 3321afe7b317SHong Zhang ts->costintegralfwd = fwd; /* Evaluate the cost integral in forward run if fwd is true */ 33220cf82b59SBarry Smith ts->costintegrand = rf; 332305755b9cSHong Zhang ts->costintegrandctx = ctx; 3324b612ec54SBarry Smith ts->drdyfunction = drdyf; 3325b612ec54SBarry Smith ts->drdpfunction = drdpf; 33266fd0887fSHong Zhang PetscFunctionReturn(0); 33276fd0887fSHong Zhang } 33286fd0887fSHong Zhang 332936eaed60SHong Zhang /*@ 3330dfb21088SHong Zhang TSGetCostIntegral - Returns the values of the integral term in the cost functions. 333136eaed60SHong Zhang It is valid to call the routine after a backward run. 333236eaed60SHong Zhang 333336eaed60SHong Zhang Not Collective 333436eaed60SHong Zhang 333536eaed60SHong Zhang Input Parameter: 333636eaed60SHong Zhang . ts - the TS context obtained from TSCreate() 333736eaed60SHong Zhang 333836eaed60SHong Zhang Output Parameter: 33392c39e106SBarry Smith . v - the vector containing the integrals for each cost function 334036eaed60SHong Zhang 334136eaed60SHong Zhang Level: intermediate 334236eaed60SHong Zhang 3343dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 334436eaed60SHong Zhang 334536eaed60SHong Zhang .keywords: TS, sensitivity analysis 334636eaed60SHong Zhang @*/ 3347dfb21088SHong Zhang PetscErrorCode TSGetCostIntegral(TS ts,Vec *v) 334836eaed60SHong Zhang { 334936eaed60SHong Zhang PetscFunctionBegin; 335036eaed60SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 335136eaed60SHong Zhang PetscValidPointer(v,2); 33522c39e106SBarry Smith *v = ts->vec_costintegral; 335336eaed60SHong Zhang PetscFunctionReturn(0); 335436eaed60SHong Zhang } 335536eaed60SHong Zhang 33566fd0887fSHong Zhang /*@ 3357022c081aSHong Zhang TSComputeCostIntegrand - Evaluates the integral function in the cost functions. 33586fd0887fSHong Zhang 33596fd0887fSHong Zhang Input Parameters: 33606fd0887fSHong Zhang + ts - the TS context 33616fd0887fSHong Zhang . t - current time 33620cf82b59SBarry Smith - y - state vector, i.e. current solution 33636fd0887fSHong Zhang 33646fd0887fSHong Zhang Output Parameter: 3365abc2977eSBarry Smith . q - vector of size numcost to hold the outputs 33666fd0887fSHong Zhang 33676fd0887fSHong Zhang Note: 33686fd0887fSHong Zhang Most users should not need to explicitly call this routine, as it 33696fd0887fSHong Zhang is used internally within the sensitivity analysis context. 33706fd0887fSHong Zhang 33716fd0887fSHong Zhang Level: developer 33726fd0887fSHong Zhang 33736fd0887fSHong Zhang .keywords: TS, compute 33746fd0887fSHong Zhang 3375dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 33766fd0887fSHong Zhang @*/ 3377022c081aSHong Zhang PetscErrorCode TSComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q) 33786fd0887fSHong Zhang { 33796fd0887fSHong Zhang PetscErrorCode ierr; 33806fd0887fSHong Zhang 33816fd0887fSHong Zhang PetscFunctionBegin; 33826fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 33830cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 33846fd0887fSHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,4); 33856fd0887fSHong Zhang 33860cf82b59SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 3387e4680132SHong Zhang if (ts->costintegrand) { 3388e4680132SHong Zhang PetscStackPush("TS user integrand in the cost function"); 33890cf82b59SBarry Smith ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr); 33906fd0887fSHong Zhang PetscStackPop; 33916fd0887fSHong Zhang } else { 33926fd0887fSHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 33936fd0887fSHong Zhang } 33946fd0887fSHong Zhang 33950cf82b59SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 33966fd0887fSHong Zhang PetscFunctionReturn(0); 33976fd0887fSHong Zhang } 33986fd0887fSHong Zhang 339905755b9cSHong Zhang /*@ 3400d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 340105755b9cSHong Zhang 340205755b9cSHong Zhang Collective on TS 340305755b9cSHong Zhang 340405755b9cSHong Zhang Input Parameters: 340505755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 340605755b9cSHong Zhang 340705755b9cSHong Zhang Notes: 3408d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 340905755b9cSHong Zhang so most users would not generally call this routine themselves. 341005755b9cSHong Zhang 341105755b9cSHong Zhang Level: developer 341205755b9cSHong Zhang 341305755b9cSHong Zhang .keywords: TS, sensitivity 3414d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction() 341505755b9cSHong Zhang @*/ 34160cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy) 341705755b9cSHong Zhang { 341805755b9cSHong Zhang PetscErrorCode ierr; 341905755b9cSHong Zhang 342005755b9cSHong Zhang PetscFunctionBegin; 342105755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 34220cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 342305755b9cSHong Zhang 342405755b9cSHong Zhang PetscStackPush("TS user DRDY function for sensitivity analysis"); 34250cf82b59SBarry Smith ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr); 342605755b9cSHong Zhang PetscStackPop; 342705755b9cSHong Zhang PetscFunctionReturn(0); 342805755b9cSHong Zhang } 342905755b9cSHong Zhang 343005755b9cSHong Zhang /*@ 3431d4aa0a58SBarry Smith TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 343205755b9cSHong Zhang 343305755b9cSHong Zhang Collective on TS 343405755b9cSHong Zhang 343505755b9cSHong Zhang Input Parameters: 343605755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 343705755b9cSHong Zhang 343805755b9cSHong Zhang Notes: 343905755b9cSHong Zhang TSDRDPFunction() is typically used for sensitivity implementation, 344005755b9cSHong Zhang so most users would not generally call this routine themselves. 344105755b9cSHong Zhang 344205755b9cSHong Zhang Level: developer 344305755b9cSHong Zhang 344405755b9cSHong Zhang .keywords: TS, sensitivity 3445d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction() 344605755b9cSHong Zhang @*/ 34470cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp) 344805755b9cSHong Zhang { 344905755b9cSHong Zhang PetscErrorCode ierr; 345005755b9cSHong Zhang 345105755b9cSHong Zhang PetscFunctionBegin; 345205755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 34530cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 345405755b9cSHong Zhang 345505755b9cSHong Zhang PetscStackPush("TS user DRDP function for sensitivity analysis"); 34560cf82b59SBarry Smith ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr); 345705755b9cSHong Zhang PetscStackPop; 345805755b9cSHong Zhang PetscFunctionReturn(0); 345905755b9cSHong Zhang } 346005755b9cSHong Zhang 3461ac226902SBarry Smith /*@C 3462000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 34633f2090d5SJed Brown called once at the beginning of each time step. 3464000e7ae3SMatthew Knepley 34653f9fe445SBarry Smith Logically Collective on TS 3466000e7ae3SMatthew Knepley 3467000e7ae3SMatthew Knepley Input Parameters: 3468000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3469000e7ae3SMatthew Knepley - func - The function 3470000e7ae3SMatthew Knepley 3471000e7ae3SMatthew Knepley Calling sequence of func: 3472000e7ae3SMatthew Knepley . func (TS ts); 3473000e7ae3SMatthew Knepley 3474000e7ae3SMatthew Knepley Level: intermediate 3475000e7ae3SMatthew Knepley 3476000e7ae3SMatthew Knepley .keywords: TS, timestep 3477dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep() 3478000e7ae3SMatthew Knepley @*/ 34797087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3480000e7ae3SMatthew Knepley { 3481000e7ae3SMatthew Knepley PetscFunctionBegin; 34820700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3483ae60f76fSBarry Smith ts->prestep = func; 3484000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3485000e7ae3SMatthew Knepley } 3486000e7ae3SMatthew Knepley 348709ee8438SJed Brown /*@ 34883f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 34893f2090d5SJed Brown 34903f2090d5SJed Brown Collective on TS 34913f2090d5SJed Brown 34923f2090d5SJed Brown Input Parameters: 34933f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 34943f2090d5SJed Brown 34953f2090d5SJed Brown Notes: 34963f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 34973f2090d5SJed Brown so most users would not generally call this routine themselves. 34983f2090d5SJed Brown 34993f2090d5SJed Brown Level: developer 35003f2090d5SJed Brown 35013f2090d5SJed Brown .keywords: TS, timestep 35029be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 35033f2090d5SJed Brown @*/ 35047087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 35053f2090d5SJed Brown { 35063f2090d5SJed Brown PetscErrorCode ierr; 35073f2090d5SJed Brown 35083f2090d5SJed Brown PetscFunctionBegin; 35090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3510ae60f76fSBarry Smith if (ts->prestep) { 35115efd42a4SStefano Zampini Vec U; 35125efd42a4SStefano Zampini PetscObjectState sprev,spost; 35135efd42a4SStefano Zampini 35145efd42a4SStefano Zampini ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 35155efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3516ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 35175efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3518dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 3519312ce896SJed Brown } 35203f2090d5SJed Brown PetscFunctionReturn(0); 35213f2090d5SJed Brown } 35223f2090d5SJed Brown 3523b8123daeSJed Brown /*@C 3524b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3525b8123daeSJed Brown called once at the beginning of each stage. 3526b8123daeSJed Brown 3527b8123daeSJed Brown Logically Collective on TS 3528b8123daeSJed Brown 3529b8123daeSJed Brown Input Parameters: 3530b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 3531b8123daeSJed Brown - func - The function 3532b8123daeSJed Brown 3533b8123daeSJed Brown Calling sequence of func: 3534b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 3535b8123daeSJed Brown 3536b8123daeSJed Brown Level: intermediate 3537b8123daeSJed Brown 3538b8123daeSJed Brown Note: 3539b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 354080275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 3541b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 3542b8123daeSJed Brown 3543b8123daeSJed Brown .keywords: TS, timestep 35449be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3545b8123daeSJed Brown @*/ 3546b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 3547b8123daeSJed Brown { 3548b8123daeSJed Brown PetscFunctionBegin; 3549b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3550ae60f76fSBarry Smith ts->prestage = func; 3551b8123daeSJed Brown PetscFunctionReturn(0); 3552b8123daeSJed Brown } 3553b8123daeSJed Brown 35549be3e283SDebojyoti Ghosh /*@C 35559be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 35569be3e283SDebojyoti Ghosh called once at the end of each stage. 35579be3e283SDebojyoti Ghosh 35589be3e283SDebojyoti Ghosh Logically Collective on TS 35599be3e283SDebojyoti Ghosh 35609be3e283SDebojyoti Ghosh Input Parameters: 35619be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 35629be3e283SDebojyoti Ghosh - func - The function 35639be3e283SDebojyoti Ghosh 35649be3e283SDebojyoti Ghosh Calling sequence of func: 35659be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 35669be3e283SDebojyoti Ghosh 35679be3e283SDebojyoti Ghosh Level: intermediate 35689be3e283SDebojyoti Ghosh 35699be3e283SDebojyoti Ghosh Note: 35709be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 357180275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 35729be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 35739be3e283SDebojyoti Ghosh 35749be3e283SDebojyoti Ghosh .keywords: TS, timestep 35759be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 35769be3e283SDebojyoti Ghosh @*/ 35779be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 35789be3e283SDebojyoti Ghosh { 35799be3e283SDebojyoti Ghosh PetscFunctionBegin; 35809be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 35819be3e283SDebojyoti Ghosh ts->poststage = func; 35829be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 35839be3e283SDebojyoti Ghosh } 35849be3e283SDebojyoti Ghosh 3585c688d042SShri Abhyankar /*@C 3586c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 3587c688d042SShri Abhyankar called once at the end of each step evaluation. 3588c688d042SShri Abhyankar 3589c688d042SShri Abhyankar Logically Collective on TS 3590c688d042SShri Abhyankar 3591c688d042SShri Abhyankar Input Parameters: 3592c688d042SShri Abhyankar + ts - The TS context obtained from TSCreate() 3593c688d042SShri Abhyankar - func - The function 3594c688d042SShri Abhyankar 3595c688d042SShri Abhyankar Calling sequence of func: 3596c688d042SShri Abhyankar . PetscErrorCode func(TS ts); 3597c688d042SShri Abhyankar 3598c688d042SShri Abhyankar Level: intermediate 3599c688d042SShri Abhyankar 3600c688d042SShri Abhyankar Note: 36011785ff2aSShri Abhyankar Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling 36021785ff2aSShri Abhyankar thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep() 3603e7e94ed4SShri Abhyankar may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step 3604e7e94ed4SShri Abhyankar solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step 3605e7e94ed4SShri Abhyankar with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime() 3606c688d042SShri Abhyankar 3607c688d042SShri Abhyankar .keywords: TS, timestep 3608c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3609c688d042SShri Abhyankar @*/ 3610c688d042SShri Abhyankar PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS)) 3611c688d042SShri Abhyankar { 3612c688d042SShri Abhyankar PetscFunctionBegin; 3613c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3614c688d042SShri Abhyankar ts->postevaluate = func; 3615c688d042SShri Abhyankar PetscFunctionReturn(0); 3616c688d042SShri Abhyankar } 3617c688d042SShri Abhyankar 3618b8123daeSJed Brown /*@ 3619b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 3620b8123daeSJed Brown 3621b8123daeSJed Brown Collective on TS 3622b8123daeSJed Brown 3623b8123daeSJed Brown Input Parameters: 3624b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 36259be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3626b8123daeSJed Brown 3627b8123daeSJed Brown Notes: 3628b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 3629b8123daeSJed Brown most users would not generally call this routine themselves. 3630b8123daeSJed Brown 3631b8123daeSJed Brown Level: developer 3632b8123daeSJed Brown 3633b8123daeSJed Brown .keywords: TS, timestep 36349be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 3635b8123daeSJed Brown @*/ 3636b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3637b8123daeSJed Brown { 3638b8123daeSJed Brown PetscErrorCode ierr; 3639b8123daeSJed Brown 3640b8123daeSJed Brown PetscFunctionBegin; 3641b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3642ae60f76fSBarry Smith if (ts->prestage) { 3643ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 3644b8123daeSJed Brown } 3645b8123daeSJed Brown PetscFunctionReturn(0); 3646b8123daeSJed Brown } 3647b8123daeSJed Brown 36489be3e283SDebojyoti Ghosh /*@ 36499be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 36509be3e283SDebojyoti Ghosh 36519be3e283SDebojyoti Ghosh Collective on TS 36529be3e283SDebojyoti Ghosh 36539be3e283SDebojyoti Ghosh Input Parameters: 36549be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 36559be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 36569be3e283SDebojyoti Ghosh stageindex - Stage number 36579be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 36589be3e283SDebojyoti Ghosh of stages) with the stage solutions 36599be3e283SDebojyoti Ghosh 36609be3e283SDebojyoti Ghosh Notes: 36619be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 36629be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 36639be3e283SDebojyoti Ghosh 36649be3e283SDebojyoti Ghosh Level: developer 36659be3e283SDebojyoti Ghosh 36669be3e283SDebojyoti Ghosh .keywords: TS, timestep 36679be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 36689be3e283SDebojyoti Ghosh @*/ 36699be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 36709be3e283SDebojyoti Ghosh { 36719be3e283SDebojyoti Ghosh PetscErrorCode ierr; 36729be3e283SDebojyoti Ghosh 36739be3e283SDebojyoti Ghosh PetscFunctionBegin; 36749be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36754beae5d8SLisandro Dalcin if (ts->poststage) { 36769be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 36779be3e283SDebojyoti Ghosh } 36789be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 36799be3e283SDebojyoti Ghosh } 36809be3e283SDebojyoti Ghosh 3681c688d042SShri Abhyankar /*@ 3682c688d042SShri Abhyankar TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate() 3683c688d042SShri Abhyankar 3684c688d042SShri Abhyankar Collective on TS 3685c688d042SShri Abhyankar 3686c688d042SShri Abhyankar Input Parameters: 3687c688d042SShri Abhyankar . ts - The TS context obtained from TSCreate() 3688c688d042SShri Abhyankar 3689c688d042SShri Abhyankar Notes: 3690c688d042SShri Abhyankar TSPostEvaluate() is typically used within time stepping implementations, 3691c688d042SShri Abhyankar most users would not generally call this routine themselves. 3692c688d042SShri Abhyankar 3693c688d042SShri Abhyankar Level: developer 3694c688d042SShri Abhyankar 3695c688d042SShri Abhyankar .keywords: TS, timestep 3696c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep() 3697c688d042SShri Abhyankar @*/ 3698c688d042SShri Abhyankar PetscErrorCode TSPostEvaluate(TS ts) 3699c688d042SShri Abhyankar { 3700c688d042SShri Abhyankar PetscErrorCode ierr; 3701c688d042SShri Abhyankar 3702c688d042SShri Abhyankar PetscFunctionBegin; 3703c688d042SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3704c688d042SShri Abhyankar if (ts->postevaluate) { 3705dcb233daSLisandro Dalcin Vec U; 3706dcb233daSLisandro Dalcin PetscObjectState sprev,spost; 3707dcb233daSLisandro Dalcin 3708dcb233daSLisandro Dalcin ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 3709dcb233daSLisandro Dalcin ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3710c688d042SShri Abhyankar PetscStackCallStandard((*ts->postevaluate),(ts)); 3711dcb233daSLisandro Dalcin ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3712dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 3713c688d042SShri Abhyankar } 3714c688d042SShri Abhyankar PetscFunctionReturn(0); 3715c688d042SShri Abhyankar } 3716c688d042SShri Abhyankar 3717ac226902SBarry Smith /*@C 3718000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 37193f2090d5SJed Brown called once at the end of each time step. 3720000e7ae3SMatthew Knepley 37213f9fe445SBarry Smith Logically Collective on TS 3722000e7ae3SMatthew Knepley 3723000e7ae3SMatthew Knepley Input Parameters: 3724000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3725000e7ae3SMatthew Knepley - func - The function 3726000e7ae3SMatthew Knepley 3727000e7ae3SMatthew Knepley Calling sequence of func: 3728b8123daeSJed Brown $ func (TS ts); 3729000e7ae3SMatthew Knepley 37301785ff2aSShri Abhyankar Notes: 37311785ff2aSShri Abhyankar The function set by TSSetPostStep() is called after each successful step. The solution vector X 37321785ff2aSShri Abhyankar obtained by TSGetSolution() may be different than that computed at the step end if the event handler 37331785ff2aSShri Abhyankar locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead. 37341785ff2aSShri Abhyankar 3735000e7ae3SMatthew Knepley Level: intermediate 3736000e7ae3SMatthew Knepley 3737000e7ae3SMatthew Knepley .keywords: TS, timestep 3738dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep() 3739000e7ae3SMatthew Knepley @*/ 37407087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 3741000e7ae3SMatthew Knepley { 3742000e7ae3SMatthew Knepley PetscFunctionBegin; 37430700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3744ae60f76fSBarry Smith ts->poststep = func; 3745000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3746000e7ae3SMatthew Knepley } 3747000e7ae3SMatthew Knepley 374809ee8438SJed Brown /*@ 37493f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 37503f2090d5SJed Brown 37513f2090d5SJed Brown Collective on TS 37523f2090d5SJed Brown 37533f2090d5SJed Brown Input Parameters: 37543f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 37553f2090d5SJed Brown 37563f2090d5SJed Brown Notes: 37573f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 37583f2090d5SJed Brown so most users would not generally call this routine themselves. 37593f2090d5SJed Brown 37603f2090d5SJed Brown Level: developer 37613f2090d5SJed Brown 37623f2090d5SJed Brown .keywords: TS, timestep 37633f2090d5SJed Brown @*/ 37647087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 37653f2090d5SJed Brown { 37663f2090d5SJed Brown PetscErrorCode ierr; 37673f2090d5SJed Brown 37683f2090d5SJed Brown PetscFunctionBegin; 37690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3770ae60f76fSBarry Smith if (ts->poststep) { 37715efd42a4SStefano Zampini Vec U; 37725efd42a4SStefano Zampini PetscObjectState sprev,spost; 37735efd42a4SStefano Zampini 37745efd42a4SStefano Zampini ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 37755efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3776ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 37775efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3778dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 377972ac3e02SJed Brown } 37803f2090d5SJed Brown PetscFunctionReturn(0); 37813f2090d5SJed Brown } 37823f2090d5SJed Brown 3783d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 3784d763cef2SBarry Smith 3785d763cef2SBarry Smith /*@C 3786a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 3787d763cef2SBarry Smith timestep to display the iteration's progress. 3788d763cef2SBarry Smith 37893f9fe445SBarry Smith Logically Collective on TS 3790d763cef2SBarry Smith 3791d763cef2SBarry Smith Input Parameters: 3792d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3793e213d8f1SJed Brown . monitor - monitoring routine 3794329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 37950298fd71SBarry Smith monitor routine (use NULL if no context is desired) 3796b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 37970298fd71SBarry Smith (may be NULL) 3798d763cef2SBarry Smith 3799e213d8f1SJed Brown Calling sequence of monitor: 3800dcb233daSLisandro Dalcin $ PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 3801d763cef2SBarry Smith 3802d763cef2SBarry Smith + ts - the TS context 380363e21af5SBarry 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) 38041f06c33eSBarry Smith . time - current time 38050910c330SBarry Smith . u - current iterate 3806d763cef2SBarry Smith - mctx - [optional] monitoring context 3807d763cef2SBarry Smith 3808d763cef2SBarry Smith Notes: 3809d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 3810d763cef2SBarry Smith already has been loaded. 3811d763cef2SBarry Smith 3812025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 3813025f1a04SBarry Smith 3814d763cef2SBarry Smith Level: intermediate 3815d763cef2SBarry Smith 3816d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3817d763cef2SBarry Smith 3818a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 3819d763cef2SBarry Smith @*/ 3820c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 3821d763cef2SBarry Smith { 382278064530SBarry Smith PetscErrorCode ierr; 382378064530SBarry Smith PetscInt i; 382478064530SBarry Smith PetscBool identical; 382578064530SBarry Smith 3826d763cef2SBarry Smith PetscFunctionBegin; 38270700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 382878064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 382978064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr); 383078064530SBarry Smith if (identical) PetscFunctionReturn(0); 383178064530SBarry Smith } 383217186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 3833d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 38348704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 3835d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 3836d763cef2SBarry Smith PetscFunctionReturn(0); 3837d763cef2SBarry Smith } 3838d763cef2SBarry Smith 3839d763cef2SBarry Smith /*@C 3840a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 3841d763cef2SBarry Smith 38423f9fe445SBarry Smith Logically Collective on TS 3843d763cef2SBarry Smith 3844d763cef2SBarry Smith Input Parameters: 3845d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3846d763cef2SBarry Smith 3847d763cef2SBarry Smith Notes: 3848d763cef2SBarry Smith There is no way to remove a single, specific monitor. 3849d763cef2SBarry Smith 3850d763cef2SBarry Smith Level: intermediate 3851d763cef2SBarry Smith 3852d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3853d763cef2SBarry Smith 3854a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 3855d763cef2SBarry Smith @*/ 38567087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 3857d763cef2SBarry Smith { 3858d952e501SBarry Smith PetscErrorCode ierr; 3859d952e501SBarry Smith PetscInt i; 3860d952e501SBarry Smith 3861d763cef2SBarry Smith PetscFunctionBegin; 38620700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3863d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 38648704b422SBarry Smith if (ts->monitordestroy[i]) { 38658704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 3866d952e501SBarry Smith } 3867d952e501SBarry Smith } 3868d763cef2SBarry Smith ts->numbermonitors = 0; 3869d763cef2SBarry Smith PetscFunctionReturn(0); 3870d763cef2SBarry Smith } 3871d763cef2SBarry Smith 3872721cd6eeSBarry Smith /*@C 3873721cd6eeSBarry Smith TSMonitorDefault - The Default monitor, prints the timestep and time for each step 38745516499fSSatish Balay 38755516499fSSatish Balay Level: intermediate 387641251cbbSSatish Balay 38775516499fSSatish Balay .keywords: TS, set, monitor 38785516499fSSatish Balay 387963e21af5SBarry Smith .seealso: TSMonitorSet() 388041251cbbSSatish Balay @*/ 3881721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf) 3882d763cef2SBarry Smith { 3883dfbe8321SBarry Smith PetscErrorCode ierr; 3884721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 388541aca3d6SBarry Smith PetscBool iascii,ibinary; 3886d132466eSBarry Smith 3887d763cef2SBarry Smith PetscFunctionBegin; 38884d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 388941aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 389041aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr); 3891721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 389241aca3d6SBarry Smith if (iascii) { 3893649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 389463e21af5SBarry Smith if (step == -1){ /* this indicates it is an interpolated solution */ 389563e21af5SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr); 389663e21af5SBarry Smith } else { 38978392e04aSShri 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); 389863e21af5SBarry Smith } 3899649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 390041aca3d6SBarry Smith } else if (ibinary) { 390141aca3d6SBarry Smith PetscMPIInt rank; 390241aca3d6SBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 390341aca3d6SBarry Smith if (!rank) { 3904450a797fSBarry Smith PetscBool skipHeader; 3905450a797fSBarry Smith PetscInt classid = REAL_FILE_CLASSID; 3906450a797fSBarry Smith 3907450a797fSBarry Smith ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr); 3908450a797fSBarry Smith if (!skipHeader) { 3909450a797fSBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 3910450a797fSBarry Smith } 391141aca3d6SBarry Smith ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr); 391241aca3d6SBarry Smith } else { 391341aca3d6SBarry Smith ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr); 391441aca3d6SBarry Smith } 391541aca3d6SBarry Smith } 3916721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3917d763cef2SBarry Smith PetscFunctionReturn(0); 3918d763cef2SBarry Smith } 3919d763cef2SBarry Smith 39209110b2e7SHong Zhang /*@C 39219110b2e7SHong Zhang TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every 39229110b2e7SHong Zhang timestep to display the iteration's progress. 39239110b2e7SHong Zhang 39249110b2e7SHong Zhang Logically Collective on TS 39259110b2e7SHong Zhang 39269110b2e7SHong Zhang Input Parameters: 39279110b2e7SHong Zhang + ts - the TS context obtained from TSCreate() 39289110b2e7SHong Zhang . adjointmonitor - monitoring routine 39299110b2e7SHong Zhang . adjointmctx - [optional] user-defined context for private data for the 39309110b2e7SHong Zhang monitor routine (use NULL if no context is desired) 39319110b2e7SHong Zhang - adjointmonitordestroy - [optional] routine that frees monitor context 39329110b2e7SHong Zhang (may be NULL) 39339110b2e7SHong Zhang 39349110b2e7SHong Zhang Calling sequence of monitor: 39359110b2e7SHong Zhang $ int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx) 39369110b2e7SHong Zhang 39379110b2e7SHong Zhang + ts - the TS context 39389110b2e7SHong 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 39399110b2e7SHong Zhang been interpolated to) 39409110b2e7SHong Zhang . time - current time 39419110b2e7SHong Zhang . u - current iterate 39429110b2e7SHong Zhang . numcost - number of cost functionos 39439110b2e7SHong Zhang . lambda - sensitivities to initial conditions 39449110b2e7SHong Zhang . mu - sensitivities to parameters 39459110b2e7SHong Zhang - adjointmctx - [optional] adjoint monitoring context 39469110b2e7SHong Zhang 39479110b2e7SHong Zhang Notes: 39489110b2e7SHong Zhang This routine adds an additional monitor to the list of monitors that 39499110b2e7SHong Zhang already has been loaded. 39509110b2e7SHong Zhang 39519110b2e7SHong Zhang Fortran notes: Only a single monitor function can be set for each TS object 39529110b2e7SHong Zhang 39539110b2e7SHong Zhang Level: intermediate 39549110b2e7SHong Zhang 39559110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 39569110b2e7SHong Zhang 39579110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel() 39589110b2e7SHong Zhang @*/ 39599110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**)) 39609110b2e7SHong Zhang { 396178064530SBarry Smith PetscErrorCode ierr; 396278064530SBarry Smith PetscInt i; 396378064530SBarry Smith PetscBool identical; 396478064530SBarry Smith 39659110b2e7SHong Zhang PetscFunctionBegin; 39669110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 396778064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 396878064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);CHKERRQ(ierr); 396978064530SBarry Smith if (identical) PetscFunctionReturn(0); 397078064530SBarry Smith } 39719110b2e7SHong Zhang if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set"); 39729110b2e7SHong Zhang ts->adjointmonitor[ts->numberadjointmonitors] = adjointmonitor; 39739110b2e7SHong Zhang ts->adjointmonitordestroy[ts->numberadjointmonitors] = adjointmdestroy; 39749110b2e7SHong Zhang ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx; 39759110b2e7SHong Zhang PetscFunctionReturn(0); 39769110b2e7SHong Zhang } 39779110b2e7SHong Zhang 39789110b2e7SHong Zhang /*@C 39799110b2e7SHong Zhang TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object. 39809110b2e7SHong Zhang 39819110b2e7SHong Zhang Logically Collective on TS 39829110b2e7SHong Zhang 39839110b2e7SHong Zhang Input Parameters: 39849110b2e7SHong Zhang . ts - the TS context obtained from TSCreate() 39859110b2e7SHong Zhang 39869110b2e7SHong Zhang Notes: 39879110b2e7SHong Zhang There is no way to remove a single, specific monitor. 39889110b2e7SHong Zhang 39899110b2e7SHong Zhang Level: intermediate 39909110b2e7SHong Zhang 39919110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 39929110b2e7SHong Zhang 39939110b2e7SHong Zhang .seealso: TSAdjointMonitorSet() 39949110b2e7SHong Zhang @*/ 39959110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorCancel(TS ts) 39969110b2e7SHong Zhang { 39979110b2e7SHong Zhang PetscErrorCode ierr; 39989110b2e7SHong Zhang PetscInt i; 39999110b2e7SHong Zhang 40009110b2e7SHong Zhang PetscFunctionBegin; 40019110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 40029110b2e7SHong Zhang for (i=0; i<ts->numberadjointmonitors; i++) { 40039110b2e7SHong Zhang if (ts->adjointmonitordestroy[i]) { 40049110b2e7SHong Zhang ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 40059110b2e7SHong Zhang } 40069110b2e7SHong Zhang } 40079110b2e7SHong Zhang ts->numberadjointmonitors = 0; 40089110b2e7SHong Zhang PetscFunctionReturn(0); 40099110b2e7SHong Zhang } 40109110b2e7SHong Zhang 4011721cd6eeSBarry Smith /*@C 4012721cd6eeSBarry Smith TSAdjointMonitorDefault - the default monitor of adjoint computations 4013110eb670SHong Zhang 4014110eb670SHong Zhang Level: intermediate 4015110eb670SHong Zhang 4016110eb670SHong Zhang .keywords: TS, set, monitor 4017110eb670SHong Zhang 4018110eb670SHong Zhang .seealso: TSAdjointMonitorSet() 4019110eb670SHong Zhang @*/ 4020721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf) 4021110eb670SHong Zhang { 4022110eb670SHong Zhang PetscErrorCode ierr; 4023721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 4024110eb670SHong Zhang 4025110eb670SHong Zhang PetscFunctionBegin; 4026110eb670SHong Zhang PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 4027721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 4028110eb670SHong Zhang ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 4029110eb670SHong 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); 4030110eb670SHong Zhang ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 4031721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 4032110eb670SHong Zhang PetscFunctionReturn(0); 4033110eb670SHong Zhang } 4034110eb670SHong Zhang 4035cd652676SJed Brown /*@ 4036cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 4037cd652676SJed Brown 4038cd652676SJed Brown Collective on TS 4039cd652676SJed Brown 4040cd652676SJed Brown Input Argument: 4041cd652676SJed Brown + ts - time stepping context 4042cd652676SJed Brown - t - time to interpolate to 4043cd652676SJed Brown 4044cd652676SJed Brown Output Argument: 40450910c330SBarry Smith . U - state at given time 4046cd652676SJed Brown 4047cd652676SJed Brown Level: intermediate 4048cd652676SJed Brown 4049cd652676SJed Brown Developer Notes: 4050cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 4051cd652676SJed Brown 4052cd652676SJed Brown .keywords: TS, set 4053cd652676SJed Brown 4054874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve() 4055cd652676SJed Brown @*/ 40560910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 4057cd652676SJed Brown { 4058cd652676SJed Brown PetscErrorCode ierr; 4059cd652676SJed Brown 4060cd652676SJed Brown PetscFunctionBegin; 4061cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4062b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 4063be5899b3SLisandro 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); 4064ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 40650910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 4066cd652676SJed Brown PetscFunctionReturn(0); 4067cd652676SJed Brown } 4068cd652676SJed Brown 4069d763cef2SBarry Smith /*@ 40706d9e5789SSean Farley TSStep - Steps one time step 4071d763cef2SBarry Smith 4072d763cef2SBarry Smith Collective on TS 4073d763cef2SBarry Smith 4074d763cef2SBarry Smith Input Parameter: 4075d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 4076d763cef2SBarry Smith 407727829d71SBarry Smith Level: developer 4078d763cef2SBarry Smith 4079b8123daeSJed Brown Notes: 408027829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 408127829d71SBarry Smith 4082b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 4083b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 4084b8123daeSJed Brown 408519eac22cSLisandro Dalcin This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the 408619eac22cSLisandro Dalcin time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 408725cb2221SBarry Smith 4088d763cef2SBarry Smith .keywords: TS, timestep, solve 4089d763cef2SBarry Smith 40909be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 4091d763cef2SBarry Smith @*/ 4092193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 4093d763cef2SBarry Smith { 4094dfbe8321SBarry Smith PetscErrorCode ierr; 4095fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 4096be5899b3SLisandro Dalcin PetscReal ptime; 4097d763cef2SBarry Smith 4098d763cef2SBarry Smith PetscFunctionBegin; 40990700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4100fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 4101fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 4102fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 4103fffbeea8SBarry Smith " type = {Preprint},\n" 4104fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 4105fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 4106302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 4107fffbeea8SBarry Smith 4108d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 410968bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 4110d405a339SMatthew Knepley 4111ef85077eSLisandro Dalcin if (ts->max_time >= PETSC_MAX_REAL && ts->max_steps == PETSC_MAX_INT) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>"); 4112a6772fa2SLisandro 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()"); 4113be5899b3SLisandro 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"); 4114a6772fa2SLisandro Dalcin 4115be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 4116be5899b3SLisandro Dalcin ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev; 41172808aa04SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 4118e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 4119d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 4120193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 4121d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 4122be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 41232808aa04SLisandro Dalcin ts->steps++; 4124be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 412528d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 4126362cd11cSLisandro Dalcin 4127362cd11cSLisandro Dalcin if (ts->reason < 0) { 4128cef5090cSJed Brown if (ts->errorifstepfailed) { 412908c7845fSBarry 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]); 413008c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 4131d2daff3dSHong Zhang } 413208c7845fSBarry Smith } else if (!ts->reason) { 413308c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 413408c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 413508c7845fSBarry Smith } 413608c7845fSBarry Smith PetscFunctionReturn(0); 413708c7845fSBarry Smith } 413808c7845fSBarry Smith 413908c7845fSBarry Smith /*@ 4140b957a604SHong Zhang TSAdjointStep - Steps one time step backward in the adjoint run 414108c7845fSBarry Smith 414208c7845fSBarry Smith Collective on TS 414308c7845fSBarry Smith 414408c7845fSBarry Smith Input Parameter: 414508c7845fSBarry Smith . ts - the TS context obtained from TSCreate() 414608c7845fSBarry Smith 41476a4d4014SLisandro Dalcin Level: intermediate 41486a4d4014SLisandro Dalcin 4149b957a604SHong Zhang .keywords: TS, adjoint, step 41506a4d4014SLisandro Dalcin 4151b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve() 41526a4d4014SLisandro Dalcin @*/ 415308c7845fSBarry Smith PetscErrorCode TSAdjointStep(TS ts) 41546a4d4014SLisandro Dalcin { 415508c7845fSBarry Smith DM dm; 41566a4d4014SLisandro Dalcin PetscErrorCode ierr; 41576a4d4014SLisandro Dalcin 41586a4d4014SLisandro Dalcin PetscFunctionBegin; 41594a2ae208SSatish Balay PetscValidHeaderSpecific(ts,TS_CLASSID,1); 416008c7845fSBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 416108c7845fSBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 4162d763cef2SBarry Smith 4163685405a1SBarry Smith ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr); 4164d763cef2SBarry Smith 4165be5899b3SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 4166be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime; 416742f2b339SBarry 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); 4168be5899b3SLisandro Dalcin ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 416942f2b339SBarry Smith ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr); 41708d0ad7a8SHong Zhang ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 41712808aa04SLisandro Dalcin ts->adjoint_steps++; ts->steps--; 4172362cd11cSLisandro Dalcin 4173362cd11cSLisandro Dalcin if (ts->reason < 0) { 4174cef5090cSJed Brown if (ts->errorifstepfailed) { 41756c4ed002SBarry 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]); 41766c4ed002SBarry 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]); 41776c4ed002SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 4178cef5090cSJed Brown } 4179362cd11cSLisandro Dalcin } else if (!ts->reason) { 41802808aa04SLisandro Dalcin if (ts->adjoint_steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 4181362cd11cSLisandro Dalcin } 4182d763cef2SBarry Smith PetscFunctionReturn(0); 4183d763cef2SBarry Smith } 4184d763cef2SBarry Smith 41857cbde773SLisandro Dalcin /*@ 41867cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 41877cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 41887cbde773SLisandro Dalcin 41897cbde773SLisandro Dalcin Collective on TS 41907cbde773SLisandro Dalcin 41917cbde773SLisandro Dalcin Input Arguments: 41927cbde773SLisandro Dalcin + ts - time stepping context 41937cbde773SLisandro Dalcin . wnormtype - norm type, either NORM_2 or NORM_INFINITY 41947cbde773SLisandro Dalcin - order - optional, desired order for the error evaluation or PETSC_DECIDE 41957cbde773SLisandro Dalcin 41967cbde773SLisandro Dalcin Output Arguments: 41977cbde773SLisandro Dalcin + order - optional, the actual order of the error evaluation 41987cbde773SLisandro Dalcin - wlte - the weighted local truncation error norm 41997cbde773SLisandro Dalcin 42007cbde773SLisandro Dalcin Level: advanced 42017cbde773SLisandro Dalcin 42027cbde773SLisandro Dalcin Notes: 42037cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 42047cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 42057cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 42067cbde773SLisandro Dalcin 42077cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm() 42087cbde773SLisandro Dalcin @*/ 42097cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte) 42107cbde773SLisandro Dalcin { 42117cbde773SLisandro Dalcin PetscErrorCode ierr; 42127cbde773SLisandro Dalcin 42137cbde773SLisandro Dalcin PetscFunctionBegin; 42147cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 42157cbde773SLisandro Dalcin PetscValidType(ts,1); 42167cbde773SLisandro Dalcin PetscValidLogicalCollectiveEnum(ts,wnormtype,4); 42177cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order,3); 42187cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts,*order,3); 42197cbde773SLisandro Dalcin PetscValidRealPointer(wlte,4); 42207cbde773SLisandro Dalcin if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 42217cbde773SLisandro Dalcin if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name); 42227cbde773SLisandro Dalcin ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr); 42237cbde773SLisandro Dalcin PetscFunctionReturn(0); 42247cbde773SLisandro Dalcin } 42257cbde773SLisandro Dalcin 422605175c85SJed Brown /*@ 422705175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 422805175c85SJed Brown 42291c3436cfSJed Brown Collective on TS 423005175c85SJed Brown 423105175c85SJed Brown Input Arguments: 42321c3436cfSJed Brown + ts - time stepping context 42331c3436cfSJed Brown . order - desired order of accuracy 42340298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 423505175c85SJed Brown 423605175c85SJed Brown Output Arguments: 42370910c330SBarry Smith . U - state at the end of the current step 423805175c85SJed Brown 423905175c85SJed Brown Level: advanced 424005175c85SJed Brown 4241108c343cSJed Brown Notes: 4242108c343cSJed Brown This function cannot be called until all stages have been evaluated. 4243108c343cSJed 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. 4244108c343cSJed Brown 42451c3436cfSJed Brown .seealso: TSStep(), TSAdapt 424605175c85SJed Brown @*/ 42470910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 424805175c85SJed Brown { 424905175c85SJed Brown PetscErrorCode ierr; 425005175c85SJed Brown 425105175c85SJed Brown PetscFunctionBegin; 425205175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 425305175c85SJed Brown PetscValidType(ts,1); 42540910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 4255ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 42560910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 425705175c85SJed Brown PetscFunctionReturn(0); 425805175c85SJed Brown } 425905175c85SJed Brown 4260b1cb36f3SHong Zhang /*@ 4261b1cb36f3SHong Zhang TSForwardCostIntegral - Evaluate the cost integral in the forward run. 4262b1cb36f3SHong Zhang 4263b1cb36f3SHong Zhang Collective on TS 4264b1cb36f3SHong Zhang 4265b1cb36f3SHong Zhang Input Arguments: 4266b1cb36f3SHong Zhang . ts - time stepping context 4267b1cb36f3SHong Zhang 4268b1cb36f3SHong Zhang Level: advanced 4269b1cb36f3SHong Zhang 4270b1cb36f3SHong Zhang Notes: 4271b1cb36f3SHong Zhang This function cannot be called until TSStep() has been completed. 4272b1cb36f3SHong Zhang 4273b1cb36f3SHong Zhang .seealso: TSSolve(), TSAdjointCostIntegral() 4274b1cb36f3SHong Zhang @*/ 4275b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts) 4276b1cb36f3SHong Zhang { 4277b1cb36f3SHong Zhang PetscErrorCode ierr; 4278b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4279b1cb36f3SHong 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); 4280b1cb36f3SHong Zhang ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr); 4281b1cb36f3SHong Zhang PetscFunctionReturn(0); 4282b1cb36f3SHong Zhang } 4283d67e68b7SBarry Smith 4284d763cef2SBarry Smith /*@ 4285d763cef2SBarry Smith TSSolve - Steps the requested number of timesteps. 4286d763cef2SBarry Smith 4287d763cef2SBarry Smith Collective on TS 4288d763cef2SBarry Smith 4289d763cef2SBarry Smith Input Parameter: 4290d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 429163e21af5SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used, 429263e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 42935a3a76d0SJed Brown 4294d763cef2SBarry Smith Level: beginner 4295d763cef2SBarry Smith 42965a3a76d0SJed Brown Notes: 42975a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 42985a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 42995a3a76d0SJed Brown stepped over the final time. 43005a3a76d0SJed Brown 4301d763cef2SBarry Smith .keywords: TS, timestep, solve 4302d763cef2SBarry Smith 430363e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime() 4304d763cef2SBarry Smith @*/ 4305cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 4306d763cef2SBarry Smith { 4307b06615a5SLisandro Dalcin Vec solution; 4308d763cef2SBarry Smith PetscErrorCode ierr; 4309d763cef2SBarry Smith 4310d763cef2SBarry Smith PetscFunctionBegin; 4311d763cef2SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4312f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 4313feed9e9dSBarry Smith 4314ee41a567SStefano Zampini if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && u) { /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */ 43150910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 4316b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 4317b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 4318b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 43195a3a76d0SJed Brown } 43200910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 4321715f1b00SHong Zhang if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE"); 4322bbd56ea5SKarl Rupp } else if (u) { 43230910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 43245a3a76d0SJed Brown } 4325b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 432668bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 4327a6772fa2SLisandro Dalcin 4328ef85077eSLisandro Dalcin if (ts->max_time >= PETSC_MAX_REAL && ts->max_steps == PETSC_MAX_INT) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>"); 4329a6772fa2SLisandro 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()"); 4330a6772fa2SLisandro 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"); 4331a6772fa2SLisandro Dalcin 4332715f1b00SHong Zhang if (ts->forward_solve) { 4333715f1b00SHong Zhang ierr = TSForwardSetUp(ts);CHKERRQ(ierr); 4334715f1b00SHong Zhang } 4335715f1b00SHong Zhang 4336e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 4337715f1b00SHong Zhang restarts the step after an event. Resetting these counters in such case causes 4338e7069c78SShri TSTrajectory to incorrectly save the output files 4339e7069c78SShri */ 4340715f1b00SHong Zhang /* reset time step and iteration counters */ 43412808aa04SLisandro Dalcin if (!ts->steps) { 43425ef26d82SJed Brown ts->ksp_its = 0; 43435ef26d82SJed Brown ts->snes_its = 0; 4344c610991cSLisandro Dalcin ts->num_snes_failures = 0; 4345c610991cSLisandro Dalcin ts->reject = 0; 43462808aa04SLisandro Dalcin ts->steprestart = PETSC_TRUE; 43472808aa04SLisandro Dalcin ts->steprollback = PETSC_FALSE; 43482808aa04SLisandro Dalcin } 4349*10bc0e4dSStefano Zampini if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && ts->ptime + ts->time_step > ts->max_time) ts->time_step = ts->max_time - ts->ptime; 4350193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 4351193ac0bcSJed Brown 4352ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 4353f05ece33SBarry Smith 4354193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 4355193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 4356a6772fa2SLisandro Dalcin if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 4357cc708dedSBarry Smith ts->solvetime = ts->ptime; 4358a6772fa2SLisandro Dalcin solution = ts->vec_sol; 4359be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 4360db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 4361db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 4362e7069c78SShri 43632808aa04SLisandro Dalcin if (!ts->steps) { 43642808aa04SLisandro Dalcin ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 43656427ac75SLisandro Dalcin ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 43662808aa04SLisandro Dalcin } 43676427ac75SLisandro Dalcin 4368e1a7a14fSJed Brown while (!ts->reason) { 43692808aa04SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 43709687d888SLisandro Dalcin if (!ts->steprollback) { 43719687d888SLisandro Dalcin ierr = TSPreStep(ts);CHKERRQ(ierr); 43729687d888SLisandro Dalcin } 4373193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 4374e783b05fSHong 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. */ 4375b1cb36f3SHong Zhang ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr); 4376b1cb36f3SHong Zhang } 437758818c2dSLisandro Dalcin if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */ 4378715f1b00SHong Zhang ierr = TSForwardStep(ts);CHKERRQ(ierr); 4379715f1b00SHong Zhang } 438010b82f12SShri Abhyankar ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 4381e783b05fSHong 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. */ 438258818c2dSLisandro Dalcin if (ts->steprollback) { 438358818c2dSLisandro Dalcin ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 438458818c2dSLisandro Dalcin } 4385e783b05fSHong Zhang if (!ts->steprollback) { 43862808aa04SLisandro Dalcin ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 43871eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 4388aeb4809dSShri Abhyankar } 4389193ac0bcSJed Brown } 43902808aa04SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 43916427ac75SLisandro Dalcin 439249354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 43930910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 4394cc708dedSBarry Smith ts->solvetime = ts->max_time; 4395b06615a5SLisandro Dalcin solution = u; 439663e21af5SBarry Smith ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr); 43970574a7fbSJed Brown } else { 4398ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 4399cc708dedSBarry Smith ts->solvetime = ts->ptime; 4400b06615a5SLisandro Dalcin solution = ts->vec_sol; 44010574a7fbSJed Brown } 4402193ac0bcSJed Brown } 4403d2daff3dSHong Zhang 4404ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 440563e21af5SBarry Smith ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr); 440656f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 4407ef222394SBarry Smith if (ts->adjoint_solve) { 4408ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 44092b0a91c0SBarry Smith } 4410d763cef2SBarry Smith PetscFunctionReturn(0); 4411d763cef2SBarry Smith } 4412d763cef2SBarry Smith 4413b1cb36f3SHong Zhang /*@ 4414b1cb36f3SHong Zhang TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run. 4415b1cb36f3SHong Zhang 4416b1cb36f3SHong Zhang Collective on TS 4417b1cb36f3SHong Zhang 4418b1cb36f3SHong Zhang Input Arguments: 4419b1cb36f3SHong Zhang . ts - time stepping context 4420b1cb36f3SHong Zhang 4421b1cb36f3SHong Zhang Level: advanced 4422b1cb36f3SHong Zhang 4423b1cb36f3SHong Zhang Notes: 4424b1cb36f3SHong Zhang This function cannot be called until TSAdjointStep() has been completed. 4425b1cb36f3SHong Zhang 4426b1cb36f3SHong Zhang .seealso: TSAdjointSolve(), TSAdjointStep 4427b1cb36f3SHong Zhang @*/ 4428b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts) 4429b1cb36f3SHong Zhang { 4430b1cb36f3SHong Zhang PetscErrorCode ierr; 4431b1cb36f3SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4432b1cb36f3SHong 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); 4433b1cb36f3SHong Zhang ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr); 4434b1cb36f3SHong Zhang PetscFunctionReturn(0); 4435b1cb36f3SHong Zhang } 4436b1cb36f3SHong Zhang 4437c88f2d44SBarry Smith /*@ 4438c88f2d44SBarry Smith TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 4439c88f2d44SBarry Smith 4440c88f2d44SBarry Smith Collective on TS 4441c88f2d44SBarry Smith 4442c88f2d44SBarry Smith Input Parameter: 44432c39e106SBarry Smith . ts - the TS context obtained from TSCreate() 4444c88f2d44SBarry Smith 4445e87fd094SBarry Smith Options Database: 4446715f1b00SHong Zhang . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial values 4447e87fd094SBarry Smith 4448c88f2d44SBarry Smith Level: intermediate 4449c88f2d44SBarry Smith 4450c88f2d44SBarry Smith Notes: 4451c88f2d44SBarry Smith This must be called after a call to TSSolve() that solves the forward problem 4452c88f2d44SBarry Smith 445373b18844SBarry Smith By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time 445473b18844SBarry Smith 4455c88f2d44SBarry Smith .keywords: TS, timestep, solve 4456c88f2d44SBarry Smith 4457b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep() 4458c88f2d44SBarry Smith @*/ 44592c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts) 4460c88f2d44SBarry Smith { 4461c88f2d44SBarry Smith PetscErrorCode ierr; 4462c88f2d44SBarry Smith 4463c88f2d44SBarry Smith PetscFunctionBegin; 4464c88f2d44SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4465c88f2d44SBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 446668bece0bSHong Zhang 4467c88f2d44SBarry Smith /* reset time step and iteration counters */ 44682808aa04SLisandro Dalcin ts->adjoint_steps = 0; 4469c88f2d44SBarry Smith ts->ksp_its = 0; 4470c88f2d44SBarry Smith ts->snes_its = 0; 4471c88f2d44SBarry Smith ts->num_snes_failures = 0; 4472c88f2d44SBarry Smith ts->reject = 0; 4473c88f2d44SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 4474c88f2d44SBarry Smith 44752808aa04SLisandro Dalcin if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->steps; 44762808aa04SLisandro Dalcin if (ts->adjoint_steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 4477c88f2d44SBarry Smith 4478c88f2d44SBarry Smith while (!ts->reason) { 44792808aa04SLisandro Dalcin ierr = TSTrajectoryGet(ts->trajectory,ts,ts->steps,&ts->ptime);CHKERRQ(ierr); 44802808aa04SLisandro Dalcin ierr = TSAdjointMonitor(ts,ts->steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 44816427ac75SLisandro Dalcin ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr); 4482616946c1SHong Zhang ierr = TSAdjointStep(ts);CHKERRQ(ierr); 4483b1cb36f3SHong Zhang if (ts->vec_costintegral && !ts->costintegralfwd) { 4484b1cb36f3SHong Zhang ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr); 4485b1cb36f3SHong Zhang } 4486c88f2d44SBarry Smith } 44872808aa04SLisandro Dalcin ierr = TSTrajectoryGet(ts->trajectory,ts,ts->steps,&ts->ptime);CHKERRQ(ierr); 44882808aa04SLisandro Dalcin ierr = TSAdjointMonitor(ts,ts->steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 4489c88f2d44SBarry Smith ts->solvetime = ts->ptime; 4490e210cd0eSHong Zhang ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr); 4491685405a1SBarry Smith ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr); 4492c88f2d44SBarry Smith PetscFunctionReturn(0); 4493c88f2d44SBarry Smith } 4494c88f2d44SBarry Smith 44957db568b7SBarry Smith /*@C 4496228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 4497228d79bcSJed Brown 4498228d79bcSJed Brown Collective on TS 4499228d79bcSJed Brown 4500228d79bcSJed Brown Input Parameters: 4501228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 4502228d79bcSJed Brown . step - step number that has just completed 4503228d79bcSJed Brown . ptime - model time of the state 45040910c330SBarry Smith - u - state at the current model time 4505228d79bcSJed Brown 4506228d79bcSJed Brown Notes: 45077db568b7SBarry Smith TSMonitor() is typically used automatically within the time stepping implementations. 45087db568b7SBarry Smith Users would almost never call this routine directly. 4509228d79bcSJed Brown 451063e21af5SBarry Smith A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions 451163e21af5SBarry Smith 45127db568b7SBarry Smith Level: developer 4513228d79bcSJed Brown 4514228d79bcSJed Brown .keywords: TS, timestep 4515228d79bcSJed Brown @*/ 45160910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 4517d763cef2SBarry Smith { 4518d6152f81SLisandro Dalcin DM dm; 4519a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 4520d6152f81SLisandro Dalcin PetscErrorCode ierr; 4521d763cef2SBarry Smith 4522d763cef2SBarry Smith PetscFunctionBegin; 4523b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4524b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4525d6152f81SLisandro Dalcin 4526d6152f81SLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 4527d6152f81SLisandro Dalcin ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr); 4528d6152f81SLisandro Dalcin 45295edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 4530d763cef2SBarry Smith for (i=0; i<n; i++) { 45310910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 4532d763cef2SBarry Smith } 45335edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 4534d763cef2SBarry Smith PetscFunctionReturn(0); 4535d763cef2SBarry Smith } 4536d763cef2SBarry Smith 45377db568b7SBarry Smith /*@C 45389110b2e7SHong Zhang TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet() 45399110b2e7SHong Zhang 45409110b2e7SHong Zhang Collective on TS 45419110b2e7SHong Zhang 45429110b2e7SHong Zhang Input Parameters: 45439110b2e7SHong Zhang + ts - time stepping context obtained from TSCreate() 45449110b2e7SHong Zhang . step - step number that has just completed 45459110b2e7SHong Zhang . ptime - model time of the state 45469110b2e7SHong Zhang . u - state at the current model time 45479110b2e7SHong Zhang . numcost - number of cost functions (dimension of lambda or mu) 45489110b2e7SHong Zhang . lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 45499110b2e7SHong Zhang - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 45509110b2e7SHong Zhang 45519110b2e7SHong Zhang Notes: 45527db568b7SBarry Smith TSAdjointMonitor() is typically used automatically within the time stepping implementations. 45537db568b7SBarry Smith Users would almost never call this routine directly. 45549110b2e7SHong Zhang 45557db568b7SBarry Smith Level: developer 45569110b2e7SHong Zhang 45579110b2e7SHong Zhang .keywords: TS, timestep 45589110b2e7SHong Zhang @*/ 45599110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu) 45609110b2e7SHong Zhang { 45619110b2e7SHong Zhang PetscErrorCode ierr; 45629110b2e7SHong Zhang PetscInt i,n = ts->numberadjointmonitors; 45639110b2e7SHong Zhang 45649110b2e7SHong Zhang PetscFunctionBegin; 45659110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45669110b2e7SHong Zhang PetscValidHeaderSpecific(u,VEC_CLASSID,4); 45679110b2e7SHong Zhang ierr = VecLockPush(u);CHKERRQ(ierr); 45689110b2e7SHong Zhang for (i=0; i<n; i++) { 45699110b2e7SHong Zhang ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 45709110b2e7SHong Zhang } 45719110b2e7SHong Zhang ierr = VecLockPop(u);CHKERRQ(ierr); 45729110b2e7SHong Zhang PetscFunctionReturn(0); 45739110b2e7SHong Zhang } 45749110b2e7SHong Zhang 4575d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 4576d763cef2SBarry Smith /*@C 45777db568b7SBarry Smith TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 4578a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 4579d763cef2SBarry Smith 4580d763cef2SBarry Smith Collective on TS 4581d763cef2SBarry Smith 4582d763cef2SBarry Smith Input Parameters: 4583d763cef2SBarry Smith + host - the X display to open, or null for the local machine 4584d763cef2SBarry Smith . label - the title to put in the title bar 45857c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 4586a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 4587a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 4588d763cef2SBarry Smith 4589d763cef2SBarry Smith Output Parameter: 45900b039ecaSBarry Smith . ctx - the context 4591d763cef2SBarry Smith 4592d763cef2SBarry Smith Options Database Key: 45934f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 45948b668821SLisandro Dalcin + -ts_monitor_lg_timestep_log - automatically sets line graph monitor 45957db568b7SBarry Smith . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 45967db568b7SBarry Smith . -ts_monitor_lg_error - monitor the error 45977db568b7SBarry Smith . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 45987db568b7SBarry Smith . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 4599b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 4600d763cef2SBarry Smith 4601d763cef2SBarry Smith Notes: 4602a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 4603d763cef2SBarry Smith 46047db568b7SBarry Smith One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 46057db568b7SBarry Smith 46067db568b7SBarry 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 46077db568b7SBarry 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 46087db568b7SBarry Smith as the first argument. 46097db568b7SBarry Smith 46107db568b7SBarry Smith One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 46117db568b7SBarry Smith 4612d763cef2SBarry Smith Level: intermediate 4613d763cef2SBarry Smith 46147db568b7SBarry Smith .keywords: TS, monitor, line graph, residual 4615d763cef2SBarry Smith 46167db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 46177db568b7SBarry Smith TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 46187db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 46197db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 46207db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 46217c922b88SBarry Smith 4622d763cef2SBarry Smith @*/ 4623a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 4624d763cef2SBarry Smith { 46257f52daa2SLisandro Dalcin PetscDraw draw; 4626dfbe8321SBarry Smith PetscErrorCode ierr; 4627d763cef2SBarry Smith 4628d763cef2SBarry Smith PetscFunctionBegin; 4629b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 46307f52daa2SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 46317f52daa2SLisandro Dalcin ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 46327f52daa2SLisandro Dalcin ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 4633287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 46347f52daa2SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 4635a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 4636d763cef2SBarry Smith PetscFunctionReturn(0); 4637d763cef2SBarry Smith } 4638d763cef2SBarry Smith 4639b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 4640d763cef2SBarry Smith { 46410b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4642c32365f1SBarry Smith PetscReal x = ptime,y; 4643dfbe8321SBarry Smith PetscErrorCode ierr; 4644d763cef2SBarry Smith 4645d763cef2SBarry Smith PetscFunctionBegin; 464663e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */ 4647b06615a5SLisandro Dalcin if (!step) { 4648a9f9c1f6SBarry Smith PetscDrawAxis axis; 46498b668821SLisandro Dalcin const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step"; 4650a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 46518b668821SLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr); 4652a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4653a9f9c1f6SBarry Smith } 4654c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 46558b668821SLisandro Dalcin if (ctx->semilogy) y = PetscLog10Real(y); 46560b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4657b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 46580b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 46596934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 46603923b477SBarry Smith } 4661d763cef2SBarry Smith PetscFunctionReturn(0); 4662d763cef2SBarry Smith } 4663d763cef2SBarry Smith 4664d763cef2SBarry Smith /*@C 4665a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 4666a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 4667d763cef2SBarry Smith 46680b039ecaSBarry Smith Collective on TSMonitorLGCtx 4669d763cef2SBarry Smith 4670d763cef2SBarry Smith Input Parameter: 46710b039ecaSBarry Smith . ctx - the monitor context 4672d763cef2SBarry Smith 4673d763cef2SBarry Smith Level: intermediate 4674d763cef2SBarry Smith 4675d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 4676d763cef2SBarry Smith 46774f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 4678d763cef2SBarry Smith @*/ 4679a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 4680d763cef2SBarry Smith { 4681dfbe8321SBarry Smith PetscErrorCode ierr; 4682d763cef2SBarry Smith 4683d763cef2SBarry Smith PetscFunctionBegin; 46847684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 46857684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 46867684fa3eSBarry Smith } 46870b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 468831152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 4689387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 4690387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 4691387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 46920b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 4693d763cef2SBarry Smith PetscFunctionReturn(0); 4694d763cef2SBarry Smith } 4695d763cef2SBarry Smith 4696d763cef2SBarry Smith /*@ 4697b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4698d763cef2SBarry Smith 4699d763cef2SBarry Smith Not Collective 4700d763cef2SBarry Smith 4701d763cef2SBarry Smith Input Parameter: 4702d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 4703d763cef2SBarry Smith 4704d763cef2SBarry Smith Output Parameter: 470519eac22cSLisandro Dalcin . t - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime(). 4706d763cef2SBarry Smith 4707d763cef2SBarry Smith Level: beginner 4708d763cef2SBarry Smith 4709b8123daeSJed Brown Note: 4710b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 47119be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 4712b8123daeSJed Brown 4713aaa6c58dSLisandro Dalcin .seealso: TSGetSolveTime(), TSSetTime(), TSGetTimeStep() 4714d763cef2SBarry Smith 4715d763cef2SBarry Smith .keywords: TS, get, time 4716d763cef2SBarry Smith @*/ 47177087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 4718d763cef2SBarry Smith { 4719d763cef2SBarry Smith PetscFunctionBegin; 47200700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4721f7cf8827SBarry Smith PetscValidRealPointer(t,2); 4722d763cef2SBarry Smith *t = ts->ptime; 4723d763cef2SBarry Smith PetscFunctionReturn(0); 4724d763cef2SBarry Smith } 4725d763cef2SBarry Smith 4726e5e524a1SHong Zhang /*@ 4727e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4728e5e524a1SHong Zhang 4729e5e524a1SHong Zhang Not Collective 4730e5e524a1SHong Zhang 4731e5e524a1SHong Zhang Input Parameter: 4732e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 4733e5e524a1SHong Zhang 4734e5e524a1SHong Zhang Output Parameter: 4735e5e524a1SHong Zhang . t - the previous time 4736e5e524a1SHong Zhang 4737e5e524a1SHong Zhang Level: beginner 4738e5e524a1SHong Zhang 4739aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep() 4740e5e524a1SHong Zhang 4741e5e524a1SHong Zhang .keywords: TS, get, time 4742e5e524a1SHong Zhang @*/ 4743e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 4744e5e524a1SHong Zhang { 4745e5e524a1SHong Zhang PetscFunctionBegin; 4746e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4747e5e524a1SHong Zhang PetscValidRealPointer(t,2); 4748e5e524a1SHong Zhang *t = ts->ptime_prev; 4749e5e524a1SHong Zhang PetscFunctionReturn(0); 4750e5e524a1SHong Zhang } 4751e5e524a1SHong Zhang 47526a4d4014SLisandro Dalcin /*@ 47536a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 47546a4d4014SLisandro Dalcin 47553f9fe445SBarry Smith Logically Collective on TS 47566a4d4014SLisandro Dalcin 47576a4d4014SLisandro Dalcin Input Parameters: 47586a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 47596a4d4014SLisandro Dalcin - time - the time 47606a4d4014SLisandro Dalcin 47616a4d4014SLisandro Dalcin Level: intermediate 47626a4d4014SLisandro Dalcin 476319eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps() 47646a4d4014SLisandro Dalcin 47656a4d4014SLisandro Dalcin .keywords: TS, set, time 47666a4d4014SLisandro Dalcin @*/ 47677087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 47686a4d4014SLisandro Dalcin { 47696a4d4014SLisandro Dalcin PetscFunctionBegin; 47700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4771c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 47726a4d4014SLisandro Dalcin ts->ptime = t; 47736a4d4014SLisandro Dalcin PetscFunctionReturn(0); 47746a4d4014SLisandro Dalcin } 47756a4d4014SLisandro Dalcin 4776d763cef2SBarry Smith /*@C 4777d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4778d763cef2SBarry Smith TS options in the database. 4779d763cef2SBarry Smith 47803f9fe445SBarry Smith Logically Collective on TS 4781d763cef2SBarry Smith 4782d763cef2SBarry Smith Input Parameter: 4783d763cef2SBarry Smith + ts - The TS context 4784d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4785d763cef2SBarry Smith 4786d763cef2SBarry Smith Notes: 4787d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4788d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4789d763cef2SBarry Smith hyphen. 4790d763cef2SBarry Smith 4791d763cef2SBarry Smith Level: advanced 4792d763cef2SBarry Smith 4793d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 4794d763cef2SBarry Smith 4795d763cef2SBarry Smith .seealso: TSSetFromOptions() 4796d763cef2SBarry Smith 4797d763cef2SBarry Smith @*/ 47987087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 4799d763cef2SBarry Smith { 4800dfbe8321SBarry Smith PetscErrorCode ierr; 4801089b2837SJed Brown SNES snes; 4802d763cef2SBarry Smith 4803d763cef2SBarry Smith PetscFunctionBegin; 48040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4805d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4806089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4807089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4808d763cef2SBarry Smith PetscFunctionReturn(0); 4809d763cef2SBarry Smith } 4810d763cef2SBarry Smith 4811d763cef2SBarry Smith /*@C 4812d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4813d763cef2SBarry Smith TS options in the database. 4814d763cef2SBarry Smith 48153f9fe445SBarry Smith Logically Collective on TS 4816d763cef2SBarry Smith 4817d763cef2SBarry Smith Input Parameter: 4818d763cef2SBarry Smith + ts - The TS context 4819d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4820d763cef2SBarry Smith 4821d763cef2SBarry Smith Notes: 4822d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4823d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4824d763cef2SBarry Smith hyphen. 4825d763cef2SBarry Smith 4826d763cef2SBarry Smith Level: advanced 4827d763cef2SBarry Smith 4828d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 4829d763cef2SBarry Smith 4830d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 4831d763cef2SBarry Smith 4832d763cef2SBarry Smith @*/ 48337087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4834d763cef2SBarry Smith { 4835dfbe8321SBarry Smith PetscErrorCode ierr; 4836089b2837SJed Brown SNES snes; 4837d763cef2SBarry Smith 4838d763cef2SBarry Smith PetscFunctionBegin; 48390700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4840d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4841089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4842089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4843d763cef2SBarry Smith PetscFunctionReturn(0); 4844d763cef2SBarry Smith } 4845d763cef2SBarry Smith 4846d763cef2SBarry Smith /*@C 4847d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4848d763cef2SBarry Smith TS options in the database. 4849d763cef2SBarry Smith 4850d763cef2SBarry Smith Not Collective 4851d763cef2SBarry Smith 4852d763cef2SBarry Smith Input Parameter: 4853d763cef2SBarry Smith . ts - The TS context 4854d763cef2SBarry Smith 4855d763cef2SBarry Smith Output Parameter: 4856d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4857d763cef2SBarry Smith 4858d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 4859d763cef2SBarry Smith sufficient length to hold the prefix. 4860d763cef2SBarry Smith 4861d763cef2SBarry Smith Level: intermediate 4862d763cef2SBarry Smith 4863d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 4864d763cef2SBarry Smith 4865d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 4866d763cef2SBarry Smith @*/ 48677087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4868d763cef2SBarry Smith { 4869dfbe8321SBarry Smith PetscErrorCode ierr; 4870d763cef2SBarry Smith 4871d763cef2SBarry Smith PetscFunctionBegin; 48720700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 48734482741eSBarry Smith PetscValidPointer(prefix,2); 4874d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4875d763cef2SBarry Smith PetscFunctionReturn(0); 4876d763cef2SBarry Smith } 4877d763cef2SBarry Smith 4878d763cef2SBarry Smith /*@C 4879d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4880d763cef2SBarry Smith 4881d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4882d763cef2SBarry Smith 4883d763cef2SBarry Smith Input Parameter: 4884d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4885d763cef2SBarry Smith 4886d763cef2SBarry Smith Output Parameters: 4887e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4888e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4889e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4890e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4891d763cef2SBarry Smith 48920298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 4893d763cef2SBarry Smith 4894d763cef2SBarry Smith Level: intermediate 4895d763cef2SBarry Smith 489680275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 4897d763cef2SBarry Smith 4898d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 4899d763cef2SBarry Smith @*/ 4900e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4901d763cef2SBarry Smith { 4902089b2837SJed Brown PetscErrorCode ierr; 490324989b8cSPeter Brune DM dm; 4904089b2837SJed Brown 4905d763cef2SBarry Smith PetscFunctionBegin; 490623a57915SBarry Smith if (Amat || Pmat) { 490723a57915SBarry Smith SNES snes; 4908089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 490923a57915SBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4910e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 491123a57915SBarry Smith } 491224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 491324989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 4914d763cef2SBarry Smith PetscFunctionReturn(0); 4915d763cef2SBarry Smith } 4916d763cef2SBarry Smith 49172eca1d9cSJed Brown /*@C 49182eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 49192eca1d9cSJed Brown 49202eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 49212eca1d9cSJed Brown 49222eca1d9cSJed Brown Input Parameter: 49232eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 49242eca1d9cSJed Brown 49252eca1d9cSJed Brown Output Parameters: 4926e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4927e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 49282eca1d9cSJed Brown . f - The function to compute the matrices 49292eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 49302eca1d9cSJed Brown 49310298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 49322eca1d9cSJed Brown 49332eca1d9cSJed Brown Level: advanced 49342eca1d9cSJed Brown 493580275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 49362eca1d9cSJed Brown 49372eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 49382eca1d9cSJed Brown @*/ 4939e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 49402eca1d9cSJed Brown { 4941089b2837SJed Brown PetscErrorCode ierr; 494224989b8cSPeter Brune DM dm; 49430910c330SBarry Smith 49442eca1d9cSJed Brown PetscFunctionBegin; 4945c0aab802Sstefano_zampini if (Amat || Pmat) { 4946c0aab802Sstefano_zampini SNES snes; 4947089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4948f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4949e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 4950c0aab802Sstefano_zampini } 495124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 495224989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 49532eca1d9cSJed Brown PetscFunctionReturn(0); 49542eca1d9cSJed Brown } 49552eca1d9cSJed Brown 49561713a123SBarry Smith /*@C 495783a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 49581713a123SBarry Smith VecView() for the solution at each timestep 49591713a123SBarry Smith 49601713a123SBarry Smith Collective on TS 49611713a123SBarry Smith 49621713a123SBarry Smith Input Parameters: 49631713a123SBarry Smith + ts - the TS context 49641713a123SBarry Smith . step - current time-step 4965142b95e3SSatish Balay . ptime - current time 49660298fd71SBarry Smith - dummy - either a viewer or NULL 49671713a123SBarry Smith 496899fdda47SBarry Smith Options Database: 496999fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 497099fdda47SBarry Smith 4971387f4636SBarry 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 497299fdda47SBarry Smith will look bad 497399fdda47SBarry Smith 49741713a123SBarry Smith Level: intermediate 49751713a123SBarry Smith 49761713a123SBarry Smith .keywords: TS, vector, monitor, view 49771713a123SBarry Smith 4978a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 49791713a123SBarry Smith @*/ 49800910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 49811713a123SBarry Smith { 4982dfbe8321SBarry Smith PetscErrorCode ierr; 498383a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4984473a3ab2SBarry Smith PetscDraw draw; 49851713a123SBarry Smith 49861713a123SBarry Smith PetscFunctionBegin; 49876083293cSBarry Smith if (!step && ictx->showinitial) { 49886083293cSBarry Smith if (!ictx->initialsolution) { 49890910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 49901713a123SBarry Smith } 49910910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 49926083293cSBarry Smith } 4993b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 49940dcf80beSBarry Smith 49956083293cSBarry Smith if (ictx->showinitial) { 49966083293cSBarry Smith PetscReal pause; 49976083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 49986083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 49996083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 50006083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 50016083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 50026083293cSBarry Smith } 50030910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 5004473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 500551fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 5006473a3ab2SBarry Smith char time[32]; 5007473a3ab2SBarry Smith 5008473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 500985b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 5010473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 5011473a3ab2SBarry Smith h = yl + .95*(yr - yl); 501251fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 5013473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 5014473a3ab2SBarry Smith } 5015473a3ab2SBarry Smith 50166083293cSBarry Smith if (ictx->showinitial) { 50176083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 50186083293cSBarry Smith } 50191713a123SBarry Smith PetscFunctionReturn(0); 50201713a123SBarry Smith } 50211713a123SBarry Smith 50229110b2e7SHong Zhang /*@C 50239110b2e7SHong Zhang TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling 50249110b2e7SHong Zhang VecView() for the sensitivities to initial states at each timestep 50259110b2e7SHong Zhang 50269110b2e7SHong Zhang Collective on TS 50279110b2e7SHong Zhang 50289110b2e7SHong Zhang Input Parameters: 50299110b2e7SHong Zhang + ts - the TS context 50309110b2e7SHong Zhang . step - current time-step 50319110b2e7SHong Zhang . ptime - current time 50329110b2e7SHong Zhang . u - current state 50339110b2e7SHong Zhang . numcost - number of cost functions 50349110b2e7SHong Zhang . lambda - sensitivities to initial conditions 50359110b2e7SHong Zhang . mu - sensitivities to parameters 50369110b2e7SHong Zhang - dummy - either a viewer or NULL 50379110b2e7SHong Zhang 50389110b2e7SHong Zhang Level: intermediate 50399110b2e7SHong Zhang 50409110b2e7SHong Zhang .keywords: TS, vector, adjoint, monitor, view 50419110b2e7SHong Zhang 50429110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView() 50439110b2e7SHong Zhang @*/ 50449110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 50459110b2e7SHong Zhang { 50469110b2e7SHong Zhang PetscErrorCode ierr; 50479110b2e7SHong Zhang TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 50489110b2e7SHong Zhang PetscDraw draw; 5049a0046e2cSSatish Balay PetscReal xl,yl,xr,yr,h; 5050a0046e2cSSatish Balay char time[32]; 50519110b2e7SHong Zhang 50529110b2e7SHong Zhang PetscFunctionBegin; 50539110b2e7SHong Zhang if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 50549110b2e7SHong Zhang 50559110b2e7SHong Zhang ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr); 50569110b2e7SHong Zhang ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 50579110b2e7SHong Zhang ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 50589110b2e7SHong Zhang ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 50599110b2e7SHong Zhang h = yl + .95*(yr - yl); 50609110b2e7SHong Zhang ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 50619110b2e7SHong Zhang ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 50629110b2e7SHong Zhang PetscFunctionReturn(0); 50639110b2e7SHong Zhang } 50649110b2e7SHong Zhang 50652d5ee99bSBarry Smith /*@C 50662d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 50672d5ee99bSBarry Smith 50682d5ee99bSBarry Smith Collective on TS 50692d5ee99bSBarry Smith 50702d5ee99bSBarry Smith Input Parameters: 50712d5ee99bSBarry Smith + ts - the TS context 50722d5ee99bSBarry Smith . step - current time-step 50732d5ee99bSBarry Smith . ptime - current time 50742d5ee99bSBarry Smith - dummy - either a viewer or NULL 50752d5ee99bSBarry Smith 50762d5ee99bSBarry Smith Level: intermediate 50772d5ee99bSBarry Smith 50782d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 50792d5ee99bSBarry Smith 50802d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 50812d5ee99bSBarry Smith @*/ 50822d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 50832d5ee99bSBarry Smith { 50842d5ee99bSBarry Smith PetscErrorCode ierr; 50852d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 50862d5ee99bSBarry Smith PetscDraw draw; 50876934998bSLisandro Dalcin PetscDrawAxis axis; 50882d5ee99bSBarry Smith PetscInt n; 50892d5ee99bSBarry Smith PetscMPIInt size; 50906934998bSLisandro Dalcin PetscReal U0,U1,xl,yl,xr,yr,h; 50912d5ee99bSBarry Smith char time[32]; 50922d5ee99bSBarry Smith const PetscScalar *U; 50932d5ee99bSBarry Smith 50942d5ee99bSBarry Smith PetscFunctionBegin; 50956934998bSLisandro Dalcin ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr); 50966934998bSLisandro Dalcin if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs"); 50972d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 50986934998bSLisandro Dalcin if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 50992d5ee99bSBarry Smith 51002d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 51016934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr); 51026934998bSLisandro Dalcin ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 51036934998bSLisandro Dalcin if (!step) { 51046934998bSLisandro Dalcin ierr = PetscDrawClear(draw);CHKERRQ(ierr); 51056934998bSLisandro Dalcin ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); 51066934998bSLisandro Dalcin } 51072d5ee99bSBarry Smith 51082d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 51096934998bSLisandro Dalcin U0 = PetscRealPart(U[0]); 51106934998bSLisandro Dalcin U1 = PetscRealPart(U[1]); 5111a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 51126934998bSLisandro Dalcin if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0); 51132d5ee99bSBarry Smith 51146934998bSLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 51156934998bSLisandro Dalcin ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr); 51162d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 51174b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 511885b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 51192d5ee99bSBarry Smith h = yl + .95*(yr - yl); 512051fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 51212d5ee99bSBarry Smith } 51226934998bSLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 51232d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 512456d62c8fSLisandro Dalcin ierr = PetscDrawPause(draw);CHKERRQ(ierr); 51256934998bSLisandro Dalcin ierr = PetscDrawSave(draw);CHKERRQ(ierr); 51262d5ee99bSBarry Smith PetscFunctionReturn(0); 51272d5ee99bSBarry Smith } 51282d5ee99bSBarry Smith 51296083293cSBarry Smith /*@C 513083a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 51316083293cSBarry Smith 51326083293cSBarry Smith Collective on TS 51336083293cSBarry Smith 51346083293cSBarry Smith Input Parameters: 51356083293cSBarry Smith . ctx - the monitor context 51366083293cSBarry Smith 51376083293cSBarry Smith Level: intermediate 51386083293cSBarry Smith 51396083293cSBarry Smith .keywords: TS, vector, monitor, view 51406083293cSBarry Smith 514183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 51426083293cSBarry Smith @*/ 514383a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 51446083293cSBarry Smith { 51456083293cSBarry Smith PetscErrorCode ierr; 51466083293cSBarry Smith 51476083293cSBarry Smith PetscFunctionBegin; 514883a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 514983a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 515083a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 51516083293cSBarry Smith PetscFunctionReturn(0); 51526083293cSBarry Smith } 51536083293cSBarry Smith 51546083293cSBarry Smith /*@C 515583a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 51566083293cSBarry Smith 51576083293cSBarry Smith Collective on TS 51586083293cSBarry Smith 51596083293cSBarry Smith Input Parameter: 51606083293cSBarry Smith . ts - time-step context 51616083293cSBarry Smith 51626083293cSBarry Smith Output Patameter: 51636083293cSBarry Smith . ctx - the monitor context 51646083293cSBarry Smith 516599fdda47SBarry Smith Options Database: 516699fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 516799fdda47SBarry Smith 51686083293cSBarry Smith Level: intermediate 51696083293cSBarry Smith 51706083293cSBarry Smith .keywords: TS, vector, monitor, view 51716083293cSBarry Smith 517283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 51736083293cSBarry Smith @*/ 517483a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 51756083293cSBarry Smith { 51766083293cSBarry Smith PetscErrorCode ierr; 51776083293cSBarry Smith 51786083293cSBarry Smith PetscFunctionBegin; 5179b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 518083a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 5181e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 5182bbd56ea5SKarl Rupp 518383a4ac43SBarry Smith (*ctx)->howoften = howoften; 5184473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 5185c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 5186473a3ab2SBarry Smith 5187473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 5188c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 51896083293cSBarry Smith PetscFunctionReturn(0); 51906083293cSBarry Smith } 51916083293cSBarry Smith 51923a471f94SBarry Smith /*@C 519383a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 51943a471f94SBarry Smith VecView() for the error at each timestep 51953a471f94SBarry Smith 51963a471f94SBarry Smith Collective on TS 51973a471f94SBarry Smith 51983a471f94SBarry Smith Input Parameters: 51993a471f94SBarry Smith + ts - the TS context 52003a471f94SBarry Smith . step - current time-step 52013a471f94SBarry Smith . ptime - current time 52020298fd71SBarry Smith - dummy - either a viewer or NULL 52033a471f94SBarry Smith 52043a471f94SBarry Smith Level: intermediate 52053a471f94SBarry Smith 52063a471f94SBarry Smith .keywords: TS, vector, monitor, view 52073a471f94SBarry Smith 52083a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 52093a471f94SBarry Smith @*/ 52100910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 52113a471f94SBarry Smith { 52123a471f94SBarry Smith PetscErrorCode ierr; 521383a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 521483a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 52153a471f94SBarry Smith Vec work; 52163a471f94SBarry Smith 52173a471f94SBarry Smith PetscFunctionBegin; 5218b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 52190910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 52203a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 52210910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 52223a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 52233a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 52243a471f94SBarry Smith PetscFunctionReturn(0); 52253a471f94SBarry Smith } 52263a471f94SBarry Smith 5227af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 52286c699258SBarry Smith /*@ 52292a808120SBarry Smith TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS 52306c699258SBarry Smith 52313f9fe445SBarry Smith Logically Collective on TS and DM 52326c699258SBarry Smith 52336c699258SBarry Smith Input Parameters: 52342a808120SBarry Smith + ts - the ODE integrator object 52352a808120SBarry Smith - dm - the dm, cannot be NULL 52366c699258SBarry Smith 52376c699258SBarry Smith Level: intermediate 52386c699258SBarry Smith 52396c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 52406c699258SBarry Smith @*/ 52417087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 52426c699258SBarry Smith { 52436c699258SBarry Smith PetscErrorCode ierr; 5244089b2837SJed Brown SNES snes; 5245942e3340SBarry Smith DMTS tsdm; 52466c699258SBarry Smith 52476c699258SBarry Smith PetscFunctionBegin; 52480700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 52492a808120SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 525070663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 5251942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 52522a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 5253942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 5254942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 525524989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 525624989b8cSPeter Brune tsdm->originaldm = dm; 525724989b8cSPeter Brune } 525824989b8cSPeter Brune } 52596bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 526024989b8cSPeter Brune } 52616c699258SBarry Smith ts->dm = dm; 5262bbd56ea5SKarl Rupp 5263089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 5264089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 52656c699258SBarry Smith PetscFunctionReturn(0); 52666c699258SBarry Smith } 52676c699258SBarry Smith 52686c699258SBarry Smith /*@ 52696c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 52706c699258SBarry Smith 52713f9fe445SBarry Smith Not Collective 52726c699258SBarry Smith 52736c699258SBarry Smith Input Parameter: 52746c699258SBarry Smith . ts - the preconditioner context 52756c699258SBarry Smith 52766c699258SBarry Smith Output Parameter: 52776c699258SBarry Smith . dm - the dm 52786c699258SBarry Smith 52796c699258SBarry Smith Level: intermediate 52806c699258SBarry Smith 52816c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 52826c699258SBarry Smith @*/ 52837087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 52846c699258SBarry Smith { 5285496e6a7aSJed Brown PetscErrorCode ierr; 5286496e6a7aSJed Brown 52876c699258SBarry Smith PetscFunctionBegin; 52880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5289496e6a7aSJed Brown if (!ts->dm) { 5290ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 5291496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 5292496e6a7aSJed Brown } 52936c699258SBarry Smith *dm = ts->dm; 52946c699258SBarry Smith PetscFunctionReturn(0); 52956c699258SBarry Smith } 52961713a123SBarry Smith 52970f5c6efeSJed Brown /*@ 52980f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 52990f5c6efeSJed Brown 53003f9fe445SBarry Smith Logically Collective on SNES 53010f5c6efeSJed Brown 53020f5c6efeSJed Brown Input Parameter: 5303d42a1c89SJed Brown + snes - nonlinear solver 53040910c330SBarry Smith . U - the current state at which to evaluate the residual 5305d42a1c89SJed Brown - ctx - user context, must be a TS 53060f5c6efeSJed Brown 53070f5c6efeSJed Brown Output Parameter: 53080f5c6efeSJed Brown . F - the nonlinear residual 53090f5c6efeSJed Brown 53100f5c6efeSJed Brown Notes: 53110f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 53120f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 53130f5c6efeSJed Brown 53140f5c6efeSJed Brown Level: advanced 53150f5c6efeSJed Brown 53160f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 53170f5c6efeSJed Brown @*/ 53180910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 53190f5c6efeSJed Brown { 53200f5c6efeSJed Brown TS ts = (TS)ctx; 53210f5c6efeSJed Brown PetscErrorCode ierr; 53220f5c6efeSJed Brown 53230f5c6efeSJed Brown PetscFunctionBegin; 53240f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 53250910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 53260f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 53270f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 53280910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 53290f5c6efeSJed Brown PetscFunctionReturn(0); 53300f5c6efeSJed Brown } 53310f5c6efeSJed Brown 53320f5c6efeSJed Brown /*@ 53330f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 53340f5c6efeSJed Brown 53350f5c6efeSJed Brown Collective on SNES 53360f5c6efeSJed Brown 53370f5c6efeSJed Brown Input Parameter: 53380f5c6efeSJed Brown + snes - nonlinear solver 53390910c330SBarry Smith . U - the current state at which to evaluate the residual 53400f5c6efeSJed Brown - ctx - user context, must be a TS 53410f5c6efeSJed Brown 53420f5c6efeSJed Brown Output Parameter: 53430f5c6efeSJed Brown + A - the Jacobian 53440f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 53450f5c6efeSJed Brown - flag - indicates any structure change in the matrix 53460f5c6efeSJed Brown 53470f5c6efeSJed Brown Notes: 53480f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 53490f5c6efeSJed Brown 53500f5c6efeSJed Brown Level: developer 53510f5c6efeSJed Brown 53520f5c6efeSJed Brown .seealso: SNESSetJacobian() 53530f5c6efeSJed Brown @*/ 5354d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 53550f5c6efeSJed Brown { 53560f5c6efeSJed Brown TS ts = (TS)ctx; 53570f5c6efeSJed Brown PetscErrorCode ierr; 53580f5c6efeSJed Brown 53590f5c6efeSJed Brown PetscFunctionBegin; 53600f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 53610910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 53620f5c6efeSJed Brown PetscValidPointer(A,3); 536394ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 53640f5c6efeSJed Brown PetscValidPointer(B,4); 536594ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 53660f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 5367d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 53680f5c6efeSJed Brown PetscFunctionReturn(0); 53690f5c6efeSJed Brown } 5370325fc9f4SBarry Smith 53710e4ef248SJed Brown /*@C 53729ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 53730e4ef248SJed Brown 53740e4ef248SJed Brown Collective on TS 53750e4ef248SJed Brown 53760e4ef248SJed Brown Input Arguments: 53770e4ef248SJed Brown + ts - time stepping context 53780e4ef248SJed Brown . t - time at which to evaluate 53790910c330SBarry Smith . U - state at which to evaluate 53800e4ef248SJed Brown - ctx - context 53810e4ef248SJed Brown 53820e4ef248SJed Brown Output Arguments: 53830e4ef248SJed Brown . F - right hand side 53840e4ef248SJed Brown 53850e4ef248SJed Brown Level: intermediate 53860e4ef248SJed Brown 53870e4ef248SJed Brown Notes: 53880e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 53890e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 53900e4ef248SJed Brown 53910e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 53920e4ef248SJed Brown @*/ 53930910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 53940e4ef248SJed Brown { 53950e4ef248SJed Brown PetscErrorCode ierr; 53960e4ef248SJed Brown Mat Arhs,Brhs; 53970e4ef248SJed Brown 53980e4ef248SJed Brown PetscFunctionBegin; 53990e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 5400d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 54010910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 54020e4ef248SJed Brown PetscFunctionReturn(0); 54030e4ef248SJed Brown } 54040e4ef248SJed Brown 54050e4ef248SJed Brown /*@C 54060e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 54070e4ef248SJed Brown 54080e4ef248SJed Brown Collective on TS 54090e4ef248SJed Brown 54100e4ef248SJed Brown Input Arguments: 54110e4ef248SJed Brown + ts - time stepping context 54120e4ef248SJed Brown . t - time at which to evaluate 54130910c330SBarry Smith . U - state at which to evaluate 54140e4ef248SJed Brown - ctx - context 54150e4ef248SJed Brown 54160e4ef248SJed Brown Output Arguments: 54170e4ef248SJed Brown + A - pointer to operator 54180e4ef248SJed Brown . B - pointer to preconditioning matrix 54190e4ef248SJed Brown - flg - matrix structure flag 54200e4ef248SJed Brown 54210e4ef248SJed Brown Level: intermediate 54220e4ef248SJed Brown 54230e4ef248SJed Brown Notes: 54240e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 54250e4ef248SJed Brown 54260e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 54270e4ef248SJed Brown @*/ 5428d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 54290e4ef248SJed Brown { 54300e4ef248SJed Brown PetscFunctionBegin; 54310e4ef248SJed Brown PetscFunctionReturn(0); 54320e4ef248SJed Brown } 54330e4ef248SJed Brown 54340026cea9SSean Farley /*@C 54350026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 54360026cea9SSean Farley 54370026cea9SSean Farley Collective on TS 54380026cea9SSean Farley 54390026cea9SSean Farley Input Arguments: 54400026cea9SSean Farley + ts - time stepping context 54410026cea9SSean Farley . t - time at which to evaluate 54420910c330SBarry Smith . U - state at which to evaluate 54430910c330SBarry Smith . Udot - time derivative of state vector 54440026cea9SSean Farley - ctx - context 54450026cea9SSean Farley 54460026cea9SSean Farley Output Arguments: 54470026cea9SSean Farley . F - left hand side 54480026cea9SSean Farley 54490026cea9SSean Farley Level: intermediate 54500026cea9SSean Farley 54510026cea9SSean Farley Notes: 54520910c330SBarry 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 54530026cea9SSean Farley user is required to write their own TSComputeIFunction. 54540026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 54550026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 54560026cea9SSean Farley 54579ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 54589ae8fd06SBarry Smith 54599ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 54600026cea9SSean Farley @*/ 54610910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 54620026cea9SSean Farley { 54630026cea9SSean Farley PetscErrorCode ierr; 54640026cea9SSean Farley Mat A,B; 54650026cea9SSean Farley 54660026cea9SSean Farley PetscFunctionBegin; 54670298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 5468d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 54690910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 54700026cea9SSean Farley PetscFunctionReturn(0); 54710026cea9SSean Farley } 54720026cea9SSean Farley 54730026cea9SSean Farley /*@C 5474b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 54750026cea9SSean Farley 54760026cea9SSean Farley Collective on TS 54770026cea9SSean Farley 54780026cea9SSean Farley Input Arguments: 54790026cea9SSean Farley + ts - time stepping context 54800026cea9SSean Farley . t - time at which to evaluate 54810910c330SBarry Smith . U - state at which to evaluate 54820910c330SBarry Smith . Udot - time derivative of state vector 54830026cea9SSean Farley . shift - shift to apply 54840026cea9SSean Farley - ctx - context 54850026cea9SSean Farley 54860026cea9SSean Farley Output Arguments: 54870026cea9SSean Farley + A - pointer to operator 54880026cea9SSean Farley . B - pointer to preconditioning matrix 54890026cea9SSean Farley - flg - matrix structure flag 54900026cea9SSean Farley 5491b41af12eSJed Brown Level: advanced 54920026cea9SSean Farley 54930026cea9SSean Farley Notes: 54940026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 54950026cea9SSean Farley 5496b41af12eSJed Brown It is only appropriate for problems of the form 5497b41af12eSJed Brown 5498b41af12eSJed Brown $ M Udot = F(U,t) 5499b41af12eSJed Brown 5500b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 5501b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 5502b41af12eSJed Brown an implicit operator of the form 5503b41af12eSJed Brown 5504b41af12eSJed Brown $ shift*M + J 5505b41af12eSJed Brown 5506b41af12eSJed 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 5507b41af12eSJed Brown a copy of M or reassemble it when requested. 5508b41af12eSJed Brown 55090026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 55100026cea9SSean Farley @*/ 5511d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 55120026cea9SSean Farley { 5513b41af12eSJed Brown PetscErrorCode ierr; 5514b41af12eSJed Brown 55150026cea9SSean Farley PetscFunctionBegin; 551694ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 5517b41af12eSJed Brown ts->ijacobian.shift = shift; 55180026cea9SSean Farley PetscFunctionReturn(0); 55190026cea9SSean Farley } 5520b41af12eSJed Brown 5521e817cc15SEmil Constantinescu /*@ 5522e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 5523e817cc15SEmil Constantinescu 5524e817cc15SEmil Constantinescu Not Collective 5525e817cc15SEmil Constantinescu 5526e817cc15SEmil Constantinescu Input Parameter: 5527e817cc15SEmil Constantinescu . ts - the TS context 5528e817cc15SEmil Constantinescu 5529e817cc15SEmil Constantinescu Output Parameter: 55304e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 5531e817cc15SEmil Constantinescu 5532e817cc15SEmil Constantinescu Level: beginner 5533e817cc15SEmil Constantinescu 5534e817cc15SEmil Constantinescu .keywords: TS, equation type 5535e817cc15SEmil Constantinescu 5536e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 5537e817cc15SEmil Constantinescu @*/ 5538e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 5539e817cc15SEmil Constantinescu { 5540e817cc15SEmil Constantinescu PetscFunctionBegin; 5541e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5542e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 5543e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 5544e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5545e817cc15SEmil Constantinescu } 5546e817cc15SEmil Constantinescu 5547e817cc15SEmil Constantinescu /*@ 5548e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 5549e817cc15SEmil Constantinescu 5550e817cc15SEmil Constantinescu Not Collective 5551e817cc15SEmil Constantinescu 5552e817cc15SEmil Constantinescu Input Parameter: 5553e817cc15SEmil Constantinescu + ts - the TS context 55541297b224SEmil Constantinescu - equation_type - see TSEquationType 5555e817cc15SEmil Constantinescu 5556e817cc15SEmil Constantinescu Level: advanced 5557e817cc15SEmil Constantinescu 5558e817cc15SEmil Constantinescu .keywords: TS, equation type 5559e817cc15SEmil Constantinescu 5560e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 5561e817cc15SEmil Constantinescu @*/ 5562e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 5563e817cc15SEmil Constantinescu { 5564e817cc15SEmil Constantinescu PetscFunctionBegin; 5565e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5566e817cc15SEmil Constantinescu ts->equation_type = equation_type; 5567e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5568e817cc15SEmil Constantinescu } 55690026cea9SSean Farley 55704af1b03aSJed Brown /*@ 55714af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 55724af1b03aSJed Brown 55734af1b03aSJed Brown Not Collective 55744af1b03aSJed Brown 55754af1b03aSJed Brown Input Parameter: 55764af1b03aSJed Brown . ts - the TS context 55774af1b03aSJed Brown 55784af1b03aSJed Brown Output Parameter: 55794af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 55804af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 55814af1b03aSJed Brown 5582487e0bb9SJed Brown Level: beginner 55834af1b03aSJed Brown 5584cd652676SJed Brown Notes: 5585cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 55864af1b03aSJed Brown 55874af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 55884af1b03aSJed Brown 55894af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 55904af1b03aSJed Brown @*/ 55914af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 55924af1b03aSJed Brown { 55934af1b03aSJed Brown PetscFunctionBegin; 55944af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 55954af1b03aSJed Brown PetscValidPointer(reason,2); 55964af1b03aSJed Brown *reason = ts->reason; 55974af1b03aSJed Brown PetscFunctionReturn(0); 55984af1b03aSJed Brown } 55994af1b03aSJed Brown 5600d6ad946cSShri Abhyankar /*@ 5601d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 5602d6ad946cSShri Abhyankar 5603d6ad946cSShri Abhyankar Not Collective 5604d6ad946cSShri Abhyankar 5605d6ad946cSShri Abhyankar Input Parameter: 5606d6ad946cSShri Abhyankar + ts - the TS context 5607d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 5608d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 5609d6ad946cSShri Abhyankar 5610f5abba47SShri Abhyankar Level: advanced 5611d6ad946cSShri Abhyankar 5612d6ad946cSShri Abhyankar Notes: 5613d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 5614d6ad946cSShri Abhyankar 5615d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 5616d6ad946cSShri Abhyankar 5617d6ad946cSShri Abhyankar .seealso: TSConvergedReason 5618d6ad946cSShri Abhyankar @*/ 5619d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 5620d6ad946cSShri Abhyankar { 5621d6ad946cSShri Abhyankar PetscFunctionBegin; 5622d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5623d6ad946cSShri Abhyankar ts->reason = reason; 5624d6ad946cSShri Abhyankar PetscFunctionReturn(0); 5625d6ad946cSShri Abhyankar } 5626d6ad946cSShri Abhyankar 5627cc708dedSBarry Smith /*@ 5628cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 5629cc708dedSBarry Smith 5630cc708dedSBarry Smith Not Collective 5631cc708dedSBarry Smith 5632cc708dedSBarry Smith Input Parameter: 5633cc708dedSBarry Smith . ts - the TS context 5634cc708dedSBarry Smith 5635cc708dedSBarry Smith Output Parameter: 563619eac22cSLisandro Dalcin . ftime - the final time. This time corresponds to the final time set with TSSetMaxTime() 5637cc708dedSBarry Smith 5638487e0bb9SJed Brown Level: beginner 5639cc708dedSBarry Smith 5640cc708dedSBarry Smith Notes: 5641cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 5642cc708dedSBarry Smith 5643cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 5644cc708dedSBarry Smith 5645cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 5646cc708dedSBarry Smith @*/ 5647cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 5648cc708dedSBarry Smith { 5649cc708dedSBarry Smith PetscFunctionBegin; 5650cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5651cc708dedSBarry Smith PetscValidPointer(ftime,2); 5652cc708dedSBarry Smith *ftime = ts->solvetime; 5653cc708dedSBarry Smith PetscFunctionReturn(0); 5654cc708dedSBarry Smith } 5655cc708dedSBarry Smith 56562c18e0fdSBarry Smith /*@ 56575ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 56589f67acb7SJed Brown used by the time integrator. 56599f67acb7SJed Brown 56609f67acb7SJed Brown Not Collective 56619f67acb7SJed Brown 56629f67acb7SJed Brown Input Parameter: 56639f67acb7SJed Brown . ts - TS context 56649f67acb7SJed Brown 56659f67acb7SJed Brown Output Parameter: 56669f67acb7SJed Brown . nits - number of nonlinear iterations 56679f67acb7SJed Brown 56689f67acb7SJed Brown Notes: 56699f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 56709f67acb7SJed Brown 56719f67acb7SJed Brown Level: intermediate 56729f67acb7SJed Brown 56739f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 56749f67acb7SJed Brown 56755ef26d82SJed Brown .seealso: TSGetKSPIterations() 56769f67acb7SJed Brown @*/ 56775ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 56789f67acb7SJed Brown { 56799f67acb7SJed Brown PetscFunctionBegin; 56809f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 56819f67acb7SJed Brown PetscValidIntPointer(nits,2); 56825ef26d82SJed Brown *nits = ts->snes_its; 56839f67acb7SJed Brown PetscFunctionReturn(0); 56849f67acb7SJed Brown } 56859f67acb7SJed Brown 56869f67acb7SJed Brown /*@ 56875ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 56889f67acb7SJed Brown used by the time integrator. 56899f67acb7SJed Brown 56909f67acb7SJed Brown Not Collective 56919f67acb7SJed Brown 56929f67acb7SJed Brown Input Parameter: 56939f67acb7SJed Brown . ts - TS context 56949f67acb7SJed Brown 56959f67acb7SJed Brown Output Parameter: 56969f67acb7SJed Brown . lits - number of linear iterations 56979f67acb7SJed Brown 56989f67acb7SJed Brown Notes: 56999f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 57009f67acb7SJed Brown 57019f67acb7SJed Brown Level: intermediate 57029f67acb7SJed Brown 57039f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 57049f67acb7SJed Brown 57055ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 57069f67acb7SJed Brown @*/ 57075ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 57089f67acb7SJed Brown { 57099f67acb7SJed Brown PetscFunctionBegin; 57109f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 57119f67acb7SJed Brown PetscValidIntPointer(lits,2); 57125ef26d82SJed Brown *lits = ts->ksp_its; 57139f67acb7SJed Brown PetscFunctionReturn(0); 57149f67acb7SJed Brown } 57159f67acb7SJed Brown 5716cef5090cSJed Brown /*@ 5717cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 5718cef5090cSJed Brown 5719cef5090cSJed Brown Not Collective 5720cef5090cSJed Brown 5721cef5090cSJed Brown Input Parameter: 5722cef5090cSJed Brown . ts - TS context 5723cef5090cSJed Brown 5724cef5090cSJed Brown Output Parameter: 5725cef5090cSJed Brown . rejects - number of steps rejected 5726cef5090cSJed Brown 5727cef5090cSJed Brown Notes: 5728cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5729cef5090cSJed Brown 5730cef5090cSJed Brown Level: intermediate 5731cef5090cSJed Brown 5732cef5090cSJed Brown .keywords: TS, get, number 5733cef5090cSJed Brown 57345ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 5735cef5090cSJed Brown @*/ 5736cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 5737cef5090cSJed Brown { 5738cef5090cSJed Brown PetscFunctionBegin; 5739cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5740cef5090cSJed Brown PetscValidIntPointer(rejects,2); 5741cef5090cSJed Brown *rejects = ts->reject; 5742cef5090cSJed Brown PetscFunctionReturn(0); 5743cef5090cSJed Brown } 5744cef5090cSJed Brown 5745cef5090cSJed Brown /*@ 5746cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 5747cef5090cSJed Brown 5748cef5090cSJed Brown Not Collective 5749cef5090cSJed Brown 5750cef5090cSJed Brown Input Parameter: 5751cef5090cSJed Brown . ts - TS context 5752cef5090cSJed Brown 5753cef5090cSJed Brown Output Parameter: 5754cef5090cSJed Brown . fails - number of failed nonlinear solves 5755cef5090cSJed Brown 5756cef5090cSJed Brown Notes: 5757cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5758cef5090cSJed Brown 5759cef5090cSJed Brown Level: intermediate 5760cef5090cSJed Brown 5761cef5090cSJed Brown .keywords: TS, get, number 5762cef5090cSJed Brown 57635ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 5764cef5090cSJed Brown @*/ 5765cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 5766cef5090cSJed Brown { 5767cef5090cSJed Brown PetscFunctionBegin; 5768cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5769cef5090cSJed Brown PetscValidIntPointer(fails,2); 5770cef5090cSJed Brown *fails = ts->num_snes_failures; 5771cef5090cSJed Brown PetscFunctionReturn(0); 5772cef5090cSJed Brown } 5773cef5090cSJed Brown 5774cef5090cSJed Brown /*@ 5775cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 5776cef5090cSJed Brown 5777cef5090cSJed Brown Not Collective 5778cef5090cSJed Brown 5779cef5090cSJed Brown Input Parameter: 5780cef5090cSJed Brown + ts - TS context 5781cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 5782cef5090cSJed Brown 5783cef5090cSJed Brown Notes: 5784cef5090cSJed Brown The counter is reset to zero for each step 5785cef5090cSJed Brown 5786cef5090cSJed Brown Options Database Key: 5787cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 5788cef5090cSJed Brown 5789cef5090cSJed Brown Level: intermediate 5790cef5090cSJed Brown 5791cef5090cSJed Brown .keywords: TS, set, maximum, number 5792cef5090cSJed Brown 57935ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5794cef5090cSJed Brown @*/ 5795cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 5796cef5090cSJed Brown { 5797cef5090cSJed Brown PetscFunctionBegin; 5798cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5799cef5090cSJed Brown ts->max_reject = rejects; 5800cef5090cSJed Brown PetscFunctionReturn(0); 5801cef5090cSJed Brown } 5802cef5090cSJed Brown 5803cef5090cSJed Brown /*@ 5804cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 5805cef5090cSJed Brown 5806cef5090cSJed Brown Not Collective 5807cef5090cSJed Brown 5808cef5090cSJed Brown Input Parameter: 5809cef5090cSJed Brown + ts - TS context 5810cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 5811cef5090cSJed Brown 5812cef5090cSJed Brown Notes: 5813cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 5814cef5090cSJed Brown 5815cef5090cSJed Brown Options Database Key: 5816cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 5817cef5090cSJed Brown 5818cef5090cSJed Brown Level: intermediate 5819cef5090cSJed Brown 5820cef5090cSJed Brown .keywords: TS, set, maximum, number 5821cef5090cSJed Brown 58225ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 5823cef5090cSJed Brown @*/ 5824cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 5825cef5090cSJed Brown { 5826cef5090cSJed Brown PetscFunctionBegin; 5827cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5828cef5090cSJed Brown ts->max_snes_failures = fails; 5829cef5090cSJed Brown PetscFunctionReturn(0); 5830cef5090cSJed Brown } 5831cef5090cSJed Brown 5832cef5090cSJed Brown /*@ 5833cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 5834cef5090cSJed Brown 5835cef5090cSJed Brown Not Collective 5836cef5090cSJed Brown 5837cef5090cSJed Brown Input Parameter: 5838cef5090cSJed Brown + ts - TS context 5839cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 5840cef5090cSJed Brown 5841cef5090cSJed Brown Options Database Key: 5842cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 5843cef5090cSJed Brown 5844cef5090cSJed Brown Level: intermediate 5845cef5090cSJed Brown 5846cef5090cSJed Brown .keywords: TS, set, error 5847cef5090cSJed Brown 58485ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5849cef5090cSJed Brown @*/ 5850cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 5851cef5090cSJed Brown { 5852cef5090cSJed Brown PetscFunctionBegin; 5853cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5854cef5090cSJed Brown ts->errorifstepfailed = err; 5855cef5090cSJed Brown PetscFunctionReturn(0); 5856cef5090cSJed Brown } 5857cef5090cSJed Brown 5858fb1732b5SBarry Smith /*@C 5859fde5950dSBarry 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 5860fb1732b5SBarry Smith 5861fb1732b5SBarry Smith Collective on TS 5862fb1732b5SBarry Smith 5863fb1732b5SBarry Smith Input Parameters: 5864fb1732b5SBarry Smith + ts - the TS context 5865fb1732b5SBarry Smith . step - current time-step 5866fb1732b5SBarry Smith . ptime - current time 58670910c330SBarry Smith . u - current state 5868721cd6eeSBarry Smith - vf - viewer and its format 5869fb1732b5SBarry Smith 5870fb1732b5SBarry Smith Level: intermediate 5871fb1732b5SBarry Smith 5872fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 5873fb1732b5SBarry Smith 5874fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5875fb1732b5SBarry Smith @*/ 5876721cd6eeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf) 5877fb1732b5SBarry Smith { 5878fb1732b5SBarry Smith PetscErrorCode ierr; 5879fb1732b5SBarry Smith 5880fb1732b5SBarry Smith PetscFunctionBegin; 5881721cd6eeSBarry Smith ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr); 5882721cd6eeSBarry Smith ierr = VecView(u,vf->viewer);CHKERRQ(ierr); 5883721cd6eeSBarry Smith ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr); 5884ed81e22dSJed Brown PetscFunctionReturn(0); 5885ed81e22dSJed Brown } 5886ed81e22dSJed Brown 5887ed81e22dSJed Brown /*@C 5888ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5889ed81e22dSJed Brown 5890ed81e22dSJed Brown Collective on TS 5891ed81e22dSJed Brown 5892ed81e22dSJed Brown Input Parameters: 5893ed81e22dSJed Brown + ts - the TS context 5894ed81e22dSJed Brown . step - current time-step 5895ed81e22dSJed Brown . ptime - current time 58960910c330SBarry Smith . u - current state 5897ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5898ed81e22dSJed Brown 5899ed81e22dSJed Brown Level: intermediate 5900ed81e22dSJed Brown 5901ed81e22dSJed Brown Notes: 5902ed81e22dSJed 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. 5903ed81e22dSJed Brown These are named according to the file name template. 5904ed81e22dSJed Brown 5905ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5906ed81e22dSJed Brown 5907ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5908ed81e22dSJed Brown 5909ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5910ed81e22dSJed Brown @*/ 59110910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5912ed81e22dSJed Brown { 5913ed81e22dSJed Brown PetscErrorCode ierr; 5914ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 5915ed81e22dSJed Brown PetscViewer viewer; 5916ed81e22dSJed Brown 5917ed81e22dSJed Brown PetscFunctionBegin; 591863e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 59198caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5920ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 59210910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 5922ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5923ed81e22dSJed Brown PetscFunctionReturn(0); 5924ed81e22dSJed Brown } 5925ed81e22dSJed Brown 5926ed81e22dSJed Brown /*@C 5927ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5928ed81e22dSJed Brown 5929ed81e22dSJed Brown Collective on TS 5930ed81e22dSJed Brown 5931ed81e22dSJed Brown Input Parameters: 5932ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5933ed81e22dSJed Brown 5934ed81e22dSJed Brown Level: intermediate 5935ed81e22dSJed Brown 5936ed81e22dSJed Brown Note: 5937ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5938ed81e22dSJed Brown 5939ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5940ed81e22dSJed Brown 5941ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5942ed81e22dSJed Brown @*/ 5943ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5944ed81e22dSJed Brown { 5945ed81e22dSJed Brown PetscErrorCode ierr; 5946ed81e22dSJed Brown 5947ed81e22dSJed Brown PetscFunctionBegin; 5948ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5949fb1732b5SBarry Smith PetscFunctionReturn(0); 5950fb1732b5SBarry Smith } 5951fb1732b5SBarry Smith 595284df9cb4SJed Brown /*@ 5953552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 595484df9cb4SJed Brown 5955ed81e22dSJed Brown Collective on TS if controller has not been created yet 595684df9cb4SJed Brown 595784df9cb4SJed Brown Input Arguments: 5958ed81e22dSJed Brown . ts - time stepping context 595984df9cb4SJed Brown 596084df9cb4SJed Brown Output Arguments: 5961ed81e22dSJed Brown . adapt - adaptive controller 596284df9cb4SJed Brown 596384df9cb4SJed Brown Level: intermediate 596484df9cb4SJed Brown 5965ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 596684df9cb4SJed Brown @*/ 5967552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 596884df9cb4SJed Brown { 596984df9cb4SJed Brown PetscErrorCode ierr; 597084df9cb4SJed Brown 597184df9cb4SJed Brown PetscFunctionBegin; 597284df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5973bec58848SLisandro Dalcin PetscValidPointer(adapt,2); 597484df9cb4SJed Brown if (!ts->adapt) { 5975ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 59763bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 59771c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 597884df9cb4SJed Brown } 5979bec58848SLisandro Dalcin *adapt = ts->adapt; 598084df9cb4SJed Brown PetscFunctionReturn(0); 598184df9cb4SJed Brown } 5982d6ebe24aSShri Abhyankar 59831c3436cfSJed Brown /*@ 59841c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 59851c3436cfSJed Brown 59861c3436cfSJed Brown Logically Collective 59871c3436cfSJed Brown 59881c3436cfSJed Brown Input Arguments: 59891c3436cfSJed Brown + ts - time integration context 59901c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 59910298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 59921c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 59930298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 59941c3436cfSJed Brown 5995a3cdaa26SBarry Smith Options Database keys: 5996a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5997a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5998a3cdaa26SBarry Smith 59993ff766beSShri Abhyankar Notes: 60003ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 60013ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 60023ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 60033ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 60043ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 60053ff766beSShri Abhyankar differential variables. 60063ff766beSShri Abhyankar 60071c3436cfSJed Brown Level: beginner 60081c3436cfSJed Brown 6009c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 60101c3436cfSJed Brown @*/ 60111c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 60121c3436cfSJed Brown { 60131c3436cfSJed Brown PetscErrorCode ierr; 60141c3436cfSJed Brown 60151c3436cfSJed Brown PetscFunctionBegin; 6016c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 60171c3436cfSJed Brown if (vatol) { 60181c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 60191c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 60201c3436cfSJed Brown ts->vatol = vatol; 60211c3436cfSJed Brown } 6022c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 60231c3436cfSJed Brown if (vrtol) { 60241c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 60251c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 60261c3436cfSJed Brown ts->vrtol = vrtol; 60271c3436cfSJed Brown } 60281c3436cfSJed Brown PetscFunctionReturn(0); 60291c3436cfSJed Brown } 60301c3436cfSJed Brown 6031c5033834SJed Brown /*@ 6032c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 6033c5033834SJed Brown 6034c5033834SJed Brown Logically Collective 6035c5033834SJed Brown 6036c5033834SJed Brown Input Arguments: 6037c5033834SJed Brown . ts - time integration context 6038c5033834SJed Brown 6039c5033834SJed Brown Output Arguments: 60400298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 60410298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 60420298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 60430298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 6044c5033834SJed Brown 6045c5033834SJed Brown Level: beginner 6046c5033834SJed Brown 6047c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 6048c5033834SJed Brown @*/ 6049c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 6050c5033834SJed Brown { 6051c5033834SJed Brown PetscFunctionBegin; 6052c5033834SJed Brown if (atol) *atol = ts->atol; 6053c5033834SJed Brown if (vatol) *vatol = ts->vatol; 6054c5033834SJed Brown if (rtol) *rtol = ts->rtol; 6055c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 6056c5033834SJed Brown PetscFunctionReturn(0); 6057c5033834SJed Brown } 6058c5033834SJed Brown 60599c6b16b5SShri Abhyankar /*@ 6060a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 60619c6b16b5SShri Abhyankar 60629c6b16b5SShri Abhyankar Collective on TS 60639c6b16b5SShri Abhyankar 60649c6b16b5SShri Abhyankar Input Arguments: 60659c6b16b5SShri Abhyankar + ts - time stepping context 6066a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 6067a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 60689c6b16b5SShri Abhyankar 60699c6b16b5SShri Abhyankar Output Arguments: 60707453f775SEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 60717453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 60727453f775SEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 60739c6b16b5SShri Abhyankar 60749c6b16b5SShri Abhyankar Level: developer 60759c6b16b5SShri Abhyankar 6076deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 60779c6b16b5SShri Abhyankar @*/ 60787453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 60799c6b16b5SShri Abhyankar { 60809c6b16b5SShri Abhyankar PetscErrorCode ierr; 60819c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 60827453f775SEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 60837453f775SEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 60849c6b16b5SShri Abhyankar const PetscScalar *u,*y; 60857453f775SEmil Constantinescu PetscReal sum,suma,sumr,gsum,gsuma,gsumr,diff; 60867453f775SEmil Constantinescu PetscReal tol,tola,tolr; 60877453f775SEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 60889c6b16b5SShri Abhyankar 60899c6b16b5SShri Abhyankar PetscFunctionBegin; 60909c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6091a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 6092a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 6093a4868fbcSLisandro Dalcin PetscValidType(U,2); 6094a4868fbcSLisandro Dalcin PetscValidType(Y,3); 6095a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 6096a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 60978a175baeSEmil Constantinescu PetscValidPointer(norma,5); 60988a175baeSEmil Constantinescu PetscValidPointer(normr,6); 6099a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 61009c6b16b5SShri Abhyankar 61019c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 61029c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 61039c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 61049c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 61059c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 61067453f775SEmil Constantinescu sum = 0.; n_loc = 0; 61077453f775SEmil Constantinescu suma = 0.; na_loc = 0; 61087453f775SEmil Constantinescu sumr = 0.; nr_loc = 0; 61099c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 61109c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 61119c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61129c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61139c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 61147453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 61157453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 61167453f775SEmil Constantinescu if(tola>0.){ 61177453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 61187453f775SEmil Constantinescu na_loc++; 61197453f775SEmil Constantinescu } 61207453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61217453f775SEmil Constantinescu if(tolr>0.){ 61227453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 61237453f775SEmil Constantinescu nr_loc++; 61247453f775SEmil Constantinescu } 61257453f775SEmil Constantinescu tol=tola+tolr; 61267453f775SEmil Constantinescu if(tol>0.){ 61277453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 61287453f775SEmil Constantinescu n_loc++; 61297453f775SEmil Constantinescu } 61309c6b16b5SShri Abhyankar } 61319c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61329c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61339c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 61349c6b16b5SShri Abhyankar const PetscScalar *atol; 61359c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61369c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 61377453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 61387453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 61397453f775SEmil Constantinescu if(tola>0.){ 61407453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 61417453f775SEmil Constantinescu na_loc++; 61427453f775SEmil Constantinescu } 61437453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61447453f775SEmil Constantinescu if(tolr>0.){ 61457453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 61467453f775SEmil Constantinescu nr_loc++; 61477453f775SEmil Constantinescu } 61487453f775SEmil Constantinescu tol=tola+tolr; 61497453f775SEmil Constantinescu if(tol>0.){ 61507453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 61517453f775SEmil Constantinescu n_loc++; 61527453f775SEmil Constantinescu } 61539c6b16b5SShri Abhyankar } 61549c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61559c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 61569c6b16b5SShri Abhyankar const PetscScalar *rtol; 61579c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61589c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 61597453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 61607453f775SEmil Constantinescu tola = ts->atol; 61617453f775SEmil Constantinescu if(tola>0.){ 61627453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 61637453f775SEmil Constantinescu na_loc++; 61647453f775SEmil Constantinescu } 61657453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61667453f775SEmil Constantinescu if(tolr>0.){ 61677453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 61687453f775SEmil Constantinescu nr_loc++; 61697453f775SEmil Constantinescu } 61707453f775SEmil Constantinescu tol=tola+tolr; 61717453f775SEmil Constantinescu if(tol>0.){ 61727453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 61737453f775SEmil Constantinescu n_loc++; 61747453f775SEmil Constantinescu } 61759c6b16b5SShri Abhyankar } 61769c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61779c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 61789c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 61797453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 61807453f775SEmil Constantinescu tola = ts->atol; 61817453f775SEmil Constantinescu if(tola>0.){ 61827453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 61837453f775SEmil Constantinescu na_loc++; 61847453f775SEmil Constantinescu } 61857453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61867453f775SEmil Constantinescu if(tolr>0.){ 61877453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 61887453f775SEmil Constantinescu nr_loc++; 61897453f775SEmil Constantinescu } 61907453f775SEmil Constantinescu tol=tola+tolr; 61917453f775SEmil Constantinescu if(tol>0.){ 61927453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 61937453f775SEmil Constantinescu n_loc++; 61947453f775SEmil Constantinescu } 61959c6b16b5SShri Abhyankar } 61969c6b16b5SShri Abhyankar } 61979c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 61989c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 61999c6b16b5SShri Abhyankar 62007453f775SEmil Constantinescu err_loc[0] = sum; 62017453f775SEmil Constantinescu err_loc[1] = suma; 62027453f775SEmil Constantinescu err_loc[2] = sumr; 62037453f775SEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 62047453f775SEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 62057453f775SEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 62067453f775SEmil Constantinescu 6207a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 62087453f775SEmil Constantinescu 62097453f775SEmil Constantinescu gsum = err_glb[0]; 62107453f775SEmil Constantinescu gsuma = err_glb[1]; 62117453f775SEmil Constantinescu gsumr = err_glb[2]; 62127453f775SEmil Constantinescu n_glb = err_glb[3]; 62137453f775SEmil Constantinescu na_glb = err_glb[4]; 62147453f775SEmil Constantinescu nr_glb = err_glb[5]; 62157453f775SEmil Constantinescu 6216b1316ef9SEmil Constantinescu *norm = 0.; 6217b1316ef9SEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 6218b1316ef9SEmil Constantinescu *norma = 0.; 6219b1316ef9SEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 6220b1316ef9SEmil Constantinescu *normr = 0.; 6221b1316ef9SEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 62229c6b16b5SShri Abhyankar 62239c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 62247453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 62257453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 62269c6b16b5SShri Abhyankar PetscFunctionReturn(0); 62279c6b16b5SShri Abhyankar } 62289c6b16b5SShri Abhyankar 62299c6b16b5SShri Abhyankar /*@ 6230a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 62319c6b16b5SShri Abhyankar 62329c6b16b5SShri Abhyankar Collective on TS 62339c6b16b5SShri Abhyankar 62349c6b16b5SShri Abhyankar Input Arguments: 62359c6b16b5SShri Abhyankar + ts - time stepping context 6236a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 6237a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 62389c6b16b5SShri Abhyankar 62399c6b16b5SShri Abhyankar Output Arguments: 62407453f775SEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 62417453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 62427453f775SEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 62439c6b16b5SShri Abhyankar 62449c6b16b5SShri Abhyankar Level: developer 62459c6b16b5SShri Abhyankar 6246deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 62479c6b16b5SShri Abhyankar @*/ 62487453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 62499c6b16b5SShri Abhyankar { 62509c6b16b5SShri Abhyankar PetscErrorCode ierr; 62517453f775SEmil Constantinescu PetscInt i,n,N,rstart; 62529c6b16b5SShri Abhyankar const PetscScalar *u,*y; 62537453f775SEmil Constantinescu PetscReal max,gmax,maxa,gmaxa,maxr,gmaxr; 62547453f775SEmil Constantinescu PetscReal tol,tola,tolr,diff; 62557453f775SEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 62569c6b16b5SShri Abhyankar 62579c6b16b5SShri Abhyankar PetscFunctionBegin; 62589c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6259a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 6260a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 6261a4868fbcSLisandro Dalcin PetscValidType(U,2); 6262a4868fbcSLisandro Dalcin PetscValidType(Y,3); 6263a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 6264a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 62658a175baeSEmil Constantinescu PetscValidPointer(norma,5); 62668a175baeSEmil Constantinescu PetscValidPointer(normr,6); 6267a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 62689c6b16b5SShri Abhyankar 62699c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 62709c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 62719c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 62729c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 62739c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 62747453f775SEmil Constantinescu 62757453f775SEmil Constantinescu max=0.; 62767453f775SEmil Constantinescu maxa=0.; 62777453f775SEmil Constantinescu maxr=0.; 62787453f775SEmil Constantinescu 62797453f775SEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 62809c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 62819c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 62829c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 62837453f775SEmil Constantinescu 62847453f775SEmil Constantinescu for (i=0; i<n; i++) { 62857453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 62867453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 62877453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 62887453f775SEmil Constantinescu tol = tola+tolr; 62897453f775SEmil Constantinescu if(tola>0.){ 62907453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 62917453f775SEmil Constantinescu } 62927453f775SEmil Constantinescu if(tolr>0.){ 62937453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 62947453f775SEmil Constantinescu } 62957453f775SEmil Constantinescu if(tol>0.){ 62967453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 62977453f775SEmil Constantinescu } 62989c6b16b5SShri Abhyankar } 62999c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 63009c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 63019c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 63029c6b16b5SShri Abhyankar const PetscScalar *atol; 63039c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 63047453f775SEmil Constantinescu for (i=0; i<n; i++) { 63057453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 63067453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 63077453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 63087453f775SEmil Constantinescu tol = tola+tolr; 63097453f775SEmil Constantinescu if(tola>0.){ 63107453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 63117453f775SEmil Constantinescu } 63127453f775SEmil Constantinescu if(tolr>0.){ 63137453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 63147453f775SEmil Constantinescu } 63157453f775SEmil Constantinescu if(tol>0.){ 63167453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 63177453f775SEmil Constantinescu } 63189c6b16b5SShri Abhyankar } 63199c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 63209c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 63219c6b16b5SShri Abhyankar const PetscScalar *rtol; 63229c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 63237453f775SEmil Constantinescu 63247453f775SEmil Constantinescu for (i=0; i<n; i++) { 63257453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 63267453f775SEmil Constantinescu tola = ts->atol; 63277453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 63287453f775SEmil Constantinescu tol = tola+tolr; 63297453f775SEmil Constantinescu if(tola>0.){ 63307453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 63317453f775SEmil Constantinescu } 63327453f775SEmil Constantinescu if(tolr>0.){ 63337453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 63347453f775SEmil Constantinescu } 63357453f775SEmil Constantinescu if(tol>0.){ 63367453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 63377453f775SEmil Constantinescu } 63389c6b16b5SShri Abhyankar } 63399c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 63409c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 63417453f775SEmil Constantinescu 63427453f775SEmil Constantinescu for (i=0; i<n; i++) { 63437453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 63447453f775SEmil Constantinescu tola = ts->atol; 63457453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 63467453f775SEmil Constantinescu tol = tola+tolr; 63477453f775SEmil Constantinescu if(tola>0.){ 63487453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 63497453f775SEmil Constantinescu } 63507453f775SEmil Constantinescu if(tolr>0.){ 63517453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 63527453f775SEmil Constantinescu } 63537453f775SEmil Constantinescu if(tol>0.){ 63547453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 63557453f775SEmil Constantinescu } 63569c6b16b5SShri Abhyankar } 63579c6b16b5SShri Abhyankar } 63589c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 63599c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 63607453f775SEmil Constantinescu err_loc[0] = max; 63617453f775SEmil Constantinescu err_loc[1] = maxa; 63627453f775SEmil Constantinescu err_loc[2] = maxr; 6363a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 63647453f775SEmil Constantinescu gmax = err_glb[0]; 63657453f775SEmil Constantinescu gmaxa = err_glb[1]; 63667453f775SEmil Constantinescu gmaxr = err_glb[2]; 63679c6b16b5SShri Abhyankar 63689c6b16b5SShri Abhyankar *norm = gmax; 63697453f775SEmil Constantinescu *norma = gmaxa; 63707453f775SEmil Constantinescu *normr = gmaxr; 63719c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 63727453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 63737453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 63749c6b16b5SShri Abhyankar PetscFunctionReturn(0); 63759c6b16b5SShri Abhyankar } 63769c6b16b5SShri Abhyankar 63771c3436cfSJed Brown /*@ 63788a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 63791c3436cfSJed Brown 63801c3436cfSJed Brown Collective on TS 63811c3436cfSJed Brown 63821c3436cfSJed Brown Input Arguments: 63831c3436cfSJed Brown + ts - time stepping context 6384a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 6385a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 6386a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 63877619abb3SShri 63881c3436cfSJed Brown Output Arguments: 63898a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 63908a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 63918a175baeSEmil Constantinescu . normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 6392a4868fbcSLisandro Dalcin 6393a4868fbcSLisandro Dalcin Options Database Keys: 6394a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 6395a4868fbcSLisandro Dalcin 63961c3436cfSJed Brown Level: developer 63971c3436cfSJed Brown 63988a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm 63991c3436cfSJed Brown @*/ 64007453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 64011c3436cfSJed Brown { 64028beabaa1SBarry Smith PetscErrorCode ierr; 64031c3436cfSJed Brown 64041c3436cfSJed Brown PetscFunctionBegin; 6405a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 64067453f775SEmil Constantinescu ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 6407a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 64087453f775SEmil Constantinescu ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 6409a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 64101c3436cfSJed Brown PetscFunctionReturn(0); 64111c3436cfSJed Brown } 64121c3436cfSJed Brown 64138a175baeSEmil Constantinescu 64148a175baeSEmil Constantinescu /*@ 64158a175baeSEmil Constantinescu TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances 64168a175baeSEmil Constantinescu 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(), TSErrorWeightedENormInfinity() 64338a175baeSEmil Constantinescu @*/ 64348a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(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 PetscInt n_loc,na_loc,nr_loc; 64398a175baeSEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 64408a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 64418a175baeSEmil Constantinescu PetscReal err,sum,suma,sumr,gsum,gsuma,gsumr; 64428a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 64438a175baeSEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 64448a175baeSEmil Constantinescu 64458a175baeSEmil Constantinescu PetscFunctionBegin; 64468a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 64478a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 64488a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 64498a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 64508a175baeSEmil Constantinescu PetscValidType(E,2); 64518a175baeSEmil Constantinescu PetscValidType(U,3); 64528a175baeSEmil Constantinescu PetscValidType(Y,4); 64538a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 64548a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 64558a175baeSEmil Constantinescu PetscValidPointer(norm,5); 64568a175baeSEmil Constantinescu PetscValidPointer(norma,6); 64578a175baeSEmil Constantinescu PetscValidPointer(normr,7); 64588a175baeSEmil Constantinescu 64598a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 64608a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 64618a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 64628a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 64638a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 64648a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 64658a175baeSEmil Constantinescu sum = 0.; n_loc = 0; 64668a175baeSEmil Constantinescu suma = 0.; na_loc = 0; 64678a175baeSEmil Constantinescu sumr = 0.; nr_loc = 0; 64688a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { 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 for (i=0; i<n; i++) { 64738a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 64748a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 64758a175baeSEmil Constantinescu if(tola>0.){ 64768a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 64778a175baeSEmil Constantinescu na_loc++; 64788a175baeSEmil Constantinescu } 64798a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 64808a175baeSEmil Constantinescu if(tolr>0.){ 64818a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 64828a175baeSEmil Constantinescu nr_loc++; 64838a175baeSEmil Constantinescu } 64848a175baeSEmil Constantinescu tol=tola+tolr; 64858a175baeSEmil Constantinescu if(tol>0.){ 64868a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 64878a175baeSEmil Constantinescu n_loc++; 64888a175baeSEmil Constantinescu } 64898a175baeSEmil Constantinescu } 64908a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 64918a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 64928a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 64938a175baeSEmil Constantinescu const PetscScalar *atol; 64948a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 64958a175baeSEmil Constantinescu for (i=0; i<n; i++) { 64968a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 64978a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 64988a175baeSEmil Constantinescu if(tola>0.){ 64998a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 65008a175baeSEmil Constantinescu na_loc++; 65018a175baeSEmil Constantinescu } 65028a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 65038a175baeSEmil Constantinescu if(tolr>0.){ 65048a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 65058a175baeSEmil Constantinescu nr_loc++; 65068a175baeSEmil Constantinescu } 65078a175baeSEmil Constantinescu tol=tola+tolr; 65088a175baeSEmil Constantinescu if(tol>0.){ 65098a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 65108a175baeSEmil Constantinescu n_loc++; 65118a175baeSEmil Constantinescu } 65128a175baeSEmil Constantinescu } 65138a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 65148a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 65158a175baeSEmil Constantinescu const PetscScalar *rtol; 65168a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 65178a175baeSEmil Constantinescu for (i=0; i<n; i++) { 65188a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 65198a175baeSEmil Constantinescu tola = ts->atol; 65208a175baeSEmil Constantinescu if(tola>0.){ 65218a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 65228a175baeSEmil Constantinescu na_loc++; 65238a175baeSEmil Constantinescu } 65248a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 65258a175baeSEmil Constantinescu if(tolr>0.){ 65268a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 65278a175baeSEmil Constantinescu nr_loc++; 65288a175baeSEmil Constantinescu } 65298a175baeSEmil Constantinescu tol=tola+tolr; 65308a175baeSEmil Constantinescu if(tol>0.){ 65318a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 65328a175baeSEmil Constantinescu n_loc++; 65338a175baeSEmil Constantinescu } 65348a175baeSEmil Constantinescu } 65358a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 65368a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 65378a175baeSEmil Constantinescu for (i=0; i<n; i++) { 65388a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 65398a175baeSEmil Constantinescu tola = ts->atol; 65408a175baeSEmil Constantinescu if(tola>0.){ 65418a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 65428a175baeSEmil Constantinescu na_loc++; 65438a175baeSEmil Constantinescu } 65448a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 65458a175baeSEmil Constantinescu if(tolr>0.){ 65468a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 65478a175baeSEmil Constantinescu nr_loc++; 65488a175baeSEmil Constantinescu } 65498a175baeSEmil Constantinescu tol=tola+tolr; 65508a175baeSEmil Constantinescu if(tol>0.){ 65518a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 65528a175baeSEmil Constantinescu n_loc++; 65538a175baeSEmil Constantinescu } 65548a175baeSEmil Constantinescu } 65558a175baeSEmil Constantinescu } 65568a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 65578a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 65588a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 65598a175baeSEmil Constantinescu 65608a175baeSEmil Constantinescu err_loc[0] = sum; 65618a175baeSEmil Constantinescu err_loc[1] = suma; 65628a175baeSEmil Constantinescu err_loc[2] = sumr; 65638a175baeSEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 65648a175baeSEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 65658a175baeSEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 65668a175baeSEmil Constantinescu 6567a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 65688a175baeSEmil Constantinescu 65698a175baeSEmil Constantinescu gsum = err_glb[0]; 65708a175baeSEmil Constantinescu gsuma = err_glb[1]; 65718a175baeSEmil Constantinescu gsumr = err_glb[2]; 65728a175baeSEmil Constantinescu n_glb = err_glb[3]; 65738a175baeSEmil Constantinescu na_glb = err_glb[4]; 65748a175baeSEmil Constantinescu nr_glb = err_glb[5]; 65758a175baeSEmil Constantinescu 65768a175baeSEmil Constantinescu *norm = 0.; 65778a175baeSEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 65788a175baeSEmil Constantinescu *norma = 0.; 65798a175baeSEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 65808a175baeSEmil Constantinescu *normr = 0.; 65818a175baeSEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 65828a175baeSEmil Constantinescu 65838a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 65848a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 65858a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 65868a175baeSEmil Constantinescu PetscFunctionReturn(0); 65878a175baeSEmil Constantinescu } 65888a175baeSEmil Constantinescu 65898a175baeSEmil Constantinescu /*@ 65908a175baeSEmil Constantinescu TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances 65918a175baeSEmil Constantinescu Collective on TS 65928a175baeSEmil Constantinescu 65938a175baeSEmil Constantinescu Input Arguments: 65948a175baeSEmil Constantinescu + ts - time stepping context 65958a175baeSEmil Constantinescu . E - error vector 65968a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 65978a175baeSEmil Constantinescu - Y - state vector, previous time step 65988a175baeSEmil Constantinescu 65998a175baeSEmil Constantinescu Output Arguments: 66008a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 66018a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 66028a175baeSEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 66038a175baeSEmil Constantinescu 66048a175baeSEmil Constantinescu Level: developer 66058a175baeSEmil Constantinescu 66068a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2() 66078a175baeSEmil Constantinescu @*/ 66088a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 66098a175baeSEmil Constantinescu { 66108a175baeSEmil Constantinescu PetscErrorCode ierr; 66118a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 66128a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 66138a175baeSEmil Constantinescu PetscReal err,max,gmax,maxa,gmaxa,maxr,gmaxr; 66148a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 66158a175baeSEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 66168a175baeSEmil Constantinescu 66178a175baeSEmil Constantinescu PetscFunctionBegin; 66188a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 66198a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 66208a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 66218a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 66228a175baeSEmil Constantinescu PetscValidType(E,2); 66238a175baeSEmil Constantinescu PetscValidType(U,3); 66248a175baeSEmil Constantinescu PetscValidType(Y,4); 66258a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 66268a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 66278a175baeSEmil Constantinescu PetscValidPointer(norm,5); 66288a175baeSEmil Constantinescu PetscValidPointer(norma,6); 66298a175baeSEmil Constantinescu PetscValidPointer(normr,7); 66308a175baeSEmil Constantinescu 66318a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 66328a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 66338a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 66348a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 66358a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 66368a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 66378a175baeSEmil Constantinescu 66388a175baeSEmil Constantinescu max=0.; 66398a175baeSEmil Constantinescu maxa=0.; 66408a175baeSEmil Constantinescu maxr=0.; 66418a175baeSEmil Constantinescu 66428a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 66438a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 66448a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 66458a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 66468a175baeSEmil Constantinescu 66478a175baeSEmil Constantinescu for (i=0; i<n; i++) { 66488a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 66498a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 66508a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 66518a175baeSEmil Constantinescu tol = tola+tolr; 66528a175baeSEmil Constantinescu if(tola>0.){ 66538a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 66548a175baeSEmil Constantinescu } 66558a175baeSEmil Constantinescu if(tolr>0.){ 66568a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 66578a175baeSEmil Constantinescu } 66588a175baeSEmil Constantinescu if(tol>0.){ 66598a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 66608a175baeSEmil Constantinescu } 66618a175baeSEmil Constantinescu } 66628a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 66638a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 66648a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 66658a175baeSEmil Constantinescu const PetscScalar *atol; 66668a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 66678a175baeSEmil Constantinescu for (i=0; i<n; i++) { 66688a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 66698a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 66708a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 66718a175baeSEmil Constantinescu tol = tola+tolr; 66728a175baeSEmil Constantinescu if(tola>0.){ 66738a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 66748a175baeSEmil Constantinescu } 66758a175baeSEmil Constantinescu if(tolr>0.){ 66768a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 66778a175baeSEmil Constantinescu } 66788a175baeSEmil Constantinescu if(tol>0.){ 66798a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 66808a175baeSEmil Constantinescu } 66818a175baeSEmil Constantinescu } 66828a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 66838a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 66848a175baeSEmil Constantinescu const PetscScalar *rtol; 66858a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 66868a175baeSEmil Constantinescu 66878a175baeSEmil Constantinescu for (i=0; i<n; i++) { 66888a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 66898a175baeSEmil Constantinescu tola = ts->atol; 66908a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 66918a175baeSEmil Constantinescu tol = tola+tolr; 66928a175baeSEmil Constantinescu if(tola>0.){ 66938a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 66948a175baeSEmil Constantinescu } 66958a175baeSEmil Constantinescu if(tolr>0.){ 66968a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 66978a175baeSEmil Constantinescu } 66988a175baeSEmil Constantinescu if(tol>0.){ 66998a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 67008a175baeSEmil Constantinescu } 67018a175baeSEmil Constantinescu } 67028a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 67038a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 67048a175baeSEmil Constantinescu 67058a175baeSEmil Constantinescu for (i=0; i<n; i++) { 67068a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 67078a175baeSEmil Constantinescu tola = ts->atol; 67088a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 67098a175baeSEmil Constantinescu tol = tola+tolr; 67108a175baeSEmil Constantinescu if(tola>0.){ 67118a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 67128a175baeSEmil Constantinescu } 67138a175baeSEmil Constantinescu if(tolr>0.){ 67148a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 67158a175baeSEmil Constantinescu } 67168a175baeSEmil Constantinescu if(tol>0.){ 67178a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 67188a175baeSEmil Constantinescu } 67198a175baeSEmil Constantinescu } 67208a175baeSEmil Constantinescu } 67218a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 67228a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 67238a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 67248a175baeSEmil Constantinescu err_loc[0] = max; 67258a175baeSEmil Constantinescu err_loc[1] = maxa; 67268a175baeSEmil Constantinescu err_loc[2] = maxr; 6727a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 67288a175baeSEmil Constantinescu gmax = err_glb[0]; 67298a175baeSEmil Constantinescu gmaxa = err_glb[1]; 67308a175baeSEmil Constantinescu gmaxr = err_glb[2]; 67318a175baeSEmil Constantinescu 67328a175baeSEmil Constantinescu *norm = gmax; 67338a175baeSEmil Constantinescu *norma = gmaxa; 67348a175baeSEmil Constantinescu *normr = gmaxr; 67358a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 67368a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 67378a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 67388a175baeSEmil Constantinescu PetscFunctionReturn(0); 67398a175baeSEmil Constantinescu } 67408a175baeSEmil Constantinescu 67418a175baeSEmil Constantinescu /*@ 67428a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 67438a175baeSEmil Constantinescu 67448a175baeSEmil Constantinescu Collective on TS 67458a175baeSEmil Constantinescu 67468a175baeSEmil Constantinescu Input Arguments: 67478a175baeSEmil Constantinescu + ts - time stepping context 67488a175baeSEmil Constantinescu . E - error vector 67498a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 67508a175baeSEmil Constantinescu . Y - state vector, previous time step 67518a175baeSEmil Constantinescu - wnormtype - norm type, either NORM_2 or NORM_INFINITY 67528a175baeSEmil Constantinescu 67538a175baeSEmil Constantinescu Output Arguments: 67548a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 67558a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 67568a175baeSEmil Constantinescu . normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 67578a175baeSEmil Constantinescu 67588a175baeSEmil Constantinescu Options Database Keys: 67598a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 67608a175baeSEmil Constantinescu 67618a175baeSEmil Constantinescu Level: developer 67628a175baeSEmil Constantinescu 67638a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 67648a175baeSEmil Constantinescu @*/ 67658a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 67668a175baeSEmil Constantinescu { 67678a175baeSEmil Constantinescu PetscErrorCode ierr; 67688a175baeSEmil Constantinescu 67698a175baeSEmil Constantinescu PetscFunctionBegin; 67708a175baeSEmil Constantinescu if (wnormtype == NORM_2) { 67718a175baeSEmil Constantinescu ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 67728a175baeSEmil Constantinescu } else if(wnormtype == NORM_INFINITY) { 67738a175baeSEmil Constantinescu ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 67748a175baeSEmil Constantinescu } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 67758a175baeSEmil Constantinescu PetscFunctionReturn(0); 67768a175baeSEmil Constantinescu } 67778a175baeSEmil Constantinescu 67788a175baeSEmil Constantinescu 67798d59e960SJed Brown /*@ 67808d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 67818d59e960SJed Brown 67828d59e960SJed Brown Logically Collective on TS 67838d59e960SJed Brown 67848d59e960SJed Brown Input Arguments: 67858d59e960SJed Brown + ts - time stepping context 67868d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 67878d59e960SJed Brown 67888d59e960SJed Brown Note: 67898d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 67908d59e960SJed Brown 67918d59e960SJed Brown Level: intermediate 67928d59e960SJed Brown 67938d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 67948d59e960SJed Brown @*/ 67958d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 67968d59e960SJed Brown { 67978d59e960SJed Brown PetscFunctionBegin; 67988d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 67998d59e960SJed Brown ts->cfltime_local = cfltime; 68008d59e960SJed Brown ts->cfltime = -1.; 68018d59e960SJed Brown PetscFunctionReturn(0); 68028d59e960SJed Brown } 68038d59e960SJed Brown 68048d59e960SJed Brown /*@ 68058d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 68068d59e960SJed Brown 68078d59e960SJed Brown Collective on TS 68088d59e960SJed Brown 68098d59e960SJed Brown Input Arguments: 68108d59e960SJed Brown . ts - time stepping context 68118d59e960SJed Brown 68128d59e960SJed Brown Output Arguments: 68138d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 68148d59e960SJed Brown 68158d59e960SJed Brown Level: advanced 68168d59e960SJed Brown 68178d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 68188d59e960SJed Brown @*/ 68198d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 68208d59e960SJed Brown { 68218d59e960SJed Brown PetscErrorCode ierr; 68228d59e960SJed Brown 68238d59e960SJed Brown PetscFunctionBegin; 68248d59e960SJed Brown if (ts->cfltime < 0) { 6825b2566f29SBarry Smith ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 68268d59e960SJed Brown } 68278d59e960SJed Brown *cfltime = ts->cfltime; 68288d59e960SJed Brown PetscFunctionReturn(0); 68298d59e960SJed Brown } 68308d59e960SJed Brown 6831d6ebe24aSShri Abhyankar /*@ 6832d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 6833d6ebe24aSShri Abhyankar 6834d6ebe24aSShri Abhyankar Input Parameters: 6835d6ebe24aSShri Abhyankar . ts - the TS context. 6836d6ebe24aSShri Abhyankar . xl - lower bound. 6837d6ebe24aSShri Abhyankar . xu - upper bound. 6838d6ebe24aSShri Abhyankar 6839d6ebe24aSShri Abhyankar Notes: 6840d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 6841e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 6842d6ebe24aSShri Abhyankar 68432bd2b0e6SSatish Balay Level: advanced 68442bd2b0e6SSatish Balay 6845d6ebe24aSShri Abhyankar @*/ 6846d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 6847d6ebe24aSShri Abhyankar { 6848d6ebe24aSShri Abhyankar PetscErrorCode ierr; 6849d6ebe24aSShri Abhyankar SNES snes; 6850d6ebe24aSShri Abhyankar 6851d6ebe24aSShri Abhyankar PetscFunctionBegin; 6852d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 6853d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 6854d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 6855d6ebe24aSShri Abhyankar } 6856d6ebe24aSShri Abhyankar 6857325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 6858c6db04a5SJed Brown #include <mex.h> 6859325fc9f4SBarry Smith 6860325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 6861325fc9f4SBarry Smith 6862325fc9f4SBarry Smith /* 6863325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 6864325fc9f4SBarry Smith TSSetFunctionMatlab(). 6865325fc9f4SBarry Smith 6866325fc9f4SBarry Smith Collective on TS 6867325fc9f4SBarry Smith 6868325fc9f4SBarry Smith Input Parameters: 6869325fc9f4SBarry Smith + snes - the TS context 68700910c330SBarry Smith - u - input vector 6871325fc9f4SBarry Smith 6872325fc9f4SBarry Smith Output Parameter: 6873325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 6874325fc9f4SBarry Smith 6875325fc9f4SBarry Smith Notes: 6876325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 6877325fc9f4SBarry Smith implementations, so most users would not generally call this routine 6878325fc9f4SBarry Smith themselves. 6879325fc9f4SBarry Smith 6880325fc9f4SBarry Smith Level: developer 6881325fc9f4SBarry Smith 6882325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6883325fc9f4SBarry Smith 6884325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6885325fc9f4SBarry Smith */ 68860910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 6887325fc9f4SBarry Smith { 6888325fc9f4SBarry Smith PetscErrorCode ierr; 6889325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6890325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 6891325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 6892325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 6893325fc9f4SBarry Smith 6894325fc9f4SBarry Smith PetscFunctionBegin; 6895325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 68960910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 68970910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 6898325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 68990910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 6900325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 6901325fc9f4SBarry Smith 6902325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 69030910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 69040910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 69050910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 6906bbd56ea5SKarl Rupp 6907325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6908325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 6909325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6910325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6911325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 6912325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 6913325fc9f4SBarry Smith prhs[6] = sctx->ctx; 6914325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 6915325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6916325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6917325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6918325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6919325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6920325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6921325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6922325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6923325fc9f4SBarry Smith PetscFunctionReturn(0); 6924325fc9f4SBarry Smith } 6925325fc9f4SBarry Smith 6926325fc9f4SBarry Smith /* 6927325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 6928325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 6929e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 6930325fc9f4SBarry Smith 6931325fc9f4SBarry Smith Logically Collective on TS 6932325fc9f4SBarry Smith 6933325fc9f4SBarry Smith Input Parameters: 6934325fc9f4SBarry Smith + ts - the TS context 6935325fc9f4SBarry Smith - func - function evaluation routine 6936325fc9f4SBarry Smith 6937325fc9f4SBarry Smith Calling sequence of func: 69380910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 6939325fc9f4SBarry Smith 6940325fc9f4SBarry Smith Level: beginner 6941325fc9f4SBarry Smith 6942325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6943325fc9f4SBarry Smith 6944325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6945325fc9f4SBarry Smith */ 6946cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 6947325fc9f4SBarry Smith { 6948325fc9f4SBarry Smith PetscErrorCode ierr; 6949325fc9f4SBarry Smith TSMatlabContext *sctx; 6950325fc9f4SBarry Smith 6951325fc9f4SBarry Smith PetscFunctionBegin; 6952325fc9f4SBarry Smith /* currently sctx is memory bleed */ 695395dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6954325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6955325fc9f4SBarry Smith /* 6956325fc9f4SBarry Smith This should work, but it doesn't 6957325fc9f4SBarry Smith sctx->ctx = ctx; 6958325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6959325fc9f4SBarry Smith */ 6960325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6961bbd56ea5SKarl Rupp 69620298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 6963325fc9f4SBarry Smith PetscFunctionReturn(0); 6964325fc9f4SBarry Smith } 6965325fc9f4SBarry Smith 6966325fc9f4SBarry Smith /* 6967325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 6968325fc9f4SBarry Smith TSSetJacobianMatlab(). 6969325fc9f4SBarry Smith 6970325fc9f4SBarry Smith Collective on TS 6971325fc9f4SBarry Smith 6972325fc9f4SBarry Smith Input Parameters: 6973cdcf91faSSean Farley + ts - the TS context 69740910c330SBarry Smith . u - input vector 6975325fc9f4SBarry Smith . A, B - the matrices 6976325fc9f4SBarry Smith - ctx - user context 6977325fc9f4SBarry Smith 6978325fc9f4SBarry Smith Level: developer 6979325fc9f4SBarry Smith 6980325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6981325fc9f4SBarry Smith 6982325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6983325fc9f4SBarry Smith @*/ 6984f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 6985325fc9f4SBarry Smith { 6986325fc9f4SBarry Smith PetscErrorCode ierr; 6987325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6988325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 6989325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 6990325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 6991325fc9f4SBarry Smith 6992325fc9f4SBarry Smith PetscFunctionBegin; 6993cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 69940910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 6995325fc9f4SBarry Smith 69960910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 6997325fc9f4SBarry Smith 6998cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 69990910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 70000910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 70010910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 70020910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 7003bbd56ea5SKarl Rupp 7004325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 7005325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 7006325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 7007325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 7008325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 7009325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 7010325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 7011325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 7012325fc9f4SBarry Smith prhs[8] = sctx->ctx; 7013325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 7014325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 7015325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 7016325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 7017325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 7018325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 7019325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 7020325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 7021325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 7022325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 7023325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 7024325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 7025325fc9f4SBarry Smith PetscFunctionReturn(0); 7026325fc9f4SBarry Smith } 7027325fc9f4SBarry Smith 7028325fc9f4SBarry Smith /* 7029325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 7030e3c5b3baSBarry 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 7031325fc9f4SBarry Smith 7032325fc9f4SBarry Smith Logically Collective on TS 7033325fc9f4SBarry Smith 7034325fc9f4SBarry Smith Input Parameters: 7035cdcf91faSSean Farley + ts - the TS context 7036325fc9f4SBarry Smith . A,B - Jacobian matrices 7037325fc9f4SBarry Smith . func - function evaluation routine 7038325fc9f4SBarry Smith - ctx - user context 7039325fc9f4SBarry Smith 7040325fc9f4SBarry Smith Calling sequence of func: 70410910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 7042325fc9f4SBarry Smith 7043325fc9f4SBarry Smith Level: developer 7044325fc9f4SBarry Smith 7045325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 7046325fc9f4SBarry Smith 7047325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 7048325fc9f4SBarry Smith */ 7049cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 7050325fc9f4SBarry Smith { 7051325fc9f4SBarry Smith PetscErrorCode ierr; 7052325fc9f4SBarry Smith TSMatlabContext *sctx; 7053325fc9f4SBarry Smith 7054325fc9f4SBarry Smith PetscFunctionBegin; 7055325fc9f4SBarry Smith /* currently sctx is memory bleed */ 705695dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 7057325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 7058325fc9f4SBarry Smith /* 7059325fc9f4SBarry Smith This should work, but it doesn't 7060325fc9f4SBarry Smith sctx->ctx = ctx; 7061325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 7062325fc9f4SBarry Smith */ 7063325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 7064bbd56ea5SKarl Rupp 7065cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 7066325fc9f4SBarry Smith PetscFunctionReturn(0); 7067325fc9f4SBarry Smith } 7068325fc9f4SBarry Smith 7069b5b1a830SBarry Smith /* 7070b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 7071b5b1a830SBarry Smith 7072b5b1a830SBarry Smith Collective on TS 7073b5b1a830SBarry Smith 7074b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 7075b5b1a830SBarry Smith @*/ 70760910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 7077b5b1a830SBarry Smith { 7078b5b1a830SBarry Smith PetscErrorCode ierr; 7079b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 7080a530c242SBarry Smith int nlhs = 1,nrhs = 6; 7081b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 7082b5b1a830SBarry Smith long long int lx = 0,ls = 0; 7083b5b1a830SBarry Smith 7084b5b1a830SBarry Smith PetscFunctionBegin; 7085cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 70860910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 7087b5b1a830SBarry Smith 7088cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 70890910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 7090bbd56ea5SKarl Rupp 7091b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 7092b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 7093b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 7094b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 7095b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 7096b5b1a830SBarry Smith prhs[5] = sctx->ctx; 7097b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 7098b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 7099b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 7100b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 7101b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 7102b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 7103b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 7104b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 7105b5b1a830SBarry Smith PetscFunctionReturn(0); 7106b5b1a830SBarry Smith } 7107b5b1a830SBarry Smith 7108b5b1a830SBarry Smith /* 7109b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 7110b5b1a830SBarry Smith 7111b5b1a830SBarry Smith Level: developer 7112b5b1a830SBarry Smith 7113b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 7114b5b1a830SBarry Smith 7115b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 7116b5b1a830SBarry Smith */ 7117cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 7118b5b1a830SBarry Smith { 7119b5b1a830SBarry Smith PetscErrorCode ierr; 7120b5b1a830SBarry Smith TSMatlabContext *sctx; 7121b5b1a830SBarry Smith 7122b5b1a830SBarry Smith PetscFunctionBegin; 7123b5b1a830SBarry Smith /* currently sctx is memory bleed */ 712495dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 7125b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 7126b5b1a830SBarry Smith /* 7127b5b1a830SBarry Smith This should work, but it doesn't 7128b5b1a830SBarry Smith sctx->ctx = ctx; 7129b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 7130b5b1a830SBarry Smith */ 7131b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 7132bbd56ea5SKarl Rupp 71330298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 7134b5b1a830SBarry Smith PetscFunctionReturn(0); 7135b5b1a830SBarry Smith } 7136325fc9f4SBarry Smith #endif 7137b3603a34SBarry Smith 7138b3603a34SBarry Smith /*@C 71394f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 7140b3603a34SBarry Smith in a time based line graph 7141b3603a34SBarry Smith 7142b3603a34SBarry Smith Collective on TS 7143b3603a34SBarry Smith 7144b3603a34SBarry Smith Input Parameters: 7145b3603a34SBarry Smith + ts - the TS context 7146b3603a34SBarry Smith . step - current time-step 7147b3603a34SBarry Smith . ptime - current time 71487db568b7SBarry Smith . u - current solution 71497db568b7SBarry Smith - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 7150b3603a34SBarry Smith 7151b3d3934dSBarry Smith Options Database: 71529ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 7153b3d3934dSBarry Smith 7154b3603a34SBarry Smith Level: intermediate 7155b3603a34SBarry Smith 71566934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component solutions in a separate window 7157b3603a34SBarry Smith 7158b3603a34SBarry Smith .keywords: TS, vector, monitor, view 7159b3603a34SBarry Smith 71607db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 71617db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 71627db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 71637db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 7164b3603a34SBarry Smith @*/ 71657db568b7SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 7166b3603a34SBarry Smith { 7167b3603a34SBarry Smith PetscErrorCode ierr; 71687db568b7SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 7169b3603a34SBarry Smith const PetscScalar *yy; 717080666b62SBarry Smith Vec v; 7171b3603a34SBarry Smith 7172b3603a34SBarry Smith PetscFunctionBegin; 717363e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 717458ff32f7SBarry Smith if (!step) { 7175a9f9c1f6SBarry Smith PetscDrawAxis axis; 71766934998bSLisandro Dalcin PetscInt dim; 7177a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 7178a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 7179bab0a581SBarry Smith if (!ctx->names) { 7180bab0a581SBarry Smith PetscBool flg; 7181bab0a581SBarry Smith /* user provides names of variables to plot but no names has been set so assume names are integer values */ 7182bab0a581SBarry Smith ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr); 7183bab0a581SBarry Smith if (flg) { 7184bab0a581SBarry Smith PetscInt i,n; 7185bab0a581SBarry Smith char **names; 7186bab0a581SBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 7187bab0a581SBarry Smith ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr); 7188bab0a581SBarry Smith for (i=0; i<n; i++) { 7189bab0a581SBarry Smith ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr); 7190bab0a581SBarry Smith ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr); 7191bab0a581SBarry Smith } 7192bab0a581SBarry Smith names[n] = NULL; 7193bab0a581SBarry Smith ctx->names = names; 7194bab0a581SBarry Smith } 7195bab0a581SBarry Smith } 7196387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 7197387f4636SBarry Smith char **displaynames; 7198387f4636SBarry Smith PetscBool flg; 7199387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 720095dccacaSBarry Smith ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr); 7201387f4636SBarry Smith ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 7202c5929fdfSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 7203387f4636SBarry Smith if (flg) { 7204a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 7205387f4636SBarry Smith } 7206387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 7207387f4636SBarry Smith } 7208387f4636SBarry Smith if (ctx->displaynames) { 7209387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 7210387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 7211387f4636SBarry Smith } else if (ctx->names) { 72120910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 72130b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 7214387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 7215b0bc92c6SBarry Smith } else { 7216b0bc92c6SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 7217b0bc92c6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 7218387f4636SBarry Smith } 72190b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 722058ff32f7SBarry Smith } 72216934998bSLisandro Dalcin 72226934998bSLisandro Dalcin if (!ctx->transform) v = u; 72236934998bSLisandro Dalcin else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);} 722480666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 72256934998bSLisandro Dalcin if (ctx->displaynames) { 72266934998bSLisandro Dalcin PetscInt i; 72276934998bSLisandro Dalcin for (i=0; i<ctx->ndisplayvariables; i++) 72286934998bSLisandro Dalcin ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]); 72296934998bSLisandro Dalcin ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 72306934998bSLisandro Dalcin } else { 7231e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 7232e3efe391SJed Brown PetscInt i,n; 72336934998bSLisandro Dalcin PetscReal *yreal; 723480666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 7235785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 7236e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 72370b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 7238e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 7239e3efe391SJed Brown #else 72400b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 7241e3efe391SJed Brown #endif 724280666b62SBarry Smith } 72436934998bSLisandro Dalcin ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 72446934998bSLisandro Dalcin if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);} 72456934998bSLisandro Dalcin 7246b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 72470b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 72486934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 72493923b477SBarry Smith } 7250b3603a34SBarry Smith PetscFunctionReturn(0); 7251b3603a34SBarry Smith } 7252b3603a34SBarry Smith 7253b037adc7SBarry Smith /*@C 725431152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 7255b037adc7SBarry Smith 7256b037adc7SBarry Smith Collective on TS 7257b037adc7SBarry Smith 7258b037adc7SBarry Smith Input Parameters: 7259b037adc7SBarry Smith + ts - the TS context 7260b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 7261b037adc7SBarry Smith 7262b037adc7SBarry Smith Level: intermediate 7263b037adc7SBarry Smith 72647db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 72657db568b7SBarry Smith 7266b037adc7SBarry Smith .keywords: TS, vector, monitor, view 7267b037adc7SBarry Smith 7268a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 7269b037adc7SBarry Smith @*/ 727031152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 7271b037adc7SBarry Smith { 7272b037adc7SBarry Smith PetscErrorCode ierr; 7273b037adc7SBarry Smith PetscInt i; 7274b037adc7SBarry Smith 7275b037adc7SBarry Smith PetscFunctionBegin; 7276b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7277b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 72785537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 7279b3d3934dSBarry Smith break; 7280b3d3934dSBarry Smith } 7281b3d3934dSBarry Smith } 7282b3d3934dSBarry Smith PetscFunctionReturn(0); 7283b3d3934dSBarry Smith } 7284b3d3934dSBarry Smith 7285e673d494SBarry Smith /*@C 7286e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 7287e673d494SBarry Smith 7288e673d494SBarry Smith Collective on TS 7289e673d494SBarry Smith 7290e673d494SBarry Smith Input Parameters: 7291e673d494SBarry Smith + ts - the TS context 7292e673d494SBarry Smith - names - the names of the components, final string must be NULL 7293e673d494SBarry Smith 7294e673d494SBarry Smith Level: intermediate 7295e673d494SBarry Smith 7296e673d494SBarry Smith .keywords: TS, vector, monitor, view 7297e673d494SBarry Smith 7298a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 7299e673d494SBarry Smith @*/ 7300e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 7301e673d494SBarry Smith { 7302e673d494SBarry Smith PetscErrorCode ierr; 7303e673d494SBarry Smith 7304e673d494SBarry Smith PetscFunctionBegin; 7305e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 7306e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 7307e673d494SBarry Smith PetscFunctionReturn(0); 7308e673d494SBarry Smith } 7309e673d494SBarry Smith 7310b3d3934dSBarry Smith /*@C 7311b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 7312b3d3934dSBarry Smith 7313b3d3934dSBarry Smith Collective on TS 7314b3d3934dSBarry Smith 7315b3d3934dSBarry Smith Input Parameter: 7316b3d3934dSBarry Smith . ts - the TS context 7317b3d3934dSBarry Smith 7318b3d3934dSBarry Smith Output Parameter: 7319b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 7320b3d3934dSBarry Smith 7321b3d3934dSBarry Smith Level: intermediate 7322b3d3934dSBarry Smith 73237db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 73247db568b7SBarry Smith 7325b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7326b3d3934dSBarry Smith 7327b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 7328b3d3934dSBarry Smith @*/ 7329b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 7330b3d3934dSBarry Smith { 7331b3d3934dSBarry Smith PetscInt i; 7332b3d3934dSBarry Smith 7333b3d3934dSBarry Smith PetscFunctionBegin; 7334b3d3934dSBarry Smith *names = NULL; 7335b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7336b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 73375537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 7338b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 7339b3d3934dSBarry Smith break; 7340387f4636SBarry Smith } 7341387f4636SBarry Smith } 7342387f4636SBarry Smith PetscFunctionReturn(0); 7343387f4636SBarry Smith } 7344387f4636SBarry Smith 7345a66092f1SBarry Smith /*@C 7346a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 7347a66092f1SBarry Smith 7348a66092f1SBarry Smith Collective on TS 7349a66092f1SBarry Smith 7350a66092f1SBarry Smith Input Parameters: 7351a66092f1SBarry Smith + ctx - the TSMonitorLG context 7352a66092f1SBarry Smith . displaynames - the names of the components, final string must be NULL 7353a66092f1SBarry Smith 7354a66092f1SBarry Smith Level: intermediate 7355a66092f1SBarry Smith 7356a66092f1SBarry Smith .keywords: TS, vector, monitor, view 7357a66092f1SBarry Smith 7358a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 7359a66092f1SBarry Smith @*/ 7360a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 7361a66092f1SBarry Smith { 7362a66092f1SBarry Smith PetscInt j = 0,k; 7363a66092f1SBarry Smith PetscErrorCode ierr; 7364a66092f1SBarry Smith 7365a66092f1SBarry Smith PetscFunctionBegin; 7366a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 7367a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 7368a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 7369a66092f1SBarry Smith while (displaynames[j]) j++; 7370a66092f1SBarry Smith ctx->ndisplayvariables = j; 7371a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 7372a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 7373a66092f1SBarry Smith j = 0; 7374a66092f1SBarry Smith while (displaynames[j]) { 7375a66092f1SBarry Smith k = 0; 7376a66092f1SBarry Smith while (ctx->names[k]) { 7377a66092f1SBarry Smith PetscBool flg; 7378a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 7379a66092f1SBarry Smith if (flg) { 7380a66092f1SBarry Smith ctx->displayvariables[j] = k; 7381a66092f1SBarry Smith break; 7382a66092f1SBarry Smith } 7383a66092f1SBarry Smith k++; 7384a66092f1SBarry Smith } 7385a66092f1SBarry Smith j++; 7386a66092f1SBarry Smith } 7387a66092f1SBarry Smith PetscFunctionReturn(0); 7388a66092f1SBarry Smith } 7389a66092f1SBarry Smith 7390387f4636SBarry Smith /*@C 7391387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 7392387f4636SBarry Smith 7393387f4636SBarry Smith Collective on TS 7394387f4636SBarry Smith 7395387f4636SBarry Smith Input Parameters: 7396387f4636SBarry Smith + ts - the TS context 7397387f4636SBarry Smith . displaynames - the names of the components, final string must be NULL 7398387f4636SBarry Smith 73997db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 74007db568b7SBarry Smith 7401387f4636SBarry Smith Level: intermediate 7402387f4636SBarry Smith 7403387f4636SBarry Smith .keywords: TS, vector, monitor, view 7404387f4636SBarry Smith 7405387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 7406387f4636SBarry Smith @*/ 7407387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 7408387f4636SBarry Smith { 7409a66092f1SBarry Smith PetscInt i; 7410387f4636SBarry Smith PetscErrorCode ierr; 7411387f4636SBarry Smith 7412387f4636SBarry Smith PetscFunctionBegin; 7413387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7414387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 74155537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 7416b3d3934dSBarry Smith break; 7417b037adc7SBarry Smith } 7418b037adc7SBarry Smith } 7419b037adc7SBarry Smith PetscFunctionReturn(0); 7420b037adc7SBarry Smith } 7421b037adc7SBarry Smith 742280666b62SBarry Smith /*@C 742380666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 742480666b62SBarry Smith 742580666b62SBarry Smith Collective on TS 742680666b62SBarry Smith 742780666b62SBarry Smith Input Parameters: 742880666b62SBarry Smith + ts - the TS context 742980666b62SBarry Smith . transform - the transform function 74307684fa3eSBarry Smith . destroy - function to destroy the optional context 743180666b62SBarry Smith - ctx - optional context used by transform function 743280666b62SBarry Smith 74337db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 74347db568b7SBarry Smith 743580666b62SBarry Smith Level: intermediate 743680666b62SBarry Smith 743780666b62SBarry Smith .keywords: TS, vector, monitor, view 743880666b62SBarry Smith 7439a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 744080666b62SBarry Smith @*/ 74417684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 744280666b62SBarry Smith { 744380666b62SBarry Smith PetscInt i; 7444a66092f1SBarry Smith PetscErrorCode ierr; 744580666b62SBarry Smith 744680666b62SBarry Smith PetscFunctionBegin; 744780666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 744880666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 74495537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 745080666b62SBarry Smith } 745180666b62SBarry Smith } 745280666b62SBarry Smith PetscFunctionReturn(0); 745380666b62SBarry Smith } 745480666b62SBarry Smith 7455e673d494SBarry Smith /*@C 7456e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 7457e673d494SBarry Smith 7458e673d494SBarry Smith Collective on TSLGCtx 7459e673d494SBarry Smith 7460e673d494SBarry Smith Input Parameters: 7461e673d494SBarry Smith + ts - the TS context 7462e673d494SBarry Smith . transform - the transform function 74637684fa3eSBarry Smith . destroy - function to destroy the optional context 7464e673d494SBarry Smith - ctx - optional context used by transform function 7465e673d494SBarry Smith 7466e673d494SBarry Smith Level: intermediate 7467e673d494SBarry Smith 7468e673d494SBarry Smith .keywords: TS, vector, monitor, view 7469e673d494SBarry Smith 7470a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 7471e673d494SBarry Smith @*/ 74727684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 7473e673d494SBarry Smith { 7474e673d494SBarry Smith PetscFunctionBegin; 7475e673d494SBarry Smith ctx->transform = transform; 74767684fa3eSBarry Smith ctx->transformdestroy = destroy; 7477e673d494SBarry Smith ctx->transformctx = tctx; 7478e673d494SBarry Smith PetscFunctionReturn(0); 7479e673d494SBarry Smith } 7480e673d494SBarry Smith 7481ef20d060SBarry Smith /*@C 74828b668821SLisandro Dalcin TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error 7483ef20d060SBarry Smith in a time based line graph 7484ef20d060SBarry Smith 7485ef20d060SBarry Smith Collective on TS 7486ef20d060SBarry Smith 7487ef20d060SBarry Smith Input Parameters: 7488ef20d060SBarry Smith + ts - the TS context 7489ef20d060SBarry Smith . step - current time-step 7490ef20d060SBarry Smith . ptime - current time 74917db568b7SBarry Smith . u - current solution 74927db568b7SBarry Smith - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 7493ef20d060SBarry Smith 7494ef20d060SBarry Smith Level: intermediate 7495ef20d060SBarry Smith 74966934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component errors in a separate window 7497abd5a294SJed Brown 7498abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 7499abd5a294SJed Brown 7500abd5a294SJed Brown Options Database Keys: 75014f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 7502ef20d060SBarry Smith 7503ef20d060SBarry Smith .keywords: TS, vector, monitor, view 7504ef20d060SBarry Smith 7505abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 7506ef20d060SBarry Smith @*/ 75070910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 7508ef20d060SBarry Smith { 7509ef20d060SBarry Smith PetscErrorCode ierr; 75100b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 7511ef20d060SBarry Smith const PetscScalar *yy; 7512ef20d060SBarry Smith Vec y; 7513ef20d060SBarry Smith 7514ef20d060SBarry Smith PetscFunctionBegin; 7515a9f9c1f6SBarry Smith if (!step) { 7516a9f9c1f6SBarry Smith PetscDrawAxis axis; 75176934998bSLisandro Dalcin PetscInt dim; 7518a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 75198b668821SLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr); 75200910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 7521a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 7522a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 7523a9f9c1f6SBarry Smith } 75240910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 7525ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 75260910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 7527ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 7528e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 7529e3efe391SJed Brown { 7530e3efe391SJed Brown PetscReal *yreal; 7531e3efe391SJed Brown PetscInt i,n; 7532e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 7533785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 7534e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 75350b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 7536e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 7537e3efe391SJed Brown } 7538e3efe391SJed Brown #else 75390b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 7540e3efe391SJed Brown #endif 7541ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 7542ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 7543b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 75440b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 75456934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 75463923b477SBarry Smith } 7547ef20d060SBarry Smith PetscFunctionReturn(0); 7548ef20d060SBarry Smith } 7549ef20d060SBarry Smith 7550201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 7551201da799SBarry Smith { 7552201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 7553201da799SBarry Smith PetscReal x = ptime,y; 7554201da799SBarry Smith PetscErrorCode ierr; 7555201da799SBarry Smith PetscInt its; 7556201da799SBarry Smith 7557201da799SBarry Smith PetscFunctionBegin; 755863e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 7559201da799SBarry Smith if (!n) { 7560201da799SBarry Smith PetscDrawAxis axis; 7561201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 7562201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 7563201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 7564201da799SBarry Smith ctx->snes_its = 0; 7565201da799SBarry Smith } 7566201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 7567201da799SBarry Smith y = its - ctx->snes_its; 7568201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 75693fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 7570201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 75716934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 7572201da799SBarry Smith } 7573201da799SBarry Smith ctx->snes_its = its; 7574201da799SBarry Smith PetscFunctionReturn(0); 7575201da799SBarry Smith } 7576201da799SBarry Smith 7577201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 7578201da799SBarry Smith { 7579201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 7580201da799SBarry Smith PetscReal x = ptime,y; 7581201da799SBarry Smith PetscErrorCode ierr; 7582201da799SBarry Smith PetscInt its; 7583201da799SBarry Smith 7584201da799SBarry Smith PetscFunctionBegin; 758563e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 7586201da799SBarry Smith if (!n) { 7587201da799SBarry Smith PetscDrawAxis axis; 7588201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 7589201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 7590201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 7591201da799SBarry Smith ctx->ksp_its = 0; 7592201da799SBarry Smith } 7593201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 7594201da799SBarry Smith y = its - ctx->ksp_its; 7595201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 759699fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 7597201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 75986934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 7599201da799SBarry Smith } 7600201da799SBarry Smith ctx->ksp_its = its; 7601201da799SBarry Smith PetscFunctionReturn(0); 7602201da799SBarry Smith } 7603f9c1d6abSBarry Smith 7604f9c1d6abSBarry Smith /*@ 7605f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 7606f9c1d6abSBarry Smith 7607f9c1d6abSBarry Smith Collective on TS and Vec 7608f9c1d6abSBarry Smith 7609f9c1d6abSBarry Smith Input Parameters: 7610f9c1d6abSBarry Smith + ts - the TS context 7611f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 7612f9c1d6abSBarry Smith 7613f9c1d6abSBarry Smith Output Parameters: 7614f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 7615f9c1d6abSBarry Smith 7616f9c1d6abSBarry Smith Level: developer 7617f9c1d6abSBarry Smith 7618f9c1d6abSBarry Smith .keywords: TS, compute 7619f9c1d6abSBarry Smith 7620f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 7621f9c1d6abSBarry Smith @*/ 7622f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 7623f9c1d6abSBarry Smith { 7624f9c1d6abSBarry Smith PetscErrorCode ierr; 7625f9c1d6abSBarry Smith 7626f9c1d6abSBarry Smith PetscFunctionBegin; 7627f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7628ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 7629f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 7630f9c1d6abSBarry Smith PetscFunctionReturn(0); 7631f9c1d6abSBarry Smith } 763224655328SShri 7633b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 7634b3d3934dSBarry Smith /*@C 7635b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 7636b3d3934dSBarry Smith 7637b3d3934dSBarry Smith Collective on TS 7638b3d3934dSBarry Smith 7639b3d3934dSBarry Smith Input Parameters: 7640b3d3934dSBarry Smith . ts - the ODE solver object 7641b3d3934dSBarry Smith 7642b3d3934dSBarry Smith Output Parameter: 7643b3d3934dSBarry Smith . ctx - the context 7644b3d3934dSBarry Smith 7645b3d3934dSBarry Smith Level: intermediate 7646b3d3934dSBarry Smith 7647b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso 7648b3d3934dSBarry Smith 7649b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 7650b3d3934dSBarry Smith 7651b3d3934dSBarry Smith @*/ 7652b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 7653b3d3934dSBarry Smith { 7654b3d3934dSBarry Smith PetscErrorCode ierr; 7655b3d3934dSBarry Smith 7656b3d3934dSBarry Smith PetscFunctionBegin; 7657a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 7658b3d3934dSBarry Smith PetscFunctionReturn(0); 7659b3d3934dSBarry Smith } 7660b3d3934dSBarry Smith 7661b3d3934dSBarry Smith /*@C 7662b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 7663b3d3934dSBarry Smith 7664b3d3934dSBarry Smith Collective on TS 7665b3d3934dSBarry Smith 7666b3d3934dSBarry Smith Input Parameters: 7667b3d3934dSBarry Smith + ts - the TS context 7668b3d3934dSBarry Smith . step - current time-step 7669b3d3934dSBarry Smith . ptime - current time 76707db568b7SBarry Smith . u - current solution 76717db568b7SBarry Smith - dctx - the envelope context 7672b3d3934dSBarry Smith 7673b3d3934dSBarry Smith Options Database: 7674b3d3934dSBarry Smith . -ts_monitor_envelope 7675b3d3934dSBarry Smith 7676b3d3934dSBarry Smith Level: intermediate 7677b3d3934dSBarry Smith 7678b3d3934dSBarry Smith Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 7679b3d3934dSBarry Smith 7680b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7681b3d3934dSBarry Smith 76827db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 7683b3d3934dSBarry Smith @*/ 76847db568b7SBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 7685b3d3934dSBarry Smith { 7686b3d3934dSBarry Smith PetscErrorCode ierr; 76877db568b7SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 7688b3d3934dSBarry Smith 7689b3d3934dSBarry Smith PetscFunctionBegin; 7690b3d3934dSBarry Smith if (!ctx->max) { 7691b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 7692b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 7693b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 7694b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 7695b3d3934dSBarry Smith } else { 7696b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 7697b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 7698b3d3934dSBarry Smith } 7699b3d3934dSBarry Smith PetscFunctionReturn(0); 7700b3d3934dSBarry Smith } 7701b3d3934dSBarry Smith 7702b3d3934dSBarry Smith /*@C 7703b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 7704b3d3934dSBarry Smith 7705b3d3934dSBarry Smith Collective on TS 7706b3d3934dSBarry Smith 7707b3d3934dSBarry Smith Input Parameter: 7708b3d3934dSBarry Smith . ts - the TS context 7709b3d3934dSBarry Smith 7710b3d3934dSBarry Smith Output Parameter: 7711b3d3934dSBarry Smith + max - the maximum values 7712b3d3934dSBarry Smith - min - the minimum values 7713b3d3934dSBarry Smith 77147db568b7SBarry Smith Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 77157db568b7SBarry Smith 7716b3d3934dSBarry Smith Level: intermediate 7717b3d3934dSBarry Smith 7718b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7719b3d3934dSBarry Smith 7720b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 7721b3d3934dSBarry Smith @*/ 7722b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 7723b3d3934dSBarry Smith { 7724b3d3934dSBarry Smith PetscInt i; 7725b3d3934dSBarry Smith 7726b3d3934dSBarry Smith PetscFunctionBegin; 7727b3d3934dSBarry Smith if (max) *max = NULL; 7728b3d3934dSBarry Smith if (min) *min = NULL; 7729b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7730b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 77315537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 7732b3d3934dSBarry Smith if (max) *max = ctx->max; 7733b3d3934dSBarry Smith if (min) *min = ctx->min; 7734b3d3934dSBarry Smith break; 7735b3d3934dSBarry Smith } 7736b3d3934dSBarry Smith } 7737b3d3934dSBarry Smith PetscFunctionReturn(0); 7738b3d3934dSBarry Smith } 7739b3d3934dSBarry Smith 7740b3d3934dSBarry Smith /*@C 7741b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 7742b3d3934dSBarry Smith 7743b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 7744b3d3934dSBarry Smith 7745b3d3934dSBarry Smith Input Parameter: 7746b3d3934dSBarry Smith . ctx - the monitor context 7747b3d3934dSBarry Smith 7748b3d3934dSBarry Smith Level: intermediate 7749b3d3934dSBarry Smith 7750b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy 7751b3d3934dSBarry Smith 77527db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 7753b3d3934dSBarry Smith @*/ 7754b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 7755b3d3934dSBarry Smith { 7756b3d3934dSBarry Smith PetscErrorCode ierr; 7757b3d3934dSBarry Smith 7758b3d3934dSBarry Smith PetscFunctionBegin; 7759b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 7760b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 7761b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 7762b3d3934dSBarry Smith PetscFunctionReturn(0); 7763b3d3934dSBarry Smith } 7764f2dee214SBarry Smith 776524655328SShri /*@ 7766dcb233daSLisandro Dalcin TSRestartStep - Flags the solver to restart the next step 7767dcb233daSLisandro Dalcin 7768dcb233daSLisandro Dalcin Collective on TS 7769dcb233daSLisandro Dalcin 7770dcb233daSLisandro Dalcin Input Parameter: 7771dcb233daSLisandro Dalcin . ts - the TS context obtained from TSCreate() 7772dcb233daSLisandro Dalcin 7773dcb233daSLisandro Dalcin Level: advanced 7774dcb233daSLisandro Dalcin 7775dcb233daSLisandro Dalcin Notes: 7776dcb233daSLisandro Dalcin Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of 7777dcb233daSLisandro Dalcin discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution 7778dcb233daSLisandro Dalcin vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For 7779dcb233daSLisandro Dalcin the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce 7780dcb233daSLisandro Dalcin discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with 7781dcb233daSLisandro Dalcin discontinuous source terms). 7782dcb233daSLisandro Dalcin 7783dcb233daSLisandro Dalcin .keywords: TS, timestep, restart 7784dcb233daSLisandro Dalcin 7785dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep() 7786dcb233daSLisandro Dalcin @*/ 7787dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts) 7788dcb233daSLisandro Dalcin { 7789dcb233daSLisandro Dalcin PetscFunctionBegin; 7790dcb233daSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7791dcb233daSLisandro Dalcin ts->steprestart = PETSC_TRUE; 7792dcb233daSLisandro Dalcin PetscFunctionReturn(0); 7793dcb233daSLisandro Dalcin } 7794dcb233daSLisandro Dalcin 7795dcb233daSLisandro Dalcin /*@ 779624655328SShri TSRollBack - Rolls back one time step 779724655328SShri 779824655328SShri Collective on TS 779924655328SShri 780024655328SShri Input Parameter: 780124655328SShri . ts - the TS context obtained from TSCreate() 780224655328SShri 780324655328SShri Level: advanced 780424655328SShri 780524655328SShri .keywords: TS, timestep, rollback 780624655328SShri 780724655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 780824655328SShri @*/ 780924655328SShri PetscErrorCode TSRollBack(TS ts) 781024655328SShri { 781124655328SShri PetscErrorCode ierr; 781224655328SShri 781324655328SShri PetscFunctionBegin; 781424655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7815b3de5cdeSLisandro Dalcin if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called"); 781624655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 781724655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 781824655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 781924655328SShri ts->ptime = ts->ptime_prev; 7820be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 78212808aa04SLisandro Dalcin ts->steps--; 7822b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 782324655328SShri PetscFunctionReturn(0); 782424655328SShri } 7825aeb4809dSShri Abhyankar 7826ff22ae23SHong Zhang /*@ 7827ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 7828ff22ae23SHong Zhang 7829ff22ae23SHong Zhang Input Parameter: 7830ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 7831ff22ae23SHong Zhang 7832ff22ae23SHong Zhang Level: advanced 7833ff22ae23SHong Zhang 7834ff22ae23SHong Zhang .keywords: TS, getstages 7835ff22ae23SHong Zhang 7836ff22ae23SHong Zhang .seealso: TSCreate() 7837ff22ae23SHong Zhang @*/ 7838ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns,Vec **Y) 7839ff22ae23SHong Zhang { 7840ff22ae23SHong Zhang PetscErrorCode ierr; 7841ff22ae23SHong Zhang 7842ff22ae23SHong Zhang PetscFunctionBegin; 7843ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7844ff22ae23SHong Zhang PetscValidPointer(ns,2); 7845ff22ae23SHong Zhang 7846ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 7847ff22ae23SHong Zhang else { 7848ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 7849ff22ae23SHong Zhang } 7850ff22ae23SHong Zhang PetscFunctionReturn(0); 7851ff22ae23SHong Zhang } 7852ff22ae23SHong Zhang 7853847ff0e1SMatthew G. Knepley /*@C 7854847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 7855847ff0e1SMatthew G. Knepley 7856847ff0e1SMatthew G. Knepley Collective on SNES 7857847ff0e1SMatthew G. Knepley 7858847ff0e1SMatthew G. Knepley Input Parameters: 7859847ff0e1SMatthew G. Knepley + ts - the TS context 7860847ff0e1SMatthew G. Knepley . t - current timestep 7861847ff0e1SMatthew G. Knepley . U - state vector 7862847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 7863847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 7864847ff0e1SMatthew G. Knepley - ctx - an optional user context 7865847ff0e1SMatthew G. Knepley 7866847ff0e1SMatthew G. Knepley Output Parameters: 7867847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 7868847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 7869847ff0e1SMatthew G. Knepley 7870847ff0e1SMatthew G. Knepley Level: intermediate 7871847ff0e1SMatthew G. Knepley 7872847ff0e1SMatthew G. Knepley Notes: 7873847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 7874847ff0e1SMatthew G. Knepley 7875847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 7876847ff0e1SMatthew G. Knepley 7877847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 7878847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 7879847ff0e1SMatthew G. Knepley 7880847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 7881847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 7882847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 7883847ff0e1SMatthew G. Knepley 7884847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse 7885847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 7886847ff0e1SMatthew G. Knepley @*/ 7887847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 7888847ff0e1SMatthew G. Knepley { 7889847ff0e1SMatthew G. Knepley SNES snes; 7890847ff0e1SMatthew G. Knepley MatFDColoring color; 7891847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 7892847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 7893847ff0e1SMatthew G. Knepley 7894847ff0e1SMatthew G. Knepley PetscFunctionBegin; 7895c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 7896847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 7897847ff0e1SMatthew G. Knepley if (!color) { 7898847ff0e1SMatthew G. Knepley DM dm; 7899847ff0e1SMatthew G. Knepley ISColoring iscoloring; 7900847ff0e1SMatthew G. Knepley 7901847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 7902847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 7903847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 7904847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 7905847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7906847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7907847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7908847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7909847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7910847ff0e1SMatthew G. Knepley } else { 7911847ff0e1SMatthew G. Knepley MatColoring mc; 7912847ff0e1SMatthew G. Knepley 7913847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 7914847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 7915847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 7916847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 7917847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 7918847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 7919847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7920847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7921847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7922847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7923847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7924847ff0e1SMatthew G. Knepley } 7925847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 7926847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 7927847ff0e1SMatthew G. Knepley } 7928847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 7929847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 7930847ff0e1SMatthew G. Knepley if (J != B) { 7931847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7932847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7933847ff0e1SMatthew G. Knepley } 7934847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 7935847ff0e1SMatthew G. Knepley } 793693b34091SDebojyoti Ghosh 7937cb9d8021SPierre Barbier de Reuille /*@ 7938cb9d8021SPierre Barbier de Reuille TSSetFunctionDomainError - Set the function testing if the current state vector is valid 7939cb9d8021SPierre Barbier de Reuille 7940cb9d8021SPierre Barbier de Reuille Input Parameters: 7941cb9d8021SPierre Barbier de Reuille ts - the TS context 7942cb9d8021SPierre Barbier de Reuille func - function called within TSFunctionDomainError 7943cb9d8021SPierre Barbier de Reuille 7944cb9d8021SPierre Barbier de Reuille Level: intermediate 7945cb9d8021SPierre Barbier de Reuille 7946cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain 7947cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError() 7948cb9d8021SPierre Barbier de Reuille @*/ 7949cb9d8021SPierre Barbier de Reuille 7950d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 7951cb9d8021SPierre Barbier de Reuille { 7952cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7953cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7954cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 7955cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7956cb9d8021SPierre Barbier de Reuille } 7957cb9d8021SPierre Barbier de Reuille 7958cb9d8021SPierre Barbier de Reuille /*@ 7959cb9d8021SPierre Barbier de Reuille TSFunctionDomainError - Check if the current state is valid 7960cb9d8021SPierre Barbier de Reuille 7961cb9d8021SPierre Barbier de Reuille Input Parameters: 7962cb9d8021SPierre Barbier de Reuille ts - the TS context 7963cb9d8021SPierre Barbier de Reuille stagetime - time of the simulation 7964d183316bSPierre Barbier de Reuille Y - state vector to check. 7965cb9d8021SPierre Barbier de Reuille 7966cb9d8021SPierre Barbier de Reuille Output Parameter: 7967cb9d8021SPierre Barbier de Reuille accept - Set to PETSC_FALSE if the current state vector is valid. 7968cb9d8021SPierre Barbier de Reuille 7969cb9d8021SPierre Barbier de Reuille Note: 7970cb9d8021SPierre Barbier de Reuille This function should be used to ensure the state is in a valid part of the space. 7971cb9d8021SPierre Barbier de Reuille For example, one can ensure here all values are positive. 797296a0c994SBarry Smith 797396a0c994SBarry Smith Level: advanced 7974cb9d8021SPierre Barbier de Reuille @*/ 7975d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 7976cb9d8021SPierre Barbier de Reuille { 7977cb9d8021SPierre Barbier de Reuille PetscErrorCode ierr; 7978cb9d8021SPierre Barbier de Reuille 7979cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7980cb9d8021SPierre Barbier de Reuille 7981cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7982cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 7983cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 7984d183316bSPierre Barbier de Reuille PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 7985cb9d8021SPierre Barbier de Reuille } 7986cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7987cb9d8021SPierre Barbier de Reuille } 79881ceb14c0SBarry Smith 798993b34091SDebojyoti Ghosh /*@C 7990e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 799193b34091SDebojyoti Ghosh 799293b34091SDebojyoti Ghosh Collective on MPI_Comm 799393b34091SDebojyoti Ghosh 799493b34091SDebojyoti Ghosh Input Parameter: 799593b34091SDebojyoti Ghosh . tsin - The input TS 799693b34091SDebojyoti Ghosh 799793b34091SDebojyoti Ghosh Output Parameter: 7998e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 799993b34091SDebojyoti Ghosh 80005eca1a21SEmil Constantinescu Notes: 80015eca1a21SEmil 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. 80025eca1a21SEmil Constantinescu 8003baa10174SEmil 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); 80045eca1a21SEmil Constantinescu 80055eca1a21SEmil Constantinescu Level: developer 800693b34091SDebojyoti Ghosh 8007e5168f73SEmil Constantinescu .keywords: TS, clone 8008e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 800993b34091SDebojyoti Ghosh @*/ 8010baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 801193b34091SDebojyoti Ghosh { 801293b34091SDebojyoti Ghosh TS t; 801393b34091SDebojyoti Ghosh PetscErrorCode ierr; 8014dc846ba4SSatish Balay SNES snes_start; 8015dc846ba4SSatish Balay DM dm; 8016dc846ba4SSatish Balay TSType type; 801793b34091SDebojyoti Ghosh 801893b34091SDebojyoti Ghosh PetscFunctionBegin; 801993b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 802093b34091SDebojyoti Ghosh *tsout = NULL; 802193b34091SDebojyoti Ghosh 80227a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 802393b34091SDebojyoti Ghosh 802493b34091SDebojyoti Ghosh /* General TS description */ 802593b34091SDebojyoti Ghosh t->numbermonitors = 0; 802693b34091SDebojyoti Ghosh t->setupcalled = 0; 802793b34091SDebojyoti Ghosh t->ksp_its = 0; 802893b34091SDebojyoti Ghosh t->snes_its = 0; 802993b34091SDebojyoti Ghosh t->nwork = 0; 803093b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 803193b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 803293b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 803393b34091SDebojyoti Ghosh 803434561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr); 803534561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr); 8036d15a3a53SEmil Constantinescu 803793b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr); 803893b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm);CHKERRQ(ierr); 803993b34091SDebojyoti Ghosh 804093b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 804151699248SLisandro Dalcin ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr); 804293b34091SDebojyoti Ghosh 8043e7069c78SShri t->trajectory = tsin->trajectory; 8044e7069c78SShri ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr); 8045e7069c78SShri 8046e7069c78SShri t->event = tsin->event; 80476b10a48eSSatish Balay if (t->event) t->event->refct++; 8048e7069c78SShri 804993b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 805093b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 8051e7069c78SShri t->ptime_prev = tsin->ptime_prev; 805293b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 805393b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 805493b34091SDebojyoti Ghosh t->steps = tsin->steps; 805593b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 805693b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 805793b34091SDebojyoti Ghosh t->atol = tsin->atol; 805893b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 805993b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 806093b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 806193b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 806293b34091SDebojyoti Ghosh 806393b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type);CHKERRQ(ierr); 806493b34091SDebojyoti Ghosh ierr = TSSetType(t,type);CHKERRQ(ierr); 806593b34091SDebojyoti Ghosh 806693b34091SDebojyoti Ghosh t->vec_sol = NULL; 806793b34091SDebojyoti Ghosh 806893b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 806993b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 807093b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 807193b34091SDebojyoti Ghosh 807293b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 807393b34091SDebojyoti Ghosh 80740d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 80750d4fed19SBarry Smith PetscInt i; 80760d4fed19SBarry Smith ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr); 80770d4fed19SBarry Smith for (i=0; i<10; i++) { 80780d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 80790d4fed19SBarry Smith } 80800d4fed19SBarry Smith } 808193b34091SDebojyoti Ghosh *tsout = t; 808293b34091SDebojyoti Ghosh PetscFunctionReturn(0); 808393b34091SDebojyoti Ghosh } 8084