1af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 2496e6a7aSJed Brown #include <petscdmshell.h> 31e25c274SJed Brown #include <petscdmda.h> 42d5ee99bSBarry Smith #include <petscviewer.h> 52d5ee99bSBarry Smith #include <petscdraw.h> 6d763cef2SBarry Smith 7d5ba7fb7SMatthew Knepley /* Logging support */ 8d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 9*a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 10d405a339SMatthew Knepley 11feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1249354f04SShri Abhyankar 13fde5950dSBarry Smith /*@C 14fde5950dSBarry Smith TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 15fde5950dSBarry Smith 16fde5950dSBarry Smith Collective on TS 17fde5950dSBarry Smith 18fde5950dSBarry Smith Input Parameters: 19fde5950dSBarry Smith + ts - TS object you wish to monitor 20fde5950dSBarry Smith . name - the monitor type one is seeking 21fde5950dSBarry Smith . help - message indicating what monitoring is done 22fde5950dSBarry Smith . manual - manual page for the monitor 23fde5950dSBarry Smith . monitor - the monitor function 24fde5950dSBarry Smith - monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects 25fde5950dSBarry Smith 26fde5950dSBarry Smith Level: developer 27fde5950dSBarry Smith 28fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 29fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 30fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 31fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 32fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 33fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 34fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 35fde5950dSBarry Smith @*/ 36721cd6eeSBarry Smith PetscErrorCode TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*)) 37fde5950dSBarry Smith { 38fde5950dSBarry Smith PetscErrorCode ierr; 39fde5950dSBarry Smith PetscViewer viewer; 40fde5950dSBarry Smith PetscViewerFormat format; 41fde5950dSBarry Smith PetscBool flg; 42fde5950dSBarry Smith 43fde5950dSBarry Smith PetscFunctionBegin; 44fde5950dSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 45fde5950dSBarry Smith if (flg) { 46721cd6eeSBarry Smith PetscViewerAndFormat *vf; 47721cd6eeSBarry Smith ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr); 48721cd6eeSBarry Smith ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 49fde5950dSBarry Smith if (monitorsetup) { 50721cd6eeSBarry Smith ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr); 51fde5950dSBarry Smith } 52721cd6eeSBarry Smith ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr); 53fde5950dSBarry Smith } 54fde5950dSBarry Smith PetscFunctionReturn(0); 55fde5950dSBarry Smith } 56fde5950dSBarry Smith 572ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type) 582ffb9264SLisandro Dalcin { 592ffb9264SLisandro Dalcin PetscErrorCode ierr; 602ffb9264SLisandro Dalcin 612ffb9264SLisandro Dalcin PetscFunctionBegin; 62b92453a8SLisandro Dalcin PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1); 63b92453a8SLisandro Dalcin PetscValidCharPointer(default_type,2); 642ffb9264SLisandro Dalcin if (!((PetscObject)adapt)->type_name) { 652ffb9264SLisandro Dalcin ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr); 662ffb9264SLisandro Dalcin } 672ffb9264SLisandro Dalcin PetscFunctionReturn(0); 682ffb9264SLisandro Dalcin } 692ffb9264SLisandro Dalcin 70bdad233fSMatthew Knepley /*@ 71bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 72bdad233fSMatthew Knepley 73bdad233fSMatthew Knepley Collective on TS 74bdad233fSMatthew Knepley 75bdad233fSMatthew Knepley Input Parameter: 76bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 77bdad233fSMatthew Knepley 78bdad233fSMatthew Knepley Options Database Keys: 79b6a60446SDebojyoti Ghosh + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE 80ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 81ef85077eSLisandro Dalcin . -ts_max_time <time> - maximum time to compute to 82ef85077eSLisandro Dalcin . -ts_max_steps <steps> - maximum number of time-steps to take 83ef85077eSLisandro Dalcin . -ts_init_time <time> - initial time to start computation 84ef85077eSLisandro Dalcin . -ts_final_time <time> - final time to compute to 853e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 86a3cdaa26SBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> whether to stop at the exact given final time and how to compute the solution at that ti,e 87a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 88a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 89a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 90a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 91a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 92847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 93bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 94de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 95de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 967cf37e64SBarry Smith . -ts_monitor_error - Monitors norm of error 976934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 988b668821SLisandro Dalcin . -ts_monitor_lg_timestep_log - Monitor log timestep size graphically 99de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 100de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 101de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 102de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 1033e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 1043e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 105fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 106e4160dc7SJulian Andrej . -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu) 107110eb670SHong Zhang . -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 10853ea634cSHong Zhang 10991b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 110bdad233fSMatthew Knepley 111bdad233fSMatthew Knepley Level: beginner 112bdad233fSMatthew Knepley 113bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 114bdad233fSMatthew Knepley 115a313700dSBarry Smith .seealso: TSGetType() 116bdad233fSMatthew Knepley @*/ 1177087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 118bdad233fSMatthew Knepley { 119bc952696SBarry Smith PetscBool opt,flg,tflg; 120dfbe8321SBarry Smith PetscErrorCode ierr; 121eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 12231748224SBarry Smith PetscReal time_step; 12349354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 124d1212d36SBarry Smith char dir[16]; 125cd11d68dSLisandro Dalcin TSIFunction ifun; 1266991f827SBarry Smith const char *defaultType; 1276991f827SBarry Smith char typeName[256]; 128bdad233fSMatthew Knepley 129bdad233fSMatthew Knepley PetscFunctionBegin; 1300700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1316991f827SBarry Smith 1326991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 133cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 134cd11d68dSLisandro Dalcin 135cd11d68dSLisandro Dalcin ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 136cd11d68dSLisandro Dalcin if (((PetscObject)ts)->type_name) 137cd11d68dSLisandro Dalcin defaultType = ((PetscObject)ts)->type_name; 138cd11d68dSLisandro Dalcin else 139cd11d68dSLisandro Dalcin defaultType = ifun ? TSBEULER : TSEULER; 1406991f827SBarry Smith ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr); 1416991f827SBarry Smith if (opt) { 1426991f827SBarry Smith ierr = TSSetType(ts,typeName);CHKERRQ(ierr); 1436991f827SBarry Smith } else { 1446991f827SBarry Smith ierr = TSSetType(ts,defaultType);CHKERRQ(ierr); 1456991f827SBarry Smith } 146bdad233fSMatthew Knepley 147bdad233fSMatthew Knepley /* Handle generic TS options */ 148ef85077eSLisandro Dalcin ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 14919eac22cSLisandro Dalcin ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1500298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 151ef85077eSLisandro Dalcin ierr = PetscOptionsReal("-ts_final_time","Final time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 15231748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 153cd11d68dSLisandro Dalcin if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);} 15449354f04SShri 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); 15549354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 1560298fd71SBarry 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); 1570298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 1580298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 1590298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 1600298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 161bdad233fSMatthew Knepley 16256f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 16356f85f32SBarry Smith { 16456f85f32SBarry Smith PetscBool set; 16556f85f32SBarry Smith flg = PETSC_FALSE; 16656f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 16756f85f32SBarry Smith if (set) { 16856f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 16956f85f32SBarry Smith } 17056f85f32SBarry Smith } 17156f85f32SBarry Smith #endif 17256f85f32SBarry Smith 173bdad233fSMatthew Knepley /* Monitor options */ 174cd11d68dSLisandro Dalcin ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr); 175fde5950dSBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr); 176fde5950dSBarry Smith 1775180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1785180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1795180491cSLisandro Dalcin 1804f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 181b3603a34SBarry Smith if (opt) { 1820b039ecaSBarry Smith TSMonitorLGCtx ctx; 1833923b477SBarry Smith PetscInt howoften = 1; 184b3603a34SBarry Smith 1850298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 1866ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 1874f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 188bdad233fSMatthew Knepley } 1896ba87a44SLisandro Dalcin 1904f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 191ef20d060SBarry Smith if (opt) { 1920b039ecaSBarry Smith TSMonitorLGCtx ctx; 1933923b477SBarry Smith PetscInt howoften = 1; 194ef20d060SBarry Smith 1950298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 1966ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 1974f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 198ef20d060SBarry Smith } 1996934998bSLisandro Dalcin 2007cf37e64SBarry Smith ierr = PetscOptionsName("-ts_monitor_error","Monitor error","TSMonitorError",&opt);CHKERRQ(ierr); 2017cf37e64SBarry Smith if (opt) { 2027cf37e64SBarry Smith ierr = TSMonitorSet(ts,TSMonitorError,NULL,NULL);CHKERRQ(ierr); 2037cf37e64SBarry Smith } 2047cf37e64SBarry Smith 2056934998bSLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 2066934998bSLisandro Dalcin if (opt) { 2076934998bSLisandro Dalcin TSMonitorLGCtx ctx; 2086934998bSLisandro Dalcin PetscInt howoften = 1; 2096934998bSLisandro Dalcin 2106934998bSLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 2116ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2126934998bSLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 2136934998bSLisandro Dalcin } 2148b668821SLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 2158b668821SLisandro Dalcin if (opt) { 2168b668821SLisandro Dalcin TSMonitorLGCtx ctx; 2178b668821SLisandro Dalcin PetscInt howoften = 1; 2188b668821SLisandro Dalcin 2198b668821SLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 2208b668821SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2218b668821SLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 2228b668821SLisandro Dalcin ctx->semilogy = PETSC_TRUE; 2238b668821SLisandro Dalcin } 2248b668821SLisandro Dalcin 225201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 226201da799SBarry Smith if (opt) { 227201da799SBarry Smith TSMonitorLGCtx ctx; 228201da799SBarry Smith PetscInt howoften = 1; 229201da799SBarry Smith 2300298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2316ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 232201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 233201da799SBarry Smith } 234201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 235201da799SBarry Smith if (opt) { 236201da799SBarry Smith TSMonitorLGCtx ctx; 237201da799SBarry Smith PetscInt howoften = 1; 238201da799SBarry Smith 2390298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2406ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 241201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 242201da799SBarry Smith } 2438189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 2448189c53fSBarry Smith if (opt) { 2458189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2468189c53fSBarry Smith PetscInt howoften = 1; 2478189c53fSBarry Smith 2480298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 2496934998bSLisandro Dalcin ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 2508189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 2518189c53fSBarry Smith } 252ef20d060SBarry Smith opt = PETSC_FALSE; 2530dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 254a7cc72afSBarry Smith if (opt) { 25583a4ac43SBarry Smith TSMonitorDrawCtx ctx; 25683a4ac43SBarry Smith PetscInt howoften = 1; 257a80ad3e0SBarry Smith 2580298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 2590ed3bfb6SBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 26083a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 261bdad233fSMatthew Knepley } 262fb1732b5SBarry Smith opt = PETSC_FALSE; 2632d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 2642d5ee99bSBarry Smith if (opt) { 2652d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2662d5ee99bSBarry Smith PetscReal bounds[4]; 2672d5ee99bSBarry Smith PetscInt n = 4; 2682d5ee99bSBarry Smith PetscDraw draw; 2696934998bSLisandro Dalcin PetscDrawAxis axis; 2702d5ee99bSBarry Smith 2712d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 2722d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2736934998bSLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr); 2742d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 2756934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr); 2766934998bSLisandro Dalcin ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 2776934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 2782d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2792d5ee99bSBarry Smith } 2802d5ee99bSBarry Smith opt = PETSC_FALSE; 2810dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 2823a471f94SBarry Smith if (opt) { 28383a4ac43SBarry Smith TSMonitorDrawCtx ctx; 28483a4ac43SBarry Smith PetscInt howoften = 1; 2853a471f94SBarry Smith 2860298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 2870ed3bfb6SBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 28883a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2893a471f94SBarry Smith } 2900ed3bfb6SBarry Smith opt = PETSC_FALSE; 2910ed3bfb6SBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr); 2920ed3bfb6SBarry Smith if (opt) { 2930ed3bfb6SBarry Smith TSMonitorDrawCtx ctx; 2940ed3bfb6SBarry Smith PetscInt howoften = 1; 2950ed3bfb6SBarry Smith 2960ed3bfb6SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr); 2970ed3bfb6SBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 2980ed3bfb6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2990ed3bfb6SBarry Smith } 300fde5950dSBarry Smith 301ed81e22dSJed Brown opt = PETSC_FALSE; 30291b97e58SBarry 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); 303ed81e22dSJed Brown if (flg) { 304ed81e22dSJed Brown const char *ptr,*ptr2; 305ed81e22dSJed Brown char *filetemplate; 306ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 307ed81e22dSJed Brown /* Do some cursory validation of the input. */ 308ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 309ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 310ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 311ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 312ce94432eSBarry 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"); 313ed81e22dSJed Brown if (ptr2) break; 314ed81e22dSJed Brown } 315ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 316ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 317ed81e22dSJed Brown } 318bdad233fSMatthew Knepley 319d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 320d1212d36SBarry Smith if (flg) { 321d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 322d1212d36SBarry Smith int ray = 0; 323d1212d36SBarry Smith DMDADirection ddir; 324d1212d36SBarry Smith DM da; 325d1212d36SBarry Smith PetscMPIInt rank; 326d1212d36SBarry Smith 327ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 328d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 329d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 330ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 331d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 332d1212d36SBarry Smith 333d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 334b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 335d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 336d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 337ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 338d1212d36SBarry Smith if (!rank) { 339d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 340d1212d36SBarry Smith } 34151b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 342d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 343d1212d36SBarry Smith } 34451b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 34551b4a12fSMatthew G. Knepley if (flg) { 34651b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 34751b4a12fSMatthew G. Knepley int ray = 0; 34851b4a12fSMatthew G. Knepley DMDADirection ddir; 34951b4a12fSMatthew G. Knepley DM da; 35051b4a12fSMatthew G. Knepley PetscInt howoften = 1; 351d1212d36SBarry Smith 35251b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 35351b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 35451b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 35551b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 35651b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 3571c3436cfSJed Brown 35851b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 359b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 36051b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 36151b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 36251b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 36351b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 36451b4a12fSMatthew G. Knepley } 365a7a1495cSBarry Smith 366b3d3934dSBarry Smith ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 367b3d3934dSBarry Smith if (opt) { 368b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 369b3d3934dSBarry Smith 370b3d3934dSBarry Smith ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 371b3d3934dSBarry Smith ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 372b3d3934dSBarry Smith } 373b3d3934dSBarry Smith 374847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 375847ff0e1SMatthew G. Knepley ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr); 376847ff0e1SMatthew G. Knepley if (flg) { 377847ff0e1SMatthew G. Knepley DM dm; 378847ff0e1SMatthew G. Knepley DMTS tdm; 379847ff0e1SMatthew G. Knepley 380847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 381847ff0e1SMatthew G. Knepley ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr); 382847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 383847ff0e1SMatthew G. Knepley ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr); 384847ff0e1SMatthew G. Knepley ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 385847ff0e1SMatthew G. Knepley } 386847ff0e1SMatthew G. Knepley 387d763cef2SBarry Smith /* Handle specific TS options */ 388d763cef2SBarry Smith if (ts->ops->setfromoptions) { 3896991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 390d763cef2SBarry Smith } 391fbc52257SHong Zhang 392a7bdc993SLisandro Dalcin /* Handle TSAdapt options */ 393a7bdc993SLisandro Dalcin ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 394a7bdc993SLisandro Dalcin ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr); 395a7bdc993SLisandro Dalcin ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr); 396a7bdc993SLisandro Dalcin 39768bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 3984f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 39968bece0bSHong Zhang ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 40068bece0bSHong Zhang if (tflg) { 40168bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 40268bece0bSHong Zhang } 403*a05bf03eSHong Zhang 404*a05bf03eSHong Zhang ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr); 405d763cef2SBarry Smith 406d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 4070633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr); 408fbc52257SHong Zhang ierr = PetscOptionsEnd();CHKERRQ(ierr); 409d763cef2SBarry Smith 4104f122a70SLisandro Dalcin if (ts->trajectory) { 4114f122a70SLisandro Dalcin ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 412d763cef2SBarry Smith } 41368bece0bSHong Zhang 4144f122a70SLisandro Dalcin ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr); 4154f122a70SLisandro Dalcin if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);} 4164f122a70SLisandro Dalcin ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr); 417d763cef2SBarry Smith PetscFunctionReturn(0); 418d763cef2SBarry Smith } 419d763cef2SBarry Smith 420d2daff3dSHong Zhang /*@ 42178fbdcc8SBarry Smith TSGetTrajectory - Gets the trajectory from a TS if it exists 42278fbdcc8SBarry Smith 42378fbdcc8SBarry Smith Collective on TS 42478fbdcc8SBarry Smith 42578fbdcc8SBarry Smith Input Parameters: 42678fbdcc8SBarry Smith . ts - the TS context obtained from TSCreate() 42778fbdcc8SBarry Smith 42878fbdcc8SBarry Smith Output Parameters; 42978fbdcc8SBarry Smith . tr - the TSTrajectory object, if it exists 43078fbdcc8SBarry Smith 43178fbdcc8SBarry Smith Note: This routine should be called after all TS options have been set 43278fbdcc8SBarry Smith 43378fbdcc8SBarry Smith Level: advanced 43478fbdcc8SBarry Smith 43578fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate() 43678fbdcc8SBarry Smith 43778fbdcc8SBarry Smith .keywords: TS, set, checkpoint, 43878fbdcc8SBarry Smith @*/ 43978fbdcc8SBarry Smith PetscErrorCode TSGetTrajectory(TS ts,TSTrajectory *tr) 44078fbdcc8SBarry Smith { 44178fbdcc8SBarry Smith PetscFunctionBegin; 44278fbdcc8SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 44378fbdcc8SBarry Smith *tr = ts->trajectory; 44478fbdcc8SBarry Smith PetscFunctionReturn(0); 44578fbdcc8SBarry Smith } 44678fbdcc8SBarry Smith 44778fbdcc8SBarry Smith /*@ 448bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 449d2daff3dSHong Zhang 450d2daff3dSHong Zhang Collective on TS 451d2daff3dSHong Zhang 452d2daff3dSHong Zhang Input Parameters: 453bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 454bc952696SBarry Smith 45578fbdcc8SBarry Smith Options Database: 45678fbdcc8SBarry Smith + -ts_save_trajectory - saves the trajectory to a file 45778fbdcc8SBarry Smith - -ts_trajectory_type type 45878fbdcc8SBarry Smith 45968bece0bSHong Zhang Note: This routine should be called after all TS options have been set 460d2daff3dSHong Zhang 46178fbdcc8SBarry Smith The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/bin/PetscBinaryIOTrajectory.py and 46278fbdcc8SBarry Smith MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m 46378fbdcc8SBarry Smith 464d2daff3dSHong Zhang Level: intermediate 465d2daff3dSHong Zhang 46678fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectoryType, TSSetTrajectoryType() 467d2daff3dSHong Zhang 468bc952696SBarry Smith .keywords: TS, set, checkpoint, 469d2daff3dSHong Zhang @*/ 470bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 471d2daff3dSHong Zhang { 472bc952696SBarry Smith PetscErrorCode ierr; 473bc952696SBarry Smith 474d2daff3dSHong Zhang PetscFunctionBegin; 475d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 476bc952696SBarry Smith if (!ts->trajectory) { 477bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 478bc952696SBarry Smith } 479d2daff3dSHong Zhang PetscFunctionReturn(0); 480d2daff3dSHong Zhang } 481d2daff3dSHong Zhang 482a7a1495cSBarry Smith /*@ 483a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 484a7a1495cSBarry Smith set with TSSetRHSJacobian(). 485a7a1495cSBarry Smith 486a7a1495cSBarry Smith Collective on TS and Vec 487a7a1495cSBarry Smith 488a7a1495cSBarry Smith Input Parameters: 489316643e7SJed Brown + ts - the TS context 490a7a1495cSBarry Smith . t - current timestep 4910910c330SBarry Smith - U - input vector 492a7a1495cSBarry Smith 493a7a1495cSBarry Smith Output Parameters: 494a7a1495cSBarry Smith + A - Jacobian matrix 495a7a1495cSBarry Smith . B - optional preconditioning matrix 496a7a1495cSBarry Smith - flag - flag indicating matrix structure 497a7a1495cSBarry Smith 498a7a1495cSBarry Smith Notes: 499a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 500a7a1495cSBarry Smith is used internally within the nonlinear solvers. 501a7a1495cSBarry Smith 50294b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 503a7a1495cSBarry Smith flag parameter. 504a7a1495cSBarry Smith 505a7a1495cSBarry Smith Level: developer 506a7a1495cSBarry Smith 507a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 508a7a1495cSBarry Smith 50994b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 510a7a1495cSBarry Smith @*/ 511d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 512a7a1495cSBarry Smith { 513dfbe8321SBarry Smith PetscErrorCode ierr; 514270bf2e7SJed Brown PetscObjectState Ustate; 5156c1e1eecSBarry Smith PetscObjectId Uid; 51624989b8cSPeter Brune DM dm; 517942e3340SBarry Smith DMTS tsdm; 51824989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 51924989b8cSPeter Brune void *ctx; 52024989b8cSPeter Brune TSIJacobian ijacobianfunc; 521b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 522a7a1495cSBarry Smith 523a7a1495cSBarry Smith PetscFunctionBegin; 5240700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5250910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5260910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 52724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 528942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 52924989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 5300298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 531b2df71adSDebojyoti Ghosh ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 53259e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 5336c1e1eecSBarry Smith ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr); 5346c1e1eecSBarry Smith if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 5350e4ef248SJed Brown PetscFunctionReturn(0); 536f8ede8e7SJed Brown } 537d90be118SSean Farley 538ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 539d90be118SSean Farley 540e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 54194ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 54294ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 543303f9b21SStefano Zampini if (B && A != B) { 54494ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 54594ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 546e1244c69SJed Brown } 547e1244c69SJed Brown ts->rhsjacobian.shift = 0; 548e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 549e1244c69SJed Brown } 550e1244c69SJed Brown 55124989b8cSPeter Brune if (rhsjacobianfunc) { 5526cd88445SBarry Smith PetscBool missing; 55394ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 554a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 555d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 556a7a1495cSBarry Smith PetscStackPop; 55794ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 5586cd88445SBarry Smith if (A) { 5596cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 5606cd88445SBarry 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"); 5616cd88445SBarry Smith } 5626cd88445SBarry Smith if (B && B != A) { 5636cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 5646cd88445SBarry 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"); 5656cd88445SBarry Smith } 566ef66eb69SBarry Smith } else { 56794ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 56894ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 569ef66eb69SBarry Smith } 5700e4ef248SJed Brown ts->rhsjacobian.time = t; 5717f79407eSBarry Smith ierr = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr); 57259e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 573a7a1495cSBarry Smith PetscFunctionReturn(0); 574a7a1495cSBarry Smith } 575a7a1495cSBarry Smith 576316643e7SJed Brown /*@ 577d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 578d763cef2SBarry Smith 579316643e7SJed Brown Collective on TS and Vec 580316643e7SJed Brown 581316643e7SJed Brown Input Parameters: 582316643e7SJed Brown + ts - the TS context 583316643e7SJed Brown . t - current time 5840910c330SBarry Smith - U - state vector 585316643e7SJed Brown 586316643e7SJed Brown Output Parameter: 587316643e7SJed Brown . y - right hand side 588316643e7SJed Brown 589316643e7SJed Brown Note: 590316643e7SJed Brown Most users should not need to explicitly call this routine, as it 591316643e7SJed Brown is used internally within the nonlinear solvers. 592316643e7SJed Brown 593316643e7SJed Brown Level: developer 594316643e7SJed Brown 595316643e7SJed Brown .keywords: TS, compute 596316643e7SJed Brown 597316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 598316643e7SJed Brown @*/ 5990910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 600d763cef2SBarry Smith { 601dfbe8321SBarry Smith PetscErrorCode ierr; 60224989b8cSPeter Brune TSRHSFunction rhsfunction; 60324989b8cSPeter Brune TSIFunction ifunction; 60424989b8cSPeter Brune void *ctx; 60524989b8cSPeter Brune DM dm; 60624989b8cSPeter Brune 607d763cef2SBarry Smith PetscFunctionBegin; 6080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6090910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6100700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 61124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 61224989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 6130298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 614d763cef2SBarry Smith 615ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 616d763cef2SBarry Smith 6170910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 61824989b8cSPeter Brune if (rhsfunction) { 619d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 6200910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 621d763cef2SBarry Smith PetscStackPop; 622214bc6a2SJed Brown } else { 623214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 624b2cd27e8SJed Brown } 62544a41b28SSean Farley 6260910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 627d763cef2SBarry Smith PetscFunctionReturn(0); 628d763cef2SBarry Smith } 629d763cef2SBarry Smith 630ef20d060SBarry Smith /*@ 631ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 632ef20d060SBarry Smith 633ef20d060SBarry Smith Collective on TS and Vec 634ef20d060SBarry Smith 635ef20d060SBarry Smith Input Parameters: 636ef20d060SBarry Smith + ts - the TS context 637ef20d060SBarry Smith - t - current time 638ef20d060SBarry Smith 639ef20d060SBarry Smith Output Parameter: 6400910c330SBarry Smith . U - the solution 641ef20d060SBarry Smith 642ef20d060SBarry Smith Note: 643ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 644ef20d060SBarry Smith is used internally within the nonlinear solvers. 645ef20d060SBarry Smith 646ef20d060SBarry Smith Level: developer 647ef20d060SBarry Smith 648ef20d060SBarry Smith .keywords: TS, compute 649ef20d060SBarry Smith 650abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 651ef20d060SBarry Smith @*/ 6520910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 653ef20d060SBarry Smith { 654ef20d060SBarry Smith PetscErrorCode ierr; 655ef20d060SBarry Smith TSSolutionFunction solutionfunction; 656ef20d060SBarry Smith void *ctx; 657ef20d060SBarry Smith DM dm; 658ef20d060SBarry Smith 659ef20d060SBarry Smith PetscFunctionBegin; 660ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6610910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 662ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 663ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 664ef20d060SBarry Smith 665ef20d060SBarry Smith if (solutionfunction) { 6669b7cd975SBarry Smith PetscStackPush("TS user solution function"); 6670910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 668ef20d060SBarry Smith PetscStackPop; 669ef20d060SBarry Smith } 670ef20d060SBarry Smith PetscFunctionReturn(0); 671ef20d060SBarry Smith } 6729b7cd975SBarry Smith /*@ 6739b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6749b7cd975SBarry Smith 6759b7cd975SBarry Smith Collective on TS and Vec 6769b7cd975SBarry Smith 6779b7cd975SBarry Smith Input Parameters: 6789b7cd975SBarry Smith + ts - the TS context 6799b7cd975SBarry Smith - t - current time 6809b7cd975SBarry Smith 6819b7cd975SBarry Smith Output Parameter: 6829b7cd975SBarry Smith . U - the function value 6839b7cd975SBarry Smith 6849b7cd975SBarry Smith Note: 6859b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 6869b7cd975SBarry Smith is used internally within the nonlinear solvers. 6879b7cd975SBarry Smith 6889b7cd975SBarry Smith Level: developer 6899b7cd975SBarry Smith 6909b7cd975SBarry Smith .keywords: TS, compute 6919b7cd975SBarry Smith 6929b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 6939b7cd975SBarry Smith @*/ 6949b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 6959b7cd975SBarry Smith { 6969b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 6979b7cd975SBarry Smith void *ctx; 6989b7cd975SBarry Smith DM dm; 6999b7cd975SBarry Smith 7009b7cd975SBarry Smith PetscFunctionBegin; 7019b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7029b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7039b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 7049b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 7059b7cd975SBarry Smith 7069b7cd975SBarry Smith if (forcing) { 7079b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 7089b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 7099b7cd975SBarry Smith PetscStackPop; 7109b7cd975SBarry Smith } 7119b7cd975SBarry Smith PetscFunctionReturn(0); 7129b7cd975SBarry Smith } 713ef20d060SBarry Smith 714214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 715214bc6a2SJed Brown { 7162dd45cf8SJed Brown Vec F; 717214bc6a2SJed Brown PetscErrorCode ierr; 718214bc6a2SJed Brown 719214bc6a2SJed Brown PetscFunctionBegin; 7200298fd71SBarry Smith *Frhs = NULL; 7210298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 722214bc6a2SJed Brown if (!ts->Frhs) { 7232dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 724214bc6a2SJed Brown } 725214bc6a2SJed Brown *Frhs = ts->Frhs; 726214bc6a2SJed Brown PetscFunctionReturn(0); 727214bc6a2SJed Brown } 728214bc6a2SJed Brown 729214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 730214bc6a2SJed Brown { 731214bc6a2SJed Brown Mat A,B; 7322dd45cf8SJed Brown PetscErrorCode ierr; 73341a1d4d2SBarry Smith TSIJacobian ijacobian; 734214bc6a2SJed Brown 735214bc6a2SJed Brown PetscFunctionBegin; 736c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 737c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 73841a1d4d2SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr); 739214bc6a2SJed Brown if (Arhs) { 740214bc6a2SJed Brown if (!ts->Arhs) { 74141a1d4d2SBarry Smith if (ijacobian) { 742214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 74341a1d4d2SBarry Smith } else { 74441a1d4d2SBarry Smith ts->Arhs = A; 74541a1d4d2SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 74641a1d4d2SBarry Smith } 7473565c898SBarry Smith } else { 7483565c898SBarry Smith PetscBool flg; 7493565c898SBarry Smith ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr); 7503565c898SBarry Smith /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */ 7513565c898SBarry Smith if (flg && !ijacobian && ts->Arhs == ts->Brhs){ 7523565c898SBarry Smith ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr); 7533565c898SBarry Smith ts->Arhs = A; 7543565c898SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 7553565c898SBarry Smith } 756214bc6a2SJed Brown } 757214bc6a2SJed Brown *Arhs = ts->Arhs; 758214bc6a2SJed Brown } 759214bc6a2SJed Brown if (Brhs) { 760214bc6a2SJed Brown if (!ts->Brhs) { 761bdb70873SJed Brown if (A != B) { 76241a1d4d2SBarry Smith if (ijacobian) { 763214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 764bdb70873SJed Brown } else { 76541a1d4d2SBarry Smith ts->Brhs = B; 76641a1d4d2SBarry Smith ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 76741a1d4d2SBarry Smith } 76841a1d4d2SBarry Smith } else { 769bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 77051699248SLisandro Dalcin ts->Brhs = ts->Arhs; 771bdb70873SJed Brown } 772214bc6a2SJed Brown } 773214bc6a2SJed Brown *Brhs = ts->Brhs; 774214bc6a2SJed Brown } 775214bc6a2SJed Brown PetscFunctionReturn(0); 776214bc6a2SJed Brown } 777214bc6a2SJed Brown 778316643e7SJed Brown /*@ 7790910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 780316643e7SJed Brown 781316643e7SJed Brown Collective on TS and Vec 782316643e7SJed Brown 783316643e7SJed Brown Input Parameters: 784316643e7SJed Brown + ts - the TS context 785316643e7SJed Brown . t - current time 7860910c330SBarry Smith . U - state vector 7870910c330SBarry Smith . Udot - time derivative of state vector 788214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 789316643e7SJed Brown 790316643e7SJed Brown Output Parameter: 791316643e7SJed Brown . Y - right hand side 792316643e7SJed Brown 793316643e7SJed Brown Note: 794316643e7SJed Brown Most users should not need to explicitly call this routine, as it 795316643e7SJed Brown is used internally within the nonlinear solvers. 796316643e7SJed Brown 797316643e7SJed Brown If the user did did not write their equations in implicit form, this 798316643e7SJed Brown function recasts them in implicit form. 799316643e7SJed Brown 800316643e7SJed Brown Level: developer 801316643e7SJed Brown 802316643e7SJed Brown .keywords: TS, compute 803316643e7SJed Brown 804316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 805316643e7SJed Brown @*/ 8060910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 807316643e7SJed Brown { 808316643e7SJed Brown PetscErrorCode ierr; 80924989b8cSPeter Brune TSIFunction ifunction; 81024989b8cSPeter Brune TSRHSFunction rhsfunction; 81124989b8cSPeter Brune void *ctx; 81224989b8cSPeter Brune DM dm; 813316643e7SJed Brown 814316643e7SJed Brown PetscFunctionBegin; 8150700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8160910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8170910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 8180700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 819316643e7SJed Brown 82024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 82124989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 8220298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 82324989b8cSPeter Brune 824ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 825d90be118SSean Farley 8260910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 82724989b8cSPeter Brune if (ifunction) { 828316643e7SJed Brown PetscStackPush("TS user implicit function"); 8290910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 830316643e7SJed Brown PetscStackPop; 831214bc6a2SJed Brown } 832214bc6a2SJed Brown if (imex) { 83324989b8cSPeter Brune if (!ifunction) { 8340910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 8352dd45cf8SJed Brown } 83624989b8cSPeter Brune } else if (rhsfunction) { 83724989b8cSPeter Brune if (ifunction) { 838214bc6a2SJed Brown Vec Frhs; 839214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 8400910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 841214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 8422dd45cf8SJed Brown } else { 8430910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 8440910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 845316643e7SJed Brown } 8464a6899ffSJed Brown } 8470910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 848316643e7SJed Brown PetscFunctionReturn(0); 849316643e7SJed Brown } 850316643e7SJed Brown 851316643e7SJed Brown /*@ 852316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 853316643e7SJed Brown 854316643e7SJed Brown Collective on TS and Vec 855316643e7SJed Brown 856316643e7SJed Brown Input 857316643e7SJed Brown Input Parameters: 858316643e7SJed Brown + ts - the TS context 859316643e7SJed Brown . t - current timestep 8600910c330SBarry Smith . U - state vector 8610910c330SBarry Smith . Udot - time derivative of state vector 862214bc6a2SJed Brown . shift - shift to apply, see note below 863214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 864316643e7SJed Brown 865316643e7SJed Brown Output Parameters: 866316643e7SJed Brown + A - Jacobian matrix 8673565c898SBarry Smith - B - matrix from which the preconditioner is constructed; often the same as A 868316643e7SJed Brown 869316643e7SJed Brown Notes: 8700910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 871316643e7SJed Brown 8720910c330SBarry Smith dF/dU + shift*dF/dUdot 873316643e7SJed Brown 874316643e7SJed Brown Most users should not need to explicitly call this routine, as it 875316643e7SJed Brown is used internally within the nonlinear solvers. 876316643e7SJed Brown 877316643e7SJed Brown Level: developer 878316643e7SJed Brown 879316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 880316643e7SJed Brown 881316643e7SJed Brown .seealso: TSSetIJacobian() 88263495f91SJed Brown @*/ 883d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 884316643e7SJed Brown { 885316643e7SJed Brown PetscErrorCode ierr; 88624989b8cSPeter Brune TSIJacobian ijacobian; 88724989b8cSPeter Brune TSRHSJacobian rhsjacobian; 88824989b8cSPeter Brune DM dm; 88924989b8cSPeter Brune void *ctx; 890316643e7SJed Brown 891316643e7SJed Brown PetscFunctionBegin; 8920700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8930910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8940910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 895316643e7SJed Brown PetscValidPointer(A,6); 89694ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 897316643e7SJed Brown PetscValidPointer(B,7); 89894ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 89924989b8cSPeter Brune 90024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 90124989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 9020298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 90324989b8cSPeter Brune 904ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 905316643e7SJed Brown 90694ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 90724989b8cSPeter Brune if (ijacobian) { 9086cd88445SBarry Smith PetscBool missing; 909316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 910d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 911316643e7SJed Brown PetscStackPop; 9126cd88445SBarry Smith ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 9136cd88445SBarry 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"); 9143565c898SBarry Smith if (B != A) { 9156cd88445SBarry Smith ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 9166cd88445SBarry 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"); 9176cd88445SBarry Smith } 9184a6899ffSJed Brown } 919214bc6a2SJed Brown if (imex) { 920b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 9214c26be97Sstefano_zampini PetscBool assembled; 92294ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 9234c26be97Sstefano_zampini ierr = MatAssembled(A,&assembled);CHKERRQ(ierr); 9244c26be97Sstefano_zampini if (!assembled) { 9254c26be97Sstefano_zampini ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9264c26be97Sstefano_zampini ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9274c26be97Sstefano_zampini } 92894ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 92994ab13aaSBarry Smith if (A != B) { 93094ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 9314c26be97Sstefano_zampini ierr = MatAssembled(B,&assembled);CHKERRQ(ierr); 9324c26be97Sstefano_zampini if (!assembled) { 9334c26be97Sstefano_zampini ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9344c26be97Sstefano_zampini ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9354c26be97Sstefano_zampini } 93694ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 937214bc6a2SJed Brown } 938214bc6a2SJed Brown } 939214bc6a2SJed Brown } else { 940e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 941e1244c69SJed Brown if (rhsjacobian) { 942214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 943d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 944e1244c69SJed Brown } 94594ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 9463565c898SBarry Smith PetscBool flg; 947e1244c69SJed Brown ts->rhsjacobian.scale = -1; 948e1244c69SJed Brown ts->rhsjacobian.shift = shift; 9493565c898SBarry Smith ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr); 9503565c898SBarry Smith /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */ 9513565c898SBarry Smith if (!flg) { 95294ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 95394ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 9543565c898SBarry Smith } 95594ab13aaSBarry Smith if (A != B) { 95694ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 95794ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 958316643e7SJed Brown } 959e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 960e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 961e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 96294ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 96394ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 96494ab13aaSBarry Smith if (A != B) { 96594ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 96694ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 967214bc6a2SJed Brown } 968316643e7SJed Brown } 96994ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 97094ab13aaSBarry Smith if (A != B) { 97194ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 972316643e7SJed Brown } 973316643e7SJed Brown } 974316643e7SJed Brown } 97594ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 976316643e7SJed Brown PetscFunctionReturn(0); 977316643e7SJed Brown } 978316643e7SJed Brown 979d763cef2SBarry Smith /*@C 980d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 981b5abc632SBarry Smith where U_t = G(t,u). 982d763cef2SBarry Smith 9833f9fe445SBarry Smith Logically Collective on TS 984d763cef2SBarry Smith 985d763cef2SBarry Smith Input Parameters: 986d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 9870298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 988d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 989d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 9900298fd71SBarry Smith function evaluation routine (may be NULL) 991d763cef2SBarry Smith 992d763cef2SBarry Smith Calling sequence of func: 99387828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 994d763cef2SBarry Smith 995d763cef2SBarry Smith + t - current timestep 996d763cef2SBarry Smith . u - input vector 997d763cef2SBarry Smith . F - function vector 998d763cef2SBarry Smith - ctx - [optional] user-defined function context 999d763cef2SBarry Smith 1000d763cef2SBarry Smith Level: beginner 1001d763cef2SBarry Smith 10022bbac0d3SBarry Smith Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 10032bbac0d3SBarry Smith 1004d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1005d763cef2SBarry Smith 1006ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 1007d763cef2SBarry Smith @*/ 1008089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 1009d763cef2SBarry Smith { 1010089b2837SJed Brown PetscErrorCode ierr; 1011089b2837SJed Brown SNES snes; 10120298fd71SBarry Smith Vec ralloc = NULL; 101324989b8cSPeter Brune DM dm; 1014d763cef2SBarry Smith 1015089b2837SJed Brown PetscFunctionBegin; 10160700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1017ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 101824989b8cSPeter Brune 101924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 102024989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 1021089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1022e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 1023e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 1024e856ceecSJed Brown r = ralloc; 1025e856ceecSJed Brown } 1026089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 1027e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1028d763cef2SBarry Smith PetscFunctionReturn(0); 1029d763cef2SBarry Smith } 1030d763cef2SBarry Smith 1031ef20d060SBarry Smith /*@C 1032abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1033ef20d060SBarry Smith 1034ef20d060SBarry Smith Logically Collective on TS 1035ef20d060SBarry Smith 1036ef20d060SBarry Smith Input Parameters: 1037ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1038ef20d060SBarry Smith . f - routine for evaluating the solution 1039ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 10400298fd71SBarry Smith function evaluation routine (may be NULL) 1041ef20d060SBarry Smith 1042ef20d060SBarry Smith Calling sequence of func: 1043ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 1044ef20d060SBarry Smith 1045ef20d060SBarry Smith + t - current timestep 1046ef20d060SBarry Smith . u - output vector 1047ef20d060SBarry Smith - ctx - [optional] user-defined function context 1048ef20d060SBarry Smith 10490ed3bfb6SBarry Smith Options Database: 10500ed3bfb6SBarry Smith + -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction() 10510ed3bfb6SBarry Smith - -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 10520ed3bfb6SBarry Smith 1053abd5a294SJed Brown Notes: 1054abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1055abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1056abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1057abd5a294SJed Brown 10584f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1059abd5a294SJed Brown 1060ef20d060SBarry Smith Level: beginner 1061ef20d060SBarry Smith 1062ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 1063ef20d060SBarry Smith 10640ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError() 1065ef20d060SBarry Smith @*/ 1066ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1067ef20d060SBarry Smith { 1068ef20d060SBarry Smith PetscErrorCode ierr; 1069ef20d060SBarry Smith DM dm; 1070ef20d060SBarry Smith 1071ef20d060SBarry Smith PetscFunctionBegin; 1072ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1073ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1074ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 1075ef20d060SBarry Smith PetscFunctionReturn(0); 1076ef20d060SBarry Smith } 1077ef20d060SBarry Smith 10789b7cd975SBarry Smith /*@C 10799b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 10809b7cd975SBarry Smith 10819b7cd975SBarry Smith Logically Collective on TS 10829b7cd975SBarry Smith 10839b7cd975SBarry Smith Input Parameters: 10849b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 1085e162b725SBarry Smith . func - routine for evaluating the forcing function 10869b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 10870298fd71SBarry Smith function evaluation routine (may be NULL) 10889b7cd975SBarry Smith 10899b7cd975SBarry Smith Calling sequence of func: 1090e162b725SBarry Smith $ func (TS ts,PetscReal t,Vec f,void *ctx); 10919b7cd975SBarry Smith 10929b7cd975SBarry Smith + t - current timestep 1093e162b725SBarry Smith . f - output vector 10949b7cd975SBarry Smith - ctx - [optional] user-defined function context 10959b7cd975SBarry Smith 10969b7cd975SBarry Smith Notes: 10979b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1098e162b725SBarry 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 1099e162b725SBarry Smith definition of the problem you are solving and hence possibly introducing bugs. 1100e162b725SBarry Smith 1101e162b725SBarry Smith This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0 1102e162b725SBarry Smith 1103e162b725SBarry 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 1104e162b725SBarry Smith parameters can be passed in the ctx variable. 11059b7cd975SBarry Smith 11069b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 11079b7cd975SBarry Smith 11089b7cd975SBarry Smith Level: beginner 11099b7cd975SBarry Smith 11109b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 11119b7cd975SBarry Smith 11129b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 11139b7cd975SBarry Smith @*/ 1114e162b725SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx) 11159b7cd975SBarry Smith { 11169b7cd975SBarry Smith PetscErrorCode ierr; 11179b7cd975SBarry Smith DM dm; 11189b7cd975SBarry Smith 11199b7cd975SBarry Smith PetscFunctionBegin; 11209b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11219b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1122e162b725SBarry Smith ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr); 11239b7cd975SBarry Smith PetscFunctionReturn(0); 11249b7cd975SBarry Smith } 11259b7cd975SBarry Smith 1126d763cef2SBarry Smith /*@C 1127f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1128b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1129d763cef2SBarry Smith 11303f9fe445SBarry Smith Logically Collective on TS 1131d763cef2SBarry Smith 1132d763cef2SBarry Smith Input Parameters: 1133d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1134e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1135e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1136d763cef2SBarry Smith . f - the Jacobian evaluation routine 1137d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 11380298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1139d763cef2SBarry Smith 1140f7ab8db6SBarry Smith Calling sequence of f: 1141ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1142d763cef2SBarry Smith 1143d763cef2SBarry Smith + t - current timestep 1144d763cef2SBarry Smith . u - input vector 1145e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1146e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1147d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1148d763cef2SBarry Smith 11496cd88445SBarry Smith Notes: 11506cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 11516cd88445SBarry Smith 11526cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1153ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1154d763cef2SBarry Smith 1155d763cef2SBarry Smith Level: beginner 1156d763cef2SBarry Smith 1157d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1158d763cef2SBarry Smith 1159ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1160d763cef2SBarry Smith 1161d763cef2SBarry Smith @*/ 1162e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1163d763cef2SBarry Smith { 1164277b19d0SLisandro Dalcin PetscErrorCode ierr; 1165089b2837SJed Brown SNES snes; 116624989b8cSPeter Brune DM dm; 116724989b8cSPeter Brune TSIJacobian ijacobian; 1168277b19d0SLisandro Dalcin 1169d763cef2SBarry Smith PetscFunctionBegin; 11700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1171e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1172e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1173e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1174e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1175d763cef2SBarry Smith 117624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 117724989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1178e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1179e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1180e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1181e1244c69SJed Brown } 11820298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1183089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11845f659677SPeter Brune if (!ijacobian) { 1185e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 11860e4ef248SJed Brown } 1187e5d3d808SBarry Smith if (Amat) { 1188e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 11890e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1190e5d3d808SBarry Smith ts->Arhs = Amat; 11910e4ef248SJed Brown } 1192e5d3d808SBarry Smith if (Pmat) { 1193e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 11940e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1195e5d3d808SBarry Smith ts->Brhs = Pmat; 11960e4ef248SJed Brown } 1197d763cef2SBarry Smith PetscFunctionReturn(0); 1198d763cef2SBarry Smith } 1199d763cef2SBarry Smith 1200316643e7SJed Brown /*@C 1201b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1202316643e7SJed Brown 12033f9fe445SBarry Smith Logically Collective on TS 1204316643e7SJed Brown 1205316643e7SJed Brown Input Parameters: 1206316643e7SJed Brown + ts - the TS context obtained from TSCreate() 12070298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1208316643e7SJed Brown . f - the function evaluation routine 12090298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1210316643e7SJed Brown 1211316643e7SJed Brown Calling sequence of f: 1212316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1213316643e7SJed Brown 1214316643e7SJed Brown + t - time at step/stage being solved 1215316643e7SJed Brown . u - state vector 1216316643e7SJed Brown . u_t - time derivative of state vector 1217316643e7SJed Brown . F - function vector 1218316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1219316643e7SJed Brown 1220316643e7SJed Brown Important: 12212bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1222316643e7SJed Brown 1223316643e7SJed Brown Level: beginner 1224316643e7SJed Brown 1225316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1226316643e7SJed Brown 1227d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1228316643e7SJed Brown @*/ 122951699248SLisandro Dalcin PetscErrorCode TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx) 1230316643e7SJed Brown { 1231089b2837SJed Brown PetscErrorCode ierr; 1232089b2837SJed Brown SNES snes; 123351699248SLisandro Dalcin Vec ralloc = NULL; 123424989b8cSPeter Brune DM dm; 1235316643e7SJed Brown 1236316643e7SJed Brown PetscFunctionBegin; 12370700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 123851699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 123924989b8cSPeter Brune 124024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 124124989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 124224989b8cSPeter Brune 1243089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 124451699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 124551699248SLisandro Dalcin ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 124651699248SLisandro Dalcin r = ralloc; 1247e856ceecSJed Brown } 124851699248SLisandro Dalcin ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 124951699248SLisandro Dalcin ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1250089b2837SJed Brown PetscFunctionReturn(0); 1251089b2837SJed Brown } 1252089b2837SJed Brown 1253089b2837SJed Brown /*@C 1254089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1255089b2837SJed Brown 1256089b2837SJed Brown Not Collective 1257089b2837SJed Brown 1258089b2837SJed Brown Input Parameter: 1259089b2837SJed Brown . ts - the TS context 1260089b2837SJed Brown 1261089b2837SJed Brown Output Parameter: 12620298fd71SBarry Smith + r - vector to hold residual (or NULL) 12630298fd71SBarry Smith . func - the function to compute residual (or NULL) 12640298fd71SBarry Smith - ctx - the function context (or NULL) 1265089b2837SJed Brown 1266089b2837SJed Brown Level: advanced 1267089b2837SJed Brown 1268089b2837SJed Brown .keywords: TS, nonlinear, get, function 1269089b2837SJed Brown 1270089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1271089b2837SJed Brown @*/ 1272089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1273089b2837SJed Brown { 1274089b2837SJed Brown PetscErrorCode ierr; 1275089b2837SJed Brown SNES snes; 127624989b8cSPeter Brune DM dm; 1277089b2837SJed Brown 1278089b2837SJed Brown PetscFunctionBegin; 1279089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1280089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12810298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 128224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 128324989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1284089b2837SJed Brown PetscFunctionReturn(0); 1285089b2837SJed Brown } 1286089b2837SJed Brown 1287089b2837SJed Brown /*@C 1288089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1289089b2837SJed Brown 1290089b2837SJed Brown Not Collective 1291089b2837SJed Brown 1292089b2837SJed Brown Input Parameter: 1293089b2837SJed Brown . ts - the TS context 1294089b2837SJed Brown 1295089b2837SJed Brown Output Parameter: 12960298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 12970298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 12980298fd71SBarry Smith - ctx - the function context (or NULL) 1299089b2837SJed Brown 1300089b2837SJed Brown Level: advanced 1301089b2837SJed Brown 1302089b2837SJed Brown .keywords: TS, nonlinear, get, function 1303089b2837SJed Brown 13042bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1305089b2837SJed Brown @*/ 1306089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1307089b2837SJed Brown { 1308089b2837SJed Brown PetscErrorCode ierr; 1309089b2837SJed Brown SNES snes; 131024989b8cSPeter Brune DM dm; 1311089b2837SJed Brown 1312089b2837SJed Brown PetscFunctionBegin; 1313089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1314089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13150298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 131624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 131724989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1318316643e7SJed Brown PetscFunctionReturn(0); 1319316643e7SJed Brown } 1320316643e7SJed Brown 1321316643e7SJed Brown /*@C 1322a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1323ae8867d6SBarry Smith provided with TSSetIFunction(). 1324316643e7SJed Brown 13253f9fe445SBarry Smith Logically Collective on TS 1326316643e7SJed Brown 1327316643e7SJed Brown Input Parameters: 1328316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1329e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1330e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1331316643e7SJed Brown . f - the Jacobian evaluation routine 13320298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1333316643e7SJed Brown 1334316643e7SJed Brown Calling sequence of f: 1335ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1336316643e7SJed Brown 1337316643e7SJed Brown + t - time at step/stage being solved 13381b4a444bSJed Brown . U - state vector 13391b4a444bSJed Brown . U_t - time derivative of state vector 1340316643e7SJed Brown . a - shift 1341e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1342e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1343316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1344316643e7SJed Brown 1345316643e7SJed Brown Notes: 1346e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1347316643e7SJed Brown 1348895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1349895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1350895c21f2SBarry Smith 1351a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1352b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1353a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1354a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1355a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1356a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1357a4f0a591SBarry Smith 13586cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13596cd88445SBarry Smith 13606cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1361ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1362ca5f011dSBarry Smith 1363316643e7SJed Brown Level: beginner 1364316643e7SJed Brown 1365316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1366316643e7SJed Brown 1367ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1368316643e7SJed Brown 1369316643e7SJed Brown @*/ 1370e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1371316643e7SJed Brown { 1372316643e7SJed Brown PetscErrorCode ierr; 1373089b2837SJed Brown SNES snes; 137424989b8cSPeter Brune DM dm; 1375316643e7SJed Brown 1376316643e7SJed Brown PetscFunctionBegin; 13770700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1378e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1379e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1380e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1381e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 138224989b8cSPeter Brune 138324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 138424989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 138524989b8cSPeter Brune 1386089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1387e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1388316643e7SJed Brown PetscFunctionReturn(0); 1389316643e7SJed Brown } 1390316643e7SJed Brown 1391e1244c69SJed Brown /*@ 1392e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1393e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1394e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1395e1244c69SJed Brown not been changed by the TS. 1396e1244c69SJed Brown 1397e1244c69SJed Brown Logically Collective 1398e1244c69SJed Brown 1399e1244c69SJed Brown Input Arguments: 1400e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1401e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1402e1244c69SJed Brown 1403e1244c69SJed Brown Level: intermediate 1404e1244c69SJed Brown 1405e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1406e1244c69SJed Brown @*/ 1407e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1408e1244c69SJed Brown { 1409e1244c69SJed Brown PetscFunctionBegin; 1410e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1411e1244c69SJed Brown PetscFunctionReturn(0); 1412e1244c69SJed Brown } 1413e1244c69SJed Brown 1414efe9872eSLisandro Dalcin /*@C 1415efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1416efe9872eSLisandro Dalcin 1417efe9872eSLisandro Dalcin Logically Collective on TS 1418efe9872eSLisandro Dalcin 1419efe9872eSLisandro Dalcin Input Parameters: 1420efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1421efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1422efe9872eSLisandro Dalcin . fun - the function evaluation routine 1423efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1424efe9872eSLisandro Dalcin 1425efe9872eSLisandro Dalcin Calling sequence of fun: 1426efe9872eSLisandro Dalcin $ fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1427efe9872eSLisandro Dalcin 1428efe9872eSLisandro Dalcin + t - time at step/stage being solved 1429efe9872eSLisandro Dalcin . U - state vector 1430efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1431efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1432efe9872eSLisandro Dalcin . F - function vector 1433efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1434efe9872eSLisandro Dalcin 1435efe9872eSLisandro Dalcin Level: beginner 1436efe9872eSLisandro Dalcin 1437efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function 1438efe9872eSLisandro Dalcin 1439efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1440efe9872eSLisandro Dalcin @*/ 1441efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx) 1442efe9872eSLisandro Dalcin { 1443efe9872eSLisandro Dalcin DM dm; 1444efe9872eSLisandro Dalcin PetscErrorCode ierr; 1445efe9872eSLisandro Dalcin 1446efe9872eSLisandro Dalcin PetscFunctionBegin; 1447efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1448efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2); 1449efe9872eSLisandro Dalcin ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr); 1450efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1451efe9872eSLisandro Dalcin ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1452efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1453efe9872eSLisandro Dalcin } 1454efe9872eSLisandro Dalcin 1455efe9872eSLisandro Dalcin /*@C 1456efe9872eSLisandro Dalcin TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1457efe9872eSLisandro Dalcin 1458efe9872eSLisandro Dalcin Not Collective 1459efe9872eSLisandro Dalcin 1460efe9872eSLisandro Dalcin Input Parameter: 1461efe9872eSLisandro Dalcin . ts - the TS context 1462efe9872eSLisandro Dalcin 1463efe9872eSLisandro Dalcin Output Parameter: 1464efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1465efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1466efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1467efe9872eSLisandro Dalcin 1468efe9872eSLisandro Dalcin Level: advanced 1469efe9872eSLisandro Dalcin 1470efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function 1471efe9872eSLisandro Dalcin 1472efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction() 1473efe9872eSLisandro Dalcin @*/ 1474efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx) 1475efe9872eSLisandro Dalcin { 1476efe9872eSLisandro Dalcin PetscErrorCode ierr; 1477efe9872eSLisandro Dalcin SNES snes; 1478efe9872eSLisandro Dalcin DM dm; 1479efe9872eSLisandro Dalcin 1480efe9872eSLisandro Dalcin PetscFunctionBegin; 1481efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1482efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1483efe9872eSLisandro Dalcin ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 1484efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1485efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1486efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1487efe9872eSLisandro Dalcin } 1488efe9872eSLisandro Dalcin 1489efe9872eSLisandro Dalcin /*@C 1490bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1491efe9872eSLisandro Dalcin where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function(). 1492efe9872eSLisandro Dalcin 1493efe9872eSLisandro Dalcin Logically Collective on TS 1494efe9872eSLisandro Dalcin 1495efe9872eSLisandro Dalcin Input Parameters: 1496efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1497efe9872eSLisandro Dalcin . J - Jacobian matrix 1498efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1499efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1500efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1501efe9872eSLisandro Dalcin 1502efe9872eSLisandro Dalcin Calling sequence of jac: 1503bc77d74cSLisandro Dalcin $ jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx); 1504efe9872eSLisandro Dalcin 1505efe9872eSLisandro Dalcin + t - time at step/stage being solved 1506efe9872eSLisandro Dalcin . U - state vector 1507efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1508efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1509efe9872eSLisandro Dalcin . v - shift for U_t 1510efe9872eSLisandro Dalcin . a - shift for U_tt 1511efe9872eSLisandro 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 1512efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1513efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1514efe9872eSLisandro Dalcin 1515efe9872eSLisandro Dalcin Notes: 1516efe9872eSLisandro Dalcin The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve. 1517efe9872eSLisandro Dalcin 1518efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1519efe9872eSLisandro 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. 1520efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1521bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1522efe9872eSLisandro Dalcin 1523efe9872eSLisandro Dalcin Level: beginner 1524efe9872eSLisandro Dalcin 1525efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian 1526efe9872eSLisandro Dalcin 1527efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1528efe9872eSLisandro Dalcin @*/ 1529efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx) 1530efe9872eSLisandro Dalcin { 1531efe9872eSLisandro Dalcin DM dm; 1532efe9872eSLisandro Dalcin PetscErrorCode ierr; 1533efe9872eSLisandro Dalcin 1534efe9872eSLisandro Dalcin PetscFunctionBegin; 1535efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1536efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2); 1537efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3); 1538efe9872eSLisandro Dalcin ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr); 1539efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1540efe9872eSLisandro Dalcin ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1541efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1542efe9872eSLisandro Dalcin } 1543efe9872eSLisandro Dalcin 1544efe9872eSLisandro Dalcin /*@C 1545efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1546efe9872eSLisandro Dalcin 1547efe9872eSLisandro Dalcin Not Collective, but parallel objects are returned if TS is parallel 1548efe9872eSLisandro Dalcin 1549efe9872eSLisandro Dalcin Input Parameter: 1550efe9872eSLisandro Dalcin . ts - The TS context obtained from TSCreate() 1551efe9872eSLisandro Dalcin 1552efe9872eSLisandro Dalcin Output Parameters: 1553efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1554efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1555efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1556efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1557efe9872eSLisandro Dalcin 1558efe9872eSLisandro Dalcin Notes: You can pass in NULL for any return argument you do not need. 1559efe9872eSLisandro Dalcin 1560efe9872eSLisandro Dalcin Level: advanced 1561efe9872eSLisandro Dalcin 156280275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 1563efe9872eSLisandro Dalcin 1564efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian 1565efe9872eSLisandro Dalcin @*/ 1566efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx) 1567efe9872eSLisandro Dalcin { 1568efe9872eSLisandro Dalcin PetscErrorCode ierr; 1569efe9872eSLisandro Dalcin SNES snes; 1570efe9872eSLisandro Dalcin DM dm; 1571efe9872eSLisandro Dalcin 1572efe9872eSLisandro Dalcin PetscFunctionBegin; 1573efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1574efe9872eSLisandro Dalcin ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 1575efe9872eSLisandro Dalcin ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr); 1576efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1577efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1578efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1579efe9872eSLisandro Dalcin } 1580efe9872eSLisandro Dalcin 1581efe9872eSLisandro Dalcin /*@ 1582efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1583efe9872eSLisandro Dalcin 1584efe9872eSLisandro Dalcin Collective on TS and Vec 1585efe9872eSLisandro Dalcin 1586efe9872eSLisandro Dalcin Input Parameters: 1587efe9872eSLisandro Dalcin + ts - the TS context 1588efe9872eSLisandro Dalcin . t - current time 1589efe9872eSLisandro Dalcin . U - state vector 1590efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1591efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1592efe9872eSLisandro Dalcin 1593efe9872eSLisandro Dalcin Output Parameter: 1594efe9872eSLisandro Dalcin . F - the residual vector 1595efe9872eSLisandro Dalcin 1596efe9872eSLisandro Dalcin Note: 1597efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1598efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1599efe9872eSLisandro Dalcin 1600efe9872eSLisandro Dalcin Level: developer 1601efe9872eSLisandro Dalcin 1602efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector 1603efe9872eSLisandro Dalcin 1604efe9872eSLisandro Dalcin .seealso: TSSetI2Function() 1605efe9872eSLisandro Dalcin @*/ 1606efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F) 1607efe9872eSLisandro Dalcin { 1608efe9872eSLisandro Dalcin DM dm; 1609efe9872eSLisandro Dalcin TSI2Function I2Function; 1610efe9872eSLisandro Dalcin void *ctx; 1611efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1612efe9872eSLisandro Dalcin PetscErrorCode ierr; 1613efe9872eSLisandro Dalcin 1614efe9872eSLisandro Dalcin PetscFunctionBegin; 1615efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1616efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1617efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1618efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1619efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F,VEC_CLASSID,6); 1620efe9872eSLisandro Dalcin 1621efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1622efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr); 1623efe9872eSLisandro Dalcin ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 1624efe9872eSLisandro Dalcin 1625efe9872eSLisandro Dalcin if (!I2Function) { 1626efe9872eSLisandro Dalcin ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr); 1627efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1628efe9872eSLisandro Dalcin } 1629efe9872eSLisandro Dalcin 1630efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1631efe9872eSLisandro Dalcin 1632efe9872eSLisandro Dalcin PetscStackPush("TS user implicit function"); 1633efe9872eSLisandro Dalcin ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr); 1634efe9872eSLisandro Dalcin PetscStackPop; 1635efe9872eSLisandro Dalcin 1636efe9872eSLisandro Dalcin if (rhsfunction) { 1637efe9872eSLisandro Dalcin Vec Frhs; 1638efe9872eSLisandro Dalcin ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 1639efe9872eSLisandro Dalcin ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 1640efe9872eSLisandro Dalcin ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr); 1641efe9872eSLisandro Dalcin } 1642efe9872eSLisandro Dalcin 1643efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1644efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1645efe9872eSLisandro Dalcin } 1646efe9872eSLisandro Dalcin 1647efe9872eSLisandro Dalcin /*@ 1648efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1649efe9872eSLisandro Dalcin 1650efe9872eSLisandro Dalcin Collective on TS and Vec 1651efe9872eSLisandro Dalcin 1652efe9872eSLisandro Dalcin Input Parameters: 1653efe9872eSLisandro Dalcin + ts - the TS context 1654efe9872eSLisandro Dalcin . t - current timestep 1655efe9872eSLisandro Dalcin . U - state vector 1656efe9872eSLisandro Dalcin . V - time derivative of state vector 1657efe9872eSLisandro Dalcin . A - second time derivative of state vector 1658efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1659efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1660efe9872eSLisandro Dalcin 1661efe9872eSLisandro Dalcin Output Parameters: 1662efe9872eSLisandro Dalcin + J - Jacobian matrix 1663efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1664efe9872eSLisandro Dalcin 1665efe9872eSLisandro Dalcin Notes: 1666efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1667efe9872eSLisandro Dalcin 1668efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1669efe9872eSLisandro Dalcin 1670efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1671efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1672efe9872eSLisandro Dalcin 1673efe9872eSLisandro Dalcin Level: developer 1674efe9872eSLisandro Dalcin 1675efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix 1676efe9872eSLisandro Dalcin 1677efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1678efe9872eSLisandro Dalcin @*/ 1679efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P) 1680efe9872eSLisandro Dalcin { 1681efe9872eSLisandro Dalcin DM dm; 1682efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1683efe9872eSLisandro Dalcin void *ctx; 1684efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1685efe9872eSLisandro Dalcin PetscErrorCode ierr; 1686efe9872eSLisandro Dalcin 1687efe9872eSLisandro Dalcin PetscFunctionBegin; 1688efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1689efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1690efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1691efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1692efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J,MAT_CLASSID,8); 1693efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P,MAT_CLASSID,9); 1694efe9872eSLisandro Dalcin 1695efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1696efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr); 1697efe9872eSLisandro Dalcin ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 1698efe9872eSLisandro Dalcin 1699efe9872eSLisandro Dalcin if (!I2Jacobian) { 1700efe9872eSLisandro Dalcin ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr); 1701efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1702efe9872eSLisandro Dalcin } 1703efe9872eSLisandro Dalcin 1704efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1705efe9872eSLisandro Dalcin 1706efe9872eSLisandro Dalcin PetscStackPush("TS user implicit Jacobian"); 1707efe9872eSLisandro Dalcin ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr); 1708efe9872eSLisandro Dalcin PetscStackPop; 1709efe9872eSLisandro Dalcin 1710efe9872eSLisandro Dalcin if (rhsjacobian) { 1711efe9872eSLisandro Dalcin Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 1712efe9872eSLisandro Dalcin ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr); 1713efe9872eSLisandro Dalcin ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr); 1714efe9872eSLisandro Dalcin ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr); 1715efe9872eSLisandro Dalcin if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);} 1716efe9872eSLisandro Dalcin } 1717efe9872eSLisandro Dalcin 1718efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1719efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1720efe9872eSLisandro Dalcin } 1721efe9872eSLisandro Dalcin 1722efe9872eSLisandro Dalcin /*@ 1723efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1724efe9872eSLisandro Dalcin for use by the TS routines handling second order equations. 1725efe9872eSLisandro Dalcin 1726efe9872eSLisandro Dalcin Logically Collective on TS and Vec 1727efe9872eSLisandro Dalcin 1728efe9872eSLisandro Dalcin Input Parameters: 1729efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1730efe9872eSLisandro Dalcin . u - the solution vector 1731efe9872eSLisandro Dalcin - v - the time derivative vector 1732efe9872eSLisandro Dalcin 1733efe9872eSLisandro Dalcin Level: beginner 1734efe9872eSLisandro Dalcin 1735efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions 1736efe9872eSLisandro Dalcin @*/ 1737efe9872eSLisandro Dalcin PetscErrorCode TS2SetSolution(TS ts,Vec u,Vec v) 1738efe9872eSLisandro Dalcin { 1739efe9872eSLisandro Dalcin PetscErrorCode ierr; 1740efe9872eSLisandro Dalcin 1741efe9872eSLisandro Dalcin PetscFunctionBegin; 1742efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1743efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1744efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v,VEC_CLASSID,3); 1745efe9872eSLisandro Dalcin ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 1746efe9872eSLisandro Dalcin ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr); 1747efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 1748efe9872eSLisandro Dalcin ts->vec_dot = v; 1749efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1750efe9872eSLisandro Dalcin } 1751efe9872eSLisandro Dalcin 1752efe9872eSLisandro Dalcin /*@ 1753efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 1754efe9872eSLisandro Dalcin for second order equations. It is valid to call this routine inside the function 1755efe9872eSLisandro Dalcin that you are evaluating in order to move to the new timestep. This vector not 1756efe9872eSLisandro Dalcin changed until the solution at the next timestep has been calculated. 1757efe9872eSLisandro Dalcin 1758efe9872eSLisandro Dalcin Not Collective, but Vec returned is parallel if TS is parallel 1759efe9872eSLisandro Dalcin 1760efe9872eSLisandro Dalcin Input Parameter: 1761efe9872eSLisandro Dalcin . ts - the TS context obtained from TSCreate() 1762efe9872eSLisandro Dalcin 1763efe9872eSLisandro Dalcin Output Parameter: 1764efe9872eSLisandro Dalcin + u - the vector containing the solution 1765efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1766efe9872eSLisandro Dalcin 1767efe9872eSLisandro Dalcin Level: intermediate 1768efe9872eSLisandro Dalcin 1769efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime() 1770efe9872eSLisandro Dalcin 1771efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution 1772efe9872eSLisandro Dalcin @*/ 1773efe9872eSLisandro Dalcin PetscErrorCode TS2GetSolution(TS ts,Vec *u,Vec *v) 1774efe9872eSLisandro Dalcin { 1775efe9872eSLisandro Dalcin PetscFunctionBegin; 1776efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1777efe9872eSLisandro Dalcin if (u) PetscValidPointer(u,2); 1778efe9872eSLisandro Dalcin if (v) PetscValidPointer(v,3); 1779efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1780efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 1781efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1782efe9872eSLisandro Dalcin } 1783efe9872eSLisandro Dalcin 178455849f57SBarry Smith /*@C 178555849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 178655849f57SBarry Smith 178755849f57SBarry Smith Collective on PetscViewer 178855849f57SBarry Smith 178955849f57SBarry Smith Input Parameters: 179055849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 179155849f57SBarry Smith some related function before a call to TSLoad(). 179255849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 179355849f57SBarry Smith 179455849f57SBarry Smith Level: intermediate 179555849f57SBarry Smith 179655849f57SBarry Smith Notes: 179755849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 179855849f57SBarry Smith 179955849f57SBarry Smith Notes for advanced users: 180055849f57SBarry Smith Most users should not need to know the details of the binary storage 180155849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 180255849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 180355849f57SBarry Smith format is 180455849f57SBarry Smith .vb 180555849f57SBarry Smith has not yet been determined 180655849f57SBarry Smith .ve 180755849f57SBarry Smith 180855849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 180955849f57SBarry Smith @*/ 1810f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 181155849f57SBarry Smith { 181255849f57SBarry Smith PetscErrorCode ierr; 181355849f57SBarry Smith PetscBool isbinary; 181455849f57SBarry Smith PetscInt classid; 181555849f57SBarry Smith char type[256]; 18162d53ad75SBarry Smith DMTS sdm; 1817ad6bc421SBarry Smith DM dm; 181855849f57SBarry Smith 181955849f57SBarry Smith PetscFunctionBegin; 1820f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 182155849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 182255849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 182355849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 182455849f57SBarry Smith 1825060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1826ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1827060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1828f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1829f2c2a1b9SBarry Smith if (ts->ops->load) { 1830f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1831f2c2a1b9SBarry Smith } 1832ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1833ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1834ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1835f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1836f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 18372d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 18382d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 183955849f57SBarry Smith PetscFunctionReturn(0); 184055849f57SBarry Smith } 184155849f57SBarry Smith 18429804daf3SBarry Smith #include <petscdraw.h> 1843e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1844e04113cfSBarry Smith #include <petscviewersaws.h> 1845f05ece33SBarry Smith #endif 18467e2c5f70SBarry Smith /*@C 1847d763cef2SBarry Smith TSView - Prints the TS data structure. 1848d763cef2SBarry Smith 18494c49b128SBarry Smith Collective on TS 1850d763cef2SBarry Smith 1851d763cef2SBarry Smith Input Parameters: 1852d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1853d763cef2SBarry Smith - viewer - visualization context 1854d763cef2SBarry Smith 1855d763cef2SBarry Smith Options Database Key: 1856d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1857d763cef2SBarry Smith 1858d763cef2SBarry Smith Notes: 1859d763cef2SBarry Smith The available visualization contexts include 1860b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1861b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1862d763cef2SBarry Smith output where only the first processor opens 1863d763cef2SBarry Smith the file. All other processors send their 1864d763cef2SBarry Smith data to the first processor to print. 1865d763cef2SBarry Smith 1866d763cef2SBarry Smith The user can open an alternative visualization context with 1867b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1868d763cef2SBarry Smith 1869d763cef2SBarry Smith Level: beginner 1870d763cef2SBarry Smith 1871d763cef2SBarry Smith .keywords: TS, timestep, view 1872d763cef2SBarry Smith 1873b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1874d763cef2SBarry Smith @*/ 18757087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1876d763cef2SBarry Smith { 1877dfbe8321SBarry Smith PetscErrorCode ierr; 187819fd82e9SBarry Smith TSType type; 18792b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 18802d53ad75SBarry Smith DMTS sdm; 1881e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1882536b137fSBarry Smith PetscBool issaws; 1883f05ece33SBarry Smith #endif 1884d763cef2SBarry Smith 1885d763cef2SBarry Smith PetscFunctionBegin; 18860700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18873050cee2SBarry Smith if (!viewer) { 1888ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 18893050cee2SBarry Smith } 18900700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1891c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1892fd16b177SBarry Smith 1893251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1894251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 189555849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 18962b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1897e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1898536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1899f05ece33SBarry Smith #endif 190032077d6dSBarry Smith if (iascii) { 1901dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 1902efd4aadfSBarry Smith if (ts->ops->view) { 1903efd4aadfSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1904efd4aadfSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1905efd4aadfSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1906efd4aadfSBarry Smith } 1907ef85077eSLisandro Dalcin if (ts->max_steps < PETSC_MAX_INT) { 190877431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 1909ef85077eSLisandro Dalcin } 1910ef85077eSLisandro Dalcin if (ts->max_time < PETSC_MAX_REAL) { 19117c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1912ef85077eSLisandro Dalcin } 1913efd4aadfSBarry Smith if (ts->usessnes) { 1914efd4aadfSBarry Smith PetscBool lin; 1915d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 19165ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1917d763cef2SBarry Smith } 19185ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1919efd4aadfSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)ts->snes,SNESKSPONLY,&lin);CHKERRQ(ierr); 1920efd4aadfSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr); 1921efd4aadfSBarry Smith } 1922193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 1923a0af407cSBarry Smith if (ts->vrtol) { 1924a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of relative error tolerances, ");CHKERRQ(ierr); 1925a0af407cSBarry Smith } else { 1926a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr); 1927a0af407cSBarry Smith } 1928a0af407cSBarry Smith if (ts->vatol) { 1929a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of absolute error tolerances\n");CHKERRQ(ierr); 1930a0af407cSBarry Smith } else { 1931a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr); 1932a0af407cSBarry Smith } 1933efd4aadfSBarry Smith ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr); 1934efd4aadfSBarry Smith if (ts->snes && ts->usessnes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 19352d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19362d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 19370f5bd95cSBarry Smith } else if (isstring) { 1938a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1939b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 194055849f57SBarry Smith } else if (isbinary) { 194155849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 194255849f57SBarry Smith MPI_Comm comm; 194355849f57SBarry Smith PetscMPIInt rank; 194455849f57SBarry Smith char type[256]; 194555849f57SBarry Smith 194655849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 194755849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 194855849f57SBarry Smith if (!rank) { 194955849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 195055849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 195155849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 195255849f57SBarry Smith } 195355849f57SBarry Smith if (ts->ops->view) { 195455849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 195555849f57SBarry Smith } 1956efd4aadfSBarry Smith if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);} 1957f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1958f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 19592d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19602d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 19612b0a91c0SBarry Smith } else if (isdraw) { 19622b0a91c0SBarry Smith PetscDraw draw; 19632b0a91c0SBarry Smith char str[36]; 196489fd9fafSBarry Smith PetscReal x,y,bottom,h; 19652b0a91c0SBarry Smith 19662b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 19672b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 19682b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 19692b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 197051fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 197189fd9fafSBarry Smith bottom = y - h; 19722b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 19732b0a91c0SBarry Smith if (ts->ops->view) { 19742b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 19752b0a91c0SBarry Smith } 1976efd4aadfSBarry Smith if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);} 1977efd4aadfSBarry Smith if (ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 19782b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1979e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1980536b137fSBarry Smith } else if (issaws) { 1981d45a07a7SBarry Smith PetscMPIInt rank; 19822657e9d9SBarry Smith const char *name; 19832657e9d9SBarry Smith 19842657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1985d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1986d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1987d45a07a7SBarry Smith char dir[1024]; 1988d45a07a7SBarry Smith 1989e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1990a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 19912657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 19922657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 19932657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1994d763cef2SBarry Smith } 19950acecf5bSBarry Smith if (ts->ops->view) { 19960acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 19970acecf5bSBarry Smith } 1998f05ece33SBarry Smith #endif 1999f05ece33SBarry Smith } 2000f05ece33SBarry Smith 2001b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2002251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 2003b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2004d763cef2SBarry Smith PetscFunctionReturn(0); 2005d763cef2SBarry Smith } 2006d763cef2SBarry Smith 2007b07ff414SBarry Smith /*@ 2008d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2009d763cef2SBarry Smith the timesteppers. 2010d763cef2SBarry Smith 20113f9fe445SBarry Smith Logically Collective on TS 2012d763cef2SBarry Smith 2013d763cef2SBarry Smith Input Parameters: 2014d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2015d763cef2SBarry Smith - usrP - optional user context 2016d763cef2SBarry Smith 2017daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 2018daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2019daf670e6SBarry Smith 2020d763cef2SBarry Smith Level: intermediate 2021d763cef2SBarry Smith 2022d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 2023d763cef2SBarry Smith 2024d763cef2SBarry Smith .seealso: TSGetApplicationContext() 2025d763cef2SBarry Smith @*/ 20267087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 2027d763cef2SBarry Smith { 2028d763cef2SBarry Smith PetscFunctionBegin; 20290700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2030d763cef2SBarry Smith ts->user = usrP; 2031d763cef2SBarry Smith PetscFunctionReturn(0); 2032d763cef2SBarry Smith } 2033d763cef2SBarry Smith 2034b07ff414SBarry Smith /*@ 2035d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2036d763cef2SBarry Smith timestepper. 2037d763cef2SBarry Smith 2038d763cef2SBarry Smith Not Collective 2039d763cef2SBarry Smith 2040d763cef2SBarry Smith Input Parameter: 2041d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2042d763cef2SBarry Smith 2043d763cef2SBarry Smith Output Parameter: 2044d763cef2SBarry Smith . usrP - user context 2045d763cef2SBarry Smith 2046daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 2047daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2048daf670e6SBarry Smith 2049d763cef2SBarry Smith Level: intermediate 2050d763cef2SBarry Smith 2051d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 2052d763cef2SBarry Smith 2053d763cef2SBarry Smith .seealso: TSSetApplicationContext() 2054d763cef2SBarry Smith @*/ 2055e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 2056d763cef2SBarry Smith { 2057d763cef2SBarry Smith PetscFunctionBegin; 20580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2059e71120c6SJed Brown *(void**)usrP = ts->user; 2060d763cef2SBarry Smith PetscFunctionReturn(0); 2061d763cef2SBarry Smith } 2062d763cef2SBarry Smith 2063d763cef2SBarry Smith /*@ 206480275a0aSLisandro Dalcin TSGetStepNumber - Gets the number of steps completed. 2065d763cef2SBarry Smith 2066d763cef2SBarry Smith Not Collective 2067d763cef2SBarry Smith 2068d763cef2SBarry Smith Input Parameter: 2069d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2070d763cef2SBarry Smith 2071d763cef2SBarry Smith Output Parameter: 207280275a0aSLisandro Dalcin . steps - number of steps completed so far 2073d763cef2SBarry Smith 2074d763cef2SBarry Smith Level: intermediate 2075d763cef2SBarry Smith 2076d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 20779be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 2078d763cef2SBarry Smith @*/ 207980275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps) 2080d763cef2SBarry Smith { 2081d763cef2SBarry Smith PetscFunctionBegin; 20820700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 208380275a0aSLisandro Dalcin PetscValidIntPointer(steps,2); 208480275a0aSLisandro Dalcin *steps = ts->steps; 208580275a0aSLisandro Dalcin PetscFunctionReturn(0); 208680275a0aSLisandro Dalcin } 208780275a0aSLisandro Dalcin 208880275a0aSLisandro Dalcin /*@ 208980275a0aSLisandro Dalcin TSSetStepNumber - Sets the number of steps completed. 209080275a0aSLisandro Dalcin 209180275a0aSLisandro Dalcin Logically Collective on TS 209280275a0aSLisandro Dalcin 209380275a0aSLisandro Dalcin Input Parameters: 209480275a0aSLisandro Dalcin + ts - the TS context 209580275a0aSLisandro Dalcin - steps - number of steps completed so far 209680275a0aSLisandro Dalcin 209780275a0aSLisandro Dalcin Notes: 209880275a0aSLisandro Dalcin For most uses of the TS solvers the user need not explicitly call 209980275a0aSLisandro Dalcin TSSetStepNumber(), as the step counter is appropriately updated in 210080275a0aSLisandro Dalcin TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to 210180275a0aSLisandro Dalcin reinitialize timestepping by setting the step counter to zero (and time 210280275a0aSLisandro Dalcin to the initial time) to solve a similar problem with different initial 210380275a0aSLisandro Dalcin conditions or parameters. Other possible use case is to continue 210480275a0aSLisandro Dalcin timestepping from a previously interrupted run in such a way that TS 210580275a0aSLisandro Dalcin monitors will be called with a initial nonzero step counter. 210680275a0aSLisandro Dalcin 210780275a0aSLisandro Dalcin Level: advanced 210880275a0aSLisandro Dalcin 210980275a0aSLisandro Dalcin .keywords: TS, timestep, set, iteration, number 211080275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution() 211180275a0aSLisandro Dalcin @*/ 211280275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps) 211380275a0aSLisandro Dalcin { 211480275a0aSLisandro Dalcin PetscFunctionBegin; 211580275a0aSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 211680275a0aSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,steps,2); 211780275a0aSLisandro Dalcin if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative"); 211880275a0aSLisandro Dalcin ts->steps = steps; 2119d763cef2SBarry Smith PetscFunctionReturn(0); 2120d763cef2SBarry Smith } 2121d763cef2SBarry Smith 2122d763cef2SBarry Smith /*@ 2123d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2124d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2125d763cef2SBarry Smith 21263f9fe445SBarry Smith Logically Collective on TS 2127d763cef2SBarry Smith 2128d763cef2SBarry Smith Input Parameters: 2129d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2130d763cef2SBarry Smith - time_step - the size of the timestep 2131d763cef2SBarry Smith 2132d763cef2SBarry Smith Level: intermediate 2133d763cef2SBarry Smith 2134aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime() 2135d763cef2SBarry Smith 2136d763cef2SBarry Smith .keywords: TS, set, timestep 2137d763cef2SBarry Smith @*/ 21387087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 2139d763cef2SBarry Smith { 2140d763cef2SBarry Smith PetscFunctionBegin; 21410700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2142c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 2143d763cef2SBarry Smith ts->time_step = time_step; 2144d763cef2SBarry Smith PetscFunctionReturn(0); 2145d763cef2SBarry Smith } 2146d763cef2SBarry Smith 2147a43b19c4SJed Brown /*@ 214849354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 214949354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 215049354f04SShri Abhyankar or just return at the final time TS computed. 2151a43b19c4SJed Brown 2152a43b19c4SJed Brown Logically Collective on TS 2153a43b19c4SJed Brown 2154a43b19c4SJed Brown Input Parameter: 2155a43b19c4SJed Brown + ts - the time-step context 215649354f04SShri Abhyankar - eftopt - exact final time option 2157a43b19c4SJed Brown 2158feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2159feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2160feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2161feed9e9dSBarry Smith 2162feed9e9dSBarry Smith Options Database: 2163feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2164feed9e9dSBarry Smith 2165ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 2166ee346746SBarry Smith then the final time you selected. 2167ee346746SBarry Smith 2168a43b19c4SJed Brown Level: beginner 2169a43b19c4SJed Brown 2170f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime() 2171a43b19c4SJed Brown @*/ 217249354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 2173a43b19c4SJed Brown { 2174a43b19c4SJed Brown PetscFunctionBegin; 2175a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 217649354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 217749354f04SShri Abhyankar ts->exact_final_time = eftopt; 2178a43b19c4SJed Brown PetscFunctionReturn(0); 2179a43b19c4SJed Brown } 2180a43b19c4SJed Brown 2181d763cef2SBarry Smith /*@ 2182f6953c82SLisandro Dalcin TSGetExactFinalTime - Gets the exact final time option. 2183f6953c82SLisandro Dalcin 2184f6953c82SLisandro Dalcin Not Collective 2185f6953c82SLisandro Dalcin 2186f6953c82SLisandro Dalcin Input Parameter: 2187f6953c82SLisandro Dalcin . ts - the TS context 2188f6953c82SLisandro Dalcin 2189f6953c82SLisandro Dalcin Output Parameter: 2190f6953c82SLisandro Dalcin . eftopt - exact final time option 2191f6953c82SLisandro Dalcin 2192f6953c82SLisandro Dalcin Level: beginner 2193f6953c82SLisandro Dalcin 2194f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime() 2195f6953c82SLisandro Dalcin @*/ 2196f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt) 2197f6953c82SLisandro Dalcin { 2198f6953c82SLisandro Dalcin PetscFunctionBegin; 2199f6953c82SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2200f6953c82SLisandro Dalcin PetscValidPointer(eftopt,2); 2201f6953c82SLisandro Dalcin *eftopt = ts->exact_final_time; 2202f6953c82SLisandro Dalcin PetscFunctionReturn(0); 2203f6953c82SLisandro Dalcin } 2204f6953c82SLisandro Dalcin 2205f6953c82SLisandro Dalcin /*@ 2206d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2207d763cef2SBarry Smith 2208d763cef2SBarry Smith Not Collective 2209d763cef2SBarry Smith 2210d763cef2SBarry Smith Input Parameter: 2211d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2212d763cef2SBarry Smith 2213d763cef2SBarry Smith Output Parameter: 2214d763cef2SBarry Smith . dt - the current timestep size 2215d763cef2SBarry Smith 2216d763cef2SBarry Smith Level: intermediate 2217d763cef2SBarry Smith 2218aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime() 2219d763cef2SBarry Smith 2220d763cef2SBarry Smith .keywords: TS, get, timestep 2221d763cef2SBarry Smith @*/ 22227087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 2223d763cef2SBarry Smith { 2224d763cef2SBarry Smith PetscFunctionBegin; 22250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2226f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 2227d763cef2SBarry Smith *dt = ts->time_step; 2228d763cef2SBarry Smith PetscFunctionReturn(0); 2229d763cef2SBarry Smith } 2230d763cef2SBarry Smith 2231d8e5e3e6SSatish Balay /*@ 2232d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2233d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2234d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2235d763cef2SBarry Smith the solution at the next timestep has been calculated. 2236d763cef2SBarry Smith 2237d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 2238d763cef2SBarry Smith 2239d763cef2SBarry Smith Input Parameter: 2240d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2241d763cef2SBarry Smith 2242d763cef2SBarry Smith Output Parameter: 2243d763cef2SBarry Smith . v - the vector containing the solution 2244d763cef2SBarry Smith 224563e21af5SBarry Smith Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested 224663e21af5SBarry Smith final time. It returns the solution at the next timestep. 224763e21af5SBarry Smith 2248d763cef2SBarry Smith Level: intermediate 2249d763cef2SBarry Smith 22500ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction() 2251d763cef2SBarry Smith 2252d763cef2SBarry Smith .keywords: TS, timestep, get, solution 2253d763cef2SBarry Smith @*/ 22547087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 2255d763cef2SBarry Smith { 2256d763cef2SBarry Smith PetscFunctionBegin; 22570700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22584482741eSBarry Smith PetscValidPointer(v,2); 22598737fe31SLisandro Dalcin *v = ts->vec_sol; 2260d763cef2SBarry Smith PetscFunctionReturn(0); 2261d763cef2SBarry Smith } 2262d763cef2SBarry Smith 226303fe5f5eSDebojyoti Ghosh /*@ 2264b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 226503fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2266b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 226703fe5f5eSDebojyoti Ghosh structure as the solution vector. 226803fe5f5eSDebojyoti Ghosh 226903fe5f5eSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 227003fe5f5eSDebojyoti Ghosh 227103fe5f5eSDebojyoti Ghosh Parameters : 227203fe5f5eSDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 2273b2bf4f3aSDebojyoti Ghosh . n - If v is PETSC_NULL, then the number of solution components is 2274b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 227503fe5f5eSDebojyoti Ghosh returned in v. 2276b2bf4f3aSDebojyoti Ghosh . v - the vector containing the n-th solution component 227703fe5f5eSDebojyoti Ghosh (may be PETSC_NULL to use this function to find out 2278b2bf4f3aSDebojyoti Ghosh the number of solutions components). 227903fe5f5eSDebojyoti Ghosh 22804cdd57e5SDebojyoti Ghosh Level: advanced 228103fe5f5eSDebojyoti Ghosh 228203fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution() 228303fe5f5eSDebojyoti Ghosh 228403fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution 228503fe5f5eSDebojyoti Ghosh @*/ 2286b2bf4f3aSDebojyoti Ghosh PetscErrorCode TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v) 228703fe5f5eSDebojyoti Ghosh { 228803fe5f5eSDebojyoti Ghosh PetscErrorCode ierr; 228903fe5f5eSDebojyoti Ghosh 229003fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 229103fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2292b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 229303fe5f5eSDebojyoti Ghosh else { 2294b2bf4f3aSDebojyoti Ghosh ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr); 229503fe5f5eSDebojyoti Ghosh } 229603fe5f5eSDebojyoti Ghosh PetscFunctionReturn(0); 229703fe5f5eSDebojyoti Ghosh } 229803fe5f5eSDebojyoti Ghosh 22994cdd57e5SDebojyoti Ghosh /*@ 23004cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 23014cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 23024cdd57e5SDebojyoti Ghosh 23034cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 23044cdd57e5SDebojyoti Ghosh 23054cdd57e5SDebojyoti Ghosh Parameters : 23064cdd57e5SDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 23074cdd57e5SDebojyoti Ghosh . v - the vector containing the auxiliary solution 23084cdd57e5SDebojyoti Ghosh 23094cdd57e5SDebojyoti Ghosh Level: intermediate 23104cdd57e5SDebojyoti Ghosh 23114cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution() 23124cdd57e5SDebojyoti Ghosh 23134cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution 23144cdd57e5SDebojyoti Ghosh @*/ 23154cdd57e5SDebojyoti Ghosh PetscErrorCode TSGetAuxSolution(TS ts,Vec *v) 23164cdd57e5SDebojyoti Ghosh { 23174cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 23184cdd57e5SDebojyoti Ghosh 23194cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23204cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2321f6356ec7SDebojyoti Ghosh if (ts->ops->getauxsolution) { 23224cdd57e5SDebojyoti Ghosh ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr); 2323f6356ec7SDebojyoti Ghosh } else { 2324f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v); CHKERRQ(ierr); 2325f6356ec7SDebojyoti Ghosh } 23264cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 23274cdd57e5SDebojyoti Ghosh } 23284cdd57e5SDebojyoti Ghosh 23294cdd57e5SDebojyoti Ghosh /*@ 23304cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 23314cdd57e5SDebojyoti Ghosh TSType has an error estimation functionality. 23324cdd57e5SDebojyoti Ghosh 23334cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 23344cdd57e5SDebojyoti Ghosh 23359657682dSDebojyoti Ghosh Note: MUST call after TSSetUp() 23369657682dSDebojyoti Ghosh 23374cdd57e5SDebojyoti Ghosh Parameters : 23384cdd57e5SDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 2339657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 23404cdd57e5SDebojyoti Ghosh . v - the vector containing the error (same size as the solution). 23414cdd57e5SDebojyoti Ghosh 23424cdd57e5SDebojyoti Ghosh Level: intermediate 23434cdd57e5SDebojyoti Ghosh 234457df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError() 23454cdd57e5SDebojyoti Ghosh 23464cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error 23474cdd57e5SDebojyoti Ghosh @*/ 23480a01e1b2SEmil Constantinescu PetscErrorCode TSGetTimeError(TS ts,PetscInt n,Vec *v) 23494cdd57e5SDebojyoti Ghosh { 23504cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 23514cdd57e5SDebojyoti Ghosh 23524cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23534cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2354f6356ec7SDebojyoti Ghosh if (ts->ops->gettimeerror) { 23550a01e1b2SEmil Constantinescu ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr); 2356f6356ec7SDebojyoti Ghosh } else { 2357f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v);CHKERRQ(ierr); 2358f6356ec7SDebojyoti Ghosh } 23594cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 23604cdd57e5SDebojyoti Ghosh } 23614cdd57e5SDebojyoti Ghosh 236257df6a1bSDebojyoti Ghosh /*@ 236357df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 236457df6a1bSDebojyoti Ghosh TSType has an error estimation functionality. This can be used 236557df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 236657df6a1bSDebojyoti Ghosh 236757df6a1bSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 236857df6a1bSDebojyoti Ghosh 236957df6a1bSDebojyoti Ghosh Parameters : 237057df6a1bSDebojyoti Ghosh . ts - the TS context obtained from TSCreate() (input parameter). 237157df6a1bSDebojyoti Ghosh . v - the vector containing the error (same size as the solution). 237257df6a1bSDebojyoti Ghosh 237357df6a1bSDebojyoti Ghosh Level: intermediate 237457df6a1bSDebojyoti Ghosh 237557df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError) 237657df6a1bSDebojyoti Ghosh 237757df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error 237857df6a1bSDebojyoti Ghosh @*/ 237957df6a1bSDebojyoti Ghosh PetscErrorCode TSSetTimeError(TS ts,Vec v) 238057df6a1bSDebojyoti Ghosh { 238157df6a1bSDebojyoti Ghosh PetscErrorCode ierr; 238257df6a1bSDebojyoti Ghosh 238357df6a1bSDebojyoti Ghosh PetscFunctionBegin; 238457df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 23859657682dSDebojyoti Ghosh if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first"); 238657df6a1bSDebojyoti Ghosh if (ts->ops->settimeerror) { 238757df6a1bSDebojyoti Ghosh ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr); 238857df6a1bSDebojyoti Ghosh } 238957df6a1bSDebojyoti Ghosh PetscFunctionReturn(0); 239057df6a1bSDebojyoti Ghosh } 239157df6a1bSDebojyoti Ghosh 2392bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2393d8e5e3e6SSatish Balay /*@ 2394bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2395d763cef2SBarry Smith 2396bdad233fSMatthew Knepley Not collective 2397d763cef2SBarry Smith 2398bdad233fSMatthew Knepley Input Parameters: 2399bdad233fSMatthew Knepley + ts - The TS 2400bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2401d763cef2SBarry Smith .vb 24020910c330SBarry Smith U_t - A U = 0 (linear) 24030910c330SBarry Smith U_t - A(t) U = 0 (linear) 24040910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2405d763cef2SBarry Smith .ve 2406d763cef2SBarry Smith 2407d763cef2SBarry Smith Level: beginner 2408d763cef2SBarry Smith 2409bdad233fSMatthew Knepley .keywords: TS, problem type 2410bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2411d763cef2SBarry Smith @*/ 24127087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2413a7cc72afSBarry Smith { 24149e2a6581SJed Brown PetscErrorCode ierr; 24159e2a6581SJed Brown 2416d763cef2SBarry Smith PetscFunctionBegin; 24170700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2418bdad233fSMatthew Knepley ts->problem_type = type; 24199e2a6581SJed Brown if (type == TS_LINEAR) { 24209e2a6581SJed Brown SNES snes; 24219e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 24229e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 24239e2a6581SJed Brown } 2424d763cef2SBarry Smith PetscFunctionReturn(0); 2425d763cef2SBarry Smith } 2426d763cef2SBarry Smith 2427bdad233fSMatthew Knepley /*@C 2428bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2429bdad233fSMatthew Knepley 2430bdad233fSMatthew Knepley Not collective 2431bdad233fSMatthew Knepley 2432bdad233fSMatthew Knepley Input Parameter: 2433bdad233fSMatthew Knepley . ts - The TS 2434bdad233fSMatthew Knepley 2435bdad233fSMatthew Knepley Output Parameter: 2436bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2437bdad233fSMatthew Knepley .vb 2438089b2837SJed Brown M U_t = A U 2439089b2837SJed Brown M(t) U_t = A(t) U 2440b5abc632SBarry Smith F(t,U,U_t) 2441bdad233fSMatthew Knepley .ve 2442bdad233fSMatthew Knepley 2443bdad233fSMatthew Knepley Level: beginner 2444bdad233fSMatthew Knepley 2445bdad233fSMatthew Knepley .keywords: TS, problem type 2446bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2447bdad233fSMatthew Knepley @*/ 24487087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2449a7cc72afSBarry Smith { 2450bdad233fSMatthew Knepley PetscFunctionBegin; 24510700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 24524482741eSBarry Smith PetscValidIntPointer(type,2); 2453bdad233fSMatthew Knepley *type = ts->problem_type; 2454bdad233fSMatthew Knepley PetscFunctionReturn(0); 2455bdad233fSMatthew Knepley } 2456d763cef2SBarry Smith 2457d763cef2SBarry Smith /*@ 2458d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 2459d763cef2SBarry Smith of a timestepper. 2460d763cef2SBarry Smith 2461d763cef2SBarry Smith Collective on TS 2462d763cef2SBarry Smith 2463d763cef2SBarry Smith Input Parameter: 2464d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2465d763cef2SBarry Smith 2466d763cef2SBarry Smith Notes: 2467d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 2468d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 2469141bd67dSStefano Zampini the call to TSStep() or TSSolve(). However, if one wishes to control this 2470d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 2471141bd67dSStefano Zampini and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve(). 2472d763cef2SBarry Smith 2473d763cef2SBarry Smith Level: advanced 2474d763cef2SBarry Smith 2475d763cef2SBarry Smith .keywords: TS, timestep, setup 2476d763cef2SBarry Smith 2477141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve() 2478d763cef2SBarry Smith @*/ 24797087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 2480d763cef2SBarry Smith { 2481dfbe8321SBarry Smith PetscErrorCode ierr; 24826c6b9e74SPeter Brune DM dm; 24836c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 2484d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 2485cd11d68dSLisandro Dalcin TSIFunction ifun; 24866c6b9e74SPeter Brune TSIJacobian ijac; 2487efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 24886c6b9e74SPeter Brune TSRHSJacobian rhsjac; 24892ffb9264SLisandro Dalcin PetscBool isnone; 2490d763cef2SBarry Smith 2491d763cef2SBarry Smith PetscFunctionBegin; 24920700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2493277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 2494277b19d0SLisandro Dalcin 24957adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 2496cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 2497cd11d68dSLisandro Dalcin ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr); 2498d763cef2SBarry Smith } 2499277b19d0SLisandro Dalcin 2500277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 2501277b19d0SLisandro Dalcin 2502e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 2503e1244c69SJed Brown Mat Amat,Pmat; 2504e1244c69SJed Brown SNES snes; 2505e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2506e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 2507e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2508e1244c69SJed Brown * have displaced the RHS matrix */ 2509e1244c69SJed Brown if (Amat == ts->Arhs) { 2510abc0d4abSBarry 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 */ 2511abc0d4abSBarry Smith ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr); 2512e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 2513e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 2514e1244c69SJed Brown } 2515e1244c69SJed Brown if (Pmat == ts->Brhs) { 2516abc0d4abSBarry Smith ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 2517e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 2518e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 2519e1244c69SJed Brown } 2520e1244c69SJed Brown } 25212ffb9264SLisandro Dalcin 25222ffb9264SLisandro Dalcin ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 25232ffb9264SLisandro Dalcin ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr); 25242ffb9264SLisandro Dalcin 2525277b19d0SLisandro Dalcin if (ts->ops->setup) { 2526000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 2527277b19d0SLisandro Dalcin } 2528277b19d0SLisandro Dalcin 25292ffb9264SLisandro Dalcin /* Attempt to check/preset a default value for the exact final time option */ 25302ffb9264SLisandro Dalcin ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr); 25312ffb9264SLisandro Dalcin if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) 25322ffb9264SLisandro Dalcin ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP; 25332ffb9264SLisandro Dalcin 2534a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 25356c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 25366c6b9e74SPeter Brune */ 25376c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 25380298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 25396c6b9e74SPeter Brune if (!func) { 25406c6b9e74SPeter Brune ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 25416c6b9e74SPeter Brune } 2542a6772fa2SLisandro 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. 25436c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 25446c6b9e74SPeter Brune */ 25450298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 25460298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 2547efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr); 25480298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 2549efe9872eSLisandro Dalcin if (!jac && (ijac || i2jac || rhsjac)) { 25506c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 25516c6b9e74SPeter Brune } 2552c0517034SDebojyoti Ghosh 2553c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 2554c0517034SDebojyoti Ghosh if (ts->ops->startingmethod) { 2555c0517034SDebojyoti Ghosh ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr); 2556c0517034SDebojyoti Ghosh } 2557c0517034SDebojyoti Ghosh 2558277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2559277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2560277b19d0SLisandro Dalcin } 2561277b19d0SLisandro Dalcin 2562f6a906c0SBarry Smith /*@ 2563277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2564277b19d0SLisandro Dalcin 2565277b19d0SLisandro Dalcin Collective on TS 2566277b19d0SLisandro Dalcin 2567277b19d0SLisandro Dalcin Input Parameter: 2568277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2569277b19d0SLisandro Dalcin 2570277b19d0SLisandro Dalcin Level: beginner 2571277b19d0SLisandro Dalcin 2572277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 2573277b19d0SLisandro Dalcin 2574277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2575277b19d0SLisandro Dalcin @*/ 2576277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2577277b19d0SLisandro Dalcin { 2578277b19d0SLisandro Dalcin PetscErrorCode ierr; 2579277b19d0SLisandro Dalcin 2580277b19d0SLisandro Dalcin PetscFunctionBegin; 2581277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2582b18ea86cSHong Zhang 2583277b19d0SLisandro Dalcin if (ts->ops->reset) { 2584277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2585277b19d0SLisandro Dalcin } 2586277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2587e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2588bbd56ea5SKarl Rupp 25894e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 25904e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2591214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 25926bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2593efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 2594e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2595e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 259638637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2597bbd56ea5SKarl Rupp 2598d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2599d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2600715f1b00SHong Zhang 2601ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 26022c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 260336eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 260413b1b0ffSHong Zhang ierr = MatDestroy(&ts->mat_sensip);CHKERRQ(ierr); 2605022c081aSHong Zhang 2606277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2607d763cef2SBarry Smith PetscFunctionReturn(0); 2608d763cef2SBarry Smith } 2609d763cef2SBarry Smith 2610d8e5e3e6SSatish Balay /*@ 2611d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2612d763cef2SBarry Smith with TSCreate(). 2613d763cef2SBarry Smith 2614d763cef2SBarry Smith Collective on TS 2615d763cef2SBarry Smith 2616d763cef2SBarry Smith Input Parameter: 2617d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2618d763cef2SBarry Smith 2619d763cef2SBarry Smith Level: beginner 2620d763cef2SBarry Smith 2621d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2622d763cef2SBarry Smith 2623d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2624d763cef2SBarry Smith @*/ 26256bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2626d763cef2SBarry Smith { 26276849ba73SBarry Smith PetscErrorCode ierr; 2628d763cef2SBarry Smith 2629d763cef2SBarry Smith PetscFunctionBegin; 26306bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 26316bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 26326bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2633d763cef2SBarry Smith 26346bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2635277b19d0SLisandro Dalcin 2636e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2637e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 26386bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 26396d4c513bSLisandro Dalcin 2640bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2641bc952696SBarry Smith 264284df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 26436427ac75SLisandro Dalcin ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr); 26446427ac75SLisandro Dalcin 26456bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 26466bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 26476bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 26480dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 26496d4c513bSLisandro Dalcin 2650a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2651d763cef2SBarry Smith PetscFunctionReturn(0); 2652d763cef2SBarry Smith } 2653d763cef2SBarry Smith 2654d8e5e3e6SSatish Balay /*@ 2655d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2656d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2657d763cef2SBarry Smith 2658d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2659d763cef2SBarry Smith 2660d763cef2SBarry Smith Input Parameter: 2661d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2662d763cef2SBarry Smith 2663d763cef2SBarry Smith Output Parameter: 2664d763cef2SBarry Smith . snes - the nonlinear solver context 2665d763cef2SBarry Smith 2666d763cef2SBarry Smith Notes: 2667d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2668d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 266994b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2670d763cef2SBarry Smith 2671d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 26720298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2673d763cef2SBarry Smith 2674d763cef2SBarry Smith Level: beginner 2675d763cef2SBarry Smith 2676d763cef2SBarry Smith .keywords: timestep, get, SNES 2677d763cef2SBarry Smith @*/ 26787087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2679d763cef2SBarry Smith { 2680d372ba47SLisandro Dalcin PetscErrorCode ierr; 2681d372ba47SLisandro Dalcin 2682d763cef2SBarry Smith PetscFunctionBegin; 26830700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26844482741eSBarry Smith PetscValidPointer(snes,2); 2685d372ba47SLisandro Dalcin if (!ts->snes) { 2686ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 26870298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 26883bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2689d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2690496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 26919e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 26929e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 26939e2a6581SJed Brown } 2694d372ba47SLisandro Dalcin } 2695d763cef2SBarry Smith *snes = ts->snes; 2696d763cef2SBarry Smith PetscFunctionReturn(0); 2697d763cef2SBarry Smith } 2698d763cef2SBarry Smith 2699deb2cd25SJed Brown /*@ 2700deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2701deb2cd25SJed Brown 2702deb2cd25SJed Brown Collective 2703deb2cd25SJed Brown 2704deb2cd25SJed Brown Input Parameter: 2705deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2706deb2cd25SJed Brown - snes - the nonlinear solver context 2707deb2cd25SJed Brown 2708deb2cd25SJed Brown Notes: 2709deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2710deb2cd25SJed Brown 2711deb2cd25SJed Brown Level: developer 2712deb2cd25SJed Brown 2713deb2cd25SJed Brown .keywords: timestep, set, SNES 2714deb2cd25SJed Brown @*/ 2715deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2716deb2cd25SJed Brown { 2717deb2cd25SJed Brown PetscErrorCode ierr; 2718d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2719deb2cd25SJed Brown 2720deb2cd25SJed Brown PetscFunctionBegin; 2721deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2722deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2723deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2724deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2725bbd56ea5SKarl Rupp 2726deb2cd25SJed Brown ts->snes = snes; 2727bbd56ea5SKarl Rupp 27280298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 27290298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2730740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 27310298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2732740132f1SEmil Constantinescu } 2733deb2cd25SJed Brown PetscFunctionReturn(0); 2734deb2cd25SJed Brown } 2735deb2cd25SJed Brown 2736d8e5e3e6SSatish Balay /*@ 273794b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2738d763cef2SBarry Smith a TS (timestepper) context. 2739d763cef2SBarry Smith 274094b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2741d763cef2SBarry Smith 2742d763cef2SBarry Smith Input Parameter: 2743d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2744d763cef2SBarry Smith 2745d763cef2SBarry Smith Output Parameter: 274694b7f48cSBarry Smith . ksp - the nonlinear solver context 2747d763cef2SBarry Smith 2748d763cef2SBarry Smith Notes: 274994b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2750d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2751d763cef2SBarry Smith KSP and PC contexts as well. 2752d763cef2SBarry Smith 275394b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 27540298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2755d763cef2SBarry Smith 2756d763cef2SBarry Smith Level: beginner 2757d763cef2SBarry Smith 275894b7f48cSBarry Smith .keywords: timestep, get, KSP 2759d763cef2SBarry Smith @*/ 27607087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2761d763cef2SBarry Smith { 2762d372ba47SLisandro Dalcin PetscErrorCode ierr; 2763089b2837SJed Brown SNES snes; 2764d372ba47SLisandro Dalcin 2765d763cef2SBarry Smith PetscFunctionBegin; 27660700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27674482741eSBarry Smith PetscValidPointer(ksp,2); 276817186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2769e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2770089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2771089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2772d763cef2SBarry Smith PetscFunctionReturn(0); 2773d763cef2SBarry Smith } 2774d763cef2SBarry Smith 2775d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2776d763cef2SBarry Smith 2777adb62b0dSMatthew Knepley /*@ 2778618ce8baSLisandro Dalcin TSSetMaxSteps - Sets the maximum number of steps to use. 2779618ce8baSLisandro Dalcin 2780618ce8baSLisandro Dalcin Logically Collective on TS 2781618ce8baSLisandro Dalcin 2782618ce8baSLisandro Dalcin Input Parameters: 2783618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 2784618ce8baSLisandro Dalcin - maxsteps - maximum number of steps to use 2785618ce8baSLisandro Dalcin 2786618ce8baSLisandro Dalcin Options Database Keys: 2787618ce8baSLisandro Dalcin . -ts_max_steps <maxsteps> - Sets maxsteps 2788618ce8baSLisandro Dalcin 2789618ce8baSLisandro Dalcin Notes: 2790618ce8baSLisandro Dalcin The default maximum number of steps is 5000 2791618ce8baSLisandro Dalcin 2792618ce8baSLisandro Dalcin Level: intermediate 2793618ce8baSLisandro Dalcin 2794618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, steps 2795618ce8baSLisandro Dalcin 2796618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime() 2797618ce8baSLisandro Dalcin @*/ 2798618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps) 2799618ce8baSLisandro Dalcin { 2800618ce8baSLisandro Dalcin PetscFunctionBegin; 2801618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2802618ce8baSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2803618ce8baSLisandro Dalcin if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative"); 2804618ce8baSLisandro Dalcin ts->max_steps = maxsteps; 2805618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2806618ce8baSLisandro Dalcin } 2807618ce8baSLisandro Dalcin 2808618ce8baSLisandro Dalcin /*@ 2809618ce8baSLisandro Dalcin TSGetMaxSteps - Gets the maximum number of steps to use. 2810618ce8baSLisandro Dalcin 2811618ce8baSLisandro Dalcin Not Collective 2812618ce8baSLisandro Dalcin 2813618ce8baSLisandro Dalcin Input Parameters: 2814618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 2815618ce8baSLisandro Dalcin 2816618ce8baSLisandro Dalcin Output Parameter: 2817618ce8baSLisandro Dalcin . maxsteps - maximum number of steps to use 2818618ce8baSLisandro Dalcin 2819618ce8baSLisandro Dalcin Level: advanced 2820618ce8baSLisandro Dalcin 2821618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, steps 2822618ce8baSLisandro Dalcin 2823618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime() 2824618ce8baSLisandro Dalcin @*/ 2825618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps) 2826618ce8baSLisandro Dalcin { 2827618ce8baSLisandro Dalcin PetscFunctionBegin; 2828618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2829618ce8baSLisandro Dalcin PetscValidIntPointer(maxsteps,2); 2830618ce8baSLisandro Dalcin *maxsteps = ts->max_steps; 2831618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2832618ce8baSLisandro Dalcin } 2833618ce8baSLisandro Dalcin 2834618ce8baSLisandro Dalcin /*@ 2835618ce8baSLisandro Dalcin TSSetMaxTime - Sets the maximum (or final) time for timestepping. 2836618ce8baSLisandro Dalcin 2837618ce8baSLisandro Dalcin Logically Collective on TS 2838618ce8baSLisandro Dalcin 2839618ce8baSLisandro Dalcin Input Parameters: 2840618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 2841618ce8baSLisandro Dalcin - maxtime - final time to step to 2842618ce8baSLisandro Dalcin 2843618ce8baSLisandro Dalcin Options Database Keys: 2844ef85077eSLisandro Dalcin . -ts_max_time <maxtime> - Sets maxtime 2845618ce8baSLisandro Dalcin 2846618ce8baSLisandro Dalcin Notes: 2847618ce8baSLisandro Dalcin The default maximum time is 5.0 2848618ce8baSLisandro Dalcin 2849618ce8baSLisandro Dalcin Level: intermediate 2850618ce8baSLisandro Dalcin 2851618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, time 2852618ce8baSLisandro Dalcin 2853618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime() 2854618ce8baSLisandro Dalcin @*/ 2855618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime) 2856618ce8baSLisandro Dalcin { 2857618ce8baSLisandro Dalcin PetscFunctionBegin; 2858618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2859618ce8baSLisandro Dalcin PetscValidLogicalCollectiveReal(ts,maxtime,2); 2860618ce8baSLisandro Dalcin ts->max_time = maxtime; 2861618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2862618ce8baSLisandro Dalcin } 2863618ce8baSLisandro Dalcin 2864618ce8baSLisandro Dalcin /*@ 2865618ce8baSLisandro Dalcin TSGetMaxTime - Gets the maximum (or final) time for timestepping. 2866618ce8baSLisandro Dalcin 2867618ce8baSLisandro Dalcin Not Collective 2868618ce8baSLisandro Dalcin 2869618ce8baSLisandro Dalcin Input Parameters: 2870618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 2871618ce8baSLisandro Dalcin 2872618ce8baSLisandro Dalcin Output Parameter: 2873618ce8baSLisandro Dalcin . maxtime - final time to step to 2874618ce8baSLisandro Dalcin 2875618ce8baSLisandro Dalcin Level: advanced 2876618ce8baSLisandro Dalcin 2877618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, time 2878618ce8baSLisandro Dalcin 2879618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps() 2880618ce8baSLisandro Dalcin @*/ 2881618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime) 2882618ce8baSLisandro Dalcin { 2883618ce8baSLisandro Dalcin PetscFunctionBegin; 2884618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2885618ce8baSLisandro Dalcin PetscValidRealPointer(maxtime,2); 2886618ce8baSLisandro Dalcin *maxtime = ts->max_time; 2887618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2888618ce8baSLisandro Dalcin } 2889618ce8baSLisandro Dalcin 2890618ce8baSLisandro Dalcin /*@ 2891aaa6c58dSLisandro Dalcin TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep(). 2892edc382c3SSatish Balay 2893edc382c3SSatish Balay Level: deprecated 2894edc382c3SSatish Balay 2895aaa6c58dSLisandro Dalcin @*/ 2896aaa6c58dSLisandro Dalcin PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 2897aaa6c58dSLisandro Dalcin { 2898aaa6c58dSLisandro Dalcin PetscErrorCode ierr; 2899aaa6c58dSLisandro Dalcin PetscFunctionBegin; 2900aaa6c58dSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2901aaa6c58dSLisandro Dalcin ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 2902aaa6c58dSLisandro Dalcin ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 2903aaa6c58dSLisandro Dalcin PetscFunctionReturn(0); 2904aaa6c58dSLisandro Dalcin } 2905aaa6c58dSLisandro Dalcin 2906aaa6c58dSLisandro Dalcin /*@ 290719eac22cSLisandro Dalcin TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime(). 2908edc382c3SSatish Balay 2909edc382c3SSatish Balay Level: deprecated 2910edc382c3SSatish Balay 2911adb62b0dSMatthew Knepley @*/ 29127087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2913adb62b0dSMatthew Knepley { 2914adb62b0dSMatthew Knepley PetscFunctionBegin; 29150700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2916abc0a331SBarry Smith if (maxsteps) { 29174482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2918adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2919adb62b0dSMatthew Knepley } 2920abc0a331SBarry Smith if (maxtime) { 29214482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2922adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2923adb62b0dSMatthew Knepley } 2924adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2925adb62b0dSMatthew Knepley } 2926adb62b0dSMatthew Knepley 2927d763cef2SBarry Smith /*@ 292819eac22cSLisandro Dalcin TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime(). 2929edc382c3SSatish Balay 2930edc382c3SSatish Balay Level: deprecated 2931edc382c3SSatish Balay 2932d763cef2SBarry Smith @*/ 29337087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2934d763cef2SBarry Smith { 2935d763cef2SBarry Smith PetscFunctionBegin; 29360700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2937c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2938c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 293939b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 294039b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2941d763cef2SBarry Smith PetscFunctionReturn(0); 2942d763cef2SBarry Smith } 2943d763cef2SBarry Smith 2944d763cef2SBarry Smith /*@ 29455c5f5948SLisandro Dalcin TSGetTimeStepNumber - Deprecated, use TSGetStepNumber(). 2946edc382c3SSatish Balay 2947edc382c3SSatish Balay Level: deprecated 2948edc382c3SSatish Balay 29495c5f5948SLisandro Dalcin @*/ 2950e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 29515c5f5948SLisandro Dalcin 295219eac22cSLisandro Dalcin /*@ 29534f4e0956SLisandro Dalcin TSGetTotalSteps - Deprecated, use TSGetStepNumber(). 2954edc382c3SSatish Balay 2955edc382c3SSatish Balay Level: deprecated 2956edc382c3SSatish Balay 29574f4e0956SLisandro Dalcin @*/ 29584f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 29594f4e0956SLisandro Dalcin 29604f4e0956SLisandro Dalcin /*@ 2961d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2962d763cef2SBarry Smith for use by the TS routines. 2963d763cef2SBarry Smith 29643f9fe445SBarry Smith Logically Collective on TS and Vec 2965d763cef2SBarry Smith 2966d763cef2SBarry Smith Input Parameters: 2967d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 29680910c330SBarry Smith - u - the solution vector 2969d763cef2SBarry Smith 2970d763cef2SBarry Smith Level: beginner 2971d763cef2SBarry Smith 2972715f1b00SHong Zhang .keywords: TS, timestep, set, solution, initial values 29730ed3bfb6SBarry Smith 29740ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate() 2975d763cef2SBarry Smith @*/ 29760910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2977d763cef2SBarry Smith { 29788737fe31SLisandro Dalcin PetscErrorCode ierr; 2979496e6a7aSJed Brown DM dm; 29808737fe31SLisandro Dalcin 2981d763cef2SBarry Smith PetscFunctionBegin; 29820700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 29830910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 29840910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 29856bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 29860910c330SBarry Smith ts->vec_sol = u; 2987bbd56ea5SKarl Rupp 2988496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 29890910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2990d763cef2SBarry Smith PetscFunctionReturn(0); 2991d763cef2SBarry Smith } 2992d763cef2SBarry Smith 2993ac226902SBarry Smith /*@C 2994000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 29953f2090d5SJed Brown called once at the beginning of each time step. 2996000e7ae3SMatthew Knepley 29973f9fe445SBarry Smith Logically Collective on TS 2998000e7ae3SMatthew Knepley 2999000e7ae3SMatthew Knepley Input Parameters: 3000000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3001000e7ae3SMatthew Knepley - func - The function 3002000e7ae3SMatthew Knepley 3003000e7ae3SMatthew Knepley Calling sequence of func: 3004000e7ae3SMatthew Knepley . func (TS ts); 3005000e7ae3SMatthew Knepley 3006000e7ae3SMatthew Knepley Level: intermediate 3007000e7ae3SMatthew Knepley 3008000e7ae3SMatthew Knepley .keywords: TS, timestep 3009dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep() 3010000e7ae3SMatthew Knepley @*/ 30117087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3012000e7ae3SMatthew Knepley { 3013000e7ae3SMatthew Knepley PetscFunctionBegin; 30140700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3015ae60f76fSBarry Smith ts->prestep = func; 3016000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3017000e7ae3SMatthew Knepley } 3018000e7ae3SMatthew Knepley 301909ee8438SJed Brown /*@ 30203f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 30213f2090d5SJed Brown 30223f2090d5SJed Brown Collective on TS 30233f2090d5SJed Brown 30243f2090d5SJed Brown Input Parameters: 30253f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 30263f2090d5SJed Brown 30273f2090d5SJed Brown Notes: 30283f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 30293f2090d5SJed Brown so most users would not generally call this routine themselves. 30303f2090d5SJed Brown 30313f2090d5SJed Brown Level: developer 30323f2090d5SJed Brown 30333f2090d5SJed Brown .keywords: TS, timestep 30349be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 30353f2090d5SJed Brown @*/ 30367087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 30373f2090d5SJed Brown { 30383f2090d5SJed Brown PetscErrorCode ierr; 30393f2090d5SJed Brown 30403f2090d5SJed Brown PetscFunctionBegin; 30410700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3042ae60f76fSBarry Smith if (ts->prestep) { 30435efd42a4SStefano Zampini Vec U; 30445efd42a4SStefano Zampini PetscObjectState sprev,spost; 30455efd42a4SStefano Zampini 30465efd42a4SStefano Zampini ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 30475efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3048ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 30495efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3050dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 3051312ce896SJed Brown } 30523f2090d5SJed Brown PetscFunctionReturn(0); 30533f2090d5SJed Brown } 30543f2090d5SJed Brown 3055b8123daeSJed Brown /*@C 3056b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3057b8123daeSJed Brown called once at the beginning of each stage. 3058b8123daeSJed Brown 3059b8123daeSJed Brown Logically Collective on TS 3060b8123daeSJed Brown 3061b8123daeSJed Brown Input Parameters: 3062b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 3063b8123daeSJed Brown - func - The function 3064b8123daeSJed Brown 3065b8123daeSJed Brown Calling sequence of func: 3066b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 3067b8123daeSJed Brown 3068b8123daeSJed Brown Level: intermediate 3069b8123daeSJed Brown 3070b8123daeSJed Brown Note: 3071b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 307280275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 3073b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 3074b8123daeSJed Brown 3075b8123daeSJed Brown .keywords: TS, timestep 30769be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3077b8123daeSJed Brown @*/ 3078b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 3079b8123daeSJed Brown { 3080b8123daeSJed Brown PetscFunctionBegin; 3081b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3082ae60f76fSBarry Smith ts->prestage = func; 3083b8123daeSJed Brown PetscFunctionReturn(0); 3084b8123daeSJed Brown } 3085b8123daeSJed Brown 30869be3e283SDebojyoti Ghosh /*@C 30879be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 30889be3e283SDebojyoti Ghosh called once at the end of each stage. 30899be3e283SDebojyoti Ghosh 30909be3e283SDebojyoti Ghosh Logically Collective on TS 30919be3e283SDebojyoti Ghosh 30929be3e283SDebojyoti Ghosh Input Parameters: 30939be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 30949be3e283SDebojyoti Ghosh - func - The function 30959be3e283SDebojyoti Ghosh 30969be3e283SDebojyoti Ghosh Calling sequence of func: 30979be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 30989be3e283SDebojyoti Ghosh 30999be3e283SDebojyoti Ghosh Level: intermediate 31009be3e283SDebojyoti Ghosh 31019be3e283SDebojyoti Ghosh Note: 31029be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 310380275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 31049be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 31059be3e283SDebojyoti Ghosh 31069be3e283SDebojyoti Ghosh .keywords: TS, timestep 31079be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 31089be3e283SDebojyoti Ghosh @*/ 31099be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 31109be3e283SDebojyoti Ghosh { 31119be3e283SDebojyoti Ghosh PetscFunctionBegin; 31129be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 31139be3e283SDebojyoti Ghosh ts->poststage = func; 31149be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 31159be3e283SDebojyoti Ghosh } 31169be3e283SDebojyoti Ghosh 3117c688d042SShri Abhyankar /*@C 3118c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 3119c688d042SShri Abhyankar called once at the end of each step evaluation. 3120c688d042SShri Abhyankar 3121c688d042SShri Abhyankar Logically Collective on TS 3122c688d042SShri Abhyankar 3123c688d042SShri Abhyankar Input Parameters: 3124c688d042SShri Abhyankar + ts - The TS context obtained from TSCreate() 3125c688d042SShri Abhyankar - func - The function 3126c688d042SShri Abhyankar 3127c688d042SShri Abhyankar Calling sequence of func: 3128c688d042SShri Abhyankar . PetscErrorCode func(TS ts); 3129c688d042SShri Abhyankar 3130c688d042SShri Abhyankar Level: intermediate 3131c688d042SShri Abhyankar 3132c688d042SShri Abhyankar Note: 31331785ff2aSShri Abhyankar Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling 31341785ff2aSShri Abhyankar thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep() 3135e7e94ed4SShri Abhyankar may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step 3136e7e94ed4SShri Abhyankar solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step 3137e7e94ed4SShri Abhyankar with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime() 3138c688d042SShri Abhyankar 3139c688d042SShri Abhyankar .keywords: TS, timestep 3140c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3141c688d042SShri Abhyankar @*/ 3142c688d042SShri Abhyankar PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS)) 3143c688d042SShri Abhyankar { 3144c688d042SShri Abhyankar PetscFunctionBegin; 3145c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3146c688d042SShri Abhyankar ts->postevaluate = func; 3147c688d042SShri Abhyankar PetscFunctionReturn(0); 3148c688d042SShri Abhyankar } 3149c688d042SShri Abhyankar 3150b8123daeSJed Brown /*@ 3151b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 3152b8123daeSJed Brown 3153b8123daeSJed Brown Collective on TS 3154b8123daeSJed Brown 3155b8123daeSJed Brown Input Parameters: 3156b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 31579be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3158b8123daeSJed Brown 3159b8123daeSJed Brown Notes: 3160b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 3161b8123daeSJed Brown most users would not generally call this routine themselves. 3162b8123daeSJed Brown 3163b8123daeSJed Brown Level: developer 3164b8123daeSJed Brown 3165b8123daeSJed Brown .keywords: TS, timestep 31669be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 3167b8123daeSJed Brown @*/ 3168b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3169b8123daeSJed Brown { 3170b8123daeSJed Brown PetscErrorCode ierr; 3171b8123daeSJed Brown 3172b8123daeSJed Brown PetscFunctionBegin; 3173b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3174ae60f76fSBarry Smith if (ts->prestage) { 3175ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 3176b8123daeSJed Brown } 3177b8123daeSJed Brown PetscFunctionReturn(0); 3178b8123daeSJed Brown } 3179b8123daeSJed Brown 31809be3e283SDebojyoti Ghosh /*@ 31819be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 31829be3e283SDebojyoti Ghosh 31839be3e283SDebojyoti Ghosh Collective on TS 31849be3e283SDebojyoti Ghosh 31859be3e283SDebojyoti Ghosh Input Parameters: 31869be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 31879be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 31889be3e283SDebojyoti Ghosh stageindex - Stage number 31899be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 31909be3e283SDebojyoti Ghosh of stages) with the stage solutions 31919be3e283SDebojyoti Ghosh 31929be3e283SDebojyoti Ghosh Notes: 31939be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 31949be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 31959be3e283SDebojyoti Ghosh 31969be3e283SDebojyoti Ghosh Level: developer 31979be3e283SDebojyoti Ghosh 31989be3e283SDebojyoti Ghosh .keywords: TS, timestep 31999be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 32009be3e283SDebojyoti Ghosh @*/ 32019be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 32029be3e283SDebojyoti Ghosh { 32039be3e283SDebojyoti Ghosh PetscErrorCode ierr; 32049be3e283SDebojyoti Ghosh 32059be3e283SDebojyoti Ghosh PetscFunctionBegin; 32069be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 32074beae5d8SLisandro Dalcin if (ts->poststage) { 32089be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 32099be3e283SDebojyoti Ghosh } 32109be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 32119be3e283SDebojyoti Ghosh } 32129be3e283SDebojyoti Ghosh 3213c688d042SShri Abhyankar /*@ 3214c688d042SShri Abhyankar TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate() 3215c688d042SShri Abhyankar 3216c688d042SShri Abhyankar Collective on TS 3217c688d042SShri Abhyankar 3218c688d042SShri Abhyankar Input Parameters: 3219c688d042SShri Abhyankar . ts - The TS context obtained from TSCreate() 3220c688d042SShri Abhyankar 3221c688d042SShri Abhyankar Notes: 3222c688d042SShri Abhyankar TSPostEvaluate() is typically used within time stepping implementations, 3223c688d042SShri Abhyankar most users would not generally call this routine themselves. 3224c688d042SShri Abhyankar 3225c688d042SShri Abhyankar Level: developer 3226c688d042SShri Abhyankar 3227c688d042SShri Abhyankar .keywords: TS, timestep 3228c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep() 3229c688d042SShri Abhyankar @*/ 3230c688d042SShri Abhyankar PetscErrorCode TSPostEvaluate(TS ts) 3231c688d042SShri Abhyankar { 3232c688d042SShri Abhyankar PetscErrorCode ierr; 3233c688d042SShri Abhyankar 3234c688d042SShri Abhyankar PetscFunctionBegin; 3235c688d042SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3236c688d042SShri Abhyankar if (ts->postevaluate) { 3237dcb233daSLisandro Dalcin Vec U; 3238dcb233daSLisandro Dalcin PetscObjectState sprev,spost; 3239dcb233daSLisandro Dalcin 3240dcb233daSLisandro Dalcin ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 3241dcb233daSLisandro Dalcin ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3242c688d042SShri Abhyankar PetscStackCallStandard((*ts->postevaluate),(ts)); 3243dcb233daSLisandro Dalcin ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3244dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 3245c688d042SShri Abhyankar } 3246c688d042SShri Abhyankar PetscFunctionReturn(0); 3247c688d042SShri Abhyankar } 3248c688d042SShri Abhyankar 3249ac226902SBarry Smith /*@C 3250000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 32513f2090d5SJed Brown called once at the end of each time step. 3252000e7ae3SMatthew Knepley 32533f9fe445SBarry Smith Logically Collective on TS 3254000e7ae3SMatthew Knepley 3255000e7ae3SMatthew Knepley Input Parameters: 3256000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3257000e7ae3SMatthew Knepley - func - The function 3258000e7ae3SMatthew Knepley 3259000e7ae3SMatthew Knepley Calling sequence of func: 3260b8123daeSJed Brown $ func (TS ts); 3261000e7ae3SMatthew Knepley 32621785ff2aSShri Abhyankar Notes: 32631785ff2aSShri Abhyankar The function set by TSSetPostStep() is called after each successful step. The solution vector X 32641785ff2aSShri Abhyankar obtained by TSGetSolution() may be different than that computed at the step end if the event handler 32651785ff2aSShri Abhyankar locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead. 32661785ff2aSShri Abhyankar 3267000e7ae3SMatthew Knepley Level: intermediate 3268000e7ae3SMatthew Knepley 3269000e7ae3SMatthew Knepley .keywords: TS, timestep 3270dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep() 3271000e7ae3SMatthew Knepley @*/ 32727087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 3273000e7ae3SMatthew Knepley { 3274000e7ae3SMatthew Knepley PetscFunctionBegin; 32750700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3276ae60f76fSBarry Smith ts->poststep = func; 3277000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3278000e7ae3SMatthew Knepley } 3279000e7ae3SMatthew Knepley 328009ee8438SJed Brown /*@ 32813f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 32823f2090d5SJed Brown 32833f2090d5SJed Brown Collective on TS 32843f2090d5SJed Brown 32853f2090d5SJed Brown Input Parameters: 32863f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 32873f2090d5SJed Brown 32883f2090d5SJed Brown Notes: 32893f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 32903f2090d5SJed Brown so most users would not generally call this routine themselves. 32913f2090d5SJed Brown 32923f2090d5SJed Brown Level: developer 32933f2090d5SJed Brown 32943f2090d5SJed Brown .keywords: TS, timestep 32953f2090d5SJed Brown @*/ 32967087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 32973f2090d5SJed Brown { 32983f2090d5SJed Brown PetscErrorCode ierr; 32993f2090d5SJed Brown 33003f2090d5SJed Brown PetscFunctionBegin; 33010700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3302ae60f76fSBarry Smith if (ts->poststep) { 33035efd42a4SStefano Zampini Vec U; 33045efd42a4SStefano Zampini PetscObjectState sprev,spost; 33055efd42a4SStefano Zampini 33065efd42a4SStefano Zampini ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 33075efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3308ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 33095efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3310dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 331172ac3e02SJed Brown } 33123f2090d5SJed Brown PetscFunctionReturn(0); 33133f2090d5SJed Brown } 33143f2090d5SJed Brown 3315d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 3316d763cef2SBarry Smith 3317d763cef2SBarry Smith /*@C 3318a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 3319d763cef2SBarry Smith timestep to display the iteration's progress. 3320d763cef2SBarry Smith 33213f9fe445SBarry Smith Logically Collective on TS 3322d763cef2SBarry Smith 3323d763cef2SBarry Smith Input Parameters: 3324d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3325e213d8f1SJed Brown . monitor - monitoring routine 3326329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 33270298fd71SBarry Smith monitor routine (use NULL if no context is desired) 3328b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 33290298fd71SBarry Smith (may be NULL) 3330d763cef2SBarry Smith 3331e213d8f1SJed Brown Calling sequence of monitor: 3332dcb233daSLisandro Dalcin $ PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 3333d763cef2SBarry Smith 3334d763cef2SBarry Smith + ts - the TS context 333563e21af5SBarry 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) 33361f06c33eSBarry Smith . time - current time 33370910c330SBarry Smith . u - current iterate 3338d763cef2SBarry Smith - mctx - [optional] monitoring context 3339d763cef2SBarry Smith 3340d763cef2SBarry Smith Notes: 3341d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 3342d763cef2SBarry Smith already has been loaded. 3343d763cef2SBarry Smith 3344025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 3345025f1a04SBarry Smith 3346d763cef2SBarry Smith Level: intermediate 3347d763cef2SBarry Smith 3348d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3349d763cef2SBarry Smith 3350a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 3351d763cef2SBarry Smith @*/ 3352c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 3353d763cef2SBarry Smith { 335478064530SBarry Smith PetscErrorCode ierr; 335578064530SBarry Smith PetscInt i; 335678064530SBarry Smith PetscBool identical; 335778064530SBarry Smith 3358d763cef2SBarry Smith PetscFunctionBegin; 33590700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 336078064530SBarry Smith for (i=0; i<ts->numbermonitors;i++) { 336178064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr); 336278064530SBarry Smith if (identical) PetscFunctionReturn(0); 336378064530SBarry Smith } 336417186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 3365d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 33668704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 3367d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 3368d763cef2SBarry Smith PetscFunctionReturn(0); 3369d763cef2SBarry Smith } 3370d763cef2SBarry Smith 3371d763cef2SBarry Smith /*@C 3372a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 3373d763cef2SBarry Smith 33743f9fe445SBarry Smith Logically Collective on TS 3375d763cef2SBarry Smith 3376d763cef2SBarry Smith Input Parameters: 3377d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3378d763cef2SBarry Smith 3379d763cef2SBarry Smith Notes: 3380d763cef2SBarry Smith There is no way to remove a single, specific monitor. 3381d763cef2SBarry Smith 3382d763cef2SBarry Smith Level: intermediate 3383d763cef2SBarry Smith 3384d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 3385d763cef2SBarry Smith 3386a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 3387d763cef2SBarry Smith @*/ 33887087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 3389d763cef2SBarry Smith { 3390d952e501SBarry Smith PetscErrorCode ierr; 3391d952e501SBarry Smith PetscInt i; 3392d952e501SBarry Smith 3393d763cef2SBarry Smith PetscFunctionBegin; 33940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3395d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 33968704b422SBarry Smith if (ts->monitordestroy[i]) { 33978704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 3398d952e501SBarry Smith } 3399d952e501SBarry Smith } 3400d763cef2SBarry Smith ts->numbermonitors = 0; 3401d763cef2SBarry Smith PetscFunctionReturn(0); 3402d763cef2SBarry Smith } 3403d763cef2SBarry Smith 3404721cd6eeSBarry Smith /*@C 3405721cd6eeSBarry Smith TSMonitorDefault - The Default monitor, prints the timestep and time for each step 34065516499fSSatish Balay 34075516499fSSatish Balay Level: intermediate 340841251cbbSSatish Balay 34095516499fSSatish Balay .keywords: TS, set, monitor 34105516499fSSatish Balay 341163e21af5SBarry Smith .seealso: TSMonitorSet() 341241251cbbSSatish Balay @*/ 3413721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf) 3414d763cef2SBarry Smith { 3415dfbe8321SBarry Smith PetscErrorCode ierr; 3416721cd6eeSBarry Smith PetscViewer viewer = vf->viewer; 341741aca3d6SBarry Smith PetscBool iascii,ibinary; 3418d132466eSBarry Smith 3419d763cef2SBarry Smith PetscFunctionBegin; 34204d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 342141aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 342241aca3d6SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr); 3423721cd6eeSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 342441aca3d6SBarry Smith if (iascii) { 3425649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 342663e21af5SBarry Smith if (step == -1){ /* this indicates it is an interpolated solution */ 342763e21af5SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr); 342863e21af5SBarry Smith } else { 34298392e04aSShri 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); 343063e21af5SBarry Smith } 3431649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 343241aca3d6SBarry Smith } else if (ibinary) { 343341aca3d6SBarry Smith PetscMPIInt rank; 343441aca3d6SBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 343541aca3d6SBarry Smith if (!rank) { 3436450a797fSBarry Smith PetscBool skipHeader; 3437450a797fSBarry Smith PetscInt classid = REAL_FILE_CLASSID; 3438450a797fSBarry Smith 3439450a797fSBarry Smith ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr); 3440450a797fSBarry Smith if (!skipHeader) { 3441450a797fSBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 3442450a797fSBarry Smith } 344341aca3d6SBarry Smith ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr); 344441aca3d6SBarry Smith } else { 344541aca3d6SBarry Smith ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr); 344641aca3d6SBarry Smith } 344741aca3d6SBarry Smith } 3448721cd6eeSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3449d763cef2SBarry Smith PetscFunctionReturn(0); 3450d763cef2SBarry Smith } 3451d763cef2SBarry Smith 3452cd652676SJed Brown /*@ 3453cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3454cd652676SJed Brown 3455cd652676SJed Brown Collective on TS 3456cd652676SJed Brown 3457cd652676SJed Brown Input Argument: 3458cd652676SJed Brown + ts - time stepping context 3459cd652676SJed Brown - t - time to interpolate to 3460cd652676SJed Brown 3461cd652676SJed Brown Output Argument: 34620910c330SBarry Smith . U - state at given time 3463cd652676SJed Brown 3464cd652676SJed Brown Level: intermediate 3465cd652676SJed Brown 3466cd652676SJed Brown Developer Notes: 3467cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3468cd652676SJed Brown 3469cd652676SJed Brown .keywords: TS, set 3470cd652676SJed Brown 3471874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve() 3472cd652676SJed Brown @*/ 34730910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3474cd652676SJed Brown { 3475cd652676SJed Brown PetscErrorCode ierr; 3476cd652676SJed Brown 3477cd652676SJed Brown PetscFunctionBegin; 3478cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3479b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3480be5899b3SLisandro 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); 3481ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 34820910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3483cd652676SJed Brown PetscFunctionReturn(0); 3484cd652676SJed Brown } 3485cd652676SJed Brown 3486d763cef2SBarry Smith /*@ 34876d9e5789SSean Farley TSStep - Steps one time step 3488d763cef2SBarry Smith 3489d763cef2SBarry Smith Collective on TS 3490d763cef2SBarry Smith 3491d763cef2SBarry Smith Input Parameter: 3492d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3493d763cef2SBarry Smith 349427829d71SBarry Smith Level: developer 3495d763cef2SBarry Smith 3496b8123daeSJed Brown Notes: 349727829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 349827829d71SBarry Smith 3499b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3500b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3501b8123daeSJed Brown 350219eac22cSLisandro Dalcin This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the 350319eac22cSLisandro Dalcin time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 350425cb2221SBarry Smith 3505d763cef2SBarry Smith .keywords: TS, timestep, solve 3506d763cef2SBarry Smith 35079be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3508d763cef2SBarry Smith @*/ 3509193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3510d763cef2SBarry Smith { 3511dfbe8321SBarry Smith PetscErrorCode ierr; 3512fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3513be5899b3SLisandro Dalcin PetscReal ptime; 3514d763cef2SBarry Smith 3515d763cef2SBarry Smith PetscFunctionBegin; 35160700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3517fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3518fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3519fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3520fffbeea8SBarry Smith " type = {Preprint},\n" 3521fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3522fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3523302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3524fffbeea8SBarry Smith 3525d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 352668bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3527d405a339SMatthew Knepley 3528ef85077eSLisandro 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>"); 3529a6772fa2SLisandro 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()"); 3530be5899b3SLisandro 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"); 3531a6772fa2SLisandro Dalcin 3532be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 3533be5899b3SLisandro Dalcin ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev; 35342808aa04SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3535e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3536d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3537193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3538d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3539be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 35402808aa04SLisandro Dalcin ts->steps++; 3541be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 354228d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3543362cd11cSLisandro Dalcin 3544362cd11cSLisandro Dalcin if (ts->reason < 0) { 3545cef5090cSJed Brown if (ts->errorifstepfailed) { 354608c7845fSBarry 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]); 354708c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3548d2daff3dSHong Zhang } 354908c7845fSBarry Smith } else if (!ts->reason) { 355008c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 355108c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 355208c7845fSBarry Smith } 355308c7845fSBarry Smith PetscFunctionReturn(0); 355408c7845fSBarry Smith } 355508c7845fSBarry Smith 355608c7845fSBarry Smith /*@ 35577cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 35587cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 35597cbde773SLisandro Dalcin 35607cbde773SLisandro Dalcin Collective on TS 35617cbde773SLisandro Dalcin 35627cbde773SLisandro Dalcin Input Arguments: 35637cbde773SLisandro Dalcin + ts - time stepping context 35647cbde773SLisandro Dalcin . wnormtype - norm type, either NORM_2 or NORM_INFINITY 35657cbde773SLisandro Dalcin - order - optional, desired order for the error evaluation or PETSC_DECIDE 35667cbde773SLisandro Dalcin 35677cbde773SLisandro Dalcin Output Arguments: 35687cbde773SLisandro Dalcin + order - optional, the actual order of the error evaluation 35697cbde773SLisandro Dalcin - wlte - the weighted local truncation error norm 35707cbde773SLisandro Dalcin 35717cbde773SLisandro Dalcin Level: advanced 35727cbde773SLisandro Dalcin 35737cbde773SLisandro Dalcin Notes: 35747cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 35757cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 35767cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 35777cbde773SLisandro Dalcin 35787cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm() 35797cbde773SLisandro Dalcin @*/ 35807cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte) 35817cbde773SLisandro Dalcin { 35827cbde773SLisandro Dalcin PetscErrorCode ierr; 35837cbde773SLisandro Dalcin 35847cbde773SLisandro Dalcin PetscFunctionBegin; 35857cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 35867cbde773SLisandro Dalcin PetscValidType(ts,1); 35877cbde773SLisandro Dalcin PetscValidLogicalCollectiveEnum(ts,wnormtype,4); 35887cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order,3); 35897cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts,*order,3); 35907cbde773SLisandro Dalcin PetscValidRealPointer(wlte,4); 35917cbde773SLisandro Dalcin if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 35927cbde773SLisandro Dalcin if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name); 35937cbde773SLisandro Dalcin ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr); 35947cbde773SLisandro Dalcin PetscFunctionReturn(0); 35957cbde773SLisandro Dalcin } 35967cbde773SLisandro Dalcin 359705175c85SJed Brown /*@ 359805175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 359905175c85SJed Brown 36001c3436cfSJed Brown Collective on TS 360105175c85SJed Brown 360205175c85SJed Brown Input Arguments: 36031c3436cfSJed Brown + ts - time stepping context 36041c3436cfSJed Brown . order - desired order of accuracy 36050298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 360605175c85SJed Brown 360705175c85SJed Brown Output Arguments: 36080910c330SBarry Smith . U - state at the end of the current step 360905175c85SJed Brown 361005175c85SJed Brown Level: advanced 361105175c85SJed Brown 3612108c343cSJed Brown Notes: 3613108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3614108c343cSJed 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. 3615108c343cSJed Brown 36161c3436cfSJed Brown .seealso: TSStep(), TSAdapt 361705175c85SJed Brown @*/ 36180910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 361905175c85SJed Brown { 362005175c85SJed Brown PetscErrorCode ierr; 362105175c85SJed Brown 362205175c85SJed Brown PetscFunctionBegin; 362305175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 362405175c85SJed Brown PetscValidType(ts,1); 36250910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3626ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 36270910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 362805175c85SJed Brown PetscFunctionReturn(0); 362905175c85SJed Brown } 363005175c85SJed Brown 3631b1cb36f3SHong Zhang /*@ 36326a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 36336a4d4014SLisandro Dalcin 36346a4d4014SLisandro Dalcin Collective on TS 36356a4d4014SLisandro Dalcin 36366a4d4014SLisandro Dalcin Input Parameter: 36376a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 363863e21af5SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used, 363963e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 36405a3a76d0SJed Brown 36416a4d4014SLisandro Dalcin Level: beginner 36426a4d4014SLisandro Dalcin 36435a3a76d0SJed Brown Notes: 36445a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 36455a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 36465a3a76d0SJed Brown stepped over the final time. 36475a3a76d0SJed Brown 36486a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 36496a4d4014SLisandro Dalcin 365063e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime() 36516a4d4014SLisandro Dalcin @*/ 3652cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 36536a4d4014SLisandro Dalcin { 3654b06615a5SLisandro Dalcin Vec solution; 36556a4d4014SLisandro Dalcin PetscErrorCode ierr; 3656f22f69f0SBarry Smith 36576a4d4014SLisandro Dalcin PetscFunctionBegin; 36580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3659f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 3660feed9e9dSBarry Smith 3661ee41a567SStefano 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 */ 36620910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 3663b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3664b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3665b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 36665a3a76d0SJed Brown } 36670910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3668715f1b00SHong Zhang if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE"); 3669bbd56ea5SKarl Rupp } else if (u) { 36700910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 36715a3a76d0SJed Brown } 3672b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 367368bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3674a6772fa2SLisandro Dalcin 3675ef85077eSLisandro 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>"); 3676a6772fa2SLisandro 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()"); 3677a6772fa2SLisandro 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"); 3678a6772fa2SLisandro Dalcin 3679715f1b00SHong Zhang if (ts->forward_solve) { 3680715f1b00SHong Zhang ierr = TSForwardSetUp(ts);CHKERRQ(ierr); 3681715f1b00SHong Zhang } 3682715f1b00SHong Zhang 3683e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 3684715f1b00SHong Zhang restarts the step after an event. Resetting these counters in such case causes 3685e7069c78SShri TSTrajectory to incorrectly save the output files 3686e7069c78SShri */ 3687715f1b00SHong Zhang /* reset time step and iteration counters */ 36882808aa04SLisandro Dalcin if (!ts->steps) { 36895ef26d82SJed Brown ts->ksp_its = 0; 36905ef26d82SJed Brown ts->snes_its = 0; 3691c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3692c610991cSLisandro Dalcin ts->reject = 0; 36932808aa04SLisandro Dalcin ts->steprestart = PETSC_TRUE; 36942808aa04SLisandro Dalcin ts->steprollback = PETSC_FALSE; 36952808aa04SLisandro Dalcin } 369610bc0e4dSStefano Zampini if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && ts->ptime + ts->time_step > ts->max_time) ts->time_step = ts->max_time - ts->ptime; 3697193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3698193ac0bcSJed Brown 3699ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3700f05ece33SBarry Smith 3701193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3702193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 3703a6772fa2SLisandro Dalcin if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3704cc708dedSBarry Smith ts->solvetime = ts->ptime; 3705a6772fa2SLisandro Dalcin solution = ts->vec_sol; 3706be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 3707db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3708db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3709e7069c78SShri 37102808aa04SLisandro Dalcin if (!ts->steps) { 37112808aa04SLisandro Dalcin ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 37126427ac75SLisandro Dalcin ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 37132808aa04SLisandro Dalcin } 37146427ac75SLisandro Dalcin 3715e1a7a14fSJed Brown while (!ts->reason) { 37162808aa04SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 37179687d888SLisandro Dalcin if (!ts->steprollback) { 37189687d888SLisandro Dalcin ierr = TSPreStep(ts);CHKERRQ(ierr); 37199687d888SLisandro Dalcin } 3720193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 3721e783b05fSHong Zhang if (ts->vec_costintegral && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */ 3722b1cb36f3SHong Zhang ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr); 3723b1cb36f3SHong Zhang } 372458818c2dSLisandro Dalcin if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */ 3725715f1b00SHong Zhang ierr = TSForwardStep(ts);CHKERRQ(ierr); 3726715f1b00SHong Zhang } 372710b82f12SShri Abhyankar ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 3728e783b05fSHong 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. */ 372958818c2dSLisandro Dalcin if (ts->steprollback) { 373058818c2dSLisandro Dalcin ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 373158818c2dSLisandro Dalcin } 3732e783b05fSHong Zhang if (!ts->steprollback) { 37332808aa04SLisandro Dalcin ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 37341eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 3735aeb4809dSShri Abhyankar } 3736193ac0bcSJed Brown } 37372808aa04SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 37386427ac75SLisandro Dalcin 373949354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 37400910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 3741cc708dedSBarry Smith ts->solvetime = ts->max_time; 3742b06615a5SLisandro Dalcin solution = u; 374363e21af5SBarry Smith ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr); 37440574a7fbSJed Brown } else { 3745ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3746cc708dedSBarry Smith ts->solvetime = ts->ptime; 3747b06615a5SLisandro Dalcin solution = ts->vec_sol; 37480574a7fbSJed Brown } 3749193ac0bcSJed Brown } 3750d2daff3dSHong Zhang 3751ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 375263e21af5SBarry Smith ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr); 375356f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 3754ef222394SBarry Smith if (ts->adjoint_solve) { 3755ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 37562b0a91c0SBarry Smith } 37576a4d4014SLisandro Dalcin PetscFunctionReturn(0); 37586a4d4014SLisandro Dalcin } 37596a4d4014SLisandro Dalcin 37607db568b7SBarry Smith /*@C 3761228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 3762228d79bcSJed Brown 3763228d79bcSJed Brown Collective on TS 3764228d79bcSJed Brown 3765228d79bcSJed Brown Input Parameters: 3766228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 3767228d79bcSJed Brown . step - step number that has just completed 3768228d79bcSJed Brown . ptime - model time of the state 37690910c330SBarry Smith - u - state at the current model time 3770228d79bcSJed Brown 3771228d79bcSJed Brown Notes: 37727db568b7SBarry Smith TSMonitor() is typically used automatically within the time stepping implementations. 37737db568b7SBarry Smith Users would almost never call this routine directly. 3774228d79bcSJed Brown 377563e21af5SBarry Smith A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions 377663e21af5SBarry Smith 37777db568b7SBarry Smith Level: developer 3778228d79bcSJed Brown 3779228d79bcSJed Brown .keywords: TS, timestep 3780228d79bcSJed Brown @*/ 37810910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 3782d763cef2SBarry Smith { 3783d6152f81SLisandro Dalcin DM dm; 3784a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 3785d6152f81SLisandro Dalcin PetscErrorCode ierr; 3786d763cef2SBarry Smith 3787d763cef2SBarry Smith PetscFunctionBegin; 3788b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3789b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 3790d6152f81SLisandro Dalcin 3791d6152f81SLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 3792d6152f81SLisandro Dalcin ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr); 3793d6152f81SLisandro Dalcin 37945edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 3795d763cef2SBarry Smith for (i=0; i<n; i++) { 37960910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 3797d763cef2SBarry Smith } 37985edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 3799d763cef2SBarry Smith PetscFunctionReturn(0); 3800d763cef2SBarry Smith } 3801d763cef2SBarry Smith 3802d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 3803d763cef2SBarry Smith /*@C 38047db568b7SBarry Smith TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 3805a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 3806d763cef2SBarry Smith 3807d763cef2SBarry Smith Collective on TS 3808d763cef2SBarry Smith 3809d763cef2SBarry Smith Input Parameters: 3810d763cef2SBarry Smith + host - the X display to open, or null for the local machine 3811d763cef2SBarry Smith . label - the title to put in the title bar 38127c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 3813a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 3814a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 3815d763cef2SBarry Smith 3816d763cef2SBarry Smith Output Parameter: 38170b039ecaSBarry Smith . ctx - the context 3818d763cef2SBarry Smith 3819d763cef2SBarry Smith Options Database Key: 38204f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 38218b668821SLisandro Dalcin + -ts_monitor_lg_timestep_log - automatically sets line graph monitor 38227db568b7SBarry Smith . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 38237db568b7SBarry Smith . -ts_monitor_lg_error - monitor the error 38247db568b7SBarry Smith . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 38257db568b7SBarry Smith . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 3826b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 3827d763cef2SBarry Smith 3828d763cef2SBarry Smith Notes: 3829a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 3830d763cef2SBarry Smith 38317db568b7SBarry Smith One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 38327db568b7SBarry Smith 38337db568b7SBarry 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 38347db568b7SBarry 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 38357db568b7SBarry Smith as the first argument. 38367db568b7SBarry Smith 38377db568b7SBarry Smith One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 38387db568b7SBarry Smith 3839d763cef2SBarry Smith Level: intermediate 3840d763cef2SBarry Smith 38417db568b7SBarry Smith .keywords: TS, monitor, line graph, residual 3842d763cef2SBarry Smith 38437db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 38447db568b7SBarry Smith TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 38457db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 38467db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 38477db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 38487c922b88SBarry Smith 3849d763cef2SBarry Smith @*/ 3850a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 3851d763cef2SBarry Smith { 38527f52daa2SLisandro Dalcin PetscDraw draw; 3853dfbe8321SBarry Smith PetscErrorCode ierr; 3854d763cef2SBarry Smith 3855d763cef2SBarry Smith PetscFunctionBegin; 3856b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 38577f52daa2SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 38587f52daa2SLisandro Dalcin ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 38597f52daa2SLisandro Dalcin ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 3860287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 38617f52daa2SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 3862a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 3863d763cef2SBarry Smith PetscFunctionReturn(0); 3864d763cef2SBarry Smith } 3865d763cef2SBarry Smith 3866b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 3867d763cef2SBarry Smith { 38680b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 3869c32365f1SBarry Smith PetscReal x = ptime,y; 3870dfbe8321SBarry Smith PetscErrorCode ierr; 3871d763cef2SBarry Smith 3872d763cef2SBarry Smith PetscFunctionBegin; 387363e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */ 3874b06615a5SLisandro Dalcin if (!step) { 3875a9f9c1f6SBarry Smith PetscDrawAxis axis; 38768b668821SLisandro Dalcin const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step"; 3877a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 38788b668821SLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr); 3879a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 3880a9f9c1f6SBarry Smith } 3881c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 38828b668821SLisandro Dalcin if (ctx->semilogy) y = PetscLog10Real(y); 38830b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 3884b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 38850b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 38866934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 38873923b477SBarry Smith } 3888d763cef2SBarry Smith PetscFunctionReturn(0); 3889d763cef2SBarry Smith } 3890d763cef2SBarry Smith 3891d763cef2SBarry Smith /*@C 3892a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 3893a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 3894d763cef2SBarry Smith 38950b039ecaSBarry Smith Collective on TSMonitorLGCtx 3896d763cef2SBarry Smith 3897d763cef2SBarry Smith Input Parameter: 38980b039ecaSBarry Smith . ctx - the monitor context 3899d763cef2SBarry Smith 3900d763cef2SBarry Smith Level: intermediate 3901d763cef2SBarry Smith 3902d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 3903d763cef2SBarry Smith 39044f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 3905d763cef2SBarry Smith @*/ 3906a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 3907d763cef2SBarry Smith { 3908dfbe8321SBarry Smith PetscErrorCode ierr; 3909d763cef2SBarry Smith 3910d763cef2SBarry Smith PetscFunctionBegin; 39117684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 39127684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 39137684fa3eSBarry Smith } 39140b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 391531152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 3916387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 3917387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 3918387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 39190b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 3920d763cef2SBarry Smith PetscFunctionReturn(0); 3921d763cef2SBarry Smith } 3922d763cef2SBarry Smith 3923d763cef2SBarry Smith /*@ 3924b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 3925d763cef2SBarry Smith 3926d763cef2SBarry Smith Not Collective 3927d763cef2SBarry Smith 3928d763cef2SBarry Smith Input Parameter: 3929d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3930d763cef2SBarry Smith 3931d763cef2SBarry Smith Output Parameter: 393219eac22cSLisandro Dalcin . t - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime(). 3933d763cef2SBarry Smith 3934d763cef2SBarry Smith Level: beginner 3935d763cef2SBarry Smith 3936b8123daeSJed Brown Note: 3937b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 39389be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 3939b8123daeSJed Brown 3940aaa6c58dSLisandro Dalcin .seealso: TSGetSolveTime(), TSSetTime(), TSGetTimeStep() 3941d763cef2SBarry Smith 3942d763cef2SBarry Smith .keywords: TS, get, time 3943d763cef2SBarry Smith @*/ 39447087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 3945d763cef2SBarry Smith { 3946d763cef2SBarry Smith PetscFunctionBegin; 39470700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3948f7cf8827SBarry Smith PetscValidRealPointer(t,2); 3949d763cef2SBarry Smith *t = ts->ptime; 3950d763cef2SBarry Smith PetscFunctionReturn(0); 3951d763cef2SBarry Smith } 3952d763cef2SBarry Smith 3953e5e524a1SHong Zhang /*@ 3954e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 3955e5e524a1SHong Zhang 3956e5e524a1SHong Zhang Not Collective 3957e5e524a1SHong Zhang 3958e5e524a1SHong Zhang Input Parameter: 3959e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 3960e5e524a1SHong Zhang 3961e5e524a1SHong Zhang Output Parameter: 3962e5e524a1SHong Zhang . t - the previous time 3963e5e524a1SHong Zhang 3964e5e524a1SHong Zhang Level: beginner 3965e5e524a1SHong Zhang 3966aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep() 3967e5e524a1SHong Zhang 3968e5e524a1SHong Zhang .keywords: TS, get, time 3969e5e524a1SHong Zhang @*/ 3970e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 3971e5e524a1SHong Zhang { 3972e5e524a1SHong Zhang PetscFunctionBegin; 3973e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3974e5e524a1SHong Zhang PetscValidRealPointer(t,2); 3975e5e524a1SHong Zhang *t = ts->ptime_prev; 3976e5e524a1SHong Zhang PetscFunctionReturn(0); 3977e5e524a1SHong Zhang } 3978e5e524a1SHong Zhang 39796a4d4014SLisandro Dalcin /*@ 39806a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 39816a4d4014SLisandro Dalcin 39823f9fe445SBarry Smith Logically Collective on TS 39836a4d4014SLisandro Dalcin 39846a4d4014SLisandro Dalcin Input Parameters: 39856a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 39866a4d4014SLisandro Dalcin - time - the time 39876a4d4014SLisandro Dalcin 39886a4d4014SLisandro Dalcin Level: intermediate 39896a4d4014SLisandro Dalcin 399019eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps() 39916a4d4014SLisandro Dalcin 39926a4d4014SLisandro Dalcin .keywords: TS, set, time 39936a4d4014SLisandro Dalcin @*/ 39947087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 39956a4d4014SLisandro Dalcin { 39966a4d4014SLisandro Dalcin PetscFunctionBegin; 39970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3998c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 39996a4d4014SLisandro Dalcin ts->ptime = t; 40006a4d4014SLisandro Dalcin PetscFunctionReturn(0); 40016a4d4014SLisandro Dalcin } 40026a4d4014SLisandro Dalcin 4003d763cef2SBarry Smith /*@C 4004d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4005d763cef2SBarry Smith TS options in the database. 4006d763cef2SBarry Smith 40073f9fe445SBarry Smith Logically Collective on TS 4008d763cef2SBarry Smith 4009d763cef2SBarry Smith Input Parameter: 4010d763cef2SBarry Smith + ts - The TS context 4011d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4012d763cef2SBarry Smith 4013d763cef2SBarry Smith Notes: 4014d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4015d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4016d763cef2SBarry Smith hyphen. 4017d763cef2SBarry Smith 4018d763cef2SBarry Smith Level: advanced 4019d763cef2SBarry Smith 4020d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 4021d763cef2SBarry Smith 4022d763cef2SBarry Smith .seealso: TSSetFromOptions() 4023d763cef2SBarry Smith 4024d763cef2SBarry Smith @*/ 40257087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 4026d763cef2SBarry Smith { 4027dfbe8321SBarry Smith PetscErrorCode ierr; 4028089b2837SJed Brown SNES snes; 4029d763cef2SBarry Smith 4030d763cef2SBarry Smith PetscFunctionBegin; 40310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4032d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4033089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4034089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4035d763cef2SBarry Smith PetscFunctionReturn(0); 4036d763cef2SBarry Smith } 4037d763cef2SBarry Smith 4038d763cef2SBarry Smith /*@C 4039d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4040d763cef2SBarry Smith TS options in the database. 4041d763cef2SBarry Smith 40423f9fe445SBarry Smith Logically Collective on TS 4043d763cef2SBarry Smith 4044d763cef2SBarry Smith Input Parameter: 4045d763cef2SBarry Smith + ts - The TS context 4046d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4047d763cef2SBarry Smith 4048d763cef2SBarry Smith Notes: 4049d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4050d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4051d763cef2SBarry Smith hyphen. 4052d763cef2SBarry Smith 4053d763cef2SBarry Smith Level: advanced 4054d763cef2SBarry Smith 4055d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 4056d763cef2SBarry Smith 4057d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 4058d763cef2SBarry Smith 4059d763cef2SBarry Smith @*/ 40607087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4061d763cef2SBarry Smith { 4062dfbe8321SBarry Smith PetscErrorCode ierr; 4063089b2837SJed Brown SNES snes; 4064d763cef2SBarry Smith 4065d763cef2SBarry Smith PetscFunctionBegin; 40660700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4067d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4068089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4069089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4070d763cef2SBarry Smith PetscFunctionReturn(0); 4071d763cef2SBarry Smith } 4072d763cef2SBarry Smith 4073d763cef2SBarry Smith /*@C 4074d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4075d763cef2SBarry Smith TS options in the database. 4076d763cef2SBarry Smith 4077d763cef2SBarry Smith Not Collective 4078d763cef2SBarry Smith 4079d763cef2SBarry Smith Input Parameter: 4080d763cef2SBarry Smith . ts - The TS context 4081d763cef2SBarry Smith 4082d763cef2SBarry Smith Output Parameter: 4083d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4084d763cef2SBarry Smith 4085d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 4086d763cef2SBarry Smith sufficient length to hold the prefix. 4087d763cef2SBarry Smith 4088d763cef2SBarry Smith Level: intermediate 4089d763cef2SBarry Smith 4090d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 4091d763cef2SBarry Smith 4092d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 4093d763cef2SBarry Smith @*/ 40947087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4095d763cef2SBarry Smith { 4096dfbe8321SBarry Smith PetscErrorCode ierr; 4097d763cef2SBarry Smith 4098d763cef2SBarry Smith PetscFunctionBegin; 40990700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 41004482741eSBarry Smith PetscValidPointer(prefix,2); 4101d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4102d763cef2SBarry Smith PetscFunctionReturn(0); 4103d763cef2SBarry Smith } 4104d763cef2SBarry Smith 4105d763cef2SBarry Smith /*@C 4106d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4107d763cef2SBarry Smith 4108d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4109d763cef2SBarry Smith 4110d763cef2SBarry Smith Input Parameter: 4111d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4112d763cef2SBarry Smith 4113d763cef2SBarry Smith Output Parameters: 4114e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4115e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4116e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4117e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4118d763cef2SBarry Smith 41190298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 4120d763cef2SBarry Smith 4121d763cef2SBarry Smith Level: intermediate 4122d763cef2SBarry Smith 412380275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 4124d763cef2SBarry Smith 4125d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 4126d763cef2SBarry Smith @*/ 4127e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4128d763cef2SBarry Smith { 4129089b2837SJed Brown PetscErrorCode ierr; 413024989b8cSPeter Brune DM dm; 4131089b2837SJed Brown 4132d763cef2SBarry Smith PetscFunctionBegin; 413323a57915SBarry Smith if (Amat || Pmat) { 413423a57915SBarry Smith SNES snes; 4135089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 413623a57915SBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4137e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 413823a57915SBarry Smith } 413924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 414024989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 4141d763cef2SBarry Smith PetscFunctionReturn(0); 4142d763cef2SBarry Smith } 4143d763cef2SBarry Smith 41442eca1d9cSJed Brown /*@C 41452eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 41462eca1d9cSJed Brown 41472eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 41482eca1d9cSJed Brown 41492eca1d9cSJed Brown Input Parameter: 41502eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 41512eca1d9cSJed Brown 41522eca1d9cSJed Brown Output Parameters: 4153e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4154e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 41552eca1d9cSJed Brown . f - The function to compute the matrices 41562eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 41572eca1d9cSJed Brown 41580298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 41592eca1d9cSJed Brown 41602eca1d9cSJed Brown Level: advanced 41612eca1d9cSJed Brown 416280275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 41632eca1d9cSJed Brown 41642eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 41652eca1d9cSJed Brown @*/ 4166e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 41672eca1d9cSJed Brown { 4168089b2837SJed Brown PetscErrorCode ierr; 416924989b8cSPeter Brune DM dm; 41700910c330SBarry Smith 41712eca1d9cSJed Brown PetscFunctionBegin; 4172c0aab802Sstefano_zampini if (Amat || Pmat) { 4173c0aab802Sstefano_zampini SNES snes; 4174089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4175f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4176e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 4177c0aab802Sstefano_zampini } 417824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 417924989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 41802eca1d9cSJed Brown PetscFunctionReturn(0); 41812eca1d9cSJed Brown } 41822eca1d9cSJed Brown 41831713a123SBarry Smith /*@C 418483a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 41851713a123SBarry Smith VecView() for the solution at each timestep 41861713a123SBarry Smith 41871713a123SBarry Smith Collective on TS 41881713a123SBarry Smith 41891713a123SBarry Smith Input Parameters: 41901713a123SBarry Smith + ts - the TS context 41911713a123SBarry Smith . step - current time-step 4192142b95e3SSatish Balay . ptime - current time 41930298fd71SBarry Smith - dummy - either a viewer or NULL 41941713a123SBarry Smith 419599fdda47SBarry Smith Options Database: 419699fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 419799fdda47SBarry Smith 4198387f4636SBarry Smith Notes: the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 419999fdda47SBarry Smith will look bad 420099fdda47SBarry Smith 42011713a123SBarry Smith Level: intermediate 42021713a123SBarry Smith 42031713a123SBarry Smith .keywords: TS, vector, monitor, view 42041713a123SBarry Smith 4205a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 42061713a123SBarry Smith @*/ 42070910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 42081713a123SBarry Smith { 4209dfbe8321SBarry Smith PetscErrorCode ierr; 421083a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4211473a3ab2SBarry Smith PetscDraw draw; 42121713a123SBarry Smith 42131713a123SBarry Smith PetscFunctionBegin; 42146083293cSBarry Smith if (!step && ictx->showinitial) { 42156083293cSBarry Smith if (!ictx->initialsolution) { 42160910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 42171713a123SBarry Smith } 42180910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 42196083293cSBarry Smith } 4220b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 42210dcf80beSBarry Smith 42226083293cSBarry Smith if (ictx->showinitial) { 42236083293cSBarry Smith PetscReal pause; 42246083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 42256083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 42266083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 42276083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 42286083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 42296083293cSBarry Smith } 42300910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 4231473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 423251fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 4233473a3ab2SBarry Smith char time[32]; 4234473a3ab2SBarry Smith 4235473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 423685b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4237473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4238473a3ab2SBarry Smith h = yl + .95*(yr - yl); 423951fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4240473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4241473a3ab2SBarry Smith } 4242473a3ab2SBarry Smith 42436083293cSBarry Smith if (ictx->showinitial) { 42446083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 42456083293cSBarry Smith } 42461713a123SBarry Smith PetscFunctionReturn(0); 42471713a123SBarry Smith } 42481713a123SBarry Smith 42499110b2e7SHong Zhang /*@C 42502d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 42512d5ee99bSBarry Smith 42522d5ee99bSBarry Smith Collective on TS 42532d5ee99bSBarry Smith 42542d5ee99bSBarry Smith Input Parameters: 42552d5ee99bSBarry Smith + ts - the TS context 42562d5ee99bSBarry Smith . step - current time-step 42572d5ee99bSBarry Smith . ptime - current time 42582d5ee99bSBarry Smith - dummy - either a viewer or NULL 42592d5ee99bSBarry Smith 42602d5ee99bSBarry Smith Level: intermediate 42612d5ee99bSBarry Smith 42622d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 42632d5ee99bSBarry Smith 42642d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 42652d5ee99bSBarry Smith @*/ 42662d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 42672d5ee99bSBarry Smith { 42682d5ee99bSBarry Smith PetscErrorCode ierr; 42692d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 42702d5ee99bSBarry Smith PetscDraw draw; 42716934998bSLisandro Dalcin PetscDrawAxis axis; 42722d5ee99bSBarry Smith PetscInt n; 42732d5ee99bSBarry Smith PetscMPIInt size; 42746934998bSLisandro Dalcin PetscReal U0,U1,xl,yl,xr,yr,h; 42752d5ee99bSBarry Smith char time[32]; 42762d5ee99bSBarry Smith const PetscScalar *U; 42772d5ee99bSBarry Smith 42782d5ee99bSBarry Smith PetscFunctionBegin; 42796934998bSLisandro Dalcin ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr); 42806934998bSLisandro Dalcin if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs"); 42812d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 42826934998bSLisandro Dalcin if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 42832d5ee99bSBarry Smith 42842d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 42856934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr); 42866934998bSLisandro Dalcin ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 42876934998bSLisandro Dalcin if (!step) { 42886934998bSLisandro Dalcin ierr = PetscDrawClear(draw);CHKERRQ(ierr); 42896934998bSLisandro Dalcin ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); 42906934998bSLisandro Dalcin } 42912d5ee99bSBarry Smith 42922d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 42936934998bSLisandro Dalcin U0 = PetscRealPart(U[0]); 42946934998bSLisandro Dalcin U1 = PetscRealPart(U[1]); 4295a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 42966934998bSLisandro Dalcin if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0); 42972d5ee99bSBarry Smith 42986934998bSLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 42996934998bSLisandro Dalcin ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr); 43002d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 43014b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 430285b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 43032d5ee99bSBarry Smith h = yl + .95*(yr - yl); 430451fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 43052d5ee99bSBarry Smith } 43066934998bSLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 43072d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 430856d62c8fSLisandro Dalcin ierr = PetscDrawPause(draw);CHKERRQ(ierr); 43096934998bSLisandro Dalcin ierr = PetscDrawSave(draw);CHKERRQ(ierr); 43102d5ee99bSBarry Smith PetscFunctionReturn(0); 43112d5ee99bSBarry Smith } 43122d5ee99bSBarry Smith 43136083293cSBarry Smith /*@C 431483a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 43156083293cSBarry Smith 43166083293cSBarry Smith Collective on TS 43176083293cSBarry Smith 43186083293cSBarry Smith Input Parameters: 43196083293cSBarry Smith . ctx - the monitor context 43206083293cSBarry Smith 43216083293cSBarry Smith Level: intermediate 43226083293cSBarry Smith 43236083293cSBarry Smith .keywords: TS, vector, monitor, view 43246083293cSBarry Smith 432583a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 43266083293cSBarry Smith @*/ 432783a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 43286083293cSBarry Smith { 43296083293cSBarry Smith PetscErrorCode ierr; 43306083293cSBarry Smith 43316083293cSBarry Smith PetscFunctionBegin; 433283a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 433383a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 433483a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 43356083293cSBarry Smith PetscFunctionReturn(0); 43366083293cSBarry Smith } 43376083293cSBarry Smith 43386083293cSBarry Smith /*@C 433983a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 43406083293cSBarry Smith 43416083293cSBarry Smith Collective on TS 43426083293cSBarry Smith 43436083293cSBarry Smith Input Parameter: 43446083293cSBarry Smith . ts - time-step context 43456083293cSBarry Smith 43466083293cSBarry Smith Output Patameter: 43476083293cSBarry Smith . ctx - the monitor context 43486083293cSBarry Smith 434999fdda47SBarry Smith Options Database: 435099fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 435199fdda47SBarry Smith 43526083293cSBarry Smith Level: intermediate 43536083293cSBarry Smith 43546083293cSBarry Smith .keywords: TS, vector, monitor, view 43556083293cSBarry Smith 435683a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 43576083293cSBarry Smith @*/ 435883a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 43596083293cSBarry Smith { 43606083293cSBarry Smith PetscErrorCode ierr; 43616083293cSBarry Smith 43626083293cSBarry Smith PetscFunctionBegin; 4363b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 436483a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4365e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4366bbd56ea5SKarl Rupp 436783a4ac43SBarry Smith (*ctx)->howoften = howoften; 4368473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 4369c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4370473a3ab2SBarry Smith 4371473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 4372c5929fdfSBarry Smith ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 43736083293cSBarry Smith PetscFunctionReturn(0); 43746083293cSBarry Smith } 43756083293cSBarry Smith 43763a471f94SBarry Smith /*@C 43770ed3bfb6SBarry Smith TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling 43780ed3bfb6SBarry Smith VecView() for the solution provided by TSSetSolutionFunction() at each timestep 43790ed3bfb6SBarry Smith 43800ed3bfb6SBarry Smith Collective on TS 43810ed3bfb6SBarry Smith 43820ed3bfb6SBarry Smith Input Parameters: 43830ed3bfb6SBarry Smith + ts - the TS context 43840ed3bfb6SBarry Smith . step - current time-step 43850ed3bfb6SBarry Smith . ptime - current time 43860ed3bfb6SBarry Smith - dummy - either a viewer or NULL 43870ed3bfb6SBarry Smith 43880ed3bfb6SBarry Smith Options Database: 43890ed3bfb6SBarry Smith . -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 43900ed3bfb6SBarry Smith 43910ed3bfb6SBarry Smith Level: intermediate 43920ed3bfb6SBarry Smith 43930ed3bfb6SBarry Smith .keywords: TS, vector, monitor, view 43940ed3bfb6SBarry Smith 43950ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 43960ed3bfb6SBarry Smith @*/ 43970ed3bfb6SBarry Smith PetscErrorCode TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 43980ed3bfb6SBarry Smith { 43990ed3bfb6SBarry Smith PetscErrorCode ierr; 44000ed3bfb6SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 44010ed3bfb6SBarry Smith PetscViewer viewer = ctx->viewer; 44020ed3bfb6SBarry Smith Vec work; 44030ed3bfb6SBarry Smith 44040ed3bfb6SBarry Smith PetscFunctionBegin; 44050ed3bfb6SBarry Smith if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 44060ed3bfb6SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 44070ed3bfb6SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 44080ed3bfb6SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 44090ed3bfb6SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 44100ed3bfb6SBarry Smith PetscFunctionReturn(0); 44110ed3bfb6SBarry Smith } 44120ed3bfb6SBarry Smith 44130ed3bfb6SBarry Smith /*@C 441483a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 44153a471f94SBarry Smith VecView() for the error at each timestep 44163a471f94SBarry Smith 44173a471f94SBarry Smith Collective on TS 44183a471f94SBarry Smith 44193a471f94SBarry Smith Input Parameters: 44203a471f94SBarry Smith + ts - the TS context 44213a471f94SBarry Smith . step - current time-step 44223a471f94SBarry Smith . ptime - current time 44230298fd71SBarry Smith - dummy - either a viewer or NULL 44243a471f94SBarry Smith 44250ed3bfb6SBarry Smith Options Database: 44260ed3bfb6SBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 44270ed3bfb6SBarry Smith 44283a471f94SBarry Smith Level: intermediate 44293a471f94SBarry Smith 44303a471f94SBarry Smith .keywords: TS, vector, monitor, view 44313a471f94SBarry Smith 44320ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 44333a471f94SBarry Smith @*/ 44340910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 44353a471f94SBarry Smith { 44363a471f94SBarry Smith PetscErrorCode ierr; 443783a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 443883a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 44393a471f94SBarry Smith Vec work; 44403a471f94SBarry Smith 44413a471f94SBarry Smith PetscFunctionBegin; 4442b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 44430910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 44443a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 44450910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 44463a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 44473a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 44483a471f94SBarry Smith PetscFunctionReturn(0); 44493a471f94SBarry Smith } 44503a471f94SBarry Smith 4451af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 44526c699258SBarry Smith /*@ 44532a808120SBarry Smith TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS 44546c699258SBarry Smith 44553f9fe445SBarry Smith Logically Collective on TS and DM 44566c699258SBarry Smith 44576c699258SBarry Smith Input Parameters: 44582a808120SBarry Smith + ts - the ODE integrator object 44592a808120SBarry Smith - dm - the dm, cannot be NULL 44606c699258SBarry Smith 44616c699258SBarry Smith Level: intermediate 44626c699258SBarry Smith 44636c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 44646c699258SBarry Smith @*/ 44657087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 44666c699258SBarry Smith { 44676c699258SBarry Smith PetscErrorCode ierr; 4468089b2837SJed Brown SNES snes; 4469942e3340SBarry Smith DMTS tsdm; 44706c699258SBarry Smith 44716c699258SBarry Smith PetscFunctionBegin; 44720700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 44732a808120SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 447470663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4475942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 44762a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4477942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4478942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 447924989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 448024989b8cSPeter Brune tsdm->originaldm = dm; 448124989b8cSPeter Brune } 448224989b8cSPeter Brune } 44836bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 448424989b8cSPeter Brune } 44856c699258SBarry Smith ts->dm = dm; 4486bbd56ea5SKarl Rupp 4487089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4488089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 44896c699258SBarry Smith PetscFunctionReturn(0); 44906c699258SBarry Smith } 44916c699258SBarry Smith 44926c699258SBarry Smith /*@ 44936c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 44946c699258SBarry Smith 44953f9fe445SBarry Smith Not Collective 44966c699258SBarry Smith 44976c699258SBarry Smith Input Parameter: 44986c699258SBarry Smith . ts - the preconditioner context 44996c699258SBarry Smith 45006c699258SBarry Smith Output Parameter: 45016c699258SBarry Smith . dm - the dm 45026c699258SBarry Smith 45036c699258SBarry Smith Level: intermediate 45046c699258SBarry Smith 45056c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 45066c699258SBarry Smith @*/ 45077087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 45086c699258SBarry Smith { 4509496e6a7aSJed Brown PetscErrorCode ierr; 4510496e6a7aSJed Brown 45116c699258SBarry Smith PetscFunctionBegin; 45120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4513496e6a7aSJed Brown if (!ts->dm) { 4514ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4515496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4516496e6a7aSJed Brown } 45176c699258SBarry Smith *dm = ts->dm; 45186c699258SBarry Smith PetscFunctionReturn(0); 45196c699258SBarry Smith } 45201713a123SBarry Smith 45210f5c6efeSJed Brown /*@ 45220f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 45230f5c6efeSJed Brown 45243f9fe445SBarry Smith Logically Collective on SNES 45250f5c6efeSJed Brown 45260f5c6efeSJed Brown Input Parameter: 4527d42a1c89SJed Brown + snes - nonlinear solver 45280910c330SBarry Smith . U - the current state at which to evaluate the residual 4529d42a1c89SJed Brown - ctx - user context, must be a TS 45300f5c6efeSJed Brown 45310f5c6efeSJed Brown Output Parameter: 45320f5c6efeSJed Brown . F - the nonlinear residual 45330f5c6efeSJed Brown 45340f5c6efeSJed Brown Notes: 45350f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 45360f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 45370f5c6efeSJed Brown 45380f5c6efeSJed Brown Level: advanced 45390f5c6efeSJed Brown 45400f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 45410f5c6efeSJed Brown @*/ 45420910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 45430f5c6efeSJed Brown { 45440f5c6efeSJed Brown TS ts = (TS)ctx; 45450f5c6efeSJed Brown PetscErrorCode ierr; 45460f5c6efeSJed Brown 45470f5c6efeSJed Brown PetscFunctionBegin; 45480f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 45490910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 45500f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 45510f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 45520910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 45530f5c6efeSJed Brown PetscFunctionReturn(0); 45540f5c6efeSJed Brown } 45550f5c6efeSJed Brown 45560f5c6efeSJed Brown /*@ 45570f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 45580f5c6efeSJed Brown 45590f5c6efeSJed Brown Collective on SNES 45600f5c6efeSJed Brown 45610f5c6efeSJed Brown Input Parameter: 45620f5c6efeSJed Brown + snes - nonlinear solver 45630910c330SBarry Smith . U - the current state at which to evaluate the residual 45640f5c6efeSJed Brown - ctx - user context, must be a TS 45650f5c6efeSJed Brown 45660f5c6efeSJed Brown Output Parameter: 45670f5c6efeSJed Brown + A - the Jacobian 45680f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 45690f5c6efeSJed Brown - flag - indicates any structure change in the matrix 45700f5c6efeSJed Brown 45710f5c6efeSJed Brown Notes: 45720f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 45730f5c6efeSJed Brown 45740f5c6efeSJed Brown Level: developer 45750f5c6efeSJed Brown 45760f5c6efeSJed Brown .seealso: SNESSetJacobian() 45770f5c6efeSJed Brown @*/ 4578d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 45790f5c6efeSJed Brown { 45800f5c6efeSJed Brown TS ts = (TS)ctx; 45810f5c6efeSJed Brown PetscErrorCode ierr; 45820f5c6efeSJed Brown 45830f5c6efeSJed Brown PetscFunctionBegin; 45840f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 45850910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 45860f5c6efeSJed Brown PetscValidPointer(A,3); 458794ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 45880f5c6efeSJed Brown PetscValidPointer(B,4); 458994ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 45900f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 4591d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 45920f5c6efeSJed Brown PetscFunctionReturn(0); 45930f5c6efeSJed Brown } 4594325fc9f4SBarry Smith 45950e4ef248SJed Brown /*@C 45969ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 45970e4ef248SJed Brown 45980e4ef248SJed Brown Collective on TS 45990e4ef248SJed Brown 46000e4ef248SJed Brown Input Arguments: 46010e4ef248SJed Brown + ts - time stepping context 46020e4ef248SJed Brown . t - time at which to evaluate 46030910c330SBarry Smith . U - state at which to evaluate 46040e4ef248SJed Brown - ctx - context 46050e4ef248SJed Brown 46060e4ef248SJed Brown Output Arguments: 46070e4ef248SJed Brown . F - right hand side 46080e4ef248SJed Brown 46090e4ef248SJed Brown Level: intermediate 46100e4ef248SJed Brown 46110e4ef248SJed Brown Notes: 46120e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 46130e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 46140e4ef248SJed Brown 46150e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 46160e4ef248SJed Brown @*/ 46170910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 46180e4ef248SJed Brown { 46190e4ef248SJed Brown PetscErrorCode ierr; 46200e4ef248SJed Brown Mat Arhs,Brhs; 46210e4ef248SJed Brown 46220e4ef248SJed Brown PetscFunctionBegin; 46230e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 4624d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 46250910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 46260e4ef248SJed Brown PetscFunctionReturn(0); 46270e4ef248SJed Brown } 46280e4ef248SJed Brown 46290e4ef248SJed Brown /*@C 46300e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 46310e4ef248SJed Brown 46320e4ef248SJed Brown Collective on TS 46330e4ef248SJed Brown 46340e4ef248SJed Brown Input Arguments: 46350e4ef248SJed Brown + ts - time stepping context 46360e4ef248SJed Brown . t - time at which to evaluate 46370910c330SBarry Smith . U - state at which to evaluate 46380e4ef248SJed Brown - ctx - context 46390e4ef248SJed Brown 46400e4ef248SJed Brown Output Arguments: 46410e4ef248SJed Brown + A - pointer to operator 46420e4ef248SJed Brown . B - pointer to preconditioning matrix 46430e4ef248SJed Brown - flg - matrix structure flag 46440e4ef248SJed Brown 46450e4ef248SJed Brown Level: intermediate 46460e4ef248SJed Brown 46470e4ef248SJed Brown Notes: 46480e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 46490e4ef248SJed Brown 46500e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 46510e4ef248SJed Brown @*/ 4652d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 46530e4ef248SJed Brown { 46540e4ef248SJed Brown PetscFunctionBegin; 46550e4ef248SJed Brown PetscFunctionReturn(0); 46560e4ef248SJed Brown } 46570e4ef248SJed Brown 46580026cea9SSean Farley /*@C 46590026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 46600026cea9SSean Farley 46610026cea9SSean Farley Collective on TS 46620026cea9SSean Farley 46630026cea9SSean Farley Input Arguments: 46640026cea9SSean Farley + ts - time stepping context 46650026cea9SSean Farley . t - time at which to evaluate 46660910c330SBarry Smith . U - state at which to evaluate 46670910c330SBarry Smith . Udot - time derivative of state vector 46680026cea9SSean Farley - ctx - context 46690026cea9SSean Farley 46700026cea9SSean Farley Output Arguments: 46710026cea9SSean Farley . F - left hand side 46720026cea9SSean Farley 46730026cea9SSean Farley Level: intermediate 46740026cea9SSean Farley 46750026cea9SSean Farley Notes: 46760910c330SBarry 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 46770026cea9SSean Farley user is required to write their own TSComputeIFunction. 46780026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 46790026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 46800026cea9SSean Farley 46819ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 46829ae8fd06SBarry Smith 46839ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 46840026cea9SSean Farley @*/ 46850910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 46860026cea9SSean Farley { 46870026cea9SSean Farley PetscErrorCode ierr; 46880026cea9SSean Farley Mat A,B; 46890026cea9SSean Farley 46900026cea9SSean Farley PetscFunctionBegin; 46910298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 4692d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 46930910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 46940026cea9SSean Farley PetscFunctionReturn(0); 46950026cea9SSean Farley } 46960026cea9SSean Farley 46970026cea9SSean Farley /*@C 4698b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 46990026cea9SSean Farley 47000026cea9SSean Farley Collective on TS 47010026cea9SSean Farley 47020026cea9SSean Farley Input Arguments: 47030026cea9SSean Farley + ts - time stepping context 47040026cea9SSean Farley . t - time at which to evaluate 47050910c330SBarry Smith . U - state at which to evaluate 47060910c330SBarry Smith . Udot - time derivative of state vector 47070026cea9SSean Farley . shift - shift to apply 47080026cea9SSean Farley - ctx - context 47090026cea9SSean Farley 47100026cea9SSean Farley Output Arguments: 47110026cea9SSean Farley + A - pointer to operator 47120026cea9SSean Farley . B - pointer to preconditioning matrix 47130026cea9SSean Farley - flg - matrix structure flag 47140026cea9SSean Farley 4715b41af12eSJed Brown Level: advanced 47160026cea9SSean Farley 47170026cea9SSean Farley Notes: 47180026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 47190026cea9SSean Farley 4720b41af12eSJed Brown It is only appropriate for problems of the form 4721b41af12eSJed Brown 4722b41af12eSJed Brown $ M Udot = F(U,t) 4723b41af12eSJed Brown 4724b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4725b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4726b41af12eSJed Brown an implicit operator of the form 4727b41af12eSJed Brown 4728b41af12eSJed Brown $ shift*M + J 4729b41af12eSJed Brown 4730b41af12eSJed 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 4731b41af12eSJed Brown a copy of M or reassemble it when requested. 4732b41af12eSJed Brown 47330026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 47340026cea9SSean Farley @*/ 4735d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 47360026cea9SSean Farley { 4737b41af12eSJed Brown PetscErrorCode ierr; 4738b41af12eSJed Brown 47390026cea9SSean Farley PetscFunctionBegin; 474094ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4741b41af12eSJed Brown ts->ijacobian.shift = shift; 47420026cea9SSean Farley PetscFunctionReturn(0); 47430026cea9SSean Farley } 4744b41af12eSJed Brown 4745e817cc15SEmil Constantinescu /*@ 4746e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 4747e817cc15SEmil Constantinescu 4748e817cc15SEmil Constantinescu Not Collective 4749e817cc15SEmil Constantinescu 4750e817cc15SEmil Constantinescu Input Parameter: 4751e817cc15SEmil Constantinescu . ts - the TS context 4752e817cc15SEmil Constantinescu 4753e817cc15SEmil Constantinescu Output Parameter: 47544e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4755e817cc15SEmil Constantinescu 4756e817cc15SEmil Constantinescu Level: beginner 4757e817cc15SEmil Constantinescu 4758e817cc15SEmil Constantinescu .keywords: TS, equation type 4759e817cc15SEmil Constantinescu 4760e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 4761e817cc15SEmil Constantinescu @*/ 4762e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4763e817cc15SEmil Constantinescu { 4764e817cc15SEmil Constantinescu PetscFunctionBegin; 4765e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4766e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 4767e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 4768e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4769e817cc15SEmil Constantinescu } 4770e817cc15SEmil Constantinescu 4771e817cc15SEmil Constantinescu /*@ 4772e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 4773e817cc15SEmil Constantinescu 4774e817cc15SEmil Constantinescu Not Collective 4775e817cc15SEmil Constantinescu 4776e817cc15SEmil Constantinescu Input Parameter: 4777e817cc15SEmil Constantinescu + ts - the TS context 47781297b224SEmil Constantinescu - equation_type - see TSEquationType 4779e817cc15SEmil Constantinescu 4780e817cc15SEmil Constantinescu Level: advanced 4781e817cc15SEmil Constantinescu 4782e817cc15SEmil Constantinescu .keywords: TS, equation type 4783e817cc15SEmil Constantinescu 4784e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 4785e817cc15SEmil Constantinescu @*/ 4786e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4787e817cc15SEmil Constantinescu { 4788e817cc15SEmil Constantinescu PetscFunctionBegin; 4789e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4790e817cc15SEmil Constantinescu ts->equation_type = equation_type; 4791e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4792e817cc15SEmil Constantinescu } 47930026cea9SSean Farley 47944af1b03aSJed Brown /*@ 47954af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 47964af1b03aSJed Brown 47974af1b03aSJed Brown Not Collective 47984af1b03aSJed Brown 47994af1b03aSJed Brown Input Parameter: 48004af1b03aSJed Brown . ts - the TS context 48014af1b03aSJed Brown 48024af1b03aSJed Brown Output Parameter: 48034af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 48044af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 48054af1b03aSJed Brown 4806487e0bb9SJed Brown Level: beginner 48074af1b03aSJed Brown 4808cd652676SJed Brown Notes: 4809cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 48104af1b03aSJed Brown 48114af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 48124af1b03aSJed Brown 48134af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 48144af1b03aSJed Brown @*/ 48154af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 48164af1b03aSJed Brown { 48174af1b03aSJed Brown PetscFunctionBegin; 48184af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 48194af1b03aSJed Brown PetscValidPointer(reason,2); 48204af1b03aSJed Brown *reason = ts->reason; 48214af1b03aSJed Brown PetscFunctionReturn(0); 48224af1b03aSJed Brown } 48234af1b03aSJed Brown 4824d6ad946cSShri Abhyankar /*@ 4825d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4826d6ad946cSShri Abhyankar 4827d6ad946cSShri Abhyankar Not Collective 4828d6ad946cSShri Abhyankar 4829d6ad946cSShri Abhyankar Input Parameter: 4830d6ad946cSShri Abhyankar + ts - the TS context 4831d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4832d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4833d6ad946cSShri Abhyankar 4834f5abba47SShri Abhyankar Level: advanced 4835d6ad946cSShri Abhyankar 4836d6ad946cSShri Abhyankar Notes: 4837d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 4838d6ad946cSShri Abhyankar 4839d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 4840d6ad946cSShri Abhyankar 4841d6ad946cSShri Abhyankar .seealso: TSConvergedReason 4842d6ad946cSShri Abhyankar @*/ 4843d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4844d6ad946cSShri Abhyankar { 4845d6ad946cSShri Abhyankar PetscFunctionBegin; 4846d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4847d6ad946cSShri Abhyankar ts->reason = reason; 4848d6ad946cSShri Abhyankar PetscFunctionReturn(0); 4849d6ad946cSShri Abhyankar } 4850d6ad946cSShri Abhyankar 4851cc708dedSBarry Smith /*@ 4852cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 4853cc708dedSBarry Smith 4854cc708dedSBarry Smith Not Collective 4855cc708dedSBarry Smith 4856cc708dedSBarry Smith Input Parameter: 4857cc708dedSBarry Smith . ts - the TS context 4858cc708dedSBarry Smith 4859cc708dedSBarry Smith Output Parameter: 486019eac22cSLisandro Dalcin . ftime - the final time. This time corresponds to the final time set with TSSetMaxTime() 4861cc708dedSBarry Smith 4862487e0bb9SJed Brown Level: beginner 4863cc708dedSBarry Smith 4864cc708dedSBarry Smith Notes: 4865cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 4866cc708dedSBarry Smith 4867cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 4868cc708dedSBarry Smith 4869cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 4870cc708dedSBarry Smith @*/ 4871cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 4872cc708dedSBarry Smith { 4873cc708dedSBarry Smith PetscFunctionBegin; 4874cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4875cc708dedSBarry Smith PetscValidPointer(ftime,2); 4876cc708dedSBarry Smith *ftime = ts->solvetime; 4877cc708dedSBarry Smith PetscFunctionReturn(0); 4878cc708dedSBarry Smith } 4879cc708dedSBarry Smith 48802c18e0fdSBarry Smith /*@ 48815ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 48829f67acb7SJed Brown used by the time integrator. 48839f67acb7SJed Brown 48849f67acb7SJed Brown Not Collective 48859f67acb7SJed Brown 48869f67acb7SJed Brown Input Parameter: 48879f67acb7SJed Brown . ts - TS context 48889f67acb7SJed Brown 48899f67acb7SJed Brown Output Parameter: 48909f67acb7SJed Brown . nits - number of nonlinear iterations 48919f67acb7SJed Brown 48929f67acb7SJed Brown Notes: 48939f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 48949f67acb7SJed Brown 48959f67acb7SJed Brown Level: intermediate 48969f67acb7SJed Brown 48979f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 48989f67acb7SJed Brown 48995ef26d82SJed Brown .seealso: TSGetKSPIterations() 49009f67acb7SJed Brown @*/ 49015ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 49029f67acb7SJed Brown { 49039f67acb7SJed Brown PetscFunctionBegin; 49049f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 49059f67acb7SJed Brown PetscValidIntPointer(nits,2); 49065ef26d82SJed Brown *nits = ts->snes_its; 49079f67acb7SJed Brown PetscFunctionReturn(0); 49089f67acb7SJed Brown } 49099f67acb7SJed Brown 49109f67acb7SJed Brown /*@ 49115ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 49129f67acb7SJed Brown used by the time integrator. 49139f67acb7SJed Brown 49149f67acb7SJed Brown Not Collective 49159f67acb7SJed Brown 49169f67acb7SJed Brown Input Parameter: 49179f67acb7SJed Brown . ts - TS context 49189f67acb7SJed Brown 49199f67acb7SJed Brown Output Parameter: 49209f67acb7SJed Brown . lits - number of linear iterations 49219f67acb7SJed Brown 49229f67acb7SJed Brown Notes: 49239f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 49249f67acb7SJed Brown 49259f67acb7SJed Brown Level: intermediate 49269f67acb7SJed Brown 49279f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 49289f67acb7SJed Brown 49295ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 49309f67acb7SJed Brown @*/ 49315ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 49329f67acb7SJed Brown { 49339f67acb7SJed Brown PetscFunctionBegin; 49349f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 49359f67acb7SJed Brown PetscValidIntPointer(lits,2); 49365ef26d82SJed Brown *lits = ts->ksp_its; 49379f67acb7SJed Brown PetscFunctionReturn(0); 49389f67acb7SJed Brown } 49399f67acb7SJed Brown 4940cef5090cSJed Brown /*@ 4941cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4942cef5090cSJed Brown 4943cef5090cSJed Brown Not Collective 4944cef5090cSJed Brown 4945cef5090cSJed Brown Input Parameter: 4946cef5090cSJed Brown . ts - TS context 4947cef5090cSJed Brown 4948cef5090cSJed Brown Output Parameter: 4949cef5090cSJed Brown . rejects - number of steps rejected 4950cef5090cSJed Brown 4951cef5090cSJed Brown Notes: 4952cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4953cef5090cSJed Brown 4954cef5090cSJed Brown Level: intermediate 4955cef5090cSJed Brown 4956cef5090cSJed Brown .keywords: TS, get, number 4957cef5090cSJed Brown 49585ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 4959cef5090cSJed Brown @*/ 4960cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 4961cef5090cSJed Brown { 4962cef5090cSJed Brown PetscFunctionBegin; 4963cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4964cef5090cSJed Brown PetscValidIntPointer(rejects,2); 4965cef5090cSJed Brown *rejects = ts->reject; 4966cef5090cSJed Brown PetscFunctionReturn(0); 4967cef5090cSJed Brown } 4968cef5090cSJed Brown 4969cef5090cSJed Brown /*@ 4970cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 4971cef5090cSJed Brown 4972cef5090cSJed Brown Not Collective 4973cef5090cSJed Brown 4974cef5090cSJed Brown Input Parameter: 4975cef5090cSJed Brown . ts - TS context 4976cef5090cSJed Brown 4977cef5090cSJed Brown Output Parameter: 4978cef5090cSJed Brown . fails - number of failed nonlinear solves 4979cef5090cSJed Brown 4980cef5090cSJed Brown Notes: 4981cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4982cef5090cSJed Brown 4983cef5090cSJed Brown Level: intermediate 4984cef5090cSJed Brown 4985cef5090cSJed Brown .keywords: TS, get, number 4986cef5090cSJed Brown 49875ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 4988cef5090cSJed Brown @*/ 4989cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 4990cef5090cSJed Brown { 4991cef5090cSJed Brown PetscFunctionBegin; 4992cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4993cef5090cSJed Brown PetscValidIntPointer(fails,2); 4994cef5090cSJed Brown *fails = ts->num_snes_failures; 4995cef5090cSJed Brown PetscFunctionReturn(0); 4996cef5090cSJed Brown } 4997cef5090cSJed Brown 4998cef5090cSJed Brown /*@ 4999cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 5000cef5090cSJed Brown 5001cef5090cSJed Brown Not Collective 5002cef5090cSJed Brown 5003cef5090cSJed Brown Input Parameter: 5004cef5090cSJed Brown + ts - TS context 5005cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 5006cef5090cSJed Brown 5007cef5090cSJed Brown Notes: 5008cef5090cSJed Brown The counter is reset to zero for each step 5009cef5090cSJed Brown 5010cef5090cSJed Brown Options Database Key: 5011cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 5012cef5090cSJed Brown 5013cef5090cSJed Brown Level: intermediate 5014cef5090cSJed Brown 5015cef5090cSJed Brown .keywords: TS, set, maximum, number 5016cef5090cSJed Brown 50175ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5018cef5090cSJed Brown @*/ 5019cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 5020cef5090cSJed Brown { 5021cef5090cSJed Brown PetscFunctionBegin; 5022cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5023cef5090cSJed Brown ts->max_reject = rejects; 5024cef5090cSJed Brown PetscFunctionReturn(0); 5025cef5090cSJed Brown } 5026cef5090cSJed Brown 5027cef5090cSJed Brown /*@ 5028cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 5029cef5090cSJed Brown 5030cef5090cSJed Brown Not Collective 5031cef5090cSJed Brown 5032cef5090cSJed Brown Input Parameter: 5033cef5090cSJed Brown + ts - TS context 5034cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 5035cef5090cSJed Brown 5036cef5090cSJed Brown Notes: 5037cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 5038cef5090cSJed Brown 5039cef5090cSJed Brown Options Database Key: 5040cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 5041cef5090cSJed Brown 5042cef5090cSJed Brown Level: intermediate 5043cef5090cSJed Brown 5044cef5090cSJed Brown .keywords: TS, set, maximum, number 5045cef5090cSJed Brown 50465ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 5047cef5090cSJed Brown @*/ 5048cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 5049cef5090cSJed Brown { 5050cef5090cSJed Brown PetscFunctionBegin; 5051cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5052cef5090cSJed Brown ts->max_snes_failures = fails; 5053cef5090cSJed Brown PetscFunctionReturn(0); 5054cef5090cSJed Brown } 5055cef5090cSJed Brown 5056cef5090cSJed Brown /*@ 5057cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 5058cef5090cSJed Brown 5059cef5090cSJed Brown Not Collective 5060cef5090cSJed Brown 5061cef5090cSJed Brown Input Parameter: 5062cef5090cSJed Brown + ts - TS context 5063cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 5064cef5090cSJed Brown 5065cef5090cSJed Brown Options Database Key: 5066cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 5067cef5090cSJed Brown 5068cef5090cSJed Brown Level: intermediate 5069cef5090cSJed Brown 5070cef5090cSJed Brown .keywords: TS, set, error 5071cef5090cSJed Brown 50725ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5073cef5090cSJed Brown @*/ 5074cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 5075cef5090cSJed Brown { 5076cef5090cSJed Brown PetscFunctionBegin; 5077cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5078cef5090cSJed Brown ts->errorifstepfailed = err; 5079cef5090cSJed Brown PetscFunctionReturn(0); 5080cef5090cSJed Brown } 5081cef5090cSJed Brown 5082fb1732b5SBarry Smith /*@C 5083fde5950dSBarry 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 5084fb1732b5SBarry Smith 5085fb1732b5SBarry Smith Collective on TS 5086fb1732b5SBarry Smith 5087fb1732b5SBarry Smith Input Parameters: 5088fb1732b5SBarry Smith + ts - the TS context 5089fb1732b5SBarry Smith . step - current time-step 5090fb1732b5SBarry Smith . ptime - current time 50910910c330SBarry Smith . u - current state 5092721cd6eeSBarry Smith - vf - viewer and its format 5093fb1732b5SBarry Smith 5094fb1732b5SBarry Smith Level: intermediate 5095fb1732b5SBarry Smith 5096fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 5097fb1732b5SBarry Smith 5098fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5099fb1732b5SBarry Smith @*/ 5100721cd6eeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf) 5101fb1732b5SBarry Smith { 5102fb1732b5SBarry Smith PetscErrorCode ierr; 5103fb1732b5SBarry Smith 5104fb1732b5SBarry Smith PetscFunctionBegin; 5105721cd6eeSBarry Smith ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr); 5106721cd6eeSBarry Smith ierr = VecView(u,vf->viewer);CHKERRQ(ierr); 5107721cd6eeSBarry Smith ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr); 5108ed81e22dSJed Brown PetscFunctionReturn(0); 5109ed81e22dSJed Brown } 5110ed81e22dSJed Brown 5111ed81e22dSJed Brown /*@C 5112ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5113ed81e22dSJed Brown 5114ed81e22dSJed Brown Collective on TS 5115ed81e22dSJed Brown 5116ed81e22dSJed Brown Input Parameters: 5117ed81e22dSJed Brown + ts - the TS context 5118ed81e22dSJed Brown . step - current time-step 5119ed81e22dSJed Brown . ptime - current time 51200910c330SBarry Smith . u - current state 5121ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5122ed81e22dSJed Brown 5123ed81e22dSJed Brown Level: intermediate 5124ed81e22dSJed Brown 5125ed81e22dSJed Brown Notes: 5126ed81e22dSJed 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. 5127ed81e22dSJed Brown These are named according to the file name template. 5128ed81e22dSJed Brown 5129ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5130ed81e22dSJed Brown 5131ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5132ed81e22dSJed Brown 5133ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5134ed81e22dSJed Brown @*/ 51350910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5136ed81e22dSJed Brown { 5137ed81e22dSJed Brown PetscErrorCode ierr; 5138ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 5139ed81e22dSJed Brown PetscViewer viewer; 5140ed81e22dSJed Brown 5141ed81e22dSJed Brown PetscFunctionBegin; 514263e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 51438caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5144ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 51450910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 5146ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5147ed81e22dSJed Brown PetscFunctionReturn(0); 5148ed81e22dSJed Brown } 5149ed81e22dSJed Brown 5150ed81e22dSJed Brown /*@C 5151ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5152ed81e22dSJed Brown 5153ed81e22dSJed Brown Collective on TS 5154ed81e22dSJed Brown 5155ed81e22dSJed Brown Input Parameters: 5156ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5157ed81e22dSJed Brown 5158ed81e22dSJed Brown Level: intermediate 5159ed81e22dSJed Brown 5160ed81e22dSJed Brown Note: 5161ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5162ed81e22dSJed Brown 5163ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5164ed81e22dSJed Brown 5165ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5166ed81e22dSJed Brown @*/ 5167ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5168ed81e22dSJed Brown { 5169ed81e22dSJed Brown PetscErrorCode ierr; 5170ed81e22dSJed Brown 5171ed81e22dSJed Brown PetscFunctionBegin; 5172ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5173fb1732b5SBarry Smith PetscFunctionReturn(0); 5174fb1732b5SBarry Smith } 5175fb1732b5SBarry Smith 517684df9cb4SJed Brown /*@ 5177552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 517884df9cb4SJed Brown 5179ed81e22dSJed Brown Collective on TS if controller has not been created yet 518084df9cb4SJed Brown 518184df9cb4SJed Brown Input Arguments: 5182ed81e22dSJed Brown . ts - time stepping context 518384df9cb4SJed Brown 518484df9cb4SJed Brown Output Arguments: 5185ed81e22dSJed Brown . adapt - adaptive controller 518684df9cb4SJed Brown 518784df9cb4SJed Brown Level: intermediate 518884df9cb4SJed Brown 5189ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 519084df9cb4SJed Brown @*/ 5191552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 519284df9cb4SJed Brown { 519384df9cb4SJed Brown PetscErrorCode ierr; 519484df9cb4SJed Brown 519584df9cb4SJed Brown PetscFunctionBegin; 519684df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5197bec58848SLisandro Dalcin PetscValidPointer(adapt,2); 519884df9cb4SJed Brown if (!ts->adapt) { 5199ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 52003bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 52011c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 520284df9cb4SJed Brown } 5203bec58848SLisandro Dalcin *adapt = ts->adapt; 520484df9cb4SJed Brown PetscFunctionReturn(0); 520584df9cb4SJed Brown } 5206d6ebe24aSShri Abhyankar 52071c3436cfSJed Brown /*@ 52081c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 52091c3436cfSJed Brown 52101c3436cfSJed Brown Logically Collective 52111c3436cfSJed Brown 52121c3436cfSJed Brown Input Arguments: 52131c3436cfSJed Brown + ts - time integration context 52141c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 52150298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 52161c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 52170298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 52181c3436cfSJed Brown 5219a3cdaa26SBarry Smith Options Database keys: 5220a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5221a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5222a3cdaa26SBarry Smith 52233ff766beSShri Abhyankar Notes: 52243ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 52253ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 52263ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 52273ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 52283ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 52293ff766beSShri Abhyankar differential variables. 52303ff766beSShri Abhyankar 52311c3436cfSJed Brown Level: beginner 52321c3436cfSJed Brown 5233c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 52341c3436cfSJed Brown @*/ 52351c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 52361c3436cfSJed Brown { 52371c3436cfSJed Brown PetscErrorCode ierr; 52381c3436cfSJed Brown 52391c3436cfSJed Brown PetscFunctionBegin; 5240c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 52411c3436cfSJed Brown if (vatol) { 52421c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 52431c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 52441c3436cfSJed Brown ts->vatol = vatol; 52451c3436cfSJed Brown } 5246c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 52471c3436cfSJed Brown if (vrtol) { 52481c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 52491c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 52501c3436cfSJed Brown ts->vrtol = vrtol; 52511c3436cfSJed Brown } 52521c3436cfSJed Brown PetscFunctionReturn(0); 52531c3436cfSJed Brown } 52541c3436cfSJed Brown 5255c5033834SJed Brown /*@ 5256c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5257c5033834SJed Brown 5258c5033834SJed Brown Logically Collective 5259c5033834SJed Brown 5260c5033834SJed Brown Input Arguments: 5261c5033834SJed Brown . ts - time integration context 5262c5033834SJed Brown 5263c5033834SJed Brown Output Arguments: 52640298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 52650298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 52660298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 52670298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 5268c5033834SJed Brown 5269c5033834SJed Brown Level: beginner 5270c5033834SJed Brown 5271c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5272c5033834SJed Brown @*/ 5273c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5274c5033834SJed Brown { 5275c5033834SJed Brown PetscFunctionBegin; 5276c5033834SJed Brown if (atol) *atol = ts->atol; 5277c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5278c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5279c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 5280c5033834SJed Brown PetscFunctionReturn(0); 5281c5033834SJed Brown } 5282c5033834SJed Brown 52839c6b16b5SShri Abhyankar /*@ 5284a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 52859c6b16b5SShri Abhyankar 52869c6b16b5SShri Abhyankar Collective on TS 52879c6b16b5SShri Abhyankar 52889c6b16b5SShri Abhyankar Input Arguments: 52899c6b16b5SShri Abhyankar + ts - time stepping context 5290a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5291a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 52929c6b16b5SShri Abhyankar 52939c6b16b5SShri Abhyankar Output Arguments: 52947453f775SEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 52957453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 52967453f775SEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 52979c6b16b5SShri Abhyankar 52989c6b16b5SShri Abhyankar Level: developer 52999c6b16b5SShri Abhyankar 5300deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 53019c6b16b5SShri Abhyankar @*/ 53027453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 53039c6b16b5SShri Abhyankar { 53049c6b16b5SShri Abhyankar PetscErrorCode ierr; 53059c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 53067453f775SEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 53077453f775SEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 53089c6b16b5SShri Abhyankar const PetscScalar *u,*y; 53097453f775SEmil Constantinescu PetscReal sum,suma,sumr,gsum,gsuma,gsumr,diff; 53107453f775SEmil Constantinescu PetscReal tol,tola,tolr; 53117453f775SEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 53129c6b16b5SShri Abhyankar 53139c6b16b5SShri Abhyankar PetscFunctionBegin; 53149c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5315a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5316a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5317a4868fbcSLisandro Dalcin PetscValidType(U,2); 5318a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5319a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5320a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 53218a175baeSEmil Constantinescu PetscValidPointer(norma,5); 53228a175baeSEmil Constantinescu PetscValidPointer(normr,6); 5323a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 53249c6b16b5SShri Abhyankar 53259c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 53269c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 53279c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 53289c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 53299c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 53307453f775SEmil Constantinescu sum = 0.; n_loc = 0; 53317453f775SEmil Constantinescu suma = 0.; na_loc = 0; 53327453f775SEmil Constantinescu sumr = 0.; nr_loc = 0; 53339c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 53349c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 53359c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53369c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 53379c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 53387453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 53397453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 53407453f775SEmil Constantinescu if(tola>0.){ 53417453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 53427453f775SEmil Constantinescu na_loc++; 53437453f775SEmil Constantinescu } 53447453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53457453f775SEmil Constantinescu if(tolr>0.){ 53467453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 53477453f775SEmil Constantinescu nr_loc++; 53487453f775SEmil Constantinescu } 53497453f775SEmil Constantinescu tol=tola+tolr; 53507453f775SEmil Constantinescu if(tol>0.){ 53517453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 53527453f775SEmil Constantinescu n_loc++; 53537453f775SEmil Constantinescu } 53549c6b16b5SShri Abhyankar } 53559c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53569c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 53579c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 53589c6b16b5SShri Abhyankar const PetscScalar *atol; 53599c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53609c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 53617453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 53627453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 53637453f775SEmil Constantinescu if(tola>0.){ 53647453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 53657453f775SEmil Constantinescu na_loc++; 53667453f775SEmil Constantinescu } 53677453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53687453f775SEmil Constantinescu if(tolr>0.){ 53697453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 53707453f775SEmil Constantinescu nr_loc++; 53717453f775SEmil Constantinescu } 53727453f775SEmil Constantinescu tol=tola+tolr; 53737453f775SEmil Constantinescu if(tol>0.){ 53747453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 53757453f775SEmil Constantinescu n_loc++; 53767453f775SEmil Constantinescu } 53779c6b16b5SShri Abhyankar } 53789c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 53799c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 53809c6b16b5SShri Abhyankar const PetscScalar *rtol; 53819c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 53829c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 53837453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 53847453f775SEmil Constantinescu tola = ts->atol; 53857453f775SEmil Constantinescu if(tola>0.){ 53867453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 53877453f775SEmil Constantinescu na_loc++; 53887453f775SEmil Constantinescu } 53897453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53907453f775SEmil Constantinescu if(tolr>0.){ 53917453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 53927453f775SEmil Constantinescu nr_loc++; 53937453f775SEmil Constantinescu } 53947453f775SEmil Constantinescu tol=tola+tolr; 53957453f775SEmil Constantinescu if(tol>0.){ 53967453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 53977453f775SEmil Constantinescu n_loc++; 53987453f775SEmil Constantinescu } 53999c6b16b5SShri Abhyankar } 54009c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 54019c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 54029c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 54037453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 54047453f775SEmil Constantinescu tola = ts->atol; 54057453f775SEmil Constantinescu if(tola>0.){ 54067453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 54077453f775SEmil Constantinescu na_loc++; 54087453f775SEmil Constantinescu } 54097453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54107453f775SEmil Constantinescu if(tolr>0.){ 54117453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 54127453f775SEmil Constantinescu nr_loc++; 54137453f775SEmil Constantinescu } 54147453f775SEmil Constantinescu tol=tola+tolr; 54157453f775SEmil Constantinescu if(tol>0.){ 54167453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 54177453f775SEmil Constantinescu n_loc++; 54187453f775SEmil Constantinescu } 54199c6b16b5SShri Abhyankar } 54209c6b16b5SShri Abhyankar } 54219c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 54229c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 54239c6b16b5SShri Abhyankar 54247453f775SEmil Constantinescu err_loc[0] = sum; 54257453f775SEmil Constantinescu err_loc[1] = suma; 54267453f775SEmil Constantinescu err_loc[2] = sumr; 54277453f775SEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 54287453f775SEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 54297453f775SEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 54307453f775SEmil Constantinescu 5431a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 54327453f775SEmil Constantinescu 54337453f775SEmil Constantinescu gsum = err_glb[0]; 54347453f775SEmil Constantinescu gsuma = err_glb[1]; 54357453f775SEmil Constantinescu gsumr = err_glb[2]; 54367453f775SEmil Constantinescu n_glb = err_glb[3]; 54377453f775SEmil Constantinescu na_glb = err_glb[4]; 54387453f775SEmil Constantinescu nr_glb = err_glb[5]; 54397453f775SEmil Constantinescu 5440b1316ef9SEmil Constantinescu *norm = 0.; 5441b1316ef9SEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 5442b1316ef9SEmil Constantinescu *norma = 0.; 5443b1316ef9SEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 5444b1316ef9SEmil Constantinescu *normr = 0.; 5445b1316ef9SEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 54469c6b16b5SShri Abhyankar 54479c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 54487453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 54497453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 54509c6b16b5SShri Abhyankar PetscFunctionReturn(0); 54519c6b16b5SShri Abhyankar } 54529c6b16b5SShri Abhyankar 54539c6b16b5SShri Abhyankar /*@ 5454a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 54559c6b16b5SShri Abhyankar 54569c6b16b5SShri Abhyankar Collective on TS 54579c6b16b5SShri Abhyankar 54589c6b16b5SShri Abhyankar Input Arguments: 54599c6b16b5SShri Abhyankar + ts - time stepping context 5460a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5461a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 54629c6b16b5SShri Abhyankar 54639c6b16b5SShri Abhyankar Output Arguments: 54647453f775SEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 54657453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 54667453f775SEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 54679c6b16b5SShri Abhyankar 54689c6b16b5SShri Abhyankar Level: developer 54699c6b16b5SShri Abhyankar 5470deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 54719c6b16b5SShri Abhyankar @*/ 54727453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 54739c6b16b5SShri Abhyankar { 54749c6b16b5SShri Abhyankar PetscErrorCode ierr; 54757453f775SEmil Constantinescu PetscInt i,n,N,rstart; 54769c6b16b5SShri Abhyankar const PetscScalar *u,*y; 54777453f775SEmil Constantinescu PetscReal max,gmax,maxa,gmaxa,maxr,gmaxr; 54787453f775SEmil Constantinescu PetscReal tol,tola,tolr,diff; 54797453f775SEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 54809c6b16b5SShri Abhyankar 54819c6b16b5SShri Abhyankar PetscFunctionBegin; 54829c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5483a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5484a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5485a4868fbcSLisandro Dalcin PetscValidType(U,2); 5486a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5487a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5488a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 54898a175baeSEmil Constantinescu PetscValidPointer(norma,5); 54908a175baeSEmil Constantinescu PetscValidPointer(normr,6); 5491a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 54929c6b16b5SShri Abhyankar 54939c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 54949c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 54959c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 54969c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 54979c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 54987453f775SEmil Constantinescu 54997453f775SEmil Constantinescu max=0.; 55007453f775SEmil Constantinescu maxa=0.; 55017453f775SEmil Constantinescu maxr=0.; 55027453f775SEmil Constantinescu 55037453f775SEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 55049c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 55059c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 55069c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 55077453f775SEmil Constantinescu 55087453f775SEmil Constantinescu for (i=0; i<n; i++) { 55097453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 55107453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 55117453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55127453f775SEmil Constantinescu tol = tola+tolr; 55137453f775SEmil Constantinescu if(tola>0.){ 55147453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 55157453f775SEmil Constantinescu } 55167453f775SEmil Constantinescu if(tolr>0.){ 55177453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 55187453f775SEmil Constantinescu } 55197453f775SEmil Constantinescu if(tol>0.){ 55207453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 55217453f775SEmil Constantinescu } 55229c6b16b5SShri Abhyankar } 55239c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 55249c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 55259c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 55269c6b16b5SShri Abhyankar const PetscScalar *atol; 55279c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 55287453f775SEmil Constantinescu for (i=0; i<n; i++) { 55297453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 55307453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 55317453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55327453f775SEmil Constantinescu tol = tola+tolr; 55337453f775SEmil Constantinescu if(tola>0.){ 55347453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 55357453f775SEmil Constantinescu } 55367453f775SEmil Constantinescu if(tolr>0.){ 55377453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 55387453f775SEmil Constantinescu } 55397453f775SEmil Constantinescu if(tol>0.){ 55407453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 55417453f775SEmil Constantinescu } 55429c6b16b5SShri Abhyankar } 55439c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 55449c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 55459c6b16b5SShri Abhyankar const PetscScalar *rtol; 55469c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 55477453f775SEmil Constantinescu 55487453f775SEmil Constantinescu for (i=0; i<n; i++) { 55497453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 55507453f775SEmil Constantinescu tola = ts->atol; 55517453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55527453f775SEmil Constantinescu tol = tola+tolr; 55537453f775SEmil Constantinescu if(tola>0.){ 55547453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 55557453f775SEmil Constantinescu } 55567453f775SEmil Constantinescu if(tolr>0.){ 55577453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 55587453f775SEmil Constantinescu } 55597453f775SEmil Constantinescu if(tol>0.){ 55607453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 55617453f775SEmil Constantinescu } 55629c6b16b5SShri Abhyankar } 55639c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 55649c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 55657453f775SEmil Constantinescu 55667453f775SEmil Constantinescu for (i=0; i<n; i++) { 55677453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 55687453f775SEmil Constantinescu tola = ts->atol; 55697453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55707453f775SEmil Constantinescu tol = tola+tolr; 55717453f775SEmil Constantinescu if(tola>0.){ 55727453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 55737453f775SEmil Constantinescu } 55747453f775SEmil Constantinescu if(tolr>0.){ 55757453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 55767453f775SEmil Constantinescu } 55777453f775SEmil Constantinescu if(tol>0.){ 55787453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 55797453f775SEmil Constantinescu } 55809c6b16b5SShri Abhyankar } 55819c6b16b5SShri Abhyankar } 55829c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 55839c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 55847453f775SEmil Constantinescu err_loc[0] = max; 55857453f775SEmil Constantinescu err_loc[1] = maxa; 55867453f775SEmil Constantinescu err_loc[2] = maxr; 5587a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 55887453f775SEmil Constantinescu gmax = err_glb[0]; 55897453f775SEmil Constantinescu gmaxa = err_glb[1]; 55907453f775SEmil Constantinescu gmaxr = err_glb[2]; 55919c6b16b5SShri Abhyankar 55929c6b16b5SShri Abhyankar *norm = gmax; 55937453f775SEmil Constantinescu *norma = gmaxa; 55947453f775SEmil Constantinescu *normr = gmaxr; 55959c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 55967453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 55977453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 55989c6b16b5SShri Abhyankar PetscFunctionReturn(0); 55999c6b16b5SShri Abhyankar } 56009c6b16b5SShri Abhyankar 56011c3436cfSJed Brown /*@ 56028a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 56031c3436cfSJed Brown 56041c3436cfSJed Brown Collective on TS 56051c3436cfSJed Brown 56061c3436cfSJed Brown Input Arguments: 56071c3436cfSJed Brown + ts - time stepping context 5608a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5609a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5610a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 56117619abb3SShri 56121c3436cfSJed Brown Output Arguments: 56138a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 56148a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 56158a175baeSEmil Constantinescu . normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 5616a4868fbcSLisandro Dalcin 5617a4868fbcSLisandro Dalcin Options Database Keys: 5618a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5619a4868fbcSLisandro Dalcin 56201c3436cfSJed Brown Level: developer 56211c3436cfSJed Brown 56228a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm 56231c3436cfSJed Brown @*/ 56247453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 56251c3436cfSJed Brown { 56268beabaa1SBarry Smith PetscErrorCode ierr; 56271c3436cfSJed Brown 56281c3436cfSJed Brown PetscFunctionBegin; 5629a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 56307453f775SEmil Constantinescu ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 5631a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 56327453f775SEmil Constantinescu ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 5633a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 56341c3436cfSJed Brown PetscFunctionReturn(0); 56351c3436cfSJed Brown } 56361c3436cfSJed Brown 56378a175baeSEmil Constantinescu 56388a175baeSEmil Constantinescu /*@ 56398a175baeSEmil Constantinescu TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances 56408a175baeSEmil Constantinescu 56418a175baeSEmil Constantinescu Collective on TS 56428a175baeSEmil Constantinescu 56438a175baeSEmil Constantinescu Input Arguments: 56448a175baeSEmil Constantinescu + ts - time stepping context 56458a175baeSEmil Constantinescu . E - error vector 56468a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 56478a175baeSEmil Constantinescu - Y - state vector, previous time step 56488a175baeSEmil Constantinescu 56498a175baeSEmil Constantinescu Output Arguments: 56508a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 56518a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 56528a175baeSEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 56538a175baeSEmil Constantinescu 56548a175baeSEmil Constantinescu Level: developer 56558a175baeSEmil Constantinescu 56568a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity() 56578a175baeSEmil Constantinescu @*/ 56588a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 56598a175baeSEmil Constantinescu { 56608a175baeSEmil Constantinescu PetscErrorCode ierr; 56618a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 56628a175baeSEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 56638a175baeSEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 56648a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 56658a175baeSEmil Constantinescu PetscReal err,sum,suma,sumr,gsum,gsuma,gsumr; 56668a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 56678a175baeSEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 56688a175baeSEmil Constantinescu 56698a175baeSEmil Constantinescu PetscFunctionBegin; 56708a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 56718a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 56728a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 56738a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 56748a175baeSEmil Constantinescu PetscValidType(E,2); 56758a175baeSEmil Constantinescu PetscValidType(U,3); 56768a175baeSEmil Constantinescu PetscValidType(Y,4); 56778a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 56788a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 56798a175baeSEmil Constantinescu PetscValidPointer(norm,5); 56808a175baeSEmil Constantinescu PetscValidPointer(norma,6); 56818a175baeSEmil Constantinescu PetscValidPointer(normr,7); 56828a175baeSEmil Constantinescu 56838a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 56848a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 56858a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 56868a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 56878a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 56888a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 56898a175baeSEmil Constantinescu sum = 0.; n_loc = 0; 56908a175baeSEmil Constantinescu suma = 0.; na_loc = 0; 56918a175baeSEmil Constantinescu sumr = 0.; nr_loc = 0; 56928a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { 56938a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 56948a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 56958a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 56968a175baeSEmil Constantinescu for (i=0; i<n; i++) { 56978a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 56988a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 56998a175baeSEmil Constantinescu if(tola>0.){ 57008a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 57018a175baeSEmil Constantinescu na_loc++; 57028a175baeSEmil Constantinescu } 57038a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 57048a175baeSEmil Constantinescu if(tolr>0.){ 57058a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 57068a175baeSEmil Constantinescu nr_loc++; 57078a175baeSEmil Constantinescu } 57088a175baeSEmil Constantinescu tol=tola+tolr; 57098a175baeSEmil Constantinescu if(tol>0.){ 57108a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 57118a175baeSEmil Constantinescu n_loc++; 57128a175baeSEmil Constantinescu } 57138a175baeSEmil Constantinescu } 57148a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 57158a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 57168a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 57178a175baeSEmil Constantinescu const PetscScalar *atol; 57188a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 57198a175baeSEmil Constantinescu for (i=0; i<n; i++) { 57208a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 57218a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 57228a175baeSEmil Constantinescu if(tola>0.){ 57238a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 57248a175baeSEmil Constantinescu na_loc++; 57258a175baeSEmil Constantinescu } 57268a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 57278a175baeSEmil Constantinescu if(tolr>0.){ 57288a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 57298a175baeSEmil Constantinescu nr_loc++; 57308a175baeSEmil Constantinescu } 57318a175baeSEmil Constantinescu tol=tola+tolr; 57328a175baeSEmil Constantinescu if(tol>0.){ 57338a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 57348a175baeSEmil Constantinescu n_loc++; 57358a175baeSEmil Constantinescu } 57368a175baeSEmil Constantinescu } 57378a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 57388a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 57398a175baeSEmil Constantinescu const PetscScalar *rtol; 57408a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 57418a175baeSEmil Constantinescu for (i=0; i<n; i++) { 57428a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 57438a175baeSEmil Constantinescu tola = ts->atol; 57448a175baeSEmil Constantinescu if(tola>0.){ 57458a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 57468a175baeSEmil Constantinescu na_loc++; 57478a175baeSEmil Constantinescu } 57488a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 57498a175baeSEmil Constantinescu if(tolr>0.){ 57508a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 57518a175baeSEmil Constantinescu nr_loc++; 57528a175baeSEmil Constantinescu } 57538a175baeSEmil Constantinescu tol=tola+tolr; 57548a175baeSEmil Constantinescu if(tol>0.){ 57558a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 57568a175baeSEmil Constantinescu n_loc++; 57578a175baeSEmil Constantinescu } 57588a175baeSEmil Constantinescu } 57598a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 57608a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 57618a175baeSEmil Constantinescu for (i=0; i<n; i++) { 57628a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 57638a175baeSEmil Constantinescu tola = ts->atol; 57648a175baeSEmil Constantinescu if(tola>0.){ 57658a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 57668a175baeSEmil Constantinescu na_loc++; 57678a175baeSEmil Constantinescu } 57688a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 57698a175baeSEmil Constantinescu if(tolr>0.){ 57708a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 57718a175baeSEmil Constantinescu nr_loc++; 57728a175baeSEmil Constantinescu } 57738a175baeSEmil Constantinescu tol=tola+tolr; 57748a175baeSEmil Constantinescu if(tol>0.){ 57758a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 57768a175baeSEmil Constantinescu n_loc++; 57778a175baeSEmil Constantinescu } 57788a175baeSEmil Constantinescu } 57798a175baeSEmil Constantinescu } 57808a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 57818a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 57828a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 57838a175baeSEmil Constantinescu 57848a175baeSEmil Constantinescu err_loc[0] = sum; 57858a175baeSEmil Constantinescu err_loc[1] = suma; 57868a175baeSEmil Constantinescu err_loc[2] = sumr; 57878a175baeSEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 57888a175baeSEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 57898a175baeSEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 57908a175baeSEmil Constantinescu 5791a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 57928a175baeSEmil Constantinescu 57938a175baeSEmil Constantinescu gsum = err_glb[0]; 57948a175baeSEmil Constantinescu gsuma = err_glb[1]; 57958a175baeSEmil Constantinescu gsumr = err_glb[2]; 57968a175baeSEmil Constantinescu n_glb = err_glb[3]; 57978a175baeSEmil Constantinescu na_glb = err_glb[4]; 57988a175baeSEmil Constantinescu nr_glb = err_glb[5]; 57998a175baeSEmil Constantinescu 58008a175baeSEmil Constantinescu *norm = 0.; 58018a175baeSEmil Constantinescu if(n_glb>0. ){*norm = PetscSqrtReal(gsum / n_glb );} 58028a175baeSEmil Constantinescu *norma = 0.; 58038a175baeSEmil Constantinescu if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);} 58048a175baeSEmil Constantinescu *normr = 0.; 58058a175baeSEmil Constantinescu if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);} 58068a175baeSEmil Constantinescu 58078a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 58088a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 58098a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 58108a175baeSEmil Constantinescu PetscFunctionReturn(0); 58118a175baeSEmil Constantinescu } 58128a175baeSEmil Constantinescu 58138a175baeSEmil Constantinescu /*@ 58148a175baeSEmil Constantinescu TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances 58158a175baeSEmil Constantinescu Collective on TS 58168a175baeSEmil Constantinescu 58178a175baeSEmil Constantinescu Input Arguments: 58188a175baeSEmil Constantinescu + ts - time stepping context 58198a175baeSEmil Constantinescu . E - error vector 58208a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 58218a175baeSEmil Constantinescu - Y - state vector, previous time step 58228a175baeSEmil Constantinescu 58238a175baeSEmil Constantinescu Output Arguments: 58248a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 means that the error matches the tolerances 58258a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 58268a175baeSEmil Constantinescu . normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 58278a175baeSEmil Constantinescu 58288a175baeSEmil Constantinescu Level: developer 58298a175baeSEmil Constantinescu 58308a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2() 58318a175baeSEmil Constantinescu @*/ 58328a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 58338a175baeSEmil Constantinescu { 58348a175baeSEmil Constantinescu PetscErrorCode ierr; 58358a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 58368a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 58378a175baeSEmil Constantinescu PetscReal err,max,gmax,maxa,gmaxa,maxr,gmaxr; 58388a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 58398a175baeSEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 58408a175baeSEmil Constantinescu 58418a175baeSEmil Constantinescu PetscFunctionBegin; 58428a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 58438a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 58448a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 58458a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 58468a175baeSEmil Constantinescu PetscValidType(E,2); 58478a175baeSEmil Constantinescu PetscValidType(U,3); 58488a175baeSEmil Constantinescu PetscValidType(Y,4); 58498a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 58508a175baeSEmil Constantinescu PetscCheckSameComm(U,2,Y,3); 58518a175baeSEmil Constantinescu PetscValidPointer(norm,5); 58528a175baeSEmil Constantinescu PetscValidPointer(norma,6); 58538a175baeSEmil Constantinescu PetscValidPointer(normr,7); 58548a175baeSEmil Constantinescu 58558a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 58568a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 58578a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 58588a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 58598a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 58608a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 58618a175baeSEmil Constantinescu 58628a175baeSEmil Constantinescu max=0.; 58638a175baeSEmil Constantinescu maxa=0.; 58648a175baeSEmil Constantinescu maxr=0.; 58658a175baeSEmil Constantinescu 58668a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 58678a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 58688a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58698a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58708a175baeSEmil Constantinescu 58718a175baeSEmil Constantinescu for (i=0; i<n; i++) { 58728a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 58738a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 58748a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58758a175baeSEmil Constantinescu tol = tola+tolr; 58768a175baeSEmil Constantinescu if(tola>0.){ 58778a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 58788a175baeSEmil Constantinescu } 58798a175baeSEmil Constantinescu if(tolr>0.){ 58808a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 58818a175baeSEmil Constantinescu } 58828a175baeSEmil Constantinescu if(tol>0.){ 58838a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 58848a175baeSEmil Constantinescu } 58858a175baeSEmil Constantinescu } 58868a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58878a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 58888a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 58898a175baeSEmil Constantinescu const PetscScalar *atol; 58908a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 58918a175baeSEmil Constantinescu for (i=0; i<n; i++) { 58928a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 58938a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 58948a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 58958a175baeSEmil Constantinescu tol = tola+tolr; 58968a175baeSEmil Constantinescu if(tola>0.){ 58978a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 58988a175baeSEmil Constantinescu } 58998a175baeSEmil Constantinescu if(tolr>0.){ 59008a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 59018a175baeSEmil Constantinescu } 59028a175baeSEmil Constantinescu if(tol>0.){ 59038a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 59048a175baeSEmil Constantinescu } 59058a175baeSEmil Constantinescu } 59068a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 59078a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 59088a175baeSEmil Constantinescu const PetscScalar *rtol; 59098a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59108a175baeSEmil Constantinescu 59118a175baeSEmil Constantinescu for (i=0; i<n; i++) { 59128a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 59138a175baeSEmil Constantinescu tola = ts->atol; 59148a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59158a175baeSEmil Constantinescu tol = tola+tolr; 59168a175baeSEmil Constantinescu if(tola>0.){ 59178a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 59188a175baeSEmil Constantinescu } 59198a175baeSEmil Constantinescu if(tolr>0.){ 59208a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 59218a175baeSEmil Constantinescu } 59228a175baeSEmil Constantinescu if(tol>0.){ 59238a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 59248a175baeSEmil Constantinescu } 59258a175baeSEmil Constantinescu } 59268a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 59278a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 59288a175baeSEmil Constantinescu 59298a175baeSEmil Constantinescu for (i=0; i<n; i++) { 59308a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 59318a175baeSEmil Constantinescu tola = ts->atol; 59328a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 59338a175baeSEmil Constantinescu tol = tola+tolr; 59348a175baeSEmil Constantinescu if(tola>0.){ 59358a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 59368a175baeSEmil Constantinescu } 59378a175baeSEmil Constantinescu if(tolr>0.){ 59388a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 59398a175baeSEmil Constantinescu } 59408a175baeSEmil Constantinescu if(tol>0.){ 59418a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 59428a175baeSEmil Constantinescu } 59438a175baeSEmil Constantinescu } 59448a175baeSEmil Constantinescu } 59458a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 59468a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 59478a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 59488a175baeSEmil Constantinescu err_loc[0] = max; 59498a175baeSEmil Constantinescu err_loc[1] = maxa; 59508a175baeSEmil Constantinescu err_loc[2] = maxr; 5951a88a9d1dSSatish Balay ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 59528a175baeSEmil Constantinescu gmax = err_glb[0]; 59538a175baeSEmil Constantinescu gmaxa = err_glb[1]; 59548a175baeSEmil Constantinescu gmaxr = err_glb[2]; 59558a175baeSEmil Constantinescu 59568a175baeSEmil Constantinescu *norm = gmax; 59578a175baeSEmil Constantinescu *norma = gmaxa; 59588a175baeSEmil Constantinescu *normr = gmaxr; 59598a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 59608a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 59618a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 59628a175baeSEmil Constantinescu PetscFunctionReturn(0); 59638a175baeSEmil Constantinescu } 59648a175baeSEmil Constantinescu 59658a175baeSEmil Constantinescu /*@ 59668a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 59678a175baeSEmil Constantinescu 59688a175baeSEmil Constantinescu Collective on TS 59698a175baeSEmil Constantinescu 59708a175baeSEmil Constantinescu Input Arguments: 59718a175baeSEmil Constantinescu + ts - time stepping context 59728a175baeSEmil Constantinescu . E - error vector 59738a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 59748a175baeSEmil Constantinescu . Y - state vector, previous time step 59758a175baeSEmil Constantinescu - wnormtype - norm type, either NORM_2 or NORM_INFINITY 59768a175baeSEmil Constantinescu 59778a175baeSEmil Constantinescu Output Arguments: 59788a175baeSEmil Constantinescu . norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 59798a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 59808a175baeSEmil Constantinescu . normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 59818a175baeSEmil Constantinescu 59828a175baeSEmil Constantinescu Options Database Keys: 59838a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 59848a175baeSEmil Constantinescu 59858a175baeSEmil Constantinescu Level: developer 59868a175baeSEmil Constantinescu 59878a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 59888a175baeSEmil Constantinescu @*/ 59898a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 59908a175baeSEmil Constantinescu { 59918a175baeSEmil Constantinescu PetscErrorCode ierr; 59928a175baeSEmil Constantinescu 59938a175baeSEmil Constantinescu PetscFunctionBegin; 59948a175baeSEmil Constantinescu if (wnormtype == NORM_2) { 59958a175baeSEmil Constantinescu ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 59968a175baeSEmil Constantinescu } else if(wnormtype == NORM_INFINITY) { 59978a175baeSEmil Constantinescu ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 59988a175baeSEmil Constantinescu } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 59998a175baeSEmil Constantinescu PetscFunctionReturn(0); 60008a175baeSEmil Constantinescu } 60018a175baeSEmil Constantinescu 60028a175baeSEmil Constantinescu 60038d59e960SJed Brown /*@ 60048d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 60058d59e960SJed Brown 60068d59e960SJed Brown Logically Collective on TS 60078d59e960SJed Brown 60088d59e960SJed Brown Input Arguments: 60098d59e960SJed Brown + ts - time stepping context 60108d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 60118d59e960SJed Brown 60128d59e960SJed Brown Note: 60138d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 60148d59e960SJed Brown 60158d59e960SJed Brown Level: intermediate 60168d59e960SJed Brown 60178d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 60188d59e960SJed Brown @*/ 60198d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 60208d59e960SJed Brown { 60218d59e960SJed Brown PetscFunctionBegin; 60228d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 60238d59e960SJed Brown ts->cfltime_local = cfltime; 60248d59e960SJed Brown ts->cfltime = -1.; 60258d59e960SJed Brown PetscFunctionReturn(0); 60268d59e960SJed Brown } 60278d59e960SJed Brown 60288d59e960SJed Brown /*@ 60298d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 60308d59e960SJed Brown 60318d59e960SJed Brown Collective on TS 60328d59e960SJed Brown 60338d59e960SJed Brown Input Arguments: 60348d59e960SJed Brown . ts - time stepping context 60358d59e960SJed Brown 60368d59e960SJed Brown Output Arguments: 60378d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 60388d59e960SJed Brown 60398d59e960SJed Brown Level: advanced 60408d59e960SJed Brown 60418d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 60428d59e960SJed Brown @*/ 60438d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 60448d59e960SJed Brown { 60458d59e960SJed Brown PetscErrorCode ierr; 60468d59e960SJed Brown 60478d59e960SJed Brown PetscFunctionBegin; 60488d59e960SJed Brown if (ts->cfltime < 0) { 6049b2566f29SBarry Smith ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 60508d59e960SJed Brown } 60518d59e960SJed Brown *cfltime = ts->cfltime; 60528d59e960SJed Brown PetscFunctionReturn(0); 60538d59e960SJed Brown } 60548d59e960SJed Brown 6055d6ebe24aSShri Abhyankar /*@ 6056d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 6057d6ebe24aSShri Abhyankar 6058d6ebe24aSShri Abhyankar Input Parameters: 6059d6ebe24aSShri Abhyankar . ts - the TS context. 6060d6ebe24aSShri Abhyankar . xl - lower bound. 6061d6ebe24aSShri Abhyankar . xu - upper bound. 6062d6ebe24aSShri Abhyankar 6063d6ebe24aSShri Abhyankar Notes: 6064d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 6065e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 6066d6ebe24aSShri Abhyankar 60672bd2b0e6SSatish Balay Level: advanced 60682bd2b0e6SSatish Balay 6069d6ebe24aSShri Abhyankar @*/ 6070d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 6071d6ebe24aSShri Abhyankar { 6072d6ebe24aSShri Abhyankar PetscErrorCode ierr; 6073d6ebe24aSShri Abhyankar SNES snes; 6074d6ebe24aSShri Abhyankar 6075d6ebe24aSShri Abhyankar PetscFunctionBegin; 6076d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 6077d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 6078d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 6079d6ebe24aSShri Abhyankar } 6080d6ebe24aSShri Abhyankar 6081325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 6082c6db04a5SJed Brown #include <mex.h> 6083325fc9f4SBarry Smith 6084325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 6085325fc9f4SBarry Smith 6086325fc9f4SBarry Smith /* 6087325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 6088325fc9f4SBarry Smith TSSetFunctionMatlab(). 6089325fc9f4SBarry Smith 6090325fc9f4SBarry Smith Collective on TS 6091325fc9f4SBarry Smith 6092325fc9f4SBarry Smith Input Parameters: 6093325fc9f4SBarry Smith + snes - the TS context 60940910c330SBarry Smith - u - input vector 6095325fc9f4SBarry Smith 6096325fc9f4SBarry Smith Output Parameter: 6097325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 6098325fc9f4SBarry Smith 6099325fc9f4SBarry Smith Notes: 6100325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 6101325fc9f4SBarry Smith implementations, so most users would not generally call this routine 6102325fc9f4SBarry Smith themselves. 6103325fc9f4SBarry Smith 6104325fc9f4SBarry Smith Level: developer 6105325fc9f4SBarry Smith 6106325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6107325fc9f4SBarry Smith 6108325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6109325fc9f4SBarry Smith */ 61100910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 6111325fc9f4SBarry Smith { 6112325fc9f4SBarry Smith PetscErrorCode ierr; 6113325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6114325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 6115325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 6116325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 6117325fc9f4SBarry Smith 6118325fc9f4SBarry Smith PetscFunctionBegin; 6119325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 61200910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 61210910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 6122325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 61230910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 6124325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 6125325fc9f4SBarry Smith 6126325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 61270910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 61280910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 61290910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 6130bbd56ea5SKarl Rupp 6131325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6132325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 6133325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6134325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6135325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 6136325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 6137325fc9f4SBarry Smith prhs[6] = sctx->ctx; 6138325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 6139325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6140325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6141325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6142325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6143325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6144325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6145325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6146325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6147325fc9f4SBarry Smith PetscFunctionReturn(0); 6148325fc9f4SBarry Smith } 6149325fc9f4SBarry Smith 6150325fc9f4SBarry Smith /* 6151325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 6152325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 6153e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 6154325fc9f4SBarry Smith 6155325fc9f4SBarry Smith Logically Collective on TS 6156325fc9f4SBarry Smith 6157325fc9f4SBarry Smith Input Parameters: 6158325fc9f4SBarry Smith + ts - the TS context 6159325fc9f4SBarry Smith - func - function evaluation routine 6160325fc9f4SBarry Smith 6161325fc9f4SBarry Smith Calling sequence of func: 61620910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 6163325fc9f4SBarry Smith 6164325fc9f4SBarry Smith Level: beginner 6165325fc9f4SBarry Smith 6166325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6167325fc9f4SBarry Smith 6168325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6169325fc9f4SBarry Smith */ 6170cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 6171325fc9f4SBarry Smith { 6172325fc9f4SBarry Smith PetscErrorCode ierr; 6173325fc9f4SBarry Smith TSMatlabContext *sctx; 6174325fc9f4SBarry Smith 6175325fc9f4SBarry Smith PetscFunctionBegin; 6176325fc9f4SBarry Smith /* currently sctx is memory bleed */ 617795dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6178325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6179325fc9f4SBarry Smith /* 6180325fc9f4SBarry Smith This should work, but it doesn't 6181325fc9f4SBarry Smith sctx->ctx = ctx; 6182325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6183325fc9f4SBarry Smith */ 6184325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6185bbd56ea5SKarl Rupp 61860298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 6187325fc9f4SBarry Smith PetscFunctionReturn(0); 6188325fc9f4SBarry Smith } 6189325fc9f4SBarry Smith 6190325fc9f4SBarry Smith /* 6191325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 6192325fc9f4SBarry Smith TSSetJacobianMatlab(). 6193325fc9f4SBarry Smith 6194325fc9f4SBarry Smith Collective on TS 6195325fc9f4SBarry Smith 6196325fc9f4SBarry Smith Input Parameters: 6197cdcf91faSSean Farley + ts - the TS context 61980910c330SBarry Smith . u - input vector 6199325fc9f4SBarry Smith . A, B - the matrices 6200325fc9f4SBarry Smith - ctx - user context 6201325fc9f4SBarry Smith 6202325fc9f4SBarry Smith Level: developer 6203325fc9f4SBarry Smith 6204325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 6205325fc9f4SBarry Smith 6206325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6207325fc9f4SBarry Smith @*/ 6208f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 6209325fc9f4SBarry Smith { 6210325fc9f4SBarry Smith PetscErrorCode ierr; 6211325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6212325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 6213325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 6214325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 6215325fc9f4SBarry Smith 6216325fc9f4SBarry Smith PetscFunctionBegin; 6217cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 62180910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 6219325fc9f4SBarry Smith 62200910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 6221325fc9f4SBarry Smith 6222cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 62230910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 62240910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 62250910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 62260910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 6227bbd56ea5SKarl Rupp 6228325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6229325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 6230325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 6231325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 6232325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 6233325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 6234325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 6235325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 6236325fc9f4SBarry Smith prhs[8] = sctx->ctx; 6237325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 6238325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6239325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 6240325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 6241325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 6242325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 6243325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 6244325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 6245325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 6246325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 6247325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 6248325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 6249325fc9f4SBarry Smith PetscFunctionReturn(0); 6250325fc9f4SBarry Smith } 6251325fc9f4SBarry Smith 6252325fc9f4SBarry Smith /* 6253325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 6254e3c5b3baSBarry Smith vector for use by the TS routines in solving ODEs from MATLAB. Here the function is a string containing the name of a MATLAB function 6255325fc9f4SBarry Smith 6256325fc9f4SBarry Smith Logically Collective on TS 6257325fc9f4SBarry Smith 6258325fc9f4SBarry Smith Input Parameters: 6259cdcf91faSSean Farley + ts - the TS context 6260325fc9f4SBarry Smith . A,B - Jacobian matrices 6261325fc9f4SBarry Smith . func - function evaluation routine 6262325fc9f4SBarry Smith - ctx - user context 6263325fc9f4SBarry Smith 6264325fc9f4SBarry Smith Calling sequence of func: 62650910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 6266325fc9f4SBarry Smith 6267325fc9f4SBarry Smith Level: developer 6268325fc9f4SBarry Smith 6269325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 6270325fc9f4SBarry Smith 6271325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6272325fc9f4SBarry Smith */ 6273cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 6274325fc9f4SBarry Smith { 6275325fc9f4SBarry Smith PetscErrorCode ierr; 6276325fc9f4SBarry Smith TSMatlabContext *sctx; 6277325fc9f4SBarry Smith 6278325fc9f4SBarry Smith PetscFunctionBegin; 6279325fc9f4SBarry Smith /* currently sctx is memory bleed */ 628095dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6281325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6282325fc9f4SBarry Smith /* 6283325fc9f4SBarry Smith This should work, but it doesn't 6284325fc9f4SBarry Smith sctx->ctx = ctx; 6285325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6286325fc9f4SBarry Smith */ 6287325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6288bbd56ea5SKarl Rupp 6289cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 6290325fc9f4SBarry Smith PetscFunctionReturn(0); 6291325fc9f4SBarry Smith } 6292325fc9f4SBarry Smith 6293b5b1a830SBarry Smith /* 6294b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 6295b5b1a830SBarry Smith 6296b5b1a830SBarry Smith Collective on TS 6297b5b1a830SBarry Smith 6298b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 6299b5b1a830SBarry Smith @*/ 63000910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 6301b5b1a830SBarry Smith { 6302b5b1a830SBarry Smith PetscErrorCode ierr; 6303b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 6304a530c242SBarry Smith int nlhs = 1,nrhs = 6; 6305b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 6306b5b1a830SBarry Smith long long int lx = 0,ls = 0; 6307b5b1a830SBarry Smith 6308b5b1a830SBarry Smith PetscFunctionBegin; 6309cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 63100910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 6311b5b1a830SBarry Smith 6312cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 63130910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 6314bbd56ea5SKarl Rupp 6315b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 6316b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 6317b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 6318b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 6319b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 6320b5b1a830SBarry Smith prhs[5] = sctx->ctx; 6321b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 6322b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 6323b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 6324b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 6325b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 6326b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 6327b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 6328b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 6329b5b1a830SBarry Smith PetscFunctionReturn(0); 6330b5b1a830SBarry Smith } 6331b5b1a830SBarry Smith 6332b5b1a830SBarry Smith /* 6333b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 6334b5b1a830SBarry Smith 6335b5b1a830SBarry Smith Level: developer 6336b5b1a830SBarry Smith 6337b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 6338b5b1a830SBarry Smith 6339b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 6340b5b1a830SBarry Smith */ 6341cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 6342b5b1a830SBarry Smith { 6343b5b1a830SBarry Smith PetscErrorCode ierr; 6344b5b1a830SBarry Smith TSMatlabContext *sctx; 6345b5b1a830SBarry Smith 6346b5b1a830SBarry Smith PetscFunctionBegin; 6347b5b1a830SBarry Smith /* currently sctx is memory bleed */ 634895dccacaSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 6349b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 6350b5b1a830SBarry Smith /* 6351b5b1a830SBarry Smith This should work, but it doesn't 6352b5b1a830SBarry Smith sctx->ctx = ctx; 6353b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 6354b5b1a830SBarry Smith */ 6355b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 6356bbd56ea5SKarl Rupp 63570298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 6358b5b1a830SBarry Smith PetscFunctionReturn(0); 6359b5b1a830SBarry Smith } 6360325fc9f4SBarry Smith #endif 6361b3603a34SBarry Smith 6362b3603a34SBarry Smith /*@C 63634f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 6364b3603a34SBarry Smith in a time based line graph 6365b3603a34SBarry Smith 6366b3603a34SBarry Smith Collective on TS 6367b3603a34SBarry Smith 6368b3603a34SBarry Smith Input Parameters: 6369b3603a34SBarry Smith + ts - the TS context 6370b3603a34SBarry Smith . step - current time-step 6371b3603a34SBarry Smith . ptime - current time 63727db568b7SBarry Smith . u - current solution 63737db568b7SBarry Smith - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 6374b3603a34SBarry Smith 6375b3d3934dSBarry Smith Options Database: 63769ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 6377b3d3934dSBarry Smith 6378b3603a34SBarry Smith Level: intermediate 6379b3603a34SBarry Smith 63806934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component solutions in a separate window 6381b3603a34SBarry Smith 6382b3603a34SBarry Smith .keywords: TS, vector, monitor, view 6383b3603a34SBarry Smith 63847db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 63857db568b7SBarry Smith TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 63867db568b7SBarry Smith TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 63877db568b7SBarry Smith TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 6388b3603a34SBarry Smith @*/ 63897db568b7SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6390b3603a34SBarry Smith { 6391b3603a34SBarry Smith PetscErrorCode ierr; 63927db568b7SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 6393b3603a34SBarry Smith const PetscScalar *yy; 639480666b62SBarry Smith Vec v; 6395b3603a34SBarry Smith 6396b3603a34SBarry Smith PetscFunctionBegin; 639763e21af5SBarry Smith if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 639858ff32f7SBarry Smith if (!step) { 6399a9f9c1f6SBarry Smith PetscDrawAxis axis; 64006934998bSLisandro Dalcin PetscInt dim; 6401a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6402a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 6403bab0a581SBarry Smith if (!ctx->names) { 6404bab0a581SBarry Smith PetscBool flg; 6405bab0a581SBarry Smith /* user provides names of variables to plot but no names has been set so assume names are integer values */ 6406bab0a581SBarry Smith ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr); 6407bab0a581SBarry Smith if (flg) { 6408bab0a581SBarry Smith PetscInt i,n; 6409bab0a581SBarry Smith char **names; 6410bab0a581SBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 6411bab0a581SBarry Smith ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr); 6412bab0a581SBarry Smith for (i=0; i<n; i++) { 6413bab0a581SBarry Smith ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr); 6414bab0a581SBarry Smith ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr); 6415bab0a581SBarry Smith } 6416bab0a581SBarry Smith names[n] = NULL; 6417bab0a581SBarry Smith ctx->names = names; 6418bab0a581SBarry Smith } 6419bab0a581SBarry Smith } 6420387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 6421387f4636SBarry Smith char **displaynames; 6422387f4636SBarry Smith PetscBool flg; 6423387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 642495dccacaSBarry Smith ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr); 6425387f4636SBarry Smith ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 6426c5929fdfSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 6427387f4636SBarry Smith if (flg) { 6428a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 6429387f4636SBarry Smith } 6430387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 6431387f4636SBarry Smith } 6432387f4636SBarry Smith if (ctx->displaynames) { 6433387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 6434387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 6435387f4636SBarry Smith } else if (ctx->names) { 64360910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 64370b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6438387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 6439b0bc92c6SBarry Smith } else { 6440b0bc92c6SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6441b0bc92c6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6442387f4636SBarry Smith } 64430b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 644458ff32f7SBarry Smith } 64456934998bSLisandro Dalcin 64466934998bSLisandro Dalcin if (!ctx->transform) v = u; 64476934998bSLisandro Dalcin else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);} 644880666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 64496934998bSLisandro Dalcin if (ctx->displaynames) { 64506934998bSLisandro Dalcin PetscInt i; 64516934998bSLisandro Dalcin for (i=0; i<ctx->ndisplayvariables; i++) 64526934998bSLisandro Dalcin ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]); 64536934998bSLisandro Dalcin ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 64546934998bSLisandro Dalcin } else { 6455e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6456e3efe391SJed Brown PetscInt i,n; 64576934998bSLisandro Dalcin PetscReal *yreal; 645880666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 6459785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6460e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 64610b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6462e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6463e3efe391SJed Brown #else 64640b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6465e3efe391SJed Brown #endif 646680666b62SBarry Smith } 64676934998bSLisandro Dalcin ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 64686934998bSLisandro Dalcin if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);} 64696934998bSLisandro Dalcin 6470b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 64710b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 64726934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 64733923b477SBarry Smith } 6474b3603a34SBarry Smith PetscFunctionReturn(0); 6475b3603a34SBarry Smith } 6476b3603a34SBarry Smith 6477b037adc7SBarry Smith /*@C 647831152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6479b037adc7SBarry Smith 6480b037adc7SBarry Smith Collective on TS 6481b037adc7SBarry Smith 6482b037adc7SBarry Smith Input Parameters: 6483b037adc7SBarry Smith + ts - the TS context 6484b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 6485b037adc7SBarry Smith 6486b037adc7SBarry Smith Level: intermediate 6487b037adc7SBarry Smith 64887db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 64897db568b7SBarry Smith 6490b037adc7SBarry Smith .keywords: TS, vector, monitor, view 6491b037adc7SBarry Smith 6492a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 6493b037adc7SBarry Smith @*/ 649431152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 6495b037adc7SBarry Smith { 6496b037adc7SBarry Smith PetscErrorCode ierr; 6497b037adc7SBarry Smith PetscInt i; 6498b037adc7SBarry Smith 6499b037adc7SBarry Smith PetscFunctionBegin; 6500b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6501b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 65025537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 6503b3d3934dSBarry Smith break; 6504b3d3934dSBarry Smith } 6505b3d3934dSBarry Smith } 6506b3d3934dSBarry Smith PetscFunctionReturn(0); 6507b3d3934dSBarry Smith } 6508b3d3934dSBarry Smith 6509e673d494SBarry Smith /*@C 6510e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6511e673d494SBarry Smith 6512e673d494SBarry Smith Collective on TS 6513e673d494SBarry Smith 6514e673d494SBarry Smith Input Parameters: 6515e673d494SBarry Smith + ts - the TS context 6516e673d494SBarry Smith - names - the names of the components, final string must be NULL 6517e673d494SBarry Smith 6518e673d494SBarry Smith Level: intermediate 6519e673d494SBarry Smith 6520e673d494SBarry Smith .keywords: TS, vector, monitor, view 6521e673d494SBarry Smith 6522a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 6523e673d494SBarry Smith @*/ 6524e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 6525e673d494SBarry Smith { 6526e673d494SBarry Smith PetscErrorCode ierr; 6527e673d494SBarry Smith 6528e673d494SBarry Smith PetscFunctionBegin; 6529e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 6530e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 6531e673d494SBarry Smith PetscFunctionReturn(0); 6532e673d494SBarry Smith } 6533e673d494SBarry Smith 6534b3d3934dSBarry Smith /*@C 6535b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 6536b3d3934dSBarry Smith 6537b3d3934dSBarry Smith Collective on TS 6538b3d3934dSBarry Smith 6539b3d3934dSBarry Smith Input Parameter: 6540b3d3934dSBarry Smith . ts - the TS context 6541b3d3934dSBarry Smith 6542b3d3934dSBarry Smith Output Parameter: 6543b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 6544b3d3934dSBarry Smith 6545b3d3934dSBarry Smith Level: intermediate 6546b3d3934dSBarry Smith 65477db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 65487db568b7SBarry Smith 6549b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6550b3d3934dSBarry Smith 6551b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6552b3d3934dSBarry Smith @*/ 6553b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 6554b3d3934dSBarry Smith { 6555b3d3934dSBarry Smith PetscInt i; 6556b3d3934dSBarry Smith 6557b3d3934dSBarry Smith PetscFunctionBegin; 6558b3d3934dSBarry Smith *names = NULL; 6559b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6560b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 65615537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 6562b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 6563b3d3934dSBarry Smith break; 6564387f4636SBarry Smith } 6565387f4636SBarry Smith } 6566387f4636SBarry Smith PetscFunctionReturn(0); 6567387f4636SBarry Smith } 6568387f4636SBarry Smith 6569a66092f1SBarry Smith /*@C 6570a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 6571a66092f1SBarry Smith 6572a66092f1SBarry Smith Collective on TS 6573a66092f1SBarry Smith 6574a66092f1SBarry Smith Input Parameters: 6575a66092f1SBarry Smith + ctx - the TSMonitorLG context 6576a66092f1SBarry Smith . displaynames - the names of the components, final string must be NULL 6577a66092f1SBarry Smith 6578a66092f1SBarry Smith Level: intermediate 6579a66092f1SBarry Smith 6580a66092f1SBarry Smith .keywords: TS, vector, monitor, view 6581a66092f1SBarry Smith 6582a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6583a66092f1SBarry Smith @*/ 6584a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 6585a66092f1SBarry Smith { 6586a66092f1SBarry Smith PetscInt j = 0,k; 6587a66092f1SBarry Smith PetscErrorCode ierr; 6588a66092f1SBarry Smith 6589a66092f1SBarry Smith PetscFunctionBegin; 6590a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 6591a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 6592a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 6593a66092f1SBarry Smith while (displaynames[j]) j++; 6594a66092f1SBarry Smith ctx->ndisplayvariables = j; 6595a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 6596a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 6597a66092f1SBarry Smith j = 0; 6598a66092f1SBarry Smith while (displaynames[j]) { 6599a66092f1SBarry Smith k = 0; 6600a66092f1SBarry Smith while (ctx->names[k]) { 6601a66092f1SBarry Smith PetscBool flg; 6602a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 6603a66092f1SBarry Smith if (flg) { 6604a66092f1SBarry Smith ctx->displayvariables[j] = k; 6605a66092f1SBarry Smith break; 6606a66092f1SBarry Smith } 6607a66092f1SBarry Smith k++; 6608a66092f1SBarry Smith } 6609a66092f1SBarry Smith j++; 6610a66092f1SBarry Smith } 6611a66092f1SBarry Smith PetscFunctionReturn(0); 6612a66092f1SBarry Smith } 6613a66092f1SBarry Smith 6614387f4636SBarry Smith /*@C 6615387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 6616387f4636SBarry Smith 6617387f4636SBarry Smith Collective on TS 6618387f4636SBarry Smith 6619387f4636SBarry Smith Input Parameters: 6620387f4636SBarry Smith + ts - the TS context 6621387f4636SBarry Smith . displaynames - the names of the components, final string must be NULL 6622387f4636SBarry Smith 66237db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 66247db568b7SBarry Smith 6625387f4636SBarry Smith Level: intermediate 6626387f4636SBarry Smith 6627387f4636SBarry Smith .keywords: TS, vector, monitor, view 6628387f4636SBarry Smith 6629387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6630387f4636SBarry Smith @*/ 6631387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 6632387f4636SBarry Smith { 6633a66092f1SBarry Smith PetscInt i; 6634387f4636SBarry Smith PetscErrorCode ierr; 6635387f4636SBarry Smith 6636387f4636SBarry Smith PetscFunctionBegin; 6637387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6638387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 66395537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 6640b3d3934dSBarry Smith break; 6641b037adc7SBarry Smith } 6642b037adc7SBarry Smith } 6643b037adc7SBarry Smith PetscFunctionReturn(0); 6644b037adc7SBarry Smith } 6645b037adc7SBarry Smith 664680666b62SBarry Smith /*@C 664780666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 664880666b62SBarry Smith 664980666b62SBarry Smith Collective on TS 665080666b62SBarry Smith 665180666b62SBarry Smith Input Parameters: 665280666b62SBarry Smith + ts - the TS context 665380666b62SBarry Smith . transform - the transform function 66547684fa3eSBarry Smith . destroy - function to destroy the optional context 665580666b62SBarry Smith - ctx - optional context used by transform function 665680666b62SBarry Smith 66577db568b7SBarry Smith Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 66587db568b7SBarry Smith 665980666b62SBarry Smith Level: intermediate 666080666b62SBarry Smith 666180666b62SBarry Smith .keywords: TS, vector, monitor, view 666280666b62SBarry Smith 6663a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 666480666b62SBarry Smith @*/ 66657684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 666680666b62SBarry Smith { 666780666b62SBarry Smith PetscInt i; 6668a66092f1SBarry Smith PetscErrorCode ierr; 666980666b62SBarry Smith 667080666b62SBarry Smith PetscFunctionBegin; 667180666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 667280666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 66735537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 667480666b62SBarry Smith } 667580666b62SBarry Smith } 667680666b62SBarry Smith PetscFunctionReturn(0); 667780666b62SBarry Smith } 667880666b62SBarry Smith 6679e673d494SBarry Smith /*@C 6680e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 6681e673d494SBarry Smith 6682e673d494SBarry Smith Collective on TSLGCtx 6683e673d494SBarry Smith 6684e673d494SBarry Smith Input Parameters: 6685e673d494SBarry Smith + ts - the TS context 6686e673d494SBarry Smith . transform - the transform function 66877684fa3eSBarry Smith . destroy - function to destroy the optional context 6688e673d494SBarry Smith - ctx - optional context used by transform function 6689e673d494SBarry Smith 6690e673d494SBarry Smith Level: intermediate 6691e673d494SBarry Smith 6692e673d494SBarry Smith .keywords: TS, vector, monitor, view 6693e673d494SBarry Smith 6694a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 6695e673d494SBarry Smith @*/ 66967684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 6697e673d494SBarry Smith { 6698e673d494SBarry Smith PetscFunctionBegin; 6699e673d494SBarry Smith ctx->transform = transform; 67007684fa3eSBarry Smith ctx->transformdestroy = destroy; 6701e673d494SBarry Smith ctx->transformctx = tctx; 6702e673d494SBarry Smith PetscFunctionReturn(0); 6703e673d494SBarry Smith } 6704e673d494SBarry Smith 6705ef20d060SBarry Smith /*@C 67068b668821SLisandro Dalcin TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error 6707ef20d060SBarry Smith in a time based line graph 6708ef20d060SBarry Smith 6709ef20d060SBarry Smith Collective on TS 6710ef20d060SBarry Smith 6711ef20d060SBarry Smith Input Parameters: 6712ef20d060SBarry Smith + ts - the TS context 6713ef20d060SBarry Smith . step - current time-step 6714ef20d060SBarry Smith . ptime - current time 67157db568b7SBarry Smith . u - current solution 67167db568b7SBarry Smith - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 6717ef20d060SBarry Smith 6718ef20d060SBarry Smith Level: intermediate 6719ef20d060SBarry Smith 67206934998bSLisandro Dalcin Notes: Each process in a parallel run displays its component errors in a separate window 6721abd5a294SJed Brown 6722abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 6723abd5a294SJed Brown 6724abd5a294SJed Brown Options Database Keys: 67254f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 6726ef20d060SBarry Smith 6727ef20d060SBarry Smith .keywords: TS, vector, monitor, view 6728ef20d060SBarry Smith 6729abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 6730ef20d060SBarry Smith @*/ 67310910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 6732ef20d060SBarry Smith { 6733ef20d060SBarry Smith PetscErrorCode ierr; 67340b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 6735ef20d060SBarry Smith const PetscScalar *yy; 6736ef20d060SBarry Smith Vec y; 6737ef20d060SBarry Smith 6738ef20d060SBarry Smith PetscFunctionBegin; 6739a9f9c1f6SBarry Smith if (!step) { 6740a9f9c1f6SBarry Smith PetscDrawAxis axis; 67416934998bSLisandro Dalcin PetscInt dim; 6742a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 67438b668821SLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr); 67440910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6745a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6746a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6747a9f9c1f6SBarry Smith } 67480910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 6749ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 67500910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 6751ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 6752e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6753e3efe391SJed Brown { 6754e3efe391SJed Brown PetscReal *yreal; 6755e3efe391SJed Brown PetscInt i,n; 6756e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 6757785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6758e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 67590b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6760e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6761e3efe391SJed Brown } 6762e3efe391SJed Brown #else 67630b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6764e3efe391SJed Brown #endif 6765ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 6766ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 6767b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 67680b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 67696934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 67703923b477SBarry Smith } 6771ef20d060SBarry Smith PetscFunctionReturn(0); 6772ef20d060SBarry Smith } 6773ef20d060SBarry Smith 67747cf37e64SBarry Smith /*@C 67757cf37e64SBarry Smith TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep 67767cf37e64SBarry Smith 67777cf37e64SBarry Smith Collective on TS 67787cf37e64SBarry Smith 67797cf37e64SBarry Smith Input Parameters: 67807cf37e64SBarry Smith + ts - the TS context 67817cf37e64SBarry Smith . step - current time-step 67827cf37e64SBarry Smith . ptime - current time 67837cf37e64SBarry Smith . u - current solution 67847cf37e64SBarry Smith - dctx - unused context 67857cf37e64SBarry Smith 67867cf37e64SBarry Smith Level: intermediate 67877cf37e64SBarry Smith 67887cf37e64SBarry Smith The user must provide the solution using TSSetSolutionFunction() to use this monitor. 67897cf37e64SBarry Smith 67907cf37e64SBarry Smith Options Database Keys: 67917cf37e64SBarry Smith . -ts_monitor_error - create a graphical monitor of error history 67927cf37e64SBarry Smith 67937cf37e64SBarry Smith .keywords: TS, vector, monitor, view 67947cf37e64SBarry Smith 67957cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 67967cf37e64SBarry Smith @*/ 67977cf37e64SBarry Smith PetscErrorCode TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 67987cf37e64SBarry Smith { 67997cf37e64SBarry Smith PetscErrorCode ierr; 68007cf37e64SBarry Smith Vec y; 68017cf37e64SBarry Smith PetscReal nrm; 68027cf37e64SBarry Smith 68037cf37e64SBarry Smith PetscFunctionBegin; 68047cf37e64SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 68057cf37e64SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 68067cf37e64SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 68077cf37e64SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 68087cf37e64SBarry Smith ierr = PetscPrintf(PetscObjectComm((PetscObject)ts),"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr); 68097cf37e64SBarry Smith PetscFunctionReturn(0); 68107cf37e64SBarry Smith } 68117cf37e64SBarry Smith 6812201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6813201da799SBarry Smith { 6814201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6815201da799SBarry Smith PetscReal x = ptime,y; 6816201da799SBarry Smith PetscErrorCode ierr; 6817201da799SBarry Smith PetscInt its; 6818201da799SBarry Smith 6819201da799SBarry Smith PetscFunctionBegin; 682063e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 6821201da799SBarry Smith if (!n) { 6822201da799SBarry Smith PetscDrawAxis axis; 6823201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6824201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 6825201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6826201da799SBarry Smith ctx->snes_its = 0; 6827201da799SBarry Smith } 6828201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 6829201da799SBarry Smith y = its - ctx->snes_its; 6830201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 68313fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6832201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 68336934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 6834201da799SBarry Smith } 6835201da799SBarry Smith ctx->snes_its = its; 6836201da799SBarry Smith PetscFunctionReturn(0); 6837201da799SBarry Smith } 6838201da799SBarry Smith 6839201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6840201da799SBarry Smith { 6841201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6842201da799SBarry Smith PetscReal x = ptime,y; 6843201da799SBarry Smith PetscErrorCode ierr; 6844201da799SBarry Smith PetscInt its; 6845201da799SBarry Smith 6846201da799SBarry Smith PetscFunctionBegin; 684763e21af5SBarry Smith if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */ 6848201da799SBarry Smith if (!n) { 6849201da799SBarry Smith PetscDrawAxis axis; 6850201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6851201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 6852201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6853201da799SBarry Smith ctx->ksp_its = 0; 6854201da799SBarry Smith } 6855201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 6856201da799SBarry Smith y = its - ctx->ksp_its; 6857201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 685899fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6859201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 68606934998bSLisandro Dalcin ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr); 6861201da799SBarry Smith } 6862201da799SBarry Smith ctx->ksp_its = its; 6863201da799SBarry Smith PetscFunctionReturn(0); 6864201da799SBarry Smith } 6865f9c1d6abSBarry Smith 6866f9c1d6abSBarry Smith /*@ 6867f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 6868f9c1d6abSBarry Smith 6869f9c1d6abSBarry Smith Collective on TS and Vec 6870f9c1d6abSBarry Smith 6871f9c1d6abSBarry Smith Input Parameters: 6872f9c1d6abSBarry Smith + ts - the TS context 6873f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 6874f9c1d6abSBarry Smith 6875f9c1d6abSBarry Smith Output Parameters: 6876f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 6877f9c1d6abSBarry Smith 6878f9c1d6abSBarry Smith Level: developer 6879f9c1d6abSBarry Smith 6880f9c1d6abSBarry Smith .keywords: TS, compute 6881f9c1d6abSBarry Smith 6882f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 6883f9c1d6abSBarry Smith @*/ 6884f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 6885f9c1d6abSBarry Smith { 6886f9c1d6abSBarry Smith PetscErrorCode ierr; 6887f9c1d6abSBarry Smith 6888f9c1d6abSBarry Smith PetscFunctionBegin; 6889f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6890ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 6891f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 6892f9c1d6abSBarry Smith PetscFunctionReturn(0); 6893f9c1d6abSBarry Smith } 689424655328SShri 6895b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 6896b3d3934dSBarry Smith /*@C 6897b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 6898b3d3934dSBarry Smith 6899b3d3934dSBarry Smith Collective on TS 6900b3d3934dSBarry Smith 6901b3d3934dSBarry Smith Input Parameters: 6902b3d3934dSBarry Smith . ts - the ODE solver object 6903b3d3934dSBarry Smith 6904b3d3934dSBarry Smith Output Parameter: 6905b3d3934dSBarry Smith . ctx - the context 6906b3d3934dSBarry Smith 6907b3d3934dSBarry Smith Level: intermediate 6908b3d3934dSBarry Smith 6909b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso 6910b3d3934dSBarry Smith 6911b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 6912b3d3934dSBarry Smith 6913b3d3934dSBarry Smith @*/ 6914b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 6915b3d3934dSBarry Smith { 6916b3d3934dSBarry Smith PetscErrorCode ierr; 6917b3d3934dSBarry Smith 6918b3d3934dSBarry Smith PetscFunctionBegin; 6919a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 6920b3d3934dSBarry Smith PetscFunctionReturn(0); 6921b3d3934dSBarry Smith } 6922b3d3934dSBarry Smith 6923b3d3934dSBarry Smith /*@C 6924b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 6925b3d3934dSBarry Smith 6926b3d3934dSBarry Smith Collective on TS 6927b3d3934dSBarry Smith 6928b3d3934dSBarry Smith Input Parameters: 6929b3d3934dSBarry Smith + ts - the TS context 6930b3d3934dSBarry Smith . step - current time-step 6931b3d3934dSBarry Smith . ptime - current time 69327db568b7SBarry Smith . u - current solution 69337db568b7SBarry Smith - dctx - the envelope context 6934b3d3934dSBarry Smith 6935b3d3934dSBarry Smith Options Database: 6936b3d3934dSBarry Smith . -ts_monitor_envelope 6937b3d3934dSBarry Smith 6938b3d3934dSBarry Smith Level: intermediate 6939b3d3934dSBarry Smith 6940b3d3934dSBarry Smith Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 6941b3d3934dSBarry Smith 6942b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6943b3d3934dSBarry Smith 69447db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 6945b3d3934dSBarry Smith @*/ 69467db568b7SBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6947b3d3934dSBarry Smith { 6948b3d3934dSBarry Smith PetscErrorCode ierr; 69497db568b7SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 6950b3d3934dSBarry Smith 6951b3d3934dSBarry Smith PetscFunctionBegin; 6952b3d3934dSBarry Smith if (!ctx->max) { 6953b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 6954b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 6955b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 6956b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 6957b3d3934dSBarry Smith } else { 6958b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 6959b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 6960b3d3934dSBarry Smith } 6961b3d3934dSBarry Smith PetscFunctionReturn(0); 6962b3d3934dSBarry Smith } 6963b3d3934dSBarry Smith 6964b3d3934dSBarry Smith /*@C 6965b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 6966b3d3934dSBarry Smith 6967b3d3934dSBarry Smith Collective on TS 6968b3d3934dSBarry Smith 6969b3d3934dSBarry Smith Input Parameter: 6970b3d3934dSBarry Smith . ts - the TS context 6971b3d3934dSBarry Smith 6972b3d3934dSBarry Smith Output Parameter: 6973b3d3934dSBarry Smith + max - the maximum values 6974b3d3934dSBarry Smith - min - the minimum values 6975b3d3934dSBarry Smith 69767db568b7SBarry Smith Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 69777db568b7SBarry Smith 6978b3d3934dSBarry Smith Level: intermediate 6979b3d3934dSBarry Smith 6980b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6981b3d3934dSBarry Smith 6982b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6983b3d3934dSBarry Smith @*/ 6984b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 6985b3d3934dSBarry Smith { 6986b3d3934dSBarry Smith PetscInt i; 6987b3d3934dSBarry Smith 6988b3d3934dSBarry Smith PetscFunctionBegin; 6989b3d3934dSBarry Smith if (max) *max = NULL; 6990b3d3934dSBarry Smith if (min) *min = NULL; 6991b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6992b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 69935537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 6994b3d3934dSBarry Smith if (max) *max = ctx->max; 6995b3d3934dSBarry Smith if (min) *min = ctx->min; 6996b3d3934dSBarry Smith break; 6997b3d3934dSBarry Smith } 6998b3d3934dSBarry Smith } 6999b3d3934dSBarry Smith PetscFunctionReturn(0); 7000b3d3934dSBarry Smith } 7001b3d3934dSBarry Smith 7002b3d3934dSBarry Smith /*@C 7003b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 7004b3d3934dSBarry Smith 7005b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 7006b3d3934dSBarry Smith 7007b3d3934dSBarry Smith Input Parameter: 7008b3d3934dSBarry Smith . ctx - the monitor context 7009b3d3934dSBarry Smith 7010b3d3934dSBarry Smith Level: intermediate 7011b3d3934dSBarry Smith 7012b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy 7013b3d3934dSBarry Smith 70147db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 7015b3d3934dSBarry Smith @*/ 7016b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 7017b3d3934dSBarry Smith { 7018b3d3934dSBarry Smith PetscErrorCode ierr; 7019b3d3934dSBarry Smith 7020b3d3934dSBarry Smith PetscFunctionBegin; 7021b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 7022b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 7023b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 7024b3d3934dSBarry Smith PetscFunctionReturn(0); 7025b3d3934dSBarry Smith } 7026f2dee214SBarry Smith 702724655328SShri /*@ 7028dcb233daSLisandro Dalcin TSRestartStep - Flags the solver to restart the next step 7029dcb233daSLisandro Dalcin 7030dcb233daSLisandro Dalcin Collective on TS 7031dcb233daSLisandro Dalcin 7032dcb233daSLisandro Dalcin Input Parameter: 7033dcb233daSLisandro Dalcin . ts - the TS context obtained from TSCreate() 7034dcb233daSLisandro Dalcin 7035dcb233daSLisandro Dalcin Level: advanced 7036dcb233daSLisandro Dalcin 7037dcb233daSLisandro Dalcin Notes: 7038dcb233daSLisandro Dalcin Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of 7039dcb233daSLisandro Dalcin discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution 7040dcb233daSLisandro Dalcin vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For 7041dcb233daSLisandro Dalcin the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce 7042dcb233daSLisandro Dalcin discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with 7043dcb233daSLisandro Dalcin discontinuous source terms). 7044dcb233daSLisandro Dalcin 7045dcb233daSLisandro Dalcin .keywords: TS, timestep, restart 7046dcb233daSLisandro Dalcin 7047dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep() 7048dcb233daSLisandro Dalcin @*/ 7049dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts) 7050dcb233daSLisandro Dalcin { 7051dcb233daSLisandro Dalcin PetscFunctionBegin; 7052dcb233daSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7053dcb233daSLisandro Dalcin ts->steprestart = PETSC_TRUE; 7054dcb233daSLisandro Dalcin PetscFunctionReturn(0); 7055dcb233daSLisandro Dalcin } 7056dcb233daSLisandro Dalcin 7057dcb233daSLisandro Dalcin /*@ 705824655328SShri TSRollBack - Rolls back one time step 705924655328SShri 706024655328SShri Collective on TS 706124655328SShri 706224655328SShri Input Parameter: 706324655328SShri . ts - the TS context obtained from TSCreate() 706424655328SShri 706524655328SShri Level: advanced 706624655328SShri 706724655328SShri .keywords: TS, timestep, rollback 706824655328SShri 706924655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 707024655328SShri @*/ 707124655328SShri PetscErrorCode TSRollBack(TS ts) 707224655328SShri { 707324655328SShri PetscErrorCode ierr; 707424655328SShri 707524655328SShri PetscFunctionBegin; 707624655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7077b3de5cdeSLisandro Dalcin if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called"); 707824655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 707924655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 708024655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 708124655328SShri ts->ptime = ts->ptime_prev; 7082be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 70832808aa04SLisandro Dalcin ts->steps--; 7084b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 708524655328SShri PetscFunctionReturn(0); 708624655328SShri } 7087aeb4809dSShri Abhyankar 7088ff22ae23SHong Zhang /*@ 7089ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 7090ff22ae23SHong Zhang 7091ff22ae23SHong Zhang Input Parameter: 7092ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 7093ff22ae23SHong Zhang 7094ff22ae23SHong Zhang Level: advanced 7095ff22ae23SHong Zhang 7096ff22ae23SHong Zhang .keywords: TS, getstages 7097ff22ae23SHong Zhang 7098ff22ae23SHong Zhang .seealso: TSCreate() 7099ff22ae23SHong Zhang @*/ 7100ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns,Vec **Y) 7101ff22ae23SHong Zhang { 7102ff22ae23SHong Zhang PetscErrorCode ierr; 7103ff22ae23SHong Zhang 7104ff22ae23SHong Zhang PetscFunctionBegin; 7105ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7106ff22ae23SHong Zhang PetscValidPointer(ns,2); 7107ff22ae23SHong Zhang 7108ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 7109ff22ae23SHong Zhang else { 7110ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 7111ff22ae23SHong Zhang } 7112ff22ae23SHong Zhang PetscFunctionReturn(0); 7113ff22ae23SHong Zhang } 7114ff22ae23SHong Zhang 7115847ff0e1SMatthew G. Knepley /*@C 7116847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 7117847ff0e1SMatthew G. Knepley 7118847ff0e1SMatthew G. Knepley Collective on SNES 7119847ff0e1SMatthew G. Knepley 7120847ff0e1SMatthew G. Knepley Input Parameters: 7121847ff0e1SMatthew G. Knepley + ts - the TS context 7122847ff0e1SMatthew G. Knepley . t - current timestep 7123847ff0e1SMatthew G. Knepley . U - state vector 7124847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 7125847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 7126847ff0e1SMatthew G. Knepley - ctx - an optional user context 7127847ff0e1SMatthew G. Knepley 7128847ff0e1SMatthew G. Knepley Output Parameters: 7129847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 7130847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 7131847ff0e1SMatthew G. Knepley 7132847ff0e1SMatthew G. Knepley Level: intermediate 7133847ff0e1SMatthew G. Knepley 7134847ff0e1SMatthew G. Knepley Notes: 7135847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 7136847ff0e1SMatthew G. Knepley 7137847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 7138847ff0e1SMatthew G. Knepley 7139847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 7140847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 7141847ff0e1SMatthew G. Knepley 7142847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 7143847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 7144847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 7145847ff0e1SMatthew G. Knepley 7146847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse 7147847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 7148847ff0e1SMatthew G. Knepley @*/ 7149847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 7150847ff0e1SMatthew G. Knepley { 7151847ff0e1SMatthew G. Knepley SNES snes; 7152847ff0e1SMatthew G. Knepley MatFDColoring color; 7153847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 7154847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 7155847ff0e1SMatthew G. Knepley 7156847ff0e1SMatthew G. Knepley PetscFunctionBegin; 7157c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 7158847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 7159847ff0e1SMatthew G. Knepley if (!color) { 7160847ff0e1SMatthew G. Knepley DM dm; 7161847ff0e1SMatthew G. Knepley ISColoring iscoloring; 7162847ff0e1SMatthew G. Knepley 7163847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 7164847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 7165847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 7166847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 7167847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7168847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7169847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7170847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7171847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7172847ff0e1SMatthew G. Knepley } else { 7173847ff0e1SMatthew G. Knepley MatColoring mc; 7174847ff0e1SMatthew G. Knepley 7175847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 7176847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 7177847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 7178847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 7179847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 7180847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 7181847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 7182847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 7183847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 7184847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 7185847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 7186847ff0e1SMatthew G. Knepley } 7187847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 7188847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 7189847ff0e1SMatthew G. Knepley } 7190847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 7191847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 7192847ff0e1SMatthew G. Knepley if (J != B) { 7193847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7194847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7195847ff0e1SMatthew G. Knepley } 7196847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 7197847ff0e1SMatthew G. Knepley } 719893b34091SDebojyoti Ghosh 7199cb9d8021SPierre Barbier de Reuille /*@ 7200cb9d8021SPierre Barbier de Reuille TSSetFunctionDomainError - Set the function testing if the current state vector is valid 7201cb9d8021SPierre Barbier de Reuille 7202cb9d8021SPierre Barbier de Reuille Input Parameters: 7203cb9d8021SPierre Barbier de Reuille ts - the TS context 7204cb9d8021SPierre Barbier de Reuille func - function called within TSFunctionDomainError 7205cb9d8021SPierre Barbier de Reuille 7206cb9d8021SPierre Barbier de Reuille Level: intermediate 7207cb9d8021SPierre Barbier de Reuille 7208cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain 7209cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError() 7210cb9d8021SPierre Barbier de Reuille @*/ 7211cb9d8021SPierre Barbier de Reuille 7212d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 7213cb9d8021SPierre Barbier de Reuille { 7214cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7215cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 7216cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 7217cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7218cb9d8021SPierre Barbier de Reuille } 7219cb9d8021SPierre Barbier de Reuille 7220cb9d8021SPierre Barbier de Reuille /*@ 7221cb9d8021SPierre Barbier de Reuille TSFunctionDomainError - Check if the current state is valid 7222cb9d8021SPierre Barbier de Reuille 7223cb9d8021SPierre Barbier de Reuille Input Parameters: 7224cb9d8021SPierre Barbier de Reuille ts - the TS context 7225cb9d8021SPierre Barbier de Reuille stagetime - time of the simulation 7226d183316bSPierre Barbier de Reuille Y - state vector to check. 7227cb9d8021SPierre Barbier de Reuille 7228cb9d8021SPierre Barbier de Reuille Output Parameter: 7229cb9d8021SPierre Barbier de Reuille accept - Set to PETSC_FALSE if the current state vector is valid. 7230cb9d8021SPierre Barbier de Reuille 7231cb9d8021SPierre Barbier de Reuille Note: 7232cb9d8021SPierre Barbier de Reuille This function should be used to ensure the state is in a valid part of the space. 7233cb9d8021SPierre Barbier de Reuille For example, one can ensure here all values are positive. 723496a0c994SBarry Smith 723596a0c994SBarry Smith Level: advanced 7236cb9d8021SPierre Barbier de Reuille @*/ 7237d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 7238cb9d8021SPierre Barbier de Reuille { 7239cb9d8021SPierre Barbier de Reuille PetscErrorCode ierr; 7240cb9d8021SPierre Barbier de Reuille 7241cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 7242cb9d8021SPierre Barbier de Reuille 7243cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7244cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 7245cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 7246d183316bSPierre Barbier de Reuille PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 7247cb9d8021SPierre Barbier de Reuille } 7248cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 7249cb9d8021SPierre Barbier de Reuille } 72501ceb14c0SBarry Smith 725193b34091SDebojyoti Ghosh /*@C 7252e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 725393b34091SDebojyoti Ghosh 725493b34091SDebojyoti Ghosh Collective on MPI_Comm 725593b34091SDebojyoti Ghosh 725693b34091SDebojyoti Ghosh Input Parameter: 725793b34091SDebojyoti Ghosh . tsin - The input TS 725893b34091SDebojyoti Ghosh 725993b34091SDebojyoti Ghosh Output Parameter: 7260e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 726193b34091SDebojyoti Ghosh 72625eca1a21SEmil Constantinescu Notes: 72635eca1a21SEmil 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. 72645eca1a21SEmil Constantinescu 7265baa10174SEmil Constantinescu When using TSDestroy() on a clone the user has to first reset the correct TS reference in the embedded SNES object: e.g.: by running SNES snes_dup=NULL; TSGetSNES(ts,&snes_dup); ierr = TSSetSNES(ts,snes_dup); 72665eca1a21SEmil Constantinescu 72675eca1a21SEmil Constantinescu Level: developer 726893b34091SDebojyoti Ghosh 7269e5168f73SEmil Constantinescu .keywords: TS, clone 7270e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 727193b34091SDebojyoti Ghosh @*/ 7272baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 727393b34091SDebojyoti Ghosh { 727493b34091SDebojyoti Ghosh TS t; 727593b34091SDebojyoti Ghosh PetscErrorCode ierr; 7276dc846ba4SSatish Balay SNES snes_start; 7277dc846ba4SSatish Balay DM dm; 7278dc846ba4SSatish Balay TSType type; 727993b34091SDebojyoti Ghosh 728093b34091SDebojyoti Ghosh PetscFunctionBegin; 728193b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 728293b34091SDebojyoti Ghosh *tsout = NULL; 728393b34091SDebojyoti Ghosh 72847a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 728593b34091SDebojyoti Ghosh 728693b34091SDebojyoti Ghosh /* General TS description */ 728793b34091SDebojyoti Ghosh t->numbermonitors = 0; 728893b34091SDebojyoti Ghosh t->setupcalled = 0; 728993b34091SDebojyoti Ghosh t->ksp_its = 0; 729093b34091SDebojyoti Ghosh t->snes_its = 0; 729193b34091SDebojyoti Ghosh t->nwork = 0; 729293b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 729393b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 729493b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 729593b34091SDebojyoti Ghosh 729634561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr); 729734561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr); 7298d15a3a53SEmil Constantinescu 729993b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr); 730093b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm);CHKERRQ(ierr); 730193b34091SDebojyoti Ghosh 730293b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 730351699248SLisandro Dalcin ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr); 730493b34091SDebojyoti Ghosh 7305e7069c78SShri t->trajectory = tsin->trajectory; 7306e7069c78SShri ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr); 7307e7069c78SShri 7308e7069c78SShri t->event = tsin->event; 73096b10a48eSSatish Balay if (t->event) t->event->refct++; 7310e7069c78SShri 731193b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 731293b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 7313e7069c78SShri t->ptime_prev = tsin->ptime_prev; 731493b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 731593b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 731693b34091SDebojyoti Ghosh t->steps = tsin->steps; 731793b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 731893b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 731993b34091SDebojyoti Ghosh t->atol = tsin->atol; 732093b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 732193b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 732293b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 732393b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 732493b34091SDebojyoti Ghosh 732593b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type);CHKERRQ(ierr); 732693b34091SDebojyoti Ghosh ierr = TSSetType(t,type);CHKERRQ(ierr); 732793b34091SDebojyoti Ghosh 732893b34091SDebojyoti Ghosh t->vec_sol = NULL; 732993b34091SDebojyoti Ghosh 733093b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 733193b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 733293b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 733393b34091SDebojyoti Ghosh 733493b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 733593b34091SDebojyoti Ghosh 73360d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 73370d4fed19SBarry Smith PetscInt i; 73380d4fed19SBarry Smith ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr); 73390d4fed19SBarry Smith for (i=0; i<10; i++) { 73400d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 73410d4fed19SBarry Smith } 73420d4fed19SBarry Smith } 734393b34091SDebojyoti Ghosh *tsout = t; 734493b34091SDebojyoti Ghosh PetscFunctionReturn(0); 734593b34091SDebojyoti Ghosh } 7346