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> 6900f6b5bSMatthew G. Knepley #include <petscconvest.h> 7d763cef2SBarry Smith 81c167fc2SEmil Constantinescu #define SkipSmallValue(a,b,tol) if(PetscAbsScalar(a)< tol || PetscAbsScalar(b)< tol) continue; 91c167fc2SEmil Constantinescu 10d5ba7fb7SMatthew Knepley /* Logging support */ 11d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 12a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 13d405a339SMatthew Knepley 14feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1549354f04SShri Abhyankar 161c167fc2SEmil Constantinescu 17fde5950dSBarry Smith /*@C 18fde5950dSBarry Smith TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 19fde5950dSBarry Smith 20fde5950dSBarry Smith Collective on TS 21fde5950dSBarry Smith 22fde5950dSBarry Smith Input Parameters: 23fde5950dSBarry Smith + ts - TS object you wish to monitor 24fde5950dSBarry Smith . name - the monitor type one is seeking 25fde5950dSBarry Smith . help - message indicating what monitoring is done 26fde5950dSBarry Smith . manual - manual page for the monitor 27fde5950dSBarry Smith . monitor - the monitor function 28fde5950dSBarry 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 29fde5950dSBarry Smith 30fde5950dSBarry Smith Level: developer 31fde5950dSBarry Smith 32fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 33fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 34fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 35fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 36fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 37fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 38fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 39fde5950dSBarry Smith @*/ 40721cd6eeSBarry 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*)) 41fde5950dSBarry Smith { 42fde5950dSBarry Smith PetscErrorCode ierr; 43fde5950dSBarry Smith PetscViewer viewer; 44fde5950dSBarry Smith PetscViewerFormat format; 45fde5950dSBarry Smith PetscBool flg; 46fde5950dSBarry Smith 47fde5950dSBarry Smith PetscFunctionBegin; 4816413a6aSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject) ts)->options,((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 49fde5950dSBarry Smith if (flg) { 50721cd6eeSBarry Smith PetscViewerAndFormat *vf; 51721cd6eeSBarry Smith ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr); 52721cd6eeSBarry Smith ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 53fde5950dSBarry Smith if (monitorsetup) { 54721cd6eeSBarry Smith ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr); 55fde5950dSBarry Smith } 56721cd6eeSBarry Smith ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr); 57fde5950dSBarry Smith } 58fde5950dSBarry Smith PetscFunctionReturn(0); 59fde5950dSBarry Smith } 60fde5950dSBarry Smith 612ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type) 622ffb9264SLisandro Dalcin { 632ffb9264SLisandro Dalcin PetscErrorCode ierr; 642ffb9264SLisandro Dalcin 652ffb9264SLisandro Dalcin PetscFunctionBegin; 66b92453a8SLisandro Dalcin PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1); 67b92453a8SLisandro Dalcin PetscValidCharPointer(default_type,2); 682ffb9264SLisandro Dalcin if (!((PetscObject)adapt)->type_name) { 692ffb9264SLisandro Dalcin ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr); 702ffb9264SLisandro Dalcin } 712ffb9264SLisandro Dalcin PetscFunctionReturn(0); 722ffb9264SLisandro Dalcin } 732ffb9264SLisandro Dalcin 74bdad233fSMatthew Knepley /*@ 75bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 76bdad233fSMatthew Knepley 77bdad233fSMatthew Knepley Collective on TS 78bdad233fSMatthew Knepley 79bdad233fSMatthew Knepley Input Parameter: 80bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 81bdad233fSMatthew Knepley 82bdad233fSMatthew Knepley Options Database Keys: 83e49d4f37SHong Zhang + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP 84ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 85ef85077eSLisandro Dalcin . -ts_max_time <time> - maximum time to compute to 86ef85077eSLisandro Dalcin . -ts_max_steps <steps> - maximum number of time-steps to take 87ef85077eSLisandro Dalcin . -ts_init_time <time> - initial time to start computation 884dc72f7fSBarry Smith . -ts_final_time <time> - final time to compute to (deprecated: use -ts_max_time) 893e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 90a3cdaa26SBarry 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 91a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 92a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 93a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 94a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 95a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 96f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function 97f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - test the Jacobian at each iteration against finite difference with RHS function 98ef222394SBarry Smith . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 99847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 100bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 101de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 102de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 1037cf37e64SBarry Smith . -ts_monitor_error - Monitors norm of error 1046934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 1058b668821SLisandro Dalcin . -ts_monitor_lg_timestep_log - Monitor log timestep size graphically 106de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 107de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 108de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 109de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 1103e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 1113e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 112fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 113e4160dc7SJulian Andrej . -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu) 1149e336e28SPatrick Sanan - -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 11553ea634cSHong Zhang 1169e336e28SPatrick Sanan Developer Note: 1179e336e28SPatrick Sanan We should unify all the -ts_monitor options in the way that -xxx_view has been unified 118bdad233fSMatthew Knepley 119bdad233fSMatthew Knepley Level: beginner 120bdad233fSMatthew Knepley 121a313700dSBarry Smith .seealso: TSGetType() 122bdad233fSMatthew Knepley @*/ 1237087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 124bdad233fSMatthew Knepley { 125bc952696SBarry Smith PetscBool opt,flg,tflg; 126dfbe8321SBarry Smith PetscErrorCode ierr; 127eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 12831748224SBarry Smith PetscReal time_step; 12949354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 130d1212d36SBarry Smith char dir[16]; 131cd11d68dSLisandro Dalcin TSIFunction ifun; 1326991f827SBarry Smith const char *defaultType; 1336991f827SBarry Smith char typeName[256]; 134bdad233fSMatthew Knepley 135bdad233fSMatthew Knepley PetscFunctionBegin; 1360700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1376991f827SBarry Smith 1386991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 139cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 140cd11d68dSLisandro Dalcin 141cd11d68dSLisandro Dalcin ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 1421ef27442SStefano Zampini if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 1431ef27442SStefano Zampini else defaultType = ifun ? TSBEULER : TSEULER; 1446991f827SBarry Smith ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr); 1456991f827SBarry Smith if (opt) { 1466991f827SBarry Smith ierr = TSSetType(ts,typeName);CHKERRQ(ierr); 1476991f827SBarry Smith } else { 1486991f827SBarry Smith ierr = TSSetType(ts,defaultType);CHKERRQ(ierr); 1496991f827SBarry Smith } 150bdad233fSMatthew Knepley 151bdad233fSMatthew Knepley /* Handle generic TS options */ 1524dc72f7fSBarry Smith ierr = PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL);CHKERRQ(ierr); 153ef85077eSLisandro Dalcin ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 15419eac22cSLisandro Dalcin ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1550298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 15631748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 157cd11d68dSLisandro Dalcin if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);} 15849354f04SShri 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); 15949354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 1600298fd71SBarry 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); 1610298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 1620298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 1630298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 1640298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 165bdad233fSMatthew Knepley 166f3b1f45cSBarry Smith ierr = PetscOptionsBool("-ts_rhs_jacobian_test_mult","Test the RHS Jacobian for consistency with RHS at each solve ","None",ts->testjacobian,&ts->testjacobian,NULL);CHKERRQ(ierr); 167f3b1f45cSBarry Smith ierr = PetscOptionsBool("-ts_rhs_jacobian_test_mult_transpose","Test the RHS Jacobian transpose for consistency with RHS at each solve ","None",ts->testjacobiantranspose,&ts->testjacobiantranspose,NULL);CHKERRQ(ierr); 1680fe4d17eSHong Zhang ierr = PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,NULL);CHKERRQ(ierr); 16956f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 17056f85f32SBarry Smith { 17156f85f32SBarry Smith PetscBool set; 17256f85f32SBarry Smith flg = PETSC_FALSE; 17356f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 17456f85f32SBarry Smith if (set) { 17556f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 17656f85f32SBarry Smith } 17756f85f32SBarry Smith } 17856f85f32SBarry Smith #endif 17956f85f32SBarry Smith 180bdad233fSMatthew Knepley /* Monitor options */ 181cd11d68dSLisandro Dalcin ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr); 182cc9c3a59SBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL);CHKERRQ(ierr); 183fde5950dSBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr); 184fde5950dSBarry Smith 1855180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1865180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1875180491cSLisandro Dalcin 1884f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 189b3603a34SBarry Smith if (opt) { 1900b039ecaSBarry Smith TSMonitorLGCtx ctx; 1913923b477SBarry Smith PetscInt howoften = 1; 192b3603a34SBarry Smith 1930298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 1946ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 1954f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 196bdad233fSMatthew Knepley } 1976ba87a44SLisandro Dalcin 1984f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 199ef20d060SBarry Smith if (opt) { 2000b039ecaSBarry Smith TSMonitorLGCtx ctx; 2013923b477SBarry Smith PetscInt howoften = 1; 202ef20d060SBarry Smith 2030298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 2046ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2054f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 206ef20d060SBarry Smith } 207edbaebb3SBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr); 2087cf37e64SBarry Smith 2096934998bSLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 2106934998bSLisandro Dalcin if (opt) { 2116934998bSLisandro Dalcin TSMonitorLGCtx ctx; 2126934998bSLisandro Dalcin PetscInt howoften = 1; 2136934998bSLisandro Dalcin 2146934998bSLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 2156ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2166934998bSLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 2176934998bSLisandro Dalcin } 2188b668821SLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 2198b668821SLisandro Dalcin if (opt) { 2208b668821SLisandro Dalcin TSMonitorLGCtx ctx; 2218b668821SLisandro Dalcin PetscInt howoften = 1; 2228b668821SLisandro Dalcin 2238b668821SLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 2248b668821SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2258b668821SLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 2268b668821SLisandro Dalcin ctx->semilogy = PETSC_TRUE; 2278b668821SLisandro Dalcin } 2288b668821SLisandro Dalcin 229201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 230201da799SBarry Smith if (opt) { 231201da799SBarry Smith TSMonitorLGCtx ctx; 232201da799SBarry Smith PetscInt howoften = 1; 233201da799SBarry Smith 2340298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2356ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 236201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 237201da799SBarry Smith } 238201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 239201da799SBarry Smith if (opt) { 240201da799SBarry Smith TSMonitorLGCtx ctx; 241201da799SBarry Smith PetscInt howoften = 1; 242201da799SBarry Smith 2430298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2446ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 245201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 246201da799SBarry Smith } 2478189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 2488189c53fSBarry Smith if (opt) { 2498189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2508189c53fSBarry Smith PetscInt howoften = 1; 2518189c53fSBarry Smith 2520298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 2536934998bSLisandro Dalcin ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 2548189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 2558189c53fSBarry Smith } 2560ec8ee2bSJoseph Pusztay ierr = PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt);CHKERRQ(ierr); 2571b575b74SJoseph Pusztay if (opt) { 2581b575b74SJoseph Pusztay TSMonitorSPCtx ctx; 2591b575b74SJoseph Pusztay PetscInt howoften = 1; 2600ec8ee2bSJoseph Pusztay ierr = PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm","TSMonitorSPSwarm",howoften,&howoften,NULL);CHKERRQ(ierr); 2611b575b74SJoseph Pusztay ierr = TSMonitorSPCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx);CHKERRQ(ierr); 2620ec8ee2bSJoseph Pusztay ierr = TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy);CHKERRQ(ierr); 2631b575b74SJoseph Pusztay } 264ef20d060SBarry Smith opt = PETSC_FALSE; 2650dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 266a7cc72afSBarry Smith if (opt) { 26783a4ac43SBarry Smith TSMonitorDrawCtx ctx; 26883a4ac43SBarry Smith PetscInt howoften = 1; 269a80ad3e0SBarry Smith 2700298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 2710ed3bfb6SBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 27283a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 273bdad233fSMatthew Knepley } 274fb1732b5SBarry Smith opt = PETSC_FALSE; 2752d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 2762d5ee99bSBarry Smith if (opt) { 2772d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2782d5ee99bSBarry Smith PetscReal bounds[4]; 2792d5ee99bSBarry Smith PetscInt n = 4; 2802d5ee99bSBarry Smith PetscDraw draw; 2816934998bSLisandro Dalcin PetscDrawAxis axis; 2822d5ee99bSBarry Smith 2832d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 2842d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2856934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr); 2862d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 2876934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr); 2886934998bSLisandro Dalcin ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 2896934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 2902d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2912d5ee99bSBarry Smith } 2922d5ee99bSBarry Smith opt = PETSC_FALSE; 2930dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 2943a471f94SBarry Smith if (opt) { 29583a4ac43SBarry Smith TSMonitorDrawCtx ctx; 29683a4ac43SBarry Smith PetscInt howoften = 1; 2973a471f94SBarry Smith 2980298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 2990ed3bfb6SBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 30083a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3013a471f94SBarry Smith } 3020ed3bfb6SBarry Smith opt = PETSC_FALSE; 3030ed3bfb6SBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr); 3040ed3bfb6SBarry Smith if (opt) { 3050ed3bfb6SBarry Smith TSMonitorDrawCtx ctx; 3060ed3bfb6SBarry Smith PetscInt howoften = 1; 3070ed3bfb6SBarry Smith 3080ed3bfb6SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr); 3090ed3bfb6SBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 3100ed3bfb6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3110ed3bfb6SBarry Smith } 312fde5950dSBarry Smith 313ed81e22dSJed Brown opt = PETSC_FALSE; 31491b97e58SBarry 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); 315ed81e22dSJed Brown if (flg) { 316ed81e22dSJed Brown const char *ptr,*ptr2; 317ed81e22dSJed Brown char *filetemplate; 318ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 319ed81e22dSJed Brown /* Do some cursory validation of the input. */ 320ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 321ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 322ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 323ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 324ce94432eSBarry 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"); 325ed81e22dSJed Brown if (ptr2) break; 326ed81e22dSJed Brown } 327ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 328ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 329ed81e22dSJed Brown } 330bdad233fSMatthew Knepley 331d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 332d1212d36SBarry Smith if (flg) { 333d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 334d1212d36SBarry Smith int ray = 0; 3353ee9839eSMatthew G. Knepley DMDirection ddir; 336d1212d36SBarry Smith DM da; 337d1212d36SBarry Smith PetscMPIInt rank; 338d1212d36SBarry Smith 339ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 3403ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3413ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 342ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 343d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 344d1212d36SBarry Smith 3455bd1e576SStefano Zampini ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %d\n",dir[0],ray);CHKERRQ(ierr); 346b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 347d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 348d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 349ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 350d1212d36SBarry Smith if (!rank) { 351d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 352d1212d36SBarry Smith } 35351b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 354d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 355d1212d36SBarry Smith } 35651b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 35751b4a12fSMatthew G. Knepley if (flg) { 35851b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 35951b4a12fSMatthew G. Knepley int ray = 0; 3603ee9839eSMatthew G. Knepley DMDirection ddir; 36151b4a12fSMatthew G. Knepley DM da; 36251b4a12fSMatthew G. Knepley PetscInt howoften = 1; 363d1212d36SBarry Smith 36451b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 3653ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3663ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 36751b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 36851b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 3691c3436cfSJed Brown 3705bd1e576SStefano Zampini ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %d\n", dir[0], ray);CHKERRQ(ierr); 371b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 37251b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 37351b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 37451b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 37551b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 37651b4a12fSMatthew G. Knepley } 377a7a1495cSBarry Smith 378b3d3934dSBarry Smith ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 379b3d3934dSBarry Smith if (opt) { 380b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 381b3d3934dSBarry Smith 382b3d3934dSBarry Smith ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 383b3d3934dSBarry Smith ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 384b3d3934dSBarry Smith } 385b3d3934dSBarry Smith 386847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 387847ff0e1SMatthew G. Knepley ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr); 388847ff0e1SMatthew G. Knepley if (flg) { 389847ff0e1SMatthew G. Knepley DM dm; 390847ff0e1SMatthew G. Knepley DMTS tdm; 391847ff0e1SMatthew G. Knepley 392847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 393847ff0e1SMatthew G. Knepley ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr); 394847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 395847ff0e1SMatthew G. Knepley ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr); 396847ff0e1SMatthew G. Knepley ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 397847ff0e1SMatthew G. Knepley } 398847ff0e1SMatthew G. Knepley 399d763cef2SBarry Smith /* Handle specific TS options */ 400d763cef2SBarry Smith if (ts->ops->setfromoptions) { 4016991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 402d763cef2SBarry Smith } 403fbc52257SHong Zhang 404a7bdc993SLisandro Dalcin /* Handle TSAdapt options */ 405a7bdc993SLisandro Dalcin ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 406a7bdc993SLisandro Dalcin ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr); 407a7bdc993SLisandro Dalcin ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr); 408a7bdc993SLisandro Dalcin 40968bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 4104f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 41168bece0bSHong Zhang ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 41268bece0bSHong Zhang if (tflg) { 41368bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 41468bece0bSHong Zhang } 415a05bf03eSHong Zhang 416a05bf03eSHong Zhang ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr); 417d763cef2SBarry Smith 418d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 4190633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr); 420fbc52257SHong Zhang ierr = PetscOptionsEnd();CHKERRQ(ierr); 421d763cef2SBarry Smith 4224f122a70SLisandro Dalcin if (ts->trajectory) { 4234f122a70SLisandro Dalcin ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 424d763cef2SBarry Smith } 42568bece0bSHong Zhang 4261ef27442SStefano Zampini /* why do we have to do this here and not during TSSetUp? */ 4274f122a70SLisandro Dalcin ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr); 4281ef27442SStefano Zampini if (ts->problem_type == TS_LINEAR) { 4291ef27442SStefano Zampini ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr); 4301ef27442SStefano Zampini if (!flg) { ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); } 4311ef27442SStefano Zampini } 4324f122a70SLisandro Dalcin ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr); 433d763cef2SBarry Smith PetscFunctionReturn(0); 434d763cef2SBarry Smith } 435d763cef2SBarry Smith 436d2daff3dSHong Zhang /*@ 43778fbdcc8SBarry Smith TSGetTrajectory - Gets the trajectory from a TS if it exists 43878fbdcc8SBarry Smith 43978fbdcc8SBarry Smith Collective on TS 44078fbdcc8SBarry Smith 44178fbdcc8SBarry Smith Input Parameters: 44278fbdcc8SBarry Smith . ts - the TS context obtained from TSCreate() 44378fbdcc8SBarry Smith 44478fbdcc8SBarry Smith Output Parameters; 44578fbdcc8SBarry Smith . tr - the TSTrajectory object, if it exists 44678fbdcc8SBarry Smith 44778fbdcc8SBarry Smith Note: This routine should be called after all TS options have been set 44878fbdcc8SBarry Smith 44978fbdcc8SBarry Smith Level: advanced 45078fbdcc8SBarry Smith 45178fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate() 45278fbdcc8SBarry Smith 45378fbdcc8SBarry Smith @*/ 45478fbdcc8SBarry Smith PetscErrorCode TSGetTrajectory(TS ts,TSTrajectory *tr) 45578fbdcc8SBarry Smith { 45678fbdcc8SBarry Smith PetscFunctionBegin; 45778fbdcc8SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45878fbdcc8SBarry Smith *tr = ts->trajectory; 45978fbdcc8SBarry Smith PetscFunctionReturn(0); 46078fbdcc8SBarry Smith } 46178fbdcc8SBarry Smith 46278fbdcc8SBarry Smith /*@ 463bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 464d2daff3dSHong Zhang 465d2daff3dSHong Zhang Collective on TS 466d2daff3dSHong Zhang 467d2daff3dSHong Zhang Input Parameters: 468bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 469bc952696SBarry Smith 47078fbdcc8SBarry Smith Options Database: 47178fbdcc8SBarry Smith + -ts_save_trajectory - saves the trajectory to a file 47278fbdcc8SBarry Smith - -ts_trajectory_type type 47378fbdcc8SBarry Smith 47468bece0bSHong Zhang Note: This routine should be called after all TS options have been set 475d2daff3dSHong Zhang 476c3a89c15SBarry Smith The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and 47778fbdcc8SBarry Smith MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m 47878fbdcc8SBarry Smith 479d2daff3dSHong Zhang Level: intermediate 480d2daff3dSHong Zhang 4812d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve() 482d2daff3dSHong Zhang 483d2daff3dSHong Zhang @*/ 484bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 485d2daff3dSHong Zhang { 486bc952696SBarry Smith PetscErrorCode ierr; 487bc952696SBarry Smith 488d2daff3dSHong Zhang PetscFunctionBegin; 489d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 490bc952696SBarry Smith if (!ts->trajectory) { 491bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 492bc952696SBarry Smith } 493d2daff3dSHong Zhang PetscFunctionReturn(0); 494d2daff3dSHong Zhang } 495d2daff3dSHong Zhang 496a7a1495cSBarry Smith /*@ 4972d29f1f2SStefano Zampini TSResetTrajectory - Destroys and recreates the internal TSTrajectory object 4982d29f1f2SStefano Zampini 4992d29f1f2SStefano Zampini Collective on TS 5002d29f1f2SStefano Zampini 5012d29f1f2SStefano Zampini Input Parameters: 5022d29f1f2SStefano Zampini . ts - the TS context obtained from TSCreate() 5032d29f1f2SStefano Zampini 5042d29f1f2SStefano Zampini Level: intermediate 5052d29f1f2SStefano Zampini 5062d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve() 5072d29f1f2SStefano Zampini 5082d29f1f2SStefano Zampini @*/ 5092d29f1f2SStefano Zampini PetscErrorCode TSResetTrajectory(TS ts) 5102d29f1f2SStefano Zampini { 5112d29f1f2SStefano Zampini PetscErrorCode ierr; 5122d29f1f2SStefano Zampini 5132d29f1f2SStefano Zampini PetscFunctionBegin; 5142d29f1f2SStefano Zampini PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5152d29f1f2SStefano Zampini if (ts->trajectory) { 5162d29f1f2SStefano Zampini ierr = TSTrajectoryDestroy(&ts->trajectory);CHKERRQ(ierr); 5172d29f1f2SStefano Zampini ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 5182d29f1f2SStefano Zampini } 5192d29f1f2SStefano Zampini PetscFunctionReturn(0); 5202d29f1f2SStefano Zampini } 5212d29f1f2SStefano Zampini 5222d29f1f2SStefano Zampini /*@ 523a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 524a7a1495cSBarry Smith set with TSSetRHSJacobian(). 525a7a1495cSBarry Smith 526d083f849SBarry Smith Collective on TS 527a7a1495cSBarry Smith 528a7a1495cSBarry Smith Input Parameters: 529316643e7SJed Brown + ts - the TS context 530a7a1495cSBarry Smith . t - current timestep 5310910c330SBarry Smith - U - input vector 532a7a1495cSBarry Smith 533a7a1495cSBarry Smith Output Parameters: 534a7a1495cSBarry Smith + A - Jacobian matrix 535a7a1495cSBarry Smith . B - optional preconditioning matrix 536a7a1495cSBarry Smith - flag - flag indicating matrix structure 537a7a1495cSBarry Smith 538a7a1495cSBarry Smith Notes: 539a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 540a7a1495cSBarry Smith is used internally within the nonlinear solvers. 541a7a1495cSBarry Smith 54294b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 543a7a1495cSBarry Smith flag parameter. 544a7a1495cSBarry Smith 545a7a1495cSBarry Smith Level: developer 546a7a1495cSBarry Smith 54794b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 548a7a1495cSBarry Smith @*/ 549d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 550a7a1495cSBarry Smith { 551dfbe8321SBarry Smith PetscErrorCode ierr; 552270bf2e7SJed Brown PetscObjectState Ustate; 5536c1e1eecSBarry Smith PetscObjectId Uid; 55424989b8cSPeter Brune DM dm; 555942e3340SBarry Smith DMTS tsdm; 55624989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 55724989b8cSPeter Brune void *ctx; 55824989b8cSPeter Brune TSIJacobian ijacobianfunc; 559b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 560a7a1495cSBarry Smith 561a7a1495cSBarry Smith PetscFunctionBegin; 5620700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5630910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5640910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 56524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 566942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 56724989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 5680298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 569b2df71adSDebojyoti Ghosh ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 57059e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 5716c1e1eecSBarry Smith ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr); 572971015bcSStefano Zampini 5736c1e1eecSBarry Smith if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 574971015bcSStefano Zampini /* restore back RHS Jacobian matrices if they have been shifted/scaled */ 575971015bcSStefano Zampini if (A == ts->Arhs) { 576971015bcSStefano Zampini if (ts->rhsjacobian.shift != 0) { 577971015bcSStefano Zampini ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 578971015bcSStefano Zampini } 579971015bcSStefano Zampini if (ts->rhsjacobian.scale != 1.) { 580971015bcSStefano Zampini ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 581971015bcSStefano Zampini } 582971015bcSStefano Zampini } 583971015bcSStefano Zampini if (B && B == ts->Brhs && A != B) { 584971015bcSStefano Zampini if (ts->rhsjacobian.shift != 0) { 585971015bcSStefano Zampini ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 586971015bcSStefano Zampini } 587971015bcSStefano Zampini if (ts->rhsjacobian.scale != 1.) { 588971015bcSStefano Zampini ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 589971015bcSStefano Zampini } 590971015bcSStefano Zampini } 591971015bcSStefano Zampini ts->rhsjacobian.shift = 0; 592971015bcSStefano Zampini ts->rhsjacobian.scale = 1.; 5930e4ef248SJed Brown PetscFunctionReturn(0); 594f8ede8e7SJed Brown } 595d90be118SSean Farley 596ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 597d90be118SSean Farley 598e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 599971015bcSStefano Zampini if (A == ts->Arhs) { 600971015bcSStefano Zampini /* MatScale has a short path for this case. 601971015bcSStefano Zampini However, this code path is taken the first time TSComputeRHSJacobian is called 602971015bcSStefano Zampini and the matrices have not assembled yet */ 603971015bcSStefano Zampini if (ts->rhsjacobian.shift != 0) { 60494ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 605971015bcSStefano Zampini } 606971015bcSStefano Zampini if (ts->rhsjacobian.scale != 1.) { 60794ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 608971015bcSStefano Zampini } 609971015bcSStefano Zampini } 610971015bcSStefano Zampini if (B && B == ts->Brhs && A != B) { 611971015bcSStefano Zampini if (ts->rhsjacobian.shift != 0) { 61294ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 613971015bcSStefano Zampini } 614971015bcSStefano Zampini if (ts->rhsjacobian.scale != 1.) { 61594ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 616e1244c69SJed Brown } 617971015bcSStefano Zampini } 618e1244c69SJed Brown } 619e1244c69SJed Brown 62024989b8cSPeter Brune if (rhsjacobianfunc) { 6216cd88445SBarry Smith PetscBool missing; 62294ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 623a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 624d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 625a7a1495cSBarry Smith PetscStackPop; 62694ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 6276cd88445SBarry Smith if (A) { 6286cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 6296cd88445SBarry 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"); 6306cd88445SBarry Smith } 6316cd88445SBarry Smith if (B && B != A) { 6326cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 6336cd88445SBarry 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"); 6346cd88445SBarry Smith } 635ef66eb69SBarry Smith } else { 63694ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 637971015bcSStefano Zampini if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 638ef66eb69SBarry Smith } 6390e4ef248SJed Brown ts->rhsjacobian.time = t; 640971015bcSStefano Zampini ts->rhsjacobian.shift = 0; 641971015bcSStefano Zampini ts->rhsjacobian.scale = 1.; 6427f79407eSBarry Smith ierr = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr); 64359e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 644a7a1495cSBarry Smith PetscFunctionReturn(0); 645a7a1495cSBarry Smith } 646a7a1495cSBarry Smith 647316643e7SJed Brown /*@ 648d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 649d763cef2SBarry Smith 650d083f849SBarry Smith Collective on TS 651316643e7SJed Brown 652316643e7SJed Brown Input Parameters: 653316643e7SJed Brown + ts - the TS context 654316643e7SJed Brown . t - current time 6550910c330SBarry Smith - U - state vector 656316643e7SJed Brown 657316643e7SJed Brown Output Parameter: 658316643e7SJed Brown . y - right hand side 659316643e7SJed Brown 660316643e7SJed Brown Note: 661316643e7SJed Brown Most users should not need to explicitly call this routine, as it 662316643e7SJed Brown is used internally within the nonlinear solvers. 663316643e7SJed Brown 664316643e7SJed Brown Level: developer 665316643e7SJed Brown 666316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 667316643e7SJed Brown @*/ 6680910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 669d763cef2SBarry Smith { 670dfbe8321SBarry Smith PetscErrorCode ierr; 67124989b8cSPeter Brune TSRHSFunction rhsfunction; 67224989b8cSPeter Brune TSIFunction ifunction; 67324989b8cSPeter Brune void *ctx; 67424989b8cSPeter Brune DM dm; 67524989b8cSPeter Brune 676d763cef2SBarry Smith PetscFunctionBegin; 6770700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6780910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6790700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 68024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 68124989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 6820298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 683d763cef2SBarry Smith 684ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 685d763cef2SBarry Smith 6860910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 68724989b8cSPeter Brune if (rhsfunction) { 688d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 6890910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 690d763cef2SBarry Smith PetscStackPop; 691214bc6a2SJed Brown } else { 692214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 693b2cd27e8SJed Brown } 69444a41b28SSean Farley 6950910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 696d763cef2SBarry Smith PetscFunctionReturn(0); 697d763cef2SBarry Smith } 698d763cef2SBarry Smith 699ef20d060SBarry Smith /*@ 700ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 701ef20d060SBarry Smith 702d083f849SBarry Smith Collective on TS 703ef20d060SBarry Smith 704ef20d060SBarry Smith Input Parameters: 705ef20d060SBarry Smith + ts - the TS context 706ef20d060SBarry Smith - t - current time 707ef20d060SBarry Smith 708ef20d060SBarry Smith Output Parameter: 7090910c330SBarry Smith . U - the solution 710ef20d060SBarry Smith 711ef20d060SBarry Smith Note: 712ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 713ef20d060SBarry Smith is used internally within the nonlinear solvers. 714ef20d060SBarry Smith 715ef20d060SBarry Smith Level: developer 716ef20d060SBarry Smith 717abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 718ef20d060SBarry Smith @*/ 7190910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 720ef20d060SBarry Smith { 721ef20d060SBarry Smith PetscErrorCode ierr; 722ef20d060SBarry Smith TSSolutionFunction solutionfunction; 723ef20d060SBarry Smith void *ctx; 724ef20d060SBarry Smith DM dm; 725ef20d060SBarry Smith 726ef20d060SBarry Smith PetscFunctionBegin; 727ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7280910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 729ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 730ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 731ef20d060SBarry Smith 732ef20d060SBarry Smith if (solutionfunction) { 7339b7cd975SBarry Smith PetscStackPush("TS user solution function"); 7340910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 735ef20d060SBarry Smith PetscStackPop; 736ef20d060SBarry Smith } 737ef20d060SBarry Smith PetscFunctionReturn(0); 738ef20d060SBarry Smith } 7399b7cd975SBarry Smith /*@ 7409b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 7419b7cd975SBarry Smith 742d083f849SBarry Smith Collective on TS 7439b7cd975SBarry Smith 7449b7cd975SBarry Smith Input Parameters: 7459b7cd975SBarry Smith + ts - the TS context 7469b7cd975SBarry Smith - t - current time 7479b7cd975SBarry Smith 7489b7cd975SBarry Smith Output Parameter: 7499b7cd975SBarry Smith . U - the function value 7509b7cd975SBarry Smith 7519b7cd975SBarry Smith Note: 7529b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 7539b7cd975SBarry Smith is used internally within the nonlinear solvers. 7549b7cd975SBarry Smith 7559b7cd975SBarry Smith Level: developer 7569b7cd975SBarry Smith 7579b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 7589b7cd975SBarry Smith @*/ 7599b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 7609b7cd975SBarry Smith { 7619b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 7629b7cd975SBarry Smith void *ctx; 7639b7cd975SBarry Smith DM dm; 7649b7cd975SBarry Smith 7659b7cd975SBarry Smith PetscFunctionBegin; 7669b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7679b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7689b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 7699b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 7709b7cd975SBarry Smith 7719b7cd975SBarry Smith if (forcing) { 7729b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 7739b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 7749b7cd975SBarry Smith PetscStackPop; 7759b7cd975SBarry Smith } 7769b7cd975SBarry Smith PetscFunctionReturn(0); 7779b7cd975SBarry Smith } 778ef20d060SBarry Smith 779214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 780214bc6a2SJed Brown { 7812dd45cf8SJed Brown Vec F; 782214bc6a2SJed Brown PetscErrorCode ierr; 783214bc6a2SJed Brown 784214bc6a2SJed Brown PetscFunctionBegin; 7850298fd71SBarry Smith *Frhs = NULL; 7860298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 787214bc6a2SJed Brown if (!ts->Frhs) { 7882dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 789214bc6a2SJed Brown } 790214bc6a2SJed Brown *Frhs = ts->Frhs; 791214bc6a2SJed Brown PetscFunctionReturn(0); 792214bc6a2SJed Brown } 793214bc6a2SJed Brown 794971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 795214bc6a2SJed Brown { 796214bc6a2SJed Brown Mat A,B; 7972dd45cf8SJed Brown PetscErrorCode ierr; 79841a1d4d2SBarry Smith TSIJacobian ijacobian; 799214bc6a2SJed Brown 800214bc6a2SJed Brown PetscFunctionBegin; 801c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 802c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 80341a1d4d2SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr); 804214bc6a2SJed Brown if (Arhs) { 805214bc6a2SJed Brown if (!ts->Arhs) { 80641a1d4d2SBarry Smith if (ijacobian) { 807214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 80841a1d4d2SBarry Smith } else { 80941a1d4d2SBarry Smith ts->Arhs = A; 81041a1d4d2SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 81141a1d4d2SBarry Smith } 8123565c898SBarry Smith } else { 8133565c898SBarry Smith PetscBool flg; 8143565c898SBarry Smith ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr); 8153565c898SBarry Smith /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */ 8163565c898SBarry Smith if (flg && !ijacobian && ts->Arhs == ts->Brhs){ 8173565c898SBarry Smith ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr); 8183565c898SBarry Smith ts->Arhs = A; 8193565c898SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 8203565c898SBarry Smith } 821214bc6a2SJed Brown } 822214bc6a2SJed Brown *Arhs = ts->Arhs; 823214bc6a2SJed Brown } 824214bc6a2SJed Brown if (Brhs) { 825214bc6a2SJed Brown if (!ts->Brhs) { 826bdb70873SJed Brown if (A != B) { 82741a1d4d2SBarry Smith if (ijacobian) { 828214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 829bdb70873SJed Brown } else { 83041a1d4d2SBarry Smith ts->Brhs = B; 83141a1d4d2SBarry Smith ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 83241a1d4d2SBarry Smith } 83341a1d4d2SBarry Smith } else { 834bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 83551699248SLisandro Dalcin ts->Brhs = ts->Arhs; 836bdb70873SJed Brown } 837214bc6a2SJed Brown } 838214bc6a2SJed Brown *Brhs = ts->Brhs; 839214bc6a2SJed Brown } 840214bc6a2SJed Brown PetscFunctionReturn(0); 841214bc6a2SJed Brown } 842214bc6a2SJed Brown 843316643e7SJed Brown /*@ 8440910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 845316643e7SJed Brown 846d083f849SBarry Smith Collective on TS 847316643e7SJed Brown 848316643e7SJed Brown Input Parameters: 849316643e7SJed Brown + ts - the TS context 850316643e7SJed Brown . t - current time 8510910c330SBarry Smith . U - state vector 8520910c330SBarry Smith . Udot - time derivative of state vector 853214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 854316643e7SJed Brown 855316643e7SJed Brown Output Parameter: 856316643e7SJed Brown . Y - right hand side 857316643e7SJed Brown 858316643e7SJed Brown Note: 859316643e7SJed Brown Most users should not need to explicitly call this routine, as it 860316643e7SJed Brown is used internally within the nonlinear solvers. 861316643e7SJed Brown 862316643e7SJed Brown If the user did did not write their equations in implicit form, this 863316643e7SJed Brown function recasts them in implicit form. 864316643e7SJed Brown 865316643e7SJed Brown Level: developer 866316643e7SJed Brown 867316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 868316643e7SJed Brown @*/ 8690910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 870316643e7SJed Brown { 871316643e7SJed Brown PetscErrorCode ierr; 87224989b8cSPeter Brune TSIFunction ifunction; 87324989b8cSPeter Brune TSRHSFunction rhsfunction; 87424989b8cSPeter Brune void *ctx; 87524989b8cSPeter Brune DM dm; 876316643e7SJed Brown 877316643e7SJed Brown PetscFunctionBegin; 8780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8790910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8800910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 8810700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 882316643e7SJed Brown 88324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 88424989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 8850298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 88624989b8cSPeter Brune 887ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 888d90be118SSean Farley 8890910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 89024989b8cSPeter Brune if (ifunction) { 891316643e7SJed Brown PetscStackPush("TS user implicit function"); 8920910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 893316643e7SJed Brown PetscStackPop; 894214bc6a2SJed Brown } 895214bc6a2SJed Brown if (imex) { 89624989b8cSPeter Brune if (!ifunction) { 8970910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 8982dd45cf8SJed Brown } 89924989b8cSPeter Brune } else if (rhsfunction) { 90024989b8cSPeter Brune if (ifunction) { 901214bc6a2SJed Brown Vec Frhs; 902214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 9030910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 904214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 9052dd45cf8SJed Brown } else { 9060910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 9070910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 908316643e7SJed Brown } 9094a6899ffSJed Brown } 9100910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 911316643e7SJed Brown PetscFunctionReturn(0); 912316643e7SJed Brown } 913316643e7SJed Brown 914316643e7SJed Brown /*@ 915316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 916316643e7SJed Brown 917d083f849SBarry Smith Collective on TS 918316643e7SJed Brown 919316643e7SJed Brown Input 920316643e7SJed Brown Input Parameters: 921316643e7SJed Brown + ts - the TS context 922316643e7SJed Brown . t - current timestep 9230910c330SBarry Smith . U - state vector 9240910c330SBarry Smith . Udot - time derivative of state vector 925214bc6a2SJed Brown . shift - shift to apply, see note below 926214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 927316643e7SJed Brown 928316643e7SJed Brown Output Parameters: 929316643e7SJed Brown + A - Jacobian matrix 9303565c898SBarry Smith - B - matrix from which the preconditioner is constructed; often the same as A 931316643e7SJed Brown 932316643e7SJed Brown Notes: 9330910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 934316643e7SJed Brown 9350910c330SBarry Smith dF/dU + shift*dF/dUdot 936316643e7SJed Brown 937316643e7SJed Brown Most users should not need to explicitly call this routine, as it 938316643e7SJed Brown is used internally within the nonlinear solvers. 939316643e7SJed Brown 940316643e7SJed Brown Level: developer 941316643e7SJed Brown 942316643e7SJed Brown .seealso: TSSetIJacobian() 94363495f91SJed Brown @*/ 944d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 945316643e7SJed Brown { 946316643e7SJed Brown PetscErrorCode ierr; 94724989b8cSPeter Brune TSIJacobian ijacobian; 94824989b8cSPeter Brune TSRHSJacobian rhsjacobian; 94924989b8cSPeter Brune DM dm; 95024989b8cSPeter Brune void *ctx; 951316643e7SJed Brown 952316643e7SJed Brown PetscFunctionBegin; 9530700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9540910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 9550910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 956316643e7SJed Brown PetscValidPointer(A,6); 95794ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 958316643e7SJed Brown PetscValidPointer(B,7); 95994ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 96024989b8cSPeter Brune 96124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 96224989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 9630298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 96424989b8cSPeter Brune 965ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 966316643e7SJed Brown 96794ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 96824989b8cSPeter Brune if (ijacobian) { 9696cd88445SBarry Smith PetscBool missing; 970316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 971d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 972316643e7SJed Brown PetscStackPop; 9736cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 9746cd88445SBarry 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"); 9753565c898SBarry Smith if (B != A) { 9766cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 9776cd88445SBarry 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"); 9786cd88445SBarry Smith } 9794a6899ffSJed Brown } 980214bc6a2SJed Brown if (imex) { 981b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 9824c26be97Sstefano_zampini PetscBool assembled; 983971015bcSStefano Zampini if (rhsjacobian) { 984971015bcSStefano Zampini Mat Arhs = NULL; 985971015bcSStefano Zampini ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr); 986971015bcSStefano Zampini if (A == Arhs) { 987971015bcSStefano Zampini if (rhsjacobian == TSComputeRHSJacobianConstant) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Unsupported operation! cannot use TSComputeRHSJacobianConstant"); 988971015bcSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 989971015bcSStefano Zampini } 990971015bcSStefano Zampini } 99194ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 9924c26be97Sstefano_zampini ierr = MatAssembled(A,&assembled);CHKERRQ(ierr); 9934c26be97Sstefano_zampini if (!assembled) { 9944c26be97Sstefano_zampini ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9954c26be97Sstefano_zampini ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9964c26be97Sstefano_zampini } 99794ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 99894ab13aaSBarry Smith if (A != B) { 99994ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 10004c26be97Sstefano_zampini ierr = MatAssembled(B,&assembled);CHKERRQ(ierr); 10014c26be97Sstefano_zampini if (!assembled) { 10024c26be97Sstefano_zampini ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10034c26be97Sstefano_zampini ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10044c26be97Sstefano_zampini } 100594ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 1006214bc6a2SJed Brown } 1007214bc6a2SJed Brown } 1008214bc6a2SJed Brown } else { 1009e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 1010e1244c69SJed Brown if (rhsjacobian) { 1011214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 1012d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 1013e1244c69SJed Brown } 101494ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 10153565c898SBarry Smith PetscBool flg; 1016e1244c69SJed Brown ts->rhsjacobian.scale = -1; 1017e1244c69SJed Brown ts->rhsjacobian.shift = shift; 10183565c898SBarry Smith ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr); 10193565c898SBarry Smith /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */ 10203565c898SBarry Smith if (!flg) { 102194ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 102294ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 10233565c898SBarry Smith } 102494ab13aaSBarry Smith if (A != B) { 102594ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 102694ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 1027316643e7SJed Brown } 1028e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 1029e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 1030e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 103194ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 103294ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 103394ab13aaSBarry Smith if (A != B) { 103494ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 103594ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 1036214bc6a2SJed Brown } 1037316643e7SJed Brown } 103894ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 103994ab13aaSBarry Smith if (A != B) { 104094ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 1041316643e7SJed Brown } 1042316643e7SJed Brown } 1043316643e7SJed Brown } 104494ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 1045316643e7SJed Brown PetscFunctionReturn(0); 1046316643e7SJed Brown } 1047316643e7SJed Brown 1048d763cef2SBarry Smith /*@C 1049d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 1050b5abc632SBarry Smith where U_t = G(t,u). 1051d763cef2SBarry Smith 10523f9fe445SBarry Smith Logically Collective on TS 1053d763cef2SBarry Smith 1054d763cef2SBarry Smith Input Parameters: 1055d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 10560298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 1057d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 1058d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 10590298fd71SBarry Smith function evaluation routine (may be NULL) 1060d763cef2SBarry Smith 1061d763cef2SBarry Smith Calling sequence of func: 10626bc98fa9SBarry Smith $ PetscErrorCode func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 1063d763cef2SBarry Smith 1064d763cef2SBarry Smith + t - current timestep 1065d763cef2SBarry Smith . u - input vector 1066d763cef2SBarry Smith . F - function vector 1067d763cef2SBarry Smith - ctx - [optional] user-defined function context 1068d763cef2SBarry Smith 1069d763cef2SBarry Smith Level: beginner 1070d763cef2SBarry Smith 107195452b02SPatrick Sanan Notes: 107295452b02SPatrick Sanan You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 10732bbac0d3SBarry Smith 1074ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 1075d763cef2SBarry Smith @*/ 1076089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 1077d763cef2SBarry Smith { 1078089b2837SJed Brown PetscErrorCode ierr; 1079089b2837SJed Brown SNES snes; 10800298fd71SBarry Smith Vec ralloc = NULL; 108124989b8cSPeter Brune DM dm; 1082d763cef2SBarry Smith 1083089b2837SJed Brown PetscFunctionBegin; 10840700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1085ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 108624989b8cSPeter Brune 108724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 108824989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 1089089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1090e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 1091e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 1092e856ceecSJed Brown r = ralloc; 1093e856ceecSJed Brown } 1094089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 1095e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1096d763cef2SBarry Smith PetscFunctionReturn(0); 1097d763cef2SBarry Smith } 1098d763cef2SBarry Smith 1099ef20d060SBarry Smith /*@C 1100abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1101ef20d060SBarry Smith 1102ef20d060SBarry Smith Logically Collective on TS 1103ef20d060SBarry Smith 1104ef20d060SBarry Smith Input Parameters: 1105ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1106ef20d060SBarry Smith . f - routine for evaluating the solution 1107ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 11080298fd71SBarry Smith function evaluation routine (may be NULL) 1109ef20d060SBarry Smith 1110ef20d060SBarry Smith Calling sequence of func: 11116bc98fa9SBarry Smith $ PetscErrorCode func (TS ts,PetscReal t,Vec u,void *ctx); 1112ef20d060SBarry Smith 1113ef20d060SBarry Smith + t - current timestep 1114ef20d060SBarry Smith . u - output vector 1115ef20d060SBarry Smith - ctx - [optional] user-defined function context 1116ef20d060SBarry Smith 11170ed3bfb6SBarry Smith Options Database: 11180ed3bfb6SBarry Smith + -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction() 11190ed3bfb6SBarry Smith - -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 11200ed3bfb6SBarry Smith 1121abd5a294SJed Brown Notes: 1122abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1123abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1124abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1125abd5a294SJed Brown 11264f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1127abd5a294SJed Brown 1128ef20d060SBarry Smith Level: beginner 1129ef20d060SBarry Smith 11300ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError() 1131ef20d060SBarry Smith @*/ 1132ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1133ef20d060SBarry Smith { 1134ef20d060SBarry Smith PetscErrorCode ierr; 1135ef20d060SBarry Smith DM dm; 1136ef20d060SBarry Smith 1137ef20d060SBarry Smith PetscFunctionBegin; 1138ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1139ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1140ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 1141ef20d060SBarry Smith PetscFunctionReturn(0); 1142ef20d060SBarry Smith } 1143ef20d060SBarry Smith 11449b7cd975SBarry Smith /*@C 11459b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 11469b7cd975SBarry Smith 11479b7cd975SBarry Smith Logically Collective on TS 11489b7cd975SBarry Smith 11499b7cd975SBarry Smith Input Parameters: 11509b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 1151e162b725SBarry Smith . func - routine for evaluating the forcing function 11529b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 11530298fd71SBarry Smith function evaluation routine (may be NULL) 11549b7cd975SBarry Smith 11559b7cd975SBarry Smith Calling sequence of func: 11566bc98fa9SBarry Smith $ PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx); 11579b7cd975SBarry Smith 11589b7cd975SBarry Smith + t - current timestep 1159e162b725SBarry Smith . f - output vector 11609b7cd975SBarry Smith - ctx - [optional] user-defined function context 11619b7cd975SBarry Smith 11629b7cd975SBarry Smith Notes: 11639b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1164e162b725SBarry 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 1165e162b725SBarry Smith definition of the problem you are solving and hence possibly introducing bugs. 1166e162b725SBarry Smith 1167e162b725SBarry Smith This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0 1168e162b725SBarry Smith 1169e162b725SBarry 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 1170e162b725SBarry Smith parameters can be passed in the ctx variable. 11719b7cd975SBarry Smith 11729b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 11739b7cd975SBarry Smith 11749b7cd975SBarry Smith Level: beginner 11759b7cd975SBarry Smith 11769b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 11779b7cd975SBarry Smith @*/ 1178e162b725SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx) 11799b7cd975SBarry Smith { 11809b7cd975SBarry Smith PetscErrorCode ierr; 11819b7cd975SBarry Smith DM dm; 11829b7cd975SBarry Smith 11839b7cd975SBarry Smith PetscFunctionBegin; 11849b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11859b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1186e162b725SBarry Smith ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr); 11879b7cd975SBarry Smith PetscFunctionReturn(0); 11889b7cd975SBarry Smith } 11899b7cd975SBarry Smith 1190d763cef2SBarry Smith /*@C 1191f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1192b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1193d763cef2SBarry Smith 11943f9fe445SBarry Smith Logically Collective on TS 1195d763cef2SBarry Smith 1196d763cef2SBarry Smith Input Parameters: 1197d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1198e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1199e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1200d763cef2SBarry Smith . f - the Jacobian evaluation routine 1201d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 12020298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1203d763cef2SBarry Smith 1204f7ab8db6SBarry Smith Calling sequence of f: 12056bc98fa9SBarry Smith $ PetscErrorCode func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1206d763cef2SBarry Smith 1207d763cef2SBarry Smith + t - current timestep 1208d763cef2SBarry Smith . u - input vector 1209e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1210e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1211d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1212d763cef2SBarry Smith 12136cd88445SBarry Smith Notes: 12146cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 12156cd88445SBarry Smith 12166cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1217ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1218d763cef2SBarry Smith 1219d763cef2SBarry Smith Level: beginner 1220d763cef2SBarry Smith 1221ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1222d763cef2SBarry Smith 1223d763cef2SBarry Smith @*/ 1224e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1225d763cef2SBarry Smith { 1226277b19d0SLisandro Dalcin PetscErrorCode ierr; 1227089b2837SJed Brown SNES snes; 122824989b8cSPeter Brune DM dm; 122924989b8cSPeter Brune TSIJacobian ijacobian; 1230277b19d0SLisandro Dalcin 1231d763cef2SBarry Smith PetscFunctionBegin; 12320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1233e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1234e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1235e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1236e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1237d763cef2SBarry Smith 123824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 123924989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1240e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1241e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1242e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1243e1244c69SJed Brown } 12440298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1245089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12465f659677SPeter Brune if (!ijacobian) { 1247e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 12480e4ef248SJed Brown } 1249e5d3d808SBarry Smith if (Amat) { 1250e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 12510e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1252e5d3d808SBarry Smith ts->Arhs = Amat; 12530e4ef248SJed Brown } 1254e5d3d808SBarry Smith if (Pmat) { 1255e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 12560e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1257e5d3d808SBarry Smith ts->Brhs = Pmat; 12580e4ef248SJed Brown } 1259d763cef2SBarry Smith PetscFunctionReturn(0); 1260d763cef2SBarry Smith } 1261d763cef2SBarry Smith 1262316643e7SJed Brown /*@C 1263b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1264316643e7SJed Brown 12653f9fe445SBarry Smith Logically Collective on TS 1266316643e7SJed Brown 1267316643e7SJed Brown Input Parameters: 1268316643e7SJed Brown + ts - the TS context obtained from TSCreate() 12690298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1270316643e7SJed Brown . f - the function evaluation routine 12710298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1272316643e7SJed Brown 1273316643e7SJed Brown Calling sequence of f: 12746bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1275316643e7SJed Brown 1276316643e7SJed Brown + t - time at step/stage being solved 1277316643e7SJed Brown . u - state vector 1278316643e7SJed Brown . u_t - time derivative of state vector 1279316643e7SJed Brown . F - function vector 1280316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1281316643e7SJed Brown 1282316643e7SJed Brown Important: 12832bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1284316643e7SJed Brown 1285316643e7SJed Brown Level: beginner 1286316643e7SJed Brown 1287d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1288316643e7SJed Brown @*/ 128951699248SLisandro Dalcin PetscErrorCode TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx) 1290316643e7SJed Brown { 1291089b2837SJed Brown PetscErrorCode ierr; 1292089b2837SJed Brown SNES snes; 129351699248SLisandro Dalcin Vec ralloc = NULL; 129424989b8cSPeter Brune DM dm; 1295316643e7SJed Brown 1296316643e7SJed Brown PetscFunctionBegin; 12970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 129851699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 129924989b8cSPeter Brune 130024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 130124989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 130224989b8cSPeter Brune 1303089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 130451699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 130551699248SLisandro Dalcin ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 130651699248SLisandro Dalcin r = ralloc; 1307e856ceecSJed Brown } 130851699248SLisandro Dalcin ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 130951699248SLisandro Dalcin ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1310089b2837SJed Brown PetscFunctionReturn(0); 1311089b2837SJed Brown } 1312089b2837SJed Brown 1313089b2837SJed Brown /*@C 1314089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1315089b2837SJed Brown 1316089b2837SJed Brown Not Collective 1317089b2837SJed Brown 1318089b2837SJed Brown Input Parameter: 1319089b2837SJed Brown . ts - the TS context 1320089b2837SJed Brown 1321089b2837SJed Brown Output Parameter: 13220298fd71SBarry Smith + r - vector to hold residual (or NULL) 13230298fd71SBarry Smith . func - the function to compute residual (or NULL) 13240298fd71SBarry Smith - ctx - the function context (or NULL) 1325089b2837SJed Brown 1326089b2837SJed Brown Level: advanced 1327089b2837SJed Brown 1328089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1329089b2837SJed Brown @*/ 1330089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1331089b2837SJed Brown { 1332089b2837SJed Brown PetscErrorCode ierr; 1333089b2837SJed Brown SNES snes; 133424989b8cSPeter Brune DM dm; 1335089b2837SJed Brown 1336089b2837SJed Brown PetscFunctionBegin; 1337089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1338089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13390298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 134024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 134124989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1342089b2837SJed Brown PetscFunctionReturn(0); 1343089b2837SJed Brown } 1344089b2837SJed Brown 1345089b2837SJed Brown /*@C 1346089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1347089b2837SJed Brown 1348089b2837SJed Brown Not Collective 1349089b2837SJed Brown 1350089b2837SJed Brown Input Parameter: 1351089b2837SJed Brown . ts - the TS context 1352089b2837SJed Brown 1353089b2837SJed Brown Output Parameter: 13540298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 13550298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 13560298fd71SBarry Smith - ctx - the function context (or NULL) 1357089b2837SJed Brown 1358089b2837SJed Brown Level: advanced 1359089b2837SJed Brown 13602bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1361089b2837SJed Brown @*/ 1362089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1363089b2837SJed Brown { 1364089b2837SJed Brown PetscErrorCode ierr; 1365089b2837SJed Brown SNES snes; 136624989b8cSPeter Brune DM dm; 1367089b2837SJed Brown 1368089b2837SJed Brown PetscFunctionBegin; 1369089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1370089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13710298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 137224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 137324989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1374316643e7SJed Brown PetscFunctionReturn(0); 1375316643e7SJed Brown } 1376316643e7SJed Brown 1377316643e7SJed Brown /*@C 1378a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1379ae8867d6SBarry Smith provided with TSSetIFunction(). 1380316643e7SJed Brown 13813f9fe445SBarry Smith Logically Collective on TS 1382316643e7SJed Brown 1383316643e7SJed Brown Input Parameters: 1384316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1385e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1386e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1387316643e7SJed Brown . f - the Jacobian evaluation routine 13880298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1389316643e7SJed Brown 1390316643e7SJed Brown Calling sequence of f: 13916bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1392316643e7SJed Brown 1393316643e7SJed Brown + t - time at step/stage being solved 13941b4a444bSJed Brown . U - state vector 13951b4a444bSJed Brown . U_t - time derivative of state vector 1396316643e7SJed Brown . a - shift 1397e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1398e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1399316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1400316643e7SJed Brown 1401316643e7SJed Brown Notes: 1402e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1403316643e7SJed Brown 1404895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1405895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1406895c21f2SBarry Smith 1407a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1408b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1409a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1410a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1411a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1412a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1413a4f0a591SBarry Smith 14146cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 14156cd88445SBarry Smith 14166cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1417ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1418ca5f011dSBarry Smith 1419316643e7SJed Brown Level: beginner 1420316643e7SJed Brown 1421ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1422316643e7SJed Brown 1423316643e7SJed Brown @*/ 1424e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1425316643e7SJed Brown { 1426316643e7SJed Brown PetscErrorCode ierr; 1427089b2837SJed Brown SNES snes; 142824989b8cSPeter Brune DM dm; 1429316643e7SJed Brown 1430316643e7SJed Brown PetscFunctionBegin; 14310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1432e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1433e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1434e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1435e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 143624989b8cSPeter Brune 143724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 143824989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 143924989b8cSPeter Brune 1440089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1441e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1442316643e7SJed Brown PetscFunctionReturn(0); 1443316643e7SJed Brown } 1444316643e7SJed Brown 1445e1244c69SJed Brown /*@ 1446e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1447e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1448e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1449e1244c69SJed Brown not been changed by the TS. 1450e1244c69SJed Brown 1451e1244c69SJed Brown Logically Collective 1452e1244c69SJed Brown 1453e1244c69SJed Brown Input Arguments: 1454e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1455e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1456e1244c69SJed Brown 1457e1244c69SJed Brown Level: intermediate 1458e1244c69SJed Brown 1459e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1460e1244c69SJed Brown @*/ 1461e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1462e1244c69SJed Brown { 1463e1244c69SJed Brown PetscFunctionBegin; 1464e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1465e1244c69SJed Brown PetscFunctionReturn(0); 1466e1244c69SJed Brown } 1467e1244c69SJed Brown 1468efe9872eSLisandro Dalcin /*@C 1469efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1470efe9872eSLisandro Dalcin 1471efe9872eSLisandro Dalcin Logically Collective on TS 1472efe9872eSLisandro Dalcin 1473efe9872eSLisandro Dalcin Input Parameters: 1474efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1475efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1476efe9872eSLisandro Dalcin . fun - the function evaluation routine 1477efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1478efe9872eSLisandro Dalcin 1479efe9872eSLisandro Dalcin Calling sequence of fun: 14806bc98fa9SBarry Smith $ PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1481efe9872eSLisandro Dalcin 1482efe9872eSLisandro Dalcin + t - time at step/stage being solved 1483efe9872eSLisandro Dalcin . U - state vector 1484efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1485efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1486efe9872eSLisandro Dalcin . F - function vector 1487efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1488efe9872eSLisandro Dalcin 1489efe9872eSLisandro Dalcin Level: beginner 1490efe9872eSLisandro Dalcin 1491efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1492efe9872eSLisandro Dalcin @*/ 1493efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx) 1494efe9872eSLisandro Dalcin { 1495efe9872eSLisandro Dalcin DM dm; 1496efe9872eSLisandro Dalcin PetscErrorCode ierr; 1497efe9872eSLisandro Dalcin 1498efe9872eSLisandro Dalcin PetscFunctionBegin; 1499efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1500efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2); 1501efe9872eSLisandro Dalcin ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr); 1502efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1503efe9872eSLisandro Dalcin ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1504efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1505efe9872eSLisandro Dalcin } 1506efe9872eSLisandro Dalcin 1507efe9872eSLisandro Dalcin /*@C 1508efe9872eSLisandro Dalcin TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1509efe9872eSLisandro Dalcin 1510efe9872eSLisandro Dalcin Not Collective 1511efe9872eSLisandro Dalcin 1512efe9872eSLisandro Dalcin Input Parameter: 1513efe9872eSLisandro Dalcin . ts - the TS context 1514efe9872eSLisandro Dalcin 1515efe9872eSLisandro Dalcin Output Parameter: 1516efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1517efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1518efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1519efe9872eSLisandro Dalcin 1520efe9872eSLisandro Dalcin Level: advanced 1521efe9872eSLisandro Dalcin 1522efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction() 1523efe9872eSLisandro Dalcin @*/ 1524efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx) 1525efe9872eSLisandro Dalcin { 1526efe9872eSLisandro Dalcin PetscErrorCode ierr; 1527efe9872eSLisandro Dalcin SNES snes; 1528efe9872eSLisandro Dalcin DM dm; 1529efe9872eSLisandro Dalcin 1530efe9872eSLisandro Dalcin PetscFunctionBegin; 1531efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1532efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1533efe9872eSLisandro Dalcin ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 1534efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1535efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1536efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1537efe9872eSLisandro Dalcin } 1538efe9872eSLisandro Dalcin 1539efe9872eSLisandro Dalcin /*@C 1540bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1541efe9872eSLisandro Dalcin where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function(). 1542efe9872eSLisandro Dalcin 1543efe9872eSLisandro Dalcin Logically Collective on TS 1544efe9872eSLisandro Dalcin 1545efe9872eSLisandro Dalcin Input Parameters: 1546efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1547efe9872eSLisandro Dalcin . J - Jacobian matrix 1548efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1549efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1550efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1551efe9872eSLisandro Dalcin 1552efe9872eSLisandro Dalcin Calling sequence of jac: 15536bc98fa9SBarry Smith $ PetscErrorCode jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx); 1554efe9872eSLisandro Dalcin 1555efe9872eSLisandro Dalcin + t - time at step/stage being solved 1556efe9872eSLisandro Dalcin . U - state vector 1557efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1558efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1559efe9872eSLisandro Dalcin . v - shift for U_t 1560efe9872eSLisandro Dalcin . a - shift for U_tt 1561efe9872eSLisandro 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 1562efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1563efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1564efe9872eSLisandro Dalcin 1565efe9872eSLisandro Dalcin Notes: 1566efe9872eSLisandro Dalcin The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve. 1567efe9872eSLisandro Dalcin 1568efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1569efe9872eSLisandro 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. 1570efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1571bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1572efe9872eSLisandro Dalcin 1573efe9872eSLisandro Dalcin Level: beginner 1574efe9872eSLisandro Dalcin 1575efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1576efe9872eSLisandro Dalcin @*/ 1577efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx) 1578efe9872eSLisandro Dalcin { 1579efe9872eSLisandro Dalcin DM dm; 1580efe9872eSLisandro Dalcin PetscErrorCode ierr; 1581efe9872eSLisandro Dalcin 1582efe9872eSLisandro Dalcin PetscFunctionBegin; 1583efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1584efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2); 1585efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3); 1586efe9872eSLisandro Dalcin ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr); 1587efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1588efe9872eSLisandro Dalcin ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1589efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1590efe9872eSLisandro Dalcin } 1591efe9872eSLisandro Dalcin 1592efe9872eSLisandro Dalcin /*@C 1593efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1594efe9872eSLisandro Dalcin 1595efe9872eSLisandro Dalcin Not Collective, but parallel objects are returned if TS is parallel 1596efe9872eSLisandro Dalcin 1597efe9872eSLisandro Dalcin Input Parameter: 1598efe9872eSLisandro Dalcin . ts - The TS context obtained from TSCreate() 1599efe9872eSLisandro Dalcin 1600efe9872eSLisandro Dalcin Output Parameters: 1601efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1602efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1603efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1604efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1605efe9872eSLisandro Dalcin 160695452b02SPatrick Sanan Notes: 160795452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 1608efe9872eSLisandro Dalcin 1609efe9872eSLisandro Dalcin Level: advanced 1610efe9872eSLisandro Dalcin 161180275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 1612efe9872eSLisandro Dalcin 1613efe9872eSLisandro Dalcin @*/ 1614efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx) 1615efe9872eSLisandro Dalcin { 1616efe9872eSLisandro Dalcin PetscErrorCode ierr; 1617efe9872eSLisandro Dalcin SNES snes; 1618efe9872eSLisandro Dalcin DM dm; 1619efe9872eSLisandro Dalcin 1620efe9872eSLisandro Dalcin PetscFunctionBegin; 1621efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1622efe9872eSLisandro Dalcin ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 1623efe9872eSLisandro Dalcin ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr); 1624efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1625efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1626efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1627efe9872eSLisandro Dalcin } 1628efe9872eSLisandro Dalcin 1629efe9872eSLisandro Dalcin /*@ 1630efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1631efe9872eSLisandro Dalcin 1632d083f849SBarry Smith Collective on TS 1633efe9872eSLisandro Dalcin 1634efe9872eSLisandro Dalcin Input Parameters: 1635efe9872eSLisandro Dalcin + ts - the TS context 1636efe9872eSLisandro Dalcin . t - current time 1637efe9872eSLisandro Dalcin . U - state vector 1638efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1639efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1640efe9872eSLisandro Dalcin 1641efe9872eSLisandro Dalcin Output Parameter: 1642efe9872eSLisandro Dalcin . F - the residual vector 1643efe9872eSLisandro Dalcin 1644efe9872eSLisandro Dalcin Note: 1645efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1646efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1647efe9872eSLisandro Dalcin 1648efe9872eSLisandro Dalcin Level: developer 1649efe9872eSLisandro Dalcin 1650efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1651efe9872eSLisandro Dalcin @*/ 1652efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F) 1653efe9872eSLisandro Dalcin { 1654efe9872eSLisandro Dalcin DM dm; 1655efe9872eSLisandro Dalcin TSI2Function I2Function; 1656efe9872eSLisandro Dalcin void *ctx; 1657efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1658efe9872eSLisandro Dalcin PetscErrorCode ierr; 1659efe9872eSLisandro Dalcin 1660efe9872eSLisandro Dalcin PetscFunctionBegin; 1661efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1662efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1663efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1664efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1665efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F,VEC_CLASSID,6); 1666efe9872eSLisandro Dalcin 1667efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1668efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr); 1669efe9872eSLisandro Dalcin ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 1670efe9872eSLisandro Dalcin 1671efe9872eSLisandro Dalcin if (!I2Function) { 1672efe9872eSLisandro Dalcin ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr); 1673efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1674efe9872eSLisandro Dalcin } 1675efe9872eSLisandro Dalcin 1676efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1677efe9872eSLisandro Dalcin 1678efe9872eSLisandro Dalcin PetscStackPush("TS user implicit function"); 1679efe9872eSLisandro Dalcin ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr); 1680efe9872eSLisandro Dalcin PetscStackPop; 1681efe9872eSLisandro Dalcin 1682efe9872eSLisandro Dalcin if (rhsfunction) { 1683efe9872eSLisandro Dalcin Vec Frhs; 1684efe9872eSLisandro Dalcin ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 1685efe9872eSLisandro Dalcin ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 1686efe9872eSLisandro Dalcin ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr); 1687efe9872eSLisandro Dalcin } 1688efe9872eSLisandro Dalcin 1689efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1690efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1691efe9872eSLisandro Dalcin } 1692efe9872eSLisandro Dalcin 1693efe9872eSLisandro Dalcin /*@ 1694efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1695efe9872eSLisandro Dalcin 1696d083f849SBarry Smith Collective on TS 1697efe9872eSLisandro Dalcin 1698efe9872eSLisandro Dalcin Input Parameters: 1699efe9872eSLisandro Dalcin + ts - the TS context 1700efe9872eSLisandro Dalcin . t - current timestep 1701efe9872eSLisandro Dalcin . U - state vector 1702efe9872eSLisandro Dalcin . V - time derivative of state vector 1703efe9872eSLisandro Dalcin . A - second time derivative of state vector 1704efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1705efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1706efe9872eSLisandro Dalcin 1707efe9872eSLisandro Dalcin Output Parameters: 1708efe9872eSLisandro Dalcin + J - Jacobian matrix 1709efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1710efe9872eSLisandro Dalcin 1711efe9872eSLisandro Dalcin Notes: 1712efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1713efe9872eSLisandro Dalcin 1714efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1715efe9872eSLisandro Dalcin 1716efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1717efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1718efe9872eSLisandro Dalcin 1719efe9872eSLisandro Dalcin Level: developer 1720efe9872eSLisandro Dalcin 1721efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1722efe9872eSLisandro Dalcin @*/ 1723efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P) 1724efe9872eSLisandro Dalcin { 1725efe9872eSLisandro Dalcin DM dm; 1726efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1727efe9872eSLisandro Dalcin void *ctx; 1728efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1729efe9872eSLisandro Dalcin PetscErrorCode ierr; 1730efe9872eSLisandro Dalcin 1731efe9872eSLisandro Dalcin PetscFunctionBegin; 1732efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1733efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1734efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1735efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1736efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J,MAT_CLASSID,8); 1737efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P,MAT_CLASSID,9); 1738efe9872eSLisandro Dalcin 1739efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1740efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr); 1741efe9872eSLisandro Dalcin ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 1742efe9872eSLisandro Dalcin 1743efe9872eSLisandro Dalcin if (!I2Jacobian) { 1744efe9872eSLisandro Dalcin ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr); 1745efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1746efe9872eSLisandro Dalcin } 1747efe9872eSLisandro Dalcin 1748efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1749efe9872eSLisandro Dalcin 1750efe9872eSLisandro Dalcin PetscStackPush("TS user implicit Jacobian"); 1751efe9872eSLisandro Dalcin ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr); 1752efe9872eSLisandro Dalcin PetscStackPop; 1753efe9872eSLisandro Dalcin 1754efe9872eSLisandro Dalcin if (rhsjacobian) { 1755efe9872eSLisandro Dalcin Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 1756efe9872eSLisandro Dalcin ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr); 1757efe9872eSLisandro Dalcin ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr); 1758efe9872eSLisandro Dalcin ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr); 1759efe9872eSLisandro Dalcin if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);} 1760efe9872eSLisandro Dalcin } 1761efe9872eSLisandro Dalcin 1762efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1763efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1764efe9872eSLisandro Dalcin } 1765efe9872eSLisandro Dalcin 1766efe9872eSLisandro Dalcin /*@ 1767efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1768efe9872eSLisandro Dalcin for use by the TS routines handling second order equations. 1769efe9872eSLisandro Dalcin 1770d083f849SBarry Smith Logically Collective on TS 1771efe9872eSLisandro Dalcin 1772efe9872eSLisandro Dalcin Input Parameters: 1773efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1774efe9872eSLisandro Dalcin . u - the solution vector 1775efe9872eSLisandro Dalcin - v - the time derivative vector 1776efe9872eSLisandro Dalcin 1777efe9872eSLisandro Dalcin Level: beginner 1778efe9872eSLisandro Dalcin 1779efe9872eSLisandro Dalcin @*/ 1780efe9872eSLisandro Dalcin PetscErrorCode TS2SetSolution(TS ts,Vec u,Vec v) 1781efe9872eSLisandro Dalcin { 1782efe9872eSLisandro Dalcin PetscErrorCode ierr; 1783efe9872eSLisandro Dalcin 1784efe9872eSLisandro Dalcin PetscFunctionBegin; 1785efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1786efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1787efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v,VEC_CLASSID,3); 1788efe9872eSLisandro Dalcin ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 1789efe9872eSLisandro Dalcin ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr); 1790efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 1791efe9872eSLisandro Dalcin ts->vec_dot = v; 1792efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1793efe9872eSLisandro Dalcin } 1794efe9872eSLisandro Dalcin 1795efe9872eSLisandro Dalcin /*@ 1796efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 1797efe9872eSLisandro Dalcin for second order equations. It is valid to call this routine inside the function 1798efe9872eSLisandro Dalcin that you are evaluating in order to move to the new timestep. This vector not 1799efe9872eSLisandro Dalcin changed until the solution at the next timestep has been calculated. 1800efe9872eSLisandro Dalcin 1801efe9872eSLisandro Dalcin Not Collective, but Vec returned is parallel if TS is parallel 1802efe9872eSLisandro Dalcin 1803efe9872eSLisandro Dalcin Input Parameter: 1804efe9872eSLisandro Dalcin . ts - the TS context obtained from TSCreate() 1805efe9872eSLisandro Dalcin 1806efe9872eSLisandro Dalcin Output Parameter: 1807efe9872eSLisandro Dalcin + u - the vector containing the solution 1808efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1809efe9872eSLisandro Dalcin 1810efe9872eSLisandro Dalcin Level: intermediate 1811efe9872eSLisandro Dalcin 1812efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime() 1813efe9872eSLisandro Dalcin 1814efe9872eSLisandro Dalcin @*/ 1815efe9872eSLisandro Dalcin PetscErrorCode TS2GetSolution(TS ts,Vec *u,Vec *v) 1816efe9872eSLisandro Dalcin { 1817efe9872eSLisandro Dalcin PetscFunctionBegin; 1818efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1819efe9872eSLisandro Dalcin if (u) PetscValidPointer(u,2); 1820efe9872eSLisandro Dalcin if (v) PetscValidPointer(v,3); 1821efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1822efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 1823efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1824efe9872eSLisandro Dalcin } 1825efe9872eSLisandro Dalcin 182655849f57SBarry Smith /*@C 182755849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 182855849f57SBarry Smith 182955849f57SBarry Smith Collective on PetscViewer 183055849f57SBarry Smith 183155849f57SBarry Smith Input Parameters: 183255849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 183355849f57SBarry Smith some related function before a call to TSLoad(). 183455849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 183555849f57SBarry Smith 183655849f57SBarry Smith Level: intermediate 183755849f57SBarry Smith 183855849f57SBarry Smith Notes: 183955849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 184055849f57SBarry Smith 184155849f57SBarry Smith Notes for advanced users: 184255849f57SBarry Smith Most users should not need to know the details of the binary storage 184355849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 184455849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 184555849f57SBarry Smith format is 184655849f57SBarry Smith .vb 184755849f57SBarry Smith has not yet been determined 184855849f57SBarry Smith .ve 184955849f57SBarry Smith 185055849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 185155849f57SBarry Smith @*/ 1852f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 185355849f57SBarry Smith { 185455849f57SBarry Smith PetscErrorCode ierr; 185555849f57SBarry Smith PetscBool isbinary; 185655849f57SBarry Smith PetscInt classid; 185755849f57SBarry Smith char type[256]; 18582d53ad75SBarry Smith DMTS sdm; 1859ad6bc421SBarry Smith DM dm; 186055849f57SBarry Smith 186155849f57SBarry Smith PetscFunctionBegin; 1862f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 186355849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 186455849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 186555849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 186655849f57SBarry Smith 1867060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1868ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1869060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1870f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1871f2c2a1b9SBarry Smith if (ts->ops->load) { 1872f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1873f2c2a1b9SBarry Smith } 1874ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1875ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1876ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1877f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1878f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 18792d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 18802d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 188155849f57SBarry Smith PetscFunctionReturn(0); 188255849f57SBarry Smith } 188355849f57SBarry Smith 18849804daf3SBarry Smith #include <petscdraw.h> 1885e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1886e04113cfSBarry Smith #include <petscviewersaws.h> 1887f05ece33SBarry Smith #endif 18887e2c5f70SBarry Smith /*@C 1889d763cef2SBarry Smith TSView - Prints the TS data structure. 1890d763cef2SBarry Smith 18914c49b128SBarry Smith Collective on TS 1892d763cef2SBarry Smith 1893d763cef2SBarry Smith Input Parameters: 1894d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1895d763cef2SBarry Smith - viewer - visualization context 1896d763cef2SBarry Smith 1897d763cef2SBarry Smith Options Database Key: 1898d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1899d763cef2SBarry Smith 1900d763cef2SBarry Smith Notes: 1901d763cef2SBarry Smith The available visualization contexts include 1902b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1903b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1904d763cef2SBarry Smith output where only the first processor opens 1905d763cef2SBarry Smith the file. All other processors send their 1906d763cef2SBarry Smith data to the first processor to print. 1907d763cef2SBarry Smith 1908d763cef2SBarry Smith The user can open an alternative visualization context with 1909b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1910d763cef2SBarry Smith 1911d763cef2SBarry Smith Level: beginner 1912d763cef2SBarry Smith 1913b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1914d763cef2SBarry Smith @*/ 19157087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1916d763cef2SBarry Smith { 1917dfbe8321SBarry Smith PetscErrorCode ierr; 191819fd82e9SBarry Smith TSType type; 19192b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 19202d53ad75SBarry Smith DMTS sdm; 1921e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1922536b137fSBarry Smith PetscBool issaws; 1923f05ece33SBarry Smith #endif 1924d763cef2SBarry Smith 1925d763cef2SBarry Smith PetscFunctionBegin; 19260700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 19273050cee2SBarry Smith if (!viewer) { 1928ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 19293050cee2SBarry Smith } 19300700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1931c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1932fd16b177SBarry Smith 1933251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1934251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 193555849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 19362b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1937e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1938536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1939f05ece33SBarry Smith #endif 194032077d6dSBarry Smith if (iascii) { 1941dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 1942efd4aadfSBarry Smith if (ts->ops->view) { 1943efd4aadfSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1944efd4aadfSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1945efd4aadfSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1946efd4aadfSBarry Smith } 1947ef85077eSLisandro Dalcin if (ts->max_steps < PETSC_MAX_INT) { 194877431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 1949ef85077eSLisandro Dalcin } 1950ef85077eSLisandro Dalcin if (ts->max_time < PETSC_MAX_REAL) { 19517c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1952ef85077eSLisandro Dalcin } 1953efd4aadfSBarry Smith if (ts->usessnes) { 1954efd4aadfSBarry Smith PetscBool lin; 1955d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 19565ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1957d763cef2SBarry Smith } 19585ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 19591ef27442SStefano Zampini ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr); 1960efd4aadfSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr); 1961efd4aadfSBarry Smith } 1962193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 1963a0af407cSBarry Smith if (ts->vrtol) { 1964a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of relative error tolerances, ");CHKERRQ(ierr); 1965a0af407cSBarry Smith } else { 1966a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr); 1967a0af407cSBarry Smith } 1968a0af407cSBarry Smith if (ts->vatol) { 1969a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of absolute error tolerances\n");CHKERRQ(ierr); 1970a0af407cSBarry Smith } else { 1971a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr); 1972a0af407cSBarry Smith } 1973825ab935SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1974efd4aadfSBarry Smith ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr); 1975825ab935SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 19760f5bd95cSBarry Smith } else if (isstring) { 1977a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 197836a9e3b9SBarry Smith ierr = PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type);CHKERRQ(ierr); 197936a9e3b9SBarry Smith if (ts->ops->view) {ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);} 198055849f57SBarry Smith } else if (isbinary) { 198155849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 198255849f57SBarry Smith MPI_Comm comm; 198355849f57SBarry Smith PetscMPIInt rank; 198455849f57SBarry Smith char type[256]; 198555849f57SBarry Smith 198655849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 198755849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 198855849f57SBarry Smith if (!rank) { 198955849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 199055849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 199155849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 199255849f57SBarry Smith } 199355849f57SBarry Smith if (ts->ops->view) { 199455849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 199555849f57SBarry Smith } 1996efd4aadfSBarry Smith if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);} 1997f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1998f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 19992d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 20002d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 20012b0a91c0SBarry Smith } else if (isdraw) { 20022b0a91c0SBarry Smith PetscDraw draw; 20032b0a91c0SBarry Smith char str[36]; 200489fd9fafSBarry Smith PetscReal x,y,bottom,h; 20052b0a91c0SBarry Smith 20062b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 20072b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 20082b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 20092b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 201051fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 201189fd9fafSBarry Smith bottom = y - h; 20122b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 20132b0a91c0SBarry Smith if (ts->ops->view) { 20142b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 20152b0a91c0SBarry Smith } 2016efd4aadfSBarry Smith if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);} 2017efd4aadfSBarry Smith if (ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 20182b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 2019e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 2020536b137fSBarry Smith } else if (issaws) { 2021d45a07a7SBarry Smith PetscMPIInt rank; 20222657e9d9SBarry Smith const char *name; 20232657e9d9SBarry Smith 20242657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 2025d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 2026d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 2027d45a07a7SBarry Smith char dir[1024]; 2028d45a07a7SBarry Smith 2029e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 2030a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 20312657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 20322657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 20332657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 2034d763cef2SBarry Smith } 20350acecf5bSBarry Smith if (ts->ops->view) { 20360acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 20370acecf5bSBarry Smith } 2038f05ece33SBarry Smith #endif 2039f05ece33SBarry Smith } 204036a9e3b9SBarry Smith if (ts->snes && ts->usessnes) { 204136a9e3b9SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 204236a9e3b9SBarry Smith ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr); 204336a9e3b9SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 204436a9e3b9SBarry Smith } 204536a9e3b9SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 204636a9e3b9SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 2047f05ece33SBarry Smith 2048b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2049251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 2050b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2051d763cef2SBarry Smith PetscFunctionReturn(0); 2052d763cef2SBarry Smith } 2053d763cef2SBarry Smith 2054b07ff414SBarry Smith /*@ 2055d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2056d763cef2SBarry Smith the timesteppers. 2057d763cef2SBarry Smith 20583f9fe445SBarry Smith Logically Collective on TS 2059d763cef2SBarry Smith 2060d763cef2SBarry Smith Input Parameters: 2061d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2062d763cef2SBarry Smith - usrP - optional user context 2063d763cef2SBarry Smith 206495452b02SPatrick Sanan Fortran Notes: 206595452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 2066daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2067daf670e6SBarry Smith 2068d763cef2SBarry Smith Level: intermediate 2069d763cef2SBarry Smith 2070d763cef2SBarry Smith .seealso: TSGetApplicationContext() 2071d763cef2SBarry Smith @*/ 20727087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 2073d763cef2SBarry Smith { 2074d763cef2SBarry Smith PetscFunctionBegin; 20750700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2076d763cef2SBarry Smith ts->user = usrP; 2077d763cef2SBarry Smith PetscFunctionReturn(0); 2078d763cef2SBarry Smith } 2079d763cef2SBarry Smith 2080b07ff414SBarry Smith /*@ 2081d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2082d763cef2SBarry Smith timestepper. 2083d763cef2SBarry Smith 2084d763cef2SBarry Smith Not Collective 2085d763cef2SBarry Smith 2086d763cef2SBarry Smith Input Parameter: 2087d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2088d763cef2SBarry Smith 2089d763cef2SBarry Smith Output Parameter: 2090d763cef2SBarry Smith . usrP - user context 2091d763cef2SBarry Smith 209295452b02SPatrick Sanan Fortran Notes: 209395452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 2094daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2095daf670e6SBarry Smith 2096d763cef2SBarry Smith Level: intermediate 2097d763cef2SBarry Smith 2098d763cef2SBarry Smith .seealso: TSSetApplicationContext() 2099d763cef2SBarry Smith @*/ 2100e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 2101d763cef2SBarry Smith { 2102d763cef2SBarry Smith PetscFunctionBegin; 21030700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2104e71120c6SJed Brown *(void**)usrP = ts->user; 2105d763cef2SBarry Smith PetscFunctionReturn(0); 2106d763cef2SBarry Smith } 2107d763cef2SBarry Smith 2108d763cef2SBarry Smith /*@ 210980275a0aSLisandro Dalcin TSGetStepNumber - Gets the number of steps completed. 2110d763cef2SBarry Smith 2111d763cef2SBarry Smith Not Collective 2112d763cef2SBarry Smith 2113d763cef2SBarry Smith Input Parameter: 2114d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2115d763cef2SBarry Smith 2116d763cef2SBarry Smith Output Parameter: 211780275a0aSLisandro Dalcin . steps - number of steps completed so far 2118d763cef2SBarry Smith 2119d763cef2SBarry Smith Level: intermediate 2120d763cef2SBarry Smith 21219be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 2122d763cef2SBarry Smith @*/ 212380275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps) 2124d763cef2SBarry Smith { 2125d763cef2SBarry Smith PetscFunctionBegin; 21260700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 212780275a0aSLisandro Dalcin PetscValidIntPointer(steps,2); 212880275a0aSLisandro Dalcin *steps = ts->steps; 212980275a0aSLisandro Dalcin PetscFunctionReturn(0); 213080275a0aSLisandro Dalcin } 213180275a0aSLisandro Dalcin 213280275a0aSLisandro Dalcin /*@ 213380275a0aSLisandro Dalcin TSSetStepNumber - Sets the number of steps completed. 213480275a0aSLisandro Dalcin 213580275a0aSLisandro Dalcin Logically Collective on TS 213680275a0aSLisandro Dalcin 213780275a0aSLisandro Dalcin Input Parameters: 213880275a0aSLisandro Dalcin + ts - the TS context 213980275a0aSLisandro Dalcin - steps - number of steps completed so far 214080275a0aSLisandro Dalcin 214180275a0aSLisandro Dalcin Notes: 214280275a0aSLisandro Dalcin For most uses of the TS solvers the user need not explicitly call 214380275a0aSLisandro Dalcin TSSetStepNumber(), as the step counter is appropriately updated in 214480275a0aSLisandro Dalcin TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to 214580275a0aSLisandro Dalcin reinitialize timestepping by setting the step counter to zero (and time 214680275a0aSLisandro Dalcin to the initial time) to solve a similar problem with different initial 214780275a0aSLisandro Dalcin conditions or parameters. Other possible use case is to continue 214880275a0aSLisandro Dalcin timestepping from a previously interrupted run in such a way that TS 214980275a0aSLisandro Dalcin monitors will be called with a initial nonzero step counter. 215080275a0aSLisandro Dalcin 215180275a0aSLisandro Dalcin Level: advanced 215280275a0aSLisandro Dalcin 215380275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution() 215480275a0aSLisandro Dalcin @*/ 215580275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps) 215680275a0aSLisandro Dalcin { 215780275a0aSLisandro Dalcin PetscFunctionBegin; 215880275a0aSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 215980275a0aSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,steps,2); 216080275a0aSLisandro Dalcin if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative"); 216180275a0aSLisandro Dalcin ts->steps = steps; 2162d763cef2SBarry Smith PetscFunctionReturn(0); 2163d763cef2SBarry Smith } 2164d763cef2SBarry Smith 2165d763cef2SBarry Smith /*@ 2166d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2167d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2168d763cef2SBarry Smith 21693f9fe445SBarry Smith Logically Collective on TS 2170d763cef2SBarry Smith 2171d763cef2SBarry Smith Input Parameters: 2172d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2173d763cef2SBarry Smith - time_step - the size of the timestep 2174d763cef2SBarry Smith 2175d763cef2SBarry Smith Level: intermediate 2176d763cef2SBarry Smith 2177aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime() 2178d763cef2SBarry Smith 2179d763cef2SBarry Smith @*/ 21807087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 2181d763cef2SBarry Smith { 2182d763cef2SBarry Smith PetscFunctionBegin; 21830700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2184c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 2185d763cef2SBarry Smith ts->time_step = time_step; 2186d763cef2SBarry Smith PetscFunctionReturn(0); 2187d763cef2SBarry Smith } 2188d763cef2SBarry Smith 2189a43b19c4SJed Brown /*@ 219049354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 219149354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 219249354f04SShri Abhyankar or just return at the final time TS computed. 2193a43b19c4SJed Brown 2194a43b19c4SJed Brown Logically Collective on TS 2195a43b19c4SJed Brown 2196a43b19c4SJed Brown Input Parameter: 2197a43b19c4SJed Brown + ts - the time-step context 219849354f04SShri Abhyankar - eftopt - exact final time option 2199a43b19c4SJed Brown 2200feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2201feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2202feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2203feed9e9dSBarry Smith 2204feed9e9dSBarry Smith Options Database: 2205feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2206feed9e9dSBarry Smith 2207ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 2208ee346746SBarry Smith then the final time you selected. 2209ee346746SBarry Smith 2210a43b19c4SJed Brown Level: beginner 2211a43b19c4SJed Brown 2212f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime() 2213a43b19c4SJed Brown @*/ 221449354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 2215a43b19c4SJed Brown { 2216a43b19c4SJed Brown PetscFunctionBegin; 2217a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 221849354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 221949354f04SShri Abhyankar ts->exact_final_time = eftopt; 2220a43b19c4SJed Brown PetscFunctionReturn(0); 2221a43b19c4SJed Brown } 2222a43b19c4SJed Brown 2223d763cef2SBarry Smith /*@ 2224f6953c82SLisandro Dalcin TSGetExactFinalTime - Gets the exact final time option. 2225f6953c82SLisandro Dalcin 2226f6953c82SLisandro Dalcin Not Collective 2227f6953c82SLisandro Dalcin 2228f6953c82SLisandro Dalcin Input Parameter: 2229f6953c82SLisandro Dalcin . ts - the TS context 2230f6953c82SLisandro Dalcin 2231f6953c82SLisandro Dalcin Output Parameter: 2232f6953c82SLisandro Dalcin . eftopt - exact final time option 2233f6953c82SLisandro Dalcin 2234f6953c82SLisandro Dalcin Level: beginner 2235f6953c82SLisandro Dalcin 2236f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime() 2237f6953c82SLisandro Dalcin @*/ 2238f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt) 2239f6953c82SLisandro Dalcin { 2240f6953c82SLisandro Dalcin PetscFunctionBegin; 2241f6953c82SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2242f6953c82SLisandro Dalcin PetscValidPointer(eftopt,2); 2243f6953c82SLisandro Dalcin *eftopt = ts->exact_final_time; 2244f6953c82SLisandro Dalcin PetscFunctionReturn(0); 2245f6953c82SLisandro Dalcin } 2246f6953c82SLisandro Dalcin 2247f6953c82SLisandro Dalcin /*@ 2248d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2249d763cef2SBarry Smith 2250d763cef2SBarry Smith Not Collective 2251d763cef2SBarry Smith 2252d763cef2SBarry Smith Input Parameter: 2253d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2254d763cef2SBarry Smith 2255d763cef2SBarry Smith Output Parameter: 2256d763cef2SBarry Smith . dt - the current timestep size 2257d763cef2SBarry Smith 2258d763cef2SBarry Smith Level: intermediate 2259d763cef2SBarry Smith 2260aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime() 2261d763cef2SBarry Smith 2262d763cef2SBarry Smith @*/ 22637087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 2264d763cef2SBarry Smith { 2265d763cef2SBarry Smith PetscFunctionBegin; 22660700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2267f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 2268d763cef2SBarry Smith *dt = ts->time_step; 2269d763cef2SBarry Smith PetscFunctionReturn(0); 2270d763cef2SBarry Smith } 2271d763cef2SBarry Smith 2272d8e5e3e6SSatish Balay /*@ 2273d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2274d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2275d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2276d763cef2SBarry Smith the solution at the next timestep has been calculated. 2277d763cef2SBarry Smith 2278d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 2279d763cef2SBarry Smith 2280d763cef2SBarry Smith Input Parameter: 2281d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2282d763cef2SBarry Smith 2283d763cef2SBarry Smith Output Parameter: 2284d763cef2SBarry Smith . v - the vector containing the solution 2285d763cef2SBarry Smith 228663e21af5SBarry Smith Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested 228763e21af5SBarry Smith final time. It returns the solution at the next timestep. 228863e21af5SBarry Smith 2289d763cef2SBarry Smith Level: intermediate 2290d763cef2SBarry Smith 22910ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction() 2292d763cef2SBarry Smith 2293d763cef2SBarry Smith @*/ 22947087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 2295d763cef2SBarry Smith { 2296d763cef2SBarry Smith PetscFunctionBegin; 22970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22984482741eSBarry Smith PetscValidPointer(v,2); 22998737fe31SLisandro Dalcin *v = ts->vec_sol; 2300d763cef2SBarry Smith PetscFunctionReturn(0); 2301d763cef2SBarry Smith } 2302d763cef2SBarry Smith 230303fe5f5eSDebojyoti Ghosh /*@ 2304b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 230503fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2306b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 230703fe5f5eSDebojyoti Ghosh structure as the solution vector. 230803fe5f5eSDebojyoti Ghosh 230903fe5f5eSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 231003fe5f5eSDebojyoti Ghosh 231103fe5f5eSDebojyoti Ghosh Parameters : 2312a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2313b2bf4f3aSDebojyoti Ghosh . n - If v is PETSC_NULL, then the number of solution components is 2314b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 231503fe5f5eSDebojyoti Ghosh returned in v. 2316a2b725a8SWilliam Gropp - v - the vector containing the n-th solution component 231703fe5f5eSDebojyoti Ghosh (may be PETSC_NULL to use this function to find out 2318b2bf4f3aSDebojyoti Ghosh the number of solutions components). 231903fe5f5eSDebojyoti Ghosh 23204cdd57e5SDebojyoti Ghosh Level: advanced 232103fe5f5eSDebojyoti Ghosh 232203fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution() 232303fe5f5eSDebojyoti Ghosh 232403fe5f5eSDebojyoti Ghosh @*/ 2325b2bf4f3aSDebojyoti Ghosh PetscErrorCode TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v) 232603fe5f5eSDebojyoti Ghosh { 232703fe5f5eSDebojyoti Ghosh PetscErrorCode ierr; 232803fe5f5eSDebojyoti Ghosh 232903fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 233003fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2331b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 233203fe5f5eSDebojyoti Ghosh else { 2333b2bf4f3aSDebojyoti Ghosh ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr); 233403fe5f5eSDebojyoti Ghosh } 233503fe5f5eSDebojyoti Ghosh PetscFunctionReturn(0); 233603fe5f5eSDebojyoti Ghosh } 233703fe5f5eSDebojyoti Ghosh 23384cdd57e5SDebojyoti Ghosh /*@ 23394cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 23404cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 23414cdd57e5SDebojyoti Ghosh 23424cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 23434cdd57e5SDebojyoti Ghosh 23444cdd57e5SDebojyoti Ghosh Parameters : 2345a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2346a2b725a8SWilliam Gropp - v - the vector containing the auxiliary solution 23474cdd57e5SDebojyoti Ghosh 23484cdd57e5SDebojyoti Ghosh Level: intermediate 23494cdd57e5SDebojyoti Ghosh 23504cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution() 23514cdd57e5SDebojyoti Ghosh 23524cdd57e5SDebojyoti Ghosh @*/ 23534cdd57e5SDebojyoti Ghosh PetscErrorCode TSGetAuxSolution(TS ts,Vec *v) 23544cdd57e5SDebojyoti Ghosh { 23554cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 23564cdd57e5SDebojyoti Ghosh 23574cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23584cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2359f6356ec7SDebojyoti Ghosh if (ts->ops->getauxsolution) { 23604cdd57e5SDebojyoti Ghosh ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr); 2361f6356ec7SDebojyoti Ghosh } else { 2362f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v); CHKERRQ(ierr); 2363f6356ec7SDebojyoti Ghosh } 23644cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 23654cdd57e5SDebojyoti Ghosh } 23664cdd57e5SDebojyoti Ghosh 23674cdd57e5SDebojyoti Ghosh /*@ 23684cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 23694cdd57e5SDebojyoti Ghosh TSType has an error estimation functionality. 23704cdd57e5SDebojyoti Ghosh 23714cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 23724cdd57e5SDebojyoti Ghosh 23739657682dSDebojyoti Ghosh Note: MUST call after TSSetUp() 23749657682dSDebojyoti Ghosh 23754cdd57e5SDebojyoti Ghosh Parameters : 2376a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2377657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 2378a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 23794cdd57e5SDebojyoti Ghosh 23804cdd57e5SDebojyoti Ghosh Level: intermediate 23814cdd57e5SDebojyoti Ghosh 238257df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError() 23834cdd57e5SDebojyoti Ghosh 23844cdd57e5SDebojyoti Ghosh @*/ 23850a01e1b2SEmil Constantinescu PetscErrorCode TSGetTimeError(TS ts,PetscInt n,Vec *v) 23864cdd57e5SDebojyoti Ghosh { 23874cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 23884cdd57e5SDebojyoti Ghosh 23894cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23904cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2391f6356ec7SDebojyoti Ghosh if (ts->ops->gettimeerror) { 23920a01e1b2SEmil Constantinescu ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr); 2393f6356ec7SDebojyoti Ghosh } else { 2394f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v);CHKERRQ(ierr); 2395f6356ec7SDebojyoti Ghosh } 23964cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 23974cdd57e5SDebojyoti Ghosh } 23984cdd57e5SDebojyoti Ghosh 239957df6a1bSDebojyoti Ghosh /*@ 240057df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 240157df6a1bSDebojyoti Ghosh TSType has an error estimation functionality. This can be used 240257df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 240357df6a1bSDebojyoti Ghosh 240457df6a1bSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 240557df6a1bSDebojyoti Ghosh 240657df6a1bSDebojyoti Ghosh Parameters : 2407a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2408a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 240957df6a1bSDebojyoti Ghosh 241057df6a1bSDebojyoti Ghosh Level: intermediate 241157df6a1bSDebojyoti Ghosh 241257df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError) 241357df6a1bSDebojyoti Ghosh 241457df6a1bSDebojyoti Ghosh @*/ 241557df6a1bSDebojyoti Ghosh PetscErrorCode TSSetTimeError(TS ts,Vec v) 241657df6a1bSDebojyoti Ghosh { 241757df6a1bSDebojyoti Ghosh PetscErrorCode ierr; 241857df6a1bSDebojyoti Ghosh 241957df6a1bSDebojyoti Ghosh PetscFunctionBegin; 242057df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 24219657682dSDebojyoti Ghosh if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first"); 242257df6a1bSDebojyoti Ghosh if (ts->ops->settimeerror) { 242357df6a1bSDebojyoti Ghosh ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr); 242457df6a1bSDebojyoti Ghosh } 242557df6a1bSDebojyoti Ghosh PetscFunctionReturn(0); 242657df6a1bSDebojyoti Ghosh } 242757df6a1bSDebojyoti Ghosh 2428bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2429d8e5e3e6SSatish Balay /*@ 2430bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2431d763cef2SBarry Smith 2432bdad233fSMatthew Knepley Not collective 2433d763cef2SBarry Smith 2434bdad233fSMatthew Knepley Input Parameters: 2435bdad233fSMatthew Knepley + ts - The TS 2436bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2437d763cef2SBarry Smith .vb 24380910c330SBarry Smith U_t - A U = 0 (linear) 24390910c330SBarry Smith U_t - A(t) U = 0 (linear) 24400910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2441d763cef2SBarry Smith .ve 2442d763cef2SBarry Smith 2443d763cef2SBarry Smith Level: beginner 2444d763cef2SBarry Smith 2445bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2446d763cef2SBarry Smith @*/ 24477087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2448a7cc72afSBarry Smith { 24499e2a6581SJed Brown PetscErrorCode ierr; 24509e2a6581SJed Brown 2451d763cef2SBarry Smith PetscFunctionBegin; 24520700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2453bdad233fSMatthew Knepley ts->problem_type = type; 24549e2a6581SJed Brown if (type == TS_LINEAR) { 24559e2a6581SJed Brown SNES snes; 24569e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 24579e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 24589e2a6581SJed Brown } 2459d763cef2SBarry Smith PetscFunctionReturn(0); 2460d763cef2SBarry Smith } 2461d763cef2SBarry Smith 2462bdad233fSMatthew Knepley /*@C 2463bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2464bdad233fSMatthew Knepley 2465bdad233fSMatthew Knepley Not collective 2466bdad233fSMatthew Knepley 2467bdad233fSMatthew Knepley Input Parameter: 2468bdad233fSMatthew Knepley . ts - The TS 2469bdad233fSMatthew Knepley 2470bdad233fSMatthew Knepley Output Parameter: 2471bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2472bdad233fSMatthew Knepley .vb 2473089b2837SJed Brown M U_t = A U 2474089b2837SJed Brown M(t) U_t = A(t) U 2475b5abc632SBarry Smith F(t,U,U_t) 2476bdad233fSMatthew Knepley .ve 2477bdad233fSMatthew Knepley 2478bdad233fSMatthew Knepley Level: beginner 2479bdad233fSMatthew Knepley 2480bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2481bdad233fSMatthew Knepley @*/ 24827087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2483a7cc72afSBarry Smith { 2484bdad233fSMatthew Knepley PetscFunctionBegin; 24850700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 24864482741eSBarry Smith PetscValidIntPointer(type,2); 2487bdad233fSMatthew Knepley *type = ts->problem_type; 2488bdad233fSMatthew Knepley PetscFunctionReturn(0); 2489bdad233fSMatthew Knepley } 2490d763cef2SBarry Smith 2491d763cef2SBarry Smith /*@ 2492d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 2493d763cef2SBarry Smith of a timestepper. 2494d763cef2SBarry Smith 2495d763cef2SBarry Smith Collective on TS 2496d763cef2SBarry Smith 2497d763cef2SBarry Smith Input Parameter: 2498d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2499d763cef2SBarry Smith 2500d763cef2SBarry Smith Notes: 2501d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 2502d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 2503141bd67dSStefano Zampini the call to TSStep() or TSSolve(). However, if one wishes to control this 2504d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 2505141bd67dSStefano Zampini and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve(). 2506d763cef2SBarry Smith 2507d763cef2SBarry Smith Level: advanced 2508d763cef2SBarry Smith 2509141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve() 2510d763cef2SBarry Smith @*/ 25117087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 2512d763cef2SBarry Smith { 2513dfbe8321SBarry Smith PetscErrorCode ierr; 25146c6b9e74SPeter Brune DM dm; 25156c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 2516d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 2517cd11d68dSLisandro Dalcin TSIFunction ifun; 25186c6b9e74SPeter Brune TSIJacobian ijac; 2519efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 25206c6b9e74SPeter Brune TSRHSJacobian rhsjac; 25212ffb9264SLisandro Dalcin PetscBool isnone; 2522d763cef2SBarry Smith 2523d763cef2SBarry Smith PetscFunctionBegin; 25240700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2525277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 2526277b19d0SLisandro Dalcin 25277adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 2528cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 2529cd11d68dSLisandro Dalcin ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr); 2530d763cef2SBarry Smith } 2531277b19d0SLisandro Dalcin 2532277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 2533277b19d0SLisandro Dalcin 2534298bade4SHong Zhang if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */ 2535298bade4SHong Zhang ierr = PetscObjectReference((PetscObject)ts->Jacprhs);CHKERRQ(ierr); 2536298bade4SHong Zhang ts->Jacp = ts->Jacprhs; 2537298bade4SHong Zhang } 2538298bade4SHong Zhang 2539cd4cee2dSHong Zhang if (ts->quadraturets) { 2540cd4cee2dSHong Zhang ierr = TSSetUp(ts->quadraturets);CHKERRQ(ierr); 2541ecf68647SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2542cd4cee2dSHong Zhang ierr = VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);CHKERRQ(ierr); 2543cd4cee2dSHong Zhang } 2544cd4cee2dSHong Zhang 2545971015bcSStefano Zampini ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr); 2546971015bcSStefano Zampini if (ts->rhsjacobian.reuse && rhsjac == TSComputeRHSJacobianConstant) { 2547e1244c69SJed Brown Mat Amat,Pmat; 2548e1244c69SJed Brown SNES snes; 2549e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2550e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 2551e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2552e1244c69SJed Brown * have displaced the RHS matrix */ 2553971015bcSStefano Zampini if (Amat && Amat == ts->Arhs) { 2554abc0d4abSBarry 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 */ 2555abc0d4abSBarry Smith ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr); 2556e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 2557e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 2558e1244c69SJed Brown } 2559971015bcSStefano Zampini if (Pmat && Pmat == ts->Brhs) { 2560abc0d4abSBarry Smith ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 2561e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 2562e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 2563e1244c69SJed Brown } 2564e1244c69SJed Brown } 25652ffb9264SLisandro Dalcin 25662ffb9264SLisandro Dalcin ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 25672ffb9264SLisandro Dalcin ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr); 25682ffb9264SLisandro Dalcin 2569277b19d0SLisandro Dalcin if (ts->ops->setup) { 2570000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 2571277b19d0SLisandro Dalcin } 2572277b19d0SLisandro Dalcin 25732ffb9264SLisandro Dalcin /* Attempt to check/preset a default value for the exact final time option */ 25742ffb9264SLisandro Dalcin ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr); 25752ffb9264SLisandro Dalcin if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) 25762ffb9264SLisandro Dalcin ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP; 25772ffb9264SLisandro Dalcin 2578a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 25796c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 25806c6b9e74SPeter Brune */ 25816c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 25820298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 25836c6b9e74SPeter Brune if (!func) { 25846c6b9e74SPeter Brune ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 25856c6b9e74SPeter Brune } 2586a6772fa2SLisandro 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. 25876c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 25886c6b9e74SPeter Brune */ 25890298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 25900298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 2591efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr); 25920298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 2593efe9872eSLisandro Dalcin if (!jac && (ijac || i2jac || rhsjac)) { 25946c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 25956c6b9e74SPeter Brune } 2596c0517034SDebojyoti Ghosh 2597c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 2598c0517034SDebojyoti Ghosh if (ts->ops->startingmethod) { 2599c0517034SDebojyoti Ghosh ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr); 2600c0517034SDebojyoti Ghosh } 2601c0517034SDebojyoti Ghosh 2602277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2603277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2604277b19d0SLisandro Dalcin } 2605277b19d0SLisandro Dalcin 2606f6a906c0SBarry Smith /*@ 2607277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2608277b19d0SLisandro Dalcin 2609277b19d0SLisandro Dalcin Collective on TS 2610277b19d0SLisandro Dalcin 2611277b19d0SLisandro Dalcin Input Parameter: 2612277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2613277b19d0SLisandro Dalcin 2614277b19d0SLisandro Dalcin Level: beginner 2615277b19d0SLisandro Dalcin 2616277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2617277b19d0SLisandro Dalcin @*/ 2618277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2619277b19d0SLisandro Dalcin { 26201d06f6b3SHong Zhang TS_RHSSplitLink ilink = ts->tsrhssplit,next; 2621277b19d0SLisandro Dalcin PetscErrorCode ierr; 2622277b19d0SLisandro Dalcin 2623277b19d0SLisandro Dalcin PetscFunctionBegin; 2624277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2625b18ea86cSHong Zhang 2626277b19d0SLisandro Dalcin if (ts->ops->reset) { 2627277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2628277b19d0SLisandro Dalcin } 2629277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2630e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2631bbd56ea5SKarl Rupp 26324e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 26334e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2634214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 26356bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2636efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 2637e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2638e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 263938637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2640bbd56ea5SKarl Rupp 2641cd4cee2dSHong Zhang ierr = MatDestroy(&ts->Jacprhs);CHKERRQ(ierr); 2642ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2643ecf68647SHong Zhang if (ts->forward_solve) { 2644ecf68647SHong Zhang ierr = TSForwardReset(ts);CHKERRQ(ierr); 2645ecf68647SHong Zhang } 2646cd4cee2dSHong Zhang if (ts->quadraturets) { 2647cd4cee2dSHong Zhang ierr = TSReset(ts->quadraturets);CHKERRQ(ierr); 264836eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2649cd4cee2dSHong Zhang } 26501d06f6b3SHong Zhang while (ilink) { 26511d06f6b3SHong Zhang next = ilink->next; 26521d06f6b3SHong Zhang ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr); 26531d06f6b3SHong Zhang ierr = PetscFree(ilink->splitname);CHKERRQ(ierr); 26541d06f6b3SHong Zhang ierr = ISDestroy(&ilink->is);CHKERRQ(ierr); 26551d06f6b3SHong Zhang ierr = PetscFree(ilink);CHKERRQ(ierr); 26561d06f6b3SHong Zhang ilink = next; 265787f4e208SHong Zhang } 2658545aaa6fSHong Zhang ts->num_rhs_splits = 0; 2659277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2660d763cef2SBarry Smith PetscFunctionReturn(0); 2661d763cef2SBarry Smith } 2662d763cef2SBarry Smith 2663d8e5e3e6SSatish Balay /*@ 2664d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2665d763cef2SBarry Smith with TSCreate(). 2666d763cef2SBarry Smith 2667d763cef2SBarry Smith Collective on TS 2668d763cef2SBarry Smith 2669d763cef2SBarry Smith Input Parameter: 2670d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2671d763cef2SBarry Smith 2672d763cef2SBarry Smith Level: beginner 2673d763cef2SBarry Smith 2674d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2675d763cef2SBarry Smith @*/ 26766bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2677d763cef2SBarry Smith { 26786849ba73SBarry Smith PetscErrorCode ierr; 2679d763cef2SBarry Smith 2680d763cef2SBarry Smith PetscFunctionBegin; 26816bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 2682ecf68647SHong Zhang PetscValidHeaderSpecific(*ts,TS_CLASSID,1); 26836bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2684d763cef2SBarry Smith 2685ecf68647SHong Zhang ierr = TSReset(*ts);CHKERRQ(ierr); 2686ecf68647SHong Zhang ierr = TSAdjointReset(*ts);CHKERRQ(ierr); 2687ecf68647SHong Zhang if ((*ts)->forward_solve) { 2688ecf68647SHong Zhang ierr = TSForwardReset(*ts);CHKERRQ(ierr); 2689ecf68647SHong Zhang } 2690e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2691e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 26926bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 26936d4c513bSLisandro Dalcin 2694bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2695bc952696SBarry Smith 269684df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 26976427ac75SLisandro Dalcin ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr); 26986427ac75SLisandro Dalcin 26996bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 27006bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 27016bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 27020dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 27036d4c513bSLisandro Dalcin 2704cd4cee2dSHong Zhang ierr = TSDestroy(&(*ts)->quadraturets);CHKERRQ(ierr); 2705a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2706d763cef2SBarry Smith PetscFunctionReturn(0); 2707d763cef2SBarry Smith } 2708d763cef2SBarry Smith 2709d8e5e3e6SSatish Balay /*@ 2710d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2711d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2712d763cef2SBarry Smith 2713d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2714d763cef2SBarry Smith 2715d763cef2SBarry Smith Input Parameter: 2716d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2717d763cef2SBarry Smith 2718d763cef2SBarry Smith Output Parameter: 2719d763cef2SBarry Smith . snes - the nonlinear solver context 2720d763cef2SBarry Smith 2721d763cef2SBarry Smith Notes: 2722d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2723d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 272494b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2725d763cef2SBarry Smith 2726d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 27270298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2728d763cef2SBarry Smith 2729d763cef2SBarry Smith Level: beginner 2730d763cef2SBarry Smith 2731d763cef2SBarry Smith @*/ 27327087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2733d763cef2SBarry Smith { 2734d372ba47SLisandro Dalcin PetscErrorCode ierr; 2735d372ba47SLisandro Dalcin 2736d763cef2SBarry Smith PetscFunctionBegin; 27370700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27384482741eSBarry Smith PetscValidPointer(snes,2); 2739d372ba47SLisandro Dalcin if (!ts->snes) { 2740ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 274116413a6aSBarry Smith ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr); 27420298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 27433bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2744d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2745496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 27469e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 27479e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 27489e2a6581SJed Brown } 2749d372ba47SLisandro Dalcin } 2750d763cef2SBarry Smith *snes = ts->snes; 2751d763cef2SBarry Smith PetscFunctionReturn(0); 2752d763cef2SBarry Smith } 2753d763cef2SBarry Smith 2754deb2cd25SJed Brown /*@ 2755deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2756deb2cd25SJed Brown 2757deb2cd25SJed Brown Collective 2758deb2cd25SJed Brown 2759deb2cd25SJed Brown Input Parameter: 2760deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2761deb2cd25SJed Brown - snes - the nonlinear solver context 2762deb2cd25SJed Brown 2763deb2cd25SJed Brown Notes: 2764deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2765deb2cd25SJed Brown 2766deb2cd25SJed Brown Level: developer 2767deb2cd25SJed Brown 2768deb2cd25SJed Brown @*/ 2769deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2770deb2cd25SJed Brown { 2771deb2cd25SJed Brown PetscErrorCode ierr; 2772d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2773deb2cd25SJed Brown 2774deb2cd25SJed Brown PetscFunctionBegin; 2775deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2776deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2777deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2778deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2779bbd56ea5SKarl Rupp 2780deb2cd25SJed Brown ts->snes = snes; 2781bbd56ea5SKarl Rupp 27820298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 27830298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2784740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 27850298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2786740132f1SEmil Constantinescu } 2787deb2cd25SJed Brown PetscFunctionReturn(0); 2788deb2cd25SJed Brown } 2789deb2cd25SJed Brown 2790d8e5e3e6SSatish Balay /*@ 279194b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2792d763cef2SBarry Smith a TS (timestepper) context. 2793d763cef2SBarry Smith 279494b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2795d763cef2SBarry Smith 2796d763cef2SBarry Smith Input Parameter: 2797d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2798d763cef2SBarry Smith 2799d763cef2SBarry Smith Output Parameter: 280094b7f48cSBarry Smith . ksp - the nonlinear solver context 2801d763cef2SBarry Smith 2802d763cef2SBarry Smith Notes: 280394b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2804d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2805d763cef2SBarry Smith KSP and PC contexts as well. 2806d763cef2SBarry Smith 280794b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 28080298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2809d763cef2SBarry Smith 2810d763cef2SBarry Smith Level: beginner 2811d763cef2SBarry Smith 2812d763cef2SBarry Smith @*/ 28137087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2814d763cef2SBarry Smith { 2815d372ba47SLisandro Dalcin PetscErrorCode ierr; 2816089b2837SJed Brown SNES snes; 2817d372ba47SLisandro Dalcin 2818d763cef2SBarry Smith PetscFunctionBegin; 28190700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28204482741eSBarry Smith PetscValidPointer(ksp,2); 282117186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2822e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2823089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2824089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2825d763cef2SBarry Smith PetscFunctionReturn(0); 2826d763cef2SBarry Smith } 2827d763cef2SBarry Smith 2828d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2829d763cef2SBarry Smith 2830adb62b0dSMatthew Knepley /*@ 2831618ce8baSLisandro Dalcin TSSetMaxSteps - Sets the maximum number of steps to use. 2832618ce8baSLisandro Dalcin 2833618ce8baSLisandro Dalcin Logically Collective on TS 2834618ce8baSLisandro Dalcin 2835618ce8baSLisandro Dalcin Input Parameters: 2836618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 2837618ce8baSLisandro Dalcin - maxsteps - maximum number of steps to use 2838618ce8baSLisandro Dalcin 2839618ce8baSLisandro Dalcin Options Database Keys: 2840618ce8baSLisandro Dalcin . -ts_max_steps <maxsteps> - Sets maxsteps 2841618ce8baSLisandro Dalcin 2842618ce8baSLisandro Dalcin Notes: 2843618ce8baSLisandro Dalcin The default maximum number of steps is 5000 2844618ce8baSLisandro Dalcin 2845618ce8baSLisandro Dalcin Level: intermediate 2846618ce8baSLisandro Dalcin 2847618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime() 2848618ce8baSLisandro Dalcin @*/ 2849618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps) 2850618ce8baSLisandro Dalcin { 2851618ce8baSLisandro Dalcin PetscFunctionBegin; 2852618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2853618ce8baSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2854618ce8baSLisandro Dalcin if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative"); 2855618ce8baSLisandro Dalcin ts->max_steps = maxsteps; 2856618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2857618ce8baSLisandro Dalcin } 2858618ce8baSLisandro Dalcin 2859618ce8baSLisandro Dalcin /*@ 2860618ce8baSLisandro Dalcin TSGetMaxSteps - Gets the maximum number of steps to use. 2861618ce8baSLisandro Dalcin 2862618ce8baSLisandro Dalcin Not Collective 2863618ce8baSLisandro Dalcin 2864618ce8baSLisandro Dalcin Input Parameters: 2865618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 2866618ce8baSLisandro Dalcin 2867618ce8baSLisandro Dalcin Output Parameter: 2868618ce8baSLisandro Dalcin . maxsteps - maximum number of steps to use 2869618ce8baSLisandro Dalcin 2870618ce8baSLisandro Dalcin Level: advanced 2871618ce8baSLisandro Dalcin 2872618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime() 2873618ce8baSLisandro Dalcin @*/ 2874618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps) 2875618ce8baSLisandro Dalcin { 2876618ce8baSLisandro Dalcin PetscFunctionBegin; 2877618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2878618ce8baSLisandro Dalcin PetscValidIntPointer(maxsteps,2); 2879618ce8baSLisandro Dalcin *maxsteps = ts->max_steps; 2880618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2881618ce8baSLisandro Dalcin } 2882618ce8baSLisandro Dalcin 2883618ce8baSLisandro Dalcin /*@ 2884618ce8baSLisandro Dalcin TSSetMaxTime - Sets the maximum (or final) time for timestepping. 2885618ce8baSLisandro Dalcin 2886618ce8baSLisandro Dalcin Logically Collective on TS 2887618ce8baSLisandro Dalcin 2888618ce8baSLisandro Dalcin Input Parameters: 2889618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 2890618ce8baSLisandro Dalcin - maxtime - final time to step to 2891618ce8baSLisandro Dalcin 2892618ce8baSLisandro Dalcin Options Database Keys: 2893ef85077eSLisandro Dalcin . -ts_max_time <maxtime> - Sets maxtime 2894618ce8baSLisandro Dalcin 2895618ce8baSLisandro Dalcin Notes: 2896618ce8baSLisandro Dalcin The default maximum time is 5.0 2897618ce8baSLisandro Dalcin 2898618ce8baSLisandro Dalcin Level: intermediate 2899618ce8baSLisandro Dalcin 2900618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime() 2901618ce8baSLisandro Dalcin @*/ 2902618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime) 2903618ce8baSLisandro Dalcin { 2904618ce8baSLisandro Dalcin PetscFunctionBegin; 2905618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2906618ce8baSLisandro Dalcin PetscValidLogicalCollectiveReal(ts,maxtime,2); 2907618ce8baSLisandro Dalcin ts->max_time = maxtime; 2908618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2909618ce8baSLisandro Dalcin } 2910618ce8baSLisandro Dalcin 2911618ce8baSLisandro Dalcin /*@ 2912618ce8baSLisandro Dalcin TSGetMaxTime - Gets the maximum (or final) time for timestepping. 2913618ce8baSLisandro Dalcin 2914618ce8baSLisandro Dalcin Not Collective 2915618ce8baSLisandro Dalcin 2916618ce8baSLisandro Dalcin Input Parameters: 2917618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 2918618ce8baSLisandro Dalcin 2919618ce8baSLisandro Dalcin Output Parameter: 2920618ce8baSLisandro Dalcin . maxtime - final time to step to 2921618ce8baSLisandro Dalcin 2922618ce8baSLisandro Dalcin Level: advanced 2923618ce8baSLisandro Dalcin 2924618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps() 2925618ce8baSLisandro Dalcin @*/ 2926618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime) 2927618ce8baSLisandro Dalcin { 2928618ce8baSLisandro Dalcin PetscFunctionBegin; 2929618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2930618ce8baSLisandro Dalcin PetscValidRealPointer(maxtime,2); 2931618ce8baSLisandro Dalcin *maxtime = ts->max_time; 2932618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2933618ce8baSLisandro Dalcin } 2934618ce8baSLisandro Dalcin 2935618ce8baSLisandro Dalcin /*@ 2936aaa6c58dSLisandro Dalcin TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep(). 2937edc382c3SSatish Balay 2938edc382c3SSatish Balay Level: deprecated 2939edc382c3SSatish Balay 2940aaa6c58dSLisandro Dalcin @*/ 2941aaa6c58dSLisandro Dalcin PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 2942aaa6c58dSLisandro Dalcin { 2943aaa6c58dSLisandro Dalcin PetscErrorCode ierr; 2944aaa6c58dSLisandro Dalcin PetscFunctionBegin; 2945aaa6c58dSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2946aaa6c58dSLisandro Dalcin ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 2947aaa6c58dSLisandro Dalcin ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 2948aaa6c58dSLisandro Dalcin PetscFunctionReturn(0); 2949aaa6c58dSLisandro Dalcin } 2950aaa6c58dSLisandro Dalcin 2951aaa6c58dSLisandro Dalcin /*@ 295219eac22cSLisandro Dalcin TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime(). 2953edc382c3SSatish Balay 2954edc382c3SSatish Balay Level: deprecated 2955edc382c3SSatish Balay 2956adb62b0dSMatthew Knepley @*/ 29577087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2958adb62b0dSMatthew Knepley { 2959adb62b0dSMatthew Knepley PetscFunctionBegin; 29600700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2961abc0a331SBarry Smith if (maxsteps) { 29624482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2963adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2964adb62b0dSMatthew Knepley } 2965abc0a331SBarry Smith if (maxtime) { 29664482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2967adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2968adb62b0dSMatthew Knepley } 2969adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2970adb62b0dSMatthew Knepley } 2971adb62b0dSMatthew Knepley 2972d763cef2SBarry Smith /*@ 297319eac22cSLisandro Dalcin TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime(). 2974edc382c3SSatish Balay 2975edc382c3SSatish Balay Level: deprecated 2976edc382c3SSatish Balay 2977d763cef2SBarry Smith @*/ 29787087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2979d763cef2SBarry Smith { 2980d763cef2SBarry Smith PetscFunctionBegin; 29810700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2982c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2983c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 298439b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 298539b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2986d763cef2SBarry Smith PetscFunctionReturn(0); 2987d763cef2SBarry Smith } 2988d763cef2SBarry Smith 2989d763cef2SBarry Smith /*@ 29905c5f5948SLisandro Dalcin TSGetTimeStepNumber - Deprecated, use TSGetStepNumber(). 2991edc382c3SSatish Balay 2992edc382c3SSatish Balay Level: deprecated 2993edc382c3SSatish Balay 29945c5f5948SLisandro Dalcin @*/ 2995e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 29965c5f5948SLisandro Dalcin 299719eac22cSLisandro Dalcin /*@ 29984f4e0956SLisandro Dalcin TSGetTotalSteps - Deprecated, use TSGetStepNumber(). 2999edc382c3SSatish Balay 3000edc382c3SSatish Balay Level: deprecated 3001edc382c3SSatish Balay 30024f4e0956SLisandro Dalcin @*/ 30034f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 30044f4e0956SLisandro Dalcin 30054f4e0956SLisandro Dalcin /*@ 3006d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 3007d763cef2SBarry Smith for use by the TS routines. 3008d763cef2SBarry Smith 3009d083f849SBarry Smith Logically Collective on TS 3010d763cef2SBarry Smith 3011d763cef2SBarry Smith Input Parameters: 3012d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 30130910c330SBarry Smith - u - the solution vector 3014d763cef2SBarry Smith 3015d763cef2SBarry Smith Level: beginner 3016d763cef2SBarry Smith 30170ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate() 3018d763cef2SBarry Smith @*/ 30190910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 3020d763cef2SBarry Smith { 30218737fe31SLisandro Dalcin PetscErrorCode ierr; 3022496e6a7aSJed Brown DM dm; 30238737fe31SLisandro Dalcin 3024d763cef2SBarry Smith PetscFunctionBegin; 30250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30260910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 30270910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 30286bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 30290910c330SBarry Smith ts->vec_sol = u; 3030bbd56ea5SKarl Rupp 3031496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 30320910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 3033d763cef2SBarry Smith PetscFunctionReturn(0); 3034d763cef2SBarry Smith } 3035d763cef2SBarry Smith 3036ac226902SBarry Smith /*@C 3037000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 30383f2090d5SJed Brown called once at the beginning of each time step. 3039000e7ae3SMatthew Knepley 30403f9fe445SBarry Smith Logically Collective on TS 3041000e7ae3SMatthew Knepley 3042000e7ae3SMatthew Knepley Input Parameters: 3043000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3044000e7ae3SMatthew Knepley - func - The function 3045000e7ae3SMatthew Knepley 3046000e7ae3SMatthew Knepley Calling sequence of func: 30476bc98fa9SBarry Smith . PetscErrorCode func (TS ts); 3048000e7ae3SMatthew Knepley 3049000e7ae3SMatthew Knepley Level: intermediate 3050000e7ae3SMatthew Knepley 3051dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep() 3052000e7ae3SMatthew Knepley @*/ 30537087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3054000e7ae3SMatthew Knepley { 3055000e7ae3SMatthew Knepley PetscFunctionBegin; 30560700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3057ae60f76fSBarry Smith ts->prestep = func; 3058000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3059000e7ae3SMatthew Knepley } 3060000e7ae3SMatthew Knepley 306109ee8438SJed Brown /*@ 30623f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 30633f2090d5SJed Brown 30643f2090d5SJed Brown Collective on TS 30653f2090d5SJed Brown 30663f2090d5SJed Brown Input Parameters: 30673f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 30683f2090d5SJed Brown 30693f2090d5SJed Brown Notes: 30703f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 30713f2090d5SJed Brown so most users would not generally call this routine themselves. 30723f2090d5SJed Brown 30733f2090d5SJed Brown Level: developer 30743f2090d5SJed Brown 30759be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 30763f2090d5SJed Brown @*/ 30777087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 30783f2090d5SJed Brown { 30793f2090d5SJed Brown PetscErrorCode ierr; 30803f2090d5SJed Brown 30813f2090d5SJed Brown PetscFunctionBegin; 30820700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3083ae60f76fSBarry Smith if (ts->prestep) { 30845efd42a4SStefano Zampini Vec U; 30855efd42a4SStefano Zampini PetscObjectState sprev,spost; 30865efd42a4SStefano Zampini 30875efd42a4SStefano Zampini ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 30885efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3089ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 30905efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3091dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 3092312ce896SJed Brown } 30933f2090d5SJed Brown PetscFunctionReturn(0); 30943f2090d5SJed Brown } 30953f2090d5SJed Brown 3096b8123daeSJed Brown /*@C 3097b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3098b8123daeSJed Brown called once at the beginning of each stage. 3099b8123daeSJed Brown 3100b8123daeSJed Brown Logically Collective on TS 3101b8123daeSJed Brown 3102b8123daeSJed Brown Input Parameters: 3103b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 3104b8123daeSJed Brown - func - The function 3105b8123daeSJed Brown 3106b8123daeSJed Brown Calling sequence of func: 3107b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 3108b8123daeSJed Brown 3109b8123daeSJed Brown Level: intermediate 3110b8123daeSJed Brown 3111b8123daeSJed Brown Note: 3112b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 311380275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 3114b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 3115b8123daeSJed Brown 31169be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3117b8123daeSJed Brown @*/ 3118b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 3119b8123daeSJed Brown { 3120b8123daeSJed Brown PetscFunctionBegin; 3121b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3122ae60f76fSBarry Smith ts->prestage = func; 3123b8123daeSJed Brown PetscFunctionReturn(0); 3124b8123daeSJed Brown } 3125b8123daeSJed Brown 31269be3e283SDebojyoti Ghosh /*@C 31279be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 31289be3e283SDebojyoti Ghosh called once at the end of each stage. 31299be3e283SDebojyoti Ghosh 31309be3e283SDebojyoti Ghosh Logically Collective on TS 31319be3e283SDebojyoti Ghosh 31329be3e283SDebojyoti Ghosh Input Parameters: 31339be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 31349be3e283SDebojyoti Ghosh - func - The function 31359be3e283SDebojyoti Ghosh 31369be3e283SDebojyoti Ghosh Calling sequence of func: 31379be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 31389be3e283SDebojyoti Ghosh 31399be3e283SDebojyoti Ghosh Level: intermediate 31409be3e283SDebojyoti Ghosh 31419be3e283SDebojyoti Ghosh Note: 31429be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 314380275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 31449be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 31459be3e283SDebojyoti Ghosh 31469be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 31479be3e283SDebojyoti Ghosh @*/ 31489be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 31499be3e283SDebojyoti Ghosh { 31509be3e283SDebojyoti Ghosh PetscFunctionBegin; 31519be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 31529be3e283SDebojyoti Ghosh ts->poststage = func; 31539be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 31549be3e283SDebojyoti Ghosh } 31559be3e283SDebojyoti Ghosh 3156c688d042SShri Abhyankar /*@C 3157c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 3158c688d042SShri Abhyankar called once at the end of each step evaluation. 3159c688d042SShri Abhyankar 3160c688d042SShri Abhyankar Logically Collective on TS 3161c688d042SShri Abhyankar 3162c688d042SShri Abhyankar Input Parameters: 3163c688d042SShri Abhyankar + ts - The TS context obtained from TSCreate() 3164c688d042SShri Abhyankar - func - The function 3165c688d042SShri Abhyankar 3166c688d042SShri Abhyankar Calling sequence of func: 3167c688d042SShri Abhyankar . PetscErrorCode func(TS ts); 3168c688d042SShri Abhyankar 3169c688d042SShri Abhyankar Level: intermediate 3170c688d042SShri Abhyankar 3171c688d042SShri Abhyankar Note: 31721785ff2aSShri Abhyankar Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling 31731785ff2aSShri Abhyankar thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep() 3174e7e94ed4SShri Abhyankar may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step 3175e7e94ed4SShri Abhyankar solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step 3176e7e94ed4SShri Abhyankar with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime() 3177c688d042SShri Abhyankar 3178c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3179c688d042SShri Abhyankar @*/ 3180c688d042SShri Abhyankar PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS)) 3181c688d042SShri Abhyankar { 3182c688d042SShri Abhyankar PetscFunctionBegin; 3183c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3184c688d042SShri Abhyankar ts->postevaluate = func; 3185c688d042SShri Abhyankar PetscFunctionReturn(0); 3186c688d042SShri Abhyankar } 3187c688d042SShri Abhyankar 3188b8123daeSJed Brown /*@ 3189b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 3190b8123daeSJed Brown 3191b8123daeSJed Brown Collective on TS 3192b8123daeSJed Brown 3193b8123daeSJed Brown Input Parameters: 3194b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 31959be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3196b8123daeSJed Brown 3197b8123daeSJed Brown Notes: 3198b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 3199b8123daeSJed Brown most users would not generally call this routine themselves. 3200b8123daeSJed Brown 3201b8123daeSJed Brown Level: developer 3202b8123daeSJed Brown 32039be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 3204b8123daeSJed Brown @*/ 3205b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3206b8123daeSJed Brown { 3207b8123daeSJed Brown PetscFunctionBegin; 3208b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3209ae60f76fSBarry Smith if (ts->prestage) { 3210ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 3211b8123daeSJed Brown } 3212b8123daeSJed Brown PetscFunctionReturn(0); 3213b8123daeSJed Brown } 3214b8123daeSJed Brown 32159be3e283SDebojyoti Ghosh /*@ 32169be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 32179be3e283SDebojyoti Ghosh 32189be3e283SDebojyoti Ghosh Collective on TS 32199be3e283SDebojyoti Ghosh 32209be3e283SDebojyoti Ghosh Input Parameters: 32219be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 32229be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 32239be3e283SDebojyoti Ghosh stageindex - Stage number 32249be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 32259be3e283SDebojyoti Ghosh of stages) with the stage solutions 32269be3e283SDebojyoti Ghosh 32279be3e283SDebojyoti Ghosh Notes: 32289be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 32299be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 32309be3e283SDebojyoti Ghosh 32319be3e283SDebojyoti Ghosh Level: developer 32329be3e283SDebojyoti Ghosh 32339be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 32349be3e283SDebojyoti Ghosh @*/ 32359be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 32369be3e283SDebojyoti Ghosh { 32379be3e283SDebojyoti Ghosh PetscFunctionBegin; 32389be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 32394beae5d8SLisandro Dalcin if (ts->poststage) { 32409be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 32419be3e283SDebojyoti Ghosh } 32429be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 32439be3e283SDebojyoti Ghosh } 32449be3e283SDebojyoti Ghosh 3245c688d042SShri Abhyankar /*@ 3246c688d042SShri Abhyankar TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate() 3247c688d042SShri Abhyankar 3248c688d042SShri Abhyankar Collective on TS 3249c688d042SShri Abhyankar 3250c688d042SShri Abhyankar Input Parameters: 3251c688d042SShri Abhyankar . ts - The TS context obtained from TSCreate() 3252c688d042SShri Abhyankar 3253c688d042SShri Abhyankar Notes: 3254c688d042SShri Abhyankar TSPostEvaluate() is typically used within time stepping implementations, 3255c688d042SShri Abhyankar most users would not generally call this routine themselves. 3256c688d042SShri Abhyankar 3257c688d042SShri Abhyankar Level: developer 3258c688d042SShri Abhyankar 3259c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep() 3260c688d042SShri Abhyankar @*/ 3261c688d042SShri Abhyankar PetscErrorCode TSPostEvaluate(TS ts) 3262c688d042SShri Abhyankar { 3263c688d042SShri Abhyankar PetscErrorCode ierr; 3264c688d042SShri Abhyankar 3265c688d042SShri Abhyankar PetscFunctionBegin; 3266c688d042SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3267c688d042SShri Abhyankar if (ts->postevaluate) { 3268dcb233daSLisandro Dalcin Vec U; 3269dcb233daSLisandro Dalcin PetscObjectState sprev,spost; 3270dcb233daSLisandro Dalcin 3271dcb233daSLisandro Dalcin ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 3272dcb233daSLisandro Dalcin ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3273c688d042SShri Abhyankar PetscStackCallStandard((*ts->postevaluate),(ts)); 3274dcb233daSLisandro Dalcin ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3275dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 3276c688d042SShri Abhyankar } 3277c688d042SShri Abhyankar PetscFunctionReturn(0); 3278c688d042SShri Abhyankar } 3279c688d042SShri Abhyankar 3280ac226902SBarry Smith /*@C 3281000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 32823f2090d5SJed Brown called once at the end of each time step. 3283000e7ae3SMatthew Knepley 32843f9fe445SBarry Smith Logically Collective on TS 3285000e7ae3SMatthew Knepley 3286000e7ae3SMatthew Knepley Input Parameters: 3287000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3288000e7ae3SMatthew Knepley - func - The function 3289000e7ae3SMatthew Knepley 3290000e7ae3SMatthew Knepley Calling sequence of func: 3291b8123daeSJed Brown $ func (TS ts); 3292000e7ae3SMatthew Knepley 32931785ff2aSShri Abhyankar Notes: 32941785ff2aSShri Abhyankar The function set by TSSetPostStep() is called after each successful step. The solution vector X 32951785ff2aSShri Abhyankar obtained by TSGetSolution() may be different than that computed at the step end if the event handler 32961785ff2aSShri Abhyankar locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead. 32971785ff2aSShri Abhyankar 3298000e7ae3SMatthew Knepley Level: intermediate 3299000e7ae3SMatthew Knepley 3300dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep() 3301000e7ae3SMatthew Knepley @*/ 33027087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 3303000e7ae3SMatthew Knepley { 3304000e7ae3SMatthew Knepley PetscFunctionBegin; 33050700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3306ae60f76fSBarry Smith ts->poststep = func; 3307000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3308000e7ae3SMatthew Knepley } 3309000e7ae3SMatthew Knepley 331009ee8438SJed Brown /*@ 33113f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 33123f2090d5SJed Brown 33133f2090d5SJed Brown Collective on TS 33143f2090d5SJed Brown 33153f2090d5SJed Brown Input Parameters: 33163f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 33173f2090d5SJed Brown 33183f2090d5SJed Brown Notes: 33193f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 33203f2090d5SJed Brown so most users would not generally call this routine themselves. 33213f2090d5SJed Brown 33223f2090d5SJed Brown Level: developer 33233f2090d5SJed Brown 33243f2090d5SJed Brown @*/ 33257087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 33263f2090d5SJed Brown { 33273f2090d5SJed Brown PetscErrorCode ierr; 33283f2090d5SJed Brown 33293f2090d5SJed Brown PetscFunctionBegin; 33300700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3331ae60f76fSBarry Smith if (ts->poststep) { 33325efd42a4SStefano Zampini Vec U; 33335efd42a4SStefano Zampini PetscObjectState sprev,spost; 33345efd42a4SStefano Zampini 33355efd42a4SStefano Zampini ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 33365efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3337ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 33385efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3339dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 334072ac3e02SJed Brown } 33413f2090d5SJed Brown PetscFunctionReturn(0); 33423f2090d5SJed Brown } 33433f2090d5SJed Brown 3344d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 3345d763cef2SBarry Smith 3346d763cef2SBarry Smith /*@C 3347a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 3348d763cef2SBarry Smith timestep to display the iteration's progress. 3349d763cef2SBarry Smith 33503f9fe445SBarry Smith Logically Collective on TS 3351d763cef2SBarry Smith 3352d763cef2SBarry Smith Input Parameters: 3353d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3354e213d8f1SJed Brown . monitor - monitoring routine 3355329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 33560298fd71SBarry Smith monitor routine (use NULL if no context is desired) 3357b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 33580298fd71SBarry Smith (may be NULL) 3359d763cef2SBarry Smith 3360e213d8f1SJed Brown Calling sequence of monitor: 3361dcb233daSLisandro Dalcin $ PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 3362d763cef2SBarry Smith 3363d763cef2SBarry Smith + ts - the TS context 336463e21af5SBarry 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) 33651f06c33eSBarry Smith . time - current time 33660910c330SBarry Smith . u - current iterate 3367d763cef2SBarry Smith - mctx - [optional] monitoring context 3368d763cef2SBarry Smith 3369d763cef2SBarry Smith Notes: 3370d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 3371d763cef2SBarry Smith already has been loaded. 3372d763cef2SBarry Smith 337395452b02SPatrick Sanan Fortran Notes: 337495452b02SPatrick Sanan Only a single monitor function can be set for each TS object 3375025f1a04SBarry Smith 3376d763cef2SBarry Smith Level: intermediate 3377d763cef2SBarry Smith 3378a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 3379d763cef2SBarry Smith @*/ 3380c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 3381d763cef2SBarry Smith { 338278064530SBarry Smith PetscErrorCode ierr; 338378064530SBarry Smith PetscInt i; 338478064530SBarry Smith PetscBool identical; 338578064530SBarry Smith 3386d763cef2SBarry Smith PetscFunctionBegin; 33870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 338878064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 338978064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr); 339078064530SBarry Smith if (identical) PetscFunctionReturn(0); 339178064530SBarry Smith } 339217186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 3393d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 33948704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 3395d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 3396d763cef2SBarry Smith PetscFunctionReturn(0); 3397d763cef2SBarry Smith } 3398d763cef2SBarry Smith 3399d763cef2SBarry Smith /*@C 3400a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 3401d763cef2SBarry Smith 34023f9fe445SBarry Smith Logically Collective on TS 3403d763cef2SBarry Smith 3404d763cef2SBarry Smith Input Parameters: 3405d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3406d763cef2SBarry Smith 3407d763cef2SBarry Smith Notes: 3408d763cef2SBarry Smith There is no way to remove a single, specific monitor. 3409d763cef2SBarry Smith 3410d763cef2SBarry Smith Level: intermediate 3411d763cef2SBarry Smith 3412a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 3413d763cef2SBarry Smith @*/ 34147087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 3415d763cef2SBarry Smith { 3416d952e501SBarry Smith PetscErrorCode ierr; 3417d952e501SBarry Smith PetscInt i; 3418d952e501SBarry Smith 3419d763cef2SBarry Smith PetscFunctionBegin; 34200700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3421d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 34228704b422SBarry Smith if (ts->monitordestroy[i]) { 34238704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 3424d952e501SBarry Smith } 3425d952e501SBarry Smith } 3426d763cef2SBarry Smith ts->numbermonitors = 0; 3427d763cef2SBarry Smith PetscFunctionReturn(0); 3428d763cef2SBarry Smith } 3429d763cef2SBarry Smith 3430721cd6eeSBarry Smith /*@C 3431721cd6eeSBarry Smith TSMonitorDefault - The Default monitor, prints the timestep and time for each step 34325516499fSSatish Balay 34335516499fSSatish Balay Level: intermediate 343441251cbbSSatish Balay 343563e21af5SBarry Smith .seealso: TSMonitorSet() 343641251cbbSSatish Balay @*/ 3437721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf) 3438d763cef2SBarry Smith { 3439dfbe8321SBarry Smith PetscErrorCode ierr; 3440721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 344141aca3d6SBarry Smith PetscBool iascii,ibinary; 3442d132466eSBarry Smith 3443d763cef2SBarry Smith PetscFunctionBegin; 34444d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 344541aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 344641aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr); 3447721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 344841aca3d6SBarry Smith if (iascii) { 3449649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 345063e21af5SBarry Smith if (step == -1){ /* this indicates it is an interpolated solution */ 345163e21af5SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr); 345263e21af5SBarry Smith } else { 34538392e04aSShri 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); 345463e21af5SBarry Smith } 3455649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 345641aca3d6SBarry Smith } else if (ibinary) { 345741aca3d6SBarry Smith PetscMPIInt rank; 345841aca3d6SBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 345941aca3d6SBarry Smith if (!rank) { 3460450a797fSBarry Smith PetscBool skipHeader; 3461450a797fSBarry Smith PetscInt classid = REAL_FILE_CLASSID; 3462450a797fSBarry Smith 3463450a797fSBarry Smith ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr); 3464450a797fSBarry Smith if (!skipHeader) { 3465450a797fSBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 3466450a797fSBarry Smith } 346741aca3d6SBarry Smith ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr); 346841aca3d6SBarry Smith } else { 346941aca3d6SBarry Smith ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr); 347041aca3d6SBarry Smith } 347141aca3d6SBarry Smith } 3472721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3473d763cef2SBarry Smith PetscFunctionReturn(0); 3474d763cef2SBarry Smith } 3475d763cef2SBarry Smith 3476cc9c3a59SBarry Smith /*@C 3477cc9c3a59SBarry Smith TSMonitorExtreme - Prints the extreme values of the solution at each timestep 3478cc9c3a59SBarry Smith 3479cc9c3a59SBarry Smith Level: intermediate 3480cc9c3a59SBarry Smith 3481cc9c3a59SBarry Smith .seealso: TSMonitorSet() 3482cc9c3a59SBarry Smith @*/ 3483cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf) 3484cc9c3a59SBarry Smith { 3485cc9c3a59SBarry Smith PetscErrorCode ierr; 3486cc9c3a59SBarry Smith PetscViewer viewer = vf->viewer; 3487cc9c3a59SBarry Smith PetscBool iascii; 3488cc9c3a59SBarry Smith PetscReal max,min; 3489cc9c3a59SBarry Smith 3490cc9c3a59SBarry Smith 3491cc9c3a59SBarry Smith PetscFunctionBegin; 3492cc9c3a59SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3493cc9c3a59SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 3494cc9c3a59SBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 3495cc9c3a59SBarry Smith if (iascii) { 3496cc9c3a59SBarry Smith ierr = VecMax(v,NULL,&max);CHKERRQ(ierr); 3497cc9c3a59SBarry Smith ierr = VecMin(v,NULL,&min);CHKERRQ(ierr); 3498cc9c3a59SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 34995132926bSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s max %g min %g\n",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)" : "",(double)max,(double)min);CHKERRQ(ierr); 3500cc9c3a59SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3501cc9c3a59SBarry Smith } 3502cc9c3a59SBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3503cc9c3a59SBarry Smith PetscFunctionReturn(0); 3504cc9c3a59SBarry Smith } 3505cc9c3a59SBarry Smith 3506cd652676SJed Brown /*@ 3507cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3508cd652676SJed Brown 3509cd652676SJed Brown Collective on TS 3510cd652676SJed Brown 3511cd652676SJed Brown Input Argument: 3512cd652676SJed Brown + ts - time stepping context 3513cd652676SJed Brown - t - time to interpolate to 3514cd652676SJed Brown 3515cd652676SJed Brown Output Argument: 35160910c330SBarry Smith . U - state at given time 3517cd652676SJed Brown 3518cd652676SJed Brown Level: intermediate 3519cd652676SJed Brown 3520cd652676SJed Brown Developer Notes: 3521cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3522cd652676SJed Brown 3523874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve() 3524cd652676SJed Brown @*/ 35250910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3526cd652676SJed Brown { 3527cd652676SJed Brown PetscErrorCode ierr; 3528cd652676SJed Brown 3529cd652676SJed Brown PetscFunctionBegin; 3530cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3531b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3532be5899b3SLisandro 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); 3533ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 35340910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3535cd652676SJed Brown PetscFunctionReturn(0); 3536cd652676SJed Brown } 3537cd652676SJed Brown 3538d763cef2SBarry Smith /*@ 35396d9e5789SSean Farley TSStep - Steps one time step 3540d763cef2SBarry Smith 3541d763cef2SBarry Smith Collective on TS 3542d763cef2SBarry Smith 3543d763cef2SBarry Smith Input Parameter: 3544d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3545d763cef2SBarry Smith 354627829d71SBarry Smith Level: developer 3547d763cef2SBarry Smith 3548b8123daeSJed Brown Notes: 354927829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 355027829d71SBarry Smith 3551b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3552b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3553b8123daeSJed Brown 355419eac22cSLisandro Dalcin This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the 355519eac22cSLisandro Dalcin time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 355625cb2221SBarry Smith 35579be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3558d763cef2SBarry Smith @*/ 3559193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3560d763cef2SBarry Smith { 3561dfbe8321SBarry Smith PetscErrorCode ierr; 3562fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3563be5899b3SLisandro Dalcin PetscReal ptime; 3564d763cef2SBarry Smith 3565d763cef2SBarry Smith PetscFunctionBegin; 35660700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3567fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3568fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3569fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3570fffbeea8SBarry Smith " type = {Preprint},\n" 3571fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3572fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3573302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3574fffbeea8SBarry Smith 3575d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 357668bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3577d405a339SMatthew Knepley 3578ef85077eSLisandro 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>"); 3579a6772fa2SLisandro 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()"); 3580be5899b3SLisandro 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"); 3581a6772fa2SLisandro Dalcin 3582be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 3583be5899b3SLisandro Dalcin ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev; 35842808aa04SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3585e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3586d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3587193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3588d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3589be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 35902808aa04SLisandro Dalcin ts->steps++; 3591be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 359228d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3593362cd11cSLisandro Dalcin 3594362cd11cSLisandro Dalcin if (ts->reason < 0) { 3595cef5090cSJed Brown if (ts->errorifstepfailed) { 359608c7845fSBarry 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]); 359708c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3598d2daff3dSHong Zhang } 359908c7845fSBarry Smith } else if (!ts->reason) { 360008c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 360108c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 360208c7845fSBarry Smith } 360308c7845fSBarry Smith PetscFunctionReturn(0); 360408c7845fSBarry Smith } 360508c7845fSBarry Smith 360608c7845fSBarry Smith /*@ 36077cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 36087cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 36097cbde773SLisandro Dalcin 36107cbde773SLisandro Dalcin Collective on TS 36117cbde773SLisandro Dalcin 36127cbde773SLisandro Dalcin Input Arguments: 36137cbde773SLisandro Dalcin + ts - time stepping context 36147cbde773SLisandro Dalcin . wnormtype - norm type, either NORM_2 or NORM_INFINITY 36157cbde773SLisandro Dalcin - order - optional, desired order for the error evaluation or PETSC_DECIDE 36167cbde773SLisandro Dalcin 36177cbde773SLisandro Dalcin Output Arguments: 36187cbde773SLisandro Dalcin + order - optional, the actual order of the error evaluation 36197cbde773SLisandro Dalcin - wlte - the weighted local truncation error norm 36207cbde773SLisandro Dalcin 36217cbde773SLisandro Dalcin Level: advanced 36227cbde773SLisandro Dalcin 36237cbde773SLisandro Dalcin Notes: 36247cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 36257cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 36267cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 36277cbde773SLisandro Dalcin 36287cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm() 36297cbde773SLisandro Dalcin @*/ 36307cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte) 36317cbde773SLisandro Dalcin { 36327cbde773SLisandro Dalcin PetscErrorCode ierr; 36337cbde773SLisandro Dalcin 36347cbde773SLisandro Dalcin PetscFunctionBegin; 36357cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36367cbde773SLisandro Dalcin PetscValidType(ts,1); 36377cbde773SLisandro Dalcin PetscValidLogicalCollectiveEnum(ts,wnormtype,4); 36387cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order,3); 36397cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts,*order,3); 36407cbde773SLisandro Dalcin PetscValidRealPointer(wlte,4); 36417cbde773SLisandro Dalcin if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 36427cbde773SLisandro Dalcin if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name); 36437cbde773SLisandro Dalcin ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr); 36447cbde773SLisandro Dalcin PetscFunctionReturn(0); 36457cbde773SLisandro Dalcin } 36467cbde773SLisandro Dalcin 364705175c85SJed Brown /*@ 364805175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 364905175c85SJed Brown 36501c3436cfSJed Brown Collective on TS 365105175c85SJed Brown 365205175c85SJed Brown Input Arguments: 36531c3436cfSJed Brown + ts - time stepping context 36541c3436cfSJed Brown . order - desired order of accuracy 36550298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 365605175c85SJed Brown 365705175c85SJed Brown Output Arguments: 36580910c330SBarry Smith . U - state at the end of the current step 365905175c85SJed Brown 366005175c85SJed Brown Level: advanced 366105175c85SJed Brown 3662108c343cSJed Brown Notes: 3663108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3664108c343cSJed 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. 3665108c343cSJed Brown 36661c3436cfSJed Brown .seealso: TSStep(), TSAdapt 366705175c85SJed Brown @*/ 36680910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 366905175c85SJed Brown { 367005175c85SJed Brown PetscErrorCode ierr; 367105175c85SJed Brown 367205175c85SJed Brown PetscFunctionBegin; 367305175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 367405175c85SJed Brown PetscValidType(ts,1); 36750910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3676ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 36770910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 367805175c85SJed Brown PetscFunctionReturn(0); 367905175c85SJed Brown } 368005175c85SJed Brown 3681aad739acSMatthew G. Knepley /*@C 3682*2e61be88SMatthew G. Knepley TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping. 3683aad739acSMatthew G. Knepley 3684aad739acSMatthew G. Knepley Not collective 3685aad739acSMatthew G. Knepley 3686aad739acSMatthew G. Knepley Input Argument: 3687aad739acSMatthew G. Knepley . ts - time stepping context 3688aad739acSMatthew G. Knepley 3689aad739acSMatthew G. Knepley Output Argument: 3690*2e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition 3691aad739acSMatthew G. Knepley 3692aad739acSMatthew G. Knepley Level: advanced 3693aad739acSMatthew G. Knepley 3694aad739acSMatthew G. Knepley Notes: 3695aad739acSMatthew G. Knepley The calling sequence for the function is 3696*2e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u) 3697aad739acSMatthew G. Knepley $ ts - The timestepping context 3698*2e61be88SMatthew G. Knepley $ u - The input vector in which the initial condition is stored 3699aad739acSMatthew G. Knepley 3700*2e61be88SMatthew G. Knepley .seealso: TSSetComputeInitialCondition(), TSComputeInitialCondition() 3701aad739acSMatthew G. Knepley @*/ 3702*2e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec)) 3703aad739acSMatthew G. Knepley { 3704aad739acSMatthew G. Knepley PetscFunctionBegin; 3705aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3706*2e61be88SMatthew G. Knepley PetscValidPointer(initCondition, 2); 3707*2e61be88SMatthew G. Knepley *initCondition = ts->ops->initcondition; 3708aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3709aad739acSMatthew G. Knepley } 3710aad739acSMatthew G. Knepley 3711aad739acSMatthew G. Knepley /*@C 3712*2e61be88SMatthew G. Knepley TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping. 3713aad739acSMatthew G. Knepley 3714aad739acSMatthew G. Knepley Logically collective on ts 3715aad739acSMatthew G. Knepley 3716aad739acSMatthew G. Knepley Input Arguments: 3717aad739acSMatthew G. Knepley + ts - time stepping context 3718*2e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition 3719aad739acSMatthew G. Knepley 3720aad739acSMatthew G. Knepley Level: advanced 3721aad739acSMatthew G. Knepley 3722aad739acSMatthew G. Knepley Notes: 3723aad739acSMatthew G. Knepley The calling sequence for the function is 3724*2e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u) 3725aad739acSMatthew G. Knepley $ ts - The timestepping context 3726*2e61be88SMatthew G. Knepley $ u - The input vector in which the initial condition is stored 3727aad739acSMatthew G. Knepley 3728*2e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSComputeInitialCondition() 3729aad739acSMatthew G. Knepley @*/ 3730*2e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec)) 3731aad739acSMatthew G. Knepley { 3732aad739acSMatthew G. Knepley PetscFunctionBegin; 3733aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3734*2e61be88SMatthew G. Knepley PetscValidFunction(initCondition, 2); 3735*2e61be88SMatthew G. Knepley ts->ops->initcondition = initCondition; 3736aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3737aad739acSMatthew G. Knepley } 3738aad739acSMatthew G. Knepley 3739aad739acSMatthew G. Knepley /*@ 3740*2e61be88SMatthew G. Knepley TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set. 3741aad739acSMatthew G. Knepley 3742aad739acSMatthew G. Knepley Collective on ts 3743aad739acSMatthew G. Knepley 3744aad739acSMatthew G. Knepley Input Arguments: 3745aad739acSMatthew G. Knepley + ts - time stepping context 3746*2e61be88SMatthew G. Knepley - u - The Vec to store the condition in which will be used in TSSolve() 3747aad739acSMatthew G. Knepley 3748aad739acSMatthew G. Knepley Level: advanced 3749aad739acSMatthew G. Knepley 3750aad739acSMatthew G. Knepley Notes: 3751aad739acSMatthew G. Knepley The calling sequence for the function is 3752*2e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u) 3753aad739acSMatthew G. Knepley $ ts - The timestepping context 3754*2e61be88SMatthew G. Knepley $ u - The input vector in which the initial condition is stored 3755aad739acSMatthew G. Knepley 3756*2e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve() 3757aad739acSMatthew G. Knepley @*/ 3758*2e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u) 3759aad739acSMatthew G. Knepley { 3760aad739acSMatthew G. Knepley PetscErrorCode ierr; 3761aad739acSMatthew G. Knepley 3762aad739acSMatthew G. Knepley PetscFunctionBegin; 3763aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3764aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3765*2e61be88SMatthew G. Knepley if (ts->ops->initcondition) {ierr = (*ts->ops->initcondition)(ts, u);CHKERRQ(ierr);} 3766aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3767aad739acSMatthew G. Knepley } 3768aad739acSMatthew G. Knepley 3769aad739acSMatthew G. Knepley /*@C 3770aad739acSMatthew G. Knepley TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping. 3771aad739acSMatthew G. Knepley 3772aad739acSMatthew G. Knepley Not collective 3773aad739acSMatthew G. Knepley 3774aad739acSMatthew G. Knepley Input Argument: 3775aad739acSMatthew G. Knepley . ts - time stepping context 3776aad739acSMatthew G. Knepley 3777aad739acSMatthew G. Knepley Output Argument: 3778aad739acSMatthew G. Knepley . exactError - The function which computes the solution error 3779aad739acSMatthew G. Knepley 3780aad739acSMatthew G. Knepley Level: advanced 3781aad739acSMatthew G. Knepley 3782aad739acSMatthew G. Knepley Notes: 3783aad739acSMatthew G. Knepley The calling sequence for the function is 3784aad739acSMatthew G. Knepley $ exactError(TS ts, Vec u) 3785aad739acSMatthew G. Knepley $ ts - The timestepping context 3786aad739acSMatthew G. Knepley $ u - The approximate solution vector 3787aad739acSMatthew G. Knepley $ e - The input vector in which the error is stored 3788aad739acSMatthew G. Knepley 3789aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError() 3790aad739acSMatthew G. Knepley @*/ 3791aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec)) 3792aad739acSMatthew G. Knepley { 3793aad739acSMatthew G. Knepley PetscFunctionBegin; 3794aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3795aad739acSMatthew G. Knepley PetscValidPointer(exactError, 2); 3796aad739acSMatthew G. Knepley *exactError = ts->ops->exacterror; 3797aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3798aad739acSMatthew G. Knepley } 3799aad739acSMatthew G. Knepley 3800aad739acSMatthew G. Knepley /*@C 3801aad739acSMatthew G. Knepley TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping. 3802aad739acSMatthew G. Knepley 3803aad739acSMatthew G. Knepley Logically collective on ts 3804aad739acSMatthew G. Knepley 3805aad739acSMatthew G. Knepley Input Arguments: 3806aad739acSMatthew G. Knepley + ts - time stepping context 3807aad739acSMatthew G. Knepley - exactError - The function which computes the solution error 3808aad739acSMatthew G. Knepley 3809aad739acSMatthew G. Knepley Level: advanced 3810aad739acSMatthew G. Knepley 3811aad739acSMatthew G. Knepley Notes: 3812aad739acSMatthew G. Knepley The calling sequence for the function is 3813aad739acSMatthew G. Knepley $ exactError(TS ts, Vec u) 3814aad739acSMatthew G. Knepley $ ts - The timestepping context 3815aad739acSMatthew G. Knepley $ u - The approximate solution vector 3816aad739acSMatthew G. Knepley $ e - The input vector in which the error is stored 3817aad739acSMatthew G. Knepley 3818aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError() 3819aad739acSMatthew G. Knepley @*/ 3820aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec)) 3821aad739acSMatthew G. Knepley { 3822aad739acSMatthew G. Knepley PetscFunctionBegin; 3823aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3824f907fdbfSMatthew G. Knepley PetscValidFunction(exactError, 2); 3825aad739acSMatthew G. Knepley ts->ops->exacterror = exactError; 3826aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3827aad739acSMatthew G. Knepley } 3828aad739acSMatthew G. Knepley 3829aad739acSMatthew G. Knepley /*@ 3830aad739acSMatthew G. Knepley TSComputeExactError - Compute the solution error for the timestepping using the function previously set. 3831aad739acSMatthew G. Knepley 3832aad739acSMatthew G. Knepley Collective on ts 3833aad739acSMatthew G. Knepley 3834aad739acSMatthew G. Knepley Input Arguments: 3835aad739acSMatthew G. Knepley + ts - time stepping context 3836aad739acSMatthew G. Knepley . u - The approximate solution 3837aad739acSMatthew G. Knepley - e - The Vec used to store the error 3838aad739acSMatthew G. Knepley 3839aad739acSMatthew G. Knepley Level: advanced 3840aad739acSMatthew G. Knepley 3841aad739acSMatthew G. Knepley Notes: 3842aad739acSMatthew G. Knepley The calling sequence for the function is 3843aad739acSMatthew G. Knepley $ exactError(TS ts, Vec u) 3844aad739acSMatthew G. Knepley $ ts - The timestepping context 3845aad739acSMatthew G. Knepley $ u - The approximate solution vector 3846aad739acSMatthew G. Knepley $ e - The input vector in which the error is stored 3847aad739acSMatthew G. Knepley 3848*2e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve() 3849aad739acSMatthew G. Knepley @*/ 3850aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e) 3851aad739acSMatthew G. Knepley { 3852aad739acSMatthew G. Knepley PetscErrorCode ierr; 3853aad739acSMatthew G. Knepley 3854aad739acSMatthew G. Knepley PetscFunctionBegin; 3855aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3856aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3857aad739acSMatthew G. Knepley PetscValidHeaderSpecific(e, VEC_CLASSID, 3); 3858aad739acSMatthew G. Knepley if (ts->ops->exacterror) {ierr = (*ts->ops->exacterror)(ts, u, e);CHKERRQ(ierr);} 3859aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3860aad739acSMatthew G. Knepley } 3861aad739acSMatthew G. Knepley 3862b1cb36f3SHong Zhang /*@ 38636a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 38646a4d4014SLisandro Dalcin 38656a4d4014SLisandro Dalcin Collective on TS 38666a4d4014SLisandro Dalcin 38676a4d4014SLisandro Dalcin Input Parameter: 38686a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 386963e21af5SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used, 387063e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 38715a3a76d0SJed Brown 38726a4d4014SLisandro Dalcin Level: beginner 38736a4d4014SLisandro Dalcin 38745a3a76d0SJed Brown Notes: 38755a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 38765a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 38775a3a76d0SJed Brown stepped over the final time. 38785a3a76d0SJed Brown 387963e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime() 38806a4d4014SLisandro Dalcin @*/ 3881cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 38826a4d4014SLisandro Dalcin { 3883b06615a5SLisandro Dalcin Vec solution; 38846a4d4014SLisandro Dalcin PetscErrorCode ierr; 3885f22f69f0SBarry Smith 38866a4d4014SLisandro Dalcin PetscFunctionBegin; 38870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3888f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 3889feed9e9dSBarry Smith 3890ee41a567SStefano 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 */ 38910910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 3892b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3893b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3894b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 38955a3a76d0SJed Brown } 38960910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3897715f1b00SHong Zhang if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE"); 3898bbd56ea5SKarl Rupp } else if (u) { 38990910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 39005a3a76d0SJed Brown } 3901b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 390268bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3903a6772fa2SLisandro Dalcin 3904ef85077eSLisandro 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>"); 3905a6772fa2SLisandro 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()"); 3906a6772fa2SLisandro 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"); 3907a6772fa2SLisandro Dalcin 3908715f1b00SHong Zhang if (ts->forward_solve) { 3909715f1b00SHong Zhang ierr = TSForwardSetUp(ts);CHKERRQ(ierr); 3910715f1b00SHong Zhang } 3911715f1b00SHong Zhang 3912e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 3913715f1b00SHong Zhang restarts the step after an event. Resetting these counters in such case causes 3914e7069c78SShri TSTrajectory to incorrectly save the output files 3915e7069c78SShri */ 3916715f1b00SHong Zhang /* reset time step and iteration counters */ 39172808aa04SLisandro Dalcin if (!ts->steps) { 39185ef26d82SJed Brown ts->ksp_its = 0; 39195ef26d82SJed Brown ts->snes_its = 0; 3920c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3921c610991cSLisandro Dalcin ts->reject = 0; 39222808aa04SLisandro Dalcin ts->steprestart = PETSC_TRUE; 39232808aa04SLisandro Dalcin ts->steprollback = PETSC_FALSE; 39242808aa04SLisandro Dalcin } 39251a3b9e0cSLisandro Dalcin if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && ts->ptime < ts->max_time && ts->ptime + ts->time_step > ts->max_time) ts->time_step = ts->max_time - ts->ptime; 3926193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3927193ac0bcSJed Brown 3928900f6b5bSMatthew G. Knepley { 3929900f6b5bSMatthew G. Knepley PetscViewer viewer; 3930900f6b5bSMatthew G. Knepley PetscViewerFormat format; 3931900f6b5bSMatthew G. Knepley PetscBool flg; 3932900f6b5bSMatthew G. Knepley static PetscBool incall = PETSC_FALSE; 3933900f6b5bSMatthew G. Knepley 3934900f6b5bSMatthew G. Knepley if (!incall) { 3935900f6b5bSMatthew G. Knepley /* Estimate the convergence rate of the time discretization */ 3936900f6b5bSMatthew G. Knepley ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg);CHKERRQ(ierr); 3937900f6b5bSMatthew G. Knepley if (flg) { 3938900f6b5bSMatthew G. Knepley PetscConvEst conv; 3939900f6b5bSMatthew G. Knepley DM dm; 3940900f6b5bSMatthew G. Knepley PetscReal *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */ 3941900f6b5bSMatthew G. Knepley PetscInt Nf; 3942900f6b5bSMatthew G. Knepley 3943900f6b5bSMatthew G. Knepley incall = PETSC_TRUE; 3944900f6b5bSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3945900f6b5bSMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 3946900f6b5bSMatthew G. Knepley ierr = PetscCalloc1(PetscMax(Nf, 1), &alpha);CHKERRQ(ierr); 3947900f6b5bSMatthew G. Knepley ierr = PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv);CHKERRQ(ierr); 3948900f6b5bSMatthew G. Knepley ierr = PetscConvEstUseTS(conv);CHKERRQ(ierr); 3949900f6b5bSMatthew G. Knepley ierr = PetscConvEstSetSolver(conv, (PetscObject) ts);CHKERRQ(ierr); 3950900f6b5bSMatthew G. Knepley ierr = PetscConvEstSetFromOptions(conv);CHKERRQ(ierr); 3951900f6b5bSMatthew G. Knepley ierr = PetscConvEstSetUp(conv);CHKERRQ(ierr); 3952900f6b5bSMatthew G. Knepley ierr = PetscConvEstGetConvRate(conv, alpha);CHKERRQ(ierr); 3953900f6b5bSMatthew G. Knepley ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr); 3954900f6b5bSMatthew G. Knepley ierr = PetscConvEstRateView(conv, alpha, viewer);CHKERRQ(ierr); 3955900f6b5bSMatthew G. Knepley ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3956900f6b5bSMatthew G. Knepley ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 3957900f6b5bSMatthew G. Knepley ierr = PetscConvEstDestroy(&conv);CHKERRQ(ierr); 3958900f6b5bSMatthew G. Knepley ierr = PetscFree(alpha);CHKERRQ(ierr); 3959900f6b5bSMatthew G. Knepley incall = PETSC_FALSE; 3960900f6b5bSMatthew G. Knepley } 3961900f6b5bSMatthew G. Knepley } 3962900f6b5bSMatthew G. Knepley } 3963900f6b5bSMatthew G. Knepley 3964ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3965f05ece33SBarry Smith 3966193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3967193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 3968a6772fa2SLisandro Dalcin if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3969cc708dedSBarry Smith ts->solvetime = ts->ptime; 3970a6772fa2SLisandro Dalcin solution = ts->vec_sol; 3971be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 3972db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3973db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3974e7069c78SShri 39752808aa04SLisandro Dalcin if (!ts->steps) { 39762808aa04SLisandro Dalcin ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39776427ac75SLisandro Dalcin ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39782808aa04SLisandro Dalcin } 39796427ac75SLisandro Dalcin 3980e1a7a14fSJed Brown while (!ts->reason) { 39812808aa04SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39829687d888SLisandro Dalcin if (!ts->steprollback) { 39839687d888SLisandro Dalcin ierr = TSPreStep(ts);CHKERRQ(ierr); 39849687d888SLisandro Dalcin } 3985193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 3986f3b1f45cSBarry Smith if (ts->testjacobian) { 3987f3b1f45cSBarry Smith ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr); 3988f3b1f45cSBarry Smith } 3989f3b1f45cSBarry Smith if (ts->testjacobiantranspose) { 3990f3b1f45cSBarry Smith ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr); 3991f3b1f45cSBarry Smith } 3992cd4cee2dSHong Zhang if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */ 3993b1cb36f3SHong Zhang ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr); 3994b1cb36f3SHong Zhang } 399558818c2dSLisandro Dalcin if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */ 3996715f1b00SHong Zhang ierr = TSForwardStep(ts);CHKERRQ(ierr); 3997715f1b00SHong Zhang } 399810b82f12SShri Abhyankar ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 3999e783b05fSHong 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. */ 400058818c2dSLisandro Dalcin if (ts->steprollback) { 400158818c2dSLisandro Dalcin ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 400258818c2dSLisandro Dalcin } 4003e783b05fSHong Zhang if (!ts->steprollback) { 40042808aa04SLisandro Dalcin ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 40051eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 4006aeb4809dSShri Abhyankar } 4007193ac0bcSJed Brown } 40082808aa04SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 40096427ac75SLisandro Dalcin 401049354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 40110910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 4012cc708dedSBarry Smith ts->solvetime = ts->max_time; 4013b06615a5SLisandro Dalcin solution = u; 401463e21af5SBarry Smith ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr); 40150574a7fbSJed Brown } else { 4016ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 4017cc708dedSBarry Smith ts->solvetime = ts->ptime; 4018b06615a5SLisandro Dalcin solution = ts->vec_sol; 40190574a7fbSJed Brown } 4020193ac0bcSJed Brown } 4021d2daff3dSHong Zhang 4022ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 402363e21af5SBarry Smith ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr); 402456f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 4025ef222394SBarry Smith if (ts->adjoint_solve) { 4026ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 40272b0a91c0SBarry Smith } 40286a4d4014SLisandro Dalcin PetscFunctionReturn(0); 40296a4d4014SLisandro Dalcin } 40306a4d4014SLisandro Dalcin 40317db568b7SBarry Smith /*@C 4032228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 4033228d79bcSJed Brown 4034228d79bcSJed Brown Collective on TS 4035228d79bcSJed Brown 4036228d79bcSJed Brown Input Parameters: 4037228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 4038228d79bcSJed Brown . step - step number that has just completed 4039228d79bcSJed Brown . ptime - model time of the state 40400910c330SBarry Smith - u - state at the current model time 4041228d79bcSJed Brown 4042228d79bcSJed Brown Notes: 40437db568b7SBarry Smith TSMonitor() is typically used automatically within the time stepping implementations. 40447db568b7SBarry Smith Users would almost never call this routine directly. 4045228d79bcSJed Brown 404663e21af5SBarry Smith A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions 404763e21af5SBarry Smith 40487db568b7SBarry Smith Level: developer 4049228d79bcSJed Brown 4050228d79bcSJed Brown @*/ 40510910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 4052d763cef2SBarry Smith { 4053d6152f81SLisandro Dalcin DM dm; 4054a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 4055d6152f81SLisandro Dalcin PetscErrorCode ierr; 4056d763cef2SBarry Smith 4057d763cef2SBarry Smith PetscFunctionBegin; 4058b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4059b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4060d6152f81SLisandro Dalcin 4061d6152f81SLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 4062d6152f81SLisandro Dalcin ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr); 4063d6152f81SLisandro Dalcin 40648860a134SJunchao Zhang ierr = VecLockReadPush(u);CHKERRQ(ierr); 4065d763cef2SBarry Smith for (i=0; i<n; i++) { 40660910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 4067d763cef2SBarry Smith } 40688860a134SJunchao Zhang ierr = VecLockReadPop(u);CHKERRQ(ierr); 4069d763cef2SBarry Smith PetscFunctionReturn(0); 4070d763cef2SBarry Smith } 4071d763cef2SBarry Smith 4072d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 4073d763cef2SBarry Smith /*@C 40747db568b7SBarry Smith TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 4075a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 4076d763cef2SBarry Smith 4077d763cef2SBarry Smith Collective on TS 4078d763cef2SBarry Smith 4079d763cef2SBarry Smith Input Parameters: 4080d763cef2SBarry Smith + host - the X display to open, or null for the local machine 4081d763cef2SBarry Smith . label - the title to put in the title bar 40827c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 4083a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 4084a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 4085d763cef2SBarry Smith 4086d763cef2SBarry Smith Output Parameter: 40870b039ecaSBarry Smith . ctx - the context 4088d763cef2SBarry Smith 4089d763cef2SBarry Smith Options Database Key: 40904f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 40918b668821SLisandro Dalcin + -ts_monitor_lg_timestep_log - automatically sets line graph monitor 40927db568b7SBarry Smith . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 40937db568b7SBarry Smith . -ts_monitor_lg_error - monitor the error 40947db568b7SBarry Smith . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 40957db568b7SBarry Smith . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 4096b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 4097d763cef2SBarry Smith 4098d763cef2SBarry Smith Notes: 4099a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 4100d763cef2SBarry Smith 41017db568b7SBarry Smith One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 41027db568b7SBarry Smith 41037db568b7SBarry 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 41047db568b7SBarry 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 41057db568b7SBarry Smith as the first argument. 41067db568b7SBarry Smith 41077db568b7SBarry Smith One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 41087db568b7SBarry Smith 4109d763cef2SBarry Smith Level: intermediate 4110d763cef2SBarry Smith 41117db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 41127db568b7SBarry Smith TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 41137db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 41147db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 41157db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 41167c922b88SBarry Smith 4117d763cef2SBarry Smith @*/ 4118a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 4119d763cef2SBarry Smith { 41207f52daa2SLisandro Dalcin PetscDraw draw; 4121dfbe8321SBarry Smith PetscErrorCode ierr; 4122d763cef2SBarry Smith 4123d763cef2SBarry Smith PetscFunctionBegin; 4124b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 41257f52daa2SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 41267f52daa2SLisandro Dalcin ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 41277f52daa2SLisandro Dalcin ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 4128287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 41297f52daa2SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 4130a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 4131d763cef2SBarry Smith PetscFunctionReturn(0); 4132d763cef2SBarry Smith } 4133d763cef2SBarry Smith 4134b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 4135d763cef2SBarry Smith { 41360b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4137c32365f1SBarry Smith PetscReal x = ptime,y; 4138dfbe8321SBarry Smith PetscErrorCode ierr; 4139d763cef2SBarry Smith 4140d763cef2SBarry Smith PetscFunctionBegin; 414163e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */ 4142b06615a5SLisandro Dalcin if (!step) { 4143a9f9c1f6SBarry Smith PetscDrawAxis axis; 41448b668821SLisandro Dalcin const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step"; 4145a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 41468b668821SLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr); 4147a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4148a9f9c1f6SBarry Smith } 4149c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 41508b668821SLisandro Dalcin if (ctx->semilogy) y = PetscLog10Real(y); 41510b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4152b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 41530b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 41546934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 41553923b477SBarry Smith } 4156d763cef2SBarry Smith PetscFunctionReturn(0); 4157d763cef2SBarry Smith } 4158d763cef2SBarry Smith 4159d763cef2SBarry Smith /*@C 4160a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 4161a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 4162d763cef2SBarry Smith 41630b039ecaSBarry Smith Collective on TSMonitorLGCtx 4164d763cef2SBarry Smith 4165d763cef2SBarry Smith Input Parameter: 41660b039ecaSBarry Smith . ctx - the monitor context 4167d763cef2SBarry Smith 4168d763cef2SBarry Smith Level: intermediate 4169d763cef2SBarry Smith 41704f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 4171d763cef2SBarry Smith @*/ 4172a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 4173d763cef2SBarry Smith { 4174dfbe8321SBarry Smith PetscErrorCode ierr; 4175d763cef2SBarry Smith 4176d763cef2SBarry Smith PetscFunctionBegin; 41777684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 41787684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 41797684fa3eSBarry Smith } 41800b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 418131152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 4182387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 4183387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 4184387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 41850b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 4186d763cef2SBarry Smith PetscFunctionReturn(0); 4187d763cef2SBarry Smith } 4188d763cef2SBarry Smith 41891b575b74SJoseph Pusztay /* 41901b575b74SJoseph Pusztay 41911b575b74SJoseph Pusztay Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations 41921b575b74SJoseph Pusztay 41931b575b74SJoseph Pusztay */ 41941b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx) 41951b575b74SJoseph Pusztay { 41961b575b74SJoseph Pusztay PetscDraw draw; 41971b575b74SJoseph Pusztay PetscErrorCode ierr; 41981b575b74SJoseph Pusztay 41991b575b74SJoseph Pusztay PetscFunctionBegin; 42001b575b74SJoseph Pusztay ierr = PetscNew(ctx);CHKERRQ(ierr); 42011b575b74SJoseph Pusztay ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 42021b575b74SJoseph Pusztay ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 42031b575b74SJoseph Pusztay ierr = PetscDrawSPCreate(draw,1,&(*ctx)->sp);CHKERRQ(ierr); 42041b575b74SJoseph Pusztay ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 42051b575b74SJoseph Pusztay (*ctx)->howoften = howoften; 42061b575b74SJoseph Pusztay PetscFunctionReturn(0); 42071b575b74SJoseph Pusztay 42081b575b74SJoseph Pusztay } 42091b575b74SJoseph Pusztay 42101b575b74SJoseph Pusztay /* 42111b575b74SJoseph Pusztay Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate 42121b575b74SJoseph Pusztay */ 42131b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx) 42141b575b74SJoseph Pusztay { 42151b575b74SJoseph Pusztay PetscErrorCode ierr; 42161b575b74SJoseph Pusztay 42171b575b74SJoseph Pusztay PetscFunctionBegin; 42181b575b74SJoseph Pusztay 42191b575b74SJoseph Pusztay ierr = PetscDrawSPDestroy(&(*ctx)->sp);CHKERRQ(ierr); 42201b575b74SJoseph Pusztay ierr = PetscFree(*ctx);CHKERRQ(ierr); 42211b575b74SJoseph Pusztay 42221b575b74SJoseph Pusztay PetscFunctionReturn(0); 42231b575b74SJoseph Pusztay 42241b575b74SJoseph Pusztay } 42251b575b74SJoseph Pusztay 4226d763cef2SBarry Smith /*@ 4227b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4228d763cef2SBarry Smith 4229d763cef2SBarry Smith Not Collective 4230d763cef2SBarry Smith 4231d763cef2SBarry Smith Input Parameter: 4232d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 4233d763cef2SBarry Smith 4234d763cef2SBarry Smith Output Parameter: 423519eac22cSLisandro Dalcin . t - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime(). 4236d763cef2SBarry Smith 4237d763cef2SBarry Smith Level: beginner 4238d763cef2SBarry Smith 4239b8123daeSJed Brown Note: 4240b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 42419be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 4242b8123daeSJed Brown 42438f199f4dSPatrick Sanan .seealso: TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber() 4244d763cef2SBarry Smith 4245d763cef2SBarry Smith @*/ 42467087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 4247d763cef2SBarry Smith { 4248d763cef2SBarry Smith PetscFunctionBegin; 42490700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4250f7cf8827SBarry Smith PetscValidRealPointer(t,2); 4251d763cef2SBarry Smith *t = ts->ptime; 4252d763cef2SBarry Smith PetscFunctionReturn(0); 4253d763cef2SBarry Smith } 4254d763cef2SBarry Smith 4255e5e524a1SHong Zhang /*@ 4256e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4257e5e524a1SHong Zhang 4258e5e524a1SHong Zhang Not Collective 4259e5e524a1SHong Zhang 4260e5e524a1SHong Zhang Input Parameter: 4261e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 4262e5e524a1SHong Zhang 4263e5e524a1SHong Zhang Output Parameter: 4264e5e524a1SHong Zhang . t - the previous time 4265e5e524a1SHong Zhang 4266e5e524a1SHong Zhang Level: beginner 4267e5e524a1SHong Zhang 4268aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep() 4269e5e524a1SHong Zhang 4270e5e524a1SHong Zhang @*/ 4271e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 4272e5e524a1SHong Zhang { 4273e5e524a1SHong Zhang PetscFunctionBegin; 4274e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4275e5e524a1SHong Zhang PetscValidRealPointer(t,2); 4276e5e524a1SHong Zhang *t = ts->ptime_prev; 4277e5e524a1SHong Zhang PetscFunctionReturn(0); 4278e5e524a1SHong Zhang } 4279e5e524a1SHong Zhang 42806a4d4014SLisandro Dalcin /*@ 42816a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 42826a4d4014SLisandro Dalcin 42833f9fe445SBarry Smith Logically Collective on TS 42846a4d4014SLisandro Dalcin 42856a4d4014SLisandro Dalcin Input Parameters: 42866a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 42876a4d4014SLisandro Dalcin - time - the time 42886a4d4014SLisandro Dalcin 42896a4d4014SLisandro Dalcin Level: intermediate 42906a4d4014SLisandro Dalcin 429119eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps() 42926a4d4014SLisandro Dalcin 42936a4d4014SLisandro Dalcin @*/ 42947087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 42956a4d4014SLisandro Dalcin { 42966a4d4014SLisandro Dalcin PetscFunctionBegin; 42970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4298c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 42996a4d4014SLisandro Dalcin ts->ptime = t; 43006a4d4014SLisandro Dalcin PetscFunctionReturn(0); 43016a4d4014SLisandro Dalcin } 43026a4d4014SLisandro Dalcin 4303d763cef2SBarry Smith /*@C 4304d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4305d763cef2SBarry Smith TS options in the database. 4306d763cef2SBarry Smith 43073f9fe445SBarry Smith Logically Collective on TS 4308d763cef2SBarry Smith 4309d763cef2SBarry Smith Input Parameter: 4310d763cef2SBarry Smith + ts - The TS context 4311d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4312d763cef2SBarry Smith 4313d763cef2SBarry Smith Notes: 4314d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4315d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4316d763cef2SBarry Smith hyphen. 4317d763cef2SBarry Smith 4318d763cef2SBarry Smith Level: advanced 4319d763cef2SBarry Smith 4320d763cef2SBarry Smith .seealso: TSSetFromOptions() 4321d763cef2SBarry Smith 4322d763cef2SBarry Smith @*/ 43237087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 4324d763cef2SBarry Smith { 4325dfbe8321SBarry Smith PetscErrorCode ierr; 4326089b2837SJed Brown SNES snes; 4327d763cef2SBarry Smith 4328d763cef2SBarry Smith PetscFunctionBegin; 43290700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4330d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4331089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4332089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4333d763cef2SBarry Smith PetscFunctionReturn(0); 4334d763cef2SBarry Smith } 4335d763cef2SBarry Smith 4336d763cef2SBarry Smith /*@C 4337d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4338d763cef2SBarry Smith TS options in the database. 4339d763cef2SBarry Smith 43403f9fe445SBarry Smith Logically Collective on TS 4341d763cef2SBarry Smith 4342d763cef2SBarry Smith Input Parameter: 4343d763cef2SBarry Smith + ts - The TS context 4344d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4345d763cef2SBarry Smith 4346d763cef2SBarry Smith Notes: 4347d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4348d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4349d763cef2SBarry Smith hyphen. 4350d763cef2SBarry Smith 4351d763cef2SBarry Smith Level: advanced 4352d763cef2SBarry Smith 4353d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 4354d763cef2SBarry Smith 4355d763cef2SBarry Smith @*/ 43567087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4357d763cef2SBarry Smith { 4358dfbe8321SBarry Smith PetscErrorCode ierr; 4359089b2837SJed Brown SNES snes; 4360d763cef2SBarry Smith 4361d763cef2SBarry Smith PetscFunctionBegin; 43620700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4363d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4364089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4365089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4366d763cef2SBarry Smith PetscFunctionReturn(0); 4367d763cef2SBarry Smith } 4368d763cef2SBarry Smith 4369d763cef2SBarry Smith /*@C 4370d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4371d763cef2SBarry Smith TS options in the database. 4372d763cef2SBarry Smith 4373d763cef2SBarry Smith Not Collective 4374d763cef2SBarry Smith 4375d763cef2SBarry Smith Input Parameter: 4376d763cef2SBarry Smith . ts - The TS context 4377d763cef2SBarry Smith 4378d763cef2SBarry Smith Output Parameter: 4379d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4380d763cef2SBarry Smith 438195452b02SPatrick Sanan Notes: 438295452b02SPatrick Sanan On the fortran side, the user should pass in a string 'prifix' of 4383d763cef2SBarry Smith sufficient length to hold the prefix. 4384d763cef2SBarry Smith 4385d763cef2SBarry Smith Level: intermediate 4386d763cef2SBarry Smith 4387d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 4388d763cef2SBarry Smith @*/ 43897087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4390d763cef2SBarry Smith { 4391dfbe8321SBarry Smith PetscErrorCode ierr; 4392d763cef2SBarry Smith 4393d763cef2SBarry Smith PetscFunctionBegin; 43940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 43954482741eSBarry Smith PetscValidPointer(prefix,2); 4396d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4397d763cef2SBarry Smith PetscFunctionReturn(0); 4398d763cef2SBarry Smith } 4399d763cef2SBarry Smith 4400d763cef2SBarry Smith /*@C 4401d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4402d763cef2SBarry Smith 4403d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4404d763cef2SBarry Smith 4405d763cef2SBarry Smith Input Parameter: 4406d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4407d763cef2SBarry Smith 4408d763cef2SBarry Smith Output Parameters: 4409e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4410e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4411e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4412e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4413d763cef2SBarry Smith 441495452b02SPatrick Sanan Notes: 441595452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 4416d763cef2SBarry Smith 4417d763cef2SBarry Smith Level: intermediate 4418d763cef2SBarry Smith 441980275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 4420d763cef2SBarry Smith 4421d763cef2SBarry Smith @*/ 4422e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4423d763cef2SBarry Smith { 4424089b2837SJed Brown PetscErrorCode ierr; 442524989b8cSPeter Brune DM dm; 4426089b2837SJed Brown 4427d763cef2SBarry Smith PetscFunctionBegin; 442823a57915SBarry Smith if (Amat || Pmat) { 442923a57915SBarry Smith SNES snes; 4430089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 443123a57915SBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4432e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 443323a57915SBarry Smith } 443424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 443524989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 4436d763cef2SBarry Smith PetscFunctionReturn(0); 4437d763cef2SBarry Smith } 4438d763cef2SBarry Smith 44392eca1d9cSJed Brown /*@C 44402eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 44412eca1d9cSJed Brown 44422eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 44432eca1d9cSJed Brown 44442eca1d9cSJed Brown Input Parameter: 44452eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 44462eca1d9cSJed Brown 44472eca1d9cSJed Brown Output Parameters: 4448e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4449e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 44502eca1d9cSJed Brown . f - The function to compute the matrices 44512eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 44522eca1d9cSJed Brown 445395452b02SPatrick Sanan Notes: 445495452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 44552eca1d9cSJed Brown 44562eca1d9cSJed Brown Level: advanced 44572eca1d9cSJed Brown 445880275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 44592eca1d9cSJed Brown 44602eca1d9cSJed Brown @*/ 4461e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 44622eca1d9cSJed Brown { 4463089b2837SJed Brown PetscErrorCode ierr; 446424989b8cSPeter Brune DM dm; 44650910c330SBarry Smith 44662eca1d9cSJed Brown PetscFunctionBegin; 4467c0aab802Sstefano_zampini if (Amat || Pmat) { 4468c0aab802Sstefano_zampini SNES snes; 4469089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4470f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4471e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 4472c0aab802Sstefano_zampini } 447324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 447424989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 44752eca1d9cSJed Brown PetscFunctionReturn(0); 44762eca1d9cSJed Brown } 44772eca1d9cSJed Brown 44781713a123SBarry Smith /*@C 447983a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 44801713a123SBarry Smith VecView() for the solution at each timestep 44811713a123SBarry Smith 44821713a123SBarry Smith Collective on TS 44831713a123SBarry Smith 44841713a123SBarry Smith Input Parameters: 44851713a123SBarry Smith + ts - the TS context 44861713a123SBarry Smith . step - current time-step 4487142b95e3SSatish Balay . ptime - current time 44880298fd71SBarry Smith - dummy - either a viewer or NULL 44891713a123SBarry Smith 449099fdda47SBarry Smith Options Database: 449199fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 449299fdda47SBarry Smith 449395452b02SPatrick Sanan Notes: 449495452b02SPatrick Sanan the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 449599fdda47SBarry Smith will look bad 449699fdda47SBarry Smith 44971713a123SBarry Smith Level: intermediate 44981713a123SBarry Smith 4499a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 45001713a123SBarry Smith @*/ 45010910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 45021713a123SBarry Smith { 4503dfbe8321SBarry Smith PetscErrorCode ierr; 450483a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4505473a3ab2SBarry Smith PetscDraw draw; 45061713a123SBarry Smith 45071713a123SBarry Smith PetscFunctionBegin; 45086083293cSBarry Smith if (!step && ictx->showinitial) { 45096083293cSBarry Smith if (!ictx->initialsolution) { 45100910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 45111713a123SBarry Smith } 45120910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 45136083293cSBarry Smith } 4514b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 45150dcf80beSBarry Smith 45166083293cSBarry Smith if (ictx->showinitial) { 45176083293cSBarry Smith PetscReal pause; 45186083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 45196083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 45206083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 45216083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 45226083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 45236083293cSBarry Smith } 45240910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 4525473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 452651fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 4527473a3ab2SBarry Smith char time[32]; 4528473a3ab2SBarry Smith 4529473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 453085b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4531473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4532473a3ab2SBarry Smith h = yl + .95*(yr - yl); 453351fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4534473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4535473a3ab2SBarry Smith } 4536473a3ab2SBarry Smith 45376083293cSBarry Smith if (ictx->showinitial) { 45386083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 45396083293cSBarry Smith } 45401713a123SBarry Smith PetscFunctionReturn(0); 45411713a123SBarry Smith } 45421713a123SBarry Smith 45439110b2e7SHong Zhang /*@C 45442d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 45452d5ee99bSBarry Smith 45462d5ee99bSBarry Smith Collective on TS 45472d5ee99bSBarry Smith 45482d5ee99bSBarry Smith Input Parameters: 45492d5ee99bSBarry Smith + ts - the TS context 45502d5ee99bSBarry Smith . step - current time-step 45512d5ee99bSBarry Smith . ptime - current time 45522d5ee99bSBarry Smith - dummy - either a viewer or NULL 45532d5ee99bSBarry Smith 45542d5ee99bSBarry Smith Level: intermediate 45552d5ee99bSBarry Smith 45562d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 45572d5ee99bSBarry Smith @*/ 45582d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 45592d5ee99bSBarry Smith { 45602d5ee99bSBarry Smith PetscErrorCode ierr; 45612d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 45622d5ee99bSBarry Smith PetscDraw draw; 45636934998bSLisandro Dalcin PetscDrawAxis axis; 45642d5ee99bSBarry Smith PetscInt n; 45652d5ee99bSBarry Smith PetscMPIInt size; 45666934998bSLisandro Dalcin PetscReal U0,U1,xl,yl,xr,yr,h; 45672d5ee99bSBarry Smith char time[32]; 45682d5ee99bSBarry Smith const PetscScalar *U; 45692d5ee99bSBarry Smith 45702d5ee99bSBarry Smith PetscFunctionBegin; 45716934998bSLisandro Dalcin ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr); 45726934998bSLisandro Dalcin if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs"); 45732d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 45746934998bSLisandro Dalcin if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 45752d5ee99bSBarry Smith 45762d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 45776934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr); 45786934998bSLisandro Dalcin ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 45796934998bSLisandro Dalcin if (!step) { 45806934998bSLisandro Dalcin ierr = PetscDrawClear(draw);CHKERRQ(ierr); 45816934998bSLisandro Dalcin ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); 45826934998bSLisandro Dalcin } 45832d5ee99bSBarry Smith 45842d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 45856934998bSLisandro Dalcin U0 = PetscRealPart(U[0]); 45866934998bSLisandro Dalcin U1 = PetscRealPart(U[1]); 4587a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 45886934998bSLisandro Dalcin if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0); 45892d5ee99bSBarry Smith 45906934998bSLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 45916934998bSLisandro Dalcin ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr); 45922d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 45934b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 459485b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 45952d5ee99bSBarry Smith h = yl + .95*(yr - yl); 459651fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 45972d5ee99bSBarry Smith } 45986934998bSLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 45992d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 460056d62c8fSLisandro Dalcin ierr = PetscDrawPause(draw);CHKERRQ(ierr); 46016934998bSLisandro Dalcin ierr = PetscDrawSave(draw);CHKERRQ(ierr); 46022d5ee99bSBarry Smith PetscFunctionReturn(0); 46032d5ee99bSBarry Smith } 46042d5ee99bSBarry Smith 46056083293cSBarry Smith /*@C 460683a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 46076083293cSBarry Smith 46086083293cSBarry Smith Collective on TS 46096083293cSBarry Smith 46106083293cSBarry Smith Input Parameters: 46116083293cSBarry Smith . ctx - the monitor context 46126083293cSBarry Smith 46136083293cSBarry Smith Level: intermediate 46146083293cSBarry Smith 461583a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 46166083293cSBarry Smith @*/ 461783a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 46186083293cSBarry Smith { 46196083293cSBarry Smith PetscErrorCode ierr; 46206083293cSBarry Smith 46216083293cSBarry Smith PetscFunctionBegin; 462283a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 462383a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 462483a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 46256083293cSBarry Smith PetscFunctionReturn(0); 46266083293cSBarry Smith } 46276083293cSBarry Smith 46286083293cSBarry Smith /*@C 462983a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 46306083293cSBarry Smith 46316083293cSBarry Smith Collective on TS 46326083293cSBarry Smith 46336083293cSBarry Smith Input Parameter: 46346083293cSBarry Smith . ts - time-step context 46356083293cSBarry Smith 46366083293cSBarry Smith Output Patameter: 46376083293cSBarry Smith . ctx - the monitor context 46386083293cSBarry Smith 463999fdda47SBarry Smith Options Database: 464099fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 464199fdda47SBarry Smith 46426083293cSBarry Smith Level: intermediate 46436083293cSBarry Smith 464483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 46456083293cSBarry Smith @*/ 464683a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 46476083293cSBarry Smith { 46486083293cSBarry Smith PetscErrorCode ierr; 46496083293cSBarry Smith 46506083293cSBarry Smith PetscFunctionBegin; 4651b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 465283a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4653e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4654bbd56ea5SKarl Rupp 465583a4ac43SBarry Smith (*ctx)->howoften = howoften; 4656473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 4657c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4658473a3ab2SBarry Smith 4659473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 4660c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 46616083293cSBarry Smith PetscFunctionReturn(0); 46626083293cSBarry Smith } 46636083293cSBarry Smith 46643a471f94SBarry Smith /*@C 46650ed3bfb6SBarry Smith TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling 46660ed3bfb6SBarry Smith VecView() for the solution provided by TSSetSolutionFunction() at each timestep 46670ed3bfb6SBarry Smith 46680ed3bfb6SBarry Smith Collective on TS 46690ed3bfb6SBarry Smith 46700ed3bfb6SBarry Smith Input Parameters: 46710ed3bfb6SBarry Smith + ts - the TS context 46720ed3bfb6SBarry Smith . step - current time-step 46730ed3bfb6SBarry Smith . ptime - current time 46740ed3bfb6SBarry Smith - dummy - either a viewer or NULL 46750ed3bfb6SBarry Smith 46760ed3bfb6SBarry Smith Options Database: 46770ed3bfb6SBarry Smith . -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 46780ed3bfb6SBarry Smith 46790ed3bfb6SBarry Smith Level: intermediate 46800ed3bfb6SBarry Smith 46810ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 46820ed3bfb6SBarry Smith @*/ 46830ed3bfb6SBarry Smith PetscErrorCode TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 46840ed3bfb6SBarry Smith { 46850ed3bfb6SBarry Smith PetscErrorCode ierr; 46860ed3bfb6SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 46870ed3bfb6SBarry Smith PetscViewer viewer = ctx->viewer; 46880ed3bfb6SBarry Smith Vec work; 46890ed3bfb6SBarry Smith 46900ed3bfb6SBarry Smith PetscFunctionBegin; 46910ed3bfb6SBarry Smith if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 46920ed3bfb6SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 46930ed3bfb6SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 46940ed3bfb6SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 46950ed3bfb6SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 46960ed3bfb6SBarry Smith PetscFunctionReturn(0); 46970ed3bfb6SBarry Smith } 46980ed3bfb6SBarry Smith 46990ed3bfb6SBarry Smith /*@C 470083a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 47013a471f94SBarry Smith VecView() for the error at each timestep 47023a471f94SBarry Smith 47033a471f94SBarry Smith Collective on TS 47043a471f94SBarry Smith 47053a471f94SBarry Smith Input Parameters: 47063a471f94SBarry Smith + ts - the TS context 47073a471f94SBarry Smith . step - current time-step 47083a471f94SBarry Smith . ptime - current time 47090298fd71SBarry Smith - dummy - either a viewer or NULL 47103a471f94SBarry Smith 47110ed3bfb6SBarry Smith Options Database: 47120ed3bfb6SBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 47130ed3bfb6SBarry Smith 47143a471f94SBarry Smith Level: intermediate 47153a471f94SBarry Smith 47160ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 47173a471f94SBarry Smith @*/ 47180910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 47193a471f94SBarry Smith { 47203a471f94SBarry Smith PetscErrorCode ierr; 472183a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 472283a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 47233a471f94SBarry Smith Vec work; 47243a471f94SBarry Smith 47253a471f94SBarry Smith PetscFunctionBegin; 4726b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 47270910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 47283a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 47290910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 47303a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 47313a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 47323a471f94SBarry Smith PetscFunctionReturn(0); 47333a471f94SBarry Smith } 47343a471f94SBarry Smith 4735af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 47366c699258SBarry Smith /*@ 47372a808120SBarry Smith TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS 47386c699258SBarry Smith 4739d083f849SBarry Smith Logically Collective on ts 47406c699258SBarry Smith 47416c699258SBarry Smith Input Parameters: 47422a808120SBarry Smith + ts - the ODE integrator object 47432a808120SBarry Smith - dm - the dm, cannot be NULL 47446c699258SBarry Smith 4745e03a659cSJed Brown Notes: 4746e03a659cSJed Brown A DM can only be used for solving one problem at a time because information about the problem is stored on the DM, 4747e03a659cSJed Brown even when not using interfaces like DMTSSetIFunction(). Use DMClone() to get a distinct DM when solving 4748e03a659cSJed Brown different problems using the same function space. 4749e03a659cSJed Brown 47506c699258SBarry Smith Level: intermediate 47516c699258SBarry Smith 47526c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 47536c699258SBarry Smith @*/ 47547087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 47556c699258SBarry Smith { 47566c699258SBarry Smith PetscErrorCode ierr; 4757089b2837SJed Brown SNES snes; 4758942e3340SBarry Smith DMTS tsdm; 47596c699258SBarry Smith 47606c699258SBarry Smith PetscFunctionBegin; 47610700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 47622a808120SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 476370663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4764942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 47652a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4766942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4767942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 476824989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 476924989b8cSPeter Brune tsdm->originaldm = dm; 477024989b8cSPeter Brune } 477124989b8cSPeter Brune } 47726bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 477324989b8cSPeter Brune } 47746c699258SBarry Smith ts->dm = dm; 4775bbd56ea5SKarl Rupp 4776089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4777089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 47786c699258SBarry Smith PetscFunctionReturn(0); 47796c699258SBarry Smith } 47806c699258SBarry Smith 47816c699258SBarry Smith /*@ 47826c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 47836c699258SBarry Smith 47843f9fe445SBarry Smith Not Collective 47856c699258SBarry Smith 47866c699258SBarry Smith Input Parameter: 47876c699258SBarry Smith . ts - the preconditioner context 47886c699258SBarry Smith 47896c699258SBarry Smith Output Parameter: 47906c699258SBarry Smith . dm - the dm 47916c699258SBarry Smith 47926c699258SBarry Smith Level: intermediate 47936c699258SBarry Smith 47946c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 47956c699258SBarry Smith @*/ 47967087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 47976c699258SBarry Smith { 4798496e6a7aSJed Brown PetscErrorCode ierr; 4799496e6a7aSJed Brown 48006c699258SBarry Smith PetscFunctionBegin; 48010700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4802496e6a7aSJed Brown if (!ts->dm) { 4803ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4804496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4805496e6a7aSJed Brown } 48066c699258SBarry Smith *dm = ts->dm; 48076c699258SBarry Smith PetscFunctionReturn(0); 48086c699258SBarry Smith } 48091713a123SBarry Smith 48100f5c6efeSJed Brown /*@ 48110f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 48120f5c6efeSJed Brown 48133f9fe445SBarry Smith Logically Collective on SNES 48140f5c6efeSJed Brown 48150f5c6efeSJed Brown Input Parameter: 4816d42a1c89SJed Brown + snes - nonlinear solver 48170910c330SBarry Smith . U - the current state at which to evaluate the residual 4818d42a1c89SJed Brown - ctx - user context, must be a TS 48190f5c6efeSJed Brown 48200f5c6efeSJed Brown Output Parameter: 48210f5c6efeSJed Brown . F - the nonlinear residual 48220f5c6efeSJed Brown 48230f5c6efeSJed Brown Notes: 48240f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 48250f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 48260f5c6efeSJed Brown 48270f5c6efeSJed Brown Level: advanced 48280f5c6efeSJed Brown 48290f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 48300f5c6efeSJed Brown @*/ 48310910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 48320f5c6efeSJed Brown { 48330f5c6efeSJed Brown TS ts = (TS)ctx; 48340f5c6efeSJed Brown PetscErrorCode ierr; 48350f5c6efeSJed Brown 48360f5c6efeSJed Brown PetscFunctionBegin; 48370f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 48380910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 48390f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 48400f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 48410910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 48420f5c6efeSJed Brown PetscFunctionReturn(0); 48430f5c6efeSJed Brown } 48440f5c6efeSJed Brown 48450f5c6efeSJed Brown /*@ 48460f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 48470f5c6efeSJed Brown 48480f5c6efeSJed Brown Collective on SNES 48490f5c6efeSJed Brown 48500f5c6efeSJed Brown Input Parameter: 48510f5c6efeSJed Brown + snes - nonlinear solver 48520910c330SBarry Smith . U - the current state at which to evaluate the residual 48530f5c6efeSJed Brown - ctx - user context, must be a TS 48540f5c6efeSJed Brown 48550f5c6efeSJed Brown Output Parameter: 48560f5c6efeSJed Brown + A - the Jacobian 48570f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 48580f5c6efeSJed Brown - flag - indicates any structure change in the matrix 48590f5c6efeSJed Brown 48600f5c6efeSJed Brown Notes: 48610f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 48620f5c6efeSJed Brown 48630f5c6efeSJed Brown Level: developer 48640f5c6efeSJed Brown 48650f5c6efeSJed Brown .seealso: SNESSetJacobian() 48660f5c6efeSJed Brown @*/ 4867d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 48680f5c6efeSJed Brown { 48690f5c6efeSJed Brown TS ts = (TS)ctx; 48700f5c6efeSJed Brown PetscErrorCode ierr; 48710f5c6efeSJed Brown 48720f5c6efeSJed Brown PetscFunctionBegin; 48730f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 48740910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 48750f5c6efeSJed Brown PetscValidPointer(A,3); 487694ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 48770f5c6efeSJed Brown PetscValidPointer(B,4); 487894ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 48790f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 4880d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 48810f5c6efeSJed Brown PetscFunctionReturn(0); 48820f5c6efeSJed Brown } 4883325fc9f4SBarry Smith 48840e4ef248SJed Brown /*@C 48859ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 48860e4ef248SJed Brown 48870e4ef248SJed Brown Collective on TS 48880e4ef248SJed Brown 48890e4ef248SJed Brown Input Arguments: 48900e4ef248SJed Brown + ts - time stepping context 48910e4ef248SJed Brown . t - time at which to evaluate 48920910c330SBarry Smith . U - state at which to evaluate 48930e4ef248SJed Brown - ctx - context 48940e4ef248SJed Brown 48950e4ef248SJed Brown Output Arguments: 48960e4ef248SJed Brown . F - right hand side 48970e4ef248SJed Brown 48980e4ef248SJed Brown Level: intermediate 48990e4ef248SJed Brown 49000e4ef248SJed Brown Notes: 49010e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 49020e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 49030e4ef248SJed Brown 49040e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 49050e4ef248SJed Brown @*/ 49060910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 49070e4ef248SJed Brown { 49080e4ef248SJed Brown PetscErrorCode ierr; 49090e4ef248SJed Brown Mat Arhs,Brhs; 49100e4ef248SJed Brown 49110e4ef248SJed Brown PetscFunctionBegin; 49120e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 4913d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 49140910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 49150e4ef248SJed Brown PetscFunctionReturn(0); 49160e4ef248SJed Brown } 49170e4ef248SJed Brown 49180e4ef248SJed Brown /*@C 49190e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 49200e4ef248SJed Brown 49210e4ef248SJed Brown Collective on TS 49220e4ef248SJed Brown 49230e4ef248SJed Brown Input Arguments: 49240e4ef248SJed Brown + ts - time stepping context 49250e4ef248SJed Brown . t - time at which to evaluate 49260910c330SBarry Smith . U - state at which to evaluate 49270e4ef248SJed Brown - ctx - context 49280e4ef248SJed Brown 49290e4ef248SJed Brown Output Arguments: 49300e4ef248SJed Brown + A - pointer to operator 49310e4ef248SJed Brown . B - pointer to preconditioning matrix 49320e4ef248SJed Brown - flg - matrix structure flag 49330e4ef248SJed Brown 49340e4ef248SJed Brown Level: intermediate 49350e4ef248SJed Brown 49360e4ef248SJed Brown Notes: 49370e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 49380e4ef248SJed Brown 49390e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 49400e4ef248SJed Brown @*/ 4941d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 49420e4ef248SJed Brown { 49430e4ef248SJed Brown PetscFunctionBegin; 49440e4ef248SJed Brown PetscFunctionReturn(0); 49450e4ef248SJed Brown } 49460e4ef248SJed Brown 49470026cea9SSean Farley /*@C 49480026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 49490026cea9SSean Farley 49500026cea9SSean Farley Collective on TS 49510026cea9SSean Farley 49520026cea9SSean Farley Input Arguments: 49530026cea9SSean Farley + ts - time stepping context 49540026cea9SSean Farley . t - time at which to evaluate 49550910c330SBarry Smith . U - state at which to evaluate 49560910c330SBarry Smith . Udot - time derivative of state vector 49570026cea9SSean Farley - ctx - context 49580026cea9SSean Farley 49590026cea9SSean Farley Output Arguments: 49600026cea9SSean Farley . F - left hand side 49610026cea9SSean Farley 49620026cea9SSean Farley Level: intermediate 49630026cea9SSean Farley 49640026cea9SSean Farley Notes: 49650910c330SBarry 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 49660026cea9SSean Farley user is required to write their own TSComputeIFunction. 49670026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 49680026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 49690026cea9SSean Farley 49709ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 49719ae8fd06SBarry Smith 49729ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 49730026cea9SSean Farley @*/ 49740910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 49750026cea9SSean Farley { 49760026cea9SSean Farley PetscErrorCode ierr; 49770026cea9SSean Farley Mat A,B; 49780026cea9SSean Farley 49790026cea9SSean Farley PetscFunctionBegin; 49800298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 4981d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 49820910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 49830026cea9SSean Farley PetscFunctionReturn(0); 49840026cea9SSean Farley } 49850026cea9SSean Farley 49860026cea9SSean Farley /*@C 4987b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 49880026cea9SSean Farley 49890026cea9SSean Farley Collective on TS 49900026cea9SSean Farley 49910026cea9SSean Farley Input Arguments: 49920026cea9SSean Farley + ts - time stepping context 49930026cea9SSean Farley . t - time at which to evaluate 49940910c330SBarry Smith . U - state at which to evaluate 49950910c330SBarry Smith . Udot - time derivative of state vector 49960026cea9SSean Farley . shift - shift to apply 49970026cea9SSean Farley - ctx - context 49980026cea9SSean Farley 49990026cea9SSean Farley Output Arguments: 50000026cea9SSean Farley + A - pointer to operator 50010026cea9SSean Farley . B - pointer to preconditioning matrix 50020026cea9SSean Farley - flg - matrix structure flag 50030026cea9SSean Farley 5004b41af12eSJed Brown Level: advanced 50050026cea9SSean Farley 50060026cea9SSean Farley Notes: 50070026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 50080026cea9SSean Farley 5009b41af12eSJed Brown It is only appropriate for problems of the form 5010b41af12eSJed Brown 5011b41af12eSJed Brown $ M Udot = F(U,t) 5012b41af12eSJed Brown 5013b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 5014b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 5015b41af12eSJed Brown an implicit operator of the form 5016b41af12eSJed Brown 5017b41af12eSJed Brown $ shift*M + J 5018b41af12eSJed Brown 5019b41af12eSJed 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 5020b41af12eSJed Brown a copy of M or reassemble it when requested. 5021b41af12eSJed Brown 50220026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 50230026cea9SSean Farley @*/ 5024d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 50250026cea9SSean Farley { 5026b41af12eSJed Brown PetscErrorCode ierr; 5027b41af12eSJed Brown 50280026cea9SSean Farley PetscFunctionBegin; 502994ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 5030b41af12eSJed Brown ts->ijacobian.shift = shift; 50310026cea9SSean Farley PetscFunctionReturn(0); 50320026cea9SSean Farley } 5033b41af12eSJed Brown 5034e817cc15SEmil Constantinescu /*@ 5035e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 5036e817cc15SEmil Constantinescu 5037e817cc15SEmil Constantinescu Not Collective 5038e817cc15SEmil Constantinescu 5039e817cc15SEmil Constantinescu Input Parameter: 5040e817cc15SEmil Constantinescu . ts - the TS context 5041e817cc15SEmil Constantinescu 5042e817cc15SEmil Constantinescu Output Parameter: 50434e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 5044e817cc15SEmil Constantinescu 5045e817cc15SEmil Constantinescu Level: beginner 5046e817cc15SEmil Constantinescu 5047e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 5048e817cc15SEmil Constantinescu @*/ 5049e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 5050e817cc15SEmil Constantinescu { 5051e817cc15SEmil Constantinescu PetscFunctionBegin; 5052e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5053e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 5054e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 5055e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5056e817cc15SEmil Constantinescu } 5057e817cc15SEmil Constantinescu 5058e817cc15SEmil Constantinescu /*@ 5059e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 5060e817cc15SEmil Constantinescu 5061e817cc15SEmil Constantinescu Not Collective 5062e817cc15SEmil Constantinescu 5063e817cc15SEmil Constantinescu Input Parameter: 5064e817cc15SEmil Constantinescu + ts - the TS context 50651297b224SEmil Constantinescu - equation_type - see TSEquationType 5066e817cc15SEmil Constantinescu 5067e817cc15SEmil Constantinescu Level: advanced 5068e817cc15SEmil Constantinescu 5069e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 5070e817cc15SEmil Constantinescu @*/ 5071e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 5072e817cc15SEmil Constantinescu { 5073e817cc15SEmil Constantinescu PetscFunctionBegin; 5074e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5075e817cc15SEmil Constantinescu ts->equation_type = equation_type; 5076e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5077e817cc15SEmil Constantinescu } 50780026cea9SSean Farley 50794af1b03aSJed Brown /*@ 50804af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 50814af1b03aSJed Brown 50824af1b03aSJed Brown Not Collective 50834af1b03aSJed Brown 50844af1b03aSJed Brown Input Parameter: 50854af1b03aSJed Brown . ts - the TS context 50864af1b03aSJed Brown 50874af1b03aSJed Brown Output Parameter: 50884af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 50894af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 50904af1b03aSJed Brown 5091487e0bb9SJed Brown Level: beginner 50924af1b03aSJed Brown 5093cd652676SJed Brown Notes: 5094cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 50954af1b03aSJed Brown 50964af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 50974af1b03aSJed Brown @*/ 50984af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 50994af1b03aSJed Brown { 51004af1b03aSJed Brown PetscFunctionBegin; 51014af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 51024af1b03aSJed Brown PetscValidPointer(reason,2); 51034af1b03aSJed Brown *reason = ts->reason; 51044af1b03aSJed Brown PetscFunctionReturn(0); 51054af1b03aSJed Brown } 51064af1b03aSJed Brown 5107d6ad946cSShri Abhyankar /*@ 5108d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 5109d6ad946cSShri Abhyankar 51106b221cbeSPatrick Sanan Logically Collective; reason must contain common value 5111d6ad946cSShri Abhyankar 51126b221cbeSPatrick Sanan Input Parameters: 5113d6ad946cSShri Abhyankar + ts - the TS context 51146b221cbeSPatrick Sanan - reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 5115d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 5116d6ad946cSShri Abhyankar 5117f5abba47SShri Abhyankar Level: advanced 5118d6ad946cSShri Abhyankar 5119d6ad946cSShri Abhyankar Notes: 51206b221cbeSPatrick Sanan Can only be called while TSSolve() is active. 5121d6ad946cSShri Abhyankar 5122d6ad946cSShri Abhyankar .seealso: TSConvergedReason 5123d6ad946cSShri Abhyankar @*/ 5124d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 5125d6ad946cSShri Abhyankar { 5126d6ad946cSShri Abhyankar PetscFunctionBegin; 5127d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5128d6ad946cSShri Abhyankar ts->reason = reason; 5129d6ad946cSShri Abhyankar PetscFunctionReturn(0); 5130d6ad946cSShri Abhyankar } 5131d6ad946cSShri Abhyankar 5132cc708dedSBarry Smith /*@ 5133cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 5134cc708dedSBarry Smith 5135cc708dedSBarry Smith Not Collective 5136cc708dedSBarry Smith 5137cc708dedSBarry Smith Input Parameter: 5138cc708dedSBarry Smith . ts - the TS context 5139cc708dedSBarry Smith 5140cc708dedSBarry Smith Output Parameter: 514119eac22cSLisandro Dalcin . ftime - the final time. This time corresponds to the final time set with TSSetMaxTime() 5142cc708dedSBarry Smith 5143487e0bb9SJed Brown Level: beginner 5144cc708dedSBarry Smith 5145cc708dedSBarry Smith Notes: 5146cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 5147cc708dedSBarry Smith 5148cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 5149cc708dedSBarry Smith @*/ 5150cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 5151cc708dedSBarry Smith { 5152cc708dedSBarry Smith PetscFunctionBegin; 5153cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5154cc708dedSBarry Smith PetscValidPointer(ftime,2); 5155cc708dedSBarry Smith *ftime = ts->solvetime; 5156cc708dedSBarry Smith PetscFunctionReturn(0); 5157cc708dedSBarry Smith } 5158cc708dedSBarry Smith 51592c18e0fdSBarry Smith /*@ 51605ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 51619f67acb7SJed Brown used by the time integrator. 51629f67acb7SJed Brown 51639f67acb7SJed Brown Not Collective 51649f67acb7SJed Brown 51659f67acb7SJed Brown Input Parameter: 51669f67acb7SJed Brown . ts - TS context 51679f67acb7SJed Brown 51689f67acb7SJed Brown Output Parameter: 51699f67acb7SJed Brown . nits - number of nonlinear iterations 51709f67acb7SJed Brown 51719f67acb7SJed Brown Notes: 51729f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 51739f67acb7SJed Brown 51749f67acb7SJed Brown Level: intermediate 51759f67acb7SJed Brown 51765ef26d82SJed Brown .seealso: TSGetKSPIterations() 51779f67acb7SJed Brown @*/ 51785ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 51799f67acb7SJed Brown { 51809f67acb7SJed Brown PetscFunctionBegin; 51819f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 51829f67acb7SJed Brown PetscValidIntPointer(nits,2); 51835ef26d82SJed Brown *nits = ts->snes_its; 51849f67acb7SJed Brown PetscFunctionReturn(0); 51859f67acb7SJed Brown } 51869f67acb7SJed Brown 51879f67acb7SJed Brown /*@ 51885ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 51899f67acb7SJed Brown used by the time integrator. 51909f67acb7SJed Brown 51919f67acb7SJed Brown Not Collective 51929f67acb7SJed Brown 51939f67acb7SJed Brown Input Parameter: 51949f67acb7SJed Brown . ts - TS context 51959f67acb7SJed Brown 51969f67acb7SJed Brown Output Parameter: 51979f67acb7SJed Brown . lits - number of linear iterations 51989f67acb7SJed Brown 51999f67acb7SJed Brown Notes: 52009f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 52019f67acb7SJed Brown 52029f67acb7SJed Brown Level: intermediate 52039f67acb7SJed Brown 52045ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 52059f67acb7SJed Brown @*/ 52065ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 52079f67acb7SJed Brown { 52089f67acb7SJed Brown PetscFunctionBegin; 52099f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 52109f67acb7SJed Brown PetscValidIntPointer(lits,2); 52115ef26d82SJed Brown *lits = ts->ksp_its; 52129f67acb7SJed Brown PetscFunctionReturn(0); 52139f67acb7SJed Brown } 52149f67acb7SJed Brown 5215cef5090cSJed Brown /*@ 5216cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 5217cef5090cSJed Brown 5218cef5090cSJed Brown Not Collective 5219cef5090cSJed Brown 5220cef5090cSJed Brown Input Parameter: 5221cef5090cSJed Brown . ts - TS context 5222cef5090cSJed Brown 5223cef5090cSJed Brown Output Parameter: 5224cef5090cSJed Brown . rejects - number of steps rejected 5225cef5090cSJed Brown 5226cef5090cSJed Brown Notes: 5227cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5228cef5090cSJed Brown 5229cef5090cSJed Brown Level: intermediate 5230cef5090cSJed Brown 52315ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 5232cef5090cSJed Brown @*/ 5233cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 5234cef5090cSJed Brown { 5235cef5090cSJed Brown PetscFunctionBegin; 5236cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5237cef5090cSJed Brown PetscValidIntPointer(rejects,2); 5238cef5090cSJed Brown *rejects = ts->reject; 5239cef5090cSJed Brown PetscFunctionReturn(0); 5240cef5090cSJed Brown } 5241cef5090cSJed Brown 5242cef5090cSJed Brown /*@ 5243cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 5244cef5090cSJed Brown 5245cef5090cSJed Brown Not Collective 5246cef5090cSJed Brown 5247cef5090cSJed Brown Input Parameter: 5248cef5090cSJed Brown . ts - TS context 5249cef5090cSJed Brown 5250cef5090cSJed Brown Output Parameter: 5251cef5090cSJed Brown . fails - number of failed nonlinear solves 5252cef5090cSJed Brown 5253cef5090cSJed Brown Notes: 5254cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5255cef5090cSJed Brown 5256cef5090cSJed Brown Level: intermediate 5257cef5090cSJed Brown 52585ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 5259cef5090cSJed Brown @*/ 5260cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 5261cef5090cSJed Brown { 5262cef5090cSJed Brown PetscFunctionBegin; 5263cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5264cef5090cSJed Brown PetscValidIntPointer(fails,2); 5265cef5090cSJed Brown *fails = ts->num_snes_failures; 5266cef5090cSJed Brown PetscFunctionReturn(0); 5267cef5090cSJed Brown } 5268cef5090cSJed Brown 5269cef5090cSJed Brown /*@ 5270cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 5271cef5090cSJed Brown 5272cef5090cSJed Brown Not Collective 5273cef5090cSJed Brown 5274cef5090cSJed Brown Input Parameter: 5275cef5090cSJed Brown + ts - TS context 5276cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 5277cef5090cSJed Brown 5278cef5090cSJed Brown Notes: 5279cef5090cSJed Brown The counter is reset to zero for each step 5280cef5090cSJed Brown 5281cef5090cSJed Brown Options Database Key: 5282cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 5283cef5090cSJed Brown 5284cef5090cSJed Brown Level: intermediate 5285cef5090cSJed Brown 52865ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5287cef5090cSJed Brown @*/ 5288cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 5289cef5090cSJed Brown { 5290cef5090cSJed Brown PetscFunctionBegin; 5291cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5292cef5090cSJed Brown ts->max_reject = rejects; 5293cef5090cSJed Brown PetscFunctionReturn(0); 5294cef5090cSJed Brown } 5295cef5090cSJed Brown 5296cef5090cSJed Brown /*@ 5297cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 5298cef5090cSJed Brown 5299cef5090cSJed Brown Not Collective 5300cef5090cSJed Brown 5301cef5090cSJed Brown Input Parameter: 5302cef5090cSJed Brown + ts - TS context 5303cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 5304cef5090cSJed Brown 5305cef5090cSJed Brown Notes: 5306cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 5307cef5090cSJed Brown 5308cef5090cSJed Brown Options Database Key: 5309cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 5310cef5090cSJed Brown 5311cef5090cSJed Brown Level: intermediate 5312cef5090cSJed Brown 53135ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 5314cef5090cSJed Brown @*/ 5315cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 5316cef5090cSJed Brown { 5317cef5090cSJed Brown PetscFunctionBegin; 5318cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5319cef5090cSJed Brown ts->max_snes_failures = fails; 5320cef5090cSJed Brown PetscFunctionReturn(0); 5321cef5090cSJed Brown } 5322cef5090cSJed Brown 5323cef5090cSJed Brown /*@ 5324cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 5325cef5090cSJed Brown 5326cef5090cSJed Brown Not Collective 5327cef5090cSJed Brown 5328cef5090cSJed Brown Input Parameter: 5329cef5090cSJed Brown + ts - TS context 5330cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 5331cef5090cSJed Brown 5332cef5090cSJed Brown Options Database Key: 5333cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 5334cef5090cSJed Brown 5335cef5090cSJed Brown Level: intermediate 5336cef5090cSJed Brown 53375ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5338cef5090cSJed Brown @*/ 5339cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 5340cef5090cSJed Brown { 5341cef5090cSJed Brown PetscFunctionBegin; 5342cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5343cef5090cSJed Brown ts->errorifstepfailed = err; 5344cef5090cSJed Brown PetscFunctionReturn(0); 5345cef5090cSJed Brown } 5346cef5090cSJed Brown 5347fb1732b5SBarry Smith /*@C 5348fde5950dSBarry 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 5349fb1732b5SBarry Smith 5350fb1732b5SBarry Smith Collective on TS 5351fb1732b5SBarry Smith 5352fb1732b5SBarry Smith Input Parameters: 5353fb1732b5SBarry Smith + ts - the TS context 5354fb1732b5SBarry Smith . step - current time-step 5355fb1732b5SBarry Smith . ptime - current time 53560910c330SBarry Smith . u - current state 5357721cd6eeSBarry Smith - vf - viewer and its format 5358fb1732b5SBarry Smith 5359fb1732b5SBarry Smith Level: intermediate 5360fb1732b5SBarry Smith 5361fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5362fb1732b5SBarry Smith @*/ 5363721cd6eeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf) 5364fb1732b5SBarry Smith { 5365fb1732b5SBarry Smith PetscErrorCode ierr; 5366fb1732b5SBarry Smith 5367fb1732b5SBarry Smith PetscFunctionBegin; 5368721cd6eeSBarry Smith ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr); 5369721cd6eeSBarry Smith ierr = VecView(u,vf->viewer);CHKERRQ(ierr); 5370721cd6eeSBarry Smith ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr); 5371ed81e22dSJed Brown PetscFunctionReturn(0); 5372ed81e22dSJed Brown } 5373ed81e22dSJed Brown 5374ed81e22dSJed Brown /*@C 5375ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5376ed81e22dSJed Brown 5377ed81e22dSJed Brown Collective on TS 5378ed81e22dSJed Brown 5379ed81e22dSJed Brown Input Parameters: 5380ed81e22dSJed Brown + ts - the TS context 5381ed81e22dSJed Brown . step - current time-step 5382ed81e22dSJed Brown . ptime - current time 53830910c330SBarry Smith . u - current state 5384ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5385ed81e22dSJed Brown 5386ed81e22dSJed Brown Level: intermediate 5387ed81e22dSJed Brown 5388ed81e22dSJed Brown Notes: 5389ed81e22dSJed 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. 5390ed81e22dSJed Brown These are named according to the file name template. 5391ed81e22dSJed Brown 5392ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5393ed81e22dSJed Brown 5394ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5395ed81e22dSJed Brown @*/ 53960910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5397ed81e22dSJed Brown { 5398ed81e22dSJed Brown PetscErrorCode ierr; 5399ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 5400ed81e22dSJed Brown PetscViewer viewer; 5401ed81e22dSJed Brown 5402ed81e22dSJed Brown PetscFunctionBegin; 540363e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 54048caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5405ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 54060910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 5407ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5408ed81e22dSJed Brown PetscFunctionReturn(0); 5409ed81e22dSJed Brown } 5410ed81e22dSJed Brown 5411ed81e22dSJed Brown /*@C 5412ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5413ed81e22dSJed Brown 5414ed81e22dSJed Brown Collective on TS 5415ed81e22dSJed Brown 5416ed81e22dSJed Brown Input Parameters: 5417ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5418ed81e22dSJed Brown 5419ed81e22dSJed Brown Level: intermediate 5420ed81e22dSJed Brown 5421ed81e22dSJed Brown Note: 5422ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5423ed81e22dSJed Brown 5424ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5425ed81e22dSJed Brown @*/ 5426ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5427ed81e22dSJed Brown { 5428ed81e22dSJed Brown PetscErrorCode ierr; 5429ed81e22dSJed Brown 5430ed81e22dSJed Brown PetscFunctionBegin; 5431ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5432fb1732b5SBarry Smith PetscFunctionReturn(0); 5433fb1732b5SBarry Smith } 5434fb1732b5SBarry Smith 543584df9cb4SJed Brown /*@ 5436552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 543784df9cb4SJed Brown 5438ed81e22dSJed Brown Collective on TS if controller has not been created yet 543984df9cb4SJed Brown 544084df9cb4SJed Brown Input Arguments: 5441ed81e22dSJed Brown . ts - time stepping context 544284df9cb4SJed Brown 544384df9cb4SJed Brown Output Arguments: 5444ed81e22dSJed Brown . adapt - adaptive controller 544584df9cb4SJed Brown 544684df9cb4SJed Brown Level: intermediate 544784df9cb4SJed Brown 5448ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 544984df9cb4SJed Brown @*/ 5450552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 545184df9cb4SJed Brown { 545284df9cb4SJed Brown PetscErrorCode ierr; 545384df9cb4SJed Brown 545484df9cb4SJed Brown PetscFunctionBegin; 545584df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5456bec58848SLisandro Dalcin PetscValidPointer(adapt,2); 545784df9cb4SJed Brown if (!ts->adapt) { 5458ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 54593bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 54601c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 546184df9cb4SJed Brown } 5462bec58848SLisandro Dalcin *adapt = ts->adapt; 546384df9cb4SJed Brown PetscFunctionReturn(0); 546484df9cb4SJed Brown } 5465d6ebe24aSShri Abhyankar 54661c3436cfSJed Brown /*@ 54671c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 54681c3436cfSJed Brown 54691c3436cfSJed Brown Logically Collective 54701c3436cfSJed Brown 54711c3436cfSJed Brown Input Arguments: 54721c3436cfSJed Brown + ts - time integration context 54731c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 54740298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 54751c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 54760298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 54771c3436cfSJed Brown 5478a3cdaa26SBarry Smith Options Database keys: 5479a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5480a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5481a3cdaa26SBarry Smith 54823ff766beSShri Abhyankar Notes: 54833ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 54843ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 54853ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 54863ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 54873ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 54883ff766beSShri Abhyankar differential variables. 54893ff766beSShri Abhyankar 54901c3436cfSJed Brown Level: beginner 54911c3436cfSJed Brown 5492c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 54931c3436cfSJed Brown @*/ 54941c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 54951c3436cfSJed Brown { 54961c3436cfSJed Brown PetscErrorCode ierr; 54971c3436cfSJed Brown 54981c3436cfSJed Brown PetscFunctionBegin; 5499c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 55001c3436cfSJed Brown if (vatol) { 55011c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 55021c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 55031c3436cfSJed Brown ts->vatol = vatol; 55041c3436cfSJed Brown } 5505c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 55061c3436cfSJed Brown if (vrtol) { 55071c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 55081c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 55091c3436cfSJed Brown ts->vrtol = vrtol; 55101c3436cfSJed Brown } 55111c3436cfSJed Brown PetscFunctionReturn(0); 55121c3436cfSJed Brown } 55131c3436cfSJed Brown 5514c5033834SJed Brown /*@ 5515c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5516c5033834SJed Brown 5517c5033834SJed Brown Logically Collective 5518c5033834SJed Brown 5519c5033834SJed Brown Input Arguments: 5520c5033834SJed Brown . ts - time integration context 5521c5033834SJed Brown 5522c5033834SJed Brown Output Arguments: 55230298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 55240298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 55250298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 55260298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 5527c5033834SJed Brown 5528c5033834SJed Brown Level: beginner 5529c5033834SJed Brown 5530c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5531c5033834SJed Brown @*/ 5532c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5533c5033834SJed Brown { 5534c5033834SJed Brown PetscFunctionBegin; 5535c5033834SJed Brown if (atol) *atol = ts->atol; 5536c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5537c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5538c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 5539c5033834SJed Brown PetscFunctionReturn(0); 5540c5033834SJed Brown } 5541c5033834SJed Brown 55429c6b16b5SShri Abhyankar /*@ 5543a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 55449c6b16b5SShri Abhyankar 55459c6b16b5SShri Abhyankar Collective on TS 55469c6b16b5SShri Abhyankar 55479c6b16b5SShri Abhyankar Input Arguments: 55489c6b16b5SShri Abhyankar + ts - time stepping context 5549a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5550a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 55519c6b16b5SShri Abhyankar 55529c6b16b5SShri Abhyankar Output Arguments: 5553a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 55547453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5555a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 55569c6b16b5SShri Abhyankar 55579c6b16b5SShri Abhyankar Level: developer 55589c6b16b5SShri Abhyankar 5559deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 55609c6b16b5SShri Abhyankar @*/ 55617453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 55629c6b16b5SShri Abhyankar { 55639c6b16b5SShri Abhyankar PetscErrorCode ierr; 55649c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 55657453f775SEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 55667453f775SEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 55679c6b16b5SShri Abhyankar const PetscScalar *u,*y; 55687453f775SEmil Constantinescu PetscReal sum,suma,sumr,gsum,gsuma,gsumr,diff; 55697453f775SEmil Constantinescu PetscReal tol,tola,tolr; 55707453f775SEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 55719c6b16b5SShri Abhyankar 55729c6b16b5SShri Abhyankar PetscFunctionBegin; 55739c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5574a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5575a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5576a4868fbcSLisandro Dalcin PetscValidType(U,2); 5577a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5578a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5579a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 55808a175baeSEmil Constantinescu PetscValidPointer(norma,5); 55818a175baeSEmil Constantinescu PetscValidPointer(normr,6); 5582a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 55839c6b16b5SShri Abhyankar 55849c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 55859c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 55869c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 55879c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 55889c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 55897453f775SEmil Constantinescu sum = 0.; n_loc = 0; 55907453f775SEmil Constantinescu suma = 0.; na_loc = 0; 55917453f775SEmil Constantinescu sumr = 0.; nr_loc = 0; 55929c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 55939c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 55949c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 55959c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 55969c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 559776cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 55987453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 55997453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 56007453f775SEmil Constantinescu if(tola>0.){ 56017453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 56027453f775SEmil Constantinescu na_loc++; 56037453f775SEmil Constantinescu } 56047453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 56057453f775SEmil Constantinescu if(tolr>0.){ 56067453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 56077453f775SEmil Constantinescu nr_loc++; 56087453f775SEmil Constantinescu } 56097453f775SEmil Constantinescu tol=tola+tolr; 56107453f775SEmil Constantinescu if(tol>0.){ 56117453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 56127453f775SEmil Constantinescu n_loc++; 56137453f775SEmil Constantinescu } 56149c6b16b5SShri Abhyankar } 56159c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 56169c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 56179c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 56189c6b16b5SShri Abhyankar const PetscScalar *atol; 56199c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 56209c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 562176cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 56227453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 56237453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 56247453f775SEmil Constantinescu if(tola>0.){ 56257453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 56267453f775SEmil Constantinescu na_loc++; 56277453f775SEmil Constantinescu } 56287453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 56297453f775SEmil Constantinescu if(tolr>0.){ 56307453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 56317453f775SEmil Constantinescu nr_loc++; 56327453f775SEmil Constantinescu } 56337453f775SEmil Constantinescu tol=tola+tolr; 56347453f775SEmil Constantinescu if(tol>0.){ 56357453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 56367453f775SEmil Constantinescu n_loc++; 56377453f775SEmil Constantinescu } 56389c6b16b5SShri Abhyankar } 56399c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 56409c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 56419c6b16b5SShri Abhyankar const PetscScalar *rtol; 56429c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 56439c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 564476cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 56457453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 56467453f775SEmil Constantinescu tola = ts->atol; 56477453f775SEmil Constantinescu if(tola>0.){ 56487453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 56497453f775SEmil Constantinescu na_loc++; 56507453f775SEmil Constantinescu } 56517453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 56527453f775SEmil Constantinescu if(tolr>0.){ 56537453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 56547453f775SEmil Constantinescu nr_loc++; 56557453f775SEmil Constantinescu } 56567453f775SEmil Constantinescu tol=tola+tolr; 56577453f775SEmil Constantinescu if(tol>0.){ 56587453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 56597453f775SEmil Constantinescu n_loc++; 56607453f775SEmil Constantinescu } 56619c6b16b5SShri Abhyankar } 56629c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 56639c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 56649c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 566576cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 56667453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 56677453f775SEmil Constantinescu tola = ts->atol; 56687453f775SEmil Constantinescu if(tola>0.){ 56697453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 56707453f775SEmil Constantinescu na_loc++; 56717453f775SEmil Constantinescu } 56727453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 56737453f775SEmil Constantinescu if(tolr>0.){ 56747453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 56757453f775SEmil Constantinescu nr_loc++; 56767453f775SEmil Constantinescu } 56777453f775SEmil Constantinescu tol=tola+tolr; 56787453f775SEmil Constantinescu if(tol>0.){ 56797453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 56807453f775SEmil Constantinescu n_loc++; 56817453f775SEmil Constantinescu } 56829c6b16b5SShri Abhyankar } 56839c6b16b5SShri Abhyankar } 56849c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 56859c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 56869c6b16b5SShri Abhyankar 56877453f775SEmil Constantinescu err_loc[0] = sum; 56887453f775SEmil Constantinescu err_loc[1] = suma; 56897453f775SEmil Constantinescu err_loc[2] = sumr; 56907453f775SEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 56917453f775SEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 56927453f775SEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 56937453f775SEmil Constantinescu 5694a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 56957453f775SEmil Constantinescu 56967453f775SEmil Constantinescu gsum = err_glb[0]; 56977453f775SEmil Constantinescu gsuma = err_glb[1]; 56987453f775SEmil Constantinescu gsumr = err_glb[2]; 56997453f775SEmil Constantinescu n_glb = err_glb[3]; 57007453f775SEmil Constantinescu na_glb = err_glb[4]; 57017453f775SEmil Constantinescu nr_glb = err_glb[5]; 57027453f775SEmil Constantinescu 5703b1316ef9SEmil Constantinescu *norm = 0.; 5704b1316ef9SEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 5705b1316ef9SEmil Constantinescu *norma = 0.; 5706b1316ef9SEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 5707b1316ef9SEmil Constantinescu *normr = 0.; 5708b1316ef9SEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 57099c6b16b5SShri Abhyankar 57109c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 57117453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 57127453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 57139c6b16b5SShri Abhyankar PetscFunctionReturn(0); 57149c6b16b5SShri Abhyankar } 57159c6b16b5SShri Abhyankar 57169c6b16b5SShri Abhyankar /*@ 5717a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 57189c6b16b5SShri Abhyankar 57199c6b16b5SShri Abhyankar Collective on TS 57209c6b16b5SShri Abhyankar 57219c6b16b5SShri Abhyankar Input Arguments: 57229c6b16b5SShri Abhyankar + ts - time stepping context 5723a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5724a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 57259c6b16b5SShri Abhyankar 57269c6b16b5SShri Abhyankar Output Arguments: 5727a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 57287453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5729a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 57309c6b16b5SShri Abhyankar 57319c6b16b5SShri Abhyankar Level: developer 57329c6b16b5SShri Abhyankar 5733deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 57349c6b16b5SShri Abhyankar @*/ 57357453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 57369c6b16b5SShri Abhyankar { 57379c6b16b5SShri Abhyankar PetscErrorCode ierr; 57387453f775SEmil Constantinescu PetscInt i,n,N,rstart; 57399c6b16b5SShri Abhyankar const PetscScalar *u,*y; 57407453f775SEmil Constantinescu PetscReal max,gmax,maxa,gmaxa,maxr,gmaxr; 57417453f775SEmil Constantinescu PetscReal tol,tola,tolr,diff; 57427453f775SEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 57439c6b16b5SShri Abhyankar 57449c6b16b5SShri Abhyankar PetscFunctionBegin; 57459c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5746a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5747a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5748a4868fbcSLisandro Dalcin PetscValidType(U,2); 5749a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5750a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5751a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 57528a175baeSEmil Constantinescu PetscValidPointer(norma,5); 57538a175baeSEmil Constantinescu PetscValidPointer(normr,6); 5754a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 57559c6b16b5SShri Abhyankar 57569c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 57579c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 57589c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 57599c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 57609c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 57617453f775SEmil Constantinescu 57627453f775SEmil Constantinescu max=0.; 57637453f775SEmil Constantinescu maxa=0.; 57647453f775SEmil Constantinescu maxr=0.; 57657453f775SEmil Constantinescu 57667453f775SEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 57679c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 57689c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 57699c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 57707453f775SEmil Constantinescu 57717453f775SEmil Constantinescu for (i=0; i<n; i++) { 577276cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 57737453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 57747453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 57757453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 57767453f775SEmil Constantinescu tol = tola+tolr; 57777453f775SEmil Constantinescu if(tola>0.){ 57787453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 57797453f775SEmil Constantinescu } 57807453f775SEmil Constantinescu if(tolr>0.){ 57817453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 57827453f775SEmil Constantinescu } 57837453f775SEmil Constantinescu if(tol>0.){ 57847453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 57857453f775SEmil Constantinescu } 57869c6b16b5SShri Abhyankar } 57879c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 57889c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 57899c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 57909c6b16b5SShri Abhyankar const PetscScalar *atol; 57919c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 57927453f775SEmil Constantinescu for (i=0; i<n; i++) { 579376cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 57947453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 57957453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 57967453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 57977453f775SEmil Constantinescu tol = tola+tolr; 57987453f775SEmil Constantinescu if(tola>0.){ 57997453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 58007453f775SEmil Constantinescu } 58017453f775SEmil Constantinescu if(tolr>0.){ 58027453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 58037453f775SEmil Constantinescu } 58047453f775SEmil Constantinescu if(tol>0.){ 58057453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 58067453f775SEmil Constantinescu } 58079c6b16b5SShri Abhyankar } 58089c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58099c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 58109c6b16b5SShri Abhyankar const PetscScalar *rtol; 58119c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58127453f775SEmil Constantinescu 58137453f775SEmil Constantinescu for (i=0; i<n; i++) { 581476cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 58157453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 58167453f775SEmil Constantinescu tola = ts->atol; 58177453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58187453f775SEmil Constantinescu tol = tola+tolr; 58197453f775SEmil Constantinescu if(tola>0.){ 58207453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 58217453f775SEmil Constantinescu } 58227453f775SEmil Constantinescu if(tolr>0.){ 58237453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 58247453f775SEmil Constantinescu } 58257453f775SEmil Constantinescu if(tol>0.){ 58267453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 58277453f775SEmil Constantinescu } 58289c6b16b5SShri Abhyankar } 58299c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58309c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 58317453f775SEmil Constantinescu 58327453f775SEmil Constantinescu for (i=0; i<n; i++) { 583376cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 58347453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 58357453f775SEmil Constantinescu tola = ts->atol; 58367453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58377453f775SEmil Constantinescu tol = tola+tolr; 58387453f775SEmil Constantinescu if(tola>0.){ 58397453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 58407453f775SEmil Constantinescu } 58417453f775SEmil Constantinescu if(tolr>0.){ 58427453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 58437453f775SEmil Constantinescu } 58447453f775SEmil Constantinescu if(tol>0.){ 58457453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 58467453f775SEmil Constantinescu } 58479c6b16b5SShri Abhyankar } 58489c6b16b5SShri Abhyankar } 58499c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 58509c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 58517453f775SEmil Constantinescu err_loc[0] = max; 58527453f775SEmil Constantinescu err_loc[1] = maxa; 58537453f775SEmil Constantinescu err_loc[2] = maxr; 5854a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 58557453f775SEmil Constantinescu gmax = err_glb[0]; 58567453f775SEmil Constantinescu gmaxa = err_glb[1]; 58577453f775SEmil Constantinescu gmaxr = err_glb[2]; 58589c6b16b5SShri Abhyankar 58599c6b16b5SShri Abhyankar *norm = gmax; 58607453f775SEmil Constantinescu *norma = gmaxa; 58617453f775SEmil Constantinescu *normr = gmaxr; 58629c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 58637453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 58647453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 58659c6b16b5SShri Abhyankar PetscFunctionReturn(0); 58669c6b16b5SShri Abhyankar } 58679c6b16b5SShri Abhyankar 58681c3436cfSJed Brown /*@ 58698a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 58701c3436cfSJed Brown 58711c3436cfSJed Brown Collective on TS 58721c3436cfSJed Brown 58731c3436cfSJed Brown Input Arguments: 58741c3436cfSJed Brown + ts - time stepping context 5875a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5876a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5877a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 58787619abb3SShri 58791c3436cfSJed Brown Output Arguments: 5880a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 58818a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5882a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 5883a4868fbcSLisandro Dalcin 5884a4868fbcSLisandro Dalcin Options Database Keys: 5885a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5886a4868fbcSLisandro Dalcin 58871c3436cfSJed Brown Level: developer 58881c3436cfSJed Brown 58898a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm 58901c3436cfSJed Brown @*/ 58917453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 58921c3436cfSJed Brown { 58938beabaa1SBarry Smith PetscErrorCode ierr; 58941c3436cfSJed Brown 58951c3436cfSJed Brown PetscFunctionBegin; 5896a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 58977453f775SEmil Constantinescu ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 5898a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 58997453f775SEmil Constantinescu ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 5900a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 59011c3436cfSJed Brown PetscFunctionReturn(0); 59021c3436cfSJed Brown } 59031c3436cfSJed Brown 59048a175baeSEmil Constantinescu 59058a175baeSEmil Constantinescu /*@ 59068a175baeSEmil Constantinescu TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances 59078a175baeSEmil Constantinescu 59088a175baeSEmil Constantinescu Collective on TS 59098a175baeSEmil Constantinescu 59108a175baeSEmil Constantinescu Input Arguments: 59118a175baeSEmil Constantinescu + ts - time stepping context 59128a175baeSEmil Constantinescu . E - error vector 59138a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 59148a175baeSEmil Constantinescu - Y - state vector, previous time step 59158a175baeSEmil Constantinescu 59168a175baeSEmil Constantinescu Output Arguments: 5917a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 59188a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5919a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 59208a175baeSEmil Constantinescu 59218a175baeSEmil Constantinescu Level: developer 59228a175baeSEmil Constantinescu 59238a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity() 59248a175baeSEmil Constantinescu @*/ 59258a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 59268a175baeSEmil Constantinescu { 59278a175baeSEmil Constantinescu PetscErrorCode ierr; 59288a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 59298a175baeSEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 59308a175baeSEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 59318a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 59328a175baeSEmil Constantinescu PetscReal err,sum,suma,sumr,gsum,gsuma,gsumr; 59338a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 59348a175baeSEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 59358a175baeSEmil Constantinescu 59368a175baeSEmil Constantinescu PetscFunctionBegin; 59378a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 59388a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 59398a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 59408a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 59418a175baeSEmil Constantinescu PetscValidType(E,2); 59428a175baeSEmil Constantinescu PetscValidType(U,3); 59438a175baeSEmil Constantinescu PetscValidType(Y,4); 59448a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 59458a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 59468a175baeSEmil Constantinescu PetscValidPointer(norm,5); 59478a175baeSEmil Constantinescu PetscValidPointer(norma,6); 59488a175baeSEmil Constantinescu PetscValidPointer(normr,7); 59498a175baeSEmil Constantinescu 59508a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 59518a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 59528a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 59538a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 59548a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 59558a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 59568a175baeSEmil Constantinescu sum = 0.; n_loc = 0; 59578a175baeSEmil Constantinescu suma = 0.; na_loc = 0; 59588a175baeSEmil Constantinescu sumr = 0.; nr_loc = 0; 59598a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { 59608a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 59618a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59628a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59638a175baeSEmil Constantinescu for (i=0; i<n; i++) { 596476cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 59658a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 59668a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 59678a175baeSEmil Constantinescu if(tola>0.){ 59688a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 59698a175baeSEmil Constantinescu na_loc++; 59708a175baeSEmil Constantinescu } 59718a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59728a175baeSEmil Constantinescu if(tolr>0.){ 59738a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 59748a175baeSEmil Constantinescu nr_loc++; 59758a175baeSEmil Constantinescu } 59768a175baeSEmil Constantinescu tol=tola+tolr; 59778a175baeSEmil Constantinescu if(tol>0.){ 59788a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 59798a175baeSEmil Constantinescu n_loc++; 59808a175baeSEmil Constantinescu } 59818a175baeSEmil Constantinescu } 59828a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59838a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59848a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 59858a175baeSEmil Constantinescu const PetscScalar *atol; 59868a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59878a175baeSEmil Constantinescu for (i=0; i<n; i++) { 598876cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 59898a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 59908a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 59918a175baeSEmil Constantinescu if(tola>0.){ 59928a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 59938a175baeSEmil Constantinescu na_loc++; 59948a175baeSEmil Constantinescu } 59958a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59968a175baeSEmil Constantinescu if(tolr>0.){ 59978a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 59988a175baeSEmil Constantinescu nr_loc++; 59998a175baeSEmil Constantinescu } 60008a175baeSEmil Constantinescu tol=tola+tolr; 60018a175baeSEmil Constantinescu if(tol>0.){ 60028a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 60038a175baeSEmil Constantinescu n_loc++; 60048a175baeSEmil Constantinescu } 60058a175baeSEmil Constantinescu } 60068a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 60078a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 60088a175baeSEmil Constantinescu const PetscScalar *rtol; 60098a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60108a175baeSEmil Constantinescu for (i=0; i<n; i++) { 601176cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 60128a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 60138a175baeSEmil Constantinescu tola = ts->atol; 60148a175baeSEmil Constantinescu if(tola>0.){ 60158a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 60168a175baeSEmil Constantinescu na_loc++; 60178a175baeSEmil Constantinescu } 60188a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60198a175baeSEmil Constantinescu if(tolr>0.){ 60208a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 60218a175baeSEmil Constantinescu nr_loc++; 60228a175baeSEmil Constantinescu } 60238a175baeSEmil Constantinescu tol=tola+tolr; 60248a175baeSEmil Constantinescu if(tol>0.){ 60258a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 60268a175baeSEmil Constantinescu n_loc++; 60278a175baeSEmil Constantinescu } 60288a175baeSEmil Constantinescu } 60298a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60308a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 60318a175baeSEmil Constantinescu for (i=0; i<n; i++) { 603276cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 60338a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 60348a175baeSEmil Constantinescu tola = ts->atol; 60358a175baeSEmil Constantinescu if(tola>0.){ 60368a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 60378a175baeSEmil Constantinescu na_loc++; 60388a175baeSEmil Constantinescu } 60398a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60408a175baeSEmil Constantinescu if(tolr>0.){ 60418a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 60428a175baeSEmil Constantinescu nr_loc++; 60438a175baeSEmil Constantinescu } 60448a175baeSEmil Constantinescu tol=tola+tolr; 60458a175baeSEmil Constantinescu if(tol>0.){ 60468a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 60478a175baeSEmil Constantinescu n_loc++; 60488a175baeSEmil Constantinescu } 60498a175baeSEmil Constantinescu } 60508a175baeSEmil Constantinescu } 60518a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 60528a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 60538a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 60548a175baeSEmil Constantinescu 60558a175baeSEmil Constantinescu err_loc[0] = sum; 60568a175baeSEmil Constantinescu err_loc[1] = suma; 60578a175baeSEmil Constantinescu err_loc[2] = sumr; 60588a175baeSEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 60598a175baeSEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 60608a175baeSEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 60618a175baeSEmil Constantinescu 6062a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 60638a175baeSEmil Constantinescu 60648a175baeSEmil Constantinescu gsum = err_glb[0]; 60658a175baeSEmil Constantinescu gsuma = err_glb[1]; 60668a175baeSEmil Constantinescu gsumr = err_glb[2]; 60678a175baeSEmil Constantinescu n_glb = err_glb[3]; 60688a175baeSEmil Constantinescu na_glb = err_glb[4]; 60698a175baeSEmil Constantinescu nr_glb = err_glb[5]; 60708a175baeSEmil Constantinescu 60718a175baeSEmil Constantinescu *norm = 0.; 60728a175baeSEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 60738a175baeSEmil Constantinescu *norma = 0.; 60748a175baeSEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 60758a175baeSEmil Constantinescu *normr = 0.; 60768a175baeSEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 60778a175baeSEmil Constantinescu 60788a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 60798a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 60808a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 60818a175baeSEmil Constantinescu PetscFunctionReturn(0); 60828a175baeSEmil Constantinescu } 60838a175baeSEmil Constantinescu 60848a175baeSEmil Constantinescu /*@ 60858a175baeSEmil Constantinescu TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances 60868a175baeSEmil Constantinescu Collective on TS 60878a175baeSEmil Constantinescu 60888a175baeSEmil Constantinescu Input Arguments: 60898a175baeSEmil Constantinescu + ts - time stepping context 60908a175baeSEmil Constantinescu . E - error vector 60918a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 60928a175baeSEmil Constantinescu - Y - state vector, previous time step 60938a175baeSEmil Constantinescu 60948a175baeSEmil Constantinescu Output Arguments: 6095a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 60968a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 6097a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 60988a175baeSEmil Constantinescu 60998a175baeSEmil Constantinescu Level: developer 61008a175baeSEmil Constantinescu 61018a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2() 61028a175baeSEmil Constantinescu @*/ 61038a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 61048a175baeSEmil Constantinescu { 61058a175baeSEmil Constantinescu PetscErrorCode ierr; 61068a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 61078a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 61088a175baeSEmil Constantinescu PetscReal err,max,gmax,maxa,gmaxa,maxr,gmaxr; 61098a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 61108a175baeSEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 61118a175baeSEmil Constantinescu 61128a175baeSEmil Constantinescu PetscFunctionBegin; 61138a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 61148a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 61158a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 61168a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 61178a175baeSEmil Constantinescu PetscValidType(E,2); 61188a175baeSEmil Constantinescu PetscValidType(U,3); 61198a175baeSEmil Constantinescu PetscValidType(Y,4); 61208a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 61218a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 61228a175baeSEmil Constantinescu PetscValidPointer(norm,5); 61238a175baeSEmil Constantinescu PetscValidPointer(norma,6); 61248a175baeSEmil Constantinescu PetscValidPointer(normr,7); 61258a175baeSEmil Constantinescu 61268a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 61278a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 61288a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 61298a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 61308a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 61318a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 61328a175baeSEmil Constantinescu 61338a175baeSEmil Constantinescu max=0.; 61348a175baeSEmil Constantinescu maxa=0.; 61358a175baeSEmil Constantinescu maxr=0.; 61368a175baeSEmil Constantinescu 61378a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 61388a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 61398a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61408a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61418a175baeSEmil Constantinescu 61428a175baeSEmil Constantinescu for (i=0; i<n; i++) { 614376cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 61448a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 61458a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 61468a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61478a175baeSEmil Constantinescu tol = tola+tolr; 61488a175baeSEmil Constantinescu if(tola>0.){ 61498a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 61508a175baeSEmil Constantinescu } 61518a175baeSEmil Constantinescu if(tolr>0.){ 61528a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 61538a175baeSEmil Constantinescu } 61548a175baeSEmil Constantinescu if(tol>0.){ 61558a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 61568a175baeSEmil Constantinescu } 61578a175baeSEmil Constantinescu } 61588a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61598a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61608a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 61618a175baeSEmil Constantinescu const PetscScalar *atol; 61628a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61638a175baeSEmil Constantinescu for (i=0; i<n; i++) { 616476cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 61658a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 61668a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 61678a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61688a175baeSEmil Constantinescu tol = tola+tolr; 61698a175baeSEmil Constantinescu if(tola>0.){ 61708a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 61718a175baeSEmil Constantinescu } 61728a175baeSEmil Constantinescu if(tolr>0.){ 61738a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 61748a175baeSEmil Constantinescu } 61758a175baeSEmil Constantinescu if(tol>0.){ 61768a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 61778a175baeSEmil Constantinescu } 61788a175baeSEmil Constantinescu } 61798a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61808a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 61818a175baeSEmil Constantinescu const PetscScalar *rtol; 61828a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61838a175baeSEmil Constantinescu 61848a175baeSEmil Constantinescu for (i=0; i<n; i++) { 618576cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 61868a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 61878a175baeSEmil Constantinescu tola = ts->atol; 61888a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61898a175baeSEmil Constantinescu tol = tola+tolr; 61908a175baeSEmil Constantinescu if(tola>0.){ 61918a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 61928a175baeSEmil Constantinescu } 61938a175baeSEmil Constantinescu if(tolr>0.){ 61948a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 61958a175baeSEmil Constantinescu } 61968a175baeSEmil Constantinescu if(tol>0.){ 61978a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 61988a175baeSEmil Constantinescu } 61998a175baeSEmil Constantinescu } 62008a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 62018a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 62028a175baeSEmil Constantinescu 62038a175baeSEmil Constantinescu for (i=0; i<n; i++) { 620476cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 62058a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 62068a175baeSEmil Constantinescu tola = ts->atol; 62078a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 62088a175baeSEmil Constantinescu tol = tola+tolr; 62098a175baeSEmil Constantinescu if(tola>0.){ 62108a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 62118a175baeSEmil Constantinescu } 62128a175baeSEmil Constantinescu if(tolr>0.){ 62138a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 62148a175baeSEmil Constantinescu } 62158a175baeSEmil Constantinescu if(tol>0.){ 62168a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 62178a175baeSEmil Constantinescu } 62188a175baeSEmil Constantinescu } 62198a175baeSEmil Constantinescu } 62208a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 62218a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 62228a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 62238a175baeSEmil Constantinescu err_loc[0] = max; 62248a175baeSEmil Constantinescu err_loc[1] = maxa; 62258a175baeSEmil Constantinescu err_loc[2] = maxr; 6226a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 62278a175baeSEmil Constantinescu gmax = err_glb[0]; 62288a175baeSEmil Constantinescu gmaxa = err_glb[1]; 62298a175baeSEmil Constantinescu gmaxr = err_glb[2]; 62308a175baeSEmil Constantinescu 62318a175baeSEmil Constantinescu *norm = gmax; 62328a175baeSEmil Constantinescu *norma = gmaxa; 62338a175baeSEmil Constantinescu *normr = gmaxr; 62348a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 62358a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 62368a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 62378a175baeSEmil Constantinescu PetscFunctionReturn(0); 62388a175baeSEmil Constantinescu } 62398a175baeSEmil Constantinescu 62408a175baeSEmil Constantinescu /*@ 62418a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 62428a175baeSEmil Constantinescu 62438a175baeSEmil Constantinescu Collective on TS 62448a175baeSEmil Constantinescu 62458a175baeSEmil Constantinescu Input Arguments: 62468a175baeSEmil Constantinescu + ts - time stepping context 62478a175baeSEmil Constantinescu . E - error vector 62488a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 62498a175baeSEmil Constantinescu . Y - state vector, previous time step 62508a175baeSEmil Constantinescu - wnormtype - norm type, either NORM_2 or NORM_INFINITY 62518a175baeSEmil Constantinescu 62528a175baeSEmil Constantinescu Output Arguments: 6253a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 62548a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 6255a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 62568a175baeSEmil Constantinescu 62578a175baeSEmil Constantinescu Options Database Keys: 62588a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 62598a175baeSEmil Constantinescu 62608a175baeSEmil Constantinescu Level: developer 62618a175baeSEmil Constantinescu 62628a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 62638a175baeSEmil Constantinescu @*/ 62648a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 62658a175baeSEmil Constantinescu { 62668a175baeSEmil Constantinescu PetscErrorCode ierr; 62678a175baeSEmil Constantinescu 62688a175baeSEmil Constantinescu PetscFunctionBegin; 62698a175baeSEmil Constantinescu if (wnormtype == NORM_2) { 62708a175baeSEmil Constantinescu ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 62718a175baeSEmil Constantinescu } else if(wnormtype == NORM_INFINITY) { 62728a175baeSEmil Constantinescu ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 62738a175baeSEmil Constantinescu } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 62748a175baeSEmil Constantinescu PetscFunctionReturn(0); 62758a175baeSEmil Constantinescu } 62768a175baeSEmil Constantinescu 62778a175baeSEmil Constantinescu 62788d59e960SJed Brown /*@ 62798d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 62808d59e960SJed Brown 62818d59e960SJed Brown Logically Collective on TS 62828d59e960SJed Brown 62838d59e960SJed Brown Input Arguments: 62848d59e960SJed Brown + ts - time stepping context 62858d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 62868d59e960SJed Brown 62878d59e960SJed Brown Note: 62888d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 62898d59e960SJed Brown 62908d59e960SJed Brown Level: intermediate 62918d59e960SJed Brown 62928d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 62938d59e960SJed Brown @*/ 62948d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 62958d59e960SJed Brown { 62968d59e960SJed Brown PetscFunctionBegin; 62978d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 62988d59e960SJed Brown ts->cfltime_local = cfltime; 62998d59e960SJed Brown ts->cfltime = -1.; 63008d59e960SJed Brown PetscFunctionReturn(0); 63018d59e960SJed Brown } 63028d59e960SJed Brown 63038d59e960SJed Brown /*@ 63048d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 63058d59e960SJed Brown 63068d59e960SJed Brown Collective on TS 63078d59e960SJed Brown 63088d59e960SJed Brown Input Arguments: 63098d59e960SJed Brown . ts - time stepping context 63108d59e960SJed Brown 63118d59e960SJed Brown Output Arguments: 63128d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 63138d59e960SJed Brown 63148d59e960SJed Brown Level: advanced 63158d59e960SJed Brown 63168d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 63178d59e960SJed Brown @*/ 63188d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 63198d59e960SJed Brown { 63208d59e960SJed Brown PetscErrorCode ierr; 63218d59e960SJed Brown 63228d59e960SJed Brown PetscFunctionBegin; 63238d59e960SJed Brown if (ts->cfltime < 0) { 6324b2566f29SBarry Smith ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 63258d59e960SJed Brown } 63268d59e960SJed Brown *cfltime = ts->cfltime; 63278d59e960SJed Brown PetscFunctionReturn(0); 63288d59e960SJed Brown } 63298d59e960SJed Brown 6330d6ebe24aSShri Abhyankar /*@ 6331d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 6332d6ebe24aSShri Abhyankar 6333d6ebe24aSShri Abhyankar Input Parameters: 6334a2b725a8SWilliam Gropp + ts - the TS context. 6335d6ebe24aSShri Abhyankar . xl - lower bound. 6336a2b725a8SWilliam Gropp - xu - upper bound. 6337d6ebe24aSShri Abhyankar 6338d6ebe24aSShri Abhyankar Notes: 6339d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 6340e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 6341d6ebe24aSShri Abhyankar 63422bd2b0e6SSatish Balay Level: advanced 63432bd2b0e6SSatish Balay 6344d6ebe24aSShri Abhyankar @*/ 6345d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 6346d6ebe24aSShri Abhyankar { 6347d6ebe24aSShri Abhyankar PetscErrorCode ierr; 6348d6ebe24aSShri Abhyankar SNES snes; 6349d6ebe24aSShri Abhyankar 6350d6ebe24aSShri Abhyankar PetscFunctionBegin; 6351d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 6352d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 6353d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 6354d6ebe24aSShri Abhyankar } 6355d6ebe24aSShri Abhyankar 6356b3603a34SBarry Smith /*@C 63574f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 6358b3603a34SBarry Smith in a time based line graph 6359b3603a34SBarry Smith 6360b3603a34SBarry Smith Collective on TS 6361b3603a34SBarry Smith 6362b3603a34SBarry Smith Input Parameters: 6363b3603a34SBarry Smith + ts - the TS context 6364b3603a34SBarry Smith . step - current time-step 6365b3603a34SBarry Smith . ptime - current time 63667db568b7SBarry Smith . u - current solution 63677db568b7SBarry Smith - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 6368b3603a34SBarry Smith 6369b3d3934dSBarry Smith Options Database: 63709ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 6371b3d3934dSBarry Smith 6372b3603a34SBarry Smith Level: intermediate 6373b3603a34SBarry Smith 637495452b02SPatrick Sanan Notes: 637595452b02SPatrick Sanan Each process in a parallel run displays its component solutions in a separate window 6376b3603a34SBarry Smith 63777db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 63787db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 63797db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 63807db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 6381b3603a34SBarry Smith @*/ 63827db568b7SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6383b3603a34SBarry Smith { 6384b3603a34SBarry Smith PetscErrorCode ierr; 63857db568b7SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 6386b3603a34SBarry Smith const PetscScalar *yy; 638780666b62SBarry Smith Vec v; 6388b3603a34SBarry Smith 6389b3603a34SBarry Smith PetscFunctionBegin; 639063e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 639158ff32f7SBarry Smith if (!step) { 6392a9f9c1f6SBarry Smith PetscDrawAxis axis; 63936934998bSLisandro Dalcin PetscInt dim; 6394a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 63950ec8ee2bSJoseph Pusztay ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 6396bab0a581SBarry Smith if (!ctx->names) { 6397bab0a581SBarry Smith PetscBool flg; 6398bab0a581SBarry Smith /* user provides names of variables to plot but no names has been set so assume names are integer values */ 6399bab0a581SBarry Smith ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr); 6400bab0a581SBarry Smith if (flg) { 6401bab0a581SBarry Smith PetscInt i,n; 6402bab0a581SBarry Smith char **names; 6403bab0a581SBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 6404bab0a581SBarry Smith ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr); 6405bab0a581SBarry Smith for (i=0; i<n; i++) { 6406bab0a581SBarry Smith ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr); 6407bab0a581SBarry Smith ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr); 6408bab0a581SBarry Smith } 6409bab0a581SBarry Smith names[n] = NULL; 6410bab0a581SBarry Smith ctx->names = names; 6411bab0a581SBarry Smith } 6412bab0a581SBarry Smith } 6413387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 6414387f4636SBarry Smith char **displaynames; 6415387f4636SBarry Smith PetscBool flg; 6416387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6417580bdb30SBarry Smith ierr = PetscCalloc1(dim+1,&displaynames);CHKERRQ(ierr); 6418c5929fdfSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 6419387f4636SBarry Smith if (flg) { 6420a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 6421387f4636SBarry Smith } 6422387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 6423387f4636SBarry Smith } 6424387f4636SBarry Smith if (ctx->displaynames) { 6425387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 6426387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 6427387f4636SBarry Smith } else if (ctx->names) { 64280910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 64290b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6430387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 6431b0bc92c6SBarry Smith } else { 6432b0bc92c6SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6433b0bc92c6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6434387f4636SBarry Smith } 64350b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 643658ff32f7SBarry Smith } 64376934998bSLisandro Dalcin 64386934998bSLisandro Dalcin if (!ctx->transform) v = u; 64396934998bSLisandro Dalcin else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);} 644080666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 64416934998bSLisandro Dalcin if (ctx->displaynames) { 64426934998bSLisandro Dalcin PetscInt i; 64436934998bSLisandro Dalcin for (i=0; i<ctx->ndisplayvariables; i++) 64446934998bSLisandro Dalcin ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]); 64456934998bSLisandro Dalcin ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 64466934998bSLisandro Dalcin } else { 6447e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6448e3efe391SJed Brown PetscInt i,n; 64496934998bSLisandro Dalcin PetscReal *yreal; 645080666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 6451785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6452e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 64530b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6454e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6455e3efe391SJed Brown #else 64560b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6457e3efe391SJed Brown #endif 645880666b62SBarry Smith } 64596934998bSLisandro Dalcin ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 64606934998bSLisandro Dalcin if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);} 64616934998bSLisandro Dalcin 6462b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 64630b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 64646934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 64653923b477SBarry Smith } 6466b3603a34SBarry Smith PetscFunctionReturn(0); 6467b3603a34SBarry Smith } 6468b3603a34SBarry Smith 6469b037adc7SBarry Smith /*@C 647031152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6471b037adc7SBarry Smith 6472b037adc7SBarry Smith Collective on TS 6473b037adc7SBarry Smith 6474b037adc7SBarry Smith Input Parameters: 6475b037adc7SBarry Smith + ts - the TS context 6476b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 6477b037adc7SBarry Smith 6478b037adc7SBarry Smith Level: intermediate 6479b037adc7SBarry Smith 648095452b02SPatrick Sanan Notes: 648195452b02SPatrick Sanan If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 64827db568b7SBarry Smith 6483a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 6484b037adc7SBarry Smith @*/ 648531152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 6486b037adc7SBarry Smith { 6487b037adc7SBarry Smith PetscErrorCode ierr; 6488b037adc7SBarry Smith PetscInt i; 6489b037adc7SBarry Smith 6490b037adc7SBarry Smith PetscFunctionBegin; 6491b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6492b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 64935537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 6494b3d3934dSBarry Smith break; 6495b3d3934dSBarry Smith } 6496b3d3934dSBarry Smith } 6497b3d3934dSBarry Smith PetscFunctionReturn(0); 6498b3d3934dSBarry Smith } 6499b3d3934dSBarry Smith 6500e673d494SBarry Smith /*@C 6501e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6502e673d494SBarry Smith 6503e673d494SBarry Smith Collective on TS 6504e673d494SBarry Smith 6505e673d494SBarry Smith Input Parameters: 6506e673d494SBarry Smith + ts - the TS context 6507e673d494SBarry Smith - names - the names of the components, final string must be NULL 6508e673d494SBarry Smith 6509e673d494SBarry Smith Level: intermediate 6510e673d494SBarry Smith 6511a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 6512e673d494SBarry Smith @*/ 6513e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 6514e673d494SBarry Smith { 6515e673d494SBarry Smith PetscErrorCode ierr; 6516e673d494SBarry Smith 6517e673d494SBarry Smith PetscFunctionBegin; 6518e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 6519e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 6520e673d494SBarry Smith PetscFunctionReturn(0); 6521e673d494SBarry Smith } 6522e673d494SBarry Smith 6523b3d3934dSBarry Smith /*@C 6524b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 6525b3d3934dSBarry Smith 6526b3d3934dSBarry Smith Collective on TS 6527b3d3934dSBarry Smith 6528b3d3934dSBarry Smith Input Parameter: 6529b3d3934dSBarry Smith . ts - the TS context 6530b3d3934dSBarry Smith 6531b3d3934dSBarry Smith Output Parameter: 6532b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 6533b3d3934dSBarry Smith 6534b3d3934dSBarry Smith Level: intermediate 6535b3d3934dSBarry Smith 653695452b02SPatrick Sanan Notes: 653795452b02SPatrick Sanan If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 65387db568b7SBarry Smith 6539b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6540b3d3934dSBarry Smith @*/ 6541b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 6542b3d3934dSBarry Smith { 6543b3d3934dSBarry Smith PetscInt i; 6544b3d3934dSBarry Smith 6545b3d3934dSBarry Smith PetscFunctionBegin; 6546b3d3934dSBarry Smith *names = NULL; 6547b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6548b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 65495537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 6550b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 6551b3d3934dSBarry Smith break; 6552387f4636SBarry Smith } 6553387f4636SBarry Smith } 6554387f4636SBarry Smith PetscFunctionReturn(0); 6555387f4636SBarry Smith } 6556387f4636SBarry Smith 6557a66092f1SBarry Smith /*@C 6558a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 6559a66092f1SBarry Smith 6560a66092f1SBarry Smith Collective on TS 6561a66092f1SBarry Smith 6562a66092f1SBarry Smith Input Parameters: 6563a66092f1SBarry Smith + ctx - the TSMonitorLG context 6564a2b725a8SWilliam Gropp - displaynames - the names of the components, final string must be NULL 6565a66092f1SBarry Smith 6566a66092f1SBarry Smith Level: intermediate 6567a66092f1SBarry Smith 6568a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6569a66092f1SBarry Smith @*/ 6570a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 6571a66092f1SBarry Smith { 6572a66092f1SBarry Smith PetscInt j = 0,k; 6573a66092f1SBarry Smith PetscErrorCode ierr; 6574a66092f1SBarry Smith 6575a66092f1SBarry Smith PetscFunctionBegin; 6576a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 6577a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 6578a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 6579a66092f1SBarry Smith while (displaynames[j]) j++; 6580a66092f1SBarry Smith ctx->ndisplayvariables = j; 6581a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 6582a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 6583a66092f1SBarry Smith j = 0; 6584a66092f1SBarry Smith while (displaynames[j]) { 6585a66092f1SBarry Smith k = 0; 6586a66092f1SBarry Smith while (ctx->names[k]) { 6587a66092f1SBarry Smith PetscBool flg; 6588a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 6589a66092f1SBarry Smith if (flg) { 6590a66092f1SBarry Smith ctx->displayvariables[j] = k; 6591a66092f1SBarry Smith break; 6592a66092f1SBarry Smith } 6593a66092f1SBarry Smith k++; 6594a66092f1SBarry Smith } 6595a66092f1SBarry Smith j++; 6596a66092f1SBarry Smith } 6597a66092f1SBarry Smith PetscFunctionReturn(0); 6598a66092f1SBarry Smith } 6599a66092f1SBarry Smith 6600387f4636SBarry Smith /*@C 6601387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 6602387f4636SBarry Smith 6603387f4636SBarry Smith Collective on TS 6604387f4636SBarry Smith 6605387f4636SBarry Smith Input Parameters: 6606387f4636SBarry Smith + ts - the TS context 6607a2b725a8SWilliam Gropp - displaynames - the names of the components, final string must be NULL 6608387f4636SBarry Smith 660995452b02SPatrick Sanan Notes: 661095452b02SPatrick Sanan If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 66117db568b7SBarry Smith 6612387f4636SBarry Smith Level: intermediate 6613387f4636SBarry Smith 6614387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6615387f4636SBarry Smith @*/ 6616387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 6617387f4636SBarry Smith { 6618a66092f1SBarry Smith PetscInt i; 6619387f4636SBarry Smith PetscErrorCode ierr; 6620387f4636SBarry Smith 6621387f4636SBarry Smith PetscFunctionBegin; 6622387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6623387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 66245537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 6625b3d3934dSBarry Smith break; 6626b037adc7SBarry Smith } 6627b037adc7SBarry Smith } 6628b037adc7SBarry Smith PetscFunctionReturn(0); 6629b037adc7SBarry Smith } 6630b037adc7SBarry Smith 663180666b62SBarry Smith /*@C 663280666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 663380666b62SBarry Smith 663480666b62SBarry Smith Collective on TS 663580666b62SBarry Smith 663680666b62SBarry Smith Input Parameters: 663780666b62SBarry Smith + ts - the TS context 663880666b62SBarry Smith . transform - the transform function 66397684fa3eSBarry Smith . destroy - function to destroy the optional context 664080666b62SBarry Smith - ctx - optional context used by transform function 664180666b62SBarry Smith 664295452b02SPatrick Sanan Notes: 664395452b02SPatrick Sanan If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 66447db568b7SBarry Smith 664580666b62SBarry Smith Level: intermediate 664680666b62SBarry Smith 6647a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 664880666b62SBarry Smith @*/ 66497684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 665080666b62SBarry Smith { 665180666b62SBarry Smith PetscInt i; 6652a66092f1SBarry Smith PetscErrorCode ierr; 665380666b62SBarry Smith 665480666b62SBarry Smith PetscFunctionBegin; 665580666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 665680666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 66575537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 665880666b62SBarry Smith } 665980666b62SBarry Smith } 666080666b62SBarry Smith PetscFunctionReturn(0); 666180666b62SBarry Smith } 666280666b62SBarry Smith 6663e673d494SBarry Smith /*@C 6664e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 6665e673d494SBarry Smith 6666e673d494SBarry Smith Collective on TSLGCtx 6667e673d494SBarry Smith 6668e673d494SBarry Smith Input Parameters: 6669e673d494SBarry Smith + ts - the TS context 6670e673d494SBarry Smith . transform - the transform function 66717684fa3eSBarry Smith . destroy - function to destroy the optional context 6672e673d494SBarry Smith - ctx - optional context used by transform function 6673e673d494SBarry Smith 6674e673d494SBarry Smith Level: intermediate 6675e673d494SBarry Smith 6676a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 6677e673d494SBarry Smith @*/ 66787684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 6679e673d494SBarry Smith { 6680e673d494SBarry Smith PetscFunctionBegin; 6681e673d494SBarry Smith ctx->transform = transform; 66827684fa3eSBarry Smith ctx->transformdestroy = destroy; 6683e673d494SBarry Smith ctx->transformctx = tctx; 6684e673d494SBarry Smith PetscFunctionReturn(0); 6685e673d494SBarry Smith } 6686e673d494SBarry Smith 6687ef20d060SBarry Smith /*@C 66888b668821SLisandro Dalcin TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error 6689ef20d060SBarry Smith in a time based line graph 6690ef20d060SBarry Smith 6691ef20d060SBarry Smith Collective on TS 6692ef20d060SBarry Smith 6693ef20d060SBarry Smith Input Parameters: 6694ef20d060SBarry Smith + ts - the TS context 6695ef20d060SBarry Smith . step - current time-step 6696ef20d060SBarry Smith . ptime - current time 66977db568b7SBarry Smith . u - current solution 66987db568b7SBarry Smith - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 6699ef20d060SBarry Smith 6700ef20d060SBarry Smith Level: intermediate 6701ef20d060SBarry Smith 670295452b02SPatrick Sanan Notes: 670395452b02SPatrick Sanan Each process in a parallel run displays its component errors in a separate window 6704abd5a294SJed Brown 6705abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 6706abd5a294SJed Brown 6707abd5a294SJed Brown Options Database Keys: 67084f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 6709ef20d060SBarry Smith 6710abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 6711ef20d060SBarry Smith @*/ 67120910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 6713ef20d060SBarry Smith { 6714ef20d060SBarry Smith PetscErrorCode ierr; 67150b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 6716ef20d060SBarry Smith const PetscScalar *yy; 6717ef20d060SBarry Smith Vec y; 6718ef20d060SBarry Smith 6719ef20d060SBarry Smith PetscFunctionBegin; 6720a9f9c1f6SBarry Smith if (!step) { 6721a9f9c1f6SBarry Smith PetscDrawAxis axis; 67226934998bSLisandro Dalcin PetscInt dim; 6723a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 67248b668821SLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr); 67250910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6726a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6727a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6728a9f9c1f6SBarry Smith } 67290910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 6730ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 67310910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 6732ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 6733e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6734e3efe391SJed Brown { 6735e3efe391SJed Brown PetscReal *yreal; 6736e3efe391SJed Brown PetscInt i,n; 6737e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 6738785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6739e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 67400b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6741e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6742e3efe391SJed Brown } 6743e3efe391SJed Brown #else 67440b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6745e3efe391SJed Brown #endif 6746ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 6747ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 6748b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 67490b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 67506934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 67513923b477SBarry Smith } 6752ef20d060SBarry Smith PetscFunctionReturn(0); 6753ef20d060SBarry Smith } 6754ef20d060SBarry Smith 67555e3b7effSJoseph Pusztay /*@C 67565e3b7effSJoseph Pusztay TSMonitorSPSwarmSolution - Graphically displays phase plots of DMSwarm particles on a scatter plot 67575e3b7effSJoseph Pusztay 67585e3b7effSJoseph Pusztay Input Parameters: 67595e3b7effSJoseph Pusztay + ts - the TS context 67605e3b7effSJoseph Pusztay . step - current time-step 67615e3b7effSJoseph Pusztay . ptime - current time 67625e3b7effSJoseph Pusztay . u - current solution 67635e3b7effSJoseph Pusztay - dctx - the TSMonitorSPCtx object that contains all the options for the monitoring, this is created with TSMonitorSPCtxCreate() 67645e3b7effSJoseph Pusztay 67655e3b7effSJoseph Pusztay Options Database: 6766918b1d10SJoseph Pusztay . -ts_monitor_sp_swarm 67675e3b7effSJoseph Pusztay 67685e3b7effSJoseph Pusztay Level: intermediate 67695e3b7effSJoseph Pusztay 67705e3b7effSJoseph Pusztay @*/ 67710ec8ee2bSJoseph Pusztay PetscErrorCode TSMonitorSPSwarmSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 67721b575b74SJoseph Pusztay { 67731b575b74SJoseph Pusztay PetscErrorCode ierr; 67741b575b74SJoseph Pusztay TSMonitorSPCtx ctx = (TSMonitorSPCtx)dctx; 67751b575b74SJoseph Pusztay const PetscScalar *yy; 6776b1670a61SJoseph Pusztay PetscReal *y,*x; 67771b575b74SJoseph Pusztay PetscInt Np, p, dim=2; 67781b575b74SJoseph Pusztay DM dm; 67791b575b74SJoseph Pusztay 67801b575b74SJoseph Pusztay PetscFunctionBegin; 67811b575b74SJoseph Pusztay 67821b575b74SJoseph Pusztay if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 67831b575b74SJoseph Pusztay if (!step) { 67841b575b74SJoseph Pusztay PetscDrawAxis axis; 67851b575b74SJoseph Pusztay ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr); 67861b575b74SJoseph Pusztay ierr = PetscDrawAxisSetLabels(axis,"Particles","X","Y");CHKERRQ(ierr); 6787895f37d5SJoseph Pusztay ierr = PetscDrawAxisSetLimits(axis, -5, 5, -5, 5);CHKERRQ(ierr); 6788895f37d5SJoseph Pusztay ierr = PetscDrawAxisSetHoldLimits(axis, PETSC_TRUE);CHKERRQ(ierr); 67891b575b74SJoseph Pusztay ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 67901b575b74SJoseph Pusztay ierr = DMGetDimension(dm, &dim); 67911b575b74SJoseph Pusztay if(dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");CHKERRQ(ierr); 67921b575b74SJoseph Pusztay ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr); 67931b575b74SJoseph Pusztay Np /= 2*dim; 67941b575b74SJoseph Pusztay ierr = PetscDrawSPSetDimension(ctx->sp, Np);CHKERRQ(ierr); 67951b575b74SJoseph Pusztay ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr); 67961b575b74SJoseph Pusztay } 67971b575b74SJoseph Pusztay 67981b575b74SJoseph Pusztay ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr); 67991b575b74SJoseph Pusztay Np /= 2*dim; 68001b575b74SJoseph Pusztay ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr); 6801895f37d5SJoseph Pusztay ierr = PetscMalloc2(Np, &x, Np, &y);CHKERRQ(ierr); 68021b575b74SJoseph Pusztay /* get points from solution vector */ 68031b575b74SJoseph Pusztay for (p=0; p<Np; ++p){ 6804b1670a61SJoseph Pusztay x[p] = PetscRealPart(yy[2*dim*p]); 6805b1670a61SJoseph Pusztay y[p] = PetscRealPart(yy[2*dim*p+1]); 6806895f37d5SJoseph Pusztay } 68071b575b74SJoseph Pusztay ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr); 68081b575b74SJoseph Pusztay 68091b575b74SJoseph Pusztay if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 68101b575b74SJoseph Pusztay ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr); 68111b575b74SJoseph Pusztay ierr = PetscDrawSPDraw(ctx->sp,PETSC_FALSE);CHKERRQ(ierr); 68121b575b74SJoseph Pusztay ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr); 68131b575b74SJoseph Pusztay } 68141b575b74SJoseph Pusztay 6815918b1d10SJoseph Pusztay ierr = PetscFree2(x, y);CHKERRQ(ierr); 6816918b1d10SJoseph Pusztay 68171b575b74SJoseph Pusztay PetscFunctionReturn(0); 68181b575b74SJoseph Pusztay } 68191b575b74SJoseph Pusztay 68201b575b74SJoseph Pusztay 68211b575b74SJoseph Pusztay 68227cf37e64SBarry Smith /*@C 68237cf37e64SBarry Smith TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep 68247cf37e64SBarry Smith 68257cf37e64SBarry Smith Collective on TS 68267cf37e64SBarry Smith 68277cf37e64SBarry Smith Input Parameters: 68287cf37e64SBarry Smith + ts - the TS context 68297cf37e64SBarry Smith . step - current time-step 68307cf37e64SBarry Smith . ptime - current time 68317cf37e64SBarry Smith . u - current solution 68327cf37e64SBarry Smith - dctx - unused context 68337cf37e64SBarry Smith 68347cf37e64SBarry Smith Level: intermediate 68357cf37e64SBarry Smith 68367cf37e64SBarry Smith The user must provide the solution using TSSetSolutionFunction() to use this monitor. 68377cf37e64SBarry Smith 68387cf37e64SBarry Smith Options Database Keys: 68397cf37e64SBarry Smith . -ts_monitor_error - create a graphical monitor of error history 68407cf37e64SBarry Smith 68417cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 68427cf37e64SBarry Smith @*/ 6843edbaebb3SBarry Smith PetscErrorCode TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf) 68447cf37e64SBarry Smith { 68457cf37e64SBarry Smith PetscErrorCode ierr; 68467cf37e64SBarry Smith Vec y; 68477cf37e64SBarry Smith PetscReal nrm; 6848edbaebb3SBarry Smith PetscBool flg; 68497cf37e64SBarry Smith 68507cf37e64SBarry Smith PetscFunctionBegin; 68517cf37e64SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 68527cf37e64SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 68537cf37e64SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 6854edbaebb3SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr); 6855edbaebb3SBarry Smith if (flg) { 68567cf37e64SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 6857edbaebb3SBarry Smith ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr); 6858edbaebb3SBarry Smith } 6859edbaebb3SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr); 6860edbaebb3SBarry Smith if (flg) { 6861edbaebb3SBarry Smith ierr = VecView(y,vf->viewer);CHKERRQ(ierr); 6862edbaebb3SBarry Smith } 6863edbaebb3SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 68647cf37e64SBarry Smith PetscFunctionReturn(0); 68657cf37e64SBarry Smith } 68667cf37e64SBarry Smith 6867201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6868201da799SBarry Smith { 6869201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6870201da799SBarry Smith PetscReal x = ptime,y; 6871201da799SBarry Smith PetscErrorCode ierr; 6872201da799SBarry Smith PetscInt its; 6873201da799SBarry Smith 6874201da799SBarry Smith PetscFunctionBegin; 687563e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 6876201da799SBarry Smith if (!n) { 6877201da799SBarry Smith PetscDrawAxis axis; 6878201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6879201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 6880201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6881201da799SBarry Smith ctx->snes_its = 0; 6882201da799SBarry Smith } 6883201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 6884201da799SBarry Smith y = its - ctx->snes_its; 6885201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 68863fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6887201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 68886934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 6889201da799SBarry Smith } 6890201da799SBarry Smith ctx->snes_its = its; 6891201da799SBarry Smith PetscFunctionReturn(0); 6892201da799SBarry Smith } 6893201da799SBarry Smith 6894201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6895201da799SBarry Smith { 6896201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6897201da799SBarry Smith PetscReal x = ptime,y; 6898201da799SBarry Smith PetscErrorCode ierr; 6899201da799SBarry Smith PetscInt its; 6900201da799SBarry Smith 6901201da799SBarry Smith PetscFunctionBegin; 690263e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 6903201da799SBarry Smith if (!n) { 6904201da799SBarry Smith PetscDrawAxis axis; 6905201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6906201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 6907201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6908201da799SBarry Smith ctx->ksp_its = 0; 6909201da799SBarry Smith } 6910201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 6911201da799SBarry Smith y = its - ctx->ksp_its; 6912201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 691399fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6914201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 69156934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 6916201da799SBarry Smith } 6917201da799SBarry Smith ctx->ksp_its = its; 6918201da799SBarry Smith PetscFunctionReturn(0); 6919201da799SBarry Smith } 6920f9c1d6abSBarry Smith 6921f9c1d6abSBarry Smith /*@ 6922f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 6923f9c1d6abSBarry Smith 6924d083f849SBarry Smith Collective on TS 6925f9c1d6abSBarry Smith 6926f9c1d6abSBarry Smith Input Parameters: 6927f9c1d6abSBarry Smith + ts - the TS context 6928f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 6929f9c1d6abSBarry Smith 6930f9c1d6abSBarry Smith Output Parameters: 6931f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 6932f9c1d6abSBarry Smith 6933f9c1d6abSBarry Smith Level: developer 6934f9c1d6abSBarry Smith 6935f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 6936f9c1d6abSBarry Smith @*/ 6937f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 6938f9c1d6abSBarry Smith { 6939f9c1d6abSBarry Smith PetscErrorCode ierr; 6940f9c1d6abSBarry Smith 6941f9c1d6abSBarry Smith PetscFunctionBegin; 6942f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6943ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 6944f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 6945f9c1d6abSBarry Smith PetscFunctionReturn(0); 6946f9c1d6abSBarry Smith } 694724655328SShri 6948b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 6949b3d3934dSBarry Smith /*@C 6950b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 6951b3d3934dSBarry Smith 6952b3d3934dSBarry Smith Collective on TS 6953b3d3934dSBarry Smith 6954b3d3934dSBarry Smith Input Parameters: 6955b3d3934dSBarry Smith . ts - the ODE solver object 6956b3d3934dSBarry Smith 6957b3d3934dSBarry Smith Output Parameter: 6958b3d3934dSBarry Smith . ctx - the context 6959b3d3934dSBarry Smith 6960b3d3934dSBarry Smith Level: intermediate 6961b3d3934dSBarry Smith 6962b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 6963b3d3934dSBarry Smith 6964b3d3934dSBarry Smith @*/ 6965b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 6966b3d3934dSBarry Smith { 6967b3d3934dSBarry Smith PetscErrorCode ierr; 6968b3d3934dSBarry Smith 6969b3d3934dSBarry Smith PetscFunctionBegin; 6970a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 6971b3d3934dSBarry Smith PetscFunctionReturn(0); 6972b3d3934dSBarry Smith } 6973b3d3934dSBarry Smith 6974b3d3934dSBarry Smith /*@C 6975b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 6976b3d3934dSBarry Smith 6977b3d3934dSBarry Smith Collective on TS 6978b3d3934dSBarry Smith 6979b3d3934dSBarry Smith Input Parameters: 6980b3d3934dSBarry Smith + ts - the TS context 6981b3d3934dSBarry Smith . step - current time-step 6982b3d3934dSBarry Smith . ptime - current time 69837db568b7SBarry Smith . u - current solution 69847db568b7SBarry Smith - dctx - the envelope context 6985b3d3934dSBarry Smith 6986b3d3934dSBarry Smith Options Database: 6987b3d3934dSBarry Smith . -ts_monitor_envelope 6988b3d3934dSBarry Smith 6989b3d3934dSBarry Smith Level: intermediate 6990b3d3934dSBarry Smith 699195452b02SPatrick Sanan Notes: 699295452b02SPatrick Sanan after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 6993b3d3934dSBarry Smith 69947db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 6995b3d3934dSBarry Smith @*/ 69967db568b7SBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6997b3d3934dSBarry Smith { 6998b3d3934dSBarry Smith PetscErrorCode ierr; 69997db568b7SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 7000b3d3934dSBarry Smith 7001b3d3934dSBarry Smith PetscFunctionBegin; 7002b3d3934dSBarry Smith if (!ctx->max) { 7003b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 7004b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 7005b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 7006b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 7007b3d3934dSBarry Smith } else { 7008b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 7009b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 7010b3d3934dSBarry Smith } 7011b3d3934dSBarry Smith PetscFunctionReturn(0); 7012b3d3934dSBarry Smith } 7013b3d3934dSBarry Smith 7014b3d3934dSBarry Smith /*@C 7015b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 7016b3d3934dSBarry Smith 7017b3d3934dSBarry Smith Collective on TS 7018b3d3934dSBarry Smith 7019b3d3934dSBarry Smith Input Parameter: 7020b3d3934dSBarry Smith . ts - the TS context 7021b3d3934dSBarry Smith 7022b3d3934dSBarry Smith Output Parameter: 7023b3d3934dSBarry Smith + max - the maximum values 7024b3d3934dSBarry Smith - min - the minimum values 7025b3d3934dSBarry Smith 702695452b02SPatrick Sanan Notes: 702795452b02SPatrick Sanan If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 70287db568b7SBarry Smith 7029b3d3934dSBarry Smith Level: intermediate 7030b3d3934dSBarry Smith 7031b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 7032b3d3934dSBarry Smith @*/ 7033b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 7034b3d3934dSBarry Smith { 7035b3d3934dSBarry Smith PetscInt i; 7036b3d3934dSBarry Smith 7037b3d3934dSBarry Smith PetscFunctionBegin; 7038b3d3934dSBarry Smith if (max) *max = NULL; 7039b3d3934dSBarry Smith if (min) *min = NULL; 7040b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7041b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 70425537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 7043b3d3934dSBarry Smith if (max) *max = ctx->max; 7044b3d3934dSBarry Smith if (min) *min = ctx->min; 7045b3d3934dSBarry Smith break; 7046b3d3934dSBarry Smith } 7047b3d3934dSBarry Smith } 7048b3d3934dSBarry Smith PetscFunctionReturn(0); 7049b3d3934dSBarry Smith } 7050b3d3934dSBarry Smith 7051b3d3934dSBarry Smith /*@C 7052b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 7053b3d3934dSBarry Smith 7054b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 7055b3d3934dSBarry Smith 7056b3d3934dSBarry Smith Input Parameter: 7057b3d3934dSBarry Smith . ctx - the monitor context 7058b3d3934dSBarry Smith 7059b3d3934dSBarry Smith Level: intermediate 7060b3d3934dSBarry Smith 70617db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 7062b3d3934dSBarry Smith @*/ 7063b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 7064b3d3934dSBarry Smith { 7065b3d3934dSBarry Smith PetscErrorCode ierr; 7066b3d3934dSBarry Smith 7067b3d3934dSBarry Smith PetscFunctionBegin; 7068b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 7069b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 7070b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 7071b3d3934dSBarry Smith PetscFunctionReturn(0); 7072b3d3934dSBarry Smith } 7073f2dee214SBarry Smith 707424655328SShri /*@ 7075dcb233daSLisandro Dalcin TSRestartStep - Flags the solver to restart the next step 7076dcb233daSLisandro Dalcin 7077dcb233daSLisandro Dalcin Collective on TS 7078dcb233daSLisandro Dalcin 7079dcb233daSLisandro Dalcin Input Parameter: 7080dcb233daSLisandro Dalcin . ts - the TS context obtained from TSCreate() 7081dcb233daSLisandro Dalcin 7082dcb233daSLisandro Dalcin Level: advanced 7083dcb233daSLisandro Dalcin 7084dcb233daSLisandro Dalcin Notes: 7085dcb233daSLisandro Dalcin Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of 7086dcb233daSLisandro Dalcin discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution 7087dcb233daSLisandro Dalcin vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For 7088dcb233daSLisandro Dalcin the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce 7089dcb233daSLisandro Dalcin discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with 7090dcb233daSLisandro Dalcin discontinuous source terms). 7091dcb233daSLisandro Dalcin 7092dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep() 7093dcb233daSLisandro Dalcin @*/ 7094dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts) 7095dcb233daSLisandro Dalcin { 7096dcb233daSLisandro Dalcin PetscFunctionBegin; 7097dcb233daSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7098dcb233daSLisandro Dalcin ts->steprestart = PETSC_TRUE; 7099dcb233daSLisandro Dalcin PetscFunctionReturn(0); 7100dcb233daSLisandro Dalcin } 7101dcb233daSLisandro Dalcin 7102dcb233daSLisandro Dalcin /*@ 710324655328SShri TSRollBack - Rolls back one time step 710424655328SShri 710524655328SShri Collective on TS 710624655328SShri 710724655328SShri Input Parameter: 710824655328SShri . ts - the TS context obtained from TSCreate() 710924655328SShri 711024655328SShri Level: advanced 711124655328SShri 711224655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 711324655328SShri @*/ 711424655328SShri PetscErrorCode TSRollBack(TS ts) 711524655328SShri { 711624655328SShri PetscErrorCode ierr; 711724655328SShri 711824655328SShri PetscFunctionBegin; 711924655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7120b3de5cdeSLisandro Dalcin if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called"); 712124655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 712224655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 712324655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 712424655328SShri ts->ptime = ts->ptime_prev; 7125be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 71262808aa04SLisandro Dalcin ts->steps--; 7127b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 712824655328SShri PetscFunctionReturn(0); 712924655328SShri } 7130aeb4809dSShri Abhyankar 7131ff22ae23SHong Zhang /*@ 7132ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 7133ff22ae23SHong Zhang 7134ff22ae23SHong Zhang Input Parameter: 7135ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 7136ff22ae23SHong Zhang 71370429704eSStefano Zampini Output Parameters: 71380429704eSStefano Zampini + ns - the number of stages 71390429704eSStefano Zampini - Y - the current stage vectors 71400429704eSStefano Zampini 7141ff22ae23SHong Zhang Level: advanced 7142ff22ae23SHong Zhang 71430429704eSStefano Zampini Notes: Both ns and Y can be NULL. 71440429704eSStefano Zampini 7145ff22ae23SHong Zhang .seealso: TSCreate() 7146ff22ae23SHong Zhang @*/ 7147ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns,Vec **Y) 7148ff22ae23SHong Zhang { 7149ff22ae23SHong Zhang PetscErrorCode ierr; 7150ff22ae23SHong Zhang 7151ff22ae23SHong Zhang PetscFunctionBegin; 7152ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 71530429704eSStefano Zampini if (ns) PetscValidPointer(ns,2); 71540429704eSStefano Zampini if (Y) PetscValidPointer(Y,3); 71550429704eSStefano Zampini if (!ts->ops->getstages) { 71560429704eSStefano Zampini if (ns) *ns = 0; 71570429704eSStefano Zampini if (Y) *Y = NULL; 71580429704eSStefano Zampini } else { 7159ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 7160ff22ae23SHong Zhang } 7161ff22ae23SHong Zhang PetscFunctionReturn(0); 7162ff22ae23SHong Zhang } 7163ff22ae23SHong Zhang 7164847ff0e1SMatthew G. Knepley /*@C 7165847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 7166847ff0e1SMatthew G. Knepley 7167847ff0e1SMatthew G. Knepley Collective on SNES 7168847ff0e1SMatthew G. Knepley 7169847ff0e1SMatthew G. Knepley Input Parameters: 7170847ff0e1SMatthew G. Knepley + ts - the TS context 7171847ff0e1SMatthew G. Knepley . t - current timestep 7172847ff0e1SMatthew G. Knepley . U - state vector 7173847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 7174847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 7175847ff0e1SMatthew G. Knepley - ctx - an optional user context 7176847ff0e1SMatthew G. Knepley 7177847ff0e1SMatthew G. Knepley Output Parameters: 7178847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 7179847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 7180847ff0e1SMatthew G. Knepley 7181847ff0e1SMatthew G. Knepley Level: intermediate 7182847ff0e1SMatthew G. Knepley 7183847ff0e1SMatthew G. Knepley Notes: 7184847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 7185847ff0e1SMatthew G. Knepley 7186847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 7187847ff0e1SMatthew G. Knepley 7188847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 7189847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 7190847ff0e1SMatthew G. Knepley 7191847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 7192847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 7193847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 7194847ff0e1SMatthew G. Knepley 7195847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 7196847ff0e1SMatthew G. Knepley @*/ 7197847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 7198847ff0e1SMatthew G. Knepley { 7199847ff0e1SMatthew G. Knepley SNES snes; 7200847ff0e1SMatthew G. Knepley MatFDColoring color; 7201847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 7202847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 7203847ff0e1SMatthew G. Knepley 7204847ff0e1SMatthew G. Knepley PetscFunctionBegin; 7205c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 7206847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 7207847ff0e1SMatthew G. Knepley if (!color) { 7208847ff0e1SMatthew G. Knepley DM dm; 7209847ff0e1SMatthew G. Knepley ISColoring iscoloring; 7210847ff0e1SMatthew G. Knepley 7211847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 7212847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 7213847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 7214847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 7215847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7216847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7217847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7218847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7219847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7220847ff0e1SMatthew G. Knepley } else { 7221847ff0e1SMatthew G. Knepley MatColoring mc; 7222847ff0e1SMatthew G. Knepley 7223847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 7224847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 7225847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 7226847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 7227847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 7228847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 7229847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7230847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7231847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7232847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7233847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7234847ff0e1SMatthew G. Knepley } 7235847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 7236847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 7237847ff0e1SMatthew G. Knepley } 7238847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 7239847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 7240847ff0e1SMatthew G. Knepley if (J != B) { 7241847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7242847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7243847ff0e1SMatthew G. Knepley } 7244847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 7245847ff0e1SMatthew G. Knepley } 724693b34091SDebojyoti Ghosh 7247cb9d8021SPierre Barbier de Reuille /*@ 72486bc98fa9SBarry Smith TSSetFunctionDomainError - Set a function that tests if the current state vector is valid 7249cb9d8021SPierre Barbier de Reuille 7250cb9d8021SPierre Barbier de Reuille Input Parameters: 72516bc98fa9SBarry Smith + ts - the TS context 72526bc98fa9SBarry Smith - func - function called within TSFunctionDomainError 72536bc98fa9SBarry Smith 72546bc98fa9SBarry Smith Calling sequence of func: 72556bc98fa9SBarry Smith $ PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject) 72566bc98fa9SBarry Smith 72576bc98fa9SBarry Smith + ts - the TS context 72586bc98fa9SBarry Smith . time - the current time (of the stage) 72596bc98fa9SBarry Smith . state - the state to check if it is valid 72606bc98fa9SBarry Smith - reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable 7261cb9d8021SPierre Barbier de Reuille 7262cb9d8021SPierre Barbier de Reuille Level: intermediate 7263cb9d8021SPierre Barbier de Reuille 72646bc98fa9SBarry Smith Notes: 72656bc98fa9SBarry Smith If an implicit ODE solver is being used then, in addition to providing this routine, the 72666bc98fa9SBarry Smith user's code should call SNESSetFunctionDomainError() when domain errors occur during 72676bc98fa9SBarry Smith function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction(). 72686bc98fa9SBarry Smith Use TSGetSNES() to obtain the SNES object 72696bc98fa9SBarry Smith 72706bc98fa9SBarry Smith Developer Notes: 72716bc98fa9SBarry Smith The naming of this function is inconsistent with the SNESSetFunctionDomainError() 72726bc98fa9SBarry Smith since one takes a function pointer and the other does not. 72736bc98fa9SBarry Smith 72746bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES() 7275cb9d8021SPierre Barbier de Reuille @*/ 7276cb9d8021SPierre Barbier de Reuille 7277d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 7278cb9d8021SPierre Barbier de Reuille { 7279cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7280cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7281cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 7282cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7283cb9d8021SPierre Barbier de Reuille } 7284cb9d8021SPierre Barbier de Reuille 7285cb9d8021SPierre Barbier de Reuille /*@ 72866bc98fa9SBarry Smith TSFunctionDomainError - Checks if the current state is valid 7287cb9d8021SPierre Barbier de Reuille 7288cb9d8021SPierre Barbier de Reuille Input Parameters: 72896bc98fa9SBarry Smith + ts - the TS context 72906bc98fa9SBarry Smith . stagetime - time of the simulation 72916bc98fa9SBarry Smith - Y - state vector to check. 7292cb9d8021SPierre Barbier de Reuille 7293cb9d8021SPierre Barbier de Reuille Output Parameter: 72946bc98fa9SBarry Smith . accept - Set to PETSC_FALSE if the current state vector is valid. 7295cb9d8021SPierre Barbier de Reuille 7296cb9d8021SPierre Barbier de Reuille Note: 72976bc98fa9SBarry Smith This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError()) 72986bc98fa9SBarry Smith to check if the current state is valid. 729996a0c994SBarry Smith 73006bc98fa9SBarry Smith Level: developer 73016bc98fa9SBarry Smith 73026bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError() 7303cb9d8021SPierre Barbier de Reuille @*/ 7304d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 7305cb9d8021SPierre Barbier de Reuille { 7306cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7307cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7308cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 7309cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 7310d183316bSPierre Barbier de Reuille PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 7311cb9d8021SPierre Barbier de Reuille } 7312cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7313cb9d8021SPierre Barbier de Reuille } 73141ceb14c0SBarry Smith 731593b34091SDebojyoti Ghosh /*@C 7316e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 731793b34091SDebojyoti Ghosh 7318d083f849SBarry Smith Collective 731993b34091SDebojyoti Ghosh 732093b34091SDebojyoti Ghosh Input Parameter: 732193b34091SDebojyoti Ghosh . tsin - The input TS 732293b34091SDebojyoti Ghosh 732393b34091SDebojyoti Ghosh Output Parameter: 7324e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 732593b34091SDebojyoti Ghosh 73265eca1a21SEmil Constantinescu Notes: 73275eca1a21SEmil 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. 73285eca1a21SEmil Constantinescu 7329928bb9adSStefano Zampini 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); TSSetSNES(ts,snes_dup); 73305eca1a21SEmil Constantinescu 73315eca1a21SEmil Constantinescu Level: developer 733293b34091SDebojyoti Ghosh 7333e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 733493b34091SDebojyoti Ghosh @*/ 7335baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 733693b34091SDebojyoti Ghosh { 733793b34091SDebojyoti Ghosh TS t; 733893b34091SDebojyoti Ghosh PetscErrorCode ierr; 7339dc846ba4SSatish Balay SNES snes_start; 7340dc846ba4SSatish Balay DM dm; 7341dc846ba4SSatish Balay TSType type; 734293b34091SDebojyoti Ghosh 734393b34091SDebojyoti Ghosh PetscFunctionBegin; 734493b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 734593b34091SDebojyoti Ghosh *tsout = NULL; 734693b34091SDebojyoti Ghosh 73477a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 734893b34091SDebojyoti Ghosh 734993b34091SDebojyoti Ghosh /* General TS description */ 735093b34091SDebojyoti Ghosh t->numbermonitors = 0; 735193b34091SDebojyoti Ghosh t->setupcalled = 0; 735293b34091SDebojyoti Ghosh t->ksp_its = 0; 735393b34091SDebojyoti Ghosh t->snes_its = 0; 735493b34091SDebojyoti Ghosh t->nwork = 0; 735593b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 735693b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 735793b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 735893b34091SDebojyoti Ghosh 735934561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr); 736034561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr); 7361d15a3a53SEmil Constantinescu 736293b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr); 736393b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm);CHKERRQ(ierr); 736493b34091SDebojyoti Ghosh 736593b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 736651699248SLisandro Dalcin ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr); 736793b34091SDebojyoti Ghosh 7368e7069c78SShri t->trajectory = tsin->trajectory; 7369e7069c78SShri ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr); 7370e7069c78SShri 7371e7069c78SShri t->event = tsin->event; 73726b10a48eSSatish Balay if (t->event) t->event->refct++; 7373e7069c78SShri 737493b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 737593b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 7376e7069c78SShri t->ptime_prev = tsin->ptime_prev; 737793b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 737893b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 737993b34091SDebojyoti Ghosh t->steps = tsin->steps; 738093b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 738193b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 738293b34091SDebojyoti Ghosh t->atol = tsin->atol; 738393b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 738493b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 738593b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 738693b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 738793b34091SDebojyoti Ghosh 738893b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type);CHKERRQ(ierr); 738993b34091SDebojyoti Ghosh ierr = TSSetType(t,type);CHKERRQ(ierr); 739093b34091SDebojyoti Ghosh 739193b34091SDebojyoti Ghosh t->vec_sol = NULL; 739293b34091SDebojyoti Ghosh 739393b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 739493b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 739593b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 739693b34091SDebojyoti Ghosh 739793b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 739893b34091SDebojyoti Ghosh 73990d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 74000d4fed19SBarry Smith PetscInt i; 74010d4fed19SBarry Smith ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr); 74020d4fed19SBarry Smith for (i=0; i<10; i++) { 74030d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 74040d4fed19SBarry Smith } 74050d4fed19SBarry Smith } 740693b34091SDebojyoti Ghosh *tsout = t; 740793b34091SDebojyoti Ghosh PetscFunctionReturn(0); 740893b34091SDebojyoti Ghosh } 7409f3b1f45cSBarry Smith 7410f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y) 7411f3b1f45cSBarry Smith { 7412f3b1f45cSBarry Smith PetscErrorCode ierr; 7413f3b1f45cSBarry Smith TS ts = (TS) ctx; 7414f3b1f45cSBarry Smith 7415f3b1f45cSBarry Smith PetscFunctionBegin; 7416f3b1f45cSBarry Smith ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr); 7417f3b1f45cSBarry Smith PetscFunctionReturn(0); 7418f3b1f45cSBarry Smith } 7419f3b1f45cSBarry Smith 7420f3b1f45cSBarry Smith /*@ 7421f3b1f45cSBarry Smith TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function. 7422f3b1f45cSBarry Smith 7423d083f849SBarry Smith Logically Collective on TS 7424f3b1f45cSBarry Smith 7425f3b1f45cSBarry Smith Input Parameters: 7426f3b1f45cSBarry Smith TS - the time stepping routine 7427f3b1f45cSBarry Smith 7428f3b1f45cSBarry Smith Output Parameter: 7429f3b1f45cSBarry Smith . flg - PETSC_TRUE if the multiply is likely correct 7430f3b1f45cSBarry Smith 7431f3b1f45cSBarry Smith Options Database: 7432f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator 7433f3b1f45cSBarry Smith 7434f3b1f45cSBarry Smith Level: advanced 7435f3b1f45cSBarry Smith 743695452b02SPatrick Sanan Notes: 743795452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 7438f3b1f45cSBarry Smith 7439f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose() 7440f3b1f45cSBarry Smith @*/ 7441f3b1f45cSBarry Smith PetscErrorCode TSRHSJacobianTest(TS ts,PetscBool *flg) 7442f3b1f45cSBarry Smith { 7443f3b1f45cSBarry Smith Mat J,B; 7444f3b1f45cSBarry Smith PetscErrorCode ierr; 7445f3b1f45cSBarry Smith TSRHSJacobian func; 7446f3b1f45cSBarry Smith void* ctx; 7447f3b1f45cSBarry Smith 7448f3b1f45cSBarry Smith PetscFunctionBegin; 7449f3b1f45cSBarry Smith ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr); 7450f3b1f45cSBarry Smith ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr); 7451f3b1f45cSBarry Smith ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr); 7452f3b1f45cSBarry Smith PetscFunctionReturn(0); 7453f3b1f45cSBarry Smith } 7454f3b1f45cSBarry Smith 7455f3b1f45cSBarry Smith /*@C 7456f3b1f45cSBarry Smith TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function. 7457f3b1f45cSBarry Smith 7458d083f849SBarry Smith Logically Collective on TS 7459f3b1f45cSBarry Smith 7460f3b1f45cSBarry Smith Input Parameters: 7461f3b1f45cSBarry Smith TS - the time stepping routine 7462f3b1f45cSBarry Smith 7463f3b1f45cSBarry Smith Output Parameter: 7464f3b1f45cSBarry Smith . flg - PETSC_TRUE if the multiply is likely correct 7465f3b1f45cSBarry Smith 7466f3b1f45cSBarry Smith Options Database: 7467f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator 7468f3b1f45cSBarry Smith 746995452b02SPatrick Sanan Notes: 747095452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 7471f3b1f45cSBarry Smith 7472f3b1f45cSBarry Smith Level: advanced 7473f3b1f45cSBarry Smith 7474f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest() 7475f3b1f45cSBarry Smith @*/ 7476f3b1f45cSBarry Smith PetscErrorCode TSRHSJacobianTestTranspose(TS ts,PetscBool *flg) 7477f3b1f45cSBarry Smith { 7478f3b1f45cSBarry Smith Mat J,B; 7479f3b1f45cSBarry Smith PetscErrorCode ierr; 7480f3b1f45cSBarry Smith void *ctx; 7481f3b1f45cSBarry Smith TSRHSJacobian func; 7482f3b1f45cSBarry Smith 7483f3b1f45cSBarry Smith PetscFunctionBegin; 7484f3b1f45cSBarry Smith ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr); 7485f3b1f45cSBarry Smith ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr); 7486f3b1f45cSBarry Smith ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr); 7487f3b1f45cSBarry Smith PetscFunctionReturn(0); 7488f3b1f45cSBarry Smith } 74890fe4d17eSHong Zhang 74900fe4d17eSHong Zhang /*@ 74910fe4d17eSHong Zhang TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used. 74920fe4d17eSHong Zhang 74930fe4d17eSHong Zhang Logically collective 74940fe4d17eSHong Zhang 74950fe4d17eSHong Zhang Input Parameter: 74960fe4d17eSHong Zhang + ts - timestepping context 74970fe4d17eSHong Zhang - use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used 74980fe4d17eSHong Zhang 74990fe4d17eSHong Zhang Options Database: 75000fe4d17eSHong Zhang . -ts_use_splitrhsfunction - <true,false> 75010fe4d17eSHong Zhang 75020fe4d17eSHong Zhang Notes: 75030fe4d17eSHong Zhang This is only useful for multirate methods 75040fe4d17eSHong Zhang 75050fe4d17eSHong Zhang Level: intermediate 75060fe4d17eSHong Zhang 75070fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction() 75080fe4d17eSHong Zhang @*/ 75090fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction) 75100fe4d17eSHong Zhang { 75110fe4d17eSHong Zhang PetscFunctionBegin; 75120fe4d17eSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 75130fe4d17eSHong Zhang ts->use_splitrhsfunction = use_splitrhsfunction; 75140fe4d17eSHong Zhang PetscFunctionReturn(0); 75150fe4d17eSHong Zhang } 75160fe4d17eSHong Zhang 75170fe4d17eSHong Zhang /*@ 75180fe4d17eSHong Zhang TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used. 75190fe4d17eSHong Zhang 75200fe4d17eSHong Zhang Not collective 75210fe4d17eSHong Zhang 75220fe4d17eSHong Zhang Input Parameter: 75230fe4d17eSHong Zhang . ts - timestepping context 75240fe4d17eSHong Zhang 75250fe4d17eSHong Zhang Output Parameter: 75260fe4d17eSHong Zhang . use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used 75270fe4d17eSHong Zhang 75280fe4d17eSHong Zhang Level: intermediate 75290fe4d17eSHong Zhang 75300fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction() 75310fe4d17eSHong Zhang @*/ 75320fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction) 75330fe4d17eSHong Zhang { 75340fe4d17eSHong Zhang PetscFunctionBegin; 75350fe4d17eSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 75360fe4d17eSHong Zhang *use_splitrhsfunction = ts->use_splitrhsfunction; 75370fe4d17eSHong Zhang PetscFunctionReturn(0); 75380fe4d17eSHong Zhang } 7539