1af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 2496e6a7aSJed Brown #include <petscdmshell.h> 31e25c274SJed Brown #include <petscdmda.h> 42d5ee99bSBarry Smith #include <petscviewer.h> 52d5ee99bSBarry Smith #include <petscdraw.h> 6d763cef2SBarry Smith 7d5ba7fb7SMatthew Knepley /* Logging support */ 8d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 9a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 10d405a339SMatthew Knepley 11feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1249354f04SShri Abhyankar 13fde5950dSBarry Smith /*@C 14fde5950dSBarry Smith TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 15fde5950dSBarry Smith 16fde5950dSBarry Smith Collective on TS 17fde5950dSBarry Smith 18fde5950dSBarry Smith Input Parameters: 19fde5950dSBarry Smith + ts - TS object you wish to monitor 20fde5950dSBarry Smith . name - the monitor type one is seeking 21fde5950dSBarry Smith . help - message indicating what monitoring is done 22fde5950dSBarry Smith . manual - manual page for the monitor 23fde5950dSBarry Smith . monitor - the monitor function 24fde5950dSBarry 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 25fde5950dSBarry Smith 26fde5950dSBarry Smith Level: developer 27fde5950dSBarry Smith 28fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 29fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 30fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 31fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 32fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 33fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 34fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 35fde5950dSBarry Smith @*/ 36721cd6eeSBarry 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*)) 37fde5950dSBarry Smith { 38fde5950dSBarry Smith PetscErrorCode ierr; 39fde5950dSBarry Smith PetscViewer viewer; 40fde5950dSBarry Smith PetscViewerFormat format; 41fde5950dSBarry Smith PetscBool flg; 42fde5950dSBarry Smith 43fde5950dSBarry Smith PetscFunctionBegin; 44fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 45fde5950dSBarry Smith if (flg) { 46721cd6eeSBarry Smith PetscViewerAndFormat *vf; 47721cd6eeSBarry Smith ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr); 48721cd6eeSBarry Smith ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 49fde5950dSBarry Smith if (monitorsetup) { 50721cd6eeSBarry Smith ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr); 51fde5950dSBarry Smith } 52721cd6eeSBarry Smith ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr); 53fde5950dSBarry Smith } 54fde5950dSBarry Smith PetscFunctionReturn(0); 55fde5950dSBarry Smith } 56fde5950dSBarry Smith 572ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type) 582ffb9264SLisandro Dalcin { 592ffb9264SLisandro Dalcin PetscErrorCode ierr; 602ffb9264SLisandro Dalcin 612ffb9264SLisandro Dalcin PetscFunctionBegin; 62b92453a8SLisandro Dalcin PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1); 63b92453a8SLisandro Dalcin PetscValidCharPointer(default_type,2); 642ffb9264SLisandro Dalcin if (!((PetscObject)adapt)->type_name) { 652ffb9264SLisandro Dalcin ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr); 662ffb9264SLisandro Dalcin } 672ffb9264SLisandro Dalcin PetscFunctionReturn(0); 682ffb9264SLisandro Dalcin } 692ffb9264SLisandro Dalcin 70bdad233fSMatthew Knepley /*@ 71bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 72bdad233fSMatthew Knepley 73bdad233fSMatthew Knepley Collective on TS 74bdad233fSMatthew Knepley 75bdad233fSMatthew Knepley Input Parameter: 76bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 77bdad233fSMatthew Knepley 78bdad233fSMatthew Knepley Options Database Keys: 79e49d4f37SHong Zhang + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP 80ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 81ef85077eSLisandro Dalcin . -ts_max_time <time> - maximum time to compute to 82ef85077eSLisandro Dalcin . -ts_max_steps <steps> - maximum number of time-steps to take 83ef85077eSLisandro Dalcin . -ts_init_time <time> - initial time to start computation 84ef85077eSLisandro Dalcin . -ts_final_time <time> - final time to compute to 853e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 86a3cdaa26SBarry 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 87a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 88a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 89a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 90a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 91a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 92f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function 93f3b1f45cSBarry 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 94ef222394SBarry Smith . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 95847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 96bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 97de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 98de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 997cf37e64SBarry Smith . -ts_monitor_error - Monitors norm of error 1006934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 1018b668821SLisandro Dalcin . -ts_monitor_lg_timestep_log - Monitor log timestep size graphically 102de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 103de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 104de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 105de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 1063e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 1073e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 108fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 109e4160dc7SJulian Andrej . -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu) 110110eb670SHong Zhang . -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 11153ea634cSHong Zhang 11291b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 113bdad233fSMatthew Knepley 114bdad233fSMatthew Knepley Level: beginner 115bdad233fSMatthew Knepley 116bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 117bdad233fSMatthew Knepley 118a313700dSBarry Smith .seealso: TSGetType() 119bdad233fSMatthew Knepley @*/ 1207087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 121bdad233fSMatthew Knepley { 122bc952696SBarry Smith PetscBool opt,flg,tflg; 123dfbe8321SBarry Smith PetscErrorCode ierr; 124eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 12531748224SBarry Smith PetscReal time_step; 12649354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 127d1212d36SBarry Smith char dir[16]; 128cd11d68dSLisandro Dalcin TSIFunction ifun; 1296991f827SBarry Smith const char *defaultType; 1306991f827SBarry Smith char typeName[256]; 131bdad233fSMatthew Knepley 132bdad233fSMatthew Knepley PetscFunctionBegin; 1330700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1346991f827SBarry Smith 1356991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 136cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 137cd11d68dSLisandro Dalcin 138cd11d68dSLisandro Dalcin ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 139cd11d68dSLisandro Dalcin if (((PetscObject)ts)->type_name) 140cd11d68dSLisandro Dalcin defaultType = ((PetscObject)ts)->type_name; 141cd11d68dSLisandro Dalcin else 142cd11d68dSLisandro Dalcin defaultType = ifun ? TSBEULER : TSEULER; 1436991f827SBarry Smith ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr); 1446991f827SBarry Smith if (opt) { 1456991f827SBarry Smith ierr = TSSetType(ts,typeName);CHKERRQ(ierr); 1466991f827SBarry Smith } else { 1476991f827SBarry Smith ierr = TSSetType(ts,defaultType);CHKERRQ(ierr); 1486991f827SBarry Smith } 149bdad233fSMatthew Knepley 150bdad233fSMatthew Knepley /* Handle generic TS options */ 151ef85077eSLisandro Dalcin ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 15219eac22cSLisandro Dalcin ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1530298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 154ef85077eSLisandro Dalcin ierr = PetscOptionsReal("-ts_final_time","Final time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 15531748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 156cd11d68dSLisandro Dalcin if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);} 15749354f04SShri 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); 15849354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 1590298fd71SBarry 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); 1600298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 1610298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 1620298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 1630298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 164bdad233fSMatthew Knepley 165f3b1f45cSBarry 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); 166f3b1f45cSBarry 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); 16756f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 16856f85f32SBarry Smith { 16956f85f32SBarry Smith PetscBool set; 17056f85f32SBarry Smith flg = PETSC_FALSE; 17156f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 17256f85f32SBarry Smith if (set) { 17356f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 17456f85f32SBarry Smith } 17556f85f32SBarry Smith } 17656f85f32SBarry Smith #endif 17756f85f32SBarry Smith 178bdad233fSMatthew Knepley /* Monitor options */ 179cd11d68dSLisandro Dalcin ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr); 180cc9c3a59SBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL);CHKERRQ(ierr); 181fde5950dSBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr); 182fde5950dSBarry Smith 1835180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1845180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1855180491cSLisandro Dalcin 1864f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 187b3603a34SBarry Smith if (opt) { 1880b039ecaSBarry Smith TSMonitorLGCtx ctx; 1893923b477SBarry Smith PetscInt howoften = 1; 190b3603a34SBarry Smith 1910298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 1926ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 1934f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 194bdad233fSMatthew Knepley } 1956ba87a44SLisandro Dalcin 1964f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 197ef20d060SBarry Smith if (opt) { 1980b039ecaSBarry Smith TSMonitorLGCtx ctx; 1993923b477SBarry Smith PetscInt howoften = 1; 200ef20d060SBarry Smith 2010298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 2026ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2034f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 204ef20d060SBarry Smith } 205edbaebb3SBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr); 2067cf37e64SBarry Smith 2076934998bSLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 2086934998bSLisandro Dalcin if (opt) { 2096934998bSLisandro Dalcin TSMonitorLGCtx ctx; 2106934998bSLisandro Dalcin PetscInt howoften = 1; 2116934998bSLisandro Dalcin 2126934998bSLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 2136ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2146934998bSLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 2156934998bSLisandro Dalcin } 2168b668821SLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 2178b668821SLisandro Dalcin if (opt) { 2188b668821SLisandro Dalcin TSMonitorLGCtx ctx; 2198b668821SLisandro Dalcin PetscInt howoften = 1; 2208b668821SLisandro Dalcin 2218b668821SLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 2228b668821SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2238b668821SLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 2248b668821SLisandro Dalcin ctx->semilogy = PETSC_TRUE; 2258b668821SLisandro Dalcin } 2268b668821SLisandro Dalcin 227201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 228201da799SBarry Smith if (opt) { 229201da799SBarry Smith TSMonitorLGCtx ctx; 230201da799SBarry Smith PetscInt howoften = 1; 231201da799SBarry Smith 2320298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2336ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 234201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 235201da799SBarry Smith } 236201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 237201da799SBarry Smith if (opt) { 238201da799SBarry Smith TSMonitorLGCtx ctx; 239201da799SBarry Smith PetscInt howoften = 1; 240201da799SBarry Smith 2410298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2426ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 243201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 244201da799SBarry Smith } 2458189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 2468189c53fSBarry Smith if (opt) { 2478189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2488189c53fSBarry Smith PetscInt howoften = 1; 2498189c53fSBarry Smith 2500298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 2516934998bSLisandro Dalcin ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 2528189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 2538189c53fSBarry Smith } 254*1b575b74SJoseph Pusztay ierr = PetscOptionsName("-ts_monitor_sp","Display particles from the dm swarm","TSMonitorSP",&opt);CHKERRQ(ierr); 255*1b575b74SJoseph Pusztay if (opt) { 256*1b575b74SJoseph Pusztay TSMonitorSPCtx ctx; 257*1b575b74SJoseph Pusztay PetscInt howoften = 1; 258*1b575b74SJoseph Pusztay ierr = PetscOptionsInt("-ts_monitor_sp","Display particles from the dm swarm","TSMonitorSP",howoften,&howoften,NULL);CHKERRQ(ierr); 259*1b575b74SJoseph Pusztay ierr = TSMonitorSPCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx);CHKERRQ(ierr); 260*1b575b74SJoseph Pusztay ierr = TSMonitorSet(ts, TSMonitorSPSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy);CHKERRQ(ierr); 261*1b575b74SJoseph Pusztay } 262ef20d060SBarry Smith opt = PETSC_FALSE; 2630dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 264a7cc72afSBarry Smith if (opt) { 26583a4ac43SBarry Smith TSMonitorDrawCtx ctx; 26683a4ac43SBarry Smith PetscInt howoften = 1; 267a80ad3e0SBarry Smith 2680298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 2690ed3bfb6SBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 27083a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 271bdad233fSMatthew Knepley } 272fb1732b5SBarry Smith opt = PETSC_FALSE; 2732d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 2742d5ee99bSBarry Smith if (opt) { 2752d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2762d5ee99bSBarry Smith PetscReal bounds[4]; 2772d5ee99bSBarry Smith PetscInt n = 4; 2782d5ee99bSBarry Smith PetscDraw draw; 2796934998bSLisandro Dalcin PetscDrawAxis axis; 2802d5ee99bSBarry Smith 2812d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 2822d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2836934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr); 2842d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 2856934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr); 2866934998bSLisandro Dalcin ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 2876934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 2882d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2892d5ee99bSBarry Smith } 2902d5ee99bSBarry Smith opt = PETSC_FALSE; 2910dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 2923a471f94SBarry Smith if (opt) { 29383a4ac43SBarry Smith TSMonitorDrawCtx ctx; 29483a4ac43SBarry Smith PetscInt howoften = 1; 2953a471f94SBarry Smith 2960298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 2970ed3bfb6SBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 29883a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2993a471f94SBarry Smith } 3000ed3bfb6SBarry Smith opt = PETSC_FALSE; 3010ed3bfb6SBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr); 3020ed3bfb6SBarry Smith if (opt) { 3030ed3bfb6SBarry Smith TSMonitorDrawCtx ctx; 3040ed3bfb6SBarry Smith PetscInt howoften = 1; 3050ed3bfb6SBarry Smith 3060ed3bfb6SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr); 3070ed3bfb6SBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 3080ed3bfb6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3090ed3bfb6SBarry Smith } 310fde5950dSBarry Smith 311ed81e22dSJed Brown opt = PETSC_FALSE; 31291b97e58SBarry 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); 313ed81e22dSJed Brown if (flg) { 314ed81e22dSJed Brown const char *ptr,*ptr2; 315ed81e22dSJed Brown char *filetemplate; 316ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 317ed81e22dSJed Brown /* Do some cursory validation of the input. */ 318ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 319ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 320ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 321ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 322ce94432eSBarry 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"); 323ed81e22dSJed Brown if (ptr2) break; 324ed81e22dSJed Brown } 325ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 326ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 327ed81e22dSJed Brown } 328bdad233fSMatthew Knepley 329d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 330d1212d36SBarry Smith if (flg) { 331d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 332d1212d36SBarry Smith int ray = 0; 333d1212d36SBarry Smith DMDADirection ddir; 334d1212d36SBarry Smith DM da; 335d1212d36SBarry Smith PetscMPIInt rank; 336d1212d36SBarry Smith 337ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 338d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 339d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 340ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 341d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 342d1212d36SBarry Smith 343d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 344b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 345d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 346d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 347ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 348d1212d36SBarry Smith if (!rank) { 349d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 350d1212d36SBarry Smith } 35151b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 352d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 353d1212d36SBarry Smith } 35451b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 35551b4a12fSMatthew G. Knepley if (flg) { 35651b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 35751b4a12fSMatthew G. Knepley int ray = 0; 35851b4a12fSMatthew G. Knepley DMDADirection ddir; 35951b4a12fSMatthew G. Knepley DM da; 36051b4a12fSMatthew G. Knepley PetscInt howoften = 1; 361d1212d36SBarry Smith 36251b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 36351b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 36451b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 36551b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 36651b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 3671c3436cfSJed Brown 36851b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 369b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 37051b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 37151b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 37251b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 37351b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 37451b4a12fSMatthew G. Knepley } 375a7a1495cSBarry Smith 376b3d3934dSBarry Smith ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 377b3d3934dSBarry Smith if (opt) { 378b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 379b3d3934dSBarry Smith 380b3d3934dSBarry Smith ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 381b3d3934dSBarry Smith ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 382b3d3934dSBarry Smith } 383b3d3934dSBarry Smith 384847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 385847ff0e1SMatthew G. Knepley ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr); 386847ff0e1SMatthew G. Knepley if (flg) { 387847ff0e1SMatthew G. Knepley DM dm; 388847ff0e1SMatthew G. Knepley DMTS tdm; 389847ff0e1SMatthew G. Knepley 390847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 391847ff0e1SMatthew G. Knepley ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr); 392847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 393847ff0e1SMatthew G. Knepley ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr); 394847ff0e1SMatthew G. Knepley ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 395847ff0e1SMatthew G. Knepley } 396847ff0e1SMatthew G. Knepley 397d763cef2SBarry Smith /* Handle specific TS options */ 398d763cef2SBarry Smith if (ts->ops->setfromoptions) { 3996991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 400d763cef2SBarry Smith } 401fbc52257SHong Zhang 402a7bdc993SLisandro Dalcin /* Handle TSAdapt options */ 403a7bdc993SLisandro Dalcin ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 404a7bdc993SLisandro Dalcin ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr); 405a7bdc993SLisandro Dalcin ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr); 406a7bdc993SLisandro Dalcin 40768bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 4084f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 40968bece0bSHong Zhang ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 41068bece0bSHong Zhang if (tflg) { 41168bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 41268bece0bSHong Zhang } 413a05bf03eSHong Zhang 414a05bf03eSHong Zhang ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr); 415d763cef2SBarry Smith 416d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 4170633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr); 418fbc52257SHong Zhang ierr = PetscOptionsEnd();CHKERRQ(ierr); 419d763cef2SBarry Smith 4204f122a70SLisandro Dalcin if (ts->trajectory) { 4214f122a70SLisandro Dalcin ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 422d763cef2SBarry Smith } 42368bece0bSHong Zhang 4244f122a70SLisandro Dalcin ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr); 4254f122a70SLisandro Dalcin if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);} 4264f122a70SLisandro Dalcin ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr); 427d763cef2SBarry Smith PetscFunctionReturn(0); 428d763cef2SBarry Smith } 429d763cef2SBarry Smith 430d2daff3dSHong Zhang /*@ 43178fbdcc8SBarry Smith TSGetTrajectory - Gets the trajectory from a TS if it exists 43278fbdcc8SBarry Smith 43378fbdcc8SBarry Smith Collective on TS 43478fbdcc8SBarry Smith 43578fbdcc8SBarry Smith Input Parameters: 43678fbdcc8SBarry Smith . ts - the TS context obtained from TSCreate() 43778fbdcc8SBarry Smith 43878fbdcc8SBarry Smith Output Parameters; 43978fbdcc8SBarry Smith . tr - the TSTrajectory object, if it exists 44078fbdcc8SBarry Smith 44178fbdcc8SBarry Smith Note: This routine should be called after all TS options have been set 44278fbdcc8SBarry Smith 44378fbdcc8SBarry Smith Level: advanced 44478fbdcc8SBarry Smith 44578fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate() 44678fbdcc8SBarry Smith 44778fbdcc8SBarry Smith .keywords: TS, set, checkpoint, 44878fbdcc8SBarry Smith @*/ 44978fbdcc8SBarry Smith PetscErrorCode TSGetTrajectory(TS ts,TSTrajectory *tr) 45078fbdcc8SBarry Smith { 45178fbdcc8SBarry Smith PetscFunctionBegin; 45278fbdcc8SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45378fbdcc8SBarry Smith *tr = ts->trajectory; 45478fbdcc8SBarry Smith PetscFunctionReturn(0); 45578fbdcc8SBarry Smith } 45678fbdcc8SBarry Smith 45778fbdcc8SBarry Smith /*@ 458bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 459d2daff3dSHong Zhang 460d2daff3dSHong Zhang Collective on TS 461d2daff3dSHong Zhang 462d2daff3dSHong Zhang Input Parameters: 463bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 464bc952696SBarry Smith 46578fbdcc8SBarry Smith Options Database: 46678fbdcc8SBarry Smith + -ts_save_trajectory - saves the trajectory to a file 46778fbdcc8SBarry Smith - -ts_trajectory_type type 46878fbdcc8SBarry Smith 46968bece0bSHong Zhang Note: This routine should be called after all TS options have been set 470d2daff3dSHong Zhang 471c3a89c15SBarry Smith The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and 47278fbdcc8SBarry Smith MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m 47378fbdcc8SBarry Smith 474d2daff3dSHong Zhang Level: intermediate 475d2daff3dSHong Zhang 47678fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectoryType, TSSetTrajectoryType() 477d2daff3dSHong Zhang 478bc952696SBarry Smith .keywords: TS, set, checkpoint, 479d2daff3dSHong Zhang @*/ 480bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 481d2daff3dSHong Zhang { 482bc952696SBarry Smith PetscErrorCode ierr; 483bc952696SBarry Smith 484d2daff3dSHong Zhang PetscFunctionBegin; 485d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 486bc952696SBarry Smith if (!ts->trajectory) { 487bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 488bc952696SBarry Smith } 489d2daff3dSHong Zhang PetscFunctionReturn(0); 490d2daff3dSHong Zhang } 491d2daff3dSHong Zhang 492a7a1495cSBarry Smith /*@ 493a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 494a7a1495cSBarry Smith set with TSSetRHSJacobian(). 495a7a1495cSBarry Smith 496a7a1495cSBarry Smith Collective on TS and Vec 497a7a1495cSBarry Smith 498a7a1495cSBarry Smith Input Parameters: 499316643e7SJed Brown + ts - the TS context 500a7a1495cSBarry Smith . t - current timestep 5010910c330SBarry Smith - U - input vector 502a7a1495cSBarry Smith 503a7a1495cSBarry Smith Output Parameters: 504a7a1495cSBarry Smith + A - Jacobian matrix 505a7a1495cSBarry Smith . B - optional preconditioning matrix 506a7a1495cSBarry Smith - flag - flag indicating matrix structure 507a7a1495cSBarry Smith 508a7a1495cSBarry Smith Notes: 509a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 510a7a1495cSBarry Smith is used internally within the nonlinear solvers. 511a7a1495cSBarry Smith 51294b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 513a7a1495cSBarry Smith flag parameter. 514a7a1495cSBarry Smith 515a7a1495cSBarry Smith Level: developer 516a7a1495cSBarry Smith 517a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 518a7a1495cSBarry Smith 51994b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 520a7a1495cSBarry Smith @*/ 521d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 522a7a1495cSBarry Smith { 523dfbe8321SBarry Smith PetscErrorCode ierr; 524270bf2e7SJed Brown PetscObjectState Ustate; 5256c1e1eecSBarry Smith PetscObjectId Uid; 52624989b8cSPeter Brune DM dm; 527942e3340SBarry Smith DMTS tsdm; 52824989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 52924989b8cSPeter Brune void *ctx; 53024989b8cSPeter Brune TSIJacobian ijacobianfunc; 531b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 532a7a1495cSBarry Smith 533a7a1495cSBarry Smith PetscFunctionBegin; 5340700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5350910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5360910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 53724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 538942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 53924989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 5400298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 541b2df71adSDebojyoti Ghosh ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 54259e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 5436c1e1eecSBarry Smith ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr); 5446c1e1eecSBarry Smith if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 5450e4ef248SJed Brown PetscFunctionReturn(0); 546f8ede8e7SJed Brown } 547d90be118SSean Farley 548ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 549d90be118SSean Farley 550e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 55194ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 55294ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 553303f9b21SStefano Zampini if (B && A != B) { 55494ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 55594ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 556e1244c69SJed Brown } 557e1244c69SJed Brown ts->rhsjacobian.shift = 0; 558e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 559e1244c69SJed Brown } 560e1244c69SJed Brown 56124989b8cSPeter Brune if (rhsjacobianfunc) { 5626cd88445SBarry Smith PetscBool missing; 56394ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 564a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 565d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 566a7a1495cSBarry Smith PetscStackPop; 56794ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 5686cd88445SBarry Smith if (A) { 5696cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 5706cd88445SBarry 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"); 5716cd88445SBarry Smith } 5726cd88445SBarry Smith if (B && B != A) { 5736cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 5746cd88445SBarry 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"); 5756cd88445SBarry Smith } 576ef66eb69SBarry Smith } else { 57794ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 57894ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 579ef66eb69SBarry Smith } 5800e4ef248SJed Brown ts->rhsjacobian.time = t; 5817f79407eSBarry Smith ierr = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr); 58259e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 583a7a1495cSBarry Smith PetscFunctionReturn(0); 584a7a1495cSBarry Smith } 585a7a1495cSBarry Smith 586316643e7SJed Brown /*@ 587d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 588d763cef2SBarry Smith 589316643e7SJed Brown Collective on TS and Vec 590316643e7SJed Brown 591316643e7SJed Brown Input Parameters: 592316643e7SJed Brown + ts - the TS context 593316643e7SJed Brown . t - current time 5940910c330SBarry Smith - U - state vector 595316643e7SJed Brown 596316643e7SJed Brown Output Parameter: 597316643e7SJed Brown . y - right hand side 598316643e7SJed Brown 599316643e7SJed Brown Note: 600316643e7SJed Brown Most users should not need to explicitly call this routine, as it 601316643e7SJed Brown is used internally within the nonlinear solvers. 602316643e7SJed Brown 603316643e7SJed Brown Level: developer 604316643e7SJed Brown 605316643e7SJed Brown .keywords: TS, compute 606316643e7SJed Brown 607316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 608316643e7SJed Brown @*/ 6090910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 610d763cef2SBarry Smith { 611dfbe8321SBarry Smith PetscErrorCode ierr; 61224989b8cSPeter Brune TSRHSFunction rhsfunction; 61324989b8cSPeter Brune TSIFunction ifunction; 61424989b8cSPeter Brune void *ctx; 61524989b8cSPeter Brune DM dm; 61624989b8cSPeter Brune 617d763cef2SBarry Smith PetscFunctionBegin; 6180700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6190910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6200700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 62124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 62224989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 6230298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 624d763cef2SBarry Smith 625ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 626d763cef2SBarry Smith 6270910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 62824989b8cSPeter Brune if (rhsfunction) { 629d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 6300910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 631d763cef2SBarry Smith PetscStackPop; 632214bc6a2SJed Brown } else { 633214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 634b2cd27e8SJed Brown } 63544a41b28SSean Farley 6360910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 637d763cef2SBarry Smith PetscFunctionReturn(0); 638d763cef2SBarry Smith } 639d763cef2SBarry Smith 640ef20d060SBarry Smith /*@ 641ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 642ef20d060SBarry Smith 643ef20d060SBarry Smith Collective on TS and Vec 644ef20d060SBarry Smith 645ef20d060SBarry Smith Input Parameters: 646ef20d060SBarry Smith + ts - the TS context 647ef20d060SBarry Smith - t - current time 648ef20d060SBarry Smith 649ef20d060SBarry Smith Output Parameter: 6500910c330SBarry Smith . U - the solution 651ef20d060SBarry Smith 652ef20d060SBarry Smith Note: 653ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 654ef20d060SBarry Smith is used internally within the nonlinear solvers. 655ef20d060SBarry Smith 656ef20d060SBarry Smith Level: developer 657ef20d060SBarry Smith 658ef20d060SBarry Smith .keywords: TS, compute 659ef20d060SBarry Smith 660abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 661ef20d060SBarry Smith @*/ 6620910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 663ef20d060SBarry Smith { 664ef20d060SBarry Smith PetscErrorCode ierr; 665ef20d060SBarry Smith TSSolutionFunction solutionfunction; 666ef20d060SBarry Smith void *ctx; 667ef20d060SBarry Smith DM dm; 668ef20d060SBarry Smith 669ef20d060SBarry Smith PetscFunctionBegin; 670ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6710910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 672ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 673ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 674ef20d060SBarry Smith 675ef20d060SBarry Smith if (solutionfunction) { 6769b7cd975SBarry Smith PetscStackPush("TS user solution function"); 6770910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 678ef20d060SBarry Smith PetscStackPop; 679ef20d060SBarry Smith } 680ef20d060SBarry Smith PetscFunctionReturn(0); 681ef20d060SBarry Smith } 6829b7cd975SBarry Smith /*@ 6839b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6849b7cd975SBarry Smith 6859b7cd975SBarry Smith Collective on TS and Vec 6869b7cd975SBarry Smith 6879b7cd975SBarry Smith Input Parameters: 6889b7cd975SBarry Smith + ts - the TS context 6899b7cd975SBarry Smith - t - current time 6909b7cd975SBarry Smith 6919b7cd975SBarry Smith Output Parameter: 6929b7cd975SBarry Smith . U - the function value 6939b7cd975SBarry Smith 6949b7cd975SBarry Smith Note: 6959b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 6969b7cd975SBarry Smith is used internally within the nonlinear solvers. 6979b7cd975SBarry Smith 6989b7cd975SBarry Smith Level: developer 6999b7cd975SBarry Smith 7009b7cd975SBarry Smith .keywords: TS, compute 7019b7cd975SBarry Smith 7029b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 7039b7cd975SBarry Smith @*/ 7049b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 7059b7cd975SBarry Smith { 7069b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 7079b7cd975SBarry Smith void *ctx; 7089b7cd975SBarry Smith DM dm; 7099b7cd975SBarry Smith 7109b7cd975SBarry Smith PetscFunctionBegin; 7119b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7129b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7139b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 7149b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 7159b7cd975SBarry Smith 7169b7cd975SBarry Smith if (forcing) { 7179b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 7189b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 7199b7cd975SBarry Smith PetscStackPop; 7209b7cd975SBarry Smith } 7219b7cd975SBarry Smith PetscFunctionReturn(0); 7229b7cd975SBarry Smith } 723ef20d060SBarry Smith 724214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 725214bc6a2SJed Brown { 7262dd45cf8SJed Brown Vec F; 727214bc6a2SJed Brown PetscErrorCode ierr; 728214bc6a2SJed Brown 729214bc6a2SJed Brown PetscFunctionBegin; 7300298fd71SBarry Smith *Frhs = NULL; 7310298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 732214bc6a2SJed Brown if (!ts->Frhs) { 7332dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 734214bc6a2SJed Brown } 735214bc6a2SJed Brown *Frhs = ts->Frhs; 736214bc6a2SJed Brown PetscFunctionReturn(0); 737214bc6a2SJed Brown } 738214bc6a2SJed Brown 739214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 740214bc6a2SJed Brown { 741214bc6a2SJed Brown Mat A,B; 7422dd45cf8SJed Brown PetscErrorCode ierr; 74341a1d4d2SBarry Smith TSIJacobian ijacobian; 744214bc6a2SJed Brown 745214bc6a2SJed Brown PetscFunctionBegin; 746c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 747c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 74841a1d4d2SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr); 749214bc6a2SJed Brown if (Arhs) { 750214bc6a2SJed Brown if (!ts->Arhs) { 75141a1d4d2SBarry Smith if (ijacobian) { 752214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 75341a1d4d2SBarry Smith } else { 75441a1d4d2SBarry Smith ts->Arhs = A; 75541a1d4d2SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 75641a1d4d2SBarry Smith } 7573565c898SBarry Smith } else { 7583565c898SBarry Smith PetscBool flg; 7593565c898SBarry Smith ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr); 7603565c898SBarry Smith /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */ 7613565c898SBarry Smith if (flg && !ijacobian && ts->Arhs == ts->Brhs){ 7623565c898SBarry Smith ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr); 7633565c898SBarry Smith ts->Arhs = A; 7643565c898SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 7653565c898SBarry Smith } 766214bc6a2SJed Brown } 767214bc6a2SJed Brown *Arhs = ts->Arhs; 768214bc6a2SJed Brown } 769214bc6a2SJed Brown if (Brhs) { 770214bc6a2SJed Brown if (!ts->Brhs) { 771bdb70873SJed Brown if (A != B) { 77241a1d4d2SBarry Smith if (ijacobian) { 773214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 774bdb70873SJed Brown } else { 77541a1d4d2SBarry Smith ts->Brhs = B; 77641a1d4d2SBarry Smith ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 77741a1d4d2SBarry Smith } 77841a1d4d2SBarry Smith } else { 779bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 78051699248SLisandro Dalcin ts->Brhs = ts->Arhs; 781bdb70873SJed Brown } 782214bc6a2SJed Brown } 783214bc6a2SJed Brown *Brhs = ts->Brhs; 784214bc6a2SJed Brown } 785214bc6a2SJed Brown PetscFunctionReturn(0); 786214bc6a2SJed Brown } 787214bc6a2SJed Brown 788316643e7SJed Brown /*@ 7890910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 790316643e7SJed Brown 791316643e7SJed Brown Collective on TS and Vec 792316643e7SJed Brown 793316643e7SJed Brown Input Parameters: 794316643e7SJed Brown + ts - the TS context 795316643e7SJed Brown . t - current time 7960910c330SBarry Smith . U - state vector 7970910c330SBarry Smith . Udot - time derivative of state vector 798214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 799316643e7SJed Brown 800316643e7SJed Brown Output Parameter: 801316643e7SJed Brown . Y - right hand side 802316643e7SJed Brown 803316643e7SJed Brown Note: 804316643e7SJed Brown Most users should not need to explicitly call this routine, as it 805316643e7SJed Brown is used internally within the nonlinear solvers. 806316643e7SJed Brown 807316643e7SJed Brown If the user did did not write their equations in implicit form, this 808316643e7SJed Brown function recasts them in implicit form. 809316643e7SJed Brown 810316643e7SJed Brown Level: developer 811316643e7SJed Brown 812316643e7SJed Brown .keywords: TS, compute 813316643e7SJed Brown 814316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 815316643e7SJed Brown @*/ 8160910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 817316643e7SJed Brown { 818316643e7SJed Brown PetscErrorCode ierr; 81924989b8cSPeter Brune TSIFunction ifunction; 82024989b8cSPeter Brune TSRHSFunction rhsfunction; 82124989b8cSPeter Brune void *ctx; 82224989b8cSPeter Brune DM dm; 823316643e7SJed Brown 824316643e7SJed Brown PetscFunctionBegin; 8250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8260910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8270910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 8280700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 829316643e7SJed Brown 83024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 83124989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 8320298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 83324989b8cSPeter Brune 834ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 835d90be118SSean Farley 8360910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 83724989b8cSPeter Brune if (ifunction) { 838316643e7SJed Brown PetscStackPush("TS user implicit function"); 8390910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 840316643e7SJed Brown PetscStackPop; 841214bc6a2SJed Brown } 842214bc6a2SJed Brown if (imex) { 84324989b8cSPeter Brune if (!ifunction) { 8440910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 8452dd45cf8SJed Brown } 84624989b8cSPeter Brune } else if (rhsfunction) { 84724989b8cSPeter Brune if (ifunction) { 848214bc6a2SJed Brown Vec Frhs; 849214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 8500910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 851214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 8522dd45cf8SJed Brown } else { 8530910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 8540910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 855316643e7SJed Brown } 8564a6899ffSJed Brown } 8570910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 858316643e7SJed Brown PetscFunctionReturn(0); 859316643e7SJed Brown } 860316643e7SJed Brown 861316643e7SJed Brown /*@ 862316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 863316643e7SJed Brown 864316643e7SJed Brown Collective on TS and Vec 865316643e7SJed Brown 866316643e7SJed Brown Input 867316643e7SJed Brown Input Parameters: 868316643e7SJed Brown + ts - the TS context 869316643e7SJed Brown . t - current timestep 8700910c330SBarry Smith . U - state vector 8710910c330SBarry Smith . Udot - time derivative of state vector 872214bc6a2SJed Brown . shift - shift to apply, see note below 873214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 874316643e7SJed Brown 875316643e7SJed Brown Output Parameters: 876316643e7SJed Brown + A - Jacobian matrix 8773565c898SBarry Smith - B - matrix from which the preconditioner is constructed; often the same as A 878316643e7SJed Brown 879316643e7SJed Brown Notes: 8800910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 881316643e7SJed Brown 8820910c330SBarry Smith dF/dU + shift*dF/dUdot 883316643e7SJed Brown 884316643e7SJed Brown Most users should not need to explicitly call this routine, as it 885316643e7SJed Brown is used internally within the nonlinear solvers. 886316643e7SJed Brown 887316643e7SJed Brown Level: developer 888316643e7SJed Brown 889316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 890316643e7SJed Brown 891316643e7SJed Brown .seealso: TSSetIJacobian() 89263495f91SJed Brown @*/ 893d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 894316643e7SJed Brown { 895316643e7SJed Brown PetscErrorCode ierr; 89624989b8cSPeter Brune TSIJacobian ijacobian; 89724989b8cSPeter Brune TSRHSJacobian rhsjacobian; 89824989b8cSPeter Brune DM dm; 89924989b8cSPeter Brune void *ctx; 900316643e7SJed Brown 901316643e7SJed Brown PetscFunctionBegin; 9020700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9030910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 9040910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 905316643e7SJed Brown PetscValidPointer(A,6); 90694ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 907316643e7SJed Brown PetscValidPointer(B,7); 90894ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 90924989b8cSPeter Brune 91024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 91124989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 9120298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 91324989b8cSPeter Brune 914ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 915316643e7SJed Brown 91694ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 91724989b8cSPeter Brune if (ijacobian) { 9186cd88445SBarry Smith PetscBool missing; 919316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 920d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 921316643e7SJed Brown PetscStackPop; 9226cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 9236cd88445SBarry 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"); 9243565c898SBarry Smith if (B != A) { 9256cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 9266cd88445SBarry 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"); 9276cd88445SBarry Smith } 9284a6899ffSJed Brown } 929214bc6a2SJed Brown if (imex) { 930b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 9314c26be97Sstefano_zampini PetscBool assembled; 93294ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 9334c26be97Sstefano_zampini ierr = MatAssembled(A,&assembled);CHKERRQ(ierr); 9344c26be97Sstefano_zampini if (!assembled) { 9354c26be97Sstefano_zampini ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9364c26be97Sstefano_zampini ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9374c26be97Sstefano_zampini } 93894ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 93994ab13aaSBarry Smith if (A != B) { 94094ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 9414c26be97Sstefano_zampini ierr = MatAssembled(B,&assembled);CHKERRQ(ierr); 9424c26be97Sstefano_zampini if (!assembled) { 9434c26be97Sstefano_zampini ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9444c26be97Sstefano_zampini ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9454c26be97Sstefano_zampini } 94694ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 947214bc6a2SJed Brown } 948214bc6a2SJed Brown } 949214bc6a2SJed Brown } else { 950e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 951e1244c69SJed Brown if (rhsjacobian) { 952214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 953d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 954e1244c69SJed Brown } 95594ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 9563565c898SBarry Smith PetscBool flg; 957e1244c69SJed Brown ts->rhsjacobian.scale = -1; 958e1244c69SJed Brown ts->rhsjacobian.shift = shift; 9593565c898SBarry Smith ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr); 9603565c898SBarry Smith /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */ 9613565c898SBarry Smith if (!flg) { 96294ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 96394ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 9643565c898SBarry Smith } 96594ab13aaSBarry Smith if (A != B) { 96694ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 96794ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 968316643e7SJed Brown } 969e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 970e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 971e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 97294ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 97394ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 97494ab13aaSBarry Smith if (A != B) { 97594ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 97694ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 977214bc6a2SJed Brown } 978316643e7SJed Brown } 97994ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 98094ab13aaSBarry Smith if (A != B) { 98194ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 982316643e7SJed Brown } 983316643e7SJed Brown } 984316643e7SJed Brown } 98594ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 986316643e7SJed Brown PetscFunctionReturn(0); 987316643e7SJed Brown } 988316643e7SJed Brown 989d763cef2SBarry Smith /*@C 990d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 991b5abc632SBarry Smith where U_t = G(t,u). 992d763cef2SBarry Smith 9933f9fe445SBarry Smith Logically Collective on TS 994d763cef2SBarry Smith 995d763cef2SBarry Smith Input Parameters: 996d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 9970298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 998d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 999d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 10000298fd71SBarry Smith function evaluation routine (may be NULL) 1001d763cef2SBarry Smith 1002d763cef2SBarry Smith Calling sequence of func: 100387828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 1004d763cef2SBarry Smith 1005d763cef2SBarry Smith + t - current timestep 1006d763cef2SBarry Smith . u - input vector 1007d763cef2SBarry Smith . F - function vector 1008d763cef2SBarry Smith - ctx - [optional] user-defined function context 1009d763cef2SBarry Smith 1010d763cef2SBarry Smith Level: beginner 1011d763cef2SBarry Smith 101295452b02SPatrick Sanan Notes: 101395452b02SPatrick Sanan You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 10142bbac0d3SBarry Smith 1015d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1016d763cef2SBarry Smith 1017ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 1018d763cef2SBarry Smith @*/ 1019089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 1020d763cef2SBarry Smith { 1021089b2837SJed Brown PetscErrorCode ierr; 1022089b2837SJed Brown SNES snes; 10230298fd71SBarry Smith Vec ralloc = NULL; 102424989b8cSPeter Brune DM dm; 1025d763cef2SBarry Smith 1026089b2837SJed Brown PetscFunctionBegin; 10270700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1028ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 102924989b8cSPeter Brune 103024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 103124989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 1032089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1033e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 1034e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 1035e856ceecSJed Brown r = ralloc; 1036e856ceecSJed Brown } 1037089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 1038e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1039d763cef2SBarry Smith PetscFunctionReturn(0); 1040d763cef2SBarry Smith } 1041d763cef2SBarry Smith 1042ef20d060SBarry Smith /*@C 1043abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1044ef20d060SBarry Smith 1045ef20d060SBarry Smith Logically Collective on TS 1046ef20d060SBarry Smith 1047ef20d060SBarry Smith Input Parameters: 1048ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1049ef20d060SBarry Smith . f - routine for evaluating the solution 1050ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 10510298fd71SBarry Smith function evaluation routine (may be NULL) 1052ef20d060SBarry Smith 1053ef20d060SBarry Smith Calling sequence of func: 1054ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 1055ef20d060SBarry Smith 1056ef20d060SBarry Smith + t - current timestep 1057ef20d060SBarry Smith . u - output vector 1058ef20d060SBarry Smith - ctx - [optional] user-defined function context 1059ef20d060SBarry Smith 10600ed3bfb6SBarry Smith Options Database: 10610ed3bfb6SBarry Smith + -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction() 10620ed3bfb6SBarry Smith - -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 10630ed3bfb6SBarry Smith 1064abd5a294SJed Brown Notes: 1065abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1066abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1067abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1068abd5a294SJed Brown 10694f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1070abd5a294SJed Brown 1071ef20d060SBarry Smith Level: beginner 1072ef20d060SBarry Smith 1073ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1074ef20d060SBarry Smith 10750ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError() 1076ef20d060SBarry Smith @*/ 1077ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1078ef20d060SBarry Smith { 1079ef20d060SBarry Smith PetscErrorCode ierr; 1080ef20d060SBarry Smith DM dm; 1081ef20d060SBarry Smith 1082ef20d060SBarry Smith PetscFunctionBegin; 1083ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1084ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1085ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 1086ef20d060SBarry Smith PetscFunctionReturn(0); 1087ef20d060SBarry Smith } 1088ef20d060SBarry Smith 10899b7cd975SBarry Smith /*@C 10909b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 10919b7cd975SBarry Smith 10929b7cd975SBarry Smith Logically Collective on TS 10939b7cd975SBarry Smith 10949b7cd975SBarry Smith Input Parameters: 10959b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 1096e162b725SBarry Smith . func - routine for evaluating the forcing function 10979b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 10980298fd71SBarry Smith function evaluation routine (may be NULL) 10999b7cd975SBarry Smith 11009b7cd975SBarry Smith Calling sequence of func: 1101e162b725SBarry Smith $ func (TS ts,PetscReal t,Vec f,void *ctx); 11029b7cd975SBarry Smith 11039b7cd975SBarry Smith + t - current timestep 1104e162b725SBarry Smith . f - output vector 11059b7cd975SBarry Smith - ctx - [optional] user-defined function context 11069b7cd975SBarry Smith 11079b7cd975SBarry Smith Notes: 11089b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1109e162b725SBarry 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 1110e162b725SBarry Smith definition of the problem you are solving and hence possibly introducing bugs. 1111e162b725SBarry Smith 1112e162b725SBarry Smith This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0 1113e162b725SBarry Smith 1114e162b725SBarry 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 1115e162b725SBarry Smith parameters can be passed in the ctx variable. 11169b7cd975SBarry Smith 11179b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 11189b7cd975SBarry Smith 11199b7cd975SBarry Smith Level: beginner 11209b7cd975SBarry Smith 11219b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 11229b7cd975SBarry Smith 11239b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 11249b7cd975SBarry Smith @*/ 1125e162b725SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx) 11269b7cd975SBarry Smith { 11279b7cd975SBarry Smith PetscErrorCode ierr; 11289b7cd975SBarry Smith DM dm; 11299b7cd975SBarry Smith 11309b7cd975SBarry Smith PetscFunctionBegin; 11319b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11329b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1133e162b725SBarry Smith ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr); 11349b7cd975SBarry Smith PetscFunctionReturn(0); 11359b7cd975SBarry Smith } 11369b7cd975SBarry Smith 1137d763cef2SBarry Smith /*@C 1138f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1139b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1140d763cef2SBarry Smith 11413f9fe445SBarry Smith Logically Collective on TS 1142d763cef2SBarry Smith 1143d763cef2SBarry Smith Input Parameters: 1144d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1145e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1146e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1147d763cef2SBarry Smith . f - the Jacobian evaluation routine 1148d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 11490298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1150d763cef2SBarry Smith 1151f7ab8db6SBarry Smith Calling sequence of f: 1152ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1153d763cef2SBarry Smith 1154d763cef2SBarry Smith + t - current timestep 1155d763cef2SBarry Smith . u - input vector 1156e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1157e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1158d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1159d763cef2SBarry Smith 11606cd88445SBarry Smith Notes: 11616cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 11626cd88445SBarry Smith 11636cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1164ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1165d763cef2SBarry Smith 1166d763cef2SBarry Smith Level: beginner 1167d763cef2SBarry Smith 1168d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1169d763cef2SBarry Smith 1170ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1171d763cef2SBarry Smith 1172d763cef2SBarry Smith @*/ 1173e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1174d763cef2SBarry Smith { 1175277b19d0SLisandro Dalcin PetscErrorCode ierr; 1176089b2837SJed Brown SNES snes; 117724989b8cSPeter Brune DM dm; 117824989b8cSPeter Brune TSIJacobian ijacobian; 1179277b19d0SLisandro Dalcin 1180d763cef2SBarry Smith PetscFunctionBegin; 11810700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1182e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1183e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1184e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1185e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1186d763cef2SBarry Smith 118724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 118824989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1189e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1190e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1191e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1192e1244c69SJed Brown } 11930298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1194089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11955f659677SPeter Brune if (!ijacobian) { 1196e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 11970e4ef248SJed Brown } 1198e5d3d808SBarry Smith if (Amat) { 1199e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 12000e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1201e5d3d808SBarry Smith ts->Arhs = Amat; 12020e4ef248SJed Brown } 1203e5d3d808SBarry Smith if (Pmat) { 1204e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 12050e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1206e5d3d808SBarry Smith ts->Brhs = Pmat; 12070e4ef248SJed Brown } 1208d763cef2SBarry Smith PetscFunctionReturn(0); 1209d763cef2SBarry Smith } 1210d763cef2SBarry Smith 1211316643e7SJed Brown /*@C 1212b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1213316643e7SJed Brown 12143f9fe445SBarry Smith Logically Collective on TS 1215316643e7SJed Brown 1216316643e7SJed Brown Input Parameters: 1217316643e7SJed Brown + ts - the TS context obtained from TSCreate() 12180298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1219316643e7SJed Brown . f - the function evaluation routine 12200298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1221316643e7SJed Brown 1222316643e7SJed Brown Calling sequence of f: 1223316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1224316643e7SJed Brown 1225316643e7SJed Brown + t - time at step/stage being solved 1226316643e7SJed Brown . u - state vector 1227316643e7SJed Brown . u_t - time derivative of state vector 1228316643e7SJed Brown . F - function vector 1229316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1230316643e7SJed Brown 1231316643e7SJed Brown Important: 12322bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1233316643e7SJed Brown 1234316643e7SJed Brown Level: beginner 1235316643e7SJed Brown 1236316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1237316643e7SJed Brown 1238d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1239316643e7SJed Brown @*/ 124051699248SLisandro Dalcin PetscErrorCode TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx) 1241316643e7SJed Brown { 1242089b2837SJed Brown PetscErrorCode ierr; 1243089b2837SJed Brown SNES snes; 124451699248SLisandro Dalcin Vec ralloc = NULL; 124524989b8cSPeter Brune DM dm; 1246316643e7SJed Brown 1247316643e7SJed Brown PetscFunctionBegin; 12480700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 124951699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 125024989b8cSPeter Brune 125124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 125224989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 125324989b8cSPeter Brune 1254089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 125551699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 125651699248SLisandro Dalcin ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 125751699248SLisandro Dalcin r = ralloc; 1258e856ceecSJed Brown } 125951699248SLisandro Dalcin ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 126051699248SLisandro Dalcin ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1261089b2837SJed Brown PetscFunctionReturn(0); 1262089b2837SJed Brown } 1263089b2837SJed Brown 1264089b2837SJed Brown /*@C 1265089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1266089b2837SJed Brown 1267089b2837SJed Brown Not Collective 1268089b2837SJed Brown 1269089b2837SJed Brown Input Parameter: 1270089b2837SJed Brown . ts - the TS context 1271089b2837SJed Brown 1272089b2837SJed Brown Output Parameter: 12730298fd71SBarry Smith + r - vector to hold residual (or NULL) 12740298fd71SBarry Smith . func - the function to compute residual (or NULL) 12750298fd71SBarry Smith - ctx - the function context (or NULL) 1276089b2837SJed Brown 1277089b2837SJed Brown Level: advanced 1278089b2837SJed Brown 1279089b2837SJed Brown .keywords: TS, nonlinear, get, function 1280089b2837SJed Brown 1281089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1282089b2837SJed Brown @*/ 1283089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1284089b2837SJed Brown { 1285089b2837SJed Brown PetscErrorCode ierr; 1286089b2837SJed Brown SNES snes; 128724989b8cSPeter Brune DM dm; 1288089b2837SJed Brown 1289089b2837SJed Brown PetscFunctionBegin; 1290089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1291089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12920298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 129324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 129424989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1295089b2837SJed Brown PetscFunctionReturn(0); 1296089b2837SJed Brown } 1297089b2837SJed Brown 1298089b2837SJed Brown /*@C 1299089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1300089b2837SJed Brown 1301089b2837SJed Brown Not Collective 1302089b2837SJed Brown 1303089b2837SJed Brown Input Parameter: 1304089b2837SJed Brown . ts - the TS context 1305089b2837SJed Brown 1306089b2837SJed Brown Output Parameter: 13070298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 13080298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 13090298fd71SBarry Smith - ctx - the function context (or NULL) 1310089b2837SJed Brown 1311089b2837SJed Brown Level: advanced 1312089b2837SJed Brown 1313089b2837SJed Brown .keywords: TS, nonlinear, get, function 1314089b2837SJed Brown 13152bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1316089b2837SJed Brown @*/ 1317089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1318089b2837SJed Brown { 1319089b2837SJed Brown PetscErrorCode ierr; 1320089b2837SJed Brown SNES snes; 132124989b8cSPeter Brune DM dm; 1322089b2837SJed Brown 1323089b2837SJed Brown PetscFunctionBegin; 1324089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1325089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13260298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 132724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 132824989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1329316643e7SJed Brown PetscFunctionReturn(0); 1330316643e7SJed Brown } 1331316643e7SJed Brown 1332316643e7SJed Brown /*@C 1333a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1334ae8867d6SBarry Smith provided with TSSetIFunction(). 1335316643e7SJed Brown 13363f9fe445SBarry Smith Logically Collective on TS 1337316643e7SJed Brown 1338316643e7SJed Brown Input Parameters: 1339316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1340e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1341e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1342316643e7SJed Brown . f - the Jacobian evaluation routine 13430298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1344316643e7SJed Brown 1345316643e7SJed Brown Calling sequence of f: 1346ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1347316643e7SJed Brown 1348316643e7SJed Brown + t - time at step/stage being solved 13491b4a444bSJed Brown . U - state vector 13501b4a444bSJed Brown . U_t - time derivative of state vector 1351316643e7SJed Brown . a - shift 1352e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1353e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1354316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1355316643e7SJed Brown 1356316643e7SJed Brown Notes: 1357e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1358316643e7SJed Brown 1359895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1360895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1361895c21f2SBarry Smith 1362a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1363b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1364a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1365a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1366a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1367a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1368a4f0a591SBarry Smith 13696cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13706cd88445SBarry Smith 13716cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1372ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1373ca5f011dSBarry Smith 1374316643e7SJed Brown Level: beginner 1375316643e7SJed Brown 1376316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1377316643e7SJed Brown 1378ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1379316643e7SJed Brown 1380316643e7SJed Brown @*/ 1381e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1382316643e7SJed Brown { 1383316643e7SJed Brown PetscErrorCode ierr; 1384089b2837SJed Brown SNES snes; 138524989b8cSPeter Brune DM dm; 1386316643e7SJed Brown 1387316643e7SJed Brown PetscFunctionBegin; 13880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1389e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1390e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1391e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1392e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 139324989b8cSPeter Brune 139424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 139524989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 139624989b8cSPeter Brune 1397089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1398e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1399316643e7SJed Brown PetscFunctionReturn(0); 1400316643e7SJed Brown } 1401316643e7SJed Brown 1402e1244c69SJed Brown /*@ 1403e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1404e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1405e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1406e1244c69SJed Brown not been changed by the TS. 1407e1244c69SJed Brown 1408e1244c69SJed Brown Logically Collective 1409e1244c69SJed Brown 1410e1244c69SJed Brown Input Arguments: 1411e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1412e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1413e1244c69SJed Brown 1414e1244c69SJed Brown Level: intermediate 1415e1244c69SJed Brown 1416e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1417e1244c69SJed Brown @*/ 1418e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1419e1244c69SJed Brown { 1420e1244c69SJed Brown PetscFunctionBegin; 1421e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1422e1244c69SJed Brown PetscFunctionReturn(0); 1423e1244c69SJed Brown } 1424e1244c69SJed Brown 1425efe9872eSLisandro Dalcin /*@C 1426efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1427efe9872eSLisandro Dalcin 1428efe9872eSLisandro Dalcin Logically Collective on TS 1429efe9872eSLisandro Dalcin 1430efe9872eSLisandro Dalcin Input Parameters: 1431efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1432efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1433efe9872eSLisandro Dalcin . fun - the function evaluation routine 1434efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1435efe9872eSLisandro Dalcin 1436efe9872eSLisandro Dalcin Calling sequence of fun: 1437efe9872eSLisandro Dalcin $ fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1438efe9872eSLisandro Dalcin 1439efe9872eSLisandro Dalcin + t - time at step/stage being solved 1440efe9872eSLisandro Dalcin . U - state vector 1441efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1442efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1443efe9872eSLisandro Dalcin . F - function vector 1444efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1445efe9872eSLisandro Dalcin 1446efe9872eSLisandro Dalcin Level: beginner 1447efe9872eSLisandro Dalcin 1448efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function 1449efe9872eSLisandro Dalcin 1450efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1451efe9872eSLisandro Dalcin @*/ 1452efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx) 1453efe9872eSLisandro Dalcin { 1454efe9872eSLisandro Dalcin DM dm; 1455efe9872eSLisandro Dalcin PetscErrorCode ierr; 1456efe9872eSLisandro Dalcin 1457efe9872eSLisandro Dalcin PetscFunctionBegin; 1458efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1459efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2); 1460efe9872eSLisandro Dalcin ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr); 1461efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1462efe9872eSLisandro Dalcin ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1463efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1464efe9872eSLisandro Dalcin } 1465efe9872eSLisandro Dalcin 1466efe9872eSLisandro Dalcin /*@C 1467efe9872eSLisandro Dalcin TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1468efe9872eSLisandro Dalcin 1469efe9872eSLisandro Dalcin Not Collective 1470efe9872eSLisandro Dalcin 1471efe9872eSLisandro Dalcin Input Parameter: 1472efe9872eSLisandro Dalcin . ts - the TS context 1473efe9872eSLisandro Dalcin 1474efe9872eSLisandro Dalcin Output Parameter: 1475efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1476efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1477efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1478efe9872eSLisandro Dalcin 1479efe9872eSLisandro Dalcin Level: advanced 1480efe9872eSLisandro Dalcin 1481efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function 1482efe9872eSLisandro Dalcin 1483efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction() 1484efe9872eSLisandro Dalcin @*/ 1485efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx) 1486efe9872eSLisandro Dalcin { 1487efe9872eSLisandro Dalcin PetscErrorCode ierr; 1488efe9872eSLisandro Dalcin SNES snes; 1489efe9872eSLisandro Dalcin DM dm; 1490efe9872eSLisandro Dalcin 1491efe9872eSLisandro Dalcin PetscFunctionBegin; 1492efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1493efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1494efe9872eSLisandro Dalcin ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 1495efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1496efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1497efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1498efe9872eSLisandro Dalcin } 1499efe9872eSLisandro Dalcin 1500efe9872eSLisandro Dalcin /*@C 1501bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1502efe9872eSLisandro Dalcin where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function(). 1503efe9872eSLisandro Dalcin 1504efe9872eSLisandro Dalcin Logically Collective on TS 1505efe9872eSLisandro Dalcin 1506efe9872eSLisandro Dalcin Input Parameters: 1507efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1508efe9872eSLisandro Dalcin . J - Jacobian matrix 1509efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1510efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1511efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1512efe9872eSLisandro Dalcin 1513efe9872eSLisandro Dalcin Calling sequence of jac: 1514bc77d74cSLisandro Dalcin $ jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx); 1515efe9872eSLisandro Dalcin 1516efe9872eSLisandro Dalcin + t - time at step/stage being solved 1517efe9872eSLisandro Dalcin . U - state vector 1518efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1519efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1520efe9872eSLisandro Dalcin . v - shift for U_t 1521efe9872eSLisandro Dalcin . a - shift for U_tt 1522efe9872eSLisandro 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 1523efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1524efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1525efe9872eSLisandro Dalcin 1526efe9872eSLisandro Dalcin Notes: 1527efe9872eSLisandro Dalcin The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve. 1528efe9872eSLisandro Dalcin 1529efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1530efe9872eSLisandro 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. 1531efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1532bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1533efe9872eSLisandro Dalcin 1534efe9872eSLisandro Dalcin Level: beginner 1535efe9872eSLisandro Dalcin 1536efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian 1537efe9872eSLisandro Dalcin 1538efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1539efe9872eSLisandro Dalcin @*/ 1540efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx) 1541efe9872eSLisandro Dalcin { 1542efe9872eSLisandro Dalcin DM dm; 1543efe9872eSLisandro Dalcin PetscErrorCode ierr; 1544efe9872eSLisandro Dalcin 1545efe9872eSLisandro Dalcin PetscFunctionBegin; 1546efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1547efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2); 1548efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3); 1549efe9872eSLisandro Dalcin ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr); 1550efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1551efe9872eSLisandro Dalcin ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1552efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1553efe9872eSLisandro Dalcin } 1554efe9872eSLisandro Dalcin 1555efe9872eSLisandro Dalcin /*@C 1556efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1557efe9872eSLisandro Dalcin 1558efe9872eSLisandro Dalcin Not Collective, but parallel objects are returned if TS is parallel 1559efe9872eSLisandro Dalcin 1560efe9872eSLisandro Dalcin Input Parameter: 1561efe9872eSLisandro Dalcin . ts - The TS context obtained from TSCreate() 1562efe9872eSLisandro Dalcin 1563efe9872eSLisandro Dalcin Output Parameters: 1564efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1565efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1566efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1567efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1568efe9872eSLisandro Dalcin 156995452b02SPatrick Sanan Notes: 157095452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 1571efe9872eSLisandro Dalcin 1572efe9872eSLisandro Dalcin Level: advanced 1573efe9872eSLisandro Dalcin 157480275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 1575efe9872eSLisandro Dalcin 1576efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian 1577efe9872eSLisandro Dalcin @*/ 1578efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx) 1579efe9872eSLisandro Dalcin { 1580efe9872eSLisandro Dalcin PetscErrorCode ierr; 1581efe9872eSLisandro Dalcin SNES snes; 1582efe9872eSLisandro Dalcin DM dm; 1583efe9872eSLisandro Dalcin 1584efe9872eSLisandro Dalcin PetscFunctionBegin; 1585efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1586efe9872eSLisandro Dalcin ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 1587efe9872eSLisandro Dalcin ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr); 1588efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1589efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1590efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1591efe9872eSLisandro Dalcin } 1592efe9872eSLisandro Dalcin 1593efe9872eSLisandro Dalcin /*@ 1594efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1595efe9872eSLisandro Dalcin 1596efe9872eSLisandro Dalcin Collective on TS and Vec 1597efe9872eSLisandro Dalcin 1598efe9872eSLisandro Dalcin Input Parameters: 1599efe9872eSLisandro Dalcin + ts - the TS context 1600efe9872eSLisandro Dalcin . t - current time 1601efe9872eSLisandro Dalcin . U - state vector 1602efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1603efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1604efe9872eSLisandro Dalcin 1605efe9872eSLisandro Dalcin Output Parameter: 1606efe9872eSLisandro Dalcin . F - the residual vector 1607efe9872eSLisandro Dalcin 1608efe9872eSLisandro Dalcin Note: 1609efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1610efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1611efe9872eSLisandro Dalcin 1612efe9872eSLisandro Dalcin Level: developer 1613efe9872eSLisandro Dalcin 1614efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector 1615efe9872eSLisandro Dalcin 1616efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1617efe9872eSLisandro Dalcin @*/ 1618efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F) 1619efe9872eSLisandro Dalcin { 1620efe9872eSLisandro Dalcin DM dm; 1621efe9872eSLisandro Dalcin TSI2Function I2Function; 1622efe9872eSLisandro Dalcin void *ctx; 1623efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1624efe9872eSLisandro Dalcin PetscErrorCode ierr; 1625efe9872eSLisandro Dalcin 1626efe9872eSLisandro Dalcin PetscFunctionBegin; 1627efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1628efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1629efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1630efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1631efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F,VEC_CLASSID,6); 1632efe9872eSLisandro Dalcin 1633efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1634efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr); 1635efe9872eSLisandro Dalcin ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 1636efe9872eSLisandro Dalcin 1637efe9872eSLisandro Dalcin if (!I2Function) { 1638efe9872eSLisandro Dalcin ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr); 1639efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1640efe9872eSLisandro Dalcin } 1641efe9872eSLisandro Dalcin 1642efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1643efe9872eSLisandro Dalcin 1644efe9872eSLisandro Dalcin PetscStackPush("TS user implicit function"); 1645efe9872eSLisandro Dalcin ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr); 1646efe9872eSLisandro Dalcin PetscStackPop; 1647efe9872eSLisandro Dalcin 1648efe9872eSLisandro Dalcin if (rhsfunction) { 1649efe9872eSLisandro Dalcin Vec Frhs; 1650efe9872eSLisandro Dalcin ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 1651efe9872eSLisandro Dalcin ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 1652efe9872eSLisandro Dalcin ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr); 1653efe9872eSLisandro Dalcin } 1654efe9872eSLisandro Dalcin 1655efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1656efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1657efe9872eSLisandro Dalcin } 1658efe9872eSLisandro Dalcin 1659efe9872eSLisandro Dalcin /*@ 1660efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1661efe9872eSLisandro Dalcin 1662efe9872eSLisandro Dalcin Collective on TS and Vec 1663efe9872eSLisandro Dalcin 1664efe9872eSLisandro Dalcin Input Parameters: 1665efe9872eSLisandro Dalcin + ts - the TS context 1666efe9872eSLisandro Dalcin . t - current timestep 1667efe9872eSLisandro Dalcin . U - state vector 1668efe9872eSLisandro Dalcin . V - time derivative of state vector 1669efe9872eSLisandro Dalcin . A - second time derivative of state vector 1670efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1671efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1672efe9872eSLisandro Dalcin 1673efe9872eSLisandro Dalcin Output Parameters: 1674efe9872eSLisandro Dalcin + J - Jacobian matrix 1675efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1676efe9872eSLisandro Dalcin 1677efe9872eSLisandro Dalcin Notes: 1678efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1679efe9872eSLisandro Dalcin 1680efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1681efe9872eSLisandro Dalcin 1682efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1683efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1684efe9872eSLisandro Dalcin 1685efe9872eSLisandro Dalcin Level: developer 1686efe9872eSLisandro Dalcin 1687efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix 1688efe9872eSLisandro Dalcin 1689efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1690efe9872eSLisandro Dalcin @*/ 1691efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P) 1692efe9872eSLisandro Dalcin { 1693efe9872eSLisandro Dalcin DM dm; 1694efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1695efe9872eSLisandro Dalcin void *ctx; 1696efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1697efe9872eSLisandro Dalcin PetscErrorCode ierr; 1698efe9872eSLisandro Dalcin 1699efe9872eSLisandro Dalcin PetscFunctionBegin; 1700efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1701efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1702efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1703efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1704efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J,MAT_CLASSID,8); 1705efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P,MAT_CLASSID,9); 1706efe9872eSLisandro Dalcin 1707efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1708efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr); 1709efe9872eSLisandro Dalcin ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 1710efe9872eSLisandro Dalcin 1711efe9872eSLisandro Dalcin if (!I2Jacobian) { 1712efe9872eSLisandro Dalcin ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr); 1713efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1714efe9872eSLisandro Dalcin } 1715efe9872eSLisandro Dalcin 1716efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1717efe9872eSLisandro Dalcin 1718efe9872eSLisandro Dalcin PetscStackPush("TS user implicit Jacobian"); 1719efe9872eSLisandro Dalcin ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr); 1720efe9872eSLisandro Dalcin PetscStackPop; 1721efe9872eSLisandro Dalcin 1722efe9872eSLisandro Dalcin if (rhsjacobian) { 1723efe9872eSLisandro Dalcin Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 1724efe9872eSLisandro Dalcin ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr); 1725efe9872eSLisandro Dalcin ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr); 1726efe9872eSLisandro Dalcin ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr); 1727efe9872eSLisandro Dalcin if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);} 1728efe9872eSLisandro Dalcin } 1729efe9872eSLisandro Dalcin 1730efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1731efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1732efe9872eSLisandro Dalcin } 1733efe9872eSLisandro Dalcin 1734efe9872eSLisandro Dalcin /*@ 1735efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1736efe9872eSLisandro Dalcin for use by the TS routines handling second order equations. 1737efe9872eSLisandro Dalcin 1738efe9872eSLisandro Dalcin Logically Collective on TS and Vec 1739efe9872eSLisandro Dalcin 1740efe9872eSLisandro Dalcin Input Parameters: 1741efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1742efe9872eSLisandro Dalcin . u - the solution vector 1743efe9872eSLisandro Dalcin - v - the time derivative vector 1744efe9872eSLisandro Dalcin 1745efe9872eSLisandro Dalcin Level: beginner 1746efe9872eSLisandro Dalcin 1747efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions 1748efe9872eSLisandro Dalcin @*/ 1749efe9872eSLisandro Dalcin PetscErrorCode TS2SetSolution(TS ts,Vec u,Vec v) 1750efe9872eSLisandro Dalcin { 1751efe9872eSLisandro Dalcin PetscErrorCode ierr; 1752efe9872eSLisandro Dalcin 1753efe9872eSLisandro Dalcin PetscFunctionBegin; 1754efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1755efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1756efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v,VEC_CLASSID,3); 1757efe9872eSLisandro Dalcin ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 1758efe9872eSLisandro Dalcin ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr); 1759efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 1760efe9872eSLisandro Dalcin ts->vec_dot = v; 1761efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1762efe9872eSLisandro Dalcin } 1763efe9872eSLisandro Dalcin 1764efe9872eSLisandro Dalcin /*@ 1765efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 1766efe9872eSLisandro Dalcin for second order equations. It is valid to call this routine inside the function 1767efe9872eSLisandro Dalcin that you are evaluating in order to move to the new timestep. This vector not 1768efe9872eSLisandro Dalcin changed until the solution at the next timestep has been calculated. 1769efe9872eSLisandro Dalcin 1770efe9872eSLisandro Dalcin Not Collective, but Vec returned is parallel if TS is parallel 1771efe9872eSLisandro Dalcin 1772efe9872eSLisandro Dalcin Input Parameter: 1773efe9872eSLisandro Dalcin . ts - the TS context obtained from TSCreate() 1774efe9872eSLisandro Dalcin 1775efe9872eSLisandro Dalcin Output Parameter: 1776efe9872eSLisandro Dalcin + u - the vector containing the solution 1777efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1778efe9872eSLisandro Dalcin 1779efe9872eSLisandro Dalcin Level: intermediate 1780efe9872eSLisandro Dalcin 1781efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime() 1782efe9872eSLisandro Dalcin 1783efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution 1784efe9872eSLisandro Dalcin @*/ 1785efe9872eSLisandro Dalcin PetscErrorCode TS2GetSolution(TS ts,Vec *u,Vec *v) 1786efe9872eSLisandro Dalcin { 1787efe9872eSLisandro Dalcin PetscFunctionBegin; 1788efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1789efe9872eSLisandro Dalcin if (u) PetscValidPointer(u,2); 1790efe9872eSLisandro Dalcin if (v) PetscValidPointer(v,3); 1791efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1792efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 1793efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1794efe9872eSLisandro Dalcin } 1795efe9872eSLisandro Dalcin 179655849f57SBarry Smith /*@C 179755849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 179855849f57SBarry Smith 179955849f57SBarry Smith Collective on PetscViewer 180055849f57SBarry Smith 180155849f57SBarry Smith Input Parameters: 180255849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 180355849f57SBarry Smith some related function before a call to TSLoad(). 180455849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 180555849f57SBarry Smith 180655849f57SBarry Smith Level: intermediate 180755849f57SBarry Smith 180855849f57SBarry Smith Notes: 180955849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 181055849f57SBarry Smith 181155849f57SBarry Smith Notes for advanced users: 181255849f57SBarry Smith Most users should not need to know the details of the binary storage 181355849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 181455849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 181555849f57SBarry Smith format is 181655849f57SBarry Smith .vb 181755849f57SBarry Smith has not yet been determined 181855849f57SBarry Smith .ve 181955849f57SBarry Smith 182055849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 182155849f57SBarry Smith @*/ 1822f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 182355849f57SBarry Smith { 182455849f57SBarry Smith PetscErrorCode ierr; 182555849f57SBarry Smith PetscBool isbinary; 182655849f57SBarry Smith PetscInt classid; 182755849f57SBarry Smith char type[256]; 18282d53ad75SBarry Smith DMTS sdm; 1829ad6bc421SBarry Smith DM dm; 183055849f57SBarry Smith 183155849f57SBarry Smith PetscFunctionBegin; 1832f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 183355849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 183455849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 183555849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 183655849f57SBarry Smith 1837060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1838ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1839060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1840f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1841f2c2a1b9SBarry Smith if (ts->ops->load) { 1842f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1843f2c2a1b9SBarry Smith } 1844ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1845ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1846ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1847f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1848f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 18492d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 18502d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 185155849f57SBarry Smith PetscFunctionReturn(0); 185255849f57SBarry Smith } 185355849f57SBarry Smith 18549804daf3SBarry Smith #include <petscdraw.h> 1855e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1856e04113cfSBarry Smith #include <petscviewersaws.h> 1857f05ece33SBarry Smith #endif 18587e2c5f70SBarry Smith /*@C 1859d763cef2SBarry Smith TSView - Prints the TS data structure. 1860d763cef2SBarry Smith 18614c49b128SBarry Smith Collective on TS 1862d763cef2SBarry Smith 1863d763cef2SBarry Smith Input Parameters: 1864d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1865d763cef2SBarry Smith - viewer - visualization context 1866d763cef2SBarry Smith 1867d763cef2SBarry Smith Options Database Key: 1868d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1869d763cef2SBarry Smith 1870d763cef2SBarry Smith Notes: 1871d763cef2SBarry Smith The available visualization contexts include 1872b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1873b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1874d763cef2SBarry Smith output where only the first processor opens 1875d763cef2SBarry Smith the file. All other processors send their 1876d763cef2SBarry Smith data to the first processor to print. 1877d763cef2SBarry Smith 1878d763cef2SBarry Smith The user can open an alternative visualization context with 1879b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1880d763cef2SBarry Smith 1881d763cef2SBarry Smith Level: beginner 1882d763cef2SBarry Smith 1883d763cef2SBarry Smith .keywords: TS, timestep, view 1884d763cef2SBarry Smith 1885b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1886d763cef2SBarry Smith @*/ 18877087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1888d763cef2SBarry Smith { 1889dfbe8321SBarry Smith PetscErrorCode ierr; 189019fd82e9SBarry Smith TSType type; 18912b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 18922d53ad75SBarry Smith DMTS sdm; 1893e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1894536b137fSBarry Smith PetscBool issaws; 1895f05ece33SBarry Smith #endif 1896d763cef2SBarry Smith 1897d763cef2SBarry Smith PetscFunctionBegin; 18980700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18993050cee2SBarry Smith if (!viewer) { 1900ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 19013050cee2SBarry Smith } 19020700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1903c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1904fd16b177SBarry Smith 1905251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1906251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 190755849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 19082b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1909e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1910536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1911f05ece33SBarry Smith #endif 191232077d6dSBarry Smith if (iascii) { 1913dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 1914efd4aadfSBarry Smith if (ts->ops->view) { 1915efd4aadfSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1916efd4aadfSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1917efd4aadfSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1918efd4aadfSBarry Smith } 1919ef85077eSLisandro Dalcin if (ts->max_steps < PETSC_MAX_INT) { 192077431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 1921ef85077eSLisandro Dalcin } 1922ef85077eSLisandro Dalcin if (ts->max_time < PETSC_MAX_REAL) { 19237c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1924ef85077eSLisandro Dalcin } 1925efd4aadfSBarry Smith if (ts->usessnes) { 1926efd4aadfSBarry Smith PetscBool lin; 1927d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 19285ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1929d763cef2SBarry Smith } 19305ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1931efd4aadfSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)ts->snes,SNESKSPONLY,&lin);CHKERRQ(ierr); 1932efd4aadfSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr); 1933efd4aadfSBarry Smith } 1934193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 1935a0af407cSBarry Smith if (ts->vrtol) { 1936a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of relative error tolerances, ");CHKERRQ(ierr); 1937a0af407cSBarry Smith } else { 1938a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr); 1939a0af407cSBarry Smith } 1940a0af407cSBarry Smith if (ts->vatol) { 1941a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of absolute error tolerances\n");CHKERRQ(ierr); 1942a0af407cSBarry Smith } else { 1943a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr); 1944a0af407cSBarry Smith } 1945825ab935SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1946efd4aadfSBarry Smith ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr); 1947825ab935SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1948825ab935SBarry Smith if (ts->snes && ts->usessnes) { 1949825ab935SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1950825ab935SBarry Smith ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr); 1951825ab935SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1952825ab935SBarry Smith } 19532d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19542d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 19550f5bd95cSBarry Smith } else if (isstring) { 1956a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1957b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 195855849f57SBarry Smith } else if (isbinary) { 195955849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 196055849f57SBarry Smith MPI_Comm comm; 196155849f57SBarry Smith PetscMPIInt rank; 196255849f57SBarry Smith char type[256]; 196355849f57SBarry Smith 196455849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 196555849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 196655849f57SBarry Smith if (!rank) { 196755849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 196855849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 196955849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 197055849f57SBarry Smith } 197155849f57SBarry Smith if (ts->ops->view) { 197255849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 197355849f57SBarry Smith } 1974efd4aadfSBarry Smith if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);} 1975f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1976f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 19772d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19782d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 19792b0a91c0SBarry Smith } else if (isdraw) { 19802b0a91c0SBarry Smith PetscDraw draw; 19812b0a91c0SBarry Smith char str[36]; 198289fd9fafSBarry Smith PetscReal x,y,bottom,h; 19832b0a91c0SBarry Smith 19842b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 19852b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 19862b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 19872b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 198851fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 198989fd9fafSBarry Smith bottom = y - h; 19902b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 19912b0a91c0SBarry Smith if (ts->ops->view) { 19922b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 19932b0a91c0SBarry Smith } 1994efd4aadfSBarry Smith if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);} 1995efd4aadfSBarry Smith if (ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 19962b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1997e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1998536b137fSBarry Smith } else if (issaws) { 1999d45a07a7SBarry Smith PetscMPIInt rank; 20002657e9d9SBarry Smith const char *name; 20012657e9d9SBarry Smith 20022657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 2003d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 2004d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 2005d45a07a7SBarry Smith char dir[1024]; 2006d45a07a7SBarry Smith 2007e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 2008a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 20092657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 20102657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 20112657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 2012d763cef2SBarry Smith } 20130acecf5bSBarry Smith if (ts->ops->view) { 20140acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 20150acecf5bSBarry Smith } 2016f05ece33SBarry Smith #endif 2017f05ece33SBarry Smith } 2018f05ece33SBarry Smith 2019b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2020251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 2021b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2022d763cef2SBarry Smith PetscFunctionReturn(0); 2023d763cef2SBarry Smith } 2024d763cef2SBarry Smith 2025b07ff414SBarry Smith /*@ 2026d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2027d763cef2SBarry Smith the timesteppers. 2028d763cef2SBarry Smith 20293f9fe445SBarry Smith Logically Collective on TS 2030d763cef2SBarry Smith 2031d763cef2SBarry Smith Input Parameters: 2032d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2033d763cef2SBarry Smith - usrP - optional user context 2034d763cef2SBarry Smith 203595452b02SPatrick Sanan Fortran Notes: 203695452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 2037daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2038daf670e6SBarry Smith 2039d763cef2SBarry Smith Level: intermediate 2040d763cef2SBarry Smith 2041d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 2042d763cef2SBarry Smith 2043d763cef2SBarry Smith .seealso: TSGetApplicationContext() 2044d763cef2SBarry Smith @*/ 20457087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 2046d763cef2SBarry Smith { 2047d763cef2SBarry Smith PetscFunctionBegin; 20480700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2049d763cef2SBarry Smith ts->user = usrP; 2050d763cef2SBarry Smith PetscFunctionReturn(0); 2051d763cef2SBarry Smith } 2052d763cef2SBarry Smith 2053b07ff414SBarry Smith /*@ 2054d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2055d763cef2SBarry Smith timestepper. 2056d763cef2SBarry Smith 2057d763cef2SBarry Smith Not Collective 2058d763cef2SBarry Smith 2059d763cef2SBarry Smith Input Parameter: 2060d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2061d763cef2SBarry Smith 2062d763cef2SBarry Smith Output Parameter: 2063d763cef2SBarry Smith . usrP - user context 2064d763cef2SBarry Smith 206595452b02SPatrick Sanan Fortran Notes: 206695452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 2067daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2068daf670e6SBarry Smith 2069d763cef2SBarry Smith Level: intermediate 2070d763cef2SBarry Smith 2071d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 2072d763cef2SBarry Smith 2073d763cef2SBarry Smith .seealso: TSSetApplicationContext() 2074d763cef2SBarry Smith @*/ 2075e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 2076d763cef2SBarry Smith { 2077d763cef2SBarry Smith PetscFunctionBegin; 20780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2079e71120c6SJed Brown *(void**)usrP = ts->user; 2080d763cef2SBarry Smith PetscFunctionReturn(0); 2081d763cef2SBarry Smith } 2082d763cef2SBarry Smith 2083d763cef2SBarry Smith /*@ 208480275a0aSLisandro Dalcin TSGetStepNumber - Gets the number of steps completed. 2085d763cef2SBarry Smith 2086d763cef2SBarry Smith Not Collective 2087d763cef2SBarry Smith 2088d763cef2SBarry Smith Input Parameter: 2089d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2090d763cef2SBarry Smith 2091d763cef2SBarry Smith Output Parameter: 209280275a0aSLisandro Dalcin . steps - number of steps completed so far 2093d763cef2SBarry Smith 2094d763cef2SBarry Smith Level: intermediate 2095d763cef2SBarry Smith 2096d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 20979be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 2098d763cef2SBarry Smith @*/ 209980275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps) 2100d763cef2SBarry Smith { 2101d763cef2SBarry Smith PetscFunctionBegin; 21020700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 210380275a0aSLisandro Dalcin PetscValidIntPointer(steps,2); 210480275a0aSLisandro Dalcin *steps = ts->steps; 210580275a0aSLisandro Dalcin PetscFunctionReturn(0); 210680275a0aSLisandro Dalcin } 210780275a0aSLisandro Dalcin 210880275a0aSLisandro Dalcin /*@ 210980275a0aSLisandro Dalcin TSSetStepNumber - Sets the number of steps completed. 211080275a0aSLisandro Dalcin 211180275a0aSLisandro Dalcin Logically Collective on TS 211280275a0aSLisandro Dalcin 211380275a0aSLisandro Dalcin Input Parameters: 211480275a0aSLisandro Dalcin + ts - the TS context 211580275a0aSLisandro Dalcin - steps - number of steps completed so far 211680275a0aSLisandro Dalcin 211780275a0aSLisandro Dalcin Notes: 211880275a0aSLisandro Dalcin For most uses of the TS solvers the user need not explicitly call 211980275a0aSLisandro Dalcin TSSetStepNumber(), as the step counter is appropriately updated in 212080275a0aSLisandro Dalcin TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to 212180275a0aSLisandro Dalcin reinitialize timestepping by setting the step counter to zero (and time 212280275a0aSLisandro Dalcin to the initial time) to solve a similar problem with different initial 212380275a0aSLisandro Dalcin conditions or parameters. Other possible use case is to continue 212480275a0aSLisandro Dalcin timestepping from a previously interrupted run in such a way that TS 212580275a0aSLisandro Dalcin monitors will be called with a initial nonzero step counter. 212680275a0aSLisandro Dalcin 212780275a0aSLisandro Dalcin Level: advanced 212880275a0aSLisandro Dalcin 212980275a0aSLisandro Dalcin .keywords: TS, timestep, set, iteration, number 213080275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution() 213180275a0aSLisandro Dalcin @*/ 213280275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps) 213380275a0aSLisandro Dalcin { 213480275a0aSLisandro Dalcin PetscFunctionBegin; 213580275a0aSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 213680275a0aSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,steps,2); 213780275a0aSLisandro Dalcin if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative"); 213880275a0aSLisandro Dalcin ts->steps = steps; 2139d763cef2SBarry Smith PetscFunctionReturn(0); 2140d763cef2SBarry Smith } 2141d763cef2SBarry Smith 2142d763cef2SBarry Smith /*@ 2143d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2144d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2145d763cef2SBarry Smith 21463f9fe445SBarry Smith Logically Collective on TS 2147d763cef2SBarry Smith 2148d763cef2SBarry Smith Input Parameters: 2149d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2150d763cef2SBarry Smith - time_step - the size of the timestep 2151d763cef2SBarry Smith 2152d763cef2SBarry Smith Level: intermediate 2153d763cef2SBarry Smith 2154aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime() 2155d763cef2SBarry Smith 2156d763cef2SBarry Smith .keywords: TS, set, timestep 2157d763cef2SBarry Smith @*/ 21587087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 2159d763cef2SBarry Smith { 2160d763cef2SBarry Smith PetscFunctionBegin; 21610700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2162c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 2163d763cef2SBarry Smith ts->time_step = time_step; 2164d763cef2SBarry Smith PetscFunctionReturn(0); 2165d763cef2SBarry Smith } 2166d763cef2SBarry Smith 2167a43b19c4SJed Brown /*@ 216849354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 216949354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 217049354f04SShri Abhyankar or just return at the final time TS computed. 2171a43b19c4SJed Brown 2172a43b19c4SJed Brown Logically Collective on TS 2173a43b19c4SJed Brown 2174a43b19c4SJed Brown Input Parameter: 2175a43b19c4SJed Brown + ts - the time-step context 217649354f04SShri Abhyankar - eftopt - exact final time option 2177a43b19c4SJed Brown 2178feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2179feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2180feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2181feed9e9dSBarry Smith 2182feed9e9dSBarry Smith Options Database: 2183feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2184feed9e9dSBarry Smith 2185ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 2186ee346746SBarry Smith then the final time you selected. 2187ee346746SBarry Smith 2188a43b19c4SJed Brown Level: beginner 2189a43b19c4SJed Brown 2190f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime() 2191a43b19c4SJed Brown @*/ 219249354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 2193a43b19c4SJed Brown { 2194a43b19c4SJed Brown PetscFunctionBegin; 2195a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 219649354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 219749354f04SShri Abhyankar ts->exact_final_time = eftopt; 2198a43b19c4SJed Brown PetscFunctionReturn(0); 2199a43b19c4SJed Brown } 2200a43b19c4SJed Brown 2201d763cef2SBarry Smith /*@ 2202f6953c82SLisandro Dalcin TSGetExactFinalTime - Gets the exact final time option. 2203f6953c82SLisandro Dalcin 2204f6953c82SLisandro Dalcin Not Collective 2205f6953c82SLisandro Dalcin 2206f6953c82SLisandro Dalcin Input Parameter: 2207f6953c82SLisandro Dalcin . ts - the TS context 2208f6953c82SLisandro Dalcin 2209f6953c82SLisandro Dalcin Output Parameter: 2210f6953c82SLisandro Dalcin . eftopt - exact final time option 2211f6953c82SLisandro Dalcin 2212f6953c82SLisandro Dalcin Level: beginner 2213f6953c82SLisandro Dalcin 2214f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime() 2215f6953c82SLisandro Dalcin @*/ 2216f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt) 2217f6953c82SLisandro Dalcin { 2218f6953c82SLisandro Dalcin PetscFunctionBegin; 2219f6953c82SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2220f6953c82SLisandro Dalcin PetscValidPointer(eftopt,2); 2221f6953c82SLisandro Dalcin *eftopt = ts->exact_final_time; 2222f6953c82SLisandro Dalcin PetscFunctionReturn(0); 2223f6953c82SLisandro Dalcin } 2224f6953c82SLisandro Dalcin 2225f6953c82SLisandro Dalcin /*@ 2226d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2227d763cef2SBarry Smith 2228d763cef2SBarry Smith Not Collective 2229d763cef2SBarry Smith 2230d763cef2SBarry Smith Input Parameter: 2231d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2232d763cef2SBarry Smith 2233d763cef2SBarry Smith Output Parameter: 2234d763cef2SBarry Smith . dt - the current timestep size 2235d763cef2SBarry Smith 2236d763cef2SBarry Smith Level: intermediate 2237d763cef2SBarry Smith 2238aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime() 2239d763cef2SBarry Smith 2240d763cef2SBarry Smith .keywords: TS, get, timestep 2241d763cef2SBarry Smith @*/ 22427087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 2243d763cef2SBarry Smith { 2244d763cef2SBarry Smith PetscFunctionBegin; 22450700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2246f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 2247d763cef2SBarry Smith *dt = ts->time_step; 2248d763cef2SBarry Smith PetscFunctionReturn(0); 2249d763cef2SBarry Smith } 2250d763cef2SBarry Smith 2251d8e5e3e6SSatish Balay /*@ 2252d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2253d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2254d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2255d763cef2SBarry Smith the solution at the next timestep has been calculated. 2256d763cef2SBarry Smith 2257d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 2258d763cef2SBarry Smith 2259d763cef2SBarry Smith Input Parameter: 2260d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2261d763cef2SBarry Smith 2262d763cef2SBarry Smith Output Parameter: 2263d763cef2SBarry Smith . v - the vector containing the solution 2264d763cef2SBarry Smith 226563e21af5SBarry Smith Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested 226663e21af5SBarry Smith final time. It returns the solution at the next timestep. 226763e21af5SBarry Smith 2268d763cef2SBarry Smith Level: intermediate 2269d763cef2SBarry Smith 22700ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction() 2271d763cef2SBarry Smith 2272d763cef2SBarry Smith .keywords: TS, timestep, get, solution 2273d763cef2SBarry Smith @*/ 22747087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 2275d763cef2SBarry Smith { 2276d763cef2SBarry Smith PetscFunctionBegin; 22770700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22784482741eSBarry Smith PetscValidPointer(v,2); 22798737fe31SLisandro Dalcin *v = ts->vec_sol; 2280d763cef2SBarry Smith PetscFunctionReturn(0); 2281d763cef2SBarry Smith } 2282d763cef2SBarry Smith 228303fe5f5eSDebojyoti Ghosh /*@ 2284b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 228503fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2286b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 228703fe5f5eSDebojyoti Ghosh structure as the solution vector. 228803fe5f5eSDebojyoti Ghosh 228903fe5f5eSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 229003fe5f5eSDebojyoti Ghosh 229103fe5f5eSDebojyoti Ghosh Parameters : 229203fe5f5eSDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 2293b2bf4f3aSDebojyoti Ghosh . n - If v is PETSC_NULL, then the number of solution components is 2294b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 229503fe5f5eSDebojyoti Ghosh returned in v. 2296b2bf4f3aSDebojyoti Ghosh . v - the vector containing the n-th solution component 229703fe5f5eSDebojyoti Ghosh (may be PETSC_NULL to use this function to find out 2298b2bf4f3aSDebojyoti Ghosh the number of solutions components). 229903fe5f5eSDebojyoti Ghosh 23004cdd57e5SDebojyoti Ghosh Level: advanced 230103fe5f5eSDebojyoti Ghosh 230203fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution() 230303fe5f5eSDebojyoti Ghosh 230403fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution 230503fe5f5eSDebojyoti Ghosh @*/ 2306b2bf4f3aSDebojyoti Ghosh PetscErrorCode TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v) 230703fe5f5eSDebojyoti Ghosh { 230803fe5f5eSDebojyoti Ghosh PetscErrorCode ierr; 230903fe5f5eSDebojyoti Ghosh 231003fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 231103fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2312b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 231303fe5f5eSDebojyoti Ghosh else { 2314b2bf4f3aSDebojyoti Ghosh ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr); 231503fe5f5eSDebojyoti Ghosh } 231603fe5f5eSDebojyoti Ghosh PetscFunctionReturn(0); 231703fe5f5eSDebojyoti Ghosh } 231803fe5f5eSDebojyoti Ghosh 23194cdd57e5SDebojyoti Ghosh /*@ 23204cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 23214cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 23224cdd57e5SDebojyoti Ghosh 23234cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 23244cdd57e5SDebojyoti Ghosh 23254cdd57e5SDebojyoti Ghosh Parameters : 23264cdd57e5SDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 23274cdd57e5SDebojyoti Ghosh . v - the vector containing the auxiliary solution 23284cdd57e5SDebojyoti Ghosh 23294cdd57e5SDebojyoti Ghosh Level: intermediate 23304cdd57e5SDebojyoti Ghosh 23314cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution() 23324cdd57e5SDebojyoti Ghosh 23334cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution 23344cdd57e5SDebojyoti Ghosh @*/ 23354cdd57e5SDebojyoti Ghosh PetscErrorCode TSGetAuxSolution(TS ts,Vec *v) 23364cdd57e5SDebojyoti Ghosh { 23374cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 23384cdd57e5SDebojyoti Ghosh 23394cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23404cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2341f6356ec7SDebojyoti Ghosh if (ts->ops->getauxsolution) { 23424cdd57e5SDebojyoti Ghosh ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr); 2343f6356ec7SDebojyoti Ghosh } else { 2344f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v); CHKERRQ(ierr); 2345f6356ec7SDebojyoti Ghosh } 23464cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 23474cdd57e5SDebojyoti Ghosh } 23484cdd57e5SDebojyoti Ghosh 23494cdd57e5SDebojyoti Ghosh /*@ 23504cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 23514cdd57e5SDebojyoti Ghosh TSType has an error estimation functionality. 23524cdd57e5SDebojyoti Ghosh 23534cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 23544cdd57e5SDebojyoti Ghosh 23559657682dSDebojyoti Ghosh Note: MUST call after TSSetUp() 23569657682dSDebojyoti Ghosh 23574cdd57e5SDebojyoti Ghosh Parameters : 23584cdd57e5SDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 2359657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 23604cdd57e5SDebojyoti Ghosh . v - the vector containing the error (same size as the solution). 23614cdd57e5SDebojyoti Ghosh 23624cdd57e5SDebojyoti Ghosh Level: intermediate 23634cdd57e5SDebojyoti Ghosh 236457df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError() 23654cdd57e5SDebojyoti Ghosh 23664cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error 23674cdd57e5SDebojyoti Ghosh @*/ 23680a01e1b2SEmil Constantinescu PetscErrorCode TSGetTimeError(TS ts,PetscInt n,Vec *v) 23694cdd57e5SDebojyoti Ghosh { 23704cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 23714cdd57e5SDebojyoti Ghosh 23724cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23734cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2374f6356ec7SDebojyoti Ghosh if (ts->ops->gettimeerror) { 23750a01e1b2SEmil Constantinescu ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr); 2376f6356ec7SDebojyoti Ghosh } else { 2377f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v);CHKERRQ(ierr); 2378f6356ec7SDebojyoti Ghosh } 23794cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 23804cdd57e5SDebojyoti Ghosh } 23814cdd57e5SDebojyoti Ghosh 238257df6a1bSDebojyoti Ghosh /*@ 238357df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 238457df6a1bSDebojyoti Ghosh TSType has an error estimation functionality. This can be used 238557df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 238657df6a1bSDebojyoti Ghosh 238757df6a1bSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 238857df6a1bSDebojyoti Ghosh 238957df6a1bSDebojyoti Ghosh Parameters : 239057df6a1bSDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 239157df6a1bSDebojyoti Ghosh . v - the vector containing the error (same size as the solution). 239257df6a1bSDebojyoti Ghosh 239357df6a1bSDebojyoti Ghosh Level: intermediate 239457df6a1bSDebojyoti Ghosh 239557df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError) 239657df6a1bSDebojyoti Ghosh 239757df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error 239857df6a1bSDebojyoti Ghosh @*/ 239957df6a1bSDebojyoti Ghosh PetscErrorCode TSSetTimeError(TS ts,Vec v) 240057df6a1bSDebojyoti Ghosh { 240157df6a1bSDebojyoti Ghosh PetscErrorCode ierr; 240257df6a1bSDebojyoti Ghosh 240357df6a1bSDebojyoti Ghosh PetscFunctionBegin; 240457df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 24059657682dSDebojyoti Ghosh if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first"); 240657df6a1bSDebojyoti Ghosh if (ts->ops->settimeerror) { 240757df6a1bSDebojyoti Ghosh ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr); 240857df6a1bSDebojyoti Ghosh } 240957df6a1bSDebojyoti Ghosh PetscFunctionReturn(0); 241057df6a1bSDebojyoti Ghosh } 241157df6a1bSDebojyoti Ghosh 2412bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2413d8e5e3e6SSatish Balay /*@ 2414bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2415d763cef2SBarry Smith 2416bdad233fSMatthew Knepley Not collective 2417d763cef2SBarry Smith 2418bdad233fSMatthew Knepley Input Parameters: 2419bdad233fSMatthew Knepley + ts - The TS 2420bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2421d763cef2SBarry Smith .vb 24220910c330SBarry Smith U_t - A U = 0 (linear) 24230910c330SBarry Smith U_t - A(t) U = 0 (linear) 24240910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2425d763cef2SBarry Smith .ve 2426d763cef2SBarry Smith 2427d763cef2SBarry Smith Level: beginner 2428d763cef2SBarry Smith 2429bdad233fSMatthew Knepley .keywords: TS, problem type 2430bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2431d763cef2SBarry Smith @*/ 24327087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2433a7cc72afSBarry Smith { 24349e2a6581SJed Brown PetscErrorCode ierr; 24359e2a6581SJed Brown 2436d763cef2SBarry Smith PetscFunctionBegin; 24370700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2438bdad233fSMatthew Knepley ts->problem_type = type; 24399e2a6581SJed Brown if (type == TS_LINEAR) { 24409e2a6581SJed Brown SNES snes; 24419e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 24429e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 24439e2a6581SJed Brown } 2444d763cef2SBarry Smith PetscFunctionReturn(0); 2445d763cef2SBarry Smith } 2446d763cef2SBarry Smith 2447bdad233fSMatthew Knepley /*@C 2448bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2449bdad233fSMatthew Knepley 2450bdad233fSMatthew Knepley Not collective 2451bdad233fSMatthew Knepley 2452bdad233fSMatthew Knepley Input Parameter: 2453bdad233fSMatthew Knepley . ts - The TS 2454bdad233fSMatthew Knepley 2455bdad233fSMatthew Knepley Output Parameter: 2456bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2457bdad233fSMatthew Knepley .vb 2458089b2837SJed Brown M U_t = A U 2459089b2837SJed Brown M(t) U_t = A(t) U 2460b5abc632SBarry Smith F(t,U,U_t) 2461bdad233fSMatthew Knepley .ve 2462bdad233fSMatthew Knepley 2463bdad233fSMatthew Knepley Level: beginner 2464bdad233fSMatthew Knepley 2465bdad233fSMatthew Knepley .keywords: TS, problem type 2466bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2467bdad233fSMatthew Knepley @*/ 24687087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2469a7cc72afSBarry Smith { 2470bdad233fSMatthew Knepley PetscFunctionBegin; 24710700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 24724482741eSBarry Smith PetscValidIntPointer(type,2); 2473bdad233fSMatthew Knepley *type = ts->problem_type; 2474bdad233fSMatthew Knepley PetscFunctionReturn(0); 2475bdad233fSMatthew Knepley } 2476d763cef2SBarry Smith 2477d763cef2SBarry Smith /*@ 2478d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 2479d763cef2SBarry Smith of a timestepper. 2480d763cef2SBarry Smith 2481d763cef2SBarry Smith Collective on TS 2482d763cef2SBarry Smith 2483d763cef2SBarry Smith Input Parameter: 2484d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2485d763cef2SBarry Smith 2486d763cef2SBarry Smith Notes: 2487d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 2488d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 2489141bd67dSStefano Zampini the call to TSStep() or TSSolve(). However, if one wishes to control this 2490d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 2491141bd67dSStefano Zampini and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve(). 2492d763cef2SBarry Smith 2493d763cef2SBarry Smith Level: advanced 2494d763cef2SBarry Smith 2495d763cef2SBarry Smith .keywords: TS, timestep, setup 2496d763cef2SBarry Smith 2497141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve() 2498d763cef2SBarry Smith @*/ 24997087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 2500d763cef2SBarry Smith { 2501dfbe8321SBarry Smith PetscErrorCode ierr; 25026c6b9e74SPeter Brune DM dm; 25036c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 2504d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 2505cd11d68dSLisandro Dalcin TSIFunction ifun; 25066c6b9e74SPeter Brune TSIJacobian ijac; 2507efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 25086c6b9e74SPeter Brune TSRHSJacobian rhsjac; 25092ffb9264SLisandro Dalcin PetscBool isnone; 2510d763cef2SBarry Smith 2511d763cef2SBarry Smith PetscFunctionBegin; 25120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2513277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 2514277b19d0SLisandro Dalcin 25157adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 2516cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 2517cd11d68dSLisandro Dalcin ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr); 2518d763cef2SBarry Smith } 2519277b19d0SLisandro Dalcin 2520277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 2521277b19d0SLisandro Dalcin 2522e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 2523e1244c69SJed Brown Mat Amat,Pmat; 2524e1244c69SJed Brown SNES snes; 2525e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2526e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 2527e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2528e1244c69SJed Brown * have displaced the RHS matrix */ 2529e1244c69SJed Brown if (Amat == ts->Arhs) { 2530abc0d4abSBarry 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 */ 2531abc0d4abSBarry Smith ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr); 2532e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 2533e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 2534e1244c69SJed Brown } 2535e1244c69SJed Brown if (Pmat == ts->Brhs) { 2536abc0d4abSBarry Smith ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 2537e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 2538e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 2539e1244c69SJed Brown } 2540e1244c69SJed Brown } 25412ffb9264SLisandro Dalcin 25422ffb9264SLisandro Dalcin ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 25432ffb9264SLisandro Dalcin ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr); 25442ffb9264SLisandro Dalcin 2545277b19d0SLisandro Dalcin if (ts->ops->setup) { 2546000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 2547277b19d0SLisandro Dalcin } 2548277b19d0SLisandro Dalcin 25492ffb9264SLisandro Dalcin /* Attempt to check/preset a default value for the exact final time option */ 25502ffb9264SLisandro Dalcin ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr); 25512ffb9264SLisandro Dalcin if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) 25522ffb9264SLisandro Dalcin ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP; 25532ffb9264SLisandro Dalcin 2554a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 25556c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 25566c6b9e74SPeter Brune */ 25576c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 25580298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 25596c6b9e74SPeter Brune if (!func) { 25606c6b9e74SPeter Brune ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 25616c6b9e74SPeter Brune } 2562a6772fa2SLisandro 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. 25636c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 25646c6b9e74SPeter Brune */ 25650298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 25660298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 2567efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr); 25680298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 2569efe9872eSLisandro Dalcin if (!jac && (ijac || i2jac || rhsjac)) { 25706c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 25716c6b9e74SPeter Brune } 2572c0517034SDebojyoti Ghosh 2573c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 2574c0517034SDebojyoti Ghosh if (ts->ops->startingmethod) { 2575c0517034SDebojyoti Ghosh ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr); 2576c0517034SDebojyoti Ghosh } 2577c0517034SDebojyoti Ghosh 2578277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2579277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2580277b19d0SLisandro Dalcin } 2581277b19d0SLisandro Dalcin 2582f6a906c0SBarry Smith /*@ 2583277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2584277b19d0SLisandro Dalcin 2585277b19d0SLisandro Dalcin Collective on TS 2586277b19d0SLisandro Dalcin 2587277b19d0SLisandro Dalcin Input Parameter: 2588277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2589277b19d0SLisandro Dalcin 2590277b19d0SLisandro Dalcin Level: beginner 2591277b19d0SLisandro Dalcin 2592277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 2593277b19d0SLisandro Dalcin 2594277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2595277b19d0SLisandro Dalcin @*/ 2596277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2597277b19d0SLisandro Dalcin { 25981d06f6b3SHong Zhang TS_RHSSplitLink ilink = ts->tsrhssplit,next; 2599277b19d0SLisandro Dalcin PetscErrorCode ierr; 2600277b19d0SLisandro Dalcin 2601277b19d0SLisandro Dalcin PetscFunctionBegin; 2602277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2603b18ea86cSHong Zhang 2604277b19d0SLisandro Dalcin if (ts->ops->reset) { 2605277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2606277b19d0SLisandro Dalcin } 2607277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2608e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2609bbd56ea5SKarl Rupp 26104e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 26114e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2612214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 26136bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2614efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 2615e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2616e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 261738637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2618bbd56ea5SKarl Rupp 2619d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2620d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2621715f1b00SHong Zhang 2622ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 26232c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 262436eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 262513b1b0ffSHong Zhang ierr = MatDestroy(&ts->mat_sensip);CHKERRQ(ierr); 2626022c081aSHong Zhang 26271d06f6b3SHong Zhang while (ilink) { 26281d06f6b3SHong Zhang next = ilink->next; 26291d06f6b3SHong Zhang ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr); 26301d06f6b3SHong Zhang ierr = PetscFree(ilink->splitname);CHKERRQ(ierr); 26311d06f6b3SHong Zhang ierr = ISDestroy(&ilink->is);CHKERRQ(ierr); 26321d06f6b3SHong Zhang ierr = PetscFree(ilink);CHKERRQ(ierr); 26331d06f6b3SHong Zhang ilink = next; 263487f4e208SHong Zhang } 2635545aaa6fSHong Zhang ts->num_rhs_splits = 0; 2636277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2637d763cef2SBarry Smith PetscFunctionReturn(0); 2638d763cef2SBarry Smith } 2639d763cef2SBarry Smith 2640d8e5e3e6SSatish Balay /*@ 2641d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2642d763cef2SBarry Smith with TSCreate(). 2643d763cef2SBarry Smith 2644d763cef2SBarry Smith Collective on TS 2645d763cef2SBarry Smith 2646d763cef2SBarry Smith Input Parameter: 2647d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2648d763cef2SBarry Smith 2649d763cef2SBarry Smith Level: beginner 2650d763cef2SBarry Smith 2651d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2652d763cef2SBarry Smith 2653d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2654d763cef2SBarry Smith @*/ 26556bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2656d763cef2SBarry Smith { 26576849ba73SBarry Smith PetscErrorCode ierr; 2658d763cef2SBarry Smith 2659d763cef2SBarry Smith PetscFunctionBegin; 26606bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 26616bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 26626bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2663d763cef2SBarry Smith 26646bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2665277b19d0SLisandro Dalcin 2666e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2667e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 26686bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 26696d4c513bSLisandro Dalcin 2670bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2671bc952696SBarry Smith 267284df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 26736427ac75SLisandro Dalcin ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr); 26746427ac75SLisandro Dalcin 26756bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 26766bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 26776bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 26780dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 26796d4c513bSLisandro Dalcin 2680a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2681d763cef2SBarry Smith PetscFunctionReturn(0); 2682d763cef2SBarry Smith } 2683d763cef2SBarry Smith 2684d8e5e3e6SSatish Balay /*@ 2685d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2686d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2687d763cef2SBarry Smith 2688d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2689d763cef2SBarry Smith 2690d763cef2SBarry Smith Input Parameter: 2691d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2692d763cef2SBarry Smith 2693d763cef2SBarry Smith Output Parameter: 2694d763cef2SBarry Smith . snes - the nonlinear solver context 2695d763cef2SBarry Smith 2696d763cef2SBarry Smith Notes: 2697d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2698d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 269994b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2700d763cef2SBarry Smith 2701d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 27020298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2703d763cef2SBarry Smith 2704d763cef2SBarry Smith Level: beginner 2705d763cef2SBarry Smith 2706d763cef2SBarry Smith .keywords: timestep, get, SNES 2707d763cef2SBarry Smith @*/ 27087087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2709d763cef2SBarry Smith { 2710d372ba47SLisandro Dalcin PetscErrorCode ierr; 2711d372ba47SLisandro Dalcin 2712d763cef2SBarry Smith PetscFunctionBegin; 27130700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27144482741eSBarry Smith PetscValidPointer(snes,2); 2715d372ba47SLisandro Dalcin if (!ts->snes) { 2716ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 27170298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 27183bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2719d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2720496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 27219e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 27229e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 27239e2a6581SJed Brown } 2724d372ba47SLisandro Dalcin } 2725d763cef2SBarry Smith *snes = ts->snes; 2726d763cef2SBarry Smith PetscFunctionReturn(0); 2727d763cef2SBarry Smith } 2728d763cef2SBarry Smith 2729deb2cd25SJed Brown /*@ 2730deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2731deb2cd25SJed Brown 2732deb2cd25SJed Brown Collective 2733deb2cd25SJed Brown 2734deb2cd25SJed Brown Input Parameter: 2735deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2736deb2cd25SJed Brown - snes - the nonlinear solver context 2737deb2cd25SJed Brown 2738deb2cd25SJed Brown Notes: 2739deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2740deb2cd25SJed Brown 2741deb2cd25SJed Brown Level: developer 2742deb2cd25SJed Brown 2743deb2cd25SJed Brown .keywords: timestep, set, SNES 2744deb2cd25SJed Brown @*/ 2745deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2746deb2cd25SJed Brown { 2747deb2cd25SJed Brown PetscErrorCode ierr; 2748d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2749deb2cd25SJed Brown 2750deb2cd25SJed Brown PetscFunctionBegin; 2751deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2752deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2753deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2754deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2755bbd56ea5SKarl Rupp 2756deb2cd25SJed Brown ts->snes = snes; 2757bbd56ea5SKarl Rupp 27580298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 27590298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2760740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 27610298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2762740132f1SEmil Constantinescu } 2763deb2cd25SJed Brown PetscFunctionReturn(0); 2764deb2cd25SJed Brown } 2765deb2cd25SJed Brown 2766d8e5e3e6SSatish Balay /*@ 276794b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2768d763cef2SBarry Smith a TS (timestepper) context. 2769d763cef2SBarry Smith 277094b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2771d763cef2SBarry Smith 2772d763cef2SBarry Smith Input Parameter: 2773d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2774d763cef2SBarry Smith 2775d763cef2SBarry Smith Output Parameter: 277694b7f48cSBarry Smith . ksp - the nonlinear solver context 2777d763cef2SBarry Smith 2778d763cef2SBarry Smith Notes: 277994b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2780d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2781d763cef2SBarry Smith KSP and PC contexts as well. 2782d763cef2SBarry Smith 278394b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 27840298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2785d763cef2SBarry Smith 2786d763cef2SBarry Smith Level: beginner 2787d763cef2SBarry Smith 278894b7f48cSBarry Smith .keywords: timestep, get, KSP 2789d763cef2SBarry Smith @*/ 27907087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2791d763cef2SBarry Smith { 2792d372ba47SLisandro Dalcin PetscErrorCode ierr; 2793089b2837SJed Brown SNES snes; 2794d372ba47SLisandro Dalcin 2795d763cef2SBarry Smith PetscFunctionBegin; 27960700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27974482741eSBarry Smith PetscValidPointer(ksp,2); 279817186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2799e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2800089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2801089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2802d763cef2SBarry Smith PetscFunctionReturn(0); 2803d763cef2SBarry Smith } 2804d763cef2SBarry Smith 2805d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2806d763cef2SBarry Smith 2807adb62b0dSMatthew Knepley /*@ 2808618ce8baSLisandro Dalcin TSSetMaxSteps - Sets the maximum number of steps to use. 2809618ce8baSLisandro Dalcin 2810618ce8baSLisandro Dalcin Logically Collective on TS 2811618ce8baSLisandro Dalcin 2812618ce8baSLisandro Dalcin Input Parameters: 2813618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 2814618ce8baSLisandro Dalcin - maxsteps - maximum number of steps to use 2815618ce8baSLisandro Dalcin 2816618ce8baSLisandro Dalcin Options Database Keys: 2817618ce8baSLisandro Dalcin . -ts_max_steps <maxsteps> - Sets maxsteps 2818618ce8baSLisandro Dalcin 2819618ce8baSLisandro Dalcin Notes: 2820618ce8baSLisandro Dalcin The default maximum number of steps is 5000 2821618ce8baSLisandro Dalcin 2822618ce8baSLisandro Dalcin Level: intermediate 2823618ce8baSLisandro Dalcin 2824618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, steps 2825618ce8baSLisandro Dalcin 2826618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime() 2827618ce8baSLisandro Dalcin @*/ 2828618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps) 2829618ce8baSLisandro Dalcin { 2830618ce8baSLisandro Dalcin PetscFunctionBegin; 2831618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2832618ce8baSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2833618ce8baSLisandro Dalcin if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative"); 2834618ce8baSLisandro Dalcin ts->max_steps = maxsteps; 2835618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2836618ce8baSLisandro Dalcin } 2837618ce8baSLisandro Dalcin 2838618ce8baSLisandro Dalcin /*@ 2839618ce8baSLisandro Dalcin TSGetMaxSteps - Gets the maximum number of steps to use. 2840618ce8baSLisandro Dalcin 2841618ce8baSLisandro Dalcin Not Collective 2842618ce8baSLisandro Dalcin 2843618ce8baSLisandro Dalcin Input Parameters: 2844618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 2845618ce8baSLisandro Dalcin 2846618ce8baSLisandro Dalcin Output Parameter: 2847618ce8baSLisandro Dalcin . maxsteps - maximum number of steps to use 2848618ce8baSLisandro Dalcin 2849618ce8baSLisandro Dalcin Level: advanced 2850618ce8baSLisandro Dalcin 2851618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, steps 2852618ce8baSLisandro Dalcin 2853618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime() 2854618ce8baSLisandro Dalcin @*/ 2855618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps) 2856618ce8baSLisandro Dalcin { 2857618ce8baSLisandro Dalcin PetscFunctionBegin; 2858618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2859618ce8baSLisandro Dalcin PetscValidIntPointer(maxsteps,2); 2860618ce8baSLisandro Dalcin *maxsteps = ts->max_steps; 2861618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2862618ce8baSLisandro Dalcin } 2863618ce8baSLisandro Dalcin 2864618ce8baSLisandro Dalcin /*@ 2865618ce8baSLisandro Dalcin TSSetMaxTime - Sets the maximum (or final) time for timestepping. 2866618ce8baSLisandro Dalcin 2867618ce8baSLisandro Dalcin Logically Collective on TS 2868618ce8baSLisandro Dalcin 2869618ce8baSLisandro Dalcin Input Parameters: 2870618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 2871618ce8baSLisandro Dalcin - maxtime - final time to step to 2872618ce8baSLisandro Dalcin 2873618ce8baSLisandro Dalcin Options Database Keys: 2874ef85077eSLisandro Dalcin . -ts_max_time <maxtime> - Sets maxtime 2875618ce8baSLisandro Dalcin 2876618ce8baSLisandro Dalcin Notes: 2877618ce8baSLisandro Dalcin The default maximum time is 5.0 2878618ce8baSLisandro Dalcin 2879618ce8baSLisandro Dalcin Level: intermediate 2880618ce8baSLisandro Dalcin 2881618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, time 2882618ce8baSLisandro Dalcin 2883618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime() 2884618ce8baSLisandro Dalcin @*/ 2885618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime) 2886618ce8baSLisandro Dalcin { 2887618ce8baSLisandro Dalcin PetscFunctionBegin; 2888618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2889618ce8baSLisandro Dalcin PetscValidLogicalCollectiveReal(ts,maxtime,2); 2890618ce8baSLisandro Dalcin ts->max_time = maxtime; 2891618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2892618ce8baSLisandro Dalcin } 2893618ce8baSLisandro Dalcin 2894618ce8baSLisandro Dalcin /*@ 2895618ce8baSLisandro Dalcin TSGetMaxTime - Gets the maximum (or final) time for timestepping. 2896618ce8baSLisandro Dalcin 2897618ce8baSLisandro Dalcin Not Collective 2898618ce8baSLisandro Dalcin 2899618ce8baSLisandro Dalcin Input Parameters: 2900618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 2901618ce8baSLisandro Dalcin 2902618ce8baSLisandro Dalcin Output Parameter: 2903618ce8baSLisandro Dalcin . maxtime - final time to step to 2904618ce8baSLisandro Dalcin 2905618ce8baSLisandro Dalcin Level: advanced 2906618ce8baSLisandro Dalcin 2907618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, time 2908618ce8baSLisandro Dalcin 2909618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps() 2910618ce8baSLisandro Dalcin @*/ 2911618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime) 2912618ce8baSLisandro Dalcin { 2913618ce8baSLisandro Dalcin PetscFunctionBegin; 2914618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2915618ce8baSLisandro Dalcin PetscValidRealPointer(maxtime,2); 2916618ce8baSLisandro Dalcin *maxtime = ts->max_time; 2917618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2918618ce8baSLisandro Dalcin } 2919618ce8baSLisandro Dalcin 2920618ce8baSLisandro Dalcin /*@ 2921aaa6c58dSLisandro Dalcin TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep(). 2922edc382c3SSatish Balay 2923edc382c3SSatish Balay Level: deprecated 2924edc382c3SSatish Balay 2925aaa6c58dSLisandro Dalcin @*/ 2926aaa6c58dSLisandro Dalcin PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 2927aaa6c58dSLisandro Dalcin { 2928aaa6c58dSLisandro Dalcin PetscErrorCode ierr; 2929aaa6c58dSLisandro Dalcin PetscFunctionBegin; 2930aaa6c58dSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2931aaa6c58dSLisandro Dalcin ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 2932aaa6c58dSLisandro Dalcin ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 2933aaa6c58dSLisandro Dalcin PetscFunctionReturn(0); 2934aaa6c58dSLisandro Dalcin } 2935aaa6c58dSLisandro Dalcin 2936aaa6c58dSLisandro Dalcin /*@ 293719eac22cSLisandro Dalcin TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime(). 2938edc382c3SSatish Balay 2939edc382c3SSatish Balay Level: deprecated 2940edc382c3SSatish Balay 2941adb62b0dSMatthew Knepley @*/ 29427087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2943adb62b0dSMatthew Knepley { 2944adb62b0dSMatthew Knepley PetscFunctionBegin; 29450700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2946abc0a331SBarry Smith if (maxsteps) { 29474482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2948adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2949adb62b0dSMatthew Knepley } 2950abc0a331SBarry Smith if (maxtime) { 29514482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2952adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2953adb62b0dSMatthew Knepley } 2954adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2955adb62b0dSMatthew Knepley } 2956adb62b0dSMatthew Knepley 2957d763cef2SBarry Smith /*@ 295819eac22cSLisandro Dalcin TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime(). 2959edc382c3SSatish Balay 2960edc382c3SSatish Balay Level: deprecated 2961edc382c3SSatish Balay 2962d763cef2SBarry Smith @*/ 29637087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2964d763cef2SBarry Smith { 2965d763cef2SBarry Smith PetscFunctionBegin; 29660700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2967c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2968c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 296939b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 297039b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2971d763cef2SBarry Smith PetscFunctionReturn(0); 2972d763cef2SBarry Smith } 2973d763cef2SBarry Smith 2974d763cef2SBarry Smith /*@ 29755c5f5948SLisandro Dalcin TSGetTimeStepNumber - Deprecated, use TSGetStepNumber(). 2976edc382c3SSatish Balay 2977edc382c3SSatish Balay Level: deprecated 2978edc382c3SSatish Balay 29795c5f5948SLisandro Dalcin @*/ 2980e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 29815c5f5948SLisandro Dalcin 298219eac22cSLisandro Dalcin /*@ 29834f4e0956SLisandro Dalcin TSGetTotalSteps - Deprecated, use TSGetStepNumber(). 2984edc382c3SSatish Balay 2985edc382c3SSatish Balay Level: deprecated 2986edc382c3SSatish Balay 29874f4e0956SLisandro Dalcin @*/ 29884f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 29894f4e0956SLisandro Dalcin 29904f4e0956SLisandro Dalcin /*@ 2991d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2992d763cef2SBarry Smith for use by the TS routines. 2993d763cef2SBarry Smith 29943f9fe445SBarry Smith Logically Collective on TS and Vec 2995d763cef2SBarry Smith 2996d763cef2SBarry Smith Input Parameters: 2997d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 29980910c330SBarry Smith - u - the solution vector 2999d763cef2SBarry Smith 3000d763cef2SBarry Smith Level: beginner 3001d763cef2SBarry Smith 3002715f1b00SHong Zhang .keywords: TS, timestep, set, solution, initial values 30030ed3bfb6SBarry Smith 30040ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate() 3005d763cef2SBarry Smith @*/ 30060910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 3007d763cef2SBarry Smith { 30088737fe31SLisandro Dalcin PetscErrorCode ierr; 3009496e6a7aSJed Brown DM dm; 30108737fe31SLisandro Dalcin 3011d763cef2SBarry Smith PetscFunctionBegin; 30120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30130910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 30140910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 30156bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 30160910c330SBarry Smith ts->vec_sol = u; 3017bbd56ea5SKarl Rupp 3018496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 30190910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 3020d763cef2SBarry Smith PetscFunctionReturn(0); 3021d763cef2SBarry Smith } 3022d763cef2SBarry Smith 3023ac226902SBarry Smith /*@C 3024000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 30253f2090d5SJed Brown called once at the beginning of each time step. 3026000e7ae3SMatthew Knepley 30273f9fe445SBarry Smith Logically Collective on TS 3028000e7ae3SMatthew Knepley 3029000e7ae3SMatthew Knepley Input Parameters: 3030000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3031000e7ae3SMatthew Knepley - func - The function 3032000e7ae3SMatthew Knepley 3033000e7ae3SMatthew Knepley Calling sequence of func: 3034000e7ae3SMatthew Knepley . func (TS ts); 3035000e7ae3SMatthew Knepley 3036000e7ae3SMatthew Knepley Level: intermediate 3037000e7ae3SMatthew Knepley 3038000e7ae3SMatthew Knepley .keywords: TS, timestep 3039dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep() 3040000e7ae3SMatthew Knepley @*/ 30417087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3042000e7ae3SMatthew Knepley { 3043000e7ae3SMatthew Knepley PetscFunctionBegin; 30440700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3045ae60f76fSBarry Smith ts->prestep = func; 3046000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3047000e7ae3SMatthew Knepley } 3048000e7ae3SMatthew Knepley 304909ee8438SJed Brown /*@ 30503f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 30513f2090d5SJed Brown 30523f2090d5SJed Brown Collective on TS 30533f2090d5SJed Brown 30543f2090d5SJed Brown Input Parameters: 30553f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 30563f2090d5SJed Brown 30573f2090d5SJed Brown Notes: 30583f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 30593f2090d5SJed Brown so most users would not generally call this routine themselves. 30603f2090d5SJed Brown 30613f2090d5SJed Brown Level: developer 30623f2090d5SJed Brown 30633f2090d5SJed Brown .keywords: TS, timestep 30649be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 30653f2090d5SJed Brown @*/ 30667087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 30673f2090d5SJed Brown { 30683f2090d5SJed Brown PetscErrorCode ierr; 30693f2090d5SJed Brown 30703f2090d5SJed Brown PetscFunctionBegin; 30710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3072ae60f76fSBarry Smith if (ts->prestep) { 30735efd42a4SStefano Zampini Vec U; 30745efd42a4SStefano Zampini PetscObjectState sprev,spost; 30755efd42a4SStefano Zampini 30765efd42a4SStefano Zampini ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 30775efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3078ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 30795efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3080dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 3081312ce896SJed Brown } 30823f2090d5SJed Brown PetscFunctionReturn(0); 30833f2090d5SJed Brown } 30843f2090d5SJed Brown 3085b8123daeSJed Brown /*@C 3086b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3087b8123daeSJed Brown called once at the beginning of each stage. 3088b8123daeSJed Brown 3089b8123daeSJed Brown Logically Collective on TS 3090b8123daeSJed Brown 3091b8123daeSJed Brown Input Parameters: 3092b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 3093b8123daeSJed Brown - func - The function 3094b8123daeSJed Brown 3095b8123daeSJed Brown Calling sequence of func: 3096b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 3097b8123daeSJed Brown 3098b8123daeSJed Brown Level: intermediate 3099b8123daeSJed Brown 3100b8123daeSJed Brown Note: 3101b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 310280275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 3103b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 3104b8123daeSJed Brown 3105b8123daeSJed Brown .keywords: TS, timestep 31069be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3107b8123daeSJed Brown @*/ 3108b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 3109b8123daeSJed Brown { 3110b8123daeSJed Brown PetscFunctionBegin; 3111b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3112ae60f76fSBarry Smith ts->prestage = func; 3113b8123daeSJed Brown PetscFunctionReturn(0); 3114b8123daeSJed Brown } 3115b8123daeSJed Brown 31169be3e283SDebojyoti Ghosh /*@C 31179be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 31189be3e283SDebojyoti Ghosh called once at the end of each stage. 31199be3e283SDebojyoti Ghosh 31209be3e283SDebojyoti Ghosh Logically Collective on TS 31219be3e283SDebojyoti Ghosh 31229be3e283SDebojyoti Ghosh Input Parameters: 31239be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 31249be3e283SDebojyoti Ghosh - func - The function 31259be3e283SDebojyoti Ghosh 31269be3e283SDebojyoti Ghosh Calling sequence of func: 31279be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 31289be3e283SDebojyoti Ghosh 31299be3e283SDebojyoti Ghosh Level: intermediate 31309be3e283SDebojyoti Ghosh 31319be3e283SDebojyoti Ghosh Note: 31329be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 313380275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 31349be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 31359be3e283SDebojyoti Ghosh 31369be3e283SDebojyoti Ghosh .keywords: TS, timestep 31379be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 31389be3e283SDebojyoti Ghosh @*/ 31399be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 31409be3e283SDebojyoti Ghosh { 31419be3e283SDebojyoti Ghosh PetscFunctionBegin; 31429be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 31439be3e283SDebojyoti Ghosh ts->poststage = func; 31449be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 31459be3e283SDebojyoti Ghosh } 31469be3e283SDebojyoti Ghosh 3147c688d042SShri Abhyankar /*@C 3148c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 3149c688d042SShri Abhyankar called once at the end of each step evaluation. 3150c688d042SShri Abhyankar 3151c688d042SShri Abhyankar Logically Collective on TS 3152c688d042SShri Abhyankar 3153c688d042SShri Abhyankar Input Parameters: 3154c688d042SShri Abhyankar + ts - The TS context obtained from TSCreate() 3155c688d042SShri Abhyankar - func - The function 3156c688d042SShri Abhyankar 3157c688d042SShri Abhyankar Calling sequence of func: 3158c688d042SShri Abhyankar . PetscErrorCode func(TS ts); 3159c688d042SShri Abhyankar 3160c688d042SShri Abhyankar Level: intermediate 3161c688d042SShri Abhyankar 3162c688d042SShri Abhyankar Note: 31631785ff2aSShri Abhyankar Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling 31641785ff2aSShri Abhyankar thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep() 3165e7e94ed4SShri Abhyankar may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step 3166e7e94ed4SShri Abhyankar solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step 3167e7e94ed4SShri Abhyankar with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime() 3168c688d042SShri Abhyankar 3169c688d042SShri Abhyankar .keywords: TS, timestep 3170c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3171c688d042SShri Abhyankar @*/ 3172c688d042SShri Abhyankar PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS)) 3173c688d042SShri Abhyankar { 3174c688d042SShri Abhyankar PetscFunctionBegin; 3175c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3176c688d042SShri Abhyankar ts->postevaluate = func; 3177c688d042SShri Abhyankar PetscFunctionReturn(0); 3178c688d042SShri Abhyankar } 3179c688d042SShri Abhyankar 3180b8123daeSJed Brown /*@ 3181b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 3182b8123daeSJed Brown 3183b8123daeSJed Brown Collective on TS 3184b8123daeSJed Brown 3185b8123daeSJed Brown Input Parameters: 3186b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 31879be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3188b8123daeSJed Brown 3189b8123daeSJed Brown Notes: 3190b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 3191b8123daeSJed Brown most users would not generally call this routine themselves. 3192b8123daeSJed Brown 3193b8123daeSJed Brown Level: developer 3194b8123daeSJed Brown 3195b8123daeSJed Brown .keywords: TS, timestep 31969be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 3197b8123daeSJed Brown @*/ 3198b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3199b8123daeSJed Brown { 3200b8123daeSJed Brown PetscErrorCode ierr; 3201b8123daeSJed Brown 3202b8123daeSJed Brown PetscFunctionBegin; 3203b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3204ae60f76fSBarry Smith if (ts->prestage) { 3205ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 3206b8123daeSJed Brown } 3207b8123daeSJed Brown PetscFunctionReturn(0); 3208b8123daeSJed Brown } 3209b8123daeSJed Brown 32109be3e283SDebojyoti Ghosh /*@ 32119be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 32129be3e283SDebojyoti Ghosh 32139be3e283SDebojyoti Ghosh Collective on TS 32149be3e283SDebojyoti Ghosh 32159be3e283SDebojyoti Ghosh Input Parameters: 32169be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 32179be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 32189be3e283SDebojyoti Ghosh stageindex - Stage number 32199be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 32209be3e283SDebojyoti Ghosh of stages) with the stage solutions 32219be3e283SDebojyoti Ghosh 32229be3e283SDebojyoti Ghosh Notes: 32239be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 32249be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 32259be3e283SDebojyoti Ghosh 32269be3e283SDebojyoti Ghosh Level: developer 32279be3e283SDebojyoti Ghosh 32289be3e283SDebojyoti Ghosh .keywords: TS, timestep 32299be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 32309be3e283SDebojyoti Ghosh @*/ 32319be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 32329be3e283SDebojyoti Ghosh { 32339be3e283SDebojyoti Ghosh PetscErrorCode ierr; 32349be3e283SDebojyoti Ghosh 32359be3e283SDebojyoti Ghosh PetscFunctionBegin; 32369be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 32374beae5d8SLisandro Dalcin if (ts->poststage) { 32389be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 32399be3e283SDebojyoti Ghosh } 32409be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 32419be3e283SDebojyoti Ghosh } 32429be3e283SDebojyoti Ghosh 3243c688d042SShri Abhyankar /*@ 3244c688d042SShri Abhyankar TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate() 3245c688d042SShri Abhyankar 3246c688d042SShri Abhyankar Collective on TS 3247c688d042SShri Abhyankar 3248c688d042SShri Abhyankar Input Parameters: 3249c688d042SShri Abhyankar . ts - The TS context obtained from TSCreate() 3250c688d042SShri Abhyankar 3251c688d042SShri Abhyankar Notes: 3252c688d042SShri Abhyankar TSPostEvaluate() is typically used within time stepping implementations, 3253c688d042SShri Abhyankar most users would not generally call this routine themselves. 3254c688d042SShri Abhyankar 3255c688d042SShri Abhyankar Level: developer 3256c688d042SShri Abhyankar 3257c688d042SShri Abhyankar .keywords: TS, timestep 3258c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep() 3259c688d042SShri Abhyankar @*/ 3260c688d042SShri Abhyankar PetscErrorCode TSPostEvaluate(TS ts) 3261c688d042SShri Abhyankar { 3262c688d042SShri Abhyankar PetscErrorCode ierr; 3263c688d042SShri Abhyankar 3264c688d042SShri Abhyankar PetscFunctionBegin; 3265c688d042SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3266c688d042SShri Abhyankar if (ts->postevaluate) { 3267dcb233daSLisandro Dalcin Vec U; 3268dcb233daSLisandro Dalcin PetscObjectState sprev,spost; 3269dcb233daSLisandro Dalcin 3270dcb233daSLisandro Dalcin ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 3271dcb233daSLisandro Dalcin ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3272c688d042SShri Abhyankar PetscStackCallStandard((*ts->postevaluate),(ts)); 3273dcb233daSLisandro Dalcin ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3274dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 3275c688d042SShri Abhyankar } 3276c688d042SShri Abhyankar PetscFunctionReturn(0); 3277c688d042SShri Abhyankar } 3278c688d042SShri Abhyankar 3279ac226902SBarry Smith /*@C 3280000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 32813f2090d5SJed Brown called once at the end of each time step. 3282000e7ae3SMatthew Knepley 32833f9fe445SBarry Smith Logically Collective on TS 3284000e7ae3SMatthew Knepley 3285000e7ae3SMatthew Knepley Input Parameters: 3286000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3287000e7ae3SMatthew Knepley - func - The function 3288000e7ae3SMatthew Knepley 3289000e7ae3SMatthew Knepley Calling sequence of func: 3290b8123daeSJed Brown $ func (TS ts); 3291000e7ae3SMatthew Knepley 32921785ff2aSShri Abhyankar Notes: 32931785ff2aSShri Abhyankar The function set by TSSetPostStep() is called after each successful step. The solution vector X 32941785ff2aSShri Abhyankar obtained by TSGetSolution() may be different than that computed at the step end if the event handler 32951785ff2aSShri Abhyankar locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead. 32961785ff2aSShri Abhyankar 3297000e7ae3SMatthew Knepley Level: intermediate 3298000e7ae3SMatthew Knepley 3299000e7ae3SMatthew Knepley .keywords: TS, timestep 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 .keywords: TS, timestep 33253f2090d5SJed Brown @*/ 33267087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 33273f2090d5SJed Brown { 33283f2090d5SJed Brown PetscErrorCode ierr; 33293f2090d5SJed Brown 33303f2090d5SJed Brown PetscFunctionBegin; 33310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3332ae60f76fSBarry Smith if (ts->poststep) { 33335efd42a4SStefano Zampini Vec U; 33345efd42a4SStefano Zampini PetscObjectState sprev,spost; 33355efd42a4SStefano Zampini 33365efd42a4SStefano Zampini ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 33375efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3338ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 33395efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3340dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 334172ac3e02SJed Brown } 33423f2090d5SJed Brown PetscFunctionReturn(0); 33433f2090d5SJed Brown } 33443f2090d5SJed Brown 3345d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 3346d763cef2SBarry Smith 3347d763cef2SBarry Smith /*@C 3348a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 3349d763cef2SBarry Smith timestep to display the iteration's progress. 3350d763cef2SBarry Smith 33513f9fe445SBarry Smith Logically Collective on TS 3352d763cef2SBarry Smith 3353d763cef2SBarry Smith Input Parameters: 3354d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3355e213d8f1SJed Brown . monitor - monitoring routine 3356329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 33570298fd71SBarry Smith monitor routine (use NULL if no context is desired) 3358b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 33590298fd71SBarry Smith (may be NULL) 3360d763cef2SBarry Smith 3361e213d8f1SJed Brown Calling sequence of monitor: 3362dcb233daSLisandro Dalcin $ PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 3363d763cef2SBarry Smith 3364d763cef2SBarry Smith + ts - the TS context 336563e21af5SBarry 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) 33661f06c33eSBarry Smith . time - current time 33670910c330SBarry Smith . u - current iterate 3368d763cef2SBarry Smith - mctx - [optional] monitoring context 3369d763cef2SBarry Smith 3370d763cef2SBarry Smith Notes: 3371d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 3372d763cef2SBarry Smith already has been loaded. 3373d763cef2SBarry Smith 337495452b02SPatrick Sanan Fortran Notes: 337595452b02SPatrick Sanan Only a single monitor function can be set for each TS object 3376025f1a04SBarry Smith 3377d763cef2SBarry Smith Level: intermediate 3378d763cef2SBarry Smith 3379d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3380d763cef2SBarry Smith 3381a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 3382d763cef2SBarry Smith @*/ 3383c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 3384d763cef2SBarry Smith { 338578064530SBarry Smith PetscErrorCode ierr; 338678064530SBarry Smith PetscInt i; 338778064530SBarry Smith PetscBool identical; 338878064530SBarry Smith 3389d763cef2SBarry Smith PetscFunctionBegin; 33900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 339178064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 339278064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr); 339378064530SBarry Smith if (identical) PetscFunctionReturn(0); 339478064530SBarry Smith } 339517186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 3396d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 33978704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 3398d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 3399d763cef2SBarry Smith PetscFunctionReturn(0); 3400d763cef2SBarry Smith } 3401d763cef2SBarry Smith 3402d763cef2SBarry Smith /*@C 3403a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 3404d763cef2SBarry Smith 34053f9fe445SBarry Smith Logically Collective on TS 3406d763cef2SBarry Smith 3407d763cef2SBarry Smith Input Parameters: 3408d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3409d763cef2SBarry Smith 3410d763cef2SBarry Smith Notes: 3411d763cef2SBarry Smith There is no way to remove a single, specific monitor. 3412d763cef2SBarry Smith 3413d763cef2SBarry Smith Level: intermediate 3414d763cef2SBarry Smith 3415d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3416d763cef2SBarry Smith 3417a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 3418d763cef2SBarry Smith @*/ 34197087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 3420d763cef2SBarry Smith { 3421d952e501SBarry Smith PetscErrorCode ierr; 3422d952e501SBarry Smith PetscInt i; 3423d952e501SBarry Smith 3424d763cef2SBarry Smith PetscFunctionBegin; 34250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3426d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 34278704b422SBarry Smith if (ts->monitordestroy[i]) { 34288704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 3429d952e501SBarry Smith } 3430d952e501SBarry Smith } 3431d763cef2SBarry Smith ts->numbermonitors = 0; 3432d763cef2SBarry Smith PetscFunctionReturn(0); 3433d763cef2SBarry Smith } 3434d763cef2SBarry Smith 3435721cd6eeSBarry Smith /*@C 3436721cd6eeSBarry Smith TSMonitorDefault - The Default monitor, prints the timestep and time for each step 34375516499fSSatish Balay 34385516499fSSatish Balay Level: intermediate 343941251cbbSSatish Balay 34405516499fSSatish Balay .keywords: TS, set, monitor 34415516499fSSatish Balay 344263e21af5SBarry Smith .seealso: TSMonitorSet() 344341251cbbSSatish Balay @*/ 3444721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf) 3445d763cef2SBarry Smith { 3446dfbe8321SBarry Smith PetscErrorCode ierr; 3447721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 344841aca3d6SBarry Smith PetscBool iascii,ibinary; 3449d132466eSBarry Smith 3450d763cef2SBarry Smith PetscFunctionBegin; 34514d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 345241aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 345341aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr); 3454721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 345541aca3d6SBarry Smith if (iascii) { 3456649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 345763e21af5SBarry Smith if (step == -1){ /* this indicates it is an interpolated solution */ 345863e21af5SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr); 345963e21af5SBarry Smith } else { 34608392e04aSShri 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); 346163e21af5SBarry Smith } 3462649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 346341aca3d6SBarry Smith } else if (ibinary) { 346441aca3d6SBarry Smith PetscMPIInt rank; 346541aca3d6SBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 346641aca3d6SBarry Smith if (!rank) { 3467450a797fSBarry Smith PetscBool skipHeader; 3468450a797fSBarry Smith PetscInt classid = REAL_FILE_CLASSID; 3469450a797fSBarry Smith 3470450a797fSBarry Smith ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr); 3471450a797fSBarry Smith if (!skipHeader) { 3472450a797fSBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 3473450a797fSBarry Smith } 347441aca3d6SBarry Smith ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr); 347541aca3d6SBarry Smith } else { 347641aca3d6SBarry Smith ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr); 347741aca3d6SBarry Smith } 347841aca3d6SBarry Smith } 3479721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3480d763cef2SBarry Smith PetscFunctionReturn(0); 3481d763cef2SBarry Smith } 3482d763cef2SBarry Smith 3483cc9c3a59SBarry Smith /*@C 3484cc9c3a59SBarry Smith TSMonitorExtreme - Prints the extreme values of the solution at each timestep 3485cc9c3a59SBarry Smith 3486cc9c3a59SBarry Smith Level: intermediate 3487cc9c3a59SBarry Smith 3488cc9c3a59SBarry Smith .keywords: TS, set, monitor 3489cc9c3a59SBarry Smith 3490cc9c3a59SBarry Smith .seealso: TSMonitorSet() 3491cc9c3a59SBarry Smith @*/ 3492cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf) 3493cc9c3a59SBarry Smith { 3494cc9c3a59SBarry Smith PetscErrorCode ierr; 3495cc9c3a59SBarry Smith PetscViewer viewer = vf->viewer; 3496cc9c3a59SBarry Smith PetscBool iascii; 3497cc9c3a59SBarry Smith PetscReal max,min; 3498cc9c3a59SBarry Smith 3499cc9c3a59SBarry Smith 3500cc9c3a59SBarry Smith PetscFunctionBegin; 3501cc9c3a59SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3502cc9c3a59SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 3503cc9c3a59SBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 3504cc9c3a59SBarry Smith if (iascii) { 3505cc9c3a59SBarry Smith ierr = VecMax(v,NULL,&max);CHKERRQ(ierr); 3506cc9c3a59SBarry Smith ierr = VecMin(v,NULL,&min);CHKERRQ(ierr); 3507cc9c3a59SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 35085132926bSBarry 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); 3509cc9c3a59SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3510cc9c3a59SBarry Smith } 3511cc9c3a59SBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3512cc9c3a59SBarry Smith PetscFunctionReturn(0); 3513cc9c3a59SBarry Smith } 3514cc9c3a59SBarry Smith 3515cd652676SJed Brown /*@ 3516cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3517cd652676SJed Brown 3518cd652676SJed Brown Collective on TS 3519cd652676SJed Brown 3520cd652676SJed Brown Input Argument: 3521cd652676SJed Brown + ts - time stepping context 3522cd652676SJed Brown - t - time to interpolate to 3523cd652676SJed Brown 3524cd652676SJed Brown Output Argument: 35250910c330SBarry Smith . U - state at given time 3526cd652676SJed Brown 3527cd652676SJed Brown Level: intermediate 3528cd652676SJed Brown 3529cd652676SJed Brown Developer Notes: 3530cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3531cd652676SJed Brown 3532cd652676SJed Brown .keywords: TS, set 3533cd652676SJed Brown 3534874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve() 3535cd652676SJed Brown @*/ 35360910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3537cd652676SJed Brown { 3538cd652676SJed Brown PetscErrorCode ierr; 3539cd652676SJed Brown 3540cd652676SJed Brown PetscFunctionBegin; 3541cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3542b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3543be5899b3SLisandro 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); 3544ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 35450910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3546cd652676SJed Brown PetscFunctionReturn(0); 3547cd652676SJed Brown } 3548cd652676SJed Brown 3549d763cef2SBarry Smith /*@ 35506d9e5789SSean Farley TSStep - Steps one time step 3551d763cef2SBarry Smith 3552d763cef2SBarry Smith Collective on TS 3553d763cef2SBarry Smith 3554d763cef2SBarry Smith Input Parameter: 3555d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3556d763cef2SBarry Smith 355727829d71SBarry Smith Level: developer 3558d763cef2SBarry Smith 3559b8123daeSJed Brown Notes: 356027829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 356127829d71SBarry Smith 3562b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3563b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3564b8123daeSJed Brown 356519eac22cSLisandro Dalcin This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the 356619eac22cSLisandro Dalcin time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 356725cb2221SBarry Smith 3568d763cef2SBarry Smith .keywords: TS, timestep, solve 3569d763cef2SBarry Smith 35709be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3571d763cef2SBarry Smith @*/ 3572193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3573d763cef2SBarry Smith { 3574dfbe8321SBarry Smith PetscErrorCode ierr; 3575fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3576be5899b3SLisandro Dalcin PetscReal ptime; 3577d763cef2SBarry Smith 3578d763cef2SBarry Smith PetscFunctionBegin; 35790700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3580fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3581fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3582fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3583fffbeea8SBarry Smith " type = {Preprint},\n" 3584fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3585fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3586302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3587fffbeea8SBarry Smith 3588d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 358968bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3590d405a339SMatthew Knepley 3591ef85077eSLisandro 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>"); 3592a6772fa2SLisandro 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()"); 3593be5899b3SLisandro 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"); 3594a6772fa2SLisandro Dalcin 3595be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 3596be5899b3SLisandro Dalcin ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev; 35972808aa04SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3598e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3599d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3600193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3601d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3602be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 36032808aa04SLisandro Dalcin ts->steps++; 3604be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 360528d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3606362cd11cSLisandro Dalcin 3607362cd11cSLisandro Dalcin if (ts->reason < 0) { 3608cef5090cSJed Brown if (ts->errorifstepfailed) { 360908c7845fSBarry 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]); 361008c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3611d2daff3dSHong Zhang } 361208c7845fSBarry Smith } else if (!ts->reason) { 361308c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 361408c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 361508c7845fSBarry Smith } 361608c7845fSBarry Smith PetscFunctionReturn(0); 361708c7845fSBarry Smith } 361808c7845fSBarry Smith 361908c7845fSBarry Smith /*@ 36207cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 36217cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 36227cbde773SLisandro Dalcin 36237cbde773SLisandro Dalcin Collective on TS 36247cbde773SLisandro Dalcin 36257cbde773SLisandro Dalcin Input Arguments: 36267cbde773SLisandro Dalcin + ts - time stepping context 36277cbde773SLisandro Dalcin . wnormtype - norm type, either NORM_2 or NORM_INFINITY 36287cbde773SLisandro Dalcin - order - optional, desired order for the error evaluation or PETSC_DECIDE 36297cbde773SLisandro Dalcin 36307cbde773SLisandro Dalcin Output Arguments: 36317cbde773SLisandro Dalcin + order - optional, the actual order of the error evaluation 36327cbde773SLisandro Dalcin - wlte - the weighted local truncation error norm 36337cbde773SLisandro Dalcin 36347cbde773SLisandro Dalcin Level: advanced 36357cbde773SLisandro Dalcin 36367cbde773SLisandro Dalcin Notes: 36377cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 36387cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 36397cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 36407cbde773SLisandro Dalcin 36417cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm() 36427cbde773SLisandro Dalcin @*/ 36437cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte) 36447cbde773SLisandro Dalcin { 36457cbde773SLisandro Dalcin PetscErrorCode ierr; 36467cbde773SLisandro Dalcin 36477cbde773SLisandro Dalcin PetscFunctionBegin; 36487cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36497cbde773SLisandro Dalcin PetscValidType(ts,1); 36507cbde773SLisandro Dalcin PetscValidLogicalCollectiveEnum(ts,wnormtype,4); 36517cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order,3); 36527cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts,*order,3); 36537cbde773SLisandro Dalcin PetscValidRealPointer(wlte,4); 36547cbde773SLisandro Dalcin if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 36557cbde773SLisandro Dalcin if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name); 36567cbde773SLisandro Dalcin ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr); 36577cbde773SLisandro Dalcin PetscFunctionReturn(0); 36587cbde773SLisandro Dalcin } 36597cbde773SLisandro Dalcin 366005175c85SJed Brown /*@ 366105175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 366205175c85SJed Brown 36631c3436cfSJed Brown Collective on TS 366405175c85SJed Brown 366505175c85SJed Brown Input Arguments: 36661c3436cfSJed Brown + ts - time stepping context 36671c3436cfSJed Brown . order - desired order of accuracy 36680298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 366905175c85SJed Brown 367005175c85SJed Brown Output Arguments: 36710910c330SBarry Smith . U - state at the end of the current step 367205175c85SJed Brown 367305175c85SJed Brown Level: advanced 367405175c85SJed Brown 3675108c343cSJed Brown Notes: 3676108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3677108c343cSJed 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. 3678108c343cSJed Brown 36791c3436cfSJed Brown .seealso: TSStep(), TSAdapt 368005175c85SJed Brown @*/ 36810910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 368205175c85SJed Brown { 368305175c85SJed Brown PetscErrorCode ierr; 368405175c85SJed Brown 368505175c85SJed Brown PetscFunctionBegin; 368605175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 368705175c85SJed Brown PetscValidType(ts,1); 36880910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3689ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 36900910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 369105175c85SJed Brown PetscFunctionReturn(0); 369205175c85SJed Brown } 369305175c85SJed Brown 3694b1cb36f3SHong Zhang /*@ 36956a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 36966a4d4014SLisandro Dalcin 36976a4d4014SLisandro Dalcin Collective on TS 36986a4d4014SLisandro Dalcin 36996a4d4014SLisandro Dalcin Input Parameter: 37006a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 370163e21af5SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used, 370263e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 37035a3a76d0SJed Brown 37046a4d4014SLisandro Dalcin Level: beginner 37056a4d4014SLisandro Dalcin 37065a3a76d0SJed Brown Notes: 37075a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 37085a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 37095a3a76d0SJed Brown stepped over the final time. 37105a3a76d0SJed Brown 37116a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 37126a4d4014SLisandro Dalcin 371363e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime() 37146a4d4014SLisandro Dalcin @*/ 3715cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 37166a4d4014SLisandro Dalcin { 3717b06615a5SLisandro Dalcin Vec solution; 37186a4d4014SLisandro Dalcin PetscErrorCode ierr; 3719f22f69f0SBarry Smith 37206a4d4014SLisandro Dalcin PetscFunctionBegin; 37210700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3722f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 3723feed9e9dSBarry Smith 3724ee41a567SStefano 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 */ 37250910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 3726b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3727b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3728b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 37295a3a76d0SJed Brown } 37300910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3731715f1b00SHong Zhang if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE"); 3732bbd56ea5SKarl Rupp } else if (u) { 37330910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 37345a3a76d0SJed Brown } 3735b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 373668bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3737a6772fa2SLisandro Dalcin 3738ef85077eSLisandro 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>"); 3739a6772fa2SLisandro 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()"); 3740a6772fa2SLisandro 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"); 3741a6772fa2SLisandro Dalcin 3742715f1b00SHong Zhang if (ts->forward_solve) { 3743715f1b00SHong Zhang ierr = TSForwardSetUp(ts);CHKERRQ(ierr); 3744715f1b00SHong Zhang } 3745715f1b00SHong Zhang 3746e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 3747715f1b00SHong Zhang restarts the step after an event. Resetting these counters in such case causes 3748e7069c78SShri TSTrajectory to incorrectly save the output files 3749e7069c78SShri */ 3750715f1b00SHong Zhang /* reset time step and iteration counters */ 37512808aa04SLisandro Dalcin if (!ts->steps) { 37525ef26d82SJed Brown ts->ksp_its = 0; 37535ef26d82SJed Brown ts->snes_its = 0; 3754c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3755c610991cSLisandro Dalcin ts->reject = 0; 37562808aa04SLisandro Dalcin ts->steprestart = PETSC_TRUE; 37572808aa04SLisandro Dalcin ts->steprollback = PETSC_FALSE; 37582808aa04SLisandro Dalcin } 375910bc0e4dSStefano Zampini if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && ts->ptime + ts->time_step > ts->max_time) ts->time_step = ts->max_time - ts->ptime; 3760193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3761193ac0bcSJed Brown 3762ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3763f05ece33SBarry Smith 3764193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3765193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 3766a6772fa2SLisandro Dalcin if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3767cc708dedSBarry Smith ts->solvetime = ts->ptime; 3768a6772fa2SLisandro Dalcin solution = ts->vec_sol; 3769be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 3770db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3771db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3772e7069c78SShri 37732808aa04SLisandro Dalcin if (!ts->steps) { 37742808aa04SLisandro Dalcin ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 37756427ac75SLisandro Dalcin ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 37762808aa04SLisandro Dalcin } 37776427ac75SLisandro Dalcin 3778e1a7a14fSJed Brown while (!ts->reason) { 37792808aa04SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 37809687d888SLisandro Dalcin if (!ts->steprollback) { 37819687d888SLisandro Dalcin ierr = TSPreStep(ts);CHKERRQ(ierr); 37829687d888SLisandro Dalcin } 3783193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 3784f3b1f45cSBarry Smith if (ts->testjacobian) { 3785f3b1f45cSBarry Smith ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr); 3786f3b1f45cSBarry Smith } 3787f3b1f45cSBarry Smith if (ts->testjacobiantranspose) { 3788f3b1f45cSBarry Smith ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr); 3789f3b1f45cSBarry Smith } 3790e783b05fSHong Zhang if (ts->vec_costintegral && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */ 3791b1cb36f3SHong Zhang ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr); 3792b1cb36f3SHong Zhang } 379358818c2dSLisandro Dalcin if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */ 3794715f1b00SHong Zhang ierr = TSForwardStep(ts);CHKERRQ(ierr); 3795715f1b00SHong Zhang } 379610b82f12SShri Abhyankar ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 3797e783b05fSHong 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. */ 379858818c2dSLisandro Dalcin if (ts->steprollback) { 379958818c2dSLisandro Dalcin ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 380058818c2dSLisandro Dalcin } 3801e783b05fSHong Zhang if (!ts->steprollback) { 38022808aa04SLisandro Dalcin ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 38031eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 3804aeb4809dSShri Abhyankar } 3805193ac0bcSJed Brown } 38062808aa04SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 38076427ac75SLisandro Dalcin 380849354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 38090910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 3810cc708dedSBarry Smith ts->solvetime = ts->max_time; 3811b06615a5SLisandro Dalcin solution = u; 381263e21af5SBarry Smith ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr); 38130574a7fbSJed Brown } else { 3814ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3815cc708dedSBarry Smith ts->solvetime = ts->ptime; 3816b06615a5SLisandro Dalcin solution = ts->vec_sol; 38170574a7fbSJed Brown } 3818193ac0bcSJed Brown } 3819d2daff3dSHong Zhang 3820ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 382163e21af5SBarry Smith ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr); 382256f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 3823ef222394SBarry Smith if (ts->adjoint_solve) { 3824ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 38252b0a91c0SBarry Smith } 38266a4d4014SLisandro Dalcin PetscFunctionReturn(0); 38276a4d4014SLisandro Dalcin } 38286a4d4014SLisandro Dalcin 38297db568b7SBarry Smith /*@C 3830228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 3831228d79bcSJed Brown 3832228d79bcSJed Brown Collective on TS 3833228d79bcSJed Brown 3834228d79bcSJed Brown Input Parameters: 3835228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 3836228d79bcSJed Brown . step - step number that has just completed 3837228d79bcSJed Brown . ptime - model time of the state 38380910c330SBarry Smith - u - state at the current model time 3839228d79bcSJed Brown 3840228d79bcSJed Brown Notes: 38417db568b7SBarry Smith TSMonitor() is typically used automatically within the time stepping implementations. 38427db568b7SBarry Smith Users would almost never call this routine directly. 3843228d79bcSJed Brown 384463e21af5SBarry Smith A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions 384563e21af5SBarry Smith 38467db568b7SBarry Smith Level: developer 3847228d79bcSJed Brown 3848228d79bcSJed Brown .keywords: TS, timestep 3849228d79bcSJed Brown @*/ 38500910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 3851d763cef2SBarry Smith { 3852d6152f81SLisandro Dalcin DM dm; 3853a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 3854d6152f81SLisandro Dalcin PetscErrorCode ierr; 3855d763cef2SBarry Smith 3856d763cef2SBarry Smith PetscFunctionBegin; 3857b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3858b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 3859d6152f81SLisandro Dalcin 3860d6152f81SLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 3861d6152f81SLisandro Dalcin ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr); 3862d6152f81SLisandro Dalcin 38635edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 3864d763cef2SBarry Smith for (i=0; i<n; i++) { 38650910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 3866d763cef2SBarry Smith } 38675edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 3868d763cef2SBarry Smith PetscFunctionReturn(0); 3869d763cef2SBarry Smith } 3870d763cef2SBarry Smith 3871d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 3872d763cef2SBarry Smith /*@C 38737db568b7SBarry Smith TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 3874a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 3875d763cef2SBarry Smith 3876d763cef2SBarry Smith Collective on TS 3877d763cef2SBarry Smith 3878d763cef2SBarry Smith Input Parameters: 3879d763cef2SBarry Smith + host - the X display to open, or null for the local machine 3880d763cef2SBarry Smith . label - the title to put in the title bar 38817c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 3882a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 3883a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 3884d763cef2SBarry Smith 3885d763cef2SBarry Smith Output Parameter: 38860b039ecaSBarry Smith . ctx - the context 3887d763cef2SBarry Smith 3888d763cef2SBarry Smith Options Database Key: 38894f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 38908b668821SLisandro Dalcin + -ts_monitor_lg_timestep_log - automatically sets line graph monitor 38917db568b7SBarry Smith . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 38927db568b7SBarry Smith . -ts_monitor_lg_error - monitor the error 38937db568b7SBarry Smith . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 38947db568b7SBarry Smith . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 3895b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 3896d763cef2SBarry Smith 3897d763cef2SBarry Smith Notes: 3898a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 3899d763cef2SBarry Smith 39007db568b7SBarry Smith One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 39017db568b7SBarry Smith 39027db568b7SBarry 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 39037db568b7SBarry 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 39047db568b7SBarry Smith as the first argument. 39057db568b7SBarry Smith 39067db568b7SBarry Smith One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 39077db568b7SBarry Smith 3908d763cef2SBarry Smith Level: intermediate 3909d763cef2SBarry Smith 39107db568b7SBarry Smith .keywords: TS, monitor, line graph, residual 3911d763cef2SBarry Smith 39127db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 39137db568b7SBarry Smith TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 39147db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 39157db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 39167db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 39177c922b88SBarry Smith 3918d763cef2SBarry Smith @*/ 3919a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 3920d763cef2SBarry Smith { 39217f52daa2SLisandro Dalcin PetscDraw draw; 3922dfbe8321SBarry Smith PetscErrorCode ierr; 3923d763cef2SBarry Smith 3924d763cef2SBarry Smith PetscFunctionBegin; 3925b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 39267f52daa2SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 39277f52daa2SLisandro Dalcin ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 39287f52daa2SLisandro Dalcin ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 3929287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 39307f52daa2SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 3931a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 3932d763cef2SBarry Smith PetscFunctionReturn(0); 3933d763cef2SBarry Smith } 3934d763cef2SBarry Smith 3935b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 3936d763cef2SBarry Smith { 39370b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 3938c32365f1SBarry Smith PetscReal x = ptime,y; 3939dfbe8321SBarry Smith PetscErrorCode ierr; 3940d763cef2SBarry Smith 3941d763cef2SBarry Smith PetscFunctionBegin; 394263e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */ 3943b06615a5SLisandro Dalcin if (!step) { 3944a9f9c1f6SBarry Smith PetscDrawAxis axis; 39458b668821SLisandro Dalcin const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step"; 3946a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 39478b668821SLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr); 3948a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 3949a9f9c1f6SBarry Smith } 3950c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 39518b668821SLisandro Dalcin if (ctx->semilogy) y = PetscLog10Real(y); 39520b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 3953b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 39540b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 39556934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 39563923b477SBarry Smith } 3957d763cef2SBarry Smith PetscFunctionReturn(0); 3958d763cef2SBarry Smith } 3959d763cef2SBarry Smith 3960d763cef2SBarry Smith /*@C 3961a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 3962a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 3963d763cef2SBarry Smith 39640b039ecaSBarry Smith Collective on TSMonitorLGCtx 3965d763cef2SBarry Smith 3966d763cef2SBarry Smith Input Parameter: 39670b039ecaSBarry Smith . ctx - the monitor context 3968d763cef2SBarry Smith 3969d763cef2SBarry Smith Level: intermediate 3970d763cef2SBarry Smith 3971d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 3972d763cef2SBarry Smith 39734f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 3974d763cef2SBarry Smith @*/ 3975a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 3976d763cef2SBarry Smith { 3977dfbe8321SBarry Smith PetscErrorCode ierr; 3978d763cef2SBarry Smith 3979d763cef2SBarry Smith PetscFunctionBegin; 39807684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 39817684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 39827684fa3eSBarry Smith } 39830b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 398431152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 3985387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 3986387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 3987387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 39880b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 3989d763cef2SBarry Smith PetscFunctionReturn(0); 3990d763cef2SBarry Smith } 3991d763cef2SBarry Smith 3992*1b575b74SJoseph Pusztay /* 3993*1b575b74SJoseph Pusztay 3994*1b575b74SJoseph Pusztay Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations 3995*1b575b74SJoseph Pusztay 3996*1b575b74SJoseph Pusztay */ 3997*1b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx) 3998*1b575b74SJoseph Pusztay { 3999*1b575b74SJoseph Pusztay PetscDraw draw; 4000*1b575b74SJoseph Pusztay PetscErrorCode ierr; 4001*1b575b74SJoseph Pusztay 4002*1b575b74SJoseph Pusztay PetscFunctionBegin; 4003*1b575b74SJoseph Pusztay ierr = PetscNew(ctx);CHKERRQ(ierr); 4004*1b575b74SJoseph Pusztay ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 4005*1b575b74SJoseph Pusztay ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 4006*1b575b74SJoseph Pusztay ierr = PetscDrawSPCreate(draw,1,&(*ctx)->sp);CHKERRQ(ierr); 4007*1b575b74SJoseph Pusztay ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 4008*1b575b74SJoseph Pusztay (*ctx)->howoften = howoften; 4009*1b575b74SJoseph Pusztay PetscFunctionReturn(0); 4010*1b575b74SJoseph Pusztay 4011*1b575b74SJoseph Pusztay } 4012*1b575b74SJoseph Pusztay 4013*1b575b74SJoseph Pusztay /* TODO: ACTUALLY ADD THE COORDINATES X, Y 4014*1b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 4015*1b575b74SJoseph Pusztay { 4016*1b575b74SJoseph Pusztay 4017*1b575b74SJoseph Pusztay TSMonitorSPCtx ctx = (TSMonitorSPCtx) monctx; 4018*1b575b74SJoseph Pusztay PetscReal x, y; 4019*1b575b74SJoseph Pusztay PetscErrorCode ierr; 4020*1b575b74SJoseph Pusztay PetscInt i; 4021*1b575b74SJoseph Pusztay PetscFunctionBegin; 4022*1b575b74SJoseph Pusztay if (step < 0) PetscFunctionReturn(0); -1 indicates an interpolated solution 4023*1b575b74SJoseph Pusztay if (!step) { 4024*1b575b74SJoseph Pusztay PetscDrawAxis axis; 4025*1b575b74SJoseph Pusztay ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr); 4026*1b575b74SJoseph Pusztay ierr = PetscDrawAxisSetLabels(axis,"Particle Positions","X","Y");CHKERRQ(ierr); 4027*1b575b74SJoseph Pusztay ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr); 4028*1b575b74SJoseph Pusztay } 4029*1b575b74SJoseph Pusztay ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 4030*1b575b74SJoseph Pusztay TODO: Add support for multiple particles properly 4031*1b575b74SJoseph Pusztay 4032*1b575b74SJoseph Pusztay ierr = PetscDrawSPAddPoint(ctx->sp,&x,&y);CHKERRQ(ierr); 4033*1b575b74SJoseph Pusztay 4034*1b575b74SJoseph Pusztay if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 4035*1b575b74SJoseph Pusztay ierr = PetscDrawSPDraw(ctx->sp);CHKERRQ(ierr); 4036*1b575b74SJoseph Pusztay ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr); 4037*1b575b74SJoseph Pusztay } 4038*1b575b74SJoseph Pusztay PetscFunctionReturn(0); 4039*1b575b74SJoseph Pusztay } 4040*1b575b74SJoseph Pusztay */ 4041*1b575b74SJoseph Pusztay /* 4042*1b575b74SJoseph Pusztay Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate 4043*1b575b74SJoseph Pusztay */ 4044*1b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx) 4045*1b575b74SJoseph Pusztay { 4046*1b575b74SJoseph Pusztay PetscErrorCode ierr; 4047*1b575b74SJoseph Pusztay 4048*1b575b74SJoseph Pusztay PetscFunctionBegin; 4049*1b575b74SJoseph Pusztay 4050*1b575b74SJoseph Pusztay ierr = PetscDrawSPDestroy(&(*ctx)->sp);CHKERRQ(ierr); 4051*1b575b74SJoseph Pusztay ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 4052*1b575b74SJoseph Pusztay ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 4053*1b575b74SJoseph Pusztay ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 4054*1b575b74SJoseph Pusztay ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 4055*1b575b74SJoseph Pusztay ierr = PetscFree(*ctx);CHKERRQ(ierr); 4056*1b575b74SJoseph Pusztay 4057*1b575b74SJoseph Pusztay PetscFunctionReturn(0); 4058*1b575b74SJoseph Pusztay 4059*1b575b74SJoseph Pusztay } 4060*1b575b74SJoseph Pusztay 4061d763cef2SBarry Smith /*@ 4062b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4063d763cef2SBarry Smith 4064d763cef2SBarry Smith Not Collective 4065d763cef2SBarry Smith 4066d763cef2SBarry Smith Input Parameter: 4067d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 4068d763cef2SBarry Smith 4069d763cef2SBarry Smith Output Parameter: 407019eac22cSLisandro Dalcin . t - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime(). 4071d763cef2SBarry Smith 4072d763cef2SBarry Smith Level: beginner 4073d763cef2SBarry Smith 4074b8123daeSJed Brown Note: 4075b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 40769be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 4077b8123daeSJed Brown 4078aaa6c58dSLisandro Dalcin .seealso: TSGetSolveTime(), TSSetTime(), TSGetTimeStep() 4079d763cef2SBarry Smith 4080d763cef2SBarry Smith .keywords: TS, get, time 4081d763cef2SBarry Smith @*/ 40827087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 4083d763cef2SBarry Smith { 4084d763cef2SBarry Smith PetscFunctionBegin; 40850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4086f7cf8827SBarry Smith PetscValidRealPointer(t,2); 4087d763cef2SBarry Smith *t = ts->ptime; 4088d763cef2SBarry Smith PetscFunctionReturn(0); 4089d763cef2SBarry Smith } 4090d763cef2SBarry Smith 4091e5e524a1SHong Zhang /*@ 4092e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4093e5e524a1SHong Zhang 4094e5e524a1SHong Zhang Not Collective 4095e5e524a1SHong Zhang 4096e5e524a1SHong Zhang Input Parameter: 4097e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 4098e5e524a1SHong Zhang 4099e5e524a1SHong Zhang Output Parameter: 4100e5e524a1SHong Zhang . t - the previous time 4101e5e524a1SHong Zhang 4102e5e524a1SHong Zhang Level: beginner 4103e5e524a1SHong Zhang 4104aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep() 4105e5e524a1SHong Zhang 4106e5e524a1SHong Zhang .keywords: TS, get, time 4107e5e524a1SHong Zhang @*/ 4108e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 4109e5e524a1SHong Zhang { 4110e5e524a1SHong Zhang PetscFunctionBegin; 4111e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4112e5e524a1SHong Zhang PetscValidRealPointer(t,2); 4113e5e524a1SHong Zhang *t = ts->ptime_prev; 4114e5e524a1SHong Zhang PetscFunctionReturn(0); 4115e5e524a1SHong Zhang } 4116e5e524a1SHong Zhang 41176a4d4014SLisandro Dalcin /*@ 41186a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 41196a4d4014SLisandro Dalcin 41203f9fe445SBarry Smith Logically Collective on TS 41216a4d4014SLisandro Dalcin 41226a4d4014SLisandro Dalcin Input Parameters: 41236a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 41246a4d4014SLisandro Dalcin - time - the time 41256a4d4014SLisandro Dalcin 41266a4d4014SLisandro Dalcin Level: intermediate 41276a4d4014SLisandro Dalcin 412819eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps() 41296a4d4014SLisandro Dalcin 41306a4d4014SLisandro Dalcin .keywords: TS, set, time 41316a4d4014SLisandro Dalcin @*/ 41327087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 41336a4d4014SLisandro Dalcin { 41346a4d4014SLisandro Dalcin PetscFunctionBegin; 41350700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4136c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 41376a4d4014SLisandro Dalcin ts->ptime = t; 41386a4d4014SLisandro Dalcin PetscFunctionReturn(0); 41396a4d4014SLisandro Dalcin } 41406a4d4014SLisandro Dalcin 4141d763cef2SBarry Smith /*@C 4142d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4143d763cef2SBarry Smith TS options in the database. 4144d763cef2SBarry Smith 41453f9fe445SBarry Smith Logically Collective on TS 4146d763cef2SBarry Smith 4147d763cef2SBarry Smith Input Parameter: 4148d763cef2SBarry Smith + ts - The TS context 4149d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4150d763cef2SBarry Smith 4151d763cef2SBarry Smith Notes: 4152d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4153d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4154d763cef2SBarry Smith hyphen. 4155d763cef2SBarry Smith 4156d763cef2SBarry Smith Level: advanced 4157d763cef2SBarry Smith 4158d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 4159d763cef2SBarry Smith 4160d763cef2SBarry Smith .seealso: TSSetFromOptions() 4161d763cef2SBarry Smith 4162d763cef2SBarry Smith @*/ 41637087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 4164d763cef2SBarry Smith { 4165dfbe8321SBarry Smith PetscErrorCode ierr; 4166089b2837SJed Brown SNES snes; 4167d763cef2SBarry Smith 4168d763cef2SBarry Smith PetscFunctionBegin; 41690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4170d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4171089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4172089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4173d763cef2SBarry Smith PetscFunctionReturn(0); 4174d763cef2SBarry Smith } 4175d763cef2SBarry Smith 4176d763cef2SBarry Smith /*@C 4177d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4178d763cef2SBarry Smith TS options in the database. 4179d763cef2SBarry Smith 41803f9fe445SBarry Smith Logically Collective on TS 4181d763cef2SBarry Smith 4182d763cef2SBarry Smith Input Parameter: 4183d763cef2SBarry Smith + ts - The TS context 4184d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4185d763cef2SBarry Smith 4186d763cef2SBarry Smith Notes: 4187d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4188d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4189d763cef2SBarry Smith hyphen. 4190d763cef2SBarry Smith 4191d763cef2SBarry Smith Level: advanced 4192d763cef2SBarry Smith 4193d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 4194d763cef2SBarry Smith 4195d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 4196d763cef2SBarry Smith 4197d763cef2SBarry Smith @*/ 41987087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4199d763cef2SBarry Smith { 4200dfbe8321SBarry Smith PetscErrorCode ierr; 4201089b2837SJed Brown SNES snes; 4202d763cef2SBarry Smith 4203d763cef2SBarry Smith PetscFunctionBegin; 42040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4205d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4206089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4207089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4208d763cef2SBarry Smith PetscFunctionReturn(0); 4209d763cef2SBarry Smith } 4210d763cef2SBarry Smith 4211d763cef2SBarry Smith /*@C 4212d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4213d763cef2SBarry Smith TS options in the database. 4214d763cef2SBarry Smith 4215d763cef2SBarry Smith Not Collective 4216d763cef2SBarry Smith 4217d763cef2SBarry Smith Input Parameter: 4218d763cef2SBarry Smith . ts - The TS context 4219d763cef2SBarry Smith 4220d763cef2SBarry Smith Output Parameter: 4221d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4222d763cef2SBarry Smith 422395452b02SPatrick Sanan Notes: 422495452b02SPatrick Sanan On the fortran side, the user should pass in a string 'prifix' of 4225d763cef2SBarry Smith sufficient length to hold the prefix. 4226d763cef2SBarry Smith 4227d763cef2SBarry Smith Level: intermediate 4228d763cef2SBarry Smith 4229d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 4230d763cef2SBarry Smith 4231d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 4232d763cef2SBarry Smith @*/ 42337087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4234d763cef2SBarry Smith { 4235dfbe8321SBarry Smith PetscErrorCode ierr; 4236d763cef2SBarry Smith 4237d763cef2SBarry Smith PetscFunctionBegin; 42380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 42394482741eSBarry Smith PetscValidPointer(prefix,2); 4240d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4241d763cef2SBarry Smith PetscFunctionReturn(0); 4242d763cef2SBarry Smith } 4243d763cef2SBarry Smith 4244d763cef2SBarry Smith /*@C 4245d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4246d763cef2SBarry Smith 4247d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4248d763cef2SBarry Smith 4249d763cef2SBarry Smith Input Parameter: 4250d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4251d763cef2SBarry Smith 4252d763cef2SBarry Smith Output Parameters: 4253e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4254e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4255e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4256e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4257d763cef2SBarry Smith 425895452b02SPatrick Sanan Notes: 425995452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 4260d763cef2SBarry Smith 4261d763cef2SBarry Smith Level: intermediate 4262d763cef2SBarry Smith 426380275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 4264d763cef2SBarry Smith 4265d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 4266d763cef2SBarry Smith @*/ 4267e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4268d763cef2SBarry Smith { 4269089b2837SJed Brown PetscErrorCode ierr; 427024989b8cSPeter Brune DM dm; 4271089b2837SJed Brown 4272d763cef2SBarry Smith PetscFunctionBegin; 427323a57915SBarry Smith if (Amat || Pmat) { 427423a57915SBarry Smith SNES snes; 4275089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 427623a57915SBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4277e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 427823a57915SBarry Smith } 427924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 428024989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 4281d763cef2SBarry Smith PetscFunctionReturn(0); 4282d763cef2SBarry Smith } 4283d763cef2SBarry Smith 42842eca1d9cSJed Brown /*@C 42852eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 42862eca1d9cSJed Brown 42872eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 42882eca1d9cSJed Brown 42892eca1d9cSJed Brown Input Parameter: 42902eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 42912eca1d9cSJed Brown 42922eca1d9cSJed Brown Output Parameters: 4293e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4294e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 42952eca1d9cSJed Brown . f - The function to compute the matrices 42962eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 42972eca1d9cSJed Brown 429895452b02SPatrick Sanan Notes: 429995452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 43002eca1d9cSJed Brown 43012eca1d9cSJed Brown Level: advanced 43022eca1d9cSJed Brown 430380275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 43042eca1d9cSJed Brown 43052eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 43062eca1d9cSJed Brown @*/ 4307e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 43082eca1d9cSJed Brown { 4309089b2837SJed Brown PetscErrorCode ierr; 431024989b8cSPeter Brune DM dm; 43110910c330SBarry Smith 43122eca1d9cSJed Brown PetscFunctionBegin; 4313c0aab802Sstefano_zampini if (Amat || Pmat) { 4314c0aab802Sstefano_zampini SNES snes; 4315089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4316f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4317e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 4318c0aab802Sstefano_zampini } 431924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 432024989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 43212eca1d9cSJed Brown PetscFunctionReturn(0); 43222eca1d9cSJed Brown } 43232eca1d9cSJed Brown 43241713a123SBarry Smith /*@C 432583a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 43261713a123SBarry Smith VecView() for the solution at each timestep 43271713a123SBarry Smith 43281713a123SBarry Smith Collective on TS 43291713a123SBarry Smith 43301713a123SBarry Smith Input Parameters: 43311713a123SBarry Smith + ts - the TS context 43321713a123SBarry Smith . step - current time-step 4333142b95e3SSatish Balay . ptime - current time 43340298fd71SBarry Smith - dummy - either a viewer or NULL 43351713a123SBarry Smith 433699fdda47SBarry Smith Options Database: 433799fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 433899fdda47SBarry Smith 433995452b02SPatrick Sanan Notes: 434095452b02SPatrick Sanan the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 434199fdda47SBarry Smith will look bad 434299fdda47SBarry Smith 43431713a123SBarry Smith Level: intermediate 43441713a123SBarry Smith 43451713a123SBarry Smith .keywords: TS, vector, monitor, view 43461713a123SBarry Smith 4347a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 43481713a123SBarry Smith @*/ 43490910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 43501713a123SBarry Smith { 4351dfbe8321SBarry Smith PetscErrorCode ierr; 435283a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4353473a3ab2SBarry Smith PetscDraw draw; 43541713a123SBarry Smith 43551713a123SBarry Smith PetscFunctionBegin; 43566083293cSBarry Smith if (!step && ictx->showinitial) { 43576083293cSBarry Smith if (!ictx->initialsolution) { 43580910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 43591713a123SBarry Smith } 43600910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 43616083293cSBarry Smith } 4362b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 43630dcf80beSBarry Smith 43646083293cSBarry Smith if (ictx->showinitial) { 43656083293cSBarry Smith PetscReal pause; 43666083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 43676083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 43686083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 43696083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 43706083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 43716083293cSBarry Smith } 43720910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 4373473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 437451fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 4375473a3ab2SBarry Smith char time[32]; 4376473a3ab2SBarry Smith 4377473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 437885b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4379473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4380473a3ab2SBarry Smith h = yl + .95*(yr - yl); 438151fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4382473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4383473a3ab2SBarry Smith } 4384473a3ab2SBarry Smith 43856083293cSBarry Smith if (ictx->showinitial) { 43866083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 43876083293cSBarry Smith } 43881713a123SBarry Smith PetscFunctionReturn(0); 43891713a123SBarry Smith } 43901713a123SBarry Smith 43919110b2e7SHong Zhang /*@C 43922d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 43932d5ee99bSBarry Smith 43942d5ee99bSBarry Smith Collective on TS 43952d5ee99bSBarry Smith 43962d5ee99bSBarry Smith Input Parameters: 43972d5ee99bSBarry Smith + ts - the TS context 43982d5ee99bSBarry Smith . step - current time-step 43992d5ee99bSBarry Smith . ptime - current time 44002d5ee99bSBarry Smith - dummy - either a viewer or NULL 44012d5ee99bSBarry Smith 44022d5ee99bSBarry Smith Level: intermediate 44032d5ee99bSBarry Smith 44042d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 44052d5ee99bSBarry Smith 44062d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 44072d5ee99bSBarry Smith @*/ 44082d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 44092d5ee99bSBarry Smith { 44102d5ee99bSBarry Smith PetscErrorCode ierr; 44112d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 44122d5ee99bSBarry Smith PetscDraw draw; 44136934998bSLisandro Dalcin PetscDrawAxis axis; 44142d5ee99bSBarry Smith PetscInt n; 44152d5ee99bSBarry Smith PetscMPIInt size; 44166934998bSLisandro Dalcin PetscReal U0,U1,xl,yl,xr,yr,h; 44172d5ee99bSBarry Smith char time[32]; 44182d5ee99bSBarry Smith const PetscScalar *U; 44192d5ee99bSBarry Smith 44202d5ee99bSBarry Smith PetscFunctionBegin; 44216934998bSLisandro Dalcin ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr); 44226934998bSLisandro Dalcin if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs"); 44232d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 44246934998bSLisandro Dalcin if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 44252d5ee99bSBarry Smith 44262d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 44276934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr); 44286934998bSLisandro Dalcin ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 44296934998bSLisandro Dalcin if (!step) { 44306934998bSLisandro Dalcin ierr = PetscDrawClear(draw);CHKERRQ(ierr); 44316934998bSLisandro Dalcin ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); 44326934998bSLisandro Dalcin } 44332d5ee99bSBarry Smith 44342d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 44356934998bSLisandro Dalcin U0 = PetscRealPart(U[0]); 44366934998bSLisandro Dalcin U1 = PetscRealPart(U[1]); 4437a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 44386934998bSLisandro Dalcin if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0); 44392d5ee99bSBarry Smith 44406934998bSLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 44416934998bSLisandro Dalcin ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr); 44422d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 44434b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 444485b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 44452d5ee99bSBarry Smith h = yl + .95*(yr - yl); 444651fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 44472d5ee99bSBarry Smith } 44486934998bSLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 44492d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 445056d62c8fSLisandro Dalcin ierr = PetscDrawPause(draw);CHKERRQ(ierr); 44516934998bSLisandro Dalcin ierr = PetscDrawSave(draw);CHKERRQ(ierr); 44522d5ee99bSBarry Smith PetscFunctionReturn(0); 44532d5ee99bSBarry Smith } 44542d5ee99bSBarry Smith 44556083293cSBarry Smith /*@C 445683a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 44576083293cSBarry Smith 44586083293cSBarry Smith Collective on TS 44596083293cSBarry Smith 44606083293cSBarry Smith Input Parameters: 44616083293cSBarry Smith . ctx - the monitor context 44626083293cSBarry Smith 44636083293cSBarry Smith Level: intermediate 44646083293cSBarry Smith 44656083293cSBarry Smith .keywords: TS, vector, monitor, view 44666083293cSBarry Smith 446783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 44686083293cSBarry Smith @*/ 446983a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 44706083293cSBarry Smith { 44716083293cSBarry Smith PetscErrorCode ierr; 44726083293cSBarry Smith 44736083293cSBarry Smith PetscFunctionBegin; 447483a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 447583a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 447683a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 44776083293cSBarry Smith PetscFunctionReturn(0); 44786083293cSBarry Smith } 44796083293cSBarry Smith 44806083293cSBarry Smith /*@C 448183a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 44826083293cSBarry Smith 44836083293cSBarry Smith Collective on TS 44846083293cSBarry Smith 44856083293cSBarry Smith Input Parameter: 44866083293cSBarry Smith . ts - time-step context 44876083293cSBarry Smith 44886083293cSBarry Smith Output Patameter: 44896083293cSBarry Smith . ctx - the monitor context 44906083293cSBarry Smith 449199fdda47SBarry Smith Options Database: 449299fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 449399fdda47SBarry Smith 44946083293cSBarry Smith Level: intermediate 44956083293cSBarry Smith 44966083293cSBarry Smith .keywords: TS, vector, monitor, view 44976083293cSBarry Smith 449883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 44996083293cSBarry Smith @*/ 450083a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 45016083293cSBarry Smith { 45026083293cSBarry Smith PetscErrorCode ierr; 45036083293cSBarry Smith 45046083293cSBarry Smith PetscFunctionBegin; 4505b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 450683a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4507e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4508bbd56ea5SKarl Rupp 450983a4ac43SBarry Smith (*ctx)->howoften = howoften; 4510473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 4511c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4512473a3ab2SBarry Smith 4513473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 4514c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 45156083293cSBarry Smith PetscFunctionReturn(0); 45166083293cSBarry Smith } 45176083293cSBarry Smith 45183a471f94SBarry Smith /*@C 45190ed3bfb6SBarry Smith TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling 45200ed3bfb6SBarry Smith VecView() for the solution provided by TSSetSolutionFunction() at each timestep 45210ed3bfb6SBarry Smith 45220ed3bfb6SBarry Smith Collective on TS 45230ed3bfb6SBarry Smith 45240ed3bfb6SBarry Smith Input Parameters: 45250ed3bfb6SBarry Smith + ts - the TS context 45260ed3bfb6SBarry Smith . step - current time-step 45270ed3bfb6SBarry Smith . ptime - current time 45280ed3bfb6SBarry Smith - dummy - either a viewer or NULL 45290ed3bfb6SBarry Smith 45300ed3bfb6SBarry Smith Options Database: 45310ed3bfb6SBarry Smith . -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 45320ed3bfb6SBarry Smith 45330ed3bfb6SBarry Smith Level: intermediate 45340ed3bfb6SBarry Smith 45350ed3bfb6SBarry Smith .keywords: TS, vector, monitor, view 45360ed3bfb6SBarry Smith 45370ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 45380ed3bfb6SBarry Smith @*/ 45390ed3bfb6SBarry Smith PetscErrorCode TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 45400ed3bfb6SBarry Smith { 45410ed3bfb6SBarry Smith PetscErrorCode ierr; 45420ed3bfb6SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 45430ed3bfb6SBarry Smith PetscViewer viewer = ctx->viewer; 45440ed3bfb6SBarry Smith Vec work; 45450ed3bfb6SBarry Smith 45460ed3bfb6SBarry Smith PetscFunctionBegin; 45470ed3bfb6SBarry Smith if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 45480ed3bfb6SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 45490ed3bfb6SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 45500ed3bfb6SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 45510ed3bfb6SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 45520ed3bfb6SBarry Smith PetscFunctionReturn(0); 45530ed3bfb6SBarry Smith } 45540ed3bfb6SBarry Smith 45550ed3bfb6SBarry Smith /*@C 455683a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 45573a471f94SBarry Smith VecView() for the error at each timestep 45583a471f94SBarry Smith 45593a471f94SBarry Smith Collective on TS 45603a471f94SBarry Smith 45613a471f94SBarry Smith Input Parameters: 45623a471f94SBarry Smith + ts - the TS context 45633a471f94SBarry Smith . step - current time-step 45643a471f94SBarry Smith . ptime - current time 45650298fd71SBarry Smith - dummy - either a viewer or NULL 45663a471f94SBarry Smith 45670ed3bfb6SBarry Smith Options Database: 45680ed3bfb6SBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 45690ed3bfb6SBarry Smith 45703a471f94SBarry Smith Level: intermediate 45713a471f94SBarry Smith 45723a471f94SBarry Smith .keywords: TS, vector, monitor, view 45733a471f94SBarry Smith 45740ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 45753a471f94SBarry Smith @*/ 45760910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 45773a471f94SBarry Smith { 45783a471f94SBarry Smith PetscErrorCode ierr; 457983a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 458083a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 45813a471f94SBarry Smith Vec work; 45823a471f94SBarry Smith 45833a471f94SBarry Smith PetscFunctionBegin; 4584b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 45850910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 45863a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 45870910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 45883a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 45893a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 45903a471f94SBarry Smith PetscFunctionReturn(0); 45913a471f94SBarry Smith } 45923a471f94SBarry Smith 4593af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 45946c699258SBarry Smith /*@ 45952a808120SBarry Smith TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS 45966c699258SBarry Smith 45973f9fe445SBarry Smith Logically Collective on TS and DM 45986c699258SBarry Smith 45996c699258SBarry Smith Input Parameters: 46002a808120SBarry Smith + ts - the ODE integrator object 46012a808120SBarry Smith - dm - the dm, cannot be NULL 46026c699258SBarry Smith 4603e03a659cSJed Brown Notes: 4604e03a659cSJed Brown A DM can only be used for solving one problem at a time because information about the problem is stored on the DM, 4605e03a659cSJed Brown even when not using interfaces like DMTSSetIFunction(). Use DMClone() to get a distinct DM when solving 4606e03a659cSJed Brown different problems using the same function space. 4607e03a659cSJed Brown 46086c699258SBarry Smith Level: intermediate 46096c699258SBarry Smith 46106c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 46116c699258SBarry Smith @*/ 46127087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 46136c699258SBarry Smith { 46146c699258SBarry Smith PetscErrorCode ierr; 4615089b2837SJed Brown SNES snes; 4616942e3340SBarry Smith DMTS tsdm; 46176c699258SBarry Smith 46186c699258SBarry Smith PetscFunctionBegin; 46190700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 46202a808120SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 462170663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4622942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 46232a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4624942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4625942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 462624989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 462724989b8cSPeter Brune tsdm->originaldm = dm; 462824989b8cSPeter Brune } 462924989b8cSPeter Brune } 46306bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 463124989b8cSPeter Brune } 46326c699258SBarry Smith ts->dm = dm; 4633bbd56ea5SKarl Rupp 4634089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4635089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 46366c699258SBarry Smith PetscFunctionReturn(0); 46376c699258SBarry Smith } 46386c699258SBarry Smith 46396c699258SBarry Smith /*@ 46406c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 46416c699258SBarry Smith 46423f9fe445SBarry Smith Not Collective 46436c699258SBarry Smith 46446c699258SBarry Smith Input Parameter: 46456c699258SBarry Smith . ts - the preconditioner context 46466c699258SBarry Smith 46476c699258SBarry Smith Output Parameter: 46486c699258SBarry Smith . dm - the dm 46496c699258SBarry Smith 46506c699258SBarry Smith Level: intermediate 46516c699258SBarry Smith 46526c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 46536c699258SBarry Smith @*/ 46547087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 46556c699258SBarry Smith { 4656496e6a7aSJed Brown PetscErrorCode ierr; 4657496e6a7aSJed Brown 46586c699258SBarry Smith PetscFunctionBegin; 46590700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4660496e6a7aSJed Brown if (!ts->dm) { 4661ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4662496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4663496e6a7aSJed Brown } 46646c699258SBarry Smith *dm = ts->dm; 46656c699258SBarry Smith PetscFunctionReturn(0); 46666c699258SBarry Smith } 46671713a123SBarry Smith 46680f5c6efeSJed Brown /*@ 46690f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 46700f5c6efeSJed Brown 46713f9fe445SBarry Smith Logically Collective on SNES 46720f5c6efeSJed Brown 46730f5c6efeSJed Brown Input Parameter: 4674d42a1c89SJed Brown + snes - nonlinear solver 46750910c330SBarry Smith . U - the current state at which to evaluate the residual 4676d42a1c89SJed Brown - ctx - user context, must be a TS 46770f5c6efeSJed Brown 46780f5c6efeSJed Brown Output Parameter: 46790f5c6efeSJed Brown . F - the nonlinear residual 46800f5c6efeSJed Brown 46810f5c6efeSJed Brown Notes: 46820f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 46830f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 46840f5c6efeSJed Brown 46850f5c6efeSJed Brown Level: advanced 46860f5c6efeSJed Brown 46870f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 46880f5c6efeSJed Brown @*/ 46890910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 46900f5c6efeSJed Brown { 46910f5c6efeSJed Brown TS ts = (TS)ctx; 46920f5c6efeSJed Brown PetscErrorCode ierr; 46930f5c6efeSJed Brown 46940f5c6efeSJed Brown PetscFunctionBegin; 46950f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 46960910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 46970f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 46980f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 46990910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 47000f5c6efeSJed Brown PetscFunctionReturn(0); 47010f5c6efeSJed Brown } 47020f5c6efeSJed Brown 47030f5c6efeSJed Brown /*@ 47040f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 47050f5c6efeSJed Brown 47060f5c6efeSJed Brown Collective on SNES 47070f5c6efeSJed Brown 47080f5c6efeSJed Brown Input Parameter: 47090f5c6efeSJed Brown + snes - nonlinear solver 47100910c330SBarry Smith . U - the current state at which to evaluate the residual 47110f5c6efeSJed Brown - ctx - user context, must be a TS 47120f5c6efeSJed Brown 47130f5c6efeSJed Brown Output Parameter: 47140f5c6efeSJed Brown + A - the Jacobian 47150f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 47160f5c6efeSJed Brown - flag - indicates any structure change in the matrix 47170f5c6efeSJed Brown 47180f5c6efeSJed Brown Notes: 47190f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 47200f5c6efeSJed Brown 47210f5c6efeSJed Brown Level: developer 47220f5c6efeSJed Brown 47230f5c6efeSJed Brown .seealso: SNESSetJacobian() 47240f5c6efeSJed Brown @*/ 4725d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 47260f5c6efeSJed Brown { 47270f5c6efeSJed Brown TS ts = (TS)ctx; 47280f5c6efeSJed Brown PetscErrorCode ierr; 47290f5c6efeSJed Brown 47300f5c6efeSJed Brown PetscFunctionBegin; 47310f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 47320910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 47330f5c6efeSJed Brown PetscValidPointer(A,3); 473494ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 47350f5c6efeSJed Brown PetscValidPointer(B,4); 473694ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 47370f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 4738d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 47390f5c6efeSJed Brown PetscFunctionReturn(0); 47400f5c6efeSJed Brown } 4741325fc9f4SBarry Smith 47420e4ef248SJed Brown /*@C 47439ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 47440e4ef248SJed Brown 47450e4ef248SJed Brown Collective on TS 47460e4ef248SJed Brown 47470e4ef248SJed Brown Input Arguments: 47480e4ef248SJed Brown + ts - time stepping context 47490e4ef248SJed Brown . t - time at which to evaluate 47500910c330SBarry Smith . U - state at which to evaluate 47510e4ef248SJed Brown - ctx - context 47520e4ef248SJed Brown 47530e4ef248SJed Brown Output Arguments: 47540e4ef248SJed Brown . F - right hand side 47550e4ef248SJed Brown 47560e4ef248SJed Brown Level: intermediate 47570e4ef248SJed Brown 47580e4ef248SJed Brown Notes: 47590e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 47600e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 47610e4ef248SJed Brown 47620e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 47630e4ef248SJed Brown @*/ 47640910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 47650e4ef248SJed Brown { 47660e4ef248SJed Brown PetscErrorCode ierr; 47670e4ef248SJed Brown Mat Arhs,Brhs; 47680e4ef248SJed Brown 47690e4ef248SJed Brown PetscFunctionBegin; 47700e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 4771d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 47720910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 47730e4ef248SJed Brown PetscFunctionReturn(0); 47740e4ef248SJed Brown } 47750e4ef248SJed Brown 47760e4ef248SJed Brown /*@C 47770e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 47780e4ef248SJed Brown 47790e4ef248SJed Brown Collective on TS 47800e4ef248SJed Brown 47810e4ef248SJed Brown Input Arguments: 47820e4ef248SJed Brown + ts - time stepping context 47830e4ef248SJed Brown . t - time at which to evaluate 47840910c330SBarry Smith . U - state at which to evaluate 47850e4ef248SJed Brown - ctx - context 47860e4ef248SJed Brown 47870e4ef248SJed Brown Output Arguments: 47880e4ef248SJed Brown + A - pointer to operator 47890e4ef248SJed Brown . B - pointer to preconditioning matrix 47900e4ef248SJed Brown - flg - matrix structure flag 47910e4ef248SJed Brown 47920e4ef248SJed Brown Level: intermediate 47930e4ef248SJed Brown 47940e4ef248SJed Brown Notes: 47950e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 47960e4ef248SJed Brown 47970e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 47980e4ef248SJed Brown @*/ 4799d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 48000e4ef248SJed Brown { 48010e4ef248SJed Brown PetscFunctionBegin; 48020e4ef248SJed Brown PetscFunctionReturn(0); 48030e4ef248SJed Brown } 48040e4ef248SJed Brown 48050026cea9SSean Farley /*@C 48060026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 48070026cea9SSean Farley 48080026cea9SSean Farley Collective on TS 48090026cea9SSean Farley 48100026cea9SSean Farley Input Arguments: 48110026cea9SSean Farley + ts - time stepping context 48120026cea9SSean Farley . t - time at which to evaluate 48130910c330SBarry Smith . U - state at which to evaluate 48140910c330SBarry Smith . Udot - time derivative of state vector 48150026cea9SSean Farley - ctx - context 48160026cea9SSean Farley 48170026cea9SSean Farley Output Arguments: 48180026cea9SSean Farley . F - left hand side 48190026cea9SSean Farley 48200026cea9SSean Farley Level: intermediate 48210026cea9SSean Farley 48220026cea9SSean Farley Notes: 48230910c330SBarry 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 48240026cea9SSean Farley user is required to write their own TSComputeIFunction. 48250026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 48260026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 48270026cea9SSean Farley 48289ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 48299ae8fd06SBarry Smith 48309ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 48310026cea9SSean Farley @*/ 48320910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 48330026cea9SSean Farley { 48340026cea9SSean Farley PetscErrorCode ierr; 48350026cea9SSean Farley Mat A,B; 48360026cea9SSean Farley 48370026cea9SSean Farley PetscFunctionBegin; 48380298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 4839d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 48400910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 48410026cea9SSean Farley PetscFunctionReturn(0); 48420026cea9SSean Farley } 48430026cea9SSean Farley 48440026cea9SSean Farley /*@C 4845b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 48460026cea9SSean Farley 48470026cea9SSean Farley Collective on TS 48480026cea9SSean Farley 48490026cea9SSean Farley Input Arguments: 48500026cea9SSean Farley + ts - time stepping context 48510026cea9SSean Farley . t - time at which to evaluate 48520910c330SBarry Smith . U - state at which to evaluate 48530910c330SBarry Smith . Udot - time derivative of state vector 48540026cea9SSean Farley . shift - shift to apply 48550026cea9SSean Farley - ctx - context 48560026cea9SSean Farley 48570026cea9SSean Farley Output Arguments: 48580026cea9SSean Farley + A - pointer to operator 48590026cea9SSean Farley . B - pointer to preconditioning matrix 48600026cea9SSean Farley - flg - matrix structure flag 48610026cea9SSean Farley 4862b41af12eSJed Brown Level: advanced 48630026cea9SSean Farley 48640026cea9SSean Farley Notes: 48650026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 48660026cea9SSean Farley 4867b41af12eSJed Brown It is only appropriate for problems of the form 4868b41af12eSJed Brown 4869b41af12eSJed Brown $ M Udot = F(U,t) 4870b41af12eSJed Brown 4871b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4872b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4873b41af12eSJed Brown an implicit operator of the form 4874b41af12eSJed Brown 4875b41af12eSJed Brown $ shift*M + J 4876b41af12eSJed Brown 4877b41af12eSJed 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 4878b41af12eSJed Brown a copy of M or reassemble it when requested. 4879b41af12eSJed Brown 48800026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 48810026cea9SSean Farley @*/ 4882d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 48830026cea9SSean Farley { 4884b41af12eSJed Brown PetscErrorCode ierr; 4885b41af12eSJed Brown 48860026cea9SSean Farley PetscFunctionBegin; 488794ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4888b41af12eSJed Brown ts->ijacobian.shift = shift; 48890026cea9SSean Farley PetscFunctionReturn(0); 48900026cea9SSean Farley } 4891b41af12eSJed Brown 4892e817cc15SEmil Constantinescu /*@ 4893e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 4894e817cc15SEmil Constantinescu 4895e817cc15SEmil Constantinescu Not Collective 4896e817cc15SEmil Constantinescu 4897e817cc15SEmil Constantinescu Input Parameter: 4898e817cc15SEmil Constantinescu . ts - the TS context 4899e817cc15SEmil Constantinescu 4900e817cc15SEmil Constantinescu Output Parameter: 49014e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4902e817cc15SEmil Constantinescu 4903e817cc15SEmil Constantinescu Level: beginner 4904e817cc15SEmil Constantinescu 4905e817cc15SEmil Constantinescu .keywords: TS, equation type 4906e817cc15SEmil Constantinescu 4907e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 4908e817cc15SEmil Constantinescu @*/ 4909e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4910e817cc15SEmil Constantinescu { 4911e817cc15SEmil Constantinescu PetscFunctionBegin; 4912e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4913e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 4914e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 4915e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4916e817cc15SEmil Constantinescu } 4917e817cc15SEmil Constantinescu 4918e817cc15SEmil Constantinescu /*@ 4919e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 4920e817cc15SEmil Constantinescu 4921e817cc15SEmil Constantinescu Not Collective 4922e817cc15SEmil Constantinescu 4923e817cc15SEmil Constantinescu Input Parameter: 4924e817cc15SEmil Constantinescu + ts - the TS context 49251297b224SEmil Constantinescu - equation_type - see TSEquationType 4926e817cc15SEmil Constantinescu 4927e817cc15SEmil Constantinescu Level: advanced 4928e817cc15SEmil Constantinescu 4929e817cc15SEmil Constantinescu .keywords: TS, equation type 4930e817cc15SEmil Constantinescu 4931e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 4932e817cc15SEmil Constantinescu @*/ 4933e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4934e817cc15SEmil Constantinescu { 4935e817cc15SEmil Constantinescu PetscFunctionBegin; 4936e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4937e817cc15SEmil Constantinescu ts->equation_type = equation_type; 4938e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4939e817cc15SEmil Constantinescu } 49400026cea9SSean Farley 49414af1b03aSJed Brown /*@ 49424af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 49434af1b03aSJed Brown 49444af1b03aSJed Brown Not Collective 49454af1b03aSJed Brown 49464af1b03aSJed Brown Input Parameter: 49474af1b03aSJed Brown . ts - the TS context 49484af1b03aSJed Brown 49494af1b03aSJed Brown Output Parameter: 49504af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 49514af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 49524af1b03aSJed Brown 4953487e0bb9SJed Brown Level: beginner 49544af1b03aSJed Brown 4955cd652676SJed Brown Notes: 4956cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 49574af1b03aSJed Brown 49584af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 49594af1b03aSJed Brown 49604af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 49614af1b03aSJed Brown @*/ 49624af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 49634af1b03aSJed Brown { 49644af1b03aSJed Brown PetscFunctionBegin; 49654af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 49664af1b03aSJed Brown PetscValidPointer(reason,2); 49674af1b03aSJed Brown *reason = ts->reason; 49684af1b03aSJed Brown PetscFunctionReturn(0); 49694af1b03aSJed Brown } 49704af1b03aSJed Brown 4971d6ad946cSShri Abhyankar /*@ 4972d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4973d6ad946cSShri Abhyankar 4974d6ad946cSShri Abhyankar Not Collective 4975d6ad946cSShri Abhyankar 4976d6ad946cSShri Abhyankar Input Parameter: 4977d6ad946cSShri Abhyankar + ts - the TS context 4978d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4979d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4980d6ad946cSShri Abhyankar 4981f5abba47SShri Abhyankar Level: advanced 4982d6ad946cSShri Abhyankar 4983d6ad946cSShri Abhyankar Notes: 4984d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 4985d6ad946cSShri Abhyankar 4986d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 4987d6ad946cSShri Abhyankar 4988d6ad946cSShri Abhyankar .seealso: TSConvergedReason 4989d6ad946cSShri Abhyankar @*/ 4990d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4991d6ad946cSShri Abhyankar { 4992d6ad946cSShri Abhyankar PetscFunctionBegin; 4993d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4994d6ad946cSShri Abhyankar ts->reason = reason; 4995d6ad946cSShri Abhyankar PetscFunctionReturn(0); 4996d6ad946cSShri Abhyankar } 4997d6ad946cSShri Abhyankar 4998cc708dedSBarry Smith /*@ 4999cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 5000cc708dedSBarry Smith 5001cc708dedSBarry Smith Not Collective 5002cc708dedSBarry Smith 5003cc708dedSBarry Smith Input Parameter: 5004cc708dedSBarry Smith . ts - the TS context 5005cc708dedSBarry Smith 5006cc708dedSBarry Smith Output Parameter: 500719eac22cSLisandro Dalcin . ftime - the final time. This time corresponds to the final time set with TSSetMaxTime() 5008cc708dedSBarry Smith 5009487e0bb9SJed Brown Level: beginner 5010cc708dedSBarry Smith 5011cc708dedSBarry Smith Notes: 5012cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 5013cc708dedSBarry Smith 5014cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 5015cc708dedSBarry Smith 5016cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 5017cc708dedSBarry Smith @*/ 5018cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 5019cc708dedSBarry Smith { 5020cc708dedSBarry Smith PetscFunctionBegin; 5021cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5022cc708dedSBarry Smith PetscValidPointer(ftime,2); 5023cc708dedSBarry Smith *ftime = ts->solvetime; 5024cc708dedSBarry Smith PetscFunctionReturn(0); 5025cc708dedSBarry Smith } 5026cc708dedSBarry Smith 50272c18e0fdSBarry Smith /*@ 50285ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 50299f67acb7SJed Brown used by the time integrator. 50309f67acb7SJed Brown 50319f67acb7SJed Brown Not Collective 50329f67acb7SJed Brown 50339f67acb7SJed Brown Input Parameter: 50349f67acb7SJed Brown . ts - TS context 50359f67acb7SJed Brown 50369f67acb7SJed Brown Output Parameter: 50379f67acb7SJed Brown . nits - number of nonlinear iterations 50389f67acb7SJed Brown 50399f67acb7SJed Brown Notes: 50409f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 50419f67acb7SJed Brown 50429f67acb7SJed Brown Level: intermediate 50439f67acb7SJed Brown 50449f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 50459f67acb7SJed Brown 50465ef26d82SJed Brown .seealso: TSGetKSPIterations() 50479f67acb7SJed Brown @*/ 50485ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 50499f67acb7SJed Brown { 50509f67acb7SJed Brown PetscFunctionBegin; 50519f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 50529f67acb7SJed Brown PetscValidIntPointer(nits,2); 50535ef26d82SJed Brown *nits = ts->snes_its; 50549f67acb7SJed Brown PetscFunctionReturn(0); 50559f67acb7SJed Brown } 50569f67acb7SJed Brown 50579f67acb7SJed Brown /*@ 50585ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 50599f67acb7SJed Brown used by the time integrator. 50609f67acb7SJed Brown 50619f67acb7SJed Brown Not Collective 50629f67acb7SJed Brown 50639f67acb7SJed Brown Input Parameter: 50649f67acb7SJed Brown . ts - TS context 50659f67acb7SJed Brown 50669f67acb7SJed Brown Output Parameter: 50679f67acb7SJed Brown . lits - number of linear iterations 50689f67acb7SJed Brown 50699f67acb7SJed Brown Notes: 50709f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 50719f67acb7SJed Brown 50729f67acb7SJed Brown Level: intermediate 50739f67acb7SJed Brown 50749f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 50759f67acb7SJed Brown 50765ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 50779f67acb7SJed Brown @*/ 50785ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 50799f67acb7SJed Brown { 50809f67acb7SJed Brown PetscFunctionBegin; 50819f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 50829f67acb7SJed Brown PetscValidIntPointer(lits,2); 50835ef26d82SJed Brown *lits = ts->ksp_its; 50849f67acb7SJed Brown PetscFunctionReturn(0); 50859f67acb7SJed Brown } 50869f67acb7SJed Brown 5087cef5090cSJed Brown /*@ 5088cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 5089cef5090cSJed Brown 5090cef5090cSJed Brown Not Collective 5091cef5090cSJed Brown 5092cef5090cSJed Brown Input Parameter: 5093cef5090cSJed Brown . ts - TS context 5094cef5090cSJed Brown 5095cef5090cSJed Brown Output Parameter: 5096cef5090cSJed Brown . rejects - number of steps rejected 5097cef5090cSJed Brown 5098cef5090cSJed Brown Notes: 5099cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5100cef5090cSJed Brown 5101cef5090cSJed Brown Level: intermediate 5102cef5090cSJed Brown 5103cef5090cSJed Brown .keywords: TS, get, number 5104cef5090cSJed Brown 51055ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 5106cef5090cSJed Brown @*/ 5107cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 5108cef5090cSJed Brown { 5109cef5090cSJed Brown PetscFunctionBegin; 5110cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5111cef5090cSJed Brown PetscValidIntPointer(rejects,2); 5112cef5090cSJed Brown *rejects = ts->reject; 5113cef5090cSJed Brown PetscFunctionReturn(0); 5114cef5090cSJed Brown } 5115cef5090cSJed Brown 5116cef5090cSJed Brown /*@ 5117cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 5118cef5090cSJed Brown 5119cef5090cSJed Brown Not Collective 5120cef5090cSJed Brown 5121cef5090cSJed Brown Input Parameter: 5122cef5090cSJed Brown . ts - TS context 5123cef5090cSJed Brown 5124cef5090cSJed Brown Output Parameter: 5125cef5090cSJed Brown . fails - number of failed nonlinear solves 5126cef5090cSJed Brown 5127cef5090cSJed Brown Notes: 5128cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5129cef5090cSJed Brown 5130cef5090cSJed Brown Level: intermediate 5131cef5090cSJed Brown 5132cef5090cSJed Brown .keywords: TS, get, number 5133cef5090cSJed Brown 51345ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 5135cef5090cSJed Brown @*/ 5136cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 5137cef5090cSJed Brown { 5138cef5090cSJed Brown PetscFunctionBegin; 5139cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5140cef5090cSJed Brown PetscValidIntPointer(fails,2); 5141cef5090cSJed Brown *fails = ts->num_snes_failures; 5142cef5090cSJed Brown PetscFunctionReturn(0); 5143cef5090cSJed Brown } 5144cef5090cSJed Brown 5145cef5090cSJed Brown /*@ 5146cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 5147cef5090cSJed Brown 5148cef5090cSJed Brown Not Collective 5149cef5090cSJed Brown 5150cef5090cSJed Brown Input Parameter: 5151cef5090cSJed Brown + ts - TS context 5152cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 5153cef5090cSJed Brown 5154cef5090cSJed Brown Notes: 5155cef5090cSJed Brown The counter is reset to zero for each step 5156cef5090cSJed Brown 5157cef5090cSJed Brown Options Database Key: 5158cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 5159cef5090cSJed Brown 5160cef5090cSJed Brown Level: intermediate 5161cef5090cSJed Brown 5162cef5090cSJed Brown .keywords: TS, set, maximum, number 5163cef5090cSJed Brown 51645ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5165cef5090cSJed Brown @*/ 5166cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 5167cef5090cSJed Brown { 5168cef5090cSJed Brown PetscFunctionBegin; 5169cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5170cef5090cSJed Brown ts->max_reject = rejects; 5171cef5090cSJed Brown PetscFunctionReturn(0); 5172cef5090cSJed Brown } 5173cef5090cSJed Brown 5174cef5090cSJed Brown /*@ 5175cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 5176cef5090cSJed Brown 5177cef5090cSJed Brown Not Collective 5178cef5090cSJed Brown 5179cef5090cSJed Brown Input Parameter: 5180cef5090cSJed Brown + ts - TS context 5181cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 5182cef5090cSJed Brown 5183cef5090cSJed Brown Notes: 5184cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 5185cef5090cSJed Brown 5186cef5090cSJed Brown Options Database Key: 5187cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 5188cef5090cSJed Brown 5189cef5090cSJed Brown Level: intermediate 5190cef5090cSJed Brown 5191cef5090cSJed Brown .keywords: TS, set, maximum, number 5192cef5090cSJed Brown 51935ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 5194cef5090cSJed Brown @*/ 5195cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 5196cef5090cSJed Brown { 5197cef5090cSJed Brown PetscFunctionBegin; 5198cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5199cef5090cSJed Brown ts->max_snes_failures = fails; 5200cef5090cSJed Brown PetscFunctionReturn(0); 5201cef5090cSJed Brown } 5202cef5090cSJed Brown 5203cef5090cSJed Brown /*@ 5204cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 5205cef5090cSJed Brown 5206cef5090cSJed Brown Not Collective 5207cef5090cSJed Brown 5208cef5090cSJed Brown Input Parameter: 5209cef5090cSJed Brown + ts - TS context 5210cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 5211cef5090cSJed Brown 5212cef5090cSJed Brown Options Database Key: 5213cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 5214cef5090cSJed Brown 5215cef5090cSJed Brown Level: intermediate 5216cef5090cSJed Brown 5217cef5090cSJed Brown .keywords: TS, set, error 5218cef5090cSJed Brown 52195ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5220cef5090cSJed Brown @*/ 5221cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 5222cef5090cSJed Brown { 5223cef5090cSJed Brown PetscFunctionBegin; 5224cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5225cef5090cSJed Brown ts->errorifstepfailed = err; 5226cef5090cSJed Brown PetscFunctionReturn(0); 5227cef5090cSJed Brown } 5228cef5090cSJed Brown 5229fb1732b5SBarry Smith /*@C 5230fde5950dSBarry 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 5231fb1732b5SBarry Smith 5232fb1732b5SBarry Smith Collective on TS 5233fb1732b5SBarry Smith 5234fb1732b5SBarry Smith Input Parameters: 5235fb1732b5SBarry Smith + ts - the TS context 5236fb1732b5SBarry Smith . step - current time-step 5237fb1732b5SBarry Smith . ptime - current time 52380910c330SBarry Smith . u - current state 5239721cd6eeSBarry Smith - vf - viewer and its format 5240fb1732b5SBarry Smith 5241fb1732b5SBarry Smith Level: intermediate 5242fb1732b5SBarry Smith 5243fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 5244fb1732b5SBarry Smith 5245fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5246fb1732b5SBarry Smith @*/ 5247721cd6eeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf) 5248fb1732b5SBarry Smith { 5249fb1732b5SBarry Smith PetscErrorCode ierr; 5250fb1732b5SBarry Smith 5251fb1732b5SBarry Smith PetscFunctionBegin; 5252721cd6eeSBarry Smith ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr); 5253721cd6eeSBarry Smith ierr = VecView(u,vf->viewer);CHKERRQ(ierr); 5254721cd6eeSBarry Smith ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr); 5255ed81e22dSJed Brown PetscFunctionReturn(0); 5256ed81e22dSJed Brown } 5257ed81e22dSJed Brown 5258ed81e22dSJed Brown /*@C 5259ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5260ed81e22dSJed Brown 5261ed81e22dSJed Brown Collective on TS 5262ed81e22dSJed Brown 5263ed81e22dSJed Brown Input Parameters: 5264ed81e22dSJed Brown + ts - the TS context 5265ed81e22dSJed Brown . step - current time-step 5266ed81e22dSJed Brown . ptime - current time 52670910c330SBarry Smith . u - current state 5268ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5269ed81e22dSJed Brown 5270ed81e22dSJed Brown Level: intermediate 5271ed81e22dSJed Brown 5272ed81e22dSJed Brown Notes: 5273ed81e22dSJed 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. 5274ed81e22dSJed Brown These are named according to the file name template. 5275ed81e22dSJed Brown 5276ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5277ed81e22dSJed Brown 5278ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5279ed81e22dSJed Brown 5280ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5281ed81e22dSJed Brown @*/ 52820910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5283ed81e22dSJed Brown { 5284ed81e22dSJed Brown PetscErrorCode ierr; 5285ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 5286ed81e22dSJed Brown PetscViewer viewer; 5287ed81e22dSJed Brown 5288ed81e22dSJed Brown PetscFunctionBegin; 528963e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 52908caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5291ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 52920910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 5293ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5294ed81e22dSJed Brown PetscFunctionReturn(0); 5295ed81e22dSJed Brown } 5296ed81e22dSJed Brown 5297ed81e22dSJed Brown /*@C 5298ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5299ed81e22dSJed Brown 5300ed81e22dSJed Brown Collective on TS 5301ed81e22dSJed Brown 5302ed81e22dSJed Brown Input Parameters: 5303ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5304ed81e22dSJed Brown 5305ed81e22dSJed Brown Level: intermediate 5306ed81e22dSJed Brown 5307ed81e22dSJed Brown Note: 5308ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5309ed81e22dSJed Brown 5310ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5311ed81e22dSJed Brown 5312ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5313ed81e22dSJed Brown @*/ 5314ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5315ed81e22dSJed Brown { 5316ed81e22dSJed Brown PetscErrorCode ierr; 5317ed81e22dSJed Brown 5318ed81e22dSJed Brown PetscFunctionBegin; 5319ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5320fb1732b5SBarry Smith PetscFunctionReturn(0); 5321fb1732b5SBarry Smith } 5322fb1732b5SBarry Smith 532384df9cb4SJed Brown /*@ 5324552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 532584df9cb4SJed Brown 5326ed81e22dSJed Brown Collective on TS if controller has not been created yet 532784df9cb4SJed Brown 532884df9cb4SJed Brown Input Arguments: 5329ed81e22dSJed Brown . ts - time stepping context 533084df9cb4SJed Brown 533184df9cb4SJed Brown Output Arguments: 5332ed81e22dSJed Brown . adapt - adaptive controller 533384df9cb4SJed Brown 533484df9cb4SJed Brown Level: intermediate 533584df9cb4SJed Brown 5336ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 533784df9cb4SJed Brown @*/ 5338552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 533984df9cb4SJed Brown { 534084df9cb4SJed Brown PetscErrorCode ierr; 534184df9cb4SJed Brown 534284df9cb4SJed Brown PetscFunctionBegin; 534384df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5344bec58848SLisandro Dalcin PetscValidPointer(adapt,2); 534584df9cb4SJed Brown if (!ts->adapt) { 5346ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 53473bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 53481c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 534984df9cb4SJed Brown } 5350bec58848SLisandro Dalcin *adapt = ts->adapt; 535184df9cb4SJed Brown PetscFunctionReturn(0); 535284df9cb4SJed Brown } 5353d6ebe24aSShri Abhyankar 53541c3436cfSJed Brown /*@ 53551c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 53561c3436cfSJed Brown 53571c3436cfSJed Brown Logically Collective 53581c3436cfSJed Brown 53591c3436cfSJed Brown Input Arguments: 53601c3436cfSJed Brown + ts - time integration context 53611c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 53620298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 53631c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 53640298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 53651c3436cfSJed Brown 5366a3cdaa26SBarry Smith Options Database keys: 5367a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5368a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5369a3cdaa26SBarry Smith 53703ff766beSShri Abhyankar Notes: 53713ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 53723ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 53733ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 53743ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 53753ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 53763ff766beSShri Abhyankar differential variables. 53773ff766beSShri Abhyankar 53781c3436cfSJed Brown Level: beginner 53791c3436cfSJed Brown 5380c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 53811c3436cfSJed Brown @*/ 53821c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 53831c3436cfSJed Brown { 53841c3436cfSJed Brown PetscErrorCode ierr; 53851c3436cfSJed Brown 53861c3436cfSJed Brown PetscFunctionBegin; 5387c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 53881c3436cfSJed Brown if (vatol) { 53891c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 53901c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 53911c3436cfSJed Brown ts->vatol = vatol; 53921c3436cfSJed Brown } 5393c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 53941c3436cfSJed Brown if (vrtol) { 53951c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 53961c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 53971c3436cfSJed Brown ts->vrtol = vrtol; 53981c3436cfSJed Brown } 53991c3436cfSJed Brown PetscFunctionReturn(0); 54001c3436cfSJed Brown } 54011c3436cfSJed Brown 5402c5033834SJed Brown /*@ 5403c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5404c5033834SJed Brown 5405c5033834SJed Brown Logically Collective 5406c5033834SJed Brown 5407c5033834SJed Brown Input Arguments: 5408c5033834SJed Brown . ts - time integration context 5409c5033834SJed Brown 5410c5033834SJed Brown Output Arguments: 54110298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 54120298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 54130298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 54140298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 5415c5033834SJed Brown 5416c5033834SJed Brown Level: beginner 5417c5033834SJed Brown 5418c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5419c5033834SJed Brown @*/ 5420c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5421c5033834SJed Brown { 5422c5033834SJed Brown PetscFunctionBegin; 5423c5033834SJed Brown if (atol) *atol = ts->atol; 5424c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5425c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5426c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 5427c5033834SJed Brown PetscFunctionReturn(0); 5428c5033834SJed Brown } 5429c5033834SJed Brown 54309c6b16b5SShri Abhyankar /*@ 5431a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 54329c6b16b5SShri Abhyankar 54339c6b16b5SShri Abhyankar Collective on TS 54349c6b16b5SShri Abhyankar 54359c6b16b5SShri Abhyankar Input Arguments: 54369c6b16b5SShri Abhyankar + ts - time stepping context 5437a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5438a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 54399c6b16b5SShri Abhyankar 54409c6b16b5SShri Abhyankar Output Arguments: 54417453f775SEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 54427453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 54437453f775SEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 54449c6b16b5SShri Abhyankar 54459c6b16b5SShri Abhyankar Level: developer 54469c6b16b5SShri Abhyankar 5447deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 54489c6b16b5SShri Abhyankar @*/ 54497453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 54509c6b16b5SShri Abhyankar { 54519c6b16b5SShri Abhyankar PetscErrorCode ierr; 54529c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 54537453f775SEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 54547453f775SEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 54559c6b16b5SShri Abhyankar const PetscScalar *u,*y; 54567453f775SEmil Constantinescu PetscReal sum,suma,sumr,gsum,gsuma,gsumr,diff; 54577453f775SEmil Constantinescu PetscReal tol,tola,tolr; 54587453f775SEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 54599c6b16b5SShri Abhyankar 54609c6b16b5SShri Abhyankar PetscFunctionBegin; 54619c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5462a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5463a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5464a4868fbcSLisandro Dalcin PetscValidType(U,2); 5465a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5466a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5467a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 54688a175baeSEmil Constantinescu PetscValidPointer(norma,5); 54698a175baeSEmil Constantinescu PetscValidPointer(normr,6); 5470a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 54719c6b16b5SShri Abhyankar 54729c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 54739c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 54749c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 54759c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 54769c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 54777453f775SEmil Constantinescu sum = 0.; n_loc = 0; 54787453f775SEmil Constantinescu suma = 0.; na_loc = 0; 54797453f775SEmil Constantinescu sumr = 0.; nr_loc = 0; 54809c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 54819c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 54829c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 54839c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 54849c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 54857453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 54867453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 54877453f775SEmil Constantinescu if(tola>0.){ 54887453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 54897453f775SEmil Constantinescu na_loc++; 54907453f775SEmil Constantinescu } 54917453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54927453f775SEmil Constantinescu if(tolr>0.){ 54937453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 54947453f775SEmil Constantinescu nr_loc++; 54957453f775SEmil Constantinescu } 54967453f775SEmil Constantinescu tol=tola+tolr; 54977453f775SEmil Constantinescu if(tol>0.){ 54987453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 54997453f775SEmil Constantinescu n_loc++; 55007453f775SEmil Constantinescu } 55019c6b16b5SShri Abhyankar } 55029c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 55039c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 55049c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 55059c6b16b5SShri Abhyankar const PetscScalar *atol; 55069c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 55079c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 55087453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 55097453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 55107453f775SEmil Constantinescu if(tola>0.){ 55117453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 55127453f775SEmil Constantinescu na_loc++; 55137453f775SEmil Constantinescu } 55147453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55157453f775SEmil Constantinescu if(tolr>0.){ 55167453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 55177453f775SEmil Constantinescu nr_loc++; 55187453f775SEmil Constantinescu } 55197453f775SEmil Constantinescu tol=tola+tolr; 55207453f775SEmil Constantinescu if(tol>0.){ 55217453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 55227453f775SEmil Constantinescu n_loc++; 55237453f775SEmil Constantinescu } 55249c6b16b5SShri Abhyankar } 55259c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 55269c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 55279c6b16b5SShri Abhyankar const PetscScalar *rtol; 55289c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 55299c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 55307453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 55317453f775SEmil Constantinescu tola = ts->atol; 55327453f775SEmil Constantinescu if(tola>0.){ 55337453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 55347453f775SEmil Constantinescu na_loc++; 55357453f775SEmil Constantinescu } 55367453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55377453f775SEmil Constantinescu if(tolr>0.){ 55387453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 55397453f775SEmil Constantinescu nr_loc++; 55407453f775SEmil Constantinescu } 55417453f775SEmil Constantinescu tol=tola+tolr; 55427453f775SEmil Constantinescu if(tol>0.){ 55437453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 55447453f775SEmil Constantinescu n_loc++; 55457453f775SEmil Constantinescu } 55469c6b16b5SShri Abhyankar } 55479c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 55489c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 55499c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 55507453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 55517453f775SEmil Constantinescu tola = ts->atol; 55527453f775SEmil Constantinescu if(tola>0.){ 55537453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 55547453f775SEmil Constantinescu na_loc++; 55557453f775SEmil Constantinescu } 55567453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55577453f775SEmil Constantinescu if(tolr>0.){ 55587453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 55597453f775SEmil Constantinescu nr_loc++; 55607453f775SEmil Constantinescu } 55617453f775SEmil Constantinescu tol=tola+tolr; 55627453f775SEmil Constantinescu if(tol>0.){ 55637453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 55647453f775SEmil Constantinescu n_loc++; 55657453f775SEmil Constantinescu } 55669c6b16b5SShri Abhyankar } 55679c6b16b5SShri Abhyankar } 55689c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 55699c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 55709c6b16b5SShri Abhyankar 55717453f775SEmil Constantinescu err_loc[0] = sum; 55727453f775SEmil Constantinescu err_loc[1] = suma; 55737453f775SEmil Constantinescu err_loc[2] = sumr; 55747453f775SEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 55757453f775SEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 55767453f775SEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 55777453f775SEmil Constantinescu 5578a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 55797453f775SEmil Constantinescu 55807453f775SEmil Constantinescu gsum = err_glb[0]; 55817453f775SEmil Constantinescu gsuma = err_glb[1]; 55827453f775SEmil Constantinescu gsumr = err_glb[2]; 55837453f775SEmil Constantinescu n_glb = err_glb[3]; 55847453f775SEmil Constantinescu na_glb = err_glb[4]; 55857453f775SEmil Constantinescu nr_glb = err_glb[5]; 55867453f775SEmil Constantinescu 5587b1316ef9SEmil Constantinescu *norm = 0.; 5588b1316ef9SEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 5589b1316ef9SEmil Constantinescu *norma = 0.; 5590b1316ef9SEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 5591b1316ef9SEmil Constantinescu *normr = 0.; 5592b1316ef9SEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 55939c6b16b5SShri Abhyankar 55949c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 55957453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 55967453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 55979c6b16b5SShri Abhyankar PetscFunctionReturn(0); 55989c6b16b5SShri Abhyankar } 55999c6b16b5SShri Abhyankar 56009c6b16b5SShri Abhyankar /*@ 5601a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 56029c6b16b5SShri Abhyankar 56039c6b16b5SShri Abhyankar Collective on TS 56049c6b16b5SShri Abhyankar 56059c6b16b5SShri Abhyankar Input Arguments: 56069c6b16b5SShri Abhyankar + ts - time stepping context 5607a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5608a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 56099c6b16b5SShri Abhyankar 56109c6b16b5SShri Abhyankar Output Arguments: 56117453f775SEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 56127453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 56137453f775SEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 56149c6b16b5SShri Abhyankar 56159c6b16b5SShri Abhyankar Level: developer 56169c6b16b5SShri Abhyankar 5617deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 56189c6b16b5SShri Abhyankar @*/ 56197453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 56209c6b16b5SShri Abhyankar { 56219c6b16b5SShri Abhyankar PetscErrorCode ierr; 56227453f775SEmil Constantinescu PetscInt i,n,N,rstart; 56239c6b16b5SShri Abhyankar const PetscScalar *u,*y; 56247453f775SEmil Constantinescu PetscReal max,gmax,maxa,gmaxa,maxr,gmaxr; 56257453f775SEmil Constantinescu PetscReal tol,tola,tolr,diff; 56267453f775SEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 56279c6b16b5SShri Abhyankar 56289c6b16b5SShri Abhyankar PetscFunctionBegin; 56299c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5630a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5631a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5632a4868fbcSLisandro Dalcin PetscValidType(U,2); 5633a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5634a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5635a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 56368a175baeSEmil Constantinescu PetscValidPointer(norma,5); 56378a175baeSEmil Constantinescu PetscValidPointer(normr,6); 5638a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 56399c6b16b5SShri Abhyankar 56409c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 56419c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 56429c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 56439c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 56449c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 56457453f775SEmil Constantinescu 56467453f775SEmil Constantinescu max=0.; 56477453f775SEmil Constantinescu maxa=0.; 56487453f775SEmil Constantinescu maxr=0.; 56497453f775SEmil Constantinescu 56507453f775SEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 56519c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 56529c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 56539c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 56547453f775SEmil Constantinescu 56557453f775SEmil Constantinescu for (i=0; i<n; i++) { 56567453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 56577453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 56587453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 56597453f775SEmil Constantinescu tol = tola+tolr; 56607453f775SEmil Constantinescu if(tola>0.){ 56617453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 56627453f775SEmil Constantinescu } 56637453f775SEmil Constantinescu if(tolr>0.){ 56647453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 56657453f775SEmil Constantinescu } 56667453f775SEmil Constantinescu if(tol>0.){ 56677453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 56687453f775SEmil Constantinescu } 56699c6b16b5SShri Abhyankar } 56709c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 56719c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 56729c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 56739c6b16b5SShri Abhyankar const PetscScalar *atol; 56749c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 56757453f775SEmil Constantinescu for (i=0; i<n; i++) { 56767453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 56777453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 56787453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 56797453f775SEmil Constantinescu tol = tola+tolr; 56807453f775SEmil Constantinescu if(tola>0.){ 56817453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 56827453f775SEmil Constantinescu } 56837453f775SEmil Constantinescu if(tolr>0.){ 56847453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 56857453f775SEmil Constantinescu } 56867453f775SEmil Constantinescu if(tol>0.){ 56877453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 56887453f775SEmil Constantinescu } 56899c6b16b5SShri Abhyankar } 56909c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 56919c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 56929c6b16b5SShri Abhyankar const PetscScalar *rtol; 56939c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 56947453f775SEmil Constantinescu 56957453f775SEmil Constantinescu for (i=0; i<n; i++) { 56967453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 56977453f775SEmil Constantinescu tola = ts->atol; 56987453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 56997453f775SEmil Constantinescu tol = tola+tolr; 57007453f775SEmil Constantinescu if(tola>0.){ 57017453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 57027453f775SEmil Constantinescu } 57037453f775SEmil Constantinescu if(tolr>0.){ 57047453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 57057453f775SEmil Constantinescu } 57067453f775SEmil Constantinescu if(tol>0.){ 57077453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 57087453f775SEmil Constantinescu } 57099c6b16b5SShri Abhyankar } 57109c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 57119c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 57127453f775SEmil Constantinescu 57137453f775SEmil Constantinescu for (i=0; i<n; i++) { 57147453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 57157453f775SEmil Constantinescu tola = ts->atol; 57167453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 57177453f775SEmil Constantinescu tol = tola+tolr; 57187453f775SEmil Constantinescu if(tola>0.){ 57197453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 57207453f775SEmil Constantinescu } 57217453f775SEmil Constantinescu if(tolr>0.){ 57227453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 57237453f775SEmil Constantinescu } 57247453f775SEmil Constantinescu if(tol>0.){ 57257453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 57267453f775SEmil Constantinescu } 57279c6b16b5SShri Abhyankar } 57289c6b16b5SShri Abhyankar } 57299c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 57309c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 57317453f775SEmil Constantinescu err_loc[0] = max; 57327453f775SEmil Constantinescu err_loc[1] = maxa; 57337453f775SEmil Constantinescu err_loc[2] = maxr; 5734a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 57357453f775SEmil Constantinescu gmax = err_glb[0]; 57367453f775SEmil Constantinescu gmaxa = err_glb[1]; 57377453f775SEmil Constantinescu gmaxr = err_glb[2]; 57389c6b16b5SShri Abhyankar 57399c6b16b5SShri Abhyankar *norm = gmax; 57407453f775SEmil Constantinescu *norma = gmaxa; 57417453f775SEmil Constantinescu *normr = gmaxr; 57429c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 57437453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 57447453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 57459c6b16b5SShri Abhyankar PetscFunctionReturn(0); 57469c6b16b5SShri Abhyankar } 57479c6b16b5SShri Abhyankar 57481c3436cfSJed Brown /*@ 57498a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 57501c3436cfSJed Brown 57511c3436cfSJed Brown Collective on TS 57521c3436cfSJed Brown 57531c3436cfSJed Brown Input Arguments: 57541c3436cfSJed Brown + ts - time stepping context 5755a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5756a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5757a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 57587619abb3SShri 57591c3436cfSJed Brown Output Arguments: 57608a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 57618a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 57628a175baeSEmil Constantinescu . normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 5763a4868fbcSLisandro Dalcin 5764a4868fbcSLisandro Dalcin Options Database Keys: 5765a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5766a4868fbcSLisandro Dalcin 57671c3436cfSJed Brown Level: developer 57681c3436cfSJed Brown 57698a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm 57701c3436cfSJed Brown @*/ 57717453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 57721c3436cfSJed Brown { 57738beabaa1SBarry Smith PetscErrorCode ierr; 57741c3436cfSJed Brown 57751c3436cfSJed Brown PetscFunctionBegin; 5776a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 57777453f775SEmil Constantinescu ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 5778a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 57797453f775SEmil Constantinescu ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 5780a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 57811c3436cfSJed Brown PetscFunctionReturn(0); 57821c3436cfSJed Brown } 57831c3436cfSJed Brown 57848a175baeSEmil Constantinescu 57858a175baeSEmil Constantinescu /*@ 57868a175baeSEmil Constantinescu TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances 57878a175baeSEmil Constantinescu 57888a175baeSEmil Constantinescu Collective on TS 57898a175baeSEmil Constantinescu 57908a175baeSEmil Constantinescu Input Arguments: 57918a175baeSEmil Constantinescu + ts - time stepping context 57928a175baeSEmil Constantinescu . E - error vector 57938a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 57948a175baeSEmil Constantinescu - Y - state vector, previous time step 57958a175baeSEmil Constantinescu 57968a175baeSEmil Constantinescu Output Arguments: 57978a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 57988a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 57998a175baeSEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 58008a175baeSEmil Constantinescu 58018a175baeSEmil Constantinescu Level: developer 58028a175baeSEmil Constantinescu 58038a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity() 58048a175baeSEmil Constantinescu @*/ 58058a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 58068a175baeSEmil Constantinescu { 58078a175baeSEmil Constantinescu PetscErrorCode ierr; 58088a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 58098a175baeSEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 58108a175baeSEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 58118a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 58128a175baeSEmil Constantinescu PetscReal err,sum,suma,sumr,gsum,gsuma,gsumr; 58138a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 58148a175baeSEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 58158a175baeSEmil Constantinescu 58168a175baeSEmil Constantinescu PetscFunctionBegin; 58178a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 58188a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 58198a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 58208a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 58218a175baeSEmil Constantinescu PetscValidType(E,2); 58228a175baeSEmil Constantinescu PetscValidType(U,3); 58238a175baeSEmil Constantinescu PetscValidType(Y,4); 58248a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 58258a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 58268a175baeSEmil Constantinescu PetscValidPointer(norm,5); 58278a175baeSEmil Constantinescu PetscValidPointer(norma,6); 58288a175baeSEmil Constantinescu PetscValidPointer(normr,7); 58298a175baeSEmil Constantinescu 58308a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 58318a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 58328a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 58338a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 58348a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 58358a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 58368a175baeSEmil Constantinescu sum = 0.; n_loc = 0; 58378a175baeSEmil Constantinescu suma = 0.; na_loc = 0; 58388a175baeSEmil Constantinescu sumr = 0.; nr_loc = 0; 58398a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { 58408a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 58418a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58428a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58438a175baeSEmil Constantinescu for (i=0; i<n; i++) { 58448a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 58458a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 58468a175baeSEmil Constantinescu if(tola>0.){ 58478a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 58488a175baeSEmil Constantinescu na_loc++; 58498a175baeSEmil Constantinescu } 58508a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58518a175baeSEmil Constantinescu if(tolr>0.){ 58528a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 58538a175baeSEmil Constantinescu nr_loc++; 58548a175baeSEmil Constantinescu } 58558a175baeSEmil Constantinescu tol=tola+tolr; 58568a175baeSEmil Constantinescu if(tol>0.){ 58578a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 58588a175baeSEmil Constantinescu n_loc++; 58598a175baeSEmil Constantinescu } 58608a175baeSEmil Constantinescu } 58618a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58628a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58638a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 58648a175baeSEmil Constantinescu const PetscScalar *atol; 58658a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58668a175baeSEmil Constantinescu for (i=0; i<n; i++) { 58678a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 58688a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 58698a175baeSEmil Constantinescu if(tola>0.){ 58708a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 58718a175baeSEmil Constantinescu na_loc++; 58728a175baeSEmil Constantinescu } 58738a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58748a175baeSEmil Constantinescu if(tolr>0.){ 58758a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 58768a175baeSEmil Constantinescu nr_loc++; 58778a175baeSEmil Constantinescu } 58788a175baeSEmil Constantinescu tol=tola+tolr; 58798a175baeSEmil Constantinescu if(tol>0.){ 58808a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 58818a175baeSEmil Constantinescu n_loc++; 58828a175baeSEmil Constantinescu } 58838a175baeSEmil Constantinescu } 58848a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58858a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 58868a175baeSEmil Constantinescu const PetscScalar *rtol; 58878a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58888a175baeSEmil Constantinescu for (i=0; i<n; i++) { 58898a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 58908a175baeSEmil Constantinescu tola = ts->atol; 58918a175baeSEmil Constantinescu if(tola>0.){ 58928a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 58938a175baeSEmil Constantinescu na_loc++; 58948a175baeSEmil Constantinescu } 58958a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58968a175baeSEmil Constantinescu if(tolr>0.){ 58978a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 58988a175baeSEmil Constantinescu nr_loc++; 58998a175baeSEmil Constantinescu } 59008a175baeSEmil Constantinescu tol=tola+tolr; 59018a175baeSEmil Constantinescu if(tol>0.){ 59028a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 59038a175baeSEmil Constantinescu n_loc++; 59048a175baeSEmil Constantinescu } 59058a175baeSEmil Constantinescu } 59068a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59078a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 59088a175baeSEmil Constantinescu for (i=0; i<n; i++) { 59098a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 59108a175baeSEmil Constantinescu tola = ts->atol; 59118a175baeSEmil Constantinescu if(tola>0.){ 59128a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 59138a175baeSEmil Constantinescu na_loc++; 59148a175baeSEmil Constantinescu } 59158a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59168a175baeSEmil Constantinescu if(tolr>0.){ 59178a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 59188a175baeSEmil Constantinescu nr_loc++; 59198a175baeSEmil Constantinescu } 59208a175baeSEmil Constantinescu tol=tola+tolr; 59218a175baeSEmil Constantinescu if(tol>0.){ 59228a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 59238a175baeSEmil Constantinescu n_loc++; 59248a175baeSEmil Constantinescu } 59258a175baeSEmil Constantinescu } 59268a175baeSEmil Constantinescu } 59278a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 59288a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 59298a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 59308a175baeSEmil Constantinescu 59318a175baeSEmil Constantinescu err_loc[0] = sum; 59328a175baeSEmil Constantinescu err_loc[1] = suma; 59338a175baeSEmil Constantinescu err_loc[2] = sumr; 59348a175baeSEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 59358a175baeSEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 59368a175baeSEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 59378a175baeSEmil Constantinescu 5938a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 59398a175baeSEmil Constantinescu 59408a175baeSEmil Constantinescu gsum = err_glb[0]; 59418a175baeSEmil Constantinescu gsuma = err_glb[1]; 59428a175baeSEmil Constantinescu gsumr = err_glb[2]; 59438a175baeSEmil Constantinescu n_glb = err_glb[3]; 59448a175baeSEmil Constantinescu na_glb = err_glb[4]; 59458a175baeSEmil Constantinescu nr_glb = err_glb[5]; 59468a175baeSEmil Constantinescu 59478a175baeSEmil Constantinescu *norm = 0.; 59488a175baeSEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 59498a175baeSEmil Constantinescu *norma = 0.; 59508a175baeSEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 59518a175baeSEmil Constantinescu *normr = 0.; 59528a175baeSEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 59538a175baeSEmil Constantinescu 59548a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 59558a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 59568a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 59578a175baeSEmil Constantinescu PetscFunctionReturn(0); 59588a175baeSEmil Constantinescu } 59598a175baeSEmil Constantinescu 59608a175baeSEmil Constantinescu /*@ 59618a175baeSEmil Constantinescu TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances 59628a175baeSEmil Constantinescu Collective on TS 59638a175baeSEmil Constantinescu 59648a175baeSEmil Constantinescu Input Arguments: 59658a175baeSEmil Constantinescu + ts - time stepping context 59668a175baeSEmil Constantinescu . E - error vector 59678a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 59688a175baeSEmil Constantinescu - Y - state vector, previous time step 59698a175baeSEmil Constantinescu 59708a175baeSEmil Constantinescu Output Arguments: 59718a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 59728a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 59738a175baeSEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 59748a175baeSEmil Constantinescu 59758a175baeSEmil Constantinescu Level: developer 59768a175baeSEmil Constantinescu 59778a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2() 59788a175baeSEmil Constantinescu @*/ 59798a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 59808a175baeSEmil Constantinescu { 59818a175baeSEmil Constantinescu PetscErrorCode ierr; 59828a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 59838a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 59848a175baeSEmil Constantinescu PetscReal err,max,gmax,maxa,gmaxa,maxr,gmaxr; 59858a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 59868a175baeSEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 59878a175baeSEmil Constantinescu 59888a175baeSEmil Constantinescu PetscFunctionBegin; 59898a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 59908a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 59918a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 59928a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 59938a175baeSEmil Constantinescu PetscValidType(E,2); 59948a175baeSEmil Constantinescu PetscValidType(U,3); 59958a175baeSEmil Constantinescu PetscValidType(Y,4); 59968a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 59978a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 59988a175baeSEmil Constantinescu PetscValidPointer(norm,5); 59998a175baeSEmil Constantinescu PetscValidPointer(norma,6); 60008a175baeSEmil Constantinescu PetscValidPointer(normr,7); 60018a175baeSEmil Constantinescu 60028a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 60038a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 60048a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 60058a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 60068a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 60078a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 60088a175baeSEmil Constantinescu 60098a175baeSEmil Constantinescu max=0.; 60108a175baeSEmil Constantinescu maxa=0.; 60118a175baeSEmil Constantinescu maxr=0.; 60128a175baeSEmil Constantinescu 60138a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 60148a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 60158a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 60168a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60178a175baeSEmil Constantinescu 60188a175baeSEmil Constantinescu for (i=0; i<n; i++) { 60198a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 60208a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 60218a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60228a175baeSEmil Constantinescu tol = tola+tolr; 60238a175baeSEmil Constantinescu if(tola>0.){ 60248a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 60258a175baeSEmil Constantinescu } 60268a175baeSEmil Constantinescu if(tolr>0.){ 60278a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 60288a175baeSEmil Constantinescu } 60298a175baeSEmil Constantinescu if(tol>0.){ 60308a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 60318a175baeSEmil Constantinescu } 60328a175baeSEmil Constantinescu } 60338a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 60348a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60358a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 60368a175baeSEmil Constantinescu const PetscScalar *atol; 60378a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 60388a175baeSEmil Constantinescu for (i=0; i<n; i++) { 60398a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 60408a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 60418a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60428a175baeSEmil Constantinescu tol = tola+tolr; 60438a175baeSEmil Constantinescu if(tola>0.){ 60448a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 60458a175baeSEmil Constantinescu } 60468a175baeSEmil Constantinescu if(tolr>0.){ 60478a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 60488a175baeSEmil Constantinescu } 60498a175baeSEmil Constantinescu if(tol>0.){ 60508a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 60518a175baeSEmil Constantinescu } 60528a175baeSEmil Constantinescu } 60538a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 60548a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 60558a175baeSEmil Constantinescu const PetscScalar *rtol; 60568a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60578a175baeSEmil Constantinescu 60588a175baeSEmil Constantinescu for (i=0; i<n; i++) { 60598a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 60608a175baeSEmil Constantinescu tola = ts->atol; 60618a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60628a175baeSEmil Constantinescu tol = tola+tolr; 60638a175baeSEmil Constantinescu if(tola>0.){ 60648a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 60658a175baeSEmil Constantinescu } 60668a175baeSEmil Constantinescu if(tolr>0.){ 60678a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 60688a175baeSEmil Constantinescu } 60698a175baeSEmil Constantinescu if(tol>0.){ 60708a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 60718a175baeSEmil Constantinescu } 60728a175baeSEmil Constantinescu } 60738a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 60748a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 60758a175baeSEmil Constantinescu 60768a175baeSEmil Constantinescu for (i=0; i<n; i++) { 60778a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 60788a175baeSEmil Constantinescu tola = ts->atol; 60798a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60808a175baeSEmil Constantinescu tol = tola+tolr; 60818a175baeSEmil Constantinescu if(tola>0.){ 60828a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 60838a175baeSEmil Constantinescu } 60848a175baeSEmil Constantinescu if(tolr>0.){ 60858a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 60868a175baeSEmil Constantinescu } 60878a175baeSEmil Constantinescu if(tol>0.){ 60888a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 60898a175baeSEmil Constantinescu } 60908a175baeSEmil Constantinescu } 60918a175baeSEmil Constantinescu } 60928a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 60938a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 60948a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 60958a175baeSEmil Constantinescu err_loc[0] = max; 60968a175baeSEmil Constantinescu err_loc[1] = maxa; 60978a175baeSEmil Constantinescu err_loc[2] = maxr; 6098a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 60998a175baeSEmil Constantinescu gmax = err_glb[0]; 61008a175baeSEmil Constantinescu gmaxa = err_glb[1]; 61018a175baeSEmil Constantinescu gmaxr = err_glb[2]; 61028a175baeSEmil Constantinescu 61038a175baeSEmil Constantinescu *norm = gmax; 61048a175baeSEmil Constantinescu *norma = gmaxa; 61058a175baeSEmil Constantinescu *normr = gmaxr; 61068a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 61078a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 61088a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 61098a175baeSEmil Constantinescu PetscFunctionReturn(0); 61108a175baeSEmil Constantinescu } 61118a175baeSEmil Constantinescu 61128a175baeSEmil Constantinescu /*@ 61138a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 61148a175baeSEmil Constantinescu 61158a175baeSEmil Constantinescu Collective on TS 61168a175baeSEmil Constantinescu 61178a175baeSEmil Constantinescu Input Arguments: 61188a175baeSEmil Constantinescu + ts - time stepping context 61198a175baeSEmil Constantinescu . E - error vector 61208a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 61218a175baeSEmil Constantinescu . Y - state vector, previous time step 61228a175baeSEmil Constantinescu - wnormtype - norm type, either NORM_2 or NORM_INFINITY 61238a175baeSEmil Constantinescu 61248a175baeSEmil Constantinescu Output Arguments: 61258a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 61268a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 61278a175baeSEmil Constantinescu . normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 61288a175baeSEmil Constantinescu 61298a175baeSEmil Constantinescu Options Database Keys: 61308a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 61318a175baeSEmil Constantinescu 61328a175baeSEmil Constantinescu Level: developer 61338a175baeSEmil Constantinescu 61348a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 61358a175baeSEmil Constantinescu @*/ 61368a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 61378a175baeSEmil Constantinescu { 61388a175baeSEmil Constantinescu PetscErrorCode ierr; 61398a175baeSEmil Constantinescu 61408a175baeSEmil Constantinescu PetscFunctionBegin; 61418a175baeSEmil Constantinescu if (wnormtype == NORM_2) { 61428a175baeSEmil Constantinescu ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 61438a175baeSEmil Constantinescu } else if(wnormtype == NORM_INFINITY) { 61448a175baeSEmil Constantinescu ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 61458a175baeSEmil Constantinescu } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 61468a175baeSEmil Constantinescu PetscFunctionReturn(0); 61478a175baeSEmil Constantinescu } 61488a175baeSEmil Constantinescu 61498a175baeSEmil Constantinescu 61508d59e960SJed Brown /*@ 61518d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 61528d59e960SJed Brown 61538d59e960SJed Brown Logically Collective on TS 61548d59e960SJed Brown 61558d59e960SJed Brown Input Arguments: 61568d59e960SJed Brown + ts - time stepping context 61578d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 61588d59e960SJed Brown 61598d59e960SJed Brown Note: 61608d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 61618d59e960SJed Brown 61628d59e960SJed Brown Level: intermediate 61638d59e960SJed Brown 61648d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 61658d59e960SJed Brown @*/ 61668d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 61678d59e960SJed Brown { 61688d59e960SJed Brown PetscFunctionBegin; 61698d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 61708d59e960SJed Brown ts->cfltime_local = cfltime; 61718d59e960SJed Brown ts->cfltime = -1.; 61728d59e960SJed Brown PetscFunctionReturn(0); 61738d59e960SJed Brown } 61748d59e960SJed Brown 61758d59e960SJed Brown /*@ 61768d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 61778d59e960SJed Brown 61788d59e960SJed Brown Collective on TS 61798d59e960SJed Brown 61808d59e960SJed Brown Input Arguments: 61818d59e960SJed Brown . ts - time stepping context 61828d59e960SJed Brown 61838d59e960SJed Brown Output Arguments: 61848d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 61858d59e960SJed Brown 61868d59e960SJed Brown Level: advanced 61878d59e960SJed Brown 61888d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 61898d59e960SJed Brown @*/ 61908d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 61918d59e960SJed Brown { 61928d59e960SJed Brown PetscErrorCode ierr; 61938d59e960SJed Brown 61948d59e960SJed Brown PetscFunctionBegin; 61958d59e960SJed Brown if (ts->cfltime < 0) { 6196b2566f29SBarry Smith ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 61978d59e960SJed Brown } 61988d59e960SJed Brown *cfltime = ts->cfltime; 61998d59e960SJed Brown PetscFunctionReturn(0); 62008d59e960SJed Brown } 62018d59e960SJed Brown 6202d6ebe24aSShri Abhyankar /*@ 6203d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 6204d6ebe24aSShri Abhyankar 6205d6ebe24aSShri Abhyankar Input Parameters: 6206d6ebe24aSShri Abhyankar . ts - the TS context. 6207d6ebe24aSShri Abhyankar . xl - lower bound. 6208d6ebe24aSShri Abhyankar . xu - upper bound. 6209d6ebe24aSShri Abhyankar 6210d6ebe24aSShri Abhyankar Notes: 6211d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 6212e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 6213d6ebe24aSShri Abhyankar 62142bd2b0e6SSatish Balay Level: advanced 62152bd2b0e6SSatish Balay 6216d6ebe24aSShri Abhyankar @*/ 6217d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 6218d6ebe24aSShri Abhyankar { 6219d6ebe24aSShri Abhyankar PetscErrorCode ierr; 6220d6ebe24aSShri Abhyankar SNES snes; 6221d6ebe24aSShri Abhyankar 6222d6ebe24aSShri Abhyankar PetscFunctionBegin; 6223d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 6224d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 6225d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 6226d6ebe24aSShri Abhyankar } 6227d6ebe24aSShri Abhyankar 6228325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 6229c6db04a5SJed Brown #include <mex.h> 6230325fc9f4SBarry Smith 6231325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 6232325fc9f4SBarry Smith 6233325fc9f4SBarry Smith /* 6234325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 6235325fc9f4SBarry Smith TSSetFunctionMatlab(). 6236325fc9f4SBarry Smith 6237325fc9f4SBarry Smith Collective on TS 6238325fc9f4SBarry Smith 6239325fc9f4SBarry Smith Input Parameters: 6240325fc9f4SBarry Smith + snes - the TS context 62410910c330SBarry Smith - u - input vector 6242325fc9f4SBarry Smith 6243325fc9f4SBarry Smith Output Parameter: 6244325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 6245325fc9f4SBarry Smith 6246325fc9f4SBarry Smith Notes: 6247325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 6248325fc9f4SBarry Smith implementations, so most users would not generally call this routine 6249325fc9f4SBarry Smith themselves. 6250325fc9f4SBarry Smith 6251325fc9f4SBarry Smith Level: developer 6252325fc9f4SBarry Smith 6253325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6254325fc9f4SBarry Smith 6255325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6256325fc9f4SBarry Smith */ 62570910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 6258325fc9f4SBarry Smith { 6259325fc9f4SBarry Smith PetscErrorCode ierr; 6260325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6261325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 6262325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 6263325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 6264325fc9f4SBarry Smith 6265325fc9f4SBarry Smith PetscFunctionBegin; 6266325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 62670910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 62680910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 6269325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 62700910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 6271325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 6272325fc9f4SBarry Smith 6273325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 62740910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 62750910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 62760910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 6277bbd56ea5SKarl Rupp 6278325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6279325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 6280325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6281325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6282325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 6283325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 6284325fc9f4SBarry Smith prhs[6] = sctx->ctx; 6285325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 6286325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6287325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6288325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6289325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6290325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6291325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6292325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6293325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6294325fc9f4SBarry Smith PetscFunctionReturn(0); 6295325fc9f4SBarry Smith } 6296325fc9f4SBarry Smith 6297325fc9f4SBarry Smith /* 6298325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 6299325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 6300e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 6301325fc9f4SBarry Smith 6302325fc9f4SBarry Smith Logically Collective on TS 6303325fc9f4SBarry Smith 6304325fc9f4SBarry Smith Input Parameters: 6305325fc9f4SBarry Smith + ts - the TS context 6306325fc9f4SBarry Smith - func - function evaluation routine 6307325fc9f4SBarry Smith 6308325fc9f4SBarry Smith Calling sequence of func: 63090910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 6310325fc9f4SBarry Smith 6311325fc9f4SBarry Smith Level: beginner 6312325fc9f4SBarry Smith 6313325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6314325fc9f4SBarry Smith 6315325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6316325fc9f4SBarry Smith */ 6317cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 6318325fc9f4SBarry Smith { 6319325fc9f4SBarry Smith PetscErrorCode ierr; 6320325fc9f4SBarry Smith TSMatlabContext *sctx; 6321325fc9f4SBarry Smith 6322325fc9f4SBarry Smith PetscFunctionBegin; 6323325fc9f4SBarry Smith /* currently sctx is memory bleed */ 632495dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6325325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6326325fc9f4SBarry Smith /* 6327325fc9f4SBarry Smith This should work, but it doesn't 6328325fc9f4SBarry Smith sctx->ctx = ctx; 6329325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6330325fc9f4SBarry Smith */ 6331325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6332bbd56ea5SKarl Rupp 63330298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 6334325fc9f4SBarry Smith PetscFunctionReturn(0); 6335325fc9f4SBarry Smith } 6336325fc9f4SBarry Smith 6337325fc9f4SBarry Smith /* 6338325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 6339325fc9f4SBarry Smith TSSetJacobianMatlab(). 6340325fc9f4SBarry Smith 6341325fc9f4SBarry Smith Collective on TS 6342325fc9f4SBarry Smith 6343325fc9f4SBarry Smith Input Parameters: 6344cdcf91faSSean Farley + ts - the TS context 63450910c330SBarry Smith . u - input vector 6346325fc9f4SBarry Smith . A, B - the matrices 6347325fc9f4SBarry Smith - ctx - user context 6348325fc9f4SBarry Smith 6349325fc9f4SBarry Smith Level: developer 6350325fc9f4SBarry Smith 6351325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6352325fc9f4SBarry Smith 6353325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6354325fc9f4SBarry Smith @*/ 6355f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 6356325fc9f4SBarry Smith { 6357325fc9f4SBarry Smith PetscErrorCode ierr; 6358325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6359325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 6360325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 6361325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 6362325fc9f4SBarry Smith 6363325fc9f4SBarry Smith PetscFunctionBegin; 6364cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 63650910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 6366325fc9f4SBarry Smith 63670910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 6368325fc9f4SBarry Smith 6369cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 63700910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 63710910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 63720910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 63730910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 6374bbd56ea5SKarl Rupp 6375325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6376325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 6377325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6378325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6379325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 6380325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 6381325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 6382325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 6383325fc9f4SBarry Smith prhs[8] = sctx->ctx; 6384325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 6385325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6386325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6387325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6388325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6389325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6390325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6391325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6392325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 6393325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 6394325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6395325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 6396325fc9f4SBarry Smith PetscFunctionReturn(0); 6397325fc9f4SBarry Smith } 6398325fc9f4SBarry Smith 6399325fc9f4SBarry Smith /* 6400325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 6401e3c5b3baSBarry Smith vector for use by the TS routines in solving ODEs from MATLAB. Here the function is a string containing the name of a MATLAB function 6402325fc9f4SBarry Smith 6403325fc9f4SBarry Smith Logically Collective on TS 6404325fc9f4SBarry Smith 6405325fc9f4SBarry Smith Input Parameters: 6406cdcf91faSSean Farley + ts - the TS context 6407325fc9f4SBarry Smith . A,B - Jacobian matrices 6408325fc9f4SBarry Smith . func - function evaluation routine 6409325fc9f4SBarry Smith - ctx - user context 6410325fc9f4SBarry Smith 6411325fc9f4SBarry Smith Calling sequence of func: 64120910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 6413325fc9f4SBarry Smith 6414325fc9f4SBarry Smith Level: developer 6415325fc9f4SBarry Smith 6416325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6417325fc9f4SBarry Smith 6418325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6419325fc9f4SBarry Smith */ 6420cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 6421325fc9f4SBarry Smith { 6422325fc9f4SBarry Smith PetscErrorCode ierr; 6423325fc9f4SBarry Smith TSMatlabContext *sctx; 6424325fc9f4SBarry Smith 6425325fc9f4SBarry Smith PetscFunctionBegin; 6426325fc9f4SBarry Smith /* currently sctx is memory bleed */ 642795dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6428325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6429325fc9f4SBarry Smith /* 6430325fc9f4SBarry Smith This should work, but it doesn't 6431325fc9f4SBarry Smith sctx->ctx = ctx; 6432325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6433325fc9f4SBarry Smith */ 6434325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6435bbd56ea5SKarl Rupp 6436cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 6437325fc9f4SBarry Smith PetscFunctionReturn(0); 6438325fc9f4SBarry Smith } 6439325fc9f4SBarry Smith 6440b5b1a830SBarry Smith /* 6441b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 6442b5b1a830SBarry Smith 6443b5b1a830SBarry Smith Collective on TS 6444b5b1a830SBarry Smith 6445b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6446b5b1a830SBarry Smith @*/ 64470910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 6448b5b1a830SBarry Smith { 6449b5b1a830SBarry Smith PetscErrorCode ierr; 6450b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6451a530c242SBarry Smith int nlhs = 1,nrhs = 6; 6452b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 6453b5b1a830SBarry Smith long long int lx = 0,ls = 0; 6454b5b1a830SBarry Smith 6455b5b1a830SBarry Smith PetscFunctionBegin; 6456cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 64570910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 6458b5b1a830SBarry Smith 6459cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 64600910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 6461bbd56ea5SKarl Rupp 6462b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6463b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 6464b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 6465b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 6466b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 6467b5b1a830SBarry Smith prhs[5] = sctx->ctx; 6468b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 6469b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6470b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 6471b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 6472b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 6473b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 6474b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 6475b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 6476b5b1a830SBarry Smith PetscFunctionReturn(0); 6477b5b1a830SBarry Smith } 6478b5b1a830SBarry Smith 6479b5b1a830SBarry Smith /* 6480b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 6481b5b1a830SBarry Smith 6482b5b1a830SBarry Smith Level: developer 6483b5b1a830SBarry Smith 6484b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 6485b5b1a830SBarry Smith 6486b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6487b5b1a830SBarry Smith */ 6488cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 6489b5b1a830SBarry Smith { 6490b5b1a830SBarry Smith PetscErrorCode ierr; 6491b5b1a830SBarry Smith TSMatlabContext *sctx; 6492b5b1a830SBarry Smith 6493b5b1a830SBarry Smith PetscFunctionBegin; 6494b5b1a830SBarry Smith /* currently sctx is memory bleed */ 649595dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6496b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6497b5b1a830SBarry Smith /* 6498b5b1a830SBarry Smith This should work, but it doesn't 6499b5b1a830SBarry Smith sctx->ctx = ctx; 6500b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6501b5b1a830SBarry Smith */ 6502b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6503bbd56ea5SKarl Rupp 65040298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 6505b5b1a830SBarry Smith PetscFunctionReturn(0); 6506b5b1a830SBarry Smith } 6507325fc9f4SBarry Smith #endif 6508b3603a34SBarry Smith 6509b3603a34SBarry Smith /*@C 65104f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 6511b3603a34SBarry Smith in a time based line graph 6512b3603a34SBarry Smith 6513b3603a34SBarry Smith Collective on TS 6514b3603a34SBarry Smith 6515b3603a34SBarry Smith Input Parameters: 6516b3603a34SBarry Smith + ts - the TS context 6517b3603a34SBarry Smith . step - current time-step 6518b3603a34SBarry Smith . ptime - current time 65197db568b7SBarry Smith . u - current solution 65207db568b7SBarry Smith - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 6521b3603a34SBarry Smith 6522b3d3934dSBarry Smith Options Database: 65239ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 6524b3d3934dSBarry Smith 6525b3603a34SBarry Smith Level: intermediate 6526b3603a34SBarry Smith 652795452b02SPatrick Sanan Notes: 652895452b02SPatrick Sanan Each process in a parallel run displays its component solutions in a separate window 6529b3603a34SBarry Smith 6530b3603a34SBarry Smith .keywords: TS, vector, monitor, view 6531b3603a34SBarry Smith 65327db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 65337db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 65347db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 65357db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 6536b3603a34SBarry Smith @*/ 65377db568b7SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6538b3603a34SBarry Smith { 6539b3603a34SBarry Smith PetscErrorCode ierr; 65407db568b7SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 6541b3603a34SBarry Smith const PetscScalar *yy; 654280666b62SBarry Smith Vec v; 6543b3603a34SBarry Smith 6544b3603a34SBarry Smith PetscFunctionBegin; 654563e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 654658ff32f7SBarry Smith if (!step) { 6547a9f9c1f6SBarry Smith PetscDrawAxis axis; 65486934998bSLisandro Dalcin PetscInt dim; 6549a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6550*1b575b74SJoseph Pusztay ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","X");CHKERRQ(ierr); 6551bab0a581SBarry Smith if (!ctx->names) { 6552bab0a581SBarry Smith PetscBool flg; 6553bab0a581SBarry Smith /* user provides names of variables to plot but no names has been set so assume names are integer values */ 6554bab0a581SBarry Smith ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr); 6555bab0a581SBarry Smith if (flg) { 6556bab0a581SBarry Smith PetscInt i,n; 6557bab0a581SBarry Smith char **names; 6558bab0a581SBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 6559bab0a581SBarry Smith ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr); 6560bab0a581SBarry Smith for (i=0; i<n; i++) { 6561bab0a581SBarry Smith ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr); 6562bab0a581SBarry Smith ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr); 6563bab0a581SBarry Smith } 6564bab0a581SBarry Smith names[n] = NULL; 6565bab0a581SBarry Smith ctx->names = names; 6566bab0a581SBarry Smith } 6567bab0a581SBarry Smith } 6568387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 6569387f4636SBarry Smith char **displaynames; 6570387f4636SBarry Smith PetscBool flg; 6571387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 657295dccacaSBarry Smith ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr); 6573387f4636SBarry Smith ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 6574c5929fdfSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 6575387f4636SBarry Smith if (flg) { 6576a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 6577387f4636SBarry Smith } 6578387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 6579387f4636SBarry Smith } 6580387f4636SBarry Smith if (ctx->displaynames) { 6581387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 6582387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 6583387f4636SBarry Smith } else if (ctx->names) { 65840910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 65850b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6586387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 6587b0bc92c6SBarry Smith } else { 6588b0bc92c6SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6589b0bc92c6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6590387f4636SBarry Smith } 65910b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 659258ff32f7SBarry Smith } 65936934998bSLisandro Dalcin 65946934998bSLisandro Dalcin if (!ctx->transform) v = u; 65956934998bSLisandro Dalcin else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);} 659680666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 65976934998bSLisandro Dalcin if (ctx->displaynames) { 65986934998bSLisandro Dalcin PetscInt i; 65996934998bSLisandro Dalcin for (i=0; i<ctx->ndisplayvariables; i++) 66006934998bSLisandro Dalcin ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]); 66016934998bSLisandro Dalcin ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 66026934998bSLisandro Dalcin } else { 6603e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6604e3efe391SJed Brown PetscInt i,n; 66056934998bSLisandro Dalcin PetscReal *yreal; 660680666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 6607785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6608e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 66090b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6610e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6611e3efe391SJed Brown #else 66120b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6613e3efe391SJed Brown #endif 661480666b62SBarry Smith } 66156934998bSLisandro Dalcin ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 66166934998bSLisandro Dalcin if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);} 66176934998bSLisandro Dalcin 6618b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 66190b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 66206934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 66213923b477SBarry Smith } 6622b3603a34SBarry Smith PetscFunctionReturn(0); 6623b3603a34SBarry Smith } 6624b3603a34SBarry Smith 6625b037adc7SBarry Smith /*@C 662631152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6627b037adc7SBarry Smith 6628b037adc7SBarry Smith Collective on TS 6629b037adc7SBarry Smith 6630b037adc7SBarry Smith Input Parameters: 6631b037adc7SBarry Smith + ts - the TS context 6632b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 6633b037adc7SBarry Smith 6634b037adc7SBarry Smith Level: intermediate 6635b037adc7SBarry Smith 663695452b02SPatrick Sanan Notes: 663795452b02SPatrick Sanan If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 66387db568b7SBarry Smith 6639b037adc7SBarry Smith .keywords: TS, vector, monitor, view 6640b037adc7SBarry Smith 6641a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 6642b037adc7SBarry Smith @*/ 664331152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 6644b037adc7SBarry Smith { 6645b037adc7SBarry Smith PetscErrorCode ierr; 6646b037adc7SBarry Smith PetscInt i; 6647b037adc7SBarry Smith 6648b037adc7SBarry Smith PetscFunctionBegin; 6649b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6650b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 66515537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 6652b3d3934dSBarry Smith break; 6653b3d3934dSBarry Smith } 6654b3d3934dSBarry Smith } 6655b3d3934dSBarry Smith PetscFunctionReturn(0); 6656b3d3934dSBarry Smith } 6657b3d3934dSBarry Smith 6658e673d494SBarry Smith /*@C 6659e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6660e673d494SBarry Smith 6661e673d494SBarry Smith Collective on TS 6662e673d494SBarry Smith 6663e673d494SBarry Smith Input Parameters: 6664e673d494SBarry Smith + ts - the TS context 6665e673d494SBarry Smith - names - the names of the components, final string must be NULL 6666e673d494SBarry Smith 6667e673d494SBarry Smith Level: intermediate 6668e673d494SBarry Smith 6669e673d494SBarry Smith .keywords: TS, vector, monitor, view 6670e673d494SBarry Smith 6671a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 6672e673d494SBarry Smith @*/ 6673e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 6674e673d494SBarry Smith { 6675e673d494SBarry Smith PetscErrorCode ierr; 6676e673d494SBarry Smith 6677e673d494SBarry Smith PetscFunctionBegin; 6678e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 6679e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 6680e673d494SBarry Smith PetscFunctionReturn(0); 6681e673d494SBarry Smith } 6682e673d494SBarry Smith 6683b3d3934dSBarry Smith /*@C 6684b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 6685b3d3934dSBarry Smith 6686b3d3934dSBarry Smith Collective on TS 6687b3d3934dSBarry Smith 6688b3d3934dSBarry Smith Input Parameter: 6689b3d3934dSBarry Smith . ts - the TS context 6690b3d3934dSBarry Smith 6691b3d3934dSBarry Smith Output Parameter: 6692b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 6693b3d3934dSBarry Smith 6694b3d3934dSBarry Smith Level: intermediate 6695b3d3934dSBarry Smith 669695452b02SPatrick Sanan Notes: 669795452b02SPatrick Sanan If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 66987db568b7SBarry Smith 6699b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6700b3d3934dSBarry Smith 6701b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6702b3d3934dSBarry Smith @*/ 6703b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 6704b3d3934dSBarry Smith { 6705b3d3934dSBarry Smith PetscInt i; 6706b3d3934dSBarry Smith 6707b3d3934dSBarry Smith PetscFunctionBegin; 6708b3d3934dSBarry Smith *names = NULL; 6709b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6710b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 67115537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 6712b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 6713b3d3934dSBarry Smith break; 6714387f4636SBarry Smith } 6715387f4636SBarry Smith } 6716387f4636SBarry Smith PetscFunctionReturn(0); 6717387f4636SBarry Smith } 6718387f4636SBarry Smith 6719a66092f1SBarry Smith /*@C 6720a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 6721a66092f1SBarry Smith 6722a66092f1SBarry Smith Collective on TS 6723a66092f1SBarry Smith 6724a66092f1SBarry Smith Input Parameters: 6725a66092f1SBarry Smith + ctx - the TSMonitorLG context 6726a66092f1SBarry Smith . displaynames - the names of the components, final string must be NULL 6727a66092f1SBarry Smith 6728a66092f1SBarry Smith Level: intermediate 6729a66092f1SBarry Smith 6730a66092f1SBarry Smith .keywords: TS, vector, monitor, view 6731a66092f1SBarry Smith 6732a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6733a66092f1SBarry Smith @*/ 6734a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 6735a66092f1SBarry Smith { 6736a66092f1SBarry Smith PetscInt j = 0,k; 6737a66092f1SBarry Smith PetscErrorCode ierr; 6738a66092f1SBarry Smith 6739a66092f1SBarry Smith PetscFunctionBegin; 6740a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 6741a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 6742a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 6743a66092f1SBarry Smith while (displaynames[j]) j++; 6744a66092f1SBarry Smith ctx->ndisplayvariables = j; 6745a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 6746a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 6747a66092f1SBarry Smith j = 0; 6748a66092f1SBarry Smith while (displaynames[j]) { 6749a66092f1SBarry Smith k = 0; 6750a66092f1SBarry Smith while (ctx->names[k]) { 6751a66092f1SBarry Smith PetscBool flg; 6752a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 6753a66092f1SBarry Smith if (flg) { 6754a66092f1SBarry Smith ctx->displayvariables[j] = k; 6755a66092f1SBarry Smith break; 6756a66092f1SBarry Smith } 6757a66092f1SBarry Smith k++; 6758a66092f1SBarry Smith } 6759a66092f1SBarry Smith j++; 6760a66092f1SBarry Smith } 6761a66092f1SBarry Smith PetscFunctionReturn(0); 6762a66092f1SBarry Smith } 6763a66092f1SBarry Smith 6764387f4636SBarry Smith /*@C 6765387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 6766387f4636SBarry Smith 6767387f4636SBarry Smith Collective on TS 6768387f4636SBarry Smith 6769387f4636SBarry Smith Input Parameters: 6770387f4636SBarry Smith + ts - the TS context 6771387f4636SBarry Smith . displaynames - the names of the components, final string must be NULL 6772387f4636SBarry Smith 677395452b02SPatrick Sanan Notes: 677495452b02SPatrick Sanan If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 67757db568b7SBarry Smith 6776387f4636SBarry Smith Level: intermediate 6777387f4636SBarry Smith 6778387f4636SBarry Smith .keywords: TS, vector, monitor, view 6779387f4636SBarry Smith 6780387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6781387f4636SBarry Smith @*/ 6782387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 6783387f4636SBarry Smith { 6784a66092f1SBarry Smith PetscInt i; 6785387f4636SBarry Smith PetscErrorCode ierr; 6786387f4636SBarry Smith 6787387f4636SBarry Smith PetscFunctionBegin; 6788387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6789387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 67905537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 6791b3d3934dSBarry Smith break; 6792b037adc7SBarry Smith } 6793b037adc7SBarry Smith } 6794b037adc7SBarry Smith PetscFunctionReturn(0); 6795b037adc7SBarry Smith } 6796b037adc7SBarry Smith 679780666b62SBarry Smith /*@C 679880666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 679980666b62SBarry Smith 680080666b62SBarry Smith Collective on TS 680180666b62SBarry Smith 680280666b62SBarry Smith Input Parameters: 680380666b62SBarry Smith + ts - the TS context 680480666b62SBarry Smith . transform - the transform function 68057684fa3eSBarry Smith . destroy - function to destroy the optional context 680680666b62SBarry Smith - ctx - optional context used by transform function 680780666b62SBarry Smith 680895452b02SPatrick Sanan Notes: 680995452b02SPatrick Sanan If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 68107db568b7SBarry Smith 681180666b62SBarry Smith Level: intermediate 681280666b62SBarry Smith 681380666b62SBarry Smith .keywords: TS, vector, monitor, view 681480666b62SBarry Smith 6815a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 681680666b62SBarry Smith @*/ 68177684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 681880666b62SBarry Smith { 681980666b62SBarry Smith PetscInt i; 6820a66092f1SBarry Smith PetscErrorCode ierr; 682180666b62SBarry Smith 682280666b62SBarry Smith PetscFunctionBegin; 682380666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 682480666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 68255537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 682680666b62SBarry Smith } 682780666b62SBarry Smith } 682880666b62SBarry Smith PetscFunctionReturn(0); 682980666b62SBarry Smith } 683080666b62SBarry Smith 6831e673d494SBarry Smith /*@C 6832e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 6833e673d494SBarry Smith 6834e673d494SBarry Smith Collective on TSLGCtx 6835e673d494SBarry Smith 6836e673d494SBarry Smith Input Parameters: 6837e673d494SBarry Smith + ts - the TS context 6838e673d494SBarry Smith . transform - the transform function 68397684fa3eSBarry Smith . destroy - function to destroy the optional context 6840e673d494SBarry Smith - ctx - optional context used by transform function 6841e673d494SBarry Smith 6842e673d494SBarry Smith Level: intermediate 6843e673d494SBarry Smith 6844e673d494SBarry Smith .keywords: TS, vector, monitor, view 6845e673d494SBarry Smith 6846a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 6847e673d494SBarry Smith @*/ 68487684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 6849e673d494SBarry Smith { 6850e673d494SBarry Smith PetscFunctionBegin; 6851e673d494SBarry Smith ctx->transform = transform; 68527684fa3eSBarry Smith ctx->transformdestroy = destroy; 6853e673d494SBarry Smith ctx->transformctx = tctx; 6854e673d494SBarry Smith PetscFunctionReturn(0); 6855e673d494SBarry Smith } 6856e673d494SBarry Smith 6857ef20d060SBarry Smith /*@C 68588b668821SLisandro Dalcin TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error 6859ef20d060SBarry Smith in a time based line graph 6860ef20d060SBarry Smith 6861ef20d060SBarry Smith Collective on TS 6862ef20d060SBarry Smith 6863ef20d060SBarry Smith Input Parameters: 6864ef20d060SBarry Smith + ts - the TS context 6865ef20d060SBarry Smith . step - current time-step 6866ef20d060SBarry Smith . ptime - current time 68677db568b7SBarry Smith . u - current solution 68687db568b7SBarry Smith - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 6869ef20d060SBarry Smith 6870ef20d060SBarry Smith Level: intermediate 6871ef20d060SBarry Smith 687295452b02SPatrick Sanan Notes: 687395452b02SPatrick Sanan Each process in a parallel run displays its component errors in a separate window 6874abd5a294SJed Brown 6875abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 6876abd5a294SJed Brown 6877abd5a294SJed Brown Options Database Keys: 68784f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 6879ef20d060SBarry Smith 6880ef20d060SBarry Smith .keywords: TS, vector, monitor, view 6881ef20d060SBarry Smith 6882abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 6883ef20d060SBarry Smith @*/ 68840910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 6885ef20d060SBarry Smith { 6886ef20d060SBarry Smith PetscErrorCode ierr; 68870b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 6888ef20d060SBarry Smith const PetscScalar *yy; 6889ef20d060SBarry Smith Vec y; 6890ef20d060SBarry Smith 6891ef20d060SBarry Smith PetscFunctionBegin; 6892a9f9c1f6SBarry Smith if (!step) { 6893a9f9c1f6SBarry Smith PetscDrawAxis axis; 68946934998bSLisandro Dalcin PetscInt dim; 6895a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 68968b668821SLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr); 68970910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6898a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6899a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6900a9f9c1f6SBarry Smith } 69010910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 6902ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 69030910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 6904ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 6905e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6906e3efe391SJed Brown { 6907e3efe391SJed Brown PetscReal *yreal; 6908e3efe391SJed Brown PetscInt i,n; 6909e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 6910785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6911e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 69120b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6913e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6914e3efe391SJed Brown } 6915e3efe391SJed Brown #else 69160b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6917e3efe391SJed Brown #endif 6918ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 6919ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 6920b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 69210b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 69226934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 69233923b477SBarry Smith } 6924ef20d060SBarry Smith PetscFunctionReturn(0); 6925ef20d060SBarry Smith } 6926ef20d060SBarry Smith 6927*1b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6928*1b575b74SJoseph Pusztay { 6929*1b575b74SJoseph Pusztay PetscErrorCode ierr; 6930*1b575b74SJoseph Pusztay TSMonitorSPCtx ctx = (TSMonitorSPCtx)dctx; 6931*1b575b74SJoseph Pusztay const PetscScalar *yy; 6932*1b575b74SJoseph Pusztay PetscScalar *y,*x; 6933*1b575b74SJoseph Pusztay PetscInt Np, p, dim=2; 6934*1b575b74SJoseph Pusztay DM dm; 6935*1b575b74SJoseph Pusztay 6936*1b575b74SJoseph Pusztay PetscFunctionBegin; 6937*1b575b74SJoseph Pusztay 6938*1b575b74SJoseph Pusztay if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 6939*1b575b74SJoseph Pusztay if (!step) { 6940*1b575b74SJoseph Pusztay PetscDrawAxis axis; 6941*1b575b74SJoseph Pusztay ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr); 6942*1b575b74SJoseph Pusztay ierr = PetscDrawAxisSetLabels(axis,"Particles","X","Y");CHKERRQ(ierr); 6943*1b575b74SJoseph Pusztay ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 6944*1b575b74SJoseph Pusztay ierr = DMGetDimension(dm, &dim); 6945*1b575b74SJoseph Pusztay if(dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");CHKERRQ(ierr); 6946*1b575b74SJoseph Pusztay ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr); 6947*1b575b74SJoseph Pusztay Np /= 2*dim; 6948*1b575b74SJoseph Pusztay ierr = PetscDrawSPSetDimension(ctx->sp, Np);CHKERRQ(ierr); 6949*1b575b74SJoseph Pusztay ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr); 6950*1b575b74SJoseph Pusztay } 6951*1b575b74SJoseph Pusztay 6952*1b575b74SJoseph Pusztay ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr); 6953*1b575b74SJoseph Pusztay Np /= 2*dim; 6954*1b575b74SJoseph Pusztay ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr); 6955*1b575b74SJoseph Pusztay ierr = PetscMalloc2(2*Np, &x, 2*Np, &y);CHKERRQ(ierr); 6956*1b575b74SJoseph Pusztay /* get points from solution vector */ 6957*1b575b74SJoseph Pusztay for (p=0; p<Np; ++p){ 6958*1b575b74SJoseph Pusztay 6959*1b575b74SJoseph Pusztay x[p] = yy[2*dim*p]; 6960*1b575b74SJoseph Pusztay y[p] = yy[2*dim*p+1]; 6961*1b575b74SJoseph Pusztay PetscPrintf(PETSC_COMM_SELF, "Val: %g\n", x[p]); 6962*1b575b74SJoseph Pusztay } 6963*1b575b74SJoseph Pusztay 6964*1b575b74SJoseph Pusztay ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr); 6965*1b575b74SJoseph Pusztay ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr); 6966*1b575b74SJoseph Pusztay 6967*1b575b74SJoseph Pusztay if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 6968*1b575b74SJoseph Pusztay ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr); 6969*1b575b74SJoseph Pusztay ierr = PetscDrawSPDraw(ctx->sp,PETSC_FALSE);CHKERRQ(ierr); 6970*1b575b74SJoseph Pusztay ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr); 6971*1b575b74SJoseph Pusztay } 6972*1b575b74SJoseph Pusztay 6973*1b575b74SJoseph Pusztay PetscFunctionReturn(0); 6974*1b575b74SJoseph Pusztay } 6975*1b575b74SJoseph Pusztay 6976*1b575b74SJoseph Pusztay 6977*1b575b74SJoseph Pusztay 69787cf37e64SBarry Smith /*@C 69797cf37e64SBarry Smith TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep 69807cf37e64SBarry Smith 69817cf37e64SBarry Smith Collective on TS 69827cf37e64SBarry Smith 69837cf37e64SBarry Smith Input Parameters: 69847cf37e64SBarry Smith + ts - the TS context 69857cf37e64SBarry Smith . step - current time-step 69867cf37e64SBarry Smith . ptime - current time 69877cf37e64SBarry Smith . u - current solution 69887cf37e64SBarry Smith - dctx - unused context 69897cf37e64SBarry Smith 69907cf37e64SBarry Smith Level: intermediate 69917cf37e64SBarry Smith 69927cf37e64SBarry Smith The user must provide the solution using TSSetSolutionFunction() to use this monitor. 69937cf37e64SBarry Smith 69947cf37e64SBarry Smith Options Database Keys: 69957cf37e64SBarry Smith . -ts_monitor_error - create a graphical monitor of error history 69967cf37e64SBarry Smith 69977cf37e64SBarry Smith .keywords: TS, vector, monitor, view 69987cf37e64SBarry Smith 69997cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 70007cf37e64SBarry Smith @*/ 7001edbaebb3SBarry Smith PetscErrorCode TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf) 70027cf37e64SBarry Smith { 70037cf37e64SBarry Smith PetscErrorCode ierr; 70047cf37e64SBarry Smith Vec y; 70057cf37e64SBarry Smith PetscReal nrm; 7006edbaebb3SBarry Smith PetscBool flg; 70077cf37e64SBarry Smith 70087cf37e64SBarry Smith PetscFunctionBegin; 70097cf37e64SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 70107cf37e64SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 70117cf37e64SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 7012edbaebb3SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr); 7013edbaebb3SBarry Smith if (flg) { 70147cf37e64SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 7015edbaebb3SBarry Smith ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr); 7016edbaebb3SBarry Smith } 7017edbaebb3SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr); 7018edbaebb3SBarry Smith if (flg) { 7019edbaebb3SBarry Smith ierr = VecView(y,vf->viewer);CHKERRQ(ierr); 7020edbaebb3SBarry Smith } 7021edbaebb3SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 70227cf37e64SBarry Smith PetscFunctionReturn(0); 70237cf37e64SBarry Smith } 70247cf37e64SBarry Smith 7025201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 7026201da799SBarry Smith { 7027201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 7028201da799SBarry Smith PetscReal x = ptime,y; 7029201da799SBarry Smith PetscErrorCode ierr; 7030201da799SBarry Smith PetscInt its; 7031201da799SBarry Smith 7032201da799SBarry Smith PetscFunctionBegin; 703363e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 7034201da799SBarry Smith if (!n) { 7035201da799SBarry Smith PetscDrawAxis axis; 7036201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 7037201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 7038201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 7039201da799SBarry Smith ctx->snes_its = 0; 7040201da799SBarry Smith } 7041201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 7042201da799SBarry Smith y = its - ctx->snes_its; 7043201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 70443fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 7045201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 70466934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 7047201da799SBarry Smith } 7048201da799SBarry Smith ctx->snes_its = its; 7049201da799SBarry Smith PetscFunctionReturn(0); 7050201da799SBarry Smith } 7051201da799SBarry Smith 7052201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 7053201da799SBarry Smith { 7054201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 7055201da799SBarry Smith PetscReal x = ptime,y; 7056201da799SBarry Smith PetscErrorCode ierr; 7057201da799SBarry Smith PetscInt its; 7058201da799SBarry Smith 7059201da799SBarry Smith PetscFunctionBegin; 706063e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 7061201da799SBarry Smith if (!n) { 7062201da799SBarry Smith PetscDrawAxis axis; 7063201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 7064201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 7065201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 7066201da799SBarry Smith ctx->ksp_its = 0; 7067201da799SBarry Smith } 7068201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 7069201da799SBarry Smith y = its - ctx->ksp_its; 7070201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 707199fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 7072201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 70736934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 7074201da799SBarry Smith } 7075201da799SBarry Smith ctx->ksp_its = its; 7076201da799SBarry Smith PetscFunctionReturn(0); 7077201da799SBarry Smith } 7078f9c1d6abSBarry Smith 7079f9c1d6abSBarry Smith /*@ 7080f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 7081f9c1d6abSBarry Smith 7082f9c1d6abSBarry Smith Collective on TS and Vec 7083f9c1d6abSBarry Smith 7084f9c1d6abSBarry Smith Input Parameters: 7085f9c1d6abSBarry Smith + ts - the TS context 7086f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 7087f9c1d6abSBarry Smith 7088f9c1d6abSBarry Smith Output Parameters: 7089f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 7090f9c1d6abSBarry Smith 7091f9c1d6abSBarry Smith Level: developer 7092f9c1d6abSBarry Smith 7093f9c1d6abSBarry Smith .keywords: TS, compute 7094f9c1d6abSBarry Smith 7095f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 7096f9c1d6abSBarry Smith @*/ 7097f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 7098f9c1d6abSBarry Smith { 7099f9c1d6abSBarry Smith PetscErrorCode ierr; 7100f9c1d6abSBarry Smith 7101f9c1d6abSBarry Smith PetscFunctionBegin; 7102f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7103ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 7104f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 7105f9c1d6abSBarry Smith PetscFunctionReturn(0); 7106f9c1d6abSBarry Smith } 710724655328SShri 7108b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 7109b3d3934dSBarry Smith /*@C 7110b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 7111b3d3934dSBarry Smith 7112b3d3934dSBarry Smith Collective on TS 7113b3d3934dSBarry Smith 7114b3d3934dSBarry Smith Input Parameters: 7115b3d3934dSBarry Smith . ts - the ODE solver object 7116b3d3934dSBarry Smith 7117b3d3934dSBarry Smith Output Parameter: 7118b3d3934dSBarry Smith . ctx - the context 7119b3d3934dSBarry Smith 7120b3d3934dSBarry Smith Level: intermediate 7121b3d3934dSBarry Smith 7122b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso 7123b3d3934dSBarry Smith 7124b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 7125b3d3934dSBarry Smith 7126b3d3934dSBarry Smith @*/ 7127b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 7128b3d3934dSBarry Smith { 7129b3d3934dSBarry Smith PetscErrorCode ierr; 7130b3d3934dSBarry Smith 7131b3d3934dSBarry Smith PetscFunctionBegin; 7132a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 7133b3d3934dSBarry Smith PetscFunctionReturn(0); 7134b3d3934dSBarry Smith } 7135b3d3934dSBarry Smith 7136b3d3934dSBarry Smith /*@C 7137b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 7138b3d3934dSBarry Smith 7139b3d3934dSBarry Smith Collective on TS 7140b3d3934dSBarry Smith 7141b3d3934dSBarry Smith Input Parameters: 7142b3d3934dSBarry Smith + ts - the TS context 7143b3d3934dSBarry Smith . step - current time-step 7144b3d3934dSBarry Smith . ptime - current time 71457db568b7SBarry Smith . u - current solution 71467db568b7SBarry Smith - dctx - the envelope context 7147b3d3934dSBarry Smith 7148b3d3934dSBarry Smith Options Database: 7149b3d3934dSBarry Smith . -ts_monitor_envelope 7150b3d3934dSBarry Smith 7151b3d3934dSBarry Smith Level: intermediate 7152b3d3934dSBarry Smith 715395452b02SPatrick Sanan Notes: 715495452b02SPatrick Sanan after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 7155b3d3934dSBarry Smith 7156b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7157b3d3934dSBarry Smith 71587db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 7159b3d3934dSBarry Smith @*/ 71607db568b7SBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 7161b3d3934dSBarry Smith { 7162b3d3934dSBarry Smith PetscErrorCode ierr; 71637db568b7SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 7164b3d3934dSBarry Smith 7165b3d3934dSBarry Smith PetscFunctionBegin; 7166b3d3934dSBarry Smith if (!ctx->max) { 7167b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 7168b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 7169b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 7170b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 7171b3d3934dSBarry Smith } else { 7172b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 7173b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 7174b3d3934dSBarry Smith } 7175b3d3934dSBarry Smith PetscFunctionReturn(0); 7176b3d3934dSBarry Smith } 7177b3d3934dSBarry Smith 7178b3d3934dSBarry Smith /*@C 7179b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 7180b3d3934dSBarry Smith 7181b3d3934dSBarry Smith Collective on TS 7182b3d3934dSBarry Smith 7183b3d3934dSBarry Smith Input Parameter: 7184b3d3934dSBarry Smith . ts - the TS context 7185b3d3934dSBarry Smith 7186b3d3934dSBarry Smith Output Parameter: 7187b3d3934dSBarry Smith + max - the maximum values 7188b3d3934dSBarry Smith - min - the minimum values 7189b3d3934dSBarry Smith 719095452b02SPatrick Sanan Notes: 719195452b02SPatrick Sanan If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 71927db568b7SBarry Smith 7193b3d3934dSBarry Smith Level: intermediate 7194b3d3934dSBarry Smith 7195b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 7196b3d3934dSBarry Smith 7197b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 7198b3d3934dSBarry Smith @*/ 7199b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 7200b3d3934dSBarry Smith { 7201b3d3934dSBarry Smith PetscInt i; 7202b3d3934dSBarry Smith 7203b3d3934dSBarry Smith PetscFunctionBegin; 7204b3d3934dSBarry Smith if (max) *max = NULL; 7205b3d3934dSBarry Smith if (min) *min = NULL; 7206b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7207b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 72085537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 7209b3d3934dSBarry Smith if (max) *max = ctx->max; 7210b3d3934dSBarry Smith if (min) *min = ctx->min; 7211b3d3934dSBarry Smith break; 7212b3d3934dSBarry Smith } 7213b3d3934dSBarry Smith } 7214b3d3934dSBarry Smith PetscFunctionReturn(0); 7215b3d3934dSBarry Smith } 7216b3d3934dSBarry Smith 7217b3d3934dSBarry Smith /*@C 7218b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 7219b3d3934dSBarry Smith 7220b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 7221b3d3934dSBarry Smith 7222b3d3934dSBarry Smith Input Parameter: 7223b3d3934dSBarry Smith . ctx - the monitor context 7224b3d3934dSBarry Smith 7225b3d3934dSBarry Smith Level: intermediate 7226b3d3934dSBarry Smith 7227b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy 7228b3d3934dSBarry Smith 72297db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 7230b3d3934dSBarry Smith @*/ 7231b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 7232b3d3934dSBarry Smith { 7233b3d3934dSBarry Smith PetscErrorCode ierr; 7234b3d3934dSBarry Smith 7235b3d3934dSBarry Smith PetscFunctionBegin; 7236b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 7237b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 7238b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 7239b3d3934dSBarry Smith PetscFunctionReturn(0); 7240b3d3934dSBarry Smith } 7241f2dee214SBarry Smith 724224655328SShri /*@ 7243dcb233daSLisandro Dalcin TSRestartStep - Flags the solver to restart the next step 7244dcb233daSLisandro Dalcin 7245dcb233daSLisandro Dalcin Collective on TS 7246dcb233daSLisandro Dalcin 7247dcb233daSLisandro Dalcin Input Parameter: 7248dcb233daSLisandro Dalcin . ts - the TS context obtained from TSCreate() 7249dcb233daSLisandro Dalcin 7250dcb233daSLisandro Dalcin Level: advanced 7251dcb233daSLisandro Dalcin 7252dcb233daSLisandro Dalcin Notes: 7253dcb233daSLisandro Dalcin Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of 7254dcb233daSLisandro Dalcin discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution 7255dcb233daSLisandro Dalcin vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For 7256dcb233daSLisandro Dalcin the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce 7257dcb233daSLisandro Dalcin discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with 7258dcb233daSLisandro Dalcin discontinuous source terms). 7259dcb233daSLisandro Dalcin 7260dcb233daSLisandro Dalcin .keywords: TS, timestep, restart 7261dcb233daSLisandro Dalcin 7262dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep() 7263dcb233daSLisandro Dalcin @*/ 7264dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts) 7265dcb233daSLisandro Dalcin { 7266dcb233daSLisandro Dalcin PetscFunctionBegin; 7267dcb233daSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7268dcb233daSLisandro Dalcin ts->steprestart = PETSC_TRUE; 7269dcb233daSLisandro Dalcin PetscFunctionReturn(0); 7270dcb233daSLisandro Dalcin } 7271dcb233daSLisandro Dalcin 7272dcb233daSLisandro Dalcin /*@ 727324655328SShri TSRollBack - Rolls back one time step 727424655328SShri 727524655328SShri Collective on TS 727624655328SShri 727724655328SShri Input Parameter: 727824655328SShri . ts - the TS context obtained from TSCreate() 727924655328SShri 728024655328SShri Level: advanced 728124655328SShri 728224655328SShri .keywords: TS, timestep, rollback 728324655328SShri 728424655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 728524655328SShri @*/ 728624655328SShri PetscErrorCode TSRollBack(TS ts) 728724655328SShri { 728824655328SShri PetscErrorCode ierr; 728924655328SShri 729024655328SShri PetscFunctionBegin; 729124655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7292b3de5cdeSLisandro Dalcin if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called"); 729324655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 729424655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 729524655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 729624655328SShri ts->ptime = ts->ptime_prev; 7297be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 72982808aa04SLisandro Dalcin ts->steps--; 7299b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 730024655328SShri PetscFunctionReturn(0); 730124655328SShri } 7302aeb4809dSShri Abhyankar 7303ff22ae23SHong Zhang /*@ 7304ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 7305ff22ae23SHong Zhang 7306ff22ae23SHong Zhang Input Parameter: 7307ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 7308ff22ae23SHong Zhang 7309ff22ae23SHong Zhang Level: advanced 7310ff22ae23SHong Zhang 7311ff22ae23SHong Zhang .keywords: TS, getstages 7312ff22ae23SHong Zhang 7313ff22ae23SHong Zhang .seealso: TSCreate() 7314ff22ae23SHong Zhang @*/ 7315ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns,Vec **Y) 7316ff22ae23SHong Zhang { 7317ff22ae23SHong Zhang PetscErrorCode ierr; 7318ff22ae23SHong Zhang 7319ff22ae23SHong Zhang PetscFunctionBegin; 7320ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7321ff22ae23SHong Zhang PetscValidPointer(ns,2); 7322ff22ae23SHong Zhang 7323ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 7324ff22ae23SHong Zhang else { 7325ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 7326ff22ae23SHong Zhang } 7327ff22ae23SHong Zhang PetscFunctionReturn(0); 7328ff22ae23SHong Zhang } 7329ff22ae23SHong Zhang 7330847ff0e1SMatthew G. Knepley /*@C 7331847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 7332847ff0e1SMatthew G. Knepley 7333847ff0e1SMatthew G. Knepley Collective on SNES 7334847ff0e1SMatthew G. Knepley 7335847ff0e1SMatthew G. Knepley Input Parameters: 7336847ff0e1SMatthew G. Knepley + ts - the TS context 7337847ff0e1SMatthew G. Knepley . t - current timestep 7338847ff0e1SMatthew G. Knepley . U - state vector 7339847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 7340847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 7341847ff0e1SMatthew G. Knepley - ctx - an optional user context 7342847ff0e1SMatthew G. Knepley 7343847ff0e1SMatthew G. Knepley Output Parameters: 7344847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 7345847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 7346847ff0e1SMatthew G. Knepley 7347847ff0e1SMatthew G. Knepley Level: intermediate 7348847ff0e1SMatthew G. Knepley 7349847ff0e1SMatthew G. Knepley Notes: 7350847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 7351847ff0e1SMatthew G. Knepley 7352847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 7353847ff0e1SMatthew G. Knepley 7354847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 7355847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 7356847ff0e1SMatthew G. Knepley 7357847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 7358847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 7359847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 7360847ff0e1SMatthew G. Knepley 7361847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse 7362847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 7363847ff0e1SMatthew G. Knepley @*/ 7364847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 7365847ff0e1SMatthew G. Knepley { 7366847ff0e1SMatthew G. Knepley SNES snes; 7367847ff0e1SMatthew G. Knepley MatFDColoring color; 7368847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 7369847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 7370847ff0e1SMatthew G. Knepley 7371847ff0e1SMatthew G. Knepley PetscFunctionBegin; 7372c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 7373847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 7374847ff0e1SMatthew G. Knepley if (!color) { 7375847ff0e1SMatthew G. Knepley DM dm; 7376847ff0e1SMatthew G. Knepley ISColoring iscoloring; 7377847ff0e1SMatthew G. Knepley 7378847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 7379847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 7380847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 7381847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 7382847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7383847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7384847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7385847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7386847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7387847ff0e1SMatthew G. Knepley } else { 7388847ff0e1SMatthew G. Knepley MatColoring mc; 7389847ff0e1SMatthew G. Knepley 7390847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 7391847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 7392847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 7393847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 7394847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 7395847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 7396847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7397847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7398847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7399847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7400847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7401847ff0e1SMatthew G. Knepley } 7402847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 7403847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 7404847ff0e1SMatthew G. Knepley } 7405847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 7406847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 7407847ff0e1SMatthew G. Knepley if (J != B) { 7408847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7409847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7410847ff0e1SMatthew G. Knepley } 7411847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 7412847ff0e1SMatthew G. Knepley } 741393b34091SDebojyoti Ghosh 7414cb9d8021SPierre Barbier de Reuille /*@ 7415cb9d8021SPierre Barbier de Reuille TSSetFunctionDomainError - Set the function testing if the current state vector is valid 7416cb9d8021SPierre Barbier de Reuille 7417cb9d8021SPierre Barbier de Reuille Input Parameters: 7418cb9d8021SPierre Barbier de Reuille ts - the TS context 7419cb9d8021SPierre Barbier de Reuille func - function called within TSFunctionDomainError 7420cb9d8021SPierre Barbier de Reuille 7421cb9d8021SPierre Barbier de Reuille Level: intermediate 7422cb9d8021SPierre Barbier de Reuille 7423cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain 7424cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError() 7425cb9d8021SPierre Barbier de Reuille @*/ 7426cb9d8021SPierre Barbier de Reuille 7427d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 7428cb9d8021SPierre Barbier de Reuille { 7429cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7430cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7431cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 7432cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7433cb9d8021SPierre Barbier de Reuille } 7434cb9d8021SPierre Barbier de Reuille 7435cb9d8021SPierre Barbier de Reuille /*@ 7436cb9d8021SPierre Barbier de Reuille TSFunctionDomainError - Check if the current state is valid 7437cb9d8021SPierre Barbier de Reuille 7438cb9d8021SPierre Barbier de Reuille Input Parameters: 7439cb9d8021SPierre Barbier de Reuille ts - the TS context 7440cb9d8021SPierre Barbier de Reuille stagetime - time of the simulation 7441d183316bSPierre Barbier de Reuille Y - state vector to check. 7442cb9d8021SPierre Barbier de Reuille 7443cb9d8021SPierre Barbier de Reuille Output Parameter: 7444cb9d8021SPierre Barbier de Reuille accept - Set to PETSC_FALSE if the current state vector is valid. 7445cb9d8021SPierre Barbier de Reuille 7446cb9d8021SPierre Barbier de Reuille Note: 7447cb9d8021SPierre Barbier de Reuille This function should be used to ensure the state is in a valid part of the space. 7448cb9d8021SPierre Barbier de Reuille For example, one can ensure here all values are positive. 744996a0c994SBarry Smith 745096a0c994SBarry Smith Level: advanced 7451cb9d8021SPierre Barbier de Reuille @*/ 7452d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 7453cb9d8021SPierre Barbier de Reuille { 7454cb9d8021SPierre Barbier de Reuille PetscErrorCode ierr; 7455cb9d8021SPierre Barbier de Reuille 7456cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7457cb9d8021SPierre Barbier de Reuille 7458cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7459cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 7460cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 7461d183316bSPierre Barbier de Reuille PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 7462cb9d8021SPierre Barbier de Reuille } 7463cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7464cb9d8021SPierre Barbier de Reuille } 74651ceb14c0SBarry Smith 746693b34091SDebojyoti Ghosh /*@C 7467e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 746893b34091SDebojyoti Ghosh 746993b34091SDebojyoti Ghosh Collective on MPI_Comm 747093b34091SDebojyoti Ghosh 747193b34091SDebojyoti Ghosh Input Parameter: 747293b34091SDebojyoti Ghosh . tsin - The input TS 747393b34091SDebojyoti Ghosh 747493b34091SDebojyoti Ghosh Output Parameter: 7475e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 747693b34091SDebojyoti Ghosh 74775eca1a21SEmil Constantinescu Notes: 74785eca1a21SEmil 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. 74795eca1a21SEmil Constantinescu 7480928bb9adSStefano 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); 74815eca1a21SEmil Constantinescu 74825eca1a21SEmil Constantinescu Level: developer 748393b34091SDebojyoti Ghosh 7484e5168f73SEmil Constantinescu .keywords: TS, clone 7485e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 748693b34091SDebojyoti Ghosh @*/ 7487baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 748893b34091SDebojyoti Ghosh { 748993b34091SDebojyoti Ghosh TS t; 749093b34091SDebojyoti Ghosh PetscErrorCode ierr; 7491dc846ba4SSatish Balay SNES snes_start; 7492dc846ba4SSatish Balay DM dm; 7493dc846ba4SSatish Balay TSType type; 749493b34091SDebojyoti Ghosh 749593b34091SDebojyoti Ghosh PetscFunctionBegin; 749693b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 749793b34091SDebojyoti Ghosh *tsout = NULL; 749893b34091SDebojyoti Ghosh 74997a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 750093b34091SDebojyoti Ghosh 750193b34091SDebojyoti Ghosh /* General TS description */ 750293b34091SDebojyoti Ghosh t->numbermonitors = 0; 750393b34091SDebojyoti Ghosh t->setupcalled = 0; 750493b34091SDebojyoti Ghosh t->ksp_its = 0; 750593b34091SDebojyoti Ghosh t->snes_its = 0; 750693b34091SDebojyoti Ghosh t->nwork = 0; 750793b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 750893b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 750993b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 751093b34091SDebojyoti Ghosh 751134561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr); 751234561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr); 7513d15a3a53SEmil Constantinescu 751493b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr); 751593b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm);CHKERRQ(ierr); 751693b34091SDebojyoti Ghosh 751793b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 751851699248SLisandro Dalcin ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr); 751993b34091SDebojyoti Ghosh 7520e7069c78SShri t->trajectory = tsin->trajectory; 7521e7069c78SShri ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr); 7522e7069c78SShri 7523e7069c78SShri t->event = tsin->event; 75246b10a48eSSatish Balay if (t->event) t->event->refct++; 7525e7069c78SShri 752693b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 752793b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 7528e7069c78SShri t->ptime_prev = tsin->ptime_prev; 752993b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 753093b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 753193b34091SDebojyoti Ghosh t->steps = tsin->steps; 753293b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 753393b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 753493b34091SDebojyoti Ghosh t->atol = tsin->atol; 753593b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 753693b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 753793b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 753893b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 753993b34091SDebojyoti Ghosh 754093b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type);CHKERRQ(ierr); 754193b34091SDebojyoti Ghosh ierr = TSSetType(t,type);CHKERRQ(ierr); 754293b34091SDebojyoti Ghosh 754393b34091SDebojyoti Ghosh t->vec_sol = NULL; 754493b34091SDebojyoti Ghosh 754593b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 754693b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 754793b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 754893b34091SDebojyoti Ghosh 754993b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 755093b34091SDebojyoti Ghosh 75510d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 75520d4fed19SBarry Smith PetscInt i; 75530d4fed19SBarry Smith ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr); 75540d4fed19SBarry Smith for (i=0; i<10; i++) { 75550d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 75560d4fed19SBarry Smith } 75570d4fed19SBarry Smith } 755893b34091SDebojyoti Ghosh *tsout = t; 755993b34091SDebojyoti Ghosh PetscFunctionReturn(0); 756093b34091SDebojyoti Ghosh } 7561f3b1f45cSBarry Smith 7562f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y) 7563f3b1f45cSBarry Smith { 7564f3b1f45cSBarry Smith PetscErrorCode ierr; 7565f3b1f45cSBarry Smith TS ts = (TS) ctx; 7566f3b1f45cSBarry Smith 7567f3b1f45cSBarry Smith PetscFunctionBegin; 7568f3b1f45cSBarry Smith ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr); 7569f3b1f45cSBarry Smith PetscFunctionReturn(0); 7570f3b1f45cSBarry Smith } 7571f3b1f45cSBarry Smith 7572f3b1f45cSBarry Smith /*@ 7573f3b1f45cSBarry Smith TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function. 7574f3b1f45cSBarry Smith 7575f3b1f45cSBarry Smith Logically Collective on TS and Mat 7576f3b1f45cSBarry Smith 7577f3b1f45cSBarry Smith Input Parameters: 7578f3b1f45cSBarry Smith TS - the time stepping routine 7579f3b1f45cSBarry Smith 7580f3b1f45cSBarry Smith Output Parameter: 7581f3b1f45cSBarry Smith . flg - PETSC_TRUE if the multiply is likely correct 7582f3b1f45cSBarry Smith 7583f3b1f45cSBarry Smith Options Database: 7584f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator 7585f3b1f45cSBarry Smith 7586f3b1f45cSBarry Smith Level: advanced 7587f3b1f45cSBarry Smith 758895452b02SPatrick Sanan Notes: 758995452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 7590f3b1f45cSBarry Smith 7591f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose() 7592f3b1f45cSBarry Smith @*/ 7593f3b1f45cSBarry Smith PetscErrorCode TSRHSJacobianTest(TS ts,PetscBool *flg) 7594f3b1f45cSBarry Smith { 7595f3b1f45cSBarry Smith Mat J,B; 7596f3b1f45cSBarry Smith PetscErrorCode ierr; 7597f3b1f45cSBarry Smith TSRHSJacobian func; 7598f3b1f45cSBarry Smith void* ctx; 7599f3b1f45cSBarry Smith 7600f3b1f45cSBarry Smith PetscFunctionBegin; 7601f3b1f45cSBarry Smith ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr); 7602f3b1f45cSBarry Smith ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr); 7603f3b1f45cSBarry Smith ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr); 7604f3b1f45cSBarry Smith PetscFunctionReturn(0); 7605f3b1f45cSBarry Smith } 7606f3b1f45cSBarry Smith 7607f3b1f45cSBarry Smith /*@C 7608f3b1f45cSBarry Smith TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function. 7609f3b1f45cSBarry Smith 7610f3b1f45cSBarry Smith Logically Collective on TS and Mat 7611f3b1f45cSBarry Smith 7612f3b1f45cSBarry Smith Input Parameters: 7613f3b1f45cSBarry Smith TS - the time stepping routine 7614f3b1f45cSBarry Smith 7615f3b1f45cSBarry Smith Output Parameter: 7616f3b1f45cSBarry Smith . flg - PETSC_TRUE if the multiply is likely correct 7617f3b1f45cSBarry Smith 7618f3b1f45cSBarry Smith Options Database: 7619f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator 7620f3b1f45cSBarry Smith 762195452b02SPatrick Sanan Notes: 762295452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 7623f3b1f45cSBarry Smith 7624f3b1f45cSBarry Smith Level: advanced 7625f3b1f45cSBarry Smith 7626f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest() 7627f3b1f45cSBarry Smith @*/ 7628f3b1f45cSBarry Smith PetscErrorCode TSRHSJacobianTestTranspose(TS ts,PetscBool *flg) 7629f3b1f45cSBarry Smith { 7630f3b1f45cSBarry Smith Mat J,B; 7631f3b1f45cSBarry Smith PetscErrorCode ierr; 7632f3b1f45cSBarry Smith void *ctx; 7633f3b1f45cSBarry Smith TSRHSJacobian func; 7634f3b1f45cSBarry Smith 7635f3b1f45cSBarry Smith PetscFunctionBegin; 7636f3b1f45cSBarry Smith ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr); 7637f3b1f45cSBarry Smith ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr); 7638f3b1f45cSBarry Smith ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr); 7639f3b1f45cSBarry Smith PetscFunctionReturn(0); 7640f3b1f45cSBarry Smith } 7641