163dd3a1aSKris Buschelman 2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h> /*I "petscts.h" I*/ 3496e6a7aSJed Brown #include <petscdmshell.h> 41e25c274SJed Brown #include <petscdmda.h> 52d5ee99bSBarry Smith #include <petscviewer.h> 62d5ee99bSBarry Smith #include <petscdraw.h> 7d763cef2SBarry Smith 8d5ba7fb7SMatthew Knepley /* Logging support */ 9d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 10166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 11d405a339SMatthew Knepley 1249354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1349354f04SShri Abhyankar 144a2ae208SSatish Balay #undef __FUNCT__ 15bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions" 16bdad233fSMatthew Knepley /* 17bdad233fSMatthew Knepley TSSetTypeFromOptions - Sets the type of ts from user options. 18bdad233fSMatthew Knepley 19bdad233fSMatthew Knepley Collective on TS 20bdad233fSMatthew Knepley 21bdad233fSMatthew Knepley Input Parameter: 22bdad233fSMatthew Knepley . ts - The ts 23bdad233fSMatthew Knepley 24bdad233fSMatthew Knepley Level: intermediate 25bdad233fSMatthew Knepley 26bdad233fSMatthew Knepley .keywords: TS, set, options, database, type 27bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType() 28bdad233fSMatthew Knepley */ 296849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts) 30bdad233fSMatthew Knepley { 31ace3abfcSBarry Smith PetscBool opt; 322fc52814SBarry Smith const char *defaultType; 33bdad233fSMatthew Knepley char typeName[256]; 34dfbe8321SBarry Smith PetscErrorCode ierr; 35bdad233fSMatthew Knepley 36bdad233fSMatthew Knepley PetscFunctionBegin; 37bbd56ea5SKarl Rupp if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 38bbd56ea5SKarl Rupp else defaultType = TSEULER; 39bdad233fSMatthew Knepley 400298fd71SBarry Smith if (!TSRegisterAllCalled) {ierr = TSRegisterAll(NULL);CHKERRQ(ierr);} 41bdad233fSMatthew Knepley ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 42a7cc72afSBarry Smith if (opt) { 43bdad233fSMatthew Knepley ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 44bdad233fSMatthew Knepley } else { 45bdad233fSMatthew Knepley ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 46bdad233fSMatthew Knepley } 47bdad233fSMatthew Knepley PetscFunctionReturn(0); 48bdad233fSMatthew Knepley } 49bdad233fSMatthew Knepley 502d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx { 512d5ee99bSBarry Smith PetscViewer viewer; 522d5ee99bSBarry Smith Vec initialsolution; 532d5ee99bSBarry Smith PetscBool showinitial; 542d5ee99bSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 552d5ee99bSBarry Smith PetscBool showtimestepandtime; 562d5ee99bSBarry Smith int color; 572d5ee99bSBarry Smith }; 582d5ee99bSBarry Smith 59bdad233fSMatthew Knepley #undef __FUNCT__ 60bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions" 61bdad233fSMatthew Knepley /*@ 62bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 63bdad233fSMatthew Knepley 64bdad233fSMatthew Knepley Collective on TS 65bdad233fSMatthew Knepley 66bdad233fSMatthew Knepley Input Parameter: 67bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 68bdad233fSMatthew Knepley 69bdad233fSMatthew Knepley Options Database Keys: 704d91e141SJed Brown + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP 71bdad233fSMatthew Knepley . -ts_max_steps maxsteps - maximum number of time-steps to take 723bca7d26SBarry Smith . -ts_final_time time - maximum time to compute to 73bdad233fSMatthew Knepley . -ts_dt dt - initial time step 74bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 75de06c3feSJed Brown . -ts_monitor_lg_timestep - Monitor timestep size graphically 76de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 77de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 78de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 79de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 80de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 81de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 822d5ee99bSBarry Smith . -ts_monitor_draw_solution_phase - Monitor solution graphically with phase diagram 83de06c3feSJed Brown . -ts_monitor_draw_error - Monitor error graphically 8491b97e58SBarry Smith . -ts_monitor_solution_binary <filename> - Save each solution to a binary file 8591b97e58SBarry Smith - -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 8691b97e58SBarry Smith 8791b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 88bdad233fSMatthew Knepley 89bdad233fSMatthew Knepley Level: beginner 90bdad233fSMatthew Knepley 91bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 92bdad233fSMatthew Knepley 93a313700dSBarry Smith .seealso: TSGetType() 94bdad233fSMatthew Knepley @*/ 957087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 96bdad233fSMatthew Knepley { 97ace3abfcSBarry Smith PetscBool opt,flg; 98dfbe8321SBarry Smith PetscErrorCode ierr; 99649052a6SBarry Smith PetscViewer monviewer; 100eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 101089b2837SJed Brown SNES snes; 1021c3436cfSJed Brown TSAdapt adapt; 10331748224SBarry Smith PetscReal time_step; 10449354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 105d1212d36SBarry Smith char dir[16]; 106bdad233fSMatthew Knepley 107bdad233fSMatthew Knepley PetscFunctionBegin; 1080700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1093194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 110a43b19c4SJed Brown /* Handle TS type options */ 111a43b19c4SJed Brown ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr); 112bdad233fSMatthew Knepley 113bdad233fSMatthew Knepley /* Handle generic TS options */ 1140298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1150298fd71SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 1160298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 11731748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 11831748224SBarry Smith if (flg) { 11931748224SBarry Smith ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 12031748224SBarry Smith } 12149354f04SShri 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); 12249354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 1230298fd71SBarry 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); 1240298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 1250298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 1260298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 1270298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 128bdad233fSMatthew Knepley 129bdad233fSMatthew Knepley /* Monitor options */ 130a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 131eabae89aSBarry Smith if (flg) { 132ce94432eSBarry Smith ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr); 133649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 134bdad233fSMatthew Knepley } 1355180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1365180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1375180491cSLisandro Dalcin 1384f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 139a7cc72afSBarry Smith if (opt) { 1400b039ecaSBarry Smith TSMonitorLGCtx ctx; 1413923b477SBarry Smith PetscInt howoften = 1; 142a80ad3e0SBarry Smith 1430298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 144ce94432eSBarry Smith ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 1454f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 146b3603a34SBarry Smith } 1474f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 148b3603a34SBarry Smith if (opt) { 1490b039ecaSBarry Smith TSMonitorLGCtx ctx; 1503923b477SBarry Smith PetscInt howoften = 1; 151b3603a34SBarry Smith 1520298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 15322d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1544f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 155bdad233fSMatthew Knepley } 1564f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 157ef20d060SBarry Smith if (opt) { 1580b039ecaSBarry Smith TSMonitorLGCtx ctx; 1593923b477SBarry Smith PetscInt howoften = 1; 160ef20d060SBarry Smith 1610298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 16222d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1634f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 164ef20d060SBarry Smith } 165201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 166201da799SBarry Smith if (opt) { 167201da799SBarry Smith TSMonitorLGCtx ctx; 168201da799SBarry Smith PetscInt howoften = 1; 169201da799SBarry Smith 1700298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 17122d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 172201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 173201da799SBarry Smith } 174201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 175201da799SBarry Smith if (opt) { 176201da799SBarry Smith TSMonitorLGCtx ctx; 177201da799SBarry Smith PetscInt howoften = 1; 178201da799SBarry Smith 1790298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 18022d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 181201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 182201da799SBarry Smith } 1838189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 1848189c53fSBarry Smith if (opt) { 1858189c53fSBarry Smith TSMonitorSPEigCtx ctx; 1868189c53fSBarry Smith PetscInt howoften = 1; 1878189c53fSBarry Smith 1880298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 18922d28d08SBarry Smith ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1908189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 1918189c53fSBarry Smith } 192ef20d060SBarry Smith opt = PETSC_FALSE; 1930dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 194a7cc72afSBarry Smith if (opt) { 19583a4ac43SBarry Smith TSMonitorDrawCtx ctx; 19683a4ac43SBarry Smith PetscInt howoften = 1; 197a80ad3e0SBarry Smith 1980298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 199ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 20083a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 201bdad233fSMatthew Knepley } 202fb1732b5SBarry Smith opt = PETSC_FALSE; 2032d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 2042d5ee99bSBarry Smith if (opt) { 2052d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2062d5ee99bSBarry Smith PetscReal bounds[4]; 2072d5ee99bSBarry Smith PetscInt n = 4; 2082d5ee99bSBarry Smith PetscDraw draw; 2092d5ee99bSBarry Smith 2102d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 2112d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2122d5ee99bSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr); 2132d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 2142d5ee99bSBarry Smith ierr = PetscDrawClear(draw);CHKERRQ(ierr); 2152d5ee99bSBarry Smith ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); 2162d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2172d5ee99bSBarry Smith } 2182d5ee99bSBarry Smith opt = PETSC_FALSE; 2190dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 2203a471f94SBarry Smith if (opt) { 22183a4ac43SBarry Smith TSMonitorDrawCtx ctx; 22283a4ac43SBarry Smith PetscInt howoften = 1; 2233a471f94SBarry Smith 2240298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 225ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 22683a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2273a471f94SBarry Smith } 2283a471f94SBarry Smith opt = PETSC_FALSE; 22991b97e58SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 230fb1732b5SBarry Smith if (flg) { 231fb1732b5SBarry Smith PetscViewer ctx; 232fb1732b5SBarry Smith if (monfilename[0]) { 233ce94432eSBarry Smith ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 234c2fbc07fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 235fb1732b5SBarry Smith } else { 236ce94432eSBarry Smith ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts)); 2370298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr); 238fb1732b5SBarry Smith } 239fb1732b5SBarry Smith } 240ed81e22dSJed Brown opt = PETSC_FALSE; 24191b97e58SBarry 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); 242ed81e22dSJed Brown if (flg) { 243ed81e22dSJed Brown const char *ptr,*ptr2; 244ed81e22dSJed Brown char *filetemplate; 245ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 246ed81e22dSJed Brown /* Do some cursory validation of the input. */ 247ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 248ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 249ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 250ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 251ce94432eSBarry 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"); 252ed81e22dSJed Brown if (ptr2) break; 253ed81e22dSJed Brown } 254ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 255ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 256ed81e22dSJed Brown } 257bdad233fSMatthew Knepley 258d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 259d1212d36SBarry Smith if (flg) { 260d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 261d1212d36SBarry Smith int ray = 0; 262d1212d36SBarry Smith DMDADirection ddir; 263d1212d36SBarry Smith DM da; 264d1212d36SBarry Smith PetscMPIInt rank; 265d1212d36SBarry Smith 266ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 267d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 268d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 269ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 270d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 271d1212d36SBarry Smith 272d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 273d1212d36SBarry Smith ierr = PetscNew(TSMonitorDMDARayCtx,&rayctx);CHKERRQ(ierr); 274d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 275d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 276ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 277d1212d36SBarry Smith if (!rank) { 278d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 279d1212d36SBarry Smith } 280d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 281d1212d36SBarry Smith } 282d1212d36SBarry Smith 283ad6bc421SBarry Smith ierr = TSGetTSAdapt(ts,&adapt);CHKERRQ(ierr); 2841c3436cfSJed Brown ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr); 2851c3436cfSJed Brown 286d52bd9f3SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 287d52bd9f3SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 288d52bd9f3SBarry Smith 289bdad233fSMatthew Knepley /* Handle specific TS options */ 290abc0a331SBarry Smith if (ts->ops->setfromoptions) { 291bdad233fSMatthew Knepley ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr); 292bdad233fSMatthew Knepley } 2935d973c19SBarry Smith 2945d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 2955d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 296bdad233fSMatthew Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 297bdad233fSMatthew Knepley PetscFunctionReturn(0); 298bdad233fSMatthew Knepley } 299bdad233fSMatthew Knepley 300bdad233fSMatthew Knepley #undef __FUNCT__ 301cdcf91faSSean Farley #undef __FUNCT__ 3024a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian" 303a7a1495cSBarry Smith /*@ 3048c385f81SBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 305a7a1495cSBarry Smith set with TSSetRHSJacobian(). 306a7a1495cSBarry Smith 307a7a1495cSBarry Smith Collective on TS and Vec 308a7a1495cSBarry Smith 309a7a1495cSBarry Smith Input Parameters: 310316643e7SJed Brown + ts - the TS context 311a7a1495cSBarry Smith . t - current timestep 3120910c330SBarry Smith - U - input vector 313a7a1495cSBarry Smith 314a7a1495cSBarry Smith Output Parameters: 315a7a1495cSBarry Smith + A - Jacobian matrix 316a7a1495cSBarry Smith . B - optional preconditioning matrix 317a7a1495cSBarry Smith - flag - flag indicating matrix structure 318a7a1495cSBarry Smith 319a7a1495cSBarry Smith Notes: 320a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 321a7a1495cSBarry Smith is used internally within the nonlinear solvers. 322a7a1495cSBarry Smith 32394b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 324a7a1495cSBarry Smith flag parameter. 325a7a1495cSBarry Smith 326a7a1495cSBarry Smith Level: developer 327a7a1495cSBarry Smith 328a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 329a7a1495cSBarry Smith 33094b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 331a7a1495cSBarry Smith @*/ 3320910c330SBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg) 333a7a1495cSBarry Smith { 334dfbe8321SBarry Smith PetscErrorCode ierr; 3350910c330SBarry Smith PetscInt Ustate; 33624989b8cSPeter Brune DM dm; 337942e3340SBarry Smith DMTS tsdm; 33824989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 33924989b8cSPeter Brune void *ctx; 34024989b8cSPeter Brune TSIJacobian ijacobianfunc; 341a7a1495cSBarry Smith 342a7a1495cSBarry Smith PetscFunctionBegin; 3430700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3440910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3450910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 34624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 347942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 34824989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 3490298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 3500910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr); 3510910c330SBarry Smith if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) { 3520e4ef248SJed Brown *flg = ts->rhsjacobian.mstructure; 3530e4ef248SJed Brown PetscFunctionReturn(0); 354f8ede8e7SJed Brown } 355d90be118SSean Farley 356ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 357d90be118SSean Farley 35824989b8cSPeter Brune if (rhsjacobianfunc) { 3590910c330SBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 360a7a1495cSBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 361a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 3620910c330SBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr); 363a7a1495cSBarry Smith PetscStackPop; 3640910c330SBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 365a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 3660700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 3670700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 368ef66eb69SBarry Smith } else { 369214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 370214bc6a2SJed Brown if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);} 371214bc6a2SJed Brown *flg = SAME_NONZERO_PATTERN; 372ef66eb69SBarry Smith } 3730e4ef248SJed Brown ts->rhsjacobian.time = t; 3740910c330SBarry Smith ts->rhsjacobian.X = U; 3750910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 3760e4ef248SJed Brown ts->rhsjacobian.mstructure = *flg; 377a7a1495cSBarry Smith PetscFunctionReturn(0); 378a7a1495cSBarry Smith } 379a7a1495cSBarry Smith 3804a2ae208SSatish Balay #undef __FUNCT__ 3814a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 382316643e7SJed Brown /*@ 383d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 384d763cef2SBarry Smith 385316643e7SJed Brown Collective on TS and Vec 386316643e7SJed Brown 387316643e7SJed Brown Input Parameters: 388316643e7SJed Brown + ts - the TS context 389316643e7SJed Brown . t - current time 3900910c330SBarry Smith - U - state vector 391316643e7SJed Brown 392316643e7SJed Brown Output Parameter: 393316643e7SJed Brown . y - right hand side 394316643e7SJed Brown 395316643e7SJed Brown Note: 396316643e7SJed Brown Most users should not need to explicitly call this routine, as it 397316643e7SJed Brown is used internally within the nonlinear solvers. 398316643e7SJed Brown 399316643e7SJed Brown Level: developer 400316643e7SJed Brown 401316643e7SJed Brown .keywords: TS, compute 402316643e7SJed Brown 403316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 404316643e7SJed Brown @*/ 4050910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 406d763cef2SBarry Smith { 407dfbe8321SBarry Smith PetscErrorCode ierr; 40824989b8cSPeter Brune TSRHSFunction rhsfunction; 40924989b8cSPeter Brune TSIFunction ifunction; 41024989b8cSPeter Brune void *ctx; 41124989b8cSPeter Brune DM dm; 41224989b8cSPeter Brune 413d763cef2SBarry Smith PetscFunctionBegin; 4140700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4150910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 4160700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 41724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 41824989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 4190298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 420d763cef2SBarry Smith 421ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 422d763cef2SBarry Smith 4230910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 42424989b8cSPeter Brune if (rhsfunction) { 425d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 4260910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 427d763cef2SBarry Smith PetscStackPop; 428214bc6a2SJed Brown } else { 429214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 430b2cd27e8SJed Brown } 43144a41b28SSean Farley 4320910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 433d763cef2SBarry Smith PetscFunctionReturn(0); 434d763cef2SBarry Smith } 435d763cef2SBarry Smith 4364a2ae208SSatish Balay #undef __FUNCT__ 437ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 438ef20d060SBarry Smith /*@ 439ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 440ef20d060SBarry Smith 441ef20d060SBarry Smith Collective on TS and Vec 442ef20d060SBarry Smith 443ef20d060SBarry Smith Input Parameters: 444ef20d060SBarry Smith + ts - the TS context 445ef20d060SBarry Smith - t - current time 446ef20d060SBarry Smith 447ef20d060SBarry Smith Output Parameter: 4480910c330SBarry Smith . U - the solution 449ef20d060SBarry Smith 450ef20d060SBarry Smith Note: 451ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 452ef20d060SBarry Smith is used internally within the nonlinear solvers. 453ef20d060SBarry Smith 454ef20d060SBarry Smith Level: developer 455ef20d060SBarry Smith 456ef20d060SBarry Smith .keywords: TS, compute 457ef20d060SBarry Smith 458abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 459ef20d060SBarry Smith @*/ 4600910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 461ef20d060SBarry Smith { 462ef20d060SBarry Smith PetscErrorCode ierr; 463ef20d060SBarry Smith TSSolutionFunction solutionfunction; 464ef20d060SBarry Smith void *ctx; 465ef20d060SBarry Smith DM dm; 466ef20d060SBarry Smith 467ef20d060SBarry Smith PetscFunctionBegin; 468ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4690910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 470ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 471ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 472ef20d060SBarry Smith 473ef20d060SBarry Smith if (solutionfunction) { 4749b7cd975SBarry Smith PetscStackPush("TS user solution function"); 4750910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 476ef20d060SBarry Smith PetscStackPop; 477ef20d060SBarry Smith } 478ef20d060SBarry Smith PetscFunctionReturn(0); 479ef20d060SBarry Smith } 4809b7cd975SBarry Smith #undef __FUNCT__ 4819b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction" 4829b7cd975SBarry Smith /*@ 4839b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 4849b7cd975SBarry Smith 4859b7cd975SBarry Smith Collective on TS and Vec 4869b7cd975SBarry Smith 4879b7cd975SBarry Smith Input Parameters: 4889b7cd975SBarry Smith + ts - the TS context 4899b7cd975SBarry Smith - t - current time 4909b7cd975SBarry Smith 4919b7cd975SBarry Smith Output Parameter: 4929b7cd975SBarry Smith . U - the function value 4939b7cd975SBarry Smith 4949b7cd975SBarry Smith Note: 4959b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 4969b7cd975SBarry Smith is used internally within the nonlinear solvers. 4979b7cd975SBarry Smith 4989b7cd975SBarry Smith Level: developer 4999b7cd975SBarry Smith 5009b7cd975SBarry Smith .keywords: TS, compute 5019b7cd975SBarry Smith 5029b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 5039b7cd975SBarry Smith @*/ 5049b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 5059b7cd975SBarry Smith { 5069b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 5079b7cd975SBarry Smith void *ctx; 5089b7cd975SBarry Smith DM dm; 5099b7cd975SBarry Smith 5109b7cd975SBarry Smith PetscFunctionBegin; 5119b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5129b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5139b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 5149b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 5159b7cd975SBarry Smith 5169b7cd975SBarry Smith if (forcing) { 5179b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 5189b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 5199b7cd975SBarry Smith PetscStackPop; 5209b7cd975SBarry Smith } 5219b7cd975SBarry Smith PetscFunctionReturn(0); 5229b7cd975SBarry Smith } 523ef20d060SBarry Smith 524ef20d060SBarry Smith #undef __FUNCT__ 525214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 526214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 527214bc6a2SJed Brown { 5282dd45cf8SJed Brown Vec F; 529214bc6a2SJed Brown PetscErrorCode ierr; 530214bc6a2SJed Brown 531214bc6a2SJed Brown PetscFunctionBegin; 5320298fd71SBarry Smith *Frhs = NULL; 5330298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 534214bc6a2SJed Brown if (!ts->Frhs) { 5352dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 536214bc6a2SJed Brown } 537214bc6a2SJed Brown *Frhs = ts->Frhs; 538214bc6a2SJed Brown PetscFunctionReturn(0); 539214bc6a2SJed Brown } 540214bc6a2SJed Brown 541214bc6a2SJed Brown #undef __FUNCT__ 542214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 543214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 544214bc6a2SJed Brown { 545214bc6a2SJed Brown Mat A,B; 5462dd45cf8SJed Brown PetscErrorCode ierr; 547214bc6a2SJed Brown 548214bc6a2SJed Brown PetscFunctionBegin; 5490298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 550214bc6a2SJed Brown if (Arhs) { 551214bc6a2SJed Brown if (!ts->Arhs) { 552214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 553214bc6a2SJed Brown } 554214bc6a2SJed Brown *Arhs = ts->Arhs; 555214bc6a2SJed Brown } 556214bc6a2SJed Brown if (Brhs) { 557214bc6a2SJed Brown if (!ts->Brhs) { 558214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 559214bc6a2SJed Brown } 560214bc6a2SJed Brown *Brhs = ts->Brhs; 561214bc6a2SJed Brown } 562214bc6a2SJed Brown PetscFunctionReturn(0); 563214bc6a2SJed Brown } 564214bc6a2SJed Brown 565214bc6a2SJed Brown #undef __FUNCT__ 566316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 567316643e7SJed Brown /*@ 5680910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 569316643e7SJed Brown 570316643e7SJed Brown Collective on TS and Vec 571316643e7SJed Brown 572316643e7SJed Brown Input Parameters: 573316643e7SJed Brown + ts - the TS context 574316643e7SJed Brown . t - current time 5750910c330SBarry Smith . U - state vector 5760910c330SBarry Smith . Udot - time derivative of state vector 577214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 578316643e7SJed Brown 579316643e7SJed Brown Output Parameter: 580316643e7SJed Brown . Y - right hand side 581316643e7SJed Brown 582316643e7SJed Brown Note: 583316643e7SJed Brown Most users should not need to explicitly call this routine, as it 584316643e7SJed Brown is used internally within the nonlinear solvers. 585316643e7SJed Brown 586316643e7SJed Brown If the user did did not write their equations in implicit form, this 587316643e7SJed Brown function recasts them in implicit form. 588316643e7SJed Brown 589316643e7SJed Brown Level: developer 590316643e7SJed Brown 591316643e7SJed Brown .keywords: TS, compute 592316643e7SJed Brown 593316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 594316643e7SJed Brown @*/ 5950910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 596316643e7SJed Brown { 597316643e7SJed Brown PetscErrorCode ierr; 59824989b8cSPeter Brune TSIFunction ifunction; 59924989b8cSPeter Brune TSRHSFunction rhsfunction; 60024989b8cSPeter Brune void *ctx; 60124989b8cSPeter Brune DM dm; 602316643e7SJed Brown 603316643e7SJed Brown PetscFunctionBegin; 6040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6050910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6060910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 6070700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 608316643e7SJed Brown 60924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 61024989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 6110298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 61224989b8cSPeter Brune 613ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 614d90be118SSean Farley 6150910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 61624989b8cSPeter Brune if (ifunction) { 617316643e7SJed Brown PetscStackPush("TS user implicit function"); 6180910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 619316643e7SJed Brown PetscStackPop; 620214bc6a2SJed Brown } 621214bc6a2SJed Brown if (imex) { 62224989b8cSPeter Brune if (!ifunction) { 6230910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 6242dd45cf8SJed Brown } 62524989b8cSPeter Brune } else if (rhsfunction) { 62624989b8cSPeter Brune if (ifunction) { 627214bc6a2SJed Brown Vec Frhs; 628214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 6290910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 630214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 6312dd45cf8SJed Brown } else { 6320910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 6330910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 634316643e7SJed Brown } 6354a6899ffSJed Brown } 6360910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 637316643e7SJed Brown PetscFunctionReturn(0); 638316643e7SJed Brown } 639316643e7SJed Brown 640316643e7SJed Brown #undef __FUNCT__ 641316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 642316643e7SJed Brown /*@ 643316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 644316643e7SJed Brown 645316643e7SJed Brown Collective on TS and Vec 646316643e7SJed Brown 647316643e7SJed Brown Input 648316643e7SJed Brown Input Parameters: 649316643e7SJed Brown + ts - the TS context 650316643e7SJed Brown . t - current timestep 6510910c330SBarry Smith . U - state vector 6520910c330SBarry Smith . Udot - time derivative of state vector 653214bc6a2SJed Brown . shift - shift to apply, see note below 654214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 655316643e7SJed Brown 656316643e7SJed Brown Output Parameters: 657316643e7SJed Brown + A - Jacobian matrix 658316643e7SJed Brown . B - optional preconditioning matrix 659316643e7SJed Brown - flag - flag indicating matrix structure 660316643e7SJed Brown 661316643e7SJed Brown Notes: 6620910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 663316643e7SJed Brown 6640910c330SBarry Smith dF/dU + shift*dF/dUdot 665316643e7SJed Brown 666316643e7SJed Brown Most users should not need to explicitly call this routine, as it 667316643e7SJed Brown is used internally within the nonlinear solvers. 668316643e7SJed Brown 669316643e7SJed Brown Level: developer 670316643e7SJed Brown 671316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 672316643e7SJed Brown 673316643e7SJed Brown .seealso: TSSetIJacobian() 67463495f91SJed Brown @*/ 6750910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex) 676316643e7SJed Brown { 6770910c330SBarry Smith PetscInt Ustate, Udotstate; 678316643e7SJed Brown PetscErrorCode ierr; 67924989b8cSPeter Brune TSIJacobian ijacobian; 68024989b8cSPeter Brune TSRHSJacobian rhsjacobian; 68124989b8cSPeter Brune DM dm; 68224989b8cSPeter Brune void *ctx; 683316643e7SJed Brown 684316643e7SJed Brown PetscFunctionBegin; 6850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6860910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6870910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 688316643e7SJed Brown PetscValidPointer(A,6); 6890700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,6); 690316643e7SJed Brown PetscValidPointer(B,7); 6910700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,7); 692316643e7SJed Brown PetscValidPointer(flg,8); 69324989b8cSPeter Brune 69424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 69524989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 6960298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 69724989b8cSPeter Brune 6980910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr); 6990910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr); 7000910c330SBarry Smith if (ts->ijacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->ijacobian.X == U && ts->ijacobian.Xstate == Ustate && ts->ijacobian.Xdot == Udot && ts->ijacobian.Xdotstate == Udotstate && ts->ijacobian.imex == imex))) { 7010026cea9SSean Farley *flg = ts->ijacobian.mstructure; 7020026cea9SSean Farley ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 7030026cea9SSean Farley PetscFunctionReturn(0); 7040026cea9SSean Farley } 705316643e7SJed Brown 706ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 707316643e7SJed Brown 7084e684422SJed Brown *flg = SAME_NONZERO_PATTERN; /* In case we're solving a linear problem in which case it wouldn't get initialized below. */ 7090910c330SBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 71024989b8cSPeter Brune if (ijacobian) { 7112dd45cf8SJed Brown *flg = DIFFERENT_NONZERO_PATTERN; 712316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 7130910c330SBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr); 714316643e7SJed Brown PetscStackPop; 715214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 716214bc6a2SJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 717214bc6a2SJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 7184a6899ffSJed Brown } 719214bc6a2SJed Brown if (imex) { 720b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 721214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 7222dd45cf8SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 723214bc6a2SJed Brown if (*A != *B) { 724214bc6a2SJed Brown ierr = MatZeroEntries(*B);CHKERRQ(ierr); 725214bc6a2SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 726214bc6a2SJed Brown } 727214bc6a2SJed Brown *flg = SAME_PRECONDITIONER; 728214bc6a2SJed Brown } 729214bc6a2SJed Brown } else { 73024989b8cSPeter Brune if (!ijacobian) { 7310910c330SBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,A,B,flg);CHKERRQ(ierr); 732214bc6a2SJed Brown ierr = MatScale(*A,-1);CHKERRQ(ierr); 733214bc6a2SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 734316643e7SJed Brown if (*A != *B) { 735316643e7SJed Brown ierr = MatScale(*B,-1);CHKERRQ(ierr); 736316643e7SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 737316643e7SJed Brown } 73824989b8cSPeter Brune } else if (rhsjacobian) { 739214bc6a2SJed Brown Mat Arhs,Brhs; 740214bc6a2SJed Brown MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN; 741214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 7420910c330SBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 743214bc6a2SJed Brown axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN; 744214bc6a2SJed Brown ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr); 745214bc6a2SJed Brown if (*A != *B) { 746214bc6a2SJed Brown ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr); 747214bc6a2SJed Brown } 748214bc6a2SJed Brown *flg = PetscMin(*flg,flg2); 749214bc6a2SJed Brown } 750316643e7SJed Brown } 7510026cea9SSean Farley 7520026cea9SSean Farley ts->ijacobian.time = t; 7530910c330SBarry Smith ts->ijacobian.X = U; 7540910c330SBarry Smith ts->ijacobian.Xdot = Udot; 755bbd56ea5SKarl Rupp 7560910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr); 7570910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr); 758bbd56ea5SKarl Rupp 7590026cea9SSean Farley ts->ijacobian.shift = shift; 7600026cea9SSean Farley ts->ijacobian.imex = imex; 7610026cea9SSean Farley ts->ijacobian.mstructure = *flg; 762bbd56ea5SKarl Rupp 7630910c330SBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 764316643e7SJed Brown PetscFunctionReturn(0); 765316643e7SJed Brown } 766316643e7SJed Brown 767316643e7SJed Brown #undef __FUNCT__ 7684a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 769d763cef2SBarry Smith /*@C 770d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 771b5abc632SBarry Smith where U_t = G(t,u). 772d763cef2SBarry Smith 7733f9fe445SBarry Smith Logically Collective on TS 774d763cef2SBarry Smith 775d763cef2SBarry Smith Input Parameters: 776d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 7770298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 778d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 779d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 7800298fd71SBarry Smith function evaluation routine (may be NULL) 781d763cef2SBarry Smith 782d763cef2SBarry Smith Calling sequence of func: 78387828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 784d763cef2SBarry Smith 785d763cef2SBarry Smith + t - current timestep 786d763cef2SBarry Smith . u - input vector 787d763cef2SBarry Smith . F - function vector 788d763cef2SBarry Smith - ctx - [optional] user-defined function context 789d763cef2SBarry Smith 790d763cef2SBarry Smith Level: beginner 791d763cef2SBarry Smith 792d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 793d763cef2SBarry Smith 794d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian() 795d763cef2SBarry Smith @*/ 796089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 797d763cef2SBarry Smith { 798089b2837SJed Brown PetscErrorCode ierr; 799089b2837SJed Brown SNES snes; 8000298fd71SBarry Smith Vec ralloc = NULL; 80124989b8cSPeter Brune DM dm; 802d763cef2SBarry Smith 803089b2837SJed Brown PetscFunctionBegin; 8040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 805ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 80624989b8cSPeter Brune 80724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 80824989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 809089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 810e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 811e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 812e856ceecSJed Brown r = ralloc; 813e856ceecSJed Brown } 814089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 815e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 816d763cef2SBarry Smith PetscFunctionReturn(0); 817d763cef2SBarry Smith } 818d763cef2SBarry Smith 8194a2ae208SSatish Balay #undef __FUNCT__ 820ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 821ef20d060SBarry Smith /*@C 822abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 823ef20d060SBarry Smith 824ef20d060SBarry Smith Logically Collective on TS 825ef20d060SBarry Smith 826ef20d060SBarry Smith Input Parameters: 827ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 828ef20d060SBarry Smith . f - routine for evaluating the solution 829ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 8300298fd71SBarry Smith function evaluation routine (may be NULL) 831ef20d060SBarry Smith 832ef20d060SBarry Smith Calling sequence of func: 833ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 834ef20d060SBarry Smith 835ef20d060SBarry Smith + t - current timestep 836ef20d060SBarry Smith . u - output vector 837ef20d060SBarry Smith - ctx - [optional] user-defined function context 838ef20d060SBarry Smith 839abd5a294SJed Brown Notes: 840abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 841abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 842abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 843abd5a294SJed Brown 8444f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 845abd5a294SJed Brown 846ef20d060SBarry Smith Level: beginner 847ef20d060SBarry Smith 848ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 849ef20d060SBarry Smith 8509b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 851ef20d060SBarry Smith @*/ 852ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 853ef20d060SBarry Smith { 854ef20d060SBarry Smith PetscErrorCode ierr; 855ef20d060SBarry Smith DM dm; 856ef20d060SBarry Smith 857ef20d060SBarry Smith PetscFunctionBegin; 858ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 859ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 860ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 861ef20d060SBarry Smith PetscFunctionReturn(0); 862ef20d060SBarry Smith } 863ef20d060SBarry Smith 864ef20d060SBarry Smith #undef __FUNCT__ 8659b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction" 8669b7cd975SBarry Smith /*@C 8679b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 8689b7cd975SBarry Smith 8699b7cd975SBarry Smith Logically Collective on TS 8709b7cd975SBarry Smith 8719b7cd975SBarry Smith Input Parameters: 8729b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 8739b7cd975SBarry Smith . f - routine for evaluating the forcing function 8749b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 8750298fd71SBarry Smith function evaluation routine (may be NULL) 8769b7cd975SBarry Smith 8779b7cd975SBarry Smith Calling sequence of func: 8789b7cd975SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 8799b7cd975SBarry Smith 8809b7cd975SBarry Smith + t - current timestep 8819b7cd975SBarry Smith . u - output vector 8829b7cd975SBarry Smith - ctx - [optional] user-defined function context 8839b7cd975SBarry Smith 8849b7cd975SBarry Smith Notes: 8859b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 8869b7cd975SBarry Smith create closed-form solutions with a non-physical forcing term. 8879b7cd975SBarry Smith 8889b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 8899b7cd975SBarry Smith 8909b7cd975SBarry Smith Level: beginner 8919b7cd975SBarry Smith 8929b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 8939b7cd975SBarry Smith 8949b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 8959b7cd975SBarry Smith @*/ 8969b7cd975SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 8979b7cd975SBarry Smith { 8989b7cd975SBarry Smith PetscErrorCode ierr; 8999b7cd975SBarry Smith DM dm; 9009b7cd975SBarry Smith 9019b7cd975SBarry Smith PetscFunctionBegin; 9029b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9039b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 9049b7cd975SBarry Smith ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr); 9059b7cd975SBarry Smith PetscFunctionReturn(0); 9069b7cd975SBarry Smith } 9079b7cd975SBarry Smith 9089b7cd975SBarry Smith #undef __FUNCT__ 9094a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 910d763cef2SBarry Smith /*@C 911d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 912b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 913d763cef2SBarry Smith 9143f9fe445SBarry Smith Logically Collective on TS 915d763cef2SBarry Smith 916d763cef2SBarry Smith Input Parameters: 917d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 918d763cef2SBarry Smith . A - Jacobian matrix 919d763cef2SBarry Smith . B - preconditioner matrix (usually same as A) 920d763cef2SBarry Smith . f - the Jacobian evaluation routine 921d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 9220298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 923d763cef2SBarry Smith 924d763cef2SBarry Smith Calling sequence of func: 92587828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 926d763cef2SBarry Smith 927d763cef2SBarry Smith + t - current timestep 928d763cef2SBarry Smith . u - input vector 929d763cef2SBarry Smith . A - matrix A, where U_t = A(t)u 930d763cef2SBarry Smith . B - preconditioner matrix, usually the same as A 931d763cef2SBarry Smith . flag - flag indicating information about the preconditioner matrix 93294b7f48cSBarry Smith structure (same as flag in KSPSetOperators()) 933d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 934d763cef2SBarry Smith 935d763cef2SBarry Smith Notes: 93694b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 937d763cef2SBarry Smith output parameter in the routine func(). Be sure to read this information! 938d763cef2SBarry Smith 939d763cef2SBarry Smith The routine func() takes Mat * as the matrix arguments rather than Mat. 940d763cef2SBarry Smith This allows the matrix evaluation routine to replace A and/or B with a 94156335db2SHong Zhang completely new matrix structure (not just different matrix elements) 942d763cef2SBarry Smith when appropriate, for instance, if the nonzero structure is changing 943d763cef2SBarry Smith throughout the global iterations. 944d763cef2SBarry Smith 945d763cef2SBarry Smith Level: beginner 946d763cef2SBarry Smith 947d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 948d763cef2SBarry Smith 949ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction() 950d763cef2SBarry Smith 951d763cef2SBarry Smith @*/ 952089b2837SJed Brown PetscErrorCode TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx) 953d763cef2SBarry Smith { 954277b19d0SLisandro Dalcin PetscErrorCode ierr; 955089b2837SJed Brown SNES snes; 95624989b8cSPeter Brune DM dm; 95724989b8cSPeter Brune TSIJacobian ijacobian; 958277b19d0SLisandro Dalcin 959d763cef2SBarry Smith PetscFunctionBegin; 9600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 961277b19d0SLisandro Dalcin if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 962277b19d0SLisandro Dalcin if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 963277b19d0SLisandro Dalcin if (A) PetscCheckSameComm(ts,1,A,2); 964277b19d0SLisandro Dalcin if (B) PetscCheckSameComm(ts,1,B,3); 965d763cef2SBarry Smith 96624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 96724989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 9680298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 96924989b8cSPeter Brune 970089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 9715f659677SPeter Brune if (!ijacobian) { 972089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 9730e4ef248SJed Brown } 9740e4ef248SJed Brown if (A) { 9750e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 9760e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 977bbd56ea5SKarl Rupp 9780e4ef248SJed Brown ts->Arhs = A; 9790e4ef248SJed Brown } 9800e4ef248SJed Brown if (B) { 9810e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 9820e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 983bbd56ea5SKarl Rupp 9840e4ef248SJed Brown ts->Brhs = B; 9850e4ef248SJed Brown } 986d763cef2SBarry Smith PetscFunctionReturn(0); 987d763cef2SBarry Smith } 988d763cef2SBarry Smith 989316643e7SJed Brown 990316643e7SJed Brown #undef __FUNCT__ 991316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 992316643e7SJed Brown /*@C 993b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 994316643e7SJed Brown 9953f9fe445SBarry Smith Logically Collective on TS 996316643e7SJed Brown 997316643e7SJed Brown Input Parameters: 998316643e7SJed Brown + ts - the TS context obtained from TSCreate() 9990298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1000316643e7SJed Brown . f - the function evaluation routine 10010298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1002316643e7SJed Brown 1003316643e7SJed Brown Calling sequence of f: 1004316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1005316643e7SJed Brown 1006316643e7SJed Brown + t - time at step/stage being solved 1007316643e7SJed Brown . u - state vector 1008316643e7SJed Brown . u_t - time derivative of state vector 1009316643e7SJed Brown . F - function vector 1010316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1011316643e7SJed Brown 1012316643e7SJed Brown Important: 1013d6cbdb99SBarry Smith The user MUST call either this routine, TSSetRHSFunction(). This routine must be used when not solving an ODE, for example a DAE. 1014316643e7SJed Brown 1015316643e7SJed Brown Level: beginner 1016316643e7SJed Brown 1017316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1018316643e7SJed Brown 1019d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1020316643e7SJed Brown @*/ 1021089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 1022316643e7SJed Brown { 1023089b2837SJed Brown PetscErrorCode ierr; 1024089b2837SJed Brown SNES snes; 10250298fd71SBarry Smith Vec resalloc = NULL; 102624989b8cSPeter Brune DM dm; 1027316643e7SJed Brown 1028316643e7SJed Brown PetscFunctionBegin; 10290700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1030ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 103124989b8cSPeter Brune 103224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 103324989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 103424989b8cSPeter Brune 1035089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1036e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 1037e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 1038e856ceecSJed Brown res = resalloc; 1039e856ceecSJed Brown } 1040089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 1041e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 1042089b2837SJed Brown PetscFunctionReturn(0); 1043089b2837SJed Brown } 1044089b2837SJed Brown 1045089b2837SJed Brown #undef __FUNCT__ 1046089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 1047089b2837SJed Brown /*@C 1048089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1049089b2837SJed Brown 1050089b2837SJed Brown Not Collective 1051089b2837SJed Brown 1052089b2837SJed Brown Input Parameter: 1053089b2837SJed Brown . ts - the TS context 1054089b2837SJed Brown 1055089b2837SJed Brown Output Parameter: 10560298fd71SBarry Smith + r - vector to hold residual (or NULL) 10570298fd71SBarry Smith . func - the function to compute residual (or NULL) 10580298fd71SBarry Smith - ctx - the function context (or NULL) 1059089b2837SJed Brown 1060089b2837SJed Brown Level: advanced 1061089b2837SJed Brown 1062089b2837SJed Brown .keywords: TS, nonlinear, get, function 1063089b2837SJed Brown 1064089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1065089b2837SJed Brown @*/ 1066089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1067089b2837SJed Brown { 1068089b2837SJed Brown PetscErrorCode ierr; 1069089b2837SJed Brown SNES snes; 107024989b8cSPeter Brune DM dm; 1071089b2837SJed Brown 1072089b2837SJed Brown PetscFunctionBegin; 1073089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1074089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 10750298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 107624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 107724989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1078089b2837SJed Brown PetscFunctionReturn(0); 1079089b2837SJed Brown } 1080089b2837SJed Brown 1081089b2837SJed Brown #undef __FUNCT__ 1082089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 1083089b2837SJed Brown /*@C 1084089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1085089b2837SJed Brown 1086089b2837SJed Brown Not Collective 1087089b2837SJed Brown 1088089b2837SJed Brown Input Parameter: 1089089b2837SJed Brown . ts - the TS context 1090089b2837SJed Brown 1091089b2837SJed Brown Output Parameter: 10920298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 10930298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 10940298fd71SBarry Smith - ctx - the function context (or NULL) 1095089b2837SJed Brown 1096089b2837SJed Brown Level: advanced 1097089b2837SJed Brown 1098089b2837SJed Brown .keywords: TS, nonlinear, get, function 1099089b2837SJed Brown 1100089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 1101089b2837SJed Brown @*/ 1102089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1103089b2837SJed Brown { 1104089b2837SJed Brown PetscErrorCode ierr; 1105089b2837SJed Brown SNES snes; 110624989b8cSPeter Brune DM dm; 1107089b2837SJed Brown 1108089b2837SJed Brown PetscFunctionBegin; 1109089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1110089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11110298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 111224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 111324989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1114316643e7SJed Brown PetscFunctionReturn(0); 1115316643e7SJed Brown } 1116316643e7SJed Brown 1117316643e7SJed Brown #undef __FUNCT__ 1118316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 1119316643e7SJed Brown /*@C 1120a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1121a4f0a591SBarry Smith you provided with TSSetIFunction(). 1122316643e7SJed Brown 11233f9fe445SBarry Smith Logically Collective on TS 1124316643e7SJed Brown 1125316643e7SJed Brown Input Parameters: 1126316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1127316643e7SJed Brown . A - Jacobian matrix 1128316643e7SJed Brown . B - preconditioning matrix for A (may be same as A) 1129316643e7SJed Brown . f - the Jacobian evaluation routine 11300298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1131316643e7SJed Brown 1132316643e7SJed Brown Calling sequence of f: 11331b4a444bSJed Brown $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx); 1134316643e7SJed Brown 1135316643e7SJed Brown + t - time at step/stage being solved 11361b4a444bSJed Brown . U - state vector 11371b4a444bSJed Brown . U_t - time derivative of state vector 1138316643e7SJed Brown . a - shift 1139b5abc632SBarry Smith . A - Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1140316643e7SJed Brown . B - preconditioning matrix for A, may be same as A 1141316643e7SJed Brown . flag - flag indicating information about the preconditioner matrix 1142316643e7SJed Brown structure (same as flag in KSPSetOperators()) 1143316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1144316643e7SJed Brown 1145316643e7SJed Brown Notes: 1146316643e7SJed Brown The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve. 1147316643e7SJed Brown 1148a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1149b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1150a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1151a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1152a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1153a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1154a4f0a591SBarry Smith 1155316643e7SJed Brown Level: beginner 1156316643e7SJed Brown 1157316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1158316643e7SJed Brown 1159ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian() 1160316643e7SJed Brown 1161316643e7SJed Brown @*/ 11627087cfbeSBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx) 1163316643e7SJed Brown { 1164316643e7SJed Brown PetscErrorCode ierr; 1165089b2837SJed Brown SNES snes; 116624989b8cSPeter Brune DM dm; 1167316643e7SJed Brown 1168316643e7SJed Brown PetscFunctionBegin; 11690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11700700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 11710700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 1172316643e7SJed Brown if (A) PetscCheckSameComm(ts,1,A,2); 1173316643e7SJed Brown if (B) PetscCheckSameComm(ts,1,B,3); 117424989b8cSPeter Brune 117524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 117624989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 117724989b8cSPeter Brune 1178089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1179089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1180316643e7SJed Brown PetscFunctionReturn(0); 1181316643e7SJed Brown } 1182316643e7SJed Brown 11834a2ae208SSatish Balay #undef __FUNCT__ 118455849f57SBarry Smith #define __FUNCT__ "TSLoad" 118555849f57SBarry Smith /*@C 118655849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 118755849f57SBarry Smith 118855849f57SBarry Smith Collective on PetscViewer 118955849f57SBarry Smith 119055849f57SBarry Smith Input Parameters: 119155849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 119255849f57SBarry Smith some related function before a call to TSLoad(). 119355849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 119455849f57SBarry Smith 119555849f57SBarry Smith Level: intermediate 119655849f57SBarry Smith 119755849f57SBarry Smith Notes: 119855849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 119955849f57SBarry Smith 120055849f57SBarry Smith Notes for advanced users: 120155849f57SBarry Smith Most users should not need to know the details of the binary storage 120255849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 120355849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 120455849f57SBarry Smith format is 120555849f57SBarry Smith .vb 120655849f57SBarry Smith has not yet been determined 120755849f57SBarry Smith .ve 120855849f57SBarry Smith 120955849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 121055849f57SBarry Smith @*/ 1211f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 121255849f57SBarry Smith { 121355849f57SBarry Smith PetscErrorCode ierr; 121455849f57SBarry Smith PetscBool isbinary; 121555849f57SBarry Smith PetscInt classid; 121655849f57SBarry Smith char type[256]; 12172d53ad75SBarry Smith DMTS sdm; 1218ad6bc421SBarry Smith DM dm; 121955849f57SBarry Smith 122055849f57SBarry Smith PetscFunctionBegin; 1221f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 122255849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 122355849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 122455849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 122555849f57SBarry Smith 122655849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 1227ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 122855849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 1229f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1230f2c2a1b9SBarry Smith if (ts->ops->load) { 1231f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1232f2c2a1b9SBarry Smith } 1233ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1234ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1235ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1236f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1237f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 12382d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 12392d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 124055849f57SBarry Smith PetscFunctionReturn(0); 124155849f57SBarry Smith } 124255849f57SBarry Smith 12439804daf3SBarry Smith #include <petscdraw.h> 124455849f57SBarry Smith #undef __FUNCT__ 12454a2ae208SSatish Balay #define __FUNCT__ "TSView" 12467e2c5f70SBarry Smith /*@C 1247d763cef2SBarry Smith TSView - Prints the TS data structure. 1248d763cef2SBarry Smith 12494c49b128SBarry Smith Collective on TS 1250d763cef2SBarry Smith 1251d763cef2SBarry Smith Input Parameters: 1252d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1253d763cef2SBarry Smith - viewer - visualization context 1254d763cef2SBarry Smith 1255d763cef2SBarry Smith Options Database Key: 1256d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1257d763cef2SBarry Smith 1258d763cef2SBarry Smith Notes: 1259d763cef2SBarry Smith The available visualization contexts include 1260b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1261b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1262d763cef2SBarry Smith output where only the first processor opens 1263d763cef2SBarry Smith the file. All other processors send their 1264d763cef2SBarry Smith data to the first processor to print. 1265d763cef2SBarry Smith 1266d763cef2SBarry Smith The user can open an alternative visualization context with 1267b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1268d763cef2SBarry Smith 1269d763cef2SBarry Smith Level: beginner 1270d763cef2SBarry Smith 1271d763cef2SBarry Smith .keywords: TS, timestep, view 1272d763cef2SBarry Smith 1273b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1274d763cef2SBarry Smith @*/ 12757087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1276d763cef2SBarry Smith { 1277dfbe8321SBarry Smith PetscErrorCode ierr; 127819fd82e9SBarry Smith TSType type; 12792b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 12802d53ad75SBarry Smith DMTS sdm; 1281d763cef2SBarry Smith 1282d763cef2SBarry Smith PetscFunctionBegin; 12830700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 12843050cee2SBarry Smith if (!viewer) { 1285ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 12863050cee2SBarry Smith } 12870700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1288c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1289fd16b177SBarry Smith 1290251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1291251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 129255849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 12932b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 129432077d6dSBarry Smith if (iascii) { 1295317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr); 129677431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 1297a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%G\n",ts->max_time);CHKERRQ(ierr); 1298d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 12995ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1300c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1301d763cef2SBarry Smith } 13025ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1303193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 13042d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 13052d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1306d52bd9f3SBarry Smith if (ts->ops->view) { 1307d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1308d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1309d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1310d52bd9f3SBarry Smith } 13110f5bd95cSBarry Smith } else if (isstring) { 1312a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1313b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 131455849f57SBarry Smith } else if (isbinary) { 131555849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 131655849f57SBarry Smith MPI_Comm comm; 131755849f57SBarry Smith PetscMPIInt rank; 131855849f57SBarry Smith char type[256]; 131955849f57SBarry Smith 132055849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 132155849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 132255849f57SBarry Smith if (!rank) { 132355849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 132455849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 132555849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 132655849f57SBarry Smith } 132755849f57SBarry Smith if (ts->ops->view) { 132855849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 132955849f57SBarry Smith } 1330f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1331f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 13322d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 13332d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 13342b0a91c0SBarry Smith } else if (isdraw) { 13352b0a91c0SBarry Smith PetscDraw draw; 13362b0a91c0SBarry Smith char str[36]; 133789fd9fafSBarry Smith PetscReal x,y,bottom,h; 13382b0a91c0SBarry Smith 13392b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 13402b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 13412b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 13422b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 13430298fd71SBarry Smith ierr = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 134489fd9fafSBarry Smith bottom = y - h; 13452b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 13462b0a91c0SBarry Smith if (ts->ops->view) { 13472b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 13482b0a91c0SBarry Smith } 13492b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1350d763cef2SBarry Smith } 1351b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1352251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1353b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1354d763cef2SBarry Smith PetscFunctionReturn(0); 1355d763cef2SBarry Smith } 1356d763cef2SBarry Smith 1357d763cef2SBarry Smith 13584a2ae208SSatish Balay #undef __FUNCT__ 13594a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1360b07ff414SBarry Smith /*@ 1361d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1362d763cef2SBarry Smith the timesteppers. 1363d763cef2SBarry Smith 13643f9fe445SBarry Smith Logically Collective on TS 1365d763cef2SBarry Smith 1366d763cef2SBarry Smith Input Parameters: 1367d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1368d763cef2SBarry Smith - usrP - optional user context 1369d763cef2SBarry Smith 1370d763cef2SBarry Smith Level: intermediate 1371d763cef2SBarry Smith 1372d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1373d763cef2SBarry Smith 1374d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1375d763cef2SBarry Smith @*/ 13767087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1377d763cef2SBarry Smith { 1378d763cef2SBarry Smith PetscFunctionBegin; 13790700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1380d763cef2SBarry Smith ts->user = usrP; 1381d763cef2SBarry Smith PetscFunctionReturn(0); 1382d763cef2SBarry Smith } 1383d763cef2SBarry Smith 13844a2ae208SSatish Balay #undef __FUNCT__ 13854a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1386b07ff414SBarry Smith /*@ 1387d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1388d763cef2SBarry Smith timestepper. 1389d763cef2SBarry Smith 1390d763cef2SBarry Smith Not Collective 1391d763cef2SBarry Smith 1392d763cef2SBarry Smith Input Parameter: 1393d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1394d763cef2SBarry Smith 1395d763cef2SBarry Smith Output Parameter: 1396d763cef2SBarry Smith . usrP - user context 1397d763cef2SBarry Smith 1398d763cef2SBarry Smith Level: intermediate 1399d763cef2SBarry Smith 1400d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1401d763cef2SBarry Smith 1402d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1403d763cef2SBarry Smith @*/ 1404e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1405d763cef2SBarry Smith { 1406d763cef2SBarry Smith PetscFunctionBegin; 14070700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1408e71120c6SJed Brown *(void**)usrP = ts->user; 1409d763cef2SBarry Smith PetscFunctionReturn(0); 1410d763cef2SBarry Smith } 1411d763cef2SBarry Smith 14124a2ae208SSatish Balay #undef __FUNCT__ 14134a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1414d763cef2SBarry Smith /*@ 1415b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1416d763cef2SBarry Smith 1417d763cef2SBarry Smith Not Collective 1418d763cef2SBarry Smith 1419d763cef2SBarry Smith Input Parameter: 1420d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1421d763cef2SBarry Smith 1422d763cef2SBarry Smith Output Parameter: 1423b8123daeSJed Brown . iter - number of steps completed so far 1424d763cef2SBarry Smith 1425d763cef2SBarry Smith Level: intermediate 1426d763cef2SBarry Smith 1427d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 1428b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep() 1429d763cef2SBarry Smith @*/ 14307087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 1431d763cef2SBarry Smith { 1432d763cef2SBarry Smith PetscFunctionBegin; 14330700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14344482741eSBarry Smith PetscValidIntPointer(iter,2); 1435d763cef2SBarry Smith *iter = ts->steps; 1436d763cef2SBarry Smith PetscFunctionReturn(0); 1437d763cef2SBarry Smith } 1438d763cef2SBarry Smith 14394a2ae208SSatish Balay #undef __FUNCT__ 14404a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1441d763cef2SBarry Smith /*@ 1442d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1443d763cef2SBarry Smith as well as the initial time. 1444d763cef2SBarry Smith 14453f9fe445SBarry Smith Logically Collective on TS 1446d763cef2SBarry Smith 1447d763cef2SBarry Smith Input Parameters: 1448d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1449d763cef2SBarry Smith . initial_time - the initial time 1450d763cef2SBarry Smith - time_step - the size of the timestep 1451d763cef2SBarry Smith 1452d763cef2SBarry Smith Level: intermediate 1453d763cef2SBarry Smith 1454d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1455d763cef2SBarry Smith 1456d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1457d763cef2SBarry Smith @*/ 14587087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1459d763cef2SBarry Smith { 1460e144a568SJed Brown PetscErrorCode ierr; 1461e144a568SJed Brown 1462d763cef2SBarry Smith PetscFunctionBegin; 14630700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1464e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1465d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1466d763cef2SBarry Smith PetscFunctionReturn(0); 1467d763cef2SBarry Smith } 1468d763cef2SBarry Smith 14694a2ae208SSatish Balay #undef __FUNCT__ 14704a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1471d763cef2SBarry Smith /*@ 1472d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1473d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1474d763cef2SBarry Smith 14753f9fe445SBarry Smith Logically Collective on TS 1476d763cef2SBarry Smith 1477d763cef2SBarry Smith Input Parameters: 1478d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1479d763cef2SBarry Smith - time_step - the size of the timestep 1480d763cef2SBarry Smith 1481d763cef2SBarry Smith Level: intermediate 1482d763cef2SBarry Smith 1483d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1484d763cef2SBarry Smith 1485d763cef2SBarry Smith .keywords: TS, set, timestep 1486d763cef2SBarry Smith @*/ 14877087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1488d763cef2SBarry Smith { 1489d763cef2SBarry Smith PetscFunctionBegin; 14900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1491c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1492d763cef2SBarry Smith ts->time_step = time_step; 149331748224SBarry Smith ts->time_step_orig = time_step; 1494d763cef2SBarry Smith PetscFunctionReturn(0); 1495d763cef2SBarry Smith } 1496d763cef2SBarry Smith 14974a2ae208SSatish Balay #undef __FUNCT__ 1498a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1499a43b19c4SJed Brown /*@ 150049354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 150149354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 150249354f04SShri Abhyankar or just return at the final time TS computed. 1503a43b19c4SJed Brown 1504a43b19c4SJed Brown Logically Collective on TS 1505a43b19c4SJed Brown 1506a43b19c4SJed Brown Input Parameter: 1507a43b19c4SJed Brown + ts - the time-step context 150849354f04SShri Abhyankar - eftopt - exact final time option 1509a43b19c4SJed Brown 1510a43b19c4SJed Brown Level: beginner 1511a43b19c4SJed Brown 1512a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 1513a43b19c4SJed Brown @*/ 151449354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 1515a43b19c4SJed Brown { 1516a43b19c4SJed Brown PetscFunctionBegin; 1517a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 151849354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 151949354f04SShri Abhyankar ts->exact_final_time = eftopt; 1520a43b19c4SJed Brown PetscFunctionReturn(0); 1521a43b19c4SJed Brown } 1522a43b19c4SJed Brown 1523a43b19c4SJed Brown #undef __FUNCT__ 15244a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1525d763cef2SBarry Smith /*@ 1526d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1527d763cef2SBarry Smith 1528d763cef2SBarry Smith Not Collective 1529d763cef2SBarry Smith 1530d763cef2SBarry Smith Input Parameter: 1531d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1532d763cef2SBarry Smith 1533d763cef2SBarry Smith Output Parameter: 1534d763cef2SBarry Smith . dt - the current timestep size 1535d763cef2SBarry Smith 1536d763cef2SBarry Smith Level: intermediate 1537d763cef2SBarry Smith 1538d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1539d763cef2SBarry Smith 1540d763cef2SBarry Smith .keywords: TS, get, timestep 1541d763cef2SBarry Smith @*/ 15427087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 1543d763cef2SBarry Smith { 1544d763cef2SBarry Smith PetscFunctionBegin; 15450700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1546f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1547d763cef2SBarry Smith *dt = ts->time_step; 1548d763cef2SBarry Smith PetscFunctionReturn(0); 1549d763cef2SBarry Smith } 1550d763cef2SBarry Smith 15514a2ae208SSatish Balay #undef __FUNCT__ 15524a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1553d8e5e3e6SSatish Balay /*@ 1554d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1555d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1556d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1557d763cef2SBarry Smith the solution at the next timestep has been calculated. 1558d763cef2SBarry Smith 1559d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1560d763cef2SBarry Smith 1561d763cef2SBarry Smith Input Parameter: 1562d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1563d763cef2SBarry Smith 1564d763cef2SBarry Smith Output Parameter: 1565d763cef2SBarry Smith . v - the vector containing the solution 1566d763cef2SBarry Smith 1567d763cef2SBarry Smith Level: intermediate 1568d763cef2SBarry Smith 1569d763cef2SBarry Smith .seealso: TSGetTimeStep() 1570d763cef2SBarry Smith 1571d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1572d763cef2SBarry Smith @*/ 15737087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1574d763cef2SBarry Smith { 1575d763cef2SBarry Smith PetscFunctionBegin; 15760700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15774482741eSBarry Smith PetscValidPointer(v,2); 15788737fe31SLisandro Dalcin *v = ts->vec_sol; 1579d763cef2SBarry Smith PetscFunctionReturn(0); 1580d763cef2SBarry Smith } 1581d763cef2SBarry Smith 1582bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 15834a2ae208SSatish Balay #undef __FUNCT__ 1584bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1585d8e5e3e6SSatish Balay /*@ 1586bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1587d763cef2SBarry Smith 1588bdad233fSMatthew Knepley Not collective 1589d763cef2SBarry Smith 1590bdad233fSMatthew Knepley Input Parameters: 1591bdad233fSMatthew Knepley + ts - The TS 1592bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1593d763cef2SBarry Smith .vb 15940910c330SBarry Smith U_t - A U = 0 (linear) 15950910c330SBarry Smith U_t - A(t) U = 0 (linear) 15960910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 1597d763cef2SBarry Smith .ve 1598d763cef2SBarry Smith 1599d763cef2SBarry Smith Level: beginner 1600d763cef2SBarry Smith 1601bdad233fSMatthew Knepley .keywords: TS, problem type 1602bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1603d763cef2SBarry Smith @*/ 16047087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1605a7cc72afSBarry Smith { 16069e2a6581SJed Brown PetscErrorCode ierr; 16079e2a6581SJed Brown 1608d763cef2SBarry Smith PetscFunctionBegin; 16090700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1610bdad233fSMatthew Knepley ts->problem_type = type; 16119e2a6581SJed Brown if (type == TS_LINEAR) { 16129e2a6581SJed Brown SNES snes; 16139e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 16149e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 16159e2a6581SJed Brown } 1616d763cef2SBarry Smith PetscFunctionReturn(0); 1617d763cef2SBarry Smith } 1618d763cef2SBarry Smith 1619bdad233fSMatthew Knepley #undef __FUNCT__ 1620bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1621bdad233fSMatthew Knepley /*@C 1622bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1623bdad233fSMatthew Knepley 1624bdad233fSMatthew Knepley Not collective 1625bdad233fSMatthew Knepley 1626bdad233fSMatthew Knepley Input Parameter: 1627bdad233fSMatthew Knepley . ts - The TS 1628bdad233fSMatthew Knepley 1629bdad233fSMatthew Knepley Output Parameter: 1630bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1631bdad233fSMatthew Knepley .vb 1632089b2837SJed Brown M U_t = A U 1633089b2837SJed Brown M(t) U_t = A(t) U 1634b5abc632SBarry Smith F(t,U,U_t) 1635bdad233fSMatthew Knepley .ve 1636bdad233fSMatthew Knepley 1637bdad233fSMatthew Knepley Level: beginner 1638bdad233fSMatthew Knepley 1639bdad233fSMatthew Knepley .keywords: TS, problem type 1640bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1641bdad233fSMatthew Knepley @*/ 16427087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1643a7cc72afSBarry Smith { 1644bdad233fSMatthew Knepley PetscFunctionBegin; 16450700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 16464482741eSBarry Smith PetscValidIntPointer(type,2); 1647bdad233fSMatthew Knepley *type = ts->problem_type; 1648bdad233fSMatthew Knepley PetscFunctionReturn(0); 1649bdad233fSMatthew Knepley } 1650d763cef2SBarry Smith 16514a2ae208SSatish Balay #undef __FUNCT__ 16524a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1653d763cef2SBarry Smith /*@ 1654d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1655d763cef2SBarry Smith of a timestepper. 1656d763cef2SBarry Smith 1657d763cef2SBarry Smith Collective on TS 1658d763cef2SBarry Smith 1659d763cef2SBarry Smith Input Parameter: 1660d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1661d763cef2SBarry Smith 1662d763cef2SBarry Smith Notes: 1663d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1664d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1665d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1666d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1667d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1668d763cef2SBarry Smith 1669d763cef2SBarry Smith Level: advanced 1670d763cef2SBarry Smith 1671d763cef2SBarry Smith .keywords: TS, timestep, setup 1672d763cef2SBarry Smith 1673d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1674d763cef2SBarry Smith @*/ 16757087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1676d763cef2SBarry Smith { 1677dfbe8321SBarry Smith PetscErrorCode ierr; 16786c6b9e74SPeter Brune DM dm; 16796c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 16806c6b9e74SPeter Brune PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*); 16816c6b9e74SPeter Brune TSIJacobian ijac; 16826c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1683d763cef2SBarry Smith 1684d763cef2SBarry Smith PetscFunctionBegin; 16850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1686277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1687277b19d0SLisandro Dalcin 16887adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 16899596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1690d763cef2SBarry Smith } 1691277b19d0SLisandro Dalcin 1692277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1693277b19d0SLisandro Dalcin 1694ad6bc421SBarry Smith ierr = TSGetTSAdapt(ts,&ts->adapt);CHKERRQ(ierr); 16951c3436cfSJed Brown 1696277b19d0SLisandro Dalcin if (ts->ops->setup) { 1697000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1698277b19d0SLisandro Dalcin } 1699277b19d0SLisandro Dalcin 17006c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 17016c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 17026c6b9e74SPeter Brune */ 17036c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 17040298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 17056c6b9e74SPeter Brune if (!func) { 17066c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 17076c6b9e74SPeter Brune } 17086c6b9e74SPeter Brune /* if the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it. 17096c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 17106c6b9e74SPeter Brune */ 17110298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 17120298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 17130298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 17146c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 17156c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 17166c6b9e74SPeter Brune } 1717277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1718277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1719277b19d0SLisandro Dalcin } 1720277b19d0SLisandro Dalcin 1721277b19d0SLisandro Dalcin #undef __FUNCT__ 1722277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1723277b19d0SLisandro Dalcin /*@ 1724277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1725277b19d0SLisandro Dalcin 1726277b19d0SLisandro Dalcin Collective on TS 1727277b19d0SLisandro Dalcin 1728277b19d0SLisandro Dalcin Input Parameter: 1729277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1730277b19d0SLisandro Dalcin 1731277b19d0SLisandro Dalcin Level: beginner 1732277b19d0SLisandro Dalcin 1733277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1734277b19d0SLisandro Dalcin 1735277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1736277b19d0SLisandro Dalcin @*/ 1737277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1738277b19d0SLisandro Dalcin { 1739277b19d0SLisandro Dalcin PetscErrorCode ierr; 1740277b19d0SLisandro Dalcin 1741277b19d0SLisandro Dalcin PetscFunctionBegin; 1742277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1743277b19d0SLisandro Dalcin if (ts->ops->reset) { 1744277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1745277b19d0SLisandro Dalcin } 1746277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 1747bbd56ea5SKarl Rupp 17484e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 17494e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1750214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 17516bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1752e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 1753e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 175438637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 1755bbd56ea5SKarl Rupp 1756277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1757d763cef2SBarry Smith PetscFunctionReturn(0); 1758d763cef2SBarry Smith } 1759d763cef2SBarry Smith 17604a2ae208SSatish Balay #undef __FUNCT__ 17614a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1762d8e5e3e6SSatish Balay /*@ 1763d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1764d763cef2SBarry Smith with TSCreate(). 1765d763cef2SBarry Smith 1766d763cef2SBarry Smith Collective on TS 1767d763cef2SBarry Smith 1768d763cef2SBarry Smith Input Parameter: 1769d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1770d763cef2SBarry Smith 1771d763cef2SBarry Smith Level: beginner 1772d763cef2SBarry Smith 1773d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1774d763cef2SBarry Smith 1775d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1776d763cef2SBarry Smith @*/ 17776bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1778d763cef2SBarry Smith { 17796849ba73SBarry Smith PetscErrorCode ierr; 1780d763cef2SBarry Smith 1781d763cef2SBarry Smith PetscFunctionBegin; 17826bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 17836bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 17846bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1785d763cef2SBarry Smith 17866bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1787277b19d0SLisandro Dalcin 1788be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 17896bf464f9SBarry Smith ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 17906bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 17916d4c513bSLisandro Dalcin 179284df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 17936bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 17946bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 17956bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 17966d4c513bSLisandro Dalcin 1797a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1798d763cef2SBarry Smith PetscFunctionReturn(0); 1799d763cef2SBarry Smith } 1800d763cef2SBarry Smith 18014a2ae208SSatish Balay #undef __FUNCT__ 18024a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1803d8e5e3e6SSatish Balay /*@ 1804d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1805d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1806d763cef2SBarry Smith 1807d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1808d763cef2SBarry Smith 1809d763cef2SBarry Smith Input Parameter: 1810d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1811d763cef2SBarry Smith 1812d763cef2SBarry Smith Output Parameter: 1813d763cef2SBarry Smith . snes - the nonlinear solver context 1814d763cef2SBarry Smith 1815d763cef2SBarry Smith Notes: 1816d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1817d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 181894b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1819d763cef2SBarry Smith 1820d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 18210298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 1822d763cef2SBarry Smith 1823d763cef2SBarry Smith Level: beginner 1824d763cef2SBarry Smith 1825d763cef2SBarry Smith .keywords: timestep, get, SNES 1826d763cef2SBarry Smith @*/ 18277087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1828d763cef2SBarry Smith { 1829d372ba47SLisandro Dalcin PetscErrorCode ierr; 1830d372ba47SLisandro Dalcin 1831d763cef2SBarry Smith PetscFunctionBegin; 18320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18334482741eSBarry Smith PetscValidPointer(snes,2); 1834d372ba47SLisandro Dalcin if (!ts->snes) { 1835ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 18360298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 1837d372ba47SLisandro Dalcin ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1838d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 1839496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 18409e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 18419e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 18429e2a6581SJed Brown } 1843d372ba47SLisandro Dalcin } 1844d763cef2SBarry Smith *snes = ts->snes; 1845d763cef2SBarry Smith PetscFunctionReturn(0); 1846d763cef2SBarry Smith } 1847d763cef2SBarry Smith 18484a2ae208SSatish Balay #undef __FUNCT__ 1849deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES" 1850deb2cd25SJed Brown /*@ 1851deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 1852deb2cd25SJed Brown 1853deb2cd25SJed Brown Collective 1854deb2cd25SJed Brown 1855deb2cd25SJed Brown Input Parameter: 1856deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 1857deb2cd25SJed Brown - snes - the nonlinear solver context 1858deb2cd25SJed Brown 1859deb2cd25SJed Brown Notes: 1860deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 1861deb2cd25SJed Brown 1862deb2cd25SJed Brown Level: developer 1863deb2cd25SJed Brown 1864deb2cd25SJed Brown .keywords: timestep, set, SNES 1865deb2cd25SJed Brown @*/ 1866deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 1867deb2cd25SJed Brown { 1868deb2cd25SJed Brown PetscErrorCode ierr; 1869740132f1SEmil Constantinescu PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*); 1870deb2cd25SJed Brown 1871deb2cd25SJed Brown PetscFunctionBegin; 1872deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1873deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 1874deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 1875deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 1876bbd56ea5SKarl Rupp 1877deb2cd25SJed Brown ts->snes = snes; 1878bbd56ea5SKarl Rupp 18790298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 18800298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 1881740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 18820298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1883740132f1SEmil Constantinescu } 1884deb2cd25SJed Brown PetscFunctionReturn(0); 1885deb2cd25SJed Brown } 1886deb2cd25SJed Brown 1887deb2cd25SJed Brown #undef __FUNCT__ 188894b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 1889d8e5e3e6SSatish Balay /*@ 189094b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 1891d763cef2SBarry Smith a TS (timestepper) context. 1892d763cef2SBarry Smith 189394b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 1894d763cef2SBarry Smith 1895d763cef2SBarry Smith Input Parameter: 1896d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1897d763cef2SBarry Smith 1898d763cef2SBarry Smith Output Parameter: 189994b7f48cSBarry Smith . ksp - the nonlinear solver context 1900d763cef2SBarry Smith 1901d763cef2SBarry Smith Notes: 190294b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 1903d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 1904d763cef2SBarry Smith KSP and PC contexts as well. 1905d763cef2SBarry Smith 190694b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 19070298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 1908d763cef2SBarry Smith 1909d763cef2SBarry Smith Level: beginner 1910d763cef2SBarry Smith 191194b7f48cSBarry Smith .keywords: timestep, get, KSP 1912d763cef2SBarry Smith @*/ 19137087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1914d763cef2SBarry Smith { 1915d372ba47SLisandro Dalcin PetscErrorCode ierr; 1916089b2837SJed Brown SNES snes; 1917d372ba47SLisandro Dalcin 1918d763cef2SBarry Smith PetscFunctionBegin; 19190700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 19204482741eSBarry Smith PetscValidPointer(ksp,2); 192117186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1922e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1923089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1924089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1925d763cef2SBarry Smith PetscFunctionReturn(0); 1926d763cef2SBarry Smith } 1927d763cef2SBarry Smith 1928d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 1929d763cef2SBarry Smith 19304a2ae208SSatish Balay #undef __FUNCT__ 1931adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 1932adb62b0dSMatthew Knepley /*@ 1933adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 1934adb62b0dSMatthew Knepley maximum time for iteration. 1935adb62b0dSMatthew Knepley 19363f9fe445SBarry Smith Not Collective 1937adb62b0dSMatthew Knepley 1938adb62b0dSMatthew Knepley Input Parameters: 1939adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 19400298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 19410298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 1942adb62b0dSMatthew Knepley 1943adb62b0dSMatthew Knepley Level: intermediate 1944adb62b0dSMatthew Knepley 1945adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 1946adb62b0dSMatthew Knepley @*/ 19477087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1948adb62b0dSMatthew Knepley { 1949adb62b0dSMatthew Knepley PetscFunctionBegin; 19500700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1951abc0a331SBarry Smith if (maxsteps) { 19524482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 1953adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 1954adb62b0dSMatthew Knepley } 1955abc0a331SBarry Smith if (maxtime) { 19564482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 1957adb62b0dSMatthew Knepley *maxtime = ts->max_time; 1958adb62b0dSMatthew Knepley } 1959adb62b0dSMatthew Knepley PetscFunctionReturn(0); 1960adb62b0dSMatthew Knepley } 1961adb62b0dSMatthew Knepley 1962adb62b0dSMatthew Knepley #undef __FUNCT__ 19634a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 1964d763cef2SBarry Smith /*@ 1965d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 1966d763cef2SBarry Smith maximum time for iteration. 1967d763cef2SBarry Smith 19683f9fe445SBarry Smith Logically Collective on TS 1969d763cef2SBarry Smith 1970d763cef2SBarry Smith Input Parameters: 1971d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1972d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 1973d763cef2SBarry Smith - maxtime - final time to iterate to 1974d763cef2SBarry Smith 1975d763cef2SBarry Smith Options Database Keys: 1976d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 19773bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 1978d763cef2SBarry Smith 1979d763cef2SBarry Smith Notes: 1980d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 1981d763cef2SBarry Smith 1982d763cef2SBarry Smith Level: intermediate 1983d763cef2SBarry Smith 1984d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 1985a43b19c4SJed Brown 1986a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 1987d763cef2SBarry Smith @*/ 19887087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1989d763cef2SBarry Smith { 1990d763cef2SBarry Smith PetscFunctionBegin; 19910700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1992c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1993c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 199439b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 199539b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 1996d763cef2SBarry Smith PetscFunctionReturn(0); 1997d763cef2SBarry Smith } 1998d763cef2SBarry Smith 19994a2ae208SSatish Balay #undef __FUNCT__ 20004a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 2001d763cef2SBarry Smith /*@ 2002d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2003d763cef2SBarry Smith for use by the TS routines. 2004d763cef2SBarry Smith 20053f9fe445SBarry Smith Logically Collective on TS and Vec 2006d763cef2SBarry Smith 2007d763cef2SBarry Smith Input Parameters: 2008d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 20090910c330SBarry Smith - u - the solution vector 2010d763cef2SBarry Smith 2011d763cef2SBarry Smith Level: beginner 2012d763cef2SBarry Smith 2013d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2014d763cef2SBarry Smith @*/ 20150910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2016d763cef2SBarry Smith { 20178737fe31SLisandro Dalcin PetscErrorCode ierr; 2018496e6a7aSJed Brown DM dm; 20198737fe31SLisandro Dalcin 2020d763cef2SBarry Smith PetscFunctionBegin; 20210700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20220910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 20230910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 20246bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2025bbd56ea5SKarl Rupp 20260910c330SBarry Smith ts->vec_sol = u; 2027bbd56ea5SKarl Rupp 2028496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 20290910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2030d763cef2SBarry Smith PetscFunctionReturn(0); 2031d763cef2SBarry Smith } 2032d763cef2SBarry Smith 2033e74ef692SMatthew Knepley #undef __FUNCT__ 2034e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 2035ac226902SBarry Smith /*@C 2036000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 20373f2090d5SJed Brown called once at the beginning of each time step. 2038000e7ae3SMatthew Knepley 20393f9fe445SBarry Smith Logically Collective on TS 2040000e7ae3SMatthew Knepley 2041000e7ae3SMatthew Knepley Input Parameters: 2042000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2043000e7ae3SMatthew Knepley - func - The function 2044000e7ae3SMatthew Knepley 2045000e7ae3SMatthew Knepley Calling sequence of func: 2046000e7ae3SMatthew Knepley . func (TS ts); 2047000e7ae3SMatthew Knepley 2048000e7ae3SMatthew Knepley Level: intermediate 2049000e7ae3SMatthew Knepley 2050b8123daeSJed Brown Note: 2051b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 2052b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 2053b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 2054b8123daeSJed Brown 2055000e7ae3SMatthew Knepley .keywords: TS, timestep 2056b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep() 2057000e7ae3SMatthew Knepley @*/ 20587087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 2059000e7ae3SMatthew Knepley { 2060000e7ae3SMatthew Knepley PetscFunctionBegin; 20610700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2062000e7ae3SMatthew Knepley ts->ops->prestep = func; 2063000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2064000e7ae3SMatthew Knepley } 2065000e7ae3SMatthew Knepley 2066e74ef692SMatthew Knepley #undef __FUNCT__ 20673f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 206809ee8438SJed Brown /*@ 20693f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 20703f2090d5SJed Brown 20713f2090d5SJed Brown Collective on TS 20723f2090d5SJed Brown 20733f2090d5SJed Brown Input Parameters: 20743f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 20753f2090d5SJed Brown 20763f2090d5SJed Brown Notes: 20773f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 20783f2090d5SJed Brown so most users would not generally call this routine themselves. 20793f2090d5SJed Brown 20803f2090d5SJed Brown Level: developer 20813f2090d5SJed Brown 20823f2090d5SJed Brown .keywords: TS, timestep 2083b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep() 20843f2090d5SJed Brown @*/ 20857087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 20863f2090d5SJed Brown { 20873f2090d5SJed Brown PetscErrorCode ierr; 20883f2090d5SJed Brown 20893f2090d5SJed Brown PetscFunctionBegin; 20900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 209172ac3e02SJed Brown if (ts->ops->prestep) { 20923f2090d5SJed Brown PetscStackPush("TS PreStep function"); 20933f2090d5SJed Brown ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 20943f2090d5SJed Brown PetscStackPop; 2095312ce896SJed Brown } 20963f2090d5SJed Brown PetscFunctionReturn(0); 20973f2090d5SJed Brown } 20983f2090d5SJed Brown 20993f2090d5SJed Brown #undef __FUNCT__ 2100b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 2101b8123daeSJed Brown /*@C 2102b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 2103b8123daeSJed Brown called once at the beginning of each stage. 2104b8123daeSJed Brown 2105b8123daeSJed Brown Logically Collective on TS 2106b8123daeSJed Brown 2107b8123daeSJed Brown Input Parameters: 2108b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 2109b8123daeSJed Brown - func - The function 2110b8123daeSJed Brown 2111b8123daeSJed Brown Calling sequence of func: 2112b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 2113b8123daeSJed Brown 2114b8123daeSJed Brown Level: intermediate 2115b8123daeSJed Brown 2116b8123daeSJed Brown Note: 2117b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 2118b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 2119b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 2120b8123daeSJed Brown 2121b8123daeSJed Brown .keywords: TS, timestep 2122b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 2123b8123daeSJed Brown @*/ 2124b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 2125b8123daeSJed Brown { 2126b8123daeSJed Brown PetscFunctionBegin; 2127b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2128b8123daeSJed Brown ts->ops->prestage = func; 2129b8123daeSJed Brown PetscFunctionReturn(0); 2130b8123daeSJed Brown } 2131b8123daeSJed Brown 2132b8123daeSJed Brown #undef __FUNCT__ 2133b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 2134b8123daeSJed Brown /*@ 2135b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 2136b8123daeSJed Brown 2137b8123daeSJed Brown Collective on TS 2138b8123daeSJed Brown 2139b8123daeSJed Brown Input Parameters: 2140b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 2141b8123daeSJed Brown 2142b8123daeSJed Brown Notes: 2143b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 2144b8123daeSJed Brown most users would not generally call this routine themselves. 2145b8123daeSJed Brown 2146b8123daeSJed Brown Level: developer 2147b8123daeSJed Brown 2148b8123daeSJed Brown .keywords: TS, timestep 2149b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep() 2150b8123daeSJed Brown @*/ 2151b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 2152b8123daeSJed Brown { 2153b8123daeSJed Brown PetscErrorCode ierr; 2154b8123daeSJed Brown 2155b8123daeSJed Brown PetscFunctionBegin; 2156b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2157b8123daeSJed Brown if (ts->ops->prestage) { 2158b8123daeSJed Brown PetscStackPush("TS PreStage function"); 2159b8123daeSJed Brown ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr); 2160b8123daeSJed Brown PetscStackPop; 2161b8123daeSJed Brown } 2162b8123daeSJed Brown PetscFunctionReturn(0); 2163b8123daeSJed Brown } 2164b8123daeSJed Brown 2165b8123daeSJed Brown #undef __FUNCT__ 2166e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 2167ac226902SBarry Smith /*@C 2168000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 21693f2090d5SJed Brown called once at the end of each time step. 2170000e7ae3SMatthew Knepley 21713f9fe445SBarry Smith Logically Collective on TS 2172000e7ae3SMatthew Knepley 2173000e7ae3SMatthew Knepley Input Parameters: 2174000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2175000e7ae3SMatthew Knepley - func - The function 2176000e7ae3SMatthew Knepley 2177000e7ae3SMatthew Knepley Calling sequence of func: 2178b8123daeSJed Brown $ func (TS ts); 2179000e7ae3SMatthew Knepley 2180000e7ae3SMatthew Knepley Level: intermediate 2181000e7ae3SMatthew Knepley 2182000e7ae3SMatthew Knepley .keywords: TS, timestep 2183b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 2184000e7ae3SMatthew Knepley @*/ 21857087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 2186000e7ae3SMatthew Knepley { 2187000e7ae3SMatthew Knepley PetscFunctionBegin; 21880700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2189000e7ae3SMatthew Knepley ts->ops->poststep = func; 2190000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2191000e7ae3SMatthew Knepley } 2192000e7ae3SMatthew Knepley 2193e74ef692SMatthew Knepley #undef __FUNCT__ 21943f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 219509ee8438SJed Brown /*@ 21963f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 21973f2090d5SJed Brown 21983f2090d5SJed Brown Collective on TS 21993f2090d5SJed Brown 22003f2090d5SJed Brown Input Parameters: 22013f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 22023f2090d5SJed Brown 22033f2090d5SJed Brown Notes: 22043f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 22053f2090d5SJed Brown so most users would not generally call this routine themselves. 22063f2090d5SJed Brown 22073f2090d5SJed Brown Level: developer 22083f2090d5SJed Brown 22093f2090d5SJed Brown .keywords: TS, timestep 22103f2090d5SJed Brown @*/ 22117087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 22123f2090d5SJed Brown { 22133f2090d5SJed Brown PetscErrorCode ierr; 22143f2090d5SJed Brown 22153f2090d5SJed Brown PetscFunctionBegin; 22160700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 221772ac3e02SJed Brown if (ts->ops->poststep) { 22183f2090d5SJed Brown PetscStackPush("TS PostStep function"); 22193f2090d5SJed Brown ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 22203f2090d5SJed Brown PetscStackPop; 222172ac3e02SJed Brown } 22223f2090d5SJed Brown PetscFunctionReturn(0); 22233f2090d5SJed Brown } 22243f2090d5SJed Brown 2225d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 2226d763cef2SBarry Smith 22274a2ae208SSatish Balay #undef __FUNCT__ 2228a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 2229d763cef2SBarry Smith /*@C 2230a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 2231d763cef2SBarry Smith timestep to display the iteration's progress. 2232d763cef2SBarry Smith 22333f9fe445SBarry Smith Logically Collective on TS 2234d763cef2SBarry Smith 2235d763cef2SBarry Smith Input Parameters: 2236d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2237e213d8f1SJed Brown . monitor - monitoring routine 2238329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 22390298fd71SBarry Smith monitor routine (use NULL if no context is desired) 2240b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 22410298fd71SBarry Smith (may be NULL) 2242d763cef2SBarry Smith 2243e213d8f1SJed Brown Calling sequence of monitor: 22440910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 2245d763cef2SBarry Smith 2246d763cef2SBarry Smith + ts - the TS context 224788c05cc5SBarry Smith . steps - iteration number (after the final time step the monitor routine is called with a step of -1, this is at the final time which may have 224888c05cc5SBarry Smith been interpolated to) 22491f06c33eSBarry Smith . time - current time 22500910c330SBarry Smith . u - current iterate 2251d763cef2SBarry Smith - mctx - [optional] monitoring context 2252d763cef2SBarry Smith 2253d763cef2SBarry Smith Notes: 2254d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 2255d763cef2SBarry Smith already has been loaded. 2256d763cef2SBarry Smith 2257025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 2258025f1a04SBarry Smith 2259d763cef2SBarry Smith Level: intermediate 2260d763cef2SBarry Smith 2261d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2262d763cef2SBarry Smith 2263a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 2264d763cef2SBarry Smith @*/ 2265c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 2266d763cef2SBarry Smith { 2267d763cef2SBarry Smith PetscFunctionBegin; 22680700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 226917186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2270d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 22718704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 2272d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 2273d763cef2SBarry Smith PetscFunctionReturn(0); 2274d763cef2SBarry Smith } 2275d763cef2SBarry Smith 22764a2ae208SSatish Balay #undef __FUNCT__ 2277a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 2278d763cef2SBarry Smith /*@C 2279a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 2280d763cef2SBarry Smith 22813f9fe445SBarry Smith Logically Collective on TS 2282d763cef2SBarry Smith 2283d763cef2SBarry Smith Input Parameters: 2284d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2285d763cef2SBarry Smith 2286d763cef2SBarry Smith Notes: 2287d763cef2SBarry Smith There is no way to remove a single, specific monitor. 2288d763cef2SBarry Smith 2289d763cef2SBarry Smith Level: intermediate 2290d763cef2SBarry Smith 2291d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2292d763cef2SBarry Smith 2293a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 2294d763cef2SBarry Smith @*/ 22957087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 2296d763cef2SBarry Smith { 2297d952e501SBarry Smith PetscErrorCode ierr; 2298d952e501SBarry Smith PetscInt i; 2299d952e501SBarry Smith 2300d763cef2SBarry Smith PetscFunctionBegin; 23010700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2302d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 23038704b422SBarry Smith if (ts->monitordestroy[i]) { 23048704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 2305d952e501SBarry Smith } 2306d952e501SBarry Smith } 2307d763cef2SBarry Smith ts->numbermonitors = 0; 2308d763cef2SBarry Smith PetscFunctionReturn(0); 2309d763cef2SBarry Smith } 2310d763cef2SBarry Smith 23114a2ae208SSatish Balay #undef __FUNCT__ 2312a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 2313d8e5e3e6SSatish Balay /*@ 2314a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 23155516499fSSatish Balay 23165516499fSSatish Balay Level: intermediate 231741251cbbSSatish Balay 23185516499fSSatish Balay .keywords: TS, set, monitor 23195516499fSSatish Balay 232041251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 232141251cbbSSatish Balay @*/ 2322649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 2323d763cef2SBarry Smith { 2324dfbe8321SBarry Smith PetscErrorCode ierr; 2325ce94432eSBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts)); 2326d132466eSBarry Smith 2327d763cef2SBarry Smith PetscFunctionBegin; 2328649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2329971832b6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr); 2330649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2331d763cef2SBarry Smith PetscFunctionReturn(0); 2332d763cef2SBarry Smith } 2333d763cef2SBarry Smith 23344a2ae208SSatish Balay #undef __FUNCT__ 2335cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 2336cd652676SJed Brown /*@ 2337cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 2338cd652676SJed Brown 2339cd652676SJed Brown Logically Collective on TS 2340cd652676SJed Brown 2341cd652676SJed Brown Input Argument: 2342cd652676SJed Brown . ts - time stepping context 2343cd652676SJed Brown 2344cd652676SJed Brown Output Argument: 2345cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 2346cd652676SJed Brown 2347cd652676SJed Brown Level: intermediate 2348cd652676SJed Brown 2349cd652676SJed Brown .keywords: TS, set 2350cd652676SJed Brown 2351cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 2352cd652676SJed Brown @*/ 2353cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 2354cd652676SJed Brown { 2355cd652676SJed Brown PetscFunctionBegin; 2356cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2357cd652676SJed Brown ts->retain_stages = flg; 2358cd652676SJed Brown PetscFunctionReturn(0); 2359cd652676SJed Brown } 2360cd652676SJed Brown 2361cd652676SJed Brown #undef __FUNCT__ 2362cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 2363cd652676SJed Brown /*@ 2364cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 2365cd652676SJed Brown 2366cd652676SJed Brown Collective on TS 2367cd652676SJed Brown 2368cd652676SJed Brown Input Argument: 2369cd652676SJed Brown + ts - time stepping context 2370cd652676SJed Brown - t - time to interpolate to 2371cd652676SJed Brown 2372cd652676SJed Brown Output Argument: 23730910c330SBarry Smith . U - state at given time 2374cd652676SJed Brown 2375cd652676SJed Brown Notes: 2376cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 2377cd652676SJed Brown 2378cd652676SJed Brown Level: intermediate 2379cd652676SJed Brown 2380cd652676SJed Brown Developer Notes: 2381cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 2382cd652676SJed Brown 2383cd652676SJed Brown .keywords: TS, set 2384cd652676SJed Brown 2385cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 2386cd652676SJed Brown @*/ 23870910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 2388cd652676SJed Brown { 2389cd652676SJed Brown PetscErrorCode ierr; 2390cd652676SJed Brown 2391cd652676SJed Brown PetscFunctionBegin; 2392cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2393ce94432eSBarry Smith if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Requested time %G not in last time steps [%G,%G]",t,ts->ptime-ts->time_step_prev,ts->ptime); 2394ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 23950910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 2396cd652676SJed Brown PetscFunctionReturn(0); 2397cd652676SJed Brown } 2398cd652676SJed Brown 2399cd652676SJed Brown #undef __FUNCT__ 24004a2ae208SSatish Balay #define __FUNCT__ "TSStep" 2401d763cef2SBarry Smith /*@ 24026d9e5789SSean Farley TSStep - Steps one time step 2403d763cef2SBarry Smith 2404d763cef2SBarry Smith Collective on TS 2405d763cef2SBarry Smith 2406d763cef2SBarry Smith Input Parameter: 2407d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2408d763cef2SBarry Smith 24096d9e5789SSean Farley Level: intermediate 2410d763cef2SBarry Smith 2411b8123daeSJed Brown Notes: 2412b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 2413b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 2414b8123daeSJed Brown 241525cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 241625cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 241725cb2221SBarry Smith 2418d763cef2SBarry Smith .keywords: TS, timestep, solve 2419d763cef2SBarry Smith 242025cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 2421d763cef2SBarry Smith @*/ 2422193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 2423d763cef2SBarry Smith { 2424362cd11cSLisandro Dalcin PetscReal ptime_prev; 2425dfbe8321SBarry Smith PetscErrorCode ierr; 2426d763cef2SBarry Smith 2427d763cef2SBarry Smith PetscFunctionBegin; 24280700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2429d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 2430d405a339SMatthew Knepley 2431362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 2432362cd11cSLisandro Dalcin ptime_prev = ts->ptime; 2433bbd56ea5SKarl Rupp 2434d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2435193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 2436d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2437bbd56ea5SKarl Rupp 2438362cd11cSLisandro Dalcin ts->time_step_prev = ts->ptime - ptime_prev; 2439362cd11cSLisandro Dalcin 2440362cd11cSLisandro Dalcin if (ts->reason < 0) { 2441cef5090cSJed Brown if (ts->errorifstepfailed) { 2442cef5090cSJed Brown if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) { 2443ce94432eSBarry Smith 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]); 2444ce94432eSBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 2445cef5090cSJed Brown } 2446362cd11cSLisandro Dalcin } else if (!ts->reason) { 2447db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 2448db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 2449362cd11cSLisandro Dalcin } 2450d763cef2SBarry Smith PetscFunctionReturn(0); 2451d763cef2SBarry Smith } 2452d763cef2SBarry Smith 24534a2ae208SSatish Balay #undef __FUNCT__ 245405175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 245505175c85SJed Brown /*@ 245605175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 245705175c85SJed Brown 24581c3436cfSJed Brown Collective on TS 245905175c85SJed Brown 246005175c85SJed Brown Input Arguments: 24611c3436cfSJed Brown + ts - time stepping context 24621c3436cfSJed Brown . order - desired order of accuracy 24630298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 246405175c85SJed Brown 246505175c85SJed Brown Output Arguments: 24660910c330SBarry Smith . U - state at the end of the current step 246705175c85SJed Brown 246805175c85SJed Brown Level: advanced 246905175c85SJed Brown 2470108c343cSJed Brown Notes: 2471108c343cSJed Brown This function cannot be called until all stages have been evaluated. 2472108c343cSJed 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. 2473108c343cSJed Brown 24741c3436cfSJed Brown .seealso: TSStep(), TSAdapt 247505175c85SJed Brown @*/ 24760910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 247705175c85SJed Brown { 247805175c85SJed Brown PetscErrorCode ierr; 247905175c85SJed Brown 248005175c85SJed Brown PetscFunctionBegin; 248105175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 248205175c85SJed Brown PetscValidType(ts,1); 24830910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 2484ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 24850910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 248605175c85SJed Brown PetscFunctionReturn(0); 248705175c85SJed Brown } 248805175c85SJed Brown 248905175c85SJed Brown #undef __FUNCT__ 24906a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 24916a4d4014SLisandro Dalcin /*@ 24926a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 24936a4d4014SLisandro Dalcin 24946a4d4014SLisandro Dalcin Collective on TS 24956a4d4014SLisandro Dalcin 24966a4d4014SLisandro Dalcin Input Parameter: 24976a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 2498cc708dedSBarry Smith - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 24995a3a76d0SJed Brown 25006a4d4014SLisandro Dalcin Level: beginner 25016a4d4014SLisandro Dalcin 25025a3a76d0SJed Brown Notes: 25035a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 25045a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 25055a3a76d0SJed Brown stepped over the final time. 25065a3a76d0SJed Brown 25076a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 25086a4d4014SLisandro Dalcin 25096a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 25106a4d4014SLisandro Dalcin @*/ 2511cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 25126a4d4014SLisandro Dalcin { 25134d7d938eSLisandro Dalcin PetscBool flg; 25144d7d938eSLisandro Dalcin PetscViewer viewer; 25156a4d4014SLisandro Dalcin PetscErrorCode ierr; 2516cffb1e40SBarry Smith PetscViewerFormat format; 2517f22f69f0SBarry Smith 25186a4d4014SLisandro Dalcin PetscFunctionBegin; 25190700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2520f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 252149354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE) { /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */ 25220910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 25235a3a76d0SJed Brown Vec y; 25240910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 25255a3a76d0SJed Brown ierr = TSSetSolution(ts,y);CHKERRQ(ierr); 25265a3a76d0SJed Brown ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */ 25275a3a76d0SJed Brown } 2528f2c2a1b9SBarry Smith if (u) { 25290910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 2530f2c2a1b9SBarry Smith } 2531bbd56ea5SKarl Rupp } else if (u) { 25320910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 25335a3a76d0SJed Brown } 2534b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 25356a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 2536193ac0bcSJed Brown ts->steps = 0; 25375ef26d82SJed Brown ts->ksp_its = 0; 25385ef26d82SJed Brown ts->snes_its = 0; 2539c610991cSLisandro Dalcin ts->num_snes_failures = 0; 2540c610991cSLisandro Dalcin ts->reject = 0; 2541193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 2542193ac0bcSJed Brown 2543193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 2544193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 25450910c330SBarry Smith ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 2546bbd56ea5SKarl Rupp 2547cc708dedSBarry Smith ts->solvetime = ts->ptime; 2548193ac0bcSJed Brown } else { 25496a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 2550362cd11cSLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2551db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 2552db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 2553e1a7a14fSJed Brown while (!ts->reason) { 2554193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 2555193ac0bcSJed Brown ierr = TSPostStep(ts);CHKERRQ(ierr); 2556193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2557193ac0bcSJed Brown } 255849354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 25590910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 2560bbd56ea5SKarl Rupp 2561cc708dedSBarry Smith ts->solvetime = ts->max_time; 25620574a7fbSJed Brown } else { 2563ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 2564cc708dedSBarry Smith ts->solvetime = ts->ptime; 25650574a7fbSJed Brown } 2566193ac0bcSJed Brown } 25673923b477SBarry Smith ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2568ce94432eSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr); 25694d7d938eSLisandro Dalcin if (flg && !PetscPreLoadingOn) { 2570cffb1e40SBarry Smith ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); 25714d7d938eSLisandro Dalcin ierr = TSView(ts,viewer);CHKERRQ(ierr); 2572cffb1e40SBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 2573cffb1e40SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 25742b0a91c0SBarry Smith } 25756a4d4014SLisandro Dalcin PetscFunctionReturn(0); 25766a4d4014SLisandro Dalcin } 25776a4d4014SLisandro Dalcin 25786a4d4014SLisandro Dalcin #undef __FUNCT__ 25794a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 2580228d79bcSJed Brown /*@ 2581228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 2582228d79bcSJed Brown 2583228d79bcSJed Brown Collective on TS 2584228d79bcSJed Brown 2585228d79bcSJed Brown Input Parameters: 2586228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 2587228d79bcSJed Brown . step - step number that has just completed 2588228d79bcSJed Brown . ptime - model time of the state 25890910c330SBarry Smith - u - state at the current model time 2590228d79bcSJed Brown 2591228d79bcSJed Brown Notes: 2592228d79bcSJed Brown TSMonitor() is typically used within the time stepping implementations. 2593228d79bcSJed Brown Users might call this function when using the TSStep() interface instead of TSSolve(). 2594228d79bcSJed Brown 2595228d79bcSJed Brown Level: advanced 2596228d79bcSJed Brown 2597228d79bcSJed Brown .keywords: TS, timestep 2598228d79bcSJed Brown @*/ 25990910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 2600d763cef2SBarry Smith { 26016849ba73SBarry Smith PetscErrorCode ierr; 2602a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 2603d763cef2SBarry Smith 2604d763cef2SBarry Smith PetscFunctionBegin; 2605d763cef2SBarry Smith for (i=0; i<n; i++) { 26060910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 2607d763cef2SBarry Smith } 2608d763cef2SBarry Smith PetscFunctionReturn(0); 2609d763cef2SBarry Smith } 2610d763cef2SBarry Smith 2611d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 26120b039ecaSBarry Smith struct _n_TSMonitorLGCtx { 26130b039ecaSBarry Smith PetscDrawLG lg; 26140b039ecaSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 2615201da799SBarry Smith PetscInt ksp_its,snes_its; 26160b039ecaSBarry Smith }; 26170b039ecaSBarry Smith 2618d763cef2SBarry Smith 26194a2ae208SSatish Balay #undef __FUNCT__ 2620a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 2621d763cef2SBarry Smith /*@C 2622a9f9c1f6SBarry Smith TSMonitorLGCtxCreate - Creates a line graph context for use with 2623a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 2624d763cef2SBarry Smith 2625d763cef2SBarry Smith Collective on TS 2626d763cef2SBarry Smith 2627d763cef2SBarry Smith Input Parameters: 2628d763cef2SBarry Smith + host - the X display to open, or null for the local machine 2629d763cef2SBarry Smith . label - the title to put in the title bar 26307c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 2631a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 2632a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 2633d763cef2SBarry Smith 2634d763cef2SBarry Smith Output Parameter: 26350b039ecaSBarry Smith . ctx - the context 2636d763cef2SBarry Smith 2637d763cef2SBarry Smith Options Database Key: 26384f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 26394f09c107SBarry Smith . -ts_monitor_lg_solution - 264099fdda47SBarry Smith . -ts_monitor_lg_error - 264199fdda47SBarry Smith . -ts_monitor_lg_ksp_iterations - 264299fdda47SBarry Smith . -ts_monitor_lg_snes_iterations - 264399fdda47SBarry Smith - -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true 2644d763cef2SBarry Smith 2645d763cef2SBarry Smith Notes: 2646a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 2647d763cef2SBarry Smith 2648d763cef2SBarry Smith Level: intermediate 2649d763cef2SBarry Smith 26507c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 2651d763cef2SBarry Smith 26524f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 26537c922b88SBarry Smith 2654d763cef2SBarry Smith @*/ 2655a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 2656d763cef2SBarry Smith { 2657b0a32e0cSBarry Smith PetscDraw win; 2658dfbe8321SBarry Smith PetscErrorCode ierr; 265999fdda47SBarry Smith PetscBool flg = PETSC_TRUE; 2660d763cef2SBarry Smith 2661d763cef2SBarry Smith PetscFunctionBegin; 2662201da799SBarry Smith ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr); 2663a80ad3e0SBarry Smith ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr); 26643fbbecb0SBarry Smith ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr); 26650b039ecaSBarry Smith ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr); 26660298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-lg_indicate_data_points",&flg,NULL);CHKERRQ(ierr); 266799fdda47SBarry Smith if (flg) { 26680b039ecaSBarry Smith ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr); 266999fdda47SBarry Smith } 26700b039ecaSBarry Smith ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr); 2671a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 2672d763cef2SBarry Smith PetscFunctionReturn(0); 2673d763cef2SBarry Smith } 2674d763cef2SBarry Smith 26754a2ae208SSatish Balay #undef __FUNCT__ 26764f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 26774f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 2678d763cef2SBarry Smith { 26790b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 2680c32365f1SBarry Smith PetscReal x = ptime,y; 2681dfbe8321SBarry Smith PetscErrorCode ierr; 2682d763cef2SBarry Smith 2683d763cef2SBarry Smith PetscFunctionBegin; 2684a9f9c1f6SBarry Smith if (!n) { 2685a9f9c1f6SBarry Smith PetscDrawAxis axis; 2686a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 2687a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 2688a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 2689a9f9c1f6SBarry Smith } 2690c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 26910b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 269299fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))) { 26930b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 26943923b477SBarry Smith } 2695d763cef2SBarry Smith PetscFunctionReturn(0); 2696d763cef2SBarry Smith } 2697d763cef2SBarry Smith 26984a2ae208SSatish Balay #undef __FUNCT__ 2699a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 2700d763cef2SBarry Smith /*@C 2701a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 2702a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 2703d763cef2SBarry Smith 27040b039ecaSBarry Smith Collective on TSMonitorLGCtx 2705d763cef2SBarry Smith 2706d763cef2SBarry Smith Input Parameter: 27070b039ecaSBarry Smith . ctx - the monitor context 2708d763cef2SBarry Smith 2709d763cef2SBarry Smith Level: intermediate 2710d763cef2SBarry Smith 2711d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 2712d763cef2SBarry Smith 27134f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 2714d763cef2SBarry Smith @*/ 2715a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 2716d763cef2SBarry Smith { 2717b0a32e0cSBarry Smith PetscDraw draw; 2718dfbe8321SBarry Smith PetscErrorCode ierr; 2719d763cef2SBarry Smith 2720d763cef2SBarry Smith PetscFunctionBegin; 27210b039ecaSBarry Smith ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr); 27226bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 27230b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 27240b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 2725d763cef2SBarry Smith PetscFunctionReturn(0); 2726d763cef2SBarry Smith } 2727d763cef2SBarry Smith 27284a2ae208SSatish Balay #undef __FUNCT__ 27294a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 2730d763cef2SBarry Smith /*@ 2731b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 2732d763cef2SBarry Smith 2733d763cef2SBarry Smith Not Collective 2734d763cef2SBarry Smith 2735d763cef2SBarry Smith Input Parameter: 2736d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2737d763cef2SBarry Smith 2738d763cef2SBarry Smith Output Parameter: 2739d763cef2SBarry Smith . t - the current time 2740d763cef2SBarry Smith 2741d763cef2SBarry Smith Level: beginner 2742d763cef2SBarry Smith 2743b8123daeSJed Brown Note: 2744b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 2745b8123daeSJed Brown TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 2746b8123daeSJed Brown 2747d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2748d763cef2SBarry Smith 2749d763cef2SBarry Smith .keywords: TS, get, time 2750d763cef2SBarry Smith @*/ 27517087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 2752d763cef2SBarry Smith { 2753d763cef2SBarry Smith PetscFunctionBegin; 27540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2755f7cf8827SBarry Smith PetscValidRealPointer(t,2); 2756d763cef2SBarry Smith *t = ts->ptime; 2757d763cef2SBarry Smith PetscFunctionReturn(0); 2758d763cef2SBarry Smith } 2759d763cef2SBarry Smith 27604a2ae208SSatish Balay #undef __FUNCT__ 27616a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 27626a4d4014SLisandro Dalcin /*@ 27636a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 27646a4d4014SLisandro Dalcin 27653f9fe445SBarry Smith Logically Collective on TS 27666a4d4014SLisandro Dalcin 27676a4d4014SLisandro Dalcin Input Parameters: 27686a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 27696a4d4014SLisandro Dalcin - time - the time 27706a4d4014SLisandro Dalcin 27716a4d4014SLisandro Dalcin Level: intermediate 27726a4d4014SLisandro Dalcin 27736a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 27746a4d4014SLisandro Dalcin 27756a4d4014SLisandro Dalcin .keywords: TS, set, time 27766a4d4014SLisandro Dalcin @*/ 27777087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 27786a4d4014SLisandro Dalcin { 27796a4d4014SLisandro Dalcin PetscFunctionBegin; 27800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2781c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 27826a4d4014SLisandro Dalcin ts->ptime = t; 27836a4d4014SLisandro Dalcin PetscFunctionReturn(0); 27846a4d4014SLisandro Dalcin } 27856a4d4014SLisandro Dalcin 27866a4d4014SLisandro Dalcin #undef __FUNCT__ 27874a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 2788d763cef2SBarry Smith /*@C 2789d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 2790d763cef2SBarry Smith TS options in the database. 2791d763cef2SBarry Smith 27923f9fe445SBarry Smith Logically Collective on TS 2793d763cef2SBarry Smith 2794d763cef2SBarry Smith Input Parameter: 2795d763cef2SBarry Smith + ts - The TS context 2796d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2797d763cef2SBarry Smith 2798d763cef2SBarry Smith Notes: 2799d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2800d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2801d763cef2SBarry Smith hyphen. 2802d763cef2SBarry Smith 2803d763cef2SBarry Smith Level: advanced 2804d763cef2SBarry Smith 2805d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 2806d763cef2SBarry Smith 2807d763cef2SBarry Smith .seealso: TSSetFromOptions() 2808d763cef2SBarry Smith 2809d763cef2SBarry Smith @*/ 28107087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 2811d763cef2SBarry Smith { 2812dfbe8321SBarry Smith PetscErrorCode ierr; 2813089b2837SJed Brown SNES snes; 2814d763cef2SBarry Smith 2815d763cef2SBarry Smith PetscFunctionBegin; 28160700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2817d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2818089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2819089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2820d763cef2SBarry Smith PetscFunctionReturn(0); 2821d763cef2SBarry Smith } 2822d763cef2SBarry Smith 2823d763cef2SBarry Smith 28244a2ae208SSatish Balay #undef __FUNCT__ 28254a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 2826d763cef2SBarry Smith /*@C 2827d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 2828d763cef2SBarry Smith TS options in the database. 2829d763cef2SBarry Smith 28303f9fe445SBarry Smith Logically Collective on TS 2831d763cef2SBarry Smith 2832d763cef2SBarry Smith Input Parameter: 2833d763cef2SBarry Smith + ts - The TS context 2834d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2835d763cef2SBarry Smith 2836d763cef2SBarry Smith Notes: 2837d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2838d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2839d763cef2SBarry Smith hyphen. 2840d763cef2SBarry Smith 2841d763cef2SBarry Smith Level: advanced 2842d763cef2SBarry Smith 2843d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 2844d763cef2SBarry Smith 2845d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 2846d763cef2SBarry Smith 2847d763cef2SBarry Smith @*/ 28487087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 2849d763cef2SBarry Smith { 2850dfbe8321SBarry Smith PetscErrorCode ierr; 2851089b2837SJed Brown SNES snes; 2852d763cef2SBarry Smith 2853d763cef2SBarry Smith PetscFunctionBegin; 28540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2855d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2856089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2857089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2858d763cef2SBarry Smith PetscFunctionReturn(0); 2859d763cef2SBarry Smith } 2860d763cef2SBarry Smith 28614a2ae208SSatish Balay #undef __FUNCT__ 28624a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2863d763cef2SBarry Smith /*@C 2864d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2865d763cef2SBarry Smith TS options in the database. 2866d763cef2SBarry Smith 2867d763cef2SBarry Smith Not Collective 2868d763cef2SBarry Smith 2869d763cef2SBarry Smith Input Parameter: 2870d763cef2SBarry Smith . ts - The TS context 2871d763cef2SBarry Smith 2872d763cef2SBarry Smith Output Parameter: 2873d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2874d763cef2SBarry Smith 2875d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2876d763cef2SBarry Smith sufficient length to hold the prefix. 2877d763cef2SBarry Smith 2878d763cef2SBarry Smith Level: intermediate 2879d763cef2SBarry Smith 2880d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2881d763cef2SBarry Smith 2882d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2883d763cef2SBarry Smith @*/ 28847087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2885d763cef2SBarry Smith { 2886dfbe8321SBarry Smith PetscErrorCode ierr; 2887d763cef2SBarry Smith 2888d763cef2SBarry Smith PetscFunctionBegin; 28890700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28904482741eSBarry Smith PetscValidPointer(prefix,2); 2891d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2892d763cef2SBarry Smith PetscFunctionReturn(0); 2893d763cef2SBarry Smith } 2894d763cef2SBarry Smith 28954a2ae208SSatish Balay #undef __FUNCT__ 28964a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2897d763cef2SBarry Smith /*@C 2898d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2899d763cef2SBarry Smith 2900d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2901d763cef2SBarry Smith 2902d763cef2SBarry Smith Input Parameter: 2903d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2904d763cef2SBarry Smith 2905d763cef2SBarry Smith Output Parameters: 2906b5abc632SBarry Smith + J - The Jacobian J of F, where U_t = G(U,t) 2907d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2908089b2837SJed Brown . func - Function to compute the Jacobian of the RHS 2909d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2910d763cef2SBarry Smith 29110298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 2912d763cef2SBarry Smith 2913d763cef2SBarry Smith Level: intermediate 2914d763cef2SBarry Smith 291526d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2916d763cef2SBarry Smith 2917d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2918d763cef2SBarry Smith @*/ 2919089b2837SJed Brown PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2920d763cef2SBarry Smith { 2921089b2837SJed Brown PetscErrorCode ierr; 2922089b2837SJed Brown SNES snes; 292324989b8cSPeter Brune DM dm; 2924089b2837SJed Brown 2925d763cef2SBarry Smith PetscFunctionBegin; 2926089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 29270298fd71SBarry Smith ierr = SNESGetJacobian(snes,J,M,NULL,NULL);CHKERRQ(ierr); 292824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 292924989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 2930d763cef2SBarry Smith PetscFunctionReturn(0); 2931d763cef2SBarry Smith } 2932d763cef2SBarry Smith 29331713a123SBarry Smith #undef __FUNCT__ 29342eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 29352eca1d9cSJed Brown /*@C 29362eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 29372eca1d9cSJed Brown 29382eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 29392eca1d9cSJed Brown 29402eca1d9cSJed Brown Input Parameter: 29412eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 29422eca1d9cSJed Brown 29432eca1d9cSJed Brown Output Parameters: 29442eca1d9cSJed Brown + A - The Jacobian of F(t,U,U_t) 29452eca1d9cSJed Brown . B - The preconditioner matrix, often the same as A 29462eca1d9cSJed Brown . f - The function to compute the matrices 29472eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 29482eca1d9cSJed Brown 29490298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 29502eca1d9cSJed Brown 29512eca1d9cSJed Brown Level: advanced 29522eca1d9cSJed Brown 29532eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 29542eca1d9cSJed Brown 29552eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 29562eca1d9cSJed Brown @*/ 29577087cfbeSBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 29582eca1d9cSJed Brown { 2959089b2837SJed Brown PetscErrorCode ierr; 2960089b2837SJed Brown SNES snes; 296124989b8cSPeter Brune DM dm; 29620910c330SBarry Smith 29632eca1d9cSJed Brown PetscFunctionBegin; 2964089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2965f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 29660298fd71SBarry Smith ierr = SNESGetJacobian(snes,A,B,NULL,NULL);CHKERRQ(ierr); 296724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 296824989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 29692eca1d9cSJed Brown PetscFunctionReturn(0); 29702eca1d9cSJed Brown } 29712eca1d9cSJed Brown 29726083293cSBarry Smith 29732eca1d9cSJed Brown #undef __FUNCT__ 297483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 29751713a123SBarry Smith /*@C 297683a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 29771713a123SBarry Smith VecView() for the solution at each timestep 29781713a123SBarry Smith 29791713a123SBarry Smith Collective on TS 29801713a123SBarry Smith 29811713a123SBarry Smith Input Parameters: 29821713a123SBarry Smith + ts - the TS context 29831713a123SBarry Smith . step - current time-step 2984142b95e3SSatish Balay . ptime - current time 29850298fd71SBarry Smith - dummy - either a viewer or NULL 29861713a123SBarry Smith 298799fdda47SBarry Smith Options Database: 298899fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 298999fdda47SBarry Smith 299099fdda47SBarry Smith Notes: the initial solution and current solution are not displayed with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 299199fdda47SBarry Smith will look bad 299299fdda47SBarry Smith 29931713a123SBarry Smith Level: intermediate 29941713a123SBarry Smith 29951713a123SBarry Smith .keywords: TS, vector, monitor, view 29961713a123SBarry Smith 2997a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 29981713a123SBarry Smith @*/ 29990910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 30001713a123SBarry Smith { 3001dfbe8321SBarry Smith PetscErrorCode ierr; 300283a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 3003473a3ab2SBarry Smith PetscDraw draw; 30041713a123SBarry Smith 30051713a123SBarry Smith PetscFunctionBegin; 30066083293cSBarry Smith if (!step && ictx->showinitial) { 30076083293cSBarry Smith if (!ictx->initialsolution) { 30080910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 30091713a123SBarry Smith } 30100910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 30116083293cSBarry Smith } 30123fbbecb0SBarry Smith if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0); 30130dcf80beSBarry Smith 30146083293cSBarry Smith if (ictx->showinitial) { 30156083293cSBarry Smith PetscReal pause; 30166083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 30176083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 30186083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 30196083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 30206083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 30216083293cSBarry Smith } 30220910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 3023473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 3024473a3ab2SBarry Smith PetscReal xl,yl,xr,yr,tw,w,h; 3025473a3ab2SBarry Smith char time[32]; 3026473a3ab2SBarry Smith size_t len; 3027473a3ab2SBarry Smith 3028473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 3029473a3ab2SBarry Smith ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr); 3030473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 3031473a3ab2SBarry Smith ierr = PetscStrlen(time,&len);CHKERRQ(ierr); 30320298fd71SBarry Smith ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr); 3033473a3ab2SBarry Smith w = xl + .5*(xr - xl) - .5*len*tw; 3034473a3ab2SBarry Smith h = yl + .95*(yr - yl); 3035473a3ab2SBarry Smith ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 3036473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 3037473a3ab2SBarry Smith } 3038473a3ab2SBarry Smith 30396083293cSBarry Smith if (ictx->showinitial) { 30406083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 30416083293cSBarry Smith } 30421713a123SBarry Smith PetscFunctionReturn(0); 30431713a123SBarry Smith } 30441713a123SBarry Smith 30452d5ee99bSBarry Smith #undef __FUNCT__ 30462d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase" 30472d5ee99bSBarry Smith /*@C 30482d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 30492d5ee99bSBarry Smith 30502d5ee99bSBarry Smith Collective on TS 30512d5ee99bSBarry Smith 30522d5ee99bSBarry Smith Input Parameters: 30532d5ee99bSBarry Smith + ts - the TS context 30542d5ee99bSBarry Smith . step - current time-step 30552d5ee99bSBarry Smith . ptime - current time 30562d5ee99bSBarry Smith - dummy - either a viewer or NULL 30572d5ee99bSBarry Smith 30582d5ee99bSBarry Smith Level: intermediate 30592d5ee99bSBarry Smith 30602d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 30612d5ee99bSBarry Smith 30622d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 30632d5ee99bSBarry Smith @*/ 30642d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 30652d5ee99bSBarry Smith { 30662d5ee99bSBarry Smith PetscErrorCode ierr; 30672d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 30682d5ee99bSBarry Smith PetscDraw draw; 30692d5ee99bSBarry Smith MPI_Comm comm; 30702d5ee99bSBarry Smith PetscInt n; 30712d5ee99bSBarry Smith PetscMPIInt size; 30722d5ee99bSBarry Smith PetscReal xl,yl,xr,yr,tw,w,h; 30732d5ee99bSBarry Smith char time[32]; 30742d5ee99bSBarry Smith size_t len; 30752d5ee99bSBarry Smith const PetscScalar *U; 30762d5ee99bSBarry Smith 30772d5ee99bSBarry Smith PetscFunctionBegin; 30782d5ee99bSBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 30792d5ee99bSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 30802d5ee99bSBarry Smith if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs"); 30812d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 30822d5ee99bSBarry Smith if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 30832d5ee99bSBarry Smith 30842d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 30852d5ee99bSBarry Smith 30862d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 3087*a4494fc1SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 3088*a4494fc1SBarry Smith if ((U[0] < xl) || (U[1] < yl) || (U[0] > xr) || (U[1] > yr)) { 3089*a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 3090*a4494fc1SBarry Smith PetscFunctionReturn(0); 3091*a4494fc1SBarry Smith } 30922d5ee99bSBarry Smith if (!step) ictx->color++; 30932d5ee99bSBarry Smith ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr); 30942d5ee99bSBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 30952d5ee99bSBarry Smith 30962d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 30972d5ee99bSBarry Smith ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr); 30982d5ee99bSBarry Smith ierr = PetscStrlen(time,&len);CHKERRQ(ierr); 30992d5ee99bSBarry Smith ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr); 31002d5ee99bSBarry Smith w = xl + .5*(xr - xl) - .5*len*tw; 31012d5ee99bSBarry Smith h = yl + .95*(yr - yl); 31022d5ee99bSBarry Smith ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 31032d5ee99bSBarry Smith } 31042d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 31052d5ee99bSBarry Smith PetscFunctionReturn(0); 31062d5ee99bSBarry Smith } 31072d5ee99bSBarry Smith 31081713a123SBarry Smith 31096c699258SBarry Smith #undef __FUNCT__ 311083a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 31116083293cSBarry Smith /*@C 311283a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 31136083293cSBarry Smith 31146083293cSBarry Smith Collective on TS 31156083293cSBarry Smith 31166083293cSBarry Smith Input Parameters: 31176083293cSBarry Smith . ctx - the monitor context 31186083293cSBarry Smith 31196083293cSBarry Smith Level: intermediate 31206083293cSBarry Smith 31216083293cSBarry Smith .keywords: TS, vector, monitor, view 31226083293cSBarry Smith 312383a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 31246083293cSBarry Smith @*/ 312583a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 31266083293cSBarry Smith { 31276083293cSBarry Smith PetscErrorCode ierr; 31286083293cSBarry Smith 31296083293cSBarry Smith PetscFunctionBegin; 313083a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 313183a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 313283a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 31336083293cSBarry Smith PetscFunctionReturn(0); 31346083293cSBarry Smith } 31356083293cSBarry Smith 31366083293cSBarry Smith #undef __FUNCT__ 313783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 31386083293cSBarry Smith /*@C 313983a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 31406083293cSBarry Smith 31416083293cSBarry Smith Collective on TS 31426083293cSBarry Smith 31436083293cSBarry Smith Input Parameter: 31446083293cSBarry Smith . ts - time-step context 31456083293cSBarry Smith 31466083293cSBarry Smith Output Patameter: 31476083293cSBarry Smith . ctx - the monitor context 31486083293cSBarry Smith 314999fdda47SBarry Smith Options Database: 315099fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 315199fdda47SBarry Smith 31526083293cSBarry Smith Level: intermediate 31536083293cSBarry Smith 31546083293cSBarry Smith .keywords: TS, vector, monitor, view 31556083293cSBarry Smith 315683a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 31576083293cSBarry Smith @*/ 315883a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 31596083293cSBarry Smith { 31606083293cSBarry Smith PetscErrorCode ierr; 31616083293cSBarry Smith 31626083293cSBarry Smith PetscFunctionBegin; 316383a4ac43SBarry Smith ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr); 316483a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 3165e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 3166bbd56ea5SKarl Rupp 316783a4ac43SBarry Smith (*ctx)->howoften = howoften; 3168473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 31690298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 3170473a3ab2SBarry Smith 3171473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 31720298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 31732d5ee99bSBarry Smith (*ctx)->color = PETSC_DRAW_WHITE; 31746083293cSBarry Smith PetscFunctionReturn(0); 31756083293cSBarry Smith } 31766083293cSBarry Smith 31776083293cSBarry Smith #undef __FUNCT__ 317883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 31793a471f94SBarry Smith /*@C 318083a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 31813a471f94SBarry Smith VecView() for the error at each timestep 31823a471f94SBarry Smith 31833a471f94SBarry Smith Collective on TS 31843a471f94SBarry Smith 31853a471f94SBarry Smith Input Parameters: 31863a471f94SBarry Smith + ts - the TS context 31873a471f94SBarry Smith . step - current time-step 31883a471f94SBarry Smith . ptime - current time 31890298fd71SBarry Smith - dummy - either a viewer or NULL 31903a471f94SBarry Smith 31913a471f94SBarry Smith Level: intermediate 31923a471f94SBarry Smith 31933a471f94SBarry Smith .keywords: TS, vector, monitor, view 31943a471f94SBarry Smith 31953a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 31963a471f94SBarry Smith @*/ 31970910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 31983a471f94SBarry Smith { 31993a471f94SBarry Smith PetscErrorCode ierr; 320083a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 320183a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 32023a471f94SBarry Smith Vec work; 32033a471f94SBarry Smith 32043a471f94SBarry Smith PetscFunctionBegin; 32053fbbecb0SBarry Smith if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0); 32060910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 32073a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 32080910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 32093a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 32103a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 32113a471f94SBarry Smith PetscFunctionReturn(0); 32123a471f94SBarry Smith } 32133a471f94SBarry Smith 32142a34c10cSBarry Smith #include <petsc-private/dmimpl.h> 32153a471f94SBarry Smith #undef __FUNCT__ 32166c699258SBarry Smith #define __FUNCT__ "TSSetDM" 32176c699258SBarry Smith /*@ 32186c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 32196c699258SBarry Smith 32203f9fe445SBarry Smith Logically Collective on TS and DM 32216c699258SBarry Smith 32226c699258SBarry Smith Input Parameters: 32236c699258SBarry Smith + ts - the preconditioner context 32246c699258SBarry Smith - dm - the dm 32256c699258SBarry Smith 32266c699258SBarry Smith Level: intermediate 32276c699258SBarry Smith 32286c699258SBarry Smith 32296c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 32306c699258SBarry Smith @*/ 32317087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 32326c699258SBarry Smith { 32336c699258SBarry Smith PetscErrorCode ierr; 3234089b2837SJed Brown SNES snes; 3235942e3340SBarry Smith DMTS tsdm; 32366c699258SBarry Smith 32376c699258SBarry Smith PetscFunctionBegin; 32380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 323970663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 3240942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 32412a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 3242942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 3243942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 324424989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 324524989b8cSPeter Brune tsdm->originaldm = dm; 324624989b8cSPeter Brune } 324724989b8cSPeter Brune } 32486bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 324924989b8cSPeter Brune } 32506c699258SBarry Smith ts->dm = dm; 3251bbd56ea5SKarl Rupp 3252089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3253089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 32546c699258SBarry Smith PetscFunctionReturn(0); 32556c699258SBarry Smith } 32566c699258SBarry Smith 32576c699258SBarry Smith #undef __FUNCT__ 32586c699258SBarry Smith #define __FUNCT__ "TSGetDM" 32596c699258SBarry Smith /*@ 32606c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 32616c699258SBarry Smith 32623f9fe445SBarry Smith Not Collective 32636c699258SBarry Smith 32646c699258SBarry Smith Input Parameter: 32656c699258SBarry Smith . ts - the preconditioner context 32666c699258SBarry Smith 32676c699258SBarry Smith Output Parameter: 32686c699258SBarry Smith . dm - the dm 32696c699258SBarry Smith 32706c699258SBarry Smith Level: intermediate 32716c699258SBarry Smith 32726c699258SBarry Smith 32736c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 32746c699258SBarry Smith @*/ 32757087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 32766c699258SBarry Smith { 3277496e6a7aSJed Brown PetscErrorCode ierr; 3278496e6a7aSJed Brown 32796c699258SBarry Smith PetscFunctionBegin; 32800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3281496e6a7aSJed Brown if (!ts->dm) { 3282ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 3283496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 3284496e6a7aSJed Brown } 32856c699258SBarry Smith *dm = ts->dm; 32866c699258SBarry Smith PetscFunctionReturn(0); 32876c699258SBarry Smith } 32881713a123SBarry Smith 32890f5c6efeSJed Brown #undef __FUNCT__ 32900f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 32910f5c6efeSJed Brown /*@ 32920f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 32930f5c6efeSJed Brown 32943f9fe445SBarry Smith Logically Collective on SNES 32950f5c6efeSJed Brown 32960f5c6efeSJed Brown Input Parameter: 3297d42a1c89SJed Brown + snes - nonlinear solver 32980910c330SBarry Smith . U - the current state at which to evaluate the residual 3299d42a1c89SJed Brown - ctx - user context, must be a TS 33000f5c6efeSJed Brown 33010f5c6efeSJed Brown Output Parameter: 33020f5c6efeSJed Brown . F - the nonlinear residual 33030f5c6efeSJed Brown 33040f5c6efeSJed Brown Notes: 33050f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 33060f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 33070f5c6efeSJed Brown 33080f5c6efeSJed Brown Level: advanced 33090f5c6efeSJed Brown 33100f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 33110f5c6efeSJed Brown @*/ 33120910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 33130f5c6efeSJed Brown { 33140f5c6efeSJed Brown TS ts = (TS)ctx; 33150f5c6efeSJed Brown PetscErrorCode ierr; 33160f5c6efeSJed Brown 33170f5c6efeSJed Brown PetscFunctionBegin; 33180f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 33190910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 33200f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 33210f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 33220910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 33230f5c6efeSJed Brown PetscFunctionReturn(0); 33240f5c6efeSJed Brown } 33250f5c6efeSJed Brown 33260f5c6efeSJed Brown #undef __FUNCT__ 33270f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 33280f5c6efeSJed Brown /*@ 33290f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 33300f5c6efeSJed Brown 33310f5c6efeSJed Brown Collective on SNES 33320f5c6efeSJed Brown 33330f5c6efeSJed Brown Input Parameter: 33340f5c6efeSJed Brown + snes - nonlinear solver 33350910c330SBarry Smith . U - the current state at which to evaluate the residual 33360f5c6efeSJed Brown - ctx - user context, must be a TS 33370f5c6efeSJed Brown 33380f5c6efeSJed Brown Output Parameter: 33390f5c6efeSJed Brown + A - the Jacobian 33400f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 33410f5c6efeSJed Brown - flag - indicates any structure change in the matrix 33420f5c6efeSJed Brown 33430f5c6efeSJed Brown Notes: 33440f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 33450f5c6efeSJed Brown 33460f5c6efeSJed Brown Level: developer 33470f5c6efeSJed Brown 33480f5c6efeSJed Brown .seealso: SNESSetJacobian() 33490f5c6efeSJed Brown @*/ 33500910c330SBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx) 33510f5c6efeSJed Brown { 33520f5c6efeSJed Brown TS ts = (TS)ctx; 33530f5c6efeSJed Brown PetscErrorCode ierr; 33540f5c6efeSJed Brown 33550f5c6efeSJed Brown PetscFunctionBegin; 33560f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 33570910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 33580f5c6efeSJed Brown PetscValidPointer(A,3); 33590f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 33600f5c6efeSJed Brown PetscValidPointer(B,4); 33610f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 33620f5c6efeSJed Brown PetscValidPointer(flag,5); 33630f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 33640910c330SBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr); 33650f5c6efeSJed Brown PetscFunctionReturn(0); 33660f5c6efeSJed Brown } 3367325fc9f4SBarry Smith 33680e4ef248SJed Brown #undef __FUNCT__ 33690e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 33700e4ef248SJed Brown /*@C 33710e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 33720e4ef248SJed Brown 33730e4ef248SJed Brown Collective on TS 33740e4ef248SJed Brown 33750e4ef248SJed Brown Input Arguments: 33760e4ef248SJed Brown + ts - time stepping context 33770e4ef248SJed Brown . t - time at which to evaluate 33780910c330SBarry Smith . U - state at which to evaluate 33790e4ef248SJed Brown - ctx - context 33800e4ef248SJed Brown 33810e4ef248SJed Brown Output Arguments: 33820e4ef248SJed Brown . F - right hand side 33830e4ef248SJed Brown 33840e4ef248SJed Brown Level: intermediate 33850e4ef248SJed Brown 33860e4ef248SJed Brown Notes: 33870e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 33880e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 33890e4ef248SJed Brown 33900e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 33910e4ef248SJed Brown @*/ 33920910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 33930e4ef248SJed Brown { 33940e4ef248SJed Brown PetscErrorCode ierr; 33950e4ef248SJed Brown Mat Arhs,Brhs; 33960e4ef248SJed Brown MatStructure flg2; 33970e4ef248SJed Brown 33980e4ef248SJed Brown PetscFunctionBegin; 33990e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 34000910c330SBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 34010910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 34020e4ef248SJed Brown PetscFunctionReturn(0); 34030e4ef248SJed Brown } 34040e4ef248SJed Brown 34050e4ef248SJed Brown #undef __FUNCT__ 34060e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 34070e4ef248SJed Brown /*@C 34080e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 34090e4ef248SJed Brown 34100e4ef248SJed Brown Collective on TS 34110e4ef248SJed Brown 34120e4ef248SJed Brown Input Arguments: 34130e4ef248SJed Brown + ts - time stepping context 34140e4ef248SJed Brown . t - time at which to evaluate 34150910c330SBarry Smith . U - state at which to evaluate 34160e4ef248SJed Brown - ctx - context 34170e4ef248SJed Brown 34180e4ef248SJed Brown Output Arguments: 34190e4ef248SJed Brown + A - pointer to operator 34200e4ef248SJed Brown . B - pointer to preconditioning matrix 34210e4ef248SJed Brown - flg - matrix structure flag 34220e4ef248SJed Brown 34230e4ef248SJed Brown Level: intermediate 34240e4ef248SJed Brown 34250e4ef248SJed Brown Notes: 34260e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 34270e4ef248SJed Brown 34280e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 34290e4ef248SJed Brown @*/ 34300910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx) 34310e4ef248SJed Brown { 34320e4ef248SJed Brown PetscFunctionBegin; 34330e4ef248SJed Brown *flg = SAME_PRECONDITIONER; 34340e4ef248SJed Brown PetscFunctionReturn(0); 34350e4ef248SJed Brown } 34360e4ef248SJed Brown 34370026cea9SSean Farley #undef __FUNCT__ 34380026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 34390026cea9SSean Farley /*@C 34400026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 34410026cea9SSean Farley 34420026cea9SSean Farley Collective on TS 34430026cea9SSean Farley 34440026cea9SSean Farley Input Arguments: 34450026cea9SSean Farley + ts - time stepping context 34460026cea9SSean Farley . t - time at which to evaluate 34470910c330SBarry Smith . U - state at which to evaluate 34480910c330SBarry Smith . Udot - time derivative of state vector 34490026cea9SSean Farley - ctx - context 34500026cea9SSean Farley 34510026cea9SSean Farley Output Arguments: 34520026cea9SSean Farley . F - left hand side 34530026cea9SSean Farley 34540026cea9SSean Farley Level: intermediate 34550026cea9SSean Farley 34560026cea9SSean Farley Notes: 34570910c330SBarry 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 34580026cea9SSean Farley user is required to write their own TSComputeIFunction. 34590026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 34600026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 34610026cea9SSean Farley 34620026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 34630026cea9SSean Farley @*/ 34640910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 34650026cea9SSean Farley { 34660026cea9SSean Farley PetscErrorCode ierr; 34670026cea9SSean Farley Mat A,B; 34680026cea9SSean Farley MatStructure flg2; 34690026cea9SSean Farley 34700026cea9SSean Farley PetscFunctionBegin; 34710298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 34720910c330SBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr); 34730910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 34740026cea9SSean Farley PetscFunctionReturn(0); 34750026cea9SSean Farley } 34760026cea9SSean Farley 34770026cea9SSean Farley #undef __FUNCT__ 34780026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 34790026cea9SSean Farley /*@C 348024e94211SJed Brown TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent. 34810026cea9SSean Farley 34820026cea9SSean Farley Collective on TS 34830026cea9SSean Farley 34840026cea9SSean Farley Input Arguments: 34850026cea9SSean Farley + ts - time stepping context 34860026cea9SSean Farley . t - time at which to evaluate 34870910c330SBarry Smith . U - state at which to evaluate 34880910c330SBarry Smith . Udot - time derivative of state vector 34890026cea9SSean Farley . shift - shift to apply 34900026cea9SSean Farley - ctx - context 34910026cea9SSean Farley 34920026cea9SSean Farley Output Arguments: 34930026cea9SSean Farley + A - pointer to operator 34940026cea9SSean Farley . B - pointer to preconditioning matrix 34950026cea9SSean Farley - flg - matrix structure flag 34960026cea9SSean Farley 34970026cea9SSean Farley Level: intermediate 34980026cea9SSean Farley 34990026cea9SSean Farley Notes: 35000026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 35010026cea9SSean Farley 35020026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 35030026cea9SSean Farley @*/ 35040910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx) 35050026cea9SSean Farley { 35060026cea9SSean Farley PetscFunctionBegin; 35070026cea9SSean Farley *flg = SAME_PRECONDITIONER; 35080026cea9SSean Farley PetscFunctionReturn(0); 35090026cea9SSean Farley } 3510e817cc15SEmil Constantinescu #undef __FUNCT__ 3511e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType" 3512e817cc15SEmil Constantinescu /*@ 3513e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 3514e817cc15SEmil Constantinescu 3515e817cc15SEmil Constantinescu Not Collective 3516e817cc15SEmil Constantinescu 3517e817cc15SEmil Constantinescu Input Parameter: 3518e817cc15SEmil Constantinescu . ts - the TS context 3519e817cc15SEmil Constantinescu 3520e817cc15SEmil Constantinescu Output Parameter: 3521e817cc15SEmil Constantinescu . equation_type - see TSEquatioType 3522e817cc15SEmil Constantinescu 3523e817cc15SEmil Constantinescu Level: beginner 3524e817cc15SEmil Constantinescu 3525e817cc15SEmil Constantinescu .keywords: TS, equation type 3526e817cc15SEmil Constantinescu 3527e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 3528e817cc15SEmil Constantinescu @*/ 3529e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 3530e817cc15SEmil Constantinescu { 3531e817cc15SEmil Constantinescu PetscFunctionBegin; 3532e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3533e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 3534e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 3535e817cc15SEmil Constantinescu PetscFunctionReturn(0); 3536e817cc15SEmil Constantinescu } 3537e817cc15SEmil Constantinescu 3538e817cc15SEmil Constantinescu #undef __FUNCT__ 3539e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType" 3540e817cc15SEmil Constantinescu /*@ 3541e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 3542e817cc15SEmil Constantinescu 3543e817cc15SEmil Constantinescu Not Collective 3544e817cc15SEmil Constantinescu 3545e817cc15SEmil Constantinescu Input Parameter: 3546e817cc15SEmil Constantinescu + ts - the TS context 3547e817cc15SEmil Constantinescu . equation_type - see TSEquatioType 3548e817cc15SEmil Constantinescu 3549e817cc15SEmil Constantinescu Level: advanced 3550e817cc15SEmil Constantinescu 3551e817cc15SEmil Constantinescu .keywords: TS, equation type 3552e817cc15SEmil Constantinescu 3553e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 3554e817cc15SEmil Constantinescu @*/ 3555e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 3556e817cc15SEmil Constantinescu { 3557e817cc15SEmil Constantinescu PetscFunctionBegin; 3558e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3559e817cc15SEmil Constantinescu ts->equation_type = equation_type; 3560e817cc15SEmil Constantinescu PetscFunctionReturn(0); 3561e817cc15SEmil Constantinescu } 35620026cea9SSean Farley 35634af1b03aSJed Brown #undef __FUNCT__ 35644af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 35654af1b03aSJed Brown /*@ 35664af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 35674af1b03aSJed Brown 35684af1b03aSJed Brown Not Collective 35694af1b03aSJed Brown 35704af1b03aSJed Brown Input Parameter: 35714af1b03aSJed Brown . ts - the TS context 35724af1b03aSJed Brown 35734af1b03aSJed Brown Output Parameter: 35744af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 35754af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 35764af1b03aSJed Brown 3577487e0bb9SJed Brown Level: beginner 35784af1b03aSJed Brown 3579cd652676SJed Brown Notes: 3580cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 35814af1b03aSJed Brown 35824af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 35834af1b03aSJed Brown 35844af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 35854af1b03aSJed Brown @*/ 35864af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 35874af1b03aSJed Brown { 35884af1b03aSJed Brown PetscFunctionBegin; 35894af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 35904af1b03aSJed Brown PetscValidPointer(reason,2); 35914af1b03aSJed Brown *reason = ts->reason; 35924af1b03aSJed Brown PetscFunctionReturn(0); 35934af1b03aSJed Brown } 35944af1b03aSJed Brown 3595fb1732b5SBarry Smith #undef __FUNCT__ 3596d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 3597d6ad946cSShri Abhyankar /*@ 3598d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 3599d6ad946cSShri Abhyankar 3600d6ad946cSShri Abhyankar Not Collective 3601d6ad946cSShri Abhyankar 3602d6ad946cSShri Abhyankar Input Parameter: 3603d6ad946cSShri Abhyankar + ts - the TS context 3604d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 3605d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 3606d6ad946cSShri Abhyankar 3607f5abba47SShri Abhyankar Level: advanced 3608d6ad946cSShri Abhyankar 3609d6ad946cSShri Abhyankar Notes: 3610d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 3611d6ad946cSShri Abhyankar 3612d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 3613d6ad946cSShri Abhyankar 3614d6ad946cSShri Abhyankar .seealso: TSConvergedReason 3615d6ad946cSShri Abhyankar @*/ 3616d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 3617d6ad946cSShri Abhyankar { 3618d6ad946cSShri Abhyankar PetscFunctionBegin; 3619d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3620d6ad946cSShri Abhyankar ts->reason = reason; 3621d6ad946cSShri Abhyankar PetscFunctionReturn(0); 3622d6ad946cSShri Abhyankar } 3623d6ad946cSShri Abhyankar 3624d6ad946cSShri Abhyankar #undef __FUNCT__ 3625cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 3626cc708dedSBarry Smith /*@ 3627cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 3628cc708dedSBarry Smith 3629cc708dedSBarry Smith Not Collective 3630cc708dedSBarry Smith 3631cc708dedSBarry Smith Input Parameter: 3632cc708dedSBarry Smith . ts - the TS context 3633cc708dedSBarry Smith 3634cc708dedSBarry Smith Output Parameter: 3635cc708dedSBarry Smith . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 3636cc708dedSBarry Smith 3637487e0bb9SJed Brown Level: beginner 3638cc708dedSBarry Smith 3639cc708dedSBarry Smith Notes: 3640cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 3641cc708dedSBarry Smith 3642cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 3643cc708dedSBarry Smith 3644cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 3645cc708dedSBarry Smith @*/ 3646cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 3647cc708dedSBarry Smith { 3648cc708dedSBarry Smith PetscFunctionBegin; 3649cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3650cc708dedSBarry Smith PetscValidPointer(ftime,2); 3651cc708dedSBarry Smith *ftime = ts->solvetime; 3652cc708dedSBarry Smith PetscFunctionReturn(0); 3653cc708dedSBarry Smith } 3654cc708dedSBarry Smith 3655cc708dedSBarry Smith #undef __FUNCT__ 36565ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 36579f67acb7SJed Brown /*@ 36585ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 36599f67acb7SJed Brown used by the time integrator. 36609f67acb7SJed Brown 36619f67acb7SJed Brown Not Collective 36629f67acb7SJed Brown 36639f67acb7SJed Brown Input Parameter: 36649f67acb7SJed Brown . ts - TS context 36659f67acb7SJed Brown 36669f67acb7SJed Brown Output Parameter: 36679f67acb7SJed Brown . nits - number of nonlinear iterations 36689f67acb7SJed Brown 36699f67acb7SJed Brown Notes: 36709f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 36719f67acb7SJed Brown 36729f67acb7SJed Brown Level: intermediate 36739f67acb7SJed Brown 36749f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 36759f67acb7SJed Brown 36765ef26d82SJed Brown .seealso: TSGetKSPIterations() 36779f67acb7SJed Brown @*/ 36785ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 36799f67acb7SJed Brown { 36809f67acb7SJed Brown PetscFunctionBegin; 36819f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36829f67acb7SJed Brown PetscValidIntPointer(nits,2); 36835ef26d82SJed Brown *nits = ts->snes_its; 36849f67acb7SJed Brown PetscFunctionReturn(0); 36859f67acb7SJed Brown } 36869f67acb7SJed Brown 36879f67acb7SJed Brown #undef __FUNCT__ 36885ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 36899f67acb7SJed Brown /*@ 36905ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 36919f67acb7SJed Brown used by the time integrator. 36929f67acb7SJed Brown 36939f67acb7SJed Brown Not Collective 36949f67acb7SJed Brown 36959f67acb7SJed Brown Input Parameter: 36969f67acb7SJed Brown . ts - TS context 36979f67acb7SJed Brown 36989f67acb7SJed Brown Output Parameter: 36999f67acb7SJed Brown . lits - number of linear iterations 37009f67acb7SJed Brown 37019f67acb7SJed Brown Notes: 37029f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 37039f67acb7SJed Brown 37049f67acb7SJed Brown Level: intermediate 37059f67acb7SJed Brown 37069f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 37079f67acb7SJed Brown 37085ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 37099f67acb7SJed Brown @*/ 37105ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 37119f67acb7SJed Brown { 37129f67acb7SJed Brown PetscFunctionBegin; 37139f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 37149f67acb7SJed Brown PetscValidIntPointer(lits,2); 37155ef26d82SJed Brown *lits = ts->ksp_its; 37169f67acb7SJed Brown PetscFunctionReturn(0); 37179f67acb7SJed Brown } 37189f67acb7SJed Brown 37199f67acb7SJed Brown #undef __FUNCT__ 3720cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 3721cef5090cSJed Brown /*@ 3722cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 3723cef5090cSJed Brown 3724cef5090cSJed Brown Not Collective 3725cef5090cSJed Brown 3726cef5090cSJed Brown Input Parameter: 3727cef5090cSJed Brown . ts - TS context 3728cef5090cSJed Brown 3729cef5090cSJed Brown Output Parameter: 3730cef5090cSJed Brown . rejects - number of steps rejected 3731cef5090cSJed Brown 3732cef5090cSJed Brown Notes: 3733cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 3734cef5090cSJed Brown 3735cef5090cSJed Brown Level: intermediate 3736cef5090cSJed Brown 3737cef5090cSJed Brown .keywords: TS, get, number 3738cef5090cSJed Brown 37395ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 3740cef5090cSJed Brown @*/ 3741cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 3742cef5090cSJed Brown { 3743cef5090cSJed Brown PetscFunctionBegin; 3744cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3745cef5090cSJed Brown PetscValidIntPointer(rejects,2); 3746cef5090cSJed Brown *rejects = ts->reject; 3747cef5090cSJed Brown PetscFunctionReturn(0); 3748cef5090cSJed Brown } 3749cef5090cSJed Brown 3750cef5090cSJed Brown #undef __FUNCT__ 3751cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 3752cef5090cSJed Brown /*@ 3753cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 3754cef5090cSJed Brown 3755cef5090cSJed Brown Not Collective 3756cef5090cSJed Brown 3757cef5090cSJed Brown Input Parameter: 3758cef5090cSJed Brown . ts - TS context 3759cef5090cSJed Brown 3760cef5090cSJed Brown Output Parameter: 3761cef5090cSJed Brown . fails - number of failed nonlinear solves 3762cef5090cSJed Brown 3763cef5090cSJed Brown Notes: 3764cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 3765cef5090cSJed Brown 3766cef5090cSJed Brown Level: intermediate 3767cef5090cSJed Brown 3768cef5090cSJed Brown .keywords: TS, get, number 3769cef5090cSJed Brown 37705ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 3771cef5090cSJed Brown @*/ 3772cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 3773cef5090cSJed Brown { 3774cef5090cSJed Brown PetscFunctionBegin; 3775cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3776cef5090cSJed Brown PetscValidIntPointer(fails,2); 3777cef5090cSJed Brown *fails = ts->num_snes_failures; 3778cef5090cSJed Brown PetscFunctionReturn(0); 3779cef5090cSJed Brown } 3780cef5090cSJed Brown 3781cef5090cSJed Brown #undef __FUNCT__ 3782cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 3783cef5090cSJed Brown /*@ 3784cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 3785cef5090cSJed Brown 3786cef5090cSJed Brown Not Collective 3787cef5090cSJed Brown 3788cef5090cSJed Brown Input Parameter: 3789cef5090cSJed Brown + ts - TS context 3790cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 3791cef5090cSJed Brown 3792cef5090cSJed Brown Notes: 3793cef5090cSJed Brown The counter is reset to zero for each step 3794cef5090cSJed Brown 3795cef5090cSJed Brown Options Database Key: 3796cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 3797cef5090cSJed Brown 3798cef5090cSJed Brown Level: intermediate 3799cef5090cSJed Brown 3800cef5090cSJed Brown .keywords: TS, set, maximum, number 3801cef5090cSJed Brown 38025ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3803cef5090cSJed Brown @*/ 3804cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 3805cef5090cSJed Brown { 3806cef5090cSJed Brown PetscFunctionBegin; 3807cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3808cef5090cSJed Brown ts->max_reject = rejects; 3809cef5090cSJed Brown PetscFunctionReturn(0); 3810cef5090cSJed Brown } 3811cef5090cSJed Brown 3812cef5090cSJed Brown #undef __FUNCT__ 3813cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 3814cef5090cSJed Brown /*@ 3815cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 3816cef5090cSJed Brown 3817cef5090cSJed Brown Not Collective 3818cef5090cSJed Brown 3819cef5090cSJed Brown Input Parameter: 3820cef5090cSJed Brown + ts - TS context 3821cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 3822cef5090cSJed Brown 3823cef5090cSJed Brown Notes: 3824cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 3825cef5090cSJed Brown 3826cef5090cSJed Brown Options Database Key: 3827cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 3828cef5090cSJed Brown 3829cef5090cSJed Brown Level: intermediate 3830cef5090cSJed Brown 3831cef5090cSJed Brown .keywords: TS, set, maximum, number 3832cef5090cSJed Brown 38335ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 3834cef5090cSJed Brown @*/ 3835cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 3836cef5090cSJed Brown { 3837cef5090cSJed Brown PetscFunctionBegin; 3838cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3839cef5090cSJed Brown ts->max_snes_failures = fails; 3840cef5090cSJed Brown PetscFunctionReturn(0); 3841cef5090cSJed Brown } 3842cef5090cSJed Brown 3843cef5090cSJed Brown #undef __FUNCT__ 3844cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()" 3845cef5090cSJed Brown /*@ 3846cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 3847cef5090cSJed Brown 3848cef5090cSJed Brown Not Collective 3849cef5090cSJed Brown 3850cef5090cSJed Brown Input Parameter: 3851cef5090cSJed Brown + ts - TS context 3852cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 3853cef5090cSJed Brown 3854cef5090cSJed Brown Options Database Key: 3855cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 3856cef5090cSJed Brown 3857cef5090cSJed Brown Level: intermediate 3858cef5090cSJed Brown 3859cef5090cSJed Brown .keywords: TS, set, error 3860cef5090cSJed Brown 38615ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3862cef5090cSJed Brown @*/ 3863cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 3864cef5090cSJed Brown { 3865cef5090cSJed Brown PetscFunctionBegin; 3866cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3867cef5090cSJed Brown ts->errorifstepfailed = err; 3868cef5090cSJed Brown PetscFunctionReturn(0); 3869cef5090cSJed Brown } 3870cef5090cSJed Brown 3871cef5090cSJed Brown #undef __FUNCT__ 3872fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 3873fb1732b5SBarry Smith /*@C 3874fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 3875fb1732b5SBarry Smith 3876fb1732b5SBarry Smith Collective on TS 3877fb1732b5SBarry Smith 3878fb1732b5SBarry Smith Input Parameters: 3879fb1732b5SBarry Smith + ts - the TS context 3880fb1732b5SBarry Smith . step - current time-step 3881fb1732b5SBarry Smith . ptime - current time 38820910c330SBarry Smith . u - current state 3883fb1732b5SBarry Smith - viewer - binary viewer 3884fb1732b5SBarry Smith 3885fb1732b5SBarry Smith Level: intermediate 3886fb1732b5SBarry Smith 3887fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 3888fb1732b5SBarry Smith 3889fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3890fb1732b5SBarry Smith @*/ 38910910c330SBarry Smith PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 3892fb1732b5SBarry Smith { 3893fb1732b5SBarry Smith PetscErrorCode ierr; 3894ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 3895fb1732b5SBarry Smith 3896fb1732b5SBarry Smith PetscFunctionBegin; 38970910c330SBarry Smith ierr = VecView(u,v);CHKERRQ(ierr); 3898ed81e22dSJed Brown PetscFunctionReturn(0); 3899ed81e22dSJed Brown } 3900ed81e22dSJed Brown 3901ed81e22dSJed Brown #undef __FUNCT__ 3902ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 3903ed81e22dSJed Brown /*@C 3904ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 3905ed81e22dSJed Brown 3906ed81e22dSJed Brown Collective on TS 3907ed81e22dSJed Brown 3908ed81e22dSJed Brown Input Parameters: 3909ed81e22dSJed Brown + ts - the TS context 3910ed81e22dSJed Brown . step - current time-step 3911ed81e22dSJed Brown . ptime - current time 39120910c330SBarry Smith . u - current state 3913ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3914ed81e22dSJed Brown 3915ed81e22dSJed Brown Level: intermediate 3916ed81e22dSJed Brown 3917ed81e22dSJed Brown Notes: 3918ed81e22dSJed 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. 3919ed81e22dSJed Brown These are named according to the file name template. 3920ed81e22dSJed Brown 3921ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 3922ed81e22dSJed Brown 3923ed81e22dSJed Brown .keywords: TS, vector, monitor, view 3924ed81e22dSJed Brown 3925ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3926ed81e22dSJed Brown @*/ 39270910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 3928ed81e22dSJed Brown { 3929ed81e22dSJed Brown PetscErrorCode ierr; 3930ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 3931ed81e22dSJed Brown PetscViewer viewer; 3932ed81e22dSJed Brown 3933ed81e22dSJed Brown PetscFunctionBegin; 39348caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 3935ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 39360910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 3937ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 3938ed81e22dSJed Brown PetscFunctionReturn(0); 3939ed81e22dSJed Brown } 3940ed81e22dSJed Brown 3941ed81e22dSJed Brown #undef __FUNCT__ 3942ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 3943ed81e22dSJed Brown /*@C 3944ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 3945ed81e22dSJed Brown 3946ed81e22dSJed Brown Collective on TS 3947ed81e22dSJed Brown 3948ed81e22dSJed Brown Input Parameters: 3949ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3950ed81e22dSJed Brown 3951ed81e22dSJed Brown Level: intermediate 3952ed81e22dSJed Brown 3953ed81e22dSJed Brown Note: 3954ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 3955ed81e22dSJed Brown 3956ed81e22dSJed Brown .keywords: TS, vector, monitor, view 3957ed81e22dSJed Brown 3958ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 3959ed81e22dSJed Brown @*/ 3960ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 3961ed81e22dSJed Brown { 3962ed81e22dSJed Brown PetscErrorCode ierr; 3963ed81e22dSJed Brown 3964ed81e22dSJed Brown PetscFunctionBegin; 3965ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 3966fb1732b5SBarry Smith PetscFunctionReturn(0); 3967fb1732b5SBarry Smith } 3968fb1732b5SBarry Smith 396984df9cb4SJed Brown #undef __FUNCT__ 3970ad6bc421SBarry Smith #define __FUNCT__ "TSGetTSAdapt" 397184df9cb4SJed Brown /*@ 3972ad6bc421SBarry Smith TSGetTSAdapt - Get the adaptive controller context for the current method 397384df9cb4SJed Brown 3974ed81e22dSJed Brown Collective on TS if controller has not been created yet 397584df9cb4SJed Brown 397684df9cb4SJed Brown Input Arguments: 3977ed81e22dSJed Brown . ts - time stepping context 397884df9cb4SJed Brown 397984df9cb4SJed Brown Output Arguments: 3980ed81e22dSJed Brown . adapt - adaptive controller 398184df9cb4SJed Brown 398284df9cb4SJed Brown Level: intermediate 398384df9cb4SJed Brown 3984ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 398584df9cb4SJed Brown @*/ 3986ad6bc421SBarry Smith PetscErrorCode TSGetTSAdapt(TS ts,TSAdapt *adapt) 398784df9cb4SJed Brown { 398884df9cb4SJed Brown PetscErrorCode ierr; 398984df9cb4SJed Brown 399084df9cb4SJed Brown PetscFunctionBegin; 399184df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 399284df9cb4SJed Brown PetscValidPointer(adapt,2); 399384df9cb4SJed Brown if (!ts->adapt) { 3994ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 39951c3436cfSJed Brown ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr); 39961c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 399784df9cb4SJed Brown } 399884df9cb4SJed Brown *adapt = ts->adapt; 399984df9cb4SJed Brown PetscFunctionReturn(0); 400084df9cb4SJed Brown } 4001d6ebe24aSShri Abhyankar 4002d6ebe24aSShri Abhyankar #undef __FUNCT__ 40031c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 40041c3436cfSJed Brown /*@ 40051c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 40061c3436cfSJed Brown 40071c3436cfSJed Brown Logically Collective 40081c3436cfSJed Brown 40091c3436cfSJed Brown Input Arguments: 40101c3436cfSJed Brown + ts - time integration context 40111c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 40120298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 40131c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 40140298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 40151c3436cfSJed Brown 40161c3436cfSJed Brown Level: beginner 40171c3436cfSJed Brown 4018c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 40191c3436cfSJed Brown @*/ 40201c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 40211c3436cfSJed Brown { 40221c3436cfSJed Brown PetscErrorCode ierr; 40231c3436cfSJed Brown 40241c3436cfSJed Brown PetscFunctionBegin; 4025c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 40261c3436cfSJed Brown if (vatol) { 40271c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 40281c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 4029bbd56ea5SKarl Rupp 40301c3436cfSJed Brown ts->vatol = vatol; 40311c3436cfSJed Brown } 4032c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 40331c3436cfSJed Brown if (vrtol) { 40341c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 40351c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 4036bbd56ea5SKarl Rupp 40371c3436cfSJed Brown ts->vrtol = vrtol; 40381c3436cfSJed Brown } 40391c3436cfSJed Brown PetscFunctionReturn(0); 40401c3436cfSJed Brown } 40411c3436cfSJed Brown 40421c3436cfSJed Brown #undef __FUNCT__ 4043c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 4044c5033834SJed Brown /*@ 4045c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 4046c5033834SJed Brown 4047c5033834SJed Brown Logically Collective 4048c5033834SJed Brown 4049c5033834SJed Brown Input Arguments: 4050c5033834SJed Brown . ts - time integration context 4051c5033834SJed Brown 4052c5033834SJed Brown Output Arguments: 40530298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 40540298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 40550298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 40560298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 4057c5033834SJed Brown 4058c5033834SJed Brown Level: beginner 4059c5033834SJed Brown 4060c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 4061c5033834SJed Brown @*/ 4062c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 4063c5033834SJed Brown { 4064c5033834SJed Brown PetscFunctionBegin; 4065c5033834SJed Brown if (atol) *atol = ts->atol; 4066c5033834SJed Brown if (vatol) *vatol = ts->vatol; 4067c5033834SJed Brown if (rtol) *rtol = ts->rtol; 4068c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 4069c5033834SJed Brown PetscFunctionReturn(0); 4070c5033834SJed Brown } 4071c5033834SJed Brown 4072c5033834SJed Brown #undef __FUNCT__ 40731c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS" 40741c3436cfSJed Brown /*@ 40751c3436cfSJed Brown TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 40761c3436cfSJed Brown 40771c3436cfSJed Brown Collective on TS 40781c3436cfSJed Brown 40791c3436cfSJed Brown Input Arguments: 40801c3436cfSJed Brown + ts - time stepping context 40811c3436cfSJed Brown - Y - state vector to be compared to ts->vec_sol 40821c3436cfSJed Brown 40831c3436cfSJed Brown Output Arguments: 40841c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 40851c3436cfSJed Brown 40861c3436cfSJed Brown Level: developer 40871c3436cfSJed Brown 40881c3436cfSJed Brown .seealso: TSSetTolerances() 40891c3436cfSJed Brown @*/ 40901c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 40911c3436cfSJed Brown { 40921c3436cfSJed Brown PetscErrorCode ierr; 40931c3436cfSJed Brown PetscInt i,n,N; 40940910c330SBarry Smith const PetscScalar *u,*y; 40950910c330SBarry Smith Vec U; 40961c3436cfSJed Brown PetscReal sum,gsum; 40971c3436cfSJed Brown 40981c3436cfSJed Brown PetscFunctionBegin; 40991c3436cfSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 41001c3436cfSJed Brown PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 41011c3436cfSJed Brown PetscValidPointer(norm,3); 41020910c330SBarry Smith U = ts->vec_sol; 41030910c330SBarry Smith PetscCheckSameTypeAndComm(U,1,Y,2); 4104ce94432eSBarry Smith if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 41051c3436cfSJed Brown 41060910c330SBarry Smith ierr = VecGetSize(U,&N);CHKERRQ(ierr); 41070910c330SBarry Smith ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 41080910c330SBarry Smith ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 41091c3436cfSJed Brown ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 41101c3436cfSJed Brown sum = 0.; 4111ceefc113SJed Brown if (ts->vatol && ts->vrtol) { 4112ceefc113SJed Brown const PetscScalar *atol,*rtol; 4113ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4114ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4115ceefc113SJed Brown for (i=0; i<n; i++) { 41160910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 41170910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4118ceefc113SJed Brown } 4119ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4120ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4121ceefc113SJed Brown } else if (ts->vatol) { /* vector atol, scalar rtol */ 4122ceefc113SJed Brown const PetscScalar *atol; 4123ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4124ceefc113SJed Brown for (i=0; i<n; i++) { 41250910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 41260910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4127ceefc113SJed Brown } 4128ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4129ceefc113SJed Brown } else if (ts->vrtol) { /* scalar atol, vector rtol */ 4130ceefc113SJed Brown const PetscScalar *rtol; 4131ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4132ceefc113SJed Brown for (i=0; i<n; i++) { 41330910c330SBarry Smith PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 41340910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4135ceefc113SJed Brown } 4136ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4137ceefc113SJed Brown } else { /* scalar atol, scalar rtol */ 41381c3436cfSJed Brown for (i=0; i<n; i++) { 41390910c330SBarry Smith PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 41400910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 41411c3436cfSJed Brown } 4142ceefc113SJed Brown } 41430910c330SBarry Smith ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 41441c3436cfSJed Brown ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 41451c3436cfSJed Brown 4146ce94432eSBarry Smith ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 41471c3436cfSJed Brown *norm = PetscSqrtReal(gsum / N); 4148ce94432eSBarry Smith if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 41491c3436cfSJed Brown PetscFunctionReturn(0); 41501c3436cfSJed Brown } 41511c3436cfSJed Brown 41521c3436cfSJed Brown #undef __FUNCT__ 41538d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 41548d59e960SJed Brown /*@ 41558d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 41568d59e960SJed Brown 41578d59e960SJed Brown Logically Collective on TS 41588d59e960SJed Brown 41598d59e960SJed Brown Input Arguments: 41608d59e960SJed Brown + ts - time stepping context 41618d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 41628d59e960SJed Brown 41638d59e960SJed Brown Note: 41648d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 41658d59e960SJed Brown 41668d59e960SJed Brown Level: intermediate 41678d59e960SJed Brown 41688d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 41698d59e960SJed Brown @*/ 41708d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 41718d59e960SJed Brown { 41728d59e960SJed Brown PetscFunctionBegin; 41738d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 41748d59e960SJed Brown ts->cfltime_local = cfltime; 41758d59e960SJed Brown ts->cfltime = -1.; 41768d59e960SJed Brown PetscFunctionReturn(0); 41778d59e960SJed Brown } 41788d59e960SJed Brown 41798d59e960SJed Brown #undef __FUNCT__ 41808d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 41818d59e960SJed Brown /*@ 41828d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 41838d59e960SJed Brown 41848d59e960SJed Brown Collective on TS 41858d59e960SJed Brown 41868d59e960SJed Brown Input Arguments: 41878d59e960SJed Brown . ts - time stepping context 41888d59e960SJed Brown 41898d59e960SJed Brown Output Arguments: 41908d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 41918d59e960SJed Brown 41928d59e960SJed Brown Level: advanced 41938d59e960SJed Brown 41948d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 41958d59e960SJed Brown @*/ 41968d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 41978d59e960SJed Brown { 41988d59e960SJed Brown PetscErrorCode ierr; 41998d59e960SJed Brown 42008d59e960SJed Brown PetscFunctionBegin; 42018d59e960SJed Brown if (ts->cfltime < 0) { 4202ce94432eSBarry Smith ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 42038d59e960SJed Brown } 42048d59e960SJed Brown *cfltime = ts->cfltime; 42058d59e960SJed Brown PetscFunctionReturn(0); 42068d59e960SJed Brown } 42078d59e960SJed Brown 42088d59e960SJed Brown #undef __FUNCT__ 4209d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 4210d6ebe24aSShri Abhyankar /*@ 4211d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 4212d6ebe24aSShri Abhyankar 4213d6ebe24aSShri Abhyankar Input Parameters: 4214d6ebe24aSShri Abhyankar . ts - the TS context. 4215d6ebe24aSShri Abhyankar . xl - lower bound. 4216d6ebe24aSShri Abhyankar . xu - upper bound. 4217d6ebe24aSShri Abhyankar 4218d6ebe24aSShri Abhyankar Notes: 4219d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 422033c0c2ddSJed Brown SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp(). 4221d6ebe24aSShri Abhyankar 42222bd2b0e6SSatish Balay Level: advanced 42232bd2b0e6SSatish Balay 4224d6ebe24aSShri Abhyankar @*/ 4225d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 4226d6ebe24aSShri Abhyankar { 4227d6ebe24aSShri Abhyankar PetscErrorCode ierr; 4228d6ebe24aSShri Abhyankar SNES snes; 4229d6ebe24aSShri Abhyankar 4230d6ebe24aSShri Abhyankar PetscFunctionBegin; 4231d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4232d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 4233d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 4234d6ebe24aSShri Abhyankar } 4235d6ebe24aSShri Abhyankar 4236325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 4237c6db04a5SJed Brown #include <mex.h> 4238325fc9f4SBarry Smith 4239325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 4240325fc9f4SBarry Smith 4241325fc9f4SBarry Smith #undef __FUNCT__ 4242325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 4243325fc9f4SBarry Smith /* 4244325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 4245325fc9f4SBarry Smith TSSetFunctionMatlab(). 4246325fc9f4SBarry Smith 4247325fc9f4SBarry Smith Collective on TS 4248325fc9f4SBarry Smith 4249325fc9f4SBarry Smith Input Parameters: 4250325fc9f4SBarry Smith + snes - the TS context 42510910c330SBarry Smith - u - input vector 4252325fc9f4SBarry Smith 4253325fc9f4SBarry Smith Output Parameter: 4254325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 4255325fc9f4SBarry Smith 4256325fc9f4SBarry Smith Notes: 4257325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 4258325fc9f4SBarry Smith implementations, so most users would not generally call this routine 4259325fc9f4SBarry Smith themselves. 4260325fc9f4SBarry Smith 4261325fc9f4SBarry Smith Level: developer 4262325fc9f4SBarry Smith 4263325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 4264325fc9f4SBarry Smith 4265325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 4266325fc9f4SBarry Smith */ 42670910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 4268325fc9f4SBarry Smith { 4269325fc9f4SBarry Smith PetscErrorCode ierr; 4270325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 4271325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 4272325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 4273325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 4274325fc9f4SBarry Smith 4275325fc9f4SBarry Smith PetscFunctionBegin; 4276325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 42770910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 42780910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 4279325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 42800910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 4281325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 4282325fc9f4SBarry Smith 4283325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 42840910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 42850910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 42860910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 4287bbd56ea5SKarl Rupp 4288325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 4289325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 4290325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 4291325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 4292325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 4293325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 4294325fc9f4SBarry Smith prhs[6] = sctx->ctx; 4295325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 4296325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4297325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 4298325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 4299325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 4300325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 4301325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 4302325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 4303325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 4304325fc9f4SBarry Smith PetscFunctionReturn(0); 4305325fc9f4SBarry Smith } 4306325fc9f4SBarry Smith 4307325fc9f4SBarry Smith 4308325fc9f4SBarry Smith #undef __FUNCT__ 4309325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 4310325fc9f4SBarry Smith /* 4311325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 4312325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 4313e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 4314325fc9f4SBarry Smith 4315325fc9f4SBarry Smith Logically Collective on TS 4316325fc9f4SBarry Smith 4317325fc9f4SBarry Smith Input Parameters: 4318325fc9f4SBarry Smith + ts - the TS context 4319325fc9f4SBarry Smith - func - function evaluation routine 4320325fc9f4SBarry Smith 4321325fc9f4SBarry Smith Calling sequence of func: 43220910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 4323325fc9f4SBarry Smith 4324325fc9f4SBarry Smith Level: beginner 4325325fc9f4SBarry Smith 4326325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 4327325fc9f4SBarry Smith 4328325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4329325fc9f4SBarry Smith */ 4330cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 4331325fc9f4SBarry Smith { 4332325fc9f4SBarry Smith PetscErrorCode ierr; 4333325fc9f4SBarry Smith TSMatlabContext *sctx; 4334325fc9f4SBarry Smith 4335325fc9f4SBarry Smith PetscFunctionBegin; 4336325fc9f4SBarry Smith /* currently sctx is memory bleed */ 4337325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4338325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4339325fc9f4SBarry Smith /* 4340325fc9f4SBarry Smith This should work, but it doesn't 4341325fc9f4SBarry Smith sctx->ctx = ctx; 4342325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4343325fc9f4SBarry Smith */ 4344325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4345bbd56ea5SKarl Rupp 43460298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 4347325fc9f4SBarry Smith PetscFunctionReturn(0); 4348325fc9f4SBarry Smith } 4349325fc9f4SBarry Smith 4350325fc9f4SBarry Smith #undef __FUNCT__ 4351325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 4352325fc9f4SBarry Smith /* 4353325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 4354325fc9f4SBarry Smith TSSetJacobianMatlab(). 4355325fc9f4SBarry Smith 4356325fc9f4SBarry Smith Collective on TS 4357325fc9f4SBarry Smith 4358325fc9f4SBarry Smith Input Parameters: 4359cdcf91faSSean Farley + ts - the TS context 43600910c330SBarry Smith . u - input vector 4361325fc9f4SBarry Smith . A, B - the matrices 4362325fc9f4SBarry Smith - ctx - user context 4363325fc9f4SBarry Smith 4364325fc9f4SBarry Smith Output Parameter: 4365325fc9f4SBarry Smith . flag - structure of the matrix 4366325fc9f4SBarry Smith 4367325fc9f4SBarry Smith Level: developer 4368325fc9f4SBarry Smith 4369325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 4370325fc9f4SBarry Smith 4371325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 4372325fc9f4SBarry Smith @*/ 43730910c330SBarry Smith PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 4374325fc9f4SBarry Smith { 4375325fc9f4SBarry Smith PetscErrorCode ierr; 4376325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 4377325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 4378325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 4379325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 4380325fc9f4SBarry Smith 4381325fc9f4SBarry Smith PetscFunctionBegin; 4382cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 43830910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 4384325fc9f4SBarry Smith 43850910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 4386325fc9f4SBarry Smith 4387cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 43880910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 43890910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 43900910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 43910910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 4392bbd56ea5SKarl Rupp 4393325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 4394325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 4395325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 4396325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 4397325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 4398325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 4399325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 4400325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 4401325fc9f4SBarry Smith prhs[8] = sctx->ctx; 4402325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 4403325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4404325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 4405325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 4406325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 4407325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 4408325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 4409325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 4410325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 4411325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 4412325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 4413325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 4414325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 4415325fc9f4SBarry Smith PetscFunctionReturn(0); 4416325fc9f4SBarry Smith } 4417325fc9f4SBarry Smith 4418325fc9f4SBarry Smith 4419325fc9f4SBarry Smith #undef __FUNCT__ 4420325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 4421325fc9f4SBarry Smith /* 4422325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 4423e3c5b3baSBarry 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 4424325fc9f4SBarry Smith 4425325fc9f4SBarry Smith Logically Collective on TS 4426325fc9f4SBarry Smith 4427325fc9f4SBarry Smith Input Parameters: 4428cdcf91faSSean Farley + ts - the TS context 4429325fc9f4SBarry Smith . A,B - Jacobian matrices 4430325fc9f4SBarry Smith . func - function evaluation routine 4431325fc9f4SBarry Smith - ctx - user context 4432325fc9f4SBarry Smith 4433325fc9f4SBarry Smith Calling sequence of func: 44340910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 4435325fc9f4SBarry Smith 4436325fc9f4SBarry Smith 4437325fc9f4SBarry Smith Level: developer 4438325fc9f4SBarry Smith 4439325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 4440325fc9f4SBarry Smith 4441325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4442325fc9f4SBarry Smith */ 4443cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 4444325fc9f4SBarry Smith { 4445325fc9f4SBarry Smith PetscErrorCode ierr; 4446325fc9f4SBarry Smith TSMatlabContext *sctx; 4447325fc9f4SBarry Smith 4448325fc9f4SBarry Smith PetscFunctionBegin; 4449325fc9f4SBarry Smith /* currently sctx is memory bleed */ 4450325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4451325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4452325fc9f4SBarry Smith /* 4453325fc9f4SBarry Smith This should work, but it doesn't 4454325fc9f4SBarry Smith sctx->ctx = ctx; 4455325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4456325fc9f4SBarry Smith */ 4457325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4458bbd56ea5SKarl Rupp 4459cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 4460325fc9f4SBarry Smith PetscFunctionReturn(0); 4461325fc9f4SBarry Smith } 4462325fc9f4SBarry Smith 4463b5b1a830SBarry Smith #undef __FUNCT__ 4464b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 4465b5b1a830SBarry Smith /* 4466b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 4467b5b1a830SBarry Smith 4468b5b1a830SBarry Smith Collective on TS 4469b5b1a830SBarry Smith 4470b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 4471b5b1a830SBarry Smith @*/ 44720910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 4473b5b1a830SBarry Smith { 4474b5b1a830SBarry Smith PetscErrorCode ierr; 4475b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 4476a530c242SBarry Smith int nlhs = 1,nrhs = 6; 4477b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 4478b5b1a830SBarry Smith long long int lx = 0,ls = 0; 4479b5b1a830SBarry Smith 4480b5b1a830SBarry Smith PetscFunctionBegin; 4481cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 44820910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4483b5b1a830SBarry Smith 4484cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 44850910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 4486bbd56ea5SKarl Rupp 4487b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 4488b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 4489b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 4490b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 4491b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 4492b5b1a830SBarry Smith prhs[5] = sctx->ctx; 4493b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 4494b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4495b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 4496b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 4497b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 4498b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 4499b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 4500b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 4501b5b1a830SBarry Smith PetscFunctionReturn(0); 4502b5b1a830SBarry Smith } 4503b5b1a830SBarry Smith 4504b5b1a830SBarry Smith 4505b5b1a830SBarry Smith #undef __FUNCT__ 4506b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 4507b5b1a830SBarry Smith /* 4508b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 4509b5b1a830SBarry Smith 4510b5b1a830SBarry Smith Level: developer 4511b5b1a830SBarry Smith 4512b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 4513b5b1a830SBarry Smith 4514b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4515b5b1a830SBarry Smith */ 4516cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 4517b5b1a830SBarry Smith { 4518b5b1a830SBarry Smith PetscErrorCode ierr; 4519b5b1a830SBarry Smith TSMatlabContext *sctx; 4520b5b1a830SBarry Smith 4521b5b1a830SBarry Smith PetscFunctionBegin; 4522b5b1a830SBarry Smith /* currently sctx is memory bleed */ 4523b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4524b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4525b5b1a830SBarry Smith /* 4526b5b1a830SBarry Smith This should work, but it doesn't 4527b5b1a830SBarry Smith sctx->ctx = ctx; 4528b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4529b5b1a830SBarry Smith */ 4530b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4531bbd56ea5SKarl Rupp 45320298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 4533b5b1a830SBarry Smith PetscFunctionReturn(0); 4534b5b1a830SBarry Smith } 4535325fc9f4SBarry Smith #endif 4536b3603a34SBarry Smith 45370b039ecaSBarry Smith 45380b039ecaSBarry Smith 4539b3603a34SBarry Smith #undef __FUNCT__ 45404f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 4541b3603a34SBarry Smith /*@C 45424f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 4543b3603a34SBarry Smith in a time based line graph 4544b3603a34SBarry Smith 4545b3603a34SBarry Smith Collective on TS 4546b3603a34SBarry Smith 4547b3603a34SBarry Smith Input Parameters: 4548b3603a34SBarry Smith + ts - the TS context 4549b3603a34SBarry Smith . step - current time-step 4550b3603a34SBarry Smith . ptime - current time 4551b3603a34SBarry Smith - lg - a line graph object 4552b3603a34SBarry Smith 4553b3603a34SBarry Smith Level: intermediate 4554b3603a34SBarry Smith 45550b039ecaSBarry Smith Notes: each process in a parallel run displays its component solutions in a separate window 4556b3603a34SBarry Smith 4557b3603a34SBarry Smith .keywords: TS, vector, monitor, view 4558b3603a34SBarry Smith 4559b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4560b3603a34SBarry Smith @*/ 45610910c330SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4562b3603a34SBarry Smith { 4563b3603a34SBarry Smith PetscErrorCode ierr; 45640b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4565b3603a34SBarry Smith const PetscScalar *yy; 456658ff32f7SBarry Smith PetscInt dim; 4567b3603a34SBarry Smith 4568b3603a34SBarry Smith PetscFunctionBegin; 456958ff32f7SBarry Smith if (!step) { 4570a9f9c1f6SBarry Smith PetscDrawAxis axis; 4571a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4572a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 45730910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 45740b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 45750b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 457658ff32f7SBarry Smith } 45770910c330SBarry Smith ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr); 4578e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 4579e3efe391SJed Brown { 4580e3efe391SJed Brown PetscReal *yreal; 4581e3efe391SJed Brown PetscInt i,n; 45820910c330SBarry Smith ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr); 4583e3efe391SJed Brown ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4584e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 45850b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4586e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4587e3efe391SJed Brown } 4588e3efe391SJed Brown #else 45890b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4590e3efe391SJed Brown #endif 45910910c330SBarry Smith ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr); 45923fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) { 45930b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 45943923b477SBarry Smith } 4595b3603a34SBarry Smith PetscFunctionReturn(0); 4596b3603a34SBarry Smith } 4597b3603a34SBarry Smith 4598b3603a34SBarry Smith #undef __FUNCT__ 45994f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 4600ef20d060SBarry Smith /*@C 46014f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 4602ef20d060SBarry Smith in a time based line graph 4603ef20d060SBarry Smith 4604ef20d060SBarry Smith Collective on TS 4605ef20d060SBarry Smith 4606ef20d060SBarry Smith Input Parameters: 4607ef20d060SBarry Smith + ts - the TS context 4608ef20d060SBarry Smith . step - current time-step 4609ef20d060SBarry Smith . ptime - current time 4610ef20d060SBarry Smith - lg - a line graph object 4611ef20d060SBarry Smith 4612ef20d060SBarry Smith Level: intermediate 4613ef20d060SBarry Smith 4614abd5a294SJed Brown Notes: 4615abd5a294SJed Brown Only for sequential solves. 4616abd5a294SJed Brown 4617abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 4618abd5a294SJed Brown 4619abd5a294SJed Brown Options Database Keys: 46204f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 4621ef20d060SBarry Smith 4622ef20d060SBarry Smith .keywords: TS, vector, monitor, view 4623ef20d060SBarry Smith 4624abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 4625ef20d060SBarry Smith @*/ 46260910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4627ef20d060SBarry Smith { 4628ef20d060SBarry Smith PetscErrorCode ierr; 46290b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4630ef20d060SBarry Smith const PetscScalar *yy; 4631ef20d060SBarry Smith Vec y; 4632a9f9c1f6SBarry Smith PetscInt dim; 4633ef20d060SBarry Smith 4634ef20d060SBarry Smith PetscFunctionBegin; 4635a9f9c1f6SBarry Smith if (!step) { 4636a9f9c1f6SBarry Smith PetscDrawAxis axis; 4637a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 46381ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 46390910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 4640a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 4641a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4642a9f9c1f6SBarry Smith } 46430910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 4644ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 46450910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 4646ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 4647e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 4648e3efe391SJed Brown { 4649e3efe391SJed Brown PetscReal *yreal; 4650e3efe391SJed Brown PetscInt i,n; 4651e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 4652e3efe391SJed Brown ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4653e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 46540b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4655e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4656e3efe391SJed Brown } 4657e3efe391SJed Brown #else 46580b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4659e3efe391SJed Brown #endif 4660ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 4661ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 46623fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) { 46630b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 46643923b477SBarry Smith } 4665ef20d060SBarry Smith PetscFunctionReturn(0); 4666ef20d060SBarry Smith } 4667ef20d060SBarry Smith 4668201da799SBarry Smith #undef __FUNCT__ 4669201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 4670201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4671201da799SBarry Smith { 4672201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4673201da799SBarry Smith PetscReal x = ptime,y; 4674201da799SBarry Smith PetscErrorCode ierr; 4675201da799SBarry Smith PetscInt its; 4676201da799SBarry Smith 4677201da799SBarry Smith PetscFunctionBegin; 4678201da799SBarry Smith if (!n) { 4679201da799SBarry Smith PetscDrawAxis axis; 4680bbd56ea5SKarl Rupp 4681201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4682201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 4683201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4684bbd56ea5SKarl Rupp 4685201da799SBarry Smith ctx->snes_its = 0; 4686201da799SBarry Smith } 4687201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 4688201da799SBarry Smith y = its - ctx->snes_its; 4689201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 46903fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 4691201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4692201da799SBarry Smith } 4693201da799SBarry Smith ctx->snes_its = its; 4694201da799SBarry Smith PetscFunctionReturn(0); 4695201da799SBarry Smith } 4696201da799SBarry Smith 4697201da799SBarry Smith #undef __FUNCT__ 4698201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 4699201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4700201da799SBarry Smith { 4701201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4702201da799SBarry Smith PetscReal x = ptime,y; 4703201da799SBarry Smith PetscErrorCode ierr; 4704201da799SBarry Smith PetscInt its; 4705201da799SBarry Smith 4706201da799SBarry Smith PetscFunctionBegin; 4707201da799SBarry Smith if (!n) { 4708201da799SBarry Smith PetscDrawAxis axis; 4709bbd56ea5SKarl Rupp 4710201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4711201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 4712201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4713bbd56ea5SKarl Rupp 4714201da799SBarry Smith ctx->ksp_its = 0; 4715201da799SBarry Smith } 4716201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 4717201da799SBarry Smith y = its - ctx->ksp_its; 4718201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 471999fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 4720201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4721201da799SBarry Smith } 4722201da799SBarry Smith ctx->ksp_its = its; 4723201da799SBarry Smith PetscFunctionReturn(0); 4724201da799SBarry Smith } 4725f9c1d6abSBarry Smith 4726f9c1d6abSBarry Smith #undef __FUNCT__ 4727f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 4728f9c1d6abSBarry Smith /*@ 4729f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 4730f9c1d6abSBarry Smith 4731f9c1d6abSBarry Smith Collective on TS and Vec 4732f9c1d6abSBarry Smith 4733f9c1d6abSBarry Smith Input Parameters: 4734f9c1d6abSBarry Smith + ts - the TS context 4735f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 4736f9c1d6abSBarry Smith 4737f9c1d6abSBarry Smith Output Parameters: 4738f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 4739f9c1d6abSBarry Smith 4740f9c1d6abSBarry Smith Level: developer 4741f9c1d6abSBarry Smith 4742f9c1d6abSBarry Smith .keywords: TS, compute 4743f9c1d6abSBarry Smith 4744f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 4745f9c1d6abSBarry Smith @*/ 4746f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 4747f9c1d6abSBarry Smith { 4748f9c1d6abSBarry Smith PetscErrorCode ierr; 4749f9c1d6abSBarry Smith 4750f9c1d6abSBarry Smith PetscFunctionBegin; 4751f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4752ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 4753f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 4754f9c1d6abSBarry Smith PetscFunctionReturn(0); 4755f9c1d6abSBarry Smith } 4756