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 71c167fc2SEmil Constantinescu #define SkipSmallValue(a,b,tol) if(PetscAbsScalar(a)< tol || PetscAbsScalar(b)< tol) continue; 81c167fc2SEmil Constantinescu 9d5ba7fb7SMatthew Knepley /* Logging support */ 10d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 11a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 12d405a339SMatthew Knepley 13feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1449354f04SShri Abhyankar 151c167fc2SEmil Constantinescu 16fde5950dSBarry Smith /*@C 17fde5950dSBarry Smith TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 18fde5950dSBarry Smith 19fde5950dSBarry Smith Collective on TS 20fde5950dSBarry Smith 21fde5950dSBarry Smith Input Parameters: 22fde5950dSBarry Smith + ts - TS object you wish to monitor 23fde5950dSBarry Smith . name - the monitor type one is seeking 24fde5950dSBarry Smith . help - message indicating what monitoring is done 25fde5950dSBarry Smith . manual - manual page for the monitor 26fde5950dSBarry Smith . monitor - the monitor function 27fde5950dSBarry 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 28fde5950dSBarry Smith 29fde5950dSBarry Smith Level: developer 30fde5950dSBarry Smith 31fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 32fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 33fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 34fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 35fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 36fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 37fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 38fde5950dSBarry Smith @*/ 39721cd6eeSBarry 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*)) 40fde5950dSBarry Smith { 41fde5950dSBarry Smith PetscErrorCode ierr; 42fde5950dSBarry Smith PetscViewer viewer; 43fde5950dSBarry Smith PetscViewerFormat format; 44fde5950dSBarry Smith PetscBool flg; 45fde5950dSBarry Smith 46fde5950dSBarry Smith PetscFunctionBegin; 4716413a6aSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject) ts)->options,((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 48fde5950dSBarry Smith if (flg) { 49721cd6eeSBarry Smith PetscViewerAndFormat *vf; 50721cd6eeSBarry Smith ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr); 51721cd6eeSBarry Smith ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 52fde5950dSBarry Smith if (monitorsetup) { 53721cd6eeSBarry Smith ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr); 54fde5950dSBarry Smith } 55721cd6eeSBarry Smith ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr); 56fde5950dSBarry Smith } 57fde5950dSBarry Smith PetscFunctionReturn(0); 58fde5950dSBarry Smith } 59fde5950dSBarry Smith 602ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type) 612ffb9264SLisandro Dalcin { 622ffb9264SLisandro Dalcin PetscErrorCode ierr; 632ffb9264SLisandro Dalcin 642ffb9264SLisandro Dalcin PetscFunctionBegin; 65b92453a8SLisandro Dalcin PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1); 66b92453a8SLisandro Dalcin PetscValidCharPointer(default_type,2); 672ffb9264SLisandro Dalcin if (!((PetscObject)adapt)->type_name) { 682ffb9264SLisandro Dalcin ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr); 692ffb9264SLisandro Dalcin } 702ffb9264SLisandro Dalcin PetscFunctionReturn(0); 712ffb9264SLisandro Dalcin } 722ffb9264SLisandro Dalcin 73bdad233fSMatthew Knepley /*@ 74bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 75bdad233fSMatthew Knepley 76bdad233fSMatthew Knepley Collective on TS 77bdad233fSMatthew Knepley 78bdad233fSMatthew Knepley Input Parameter: 79bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 80bdad233fSMatthew Knepley 81bdad233fSMatthew Knepley Options Database Keys: 82e49d4f37SHong Zhang + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP 83ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 84ef85077eSLisandro Dalcin . -ts_max_time <time> - maximum time to compute to 85ef85077eSLisandro Dalcin . -ts_max_steps <steps> - maximum number of time-steps to take 86ef85077eSLisandro Dalcin . -ts_init_time <time> - initial time to start computation 874dc72f7fSBarry Smith . -ts_final_time <time> - final time to compute to (deprecated: use -ts_max_time) 883e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 89a3cdaa26SBarry 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 90a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 91a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 92a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 93a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 94a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 95f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function 96f3b1f45cSBarry 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 97ef222394SBarry Smith . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 98847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 99bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 100de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 101de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 1027cf37e64SBarry Smith . -ts_monitor_error - Monitors norm of error 1036934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 1048b668821SLisandro Dalcin . -ts_monitor_lg_timestep_log - Monitor log timestep size graphically 105de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 106de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 107de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 108de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 1093e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 1103e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 111fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 112e4160dc7SJulian Andrej . -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu) 1139e336e28SPatrick Sanan - -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 11453ea634cSHong Zhang 1159e336e28SPatrick Sanan Developer Note: 1169e336e28SPatrick Sanan We should unify all the -ts_monitor options in the way that -xxx_view has been unified 117bdad233fSMatthew Knepley 118bdad233fSMatthew Knepley Level: beginner 119bdad233fSMatthew Knepley 120a313700dSBarry Smith .seealso: TSGetType() 121bdad233fSMatthew Knepley @*/ 1227087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 123bdad233fSMatthew Knepley { 124bc952696SBarry Smith PetscBool opt,flg,tflg; 125dfbe8321SBarry Smith PetscErrorCode ierr; 126eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 12731748224SBarry Smith PetscReal time_step; 12849354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 129d1212d36SBarry Smith char dir[16]; 130cd11d68dSLisandro Dalcin TSIFunction ifun; 1316991f827SBarry Smith const char *defaultType; 1326991f827SBarry Smith char typeName[256]; 133bdad233fSMatthew Knepley 134bdad233fSMatthew Knepley PetscFunctionBegin; 1350700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1366991f827SBarry Smith 1376991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 138cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 139cd11d68dSLisandro Dalcin 140cd11d68dSLisandro Dalcin ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 1411ef27442SStefano Zampini if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 1421ef27442SStefano Zampini else 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 */ 1514dc72f7fSBarry Smith ierr = PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL);CHKERRQ(ierr); 152ef85077eSLisandro Dalcin ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 15319eac22cSLisandro Dalcin ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1540298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,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); 1670fe4d17eSHong Zhang ierr = PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,NULL);CHKERRQ(ierr); 16856f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 16956f85f32SBarry Smith { 17056f85f32SBarry Smith PetscBool set; 17156f85f32SBarry Smith flg = PETSC_FALSE; 17256f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 17356f85f32SBarry Smith if (set) { 17456f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 17556f85f32SBarry Smith } 17656f85f32SBarry Smith } 17756f85f32SBarry Smith #endif 17856f85f32SBarry Smith 179bdad233fSMatthew Knepley /* Monitor options */ 180cd11d68dSLisandro Dalcin ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr); 181cc9c3a59SBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL);CHKERRQ(ierr); 182fde5950dSBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr); 183fde5950dSBarry Smith 1845180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1855180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1865180491cSLisandro Dalcin 1874f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 188b3603a34SBarry Smith if (opt) { 1890b039ecaSBarry Smith TSMonitorLGCtx ctx; 1903923b477SBarry Smith PetscInt howoften = 1; 191b3603a34SBarry Smith 1920298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 1936ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 1944f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 195bdad233fSMatthew Knepley } 1966ba87a44SLisandro Dalcin 1974f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 198ef20d060SBarry Smith if (opt) { 1990b039ecaSBarry Smith TSMonitorLGCtx ctx; 2003923b477SBarry Smith PetscInt howoften = 1; 201ef20d060SBarry Smith 2020298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 2036ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2044f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 205ef20d060SBarry Smith } 206edbaebb3SBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr); 2077cf37e64SBarry Smith 2086934998bSLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 2096934998bSLisandro Dalcin if (opt) { 2106934998bSLisandro Dalcin TSMonitorLGCtx ctx; 2116934998bSLisandro Dalcin PetscInt howoften = 1; 2126934998bSLisandro Dalcin 2136934998bSLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 2146ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2156934998bSLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 2166934998bSLisandro Dalcin } 2178b668821SLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 2188b668821SLisandro Dalcin if (opt) { 2198b668821SLisandro Dalcin TSMonitorLGCtx ctx; 2208b668821SLisandro Dalcin PetscInt howoften = 1; 2218b668821SLisandro Dalcin 2228b668821SLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 2238b668821SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2248b668821SLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 2258b668821SLisandro Dalcin ctx->semilogy = PETSC_TRUE; 2268b668821SLisandro Dalcin } 2278b668821SLisandro Dalcin 228201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 229201da799SBarry Smith if (opt) { 230201da799SBarry Smith TSMonitorLGCtx ctx; 231201da799SBarry Smith PetscInt howoften = 1; 232201da799SBarry Smith 2330298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2346ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 235201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 236201da799SBarry Smith } 237201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 238201da799SBarry Smith if (opt) { 239201da799SBarry Smith TSMonitorLGCtx ctx; 240201da799SBarry Smith PetscInt howoften = 1; 241201da799SBarry Smith 2420298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2436ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 244201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 245201da799SBarry Smith } 2468189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 2478189c53fSBarry Smith if (opt) { 2488189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2498189c53fSBarry Smith PetscInt howoften = 1; 2508189c53fSBarry Smith 2510298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 2526934998bSLisandro Dalcin ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 2538189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 2548189c53fSBarry Smith } 2550ec8ee2bSJoseph Pusztay ierr = PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt);CHKERRQ(ierr); 2561b575b74SJoseph Pusztay if (opt) { 2571b575b74SJoseph Pusztay TSMonitorSPCtx ctx; 2581b575b74SJoseph Pusztay PetscInt howoften = 1; 2590ec8ee2bSJoseph Pusztay ierr = PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm","TSMonitorSPSwarm",howoften,&howoften,NULL);CHKERRQ(ierr); 2601b575b74SJoseph Pusztay ierr = TSMonitorSPCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx);CHKERRQ(ierr); 2610ec8ee2bSJoseph Pusztay ierr = TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy);CHKERRQ(ierr); 2621b575b74SJoseph Pusztay } 263ef20d060SBarry Smith opt = PETSC_FALSE; 2640dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 265a7cc72afSBarry Smith if (opt) { 26683a4ac43SBarry Smith TSMonitorDrawCtx ctx; 26783a4ac43SBarry Smith PetscInt howoften = 1; 268a80ad3e0SBarry Smith 2690298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 2700ed3bfb6SBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 27183a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 272bdad233fSMatthew Knepley } 273fb1732b5SBarry Smith opt = PETSC_FALSE; 2742d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 2752d5ee99bSBarry Smith if (opt) { 2762d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2772d5ee99bSBarry Smith PetscReal bounds[4]; 2782d5ee99bSBarry Smith PetscInt n = 4; 2792d5ee99bSBarry Smith PetscDraw draw; 2806934998bSLisandro Dalcin PetscDrawAxis axis; 2812d5ee99bSBarry Smith 2822d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 2832d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2846934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr); 2852d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 2866934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr); 2876934998bSLisandro Dalcin ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 2886934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 2892d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2902d5ee99bSBarry Smith } 2912d5ee99bSBarry Smith opt = PETSC_FALSE; 2920dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 2933a471f94SBarry Smith if (opt) { 29483a4ac43SBarry Smith TSMonitorDrawCtx ctx; 29583a4ac43SBarry Smith PetscInt howoften = 1; 2963a471f94SBarry Smith 2970298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 2980ed3bfb6SBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 29983a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3003a471f94SBarry Smith } 3010ed3bfb6SBarry Smith opt = PETSC_FALSE; 3020ed3bfb6SBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr); 3030ed3bfb6SBarry Smith if (opt) { 3040ed3bfb6SBarry Smith TSMonitorDrawCtx ctx; 3050ed3bfb6SBarry Smith PetscInt howoften = 1; 3060ed3bfb6SBarry Smith 3070ed3bfb6SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr); 3080ed3bfb6SBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 3090ed3bfb6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 3100ed3bfb6SBarry Smith } 311fde5950dSBarry Smith 312ed81e22dSJed Brown opt = PETSC_FALSE; 31391b97e58SBarry 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); 314ed81e22dSJed Brown if (flg) { 315ed81e22dSJed Brown const char *ptr,*ptr2; 316ed81e22dSJed Brown char *filetemplate; 317ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 318ed81e22dSJed Brown /* Do some cursory validation of the input. */ 319ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 320ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 321ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 322ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 323ce94432eSBarry 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"); 324ed81e22dSJed Brown if (ptr2) break; 325ed81e22dSJed Brown } 326ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 327ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 328ed81e22dSJed Brown } 329bdad233fSMatthew Knepley 330d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 331d1212d36SBarry Smith if (flg) { 332d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 333d1212d36SBarry Smith int ray = 0; 3343ee9839eSMatthew G. Knepley DMDirection ddir; 335d1212d36SBarry Smith DM da; 336d1212d36SBarry Smith PetscMPIInt rank; 337d1212d36SBarry Smith 338ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 3393ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3403ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 341ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 342d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 343d1212d36SBarry Smith 3445bd1e576SStefano Zampini ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %d\n",dir[0],ray);CHKERRQ(ierr); 345b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 346d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 347d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 348ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 349d1212d36SBarry Smith if (!rank) { 350d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 351d1212d36SBarry Smith } 35251b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 353d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 354d1212d36SBarry Smith } 35551b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 35651b4a12fSMatthew G. Knepley if (flg) { 35751b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 35851b4a12fSMatthew G. Knepley int ray = 0; 3593ee9839eSMatthew G. Knepley DMDirection ddir; 36051b4a12fSMatthew G. Knepley DM da; 36151b4a12fSMatthew G. Knepley PetscInt howoften = 1; 362d1212d36SBarry Smith 36351b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 3643ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3653ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 36651b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 36751b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 3681c3436cfSJed Brown 3695bd1e576SStefano Zampini ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %d\n", dir[0], ray);CHKERRQ(ierr); 370b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 37151b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 37251b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 37351b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 37451b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 37551b4a12fSMatthew G. Knepley } 376a7a1495cSBarry Smith 377b3d3934dSBarry Smith ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 378b3d3934dSBarry Smith if (opt) { 379b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 380b3d3934dSBarry Smith 381b3d3934dSBarry Smith ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 382b3d3934dSBarry Smith ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 383b3d3934dSBarry Smith } 384b3d3934dSBarry Smith 385847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 386847ff0e1SMatthew G. Knepley ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr); 387847ff0e1SMatthew G. Knepley if (flg) { 388847ff0e1SMatthew G. Knepley DM dm; 389847ff0e1SMatthew G. Knepley DMTS tdm; 390847ff0e1SMatthew G. Knepley 391847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 392847ff0e1SMatthew G. Knepley ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr); 393847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 394847ff0e1SMatthew G. Knepley ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr); 395847ff0e1SMatthew G. Knepley ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 396847ff0e1SMatthew G. Knepley } 397847ff0e1SMatthew G. Knepley 398d763cef2SBarry Smith /* Handle specific TS options */ 399d763cef2SBarry Smith if (ts->ops->setfromoptions) { 4006991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 401d763cef2SBarry Smith } 402fbc52257SHong Zhang 403a7bdc993SLisandro Dalcin /* Handle TSAdapt options */ 404a7bdc993SLisandro Dalcin ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 405a7bdc993SLisandro Dalcin ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr); 406a7bdc993SLisandro Dalcin ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr); 407a7bdc993SLisandro Dalcin 40868bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 4094f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 41068bece0bSHong Zhang ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 41168bece0bSHong Zhang if (tflg) { 41268bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 41368bece0bSHong Zhang } 414a05bf03eSHong Zhang 415a05bf03eSHong Zhang ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr); 416d763cef2SBarry Smith 417d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 4180633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr); 419fbc52257SHong Zhang ierr = PetscOptionsEnd();CHKERRQ(ierr); 420d763cef2SBarry Smith 4214f122a70SLisandro Dalcin if (ts->trajectory) { 4224f122a70SLisandro Dalcin ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 423d763cef2SBarry Smith } 42468bece0bSHong Zhang 4251ef27442SStefano Zampini /* why do we have to do this here and not during TSSetUp? */ 4264f122a70SLisandro Dalcin ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr); 4271ef27442SStefano Zampini if (ts->problem_type == TS_LINEAR) { 4281ef27442SStefano Zampini ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr); 4291ef27442SStefano Zampini if (!flg) { ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); } 4301ef27442SStefano Zampini } 4314f122a70SLisandro Dalcin ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr); 432d763cef2SBarry Smith PetscFunctionReturn(0); 433d763cef2SBarry Smith } 434d763cef2SBarry Smith 435d2daff3dSHong Zhang /*@ 43678fbdcc8SBarry Smith TSGetTrajectory - Gets the trajectory from a TS if it exists 43778fbdcc8SBarry Smith 43878fbdcc8SBarry Smith Collective on TS 43978fbdcc8SBarry Smith 44078fbdcc8SBarry Smith Input Parameters: 44178fbdcc8SBarry Smith . ts - the TS context obtained from TSCreate() 44278fbdcc8SBarry Smith 44378fbdcc8SBarry Smith Output Parameters; 44478fbdcc8SBarry Smith . tr - the TSTrajectory object, if it exists 44578fbdcc8SBarry Smith 44678fbdcc8SBarry Smith Note: This routine should be called after all TS options have been set 44778fbdcc8SBarry Smith 44878fbdcc8SBarry Smith Level: advanced 44978fbdcc8SBarry Smith 45078fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate() 45178fbdcc8SBarry Smith 45278fbdcc8SBarry Smith @*/ 45378fbdcc8SBarry Smith PetscErrorCode TSGetTrajectory(TS ts,TSTrajectory *tr) 45478fbdcc8SBarry Smith { 45578fbdcc8SBarry Smith PetscFunctionBegin; 45678fbdcc8SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45778fbdcc8SBarry Smith *tr = ts->trajectory; 45878fbdcc8SBarry Smith PetscFunctionReturn(0); 45978fbdcc8SBarry Smith } 46078fbdcc8SBarry Smith 46178fbdcc8SBarry Smith /*@ 462bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 463d2daff3dSHong Zhang 464d2daff3dSHong Zhang Collective on TS 465d2daff3dSHong Zhang 466d2daff3dSHong Zhang Input Parameters: 467bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 468bc952696SBarry Smith 46978fbdcc8SBarry Smith Options Database: 47078fbdcc8SBarry Smith + -ts_save_trajectory - saves the trajectory to a file 47178fbdcc8SBarry Smith - -ts_trajectory_type type 47278fbdcc8SBarry Smith 47368bece0bSHong Zhang Note: This routine should be called after all TS options have been set 474d2daff3dSHong Zhang 475c3a89c15SBarry Smith The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and 47678fbdcc8SBarry Smith MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m 47778fbdcc8SBarry Smith 478d2daff3dSHong Zhang Level: intermediate 479d2daff3dSHong Zhang 4802d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve() 481d2daff3dSHong Zhang 482d2daff3dSHong Zhang @*/ 483bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 484d2daff3dSHong Zhang { 485bc952696SBarry Smith PetscErrorCode ierr; 486bc952696SBarry Smith 487d2daff3dSHong Zhang PetscFunctionBegin; 488d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 489bc952696SBarry Smith if (!ts->trajectory) { 490bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 491bc952696SBarry Smith } 492d2daff3dSHong Zhang PetscFunctionReturn(0); 493d2daff3dSHong Zhang } 494d2daff3dSHong Zhang 495a7a1495cSBarry Smith /*@ 4962d29f1f2SStefano Zampini TSResetTrajectory - Destroys and recreates the internal TSTrajectory object 4972d29f1f2SStefano Zampini 4982d29f1f2SStefano Zampini Collective on TS 4992d29f1f2SStefano Zampini 5002d29f1f2SStefano Zampini Input Parameters: 5012d29f1f2SStefano Zampini . ts - the TS context obtained from TSCreate() 5022d29f1f2SStefano Zampini 5032d29f1f2SStefano Zampini Level: intermediate 5042d29f1f2SStefano Zampini 5052d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve() 5062d29f1f2SStefano Zampini 5072d29f1f2SStefano Zampini @*/ 5082d29f1f2SStefano Zampini PetscErrorCode TSResetTrajectory(TS ts) 5092d29f1f2SStefano Zampini { 5102d29f1f2SStefano Zampini PetscErrorCode ierr; 5112d29f1f2SStefano Zampini 5122d29f1f2SStefano Zampini PetscFunctionBegin; 5132d29f1f2SStefano Zampini PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5142d29f1f2SStefano Zampini if (ts->trajectory) { 5152d29f1f2SStefano Zampini ierr = TSTrajectoryDestroy(&ts->trajectory);CHKERRQ(ierr); 5162d29f1f2SStefano Zampini ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 5172d29f1f2SStefano Zampini } 5182d29f1f2SStefano Zampini PetscFunctionReturn(0); 5192d29f1f2SStefano Zampini } 5202d29f1f2SStefano Zampini 5212d29f1f2SStefano Zampini /*@ 522a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 523a7a1495cSBarry Smith set with TSSetRHSJacobian(). 524a7a1495cSBarry Smith 525d083f849SBarry Smith Collective on TS 526a7a1495cSBarry Smith 527a7a1495cSBarry Smith Input Parameters: 528316643e7SJed Brown + ts - the TS context 529a7a1495cSBarry Smith . t - current timestep 5300910c330SBarry Smith - U - input vector 531a7a1495cSBarry Smith 532a7a1495cSBarry Smith Output Parameters: 533a7a1495cSBarry Smith + A - Jacobian matrix 534a7a1495cSBarry Smith . B - optional preconditioning matrix 535a7a1495cSBarry Smith - flag - flag indicating matrix structure 536a7a1495cSBarry Smith 537a7a1495cSBarry Smith Notes: 538a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 539a7a1495cSBarry Smith is used internally within the nonlinear solvers. 540a7a1495cSBarry Smith 54194b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 542a7a1495cSBarry Smith flag parameter. 543a7a1495cSBarry Smith 544a7a1495cSBarry Smith Level: developer 545a7a1495cSBarry Smith 54694b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 547a7a1495cSBarry Smith @*/ 548d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 549a7a1495cSBarry Smith { 550dfbe8321SBarry Smith PetscErrorCode ierr; 551270bf2e7SJed Brown PetscObjectState Ustate; 5526c1e1eecSBarry Smith PetscObjectId Uid; 55324989b8cSPeter Brune DM dm; 554942e3340SBarry Smith DMTS tsdm; 55524989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 55624989b8cSPeter Brune void *ctx; 55724989b8cSPeter Brune TSIJacobian ijacobianfunc; 558b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 559a7a1495cSBarry Smith 560a7a1495cSBarry Smith PetscFunctionBegin; 5610700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5620910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5630910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 56424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 565942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 56624989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 5670298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 568b2df71adSDebojyoti Ghosh ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 56959e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 5706c1e1eecSBarry Smith ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr); 571971015bcSStefano Zampini 5726c1e1eecSBarry Smith if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 573971015bcSStefano Zampini /* restore back RHS Jacobian matrices if they have been shifted/scaled */ 574971015bcSStefano Zampini if (A == ts->Arhs) { 575971015bcSStefano Zampini if (ts->rhsjacobian.shift != 0) { 576971015bcSStefano Zampini ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 577971015bcSStefano Zampini } 578971015bcSStefano Zampini if (ts->rhsjacobian.scale != 1.) { 579971015bcSStefano Zampini ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 580971015bcSStefano Zampini } 581971015bcSStefano Zampini } 582971015bcSStefano Zampini if (B && B == ts->Brhs && A != B) { 583971015bcSStefano Zampini if (ts->rhsjacobian.shift != 0) { 584971015bcSStefano Zampini ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 585971015bcSStefano Zampini } 586971015bcSStefano Zampini if (ts->rhsjacobian.scale != 1.) { 587971015bcSStefano Zampini ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 588971015bcSStefano Zampini } 589971015bcSStefano Zampini } 590971015bcSStefano Zampini ts->rhsjacobian.shift = 0; 591971015bcSStefano Zampini ts->rhsjacobian.scale = 1.; 5920e4ef248SJed Brown PetscFunctionReturn(0); 593f8ede8e7SJed Brown } 594d90be118SSean Farley 595ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 596d90be118SSean Farley 597e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 598971015bcSStefano Zampini if (A == ts->Arhs) { 599971015bcSStefano Zampini /* MatScale has a short path for this case. 600971015bcSStefano Zampini However, this code path is taken the first time TSComputeRHSJacobian is called 601971015bcSStefano Zampini and the matrices have not assembled yet */ 602971015bcSStefano Zampini if (ts->rhsjacobian.shift != 0) { 60394ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 604971015bcSStefano Zampini } 605971015bcSStefano Zampini if (ts->rhsjacobian.scale != 1.) { 60694ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 607971015bcSStefano Zampini } 608971015bcSStefano Zampini } 609971015bcSStefano Zampini if (B && B == ts->Brhs && A != B) { 610971015bcSStefano Zampini if (ts->rhsjacobian.shift != 0) { 61194ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 612971015bcSStefano Zampini } 613971015bcSStefano Zampini if (ts->rhsjacobian.scale != 1.) { 61494ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 615e1244c69SJed Brown } 616971015bcSStefano Zampini } 617e1244c69SJed Brown } 618e1244c69SJed Brown 61924989b8cSPeter Brune if (rhsjacobianfunc) { 6206cd88445SBarry Smith PetscBool missing; 62194ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 622a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 623d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 624a7a1495cSBarry Smith PetscStackPop; 62594ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 6266cd88445SBarry Smith if (A) { 6276cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 6286cd88445SBarry 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"); 6296cd88445SBarry Smith } 6306cd88445SBarry Smith if (B && B != A) { 6316cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 6326cd88445SBarry 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"); 6336cd88445SBarry Smith } 634ef66eb69SBarry Smith } else { 63594ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 636971015bcSStefano Zampini if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 637ef66eb69SBarry Smith } 6380e4ef248SJed Brown ts->rhsjacobian.time = t; 639971015bcSStefano Zampini ts->rhsjacobian.shift = 0; 640971015bcSStefano Zampini ts->rhsjacobian.scale = 1.; 6417f79407eSBarry Smith ierr = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr); 64259e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 643a7a1495cSBarry Smith PetscFunctionReturn(0); 644a7a1495cSBarry Smith } 645a7a1495cSBarry Smith 646316643e7SJed Brown /*@ 647d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 648d763cef2SBarry Smith 649d083f849SBarry Smith Collective on TS 650316643e7SJed Brown 651316643e7SJed Brown Input Parameters: 652316643e7SJed Brown + ts - the TS context 653316643e7SJed Brown . t - current time 6540910c330SBarry Smith - U - state vector 655316643e7SJed Brown 656316643e7SJed Brown Output Parameter: 657316643e7SJed Brown . y - right hand side 658316643e7SJed Brown 659316643e7SJed Brown Note: 660316643e7SJed Brown Most users should not need to explicitly call this routine, as it 661316643e7SJed Brown is used internally within the nonlinear solvers. 662316643e7SJed Brown 663316643e7SJed Brown Level: developer 664316643e7SJed Brown 665316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 666316643e7SJed Brown @*/ 6670910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 668d763cef2SBarry Smith { 669dfbe8321SBarry Smith PetscErrorCode ierr; 67024989b8cSPeter Brune TSRHSFunction rhsfunction; 67124989b8cSPeter Brune TSIFunction ifunction; 67224989b8cSPeter Brune void *ctx; 67324989b8cSPeter Brune DM dm; 67424989b8cSPeter Brune 675d763cef2SBarry Smith PetscFunctionBegin; 6760700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6770910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6780700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 67924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 68024989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 6810298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 682d763cef2SBarry Smith 683ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 684d763cef2SBarry Smith 6850910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 68624989b8cSPeter Brune if (rhsfunction) { 687d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 6880910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 689d763cef2SBarry Smith PetscStackPop; 690214bc6a2SJed Brown } else { 691214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 692b2cd27e8SJed Brown } 69344a41b28SSean Farley 6940910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 695d763cef2SBarry Smith PetscFunctionReturn(0); 696d763cef2SBarry Smith } 697d763cef2SBarry Smith 698ef20d060SBarry Smith /*@ 699ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 700ef20d060SBarry Smith 701d083f849SBarry Smith Collective on TS 702ef20d060SBarry Smith 703ef20d060SBarry Smith Input Parameters: 704ef20d060SBarry Smith + ts - the TS context 705ef20d060SBarry Smith - t - current time 706ef20d060SBarry Smith 707ef20d060SBarry Smith Output Parameter: 7080910c330SBarry Smith . U - the solution 709ef20d060SBarry Smith 710ef20d060SBarry Smith Note: 711ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 712ef20d060SBarry Smith is used internally within the nonlinear solvers. 713ef20d060SBarry Smith 714ef20d060SBarry Smith Level: developer 715ef20d060SBarry Smith 716abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 717ef20d060SBarry Smith @*/ 7180910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 719ef20d060SBarry Smith { 720ef20d060SBarry Smith PetscErrorCode ierr; 721ef20d060SBarry Smith TSSolutionFunction solutionfunction; 722ef20d060SBarry Smith void *ctx; 723ef20d060SBarry Smith DM dm; 724ef20d060SBarry Smith 725ef20d060SBarry Smith PetscFunctionBegin; 726ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7270910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 728ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 729ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 730ef20d060SBarry Smith 731ef20d060SBarry Smith if (solutionfunction) { 7329b7cd975SBarry Smith PetscStackPush("TS user solution function"); 7330910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 734ef20d060SBarry Smith PetscStackPop; 735ef20d060SBarry Smith } 736ef20d060SBarry Smith PetscFunctionReturn(0); 737ef20d060SBarry Smith } 7389b7cd975SBarry Smith /*@ 7399b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 7409b7cd975SBarry Smith 741d083f849SBarry Smith Collective on TS 7429b7cd975SBarry Smith 7439b7cd975SBarry Smith Input Parameters: 7449b7cd975SBarry Smith + ts - the TS context 7459b7cd975SBarry Smith - t - current time 7469b7cd975SBarry Smith 7479b7cd975SBarry Smith Output Parameter: 7489b7cd975SBarry Smith . U - the function value 7499b7cd975SBarry Smith 7509b7cd975SBarry Smith Note: 7519b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 7529b7cd975SBarry Smith is used internally within the nonlinear solvers. 7539b7cd975SBarry Smith 7549b7cd975SBarry Smith Level: developer 7559b7cd975SBarry Smith 7569b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 7579b7cd975SBarry Smith @*/ 7589b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 7599b7cd975SBarry Smith { 7609b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 7619b7cd975SBarry Smith void *ctx; 7629b7cd975SBarry Smith DM dm; 7639b7cd975SBarry Smith 7649b7cd975SBarry Smith PetscFunctionBegin; 7659b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7669b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7679b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 7689b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 7699b7cd975SBarry Smith 7709b7cd975SBarry Smith if (forcing) { 7719b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 7729b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 7739b7cd975SBarry Smith PetscStackPop; 7749b7cd975SBarry Smith } 7759b7cd975SBarry Smith PetscFunctionReturn(0); 7769b7cd975SBarry Smith } 777ef20d060SBarry Smith 778214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 779214bc6a2SJed Brown { 7802dd45cf8SJed Brown Vec F; 781214bc6a2SJed Brown PetscErrorCode ierr; 782214bc6a2SJed Brown 783214bc6a2SJed Brown PetscFunctionBegin; 7840298fd71SBarry Smith *Frhs = NULL; 7850298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 786214bc6a2SJed Brown if (!ts->Frhs) { 7872dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 788214bc6a2SJed Brown } 789214bc6a2SJed Brown *Frhs = ts->Frhs; 790214bc6a2SJed Brown PetscFunctionReturn(0); 791214bc6a2SJed Brown } 792214bc6a2SJed Brown 793971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 794214bc6a2SJed Brown { 795214bc6a2SJed Brown Mat A,B; 7962dd45cf8SJed Brown PetscErrorCode ierr; 79741a1d4d2SBarry Smith TSIJacobian ijacobian; 798214bc6a2SJed Brown 799214bc6a2SJed Brown PetscFunctionBegin; 800c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 801c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 80241a1d4d2SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr); 803214bc6a2SJed Brown if (Arhs) { 804214bc6a2SJed Brown if (!ts->Arhs) { 80541a1d4d2SBarry Smith if (ijacobian) { 806214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 80741a1d4d2SBarry Smith } else { 80841a1d4d2SBarry Smith ts->Arhs = A; 80941a1d4d2SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 81041a1d4d2SBarry Smith } 8113565c898SBarry Smith } else { 8123565c898SBarry Smith PetscBool flg; 8133565c898SBarry Smith ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr); 8143565c898SBarry Smith /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */ 8153565c898SBarry Smith if (flg && !ijacobian && ts->Arhs == ts->Brhs){ 8163565c898SBarry Smith ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr); 8173565c898SBarry Smith ts->Arhs = A; 8183565c898SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 8193565c898SBarry Smith } 820214bc6a2SJed Brown } 821214bc6a2SJed Brown *Arhs = ts->Arhs; 822214bc6a2SJed Brown } 823214bc6a2SJed Brown if (Brhs) { 824214bc6a2SJed Brown if (!ts->Brhs) { 825bdb70873SJed Brown if (A != B) { 82641a1d4d2SBarry Smith if (ijacobian) { 827214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 828bdb70873SJed Brown } else { 82941a1d4d2SBarry Smith ts->Brhs = B; 83041a1d4d2SBarry Smith ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 83141a1d4d2SBarry Smith } 83241a1d4d2SBarry Smith } else { 833bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 83451699248SLisandro Dalcin ts->Brhs = ts->Arhs; 835bdb70873SJed Brown } 836214bc6a2SJed Brown } 837214bc6a2SJed Brown *Brhs = ts->Brhs; 838214bc6a2SJed Brown } 839214bc6a2SJed Brown PetscFunctionReturn(0); 840214bc6a2SJed Brown } 841214bc6a2SJed Brown 842316643e7SJed Brown /*@ 8430910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 844316643e7SJed Brown 845d083f849SBarry Smith Collective on TS 846316643e7SJed Brown 847316643e7SJed Brown Input Parameters: 848316643e7SJed Brown + ts - the TS context 849316643e7SJed Brown . t - current time 8500910c330SBarry Smith . U - state vector 8510910c330SBarry Smith . Udot - time derivative of state vector 852214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 853316643e7SJed Brown 854316643e7SJed Brown Output Parameter: 855316643e7SJed Brown . Y - right hand side 856316643e7SJed Brown 857316643e7SJed Brown Note: 858316643e7SJed Brown Most users should not need to explicitly call this routine, as it 859316643e7SJed Brown is used internally within the nonlinear solvers. 860316643e7SJed Brown 861316643e7SJed Brown If the user did did not write their equations in implicit form, this 862316643e7SJed Brown function recasts them in implicit form. 863316643e7SJed Brown 864316643e7SJed Brown Level: developer 865316643e7SJed Brown 866316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 867316643e7SJed Brown @*/ 8680910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 869316643e7SJed Brown { 870316643e7SJed Brown PetscErrorCode ierr; 87124989b8cSPeter Brune TSIFunction ifunction; 87224989b8cSPeter Brune TSRHSFunction rhsfunction; 87324989b8cSPeter Brune void *ctx; 87424989b8cSPeter Brune DM dm; 875316643e7SJed Brown 876316643e7SJed Brown PetscFunctionBegin; 8770700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8780910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8790910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 8800700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 881316643e7SJed Brown 88224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 88324989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 8840298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 88524989b8cSPeter Brune 886ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 887d90be118SSean Farley 8880910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 88924989b8cSPeter Brune if (ifunction) { 890316643e7SJed Brown PetscStackPush("TS user implicit function"); 8910910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 892316643e7SJed Brown PetscStackPop; 893214bc6a2SJed Brown } 894214bc6a2SJed Brown if (imex) { 89524989b8cSPeter Brune if (!ifunction) { 8960910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 8972dd45cf8SJed Brown } 89824989b8cSPeter Brune } else if (rhsfunction) { 89924989b8cSPeter Brune if (ifunction) { 900214bc6a2SJed Brown Vec Frhs; 901214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 9020910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 903214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 9042dd45cf8SJed Brown } else { 9050910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 9060910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 907316643e7SJed Brown } 9084a6899ffSJed Brown } 9090910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 910316643e7SJed Brown PetscFunctionReturn(0); 911316643e7SJed Brown } 912316643e7SJed Brown 913316643e7SJed Brown /*@ 914316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 915316643e7SJed Brown 916d083f849SBarry Smith Collective on TS 917316643e7SJed Brown 918316643e7SJed Brown Input 919316643e7SJed Brown Input Parameters: 920316643e7SJed Brown + ts - the TS context 921316643e7SJed Brown . t - current timestep 9220910c330SBarry Smith . U - state vector 9230910c330SBarry Smith . Udot - time derivative of state vector 924214bc6a2SJed Brown . shift - shift to apply, see note below 925214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 926316643e7SJed Brown 927316643e7SJed Brown Output Parameters: 928316643e7SJed Brown + A - Jacobian matrix 9293565c898SBarry Smith - B - matrix from which the preconditioner is constructed; often the same as A 930316643e7SJed Brown 931316643e7SJed Brown Notes: 9320910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 933316643e7SJed Brown 9340910c330SBarry Smith dF/dU + shift*dF/dUdot 935316643e7SJed Brown 936316643e7SJed Brown Most users should not need to explicitly call this routine, as it 937316643e7SJed Brown is used internally within the nonlinear solvers. 938316643e7SJed Brown 939316643e7SJed Brown Level: developer 940316643e7SJed Brown 941316643e7SJed Brown .seealso: TSSetIJacobian() 94263495f91SJed Brown @*/ 943d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 944316643e7SJed Brown { 945316643e7SJed Brown PetscErrorCode ierr; 94624989b8cSPeter Brune TSIJacobian ijacobian; 94724989b8cSPeter Brune TSRHSJacobian rhsjacobian; 94824989b8cSPeter Brune DM dm; 94924989b8cSPeter Brune void *ctx; 950316643e7SJed Brown 951316643e7SJed Brown PetscFunctionBegin; 9520700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9530910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 9540910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 955316643e7SJed Brown PetscValidPointer(A,6); 95694ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 957316643e7SJed Brown PetscValidPointer(B,7); 95894ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 95924989b8cSPeter Brune 96024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 96124989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 9620298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 96324989b8cSPeter Brune 964ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 965316643e7SJed Brown 96694ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 96724989b8cSPeter Brune if (ijacobian) { 9686cd88445SBarry Smith PetscBool missing; 969316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 970d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 971316643e7SJed Brown PetscStackPop; 9726cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 9736cd88445SBarry 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"); 9743565c898SBarry Smith if (B != A) { 9756cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 9766cd88445SBarry 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"); 9776cd88445SBarry Smith } 9784a6899ffSJed Brown } 979214bc6a2SJed Brown if (imex) { 980b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 9814c26be97Sstefano_zampini PetscBool assembled; 982971015bcSStefano Zampini if (rhsjacobian) { 983971015bcSStefano Zampini Mat Arhs = NULL; 984971015bcSStefano Zampini ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr); 985971015bcSStefano Zampini if (A == Arhs) { 986971015bcSStefano Zampini if (rhsjacobian == TSComputeRHSJacobianConstant) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Unsupported operation! cannot use TSComputeRHSJacobianConstant"); 987971015bcSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 988971015bcSStefano Zampini } 989971015bcSStefano Zampini } 99094ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 9914c26be97Sstefano_zampini ierr = MatAssembled(A,&assembled);CHKERRQ(ierr); 9924c26be97Sstefano_zampini if (!assembled) { 9934c26be97Sstefano_zampini ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9944c26be97Sstefano_zampini ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9954c26be97Sstefano_zampini } 99694ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 99794ab13aaSBarry Smith if (A != B) { 99894ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 9994c26be97Sstefano_zampini ierr = MatAssembled(B,&assembled);CHKERRQ(ierr); 10004c26be97Sstefano_zampini if (!assembled) { 10014c26be97Sstefano_zampini ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10024c26be97Sstefano_zampini ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10034c26be97Sstefano_zampini } 100494ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 1005214bc6a2SJed Brown } 1006214bc6a2SJed Brown } 1007214bc6a2SJed Brown } else { 1008e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 1009e1244c69SJed Brown if (rhsjacobian) { 1010214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 1011d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 1012e1244c69SJed Brown } 101394ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 10143565c898SBarry Smith PetscBool flg; 1015e1244c69SJed Brown ts->rhsjacobian.scale = -1; 1016e1244c69SJed Brown ts->rhsjacobian.shift = shift; 10173565c898SBarry Smith ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr); 10183565c898SBarry Smith /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */ 10193565c898SBarry Smith if (!flg) { 102094ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 102194ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 10223565c898SBarry Smith } 102394ab13aaSBarry Smith if (A != B) { 102494ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 102594ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 1026316643e7SJed Brown } 1027e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 1028e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 1029e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 103094ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 103194ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 103294ab13aaSBarry Smith if (A != B) { 103394ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 103494ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 1035214bc6a2SJed Brown } 1036316643e7SJed Brown } 103794ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 103894ab13aaSBarry Smith if (A != B) { 103994ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 1040316643e7SJed Brown } 1041316643e7SJed Brown } 1042316643e7SJed Brown } 104394ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 1044316643e7SJed Brown PetscFunctionReturn(0); 1045316643e7SJed Brown } 1046316643e7SJed Brown 1047d763cef2SBarry Smith /*@C 1048d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 1049b5abc632SBarry Smith where U_t = G(t,u). 1050d763cef2SBarry Smith 10513f9fe445SBarry Smith Logically Collective on TS 1052d763cef2SBarry Smith 1053d763cef2SBarry Smith Input Parameters: 1054d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 10550298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 1056d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 1057d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 10580298fd71SBarry Smith function evaluation routine (may be NULL) 1059d763cef2SBarry Smith 1060d763cef2SBarry Smith Calling sequence of func: 10616bc98fa9SBarry Smith $ PetscErrorCode func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 1062d763cef2SBarry Smith 1063d763cef2SBarry Smith + t - current timestep 1064d763cef2SBarry Smith . u - input vector 1065d763cef2SBarry Smith . F - function vector 1066d763cef2SBarry Smith - ctx - [optional] user-defined function context 1067d763cef2SBarry Smith 1068d763cef2SBarry Smith Level: beginner 1069d763cef2SBarry Smith 107095452b02SPatrick Sanan Notes: 107195452b02SPatrick Sanan You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 10722bbac0d3SBarry Smith 1073ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 1074d763cef2SBarry Smith @*/ 1075089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 1076d763cef2SBarry Smith { 1077089b2837SJed Brown PetscErrorCode ierr; 1078089b2837SJed Brown SNES snes; 10790298fd71SBarry Smith Vec ralloc = NULL; 108024989b8cSPeter Brune DM dm; 1081d763cef2SBarry Smith 1082089b2837SJed Brown PetscFunctionBegin; 10830700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1084ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 108524989b8cSPeter Brune 108624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 108724989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 1088089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1089e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 1090e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 1091e856ceecSJed Brown r = ralloc; 1092e856ceecSJed Brown } 1093089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 1094e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1095d763cef2SBarry Smith PetscFunctionReturn(0); 1096d763cef2SBarry Smith } 1097d763cef2SBarry Smith 1098ef20d060SBarry Smith /*@C 1099abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1100ef20d060SBarry Smith 1101ef20d060SBarry Smith Logically Collective on TS 1102ef20d060SBarry Smith 1103ef20d060SBarry Smith Input Parameters: 1104ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1105ef20d060SBarry Smith . f - routine for evaluating the solution 1106ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 11070298fd71SBarry Smith function evaluation routine (may be NULL) 1108ef20d060SBarry Smith 1109ef20d060SBarry Smith Calling sequence of func: 11106bc98fa9SBarry Smith $ PetscErrorCode func (TS ts,PetscReal t,Vec u,void *ctx); 1111ef20d060SBarry Smith 1112ef20d060SBarry Smith + t - current timestep 1113ef20d060SBarry Smith . u - output vector 1114ef20d060SBarry Smith - ctx - [optional] user-defined function context 1115ef20d060SBarry Smith 11160ed3bfb6SBarry Smith Options Database: 11170ed3bfb6SBarry Smith + -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction() 11180ed3bfb6SBarry Smith - -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 11190ed3bfb6SBarry Smith 1120abd5a294SJed Brown Notes: 1121abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1122abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1123abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1124abd5a294SJed Brown 11254f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1126abd5a294SJed Brown 1127ef20d060SBarry Smith Level: beginner 1128ef20d060SBarry Smith 11290ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError() 1130ef20d060SBarry Smith @*/ 1131ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1132ef20d060SBarry Smith { 1133ef20d060SBarry Smith PetscErrorCode ierr; 1134ef20d060SBarry Smith DM dm; 1135ef20d060SBarry Smith 1136ef20d060SBarry Smith PetscFunctionBegin; 1137ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1138ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1139ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 1140ef20d060SBarry Smith PetscFunctionReturn(0); 1141ef20d060SBarry Smith } 1142ef20d060SBarry Smith 11439b7cd975SBarry Smith /*@C 11449b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 11459b7cd975SBarry Smith 11469b7cd975SBarry Smith Logically Collective on TS 11479b7cd975SBarry Smith 11489b7cd975SBarry Smith Input Parameters: 11499b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 1150e162b725SBarry Smith . func - routine for evaluating the forcing function 11519b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 11520298fd71SBarry Smith function evaluation routine (may be NULL) 11539b7cd975SBarry Smith 11549b7cd975SBarry Smith Calling sequence of func: 11556bc98fa9SBarry Smith $ PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx); 11569b7cd975SBarry Smith 11579b7cd975SBarry Smith + t - current timestep 1158e162b725SBarry Smith . f - output vector 11599b7cd975SBarry Smith - ctx - [optional] user-defined function context 11609b7cd975SBarry Smith 11619b7cd975SBarry Smith Notes: 11629b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1163e162b725SBarry 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 1164e162b725SBarry Smith definition of the problem you are solving and hence possibly introducing bugs. 1165e162b725SBarry Smith 1166e162b725SBarry Smith This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0 1167e162b725SBarry Smith 1168e162b725SBarry 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 1169e162b725SBarry Smith parameters can be passed in the ctx variable. 11709b7cd975SBarry Smith 11719b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 11729b7cd975SBarry Smith 11739b7cd975SBarry Smith Level: beginner 11749b7cd975SBarry Smith 11759b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 11769b7cd975SBarry Smith @*/ 1177e162b725SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx) 11789b7cd975SBarry Smith { 11799b7cd975SBarry Smith PetscErrorCode ierr; 11809b7cd975SBarry Smith DM dm; 11819b7cd975SBarry Smith 11829b7cd975SBarry Smith PetscFunctionBegin; 11839b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11849b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1185e162b725SBarry Smith ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr); 11869b7cd975SBarry Smith PetscFunctionReturn(0); 11879b7cd975SBarry Smith } 11889b7cd975SBarry Smith 1189d763cef2SBarry Smith /*@C 1190f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1191b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1192d763cef2SBarry Smith 11933f9fe445SBarry Smith Logically Collective on TS 1194d763cef2SBarry Smith 1195d763cef2SBarry Smith Input Parameters: 1196d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1197e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1198e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1199d763cef2SBarry Smith . f - the Jacobian evaluation routine 1200d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 12010298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1202d763cef2SBarry Smith 1203f7ab8db6SBarry Smith Calling sequence of f: 12046bc98fa9SBarry Smith $ PetscErrorCode func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1205d763cef2SBarry Smith 1206d763cef2SBarry Smith + t - current timestep 1207d763cef2SBarry Smith . u - input vector 1208e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1209e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1210d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1211d763cef2SBarry Smith 12126cd88445SBarry Smith Notes: 12136cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 12146cd88445SBarry Smith 12156cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1216ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1217d763cef2SBarry Smith 1218d763cef2SBarry Smith Level: beginner 1219d763cef2SBarry Smith 1220ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1221d763cef2SBarry Smith 1222d763cef2SBarry Smith @*/ 1223e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1224d763cef2SBarry Smith { 1225277b19d0SLisandro Dalcin PetscErrorCode ierr; 1226089b2837SJed Brown SNES snes; 122724989b8cSPeter Brune DM dm; 122824989b8cSPeter Brune TSIJacobian ijacobian; 1229277b19d0SLisandro Dalcin 1230d763cef2SBarry Smith PetscFunctionBegin; 12310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1232e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1233e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1234e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1235e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1236d763cef2SBarry Smith 123724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 123824989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1239e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1240e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1241e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1242e1244c69SJed Brown } 12430298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1244089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12455f659677SPeter Brune if (!ijacobian) { 1246e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 12470e4ef248SJed Brown } 1248e5d3d808SBarry Smith if (Amat) { 1249e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 12500e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1251e5d3d808SBarry Smith ts->Arhs = Amat; 12520e4ef248SJed Brown } 1253e5d3d808SBarry Smith if (Pmat) { 1254e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 12550e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1256e5d3d808SBarry Smith ts->Brhs = Pmat; 12570e4ef248SJed Brown } 1258d763cef2SBarry Smith PetscFunctionReturn(0); 1259d763cef2SBarry Smith } 1260d763cef2SBarry Smith 1261316643e7SJed Brown /*@C 1262b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1263316643e7SJed Brown 12643f9fe445SBarry Smith Logically Collective on TS 1265316643e7SJed Brown 1266316643e7SJed Brown Input Parameters: 1267316643e7SJed Brown + ts - the TS context obtained from TSCreate() 12680298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1269316643e7SJed Brown . f - the function evaluation routine 12700298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1271316643e7SJed Brown 1272316643e7SJed Brown Calling sequence of f: 12736bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1274316643e7SJed Brown 1275316643e7SJed Brown + t - time at step/stage being solved 1276316643e7SJed Brown . u - state vector 1277316643e7SJed Brown . u_t - time derivative of state vector 1278316643e7SJed Brown . F - function vector 1279316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1280316643e7SJed Brown 1281316643e7SJed Brown Important: 12822bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1283316643e7SJed Brown 1284316643e7SJed Brown Level: beginner 1285316643e7SJed Brown 1286d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1287316643e7SJed Brown @*/ 128851699248SLisandro Dalcin PetscErrorCode TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx) 1289316643e7SJed Brown { 1290089b2837SJed Brown PetscErrorCode ierr; 1291089b2837SJed Brown SNES snes; 129251699248SLisandro Dalcin Vec ralloc = NULL; 129324989b8cSPeter Brune DM dm; 1294316643e7SJed Brown 1295316643e7SJed Brown PetscFunctionBegin; 12960700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 129751699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 129824989b8cSPeter Brune 129924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 130024989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 130124989b8cSPeter Brune 1302089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 130351699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 130451699248SLisandro Dalcin ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 130551699248SLisandro Dalcin r = ralloc; 1306e856ceecSJed Brown } 130751699248SLisandro Dalcin ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 130851699248SLisandro Dalcin ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1309089b2837SJed Brown PetscFunctionReturn(0); 1310089b2837SJed Brown } 1311089b2837SJed Brown 1312089b2837SJed Brown /*@C 1313089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1314089b2837SJed Brown 1315089b2837SJed Brown Not Collective 1316089b2837SJed Brown 1317089b2837SJed Brown Input Parameter: 1318089b2837SJed Brown . ts - the TS context 1319089b2837SJed Brown 1320089b2837SJed Brown Output Parameter: 13210298fd71SBarry Smith + r - vector to hold residual (or NULL) 13220298fd71SBarry Smith . func - the function to compute residual (or NULL) 13230298fd71SBarry Smith - ctx - the function context (or NULL) 1324089b2837SJed Brown 1325089b2837SJed Brown Level: advanced 1326089b2837SJed Brown 1327089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1328089b2837SJed Brown @*/ 1329089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1330089b2837SJed Brown { 1331089b2837SJed Brown PetscErrorCode ierr; 1332089b2837SJed Brown SNES snes; 133324989b8cSPeter Brune DM dm; 1334089b2837SJed Brown 1335089b2837SJed Brown PetscFunctionBegin; 1336089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1337089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13380298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 133924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 134024989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1341089b2837SJed Brown PetscFunctionReturn(0); 1342089b2837SJed Brown } 1343089b2837SJed Brown 1344089b2837SJed Brown /*@C 1345089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1346089b2837SJed Brown 1347089b2837SJed Brown Not Collective 1348089b2837SJed Brown 1349089b2837SJed Brown Input Parameter: 1350089b2837SJed Brown . ts - the TS context 1351089b2837SJed Brown 1352089b2837SJed Brown Output Parameter: 13530298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 13540298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 13550298fd71SBarry Smith - ctx - the function context (or NULL) 1356089b2837SJed Brown 1357089b2837SJed Brown Level: advanced 1358089b2837SJed Brown 13592bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1360089b2837SJed Brown @*/ 1361089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1362089b2837SJed Brown { 1363089b2837SJed Brown PetscErrorCode ierr; 1364089b2837SJed Brown SNES snes; 136524989b8cSPeter Brune DM dm; 1366089b2837SJed Brown 1367089b2837SJed Brown PetscFunctionBegin; 1368089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1369089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13700298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 137124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 137224989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1373316643e7SJed Brown PetscFunctionReturn(0); 1374316643e7SJed Brown } 1375316643e7SJed Brown 1376316643e7SJed Brown /*@C 1377a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1378ae8867d6SBarry Smith provided with TSSetIFunction(). 1379316643e7SJed Brown 13803f9fe445SBarry Smith Logically Collective on TS 1381316643e7SJed Brown 1382316643e7SJed Brown Input Parameters: 1383316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1384e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1385e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1386316643e7SJed Brown . f - the Jacobian evaluation routine 13870298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1388316643e7SJed Brown 1389316643e7SJed Brown Calling sequence of f: 13906bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1391316643e7SJed Brown 1392316643e7SJed Brown + t - time at step/stage being solved 13931b4a444bSJed Brown . U - state vector 13941b4a444bSJed Brown . U_t - time derivative of state vector 1395316643e7SJed Brown . a - shift 1396e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1397e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1398316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1399316643e7SJed Brown 1400316643e7SJed Brown Notes: 1401e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1402316643e7SJed Brown 1403895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1404895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1405895c21f2SBarry Smith 1406a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1407b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1408a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1409a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1410a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1411a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1412a4f0a591SBarry Smith 14136cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 14146cd88445SBarry Smith 14156cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1416ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1417ca5f011dSBarry Smith 1418316643e7SJed Brown Level: beginner 1419316643e7SJed Brown 1420ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1421316643e7SJed Brown 1422316643e7SJed Brown @*/ 1423e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1424316643e7SJed Brown { 1425316643e7SJed Brown PetscErrorCode ierr; 1426089b2837SJed Brown SNES snes; 142724989b8cSPeter Brune DM dm; 1428316643e7SJed Brown 1429316643e7SJed Brown PetscFunctionBegin; 14300700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1431e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1432e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1433e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1434e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 143524989b8cSPeter Brune 143624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 143724989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 143824989b8cSPeter Brune 1439089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1440e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1441316643e7SJed Brown PetscFunctionReturn(0); 1442316643e7SJed Brown } 1443316643e7SJed Brown 1444e1244c69SJed Brown /*@ 1445e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1446e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1447e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1448e1244c69SJed Brown not been changed by the TS. 1449e1244c69SJed Brown 1450e1244c69SJed Brown Logically Collective 1451e1244c69SJed Brown 1452e1244c69SJed Brown Input Arguments: 1453e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1454e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1455e1244c69SJed Brown 1456e1244c69SJed Brown Level: intermediate 1457e1244c69SJed Brown 1458e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1459e1244c69SJed Brown @*/ 1460e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1461e1244c69SJed Brown { 1462e1244c69SJed Brown PetscFunctionBegin; 1463e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1464e1244c69SJed Brown PetscFunctionReturn(0); 1465e1244c69SJed Brown } 1466e1244c69SJed Brown 1467efe9872eSLisandro Dalcin /*@C 1468efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1469efe9872eSLisandro Dalcin 1470efe9872eSLisandro Dalcin Logically Collective on TS 1471efe9872eSLisandro Dalcin 1472efe9872eSLisandro Dalcin Input Parameters: 1473efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1474efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1475efe9872eSLisandro Dalcin . fun - the function evaluation routine 1476efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1477efe9872eSLisandro Dalcin 1478efe9872eSLisandro Dalcin Calling sequence of fun: 14796bc98fa9SBarry Smith $ PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1480efe9872eSLisandro Dalcin 1481efe9872eSLisandro Dalcin + t - time at step/stage being solved 1482efe9872eSLisandro Dalcin . U - state vector 1483efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1484efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1485efe9872eSLisandro Dalcin . F - function vector 1486efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1487efe9872eSLisandro Dalcin 1488efe9872eSLisandro Dalcin Level: beginner 1489efe9872eSLisandro Dalcin 1490efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1491efe9872eSLisandro Dalcin @*/ 1492efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx) 1493efe9872eSLisandro Dalcin { 1494efe9872eSLisandro Dalcin DM dm; 1495efe9872eSLisandro Dalcin PetscErrorCode ierr; 1496efe9872eSLisandro Dalcin 1497efe9872eSLisandro Dalcin PetscFunctionBegin; 1498efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1499efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2); 1500efe9872eSLisandro Dalcin ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr); 1501efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1502efe9872eSLisandro Dalcin ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1503efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1504efe9872eSLisandro Dalcin } 1505efe9872eSLisandro Dalcin 1506efe9872eSLisandro Dalcin /*@C 1507efe9872eSLisandro Dalcin TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1508efe9872eSLisandro Dalcin 1509efe9872eSLisandro Dalcin Not Collective 1510efe9872eSLisandro Dalcin 1511efe9872eSLisandro Dalcin Input Parameter: 1512efe9872eSLisandro Dalcin . ts - the TS context 1513efe9872eSLisandro Dalcin 1514efe9872eSLisandro Dalcin Output Parameter: 1515efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1516efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1517efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1518efe9872eSLisandro Dalcin 1519efe9872eSLisandro Dalcin Level: advanced 1520efe9872eSLisandro Dalcin 1521efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction() 1522efe9872eSLisandro Dalcin @*/ 1523efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx) 1524efe9872eSLisandro Dalcin { 1525efe9872eSLisandro Dalcin PetscErrorCode ierr; 1526efe9872eSLisandro Dalcin SNES snes; 1527efe9872eSLisandro Dalcin DM dm; 1528efe9872eSLisandro Dalcin 1529efe9872eSLisandro Dalcin PetscFunctionBegin; 1530efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1531efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1532efe9872eSLisandro Dalcin ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 1533efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1534efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1535efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1536efe9872eSLisandro Dalcin } 1537efe9872eSLisandro Dalcin 1538efe9872eSLisandro Dalcin /*@C 1539bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1540efe9872eSLisandro Dalcin where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function(). 1541efe9872eSLisandro Dalcin 1542efe9872eSLisandro Dalcin Logically Collective on TS 1543efe9872eSLisandro Dalcin 1544efe9872eSLisandro Dalcin Input Parameters: 1545efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1546efe9872eSLisandro Dalcin . J - Jacobian matrix 1547efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1548efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1549efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1550efe9872eSLisandro Dalcin 1551efe9872eSLisandro Dalcin Calling sequence of jac: 15526bc98fa9SBarry Smith $ PetscErrorCode jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx); 1553efe9872eSLisandro Dalcin 1554efe9872eSLisandro Dalcin + t - time at step/stage being solved 1555efe9872eSLisandro Dalcin . U - state vector 1556efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1557efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1558efe9872eSLisandro Dalcin . v - shift for U_t 1559efe9872eSLisandro Dalcin . a - shift for U_tt 1560efe9872eSLisandro 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 1561efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1562efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1563efe9872eSLisandro Dalcin 1564efe9872eSLisandro Dalcin Notes: 1565efe9872eSLisandro Dalcin The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve. 1566efe9872eSLisandro Dalcin 1567efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1568efe9872eSLisandro 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. 1569efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1570bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1571efe9872eSLisandro Dalcin 1572efe9872eSLisandro Dalcin Level: beginner 1573efe9872eSLisandro Dalcin 1574efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1575efe9872eSLisandro Dalcin @*/ 1576efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx) 1577efe9872eSLisandro Dalcin { 1578efe9872eSLisandro Dalcin DM dm; 1579efe9872eSLisandro Dalcin PetscErrorCode ierr; 1580efe9872eSLisandro Dalcin 1581efe9872eSLisandro Dalcin PetscFunctionBegin; 1582efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1583efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2); 1584efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3); 1585efe9872eSLisandro Dalcin ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr); 1586efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1587efe9872eSLisandro Dalcin ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1588efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1589efe9872eSLisandro Dalcin } 1590efe9872eSLisandro Dalcin 1591efe9872eSLisandro Dalcin /*@C 1592efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1593efe9872eSLisandro Dalcin 1594efe9872eSLisandro Dalcin Not Collective, but parallel objects are returned if TS is parallel 1595efe9872eSLisandro Dalcin 1596efe9872eSLisandro Dalcin Input Parameter: 1597efe9872eSLisandro Dalcin . ts - The TS context obtained from TSCreate() 1598efe9872eSLisandro Dalcin 1599efe9872eSLisandro Dalcin Output Parameters: 1600efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1601efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1602efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1603efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1604efe9872eSLisandro Dalcin 160595452b02SPatrick Sanan Notes: 160695452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 1607efe9872eSLisandro Dalcin 1608efe9872eSLisandro Dalcin Level: advanced 1609efe9872eSLisandro Dalcin 161080275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 1611efe9872eSLisandro Dalcin 1612efe9872eSLisandro Dalcin @*/ 1613efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx) 1614efe9872eSLisandro Dalcin { 1615efe9872eSLisandro Dalcin PetscErrorCode ierr; 1616efe9872eSLisandro Dalcin SNES snes; 1617efe9872eSLisandro Dalcin DM dm; 1618efe9872eSLisandro Dalcin 1619efe9872eSLisandro Dalcin PetscFunctionBegin; 1620efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1621efe9872eSLisandro Dalcin ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 1622efe9872eSLisandro Dalcin ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr); 1623efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1624efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1625efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1626efe9872eSLisandro Dalcin } 1627efe9872eSLisandro Dalcin 1628efe9872eSLisandro Dalcin /*@ 1629efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1630efe9872eSLisandro Dalcin 1631d083f849SBarry Smith Collective on TS 1632efe9872eSLisandro Dalcin 1633efe9872eSLisandro Dalcin Input Parameters: 1634efe9872eSLisandro Dalcin + ts - the TS context 1635efe9872eSLisandro Dalcin . t - current time 1636efe9872eSLisandro Dalcin . U - state vector 1637efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1638efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1639efe9872eSLisandro Dalcin 1640efe9872eSLisandro Dalcin Output Parameter: 1641efe9872eSLisandro Dalcin . F - the residual vector 1642efe9872eSLisandro Dalcin 1643efe9872eSLisandro Dalcin Note: 1644efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1645efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1646efe9872eSLisandro Dalcin 1647efe9872eSLisandro Dalcin Level: developer 1648efe9872eSLisandro Dalcin 1649efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1650efe9872eSLisandro Dalcin @*/ 1651efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F) 1652efe9872eSLisandro Dalcin { 1653efe9872eSLisandro Dalcin DM dm; 1654efe9872eSLisandro Dalcin TSI2Function I2Function; 1655efe9872eSLisandro Dalcin void *ctx; 1656efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1657efe9872eSLisandro Dalcin PetscErrorCode ierr; 1658efe9872eSLisandro Dalcin 1659efe9872eSLisandro Dalcin PetscFunctionBegin; 1660efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1661efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1662efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1663efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1664efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F,VEC_CLASSID,6); 1665efe9872eSLisandro Dalcin 1666efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1667efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr); 1668efe9872eSLisandro Dalcin ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 1669efe9872eSLisandro Dalcin 1670efe9872eSLisandro Dalcin if (!I2Function) { 1671efe9872eSLisandro Dalcin ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr); 1672efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1673efe9872eSLisandro Dalcin } 1674efe9872eSLisandro Dalcin 1675efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1676efe9872eSLisandro Dalcin 1677efe9872eSLisandro Dalcin PetscStackPush("TS user implicit function"); 1678efe9872eSLisandro Dalcin ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr); 1679efe9872eSLisandro Dalcin PetscStackPop; 1680efe9872eSLisandro Dalcin 1681efe9872eSLisandro Dalcin if (rhsfunction) { 1682efe9872eSLisandro Dalcin Vec Frhs; 1683efe9872eSLisandro Dalcin ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 1684efe9872eSLisandro Dalcin ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 1685efe9872eSLisandro Dalcin ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr); 1686efe9872eSLisandro Dalcin } 1687efe9872eSLisandro Dalcin 1688efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1689efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1690efe9872eSLisandro Dalcin } 1691efe9872eSLisandro Dalcin 1692efe9872eSLisandro Dalcin /*@ 1693efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1694efe9872eSLisandro Dalcin 1695d083f849SBarry Smith Collective on TS 1696efe9872eSLisandro Dalcin 1697efe9872eSLisandro Dalcin Input Parameters: 1698efe9872eSLisandro Dalcin + ts - the TS context 1699efe9872eSLisandro Dalcin . t - current timestep 1700efe9872eSLisandro Dalcin . U - state vector 1701efe9872eSLisandro Dalcin . V - time derivative of state vector 1702efe9872eSLisandro Dalcin . A - second time derivative of state vector 1703efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1704efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1705efe9872eSLisandro Dalcin 1706efe9872eSLisandro Dalcin Output Parameters: 1707efe9872eSLisandro Dalcin + J - Jacobian matrix 1708efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1709efe9872eSLisandro Dalcin 1710efe9872eSLisandro Dalcin Notes: 1711efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1712efe9872eSLisandro Dalcin 1713efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1714efe9872eSLisandro Dalcin 1715efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1716efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1717efe9872eSLisandro Dalcin 1718efe9872eSLisandro Dalcin Level: developer 1719efe9872eSLisandro Dalcin 1720efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1721efe9872eSLisandro Dalcin @*/ 1722efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P) 1723efe9872eSLisandro Dalcin { 1724efe9872eSLisandro Dalcin DM dm; 1725efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1726efe9872eSLisandro Dalcin void *ctx; 1727efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1728efe9872eSLisandro Dalcin PetscErrorCode ierr; 1729efe9872eSLisandro Dalcin 1730efe9872eSLisandro Dalcin PetscFunctionBegin; 1731efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1732efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1733efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1734efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1735efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J,MAT_CLASSID,8); 1736efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P,MAT_CLASSID,9); 1737efe9872eSLisandro Dalcin 1738efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1739efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr); 1740efe9872eSLisandro Dalcin ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 1741efe9872eSLisandro Dalcin 1742efe9872eSLisandro Dalcin if (!I2Jacobian) { 1743efe9872eSLisandro Dalcin ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr); 1744efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1745efe9872eSLisandro Dalcin } 1746efe9872eSLisandro Dalcin 1747efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1748efe9872eSLisandro Dalcin 1749efe9872eSLisandro Dalcin PetscStackPush("TS user implicit Jacobian"); 1750efe9872eSLisandro Dalcin ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr); 1751efe9872eSLisandro Dalcin PetscStackPop; 1752efe9872eSLisandro Dalcin 1753efe9872eSLisandro Dalcin if (rhsjacobian) { 1754efe9872eSLisandro Dalcin Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 1755efe9872eSLisandro Dalcin ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr); 1756efe9872eSLisandro Dalcin ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr); 1757efe9872eSLisandro Dalcin ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr); 1758efe9872eSLisandro Dalcin if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);} 1759efe9872eSLisandro Dalcin } 1760efe9872eSLisandro Dalcin 1761efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1762efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1763efe9872eSLisandro Dalcin } 1764efe9872eSLisandro Dalcin 1765efe9872eSLisandro Dalcin /*@ 1766efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1767efe9872eSLisandro Dalcin for use by the TS routines handling second order equations. 1768efe9872eSLisandro Dalcin 1769d083f849SBarry Smith Logically Collective on TS 1770efe9872eSLisandro Dalcin 1771efe9872eSLisandro Dalcin Input Parameters: 1772efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1773efe9872eSLisandro Dalcin . u - the solution vector 1774efe9872eSLisandro Dalcin - v - the time derivative vector 1775efe9872eSLisandro Dalcin 1776efe9872eSLisandro Dalcin Level: beginner 1777efe9872eSLisandro Dalcin 1778efe9872eSLisandro Dalcin @*/ 1779efe9872eSLisandro Dalcin PetscErrorCode TS2SetSolution(TS ts,Vec u,Vec v) 1780efe9872eSLisandro Dalcin { 1781efe9872eSLisandro Dalcin PetscErrorCode ierr; 1782efe9872eSLisandro Dalcin 1783efe9872eSLisandro Dalcin PetscFunctionBegin; 1784efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1785efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1786efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v,VEC_CLASSID,3); 1787efe9872eSLisandro Dalcin ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 1788efe9872eSLisandro Dalcin ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr); 1789efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 1790efe9872eSLisandro Dalcin ts->vec_dot = v; 1791efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1792efe9872eSLisandro Dalcin } 1793efe9872eSLisandro Dalcin 1794efe9872eSLisandro Dalcin /*@ 1795efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 1796efe9872eSLisandro Dalcin for second order equations. It is valid to call this routine inside the function 1797efe9872eSLisandro Dalcin that you are evaluating in order to move to the new timestep. This vector not 1798efe9872eSLisandro Dalcin changed until the solution at the next timestep has been calculated. 1799efe9872eSLisandro Dalcin 1800efe9872eSLisandro Dalcin Not Collective, but Vec returned is parallel if TS is parallel 1801efe9872eSLisandro Dalcin 1802efe9872eSLisandro Dalcin Input Parameter: 1803efe9872eSLisandro Dalcin . ts - the TS context obtained from TSCreate() 1804efe9872eSLisandro Dalcin 1805efe9872eSLisandro Dalcin Output Parameter: 1806efe9872eSLisandro Dalcin + u - the vector containing the solution 1807efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1808efe9872eSLisandro Dalcin 1809efe9872eSLisandro Dalcin Level: intermediate 1810efe9872eSLisandro Dalcin 1811efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime() 1812efe9872eSLisandro Dalcin 1813efe9872eSLisandro Dalcin @*/ 1814efe9872eSLisandro Dalcin PetscErrorCode TS2GetSolution(TS ts,Vec *u,Vec *v) 1815efe9872eSLisandro Dalcin { 1816efe9872eSLisandro Dalcin PetscFunctionBegin; 1817efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1818efe9872eSLisandro Dalcin if (u) PetscValidPointer(u,2); 1819efe9872eSLisandro Dalcin if (v) PetscValidPointer(v,3); 1820efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1821efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 1822efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1823efe9872eSLisandro Dalcin } 1824efe9872eSLisandro Dalcin 182555849f57SBarry Smith /*@C 182655849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 182755849f57SBarry Smith 182855849f57SBarry Smith Collective on PetscViewer 182955849f57SBarry Smith 183055849f57SBarry Smith Input Parameters: 183155849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 183255849f57SBarry Smith some related function before a call to TSLoad(). 183355849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 183455849f57SBarry Smith 183555849f57SBarry Smith Level: intermediate 183655849f57SBarry Smith 183755849f57SBarry Smith Notes: 183855849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 183955849f57SBarry Smith 184055849f57SBarry Smith Notes for advanced users: 184155849f57SBarry Smith Most users should not need to know the details of the binary storage 184255849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 184355849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 184455849f57SBarry Smith format is 184555849f57SBarry Smith .vb 184655849f57SBarry Smith has not yet been determined 184755849f57SBarry Smith .ve 184855849f57SBarry Smith 184955849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 185055849f57SBarry Smith @*/ 1851f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 185255849f57SBarry Smith { 185355849f57SBarry Smith PetscErrorCode ierr; 185455849f57SBarry Smith PetscBool isbinary; 185555849f57SBarry Smith PetscInt classid; 185655849f57SBarry Smith char type[256]; 18572d53ad75SBarry Smith DMTS sdm; 1858ad6bc421SBarry Smith DM dm; 185955849f57SBarry Smith 186055849f57SBarry Smith PetscFunctionBegin; 1861f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 186255849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 186355849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 186455849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 186555849f57SBarry Smith 1866060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1867ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1868060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1869f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1870f2c2a1b9SBarry Smith if (ts->ops->load) { 1871f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1872f2c2a1b9SBarry Smith } 1873ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1874ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1875ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1876f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1877f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 18782d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 18792d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 188055849f57SBarry Smith PetscFunctionReturn(0); 188155849f57SBarry Smith } 188255849f57SBarry Smith 18839804daf3SBarry Smith #include <petscdraw.h> 1884e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1885e04113cfSBarry Smith #include <petscviewersaws.h> 1886f05ece33SBarry Smith #endif 18877e2c5f70SBarry Smith /*@C 1888d763cef2SBarry Smith TSView - Prints the TS data structure. 1889d763cef2SBarry Smith 18904c49b128SBarry Smith Collective on TS 1891d763cef2SBarry Smith 1892d763cef2SBarry Smith Input Parameters: 1893d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1894d763cef2SBarry Smith - viewer - visualization context 1895d763cef2SBarry Smith 1896d763cef2SBarry Smith Options Database Key: 1897d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1898d763cef2SBarry Smith 1899d763cef2SBarry Smith Notes: 1900d763cef2SBarry Smith The available visualization contexts include 1901b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1902b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1903d763cef2SBarry Smith output where only the first processor opens 1904d763cef2SBarry Smith the file. All other processors send their 1905d763cef2SBarry Smith data to the first processor to print. 1906d763cef2SBarry Smith 1907d763cef2SBarry Smith The user can open an alternative visualization context with 1908b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1909d763cef2SBarry Smith 1910d763cef2SBarry Smith Level: beginner 1911d763cef2SBarry Smith 1912b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1913d763cef2SBarry Smith @*/ 19147087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1915d763cef2SBarry Smith { 1916dfbe8321SBarry Smith PetscErrorCode ierr; 191719fd82e9SBarry Smith TSType type; 19182b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 19192d53ad75SBarry Smith DMTS sdm; 1920e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1921536b137fSBarry Smith PetscBool issaws; 1922f05ece33SBarry Smith #endif 1923d763cef2SBarry Smith 1924d763cef2SBarry Smith PetscFunctionBegin; 19250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 19263050cee2SBarry Smith if (!viewer) { 1927ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 19283050cee2SBarry Smith } 19290700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1930c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1931fd16b177SBarry Smith 1932251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1933251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 193455849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 19352b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1936e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1937536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1938f05ece33SBarry Smith #endif 193932077d6dSBarry Smith if (iascii) { 1940dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 1941efd4aadfSBarry Smith if (ts->ops->view) { 1942efd4aadfSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1943efd4aadfSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1944efd4aadfSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1945efd4aadfSBarry Smith } 1946ef85077eSLisandro Dalcin if (ts->max_steps < PETSC_MAX_INT) { 194777431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 1948ef85077eSLisandro Dalcin } 1949ef85077eSLisandro Dalcin if (ts->max_time < PETSC_MAX_REAL) { 19507c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1951ef85077eSLisandro Dalcin } 1952efd4aadfSBarry Smith if (ts->usessnes) { 1953efd4aadfSBarry Smith PetscBool lin; 1954d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 19555ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1956d763cef2SBarry Smith } 19575ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 19581ef27442SStefano Zampini ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr); 1959efd4aadfSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr); 1960efd4aadfSBarry Smith } 1961193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 1962a0af407cSBarry Smith if (ts->vrtol) { 1963a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of relative error tolerances, ");CHKERRQ(ierr); 1964a0af407cSBarry Smith } else { 1965a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr); 1966a0af407cSBarry Smith } 1967a0af407cSBarry Smith if (ts->vatol) { 1968a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of absolute error tolerances\n");CHKERRQ(ierr); 1969a0af407cSBarry Smith } else { 1970a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr); 1971a0af407cSBarry Smith } 1972825ab935SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1973efd4aadfSBarry Smith ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr); 1974825ab935SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 19750f5bd95cSBarry Smith } else if (isstring) { 1976a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 197736a9e3b9SBarry Smith ierr = PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type);CHKERRQ(ierr); 197836a9e3b9SBarry Smith if (ts->ops->view) {ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);} 197955849f57SBarry Smith } else if (isbinary) { 198055849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 198155849f57SBarry Smith MPI_Comm comm; 198255849f57SBarry Smith PetscMPIInt rank; 198355849f57SBarry Smith char type[256]; 198455849f57SBarry Smith 198555849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 198655849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 198755849f57SBarry Smith if (!rank) { 198855849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 198955849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 199055849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 199155849f57SBarry Smith } 199255849f57SBarry Smith if (ts->ops->view) { 199355849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 199455849f57SBarry Smith } 1995efd4aadfSBarry Smith if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);} 1996f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1997f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 19982d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19992d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 20002b0a91c0SBarry Smith } else if (isdraw) { 20012b0a91c0SBarry Smith PetscDraw draw; 20022b0a91c0SBarry Smith char str[36]; 200389fd9fafSBarry Smith PetscReal x,y,bottom,h; 20042b0a91c0SBarry Smith 20052b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 20062b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 20072b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 20082b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 200951fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 201089fd9fafSBarry Smith bottom = y - h; 20112b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 20122b0a91c0SBarry Smith if (ts->ops->view) { 20132b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 20142b0a91c0SBarry Smith } 2015efd4aadfSBarry Smith if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);} 2016efd4aadfSBarry Smith if (ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 20172b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 2018e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 2019536b137fSBarry Smith } else if (issaws) { 2020d45a07a7SBarry Smith PetscMPIInt rank; 20212657e9d9SBarry Smith const char *name; 20222657e9d9SBarry Smith 20232657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 2024d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 2025d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 2026d45a07a7SBarry Smith char dir[1024]; 2027d45a07a7SBarry Smith 2028e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 2029a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 20302657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 20312657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 20322657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 2033d763cef2SBarry Smith } 20340acecf5bSBarry Smith if (ts->ops->view) { 20350acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 20360acecf5bSBarry Smith } 2037f05ece33SBarry Smith #endif 2038f05ece33SBarry Smith } 203936a9e3b9SBarry Smith if (ts->snes && ts->usessnes) { 204036a9e3b9SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 204136a9e3b9SBarry Smith ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr); 204236a9e3b9SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 204336a9e3b9SBarry Smith } 204436a9e3b9SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 204536a9e3b9SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 2046f05ece33SBarry Smith 2047b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2048251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 2049b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2050d763cef2SBarry Smith PetscFunctionReturn(0); 2051d763cef2SBarry Smith } 2052d763cef2SBarry Smith 2053b07ff414SBarry Smith /*@ 2054d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2055d763cef2SBarry Smith the timesteppers. 2056d763cef2SBarry Smith 20573f9fe445SBarry Smith Logically Collective on TS 2058d763cef2SBarry Smith 2059d763cef2SBarry Smith Input Parameters: 2060d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2061d763cef2SBarry Smith - usrP - optional user context 2062d763cef2SBarry Smith 206395452b02SPatrick Sanan Fortran Notes: 206495452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 2065daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2066daf670e6SBarry Smith 2067d763cef2SBarry Smith Level: intermediate 2068d763cef2SBarry Smith 2069d763cef2SBarry Smith .seealso: TSGetApplicationContext() 2070d763cef2SBarry Smith @*/ 20717087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 2072d763cef2SBarry Smith { 2073d763cef2SBarry Smith PetscFunctionBegin; 20740700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2075d763cef2SBarry Smith ts->user = usrP; 2076d763cef2SBarry Smith PetscFunctionReturn(0); 2077d763cef2SBarry Smith } 2078d763cef2SBarry Smith 2079b07ff414SBarry Smith /*@ 2080d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2081d763cef2SBarry Smith timestepper. 2082d763cef2SBarry Smith 2083d763cef2SBarry Smith Not Collective 2084d763cef2SBarry Smith 2085d763cef2SBarry Smith Input Parameter: 2086d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2087d763cef2SBarry Smith 2088d763cef2SBarry Smith Output Parameter: 2089d763cef2SBarry Smith . usrP - user context 2090d763cef2SBarry Smith 209195452b02SPatrick Sanan Fortran Notes: 209295452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 2093daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2094daf670e6SBarry Smith 2095d763cef2SBarry Smith Level: intermediate 2096d763cef2SBarry Smith 2097d763cef2SBarry Smith .seealso: TSSetApplicationContext() 2098d763cef2SBarry Smith @*/ 2099e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 2100d763cef2SBarry Smith { 2101d763cef2SBarry Smith PetscFunctionBegin; 21020700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2103e71120c6SJed Brown *(void**)usrP = ts->user; 2104d763cef2SBarry Smith PetscFunctionReturn(0); 2105d763cef2SBarry Smith } 2106d763cef2SBarry Smith 2107d763cef2SBarry Smith /*@ 210880275a0aSLisandro Dalcin TSGetStepNumber - Gets the number of steps completed. 2109d763cef2SBarry Smith 2110d763cef2SBarry Smith Not Collective 2111d763cef2SBarry Smith 2112d763cef2SBarry Smith Input Parameter: 2113d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2114d763cef2SBarry Smith 2115d763cef2SBarry Smith Output Parameter: 211680275a0aSLisandro Dalcin . steps - number of steps completed so far 2117d763cef2SBarry Smith 2118d763cef2SBarry Smith Level: intermediate 2119d763cef2SBarry Smith 21209be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 2121d763cef2SBarry Smith @*/ 212280275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps) 2123d763cef2SBarry Smith { 2124d763cef2SBarry Smith PetscFunctionBegin; 21250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 212680275a0aSLisandro Dalcin PetscValidIntPointer(steps,2); 212780275a0aSLisandro Dalcin *steps = ts->steps; 212880275a0aSLisandro Dalcin PetscFunctionReturn(0); 212980275a0aSLisandro Dalcin } 213080275a0aSLisandro Dalcin 213180275a0aSLisandro Dalcin /*@ 213280275a0aSLisandro Dalcin TSSetStepNumber - Sets the number of steps completed. 213380275a0aSLisandro Dalcin 213480275a0aSLisandro Dalcin Logically Collective on TS 213580275a0aSLisandro Dalcin 213680275a0aSLisandro Dalcin Input Parameters: 213780275a0aSLisandro Dalcin + ts - the TS context 213880275a0aSLisandro Dalcin - steps - number of steps completed so far 213980275a0aSLisandro Dalcin 214080275a0aSLisandro Dalcin Notes: 214180275a0aSLisandro Dalcin For most uses of the TS solvers the user need not explicitly call 214280275a0aSLisandro Dalcin TSSetStepNumber(), as the step counter is appropriately updated in 214380275a0aSLisandro Dalcin TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to 214480275a0aSLisandro Dalcin reinitialize timestepping by setting the step counter to zero (and time 214580275a0aSLisandro Dalcin to the initial time) to solve a similar problem with different initial 214680275a0aSLisandro Dalcin conditions or parameters. Other possible use case is to continue 214780275a0aSLisandro Dalcin timestepping from a previously interrupted run in such a way that TS 214880275a0aSLisandro Dalcin monitors will be called with a initial nonzero step counter. 214980275a0aSLisandro Dalcin 215080275a0aSLisandro Dalcin Level: advanced 215180275a0aSLisandro Dalcin 215280275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution() 215380275a0aSLisandro Dalcin @*/ 215480275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps) 215580275a0aSLisandro Dalcin { 215680275a0aSLisandro Dalcin PetscFunctionBegin; 215780275a0aSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 215880275a0aSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,steps,2); 215980275a0aSLisandro Dalcin if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative"); 216080275a0aSLisandro Dalcin ts->steps = steps; 2161d763cef2SBarry Smith PetscFunctionReturn(0); 2162d763cef2SBarry Smith } 2163d763cef2SBarry Smith 2164d763cef2SBarry Smith /*@ 2165d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2166d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2167d763cef2SBarry Smith 21683f9fe445SBarry Smith Logically Collective on TS 2169d763cef2SBarry Smith 2170d763cef2SBarry Smith Input Parameters: 2171d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2172d763cef2SBarry Smith - time_step - the size of the timestep 2173d763cef2SBarry Smith 2174d763cef2SBarry Smith Level: intermediate 2175d763cef2SBarry Smith 2176aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime() 2177d763cef2SBarry Smith 2178d763cef2SBarry Smith @*/ 21797087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 2180d763cef2SBarry Smith { 2181d763cef2SBarry Smith PetscFunctionBegin; 21820700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2183c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 2184d763cef2SBarry Smith ts->time_step = time_step; 2185d763cef2SBarry Smith PetscFunctionReturn(0); 2186d763cef2SBarry Smith } 2187d763cef2SBarry Smith 2188a43b19c4SJed Brown /*@ 218949354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 219049354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 219149354f04SShri Abhyankar or just return at the final time TS computed. 2192a43b19c4SJed Brown 2193a43b19c4SJed Brown Logically Collective on TS 2194a43b19c4SJed Brown 2195a43b19c4SJed Brown Input Parameter: 2196a43b19c4SJed Brown + ts - the time-step context 219749354f04SShri Abhyankar - eftopt - exact final time option 2198a43b19c4SJed Brown 2199feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2200feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2201feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2202feed9e9dSBarry Smith 2203feed9e9dSBarry Smith Options Database: 2204feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2205feed9e9dSBarry Smith 2206ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 2207ee346746SBarry Smith then the final time you selected. 2208ee346746SBarry Smith 2209a43b19c4SJed Brown Level: beginner 2210a43b19c4SJed Brown 2211f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime() 2212a43b19c4SJed Brown @*/ 221349354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 2214a43b19c4SJed Brown { 2215a43b19c4SJed Brown PetscFunctionBegin; 2216a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 221749354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 221849354f04SShri Abhyankar ts->exact_final_time = eftopt; 2219a43b19c4SJed Brown PetscFunctionReturn(0); 2220a43b19c4SJed Brown } 2221a43b19c4SJed Brown 2222d763cef2SBarry Smith /*@ 2223f6953c82SLisandro Dalcin TSGetExactFinalTime - Gets the exact final time option. 2224f6953c82SLisandro Dalcin 2225f6953c82SLisandro Dalcin Not Collective 2226f6953c82SLisandro Dalcin 2227f6953c82SLisandro Dalcin Input Parameter: 2228f6953c82SLisandro Dalcin . ts - the TS context 2229f6953c82SLisandro Dalcin 2230f6953c82SLisandro Dalcin Output Parameter: 2231f6953c82SLisandro Dalcin . eftopt - exact final time option 2232f6953c82SLisandro Dalcin 2233f6953c82SLisandro Dalcin Level: beginner 2234f6953c82SLisandro Dalcin 2235f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime() 2236f6953c82SLisandro Dalcin @*/ 2237f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt) 2238f6953c82SLisandro Dalcin { 2239f6953c82SLisandro Dalcin PetscFunctionBegin; 2240f6953c82SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2241f6953c82SLisandro Dalcin PetscValidPointer(eftopt,2); 2242f6953c82SLisandro Dalcin *eftopt = ts->exact_final_time; 2243f6953c82SLisandro Dalcin PetscFunctionReturn(0); 2244f6953c82SLisandro Dalcin } 2245f6953c82SLisandro Dalcin 2246f6953c82SLisandro Dalcin /*@ 2247d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2248d763cef2SBarry Smith 2249d763cef2SBarry Smith Not Collective 2250d763cef2SBarry Smith 2251d763cef2SBarry Smith Input Parameter: 2252d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2253d763cef2SBarry Smith 2254d763cef2SBarry Smith Output Parameter: 2255d763cef2SBarry Smith . dt - the current timestep size 2256d763cef2SBarry Smith 2257d763cef2SBarry Smith Level: intermediate 2258d763cef2SBarry Smith 2259aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime() 2260d763cef2SBarry Smith 2261d763cef2SBarry Smith @*/ 22627087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 2263d763cef2SBarry Smith { 2264d763cef2SBarry Smith PetscFunctionBegin; 22650700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2266f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 2267d763cef2SBarry Smith *dt = ts->time_step; 2268d763cef2SBarry Smith PetscFunctionReturn(0); 2269d763cef2SBarry Smith } 2270d763cef2SBarry Smith 2271d8e5e3e6SSatish Balay /*@ 2272d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2273d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2274d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2275d763cef2SBarry Smith the solution at the next timestep has been calculated. 2276d763cef2SBarry Smith 2277d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 2278d763cef2SBarry Smith 2279d763cef2SBarry Smith Input Parameter: 2280d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2281d763cef2SBarry Smith 2282d763cef2SBarry Smith Output Parameter: 2283d763cef2SBarry Smith . v - the vector containing the solution 2284d763cef2SBarry Smith 228563e21af5SBarry Smith Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested 228663e21af5SBarry Smith final time. It returns the solution at the next timestep. 228763e21af5SBarry Smith 2288d763cef2SBarry Smith Level: intermediate 2289d763cef2SBarry Smith 22900ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction() 2291d763cef2SBarry Smith 2292d763cef2SBarry Smith @*/ 22937087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 2294d763cef2SBarry Smith { 2295d763cef2SBarry Smith PetscFunctionBegin; 22960700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22974482741eSBarry Smith PetscValidPointer(v,2); 22988737fe31SLisandro Dalcin *v = ts->vec_sol; 2299d763cef2SBarry Smith PetscFunctionReturn(0); 2300d763cef2SBarry Smith } 2301d763cef2SBarry Smith 230203fe5f5eSDebojyoti Ghosh /*@ 2303b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 230403fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2305b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 230603fe5f5eSDebojyoti Ghosh structure as the solution vector. 230703fe5f5eSDebojyoti Ghosh 230803fe5f5eSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 230903fe5f5eSDebojyoti Ghosh 231003fe5f5eSDebojyoti Ghosh Parameters : 2311a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2312b2bf4f3aSDebojyoti Ghosh . n - If v is PETSC_NULL, then the number of solution components is 2313b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 231403fe5f5eSDebojyoti Ghosh returned in v. 2315a2b725a8SWilliam Gropp - v - the vector containing the n-th solution component 231603fe5f5eSDebojyoti Ghosh (may be PETSC_NULL to use this function to find out 2317b2bf4f3aSDebojyoti Ghosh the number of solutions components). 231803fe5f5eSDebojyoti Ghosh 23194cdd57e5SDebojyoti Ghosh Level: advanced 232003fe5f5eSDebojyoti Ghosh 232103fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution() 232203fe5f5eSDebojyoti Ghosh 232303fe5f5eSDebojyoti Ghosh @*/ 2324b2bf4f3aSDebojyoti Ghosh PetscErrorCode TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v) 232503fe5f5eSDebojyoti Ghosh { 232603fe5f5eSDebojyoti Ghosh PetscErrorCode ierr; 232703fe5f5eSDebojyoti Ghosh 232803fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 232903fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2330b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 233103fe5f5eSDebojyoti Ghosh else { 2332b2bf4f3aSDebojyoti Ghosh ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr); 233303fe5f5eSDebojyoti Ghosh } 233403fe5f5eSDebojyoti Ghosh PetscFunctionReturn(0); 233503fe5f5eSDebojyoti Ghosh } 233603fe5f5eSDebojyoti Ghosh 23374cdd57e5SDebojyoti Ghosh /*@ 23384cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 23394cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 23404cdd57e5SDebojyoti Ghosh 23414cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 23424cdd57e5SDebojyoti Ghosh 23434cdd57e5SDebojyoti Ghosh Parameters : 2344a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2345a2b725a8SWilliam Gropp - v - the vector containing the auxiliary solution 23464cdd57e5SDebojyoti Ghosh 23474cdd57e5SDebojyoti Ghosh Level: intermediate 23484cdd57e5SDebojyoti Ghosh 23494cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution() 23504cdd57e5SDebojyoti Ghosh 23514cdd57e5SDebojyoti Ghosh @*/ 23524cdd57e5SDebojyoti Ghosh PetscErrorCode TSGetAuxSolution(TS ts,Vec *v) 23534cdd57e5SDebojyoti Ghosh { 23544cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 23554cdd57e5SDebojyoti Ghosh 23564cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23574cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2358f6356ec7SDebojyoti Ghosh if (ts->ops->getauxsolution) { 23594cdd57e5SDebojyoti Ghosh ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr); 2360f6356ec7SDebojyoti Ghosh } else { 2361f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v); CHKERRQ(ierr); 2362f6356ec7SDebojyoti Ghosh } 23634cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 23644cdd57e5SDebojyoti Ghosh } 23654cdd57e5SDebojyoti Ghosh 23664cdd57e5SDebojyoti Ghosh /*@ 23674cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 23684cdd57e5SDebojyoti Ghosh TSType has an error estimation functionality. 23694cdd57e5SDebojyoti Ghosh 23704cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 23714cdd57e5SDebojyoti Ghosh 23729657682dSDebojyoti Ghosh Note: MUST call after TSSetUp() 23739657682dSDebojyoti Ghosh 23744cdd57e5SDebojyoti Ghosh Parameters : 2375a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2376657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 2377a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 23784cdd57e5SDebojyoti Ghosh 23794cdd57e5SDebojyoti Ghosh Level: intermediate 23804cdd57e5SDebojyoti Ghosh 238157df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError() 23824cdd57e5SDebojyoti Ghosh 23834cdd57e5SDebojyoti Ghosh @*/ 23840a01e1b2SEmil Constantinescu PetscErrorCode TSGetTimeError(TS ts,PetscInt n,Vec *v) 23854cdd57e5SDebojyoti Ghosh { 23864cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 23874cdd57e5SDebojyoti Ghosh 23884cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23894cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2390f6356ec7SDebojyoti Ghosh if (ts->ops->gettimeerror) { 23910a01e1b2SEmil Constantinescu ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr); 2392f6356ec7SDebojyoti Ghosh } else { 2393f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v);CHKERRQ(ierr); 2394f6356ec7SDebojyoti Ghosh } 23954cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 23964cdd57e5SDebojyoti Ghosh } 23974cdd57e5SDebojyoti Ghosh 239857df6a1bSDebojyoti Ghosh /*@ 239957df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 240057df6a1bSDebojyoti Ghosh TSType has an error estimation functionality. This can be used 240157df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 240257df6a1bSDebojyoti Ghosh 240357df6a1bSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 240457df6a1bSDebojyoti Ghosh 240557df6a1bSDebojyoti Ghosh Parameters : 2406a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2407a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 240857df6a1bSDebojyoti Ghosh 240957df6a1bSDebojyoti Ghosh Level: intermediate 241057df6a1bSDebojyoti Ghosh 241157df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError) 241257df6a1bSDebojyoti Ghosh 241357df6a1bSDebojyoti Ghosh @*/ 241457df6a1bSDebojyoti Ghosh PetscErrorCode TSSetTimeError(TS ts,Vec v) 241557df6a1bSDebojyoti Ghosh { 241657df6a1bSDebojyoti Ghosh PetscErrorCode ierr; 241757df6a1bSDebojyoti Ghosh 241857df6a1bSDebojyoti Ghosh PetscFunctionBegin; 241957df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 24209657682dSDebojyoti Ghosh if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first"); 242157df6a1bSDebojyoti Ghosh if (ts->ops->settimeerror) { 242257df6a1bSDebojyoti Ghosh ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr); 242357df6a1bSDebojyoti Ghosh } 242457df6a1bSDebojyoti Ghosh PetscFunctionReturn(0); 242557df6a1bSDebojyoti Ghosh } 242657df6a1bSDebojyoti Ghosh 2427bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2428d8e5e3e6SSatish Balay /*@ 2429bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2430d763cef2SBarry Smith 2431bdad233fSMatthew Knepley Not collective 2432d763cef2SBarry Smith 2433bdad233fSMatthew Knepley Input Parameters: 2434bdad233fSMatthew Knepley + ts - The TS 2435bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2436d763cef2SBarry Smith .vb 24370910c330SBarry Smith U_t - A U = 0 (linear) 24380910c330SBarry Smith U_t - A(t) U = 0 (linear) 24390910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2440d763cef2SBarry Smith .ve 2441d763cef2SBarry Smith 2442d763cef2SBarry Smith Level: beginner 2443d763cef2SBarry Smith 2444bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2445d763cef2SBarry Smith @*/ 24467087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2447a7cc72afSBarry Smith { 24489e2a6581SJed Brown PetscErrorCode ierr; 24499e2a6581SJed Brown 2450d763cef2SBarry Smith PetscFunctionBegin; 24510700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2452bdad233fSMatthew Knepley ts->problem_type = type; 24539e2a6581SJed Brown if (type == TS_LINEAR) { 24549e2a6581SJed Brown SNES snes; 24559e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 24569e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 24579e2a6581SJed Brown } 2458d763cef2SBarry Smith PetscFunctionReturn(0); 2459d763cef2SBarry Smith } 2460d763cef2SBarry Smith 2461bdad233fSMatthew Knepley /*@C 2462bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2463bdad233fSMatthew Knepley 2464bdad233fSMatthew Knepley Not collective 2465bdad233fSMatthew Knepley 2466bdad233fSMatthew Knepley Input Parameter: 2467bdad233fSMatthew Knepley . ts - The TS 2468bdad233fSMatthew Knepley 2469bdad233fSMatthew Knepley Output Parameter: 2470bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2471bdad233fSMatthew Knepley .vb 2472089b2837SJed Brown M U_t = A U 2473089b2837SJed Brown M(t) U_t = A(t) U 2474b5abc632SBarry Smith F(t,U,U_t) 2475bdad233fSMatthew Knepley .ve 2476bdad233fSMatthew Knepley 2477bdad233fSMatthew Knepley Level: beginner 2478bdad233fSMatthew Knepley 2479bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2480bdad233fSMatthew Knepley @*/ 24817087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2482a7cc72afSBarry Smith { 2483bdad233fSMatthew Knepley PetscFunctionBegin; 24840700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 24854482741eSBarry Smith PetscValidIntPointer(type,2); 2486bdad233fSMatthew Knepley *type = ts->problem_type; 2487bdad233fSMatthew Knepley PetscFunctionReturn(0); 2488bdad233fSMatthew Knepley } 2489d763cef2SBarry Smith 2490d763cef2SBarry Smith /*@ 2491d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 2492d763cef2SBarry Smith of a timestepper. 2493d763cef2SBarry Smith 2494d763cef2SBarry Smith Collective on TS 2495d763cef2SBarry Smith 2496d763cef2SBarry Smith Input Parameter: 2497d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2498d763cef2SBarry Smith 2499d763cef2SBarry Smith Notes: 2500d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 2501d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 2502141bd67dSStefano Zampini the call to TSStep() or TSSolve(). However, if one wishes to control this 2503d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 2504141bd67dSStefano Zampini and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve(). 2505d763cef2SBarry Smith 2506d763cef2SBarry Smith Level: advanced 2507d763cef2SBarry Smith 2508141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve() 2509d763cef2SBarry Smith @*/ 25107087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 2511d763cef2SBarry Smith { 2512dfbe8321SBarry Smith PetscErrorCode ierr; 25136c6b9e74SPeter Brune DM dm; 25146c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 2515d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 2516cd11d68dSLisandro Dalcin TSIFunction ifun; 25176c6b9e74SPeter Brune TSIJacobian ijac; 2518efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 25196c6b9e74SPeter Brune TSRHSJacobian rhsjac; 25202ffb9264SLisandro Dalcin PetscBool isnone; 2521d763cef2SBarry Smith 2522d763cef2SBarry Smith PetscFunctionBegin; 25230700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2524277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 2525277b19d0SLisandro Dalcin 25267adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 2527cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 2528cd11d68dSLisandro Dalcin ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr); 2529d763cef2SBarry Smith } 2530277b19d0SLisandro Dalcin 2531277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 2532277b19d0SLisandro Dalcin 2533298bade4SHong Zhang if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */ 2534298bade4SHong Zhang ierr = PetscObjectReference((PetscObject)ts->Jacprhs);CHKERRQ(ierr); 2535298bade4SHong Zhang ts->Jacp = ts->Jacprhs; 2536298bade4SHong Zhang } 2537298bade4SHong Zhang 2538cd4cee2dSHong Zhang if (ts->quadraturets) { 2539cd4cee2dSHong Zhang ierr = TSSetUp(ts->quadraturets);CHKERRQ(ierr); 2540ecf68647SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2541cd4cee2dSHong Zhang ierr = VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);CHKERRQ(ierr); 2542cd4cee2dSHong Zhang } 2543cd4cee2dSHong Zhang 2544971015bcSStefano Zampini ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr); 2545971015bcSStefano Zampini if (ts->rhsjacobian.reuse && rhsjac == TSComputeRHSJacobianConstant) { 2546e1244c69SJed Brown Mat Amat,Pmat; 2547e1244c69SJed Brown SNES snes; 2548e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2549e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 2550e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2551e1244c69SJed Brown * have displaced the RHS matrix */ 2552971015bcSStefano Zampini if (Amat && Amat == ts->Arhs) { 2553abc0d4abSBarry 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 */ 2554abc0d4abSBarry Smith ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr); 2555e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 2556e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 2557e1244c69SJed Brown } 2558971015bcSStefano Zampini if (Pmat && Pmat == ts->Brhs) { 2559abc0d4abSBarry Smith ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 2560e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 2561e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 2562e1244c69SJed Brown } 2563e1244c69SJed Brown } 25642ffb9264SLisandro Dalcin 25652ffb9264SLisandro Dalcin ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 25662ffb9264SLisandro Dalcin ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr); 25672ffb9264SLisandro Dalcin 2568277b19d0SLisandro Dalcin if (ts->ops->setup) { 2569000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 2570277b19d0SLisandro Dalcin } 2571277b19d0SLisandro Dalcin 25722ffb9264SLisandro Dalcin /* Attempt to check/preset a default value for the exact final time option */ 25732ffb9264SLisandro Dalcin ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr); 25742ffb9264SLisandro Dalcin if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) 25752ffb9264SLisandro Dalcin ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP; 25762ffb9264SLisandro Dalcin 2577a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 25786c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 25796c6b9e74SPeter Brune */ 25806c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 25810298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 25826c6b9e74SPeter Brune if (!func) { 25836c6b9e74SPeter Brune ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 25846c6b9e74SPeter Brune } 2585a6772fa2SLisandro 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. 25866c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 25876c6b9e74SPeter Brune */ 25880298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 25890298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 2590efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr); 25910298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 2592efe9872eSLisandro Dalcin if (!jac && (ijac || i2jac || rhsjac)) { 25936c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 25946c6b9e74SPeter Brune } 2595c0517034SDebojyoti Ghosh 2596c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 2597c0517034SDebojyoti Ghosh if (ts->ops->startingmethod) { 2598c0517034SDebojyoti Ghosh ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr); 2599c0517034SDebojyoti Ghosh } 2600c0517034SDebojyoti Ghosh 2601277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2602277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2603277b19d0SLisandro Dalcin } 2604277b19d0SLisandro Dalcin 2605f6a906c0SBarry Smith /*@ 2606277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2607277b19d0SLisandro Dalcin 2608277b19d0SLisandro Dalcin Collective on TS 2609277b19d0SLisandro Dalcin 2610277b19d0SLisandro Dalcin Input Parameter: 2611277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2612277b19d0SLisandro Dalcin 2613277b19d0SLisandro Dalcin Level: beginner 2614277b19d0SLisandro Dalcin 2615277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2616277b19d0SLisandro Dalcin @*/ 2617277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2618277b19d0SLisandro Dalcin { 26191d06f6b3SHong Zhang TS_RHSSplitLink ilink = ts->tsrhssplit,next; 2620277b19d0SLisandro Dalcin PetscErrorCode ierr; 2621277b19d0SLisandro Dalcin 2622277b19d0SLisandro Dalcin PetscFunctionBegin; 2623277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2624b18ea86cSHong Zhang 2625277b19d0SLisandro Dalcin if (ts->ops->reset) { 2626277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2627277b19d0SLisandro Dalcin } 2628277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2629e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2630bbd56ea5SKarl Rupp 26314e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 26324e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2633214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 26346bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2635efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 2636e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2637e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 263838637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2639bbd56ea5SKarl Rupp 2640cd4cee2dSHong Zhang ierr = MatDestroy(&ts->Jacprhs);CHKERRQ(ierr); 2641ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2642ecf68647SHong Zhang if (ts->forward_solve) { 2643ecf68647SHong Zhang ierr = TSForwardReset(ts);CHKERRQ(ierr); 2644ecf68647SHong Zhang } 2645cd4cee2dSHong Zhang if (ts->quadraturets) { 2646cd4cee2dSHong Zhang ierr = TSReset(ts->quadraturets);CHKERRQ(ierr); 264736eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2648cd4cee2dSHong Zhang } 26491d06f6b3SHong Zhang while (ilink) { 26501d06f6b3SHong Zhang next = ilink->next; 26511d06f6b3SHong Zhang ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr); 26521d06f6b3SHong Zhang ierr = PetscFree(ilink->splitname);CHKERRQ(ierr); 26531d06f6b3SHong Zhang ierr = ISDestroy(&ilink->is);CHKERRQ(ierr); 26541d06f6b3SHong Zhang ierr = PetscFree(ilink);CHKERRQ(ierr); 26551d06f6b3SHong Zhang ilink = next; 265687f4e208SHong Zhang } 2657545aaa6fSHong Zhang ts->num_rhs_splits = 0; 2658277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2659d763cef2SBarry Smith PetscFunctionReturn(0); 2660d763cef2SBarry Smith } 2661d763cef2SBarry Smith 2662d8e5e3e6SSatish Balay /*@ 2663d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2664d763cef2SBarry Smith with TSCreate(). 2665d763cef2SBarry Smith 2666d763cef2SBarry Smith Collective on TS 2667d763cef2SBarry Smith 2668d763cef2SBarry Smith Input Parameter: 2669d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2670d763cef2SBarry Smith 2671d763cef2SBarry Smith Level: beginner 2672d763cef2SBarry Smith 2673d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2674d763cef2SBarry Smith @*/ 26756bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2676d763cef2SBarry Smith { 26776849ba73SBarry Smith PetscErrorCode ierr; 2678d763cef2SBarry Smith 2679d763cef2SBarry Smith PetscFunctionBegin; 26806bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 2681ecf68647SHong Zhang PetscValidHeaderSpecific(*ts,TS_CLASSID,1); 26826bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2683d763cef2SBarry Smith 2684ecf68647SHong Zhang ierr = TSReset(*ts);CHKERRQ(ierr); 2685ecf68647SHong Zhang ierr = TSAdjointReset(*ts);CHKERRQ(ierr); 2686ecf68647SHong Zhang if ((*ts)->forward_solve) { 2687ecf68647SHong Zhang ierr = TSForwardReset(*ts);CHKERRQ(ierr); 2688ecf68647SHong Zhang } 2689e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2690e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 26916bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 26926d4c513bSLisandro Dalcin 2693bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2694bc952696SBarry Smith 269584df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 26966427ac75SLisandro Dalcin ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr); 26976427ac75SLisandro Dalcin 26986bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 26996bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 27006bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 27010dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 27026d4c513bSLisandro Dalcin 2703cd4cee2dSHong Zhang ierr = TSDestroy(&(*ts)->quadraturets);CHKERRQ(ierr); 2704a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2705d763cef2SBarry Smith PetscFunctionReturn(0); 2706d763cef2SBarry Smith } 2707d763cef2SBarry Smith 2708d8e5e3e6SSatish Balay /*@ 2709d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2710d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2711d763cef2SBarry Smith 2712d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2713d763cef2SBarry Smith 2714d763cef2SBarry Smith Input Parameter: 2715d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2716d763cef2SBarry Smith 2717d763cef2SBarry Smith Output Parameter: 2718d763cef2SBarry Smith . snes - the nonlinear solver context 2719d763cef2SBarry Smith 2720d763cef2SBarry Smith Notes: 2721d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2722d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 272394b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2724d763cef2SBarry Smith 2725d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 27260298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2727d763cef2SBarry Smith 2728d763cef2SBarry Smith Level: beginner 2729d763cef2SBarry Smith 2730d763cef2SBarry Smith @*/ 27317087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2732d763cef2SBarry Smith { 2733d372ba47SLisandro Dalcin PetscErrorCode ierr; 2734d372ba47SLisandro Dalcin 2735d763cef2SBarry Smith PetscFunctionBegin; 27360700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27374482741eSBarry Smith PetscValidPointer(snes,2); 2738d372ba47SLisandro Dalcin if (!ts->snes) { 2739ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 274016413a6aSBarry Smith ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr); 27410298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 27423bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2743d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2744496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 27459e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 27469e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 27479e2a6581SJed Brown } 2748d372ba47SLisandro Dalcin } 2749d763cef2SBarry Smith *snes = ts->snes; 2750d763cef2SBarry Smith PetscFunctionReturn(0); 2751d763cef2SBarry Smith } 2752d763cef2SBarry Smith 2753deb2cd25SJed Brown /*@ 2754deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2755deb2cd25SJed Brown 2756deb2cd25SJed Brown Collective 2757deb2cd25SJed Brown 2758deb2cd25SJed Brown Input Parameter: 2759deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2760deb2cd25SJed Brown - snes - the nonlinear solver context 2761deb2cd25SJed Brown 2762deb2cd25SJed Brown Notes: 2763deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2764deb2cd25SJed Brown 2765deb2cd25SJed Brown Level: developer 2766deb2cd25SJed Brown 2767deb2cd25SJed Brown @*/ 2768deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2769deb2cd25SJed Brown { 2770deb2cd25SJed Brown PetscErrorCode ierr; 2771d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2772deb2cd25SJed Brown 2773deb2cd25SJed Brown PetscFunctionBegin; 2774deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2775deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2776deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2777deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2778bbd56ea5SKarl Rupp 2779deb2cd25SJed Brown ts->snes = snes; 2780bbd56ea5SKarl Rupp 27810298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 27820298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2783740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 27840298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2785740132f1SEmil Constantinescu } 2786deb2cd25SJed Brown PetscFunctionReturn(0); 2787deb2cd25SJed Brown } 2788deb2cd25SJed Brown 2789d8e5e3e6SSatish Balay /*@ 279094b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2791d763cef2SBarry Smith a TS (timestepper) context. 2792d763cef2SBarry Smith 279394b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2794d763cef2SBarry Smith 2795d763cef2SBarry Smith Input Parameter: 2796d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2797d763cef2SBarry Smith 2798d763cef2SBarry Smith Output Parameter: 279994b7f48cSBarry Smith . ksp - the nonlinear solver context 2800d763cef2SBarry Smith 2801d763cef2SBarry Smith Notes: 280294b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2803d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2804d763cef2SBarry Smith KSP and PC contexts as well. 2805d763cef2SBarry Smith 280694b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 28070298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2808d763cef2SBarry Smith 2809d763cef2SBarry Smith Level: beginner 2810d763cef2SBarry Smith 2811d763cef2SBarry Smith @*/ 28127087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2813d763cef2SBarry Smith { 2814d372ba47SLisandro Dalcin PetscErrorCode ierr; 2815089b2837SJed Brown SNES snes; 2816d372ba47SLisandro Dalcin 2817d763cef2SBarry Smith PetscFunctionBegin; 28180700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28194482741eSBarry Smith PetscValidPointer(ksp,2); 282017186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2821e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2822089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2823089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2824d763cef2SBarry Smith PetscFunctionReturn(0); 2825d763cef2SBarry Smith } 2826d763cef2SBarry Smith 2827d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2828d763cef2SBarry Smith 2829adb62b0dSMatthew Knepley /*@ 2830618ce8baSLisandro Dalcin TSSetMaxSteps - Sets the maximum number of steps to use. 2831618ce8baSLisandro Dalcin 2832618ce8baSLisandro Dalcin Logically Collective on TS 2833618ce8baSLisandro Dalcin 2834618ce8baSLisandro Dalcin Input Parameters: 2835618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 2836618ce8baSLisandro Dalcin - maxsteps - maximum number of steps to use 2837618ce8baSLisandro Dalcin 2838618ce8baSLisandro Dalcin Options Database Keys: 2839618ce8baSLisandro Dalcin . -ts_max_steps <maxsteps> - Sets maxsteps 2840618ce8baSLisandro Dalcin 2841618ce8baSLisandro Dalcin Notes: 2842618ce8baSLisandro Dalcin The default maximum number of steps is 5000 2843618ce8baSLisandro Dalcin 2844618ce8baSLisandro Dalcin Level: intermediate 2845618ce8baSLisandro Dalcin 2846618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime() 2847618ce8baSLisandro Dalcin @*/ 2848618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps) 2849618ce8baSLisandro Dalcin { 2850618ce8baSLisandro Dalcin PetscFunctionBegin; 2851618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2852618ce8baSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2853618ce8baSLisandro Dalcin if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative"); 2854618ce8baSLisandro Dalcin ts->max_steps = maxsteps; 2855618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2856618ce8baSLisandro Dalcin } 2857618ce8baSLisandro Dalcin 2858618ce8baSLisandro Dalcin /*@ 2859618ce8baSLisandro Dalcin TSGetMaxSteps - Gets the maximum number of steps to use. 2860618ce8baSLisandro Dalcin 2861618ce8baSLisandro Dalcin Not Collective 2862618ce8baSLisandro Dalcin 2863618ce8baSLisandro Dalcin Input Parameters: 2864618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 2865618ce8baSLisandro Dalcin 2866618ce8baSLisandro Dalcin Output Parameter: 2867618ce8baSLisandro Dalcin . maxsteps - maximum number of steps to use 2868618ce8baSLisandro Dalcin 2869618ce8baSLisandro Dalcin Level: advanced 2870618ce8baSLisandro Dalcin 2871618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime() 2872618ce8baSLisandro Dalcin @*/ 2873618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps) 2874618ce8baSLisandro Dalcin { 2875618ce8baSLisandro Dalcin PetscFunctionBegin; 2876618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2877618ce8baSLisandro Dalcin PetscValidIntPointer(maxsteps,2); 2878618ce8baSLisandro Dalcin *maxsteps = ts->max_steps; 2879618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2880618ce8baSLisandro Dalcin } 2881618ce8baSLisandro Dalcin 2882618ce8baSLisandro Dalcin /*@ 2883618ce8baSLisandro Dalcin TSSetMaxTime - Sets the maximum (or final) time for timestepping. 2884618ce8baSLisandro Dalcin 2885618ce8baSLisandro Dalcin Logically Collective on TS 2886618ce8baSLisandro Dalcin 2887618ce8baSLisandro Dalcin Input Parameters: 2888618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 2889618ce8baSLisandro Dalcin - maxtime - final time to step to 2890618ce8baSLisandro Dalcin 2891618ce8baSLisandro Dalcin Options Database Keys: 2892ef85077eSLisandro Dalcin . -ts_max_time <maxtime> - Sets maxtime 2893618ce8baSLisandro Dalcin 2894618ce8baSLisandro Dalcin Notes: 2895618ce8baSLisandro Dalcin The default maximum time is 5.0 2896618ce8baSLisandro Dalcin 2897618ce8baSLisandro Dalcin Level: intermediate 2898618ce8baSLisandro Dalcin 2899618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime() 2900618ce8baSLisandro Dalcin @*/ 2901618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime) 2902618ce8baSLisandro Dalcin { 2903618ce8baSLisandro Dalcin PetscFunctionBegin; 2904618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2905618ce8baSLisandro Dalcin PetscValidLogicalCollectiveReal(ts,maxtime,2); 2906618ce8baSLisandro Dalcin ts->max_time = maxtime; 2907618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2908618ce8baSLisandro Dalcin } 2909618ce8baSLisandro Dalcin 2910618ce8baSLisandro Dalcin /*@ 2911618ce8baSLisandro Dalcin TSGetMaxTime - Gets the maximum (or final) time for timestepping. 2912618ce8baSLisandro Dalcin 2913618ce8baSLisandro Dalcin Not Collective 2914618ce8baSLisandro Dalcin 2915618ce8baSLisandro Dalcin Input Parameters: 2916618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 2917618ce8baSLisandro Dalcin 2918618ce8baSLisandro Dalcin Output Parameter: 2919618ce8baSLisandro Dalcin . maxtime - final time to step to 2920618ce8baSLisandro Dalcin 2921618ce8baSLisandro Dalcin Level: advanced 2922618ce8baSLisandro Dalcin 2923618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps() 2924618ce8baSLisandro Dalcin @*/ 2925618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime) 2926618ce8baSLisandro Dalcin { 2927618ce8baSLisandro Dalcin PetscFunctionBegin; 2928618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2929618ce8baSLisandro Dalcin PetscValidRealPointer(maxtime,2); 2930618ce8baSLisandro Dalcin *maxtime = ts->max_time; 2931618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2932618ce8baSLisandro Dalcin } 2933618ce8baSLisandro Dalcin 2934618ce8baSLisandro Dalcin /*@ 2935aaa6c58dSLisandro Dalcin TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep(). 2936edc382c3SSatish Balay 2937edc382c3SSatish Balay Level: deprecated 2938edc382c3SSatish Balay 2939aaa6c58dSLisandro Dalcin @*/ 2940aaa6c58dSLisandro Dalcin PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 2941aaa6c58dSLisandro Dalcin { 2942aaa6c58dSLisandro Dalcin PetscErrorCode ierr; 2943aaa6c58dSLisandro Dalcin PetscFunctionBegin; 2944aaa6c58dSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2945aaa6c58dSLisandro Dalcin ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 2946aaa6c58dSLisandro Dalcin ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 2947aaa6c58dSLisandro Dalcin PetscFunctionReturn(0); 2948aaa6c58dSLisandro Dalcin } 2949aaa6c58dSLisandro Dalcin 2950aaa6c58dSLisandro Dalcin /*@ 295119eac22cSLisandro Dalcin TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime(). 2952edc382c3SSatish Balay 2953edc382c3SSatish Balay Level: deprecated 2954edc382c3SSatish Balay 2955adb62b0dSMatthew Knepley @*/ 29567087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2957adb62b0dSMatthew Knepley { 2958adb62b0dSMatthew Knepley PetscFunctionBegin; 29590700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2960abc0a331SBarry Smith if (maxsteps) { 29614482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2962adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2963adb62b0dSMatthew Knepley } 2964abc0a331SBarry Smith if (maxtime) { 29654482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2966adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2967adb62b0dSMatthew Knepley } 2968adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2969adb62b0dSMatthew Knepley } 2970adb62b0dSMatthew Knepley 2971d763cef2SBarry Smith /*@ 297219eac22cSLisandro Dalcin TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime(). 2973edc382c3SSatish Balay 2974edc382c3SSatish Balay Level: deprecated 2975edc382c3SSatish Balay 2976d763cef2SBarry Smith @*/ 29777087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2978d763cef2SBarry Smith { 2979d763cef2SBarry Smith PetscFunctionBegin; 29800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2981c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2982c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 298339b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 298439b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2985d763cef2SBarry Smith PetscFunctionReturn(0); 2986d763cef2SBarry Smith } 2987d763cef2SBarry Smith 2988d763cef2SBarry Smith /*@ 29895c5f5948SLisandro Dalcin TSGetTimeStepNumber - Deprecated, use TSGetStepNumber(). 2990edc382c3SSatish Balay 2991edc382c3SSatish Balay Level: deprecated 2992edc382c3SSatish Balay 29935c5f5948SLisandro Dalcin @*/ 2994e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 29955c5f5948SLisandro Dalcin 299619eac22cSLisandro Dalcin /*@ 29974f4e0956SLisandro Dalcin TSGetTotalSteps - Deprecated, use TSGetStepNumber(). 2998edc382c3SSatish Balay 2999edc382c3SSatish Balay Level: deprecated 3000edc382c3SSatish Balay 30014f4e0956SLisandro Dalcin @*/ 30024f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 30034f4e0956SLisandro Dalcin 30044f4e0956SLisandro Dalcin /*@ 3005d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 3006d763cef2SBarry Smith for use by the TS routines. 3007d763cef2SBarry Smith 3008d083f849SBarry Smith Logically Collective on TS 3009d763cef2SBarry Smith 3010d763cef2SBarry Smith Input Parameters: 3011d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 30120910c330SBarry Smith - u - the solution vector 3013d763cef2SBarry Smith 3014d763cef2SBarry Smith Level: beginner 3015d763cef2SBarry Smith 30160ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate() 3017d763cef2SBarry Smith @*/ 30180910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 3019d763cef2SBarry Smith { 30208737fe31SLisandro Dalcin PetscErrorCode ierr; 3021496e6a7aSJed Brown DM dm; 30228737fe31SLisandro Dalcin 3023d763cef2SBarry Smith PetscFunctionBegin; 30240700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30250910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 30260910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 30276bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 30280910c330SBarry Smith ts->vec_sol = u; 3029bbd56ea5SKarl Rupp 3030496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 30310910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 3032d763cef2SBarry Smith PetscFunctionReturn(0); 3033d763cef2SBarry Smith } 3034d763cef2SBarry Smith 3035ac226902SBarry Smith /*@C 3036000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 30373f2090d5SJed Brown called once at the beginning of each time step. 3038000e7ae3SMatthew Knepley 30393f9fe445SBarry Smith Logically Collective on TS 3040000e7ae3SMatthew Knepley 3041000e7ae3SMatthew Knepley Input Parameters: 3042000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3043000e7ae3SMatthew Knepley - func - The function 3044000e7ae3SMatthew Knepley 3045000e7ae3SMatthew Knepley Calling sequence of func: 30466bc98fa9SBarry Smith . PetscErrorCode func (TS ts); 3047000e7ae3SMatthew Knepley 3048000e7ae3SMatthew Knepley Level: intermediate 3049000e7ae3SMatthew Knepley 3050dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep() 3051000e7ae3SMatthew Knepley @*/ 30527087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3053000e7ae3SMatthew Knepley { 3054000e7ae3SMatthew Knepley PetscFunctionBegin; 30550700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3056ae60f76fSBarry Smith ts->prestep = func; 3057000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3058000e7ae3SMatthew Knepley } 3059000e7ae3SMatthew Knepley 306009ee8438SJed Brown /*@ 30613f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 30623f2090d5SJed Brown 30633f2090d5SJed Brown Collective on TS 30643f2090d5SJed Brown 30653f2090d5SJed Brown Input Parameters: 30663f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 30673f2090d5SJed Brown 30683f2090d5SJed Brown Notes: 30693f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 30703f2090d5SJed Brown so most users would not generally call this routine themselves. 30713f2090d5SJed Brown 30723f2090d5SJed Brown Level: developer 30733f2090d5SJed Brown 30749be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 30753f2090d5SJed Brown @*/ 30767087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 30773f2090d5SJed Brown { 30783f2090d5SJed Brown PetscErrorCode ierr; 30793f2090d5SJed Brown 30803f2090d5SJed Brown PetscFunctionBegin; 30810700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3082ae60f76fSBarry Smith if (ts->prestep) { 30835efd42a4SStefano Zampini Vec U; 30845efd42a4SStefano Zampini PetscObjectState sprev,spost; 30855efd42a4SStefano Zampini 30865efd42a4SStefano Zampini ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 30875efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3088ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 30895efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3090dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 3091312ce896SJed Brown } 30923f2090d5SJed Brown PetscFunctionReturn(0); 30933f2090d5SJed Brown } 30943f2090d5SJed Brown 3095b8123daeSJed Brown /*@C 3096b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3097b8123daeSJed Brown called once at the beginning of each stage. 3098b8123daeSJed Brown 3099b8123daeSJed Brown Logically Collective on TS 3100b8123daeSJed Brown 3101b8123daeSJed Brown Input Parameters: 3102b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 3103b8123daeSJed Brown - func - The function 3104b8123daeSJed Brown 3105b8123daeSJed Brown Calling sequence of func: 3106b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 3107b8123daeSJed Brown 3108b8123daeSJed Brown Level: intermediate 3109b8123daeSJed Brown 3110b8123daeSJed Brown Note: 3111b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 311280275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 3113b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 3114b8123daeSJed Brown 31159be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3116b8123daeSJed Brown @*/ 3117b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 3118b8123daeSJed Brown { 3119b8123daeSJed Brown PetscFunctionBegin; 3120b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3121ae60f76fSBarry Smith ts->prestage = func; 3122b8123daeSJed Brown PetscFunctionReturn(0); 3123b8123daeSJed Brown } 3124b8123daeSJed Brown 31259be3e283SDebojyoti Ghosh /*@C 31269be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 31279be3e283SDebojyoti Ghosh called once at the end of each stage. 31289be3e283SDebojyoti Ghosh 31299be3e283SDebojyoti Ghosh Logically Collective on TS 31309be3e283SDebojyoti Ghosh 31319be3e283SDebojyoti Ghosh Input Parameters: 31329be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 31339be3e283SDebojyoti Ghosh - func - The function 31349be3e283SDebojyoti Ghosh 31359be3e283SDebojyoti Ghosh Calling sequence of func: 31369be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 31379be3e283SDebojyoti Ghosh 31389be3e283SDebojyoti Ghosh Level: intermediate 31399be3e283SDebojyoti Ghosh 31409be3e283SDebojyoti Ghosh Note: 31419be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 314280275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 31439be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 31449be3e283SDebojyoti Ghosh 31459be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 31469be3e283SDebojyoti Ghosh @*/ 31479be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 31489be3e283SDebojyoti Ghosh { 31499be3e283SDebojyoti Ghosh PetscFunctionBegin; 31509be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 31519be3e283SDebojyoti Ghosh ts->poststage = func; 31529be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 31539be3e283SDebojyoti Ghosh } 31549be3e283SDebojyoti Ghosh 3155c688d042SShri Abhyankar /*@C 3156c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 3157c688d042SShri Abhyankar called once at the end of each step evaluation. 3158c688d042SShri Abhyankar 3159c688d042SShri Abhyankar Logically Collective on TS 3160c688d042SShri Abhyankar 3161c688d042SShri Abhyankar Input Parameters: 3162c688d042SShri Abhyankar + ts - The TS context obtained from TSCreate() 3163c688d042SShri Abhyankar - func - The function 3164c688d042SShri Abhyankar 3165c688d042SShri Abhyankar Calling sequence of func: 3166c688d042SShri Abhyankar . PetscErrorCode func(TS ts); 3167c688d042SShri Abhyankar 3168c688d042SShri Abhyankar Level: intermediate 3169c688d042SShri Abhyankar 3170c688d042SShri Abhyankar Note: 31711785ff2aSShri Abhyankar Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling 31721785ff2aSShri Abhyankar thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep() 3173e7e94ed4SShri Abhyankar may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step 3174e7e94ed4SShri Abhyankar solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step 3175e7e94ed4SShri Abhyankar with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime() 3176c688d042SShri Abhyankar 3177c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3178c688d042SShri Abhyankar @*/ 3179c688d042SShri Abhyankar PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS)) 3180c688d042SShri Abhyankar { 3181c688d042SShri Abhyankar PetscFunctionBegin; 3182c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3183c688d042SShri Abhyankar ts->postevaluate = func; 3184c688d042SShri Abhyankar PetscFunctionReturn(0); 3185c688d042SShri Abhyankar } 3186c688d042SShri Abhyankar 3187b8123daeSJed Brown /*@ 3188b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 3189b8123daeSJed Brown 3190b8123daeSJed Brown Collective on TS 3191b8123daeSJed Brown 3192b8123daeSJed Brown Input Parameters: 3193b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 31949be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3195b8123daeSJed Brown 3196b8123daeSJed Brown Notes: 3197b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 3198b8123daeSJed Brown most users would not generally call this routine themselves. 3199b8123daeSJed Brown 3200b8123daeSJed Brown Level: developer 3201b8123daeSJed Brown 32029be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 3203b8123daeSJed Brown @*/ 3204b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3205b8123daeSJed Brown { 3206b8123daeSJed Brown PetscFunctionBegin; 3207b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3208ae60f76fSBarry Smith if (ts->prestage) { 3209ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 3210b8123daeSJed Brown } 3211b8123daeSJed Brown PetscFunctionReturn(0); 3212b8123daeSJed Brown } 3213b8123daeSJed Brown 32149be3e283SDebojyoti Ghosh /*@ 32159be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 32169be3e283SDebojyoti Ghosh 32179be3e283SDebojyoti Ghosh Collective on TS 32189be3e283SDebojyoti Ghosh 32199be3e283SDebojyoti Ghosh Input Parameters: 32209be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 32219be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 32229be3e283SDebojyoti Ghosh stageindex - Stage number 32239be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 32249be3e283SDebojyoti Ghosh of stages) with the stage solutions 32259be3e283SDebojyoti Ghosh 32269be3e283SDebojyoti Ghosh Notes: 32279be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 32289be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 32299be3e283SDebojyoti Ghosh 32309be3e283SDebojyoti Ghosh Level: developer 32319be3e283SDebojyoti Ghosh 32329be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 32339be3e283SDebojyoti Ghosh @*/ 32349be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 32359be3e283SDebojyoti Ghosh { 32369be3e283SDebojyoti Ghosh PetscFunctionBegin; 32379be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 32384beae5d8SLisandro Dalcin if (ts->poststage) { 32399be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 32409be3e283SDebojyoti Ghosh } 32419be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 32429be3e283SDebojyoti Ghosh } 32439be3e283SDebojyoti Ghosh 3244c688d042SShri Abhyankar /*@ 3245c688d042SShri Abhyankar TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate() 3246c688d042SShri Abhyankar 3247c688d042SShri Abhyankar Collective on TS 3248c688d042SShri Abhyankar 3249c688d042SShri Abhyankar Input Parameters: 3250c688d042SShri Abhyankar . ts - The TS context obtained from TSCreate() 3251c688d042SShri Abhyankar 3252c688d042SShri Abhyankar Notes: 3253c688d042SShri Abhyankar TSPostEvaluate() is typically used within time stepping implementations, 3254c688d042SShri Abhyankar most users would not generally call this routine themselves. 3255c688d042SShri Abhyankar 3256c688d042SShri Abhyankar Level: developer 3257c688d042SShri Abhyankar 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 3299dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep() 3300000e7ae3SMatthew Knepley @*/ 33017087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 3302000e7ae3SMatthew Knepley { 3303000e7ae3SMatthew Knepley PetscFunctionBegin; 33040700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3305ae60f76fSBarry Smith ts->poststep = func; 3306000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3307000e7ae3SMatthew Knepley } 3308000e7ae3SMatthew Knepley 330909ee8438SJed Brown /*@ 33103f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 33113f2090d5SJed Brown 33123f2090d5SJed Brown Collective on TS 33133f2090d5SJed Brown 33143f2090d5SJed Brown Input Parameters: 33153f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 33163f2090d5SJed Brown 33173f2090d5SJed Brown Notes: 33183f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 33193f2090d5SJed Brown so most users would not generally call this routine themselves. 33203f2090d5SJed Brown 33213f2090d5SJed Brown Level: developer 33223f2090d5SJed Brown 33233f2090d5SJed Brown @*/ 33247087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 33253f2090d5SJed Brown { 33263f2090d5SJed Brown PetscErrorCode ierr; 33273f2090d5SJed Brown 33283f2090d5SJed Brown PetscFunctionBegin; 33290700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3330ae60f76fSBarry Smith if (ts->poststep) { 33315efd42a4SStefano Zampini Vec U; 33325efd42a4SStefano Zampini PetscObjectState sprev,spost; 33335efd42a4SStefano Zampini 33345efd42a4SStefano Zampini ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 33355efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3336ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 33375efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3338dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 333972ac3e02SJed Brown } 33403f2090d5SJed Brown PetscFunctionReturn(0); 33413f2090d5SJed Brown } 33423f2090d5SJed Brown 3343d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 3344d763cef2SBarry Smith 3345d763cef2SBarry Smith /*@C 3346a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 3347d763cef2SBarry Smith timestep to display the iteration's progress. 3348d763cef2SBarry Smith 33493f9fe445SBarry Smith Logically Collective on TS 3350d763cef2SBarry Smith 3351d763cef2SBarry Smith Input Parameters: 3352d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3353e213d8f1SJed Brown . monitor - monitoring routine 3354329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 33550298fd71SBarry Smith monitor routine (use NULL if no context is desired) 3356b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 33570298fd71SBarry Smith (may be NULL) 3358d763cef2SBarry Smith 3359e213d8f1SJed Brown Calling sequence of monitor: 3360dcb233daSLisandro Dalcin $ PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 3361d763cef2SBarry Smith 3362d763cef2SBarry Smith + ts - the TS context 336363e21af5SBarry 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) 33641f06c33eSBarry Smith . time - current time 33650910c330SBarry Smith . u - current iterate 3366d763cef2SBarry Smith - mctx - [optional] monitoring context 3367d763cef2SBarry Smith 3368d763cef2SBarry Smith Notes: 3369d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 3370d763cef2SBarry Smith already has been loaded. 3371d763cef2SBarry Smith 337295452b02SPatrick Sanan Fortran Notes: 337395452b02SPatrick Sanan Only a single monitor function can be set for each TS object 3374025f1a04SBarry Smith 3375d763cef2SBarry Smith Level: intermediate 3376d763cef2SBarry Smith 3377a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 3378d763cef2SBarry Smith @*/ 3379c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 3380d763cef2SBarry Smith { 338178064530SBarry Smith PetscErrorCode ierr; 338278064530SBarry Smith PetscInt i; 338378064530SBarry Smith PetscBool identical; 338478064530SBarry Smith 3385d763cef2SBarry Smith PetscFunctionBegin; 33860700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 338778064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 338878064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr); 338978064530SBarry Smith if (identical) PetscFunctionReturn(0); 339078064530SBarry Smith } 339117186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 3392d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 33938704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 3394d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 3395d763cef2SBarry Smith PetscFunctionReturn(0); 3396d763cef2SBarry Smith } 3397d763cef2SBarry Smith 3398d763cef2SBarry Smith /*@C 3399a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 3400d763cef2SBarry Smith 34013f9fe445SBarry Smith Logically Collective on TS 3402d763cef2SBarry Smith 3403d763cef2SBarry Smith Input Parameters: 3404d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3405d763cef2SBarry Smith 3406d763cef2SBarry Smith Notes: 3407d763cef2SBarry Smith There is no way to remove a single, specific monitor. 3408d763cef2SBarry Smith 3409d763cef2SBarry Smith Level: intermediate 3410d763cef2SBarry Smith 3411a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 3412d763cef2SBarry Smith @*/ 34137087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 3414d763cef2SBarry Smith { 3415d952e501SBarry Smith PetscErrorCode ierr; 3416d952e501SBarry Smith PetscInt i; 3417d952e501SBarry Smith 3418d763cef2SBarry Smith PetscFunctionBegin; 34190700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3420d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 34218704b422SBarry Smith if (ts->monitordestroy[i]) { 34228704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 3423d952e501SBarry Smith } 3424d952e501SBarry Smith } 3425d763cef2SBarry Smith ts->numbermonitors = 0; 3426d763cef2SBarry Smith PetscFunctionReturn(0); 3427d763cef2SBarry Smith } 3428d763cef2SBarry Smith 3429721cd6eeSBarry Smith /*@C 3430721cd6eeSBarry Smith TSMonitorDefault - The Default monitor, prints the timestep and time for each step 34315516499fSSatish Balay 34325516499fSSatish Balay Level: intermediate 343341251cbbSSatish Balay 343463e21af5SBarry Smith .seealso: TSMonitorSet() 343541251cbbSSatish Balay @*/ 3436721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf) 3437d763cef2SBarry Smith { 3438dfbe8321SBarry Smith PetscErrorCode ierr; 3439721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 344041aca3d6SBarry Smith PetscBool iascii,ibinary; 3441d132466eSBarry Smith 3442d763cef2SBarry Smith PetscFunctionBegin; 34434d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 344441aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 344541aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr); 3446721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 344741aca3d6SBarry Smith if (iascii) { 3448649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 344963e21af5SBarry Smith if (step == -1){ /* this indicates it is an interpolated solution */ 345063e21af5SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr); 345163e21af5SBarry Smith } else { 34528392e04aSShri 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); 345363e21af5SBarry Smith } 3454649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 345541aca3d6SBarry Smith } else if (ibinary) { 345641aca3d6SBarry Smith PetscMPIInt rank; 345741aca3d6SBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 345841aca3d6SBarry Smith if (!rank) { 3459450a797fSBarry Smith PetscBool skipHeader; 3460450a797fSBarry Smith PetscInt classid = REAL_FILE_CLASSID; 3461450a797fSBarry Smith 3462450a797fSBarry Smith ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr); 3463450a797fSBarry Smith if (!skipHeader) { 3464450a797fSBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 3465450a797fSBarry Smith } 346641aca3d6SBarry Smith ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr); 346741aca3d6SBarry Smith } else { 346841aca3d6SBarry Smith ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr); 346941aca3d6SBarry Smith } 347041aca3d6SBarry Smith } 3471721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3472d763cef2SBarry Smith PetscFunctionReturn(0); 3473d763cef2SBarry Smith } 3474d763cef2SBarry Smith 3475cc9c3a59SBarry Smith /*@C 3476cc9c3a59SBarry Smith TSMonitorExtreme - Prints the extreme values of the solution at each timestep 3477cc9c3a59SBarry Smith 3478cc9c3a59SBarry Smith Level: intermediate 3479cc9c3a59SBarry Smith 3480cc9c3a59SBarry Smith .seealso: TSMonitorSet() 3481cc9c3a59SBarry Smith @*/ 3482cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf) 3483cc9c3a59SBarry Smith { 3484cc9c3a59SBarry Smith PetscErrorCode ierr; 3485cc9c3a59SBarry Smith PetscViewer viewer = vf->viewer; 3486cc9c3a59SBarry Smith PetscBool iascii; 3487cc9c3a59SBarry Smith PetscReal max,min; 3488cc9c3a59SBarry Smith 3489cc9c3a59SBarry Smith 3490cc9c3a59SBarry Smith PetscFunctionBegin; 3491cc9c3a59SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3492cc9c3a59SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 3493cc9c3a59SBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 3494cc9c3a59SBarry Smith if (iascii) { 3495cc9c3a59SBarry Smith ierr = VecMax(v,NULL,&max);CHKERRQ(ierr); 3496cc9c3a59SBarry Smith ierr = VecMin(v,NULL,&min);CHKERRQ(ierr); 3497cc9c3a59SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 34985132926bSBarry 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); 3499cc9c3a59SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3500cc9c3a59SBarry Smith } 3501cc9c3a59SBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3502cc9c3a59SBarry Smith PetscFunctionReturn(0); 3503cc9c3a59SBarry Smith } 3504cc9c3a59SBarry Smith 3505cd652676SJed Brown /*@ 3506cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3507cd652676SJed Brown 3508cd652676SJed Brown Collective on TS 3509cd652676SJed Brown 3510cd652676SJed Brown Input Argument: 3511cd652676SJed Brown + ts - time stepping context 3512cd652676SJed Brown - t - time to interpolate to 3513cd652676SJed Brown 3514cd652676SJed Brown Output Argument: 35150910c330SBarry Smith . U - state at given time 3516cd652676SJed Brown 3517cd652676SJed Brown Level: intermediate 3518cd652676SJed Brown 3519cd652676SJed Brown Developer Notes: 3520cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3521cd652676SJed Brown 3522874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve() 3523cd652676SJed Brown @*/ 35240910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3525cd652676SJed Brown { 3526cd652676SJed Brown PetscErrorCode ierr; 3527cd652676SJed Brown 3528cd652676SJed Brown PetscFunctionBegin; 3529cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3530b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3531be5899b3SLisandro 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); 3532ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 35330910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3534cd652676SJed Brown PetscFunctionReturn(0); 3535cd652676SJed Brown } 3536cd652676SJed Brown 3537d763cef2SBarry Smith /*@ 35386d9e5789SSean Farley TSStep - Steps one time step 3539d763cef2SBarry Smith 3540d763cef2SBarry Smith Collective on TS 3541d763cef2SBarry Smith 3542d763cef2SBarry Smith Input Parameter: 3543d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3544d763cef2SBarry Smith 354527829d71SBarry Smith Level: developer 3546d763cef2SBarry Smith 3547b8123daeSJed Brown Notes: 354827829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 354927829d71SBarry Smith 3550b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3551b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3552b8123daeSJed Brown 355319eac22cSLisandro Dalcin This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the 355419eac22cSLisandro Dalcin time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 355525cb2221SBarry Smith 35569be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3557d763cef2SBarry Smith @*/ 3558193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3559d763cef2SBarry Smith { 3560dfbe8321SBarry Smith PetscErrorCode ierr; 3561fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3562be5899b3SLisandro Dalcin PetscReal ptime; 3563d763cef2SBarry Smith 3564d763cef2SBarry Smith PetscFunctionBegin; 35650700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3566fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3567fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3568fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3569fffbeea8SBarry Smith " type = {Preprint},\n" 3570fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3571fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3572302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3573fffbeea8SBarry Smith 3574d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 357568bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3576d405a339SMatthew Knepley 3577ef85077eSLisandro 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>"); 3578a6772fa2SLisandro 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()"); 3579be5899b3SLisandro 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"); 3580a6772fa2SLisandro Dalcin 3581be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 3582be5899b3SLisandro Dalcin ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev; 35832808aa04SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3584e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3585d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3586193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3587d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3588be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 35892808aa04SLisandro Dalcin ts->steps++; 3590be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 359128d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3592362cd11cSLisandro Dalcin 3593362cd11cSLisandro Dalcin if (ts->reason < 0) { 3594cef5090cSJed Brown if (ts->errorifstepfailed) { 359508c7845fSBarry 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]); 359608c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3597d2daff3dSHong Zhang } 359808c7845fSBarry Smith } else if (!ts->reason) { 359908c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 360008c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 360108c7845fSBarry Smith } 360208c7845fSBarry Smith PetscFunctionReturn(0); 360308c7845fSBarry Smith } 360408c7845fSBarry Smith 360508c7845fSBarry Smith /*@ 36067cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 36077cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 36087cbde773SLisandro Dalcin 36097cbde773SLisandro Dalcin Collective on TS 36107cbde773SLisandro Dalcin 36117cbde773SLisandro Dalcin Input Arguments: 36127cbde773SLisandro Dalcin + ts - time stepping context 36137cbde773SLisandro Dalcin . wnormtype - norm type, either NORM_2 or NORM_INFINITY 36147cbde773SLisandro Dalcin - order - optional, desired order for the error evaluation or PETSC_DECIDE 36157cbde773SLisandro Dalcin 36167cbde773SLisandro Dalcin Output Arguments: 36177cbde773SLisandro Dalcin + order - optional, the actual order of the error evaluation 36187cbde773SLisandro Dalcin - wlte - the weighted local truncation error norm 36197cbde773SLisandro Dalcin 36207cbde773SLisandro Dalcin Level: advanced 36217cbde773SLisandro Dalcin 36227cbde773SLisandro Dalcin Notes: 36237cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 36247cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 36257cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 36267cbde773SLisandro Dalcin 36277cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm() 36287cbde773SLisandro Dalcin @*/ 36297cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte) 36307cbde773SLisandro Dalcin { 36317cbde773SLisandro Dalcin PetscErrorCode ierr; 36327cbde773SLisandro Dalcin 36337cbde773SLisandro Dalcin PetscFunctionBegin; 36347cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36357cbde773SLisandro Dalcin PetscValidType(ts,1); 36367cbde773SLisandro Dalcin PetscValidLogicalCollectiveEnum(ts,wnormtype,4); 36377cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order,3); 36387cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts,*order,3); 36397cbde773SLisandro Dalcin PetscValidRealPointer(wlte,4); 36407cbde773SLisandro Dalcin if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 36417cbde773SLisandro Dalcin if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name); 36427cbde773SLisandro Dalcin ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr); 36437cbde773SLisandro Dalcin PetscFunctionReturn(0); 36447cbde773SLisandro Dalcin } 36457cbde773SLisandro Dalcin 364605175c85SJed Brown /*@ 364705175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 364805175c85SJed Brown 36491c3436cfSJed Brown Collective on TS 365005175c85SJed Brown 365105175c85SJed Brown Input Arguments: 36521c3436cfSJed Brown + ts - time stepping context 36531c3436cfSJed Brown . order - desired order of accuracy 36540298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 365505175c85SJed Brown 365605175c85SJed Brown Output Arguments: 36570910c330SBarry Smith . U - state at the end of the current step 365805175c85SJed Brown 365905175c85SJed Brown Level: advanced 366005175c85SJed Brown 3661108c343cSJed Brown Notes: 3662108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3663108c343cSJed 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. 3664108c343cSJed Brown 36651c3436cfSJed Brown .seealso: TSStep(), TSAdapt 366605175c85SJed Brown @*/ 36670910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 366805175c85SJed Brown { 366905175c85SJed Brown PetscErrorCode ierr; 367005175c85SJed Brown 367105175c85SJed Brown PetscFunctionBegin; 367205175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 367305175c85SJed Brown PetscValidType(ts,1); 36740910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3675ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 36760910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 367705175c85SJed Brown PetscFunctionReturn(0); 367805175c85SJed Brown } 367905175c85SJed Brown 3680*aad739acSMatthew G. Knepley /*@C 3681*aad739acSMatthew G. Knepley TSGetComputeInitialGuess - Get the function used to automatically compute an initial guess for the timestepping. 3682*aad739acSMatthew G. Knepley 3683*aad739acSMatthew G. Knepley Not collective 3684*aad739acSMatthew G. Knepley 3685*aad739acSMatthew G. Knepley Input Argument: 3686*aad739acSMatthew G. Knepley . ts - time stepping context 3687*aad739acSMatthew G. Knepley 3688*aad739acSMatthew G. Knepley Output Argument: 3689*aad739acSMatthew G. Knepley . initGuess - The function which computes an initial guess 3690*aad739acSMatthew G. Knepley 3691*aad739acSMatthew G. Knepley Level: advanced 3692*aad739acSMatthew G. Knepley 3693*aad739acSMatthew G. Knepley Notes: 3694*aad739acSMatthew G. Knepley The calling sequence for the function is 3695*aad739acSMatthew G. Knepley $ initGuess(TS ts, Vec u) 3696*aad739acSMatthew G. Knepley $ ts - The timestepping context 3697*aad739acSMatthew G. Knepley $ u - The input vector in which the initial guess is stored 3698*aad739acSMatthew G. Knepley 3699*aad739acSMatthew G. Knepley .seealso: TSSetComputeInitialGuess(), TSComputeInitialGuess() 3700*aad739acSMatthew G. Knepley @*/ 3701*aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeInitialGuess(TS ts, PetscErrorCode (**initGuess)(TS, Vec)) 3702*aad739acSMatthew G. Knepley { 3703*aad739acSMatthew G. Knepley PetscFunctionBegin; 3704*aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3705*aad739acSMatthew G. Knepley PetscValidPointer(initGuess, 2); 3706*aad739acSMatthew G. Knepley *initGuess = ts->ops->initguess; 3707*aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3708*aad739acSMatthew G. Knepley } 3709*aad739acSMatthew G. Knepley 3710*aad739acSMatthew G. Knepley /*@C 3711*aad739acSMatthew G. Knepley TSSetComputeInitialGuess - Set the function used to automatically compute an initial guess for the timestepping. 3712*aad739acSMatthew G. Knepley 3713*aad739acSMatthew G. Knepley Logically collective on ts 3714*aad739acSMatthew G. Knepley 3715*aad739acSMatthew G. Knepley Input Arguments: 3716*aad739acSMatthew G. Knepley + ts - time stepping context 3717*aad739acSMatthew G. Knepley - initGuess - The function which computes an initial guess 3718*aad739acSMatthew G. Knepley 3719*aad739acSMatthew G. Knepley Level: advanced 3720*aad739acSMatthew G. Knepley 3721*aad739acSMatthew G. Knepley Notes: 3722*aad739acSMatthew G. Knepley The calling sequence for the function is 3723*aad739acSMatthew G. Knepley $ initGuess(TS ts, Vec u) 3724*aad739acSMatthew G. Knepley $ ts - The timestepping context 3725*aad739acSMatthew G. Knepley $ u - The input vector in which the initial guess is stored 3726*aad739acSMatthew G. Knepley 3727*aad739acSMatthew G. Knepley .seealso: TSGetComputeInitialGuess(), TSComputeInitialGuess() 3728*aad739acSMatthew G. Knepley @*/ 3729*aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeInitialGuess(TS ts, PetscErrorCode (*initGuess)(TS, Vec)) 3730*aad739acSMatthew G. Knepley { 3731*aad739acSMatthew G. Knepley PetscFunctionBegin; 3732*aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3733*aad739acSMatthew G. Knepley PetscValidPointer(initGuess, 2); 3734*aad739acSMatthew G. Knepley ts->ops->initguess = initGuess; 3735*aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3736*aad739acSMatthew G. Knepley } 3737*aad739acSMatthew G. Knepley 3738*aad739acSMatthew G. Knepley /*@ 3739*aad739acSMatthew G. Knepley TSComputeInitialGuess - Compute an initial guess for the timestepping using the function previously set. 3740*aad739acSMatthew G. Knepley 3741*aad739acSMatthew G. Knepley Collective on ts 3742*aad739acSMatthew G. Knepley 3743*aad739acSMatthew G. Knepley Input Arguments: 3744*aad739acSMatthew G. Knepley + ts - time stepping context 3745*aad739acSMatthew G. Knepley - u - The Vec to store the guess in which will be used in TSSolve() 3746*aad739acSMatthew G. Knepley 3747*aad739acSMatthew G. Knepley Level: advanced 3748*aad739acSMatthew G. Knepley 3749*aad739acSMatthew G. Knepley Notes: 3750*aad739acSMatthew G. Knepley The calling sequence for the function is 3751*aad739acSMatthew G. Knepley $ initGuess(TS ts, Vec u) 3752*aad739acSMatthew G. Knepley $ ts - The timestepping context 3753*aad739acSMatthew G. Knepley $ u - The input vector in which the initial guess is stored 3754*aad739acSMatthew G. Knepley 3755*aad739acSMatthew G. Knepley .seealso: TSGetComputeInitialGuess(), TSSetComputeInitialGuess(), TSSolve() 3756*aad739acSMatthew G. Knepley @*/ 3757*aad739acSMatthew G. Knepley PetscErrorCode TSComputeInitialGuess(TS ts, Vec u) 3758*aad739acSMatthew G. Knepley { 3759*aad739acSMatthew G. Knepley PetscErrorCode ierr; 3760*aad739acSMatthew G. Knepley 3761*aad739acSMatthew G. Knepley PetscFunctionBegin; 3762*aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3763*aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3764*aad739acSMatthew G. Knepley if (ts->ops->initguess) {ierr = (*ts->ops->initguess)(ts, u);CHKERRQ(ierr);} 3765*aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3766*aad739acSMatthew G. Knepley } 3767*aad739acSMatthew G. Knepley 3768*aad739acSMatthew G. Knepley /*@C 3769*aad739acSMatthew G. Knepley TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping. 3770*aad739acSMatthew G. Knepley 3771*aad739acSMatthew G. Knepley Not collective 3772*aad739acSMatthew G. Knepley 3773*aad739acSMatthew G. Knepley Input Argument: 3774*aad739acSMatthew G. Knepley . ts - time stepping context 3775*aad739acSMatthew G. Knepley 3776*aad739acSMatthew G. Knepley Output Argument: 3777*aad739acSMatthew G. Knepley . exactError - The function which computes the solution error 3778*aad739acSMatthew G. Knepley 3779*aad739acSMatthew G. Knepley Level: advanced 3780*aad739acSMatthew G. Knepley 3781*aad739acSMatthew G. Knepley Notes: 3782*aad739acSMatthew G. Knepley The calling sequence for the function is 3783*aad739acSMatthew G. Knepley $ exactError(TS ts, Vec u) 3784*aad739acSMatthew G. Knepley $ ts - The timestepping context 3785*aad739acSMatthew G. Knepley $ u - The approximate solution vector 3786*aad739acSMatthew G. Knepley $ e - The input vector in which the error is stored 3787*aad739acSMatthew G. Knepley 3788*aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError() 3789*aad739acSMatthew G. Knepley @*/ 3790*aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec)) 3791*aad739acSMatthew G. Knepley { 3792*aad739acSMatthew G. Knepley PetscFunctionBegin; 3793*aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3794*aad739acSMatthew G. Knepley PetscValidPointer(exactError, 2); 3795*aad739acSMatthew G. Knepley *exactError = ts->ops->exacterror; 3796*aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3797*aad739acSMatthew G. Knepley } 3798*aad739acSMatthew G. Knepley 3799*aad739acSMatthew G. Knepley /*@C 3800*aad739acSMatthew G. Knepley TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping. 3801*aad739acSMatthew G. Knepley 3802*aad739acSMatthew G. Knepley Logically collective on ts 3803*aad739acSMatthew G. Knepley 3804*aad739acSMatthew G. Knepley Input Arguments: 3805*aad739acSMatthew G. Knepley + ts - time stepping context 3806*aad739acSMatthew G. Knepley - exactError - The function which computes the solution error 3807*aad739acSMatthew G. Knepley 3808*aad739acSMatthew G. Knepley Level: advanced 3809*aad739acSMatthew G. Knepley 3810*aad739acSMatthew G. Knepley Notes: 3811*aad739acSMatthew G. Knepley The calling sequence for the function is 3812*aad739acSMatthew G. Knepley $ exactError(TS ts, Vec u) 3813*aad739acSMatthew G. Knepley $ ts - The timestepping context 3814*aad739acSMatthew G. Knepley $ u - The approximate solution vector 3815*aad739acSMatthew G. Knepley $ e - The input vector in which the error is stored 3816*aad739acSMatthew G. Knepley 3817*aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError() 3818*aad739acSMatthew G. Knepley @*/ 3819*aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec)) 3820*aad739acSMatthew G. Knepley { 3821*aad739acSMatthew G. Knepley PetscFunctionBegin; 3822*aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3823*aad739acSMatthew G. Knepley PetscValidPointer(exactError, 2); 3824*aad739acSMatthew G. Knepley ts->ops->exacterror = exactError; 3825*aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3826*aad739acSMatthew G. Knepley } 3827*aad739acSMatthew G. Knepley 3828*aad739acSMatthew G. Knepley /*@ 3829*aad739acSMatthew G. Knepley TSComputeExactError - Compute the solution error for the timestepping using the function previously set. 3830*aad739acSMatthew G. Knepley 3831*aad739acSMatthew G. Knepley Collective on ts 3832*aad739acSMatthew G. Knepley 3833*aad739acSMatthew G. Knepley Input Arguments: 3834*aad739acSMatthew G. Knepley + ts - time stepping context 3835*aad739acSMatthew G. Knepley . u - The approximate solution 3836*aad739acSMatthew G. Knepley - e - The Vec used to store the error 3837*aad739acSMatthew G. Knepley 3838*aad739acSMatthew G. Knepley Level: advanced 3839*aad739acSMatthew G. Knepley 3840*aad739acSMatthew G. Knepley Notes: 3841*aad739acSMatthew G. Knepley The calling sequence for the function is 3842*aad739acSMatthew G. Knepley $ exactError(TS ts, Vec u) 3843*aad739acSMatthew G. Knepley $ ts - The timestepping context 3844*aad739acSMatthew G. Knepley $ u - The approximate solution vector 3845*aad739acSMatthew G. Knepley $ e - The input vector in which the error is stored 3846*aad739acSMatthew G. Knepley 3847*aad739acSMatthew G. Knepley .seealso: TSGetComputeInitialGuess(), TSSetComputeInitialGuess(), TSSolve() 3848*aad739acSMatthew G. Knepley @*/ 3849*aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e) 3850*aad739acSMatthew G. Knepley { 3851*aad739acSMatthew G. Knepley PetscErrorCode ierr; 3852*aad739acSMatthew G. Knepley 3853*aad739acSMatthew G. Knepley PetscFunctionBegin; 3854*aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3855*aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3856*aad739acSMatthew G. Knepley PetscValidHeaderSpecific(e, VEC_CLASSID, 3); 3857*aad739acSMatthew G. Knepley if (ts->ops->exacterror) {ierr = (*ts->ops->exacterror)(ts, u, e);CHKERRQ(ierr);} 3858*aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3859*aad739acSMatthew G. Knepley } 3860*aad739acSMatthew G. Knepley 3861b1cb36f3SHong Zhang /*@ 38626a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 38636a4d4014SLisandro Dalcin 38646a4d4014SLisandro Dalcin Collective on TS 38656a4d4014SLisandro Dalcin 38666a4d4014SLisandro Dalcin Input Parameter: 38676a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 386863e21af5SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used, 386963e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 38705a3a76d0SJed Brown 38716a4d4014SLisandro Dalcin Level: beginner 38726a4d4014SLisandro Dalcin 38735a3a76d0SJed Brown Notes: 38745a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 38755a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 38765a3a76d0SJed Brown stepped over the final time. 38775a3a76d0SJed Brown 387863e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime() 38796a4d4014SLisandro Dalcin @*/ 3880cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 38816a4d4014SLisandro Dalcin { 3882b06615a5SLisandro Dalcin Vec solution; 38836a4d4014SLisandro Dalcin PetscErrorCode ierr; 3884f22f69f0SBarry Smith 38856a4d4014SLisandro Dalcin PetscFunctionBegin; 38860700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3887f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 3888feed9e9dSBarry Smith 3889ee41a567SStefano 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 */ 38900910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 3891b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3892b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3893b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 38945a3a76d0SJed Brown } 38950910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3896715f1b00SHong Zhang if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE"); 3897bbd56ea5SKarl Rupp } else if (u) { 38980910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 38995a3a76d0SJed Brown } 3900b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 390168bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3902a6772fa2SLisandro Dalcin 3903ef85077eSLisandro 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>"); 3904a6772fa2SLisandro 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()"); 3905a6772fa2SLisandro 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"); 3906a6772fa2SLisandro Dalcin 3907715f1b00SHong Zhang if (ts->forward_solve) { 3908715f1b00SHong Zhang ierr = TSForwardSetUp(ts);CHKERRQ(ierr); 3909715f1b00SHong Zhang } 3910715f1b00SHong Zhang 3911e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 3912715f1b00SHong Zhang restarts the step after an event. Resetting these counters in such case causes 3913e7069c78SShri TSTrajectory to incorrectly save the output files 3914e7069c78SShri */ 3915715f1b00SHong Zhang /* reset time step and iteration counters */ 39162808aa04SLisandro Dalcin if (!ts->steps) { 39175ef26d82SJed Brown ts->ksp_its = 0; 39185ef26d82SJed Brown ts->snes_its = 0; 3919c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3920c610991cSLisandro Dalcin ts->reject = 0; 39212808aa04SLisandro Dalcin ts->steprestart = PETSC_TRUE; 39222808aa04SLisandro Dalcin ts->steprollback = PETSC_FALSE; 39232808aa04SLisandro Dalcin } 39241a3b9e0cSLisandro Dalcin if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && ts->ptime < ts->max_time && ts->ptime + ts->time_step > ts->max_time) ts->time_step = ts->max_time - ts->ptime; 3925193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3926193ac0bcSJed Brown 3927ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3928f05ece33SBarry Smith 3929193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3930193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 3931a6772fa2SLisandro Dalcin if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3932cc708dedSBarry Smith ts->solvetime = ts->ptime; 3933a6772fa2SLisandro Dalcin solution = ts->vec_sol; 3934be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 3935db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3936db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3937e7069c78SShri 39382808aa04SLisandro Dalcin if (!ts->steps) { 39392808aa04SLisandro Dalcin ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39406427ac75SLisandro Dalcin ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39412808aa04SLisandro Dalcin } 39426427ac75SLisandro Dalcin 3943e1a7a14fSJed Brown while (!ts->reason) { 39442808aa04SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39459687d888SLisandro Dalcin if (!ts->steprollback) { 39469687d888SLisandro Dalcin ierr = TSPreStep(ts);CHKERRQ(ierr); 39479687d888SLisandro Dalcin } 3948193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 3949f3b1f45cSBarry Smith if (ts->testjacobian) { 3950f3b1f45cSBarry Smith ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr); 3951f3b1f45cSBarry Smith } 3952f3b1f45cSBarry Smith if (ts->testjacobiantranspose) { 3953f3b1f45cSBarry Smith ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr); 3954f3b1f45cSBarry Smith } 3955cd4cee2dSHong Zhang if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */ 3956b1cb36f3SHong Zhang ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr); 3957b1cb36f3SHong Zhang } 395858818c2dSLisandro Dalcin if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */ 3959715f1b00SHong Zhang ierr = TSForwardStep(ts);CHKERRQ(ierr); 3960715f1b00SHong Zhang } 396110b82f12SShri Abhyankar ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 3962e783b05fSHong 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. */ 396358818c2dSLisandro Dalcin if (ts->steprollback) { 396458818c2dSLisandro Dalcin ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 396558818c2dSLisandro Dalcin } 3966e783b05fSHong Zhang if (!ts->steprollback) { 39672808aa04SLisandro Dalcin ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39681eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 3969aeb4809dSShri Abhyankar } 3970193ac0bcSJed Brown } 39712808aa04SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39726427ac75SLisandro Dalcin 397349354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 39740910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 3975cc708dedSBarry Smith ts->solvetime = ts->max_time; 3976b06615a5SLisandro Dalcin solution = u; 397763e21af5SBarry Smith ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr); 39780574a7fbSJed Brown } else { 3979ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3980cc708dedSBarry Smith ts->solvetime = ts->ptime; 3981b06615a5SLisandro Dalcin solution = ts->vec_sol; 39820574a7fbSJed Brown } 3983193ac0bcSJed Brown } 3984d2daff3dSHong Zhang 3985ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 398663e21af5SBarry Smith ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr); 398756f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 3988ef222394SBarry Smith if (ts->adjoint_solve) { 3989ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 39902b0a91c0SBarry Smith } 39916a4d4014SLisandro Dalcin PetscFunctionReturn(0); 39926a4d4014SLisandro Dalcin } 39936a4d4014SLisandro Dalcin 39947db568b7SBarry Smith /*@C 3995228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 3996228d79bcSJed Brown 3997228d79bcSJed Brown Collective on TS 3998228d79bcSJed Brown 3999228d79bcSJed Brown Input Parameters: 4000228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 4001228d79bcSJed Brown . step - step number that has just completed 4002228d79bcSJed Brown . ptime - model time of the state 40030910c330SBarry Smith - u - state at the current model time 4004228d79bcSJed Brown 4005228d79bcSJed Brown Notes: 40067db568b7SBarry Smith TSMonitor() is typically used automatically within the time stepping implementations. 40077db568b7SBarry Smith Users would almost never call this routine directly. 4008228d79bcSJed Brown 400963e21af5SBarry Smith A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions 401063e21af5SBarry Smith 40117db568b7SBarry Smith Level: developer 4012228d79bcSJed Brown 4013228d79bcSJed Brown @*/ 40140910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 4015d763cef2SBarry Smith { 4016d6152f81SLisandro Dalcin DM dm; 4017a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 4018d6152f81SLisandro Dalcin PetscErrorCode ierr; 4019d763cef2SBarry Smith 4020d763cef2SBarry Smith PetscFunctionBegin; 4021b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4022b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4023d6152f81SLisandro Dalcin 4024d6152f81SLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 4025d6152f81SLisandro Dalcin ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr); 4026d6152f81SLisandro Dalcin 40278860a134SJunchao Zhang ierr = VecLockReadPush(u);CHKERRQ(ierr); 4028d763cef2SBarry Smith for (i=0; i<n; i++) { 40290910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 4030d763cef2SBarry Smith } 40318860a134SJunchao Zhang ierr = VecLockReadPop(u);CHKERRQ(ierr); 4032d763cef2SBarry Smith PetscFunctionReturn(0); 4033d763cef2SBarry Smith } 4034d763cef2SBarry Smith 4035d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 4036d763cef2SBarry Smith /*@C 40377db568b7SBarry Smith TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 4038a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 4039d763cef2SBarry Smith 4040d763cef2SBarry Smith Collective on TS 4041d763cef2SBarry Smith 4042d763cef2SBarry Smith Input Parameters: 4043d763cef2SBarry Smith + host - the X display to open, or null for the local machine 4044d763cef2SBarry Smith . label - the title to put in the title bar 40457c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 4046a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 4047a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 4048d763cef2SBarry Smith 4049d763cef2SBarry Smith Output Parameter: 40500b039ecaSBarry Smith . ctx - the context 4051d763cef2SBarry Smith 4052d763cef2SBarry Smith Options Database Key: 40534f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 40548b668821SLisandro Dalcin + -ts_monitor_lg_timestep_log - automatically sets line graph monitor 40557db568b7SBarry Smith . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 40567db568b7SBarry Smith . -ts_monitor_lg_error - monitor the error 40577db568b7SBarry Smith . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 40587db568b7SBarry Smith . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 4059b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 4060d763cef2SBarry Smith 4061d763cef2SBarry Smith Notes: 4062a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 4063d763cef2SBarry Smith 40647db568b7SBarry Smith One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 40657db568b7SBarry Smith 40667db568b7SBarry 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 40677db568b7SBarry 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 40687db568b7SBarry Smith as the first argument. 40697db568b7SBarry Smith 40707db568b7SBarry Smith One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 40717db568b7SBarry Smith 4072d763cef2SBarry Smith Level: intermediate 4073d763cef2SBarry Smith 40747db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 40757db568b7SBarry Smith TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 40767db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 40777db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 40787db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 40797c922b88SBarry Smith 4080d763cef2SBarry Smith @*/ 4081a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 4082d763cef2SBarry Smith { 40837f52daa2SLisandro Dalcin PetscDraw draw; 4084dfbe8321SBarry Smith PetscErrorCode ierr; 4085d763cef2SBarry Smith 4086d763cef2SBarry Smith PetscFunctionBegin; 4087b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 40887f52daa2SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 40897f52daa2SLisandro Dalcin ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 40907f52daa2SLisandro Dalcin ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 4091287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 40927f52daa2SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 4093a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 4094d763cef2SBarry Smith PetscFunctionReturn(0); 4095d763cef2SBarry Smith } 4096d763cef2SBarry Smith 4097b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 4098d763cef2SBarry Smith { 40990b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4100c32365f1SBarry Smith PetscReal x = ptime,y; 4101dfbe8321SBarry Smith PetscErrorCode ierr; 4102d763cef2SBarry Smith 4103d763cef2SBarry Smith PetscFunctionBegin; 410463e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */ 4105b06615a5SLisandro Dalcin if (!step) { 4106a9f9c1f6SBarry Smith PetscDrawAxis axis; 41078b668821SLisandro Dalcin const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step"; 4108a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 41098b668821SLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr); 4110a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4111a9f9c1f6SBarry Smith } 4112c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 41138b668821SLisandro Dalcin if (ctx->semilogy) y = PetscLog10Real(y); 41140b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4115b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 41160b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 41176934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 41183923b477SBarry Smith } 4119d763cef2SBarry Smith PetscFunctionReturn(0); 4120d763cef2SBarry Smith } 4121d763cef2SBarry Smith 4122d763cef2SBarry Smith /*@C 4123a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 4124a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 4125d763cef2SBarry Smith 41260b039ecaSBarry Smith Collective on TSMonitorLGCtx 4127d763cef2SBarry Smith 4128d763cef2SBarry Smith Input Parameter: 41290b039ecaSBarry Smith . ctx - the monitor context 4130d763cef2SBarry Smith 4131d763cef2SBarry Smith Level: intermediate 4132d763cef2SBarry Smith 41334f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 4134d763cef2SBarry Smith @*/ 4135a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 4136d763cef2SBarry Smith { 4137dfbe8321SBarry Smith PetscErrorCode ierr; 4138d763cef2SBarry Smith 4139d763cef2SBarry Smith PetscFunctionBegin; 41407684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 41417684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 41427684fa3eSBarry Smith } 41430b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 414431152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 4145387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 4146387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 4147387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 41480b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 4149d763cef2SBarry Smith PetscFunctionReturn(0); 4150d763cef2SBarry Smith } 4151d763cef2SBarry Smith 41521b575b74SJoseph Pusztay /* 41531b575b74SJoseph Pusztay 41541b575b74SJoseph Pusztay Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations 41551b575b74SJoseph Pusztay 41561b575b74SJoseph Pusztay */ 41571b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx) 41581b575b74SJoseph Pusztay { 41591b575b74SJoseph Pusztay PetscDraw draw; 41601b575b74SJoseph Pusztay PetscErrorCode ierr; 41611b575b74SJoseph Pusztay 41621b575b74SJoseph Pusztay PetscFunctionBegin; 41631b575b74SJoseph Pusztay ierr = PetscNew(ctx);CHKERRQ(ierr); 41641b575b74SJoseph Pusztay ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 41651b575b74SJoseph Pusztay ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 41661b575b74SJoseph Pusztay ierr = PetscDrawSPCreate(draw,1,&(*ctx)->sp);CHKERRQ(ierr); 41671b575b74SJoseph Pusztay ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 41681b575b74SJoseph Pusztay (*ctx)->howoften = howoften; 41691b575b74SJoseph Pusztay PetscFunctionReturn(0); 41701b575b74SJoseph Pusztay 41711b575b74SJoseph Pusztay } 41721b575b74SJoseph Pusztay 41731b575b74SJoseph Pusztay /* 41741b575b74SJoseph Pusztay Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate 41751b575b74SJoseph Pusztay */ 41761b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx) 41771b575b74SJoseph Pusztay { 41781b575b74SJoseph Pusztay PetscErrorCode ierr; 41791b575b74SJoseph Pusztay 41801b575b74SJoseph Pusztay PetscFunctionBegin; 41811b575b74SJoseph Pusztay 41821b575b74SJoseph Pusztay ierr = PetscDrawSPDestroy(&(*ctx)->sp);CHKERRQ(ierr); 41831b575b74SJoseph Pusztay ierr = PetscFree(*ctx);CHKERRQ(ierr); 41841b575b74SJoseph Pusztay 41851b575b74SJoseph Pusztay PetscFunctionReturn(0); 41861b575b74SJoseph Pusztay 41871b575b74SJoseph Pusztay } 41881b575b74SJoseph Pusztay 4189d763cef2SBarry Smith /*@ 4190b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4191d763cef2SBarry Smith 4192d763cef2SBarry Smith Not Collective 4193d763cef2SBarry Smith 4194d763cef2SBarry Smith Input Parameter: 4195d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 4196d763cef2SBarry Smith 4197d763cef2SBarry Smith Output Parameter: 419819eac22cSLisandro Dalcin . t - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime(). 4199d763cef2SBarry Smith 4200d763cef2SBarry Smith Level: beginner 4201d763cef2SBarry Smith 4202b8123daeSJed Brown Note: 4203b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 42049be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 4205b8123daeSJed Brown 42068f199f4dSPatrick Sanan .seealso: TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber() 4207d763cef2SBarry Smith 4208d763cef2SBarry Smith @*/ 42097087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 4210d763cef2SBarry Smith { 4211d763cef2SBarry Smith PetscFunctionBegin; 42120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4213f7cf8827SBarry Smith PetscValidRealPointer(t,2); 4214d763cef2SBarry Smith *t = ts->ptime; 4215d763cef2SBarry Smith PetscFunctionReturn(0); 4216d763cef2SBarry Smith } 4217d763cef2SBarry Smith 4218e5e524a1SHong Zhang /*@ 4219e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4220e5e524a1SHong Zhang 4221e5e524a1SHong Zhang Not Collective 4222e5e524a1SHong Zhang 4223e5e524a1SHong Zhang Input Parameter: 4224e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 4225e5e524a1SHong Zhang 4226e5e524a1SHong Zhang Output Parameter: 4227e5e524a1SHong Zhang . t - the previous time 4228e5e524a1SHong Zhang 4229e5e524a1SHong Zhang Level: beginner 4230e5e524a1SHong Zhang 4231aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep() 4232e5e524a1SHong Zhang 4233e5e524a1SHong Zhang @*/ 4234e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 4235e5e524a1SHong Zhang { 4236e5e524a1SHong Zhang PetscFunctionBegin; 4237e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4238e5e524a1SHong Zhang PetscValidRealPointer(t,2); 4239e5e524a1SHong Zhang *t = ts->ptime_prev; 4240e5e524a1SHong Zhang PetscFunctionReturn(0); 4241e5e524a1SHong Zhang } 4242e5e524a1SHong Zhang 42436a4d4014SLisandro Dalcin /*@ 42446a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 42456a4d4014SLisandro Dalcin 42463f9fe445SBarry Smith Logically Collective on TS 42476a4d4014SLisandro Dalcin 42486a4d4014SLisandro Dalcin Input Parameters: 42496a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 42506a4d4014SLisandro Dalcin - time - the time 42516a4d4014SLisandro Dalcin 42526a4d4014SLisandro Dalcin Level: intermediate 42536a4d4014SLisandro Dalcin 425419eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps() 42556a4d4014SLisandro Dalcin 42566a4d4014SLisandro Dalcin @*/ 42577087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 42586a4d4014SLisandro Dalcin { 42596a4d4014SLisandro Dalcin PetscFunctionBegin; 42600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4261c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 42626a4d4014SLisandro Dalcin ts->ptime = t; 42636a4d4014SLisandro Dalcin PetscFunctionReturn(0); 42646a4d4014SLisandro Dalcin } 42656a4d4014SLisandro Dalcin 4266d763cef2SBarry Smith /*@C 4267d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4268d763cef2SBarry Smith TS options in the database. 4269d763cef2SBarry Smith 42703f9fe445SBarry Smith Logically Collective on TS 4271d763cef2SBarry Smith 4272d763cef2SBarry Smith Input Parameter: 4273d763cef2SBarry Smith + ts - The TS context 4274d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4275d763cef2SBarry Smith 4276d763cef2SBarry Smith Notes: 4277d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4278d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4279d763cef2SBarry Smith hyphen. 4280d763cef2SBarry Smith 4281d763cef2SBarry Smith Level: advanced 4282d763cef2SBarry Smith 4283d763cef2SBarry Smith .seealso: TSSetFromOptions() 4284d763cef2SBarry Smith 4285d763cef2SBarry Smith @*/ 42867087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 4287d763cef2SBarry Smith { 4288dfbe8321SBarry Smith PetscErrorCode ierr; 4289089b2837SJed Brown SNES snes; 4290d763cef2SBarry Smith 4291d763cef2SBarry Smith PetscFunctionBegin; 42920700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4293d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4294089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4295089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4296d763cef2SBarry Smith PetscFunctionReturn(0); 4297d763cef2SBarry Smith } 4298d763cef2SBarry Smith 4299d763cef2SBarry Smith /*@C 4300d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4301d763cef2SBarry Smith TS options in the database. 4302d763cef2SBarry Smith 43033f9fe445SBarry Smith Logically Collective on TS 4304d763cef2SBarry Smith 4305d763cef2SBarry Smith Input Parameter: 4306d763cef2SBarry Smith + ts - The TS context 4307d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4308d763cef2SBarry Smith 4309d763cef2SBarry Smith Notes: 4310d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4311d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4312d763cef2SBarry Smith hyphen. 4313d763cef2SBarry Smith 4314d763cef2SBarry Smith Level: advanced 4315d763cef2SBarry Smith 4316d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 4317d763cef2SBarry Smith 4318d763cef2SBarry Smith @*/ 43197087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4320d763cef2SBarry Smith { 4321dfbe8321SBarry Smith PetscErrorCode ierr; 4322089b2837SJed Brown SNES snes; 4323d763cef2SBarry Smith 4324d763cef2SBarry Smith PetscFunctionBegin; 43250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4326d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4327089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4328089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4329d763cef2SBarry Smith PetscFunctionReturn(0); 4330d763cef2SBarry Smith } 4331d763cef2SBarry Smith 4332d763cef2SBarry Smith /*@C 4333d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4334d763cef2SBarry Smith TS options in the database. 4335d763cef2SBarry Smith 4336d763cef2SBarry Smith Not Collective 4337d763cef2SBarry Smith 4338d763cef2SBarry Smith Input Parameter: 4339d763cef2SBarry Smith . ts - The TS context 4340d763cef2SBarry Smith 4341d763cef2SBarry Smith Output Parameter: 4342d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4343d763cef2SBarry Smith 434495452b02SPatrick Sanan Notes: 434595452b02SPatrick Sanan On the fortran side, the user should pass in a string 'prifix' of 4346d763cef2SBarry Smith sufficient length to hold the prefix. 4347d763cef2SBarry Smith 4348d763cef2SBarry Smith Level: intermediate 4349d763cef2SBarry Smith 4350d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 4351d763cef2SBarry Smith @*/ 43527087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4353d763cef2SBarry Smith { 4354dfbe8321SBarry Smith PetscErrorCode ierr; 4355d763cef2SBarry Smith 4356d763cef2SBarry Smith PetscFunctionBegin; 43570700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 43584482741eSBarry Smith PetscValidPointer(prefix,2); 4359d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4360d763cef2SBarry Smith PetscFunctionReturn(0); 4361d763cef2SBarry Smith } 4362d763cef2SBarry Smith 4363d763cef2SBarry Smith /*@C 4364d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4365d763cef2SBarry Smith 4366d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4367d763cef2SBarry Smith 4368d763cef2SBarry Smith Input Parameter: 4369d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4370d763cef2SBarry Smith 4371d763cef2SBarry Smith Output Parameters: 4372e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4373e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4374e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4375e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4376d763cef2SBarry Smith 437795452b02SPatrick Sanan Notes: 437895452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 4379d763cef2SBarry Smith 4380d763cef2SBarry Smith Level: intermediate 4381d763cef2SBarry Smith 438280275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 4383d763cef2SBarry Smith 4384d763cef2SBarry Smith @*/ 4385e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4386d763cef2SBarry Smith { 4387089b2837SJed Brown PetscErrorCode ierr; 438824989b8cSPeter Brune DM dm; 4389089b2837SJed Brown 4390d763cef2SBarry Smith PetscFunctionBegin; 439123a57915SBarry Smith if (Amat || Pmat) { 439223a57915SBarry Smith SNES snes; 4393089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 439423a57915SBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4395e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 439623a57915SBarry Smith } 439724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 439824989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 4399d763cef2SBarry Smith PetscFunctionReturn(0); 4400d763cef2SBarry Smith } 4401d763cef2SBarry Smith 44022eca1d9cSJed Brown /*@C 44032eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 44042eca1d9cSJed Brown 44052eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 44062eca1d9cSJed Brown 44072eca1d9cSJed Brown Input Parameter: 44082eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 44092eca1d9cSJed Brown 44102eca1d9cSJed Brown Output Parameters: 4411e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4412e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 44132eca1d9cSJed Brown . f - The function to compute the matrices 44142eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 44152eca1d9cSJed Brown 441695452b02SPatrick Sanan Notes: 441795452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 44182eca1d9cSJed Brown 44192eca1d9cSJed Brown Level: advanced 44202eca1d9cSJed Brown 442180275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 44222eca1d9cSJed Brown 44232eca1d9cSJed Brown @*/ 4424e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 44252eca1d9cSJed Brown { 4426089b2837SJed Brown PetscErrorCode ierr; 442724989b8cSPeter Brune DM dm; 44280910c330SBarry Smith 44292eca1d9cSJed Brown PetscFunctionBegin; 4430c0aab802Sstefano_zampini if (Amat || Pmat) { 4431c0aab802Sstefano_zampini SNES snes; 4432089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4433f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4434e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 4435c0aab802Sstefano_zampini } 443624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 443724989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 44382eca1d9cSJed Brown PetscFunctionReturn(0); 44392eca1d9cSJed Brown } 44402eca1d9cSJed Brown 44411713a123SBarry Smith /*@C 444283a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 44431713a123SBarry Smith VecView() for the solution at each timestep 44441713a123SBarry Smith 44451713a123SBarry Smith Collective on TS 44461713a123SBarry Smith 44471713a123SBarry Smith Input Parameters: 44481713a123SBarry Smith + ts - the TS context 44491713a123SBarry Smith . step - current time-step 4450142b95e3SSatish Balay . ptime - current time 44510298fd71SBarry Smith - dummy - either a viewer or NULL 44521713a123SBarry Smith 445399fdda47SBarry Smith Options Database: 445499fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 445599fdda47SBarry Smith 445695452b02SPatrick Sanan Notes: 445795452b02SPatrick Sanan the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 445899fdda47SBarry Smith will look bad 445999fdda47SBarry Smith 44601713a123SBarry Smith Level: intermediate 44611713a123SBarry Smith 4462a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 44631713a123SBarry Smith @*/ 44640910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 44651713a123SBarry Smith { 4466dfbe8321SBarry Smith PetscErrorCode ierr; 446783a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4468473a3ab2SBarry Smith PetscDraw draw; 44691713a123SBarry Smith 44701713a123SBarry Smith PetscFunctionBegin; 44716083293cSBarry Smith if (!step && ictx->showinitial) { 44726083293cSBarry Smith if (!ictx->initialsolution) { 44730910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 44741713a123SBarry Smith } 44750910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 44766083293cSBarry Smith } 4477b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 44780dcf80beSBarry Smith 44796083293cSBarry Smith if (ictx->showinitial) { 44806083293cSBarry Smith PetscReal pause; 44816083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 44826083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 44836083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 44846083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 44856083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 44866083293cSBarry Smith } 44870910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 4488473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 448951fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 4490473a3ab2SBarry Smith char time[32]; 4491473a3ab2SBarry Smith 4492473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 449385b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4494473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4495473a3ab2SBarry Smith h = yl + .95*(yr - yl); 449651fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4497473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4498473a3ab2SBarry Smith } 4499473a3ab2SBarry Smith 45006083293cSBarry Smith if (ictx->showinitial) { 45016083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 45026083293cSBarry Smith } 45031713a123SBarry Smith PetscFunctionReturn(0); 45041713a123SBarry Smith } 45051713a123SBarry Smith 45069110b2e7SHong Zhang /*@C 45072d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 45082d5ee99bSBarry Smith 45092d5ee99bSBarry Smith Collective on TS 45102d5ee99bSBarry Smith 45112d5ee99bSBarry Smith Input Parameters: 45122d5ee99bSBarry Smith + ts - the TS context 45132d5ee99bSBarry Smith . step - current time-step 45142d5ee99bSBarry Smith . ptime - current time 45152d5ee99bSBarry Smith - dummy - either a viewer or NULL 45162d5ee99bSBarry Smith 45172d5ee99bSBarry Smith Level: intermediate 45182d5ee99bSBarry Smith 45192d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 45202d5ee99bSBarry Smith @*/ 45212d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 45222d5ee99bSBarry Smith { 45232d5ee99bSBarry Smith PetscErrorCode ierr; 45242d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 45252d5ee99bSBarry Smith PetscDraw draw; 45266934998bSLisandro Dalcin PetscDrawAxis axis; 45272d5ee99bSBarry Smith PetscInt n; 45282d5ee99bSBarry Smith PetscMPIInt size; 45296934998bSLisandro Dalcin PetscReal U0,U1,xl,yl,xr,yr,h; 45302d5ee99bSBarry Smith char time[32]; 45312d5ee99bSBarry Smith const PetscScalar *U; 45322d5ee99bSBarry Smith 45332d5ee99bSBarry Smith PetscFunctionBegin; 45346934998bSLisandro Dalcin ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr); 45356934998bSLisandro Dalcin if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs"); 45362d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 45376934998bSLisandro Dalcin if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 45382d5ee99bSBarry Smith 45392d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 45406934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr); 45416934998bSLisandro Dalcin ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 45426934998bSLisandro Dalcin if (!step) { 45436934998bSLisandro Dalcin ierr = PetscDrawClear(draw);CHKERRQ(ierr); 45446934998bSLisandro Dalcin ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); 45456934998bSLisandro Dalcin } 45462d5ee99bSBarry Smith 45472d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 45486934998bSLisandro Dalcin U0 = PetscRealPart(U[0]); 45496934998bSLisandro Dalcin U1 = PetscRealPart(U[1]); 4550a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 45516934998bSLisandro Dalcin if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0); 45522d5ee99bSBarry Smith 45536934998bSLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 45546934998bSLisandro Dalcin ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr); 45552d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 45564b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 455785b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 45582d5ee99bSBarry Smith h = yl + .95*(yr - yl); 455951fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 45602d5ee99bSBarry Smith } 45616934998bSLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 45622d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 456356d62c8fSLisandro Dalcin ierr = PetscDrawPause(draw);CHKERRQ(ierr); 45646934998bSLisandro Dalcin ierr = PetscDrawSave(draw);CHKERRQ(ierr); 45652d5ee99bSBarry Smith PetscFunctionReturn(0); 45662d5ee99bSBarry Smith } 45672d5ee99bSBarry Smith 45686083293cSBarry Smith /*@C 456983a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 45706083293cSBarry Smith 45716083293cSBarry Smith Collective on TS 45726083293cSBarry Smith 45736083293cSBarry Smith Input Parameters: 45746083293cSBarry Smith . ctx - the monitor context 45756083293cSBarry Smith 45766083293cSBarry Smith Level: intermediate 45776083293cSBarry Smith 457883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 45796083293cSBarry Smith @*/ 458083a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 45816083293cSBarry Smith { 45826083293cSBarry Smith PetscErrorCode ierr; 45836083293cSBarry Smith 45846083293cSBarry Smith PetscFunctionBegin; 458583a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 458683a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 458783a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 45886083293cSBarry Smith PetscFunctionReturn(0); 45896083293cSBarry Smith } 45906083293cSBarry Smith 45916083293cSBarry Smith /*@C 459283a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 45936083293cSBarry Smith 45946083293cSBarry Smith Collective on TS 45956083293cSBarry Smith 45966083293cSBarry Smith Input Parameter: 45976083293cSBarry Smith . ts - time-step context 45986083293cSBarry Smith 45996083293cSBarry Smith Output Patameter: 46006083293cSBarry Smith . ctx - the monitor context 46016083293cSBarry Smith 460299fdda47SBarry Smith Options Database: 460399fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 460499fdda47SBarry Smith 46056083293cSBarry Smith Level: intermediate 46066083293cSBarry Smith 460783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 46086083293cSBarry Smith @*/ 460983a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 46106083293cSBarry Smith { 46116083293cSBarry Smith PetscErrorCode ierr; 46126083293cSBarry Smith 46136083293cSBarry Smith PetscFunctionBegin; 4614b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 461583a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4616e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4617bbd56ea5SKarl Rupp 461883a4ac43SBarry Smith (*ctx)->howoften = howoften; 4619473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 4620c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4621473a3ab2SBarry Smith 4622473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 4623c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 46246083293cSBarry Smith PetscFunctionReturn(0); 46256083293cSBarry Smith } 46266083293cSBarry Smith 46273a471f94SBarry Smith /*@C 46280ed3bfb6SBarry Smith TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling 46290ed3bfb6SBarry Smith VecView() for the solution provided by TSSetSolutionFunction() at each timestep 46300ed3bfb6SBarry Smith 46310ed3bfb6SBarry Smith Collective on TS 46320ed3bfb6SBarry Smith 46330ed3bfb6SBarry Smith Input Parameters: 46340ed3bfb6SBarry Smith + ts - the TS context 46350ed3bfb6SBarry Smith . step - current time-step 46360ed3bfb6SBarry Smith . ptime - current time 46370ed3bfb6SBarry Smith - dummy - either a viewer or NULL 46380ed3bfb6SBarry Smith 46390ed3bfb6SBarry Smith Options Database: 46400ed3bfb6SBarry Smith . -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 46410ed3bfb6SBarry Smith 46420ed3bfb6SBarry Smith Level: intermediate 46430ed3bfb6SBarry Smith 46440ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 46450ed3bfb6SBarry Smith @*/ 46460ed3bfb6SBarry Smith PetscErrorCode TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 46470ed3bfb6SBarry Smith { 46480ed3bfb6SBarry Smith PetscErrorCode ierr; 46490ed3bfb6SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 46500ed3bfb6SBarry Smith PetscViewer viewer = ctx->viewer; 46510ed3bfb6SBarry Smith Vec work; 46520ed3bfb6SBarry Smith 46530ed3bfb6SBarry Smith PetscFunctionBegin; 46540ed3bfb6SBarry Smith if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 46550ed3bfb6SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 46560ed3bfb6SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 46570ed3bfb6SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 46580ed3bfb6SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 46590ed3bfb6SBarry Smith PetscFunctionReturn(0); 46600ed3bfb6SBarry Smith } 46610ed3bfb6SBarry Smith 46620ed3bfb6SBarry Smith /*@C 466383a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 46643a471f94SBarry Smith VecView() for the error at each timestep 46653a471f94SBarry Smith 46663a471f94SBarry Smith Collective on TS 46673a471f94SBarry Smith 46683a471f94SBarry Smith Input Parameters: 46693a471f94SBarry Smith + ts - the TS context 46703a471f94SBarry Smith . step - current time-step 46713a471f94SBarry Smith . ptime - current time 46720298fd71SBarry Smith - dummy - either a viewer or NULL 46733a471f94SBarry Smith 46740ed3bfb6SBarry Smith Options Database: 46750ed3bfb6SBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 46760ed3bfb6SBarry Smith 46773a471f94SBarry Smith Level: intermediate 46783a471f94SBarry Smith 46790ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 46803a471f94SBarry Smith @*/ 46810910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 46823a471f94SBarry Smith { 46833a471f94SBarry Smith PetscErrorCode ierr; 468483a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 468583a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 46863a471f94SBarry Smith Vec work; 46873a471f94SBarry Smith 46883a471f94SBarry Smith PetscFunctionBegin; 4689b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 46900910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 46913a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 46920910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 46933a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 46943a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 46953a471f94SBarry Smith PetscFunctionReturn(0); 46963a471f94SBarry Smith } 46973a471f94SBarry Smith 4698af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 46996c699258SBarry Smith /*@ 47002a808120SBarry Smith TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS 47016c699258SBarry Smith 4702d083f849SBarry Smith Logically Collective on ts 47036c699258SBarry Smith 47046c699258SBarry Smith Input Parameters: 47052a808120SBarry Smith + ts - the ODE integrator object 47062a808120SBarry Smith - dm - the dm, cannot be NULL 47076c699258SBarry Smith 4708e03a659cSJed Brown Notes: 4709e03a659cSJed Brown A DM can only be used for solving one problem at a time because information about the problem is stored on the DM, 4710e03a659cSJed Brown even when not using interfaces like DMTSSetIFunction(). Use DMClone() to get a distinct DM when solving 4711e03a659cSJed Brown different problems using the same function space. 4712e03a659cSJed Brown 47136c699258SBarry Smith Level: intermediate 47146c699258SBarry Smith 47156c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 47166c699258SBarry Smith @*/ 47177087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 47186c699258SBarry Smith { 47196c699258SBarry Smith PetscErrorCode ierr; 4720089b2837SJed Brown SNES snes; 4721942e3340SBarry Smith DMTS tsdm; 47226c699258SBarry Smith 47236c699258SBarry Smith PetscFunctionBegin; 47240700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 47252a808120SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 472670663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4727942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 47282a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4729942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4730942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 473124989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 473224989b8cSPeter Brune tsdm->originaldm = dm; 473324989b8cSPeter Brune } 473424989b8cSPeter Brune } 47356bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 473624989b8cSPeter Brune } 47376c699258SBarry Smith ts->dm = dm; 4738bbd56ea5SKarl Rupp 4739089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4740089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 47416c699258SBarry Smith PetscFunctionReturn(0); 47426c699258SBarry Smith } 47436c699258SBarry Smith 47446c699258SBarry Smith /*@ 47456c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 47466c699258SBarry Smith 47473f9fe445SBarry Smith Not Collective 47486c699258SBarry Smith 47496c699258SBarry Smith Input Parameter: 47506c699258SBarry Smith . ts - the preconditioner context 47516c699258SBarry Smith 47526c699258SBarry Smith Output Parameter: 47536c699258SBarry Smith . dm - the dm 47546c699258SBarry Smith 47556c699258SBarry Smith Level: intermediate 47566c699258SBarry Smith 47576c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 47586c699258SBarry Smith @*/ 47597087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 47606c699258SBarry Smith { 4761496e6a7aSJed Brown PetscErrorCode ierr; 4762496e6a7aSJed Brown 47636c699258SBarry Smith PetscFunctionBegin; 47640700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4765496e6a7aSJed Brown if (!ts->dm) { 4766ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4767496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4768496e6a7aSJed Brown } 47696c699258SBarry Smith *dm = ts->dm; 47706c699258SBarry Smith PetscFunctionReturn(0); 47716c699258SBarry Smith } 47721713a123SBarry Smith 47730f5c6efeSJed Brown /*@ 47740f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 47750f5c6efeSJed Brown 47763f9fe445SBarry Smith Logically Collective on SNES 47770f5c6efeSJed Brown 47780f5c6efeSJed Brown Input Parameter: 4779d42a1c89SJed Brown + snes - nonlinear solver 47800910c330SBarry Smith . U - the current state at which to evaluate the residual 4781d42a1c89SJed Brown - ctx - user context, must be a TS 47820f5c6efeSJed Brown 47830f5c6efeSJed Brown Output Parameter: 47840f5c6efeSJed Brown . F - the nonlinear residual 47850f5c6efeSJed Brown 47860f5c6efeSJed Brown Notes: 47870f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 47880f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 47890f5c6efeSJed Brown 47900f5c6efeSJed Brown Level: advanced 47910f5c6efeSJed Brown 47920f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 47930f5c6efeSJed Brown @*/ 47940910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 47950f5c6efeSJed Brown { 47960f5c6efeSJed Brown TS ts = (TS)ctx; 47970f5c6efeSJed Brown PetscErrorCode ierr; 47980f5c6efeSJed Brown 47990f5c6efeSJed Brown PetscFunctionBegin; 48000f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 48010910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 48020f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 48030f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 48040910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 48050f5c6efeSJed Brown PetscFunctionReturn(0); 48060f5c6efeSJed Brown } 48070f5c6efeSJed Brown 48080f5c6efeSJed Brown /*@ 48090f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 48100f5c6efeSJed Brown 48110f5c6efeSJed Brown Collective on SNES 48120f5c6efeSJed Brown 48130f5c6efeSJed Brown Input Parameter: 48140f5c6efeSJed Brown + snes - nonlinear solver 48150910c330SBarry Smith . U - the current state at which to evaluate the residual 48160f5c6efeSJed Brown - ctx - user context, must be a TS 48170f5c6efeSJed Brown 48180f5c6efeSJed Brown Output Parameter: 48190f5c6efeSJed Brown + A - the Jacobian 48200f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 48210f5c6efeSJed Brown - flag - indicates any structure change in the matrix 48220f5c6efeSJed Brown 48230f5c6efeSJed Brown Notes: 48240f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 48250f5c6efeSJed Brown 48260f5c6efeSJed Brown Level: developer 48270f5c6efeSJed Brown 48280f5c6efeSJed Brown .seealso: SNESSetJacobian() 48290f5c6efeSJed Brown @*/ 4830d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 48310f5c6efeSJed Brown { 48320f5c6efeSJed Brown TS ts = (TS)ctx; 48330f5c6efeSJed Brown PetscErrorCode ierr; 48340f5c6efeSJed Brown 48350f5c6efeSJed Brown PetscFunctionBegin; 48360f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 48370910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 48380f5c6efeSJed Brown PetscValidPointer(A,3); 483994ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 48400f5c6efeSJed Brown PetscValidPointer(B,4); 484194ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 48420f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 4843d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 48440f5c6efeSJed Brown PetscFunctionReturn(0); 48450f5c6efeSJed Brown } 4846325fc9f4SBarry Smith 48470e4ef248SJed Brown /*@C 48489ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 48490e4ef248SJed Brown 48500e4ef248SJed Brown Collective on TS 48510e4ef248SJed Brown 48520e4ef248SJed Brown Input Arguments: 48530e4ef248SJed Brown + ts - time stepping context 48540e4ef248SJed Brown . t - time at which to evaluate 48550910c330SBarry Smith . U - state at which to evaluate 48560e4ef248SJed Brown - ctx - context 48570e4ef248SJed Brown 48580e4ef248SJed Brown Output Arguments: 48590e4ef248SJed Brown . F - right hand side 48600e4ef248SJed Brown 48610e4ef248SJed Brown Level: intermediate 48620e4ef248SJed Brown 48630e4ef248SJed Brown Notes: 48640e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 48650e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 48660e4ef248SJed Brown 48670e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 48680e4ef248SJed Brown @*/ 48690910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 48700e4ef248SJed Brown { 48710e4ef248SJed Brown PetscErrorCode ierr; 48720e4ef248SJed Brown Mat Arhs,Brhs; 48730e4ef248SJed Brown 48740e4ef248SJed Brown PetscFunctionBegin; 48750e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 4876d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 48770910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 48780e4ef248SJed Brown PetscFunctionReturn(0); 48790e4ef248SJed Brown } 48800e4ef248SJed Brown 48810e4ef248SJed Brown /*@C 48820e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 48830e4ef248SJed Brown 48840e4ef248SJed Brown Collective on TS 48850e4ef248SJed Brown 48860e4ef248SJed Brown Input Arguments: 48870e4ef248SJed Brown + ts - time stepping context 48880e4ef248SJed Brown . t - time at which to evaluate 48890910c330SBarry Smith . U - state at which to evaluate 48900e4ef248SJed Brown - ctx - context 48910e4ef248SJed Brown 48920e4ef248SJed Brown Output Arguments: 48930e4ef248SJed Brown + A - pointer to operator 48940e4ef248SJed Brown . B - pointer to preconditioning matrix 48950e4ef248SJed Brown - flg - matrix structure flag 48960e4ef248SJed Brown 48970e4ef248SJed Brown Level: intermediate 48980e4ef248SJed Brown 48990e4ef248SJed Brown Notes: 49000e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 49010e4ef248SJed Brown 49020e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 49030e4ef248SJed Brown @*/ 4904d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 49050e4ef248SJed Brown { 49060e4ef248SJed Brown PetscFunctionBegin; 49070e4ef248SJed Brown PetscFunctionReturn(0); 49080e4ef248SJed Brown } 49090e4ef248SJed Brown 49100026cea9SSean Farley /*@C 49110026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 49120026cea9SSean Farley 49130026cea9SSean Farley Collective on TS 49140026cea9SSean Farley 49150026cea9SSean Farley Input Arguments: 49160026cea9SSean Farley + ts - time stepping context 49170026cea9SSean Farley . t - time at which to evaluate 49180910c330SBarry Smith . U - state at which to evaluate 49190910c330SBarry Smith . Udot - time derivative of state vector 49200026cea9SSean Farley - ctx - context 49210026cea9SSean Farley 49220026cea9SSean Farley Output Arguments: 49230026cea9SSean Farley . F - left hand side 49240026cea9SSean Farley 49250026cea9SSean Farley Level: intermediate 49260026cea9SSean Farley 49270026cea9SSean Farley Notes: 49280910c330SBarry 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 49290026cea9SSean Farley user is required to write their own TSComputeIFunction. 49300026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 49310026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 49320026cea9SSean Farley 49339ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 49349ae8fd06SBarry Smith 49359ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 49360026cea9SSean Farley @*/ 49370910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 49380026cea9SSean Farley { 49390026cea9SSean Farley PetscErrorCode ierr; 49400026cea9SSean Farley Mat A,B; 49410026cea9SSean Farley 49420026cea9SSean Farley PetscFunctionBegin; 49430298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 4944d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 49450910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 49460026cea9SSean Farley PetscFunctionReturn(0); 49470026cea9SSean Farley } 49480026cea9SSean Farley 49490026cea9SSean Farley /*@C 4950b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 49510026cea9SSean Farley 49520026cea9SSean Farley Collective on TS 49530026cea9SSean Farley 49540026cea9SSean Farley Input Arguments: 49550026cea9SSean Farley + ts - time stepping context 49560026cea9SSean Farley . t - time at which to evaluate 49570910c330SBarry Smith . U - state at which to evaluate 49580910c330SBarry Smith . Udot - time derivative of state vector 49590026cea9SSean Farley . shift - shift to apply 49600026cea9SSean Farley - ctx - context 49610026cea9SSean Farley 49620026cea9SSean Farley Output Arguments: 49630026cea9SSean Farley + A - pointer to operator 49640026cea9SSean Farley . B - pointer to preconditioning matrix 49650026cea9SSean Farley - flg - matrix structure flag 49660026cea9SSean Farley 4967b41af12eSJed Brown Level: advanced 49680026cea9SSean Farley 49690026cea9SSean Farley Notes: 49700026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 49710026cea9SSean Farley 4972b41af12eSJed Brown It is only appropriate for problems of the form 4973b41af12eSJed Brown 4974b41af12eSJed Brown $ M Udot = F(U,t) 4975b41af12eSJed Brown 4976b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4977b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4978b41af12eSJed Brown an implicit operator of the form 4979b41af12eSJed Brown 4980b41af12eSJed Brown $ shift*M + J 4981b41af12eSJed Brown 4982b41af12eSJed 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 4983b41af12eSJed Brown a copy of M or reassemble it when requested. 4984b41af12eSJed Brown 49850026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 49860026cea9SSean Farley @*/ 4987d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 49880026cea9SSean Farley { 4989b41af12eSJed Brown PetscErrorCode ierr; 4990b41af12eSJed Brown 49910026cea9SSean Farley PetscFunctionBegin; 499294ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4993b41af12eSJed Brown ts->ijacobian.shift = shift; 49940026cea9SSean Farley PetscFunctionReturn(0); 49950026cea9SSean Farley } 4996b41af12eSJed Brown 4997e817cc15SEmil Constantinescu /*@ 4998e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 4999e817cc15SEmil Constantinescu 5000e817cc15SEmil Constantinescu Not Collective 5001e817cc15SEmil Constantinescu 5002e817cc15SEmil Constantinescu Input Parameter: 5003e817cc15SEmil Constantinescu . ts - the TS context 5004e817cc15SEmil Constantinescu 5005e817cc15SEmil Constantinescu Output Parameter: 50064e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 5007e817cc15SEmil Constantinescu 5008e817cc15SEmil Constantinescu Level: beginner 5009e817cc15SEmil Constantinescu 5010e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 5011e817cc15SEmil Constantinescu @*/ 5012e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 5013e817cc15SEmil Constantinescu { 5014e817cc15SEmil Constantinescu PetscFunctionBegin; 5015e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5016e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 5017e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 5018e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5019e817cc15SEmil Constantinescu } 5020e817cc15SEmil Constantinescu 5021e817cc15SEmil Constantinescu /*@ 5022e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 5023e817cc15SEmil Constantinescu 5024e817cc15SEmil Constantinescu Not Collective 5025e817cc15SEmil Constantinescu 5026e817cc15SEmil Constantinescu Input Parameter: 5027e817cc15SEmil Constantinescu + ts - the TS context 50281297b224SEmil Constantinescu - equation_type - see TSEquationType 5029e817cc15SEmil Constantinescu 5030e817cc15SEmil Constantinescu Level: advanced 5031e817cc15SEmil Constantinescu 5032e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 5033e817cc15SEmil Constantinescu @*/ 5034e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 5035e817cc15SEmil Constantinescu { 5036e817cc15SEmil Constantinescu PetscFunctionBegin; 5037e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5038e817cc15SEmil Constantinescu ts->equation_type = equation_type; 5039e817cc15SEmil Constantinescu PetscFunctionReturn(0); 5040e817cc15SEmil Constantinescu } 50410026cea9SSean Farley 50424af1b03aSJed Brown /*@ 50434af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 50444af1b03aSJed Brown 50454af1b03aSJed Brown Not Collective 50464af1b03aSJed Brown 50474af1b03aSJed Brown Input Parameter: 50484af1b03aSJed Brown . ts - the TS context 50494af1b03aSJed Brown 50504af1b03aSJed Brown Output Parameter: 50514af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 50524af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 50534af1b03aSJed Brown 5054487e0bb9SJed Brown Level: beginner 50554af1b03aSJed Brown 5056cd652676SJed Brown Notes: 5057cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 50584af1b03aSJed Brown 50594af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 50604af1b03aSJed Brown @*/ 50614af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 50624af1b03aSJed Brown { 50634af1b03aSJed Brown PetscFunctionBegin; 50644af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 50654af1b03aSJed Brown PetscValidPointer(reason,2); 50664af1b03aSJed Brown *reason = ts->reason; 50674af1b03aSJed Brown PetscFunctionReturn(0); 50684af1b03aSJed Brown } 50694af1b03aSJed Brown 5070d6ad946cSShri Abhyankar /*@ 5071d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 5072d6ad946cSShri Abhyankar 50736b221cbeSPatrick Sanan Logically Collective; reason must contain common value 5074d6ad946cSShri Abhyankar 50756b221cbeSPatrick Sanan Input Parameters: 5076d6ad946cSShri Abhyankar + ts - the TS context 50776b221cbeSPatrick Sanan - reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 5078d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 5079d6ad946cSShri Abhyankar 5080f5abba47SShri Abhyankar Level: advanced 5081d6ad946cSShri Abhyankar 5082d6ad946cSShri Abhyankar Notes: 50836b221cbeSPatrick Sanan Can only be called while TSSolve() is active. 5084d6ad946cSShri Abhyankar 5085d6ad946cSShri Abhyankar .seealso: TSConvergedReason 5086d6ad946cSShri Abhyankar @*/ 5087d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 5088d6ad946cSShri Abhyankar { 5089d6ad946cSShri Abhyankar PetscFunctionBegin; 5090d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5091d6ad946cSShri Abhyankar ts->reason = reason; 5092d6ad946cSShri Abhyankar PetscFunctionReturn(0); 5093d6ad946cSShri Abhyankar } 5094d6ad946cSShri Abhyankar 5095cc708dedSBarry Smith /*@ 5096cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 5097cc708dedSBarry Smith 5098cc708dedSBarry Smith Not Collective 5099cc708dedSBarry Smith 5100cc708dedSBarry Smith Input Parameter: 5101cc708dedSBarry Smith . ts - the TS context 5102cc708dedSBarry Smith 5103cc708dedSBarry Smith Output Parameter: 510419eac22cSLisandro Dalcin . ftime - the final time. This time corresponds to the final time set with TSSetMaxTime() 5105cc708dedSBarry Smith 5106487e0bb9SJed Brown Level: beginner 5107cc708dedSBarry Smith 5108cc708dedSBarry Smith Notes: 5109cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 5110cc708dedSBarry Smith 5111cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 5112cc708dedSBarry Smith @*/ 5113cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 5114cc708dedSBarry Smith { 5115cc708dedSBarry Smith PetscFunctionBegin; 5116cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5117cc708dedSBarry Smith PetscValidPointer(ftime,2); 5118cc708dedSBarry Smith *ftime = ts->solvetime; 5119cc708dedSBarry Smith PetscFunctionReturn(0); 5120cc708dedSBarry Smith } 5121cc708dedSBarry Smith 51222c18e0fdSBarry Smith /*@ 51235ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 51249f67acb7SJed Brown used by the time integrator. 51259f67acb7SJed Brown 51269f67acb7SJed Brown Not Collective 51279f67acb7SJed Brown 51289f67acb7SJed Brown Input Parameter: 51299f67acb7SJed Brown . ts - TS context 51309f67acb7SJed Brown 51319f67acb7SJed Brown Output Parameter: 51329f67acb7SJed Brown . nits - number of nonlinear iterations 51339f67acb7SJed Brown 51349f67acb7SJed Brown Notes: 51359f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 51369f67acb7SJed Brown 51379f67acb7SJed Brown Level: intermediate 51389f67acb7SJed Brown 51395ef26d82SJed Brown .seealso: TSGetKSPIterations() 51409f67acb7SJed Brown @*/ 51415ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 51429f67acb7SJed Brown { 51439f67acb7SJed Brown PetscFunctionBegin; 51449f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 51459f67acb7SJed Brown PetscValidIntPointer(nits,2); 51465ef26d82SJed Brown *nits = ts->snes_its; 51479f67acb7SJed Brown PetscFunctionReturn(0); 51489f67acb7SJed Brown } 51499f67acb7SJed Brown 51509f67acb7SJed Brown /*@ 51515ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 51529f67acb7SJed Brown used by the time integrator. 51539f67acb7SJed Brown 51549f67acb7SJed Brown Not Collective 51559f67acb7SJed Brown 51569f67acb7SJed Brown Input Parameter: 51579f67acb7SJed Brown . ts - TS context 51589f67acb7SJed Brown 51599f67acb7SJed Brown Output Parameter: 51609f67acb7SJed Brown . lits - number of linear iterations 51619f67acb7SJed Brown 51629f67acb7SJed Brown Notes: 51639f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 51649f67acb7SJed Brown 51659f67acb7SJed Brown Level: intermediate 51669f67acb7SJed Brown 51675ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 51689f67acb7SJed Brown @*/ 51695ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 51709f67acb7SJed Brown { 51719f67acb7SJed Brown PetscFunctionBegin; 51729f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 51739f67acb7SJed Brown PetscValidIntPointer(lits,2); 51745ef26d82SJed Brown *lits = ts->ksp_its; 51759f67acb7SJed Brown PetscFunctionReturn(0); 51769f67acb7SJed Brown } 51779f67acb7SJed Brown 5178cef5090cSJed Brown /*@ 5179cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 5180cef5090cSJed Brown 5181cef5090cSJed Brown Not Collective 5182cef5090cSJed Brown 5183cef5090cSJed Brown Input Parameter: 5184cef5090cSJed Brown . ts - TS context 5185cef5090cSJed Brown 5186cef5090cSJed Brown Output Parameter: 5187cef5090cSJed Brown . rejects - number of steps rejected 5188cef5090cSJed Brown 5189cef5090cSJed Brown Notes: 5190cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5191cef5090cSJed Brown 5192cef5090cSJed Brown Level: intermediate 5193cef5090cSJed Brown 51945ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 5195cef5090cSJed Brown @*/ 5196cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 5197cef5090cSJed Brown { 5198cef5090cSJed Brown PetscFunctionBegin; 5199cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5200cef5090cSJed Brown PetscValidIntPointer(rejects,2); 5201cef5090cSJed Brown *rejects = ts->reject; 5202cef5090cSJed Brown PetscFunctionReturn(0); 5203cef5090cSJed Brown } 5204cef5090cSJed Brown 5205cef5090cSJed Brown /*@ 5206cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 5207cef5090cSJed Brown 5208cef5090cSJed Brown Not Collective 5209cef5090cSJed Brown 5210cef5090cSJed Brown Input Parameter: 5211cef5090cSJed Brown . ts - TS context 5212cef5090cSJed Brown 5213cef5090cSJed Brown Output Parameter: 5214cef5090cSJed Brown . fails - number of failed nonlinear solves 5215cef5090cSJed Brown 5216cef5090cSJed Brown Notes: 5217cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 5218cef5090cSJed Brown 5219cef5090cSJed Brown Level: intermediate 5220cef5090cSJed Brown 52215ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 5222cef5090cSJed Brown @*/ 5223cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 5224cef5090cSJed Brown { 5225cef5090cSJed Brown PetscFunctionBegin; 5226cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5227cef5090cSJed Brown PetscValidIntPointer(fails,2); 5228cef5090cSJed Brown *fails = ts->num_snes_failures; 5229cef5090cSJed Brown PetscFunctionReturn(0); 5230cef5090cSJed Brown } 5231cef5090cSJed Brown 5232cef5090cSJed Brown /*@ 5233cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 5234cef5090cSJed Brown 5235cef5090cSJed Brown Not Collective 5236cef5090cSJed Brown 5237cef5090cSJed Brown Input Parameter: 5238cef5090cSJed Brown + ts - TS context 5239cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 5240cef5090cSJed Brown 5241cef5090cSJed Brown Notes: 5242cef5090cSJed Brown The counter is reset to zero for each step 5243cef5090cSJed Brown 5244cef5090cSJed Brown Options Database Key: 5245cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 5246cef5090cSJed Brown 5247cef5090cSJed Brown Level: intermediate 5248cef5090cSJed Brown 52495ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5250cef5090cSJed Brown @*/ 5251cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 5252cef5090cSJed Brown { 5253cef5090cSJed Brown PetscFunctionBegin; 5254cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5255cef5090cSJed Brown ts->max_reject = rejects; 5256cef5090cSJed Brown PetscFunctionReturn(0); 5257cef5090cSJed Brown } 5258cef5090cSJed Brown 5259cef5090cSJed Brown /*@ 5260cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 5261cef5090cSJed Brown 5262cef5090cSJed Brown Not Collective 5263cef5090cSJed Brown 5264cef5090cSJed Brown Input Parameter: 5265cef5090cSJed Brown + ts - TS context 5266cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 5267cef5090cSJed Brown 5268cef5090cSJed Brown Notes: 5269cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 5270cef5090cSJed Brown 5271cef5090cSJed Brown Options Database Key: 5272cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 5273cef5090cSJed Brown 5274cef5090cSJed Brown Level: intermediate 5275cef5090cSJed Brown 52765ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 5277cef5090cSJed Brown @*/ 5278cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 5279cef5090cSJed Brown { 5280cef5090cSJed Brown PetscFunctionBegin; 5281cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5282cef5090cSJed Brown ts->max_snes_failures = fails; 5283cef5090cSJed Brown PetscFunctionReturn(0); 5284cef5090cSJed Brown } 5285cef5090cSJed Brown 5286cef5090cSJed Brown /*@ 5287cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 5288cef5090cSJed Brown 5289cef5090cSJed Brown Not Collective 5290cef5090cSJed Brown 5291cef5090cSJed Brown Input Parameter: 5292cef5090cSJed Brown + ts - TS context 5293cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 5294cef5090cSJed Brown 5295cef5090cSJed Brown Options Database Key: 5296cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 5297cef5090cSJed Brown 5298cef5090cSJed Brown Level: intermediate 5299cef5090cSJed Brown 53005ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5301cef5090cSJed Brown @*/ 5302cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 5303cef5090cSJed Brown { 5304cef5090cSJed Brown PetscFunctionBegin; 5305cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5306cef5090cSJed Brown ts->errorifstepfailed = err; 5307cef5090cSJed Brown PetscFunctionReturn(0); 5308cef5090cSJed Brown } 5309cef5090cSJed Brown 5310fb1732b5SBarry Smith /*@C 5311fde5950dSBarry 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 5312fb1732b5SBarry Smith 5313fb1732b5SBarry Smith Collective on TS 5314fb1732b5SBarry Smith 5315fb1732b5SBarry Smith Input Parameters: 5316fb1732b5SBarry Smith + ts - the TS context 5317fb1732b5SBarry Smith . step - current time-step 5318fb1732b5SBarry Smith . ptime - current time 53190910c330SBarry Smith . u - current state 5320721cd6eeSBarry Smith - vf - viewer and its format 5321fb1732b5SBarry Smith 5322fb1732b5SBarry Smith Level: intermediate 5323fb1732b5SBarry Smith 5324fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5325fb1732b5SBarry Smith @*/ 5326721cd6eeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf) 5327fb1732b5SBarry Smith { 5328fb1732b5SBarry Smith PetscErrorCode ierr; 5329fb1732b5SBarry Smith 5330fb1732b5SBarry Smith PetscFunctionBegin; 5331721cd6eeSBarry Smith ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr); 5332721cd6eeSBarry Smith ierr = VecView(u,vf->viewer);CHKERRQ(ierr); 5333721cd6eeSBarry Smith ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr); 5334ed81e22dSJed Brown PetscFunctionReturn(0); 5335ed81e22dSJed Brown } 5336ed81e22dSJed Brown 5337ed81e22dSJed Brown /*@C 5338ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5339ed81e22dSJed Brown 5340ed81e22dSJed Brown Collective on TS 5341ed81e22dSJed Brown 5342ed81e22dSJed Brown Input Parameters: 5343ed81e22dSJed Brown + ts - the TS context 5344ed81e22dSJed Brown . step - current time-step 5345ed81e22dSJed Brown . ptime - current time 53460910c330SBarry Smith . u - current state 5347ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5348ed81e22dSJed Brown 5349ed81e22dSJed Brown Level: intermediate 5350ed81e22dSJed Brown 5351ed81e22dSJed Brown Notes: 5352ed81e22dSJed 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. 5353ed81e22dSJed Brown These are named according to the file name template. 5354ed81e22dSJed Brown 5355ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5356ed81e22dSJed Brown 5357ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5358ed81e22dSJed Brown @*/ 53590910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5360ed81e22dSJed Brown { 5361ed81e22dSJed Brown PetscErrorCode ierr; 5362ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 5363ed81e22dSJed Brown PetscViewer viewer; 5364ed81e22dSJed Brown 5365ed81e22dSJed Brown PetscFunctionBegin; 536663e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 53678caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5368ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 53690910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 5370ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5371ed81e22dSJed Brown PetscFunctionReturn(0); 5372ed81e22dSJed Brown } 5373ed81e22dSJed Brown 5374ed81e22dSJed Brown /*@C 5375ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5376ed81e22dSJed Brown 5377ed81e22dSJed Brown Collective on TS 5378ed81e22dSJed Brown 5379ed81e22dSJed Brown Input Parameters: 5380ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5381ed81e22dSJed Brown 5382ed81e22dSJed Brown Level: intermediate 5383ed81e22dSJed Brown 5384ed81e22dSJed Brown Note: 5385ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5386ed81e22dSJed Brown 5387ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5388ed81e22dSJed Brown @*/ 5389ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5390ed81e22dSJed Brown { 5391ed81e22dSJed Brown PetscErrorCode ierr; 5392ed81e22dSJed Brown 5393ed81e22dSJed Brown PetscFunctionBegin; 5394ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5395fb1732b5SBarry Smith PetscFunctionReturn(0); 5396fb1732b5SBarry Smith } 5397fb1732b5SBarry Smith 539884df9cb4SJed Brown /*@ 5399552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 540084df9cb4SJed Brown 5401ed81e22dSJed Brown Collective on TS if controller has not been created yet 540284df9cb4SJed Brown 540384df9cb4SJed Brown Input Arguments: 5404ed81e22dSJed Brown . ts - time stepping context 540584df9cb4SJed Brown 540684df9cb4SJed Brown Output Arguments: 5407ed81e22dSJed Brown . adapt - adaptive controller 540884df9cb4SJed Brown 540984df9cb4SJed Brown Level: intermediate 541084df9cb4SJed Brown 5411ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 541284df9cb4SJed Brown @*/ 5413552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 541484df9cb4SJed Brown { 541584df9cb4SJed Brown PetscErrorCode ierr; 541684df9cb4SJed Brown 541784df9cb4SJed Brown PetscFunctionBegin; 541884df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5419bec58848SLisandro Dalcin PetscValidPointer(adapt,2); 542084df9cb4SJed Brown if (!ts->adapt) { 5421ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 54223bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 54231c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 542484df9cb4SJed Brown } 5425bec58848SLisandro Dalcin *adapt = ts->adapt; 542684df9cb4SJed Brown PetscFunctionReturn(0); 542784df9cb4SJed Brown } 5428d6ebe24aSShri Abhyankar 54291c3436cfSJed Brown /*@ 54301c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 54311c3436cfSJed Brown 54321c3436cfSJed Brown Logically Collective 54331c3436cfSJed Brown 54341c3436cfSJed Brown Input Arguments: 54351c3436cfSJed Brown + ts - time integration context 54361c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 54370298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 54381c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 54390298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 54401c3436cfSJed Brown 5441a3cdaa26SBarry Smith Options Database keys: 5442a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5443a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5444a3cdaa26SBarry Smith 54453ff766beSShri Abhyankar Notes: 54463ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 54473ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 54483ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 54493ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 54503ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 54513ff766beSShri Abhyankar differential variables. 54523ff766beSShri Abhyankar 54531c3436cfSJed Brown Level: beginner 54541c3436cfSJed Brown 5455c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 54561c3436cfSJed Brown @*/ 54571c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 54581c3436cfSJed Brown { 54591c3436cfSJed Brown PetscErrorCode ierr; 54601c3436cfSJed Brown 54611c3436cfSJed Brown PetscFunctionBegin; 5462c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 54631c3436cfSJed Brown if (vatol) { 54641c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 54651c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 54661c3436cfSJed Brown ts->vatol = vatol; 54671c3436cfSJed Brown } 5468c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 54691c3436cfSJed Brown if (vrtol) { 54701c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 54711c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 54721c3436cfSJed Brown ts->vrtol = vrtol; 54731c3436cfSJed Brown } 54741c3436cfSJed Brown PetscFunctionReturn(0); 54751c3436cfSJed Brown } 54761c3436cfSJed Brown 5477c5033834SJed Brown /*@ 5478c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5479c5033834SJed Brown 5480c5033834SJed Brown Logically Collective 5481c5033834SJed Brown 5482c5033834SJed Brown Input Arguments: 5483c5033834SJed Brown . ts - time integration context 5484c5033834SJed Brown 5485c5033834SJed Brown Output Arguments: 54860298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 54870298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 54880298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 54890298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 5490c5033834SJed Brown 5491c5033834SJed Brown Level: beginner 5492c5033834SJed Brown 5493c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5494c5033834SJed Brown @*/ 5495c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5496c5033834SJed Brown { 5497c5033834SJed Brown PetscFunctionBegin; 5498c5033834SJed Brown if (atol) *atol = ts->atol; 5499c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5500c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5501c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 5502c5033834SJed Brown PetscFunctionReturn(0); 5503c5033834SJed Brown } 5504c5033834SJed Brown 55059c6b16b5SShri Abhyankar /*@ 5506a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 55079c6b16b5SShri Abhyankar 55089c6b16b5SShri Abhyankar Collective on TS 55099c6b16b5SShri Abhyankar 55109c6b16b5SShri Abhyankar Input Arguments: 55119c6b16b5SShri Abhyankar + ts - time stepping context 5512a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5513a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 55149c6b16b5SShri Abhyankar 55159c6b16b5SShri Abhyankar Output Arguments: 5516a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 55177453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5518a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 55199c6b16b5SShri Abhyankar 55209c6b16b5SShri Abhyankar Level: developer 55219c6b16b5SShri Abhyankar 5522deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 55239c6b16b5SShri Abhyankar @*/ 55247453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 55259c6b16b5SShri Abhyankar { 55269c6b16b5SShri Abhyankar PetscErrorCode ierr; 55279c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 55287453f775SEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 55297453f775SEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 55309c6b16b5SShri Abhyankar const PetscScalar *u,*y; 55317453f775SEmil Constantinescu PetscReal sum,suma,sumr,gsum,gsuma,gsumr,diff; 55327453f775SEmil Constantinescu PetscReal tol,tola,tolr; 55337453f775SEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 55349c6b16b5SShri Abhyankar 55359c6b16b5SShri Abhyankar PetscFunctionBegin; 55369c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5537a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5538a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5539a4868fbcSLisandro Dalcin PetscValidType(U,2); 5540a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5541a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5542a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 55438a175baeSEmil Constantinescu PetscValidPointer(norma,5); 55448a175baeSEmil Constantinescu PetscValidPointer(normr,6); 5545a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 55469c6b16b5SShri Abhyankar 55479c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 55489c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 55499c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 55509c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 55519c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 55527453f775SEmil Constantinescu sum = 0.; n_loc = 0; 55537453f775SEmil Constantinescu suma = 0.; na_loc = 0; 55547453f775SEmil Constantinescu sumr = 0.; nr_loc = 0; 55559c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 55569c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 55579c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 55589c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 55599c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 556076cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 55617453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 55627453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 55637453f775SEmil Constantinescu if(tola>0.){ 55647453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 55657453f775SEmil Constantinescu na_loc++; 55667453f775SEmil Constantinescu } 55677453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55687453f775SEmil Constantinescu if(tolr>0.){ 55697453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 55707453f775SEmil Constantinescu nr_loc++; 55717453f775SEmil Constantinescu } 55727453f775SEmil Constantinescu tol=tola+tolr; 55737453f775SEmil Constantinescu if(tol>0.){ 55747453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 55757453f775SEmil Constantinescu n_loc++; 55767453f775SEmil Constantinescu } 55779c6b16b5SShri Abhyankar } 55789c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 55799c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 55809c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 55819c6b16b5SShri Abhyankar const PetscScalar *atol; 55829c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 55839c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 558476cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 55857453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 55867453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 55877453f775SEmil Constantinescu if(tola>0.){ 55887453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 55897453f775SEmil Constantinescu na_loc++; 55907453f775SEmil Constantinescu } 55917453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55927453f775SEmil Constantinescu if(tolr>0.){ 55937453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 55947453f775SEmil Constantinescu nr_loc++; 55957453f775SEmil Constantinescu } 55967453f775SEmil Constantinescu tol=tola+tolr; 55977453f775SEmil Constantinescu if(tol>0.){ 55987453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 55997453f775SEmil Constantinescu n_loc++; 56007453f775SEmil Constantinescu } 56019c6b16b5SShri Abhyankar } 56029c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 56039c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 56049c6b16b5SShri Abhyankar const PetscScalar *rtol; 56059c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 56069c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 560776cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 56087453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 56097453f775SEmil Constantinescu tola = ts->atol; 56107453f775SEmil Constantinescu if(tola>0.){ 56117453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 56127453f775SEmil Constantinescu na_loc++; 56137453f775SEmil Constantinescu } 56147453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 56157453f775SEmil Constantinescu if(tolr>0.){ 56167453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 56177453f775SEmil Constantinescu nr_loc++; 56187453f775SEmil Constantinescu } 56197453f775SEmil Constantinescu tol=tola+tolr; 56207453f775SEmil Constantinescu if(tol>0.){ 56217453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 56227453f775SEmil Constantinescu n_loc++; 56237453f775SEmil Constantinescu } 56249c6b16b5SShri Abhyankar } 56259c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 56269c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 56279c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 562876cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 56297453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 56307453f775SEmil Constantinescu tola = ts->atol; 56317453f775SEmil Constantinescu if(tola>0.){ 56327453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 56337453f775SEmil Constantinescu na_loc++; 56347453f775SEmil Constantinescu } 56357453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 56367453f775SEmil Constantinescu if(tolr>0.){ 56377453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 56387453f775SEmil Constantinescu nr_loc++; 56397453f775SEmil Constantinescu } 56407453f775SEmil Constantinescu tol=tola+tolr; 56417453f775SEmil Constantinescu if(tol>0.){ 56427453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 56437453f775SEmil Constantinescu n_loc++; 56447453f775SEmil Constantinescu } 56459c6b16b5SShri Abhyankar } 56469c6b16b5SShri Abhyankar } 56479c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 56489c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 56499c6b16b5SShri Abhyankar 56507453f775SEmil Constantinescu err_loc[0] = sum; 56517453f775SEmil Constantinescu err_loc[1] = suma; 56527453f775SEmil Constantinescu err_loc[2] = sumr; 56537453f775SEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 56547453f775SEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 56557453f775SEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 56567453f775SEmil Constantinescu 5657a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 56587453f775SEmil Constantinescu 56597453f775SEmil Constantinescu gsum = err_glb[0]; 56607453f775SEmil Constantinescu gsuma = err_glb[1]; 56617453f775SEmil Constantinescu gsumr = err_glb[2]; 56627453f775SEmil Constantinescu n_glb = err_glb[3]; 56637453f775SEmil Constantinescu na_glb = err_glb[4]; 56647453f775SEmil Constantinescu nr_glb = err_glb[5]; 56657453f775SEmil Constantinescu 5666b1316ef9SEmil Constantinescu *norm = 0.; 5667b1316ef9SEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 5668b1316ef9SEmil Constantinescu *norma = 0.; 5669b1316ef9SEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 5670b1316ef9SEmil Constantinescu *normr = 0.; 5671b1316ef9SEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 56729c6b16b5SShri Abhyankar 56739c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 56747453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 56757453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 56769c6b16b5SShri Abhyankar PetscFunctionReturn(0); 56779c6b16b5SShri Abhyankar } 56789c6b16b5SShri Abhyankar 56799c6b16b5SShri Abhyankar /*@ 5680a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 56819c6b16b5SShri Abhyankar 56829c6b16b5SShri Abhyankar Collective on TS 56839c6b16b5SShri Abhyankar 56849c6b16b5SShri Abhyankar Input Arguments: 56859c6b16b5SShri Abhyankar + ts - time stepping context 5686a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5687a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 56889c6b16b5SShri Abhyankar 56899c6b16b5SShri Abhyankar Output Arguments: 5690a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 56917453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5692a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 56939c6b16b5SShri Abhyankar 56949c6b16b5SShri Abhyankar Level: developer 56959c6b16b5SShri Abhyankar 5696deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 56979c6b16b5SShri Abhyankar @*/ 56987453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 56999c6b16b5SShri Abhyankar { 57009c6b16b5SShri Abhyankar PetscErrorCode ierr; 57017453f775SEmil Constantinescu PetscInt i,n,N,rstart; 57029c6b16b5SShri Abhyankar const PetscScalar *u,*y; 57037453f775SEmil Constantinescu PetscReal max,gmax,maxa,gmaxa,maxr,gmaxr; 57047453f775SEmil Constantinescu PetscReal tol,tola,tolr,diff; 57057453f775SEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 57069c6b16b5SShri Abhyankar 57079c6b16b5SShri Abhyankar PetscFunctionBegin; 57089c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5709a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5710a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5711a4868fbcSLisandro Dalcin PetscValidType(U,2); 5712a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5713a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5714a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 57158a175baeSEmil Constantinescu PetscValidPointer(norma,5); 57168a175baeSEmil Constantinescu PetscValidPointer(normr,6); 5717a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 57189c6b16b5SShri Abhyankar 57199c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 57209c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 57219c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 57229c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 57239c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 57247453f775SEmil Constantinescu 57257453f775SEmil Constantinescu max=0.; 57267453f775SEmil Constantinescu maxa=0.; 57277453f775SEmil Constantinescu maxr=0.; 57287453f775SEmil Constantinescu 57297453f775SEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 57309c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 57319c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 57329c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 57337453f775SEmil Constantinescu 57347453f775SEmil Constantinescu for (i=0; i<n; i++) { 573576cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 57367453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 57377453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 57387453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 57397453f775SEmil Constantinescu tol = tola+tolr; 57407453f775SEmil Constantinescu if(tola>0.){ 57417453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 57427453f775SEmil Constantinescu } 57437453f775SEmil Constantinescu if(tolr>0.){ 57447453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 57457453f775SEmil Constantinescu } 57467453f775SEmil Constantinescu if(tol>0.){ 57477453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 57487453f775SEmil Constantinescu } 57499c6b16b5SShri Abhyankar } 57509c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 57519c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 57529c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 57539c6b16b5SShri Abhyankar const PetscScalar *atol; 57549c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 57557453f775SEmil Constantinescu for (i=0; i<n; i++) { 575676cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 57577453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 57587453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 57597453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 57607453f775SEmil Constantinescu tol = tola+tolr; 57617453f775SEmil Constantinescu if(tola>0.){ 57627453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 57637453f775SEmil Constantinescu } 57647453f775SEmil Constantinescu if(tolr>0.){ 57657453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 57667453f775SEmil Constantinescu } 57677453f775SEmil Constantinescu if(tol>0.){ 57687453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 57697453f775SEmil Constantinescu } 57709c6b16b5SShri Abhyankar } 57719c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 57729c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 57739c6b16b5SShri Abhyankar const PetscScalar *rtol; 57749c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 57757453f775SEmil Constantinescu 57767453f775SEmil Constantinescu for (i=0; i<n; i++) { 577776cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 57787453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 57797453f775SEmil Constantinescu tola = ts->atol; 57807453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 57817453f775SEmil Constantinescu tol = tola+tolr; 57827453f775SEmil Constantinescu if(tola>0.){ 57837453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 57847453f775SEmil Constantinescu } 57857453f775SEmil Constantinescu if(tolr>0.){ 57867453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 57877453f775SEmil Constantinescu } 57887453f775SEmil Constantinescu if(tol>0.){ 57897453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 57907453f775SEmil Constantinescu } 57919c6b16b5SShri Abhyankar } 57929c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 57939c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 57947453f775SEmil Constantinescu 57957453f775SEmil Constantinescu for (i=0; i<n; i++) { 579676cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 57977453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 57987453f775SEmil Constantinescu tola = ts->atol; 57997453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58007453f775SEmil Constantinescu tol = tola+tolr; 58017453f775SEmil Constantinescu if(tola>0.){ 58027453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 58037453f775SEmil Constantinescu } 58047453f775SEmil Constantinescu if(tolr>0.){ 58057453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 58067453f775SEmil Constantinescu } 58077453f775SEmil Constantinescu if(tol>0.){ 58087453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 58097453f775SEmil Constantinescu } 58109c6b16b5SShri Abhyankar } 58119c6b16b5SShri Abhyankar } 58129c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 58139c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 58147453f775SEmil Constantinescu err_loc[0] = max; 58157453f775SEmil Constantinescu err_loc[1] = maxa; 58167453f775SEmil Constantinescu err_loc[2] = maxr; 5817a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 58187453f775SEmil Constantinescu gmax = err_glb[0]; 58197453f775SEmil Constantinescu gmaxa = err_glb[1]; 58207453f775SEmil Constantinescu gmaxr = err_glb[2]; 58219c6b16b5SShri Abhyankar 58229c6b16b5SShri Abhyankar *norm = gmax; 58237453f775SEmil Constantinescu *norma = gmaxa; 58247453f775SEmil Constantinescu *normr = gmaxr; 58259c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 58267453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 58277453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 58289c6b16b5SShri Abhyankar PetscFunctionReturn(0); 58299c6b16b5SShri Abhyankar } 58309c6b16b5SShri Abhyankar 58311c3436cfSJed Brown /*@ 58328a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 58331c3436cfSJed Brown 58341c3436cfSJed Brown Collective on TS 58351c3436cfSJed Brown 58361c3436cfSJed Brown Input Arguments: 58371c3436cfSJed Brown + ts - time stepping context 5838a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5839a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5840a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 58417619abb3SShri 58421c3436cfSJed Brown Output Arguments: 5843a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 58448a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5845a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 5846a4868fbcSLisandro Dalcin 5847a4868fbcSLisandro Dalcin Options Database Keys: 5848a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5849a4868fbcSLisandro Dalcin 58501c3436cfSJed Brown Level: developer 58511c3436cfSJed Brown 58528a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm 58531c3436cfSJed Brown @*/ 58547453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 58551c3436cfSJed Brown { 58568beabaa1SBarry Smith PetscErrorCode ierr; 58571c3436cfSJed Brown 58581c3436cfSJed Brown PetscFunctionBegin; 5859a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 58607453f775SEmil Constantinescu ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 5861a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 58627453f775SEmil Constantinescu ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 5863a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 58641c3436cfSJed Brown PetscFunctionReturn(0); 58651c3436cfSJed Brown } 58661c3436cfSJed Brown 58678a175baeSEmil Constantinescu 58688a175baeSEmil Constantinescu /*@ 58698a175baeSEmil Constantinescu TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances 58708a175baeSEmil Constantinescu 58718a175baeSEmil Constantinescu Collective on TS 58728a175baeSEmil Constantinescu 58738a175baeSEmil Constantinescu Input Arguments: 58748a175baeSEmil Constantinescu + ts - time stepping context 58758a175baeSEmil Constantinescu . E - error vector 58768a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 58778a175baeSEmil Constantinescu - Y - state vector, previous time step 58788a175baeSEmil Constantinescu 58798a175baeSEmil Constantinescu Output Arguments: 5880a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 58818a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5882a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 58838a175baeSEmil Constantinescu 58848a175baeSEmil Constantinescu Level: developer 58858a175baeSEmil Constantinescu 58868a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity() 58878a175baeSEmil Constantinescu @*/ 58888a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 58898a175baeSEmil Constantinescu { 58908a175baeSEmil Constantinescu PetscErrorCode ierr; 58918a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 58928a175baeSEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 58938a175baeSEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 58948a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 58958a175baeSEmil Constantinescu PetscReal err,sum,suma,sumr,gsum,gsuma,gsumr; 58968a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 58978a175baeSEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 58988a175baeSEmil Constantinescu 58998a175baeSEmil Constantinescu PetscFunctionBegin; 59008a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 59018a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 59028a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 59038a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 59048a175baeSEmil Constantinescu PetscValidType(E,2); 59058a175baeSEmil Constantinescu PetscValidType(U,3); 59068a175baeSEmil Constantinescu PetscValidType(Y,4); 59078a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 59088a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 59098a175baeSEmil Constantinescu PetscValidPointer(norm,5); 59108a175baeSEmil Constantinescu PetscValidPointer(norma,6); 59118a175baeSEmil Constantinescu PetscValidPointer(normr,7); 59128a175baeSEmil Constantinescu 59138a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 59148a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 59158a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 59168a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 59178a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 59188a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 59198a175baeSEmil Constantinescu sum = 0.; n_loc = 0; 59208a175baeSEmil Constantinescu suma = 0.; na_loc = 0; 59218a175baeSEmil Constantinescu sumr = 0.; nr_loc = 0; 59228a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { 59238a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 59248a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59258a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59268a175baeSEmil Constantinescu for (i=0; i<n; i++) { 592776cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 59288a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 59298a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 59308a175baeSEmil Constantinescu if(tola>0.){ 59318a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 59328a175baeSEmil Constantinescu na_loc++; 59338a175baeSEmil Constantinescu } 59348a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59358a175baeSEmil Constantinescu if(tolr>0.){ 59368a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 59378a175baeSEmil Constantinescu nr_loc++; 59388a175baeSEmil Constantinescu } 59398a175baeSEmil Constantinescu tol=tola+tolr; 59408a175baeSEmil Constantinescu if(tol>0.){ 59418a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 59428a175baeSEmil Constantinescu n_loc++; 59438a175baeSEmil Constantinescu } 59448a175baeSEmil Constantinescu } 59458a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59468a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59478a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 59488a175baeSEmil Constantinescu const PetscScalar *atol; 59498a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59508a175baeSEmil Constantinescu for (i=0; i<n; i++) { 595176cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 59528a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 59538a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 59548a175baeSEmil Constantinescu if(tola>0.){ 59558a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 59568a175baeSEmil Constantinescu na_loc++; 59578a175baeSEmil Constantinescu } 59588a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59598a175baeSEmil Constantinescu if(tolr>0.){ 59608a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 59618a175baeSEmil Constantinescu nr_loc++; 59628a175baeSEmil Constantinescu } 59638a175baeSEmil Constantinescu tol=tola+tolr; 59648a175baeSEmil Constantinescu if(tol>0.){ 59658a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 59668a175baeSEmil Constantinescu n_loc++; 59678a175baeSEmil Constantinescu } 59688a175baeSEmil Constantinescu } 59698a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59708a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 59718a175baeSEmil Constantinescu const PetscScalar *rtol; 59728a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59738a175baeSEmil Constantinescu for (i=0; i<n; i++) { 597476cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 59758a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 59768a175baeSEmil Constantinescu tola = ts->atol; 59778a175baeSEmil Constantinescu if(tola>0.){ 59788a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 59798a175baeSEmil Constantinescu na_loc++; 59808a175baeSEmil Constantinescu } 59818a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59828a175baeSEmil Constantinescu if(tolr>0.){ 59838a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 59848a175baeSEmil Constantinescu nr_loc++; 59858a175baeSEmil Constantinescu } 59868a175baeSEmil Constantinescu tol=tola+tolr; 59878a175baeSEmil Constantinescu if(tol>0.){ 59888a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 59898a175baeSEmil Constantinescu n_loc++; 59908a175baeSEmil Constantinescu } 59918a175baeSEmil Constantinescu } 59928a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59938a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 59948a175baeSEmil Constantinescu for (i=0; i<n; i++) { 599576cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 59968a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 59978a175baeSEmil Constantinescu tola = ts->atol; 59988a175baeSEmil Constantinescu if(tola>0.){ 59998a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 60008a175baeSEmil Constantinescu na_loc++; 60018a175baeSEmil Constantinescu } 60028a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 60038a175baeSEmil Constantinescu if(tolr>0.){ 60048a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 60058a175baeSEmil Constantinescu nr_loc++; 60068a175baeSEmil Constantinescu } 60078a175baeSEmil Constantinescu tol=tola+tolr; 60088a175baeSEmil Constantinescu if(tol>0.){ 60098a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 60108a175baeSEmil Constantinescu n_loc++; 60118a175baeSEmil Constantinescu } 60128a175baeSEmil Constantinescu } 60138a175baeSEmil Constantinescu } 60148a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 60158a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 60168a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 60178a175baeSEmil Constantinescu 60188a175baeSEmil Constantinescu err_loc[0] = sum; 60198a175baeSEmil Constantinescu err_loc[1] = suma; 60208a175baeSEmil Constantinescu err_loc[2] = sumr; 60218a175baeSEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 60228a175baeSEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 60238a175baeSEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 60248a175baeSEmil Constantinescu 6025a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 60268a175baeSEmil Constantinescu 60278a175baeSEmil Constantinescu gsum = err_glb[0]; 60288a175baeSEmil Constantinescu gsuma = err_glb[1]; 60298a175baeSEmil Constantinescu gsumr = err_glb[2]; 60308a175baeSEmil Constantinescu n_glb = err_glb[3]; 60318a175baeSEmil Constantinescu na_glb = err_glb[4]; 60328a175baeSEmil Constantinescu nr_glb = err_glb[5]; 60338a175baeSEmil Constantinescu 60348a175baeSEmil Constantinescu *norm = 0.; 60358a175baeSEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 60368a175baeSEmil Constantinescu *norma = 0.; 60378a175baeSEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 60388a175baeSEmil Constantinescu *normr = 0.; 60398a175baeSEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 60408a175baeSEmil Constantinescu 60418a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 60428a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 60438a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 60448a175baeSEmil Constantinescu PetscFunctionReturn(0); 60458a175baeSEmil Constantinescu } 60468a175baeSEmil Constantinescu 60478a175baeSEmil Constantinescu /*@ 60488a175baeSEmil Constantinescu TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances 60498a175baeSEmil Constantinescu Collective on TS 60508a175baeSEmil Constantinescu 60518a175baeSEmil Constantinescu Input Arguments: 60528a175baeSEmil Constantinescu + ts - time stepping context 60538a175baeSEmil Constantinescu . E - error vector 60548a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 60558a175baeSEmil Constantinescu - Y - state vector, previous time step 60568a175baeSEmil Constantinescu 60578a175baeSEmil Constantinescu Output Arguments: 6058a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 60598a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 6060a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 60618a175baeSEmil Constantinescu 60628a175baeSEmil Constantinescu Level: developer 60638a175baeSEmil Constantinescu 60648a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2() 60658a175baeSEmil Constantinescu @*/ 60668a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 60678a175baeSEmil Constantinescu { 60688a175baeSEmil Constantinescu PetscErrorCode ierr; 60698a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 60708a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 60718a175baeSEmil Constantinescu PetscReal err,max,gmax,maxa,gmaxa,maxr,gmaxr; 60728a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 60738a175baeSEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 60748a175baeSEmil Constantinescu 60758a175baeSEmil Constantinescu PetscFunctionBegin; 60768a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 60778a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 60788a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 60798a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 60808a175baeSEmil Constantinescu PetscValidType(E,2); 60818a175baeSEmil Constantinescu PetscValidType(U,3); 60828a175baeSEmil Constantinescu PetscValidType(Y,4); 60838a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 60848a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 60858a175baeSEmil Constantinescu PetscValidPointer(norm,5); 60868a175baeSEmil Constantinescu PetscValidPointer(norma,6); 60878a175baeSEmil Constantinescu PetscValidPointer(normr,7); 60888a175baeSEmil Constantinescu 60898a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 60908a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 60918a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 60928a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 60938a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 60948a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 60958a175baeSEmil Constantinescu 60968a175baeSEmil Constantinescu max=0.; 60978a175baeSEmil Constantinescu maxa=0.; 60988a175baeSEmil Constantinescu maxr=0.; 60998a175baeSEmil Constantinescu 61008a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 61018a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 61028a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61038a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61048a175baeSEmil Constantinescu 61058a175baeSEmil Constantinescu for (i=0; i<n; i++) { 610676cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 61078a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 61088a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 61098a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61108a175baeSEmil Constantinescu tol = tola+tolr; 61118a175baeSEmil Constantinescu if(tola>0.){ 61128a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 61138a175baeSEmil Constantinescu } 61148a175baeSEmil Constantinescu if(tolr>0.){ 61158a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 61168a175baeSEmil Constantinescu } 61178a175baeSEmil Constantinescu if(tol>0.){ 61188a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 61198a175baeSEmil Constantinescu } 61208a175baeSEmil Constantinescu } 61218a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61228a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61238a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 61248a175baeSEmil Constantinescu const PetscScalar *atol; 61258a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61268a175baeSEmil Constantinescu for (i=0; i<n; i++) { 612776cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 61288a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 61298a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 61308a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61318a175baeSEmil Constantinescu tol = tola+tolr; 61328a175baeSEmil Constantinescu if(tola>0.){ 61338a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 61348a175baeSEmil Constantinescu } 61358a175baeSEmil Constantinescu if(tolr>0.){ 61368a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 61378a175baeSEmil Constantinescu } 61388a175baeSEmil Constantinescu if(tol>0.){ 61398a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 61408a175baeSEmil Constantinescu } 61418a175baeSEmil Constantinescu } 61428a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 61438a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 61448a175baeSEmil Constantinescu const PetscScalar *rtol; 61458a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61468a175baeSEmil Constantinescu 61478a175baeSEmil Constantinescu for (i=0; i<n; i++) { 614876cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 61498a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 61508a175baeSEmil Constantinescu tola = ts->atol; 61518a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61528a175baeSEmil Constantinescu tol = tola+tolr; 61538a175baeSEmil Constantinescu if(tola>0.){ 61548a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 61558a175baeSEmil Constantinescu } 61568a175baeSEmil Constantinescu if(tolr>0.){ 61578a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 61588a175baeSEmil Constantinescu } 61598a175baeSEmil Constantinescu if(tol>0.){ 61608a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 61618a175baeSEmil Constantinescu } 61628a175baeSEmil Constantinescu } 61638a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 61648a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 61658a175baeSEmil Constantinescu 61668a175baeSEmil Constantinescu for (i=0; i<n; i++) { 616776cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 61688a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 61698a175baeSEmil Constantinescu tola = ts->atol; 61708a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 61718a175baeSEmil Constantinescu tol = tola+tolr; 61728a175baeSEmil Constantinescu if(tola>0.){ 61738a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 61748a175baeSEmil Constantinescu } 61758a175baeSEmil Constantinescu if(tolr>0.){ 61768a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 61778a175baeSEmil Constantinescu } 61788a175baeSEmil Constantinescu if(tol>0.){ 61798a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 61808a175baeSEmil Constantinescu } 61818a175baeSEmil Constantinescu } 61828a175baeSEmil Constantinescu } 61838a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 61848a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 61858a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 61868a175baeSEmil Constantinescu err_loc[0] = max; 61878a175baeSEmil Constantinescu err_loc[1] = maxa; 61888a175baeSEmil Constantinescu err_loc[2] = maxr; 6189a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 61908a175baeSEmil Constantinescu gmax = err_glb[0]; 61918a175baeSEmil Constantinescu gmaxa = err_glb[1]; 61928a175baeSEmil Constantinescu gmaxr = err_glb[2]; 61938a175baeSEmil Constantinescu 61948a175baeSEmil Constantinescu *norm = gmax; 61958a175baeSEmil Constantinescu *norma = gmaxa; 61968a175baeSEmil Constantinescu *normr = gmaxr; 61978a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 61988a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 61998a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 62008a175baeSEmil Constantinescu PetscFunctionReturn(0); 62018a175baeSEmil Constantinescu } 62028a175baeSEmil Constantinescu 62038a175baeSEmil Constantinescu /*@ 62048a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 62058a175baeSEmil Constantinescu 62068a175baeSEmil Constantinescu Collective on TS 62078a175baeSEmil Constantinescu 62088a175baeSEmil Constantinescu Input Arguments: 62098a175baeSEmil Constantinescu + ts - time stepping context 62108a175baeSEmil Constantinescu . E - error vector 62118a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 62128a175baeSEmil Constantinescu . Y - state vector, previous time step 62138a175baeSEmil Constantinescu - wnormtype - norm type, either NORM_2 or NORM_INFINITY 62148a175baeSEmil Constantinescu 62158a175baeSEmil Constantinescu Output Arguments: 6216a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 62178a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 6218a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 62198a175baeSEmil Constantinescu 62208a175baeSEmil Constantinescu Options Database Keys: 62218a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 62228a175baeSEmil Constantinescu 62238a175baeSEmil Constantinescu Level: developer 62248a175baeSEmil Constantinescu 62258a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 62268a175baeSEmil Constantinescu @*/ 62278a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 62288a175baeSEmil Constantinescu { 62298a175baeSEmil Constantinescu PetscErrorCode ierr; 62308a175baeSEmil Constantinescu 62318a175baeSEmil Constantinescu PetscFunctionBegin; 62328a175baeSEmil Constantinescu if (wnormtype == NORM_2) { 62338a175baeSEmil Constantinescu ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 62348a175baeSEmil Constantinescu } else if(wnormtype == NORM_INFINITY) { 62358a175baeSEmil Constantinescu ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 62368a175baeSEmil Constantinescu } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 62378a175baeSEmil Constantinescu PetscFunctionReturn(0); 62388a175baeSEmil Constantinescu } 62398a175baeSEmil Constantinescu 62408a175baeSEmil Constantinescu 62418d59e960SJed Brown /*@ 62428d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 62438d59e960SJed Brown 62448d59e960SJed Brown Logically Collective on TS 62458d59e960SJed Brown 62468d59e960SJed Brown Input Arguments: 62478d59e960SJed Brown + ts - time stepping context 62488d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 62498d59e960SJed Brown 62508d59e960SJed Brown Note: 62518d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 62528d59e960SJed Brown 62538d59e960SJed Brown Level: intermediate 62548d59e960SJed Brown 62558d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 62568d59e960SJed Brown @*/ 62578d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 62588d59e960SJed Brown { 62598d59e960SJed Brown PetscFunctionBegin; 62608d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 62618d59e960SJed Brown ts->cfltime_local = cfltime; 62628d59e960SJed Brown ts->cfltime = -1.; 62638d59e960SJed Brown PetscFunctionReturn(0); 62648d59e960SJed Brown } 62658d59e960SJed Brown 62668d59e960SJed Brown /*@ 62678d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 62688d59e960SJed Brown 62698d59e960SJed Brown Collective on TS 62708d59e960SJed Brown 62718d59e960SJed Brown Input Arguments: 62728d59e960SJed Brown . ts - time stepping context 62738d59e960SJed Brown 62748d59e960SJed Brown Output Arguments: 62758d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 62768d59e960SJed Brown 62778d59e960SJed Brown Level: advanced 62788d59e960SJed Brown 62798d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 62808d59e960SJed Brown @*/ 62818d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 62828d59e960SJed Brown { 62838d59e960SJed Brown PetscErrorCode ierr; 62848d59e960SJed Brown 62858d59e960SJed Brown PetscFunctionBegin; 62868d59e960SJed Brown if (ts->cfltime < 0) { 6287b2566f29SBarry Smith ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 62888d59e960SJed Brown } 62898d59e960SJed Brown *cfltime = ts->cfltime; 62908d59e960SJed Brown PetscFunctionReturn(0); 62918d59e960SJed Brown } 62928d59e960SJed Brown 6293d6ebe24aSShri Abhyankar /*@ 6294d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 6295d6ebe24aSShri Abhyankar 6296d6ebe24aSShri Abhyankar Input Parameters: 6297a2b725a8SWilliam Gropp + ts - the TS context. 6298d6ebe24aSShri Abhyankar . xl - lower bound. 6299a2b725a8SWilliam Gropp - xu - upper bound. 6300d6ebe24aSShri Abhyankar 6301d6ebe24aSShri Abhyankar Notes: 6302d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 6303e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 6304d6ebe24aSShri Abhyankar 63052bd2b0e6SSatish Balay Level: advanced 63062bd2b0e6SSatish Balay 6307d6ebe24aSShri Abhyankar @*/ 6308d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 6309d6ebe24aSShri Abhyankar { 6310d6ebe24aSShri Abhyankar PetscErrorCode ierr; 6311d6ebe24aSShri Abhyankar SNES snes; 6312d6ebe24aSShri Abhyankar 6313d6ebe24aSShri Abhyankar PetscFunctionBegin; 6314d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 6315d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 6316d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 6317d6ebe24aSShri Abhyankar } 6318d6ebe24aSShri Abhyankar 6319b3603a34SBarry Smith /*@C 63204f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 6321b3603a34SBarry Smith in a time based line graph 6322b3603a34SBarry Smith 6323b3603a34SBarry Smith Collective on TS 6324b3603a34SBarry Smith 6325b3603a34SBarry Smith Input Parameters: 6326b3603a34SBarry Smith + ts - the TS context 6327b3603a34SBarry Smith . step - current time-step 6328b3603a34SBarry Smith . ptime - current time 63297db568b7SBarry Smith . u - current solution 63307db568b7SBarry Smith - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 6331b3603a34SBarry Smith 6332b3d3934dSBarry Smith Options Database: 63339ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 6334b3d3934dSBarry Smith 6335b3603a34SBarry Smith Level: intermediate 6336b3603a34SBarry Smith 633795452b02SPatrick Sanan Notes: 633895452b02SPatrick Sanan Each process in a parallel run displays its component solutions in a separate window 6339b3603a34SBarry Smith 63407db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 63417db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 63427db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 63437db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 6344b3603a34SBarry Smith @*/ 63457db568b7SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6346b3603a34SBarry Smith { 6347b3603a34SBarry Smith PetscErrorCode ierr; 63487db568b7SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 6349b3603a34SBarry Smith const PetscScalar *yy; 635080666b62SBarry Smith Vec v; 6351b3603a34SBarry Smith 6352b3603a34SBarry Smith PetscFunctionBegin; 635363e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 635458ff32f7SBarry Smith if (!step) { 6355a9f9c1f6SBarry Smith PetscDrawAxis axis; 63566934998bSLisandro Dalcin PetscInt dim; 6357a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 63580ec8ee2bSJoseph Pusztay ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 6359bab0a581SBarry Smith if (!ctx->names) { 6360bab0a581SBarry Smith PetscBool flg; 6361bab0a581SBarry Smith /* user provides names of variables to plot but no names has been set so assume names are integer values */ 6362bab0a581SBarry Smith ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr); 6363bab0a581SBarry Smith if (flg) { 6364bab0a581SBarry Smith PetscInt i,n; 6365bab0a581SBarry Smith char **names; 6366bab0a581SBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 6367bab0a581SBarry Smith ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr); 6368bab0a581SBarry Smith for (i=0; i<n; i++) { 6369bab0a581SBarry Smith ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr); 6370bab0a581SBarry Smith ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr); 6371bab0a581SBarry Smith } 6372bab0a581SBarry Smith names[n] = NULL; 6373bab0a581SBarry Smith ctx->names = names; 6374bab0a581SBarry Smith } 6375bab0a581SBarry Smith } 6376387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 6377387f4636SBarry Smith char **displaynames; 6378387f4636SBarry Smith PetscBool flg; 6379387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6380580bdb30SBarry Smith ierr = PetscCalloc1(dim+1,&displaynames);CHKERRQ(ierr); 6381c5929fdfSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 6382387f4636SBarry Smith if (flg) { 6383a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 6384387f4636SBarry Smith } 6385387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 6386387f4636SBarry Smith } 6387387f4636SBarry Smith if (ctx->displaynames) { 6388387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 6389387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 6390387f4636SBarry Smith } else if (ctx->names) { 63910910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 63920b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6393387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 6394b0bc92c6SBarry Smith } else { 6395b0bc92c6SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6396b0bc92c6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6397387f4636SBarry Smith } 63980b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 639958ff32f7SBarry Smith } 64006934998bSLisandro Dalcin 64016934998bSLisandro Dalcin if (!ctx->transform) v = u; 64026934998bSLisandro Dalcin else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);} 640380666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 64046934998bSLisandro Dalcin if (ctx->displaynames) { 64056934998bSLisandro Dalcin PetscInt i; 64066934998bSLisandro Dalcin for (i=0; i<ctx->ndisplayvariables; i++) 64076934998bSLisandro Dalcin ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]); 64086934998bSLisandro Dalcin ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 64096934998bSLisandro Dalcin } else { 6410e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6411e3efe391SJed Brown PetscInt i,n; 64126934998bSLisandro Dalcin PetscReal *yreal; 641380666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 6414785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6415e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 64160b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6417e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6418e3efe391SJed Brown #else 64190b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6420e3efe391SJed Brown #endif 642180666b62SBarry Smith } 64226934998bSLisandro Dalcin ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 64236934998bSLisandro Dalcin if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);} 64246934998bSLisandro Dalcin 6425b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 64260b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 64276934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 64283923b477SBarry Smith } 6429b3603a34SBarry Smith PetscFunctionReturn(0); 6430b3603a34SBarry Smith } 6431b3603a34SBarry Smith 6432b037adc7SBarry Smith /*@C 643331152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6434b037adc7SBarry Smith 6435b037adc7SBarry Smith Collective on TS 6436b037adc7SBarry Smith 6437b037adc7SBarry Smith Input Parameters: 6438b037adc7SBarry Smith + ts - the TS context 6439b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 6440b037adc7SBarry Smith 6441b037adc7SBarry Smith Level: intermediate 6442b037adc7SBarry Smith 644395452b02SPatrick Sanan Notes: 644495452b02SPatrick Sanan If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 64457db568b7SBarry Smith 6446a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 6447b037adc7SBarry Smith @*/ 644831152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 6449b037adc7SBarry Smith { 6450b037adc7SBarry Smith PetscErrorCode ierr; 6451b037adc7SBarry Smith PetscInt i; 6452b037adc7SBarry Smith 6453b037adc7SBarry Smith PetscFunctionBegin; 6454b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6455b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 64565537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 6457b3d3934dSBarry Smith break; 6458b3d3934dSBarry Smith } 6459b3d3934dSBarry Smith } 6460b3d3934dSBarry Smith PetscFunctionReturn(0); 6461b3d3934dSBarry Smith } 6462b3d3934dSBarry Smith 6463e673d494SBarry Smith /*@C 6464e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6465e673d494SBarry Smith 6466e673d494SBarry Smith Collective on TS 6467e673d494SBarry Smith 6468e673d494SBarry Smith Input Parameters: 6469e673d494SBarry Smith + ts - the TS context 6470e673d494SBarry Smith - names - the names of the components, final string must be NULL 6471e673d494SBarry Smith 6472e673d494SBarry Smith Level: intermediate 6473e673d494SBarry Smith 6474a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 6475e673d494SBarry Smith @*/ 6476e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 6477e673d494SBarry Smith { 6478e673d494SBarry Smith PetscErrorCode ierr; 6479e673d494SBarry Smith 6480e673d494SBarry Smith PetscFunctionBegin; 6481e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 6482e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 6483e673d494SBarry Smith PetscFunctionReturn(0); 6484e673d494SBarry Smith } 6485e673d494SBarry Smith 6486b3d3934dSBarry Smith /*@C 6487b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 6488b3d3934dSBarry Smith 6489b3d3934dSBarry Smith Collective on TS 6490b3d3934dSBarry Smith 6491b3d3934dSBarry Smith Input Parameter: 6492b3d3934dSBarry Smith . ts - the TS context 6493b3d3934dSBarry Smith 6494b3d3934dSBarry Smith Output Parameter: 6495b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 6496b3d3934dSBarry Smith 6497b3d3934dSBarry Smith Level: intermediate 6498b3d3934dSBarry Smith 649995452b02SPatrick Sanan Notes: 650095452b02SPatrick Sanan If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 65017db568b7SBarry Smith 6502b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6503b3d3934dSBarry Smith @*/ 6504b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 6505b3d3934dSBarry Smith { 6506b3d3934dSBarry Smith PetscInt i; 6507b3d3934dSBarry Smith 6508b3d3934dSBarry Smith PetscFunctionBegin; 6509b3d3934dSBarry Smith *names = NULL; 6510b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6511b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 65125537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 6513b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 6514b3d3934dSBarry Smith break; 6515387f4636SBarry Smith } 6516387f4636SBarry Smith } 6517387f4636SBarry Smith PetscFunctionReturn(0); 6518387f4636SBarry Smith } 6519387f4636SBarry Smith 6520a66092f1SBarry Smith /*@C 6521a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 6522a66092f1SBarry Smith 6523a66092f1SBarry Smith Collective on TS 6524a66092f1SBarry Smith 6525a66092f1SBarry Smith Input Parameters: 6526a66092f1SBarry Smith + ctx - the TSMonitorLG context 6527a2b725a8SWilliam Gropp - displaynames - the names of the components, final string must be NULL 6528a66092f1SBarry Smith 6529a66092f1SBarry Smith Level: intermediate 6530a66092f1SBarry Smith 6531a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6532a66092f1SBarry Smith @*/ 6533a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 6534a66092f1SBarry Smith { 6535a66092f1SBarry Smith PetscInt j = 0,k; 6536a66092f1SBarry Smith PetscErrorCode ierr; 6537a66092f1SBarry Smith 6538a66092f1SBarry Smith PetscFunctionBegin; 6539a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 6540a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 6541a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 6542a66092f1SBarry Smith while (displaynames[j]) j++; 6543a66092f1SBarry Smith ctx->ndisplayvariables = j; 6544a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 6545a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 6546a66092f1SBarry Smith j = 0; 6547a66092f1SBarry Smith while (displaynames[j]) { 6548a66092f1SBarry Smith k = 0; 6549a66092f1SBarry Smith while (ctx->names[k]) { 6550a66092f1SBarry Smith PetscBool flg; 6551a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 6552a66092f1SBarry Smith if (flg) { 6553a66092f1SBarry Smith ctx->displayvariables[j] = k; 6554a66092f1SBarry Smith break; 6555a66092f1SBarry Smith } 6556a66092f1SBarry Smith k++; 6557a66092f1SBarry Smith } 6558a66092f1SBarry Smith j++; 6559a66092f1SBarry Smith } 6560a66092f1SBarry Smith PetscFunctionReturn(0); 6561a66092f1SBarry Smith } 6562a66092f1SBarry Smith 6563387f4636SBarry Smith /*@C 6564387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 6565387f4636SBarry Smith 6566387f4636SBarry Smith Collective on TS 6567387f4636SBarry Smith 6568387f4636SBarry Smith Input Parameters: 6569387f4636SBarry Smith + ts - the TS context 6570a2b725a8SWilliam Gropp - displaynames - the names of the components, final string must be NULL 6571387f4636SBarry Smith 657295452b02SPatrick Sanan Notes: 657395452b02SPatrick Sanan If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 65747db568b7SBarry Smith 6575387f4636SBarry Smith Level: intermediate 6576387f4636SBarry Smith 6577387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6578387f4636SBarry Smith @*/ 6579387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 6580387f4636SBarry Smith { 6581a66092f1SBarry Smith PetscInt i; 6582387f4636SBarry Smith PetscErrorCode ierr; 6583387f4636SBarry Smith 6584387f4636SBarry Smith PetscFunctionBegin; 6585387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6586387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 65875537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 6588b3d3934dSBarry Smith break; 6589b037adc7SBarry Smith } 6590b037adc7SBarry Smith } 6591b037adc7SBarry Smith PetscFunctionReturn(0); 6592b037adc7SBarry Smith } 6593b037adc7SBarry Smith 659480666b62SBarry Smith /*@C 659580666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 659680666b62SBarry Smith 659780666b62SBarry Smith Collective on TS 659880666b62SBarry Smith 659980666b62SBarry Smith Input Parameters: 660080666b62SBarry Smith + ts - the TS context 660180666b62SBarry Smith . transform - the transform function 66027684fa3eSBarry Smith . destroy - function to destroy the optional context 660380666b62SBarry Smith - ctx - optional context used by transform function 660480666b62SBarry Smith 660595452b02SPatrick Sanan Notes: 660695452b02SPatrick Sanan If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 66077db568b7SBarry Smith 660880666b62SBarry Smith Level: intermediate 660980666b62SBarry Smith 6610a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 661180666b62SBarry Smith @*/ 66127684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 661380666b62SBarry Smith { 661480666b62SBarry Smith PetscInt i; 6615a66092f1SBarry Smith PetscErrorCode ierr; 661680666b62SBarry Smith 661780666b62SBarry Smith PetscFunctionBegin; 661880666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 661980666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 66205537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 662180666b62SBarry Smith } 662280666b62SBarry Smith } 662380666b62SBarry Smith PetscFunctionReturn(0); 662480666b62SBarry Smith } 662580666b62SBarry Smith 6626e673d494SBarry Smith /*@C 6627e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 6628e673d494SBarry Smith 6629e673d494SBarry Smith Collective on TSLGCtx 6630e673d494SBarry Smith 6631e673d494SBarry Smith Input Parameters: 6632e673d494SBarry Smith + ts - the TS context 6633e673d494SBarry Smith . transform - the transform function 66347684fa3eSBarry Smith . destroy - function to destroy the optional context 6635e673d494SBarry Smith - ctx - optional context used by transform function 6636e673d494SBarry Smith 6637e673d494SBarry Smith Level: intermediate 6638e673d494SBarry Smith 6639a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 6640e673d494SBarry Smith @*/ 66417684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 6642e673d494SBarry Smith { 6643e673d494SBarry Smith PetscFunctionBegin; 6644e673d494SBarry Smith ctx->transform = transform; 66457684fa3eSBarry Smith ctx->transformdestroy = destroy; 6646e673d494SBarry Smith ctx->transformctx = tctx; 6647e673d494SBarry Smith PetscFunctionReturn(0); 6648e673d494SBarry Smith } 6649e673d494SBarry Smith 6650ef20d060SBarry Smith /*@C 66518b668821SLisandro Dalcin TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error 6652ef20d060SBarry Smith in a time based line graph 6653ef20d060SBarry Smith 6654ef20d060SBarry Smith Collective on TS 6655ef20d060SBarry Smith 6656ef20d060SBarry Smith Input Parameters: 6657ef20d060SBarry Smith + ts - the TS context 6658ef20d060SBarry Smith . step - current time-step 6659ef20d060SBarry Smith . ptime - current time 66607db568b7SBarry Smith . u - current solution 66617db568b7SBarry Smith - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 6662ef20d060SBarry Smith 6663ef20d060SBarry Smith Level: intermediate 6664ef20d060SBarry Smith 666595452b02SPatrick Sanan Notes: 666695452b02SPatrick Sanan Each process in a parallel run displays its component errors in a separate window 6667abd5a294SJed Brown 6668abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 6669abd5a294SJed Brown 6670abd5a294SJed Brown Options Database Keys: 66714f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 6672ef20d060SBarry Smith 6673abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 6674ef20d060SBarry Smith @*/ 66750910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 6676ef20d060SBarry Smith { 6677ef20d060SBarry Smith PetscErrorCode ierr; 66780b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 6679ef20d060SBarry Smith const PetscScalar *yy; 6680ef20d060SBarry Smith Vec y; 6681ef20d060SBarry Smith 6682ef20d060SBarry Smith PetscFunctionBegin; 6683a9f9c1f6SBarry Smith if (!step) { 6684a9f9c1f6SBarry Smith PetscDrawAxis axis; 66856934998bSLisandro Dalcin PetscInt dim; 6686a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 66878b668821SLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr); 66880910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6689a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6690a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6691a9f9c1f6SBarry Smith } 66920910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 6693ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 66940910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 6695ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 6696e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6697e3efe391SJed Brown { 6698e3efe391SJed Brown PetscReal *yreal; 6699e3efe391SJed Brown PetscInt i,n; 6700e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 6701785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6702e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 67030b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6704e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6705e3efe391SJed Brown } 6706e3efe391SJed Brown #else 67070b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6708e3efe391SJed Brown #endif 6709ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 6710ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 6711b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 67120b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 67136934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 67143923b477SBarry Smith } 6715ef20d060SBarry Smith PetscFunctionReturn(0); 6716ef20d060SBarry Smith } 6717ef20d060SBarry Smith 67185e3b7effSJoseph Pusztay /*@C 67195e3b7effSJoseph Pusztay TSMonitorSPSwarmSolution - Graphically displays phase plots of DMSwarm particles on a scatter plot 67205e3b7effSJoseph Pusztay 67215e3b7effSJoseph Pusztay Input Parameters: 67225e3b7effSJoseph Pusztay + ts - the TS context 67235e3b7effSJoseph Pusztay . step - current time-step 67245e3b7effSJoseph Pusztay . ptime - current time 67255e3b7effSJoseph Pusztay . u - current solution 67265e3b7effSJoseph Pusztay - dctx - the TSMonitorSPCtx object that contains all the options for the monitoring, this is created with TSMonitorSPCtxCreate() 67275e3b7effSJoseph Pusztay 67285e3b7effSJoseph Pusztay Options Database: 6729918b1d10SJoseph Pusztay . -ts_monitor_sp_swarm 67305e3b7effSJoseph Pusztay 67315e3b7effSJoseph Pusztay Level: intermediate 67325e3b7effSJoseph Pusztay 67335e3b7effSJoseph Pusztay @*/ 67340ec8ee2bSJoseph Pusztay PetscErrorCode TSMonitorSPSwarmSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 67351b575b74SJoseph Pusztay { 67361b575b74SJoseph Pusztay PetscErrorCode ierr; 67371b575b74SJoseph Pusztay TSMonitorSPCtx ctx = (TSMonitorSPCtx)dctx; 67381b575b74SJoseph Pusztay const PetscScalar *yy; 6739b1670a61SJoseph Pusztay PetscReal *y,*x; 67401b575b74SJoseph Pusztay PetscInt Np, p, dim=2; 67411b575b74SJoseph Pusztay DM dm; 67421b575b74SJoseph Pusztay 67431b575b74SJoseph Pusztay PetscFunctionBegin; 67441b575b74SJoseph Pusztay 67451b575b74SJoseph Pusztay if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 67461b575b74SJoseph Pusztay if (!step) { 67471b575b74SJoseph Pusztay PetscDrawAxis axis; 67481b575b74SJoseph Pusztay ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr); 67491b575b74SJoseph Pusztay ierr = PetscDrawAxisSetLabels(axis,"Particles","X","Y");CHKERRQ(ierr); 6750895f37d5SJoseph Pusztay ierr = PetscDrawAxisSetLimits(axis, -5, 5, -5, 5);CHKERRQ(ierr); 6751895f37d5SJoseph Pusztay ierr = PetscDrawAxisSetHoldLimits(axis, PETSC_TRUE);CHKERRQ(ierr); 67521b575b74SJoseph Pusztay ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 67531b575b74SJoseph Pusztay ierr = DMGetDimension(dm, &dim); 67541b575b74SJoseph Pusztay if(dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");CHKERRQ(ierr); 67551b575b74SJoseph Pusztay ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr); 67561b575b74SJoseph Pusztay Np /= 2*dim; 67571b575b74SJoseph Pusztay ierr = PetscDrawSPSetDimension(ctx->sp, Np);CHKERRQ(ierr); 67581b575b74SJoseph Pusztay ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr); 67591b575b74SJoseph Pusztay } 67601b575b74SJoseph Pusztay 67611b575b74SJoseph Pusztay ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr); 67621b575b74SJoseph Pusztay Np /= 2*dim; 67631b575b74SJoseph Pusztay ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr); 6764895f37d5SJoseph Pusztay ierr = PetscMalloc2(Np, &x, Np, &y);CHKERRQ(ierr); 67651b575b74SJoseph Pusztay /* get points from solution vector */ 67661b575b74SJoseph Pusztay for (p=0; p<Np; ++p){ 6767b1670a61SJoseph Pusztay x[p] = PetscRealPart(yy[2*dim*p]); 6768b1670a61SJoseph Pusztay y[p] = PetscRealPart(yy[2*dim*p+1]); 6769895f37d5SJoseph Pusztay } 67701b575b74SJoseph Pusztay ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr); 67711b575b74SJoseph Pusztay 67721b575b74SJoseph Pusztay if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 67731b575b74SJoseph Pusztay ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr); 67741b575b74SJoseph Pusztay ierr = PetscDrawSPDraw(ctx->sp,PETSC_FALSE);CHKERRQ(ierr); 67751b575b74SJoseph Pusztay ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr); 67761b575b74SJoseph Pusztay } 67771b575b74SJoseph Pusztay 6778918b1d10SJoseph Pusztay ierr = PetscFree2(x, y);CHKERRQ(ierr); 6779918b1d10SJoseph Pusztay 67801b575b74SJoseph Pusztay PetscFunctionReturn(0); 67811b575b74SJoseph Pusztay } 67821b575b74SJoseph Pusztay 67831b575b74SJoseph Pusztay 67841b575b74SJoseph Pusztay 67857cf37e64SBarry Smith /*@C 67867cf37e64SBarry Smith TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep 67877cf37e64SBarry Smith 67887cf37e64SBarry Smith Collective on TS 67897cf37e64SBarry Smith 67907cf37e64SBarry Smith Input Parameters: 67917cf37e64SBarry Smith + ts - the TS context 67927cf37e64SBarry Smith . step - current time-step 67937cf37e64SBarry Smith . ptime - current time 67947cf37e64SBarry Smith . u - current solution 67957cf37e64SBarry Smith - dctx - unused context 67967cf37e64SBarry Smith 67977cf37e64SBarry Smith Level: intermediate 67987cf37e64SBarry Smith 67997cf37e64SBarry Smith The user must provide the solution using TSSetSolutionFunction() to use this monitor. 68007cf37e64SBarry Smith 68017cf37e64SBarry Smith Options Database Keys: 68027cf37e64SBarry Smith . -ts_monitor_error - create a graphical monitor of error history 68037cf37e64SBarry Smith 68047cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 68057cf37e64SBarry Smith @*/ 6806edbaebb3SBarry Smith PetscErrorCode TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf) 68077cf37e64SBarry Smith { 68087cf37e64SBarry Smith PetscErrorCode ierr; 68097cf37e64SBarry Smith Vec y; 68107cf37e64SBarry Smith PetscReal nrm; 6811edbaebb3SBarry Smith PetscBool flg; 68127cf37e64SBarry Smith 68137cf37e64SBarry Smith PetscFunctionBegin; 68147cf37e64SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 68157cf37e64SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 68167cf37e64SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 6817edbaebb3SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr); 6818edbaebb3SBarry Smith if (flg) { 68197cf37e64SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 6820edbaebb3SBarry Smith ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr); 6821edbaebb3SBarry Smith } 6822edbaebb3SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr); 6823edbaebb3SBarry Smith if (flg) { 6824edbaebb3SBarry Smith ierr = VecView(y,vf->viewer);CHKERRQ(ierr); 6825edbaebb3SBarry Smith } 6826edbaebb3SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 68277cf37e64SBarry Smith PetscFunctionReturn(0); 68287cf37e64SBarry Smith } 68297cf37e64SBarry Smith 6830201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6831201da799SBarry Smith { 6832201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6833201da799SBarry Smith PetscReal x = ptime,y; 6834201da799SBarry Smith PetscErrorCode ierr; 6835201da799SBarry Smith PetscInt its; 6836201da799SBarry Smith 6837201da799SBarry Smith PetscFunctionBegin; 683863e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 6839201da799SBarry Smith if (!n) { 6840201da799SBarry Smith PetscDrawAxis axis; 6841201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6842201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 6843201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6844201da799SBarry Smith ctx->snes_its = 0; 6845201da799SBarry Smith } 6846201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 6847201da799SBarry Smith y = its - ctx->snes_its; 6848201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 68493fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6850201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 68516934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 6852201da799SBarry Smith } 6853201da799SBarry Smith ctx->snes_its = its; 6854201da799SBarry Smith PetscFunctionReturn(0); 6855201da799SBarry Smith } 6856201da799SBarry Smith 6857201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6858201da799SBarry Smith { 6859201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6860201da799SBarry Smith PetscReal x = ptime,y; 6861201da799SBarry Smith PetscErrorCode ierr; 6862201da799SBarry Smith PetscInt its; 6863201da799SBarry Smith 6864201da799SBarry Smith PetscFunctionBegin; 686563e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 6866201da799SBarry Smith if (!n) { 6867201da799SBarry Smith PetscDrawAxis axis; 6868201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6869201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 6870201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6871201da799SBarry Smith ctx->ksp_its = 0; 6872201da799SBarry Smith } 6873201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 6874201da799SBarry Smith y = its - ctx->ksp_its; 6875201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 687699fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6877201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 68786934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 6879201da799SBarry Smith } 6880201da799SBarry Smith ctx->ksp_its = its; 6881201da799SBarry Smith PetscFunctionReturn(0); 6882201da799SBarry Smith } 6883f9c1d6abSBarry Smith 6884f9c1d6abSBarry Smith /*@ 6885f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 6886f9c1d6abSBarry Smith 6887d083f849SBarry Smith Collective on TS 6888f9c1d6abSBarry Smith 6889f9c1d6abSBarry Smith Input Parameters: 6890f9c1d6abSBarry Smith + ts - the TS context 6891f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 6892f9c1d6abSBarry Smith 6893f9c1d6abSBarry Smith Output Parameters: 6894f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 6895f9c1d6abSBarry Smith 6896f9c1d6abSBarry Smith Level: developer 6897f9c1d6abSBarry Smith 6898f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 6899f9c1d6abSBarry Smith @*/ 6900f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 6901f9c1d6abSBarry Smith { 6902f9c1d6abSBarry Smith PetscErrorCode ierr; 6903f9c1d6abSBarry Smith 6904f9c1d6abSBarry Smith PetscFunctionBegin; 6905f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6906ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 6907f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 6908f9c1d6abSBarry Smith PetscFunctionReturn(0); 6909f9c1d6abSBarry Smith } 691024655328SShri 6911b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 6912b3d3934dSBarry Smith /*@C 6913b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 6914b3d3934dSBarry Smith 6915b3d3934dSBarry Smith Collective on TS 6916b3d3934dSBarry Smith 6917b3d3934dSBarry Smith Input Parameters: 6918b3d3934dSBarry Smith . ts - the ODE solver object 6919b3d3934dSBarry Smith 6920b3d3934dSBarry Smith Output Parameter: 6921b3d3934dSBarry Smith . ctx - the context 6922b3d3934dSBarry Smith 6923b3d3934dSBarry Smith Level: intermediate 6924b3d3934dSBarry Smith 6925b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 6926b3d3934dSBarry Smith 6927b3d3934dSBarry Smith @*/ 6928b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 6929b3d3934dSBarry Smith { 6930b3d3934dSBarry Smith PetscErrorCode ierr; 6931b3d3934dSBarry Smith 6932b3d3934dSBarry Smith PetscFunctionBegin; 6933a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 6934b3d3934dSBarry Smith PetscFunctionReturn(0); 6935b3d3934dSBarry Smith } 6936b3d3934dSBarry Smith 6937b3d3934dSBarry Smith /*@C 6938b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 6939b3d3934dSBarry Smith 6940b3d3934dSBarry Smith Collective on TS 6941b3d3934dSBarry Smith 6942b3d3934dSBarry Smith Input Parameters: 6943b3d3934dSBarry Smith + ts - the TS context 6944b3d3934dSBarry Smith . step - current time-step 6945b3d3934dSBarry Smith . ptime - current time 69467db568b7SBarry Smith . u - current solution 69477db568b7SBarry Smith - dctx - the envelope context 6948b3d3934dSBarry Smith 6949b3d3934dSBarry Smith Options Database: 6950b3d3934dSBarry Smith . -ts_monitor_envelope 6951b3d3934dSBarry Smith 6952b3d3934dSBarry Smith Level: intermediate 6953b3d3934dSBarry Smith 695495452b02SPatrick Sanan Notes: 695595452b02SPatrick Sanan after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 6956b3d3934dSBarry Smith 69577db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 6958b3d3934dSBarry Smith @*/ 69597db568b7SBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6960b3d3934dSBarry Smith { 6961b3d3934dSBarry Smith PetscErrorCode ierr; 69627db568b7SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 6963b3d3934dSBarry Smith 6964b3d3934dSBarry Smith PetscFunctionBegin; 6965b3d3934dSBarry Smith if (!ctx->max) { 6966b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 6967b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 6968b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 6969b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 6970b3d3934dSBarry Smith } else { 6971b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 6972b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 6973b3d3934dSBarry Smith } 6974b3d3934dSBarry Smith PetscFunctionReturn(0); 6975b3d3934dSBarry Smith } 6976b3d3934dSBarry Smith 6977b3d3934dSBarry Smith /*@C 6978b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 6979b3d3934dSBarry Smith 6980b3d3934dSBarry Smith Collective on TS 6981b3d3934dSBarry Smith 6982b3d3934dSBarry Smith Input Parameter: 6983b3d3934dSBarry Smith . ts - the TS context 6984b3d3934dSBarry Smith 6985b3d3934dSBarry Smith Output Parameter: 6986b3d3934dSBarry Smith + max - the maximum values 6987b3d3934dSBarry Smith - min - the minimum values 6988b3d3934dSBarry Smith 698995452b02SPatrick Sanan Notes: 699095452b02SPatrick Sanan If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 69917db568b7SBarry Smith 6992b3d3934dSBarry Smith Level: intermediate 6993b3d3934dSBarry Smith 6994b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6995b3d3934dSBarry Smith @*/ 6996b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 6997b3d3934dSBarry Smith { 6998b3d3934dSBarry Smith PetscInt i; 6999b3d3934dSBarry Smith 7000b3d3934dSBarry Smith PetscFunctionBegin; 7001b3d3934dSBarry Smith if (max) *max = NULL; 7002b3d3934dSBarry Smith if (min) *min = NULL; 7003b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 7004b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 70055537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 7006b3d3934dSBarry Smith if (max) *max = ctx->max; 7007b3d3934dSBarry Smith if (min) *min = ctx->min; 7008b3d3934dSBarry Smith break; 7009b3d3934dSBarry Smith } 7010b3d3934dSBarry Smith } 7011b3d3934dSBarry Smith PetscFunctionReturn(0); 7012b3d3934dSBarry Smith } 7013b3d3934dSBarry Smith 7014b3d3934dSBarry Smith /*@C 7015b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 7016b3d3934dSBarry Smith 7017b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 7018b3d3934dSBarry Smith 7019b3d3934dSBarry Smith Input Parameter: 7020b3d3934dSBarry Smith . ctx - the monitor context 7021b3d3934dSBarry Smith 7022b3d3934dSBarry Smith Level: intermediate 7023b3d3934dSBarry Smith 70247db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 7025b3d3934dSBarry Smith @*/ 7026b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 7027b3d3934dSBarry Smith { 7028b3d3934dSBarry Smith PetscErrorCode ierr; 7029b3d3934dSBarry Smith 7030b3d3934dSBarry Smith PetscFunctionBegin; 7031b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 7032b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 7033b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 7034b3d3934dSBarry Smith PetscFunctionReturn(0); 7035b3d3934dSBarry Smith } 7036f2dee214SBarry Smith 703724655328SShri /*@ 7038dcb233daSLisandro Dalcin TSRestartStep - Flags the solver to restart the next step 7039dcb233daSLisandro Dalcin 7040dcb233daSLisandro Dalcin Collective on TS 7041dcb233daSLisandro Dalcin 7042dcb233daSLisandro Dalcin Input Parameter: 7043dcb233daSLisandro Dalcin . ts - the TS context obtained from TSCreate() 7044dcb233daSLisandro Dalcin 7045dcb233daSLisandro Dalcin Level: advanced 7046dcb233daSLisandro Dalcin 7047dcb233daSLisandro Dalcin Notes: 7048dcb233daSLisandro Dalcin Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of 7049dcb233daSLisandro Dalcin discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution 7050dcb233daSLisandro Dalcin vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For 7051dcb233daSLisandro Dalcin the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce 7052dcb233daSLisandro Dalcin discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with 7053dcb233daSLisandro Dalcin discontinuous source terms). 7054dcb233daSLisandro Dalcin 7055dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep() 7056dcb233daSLisandro Dalcin @*/ 7057dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts) 7058dcb233daSLisandro Dalcin { 7059dcb233daSLisandro Dalcin PetscFunctionBegin; 7060dcb233daSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7061dcb233daSLisandro Dalcin ts->steprestart = PETSC_TRUE; 7062dcb233daSLisandro Dalcin PetscFunctionReturn(0); 7063dcb233daSLisandro Dalcin } 7064dcb233daSLisandro Dalcin 7065dcb233daSLisandro Dalcin /*@ 706624655328SShri TSRollBack - Rolls back one time step 706724655328SShri 706824655328SShri Collective on TS 706924655328SShri 707024655328SShri Input Parameter: 707124655328SShri . ts - the TS context obtained from TSCreate() 707224655328SShri 707324655328SShri Level: advanced 707424655328SShri 707524655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 707624655328SShri @*/ 707724655328SShri PetscErrorCode TSRollBack(TS ts) 707824655328SShri { 707924655328SShri PetscErrorCode ierr; 708024655328SShri 708124655328SShri PetscFunctionBegin; 708224655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7083b3de5cdeSLisandro Dalcin if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called"); 708424655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 708524655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 708624655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 708724655328SShri ts->ptime = ts->ptime_prev; 7088be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 70892808aa04SLisandro Dalcin ts->steps--; 7090b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 709124655328SShri PetscFunctionReturn(0); 709224655328SShri } 7093aeb4809dSShri Abhyankar 7094ff22ae23SHong Zhang /*@ 7095ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 7096ff22ae23SHong Zhang 7097ff22ae23SHong Zhang Input Parameter: 7098ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 7099ff22ae23SHong Zhang 71000429704eSStefano Zampini Output Parameters: 71010429704eSStefano Zampini + ns - the number of stages 71020429704eSStefano Zampini - Y - the current stage vectors 71030429704eSStefano Zampini 7104ff22ae23SHong Zhang Level: advanced 7105ff22ae23SHong Zhang 71060429704eSStefano Zampini Notes: Both ns and Y can be NULL. 71070429704eSStefano Zampini 7108ff22ae23SHong Zhang .seealso: TSCreate() 7109ff22ae23SHong Zhang @*/ 7110ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns,Vec **Y) 7111ff22ae23SHong Zhang { 7112ff22ae23SHong Zhang PetscErrorCode ierr; 7113ff22ae23SHong Zhang 7114ff22ae23SHong Zhang PetscFunctionBegin; 7115ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 71160429704eSStefano Zampini if (ns) PetscValidPointer(ns,2); 71170429704eSStefano Zampini if (Y) PetscValidPointer(Y,3); 71180429704eSStefano Zampini if (!ts->ops->getstages) { 71190429704eSStefano Zampini if (ns) *ns = 0; 71200429704eSStefano Zampini if (Y) *Y = NULL; 71210429704eSStefano Zampini } else { 7122ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 7123ff22ae23SHong Zhang } 7124ff22ae23SHong Zhang PetscFunctionReturn(0); 7125ff22ae23SHong Zhang } 7126ff22ae23SHong Zhang 7127847ff0e1SMatthew G. Knepley /*@C 7128847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 7129847ff0e1SMatthew G. Knepley 7130847ff0e1SMatthew G. Knepley Collective on SNES 7131847ff0e1SMatthew G. Knepley 7132847ff0e1SMatthew G. Knepley Input Parameters: 7133847ff0e1SMatthew G. Knepley + ts - the TS context 7134847ff0e1SMatthew G. Knepley . t - current timestep 7135847ff0e1SMatthew G. Knepley . U - state vector 7136847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 7137847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 7138847ff0e1SMatthew G. Knepley - ctx - an optional user context 7139847ff0e1SMatthew G. Knepley 7140847ff0e1SMatthew G. Knepley Output Parameters: 7141847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 7142847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 7143847ff0e1SMatthew G. Knepley 7144847ff0e1SMatthew G. Knepley Level: intermediate 7145847ff0e1SMatthew G. Knepley 7146847ff0e1SMatthew G. Knepley Notes: 7147847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 7148847ff0e1SMatthew G. Knepley 7149847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 7150847ff0e1SMatthew G. Knepley 7151847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 7152847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 7153847ff0e1SMatthew G. Knepley 7154847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 7155847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 7156847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 7157847ff0e1SMatthew G. Knepley 7158847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 7159847ff0e1SMatthew G. Knepley @*/ 7160847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 7161847ff0e1SMatthew G. Knepley { 7162847ff0e1SMatthew G. Knepley SNES snes; 7163847ff0e1SMatthew G. Knepley MatFDColoring color; 7164847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 7165847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 7166847ff0e1SMatthew G. Knepley 7167847ff0e1SMatthew G. Knepley PetscFunctionBegin; 7168c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 7169847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 7170847ff0e1SMatthew G. Knepley if (!color) { 7171847ff0e1SMatthew G. Knepley DM dm; 7172847ff0e1SMatthew G. Knepley ISColoring iscoloring; 7173847ff0e1SMatthew G. Knepley 7174847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 7175847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 7176847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 7177847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 7178847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7179847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7180847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7181847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7182847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7183847ff0e1SMatthew G. Knepley } else { 7184847ff0e1SMatthew G. Knepley MatColoring mc; 7185847ff0e1SMatthew G. Knepley 7186847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 7187847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 7188847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 7189847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 7190847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 7191847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 7192847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7193847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7194847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7195847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7196847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7197847ff0e1SMatthew G. Knepley } 7198847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 7199847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 7200847ff0e1SMatthew G. Knepley } 7201847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 7202847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 7203847ff0e1SMatthew G. Knepley if (J != B) { 7204847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7205847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7206847ff0e1SMatthew G. Knepley } 7207847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 7208847ff0e1SMatthew G. Knepley } 720993b34091SDebojyoti Ghosh 7210cb9d8021SPierre Barbier de Reuille /*@ 72116bc98fa9SBarry Smith TSSetFunctionDomainError - Set a function that tests if the current state vector is valid 7212cb9d8021SPierre Barbier de Reuille 7213cb9d8021SPierre Barbier de Reuille Input Parameters: 72146bc98fa9SBarry Smith + ts - the TS context 72156bc98fa9SBarry Smith - func - function called within TSFunctionDomainError 72166bc98fa9SBarry Smith 72176bc98fa9SBarry Smith Calling sequence of func: 72186bc98fa9SBarry Smith $ PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject) 72196bc98fa9SBarry Smith 72206bc98fa9SBarry Smith + ts - the TS context 72216bc98fa9SBarry Smith . time - the current time (of the stage) 72226bc98fa9SBarry Smith . state - the state to check if it is valid 72236bc98fa9SBarry Smith - reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable 7224cb9d8021SPierre Barbier de Reuille 7225cb9d8021SPierre Barbier de Reuille Level: intermediate 7226cb9d8021SPierre Barbier de Reuille 72276bc98fa9SBarry Smith Notes: 72286bc98fa9SBarry Smith If an implicit ODE solver is being used then, in addition to providing this routine, the 72296bc98fa9SBarry Smith user's code should call SNESSetFunctionDomainError() when domain errors occur during 72306bc98fa9SBarry Smith function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction(). 72316bc98fa9SBarry Smith Use TSGetSNES() to obtain the SNES object 72326bc98fa9SBarry Smith 72336bc98fa9SBarry Smith Developer Notes: 72346bc98fa9SBarry Smith The naming of this function is inconsistent with the SNESSetFunctionDomainError() 72356bc98fa9SBarry Smith since one takes a function pointer and the other does not. 72366bc98fa9SBarry Smith 72376bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES() 7238cb9d8021SPierre Barbier de Reuille @*/ 7239cb9d8021SPierre Barbier de Reuille 7240d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 7241cb9d8021SPierre Barbier de Reuille { 7242cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7243cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7244cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 7245cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7246cb9d8021SPierre Barbier de Reuille } 7247cb9d8021SPierre Barbier de Reuille 7248cb9d8021SPierre Barbier de Reuille /*@ 72496bc98fa9SBarry Smith TSFunctionDomainError - Checks if the current state is valid 7250cb9d8021SPierre Barbier de Reuille 7251cb9d8021SPierre Barbier de Reuille Input Parameters: 72526bc98fa9SBarry Smith + ts - the TS context 72536bc98fa9SBarry Smith . stagetime - time of the simulation 72546bc98fa9SBarry Smith - Y - state vector to check. 7255cb9d8021SPierre Barbier de Reuille 7256cb9d8021SPierre Barbier de Reuille Output Parameter: 72576bc98fa9SBarry Smith . accept - Set to PETSC_FALSE if the current state vector is valid. 7258cb9d8021SPierre Barbier de Reuille 7259cb9d8021SPierre Barbier de Reuille Note: 72606bc98fa9SBarry Smith This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError()) 72616bc98fa9SBarry Smith to check if the current state is valid. 726296a0c994SBarry Smith 72636bc98fa9SBarry Smith Level: developer 72646bc98fa9SBarry Smith 72656bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError() 7266cb9d8021SPierre Barbier de Reuille @*/ 7267d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 7268cb9d8021SPierre Barbier de Reuille { 7269cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7270cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7271cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 7272cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 7273d183316bSPierre Barbier de Reuille PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 7274cb9d8021SPierre Barbier de Reuille } 7275cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7276cb9d8021SPierre Barbier de Reuille } 72771ceb14c0SBarry Smith 727893b34091SDebojyoti Ghosh /*@C 7279e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 728093b34091SDebojyoti Ghosh 7281d083f849SBarry Smith Collective 728293b34091SDebojyoti Ghosh 728393b34091SDebojyoti Ghosh Input Parameter: 728493b34091SDebojyoti Ghosh . tsin - The input TS 728593b34091SDebojyoti Ghosh 728693b34091SDebojyoti Ghosh Output Parameter: 7287e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 728893b34091SDebojyoti Ghosh 72895eca1a21SEmil Constantinescu Notes: 72905eca1a21SEmil 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. 72915eca1a21SEmil Constantinescu 7292928bb9adSStefano 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); 72935eca1a21SEmil Constantinescu 72945eca1a21SEmil Constantinescu Level: developer 729593b34091SDebojyoti Ghosh 7296e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 729793b34091SDebojyoti Ghosh @*/ 7298baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 729993b34091SDebojyoti Ghosh { 730093b34091SDebojyoti Ghosh TS t; 730193b34091SDebojyoti Ghosh PetscErrorCode ierr; 7302dc846ba4SSatish Balay SNES snes_start; 7303dc846ba4SSatish Balay DM dm; 7304dc846ba4SSatish Balay TSType type; 730593b34091SDebojyoti Ghosh 730693b34091SDebojyoti Ghosh PetscFunctionBegin; 730793b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 730893b34091SDebojyoti Ghosh *tsout = NULL; 730993b34091SDebojyoti Ghosh 73107a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 731193b34091SDebojyoti Ghosh 731293b34091SDebojyoti Ghosh /* General TS description */ 731393b34091SDebojyoti Ghosh t->numbermonitors = 0; 731493b34091SDebojyoti Ghosh t->setupcalled = 0; 731593b34091SDebojyoti Ghosh t->ksp_its = 0; 731693b34091SDebojyoti Ghosh t->snes_its = 0; 731793b34091SDebojyoti Ghosh t->nwork = 0; 731893b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 731993b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 732093b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 732193b34091SDebojyoti Ghosh 732234561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr); 732334561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr); 7324d15a3a53SEmil Constantinescu 732593b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr); 732693b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm);CHKERRQ(ierr); 732793b34091SDebojyoti Ghosh 732893b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 732951699248SLisandro Dalcin ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr); 733093b34091SDebojyoti Ghosh 7331e7069c78SShri t->trajectory = tsin->trajectory; 7332e7069c78SShri ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr); 7333e7069c78SShri 7334e7069c78SShri t->event = tsin->event; 73356b10a48eSSatish Balay if (t->event) t->event->refct++; 7336e7069c78SShri 733793b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 733893b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 7339e7069c78SShri t->ptime_prev = tsin->ptime_prev; 734093b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 734193b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 734293b34091SDebojyoti Ghosh t->steps = tsin->steps; 734393b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 734493b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 734593b34091SDebojyoti Ghosh t->atol = tsin->atol; 734693b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 734793b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 734893b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 734993b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 735093b34091SDebojyoti Ghosh 735193b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type);CHKERRQ(ierr); 735293b34091SDebojyoti Ghosh ierr = TSSetType(t,type);CHKERRQ(ierr); 735393b34091SDebojyoti Ghosh 735493b34091SDebojyoti Ghosh t->vec_sol = NULL; 735593b34091SDebojyoti Ghosh 735693b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 735793b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 735893b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 735993b34091SDebojyoti Ghosh 736093b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 736193b34091SDebojyoti Ghosh 73620d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 73630d4fed19SBarry Smith PetscInt i; 73640d4fed19SBarry Smith ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr); 73650d4fed19SBarry Smith for (i=0; i<10; i++) { 73660d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 73670d4fed19SBarry Smith } 73680d4fed19SBarry Smith } 736993b34091SDebojyoti Ghosh *tsout = t; 737093b34091SDebojyoti Ghosh PetscFunctionReturn(0); 737193b34091SDebojyoti Ghosh } 7372f3b1f45cSBarry Smith 7373f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y) 7374f3b1f45cSBarry Smith { 7375f3b1f45cSBarry Smith PetscErrorCode ierr; 7376f3b1f45cSBarry Smith TS ts = (TS) ctx; 7377f3b1f45cSBarry Smith 7378f3b1f45cSBarry Smith PetscFunctionBegin; 7379f3b1f45cSBarry Smith ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr); 7380f3b1f45cSBarry Smith PetscFunctionReturn(0); 7381f3b1f45cSBarry Smith } 7382f3b1f45cSBarry Smith 7383f3b1f45cSBarry Smith /*@ 7384f3b1f45cSBarry Smith TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function. 7385f3b1f45cSBarry Smith 7386d083f849SBarry Smith Logically Collective on TS 7387f3b1f45cSBarry Smith 7388f3b1f45cSBarry Smith Input Parameters: 7389f3b1f45cSBarry Smith TS - the time stepping routine 7390f3b1f45cSBarry Smith 7391f3b1f45cSBarry Smith Output Parameter: 7392f3b1f45cSBarry Smith . flg - PETSC_TRUE if the multiply is likely correct 7393f3b1f45cSBarry Smith 7394f3b1f45cSBarry Smith Options Database: 7395f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator 7396f3b1f45cSBarry Smith 7397f3b1f45cSBarry Smith Level: advanced 7398f3b1f45cSBarry Smith 739995452b02SPatrick Sanan Notes: 740095452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 7401f3b1f45cSBarry Smith 7402f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose() 7403f3b1f45cSBarry Smith @*/ 7404f3b1f45cSBarry Smith PetscErrorCode TSRHSJacobianTest(TS ts,PetscBool *flg) 7405f3b1f45cSBarry Smith { 7406f3b1f45cSBarry Smith Mat J,B; 7407f3b1f45cSBarry Smith PetscErrorCode ierr; 7408f3b1f45cSBarry Smith TSRHSJacobian func; 7409f3b1f45cSBarry Smith void* ctx; 7410f3b1f45cSBarry Smith 7411f3b1f45cSBarry Smith PetscFunctionBegin; 7412f3b1f45cSBarry Smith ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr); 7413f3b1f45cSBarry Smith ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr); 7414f3b1f45cSBarry Smith ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr); 7415f3b1f45cSBarry Smith PetscFunctionReturn(0); 7416f3b1f45cSBarry Smith } 7417f3b1f45cSBarry Smith 7418f3b1f45cSBarry Smith /*@C 7419f3b1f45cSBarry Smith TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function. 7420f3b1f45cSBarry Smith 7421d083f849SBarry Smith Logically Collective on TS 7422f3b1f45cSBarry Smith 7423f3b1f45cSBarry Smith Input Parameters: 7424f3b1f45cSBarry Smith TS - the time stepping routine 7425f3b1f45cSBarry Smith 7426f3b1f45cSBarry Smith Output Parameter: 7427f3b1f45cSBarry Smith . flg - PETSC_TRUE if the multiply is likely correct 7428f3b1f45cSBarry Smith 7429f3b1f45cSBarry Smith Options Database: 7430f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator 7431f3b1f45cSBarry Smith 743295452b02SPatrick Sanan Notes: 743395452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 7434f3b1f45cSBarry Smith 7435f3b1f45cSBarry Smith Level: advanced 7436f3b1f45cSBarry Smith 7437f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest() 7438f3b1f45cSBarry Smith @*/ 7439f3b1f45cSBarry Smith PetscErrorCode TSRHSJacobianTestTranspose(TS ts,PetscBool *flg) 7440f3b1f45cSBarry Smith { 7441f3b1f45cSBarry Smith Mat J,B; 7442f3b1f45cSBarry Smith PetscErrorCode ierr; 7443f3b1f45cSBarry Smith void *ctx; 7444f3b1f45cSBarry Smith TSRHSJacobian func; 7445f3b1f45cSBarry Smith 7446f3b1f45cSBarry Smith PetscFunctionBegin; 7447f3b1f45cSBarry Smith ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr); 7448f3b1f45cSBarry Smith ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr); 7449f3b1f45cSBarry Smith ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr); 7450f3b1f45cSBarry Smith PetscFunctionReturn(0); 7451f3b1f45cSBarry Smith } 74520fe4d17eSHong Zhang 74530fe4d17eSHong Zhang /*@ 74540fe4d17eSHong Zhang TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used. 74550fe4d17eSHong Zhang 74560fe4d17eSHong Zhang Logically collective 74570fe4d17eSHong Zhang 74580fe4d17eSHong Zhang Input Parameter: 74590fe4d17eSHong Zhang + ts - timestepping context 74600fe4d17eSHong Zhang - use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used 74610fe4d17eSHong Zhang 74620fe4d17eSHong Zhang Options Database: 74630fe4d17eSHong Zhang . -ts_use_splitrhsfunction - <true,false> 74640fe4d17eSHong Zhang 74650fe4d17eSHong Zhang Notes: 74660fe4d17eSHong Zhang This is only useful for multirate methods 74670fe4d17eSHong Zhang 74680fe4d17eSHong Zhang Level: intermediate 74690fe4d17eSHong Zhang 74700fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction() 74710fe4d17eSHong Zhang @*/ 74720fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction) 74730fe4d17eSHong Zhang { 74740fe4d17eSHong Zhang PetscFunctionBegin; 74750fe4d17eSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 74760fe4d17eSHong Zhang ts->use_splitrhsfunction = use_splitrhsfunction; 74770fe4d17eSHong Zhang PetscFunctionReturn(0); 74780fe4d17eSHong Zhang } 74790fe4d17eSHong Zhang 74800fe4d17eSHong Zhang /*@ 74810fe4d17eSHong Zhang TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used. 74820fe4d17eSHong Zhang 74830fe4d17eSHong Zhang Not collective 74840fe4d17eSHong Zhang 74850fe4d17eSHong Zhang Input Parameter: 74860fe4d17eSHong Zhang . ts - timestepping context 74870fe4d17eSHong Zhang 74880fe4d17eSHong Zhang Output Parameter: 74890fe4d17eSHong Zhang . use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used 74900fe4d17eSHong Zhang 74910fe4d17eSHong Zhang Level: intermediate 74920fe4d17eSHong Zhang 74930fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction() 74940fe4d17eSHong Zhang @*/ 74950fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction) 74960fe4d17eSHong Zhang { 74970fe4d17eSHong Zhang PetscFunctionBegin; 74980fe4d17eSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 74990fe4d17eSHong Zhang *use_splitrhsfunction = ts->use_splitrhsfunction; 75000fe4d17eSHong Zhang PetscFunctionReturn(0); 75010fe4d17eSHong Zhang } 7502