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 40607a6623SBarry Smith if (!TSRegisterAllCalled) {ierr = TSRegisterAll();CHKERRQ(ierr);} 41a264d7a6SBarry Smith ierr = PetscOptionsFList("-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; 524b363babSBarry Smith PetscDrawAxis axis; 532d5ee99bSBarry Smith Vec initialsolution; 542d5ee99bSBarry Smith PetscBool showinitial; 552d5ee99bSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 562d5ee99bSBarry Smith PetscBool showtimestepandtime; 572d5ee99bSBarry Smith int color; 582d5ee99bSBarry Smith }; 592d5ee99bSBarry Smith 60bdad233fSMatthew Knepley #undef __FUNCT__ 61bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions" 62bdad233fSMatthew Knepley /*@ 63bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 64bdad233fSMatthew Knepley 65bdad233fSMatthew Knepley Collective on TS 66bdad233fSMatthew Knepley 67bdad233fSMatthew Knepley Input Parameter: 68bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 69bdad233fSMatthew Knepley 70bdad233fSMatthew Knepley Options Database Keys: 714d91e141SJed Brown + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP 72bdad233fSMatthew Knepley . -ts_max_steps maxsteps - maximum number of time-steps to take 733bca7d26SBarry Smith . -ts_final_time time - maximum time to compute to 74bdad233fSMatthew Knepley . -ts_dt dt - initial time step 75bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 76de06c3feSJed Brown . -ts_monitor_lg_timestep - Monitor timestep size graphically 77de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 78de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 79de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 80de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 81de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 82de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 832d5ee99bSBarry Smith . -ts_monitor_draw_solution_phase - Monitor solution graphically with phase diagram 84de06c3feSJed Brown . -ts_monitor_draw_error - Monitor error graphically 8591b97e58SBarry Smith . -ts_monitor_solution_binary <filename> - Save each solution to a binary file 8691b97e58SBarry Smith - -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 8791b97e58SBarry Smith 8891b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 89bdad233fSMatthew Knepley 90bdad233fSMatthew Knepley Level: beginner 91bdad233fSMatthew Knepley 92bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 93bdad233fSMatthew Knepley 94a313700dSBarry Smith .seealso: TSGetType() 95bdad233fSMatthew Knepley @*/ 967087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 97bdad233fSMatthew Knepley { 98ace3abfcSBarry Smith PetscBool opt,flg; 99dfbe8321SBarry Smith PetscErrorCode ierr; 100649052a6SBarry Smith PetscViewer monviewer; 101eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 102089b2837SJed Brown SNES snes; 1031c3436cfSJed Brown TSAdapt adapt; 10431748224SBarry Smith PetscReal time_step; 10549354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 106d1212d36SBarry Smith char dir[16]; 107bdad233fSMatthew Knepley 108bdad233fSMatthew Knepley PetscFunctionBegin; 1090700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1103194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 111a43b19c4SJed Brown /* Handle TS type options */ 112a43b19c4SJed Brown ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr); 113bdad233fSMatthew Knepley 114bdad233fSMatthew Knepley /* Handle generic TS options */ 1150298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1160298fd71SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 1170298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 11831748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 11931748224SBarry Smith if (flg) { 12031748224SBarry Smith ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 12131748224SBarry Smith } 12249354f04SShri 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); 12349354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 1240298fd71SBarry 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); 1250298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 1260298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 1270298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 1280298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 129bdad233fSMatthew Knepley 13056f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 13156f85f32SBarry Smith { 13256f85f32SBarry Smith PetscBool set; 13356f85f32SBarry Smith flg = PETSC_FALSE; 13456f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 13556f85f32SBarry Smith if (set) { 13656f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 13756f85f32SBarry Smith } 13856f85f32SBarry Smith } 13956f85f32SBarry Smith #endif 14056f85f32SBarry Smith 141bdad233fSMatthew Knepley /* Monitor options */ 142a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 143eabae89aSBarry Smith if (flg) { 144ce94432eSBarry Smith ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr); 145649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 146bdad233fSMatthew Knepley } 1475180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1485180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1495180491cSLisandro Dalcin 1504f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 151a7cc72afSBarry Smith if (opt) { 1520b039ecaSBarry Smith TSMonitorLGCtx ctx; 1533923b477SBarry Smith PetscInt howoften = 1; 154a80ad3e0SBarry Smith 1550298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 156ce94432eSBarry Smith ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 1574f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 158b3603a34SBarry Smith } 1594f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 160b3603a34SBarry Smith if (opt) { 1610b039ecaSBarry Smith TSMonitorLGCtx ctx; 1623923b477SBarry Smith PetscInt howoften = 1; 163b3603a34SBarry Smith 1640298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 16522d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1664f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 167bdad233fSMatthew Knepley } 1684f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 169ef20d060SBarry Smith if (opt) { 1700b039ecaSBarry Smith TSMonitorLGCtx ctx; 1713923b477SBarry Smith PetscInt howoften = 1; 172ef20d060SBarry Smith 1730298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 17422d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1754f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 176ef20d060SBarry Smith } 177201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 178201da799SBarry Smith if (opt) { 179201da799SBarry Smith TSMonitorLGCtx ctx; 180201da799SBarry Smith PetscInt howoften = 1; 181201da799SBarry Smith 1820298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 18322d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 184201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 185201da799SBarry Smith } 186201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 187201da799SBarry Smith if (opt) { 188201da799SBarry Smith TSMonitorLGCtx ctx; 189201da799SBarry Smith PetscInt howoften = 1; 190201da799SBarry Smith 1910298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 19222d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 193201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 194201da799SBarry Smith } 1958189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 1968189c53fSBarry Smith if (opt) { 1978189c53fSBarry Smith TSMonitorSPEigCtx ctx; 1988189c53fSBarry Smith PetscInt howoften = 1; 1998189c53fSBarry Smith 2000298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 20122d28d08SBarry Smith ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 2028189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 2038189c53fSBarry Smith } 204ef20d060SBarry Smith opt = PETSC_FALSE; 2050dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 206a7cc72afSBarry Smith if (opt) { 20783a4ac43SBarry Smith TSMonitorDrawCtx ctx; 20883a4ac43SBarry Smith PetscInt howoften = 1; 209a80ad3e0SBarry Smith 2100298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 211ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 21283a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 213bdad233fSMatthew Knepley } 214fb1732b5SBarry Smith opt = PETSC_FALSE; 2152d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 2162d5ee99bSBarry Smith if (opt) { 2172d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2182d5ee99bSBarry Smith PetscReal bounds[4]; 2192d5ee99bSBarry Smith PetscInt n = 4; 2202d5ee99bSBarry Smith PetscDraw draw; 2212d5ee99bSBarry Smith 2222d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 2232d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2242d5ee99bSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr); 2252d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 2262d5ee99bSBarry Smith ierr = PetscDrawClear(draw);CHKERRQ(ierr); 2274b363babSBarry Smith ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr); 2284b363babSBarry Smith ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 229f54adfc0SBarry Smith ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 2304b363babSBarry Smith ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr); 23107995780SPeter Brune /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */ 2322d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2332d5ee99bSBarry Smith } 2342d5ee99bSBarry Smith opt = PETSC_FALSE; 2350dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 2363a471f94SBarry Smith if (opt) { 23783a4ac43SBarry Smith TSMonitorDrawCtx ctx; 23883a4ac43SBarry Smith PetscInt howoften = 1; 2393a471f94SBarry Smith 2400298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 241ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 24283a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2433a471f94SBarry Smith } 2443a471f94SBarry Smith opt = PETSC_FALSE; 24591b97e58SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 246fb1732b5SBarry Smith if (flg) { 247fb1732b5SBarry Smith PetscViewer ctx; 248fb1732b5SBarry Smith if (monfilename[0]) { 249ce94432eSBarry Smith ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 250c2fbc07fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 251fb1732b5SBarry Smith } else { 252ce94432eSBarry Smith ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts)); 2530298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr); 254fb1732b5SBarry Smith } 255fb1732b5SBarry Smith } 256ed81e22dSJed Brown opt = PETSC_FALSE; 25791b97e58SBarry 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); 258ed81e22dSJed Brown if (flg) { 259ed81e22dSJed Brown const char *ptr,*ptr2; 260ed81e22dSJed Brown char *filetemplate; 261ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 262ed81e22dSJed Brown /* Do some cursory validation of the input. */ 263ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 264ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 265ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 266ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 267ce94432eSBarry 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"); 268ed81e22dSJed Brown if (ptr2) break; 269ed81e22dSJed Brown } 270ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 271ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 272ed81e22dSJed Brown } 273bdad233fSMatthew Knepley 274d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 275d1212d36SBarry Smith if (flg) { 276d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 277d1212d36SBarry Smith int ray = 0; 278d1212d36SBarry Smith DMDADirection ddir; 279d1212d36SBarry Smith DM da; 280d1212d36SBarry Smith PetscMPIInt rank; 281d1212d36SBarry Smith 282ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 283d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 284d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 285ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 286d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 287d1212d36SBarry Smith 288d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 289b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 290d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 291d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 292ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 293d1212d36SBarry Smith if (!rank) { 294d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 295d1212d36SBarry Smith } 29651b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 297d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 298d1212d36SBarry Smith } 29951b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 30051b4a12fSMatthew G. Knepley if (flg) { 30151b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 30251b4a12fSMatthew G. Knepley int ray = 0; 30351b4a12fSMatthew G. Knepley DMDADirection ddir; 30451b4a12fSMatthew G. Knepley DM da; 30551b4a12fSMatthew G. Knepley PetscInt howoften = 1; 30651b4a12fSMatthew G. Knepley 30751b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 30851b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 30951b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 31051b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 31151b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 31251b4a12fSMatthew G. Knepley 31351b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 314b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 31551b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 31651b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 31751b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 31851b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 31951b4a12fSMatthew G. Knepley } 320d1212d36SBarry Smith 3217781c08eSBarry Smith /* 3227781c08eSBarry Smith This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui 3237781c08eSBarry Smith will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin() 3247781c08eSBarry Smith */ 325552698daSJed Brown ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr); 3261c3436cfSJed Brown ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr); 3271c3436cfSJed Brown 328d52bd9f3SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 329d52bd9f3SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 330d52bd9f3SBarry Smith 331bdad233fSMatthew Knepley /* Handle specific TS options */ 332abc0a331SBarry Smith if (ts->ops->setfromoptions) { 333bdad233fSMatthew Knepley ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr); 334bdad233fSMatthew Knepley } 3355d973c19SBarry Smith 3365d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 3375d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 338bdad233fSMatthew Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 339bdad233fSMatthew Knepley PetscFunctionReturn(0); 340bdad233fSMatthew Knepley } 341bdad233fSMatthew Knepley 342bdad233fSMatthew Knepley #undef __FUNCT__ 343cdcf91faSSean Farley #undef __FUNCT__ 3444a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian" 345a7a1495cSBarry Smith /*@ 3468c385f81SBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 347a7a1495cSBarry Smith set with TSSetRHSJacobian(). 348a7a1495cSBarry Smith 349a7a1495cSBarry Smith Collective on TS and Vec 350a7a1495cSBarry Smith 351a7a1495cSBarry Smith Input Parameters: 352316643e7SJed Brown + ts - the TS context 353a7a1495cSBarry Smith . t - current timestep 3540910c330SBarry Smith - U - input vector 355a7a1495cSBarry Smith 356a7a1495cSBarry Smith Output Parameters: 357a7a1495cSBarry Smith + A - Jacobian matrix 358a7a1495cSBarry Smith . B - optional preconditioning matrix 359a7a1495cSBarry Smith - flag - flag indicating matrix structure 360a7a1495cSBarry Smith 361a7a1495cSBarry Smith Notes: 362a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 363a7a1495cSBarry Smith is used internally within the nonlinear solvers. 364a7a1495cSBarry Smith 36594b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 366a7a1495cSBarry Smith flag parameter. 367a7a1495cSBarry Smith 368a7a1495cSBarry Smith Level: developer 369a7a1495cSBarry Smith 370a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 371a7a1495cSBarry Smith 37294b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 373a7a1495cSBarry Smith @*/ 374*94ab13aaSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B,MatStructure *flg) 375a7a1495cSBarry Smith { 376dfbe8321SBarry Smith PetscErrorCode ierr; 377270bf2e7SJed Brown PetscObjectState Ustate; 37824989b8cSPeter Brune DM dm; 379942e3340SBarry Smith DMTS tsdm; 38024989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 38124989b8cSPeter Brune void *ctx; 38224989b8cSPeter Brune TSIJacobian ijacobianfunc; 383a7a1495cSBarry Smith 384a7a1495cSBarry Smith PetscFunctionBegin; 3850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3860910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3870910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 38824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 389942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 39024989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 3910298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 39259e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 3930910c330SBarry Smith if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) { 3940e4ef248SJed Brown *flg = ts->rhsjacobian.mstructure; 3950e4ef248SJed Brown PetscFunctionReturn(0); 396f8ede8e7SJed Brown } 397d90be118SSean Farley 398ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 399d90be118SSean Farley 400e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 401*94ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 402*94ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 403*94ab13aaSBarry Smith if (A != B) { 404*94ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 405*94ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 406e1244c69SJed Brown } 407e1244c69SJed Brown ts->rhsjacobian.shift = 0; 408e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 409e1244c69SJed Brown } 410e1244c69SJed Brown 41124989b8cSPeter Brune if (rhsjacobianfunc) { 412*94ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 413a7a1495cSBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 414a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 4150910c330SBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr); 416a7a1495cSBarry Smith PetscStackPop; 417*94ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 418a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 419*94ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,4); 420*94ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,5); 421ef66eb69SBarry Smith } else { 422*94ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 423*94ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 424214bc6a2SJed Brown *flg = SAME_NONZERO_PATTERN; 425ef66eb69SBarry Smith } 4260e4ef248SJed Brown ts->rhsjacobian.time = t; 4270910c330SBarry Smith ts->rhsjacobian.X = U; 42859e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 4290e4ef248SJed Brown ts->rhsjacobian.mstructure = *flg; 430a7a1495cSBarry Smith PetscFunctionReturn(0); 431a7a1495cSBarry Smith } 432a7a1495cSBarry Smith 4334a2ae208SSatish Balay #undef __FUNCT__ 4344a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 435316643e7SJed Brown /*@ 436d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 437d763cef2SBarry Smith 438316643e7SJed Brown Collective on TS and Vec 439316643e7SJed Brown 440316643e7SJed Brown Input Parameters: 441316643e7SJed Brown + ts - the TS context 442316643e7SJed Brown . t - current time 4430910c330SBarry Smith - U - state vector 444316643e7SJed Brown 445316643e7SJed Brown Output Parameter: 446316643e7SJed Brown . y - right hand side 447316643e7SJed Brown 448316643e7SJed Brown Note: 449316643e7SJed Brown Most users should not need to explicitly call this routine, as it 450316643e7SJed Brown is used internally within the nonlinear solvers. 451316643e7SJed Brown 452316643e7SJed Brown Level: developer 453316643e7SJed Brown 454316643e7SJed Brown .keywords: TS, compute 455316643e7SJed Brown 456316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 457316643e7SJed Brown @*/ 4580910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 459d763cef2SBarry Smith { 460dfbe8321SBarry Smith PetscErrorCode ierr; 46124989b8cSPeter Brune TSRHSFunction rhsfunction; 46224989b8cSPeter Brune TSIFunction ifunction; 46324989b8cSPeter Brune void *ctx; 46424989b8cSPeter Brune DM dm; 46524989b8cSPeter Brune 466d763cef2SBarry Smith PetscFunctionBegin; 4670700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4680910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 4690700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 47024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 47124989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 4720298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 473d763cef2SBarry Smith 474ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 475d763cef2SBarry Smith 4760910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 47724989b8cSPeter Brune if (rhsfunction) { 478d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 4790910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 480d763cef2SBarry Smith PetscStackPop; 481214bc6a2SJed Brown } else { 482214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 483b2cd27e8SJed Brown } 48444a41b28SSean Farley 4850910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 486d763cef2SBarry Smith PetscFunctionReturn(0); 487d763cef2SBarry Smith } 488d763cef2SBarry Smith 4894a2ae208SSatish Balay #undef __FUNCT__ 490ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 491ef20d060SBarry Smith /*@ 492ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 493ef20d060SBarry Smith 494ef20d060SBarry Smith Collective on TS and Vec 495ef20d060SBarry Smith 496ef20d060SBarry Smith Input Parameters: 497ef20d060SBarry Smith + ts - the TS context 498ef20d060SBarry Smith - t - current time 499ef20d060SBarry Smith 500ef20d060SBarry Smith Output Parameter: 5010910c330SBarry Smith . U - the solution 502ef20d060SBarry Smith 503ef20d060SBarry Smith Note: 504ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 505ef20d060SBarry Smith is used internally within the nonlinear solvers. 506ef20d060SBarry Smith 507ef20d060SBarry Smith Level: developer 508ef20d060SBarry Smith 509ef20d060SBarry Smith .keywords: TS, compute 510ef20d060SBarry Smith 511abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 512ef20d060SBarry Smith @*/ 5130910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 514ef20d060SBarry Smith { 515ef20d060SBarry Smith PetscErrorCode ierr; 516ef20d060SBarry Smith TSSolutionFunction solutionfunction; 517ef20d060SBarry Smith void *ctx; 518ef20d060SBarry Smith DM dm; 519ef20d060SBarry Smith 520ef20d060SBarry Smith PetscFunctionBegin; 521ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5220910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 523ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 524ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 525ef20d060SBarry Smith 526ef20d060SBarry Smith if (solutionfunction) { 5279b7cd975SBarry Smith PetscStackPush("TS user solution function"); 5280910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 529ef20d060SBarry Smith PetscStackPop; 530ef20d060SBarry Smith } 531ef20d060SBarry Smith PetscFunctionReturn(0); 532ef20d060SBarry Smith } 5339b7cd975SBarry Smith #undef __FUNCT__ 5349b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction" 5359b7cd975SBarry Smith /*@ 5369b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 5379b7cd975SBarry Smith 5389b7cd975SBarry Smith Collective on TS and Vec 5399b7cd975SBarry Smith 5409b7cd975SBarry Smith Input Parameters: 5419b7cd975SBarry Smith + ts - the TS context 5429b7cd975SBarry Smith - t - current time 5439b7cd975SBarry Smith 5449b7cd975SBarry Smith Output Parameter: 5459b7cd975SBarry Smith . U - the function value 5469b7cd975SBarry Smith 5479b7cd975SBarry Smith Note: 5489b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 5499b7cd975SBarry Smith is used internally within the nonlinear solvers. 5509b7cd975SBarry Smith 5519b7cd975SBarry Smith Level: developer 5529b7cd975SBarry Smith 5539b7cd975SBarry Smith .keywords: TS, compute 5549b7cd975SBarry Smith 5559b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 5569b7cd975SBarry Smith @*/ 5579b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 5589b7cd975SBarry Smith { 5599b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 5609b7cd975SBarry Smith void *ctx; 5619b7cd975SBarry Smith DM dm; 5629b7cd975SBarry Smith 5639b7cd975SBarry Smith PetscFunctionBegin; 5649b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5659b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5669b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 5679b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 5689b7cd975SBarry Smith 5699b7cd975SBarry Smith if (forcing) { 5709b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 5719b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 5729b7cd975SBarry Smith PetscStackPop; 5739b7cd975SBarry Smith } 5749b7cd975SBarry Smith PetscFunctionReturn(0); 5759b7cd975SBarry Smith } 576ef20d060SBarry Smith 577ef20d060SBarry Smith #undef __FUNCT__ 578214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 579214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 580214bc6a2SJed Brown { 5812dd45cf8SJed Brown Vec F; 582214bc6a2SJed Brown PetscErrorCode ierr; 583214bc6a2SJed Brown 584214bc6a2SJed Brown PetscFunctionBegin; 5850298fd71SBarry Smith *Frhs = NULL; 5860298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 587214bc6a2SJed Brown if (!ts->Frhs) { 5882dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 589214bc6a2SJed Brown } 590214bc6a2SJed Brown *Frhs = ts->Frhs; 591214bc6a2SJed Brown PetscFunctionReturn(0); 592214bc6a2SJed Brown } 593214bc6a2SJed Brown 594214bc6a2SJed Brown #undef __FUNCT__ 595214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 596214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 597214bc6a2SJed Brown { 598214bc6a2SJed Brown Mat A,B; 5992dd45cf8SJed Brown PetscErrorCode ierr; 600214bc6a2SJed Brown 601214bc6a2SJed Brown PetscFunctionBegin; 6020298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 603214bc6a2SJed Brown if (Arhs) { 604214bc6a2SJed Brown if (!ts->Arhs) { 605214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 606214bc6a2SJed Brown } 607214bc6a2SJed Brown *Arhs = ts->Arhs; 608214bc6a2SJed Brown } 609214bc6a2SJed Brown if (Brhs) { 610214bc6a2SJed Brown if (!ts->Brhs) { 611bdb70873SJed Brown if (A != B) { 612214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 613bdb70873SJed Brown } else { 614bdb70873SJed Brown ts->Brhs = ts->Arhs; 615bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 616bdb70873SJed Brown } 617214bc6a2SJed Brown } 618214bc6a2SJed Brown *Brhs = ts->Brhs; 619214bc6a2SJed Brown } 620214bc6a2SJed Brown PetscFunctionReturn(0); 621214bc6a2SJed Brown } 622214bc6a2SJed Brown 623214bc6a2SJed Brown #undef __FUNCT__ 624316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 625316643e7SJed Brown /*@ 6260910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 627316643e7SJed Brown 628316643e7SJed Brown Collective on TS and Vec 629316643e7SJed Brown 630316643e7SJed Brown Input Parameters: 631316643e7SJed Brown + ts - the TS context 632316643e7SJed Brown . t - current time 6330910c330SBarry Smith . U - state vector 6340910c330SBarry Smith . Udot - time derivative of state vector 635214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 636316643e7SJed Brown 637316643e7SJed Brown Output Parameter: 638316643e7SJed Brown . Y - right hand side 639316643e7SJed Brown 640316643e7SJed Brown Note: 641316643e7SJed Brown Most users should not need to explicitly call this routine, as it 642316643e7SJed Brown is used internally within the nonlinear solvers. 643316643e7SJed Brown 644316643e7SJed Brown If the user did did not write their equations in implicit form, this 645316643e7SJed Brown function recasts them in implicit form. 646316643e7SJed Brown 647316643e7SJed Brown Level: developer 648316643e7SJed Brown 649316643e7SJed Brown .keywords: TS, compute 650316643e7SJed Brown 651316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 652316643e7SJed Brown @*/ 6530910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 654316643e7SJed Brown { 655316643e7SJed Brown PetscErrorCode ierr; 65624989b8cSPeter Brune TSIFunction ifunction; 65724989b8cSPeter Brune TSRHSFunction rhsfunction; 65824989b8cSPeter Brune void *ctx; 65924989b8cSPeter Brune DM dm; 660316643e7SJed Brown 661316643e7SJed Brown PetscFunctionBegin; 6620700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6630910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6640910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 6650700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 666316643e7SJed Brown 66724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 66824989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 6690298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 67024989b8cSPeter Brune 671ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 672d90be118SSean Farley 6730910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 67424989b8cSPeter Brune if (ifunction) { 675316643e7SJed Brown PetscStackPush("TS user implicit function"); 6760910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 677316643e7SJed Brown PetscStackPop; 678214bc6a2SJed Brown } 679214bc6a2SJed Brown if (imex) { 68024989b8cSPeter Brune if (!ifunction) { 6810910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 6822dd45cf8SJed Brown } 68324989b8cSPeter Brune } else if (rhsfunction) { 68424989b8cSPeter Brune if (ifunction) { 685214bc6a2SJed Brown Vec Frhs; 686214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 6870910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 688214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 6892dd45cf8SJed Brown } else { 6900910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 6910910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 692316643e7SJed Brown } 6934a6899ffSJed Brown } 6940910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 695316643e7SJed Brown PetscFunctionReturn(0); 696316643e7SJed Brown } 697316643e7SJed Brown 698316643e7SJed Brown #undef __FUNCT__ 699316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 700316643e7SJed Brown /*@ 701316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 702316643e7SJed Brown 703316643e7SJed Brown Collective on TS and Vec 704316643e7SJed Brown 705316643e7SJed Brown Input 706316643e7SJed Brown Input Parameters: 707316643e7SJed Brown + ts - the TS context 708316643e7SJed Brown . t - current timestep 7090910c330SBarry Smith . U - state vector 7100910c330SBarry Smith . Udot - time derivative of state vector 711214bc6a2SJed Brown . shift - shift to apply, see note below 712214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 713316643e7SJed Brown 714316643e7SJed Brown Output Parameters: 715316643e7SJed Brown + A - Jacobian matrix 716316643e7SJed Brown . B - optional preconditioning matrix 717316643e7SJed Brown - flag - flag indicating matrix structure 718316643e7SJed Brown 719316643e7SJed Brown Notes: 7200910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 721316643e7SJed Brown 7220910c330SBarry Smith dF/dU + shift*dF/dUdot 723316643e7SJed Brown 724316643e7SJed Brown Most users should not need to explicitly call this routine, as it 725316643e7SJed Brown is used internally within the nonlinear solvers. 726316643e7SJed Brown 727316643e7SJed Brown Level: developer 728316643e7SJed Brown 729316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 730316643e7SJed Brown 731316643e7SJed Brown .seealso: TSSetIJacobian() 73263495f91SJed Brown @*/ 733*94ab13aaSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,MatStructure *flg,PetscBool imex) 734316643e7SJed Brown { 735316643e7SJed Brown PetscErrorCode ierr; 73624989b8cSPeter Brune TSIJacobian ijacobian; 73724989b8cSPeter Brune TSRHSJacobian rhsjacobian; 73824989b8cSPeter Brune DM dm; 73924989b8cSPeter Brune void *ctx; 740316643e7SJed Brown 741316643e7SJed Brown PetscFunctionBegin; 7420700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7430910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7440910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 745316643e7SJed Brown PetscValidPointer(A,6); 746*94ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 747316643e7SJed Brown PetscValidPointer(B,7); 748*94ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 749316643e7SJed Brown PetscValidPointer(flg,8); 75024989b8cSPeter Brune 75124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 75224989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 7530298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 75424989b8cSPeter Brune 755ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 756316643e7SJed Brown 7574e684422SJed Brown *flg = SAME_NONZERO_PATTERN; /* In case we're solving a linear problem in which case it wouldn't get initialized below. */ 758*94ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 75924989b8cSPeter Brune if (ijacobian) { 7602dd45cf8SJed Brown *flg = DIFFERENT_NONZERO_PATTERN; 761316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 7620910c330SBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr); 763316643e7SJed Brown PetscStackPop; 764214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 765*94ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,4); 766*94ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,5); 7674a6899ffSJed Brown } 768214bc6a2SJed Brown if (imex) { 769b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 770*94ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 771*94ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 772*94ab13aaSBarry Smith if (A != B) { 773*94ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 774*94ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 775214bc6a2SJed Brown } 776214bc6a2SJed Brown *flg = SAME_PRECONDITIONER; 777214bc6a2SJed Brown } 778214bc6a2SJed Brown } else { 779e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 780e1244c69SJed Brown MatStructure flg2; 781e1244c69SJed Brown if (rhsjacobian) { 782bd373395SBarry Smith if (ijacobian) { 783e1244c69SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 784bd373395SBarry Smith } else { 785bd373395SBarry Smith ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr); 786bd373395SBarry Smith } 787*94ab13aaSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs,&flg2);CHKERRQ(ierr); 788e1244c69SJed Brown } 789*94ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 790e1244c69SJed Brown ts->rhsjacobian.scale = -1; 791e1244c69SJed Brown ts->rhsjacobian.shift = shift; 792*94ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 793*94ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 794*94ab13aaSBarry Smith if (A != B) { 795*94ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 796*94ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 797316643e7SJed Brown } 798e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 799e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 800e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 801*94ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 802*94ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 803*94ab13aaSBarry Smith if (A != B) { 804*94ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 805*94ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 806e1244c69SJed Brown } 807e1244c69SJed Brown } 808*94ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 809*94ab13aaSBarry Smith if (A != B) { 810*94ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 811214bc6a2SJed Brown } 812214bc6a2SJed Brown *flg = PetscMin(*flg,flg2); 813214bc6a2SJed Brown } 814316643e7SJed Brown } 815*94ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 816316643e7SJed Brown PetscFunctionReturn(0); 817316643e7SJed Brown } 818316643e7SJed Brown 819316643e7SJed Brown #undef __FUNCT__ 8204a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 821d763cef2SBarry Smith /*@C 822d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 823b5abc632SBarry Smith where U_t = G(t,u). 824d763cef2SBarry Smith 8253f9fe445SBarry Smith Logically Collective on TS 826d763cef2SBarry Smith 827d763cef2SBarry Smith Input Parameters: 828d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 8290298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 830d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 831d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 8320298fd71SBarry Smith function evaluation routine (may be NULL) 833d763cef2SBarry Smith 834d763cef2SBarry Smith Calling sequence of func: 83587828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 836d763cef2SBarry Smith 837d763cef2SBarry Smith + t - current timestep 838d763cef2SBarry Smith . u - input vector 839d763cef2SBarry Smith . F - function vector 840d763cef2SBarry Smith - ctx - [optional] user-defined function context 841d763cef2SBarry Smith 842d763cef2SBarry Smith Level: beginner 843d763cef2SBarry Smith 844d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 845d763cef2SBarry Smith 846d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian() 847d763cef2SBarry Smith @*/ 848089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 849d763cef2SBarry Smith { 850089b2837SJed Brown PetscErrorCode ierr; 851089b2837SJed Brown SNES snes; 8520298fd71SBarry Smith Vec ralloc = NULL; 85324989b8cSPeter Brune DM dm; 854d763cef2SBarry Smith 855089b2837SJed Brown PetscFunctionBegin; 8560700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 857ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 85824989b8cSPeter Brune 85924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 86024989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 861089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 862e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 863e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 864e856ceecSJed Brown r = ralloc; 865e856ceecSJed Brown } 866089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 867e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 868d763cef2SBarry Smith PetscFunctionReturn(0); 869d763cef2SBarry Smith } 870d763cef2SBarry Smith 8714a2ae208SSatish Balay #undef __FUNCT__ 872ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 873ef20d060SBarry Smith /*@C 874abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 875ef20d060SBarry Smith 876ef20d060SBarry Smith Logically Collective on TS 877ef20d060SBarry Smith 878ef20d060SBarry Smith Input Parameters: 879ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 880ef20d060SBarry Smith . f - routine for evaluating the solution 881ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 8820298fd71SBarry Smith function evaluation routine (may be NULL) 883ef20d060SBarry Smith 884ef20d060SBarry Smith Calling sequence of func: 885ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 886ef20d060SBarry Smith 887ef20d060SBarry Smith + t - current timestep 888ef20d060SBarry Smith . u - output vector 889ef20d060SBarry Smith - ctx - [optional] user-defined function context 890ef20d060SBarry Smith 891abd5a294SJed Brown Notes: 892abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 893abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 894abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 895abd5a294SJed Brown 8964f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 897abd5a294SJed Brown 898ef20d060SBarry Smith Level: beginner 899ef20d060SBarry Smith 900ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 901ef20d060SBarry Smith 9029b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 903ef20d060SBarry Smith @*/ 904ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 905ef20d060SBarry Smith { 906ef20d060SBarry Smith PetscErrorCode ierr; 907ef20d060SBarry Smith DM dm; 908ef20d060SBarry Smith 909ef20d060SBarry Smith PetscFunctionBegin; 910ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 911ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 912ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 913ef20d060SBarry Smith PetscFunctionReturn(0); 914ef20d060SBarry Smith } 915ef20d060SBarry Smith 916ef20d060SBarry Smith #undef __FUNCT__ 9179b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction" 9189b7cd975SBarry Smith /*@C 9199b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 9209b7cd975SBarry Smith 9219b7cd975SBarry Smith Logically Collective on TS 9229b7cd975SBarry Smith 9239b7cd975SBarry Smith Input Parameters: 9249b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 9259b7cd975SBarry Smith . f - routine for evaluating the forcing function 9269b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 9270298fd71SBarry Smith function evaluation routine (may be NULL) 9289b7cd975SBarry Smith 9299b7cd975SBarry Smith Calling sequence of func: 9309b7cd975SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 9319b7cd975SBarry Smith 9329b7cd975SBarry Smith + t - current timestep 9339b7cd975SBarry Smith . u - output vector 9349b7cd975SBarry Smith - ctx - [optional] user-defined function context 9359b7cd975SBarry Smith 9369b7cd975SBarry Smith Notes: 9379b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 9389b7cd975SBarry Smith create closed-form solutions with a non-physical forcing term. 9399b7cd975SBarry Smith 9409b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 9419b7cd975SBarry Smith 9429b7cd975SBarry Smith Level: beginner 9439b7cd975SBarry Smith 9449b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 9459b7cd975SBarry Smith 9469b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 9479b7cd975SBarry Smith @*/ 9489b7cd975SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 9499b7cd975SBarry Smith { 9509b7cd975SBarry Smith PetscErrorCode ierr; 9519b7cd975SBarry Smith DM dm; 9529b7cd975SBarry Smith 9539b7cd975SBarry Smith PetscFunctionBegin; 9549b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9559b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 9569b7cd975SBarry Smith ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr); 9579b7cd975SBarry Smith PetscFunctionReturn(0); 9589b7cd975SBarry Smith } 9599b7cd975SBarry Smith 9609b7cd975SBarry Smith #undef __FUNCT__ 9614a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 962d763cef2SBarry Smith /*@C 963d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 964b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 965d763cef2SBarry Smith 9663f9fe445SBarry Smith Logically Collective on TS 967d763cef2SBarry Smith 968d763cef2SBarry Smith Input Parameters: 969d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 970e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 971e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 972d763cef2SBarry Smith . f - the Jacobian evaluation routine 973d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 9740298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 975d763cef2SBarry Smith 976d763cef2SBarry Smith Calling sequence of func: 97787828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 978d763cef2SBarry Smith 979d763cef2SBarry Smith + t - current timestep 980d763cef2SBarry Smith . u - input vector 981e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 982e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 983d763cef2SBarry Smith . flag - flag indicating information about the preconditioner matrix 98494b7f48cSBarry Smith structure (same as flag in KSPSetOperators()) 985d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 986d763cef2SBarry Smith 987d763cef2SBarry Smith Notes: 98894b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 989d763cef2SBarry Smith output parameter in the routine func(). Be sure to read this information! 990d763cef2SBarry Smith 991d763cef2SBarry Smith The routine func() takes Mat * as the matrix arguments rather than Mat. 992d763cef2SBarry Smith This allows the matrix evaluation routine to replace A and/or B with a 99356335db2SHong Zhang completely new matrix structure (not just different matrix elements) 994d763cef2SBarry Smith when appropriate, for instance, if the nonzero structure is changing 995d763cef2SBarry Smith throughout the global iterations. 996d763cef2SBarry Smith 997d763cef2SBarry Smith Level: beginner 998d763cef2SBarry Smith 999d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1000d763cef2SBarry Smith 1001e1244c69SJed Brown .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse() 1002d763cef2SBarry Smith 1003d763cef2SBarry Smith @*/ 1004e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1005d763cef2SBarry Smith { 1006277b19d0SLisandro Dalcin PetscErrorCode ierr; 1007089b2837SJed Brown SNES snes; 100824989b8cSPeter Brune DM dm; 100924989b8cSPeter Brune TSIJacobian ijacobian; 1010277b19d0SLisandro Dalcin 1011d763cef2SBarry Smith PetscFunctionBegin; 10120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1013e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1014e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1015e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1016e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1017d763cef2SBarry Smith 101824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 101924989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1020e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1021e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1022e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1023e1244c69SJed Brown } 10240298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1025089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 10265f659677SPeter Brune if (!ijacobian) { 1027e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 10280e4ef248SJed Brown } 1029e5d3d808SBarry Smith if (Amat) { 1030e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 10310e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1032bbd56ea5SKarl Rupp 1033e5d3d808SBarry Smith ts->Arhs = Amat; 10340e4ef248SJed Brown } 1035e5d3d808SBarry Smith if (Pmat) { 1036e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 10370e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1038bbd56ea5SKarl Rupp 1039e5d3d808SBarry Smith ts->Brhs = Pmat; 10400e4ef248SJed Brown } 1041d763cef2SBarry Smith PetscFunctionReturn(0); 1042d763cef2SBarry Smith } 1043d763cef2SBarry Smith 1044316643e7SJed Brown 1045316643e7SJed Brown #undef __FUNCT__ 1046316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 1047316643e7SJed Brown /*@C 1048b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1049316643e7SJed Brown 10503f9fe445SBarry Smith Logically Collective on TS 1051316643e7SJed Brown 1052316643e7SJed Brown Input Parameters: 1053316643e7SJed Brown + ts - the TS context obtained from TSCreate() 10540298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1055316643e7SJed Brown . f - the function evaluation routine 10560298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1057316643e7SJed Brown 1058316643e7SJed Brown Calling sequence of f: 1059316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1060316643e7SJed Brown 1061316643e7SJed Brown + t - time at step/stage being solved 1062316643e7SJed Brown . u - state vector 1063316643e7SJed Brown . u_t - time derivative of state vector 1064316643e7SJed Brown . F - function vector 1065316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1066316643e7SJed Brown 1067316643e7SJed Brown Important: 1068d6cbdb99SBarry Smith The user MUST call either this routine, TSSetRHSFunction(). This routine must be used when not solving an ODE, for example a DAE. 1069316643e7SJed Brown 1070316643e7SJed Brown Level: beginner 1071316643e7SJed Brown 1072316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1073316643e7SJed Brown 1074d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1075316643e7SJed Brown @*/ 1076089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 1077316643e7SJed Brown { 1078089b2837SJed Brown PetscErrorCode ierr; 1079089b2837SJed Brown SNES snes; 10800298fd71SBarry Smith Vec resalloc = NULL; 108124989b8cSPeter Brune DM dm; 1082316643e7SJed Brown 1083316643e7SJed Brown PetscFunctionBegin; 10840700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1085ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 108624989b8cSPeter Brune 108724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 108824989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 108924989b8cSPeter Brune 1090089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1091e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 1092e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 1093e856ceecSJed Brown res = resalloc; 1094e856ceecSJed Brown } 1095089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 1096e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 1097089b2837SJed Brown PetscFunctionReturn(0); 1098089b2837SJed Brown } 1099089b2837SJed Brown 1100089b2837SJed Brown #undef __FUNCT__ 1101089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 1102089b2837SJed Brown /*@C 1103089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1104089b2837SJed Brown 1105089b2837SJed Brown Not Collective 1106089b2837SJed Brown 1107089b2837SJed Brown Input Parameter: 1108089b2837SJed Brown . ts - the TS context 1109089b2837SJed Brown 1110089b2837SJed Brown Output Parameter: 11110298fd71SBarry Smith + r - vector to hold residual (or NULL) 11120298fd71SBarry Smith . func - the function to compute residual (or NULL) 11130298fd71SBarry Smith - ctx - the function context (or NULL) 1114089b2837SJed Brown 1115089b2837SJed Brown Level: advanced 1116089b2837SJed Brown 1117089b2837SJed Brown .keywords: TS, nonlinear, get, function 1118089b2837SJed Brown 1119089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1120089b2837SJed Brown @*/ 1121089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1122089b2837SJed Brown { 1123089b2837SJed Brown PetscErrorCode ierr; 1124089b2837SJed Brown SNES snes; 112524989b8cSPeter Brune DM dm; 1126089b2837SJed Brown 1127089b2837SJed Brown PetscFunctionBegin; 1128089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1129089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11300298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 113124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 113224989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1133089b2837SJed Brown PetscFunctionReturn(0); 1134089b2837SJed Brown } 1135089b2837SJed Brown 1136089b2837SJed Brown #undef __FUNCT__ 1137089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 1138089b2837SJed Brown /*@C 1139089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1140089b2837SJed Brown 1141089b2837SJed Brown Not Collective 1142089b2837SJed Brown 1143089b2837SJed Brown Input Parameter: 1144089b2837SJed Brown . ts - the TS context 1145089b2837SJed Brown 1146089b2837SJed Brown Output Parameter: 11470298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 11480298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 11490298fd71SBarry Smith - ctx - the function context (or NULL) 1150089b2837SJed Brown 1151089b2837SJed Brown Level: advanced 1152089b2837SJed Brown 1153089b2837SJed Brown .keywords: TS, nonlinear, get, function 1154089b2837SJed Brown 1155089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 1156089b2837SJed Brown @*/ 1157089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1158089b2837SJed Brown { 1159089b2837SJed Brown PetscErrorCode ierr; 1160089b2837SJed Brown SNES snes; 116124989b8cSPeter Brune DM dm; 1162089b2837SJed Brown 1163089b2837SJed Brown PetscFunctionBegin; 1164089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1165089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11660298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 116724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 116824989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1169316643e7SJed Brown PetscFunctionReturn(0); 1170316643e7SJed Brown } 1171316643e7SJed Brown 1172316643e7SJed Brown #undef __FUNCT__ 1173316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 1174316643e7SJed Brown /*@C 1175a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1176a4f0a591SBarry Smith you provided with TSSetIFunction(). 1177316643e7SJed Brown 11783f9fe445SBarry Smith Logically Collective on TS 1179316643e7SJed Brown 1180316643e7SJed Brown Input Parameters: 1181316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1182e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1183e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1184316643e7SJed Brown . f - the Jacobian evaluation routine 11850298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1186316643e7SJed Brown 1187316643e7SJed Brown Calling sequence of f: 1188e5d3d808SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *Amat,Mat *Pmat,MatStructure *flag,void *ctx); 1189316643e7SJed Brown 1190316643e7SJed Brown + t - time at step/stage being solved 11911b4a444bSJed Brown . U - state vector 11921b4a444bSJed Brown . U_t - time derivative of state vector 1193316643e7SJed Brown . a - shift 1194e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1195e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1196316643e7SJed Brown . flag - flag indicating information about the preconditioner matrix 1197316643e7SJed Brown structure (same as flag in KSPSetOperators()) 1198316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1199316643e7SJed Brown 1200316643e7SJed Brown Notes: 1201e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1202316643e7SJed Brown 1203a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1204b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1205a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1206a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1207a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1208a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1209a4f0a591SBarry Smith 1210316643e7SJed Brown Level: beginner 1211316643e7SJed Brown 1212316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1213316643e7SJed Brown 12148d359177SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault() 1215316643e7SJed Brown 1216316643e7SJed Brown @*/ 1217e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1218316643e7SJed Brown { 1219316643e7SJed Brown PetscErrorCode ierr; 1220089b2837SJed Brown SNES snes; 122124989b8cSPeter Brune DM dm; 1222316643e7SJed Brown 1223316643e7SJed Brown PetscFunctionBegin; 12240700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1225e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1226e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1227e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1228e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 122924989b8cSPeter Brune 123024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 123124989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 123224989b8cSPeter Brune 1233089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1234e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1235316643e7SJed Brown PetscFunctionReturn(0); 1236316643e7SJed Brown } 1237316643e7SJed Brown 12384a2ae208SSatish Balay #undef __FUNCT__ 1239e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse" 1240e1244c69SJed Brown /*@ 1241e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1242e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1243e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1244e1244c69SJed Brown not been changed by the TS. 1245e1244c69SJed Brown 1246e1244c69SJed Brown Logically Collective 1247e1244c69SJed Brown 1248e1244c69SJed Brown Input Arguments: 1249e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1250e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1251e1244c69SJed Brown 1252e1244c69SJed Brown Level: intermediate 1253e1244c69SJed Brown 1254e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1255e1244c69SJed Brown @*/ 1256e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1257e1244c69SJed Brown { 1258e1244c69SJed Brown PetscFunctionBegin; 1259e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1260e1244c69SJed Brown PetscFunctionReturn(0); 1261e1244c69SJed Brown } 1262e1244c69SJed Brown 1263e1244c69SJed Brown #undef __FUNCT__ 126455849f57SBarry Smith #define __FUNCT__ "TSLoad" 126555849f57SBarry Smith /*@C 126655849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 126755849f57SBarry Smith 126855849f57SBarry Smith Collective on PetscViewer 126955849f57SBarry Smith 127055849f57SBarry Smith Input Parameters: 127155849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 127255849f57SBarry Smith some related function before a call to TSLoad(). 127355849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 127455849f57SBarry Smith 127555849f57SBarry Smith Level: intermediate 127655849f57SBarry Smith 127755849f57SBarry Smith Notes: 127855849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 127955849f57SBarry Smith 128055849f57SBarry Smith Notes for advanced users: 128155849f57SBarry Smith Most users should not need to know the details of the binary storage 128255849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 128355849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 128455849f57SBarry Smith format is 128555849f57SBarry Smith .vb 128655849f57SBarry Smith has not yet been determined 128755849f57SBarry Smith .ve 128855849f57SBarry Smith 128955849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 129055849f57SBarry Smith @*/ 1291f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 129255849f57SBarry Smith { 129355849f57SBarry Smith PetscErrorCode ierr; 129455849f57SBarry Smith PetscBool isbinary; 129555849f57SBarry Smith PetscInt classid; 129655849f57SBarry Smith char type[256]; 12972d53ad75SBarry Smith DMTS sdm; 1298ad6bc421SBarry Smith DM dm; 129955849f57SBarry Smith 130055849f57SBarry Smith PetscFunctionBegin; 1301f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 130255849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 130355849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 130455849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 130555849f57SBarry Smith 130655849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 1307ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 130855849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 1309f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1310f2c2a1b9SBarry Smith if (ts->ops->load) { 1311f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1312f2c2a1b9SBarry Smith } 1313ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1314ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1315ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1316f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1317f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 13182d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 13192d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 132055849f57SBarry Smith PetscFunctionReturn(0); 132155849f57SBarry Smith } 132255849f57SBarry Smith 13239804daf3SBarry Smith #include <petscdraw.h> 1324e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1325e04113cfSBarry Smith #include <petscviewersaws.h> 1326f05ece33SBarry Smith #endif 132755849f57SBarry Smith #undef __FUNCT__ 13284a2ae208SSatish Balay #define __FUNCT__ "TSView" 13297e2c5f70SBarry Smith /*@C 1330d763cef2SBarry Smith TSView - Prints the TS data structure. 1331d763cef2SBarry Smith 13324c49b128SBarry Smith Collective on TS 1333d763cef2SBarry Smith 1334d763cef2SBarry Smith Input Parameters: 1335d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1336d763cef2SBarry Smith - viewer - visualization context 1337d763cef2SBarry Smith 1338d763cef2SBarry Smith Options Database Key: 1339d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1340d763cef2SBarry Smith 1341d763cef2SBarry Smith Notes: 1342d763cef2SBarry Smith The available visualization contexts include 1343b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1344b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1345d763cef2SBarry Smith output where only the first processor opens 1346d763cef2SBarry Smith the file. All other processors send their 1347d763cef2SBarry Smith data to the first processor to print. 1348d763cef2SBarry Smith 1349d763cef2SBarry Smith The user can open an alternative visualization context with 1350b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1351d763cef2SBarry Smith 1352d763cef2SBarry Smith Level: beginner 1353d763cef2SBarry Smith 1354d763cef2SBarry Smith .keywords: TS, timestep, view 1355d763cef2SBarry Smith 1356b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1357d763cef2SBarry Smith @*/ 13587087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1359d763cef2SBarry Smith { 1360dfbe8321SBarry Smith PetscErrorCode ierr; 136119fd82e9SBarry Smith TSType type; 13622b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 13632d53ad75SBarry Smith DMTS sdm; 1364e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1365f05ece33SBarry Smith PetscBool isams; 1366f05ece33SBarry Smith #endif 1367d763cef2SBarry Smith 1368d763cef2SBarry Smith PetscFunctionBegin; 13690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13703050cee2SBarry Smith if (!viewer) { 1371ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 13723050cee2SBarry Smith } 13730700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1374c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1375fd16b177SBarry Smith 1376251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1377251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 137855849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 13792b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1380e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1381e04113cfSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&isams);CHKERRQ(ierr); 1382f05ece33SBarry Smith #endif 138332077d6dSBarry Smith if (iascii) { 1384dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 138577431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 13867c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1387d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 13885ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1389c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1390d763cef2SBarry Smith } 13915ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1392193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 13932d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 13942d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1395d52bd9f3SBarry Smith if (ts->ops->view) { 1396d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1397d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1398d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1399d52bd9f3SBarry Smith } 14000f5bd95cSBarry Smith } else if (isstring) { 1401a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1402b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 140355849f57SBarry Smith } else if (isbinary) { 140455849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 140555849f57SBarry Smith MPI_Comm comm; 140655849f57SBarry Smith PetscMPIInt rank; 140755849f57SBarry Smith char type[256]; 140855849f57SBarry Smith 140955849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 141055849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 141155849f57SBarry Smith if (!rank) { 141255849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 141355849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 141455849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 141555849f57SBarry Smith } 141655849f57SBarry Smith if (ts->ops->view) { 141755849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 141855849f57SBarry Smith } 1419f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1420f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 14212d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 14222d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 14232b0a91c0SBarry Smith } else if (isdraw) { 14242b0a91c0SBarry Smith PetscDraw draw; 14252b0a91c0SBarry Smith char str[36]; 142689fd9fafSBarry Smith PetscReal x,y,bottom,h; 14272b0a91c0SBarry Smith 14282b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 14292b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 14302b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 14312b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 14320298fd71SBarry Smith ierr = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 143389fd9fafSBarry Smith bottom = y - h; 14342b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 14352b0a91c0SBarry Smith if (ts->ops->view) { 14362b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 14372b0a91c0SBarry Smith } 14382b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1439e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1440f05ece33SBarry Smith } else if (isams) { 1441d45a07a7SBarry Smith PetscMPIInt rank; 14422657e9d9SBarry Smith const char *name; 14432657e9d9SBarry Smith 14442657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1445d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1446d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1447d45a07a7SBarry Smith char dir[1024]; 1448d45a07a7SBarry Smith 1449e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1450a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 14512657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 14522657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 14532657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1454d763cef2SBarry Smith } 14550acecf5bSBarry Smith if (ts->ops->view) { 14560acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 14570acecf5bSBarry Smith } 1458f05ece33SBarry Smith #endif 1459f05ece33SBarry Smith } 1460f05ece33SBarry Smith 1461b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1462251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1463b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1464d763cef2SBarry Smith PetscFunctionReturn(0); 1465d763cef2SBarry Smith } 1466d763cef2SBarry Smith 1467d763cef2SBarry Smith 14684a2ae208SSatish Balay #undef __FUNCT__ 14694a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1470b07ff414SBarry Smith /*@ 1471d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1472d763cef2SBarry Smith the timesteppers. 1473d763cef2SBarry Smith 14743f9fe445SBarry Smith Logically Collective on TS 1475d763cef2SBarry Smith 1476d763cef2SBarry Smith Input Parameters: 1477d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1478d763cef2SBarry Smith - usrP - optional user context 1479d763cef2SBarry Smith 1480d763cef2SBarry Smith Level: intermediate 1481d763cef2SBarry Smith 1482d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1483d763cef2SBarry Smith 1484d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1485d763cef2SBarry Smith @*/ 14867087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1487d763cef2SBarry Smith { 1488d763cef2SBarry Smith PetscFunctionBegin; 14890700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1490d763cef2SBarry Smith ts->user = usrP; 1491d763cef2SBarry Smith PetscFunctionReturn(0); 1492d763cef2SBarry Smith } 1493d763cef2SBarry Smith 14944a2ae208SSatish Balay #undef __FUNCT__ 14954a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1496b07ff414SBarry Smith /*@ 1497d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1498d763cef2SBarry Smith timestepper. 1499d763cef2SBarry Smith 1500d763cef2SBarry Smith Not Collective 1501d763cef2SBarry Smith 1502d763cef2SBarry Smith Input Parameter: 1503d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1504d763cef2SBarry Smith 1505d763cef2SBarry Smith Output Parameter: 1506d763cef2SBarry Smith . usrP - user context 1507d763cef2SBarry Smith 1508d763cef2SBarry Smith Level: intermediate 1509d763cef2SBarry Smith 1510d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1511d763cef2SBarry Smith 1512d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1513d763cef2SBarry Smith @*/ 1514e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1515d763cef2SBarry Smith { 1516d763cef2SBarry Smith PetscFunctionBegin; 15170700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1518e71120c6SJed Brown *(void**)usrP = ts->user; 1519d763cef2SBarry Smith PetscFunctionReturn(0); 1520d763cef2SBarry Smith } 1521d763cef2SBarry Smith 15224a2ae208SSatish Balay #undef __FUNCT__ 15234a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1524d763cef2SBarry Smith /*@ 1525b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1526d763cef2SBarry Smith 1527d763cef2SBarry Smith Not Collective 1528d763cef2SBarry Smith 1529d763cef2SBarry Smith Input Parameter: 1530d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1531d763cef2SBarry Smith 1532d763cef2SBarry Smith Output Parameter: 1533b8123daeSJed Brown . iter - number of steps completed so far 1534d763cef2SBarry Smith 1535d763cef2SBarry Smith Level: intermediate 1536d763cef2SBarry Smith 1537d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 15389be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 1539d763cef2SBarry Smith @*/ 15407087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 1541d763cef2SBarry Smith { 1542d763cef2SBarry Smith PetscFunctionBegin; 15430700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15444482741eSBarry Smith PetscValidIntPointer(iter,2); 1545d763cef2SBarry Smith *iter = ts->steps; 1546d763cef2SBarry Smith PetscFunctionReturn(0); 1547d763cef2SBarry Smith } 1548d763cef2SBarry Smith 15494a2ae208SSatish Balay #undef __FUNCT__ 15504a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1551d763cef2SBarry Smith /*@ 1552d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1553d763cef2SBarry Smith as well as the initial time. 1554d763cef2SBarry Smith 15553f9fe445SBarry Smith Logically Collective on TS 1556d763cef2SBarry Smith 1557d763cef2SBarry Smith Input Parameters: 1558d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1559d763cef2SBarry Smith . initial_time - the initial time 1560d763cef2SBarry Smith - time_step - the size of the timestep 1561d763cef2SBarry Smith 1562d763cef2SBarry Smith Level: intermediate 1563d763cef2SBarry Smith 1564d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1565d763cef2SBarry Smith 1566d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1567d763cef2SBarry Smith @*/ 15687087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1569d763cef2SBarry Smith { 1570e144a568SJed Brown PetscErrorCode ierr; 1571e144a568SJed Brown 1572d763cef2SBarry Smith PetscFunctionBegin; 15730700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1574e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1575d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1576d763cef2SBarry Smith PetscFunctionReturn(0); 1577d763cef2SBarry Smith } 1578d763cef2SBarry Smith 15794a2ae208SSatish Balay #undef __FUNCT__ 15804a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1581d763cef2SBarry Smith /*@ 1582d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1583d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1584d763cef2SBarry Smith 15853f9fe445SBarry Smith Logically Collective on TS 1586d763cef2SBarry Smith 1587d763cef2SBarry Smith Input Parameters: 1588d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1589d763cef2SBarry Smith - time_step - the size of the timestep 1590d763cef2SBarry Smith 1591d763cef2SBarry Smith Level: intermediate 1592d763cef2SBarry Smith 1593d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1594d763cef2SBarry Smith 1595d763cef2SBarry Smith .keywords: TS, set, timestep 1596d763cef2SBarry Smith @*/ 15977087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1598d763cef2SBarry Smith { 1599d763cef2SBarry Smith PetscFunctionBegin; 16000700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1601c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1602d763cef2SBarry Smith ts->time_step = time_step; 160331748224SBarry Smith ts->time_step_orig = time_step; 1604d763cef2SBarry Smith PetscFunctionReturn(0); 1605d763cef2SBarry Smith } 1606d763cef2SBarry Smith 16074a2ae208SSatish Balay #undef __FUNCT__ 1608a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1609a43b19c4SJed Brown /*@ 161049354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 161149354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 161249354f04SShri Abhyankar or just return at the final time TS computed. 1613a43b19c4SJed Brown 1614a43b19c4SJed Brown Logically Collective on TS 1615a43b19c4SJed Brown 1616a43b19c4SJed Brown Input Parameter: 1617a43b19c4SJed Brown + ts - the time-step context 161849354f04SShri Abhyankar - eftopt - exact final time option 1619a43b19c4SJed Brown 1620a43b19c4SJed Brown Level: beginner 1621a43b19c4SJed Brown 1622a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 1623a43b19c4SJed Brown @*/ 162449354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 1625a43b19c4SJed Brown { 1626a43b19c4SJed Brown PetscFunctionBegin; 1627a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 162849354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 162949354f04SShri Abhyankar ts->exact_final_time = eftopt; 1630a43b19c4SJed Brown PetscFunctionReturn(0); 1631a43b19c4SJed Brown } 1632a43b19c4SJed Brown 1633a43b19c4SJed Brown #undef __FUNCT__ 16344a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1635d763cef2SBarry Smith /*@ 1636d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1637d763cef2SBarry Smith 1638d763cef2SBarry Smith Not Collective 1639d763cef2SBarry Smith 1640d763cef2SBarry Smith Input Parameter: 1641d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1642d763cef2SBarry Smith 1643d763cef2SBarry Smith Output Parameter: 1644d763cef2SBarry Smith . dt - the current timestep size 1645d763cef2SBarry Smith 1646d763cef2SBarry Smith Level: intermediate 1647d763cef2SBarry Smith 1648d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1649d763cef2SBarry Smith 1650d763cef2SBarry Smith .keywords: TS, get, timestep 1651d763cef2SBarry Smith @*/ 16527087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 1653d763cef2SBarry Smith { 1654d763cef2SBarry Smith PetscFunctionBegin; 16550700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1656f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1657d763cef2SBarry Smith *dt = ts->time_step; 1658d763cef2SBarry Smith PetscFunctionReturn(0); 1659d763cef2SBarry Smith } 1660d763cef2SBarry Smith 16614a2ae208SSatish Balay #undef __FUNCT__ 16624a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1663d8e5e3e6SSatish Balay /*@ 1664d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1665d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1666d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1667d763cef2SBarry Smith the solution at the next timestep has been calculated. 1668d763cef2SBarry Smith 1669d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1670d763cef2SBarry Smith 1671d763cef2SBarry Smith Input Parameter: 1672d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1673d763cef2SBarry Smith 1674d763cef2SBarry Smith Output Parameter: 1675d763cef2SBarry Smith . v - the vector containing the solution 1676d763cef2SBarry Smith 1677d763cef2SBarry Smith Level: intermediate 1678d763cef2SBarry Smith 1679d763cef2SBarry Smith .seealso: TSGetTimeStep() 1680d763cef2SBarry Smith 1681d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1682d763cef2SBarry Smith @*/ 16837087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1684d763cef2SBarry Smith { 1685d763cef2SBarry Smith PetscFunctionBegin; 16860700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 16874482741eSBarry Smith PetscValidPointer(v,2); 16888737fe31SLisandro Dalcin *v = ts->vec_sol; 1689d763cef2SBarry Smith PetscFunctionReturn(0); 1690d763cef2SBarry Smith } 1691d763cef2SBarry Smith 1692bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 16934a2ae208SSatish Balay #undef __FUNCT__ 1694bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1695d8e5e3e6SSatish Balay /*@ 1696bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1697d763cef2SBarry Smith 1698bdad233fSMatthew Knepley Not collective 1699d763cef2SBarry Smith 1700bdad233fSMatthew Knepley Input Parameters: 1701bdad233fSMatthew Knepley + ts - The TS 1702bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1703d763cef2SBarry Smith .vb 17040910c330SBarry Smith U_t - A U = 0 (linear) 17050910c330SBarry Smith U_t - A(t) U = 0 (linear) 17060910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 1707d763cef2SBarry Smith .ve 1708d763cef2SBarry Smith 1709d763cef2SBarry Smith Level: beginner 1710d763cef2SBarry Smith 1711bdad233fSMatthew Knepley .keywords: TS, problem type 1712bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1713d763cef2SBarry Smith @*/ 17147087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1715a7cc72afSBarry Smith { 17169e2a6581SJed Brown PetscErrorCode ierr; 17179e2a6581SJed Brown 1718d763cef2SBarry Smith PetscFunctionBegin; 17190700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1720bdad233fSMatthew Knepley ts->problem_type = type; 17219e2a6581SJed Brown if (type == TS_LINEAR) { 17229e2a6581SJed Brown SNES snes; 17239e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 17249e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 17259e2a6581SJed Brown } 1726d763cef2SBarry Smith PetscFunctionReturn(0); 1727d763cef2SBarry Smith } 1728d763cef2SBarry Smith 1729bdad233fSMatthew Knepley #undef __FUNCT__ 1730bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1731bdad233fSMatthew Knepley /*@C 1732bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1733bdad233fSMatthew Knepley 1734bdad233fSMatthew Knepley Not collective 1735bdad233fSMatthew Knepley 1736bdad233fSMatthew Knepley Input Parameter: 1737bdad233fSMatthew Knepley . ts - The TS 1738bdad233fSMatthew Knepley 1739bdad233fSMatthew Knepley Output Parameter: 1740bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1741bdad233fSMatthew Knepley .vb 1742089b2837SJed Brown M U_t = A U 1743089b2837SJed Brown M(t) U_t = A(t) U 1744b5abc632SBarry Smith F(t,U,U_t) 1745bdad233fSMatthew Knepley .ve 1746bdad233fSMatthew Knepley 1747bdad233fSMatthew Knepley Level: beginner 1748bdad233fSMatthew Knepley 1749bdad233fSMatthew Knepley .keywords: TS, problem type 1750bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1751bdad233fSMatthew Knepley @*/ 17527087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1753a7cc72afSBarry Smith { 1754bdad233fSMatthew Knepley PetscFunctionBegin; 17550700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 17564482741eSBarry Smith PetscValidIntPointer(type,2); 1757bdad233fSMatthew Knepley *type = ts->problem_type; 1758bdad233fSMatthew Knepley PetscFunctionReturn(0); 1759bdad233fSMatthew Knepley } 1760d763cef2SBarry Smith 17614a2ae208SSatish Balay #undef __FUNCT__ 17624a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1763d763cef2SBarry Smith /*@ 1764d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1765d763cef2SBarry Smith of a timestepper. 1766d763cef2SBarry Smith 1767d763cef2SBarry Smith Collective on TS 1768d763cef2SBarry Smith 1769d763cef2SBarry Smith Input Parameter: 1770d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1771d763cef2SBarry Smith 1772d763cef2SBarry Smith Notes: 1773d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1774d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1775d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1776d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1777d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1778d763cef2SBarry Smith 1779d763cef2SBarry Smith Level: advanced 1780d763cef2SBarry Smith 1781d763cef2SBarry Smith .keywords: TS, timestep, setup 1782d763cef2SBarry Smith 1783d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1784d763cef2SBarry Smith @*/ 17857087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1786d763cef2SBarry Smith { 1787dfbe8321SBarry Smith PetscErrorCode ierr; 17886c6b9e74SPeter Brune DM dm; 17896c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 1790*94ab13aaSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,MatStructure*,void*); 17916c6b9e74SPeter Brune TSIJacobian ijac; 17926c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1793d763cef2SBarry Smith 1794d763cef2SBarry Smith PetscFunctionBegin; 17950700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1796277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1797277b19d0SLisandro Dalcin 17987adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 17999596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1800d763cef2SBarry Smith } 1801277b19d0SLisandro Dalcin 1802277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1803277b19d0SLisandro Dalcin 1804552698daSJed Brown ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 18051c3436cfSJed Brown 1806e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 1807e1244c69SJed Brown Mat Amat,Pmat; 1808e1244c69SJed Brown SNES snes; 1809e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1810e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 1811e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 1812e1244c69SJed Brown * have displaced the RHS matrix */ 1813e1244c69SJed Brown if (Amat == ts->Arhs) { 1814e1244c69SJed Brown ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 1815e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 1816e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 1817e1244c69SJed Brown } 1818e1244c69SJed Brown if (Pmat == ts->Brhs) { 1819e1244c69SJed Brown ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 1820e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 1821e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 1822e1244c69SJed Brown } 1823e1244c69SJed Brown } 1824e1244c69SJed Brown 1825277b19d0SLisandro Dalcin if (ts->ops->setup) { 1826000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1827277b19d0SLisandro Dalcin } 1828277b19d0SLisandro Dalcin 18296c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 18306c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 18316c6b9e74SPeter Brune */ 18326c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 18330298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 18346c6b9e74SPeter Brune if (!func) { 18356c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 18366c6b9e74SPeter Brune } 18376c6b9e74SPeter 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. 18386c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 18396c6b9e74SPeter Brune */ 18400298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 18410298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 18420298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 18436c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 18446c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 18456c6b9e74SPeter Brune } 1846277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1847277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1848277b19d0SLisandro Dalcin } 1849277b19d0SLisandro Dalcin 1850277b19d0SLisandro Dalcin #undef __FUNCT__ 1851277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1852277b19d0SLisandro Dalcin /*@ 1853277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1854277b19d0SLisandro Dalcin 1855277b19d0SLisandro Dalcin Collective on TS 1856277b19d0SLisandro Dalcin 1857277b19d0SLisandro Dalcin Input Parameter: 1858277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1859277b19d0SLisandro Dalcin 1860277b19d0SLisandro Dalcin Level: beginner 1861277b19d0SLisandro Dalcin 1862277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1863277b19d0SLisandro Dalcin 1864277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1865277b19d0SLisandro Dalcin @*/ 1866277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1867277b19d0SLisandro Dalcin { 1868277b19d0SLisandro Dalcin PetscErrorCode ierr; 1869277b19d0SLisandro Dalcin 1870277b19d0SLisandro Dalcin PetscFunctionBegin; 1871277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1872277b19d0SLisandro Dalcin if (ts->ops->reset) { 1873277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1874277b19d0SLisandro Dalcin } 1875277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 1876bbd56ea5SKarl Rupp 18774e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 18784e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1879214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 18806bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1881e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 1882e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 188338637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 1884bbd56ea5SKarl Rupp 1885277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1886d763cef2SBarry Smith PetscFunctionReturn(0); 1887d763cef2SBarry Smith } 1888d763cef2SBarry Smith 18894a2ae208SSatish Balay #undef __FUNCT__ 18904a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1891d8e5e3e6SSatish Balay /*@ 1892d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1893d763cef2SBarry Smith with TSCreate(). 1894d763cef2SBarry Smith 1895d763cef2SBarry Smith Collective on TS 1896d763cef2SBarry Smith 1897d763cef2SBarry Smith Input Parameter: 1898d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1899d763cef2SBarry Smith 1900d763cef2SBarry Smith Level: beginner 1901d763cef2SBarry Smith 1902d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1903d763cef2SBarry Smith 1904d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1905d763cef2SBarry Smith @*/ 19066bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1907d763cef2SBarry Smith { 19086849ba73SBarry Smith PetscErrorCode ierr; 1909d763cef2SBarry Smith 1910d763cef2SBarry Smith PetscFunctionBegin; 19116bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 19126bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 19136bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1914d763cef2SBarry Smith 19156bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1916277b19d0SLisandro Dalcin 1917e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 1918e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 19196bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 19206d4c513bSLisandro Dalcin 192184df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 19226bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 19236bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 19246bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 19256d4c513bSLisandro Dalcin 1926a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1927d763cef2SBarry Smith PetscFunctionReturn(0); 1928d763cef2SBarry Smith } 1929d763cef2SBarry Smith 19304a2ae208SSatish Balay #undef __FUNCT__ 19314a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1932d8e5e3e6SSatish Balay /*@ 1933d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1934d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1935d763cef2SBarry Smith 1936d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1937d763cef2SBarry Smith 1938d763cef2SBarry Smith Input Parameter: 1939d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1940d763cef2SBarry Smith 1941d763cef2SBarry Smith Output Parameter: 1942d763cef2SBarry Smith . snes - the nonlinear solver context 1943d763cef2SBarry Smith 1944d763cef2SBarry Smith Notes: 1945d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1946d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 194794b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1948d763cef2SBarry Smith 1949d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 19500298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 1951d763cef2SBarry Smith 1952d763cef2SBarry Smith Level: beginner 1953d763cef2SBarry Smith 1954d763cef2SBarry Smith .keywords: timestep, get, SNES 1955d763cef2SBarry Smith @*/ 19567087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1957d763cef2SBarry Smith { 1958d372ba47SLisandro Dalcin PetscErrorCode ierr; 1959d372ba47SLisandro Dalcin 1960d763cef2SBarry Smith PetscFunctionBegin; 19610700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 19624482741eSBarry Smith PetscValidPointer(snes,2); 1963d372ba47SLisandro Dalcin if (!ts->snes) { 1964ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 19650298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 19663bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 1967d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 1968496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 19699e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 19709e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 19719e2a6581SJed Brown } 1972d372ba47SLisandro Dalcin } 1973d763cef2SBarry Smith *snes = ts->snes; 1974d763cef2SBarry Smith PetscFunctionReturn(0); 1975d763cef2SBarry Smith } 1976d763cef2SBarry Smith 19774a2ae208SSatish Balay #undef __FUNCT__ 1978deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES" 1979deb2cd25SJed Brown /*@ 1980deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 1981deb2cd25SJed Brown 1982deb2cd25SJed Brown Collective 1983deb2cd25SJed Brown 1984deb2cd25SJed Brown Input Parameter: 1985deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 1986deb2cd25SJed Brown - snes - the nonlinear solver context 1987deb2cd25SJed Brown 1988deb2cd25SJed Brown Notes: 1989deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 1990deb2cd25SJed Brown 1991deb2cd25SJed Brown Level: developer 1992deb2cd25SJed Brown 1993deb2cd25SJed Brown .keywords: timestep, set, SNES 1994deb2cd25SJed Brown @*/ 1995deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 1996deb2cd25SJed Brown { 1997deb2cd25SJed Brown PetscErrorCode ierr; 1998*94ab13aaSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,MatStructure*,void*); 1999deb2cd25SJed Brown 2000deb2cd25SJed Brown PetscFunctionBegin; 2001deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2002deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2003deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2004deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2005bbd56ea5SKarl Rupp 2006deb2cd25SJed Brown ts->snes = snes; 2007bbd56ea5SKarl Rupp 20080298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 20090298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2010740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 20110298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2012740132f1SEmil Constantinescu } 2013deb2cd25SJed Brown PetscFunctionReturn(0); 2014deb2cd25SJed Brown } 2015deb2cd25SJed Brown 2016deb2cd25SJed Brown #undef __FUNCT__ 201794b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 2018d8e5e3e6SSatish Balay /*@ 201994b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2020d763cef2SBarry Smith a TS (timestepper) context. 2021d763cef2SBarry Smith 202294b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2023d763cef2SBarry Smith 2024d763cef2SBarry Smith Input Parameter: 2025d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2026d763cef2SBarry Smith 2027d763cef2SBarry Smith Output Parameter: 202894b7f48cSBarry Smith . ksp - the nonlinear solver context 2029d763cef2SBarry Smith 2030d763cef2SBarry Smith Notes: 203194b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2032d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2033d763cef2SBarry Smith KSP and PC contexts as well. 2034d763cef2SBarry Smith 203594b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 20360298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2037d763cef2SBarry Smith 2038d763cef2SBarry Smith Level: beginner 2039d763cef2SBarry Smith 204094b7f48cSBarry Smith .keywords: timestep, get, KSP 2041d763cef2SBarry Smith @*/ 20427087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2043d763cef2SBarry Smith { 2044d372ba47SLisandro Dalcin PetscErrorCode ierr; 2045089b2837SJed Brown SNES snes; 2046d372ba47SLisandro Dalcin 2047d763cef2SBarry Smith PetscFunctionBegin; 20480700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20494482741eSBarry Smith PetscValidPointer(ksp,2); 205017186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2051e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2052089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2053089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2054d763cef2SBarry Smith PetscFunctionReturn(0); 2055d763cef2SBarry Smith } 2056d763cef2SBarry Smith 2057d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2058d763cef2SBarry Smith 20594a2ae208SSatish Balay #undef __FUNCT__ 2060adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 2061adb62b0dSMatthew Knepley /*@ 2062adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2063adb62b0dSMatthew Knepley maximum time for iteration. 2064adb62b0dSMatthew Knepley 20653f9fe445SBarry Smith Not Collective 2066adb62b0dSMatthew Knepley 2067adb62b0dSMatthew Knepley Input Parameters: 2068adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 20690298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 20700298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2071adb62b0dSMatthew Knepley 2072adb62b0dSMatthew Knepley Level: intermediate 2073adb62b0dSMatthew Knepley 2074adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2075adb62b0dSMatthew Knepley @*/ 20767087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2077adb62b0dSMatthew Knepley { 2078adb62b0dSMatthew Knepley PetscFunctionBegin; 20790700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2080abc0a331SBarry Smith if (maxsteps) { 20814482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2082adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2083adb62b0dSMatthew Knepley } 2084abc0a331SBarry Smith if (maxtime) { 20854482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2086adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2087adb62b0dSMatthew Knepley } 2088adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2089adb62b0dSMatthew Knepley } 2090adb62b0dSMatthew Knepley 2091adb62b0dSMatthew Knepley #undef __FUNCT__ 20924a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 2093d763cef2SBarry Smith /*@ 2094d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2095d763cef2SBarry Smith maximum time for iteration. 2096d763cef2SBarry Smith 20973f9fe445SBarry Smith Logically Collective on TS 2098d763cef2SBarry Smith 2099d763cef2SBarry Smith Input Parameters: 2100d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2101d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2102d763cef2SBarry Smith - maxtime - final time to iterate to 2103d763cef2SBarry Smith 2104d763cef2SBarry Smith Options Database Keys: 2105d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 21063bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2107d763cef2SBarry Smith 2108d763cef2SBarry Smith Notes: 2109d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2110d763cef2SBarry Smith 2111d763cef2SBarry Smith Level: intermediate 2112d763cef2SBarry Smith 2113d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2114a43b19c4SJed Brown 2115a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2116d763cef2SBarry Smith @*/ 21177087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2118d763cef2SBarry Smith { 2119d763cef2SBarry Smith PetscFunctionBegin; 21200700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2121c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2122c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 212339b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 212439b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2125d763cef2SBarry Smith PetscFunctionReturn(0); 2126d763cef2SBarry Smith } 2127d763cef2SBarry Smith 21284a2ae208SSatish Balay #undef __FUNCT__ 21294a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 2130d763cef2SBarry Smith /*@ 2131d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2132d763cef2SBarry Smith for use by the TS routines. 2133d763cef2SBarry Smith 21343f9fe445SBarry Smith Logically Collective on TS and Vec 2135d763cef2SBarry Smith 2136d763cef2SBarry Smith Input Parameters: 2137d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 21380910c330SBarry Smith - u - the solution vector 2139d763cef2SBarry Smith 2140d763cef2SBarry Smith Level: beginner 2141d763cef2SBarry Smith 2142d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2143d763cef2SBarry Smith @*/ 21440910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2145d763cef2SBarry Smith { 21468737fe31SLisandro Dalcin PetscErrorCode ierr; 2147496e6a7aSJed Brown DM dm; 21488737fe31SLisandro Dalcin 2149d763cef2SBarry Smith PetscFunctionBegin; 21500700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 21510910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 21520910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 21536bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2154bbd56ea5SKarl Rupp 21550910c330SBarry Smith ts->vec_sol = u; 2156bbd56ea5SKarl Rupp 2157496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 21580910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2159d763cef2SBarry Smith PetscFunctionReturn(0); 2160d763cef2SBarry Smith } 2161d763cef2SBarry Smith 2162e74ef692SMatthew Knepley #undef __FUNCT__ 2163e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 2164ac226902SBarry Smith /*@C 2165000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 21663f2090d5SJed Brown called once at the beginning of each time step. 2167000e7ae3SMatthew Knepley 21683f9fe445SBarry Smith Logically Collective on TS 2169000e7ae3SMatthew Knepley 2170000e7ae3SMatthew Knepley Input Parameters: 2171000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2172000e7ae3SMatthew Knepley - func - The function 2173000e7ae3SMatthew Knepley 2174000e7ae3SMatthew Knepley Calling sequence of func: 2175000e7ae3SMatthew Knepley . func (TS ts); 2176000e7ae3SMatthew Knepley 2177000e7ae3SMatthew Knepley Level: intermediate 2178000e7ae3SMatthew Knepley 2179b8123daeSJed Brown Note: 2180b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 2181b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 2182b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 2183b8123daeSJed Brown 2184000e7ae3SMatthew Knepley .keywords: TS, timestep 21859be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 2186000e7ae3SMatthew Knepley @*/ 21877087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 2188000e7ae3SMatthew Knepley { 2189000e7ae3SMatthew Knepley PetscFunctionBegin; 21900700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2191ae60f76fSBarry Smith ts->prestep = func; 2192000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2193000e7ae3SMatthew Knepley } 2194000e7ae3SMatthew Knepley 2195e74ef692SMatthew Knepley #undef __FUNCT__ 21963f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 219709ee8438SJed Brown /*@ 21983f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 21993f2090d5SJed Brown 22003f2090d5SJed Brown Collective on TS 22013f2090d5SJed Brown 22023f2090d5SJed Brown Input Parameters: 22033f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 22043f2090d5SJed Brown 22053f2090d5SJed Brown Notes: 22063f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 22073f2090d5SJed Brown so most users would not generally call this routine themselves. 22083f2090d5SJed Brown 22093f2090d5SJed Brown Level: developer 22103f2090d5SJed Brown 22113f2090d5SJed Brown .keywords: TS, timestep 22129be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 22133f2090d5SJed Brown @*/ 22147087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 22153f2090d5SJed Brown { 22163f2090d5SJed Brown PetscErrorCode ierr; 22173f2090d5SJed Brown 22183f2090d5SJed Brown PetscFunctionBegin; 22190700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2220ae60f76fSBarry Smith if (ts->prestep) { 2221ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 2222312ce896SJed Brown } 22233f2090d5SJed Brown PetscFunctionReturn(0); 22243f2090d5SJed Brown } 22253f2090d5SJed Brown 22263f2090d5SJed Brown #undef __FUNCT__ 2227b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 2228b8123daeSJed Brown /*@C 2229b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 2230b8123daeSJed Brown called once at the beginning of each stage. 2231b8123daeSJed Brown 2232b8123daeSJed Brown Logically Collective on TS 2233b8123daeSJed Brown 2234b8123daeSJed Brown Input Parameters: 2235b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 2236b8123daeSJed Brown - func - The function 2237b8123daeSJed Brown 2238b8123daeSJed Brown Calling sequence of func: 2239b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 2240b8123daeSJed Brown 2241b8123daeSJed Brown Level: intermediate 2242b8123daeSJed Brown 2243b8123daeSJed Brown Note: 2244b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 2245b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 2246b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 2247b8123daeSJed Brown 2248b8123daeSJed Brown .keywords: TS, timestep 22499be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 2250b8123daeSJed Brown @*/ 2251b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 2252b8123daeSJed Brown { 2253b8123daeSJed Brown PetscFunctionBegin; 2254b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2255ae60f76fSBarry Smith ts->prestage = func; 2256b8123daeSJed Brown PetscFunctionReturn(0); 2257b8123daeSJed Brown } 2258b8123daeSJed Brown 2259b8123daeSJed Brown #undef __FUNCT__ 22609be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage" 22619be3e283SDebojyoti Ghosh /*@C 22629be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 22639be3e283SDebojyoti Ghosh called once at the end of each stage. 22649be3e283SDebojyoti Ghosh 22659be3e283SDebojyoti Ghosh Logically Collective on TS 22669be3e283SDebojyoti Ghosh 22679be3e283SDebojyoti Ghosh Input Parameters: 22689be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 22699be3e283SDebojyoti Ghosh - func - The function 22709be3e283SDebojyoti Ghosh 22719be3e283SDebojyoti Ghosh Calling sequence of func: 22729be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 22739be3e283SDebojyoti Ghosh 22749be3e283SDebojyoti Ghosh Level: intermediate 22759be3e283SDebojyoti Ghosh 22769be3e283SDebojyoti Ghosh Note: 22779be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 22789be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 22799be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 22809be3e283SDebojyoti Ghosh 22819be3e283SDebojyoti Ghosh .keywords: TS, timestep 22829be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 22839be3e283SDebojyoti Ghosh @*/ 22849be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 22859be3e283SDebojyoti Ghosh { 22869be3e283SDebojyoti Ghosh PetscFunctionBegin; 22879be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 22889be3e283SDebojyoti Ghosh ts->poststage = func; 22899be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 22909be3e283SDebojyoti Ghosh } 22919be3e283SDebojyoti Ghosh 22929be3e283SDebojyoti Ghosh #undef __FUNCT__ 2293b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 2294b8123daeSJed Brown /*@ 2295b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 2296b8123daeSJed Brown 2297b8123daeSJed Brown Collective on TS 2298b8123daeSJed Brown 2299b8123daeSJed Brown Input Parameters: 2300b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 23019be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 2302b8123daeSJed Brown 2303b8123daeSJed Brown Notes: 2304b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 2305b8123daeSJed Brown most users would not generally call this routine themselves. 2306b8123daeSJed Brown 2307b8123daeSJed Brown Level: developer 2308b8123daeSJed Brown 2309b8123daeSJed Brown .keywords: TS, timestep 23109be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 2311b8123daeSJed Brown @*/ 2312b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 2313b8123daeSJed Brown { 2314b8123daeSJed Brown PetscErrorCode ierr; 2315b8123daeSJed Brown 2316b8123daeSJed Brown PetscFunctionBegin; 2317b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2318ae60f76fSBarry Smith if (ts->prestage) { 2319ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 2320b8123daeSJed Brown } 2321b8123daeSJed Brown PetscFunctionReturn(0); 2322b8123daeSJed Brown } 2323b8123daeSJed Brown 2324b8123daeSJed Brown #undef __FUNCT__ 23259be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage" 23269be3e283SDebojyoti Ghosh /*@ 23279be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 23289be3e283SDebojyoti Ghosh 23299be3e283SDebojyoti Ghosh Collective on TS 23309be3e283SDebojyoti Ghosh 23319be3e283SDebojyoti Ghosh Input Parameters: 23329be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 23339be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 23349be3e283SDebojyoti Ghosh stageindex - Stage number 23359be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 23369be3e283SDebojyoti Ghosh of stages) with the stage solutions 23379be3e283SDebojyoti Ghosh 23389be3e283SDebojyoti Ghosh Notes: 23399be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 23409be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 23419be3e283SDebojyoti Ghosh 23429be3e283SDebojyoti Ghosh Level: developer 23439be3e283SDebojyoti Ghosh 23449be3e283SDebojyoti Ghosh .keywords: TS, timestep 23459be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 23469be3e283SDebojyoti Ghosh @*/ 23479be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 23489be3e283SDebojyoti Ghosh { 23499be3e283SDebojyoti Ghosh PetscErrorCode ierr; 23509be3e283SDebojyoti Ghosh 23519be3e283SDebojyoti Ghosh PetscFunctionBegin; 23529be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 23539be3e283SDebojyoti Ghosh if (ts->prestage) { 23549be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 23559be3e283SDebojyoti Ghosh } 23569be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 23579be3e283SDebojyoti Ghosh } 23589be3e283SDebojyoti Ghosh 23599be3e283SDebojyoti Ghosh #undef __FUNCT__ 2360e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 2361ac226902SBarry Smith /*@C 2362000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 23633f2090d5SJed Brown called once at the end of each time step. 2364000e7ae3SMatthew Knepley 23653f9fe445SBarry Smith Logically Collective on TS 2366000e7ae3SMatthew Knepley 2367000e7ae3SMatthew Knepley Input Parameters: 2368000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2369000e7ae3SMatthew Knepley - func - The function 2370000e7ae3SMatthew Knepley 2371000e7ae3SMatthew Knepley Calling sequence of func: 2372b8123daeSJed Brown $ func (TS ts); 2373000e7ae3SMatthew Knepley 2374000e7ae3SMatthew Knepley Level: intermediate 2375000e7ae3SMatthew Knepley 2376000e7ae3SMatthew Knepley .keywords: TS, timestep 2377b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 2378000e7ae3SMatthew Knepley @*/ 23797087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 2380000e7ae3SMatthew Knepley { 2381000e7ae3SMatthew Knepley PetscFunctionBegin; 23820700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2383ae60f76fSBarry Smith ts->poststep = func; 2384000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2385000e7ae3SMatthew Knepley } 2386000e7ae3SMatthew Knepley 2387e74ef692SMatthew Knepley #undef __FUNCT__ 23883f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 238909ee8438SJed Brown /*@ 23903f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 23913f2090d5SJed Brown 23923f2090d5SJed Brown Collective on TS 23933f2090d5SJed Brown 23943f2090d5SJed Brown Input Parameters: 23953f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 23963f2090d5SJed Brown 23973f2090d5SJed Brown Notes: 23983f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 23993f2090d5SJed Brown so most users would not generally call this routine themselves. 24003f2090d5SJed Brown 24013f2090d5SJed Brown Level: developer 24023f2090d5SJed Brown 24033f2090d5SJed Brown .keywords: TS, timestep 24043f2090d5SJed Brown @*/ 24057087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 24063f2090d5SJed Brown { 24073f2090d5SJed Brown PetscErrorCode ierr; 24083f2090d5SJed Brown 24093f2090d5SJed Brown PetscFunctionBegin; 24100700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2411ae60f76fSBarry Smith if (ts->poststep) { 2412ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 241372ac3e02SJed Brown } 24143f2090d5SJed Brown PetscFunctionReturn(0); 24153f2090d5SJed Brown } 24163f2090d5SJed Brown 2417d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 2418d763cef2SBarry Smith 24194a2ae208SSatish Balay #undef __FUNCT__ 2420a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 2421d763cef2SBarry Smith /*@C 2422a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 2423d763cef2SBarry Smith timestep to display the iteration's progress. 2424d763cef2SBarry Smith 24253f9fe445SBarry Smith Logically Collective on TS 2426d763cef2SBarry Smith 2427d763cef2SBarry Smith Input Parameters: 2428d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2429e213d8f1SJed Brown . monitor - monitoring routine 2430329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 24310298fd71SBarry Smith monitor routine (use NULL if no context is desired) 2432b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 24330298fd71SBarry Smith (may be NULL) 2434d763cef2SBarry Smith 2435e213d8f1SJed Brown Calling sequence of monitor: 24360910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 2437d763cef2SBarry Smith 2438d763cef2SBarry Smith + ts - the TS context 243988c05cc5SBarry 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 244088c05cc5SBarry Smith been interpolated to) 24411f06c33eSBarry Smith . time - current time 24420910c330SBarry Smith . u - current iterate 2443d763cef2SBarry Smith - mctx - [optional] monitoring context 2444d763cef2SBarry Smith 2445d763cef2SBarry Smith Notes: 2446d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 2447d763cef2SBarry Smith already has been loaded. 2448d763cef2SBarry Smith 2449025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 2450025f1a04SBarry Smith 2451d763cef2SBarry Smith Level: intermediate 2452d763cef2SBarry Smith 2453d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2454d763cef2SBarry Smith 2455a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 2456d763cef2SBarry Smith @*/ 2457c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 2458d763cef2SBarry Smith { 2459d763cef2SBarry Smith PetscFunctionBegin; 24600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 246117186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2462d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 24638704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 2464d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 2465d763cef2SBarry Smith PetscFunctionReturn(0); 2466d763cef2SBarry Smith } 2467d763cef2SBarry Smith 24684a2ae208SSatish Balay #undef __FUNCT__ 2469a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 2470d763cef2SBarry Smith /*@C 2471a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 2472d763cef2SBarry Smith 24733f9fe445SBarry Smith Logically Collective on TS 2474d763cef2SBarry Smith 2475d763cef2SBarry Smith Input Parameters: 2476d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2477d763cef2SBarry Smith 2478d763cef2SBarry Smith Notes: 2479d763cef2SBarry Smith There is no way to remove a single, specific monitor. 2480d763cef2SBarry Smith 2481d763cef2SBarry Smith Level: intermediate 2482d763cef2SBarry Smith 2483d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2484d763cef2SBarry Smith 2485a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 2486d763cef2SBarry Smith @*/ 24877087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 2488d763cef2SBarry Smith { 2489d952e501SBarry Smith PetscErrorCode ierr; 2490d952e501SBarry Smith PetscInt i; 2491d952e501SBarry Smith 2492d763cef2SBarry Smith PetscFunctionBegin; 24930700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2494d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 24958704b422SBarry Smith if (ts->monitordestroy[i]) { 24968704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 2497d952e501SBarry Smith } 2498d952e501SBarry Smith } 2499d763cef2SBarry Smith ts->numbermonitors = 0; 2500d763cef2SBarry Smith PetscFunctionReturn(0); 2501d763cef2SBarry Smith } 2502d763cef2SBarry Smith 25034a2ae208SSatish Balay #undef __FUNCT__ 2504a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 2505d8e5e3e6SSatish Balay /*@ 2506a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 25075516499fSSatish Balay 25085516499fSSatish Balay Level: intermediate 250941251cbbSSatish Balay 25105516499fSSatish Balay .keywords: TS, set, monitor 25115516499fSSatish Balay 251241251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 251341251cbbSSatish Balay @*/ 2514649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 2515d763cef2SBarry Smith { 2516dfbe8321SBarry Smith PetscErrorCode ierr; 2517ce94432eSBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts)); 2518d132466eSBarry Smith 2519d763cef2SBarry Smith PetscFunctionBegin; 2520649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2521971832b6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr); 2522649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2523d763cef2SBarry Smith PetscFunctionReturn(0); 2524d763cef2SBarry Smith } 2525d763cef2SBarry Smith 25264a2ae208SSatish Balay #undef __FUNCT__ 2527cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 2528cd652676SJed Brown /*@ 2529cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 2530cd652676SJed Brown 2531cd652676SJed Brown Logically Collective on TS 2532cd652676SJed Brown 2533cd652676SJed Brown Input Argument: 2534cd652676SJed Brown . ts - time stepping context 2535cd652676SJed Brown 2536cd652676SJed Brown Output Argument: 2537cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 2538cd652676SJed Brown 2539cd652676SJed Brown Level: intermediate 2540cd652676SJed Brown 2541cd652676SJed Brown .keywords: TS, set 2542cd652676SJed Brown 2543cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 2544cd652676SJed Brown @*/ 2545cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 2546cd652676SJed Brown { 2547cd652676SJed Brown PetscFunctionBegin; 2548cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2549cd652676SJed Brown ts->retain_stages = flg; 2550cd652676SJed Brown PetscFunctionReturn(0); 2551cd652676SJed Brown } 2552cd652676SJed Brown 2553cd652676SJed Brown #undef __FUNCT__ 2554cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 2555cd652676SJed Brown /*@ 2556cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 2557cd652676SJed Brown 2558cd652676SJed Brown Collective on TS 2559cd652676SJed Brown 2560cd652676SJed Brown Input Argument: 2561cd652676SJed Brown + ts - time stepping context 2562cd652676SJed Brown - t - time to interpolate to 2563cd652676SJed Brown 2564cd652676SJed Brown Output Argument: 25650910c330SBarry Smith . U - state at given time 2566cd652676SJed Brown 2567cd652676SJed Brown Notes: 2568cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 2569cd652676SJed Brown 2570cd652676SJed Brown Level: intermediate 2571cd652676SJed Brown 2572cd652676SJed Brown Developer Notes: 2573cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 2574cd652676SJed Brown 2575cd652676SJed Brown .keywords: TS, set 2576cd652676SJed Brown 2577cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 2578cd652676SJed Brown @*/ 25790910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 2580cd652676SJed Brown { 2581cd652676SJed Brown PetscErrorCode ierr; 2582cd652676SJed Brown 2583cd652676SJed Brown PetscFunctionBegin; 2584cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2585b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 25866712e2f1SBarry 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,(double)(ts->ptime-ts->time_step_prev),(double)ts->ptime); 2587ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 25880910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 2589cd652676SJed Brown PetscFunctionReturn(0); 2590cd652676SJed Brown } 2591cd652676SJed Brown 2592cd652676SJed Brown #undef __FUNCT__ 25934a2ae208SSatish Balay #define __FUNCT__ "TSStep" 2594d763cef2SBarry Smith /*@ 25956d9e5789SSean Farley TSStep - Steps one time step 2596d763cef2SBarry Smith 2597d763cef2SBarry Smith Collective on TS 2598d763cef2SBarry Smith 2599d763cef2SBarry Smith Input Parameter: 2600d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2601d763cef2SBarry Smith 26026d9e5789SSean Farley Level: intermediate 2603d763cef2SBarry Smith 2604b8123daeSJed Brown Notes: 2605b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 2606b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 2607b8123daeSJed Brown 260825cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 260925cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 261025cb2221SBarry Smith 2611d763cef2SBarry Smith .keywords: TS, timestep, solve 2612d763cef2SBarry Smith 26139be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 2614d763cef2SBarry Smith @*/ 2615193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 2616d763cef2SBarry Smith { 2617362cd11cSLisandro Dalcin PetscReal ptime_prev; 2618dfbe8321SBarry Smith PetscErrorCode ierr; 2619fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 2620d763cef2SBarry Smith 2621d763cef2SBarry Smith PetscFunctionBegin; 26220700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2623fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 2624fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 2625fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 2626fffbeea8SBarry Smith " type = {Preprint},\n" 2627fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 2628fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 2629fffbeea8SBarry Smith " year = {2014}\n}\n",&cite); 2630fffbeea8SBarry Smith 2631d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 2632d405a339SMatthew Knepley 2633362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 2634362cd11cSLisandro Dalcin ptime_prev = ts->ptime; 2635bbd56ea5SKarl Rupp 2636d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2637193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 2638d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2639bbd56ea5SKarl Rupp 2640362cd11cSLisandro Dalcin ts->time_step_prev = ts->ptime - ptime_prev; 2641362cd11cSLisandro Dalcin 2642362cd11cSLisandro Dalcin if (ts->reason < 0) { 2643cef5090cSJed Brown if (ts->errorifstepfailed) { 2644cef5090cSJed Brown if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) { 2645ce94432eSBarry 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]); 2646ce94432eSBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 2647cef5090cSJed Brown } 2648362cd11cSLisandro Dalcin } else if (!ts->reason) { 2649db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 2650db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 2651362cd11cSLisandro Dalcin } 2652d763cef2SBarry Smith PetscFunctionReturn(0); 2653d763cef2SBarry Smith } 2654d763cef2SBarry Smith 26554a2ae208SSatish Balay #undef __FUNCT__ 265605175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 265705175c85SJed Brown /*@ 265805175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 265905175c85SJed Brown 26601c3436cfSJed Brown Collective on TS 266105175c85SJed Brown 266205175c85SJed Brown Input Arguments: 26631c3436cfSJed Brown + ts - time stepping context 26641c3436cfSJed Brown . order - desired order of accuracy 26650298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 266605175c85SJed Brown 266705175c85SJed Brown Output Arguments: 26680910c330SBarry Smith . U - state at the end of the current step 266905175c85SJed Brown 267005175c85SJed Brown Level: advanced 267105175c85SJed Brown 2672108c343cSJed Brown Notes: 2673108c343cSJed Brown This function cannot be called until all stages have been evaluated. 2674108c343cSJed 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. 2675108c343cSJed Brown 26761c3436cfSJed Brown .seealso: TSStep(), TSAdapt 267705175c85SJed Brown @*/ 26780910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 267905175c85SJed Brown { 268005175c85SJed Brown PetscErrorCode ierr; 268105175c85SJed Brown 268205175c85SJed Brown PetscFunctionBegin; 268305175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 268405175c85SJed Brown PetscValidType(ts,1); 26850910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 2686ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 26870910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 268805175c85SJed Brown PetscFunctionReturn(0); 268905175c85SJed Brown } 269005175c85SJed Brown 269105175c85SJed Brown #undef __FUNCT__ 26926a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 26936a4d4014SLisandro Dalcin /*@ 26946a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 26956a4d4014SLisandro Dalcin 26966a4d4014SLisandro Dalcin Collective on TS 26976a4d4014SLisandro Dalcin 26986a4d4014SLisandro Dalcin Input Parameter: 26996a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 2700cc708dedSBarry Smith - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 27015a3a76d0SJed Brown 27026a4d4014SLisandro Dalcin Level: beginner 27036a4d4014SLisandro Dalcin 27045a3a76d0SJed Brown Notes: 27055a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 27065a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 27075a3a76d0SJed Brown stepped over the final time. 27085a3a76d0SJed Brown 27096a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 27106a4d4014SLisandro Dalcin 27116a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 27126a4d4014SLisandro Dalcin @*/ 2713cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 27146a4d4014SLisandro Dalcin { 2715b06615a5SLisandro Dalcin Vec solution; 27166a4d4014SLisandro Dalcin PetscErrorCode ierr; 2717f22f69f0SBarry Smith 27186a4d4014SLisandro Dalcin PetscFunctionBegin; 27190700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2720f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 272149354f04SShri 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 */ 2722b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 27230910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 2724b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 2725b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 2726b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 27275a3a76d0SJed Brown } 27280910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 2729bbd56ea5SKarl Rupp } else if (u) { 27300910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 27315a3a76d0SJed Brown } 2732b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 27336a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 2734193ac0bcSJed Brown ts->steps = 0; 27355ef26d82SJed Brown ts->ksp_its = 0; 27365ef26d82SJed Brown ts->snes_its = 0; 2737c610991cSLisandro Dalcin ts->num_snes_failures = 0; 2738c610991cSLisandro Dalcin ts->reject = 0; 2739193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 2740193ac0bcSJed Brown 2741ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 2742f05ece33SBarry Smith 2743193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 2744193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 27450910c330SBarry Smith ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 2746cc708dedSBarry Smith ts->solvetime = ts->ptime; 2747193ac0bcSJed Brown } else { 27486a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 2749db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 2750db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 2751e1a7a14fSJed Brown while (!ts->reason) { 2752b06615a5SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2753193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 2754193ac0bcSJed Brown ierr = TSPostStep(ts);CHKERRQ(ierr); 2755193ac0bcSJed Brown } 275649354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 27570910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 2758cc708dedSBarry Smith ts->solvetime = ts->max_time; 2759b06615a5SLisandro Dalcin solution = u; 27600574a7fbSJed Brown } else { 2761ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 2762cc708dedSBarry Smith ts->solvetime = ts->ptime; 2763b06615a5SLisandro Dalcin solution = ts->vec_sol; 27640574a7fbSJed Brown } 2765b06615a5SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr); 2766193ac0bcSJed Brown } 2767ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 276856f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 27696a4d4014SLisandro Dalcin PetscFunctionReturn(0); 27706a4d4014SLisandro Dalcin } 27716a4d4014SLisandro Dalcin 27726a4d4014SLisandro Dalcin #undef __FUNCT__ 27734a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 2774228d79bcSJed Brown /*@ 2775228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 2776228d79bcSJed Brown 2777228d79bcSJed Brown Collective on TS 2778228d79bcSJed Brown 2779228d79bcSJed Brown Input Parameters: 2780228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 2781228d79bcSJed Brown . step - step number that has just completed 2782228d79bcSJed Brown . ptime - model time of the state 27830910c330SBarry Smith - u - state at the current model time 2784228d79bcSJed Brown 2785228d79bcSJed Brown Notes: 2786228d79bcSJed Brown TSMonitor() is typically used within the time stepping implementations. 2787228d79bcSJed Brown Users might call this function when using the TSStep() interface instead of TSSolve(). 2788228d79bcSJed Brown 2789228d79bcSJed Brown Level: advanced 2790228d79bcSJed Brown 2791228d79bcSJed Brown .keywords: TS, timestep 2792228d79bcSJed Brown @*/ 27930910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 2794d763cef2SBarry Smith { 27956849ba73SBarry Smith PetscErrorCode ierr; 2796a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 2797d763cef2SBarry Smith 2798d763cef2SBarry Smith PetscFunctionBegin; 2799b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2800b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 2801d763cef2SBarry Smith for (i=0; i<n; i++) { 28020910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 2803d763cef2SBarry Smith } 2804d763cef2SBarry Smith PetscFunctionReturn(0); 2805d763cef2SBarry Smith } 2806d763cef2SBarry Smith 2807d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 28084a2ae208SSatish Balay #undef __FUNCT__ 2809a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 2810d763cef2SBarry Smith /*@C 2811a9f9c1f6SBarry Smith TSMonitorLGCtxCreate - Creates a line graph context for use with 2812a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 2813d763cef2SBarry Smith 2814d763cef2SBarry Smith Collective on TS 2815d763cef2SBarry Smith 2816d763cef2SBarry Smith Input Parameters: 2817d763cef2SBarry Smith + host - the X display to open, or null for the local machine 2818d763cef2SBarry Smith . label - the title to put in the title bar 28197c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 2820a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 2821a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 2822d763cef2SBarry Smith 2823d763cef2SBarry Smith Output Parameter: 28240b039ecaSBarry Smith . ctx - the context 2825d763cef2SBarry Smith 2826d763cef2SBarry Smith Options Database Key: 28274f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 28284f09c107SBarry Smith . -ts_monitor_lg_solution - 282999fdda47SBarry Smith . -ts_monitor_lg_error - 283099fdda47SBarry Smith . -ts_monitor_lg_ksp_iterations - 283199fdda47SBarry Smith . -ts_monitor_lg_snes_iterations - 283299fdda47SBarry Smith - -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true 2833d763cef2SBarry Smith 2834d763cef2SBarry Smith Notes: 2835a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 2836d763cef2SBarry Smith 2837d763cef2SBarry Smith Level: intermediate 2838d763cef2SBarry Smith 28397c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 2840d763cef2SBarry Smith 28414f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 28427c922b88SBarry Smith 2843d763cef2SBarry Smith @*/ 2844a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 2845d763cef2SBarry Smith { 2846b0a32e0cSBarry Smith PetscDraw win; 2847dfbe8321SBarry Smith PetscErrorCode ierr; 2848d763cef2SBarry Smith 2849d763cef2SBarry Smith PetscFunctionBegin; 2850b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 2851a80ad3e0SBarry Smith ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr); 28523fbbecb0SBarry Smith ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr); 28530b039ecaSBarry Smith ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr); 28543bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr); 2855287de1a7SBarry Smith ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr); 2856287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 2857a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 2858d763cef2SBarry Smith PetscFunctionReturn(0); 2859d763cef2SBarry Smith } 2860d763cef2SBarry Smith 28614a2ae208SSatish Balay #undef __FUNCT__ 28624f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 2863b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 2864d763cef2SBarry Smith { 28650b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 2866c32365f1SBarry Smith PetscReal x = ptime,y; 2867dfbe8321SBarry Smith PetscErrorCode ierr; 2868d763cef2SBarry Smith 2869d763cef2SBarry Smith PetscFunctionBegin; 2870b06615a5SLisandro Dalcin if (!step) { 2871a9f9c1f6SBarry Smith PetscDrawAxis axis; 2872a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 2873a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 2874a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 2875287de1a7SBarry Smith ierr = PetscDrawLGIndicateDataPoints(ctx->lg,PETSC_TRUE);CHKERRQ(ierr); 2876a9f9c1f6SBarry Smith } 2877c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 28780b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 2879b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 28800b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 28813923b477SBarry Smith } 2882d763cef2SBarry Smith PetscFunctionReturn(0); 2883d763cef2SBarry Smith } 2884d763cef2SBarry Smith 28854a2ae208SSatish Balay #undef __FUNCT__ 2886a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 2887d763cef2SBarry Smith /*@C 2888a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 2889a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 2890d763cef2SBarry Smith 28910b039ecaSBarry Smith Collective on TSMonitorLGCtx 2892d763cef2SBarry Smith 2893d763cef2SBarry Smith Input Parameter: 28940b039ecaSBarry Smith . ctx - the monitor context 2895d763cef2SBarry Smith 2896d763cef2SBarry Smith Level: intermediate 2897d763cef2SBarry Smith 2898d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 2899d763cef2SBarry Smith 29004f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 2901d763cef2SBarry Smith @*/ 2902a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 2903d763cef2SBarry Smith { 2904b0a32e0cSBarry Smith PetscDraw draw; 2905dfbe8321SBarry Smith PetscErrorCode ierr; 2906d763cef2SBarry Smith 2907d763cef2SBarry Smith PetscFunctionBegin; 29080b039ecaSBarry Smith ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr); 29096bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 29100b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 29110b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 2912d763cef2SBarry Smith PetscFunctionReturn(0); 2913d763cef2SBarry Smith } 2914d763cef2SBarry Smith 29154a2ae208SSatish Balay #undef __FUNCT__ 29164a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 2917d763cef2SBarry Smith /*@ 2918b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 2919d763cef2SBarry Smith 2920d763cef2SBarry Smith Not Collective 2921d763cef2SBarry Smith 2922d763cef2SBarry Smith Input Parameter: 2923d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2924d763cef2SBarry Smith 2925d763cef2SBarry Smith Output Parameter: 2926d763cef2SBarry Smith . t - the current time 2927d763cef2SBarry Smith 2928d763cef2SBarry Smith Level: beginner 2929d763cef2SBarry Smith 2930b8123daeSJed Brown Note: 2931b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 29329be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 2933b8123daeSJed Brown 2934d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2935d763cef2SBarry Smith 2936d763cef2SBarry Smith .keywords: TS, get, time 2937d763cef2SBarry Smith @*/ 29387087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 2939d763cef2SBarry Smith { 2940d763cef2SBarry Smith PetscFunctionBegin; 29410700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2942f7cf8827SBarry Smith PetscValidRealPointer(t,2); 2943d763cef2SBarry Smith *t = ts->ptime; 2944d763cef2SBarry Smith PetscFunctionReturn(0); 2945d763cef2SBarry Smith } 2946d763cef2SBarry Smith 29474a2ae208SSatish Balay #undef __FUNCT__ 29486a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 29496a4d4014SLisandro Dalcin /*@ 29506a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 29516a4d4014SLisandro Dalcin 29523f9fe445SBarry Smith Logically Collective on TS 29536a4d4014SLisandro Dalcin 29546a4d4014SLisandro Dalcin Input Parameters: 29556a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 29566a4d4014SLisandro Dalcin - time - the time 29576a4d4014SLisandro Dalcin 29586a4d4014SLisandro Dalcin Level: intermediate 29596a4d4014SLisandro Dalcin 29606a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 29616a4d4014SLisandro Dalcin 29626a4d4014SLisandro Dalcin .keywords: TS, set, time 29636a4d4014SLisandro Dalcin @*/ 29647087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 29656a4d4014SLisandro Dalcin { 29666a4d4014SLisandro Dalcin PetscFunctionBegin; 29670700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2968c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 29696a4d4014SLisandro Dalcin ts->ptime = t; 29706a4d4014SLisandro Dalcin PetscFunctionReturn(0); 29716a4d4014SLisandro Dalcin } 29726a4d4014SLisandro Dalcin 29736a4d4014SLisandro Dalcin #undef __FUNCT__ 29744a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 2975d763cef2SBarry Smith /*@C 2976d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 2977d763cef2SBarry Smith TS options in the database. 2978d763cef2SBarry Smith 29793f9fe445SBarry Smith Logically Collective on TS 2980d763cef2SBarry Smith 2981d763cef2SBarry Smith Input Parameter: 2982d763cef2SBarry Smith + ts - The TS context 2983d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2984d763cef2SBarry Smith 2985d763cef2SBarry Smith Notes: 2986d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2987d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2988d763cef2SBarry Smith hyphen. 2989d763cef2SBarry Smith 2990d763cef2SBarry Smith Level: advanced 2991d763cef2SBarry Smith 2992d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 2993d763cef2SBarry Smith 2994d763cef2SBarry Smith .seealso: TSSetFromOptions() 2995d763cef2SBarry Smith 2996d763cef2SBarry Smith @*/ 29977087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 2998d763cef2SBarry Smith { 2999dfbe8321SBarry Smith PetscErrorCode ierr; 3000089b2837SJed Brown SNES snes; 3001d763cef2SBarry Smith 3002d763cef2SBarry Smith PetscFunctionBegin; 30030700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3004d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3005089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3006089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3007d763cef2SBarry Smith PetscFunctionReturn(0); 3008d763cef2SBarry Smith } 3009d763cef2SBarry Smith 3010d763cef2SBarry Smith 30114a2ae208SSatish Balay #undef __FUNCT__ 30124a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 3013d763cef2SBarry Smith /*@C 3014d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 3015d763cef2SBarry Smith TS options in the database. 3016d763cef2SBarry Smith 30173f9fe445SBarry Smith Logically Collective on TS 3018d763cef2SBarry Smith 3019d763cef2SBarry Smith Input Parameter: 3020d763cef2SBarry Smith + ts - The TS context 3021d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3022d763cef2SBarry Smith 3023d763cef2SBarry Smith Notes: 3024d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3025d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3026d763cef2SBarry Smith hyphen. 3027d763cef2SBarry Smith 3028d763cef2SBarry Smith Level: advanced 3029d763cef2SBarry Smith 3030d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 3031d763cef2SBarry Smith 3032d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 3033d763cef2SBarry Smith 3034d763cef2SBarry Smith @*/ 30357087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 3036d763cef2SBarry Smith { 3037dfbe8321SBarry Smith PetscErrorCode ierr; 3038089b2837SJed Brown SNES snes; 3039d763cef2SBarry Smith 3040d763cef2SBarry Smith PetscFunctionBegin; 30410700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3042d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3043089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3044089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3045d763cef2SBarry Smith PetscFunctionReturn(0); 3046d763cef2SBarry Smith } 3047d763cef2SBarry Smith 30484a2ae208SSatish Balay #undef __FUNCT__ 30494a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 3050d763cef2SBarry Smith /*@C 3051d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 3052d763cef2SBarry Smith TS options in the database. 3053d763cef2SBarry Smith 3054d763cef2SBarry Smith Not Collective 3055d763cef2SBarry Smith 3056d763cef2SBarry Smith Input Parameter: 3057d763cef2SBarry Smith . ts - The TS context 3058d763cef2SBarry Smith 3059d763cef2SBarry Smith Output Parameter: 3060d763cef2SBarry Smith . prefix - A pointer to the prefix string used 3061d763cef2SBarry Smith 3062d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 3063d763cef2SBarry Smith sufficient length to hold the prefix. 3064d763cef2SBarry Smith 3065d763cef2SBarry Smith Level: intermediate 3066d763cef2SBarry Smith 3067d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 3068d763cef2SBarry Smith 3069d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 3070d763cef2SBarry Smith @*/ 30717087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 3072d763cef2SBarry Smith { 3073dfbe8321SBarry Smith PetscErrorCode ierr; 3074d763cef2SBarry Smith 3075d763cef2SBarry Smith PetscFunctionBegin; 30760700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30774482741eSBarry Smith PetscValidPointer(prefix,2); 3078d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3079d763cef2SBarry Smith PetscFunctionReturn(0); 3080d763cef2SBarry Smith } 3081d763cef2SBarry Smith 30824a2ae208SSatish Balay #undef __FUNCT__ 30834a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 3084d763cef2SBarry Smith /*@C 3085d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 3086d763cef2SBarry Smith 3087d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 3088d763cef2SBarry Smith 3089d763cef2SBarry Smith Input Parameter: 3090d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 3091d763cef2SBarry Smith 3092d763cef2SBarry Smith Output Parameters: 3093e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 3094e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 3095e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 3096e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 3097d763cef2SBarry Smith 30980298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 3099d763cef2SBarry Smith 3100d763cef2SBarry Smith Level: intermediate 3101d763cef2SBarry Smith 310226d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 3103d763cef2SBarry Smith 3104d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 3105d763cef2SBarry Smith @*/ 3106e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 3107d763cef2SBarry Smith { 3108089b2837SJed Brown PetscErrorCode ierr; 3109089b2837SJed Brown SNES snes; 311024989b8cSPeter Brune DM dm; 3111089b2837SJed Brown 3112d763cef2SBarry Smith PetscFunctionBegin; 3113089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3114e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 311524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 311624989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 3117d763cef2SBarry Smith PetscFunctionReturn(0); 3118d763cef2SBarry Smith } 3119d763cef2SBarry Smith 31201713a123SBarry Smith #undef __FUNCT__ 31212eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 31222eca1d9cSJed Brown /*@C 31232eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 31242eca1d9cSJed Brown 31252eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 31262eca1d9cSJed Brown 31272eca1d9cSJed Brown Input Parameter: 31282eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 31292eca1d9cSJed Brown 31302eca1d9cSJed Brown Output Parameters: 3131e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 3132e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 31332eca1d9cSJed Brown . f - The function to compute the matrices 31342eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 31352eca1d9cSJed Brown 31360298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 31372eca1d9cSJed Brown 31382eca1d9cSJed Brown Level: advanced 31392eca1d9cSJed Brown 31402eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 31412eca1d9cSJed Brown 31422eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 31432eca1d9cSJed Brown @*/ 3144e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 31452eca1d9cSJed Brown { 3146089b2837SJed Brown PetscErrorCode ierr; 3147089b2837SJed Brown SNES snes; 314824989b8cSPeter Brune DM dm; 31490910c330SBarry Smith 31502eca1d9cSJed Brown PetscFunctionBegin; 3151089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3152f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 3153e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 315424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 315524989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 31562eca1d9cSJed Brown PetscFunctionReturn(0); 31572eca1d9cSJed Brown } 31582eca1d9cSJed Brown 31596083293cSBarry Smith 31602eca1d9cSJed Brown #undef __FUNCT__ 316183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 31621713a123SBarry Smith /*@C 316383a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 31641713a123SBarry Smith VecView() for the solution at each timestep 31651713a123SBarry Smith 31661713a123SBarry Smith Collective on TS 31671713a123SBarry Smith 31681713a123SBarry Smith Input Parameters: 31691713a123SBarry Smith + ts - the TS context 31701713a123SBarry Smith . step - current time-step 3171142b95e3SSatish Balay . ptime - current time 31720298fd71SBarry Smith - dummy - either a viewer or NULL 31731713a123SBarry Smith 317499fdda47SBarry Smith Options Database: 317599fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 317699fdda47SBarry Smith 317799fdda47SBarry 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 317899fdda47SBarry Smith will look bad 317999fdda47SBarry Smith 31801713a123SBarry Smith Level: intermediate 31811713a123SBarry Smith 31821713a123SBarry Smith .keywords: TS, vector, monitor, view 31831713a123SBarry Smith 3184a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 31851713a123SBarry Smith @*/ 31860910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 31871713a123SBarry Smith { 3188dfbe8321SBarry Smith PetscErrorCode ierr; 318983a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 3190473a3ab2SBarry Smith PetscDraw draw; 31911713a123SBarry Smith 31921713a123SBarry Smith PetscFunctionBegin; 31936083293cSBarry Smith if (!step && ictx->showinitial) { 31946083293cSBarry Smith if (!ictx->initialsolution) { 31950910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 31961713a123SBarry Smith } 31970910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 31986083293cSBarry Smith } 3199b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 32000dcf80beSBarry Smith 32016083293cSBarry Smith if (ictx->showinitial) { 32026083293cSBarry Smith PetscReal pause; 32036083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 32046083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 32056083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 32066083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 32076083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 32086083293cSBarry Smith } 32090910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 3210473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 3211473a3ab2SBarry Smith PetscReal xl,yl,xr,yr,tw,w,h; 3212473a3ab2SBarry Smith char time[32]; 3213473a3ab2SBarry Smith size_t len; 3214473a3ab2SBarry Smith 3215473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 3216473a3ab2SBarry Smith ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr); 3217473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 3218473a3ab2SBarry Smith ierr = PetscStrlen(time,&len);CHKERRQ(ierr); 32190298fd71SBarry Smith ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr); 3220473a3ab2SBarry Smith w = xl + .5*(xr - xl) - .5*len*tw; 3221473a3ab2SBarry Smith h = yl + .95*(yr - yl); 3222473a3ab2SBarry Smith ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 3223473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 3224473a3ab2SBarry Smith } 3225473a3ab2SBarry Smith 32266083293cSBarry Smith if (ictx->showinitial) { 32276083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 32286083293cSBarry Smith } 32291713a123SBarry Smith PetscFunctionReturn(0); 32301713a123SBarry Smith } 32311713a123SBarry Smith 32322d5ee99bSBarry Smith #undef __FUNCT__ 32332d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase" 32342d5ee99bSBarry Smith /*@C 32352d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 32362d5ee99bSBarry Smith 32372d5ee99bSBarry Smith Collective on TS 32382d5ee99bSBarry Smith 32392d5ee99bSBarry Smith Input Parameters: 32402d5ee99bSBarry Smith + ts - the TS context 32412d5ee99bSBarry Smith . step - current time-step 32422d5ee99bSBarry Smith . ptime - current time 32432d5ee99bSBarry Smith - dummy - either a viewer or NULL 32442d5ee99bSBarry Smith 32452d5ee99bSBarry Smith Level: intermediate 32462d5ee99bSBarry Smith 32472d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 32482d5ee99bSBarry Smith 32492d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 32502d5ee99bSBarry Smith @*/ 32512d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 32522d5ee99bSBarry Smith { 32532d5ee99bSBarry Smith PetscErrorCode ierr; 32542d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 32552d5ee99bSBarry Smith PetscDraw draw; 32562d5ee99bSBarry Smith MPI_Comm comm; 32572d5ee99bSBarry Smith PetscInt n; 32582d5ee99bSBarry Smith PetscMPIInt size; 32592d5ee99bSBarry Smith PetscReal xl,yl,xr,yr,tw,w,h; 32602d5ee99bSBarry Smith char time[32]; 32612d5ee99bSBarry Smith size_t len; 32622d5ee99bSBarry Smith const PetscScalar *U; 32632d5ee99bSBarry Smith 32642d5ee99bSBarry Smith PetscFunctionBegin; 32652d5ee99bSBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 32662d5ee99bSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 32672d5ee99bSBarry Smith if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs"); 32682d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 32692d5ee99bSBarry Smith if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 32702d5ee99bSBarry Smith 32712d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 32722d5ee99bSBarry Smith 32732d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 32744b363babSBarry Smith ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 3275ba5783adSMatthew G Knepley if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) { 3276a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 3277a4494fc1SBarry Smith PetscFunctionReturn(0); 3278a4494fc1SBarry Smith } 32792d5ee99bSBarry Smith if (!step) ictx->color++; 32802d5ee99bSBarry Smith ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr); 32812d5ee99bSBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 32822d5ee99bSBarry Smith 32832d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 32844b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 32852d5ee99bSBarry Smith ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr); 32862d5ee99bSBarry Smith ierr = PetscStrlen(time,&len);CHKERRQ(ierr); 32872d5ee99bSBarry Smith ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr); 32882d5ee99bSBarry Smith w = xl + .5*(xr - xl) - .5*len*tw; 32892d5ee99bSBarry Smith h = yl + .95*(yr - yl); 32902d5ee99bSBarry Smith ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 32912d5ee99bSBarry Smith } 32922d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 32932d5ee99bSBarry Smith PetscFunctionReturn(0); 32942d5ee99bSBarry Smith } 32952d5ee99bSBarry Smith 32961713a123SBarry Smith 32976c699258SBarry Smith #undef __FUNCT__ 329883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 32996083293cSBarry Smith /*@C 330083a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 33016083293cSBarry Smith 33026083293cSBarry Smith Collective on TS 33036083293cSBarry Smith 33046083293cSBarry Smith Input Parameters: 33056083293cSBarry Smith . ctx - the monitor context 33066083293cSBarry Smith 33076083293cSBarry Smith Level: intermediate 33086083293cSBarry Smith 33096083293cSBarry Smith .keywords: TS, vector, monitor, view 33106083293cSBarry Smith 331183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 33126083293cSBarry Smith @*/ 331383a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 33146083293cSBarry Smith { 33156083293cSBarry Smith PetscErrorCode ierr; 33166083293cSBarry Smith 33176083293cSBarry Smith PetscFunctionBegin; 33184b363babSBarry Smith ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr); 331983a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 332083a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 332183a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 33226083293cSBarry Smith PetscFunctionReturn(0); 33236083293cSBarry Smith } 33246083293cSBarry Smith 33256083293cSBarry Smith #undef __FUNCT__ 332683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 33276083293cSBarry Smith /*@C 332883a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 33296083293cSBarry Smith 33306083293cSBarry Smith Collective on TS 33316083293cSBarry Smith 33326083293cSBarry Smith Input Parameter: 33336083293cSBarry Smith . ts - time-step context 33346083293cSBarry Smith 33356083293cSBarry Smith Output Patameter: 33366083293cSBarry Smith . ctx - the monitor context 33376083293cSBarry Smith 333899fdda47SBarry Smith Options Database: 333999fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 334099fdda47SBarry Smith 33416083293cSBarry Smith Level: intermediate 33426083293cSBarry Smith 33436083293cSBarry Smith .keywords: TS, vector, monitor, view 33446083293cSBarry Smith 334583a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 33466083293cSBarry Smith @*/ 334783a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 33486083293cSBarry Smith { 33496083293cSBarry Smith PetscErrorCode ierr; 33506083293cSBarry Smith 33516083293cSBarry Smith PetscFunctionBegin; 3352b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 335383a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 3354e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 3355bbd56ea5SKarl Rupp 335683a4ac43SBarry Smith (*ctx)->howoften = howoften; 3357473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 33580298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 3359473a3ab2SBarry Smith 3360473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 33610298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 33622d5ee99bSBarry Smith (*ctx)->color = PETSC_DRAW_WHITE; 33636083293cSBarry Smith PetscFunctionReturn(0); 33646083293cSBarry Smith } 33656083293cSBarry Smith 33666083293cSBarry Smith #undef __FUNCT__ 336783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 33683a471f94SBarry Smith /*@C 336983a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 33703a471f94SBarry Smith VecView() for the error at each timestep 33713a471f94SBarry Smith 33723a471f94SBarry Smith Collective on TS 33733a471f94SBarry Smith 33743a471f94SBarry Smith Input Parameters: 33753a471f94SBarry Smith + ts - the TS context 33763a471f94SBarry Smith . step - current time-step 33773a471f94SBarry Smith . ptime - current time 33780298fd71SBarry Smith - dummy - either a viewer or NULL 33793a471f94SBarry Smith 33803a471f94SBarry Smith Level: intermediate 33813a471f94SBarry Smith 33823a471f94SBarry Smith .keywords: TS, vector, monitor, view 33833a471f94SBarry Smith 33843a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 33853a471f94SBarry Smith @*/ 33860910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 33873a471f94SBarry Smith { 33883a471f94SBarry Smith PetscErrorCode ierr; 338983a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 339083a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 33913a471f94SBarry Smith Vec work; 33923a471f94SBarry Smith 33933a471f94SBarry Smith PetscFunctionBegin; 3394b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 33950910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 33963a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 33970910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 33983a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 33993a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 34003a471f94SBarry Smith PetscFunctionReturn(0); 34013a471f94SBarry Smith } 34023a471f94SBarry Smith 34032a34c10cSBarry Smith #include <petsc-private/dmimpl.h> 34043a471f94SBarry Smith #undef __FUNCT__ 34056c699258SBarry Smith #define __FUNCT__ "TSSetDM" 34066c699258SBarry Smith /*@ 34076c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 34086c699258SBarry Smith 34093f9fe445SBarry Smith Logically Collective on TS and DM 34106c699258SBarry Smith 34116c699258SBarry Smith Input Parameters: 34126c699258SBarry Smith + ts - the preconditioner context 34136c699258SBarry Smith - dm - the dm 34146c699258SBarry Smith 34156c699258SBarry Smith Level: intermediate 34166c699258SBarry Smith 34176c699258SBarry Smith 34186c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 34196c699258SBarry Smith @*/ 34207087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 34216c699258SBarry Smith { 34226c699258SBarry Smith PetscErrorCode ierr; 3423089b2837SJed Brown SNES snes; 3424942e3340SBarry Smith DMTS tsdm; 34256c699258SBarry Smith 34266c699258SBarry Smith PetscFunctionBegin; 34270700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 342870663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 3429942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 34302a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 3431942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 3432942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 343324989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 343424989b8cSPeter Brune tsdm->originaldm = dm; 343524989b8cSPeter Brune } 343624989b8cSPeter Brune } 34376bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 343824989b8cSPeter Brune } 34396c699258SBarry Smith ts->dm = dm; 3440bbd56ea5SKarl Rupp 3441089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3442089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 34436c699258SBarry Smith PetscFunctionReturn(0); 34446c699258SBarry Smith } 34456c699258SBarry Smith 34466c699258SBarry Smith #undef __FUNCT__ 34476c699258SBarry Smith #define __FUNCT__ "TSGetDM" 34486c699258SBarry Smith /*@ 34496c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 34506c699258SBarry Smith 34513f9fe445SBarry Smith Not Collective 34526c699258SBarry Smith 34536c699258SBarry Smith Input Parameter: 34546c699258SBarry Smith . ts - the preconditioner context 34556c699258SBarry Smith 34566c699258SBarry Smith Output Parameter: 34576c699258SBarry Smith . dm - the dm 34586c699258SBarry Smith 34596c699258SBarry Smith Level: intermediate 34606c699258SBarry Smith 34616c699258SBarry Smith 34626c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 34636c699258SBarry Smith @*/ 34647087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 34656c699258SBarry Smith { 3466496e6a7aSJed Brown PetscErrorCode ierr; 3467496e6a7aSJed Brown 34686c699258SBarry Smith PetscFunctionBegin; 34690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3470496e6a7aSJed Brown if (!ts->dm) { 3471ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 3472496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 3473496e6a7aSJed Brown } 34746c699258SBarry Smith *dm = ts->dm; 34756c699258SBarry Smith PetscFunctionReturn(0); 34766c699258SBarry Smith } 34771713a123SBarry Smith 34780f5c6efeSJed Brown #undef __FUNCT__ 34790f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 34800f5c6efeSJed Brown /*@ 34810f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 34820f5c6efeSJed Brown 34833f9fe445SBarry Smith Logically Collective on SNES 34840f5c6efeSJed Brown 34850f5c6efeSJed Brown Input Parameter: 3486d42a1c89SJed Brown + snes - nonlinear solver 34870910c330SBarry Smith . U - the current state at which to evaluate the residual 3488d42a1c89SJed Brown - ctx - user context, must be a TS 34890f5c6efeSJed Brown 34900f5c6efeSJed Brown Output Parameter: 34910f5c6efeSJed Brown . F - the nonlinear residual 34920f5c6efeSJed Brown 34930f5c6efeSJed Brown Notes: 34940f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 34950f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 34960f5c6efeSJed Brown 34970f5c6efeSJed Brown Level: advanced 34980f5c6efeSJed Brown 34990f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 35000f5c6efeSJed Brown @*/ 35010910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 35020f5c6efeSJed Brown { 35030f5c6efeSJed Brown TS ts = (TS)ctx; 35040f5c6efeSJed Brown PetscErrorCode ierr; 35050f5c6efeSJed Brown 35060f5c6efeSJed Brown PetscFunctionBegin; 35070f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 35080910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 35090f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 35100f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 35110910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 35120f5c6efeSJed Brown PetscFunctionReturn(0); 35130f5c6efeSJed Brown } 35140f5c6efeSJed Brown 35150f5c6efeSJed Brown #undef __FUNCT__ 35160f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 35170f5c6efeSJed Brown /*@ 35180f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 35190f5c6efeSJed Brown 35200f5c6efeSJed Brown Collective on SNES 35210f5c6efeSJed Brown 35220f5c6efeSJed Brown Input Parameter: 35230f5c6efeSJed Brown + snes - nonlinear solver 35240910c330SBarry Smith . U - the current state at which to evaluate the residual 35250f5c6efeSJed Brown - ctx - user context, must be a TS 35260f5c6efeSJed Brown 35270f5c6efeSJed Brown Output Parameter: 35280f5c6efeSJed Brown + A - the Jacobian 35290f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 35300f5c6efeSJed Brown - flag - indicates any structure change in the matrix 35310f5c6efeSJed Brown 35320f5c6efeSJed Brown Notes: 35330f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 35340f5c6efeSJed Brown 35350f5c6efeSJed Brown Level: developer 35360f5c6efeSJed Brown 35370f5c6efeSJed Brown .seealso: SNESSetJacobian() 35380f5c6efeSJed Brown @*/ 3539*94ab13aaSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,MatStructure *flag,void *ctx) 35400f5c6efeSJed Brown { 35410f5c6efeSJed Brown TS ts = (TS)ctx; 35420f5c6efeSJed Brown PetscErrorCode ierr; 35430f5c6efeSJed Brown 35440f5c6efeSJed Brown PetscFunctionBegin; 35450f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 35460910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 35470f5c6efeSJed Brown PetscValidPointer(A,3); 3548*94ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 35490f5c6efeSJed Brown PetscValidPointer(B,4); 3550*94ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 35510f5c6efeSJed Brown PetscValidPointer(flag,5); 35520f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 35530910c330SBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr); 35540f5c6efeSJed Brown PetscFunctionReturn(0); 35550f5c6efeSJed Brown } 3556325fc9f4SBarry Smith 35570e4ef248SJed Brown #undef __FUNCT__ 35580e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 35590e4ef248SJed Brown /*@C 35600e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 35610e4ef248SJed Brown 35620e4ef248SJed Brown Collective on TS 35630e4ef248SJed Brown 35640e4ef248SJed Brown Input Arguments: 35650e4ef248SJed Brown + ts - time stepping context 35660e4ef248SJed Brown . t - time at which to evaluate 35670910c330SBarry Smith . U - state at which to evaluate 35680e4ef248SJed Brown - ctx - context 35690e4ef248SJed Brown 35700e4ef248SJed Brown Output Arguments: 35710e4ef248SJed Brown . F - right hand side 35720e4ef248SJed Brown 35730e4ef248SJed Brown Level: intermediate 35740e4ef248SJed Brown 35750e4ef248SJed Brown Notes: 35760e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 35770e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 35780e4ef248SJed Brown 35790e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 35800e4ef248SJed Brown @*/ 35810910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 35820e4ef248SJed Brown { 35830e4ef248SJed Brown PetscErrorCode ierr; 35840e4ef248SJed Brown Mat Arhs,Brhs; 35850e4ef248SJed Brown MatStructure flg2; 35860e4ef248SJed Brown 35870e4ef248SJed Brown PetscFunctionBegin; 35880e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 3589*94ab13aaSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs,&flg2);CHKERRQ(ierr); 35900910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 35910e4ef248SJed Brown PetscFunctionReturn(0); 35920e4ef248SJed Brown } 35930e4ef248SJed Brown 35940e4ef248SJed Brown #undef __FUNCT__ 35950e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 35960e4ef248SJed Brown /*@C 35970e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 35980e4ef248SJed Brown 35990e4ef248SJed Brown Collective on TS 36000e4ef248SJed Brown 36010e4ef248SJed Brown Input Arguments: 36020e4ef248SJed Brown + ts - time stepping context 36030e4ef248SJed Brown . t - time at which to evaluate 36040910c330SBarry Smith . U - state at which to evaluate 36050e4ef248SJed Brown - ctx - context 36060e4ef248SJed Brown 36070e4ef248SJed Brown Output Arguments: 36080e4ef248SJed Brown + A - pointer to operator 36090e4ef248SJed Brown . B - pointer to preconditioning matrix 36100e4ef248SJed Brown - flg - matrix structure flag 36110e4ef248SJed Brown 36120e4ef248SJed Brown Level: intermediate 36130e4ef248SJed Brown 36140e4ef248SJed Brown Notes: 36150e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 36160e4ef248SJed Brown 36170e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 36180e4ef248SJed Brown @*/ 3619*94ab13aaSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,MatStructure *flg,void *ctx) 36200e4ef248SJed Brown { 36210e4ef248SJed Brown PetscFunctionBegin; 36220e4ef248SJed Brown *flg = SAME_PRECONDITIONER; 36230e4ef248SJed Brown PetscFunctionReturn(0); 36240e4ef248SJed Brown } 36250e4ef248SJed Brown 36260026cea9SSean Farley #undef __FUNCT__ 36270026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 36280026cea9SSean Farley /*@C 36290026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 36300026cea9SSean Farley 36310026cea9SSean Farley Collective on TS 36320026cea9SSean Farley 36330026cea9SSean Farley Input Arguments: 36340026cea9SSean Farley + ts - time stepping context 36350026cea9SSean Farley . t - time at which to evaluate 36360910c330SBarry Smith . U - state at which to evaluate 36370910c330SBarry Smith . Udot - time derivative of state vector 36380026cea9SSean Farley - ctx - context 36390026cea9SSean Farley 36400026cea9SSean Farley Output Arguments: 36410026cea9SSean Farley . F - left hand side 36420026cea9SSean Farley 36430026cea9SSean Farley Level: intermediate 36440026cea9SSean Farley 36450026cea9SSean Farley Notes: 36460910c330SBarry 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 36470026cea9SSean Farley user is required to write their own TSComputeIFunction. 36480026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 36490026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 36500026cea9SSean Farley 36510026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 36520026cea9SSean Farley @*/ 36530910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 36540026cea9SSean Farley { 36550026cea9SSean Farley PetscErrorCode ierr; 36560026cea9SSean Farley Mat A,B; 36570026cea9SSean Farley MatStructure flg2; 36580026cea9SSean Farley 36590026cea9SSean Farley PetscFunctionBegin; 36600298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 3661*94ab13aaSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,&flg2,PETSC_TRUE);CHKERRQ(ierr); 36620910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 36630026cea9SSean Farley PetscFunctionReturn(0); 36640026cea9SSean Farley } 36650026cea9SSean Farley 36660026cea9SSean Farley #undef __FUNCT__ 36670026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 36680026cea9SSean Farley /*@C 3669b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 36700026cea9SSean Farley 36710026cea9SSean Farley Collective on TS 36720026cea9SSean Farley 36730026cea9SSean Farley Input Arguments: 36740026cea9SSean Farley + ts - time stepping context 36750026cea9SSean Farley . t - time at which to evaluate 36760910c330SBarry Smith . U - state at which to evaluate 36770910c330SBarry Smith . Udot - time derivative of state vector 36780026cea9SSean Farley . shift - shift to apply 36790026cea9SSean Farley - ctx - context 36800026cea9SSean Farley 36810026cea9SSean Farley Output Arguments: 36820026cea9SSean Farley + A - pointer to operator 36830026cea9SSean Farley . B - pointer to preconditioning matrix 36840026cea9SSean Farley - flg - matrix structure flag 36850026cea9SSean Farley 3686b41af12eSJed Brown Level: advanced 36870026cea9SSean Farley 36880026cea9SSean Farley Notes: 36890026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 36900026cea9SSean Farley 3691b41af12eSJed Brown It is only appropriate for problems of the form 3692b41af12eSJed Brown 3693b41af12eSJed Brown $ M Udot = F(U,t) 3694b41af12eSJed Brown 3695b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 3696b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 3697b41af12eSJed Brown an implicit operator of the form 3698b41af12eSJed Brown 3699b41af12eSJed Brown $ shift*M + J 3700b41af12eSJed Brown 3701b41af12eSJed Brown where J is the Jacobian of -F(U). Support may be added in a future version of PETSc, but for now, the user must store 3702b41af12eSJed Brown a copy of M or reassemble it when requested. 3703b41af12eSJed Brown 37040026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 37050026cea9SSean Farley @*/ 3706*94ab13aaSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,MatStructure *flg,void *ctx) 37070026cea9SSean Farley { 3708b41af12eSJed Brown PetscErrorCode ierr; 3709b41af12eSJed Brown 37100026cea9SSean Farley PetscFunctionBegin; 3711*94ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 3712b41af12eSJed Brown ts->ijacobian.shift = shift; 37130026cea9SSean Farley *flg = SAME_PRECONDITIONER; 37140026cea9SSean Farley PetscFunctionReturn(0); 37150026cea9SSean Farley } 3716b41af12eSJed Brown 3717e817cc15SEmil Constantinescu #undef __FUNCT__ 3718e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType" 3719e817cc15SEmil Constantinescu /*@ 3720e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 3721e817cc15SEmil Constantinescu 3722e817cc15SEmil Constantinescu Not Collective 3723e817cc15SEmil Constantinescu 3724e817cc15SEmil Constantinescu Input Parameter: 3725e817cc15SEmil Constantinescu . ts - the TS context 3726e817cc15SEmil Constantinescu 3727e817cc15SEmil Constantinescu Output Parameter: 37284e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 3729e817cc15SEmil Constantinescu 3730e817cc15SEmil Constantinescu Level: beginner 3731e817cc15SEmil Constantinescu 3732e817cc15SEmil Constantinescu .keywords: TS, equation type 3733e817cc15SEmil Constantinescu 3734e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 3735e817cc15SEmil Constantinescu @*/ 3736e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 3737e817cc15SEmil Constantinescu { 3738e817cc15SEmil Constantinescu PetscFunctionBegin; 3739e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3740e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 3741e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 3742e817cc15SEmil Constantinescu PetscFunctionReturn(0); 3743e817cc15SEmil Constantinescu } 3744e817cc15SEmil Constantinescu 3745e817cc15SEmil Constantinescu #undef __FUNCT__ 3746e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType" 3747e817cc15SEmil Constantinescu /*@ 3748e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 3749e817cc15SEmil Constantinescu 3750e817cc15SEmil Constantinescu Not Collective 3751e817cc15SEmil Constantinescu 3752e817cc15SEmil Constantinescu Input Parameter: 3753e817cc15SEmil Constantinescu + ts - the TS context 37544e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 3755e817cc15SEmil Constantinescu 3756e817cc15SEmil Constantinescu Level: advanced 3757e817cc15SEmil Constantinescu 3758e817cc15SEmil Constantinescu .keywords: TS, equation type 3759e817cc15SEmil Constantinescu 3760e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 3761e817cc15SEmil Constantinescu @*/ 3762e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 3763e817cc15SEmil Constantinescu { 3764e817cc15SEmil Constantinescu PetscFunctionBegin; 3765e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3766e817cc15SEmil Constantinescu ts->equation_type = equation_type; 3767e817cc15SEmil Constantinescu PetscFunctionReturn(0); 3768e817cc15SEmil Constantinescu } 37690026cea9SSean Farley 37704af1b03aSJed Brown #undef __FUNCT__ 37714af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 37724af1b03aSJed Brown /*@ 37734af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 37744af1b03aSJed Brown 37754af1b03aSJed Brown Not Collective 37764af1b03aSJed Brown 37774af1b03aSJed Brown Input Parameter: 37784af1b03aSJed Brown . ts - the TS context 37794af1b03aSJed Brown 37804af1b03aSJed Brown Output Parameter: 37814af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 37824af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 37834af1b03aSJed Brown 3784487e0bb9SJed Brown Level: beginner 37854af1b03aSJed Brown 3786cd652676SJed Brown Notes: 3787cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 37884af1b03aSJed Brown 37894af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 37904af1b03aSJed Brown 37914af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 37924af1b03aSJed Brown @*/ 37934af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 37944af1b03aSJed Brown { 37954af1b03aSJed Brown PetscFunctionBegin; 37964af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 37974af1b03aSJed Brown PetscValidPointer(reason,2); 37984af1b03aSJed Brown *reason = ts->reason; 37994af1b03aSJed Brown PetscFunctionReturn(0); 38004af1b03aSJed Brown } 38014af1b03aSJed Brown 3802fb1732b5SBarry Smith #undef __FUNCT__ 3803d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 3804d6ad946cSShri Abhyankar /*@ 3805d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 3806d6ad946cSShri Abhyankar 3807d6ad946cSShri Abhyankar Not Collective 3808d6ad946cSShri Abhyankar 3809d6ad946cSShri Abhyankar Input Parameter: 3810d6ad946cSShri Abhyankar + ts - the TS context 3811d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 3812d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 3813d6ad946cSShri Abhyankar 3814f5abba47SShri Abhyankar Level: advanced 3815d6ad946cSShri Abhyankar 3816d6ad946cSShri Abhyankar Notes: 3817d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 3818d6ad946cSShri Abhyankar 3819d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 3820d6ad946cSShri Abhyankar 3821d6ad946cSShri Abhyankar .seealso: TSConvergedReason 3822d6ad946cSShri Abhyankar @*/ 3823d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 3824d6ad946cSShri Abhyankar { 3825d6ad946cSShri Abhyankar PetscFunctionBegin; 3826d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3827d6ad946cSShri Abhyankar ts->reason = reason; 3828d6ad946cSShri Abhyankar PetscFunctionReturn(0); 3829d6ad946cSShri Abhyankar } 3830d6ad946cSShri Abhyankar 3831d6ad946cSShri Abhyankar #undef __FUNCT__ 3832cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 3833cc708dedSBarry Smith /*@ 3834cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 3835cc708dedSBarry Smith 3836cc708dedSBarry Smith Not Collective 3837cc708dedSBarry Smith 3838cc708dedSBarry Smith Input Parameter: 3839cc708dedSBarry Smith . ts - the TS context 3840cc708dedSBarry Smith 3841cc708dedSBarry Smith Output Parameter: 3842cc708dedSBarry Smith . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 3843cc708dedSBarry Smith 3844487e0bb9SJed Brown Level: beginner 3845cc708dedSBarry Smith 3846cc708dedSBarry Smith Notes: 3847cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 3848cc708dedSBarry Smith 3849cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 3850cc708dedSBarry Smith 3851cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 3852cc708dedSBarry Smith @*/ 3853cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 3854cc708dedSBarry Smith { 3855cc708dedSBarry Smith PetscFunctionBegin; 3856cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3857cc708dedSBarry Smith PetscValidPointer(ftime,2); 3858cc708dedSBarry Smith *ftime = ts->solvetime; 3859cc708dedSBarry Smith PetscFunctionReturn(0); 3860cc708dedSBarry Smith } 3861cc708dedSBarry Smith 3862cc708dedSBarry Smith #undef __FUNCT__ 38635ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 38649f67acb7SJed Brown /*@ 38655ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 38669f67acb7SJed Brown used by the time integrator. 38679f67acb7SJed Brown 38689f67acb7SJed Brown Not Collective 38699f67acb7SJed Brown 38709f67acb7SJed Brown Input Parameter: 38719f67acb7SJed Brown . ts - TS context 38729f67acb7SJed Brown 38739f67acb7SJed Brown Output Parameter: 38749f67acb7SJed Brown . nits - number of nonlinear iterations 38759f67acb7SJed Brown 38769f67acb7SJed Brown Notes: 38779f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 38789f67acb7SJed Brown 38799f67acb7SJed Brown Level: intermediate 38809f67acb7SJed Brown 38819f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 38829f67acb7SJed Brown 38835ef26d82SJed Brown .seealso: TSGetKSPIterations() 38849f67acb7SJed Brown @*/ 38855ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 38869f67acb7SJed Brown { 38879f67acb7SJed Brown PetscFunctionBegin; 38889f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 38899f67acb7SJed Brown PetscValidIntPointer(nits,2); 38905ef26d82SJed Brown *nits = ts->snes_its; 38919f67acb7SJed Brown PetscFunctionReturn(0); 38929f67acb7SJed Brown } 38939f67acb7SJed Brown 38949f67acb7SJed Brown #undef __FUNCT__ 38955ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 38969f67acb7SJed Brown /*@ 38975ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 38989f67acb7SJed Brown used by the time integrator. 38999f67acb7SJed Brown 39009f67acb7SJed Brown Not Collective 39019f67acb7SJed Brown 39029f67acb7SJed Brown Input Parameter: 39039f67acb7SJed Brown . ts - TS context 39049f67acb7SJed Brown 39059f67acb7SJed Brown Output Parameter: 39069f67acb7SJed Brown . lits - number of linear iterations 39079f67acb7SJed Brown 39089f67acb7SJed Brown Notes: 39099f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 39109f67acb7SJed Brown 39119f67acb7SJed Brown Level: intermediate 39129f67acb7SJed Brown 39139f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 39149f67acb7SJed Brown 39155ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 39169f67acb7SJed Brown @*/ 39175ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 39189f67acb7SJed Brown { 39199f67acb7SJed Brown PetscFunctionBegin; 39209f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 39219f67acb7SJed Brown PetscValidIntPointer(lits,2); 39225ef26d82SJed Brown *lits = ts->ksp_its; 39239f67acb7SJed Brown PetscFunctionReturn(0); 39249f67acb7SJed Brown } 39259f67acb7SJed Brown 39269f67acb7SJed Brown #undef __FUNCT__ 3927cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 3928cef5090cSJed Brown /*@ 3929cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 3930cef5090cSJed Brown 3931cef5090cSJed Brown Not Collective 3932cef5090cSJed Brown 3933cef5090cSJed Brown Input Parameter: 3934cef5090cSJed Brown . ts - TS context 3935cef5090cSJed Brown 3936cef5090cSJed Brown Output Parameter: 3937cef5090cSJed Brown . rejects - number of steps rejected 3938cef5090cSJed Brown 3939cef5090cSJed Brown Notes: 3940cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 3941cef5090cSJed Brown 3942cef5090cSJed Brown Level: intermediate 3943cef5090cSJed Brown 3944cef5090cSJed Brown .keywords: TS, get, number 3945cef5090cSJed Brown 39465ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 3947cef5090cSJed Brown @*/ 3948cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 3949cef5090cSJed Brown { 3950cef5090cSJed Brown PetscFunctionBegin; 3951cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3952cef5090cSJed Brown PetscValidIntPointer(rejects,2); 3953cef5090cSJed Brown *rejects = ts->reject; 3954cef5090cSJed Brown PetscFunctionReturn(0); 3955cef5090cSJed Brown } 3956cef5090cSJed Brown 3957cef5090cSJed Brown #undef __FUNCT__ 3958cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 3959cef5090cSJed Brown /*@ 3960cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 3961cef5090cSJed Brown 3962cef5090cSJed Brown Not Collective 3963cef5090cSJed Brown 3964cef5090cSJed Brown Input Parameter: 3965cef5090cSJed Brown . ts - TS context 3966cef5090cSJed Brown 3967cef5090cSJed Brown Output Parameter: 3968cef5090cSJed Brown . fails - number of failed nonlinear solves 3969cef5090cSJed Brown 3970cef5090cSJed Brown Notes: 3971cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 3972cef5090cSJed Brown 3973cef5090cSJed Brown Level: intermediate 3974cef5090cSJed Brown 3975cef5090cSJed Brown .keywords: TS, get, number 3976cef5090cSJed Brown 39775ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 3978cef5090cSJed Brown @*/ 3979cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 3980cef5090cSJed Brown { 3981cef5090cSJed Brown PetscFunctionBegin; 3982cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3983cef5090cSJed Brown PetscValidIntPointer(fails,2); 3984cef5090cSJed Brown *fails = ts->num_snes_failures; 3985cef5090cSJed Brown PetscFunctionReturn(0); 3986cef5090cSJed Brown } 3987cef5090cSJed Brown 3988cef5090cSJed Brown #undef __FUNCT__ 3989cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 3990cef5090cSJed Brown /*@ 3991cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 3992cef5090cSJed Brown 3993cef5090cSJed Brown Not Collective 3994cef5090cSJed Brown 3995cef5090cSJed Brown Input Parameter: 3996cef5090cSJed Brown + ts - TS context 3997cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 3998cef5090cSJed Brown 3999cef5090cSJed Brown Notes: 4000cef5090cSJed Brown The counter is reset to zero for each step 4001cef5090cSJed Brown 4002cef5090cSJed Brown Options Database Key: 4003cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4004cef5090cSJed Brown 4005cef5090cSJed Brown Level: intermediate 4006cef5090cSJed Brown 4007cef5090cSJed Brown .keywords: TS, set, maximum, number 4008cef5090cSJed Brown 40095ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4010cef5090cSJed Brown @*/ 4011cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 4012cef5090cSJed Brown { 4013cef5090cSJed Brown PetscFunctionBegin; 4014cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4015cef5090cSJed Brown ts->max_reject = rejects; 4016cef5090cSJed Brown PetscFunctionReturn(0); 4017cef5090cSJed Brown } 4018cef5090cSJed Brown 4019cef5090cSJed Brown #undef __FUNCT__ 4020cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 4021cef5090cSJed Brown /*@ 4022cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 4023cef5090cSJed Brown 4024cef5090cSJed Brown Not Collective 4025cef5090cSJed Brown 4026cef5090cSJed Brown Input Parameter: 4027cef5090cSJed Brown + ts - TS context 4028cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 4029cef5090cSJed Brown 4030cef5090cSJed Brown Notes: 4031cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 4032cef5090cSJed Brown 4033cef5090cSJed Brown Options Database Key: 4034cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4035cef5090cSJed Brown 4036cef5090cSJed Brown Level: intermediate 4037cef5090cSJed Brown 4038cef5090cSJed Brown .keywords: TS, set, maximum, number 4039cef5090cSJed Brown 40405ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 4041cef5090cSJed Brown @*/ 4042cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 4043cef5090cSJed Brown { 4044cef5090cSJed Brown PetscFunctionBegin; 4045cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4046cef5090cSJed Brown ts->max_snes_failures = fails; 4047cef5090cSJed Brown PetscFunctionReturn(0); 4048cef5090cSJed Brown } 4049cef5090cSJed Brown 4050cef5090cSJed Brown #undef __FUNCT__ 40514e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails" 4052cef5090cSJed Brown /*@ 4053cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 4054cef5090cSJed Brown 4055cef5090cSJed Brown Not Collective 4056cef5090cSJed Brown 4057cef5090cSJed Brown Input Parameter: 4058cef5090cSJed Brown + ts - TS context 4059cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 4060cef5090cSJed Brown 4061cef5090cSJed Brown Options Database Key: 4062cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 4063cef5090cSJed Brown 4064cef5090cSJed Brown Level: intermediate 4065cef5090cSJed Brown 4066cef5090cSJed Brown .keywords: TS, set, error 4067cef5090cSJed Brown 40685ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4069cef5090cSJed Brown @*/ 4070cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 4071cef5090cSJed Brown { 4072cef5090cSJed Brown PetscFunctionBegin; 4073cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4074cef5090cSJed Brown ts->errorifstepfailed = err; 4075cef5090cSJed Brown PetscFunctionReturn(0); 4076cef5090cSJed Brown } 4077cef5090cSJed Brown 4078cef5090cSJed Brown #undef __FUNCT__ 4079fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 4080fb1732b5SBarry Smith /*@C 4081fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 4082fb1732b5SBarry Smith 4083fb1732b5SBarry Smith Collective on TS 4084fb1732b5SBarry Smith 4085fb1732b5SBarry Smith Input Parameters: 4086fb1732b5SBarry Smith + ts - the TS context 4087fb1732b5SBarry Smith . step - current time-step 4088fb1732b5SBarry Smith . ptime - current time 40890910c330SBarry Smith . u - current state 4090fb1732b5SBarry Smith - viewer - binary viewer 4091fb1732b5SBarry Smith 4092fb1732b5SBarry Smith Level: intermediate 4093fb1732b5SBarry Smith 4094fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 4095fb1732b5SBarry Smith 4096fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4097fb1732b5SBarry Smith @*/ 40980910c330SBarry Smith PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 4099fb1732b5SBarry Smith { 4100fb1732b5SBarry Smith PetscErrorCode ierr; 4101ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 4102fb1732b5SBarry Smith 4103fb1732b5SBarry Smith PetscFunctionBegin; 41040910c330SBarry Smith ierr = VecView(u,v);CHKERRQ(ierr); 4105ed81e22dSJed Brown PetscFunctionReturn(0); 4106ed81e22dSJed Brown } 4107ed81e22dSJed Brown 4108ed81e22dSJed Brown #undef __FUNCT__ 4109ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 4110ed81e22dSJed Brown /*@C 4111ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 4112ed81e22dSJed Brown 4113ed81e22dSJed Brown Collective on TS 4114ed81e22dSJed Brown 4115ed81e22dSJed Brown Input Parameters: 4116ed81e22dSJed Brown + ts - the TS context 4117ed81e22dSJed Brown . step - current time-step 4118ed81e22dSJed Brown . ptime - current time 41190910c330SBarry Smith . u - current state 4120ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 4121ed81e22dSJed Brown 4122ed81e22dSJed Brown Level: intermediate 4123ed81e22dSJed Brown 4124ed81e22dSJed Brown Notes: 4125ed81e22dSJed 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. 4126ed81e22dSJed Brown These are named according to the file name template. 4127ed81e22dSJed Brown 4128ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 4129ed81e22dSJed Brown 4130ed81e22dSJed Brown .keywords: TS, vector, monitor, view 4131ed81e22dSJed Brown 4132ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4133ed81e22dSJed Brown @*/ 41340910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 4135ed81e22dSJed Brown { 4136ed81e22dSJed Brown PetscErrorCode ierr; 4137ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 4138ed81e22dSJed Brown PetscViewer viewer; 4139ed81e22dSJed Brown 4140ed81e22dSJed Brown PetscFunctionBegin; 41418caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 4142ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 41430910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 4144ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 4145ed81e22dSJed Brown PetscFunctionReturn(0); 4146ed81e22dSJed Brown } 4147ed81e22dSJed Brown 4148ed81e22dSJed Brown #undef __FUNCT__ 4149ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 4150ed81e22dSJed Brown /*@C 4151ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 4152ed81e22dSJed Brown 4153ed81e22dSJed Brown Collective on TS 4154ed81e22dSJed Brown 4155ed81e22dSJed Brown Input Parameters: 4156ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 4157ed81e22dSJed Brown 4158ed81e22dSJed Brown Level: intermediate 4159ed81e22dSJed Brown 4160ed81e22dSJed Brown Note: 4161ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 4162ed81e22dSJed Brown 4163ed81e22dSJed Brown .keywords: TS, vector, monitor, view 4164ed81e22dSJed Brown 4165ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 4166ed81e22dSJed Brown @*/ 4167ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 4168ed81e22dSJed Brown { 4169ed81e22dSJed Brown PetscErrorCode ierr; 4170ed81e22dSJed Brown 4171ed81e22dSJed Brown PetscFunctionBegin; 4172ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 4173fb1732b5SBarry Smith PetscFunctionReturn(0); 4174fb1732b5SBarry Smith } 4175fb1732b5SBarry Smith 417684df9cb4SJed Brown #undef __FUNCT__ 4177552698daSJed Brown #define __FUNCT__ "TSGetAdapt" 417884df9cb4SJed Brown /*@ 4179552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 418084df9cb4SJed Brown 4181ed81e22dSJed Brown Collective on TS if controller has not been created yet 418284df9cb4SJed Brown 418384df9cb4SJed Brown Input Arguments: 4184ed81e22dSJed Brown . ts - time stepping context 418584df9cb4SJed Brown 418684df9cb4SJed Brown Output Arguments: 4187ed81e22dSJed Brown . adapt - adaptive controller 418884df9cb4SJed Brown 418984df9cb4SJed Brown Level: intermediate 419084df9cb4SJed Brown 4191ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 419284df9cb4SJed Brown @*/ 4193552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 419484df9cb4SJed Brown { 419584df9cb4SJed Brown PetscErrorCode ierr; 419684df9cb4SJed Brown 419784df9cb4SJed Brown PetscFunctionBegin; 419884df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 419984df9cb4SJed Brown PetscValidPointer(adapt,2); 420084df9cb4SJed Brown if (!ts->adapt) { 4201ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 42023bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 42031c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 420484df9cb4SJed Brown } 420584df9cb4SJed Brown *adapt = ts->adapt; 420684df9cb4SJed Brown PetscFunctionReturn(0); 420784df9cb4SJed Brown } 4208d6ebe24aSShri Abhyankar 4209d6ebe24aSShri Abhyankar #undef __FUNCT__ 42101c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 42111c3436cfSJed Brown /*@ 42121c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 42131c3436cfSJed Brown 42141c3436cfSJed Brown Logically Collective 42151c3436cfSJed Brown 42161c3436cfSJed Brown Input Arguments: 42171c3436cfSJed Brown + ts - time integration context 42181c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 42190298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 42201c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 42210298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 42221c3436cfSJed Brown 42231c3436cfSJed Brown Level: beginner 42241c3436cfSJed Brown 4225c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 42261c3436cfSJed Brown @*/ 42271c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 42281c3436cfSJed Brown { 42291c3436cfSJed Brown PetscErrorCode ierr; 42301c3436cfSJed Brown 42311c3436cfSJed Brown PetscFunctionBegin; 4232c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 42331c3436cfSJed Brown if (vatol) { 42341c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 42351c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 4236bbd56ea5SKarl Rupp 42371c3436cfSJed Brown ts->vatol = vatol; 42381c3436cfSJed Brown } 4239c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 42401c3436cfSJed Brown if (vrtol) { 42411c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 42421c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 4243bbd56ea5SKarl Rupp 42441c3436cfSJed Brown ts->vrtol = vrtol; 42451c3436cfSJed Brown } 42461c3436cfSJed Brown PetscFunctionReturn(0); 42471c3436cfSJed Brown } 42481c3436cfSJed Brown 42491c3436cfSJed Brown #undef __FUNCT__ 4250c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 4251c5033834SJed Brown /*@ 4252c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 4253c5033834SJed Brown 4254c5033834SJed Brown Logically Collective 4255c5033834SJed Brown 4256c5033834SJed Brown Input Arguments: 4257c5033834SJed Brown . ts - time integration context 4258c5033834SJed Brown 4259c5033834SJed Brown Output Arguments: 42600298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 42610298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 42620298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 42630298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 4264c5033834SJed Brown 4265c5033834SJed Brown Level: beginner 4266c5033834SJed Brown 4267c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 4268c5033834SJed Brown @*/ 4269c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 4270c5033834SJed Brown { 4271c5033834SJed Brown PetscFunctionBegin; 4272c5033834SJed Brown if (atol) *atol = ts->atol; 4273c5033834SJed Brown if (vatol) *vatol = ts->vatol; 4274c5033834SJed Brown if (rtol) *rtol = ts->rtol; 4275c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 4276c5033834SJed Brown PetscFunctionReturn(0); 4277c5033834SJed Brown } 4278c5033834SJed Brown 4279c5033834SJed Brown #undef __FUNCT__ 42801c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS" 42811c3436cfSJed Brown /*@ 42821c3436cfSJed Brown TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 42831c3436cfSJed Brown 42841c3436cfSJed Brown Collective on TS 42851c3436cfSJed Brown 42861c3436cfSJed Brown Input Arguments: 42871c3436cfSJed Brown + ts - time stepping context 42881c3436cfSJed Brown - Y - state vector to be compared to ts->vec_sol 42891c3436cfSJed Brown 42901c3436cfSJed Brown Output Arguments: 42911c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 42921c3436cfSJed Brown 42931c3436cfSJed Brown Level: developer 42941c3436cfSJed Brown 42951c3436cfSJed Brown .seealso: TSSetTolerances() 42961c3436cfSJed Brown @*/ 42971c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 42981c3436cfSJed Brown { 42991c3436cfSJed Brown PetscErrorCode ierr; 43001c3436cfSJed Brown PetscInt i,n,N; 43010910c330SBarry Smith const PetscScalar *u,*y; 43020910c330SBarry Smith Vec U; 43031c3436cfSJed Brown PetscReal sum,gsum; 43041c3436cfSJed Brown 43051c3436cfSJed Brown PetscFunctionBegin; 43061c3436cfSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 43071c3436cfSJed Brown PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 43081c3436cfSJed Brown PetscValidPointer(norm,3); 43090910c330SBarry Smith U = ts->vec_sol; 43100910c330SBarry Smith PetscCheckSameTypeAndComm(U,1,Y,2); 4311ce94432eSBarry Smith if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 43121c3436cfSJed Brown 43130910c330SBarry Smith ierr = VecGetSize(U,&N);CHKERRQ(ierr); 43140910c330SBarry Smith ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 43150910c330SBarry Smith ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 43161c3436cfSJed Brown ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 43171c3436cfSJed Brown sum = 0.; 4318ceefc113SJed Brown if (ts->vatol && ts->vrtol) { 4319ceefc113SJed Brown const PetscScalar *atol,*rtol; 4320ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4321ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4322ceefc113SJed Brown for (i=0; i<n; i++) { 43230910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 43240910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4325ceefc113SJed Brown } 4326ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4327ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4328ceefc113SJed Brown } else if (ts->vatol) { /* vector atol, scalar rtol */ 4329ceefc113SJed Brown const PetscScalar *atol; 4330ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4331ceefc113SJed Brown for (i=0; i<n; i++) { 43320910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 43330910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4334ceefc113SJed Brown } 4335ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4336ceefc113SJed Brown } else if (ts->vrtol) { /* scalar atol, vector rtol */ 4337ceefc113SJed Brown const PetscScalar *rtol; 4338ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4339ceefc113SJed Brown for (i=0; i<n; i++) { 43400910c330SBarry Smith PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 43410910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4342ceefc113SJed Brown } 4343ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4344ceefc113SJed Brown } else { /* scalar atol, scalar rtol */ 43451c3436cfSJed Brown for (i=0; i<n; i++) { 43460910c330SBarry Smith PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 43470910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 43481c3436cfSJed Brown } 4349ceefc113SJed Brown } 43500910c330SBarry Smith ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 43511c3436cfSJed Brown ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 43521c3436cfSJed Brown 4353ce94432eSBarry Smith ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 43541c3436cfSJed Brown *norm = PetscSqrtReal(gsum / N); 4355ce94432eSBarry Smith if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 43561c3436cfSJed Brown PetscFunctionReturn(0); 43571c3436cfSJed Brown } 43581c3436cfSJed Brown 43591c3436cfSJed Brown #undef __FUNCT__ 43608d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 43618d59e960SJed Brown /*@ 43628d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 43638d59e960SJed Brown 43648d59e960SJed Brown Logically Collective on TS 43658d59e960SJed Brown 43668d59e960SJed Brown Input Arguments: 43678d59e960SJed Brown + ts - time stepping context 43688d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 43698d59e960SJed Brown 43708d59e960SJed Brown Note: 43718d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 43728d59e960SJed Brown 43738d59e960SJed Brown Level: intermediate 43748d59e960SJed Brown 43758d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 43768d59e960SJed Brown @*/ 43778d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 43788d59e960SJed Brown { 43798d59e960SJed Brown PetscFunctionBegin; 43808d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 43818d59e960SJed Brown ts->cfltime_local = cfltime; 43828d59e960SJed Brown ts->cfltime = -1.; 43838d59e960SJed Brown PetscFunctionReturn(0); 43848d59e960SJed Brown } 43858d59e960SJed Brown 43868d59e960SJed Brown #undef __FUNCT__ 43878d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 43888d59e960SJed Brown /*@ 43898d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 43908d59e960SJed Brown 43918d59e960SJed Brown Collective on TS 43928d59e960SJed Brown 43938d59e960SJed Brown Input Arguments: 43948d59e960SJed Brown . ts - time stepping context 43958d59e960SJed Brown 43968d59e960SJed Brown Output Arguments: 43978d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 43988d59e960SJed Brown 43998d59e960SJed Brown Level: advanced 44008d59e960SJed Brown 44018d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 44028d59e960SJed Brown @*/ 44038d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 44048d59e960SJed Brown { 44058d59e960SJed Brown PetscErrorCode ierr; 44068d59e960SJed Brown 44078d59e960SJed Brown PetscFunctionBegin; 44088d59e960SJed Brown if (ts->cfltime < 0) { 4409ce94432eSBarry Smith ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 44108d59e960SJed Brown } 44118d59e960SJed Brown *cfltime = ts->cfltime; 44128d59e960SJed Brown PetscFunctionReturn(0); 44138d59e960SJed Brown } 44148d59e960SJed Brown 44158d59e960SJed Brown #undef __FUNCT__ 4416d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 4417d6ebe24aSShri Abhyankar /*@ 4418d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 4419d6ebe24aSShri Abhyankar 4420d6ebe24aSShri Abhyankar Input Parameters: 4421d6ebe24aSShri Abhyankar . ts - the TS context. 4422d6ebe24aSShri Abhyankar . xl - lower bound. 4423d6ebe24aSShri Abhyankar . xu - upper bound. 4424d6ebe24aSShri Abhyankar 4425d6ebe24aSShri Abhyankar Notes: 4426d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 4427e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 4428d6ebe24aSShri Abhyankar 44292bd2b0e6SSatish Balay Level: advanced 44302bd2b0e6SSatish Balay 4431d6ebe24aSShri Abhyankar @*/ 4432d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 4433d6ebe24aSShri Abhyankar { 4434d6ebe24aSShri Abhyankar PetscErrorCode ierr; 4435d6ebe24aSShri Abhyankar SNES snes; 4436d6ebe24aSShri Abhyankar 4437d6ebe24aSShri Abhyankar PetscFunctionBegin; 4438d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4439d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 4440d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 4441d6ebe24aSShri Abhyankar } 4442d6ebe24aSShri Abhyankar 4443325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 4444c6db04a5SJed Brown #include <mex.h> 4445325fc9f4SBarry Smith 4446325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 4447325fc9f4SBarry Smith 4448325fc9f4SBarry Smith #undef __FUNCT__ 4449325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 4450325fc9f4SBarry Smith /* 4451325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 4452325fc9f4SBarry Smith TSSetFunctionMatlab(). 4453325fc9f4SBarry Smith 4454325fc9f4SBarry Smith Collective on TS 4455325fc9f4SBarry Smith 4456325fc9f4SBarry Smith Input Parameters: 4457325fc9f4SBarry Smith + snes - the TS context 44580910c330SBarry Smith - u - input vector 4459325fc9f4SBarry Smith 4460325fc9f4SBarry Smith Output Parameter: 4461325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 4462325fc9f4SBarry Smith 4463325fc9f4SBarry Smith Notes: 4464325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 4465325fc9f4SBarry Smith implementations, so most users would not generally call this routine 4466325fc9f4SBarry Smith themselves. 4467325fc9f4SBarry Smith 4468325fc9f4SBarry Smith Level: developer 4469325fc9f4SBarry Smith 4470325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 4471325fc9f4SBarry Smith 4472325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 4473325fc9f4SBarry Smith */ 44740910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 4475325fc9f4SBarry Smith { 4476325fc9f4SBarry Smith PetscErrorCode ierr; 4477325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 4478325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 4479325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 4480325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 4481325fc9f4SBarry Smith 4482325fc9f4SBarry Smith PetscFunctionBegin; 4483325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 44840910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 44850910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 4486325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 44870910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 4488325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 4489325fc9f4SBarry Smith 4490325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 44910910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 44920910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 44930910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 4494bbd56ea5SKarl Rupp 4495325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 4496325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 4497325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 4498325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 4499325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 4500325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 4501325fc9f4SBarry Smith prhs[6] = sctx->ctx; 4502325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 4503325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4504325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 4505325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 4506325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 4507325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 4508325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 4509325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 4510325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 4511325fc9f4SBarry Smith PetscFunctionReturn(0); 4512325fc9f4SBarry Smith } 4513325fc9f4SBarry Smith 4514325fc9f4SBarry Smith 4515325fc9f4SBarry Smith #undef __FUNCT__ 4516325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 4517325fc9f4SBarry Smith /* 4518325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 4519325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 4520e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 4521325fc9f4SBarry Smith 4522325fc9f4SBarry Smith Logically Collective on TS 4523325fc9f4SBarry Smith 4524325fc9f4SBarry Smith Input Parameters: 4525325fc9f4SBarry Smith + ts - the TS context 4526325fc9f4SBarry Smith - func - function evaluation routine 4527325fc9f4SBarry Smith 4528325fc9f4SBarry Smith Calling sequence of func: 45290910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 4530325fc9f4SBarry Smith 4531325fc9f4SBarry Smith Level: beginner 4532325fc9f4SBarry Smith 4533325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 4534325fc9f4SBarry Smith 4535325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4536325fc9f4SBarry Smith */ 4537cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 4538325fc9f4SBarry Smith { 4539325fc9f4SBarry Smith PetscErrorCode ierr; 4540325fc9f4SBarry Smith TSMatlabContext *sctx; 4541325fc9f4SBarry Smith 4542325fc9f4SBarry Smith PetscFunctionBegin; 4543325fc9f4SBarry Smith /* currently sctx is memory bleed */ 4544325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4545325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4546325fc9f4SBarry Smith /* 4547325fc9f4SBarry Smith This should work, but it doesn't 4548325fc9f4SBarry Smith sctx->ctx = ctx; 4549325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4550325fc9f4SBarry Smith */ 4551325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4552bbd56ea5SKarl Rupp 45530298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 4554325fc9f4SBarry Smith PetscFunctionReturn(0); 4555325fc9f4SBarry Smith } 4556325fc9f4SBarry Smith 4557325fc9f4SBarry Smith #undef __FUNCT__ 4558325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 4559325fc9f4SBarry Smith /* 4560325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 4561325fc9f4SBarry Smith TSSetJacobianMatlab(). 4562325fc9f4SBarry Smith 4563325fc9f4SBarry Smith Collective on TS 4564325fc9f4SBarry Smith 4565325fc9f4SBarry Smith Input Parameters: 4566cdcf91faSSean Farley + ts - the TS context 45670910c330SBarry Smith . u - input vector 4568325fc9f4SBarry Smith . A, B - the matrices 4569325fc9f4SBarry Smith - ctx - user context 4570325fc9f4SBarry Smith 4571325fc9f4SBarry Smith Output Parameter: 4572325fc9f4SBarry Smith . flag - structure of the matrix 4573325fc9f4SBarry Smith 4574325fc9f4SBarry Smith Level: developer 4575325fc9f4SBarry Smith 4576325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 4577325fc9f4SBarry Smith 4578325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 4579325fc9f4SBarry Smith @*/ 45800910c330SBarry Smith PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 4581325fc9f4SBarry Smith { 4582325fc9f4SBarry Smith PetscErrorCode ierr; 4583325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 4584325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 4585325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 4586325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 4587325fc9f4SBarry Smith 4588325fc9f4SBarry Smith PetscFunctionBegin; 4589cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45900910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 4591325fc9f4SBarry Smith 45920910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 4593325fc9f4SBarry Smith 4594cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 45950910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 45960910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 45970910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 45980910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 4599bbd56ea5SKarl Rupp 4600325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 4601325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 4602325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 4603325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 4604325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 4605325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 4606325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 4607325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 4608325fc9f4SBarry Smith prhs[8] = sctx->ctx; 4609325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 4610325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4611325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 4612325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 4613325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 4614325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 4615325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 4616325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 4617325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 4618325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 4619325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 4620325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 4621325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 4622325fc9f4SBarry Smith PetscFunctionReturn(0); 4623325fc9f4SBarry Smith } 4624325fc9f4SBarry Smith 4625325fc9f4SBarry Smith 4626325fc9f4SBarry Smith #undef __FUNCT__ 4627325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 4628325fc9f4SBarry Smith /* 4629325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 4630e3c5b3baSBarry 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 4631325fc9f4SBarry Smith 4632325fc9f4SBarry Smith Logically Collective on TS 4633325fc9f4SBarry Smith 4634325fc9f4SBarry Smith Input Parameters: 4635cdcf91faSSean Farley + ts - the TS context 4636325fc9f4SBarry Smith . A,B - Jacobian matrices 4637325fc9f4SBarry Smith . func - function evaluation routine 4638325fc9f4SBarry Smith - ctx - user context 4639325fc9f4SBarry Smith 4640325fc9f4SBarry Smith Calling sequence of func: 46410910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 4642325fc9f4SBarry Smith 4643325fc9f4SBarry Smith 4644325fc9f4SBarry Smith Level: developer 4645325fc9f4SBarry Smith 4646325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 4647325fc9f4SBarry Smith 4648325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4649325fc9f4SBarry Smith */ 4650cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 4651325fc9f4SBarry Smith { 4652325fc9f4SBarry Smith PetscErrorCode ierr; 4653325fc9f4SBarry Smith TSMatlabContext *sctx; 4654325fc9f4SBarry Smith 4655325fc9f4SBarry Smith PetscFunctionBegin; 4656325fc9f4SBarry Smith /* currently sctx is memory bleed */ 4657325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4658325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4659325fc9f4SBarry Smith /* 4660325fc9f4SBarry Smith This should work, but it doesn't 4661325fc9f4SBarry Smith sctx->ctx = ctx; 4662325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4663325fc9f4SBarry Smith */ 4664325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4665bbd56ea5SKarl Rupp 4666cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 4667325fc9f4SBarry Smith PetscFunctionReturn(0); 4668325fc9f4SBarry Smith } 4669325fc9f4SBarry Smith 4670b5b1a830SBarry Smith #undef __FUNCT__ 4671b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 4672b5b1a830SBarry Smith /* 4673b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 4674b5b1a830SBarry Smith 4675b5b1a830SBarry Smith Collective on TS 4676b5b1a830SBarry Smith 4677b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 4678b5b1a830SBarry Smith @*/ 46790910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 4680b5b1a830SBarry Smith { 4681b5b1a830SBarry Smith PetscErrorCode ierr; 4682b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 4683a530c242SBarry Smith int nlhs = 1,nrhs = 6; 4684b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 4685b5b1a830SBarry Smith long long int lx = 0,ls = 0; 4686b5b1a830SBarry Smith 4687b5b1a830SBarry Smith PetscFunctionBegin; 4688cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 46890910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4690b5b1a830SBarry Smith 4691cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 46920910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 4693bbd56ea5SKarl Rupp 4694b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 4695b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 4696b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 4697b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 4698b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 4699b5b1a830SBarry Smith prhs[5] = sctx->ctx; 4700b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 4701b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4702b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 4703b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 4704b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 4705b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 4706b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 4707b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 4708b5b1a830SBarry Smith PetscFunctionReturn(0); 4709b5b1a830SBarry Smith } 4710b5b1a830SBarry Smith 4711b5b1a830SBarry Smith 4712b5b1a830SBarry Smith #undef __FUNCT__ 4713b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 4714b5b1a830SBarry Smith /* 4715b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 4716b5b1a830SBarry Smith 4717b5b1a830SBarry Smith Level: developer 4718b5b1a830SBarry Smith 4719b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 4720b5b1a830SBarry Smith 4721b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4722b5b1a830SBarry Smith */ 4723cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 4724b5b1a830SBarry Smith { 4725b5b1a830SBarry Smith PetscErrorCode ierr; 4726b5b1a830SBarry Smith TSMatlabContext *sctx; 4727b5b1a830SBarry Smith 4728b5b1a830SBarry Smith PetscFunctionBegin; 4729b5b1a830SBarry Smith /* currently sctx is memory bleed */ 4730b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4731b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4732b5b1a830SBarry Smith /* 4733b5b1a830SBarry Smith This should work, but it doesn't 4734b5b1a830SBarry Smith sctx->ctx = ctx; 4735b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4736b5b1a830SBarry Smith */ 4737b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4738bbd56ea5SKarl Rupp 47390298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 4740b5b1a830SBarry Smith PetscFunctionReturn(0); 4741b5b1a830SBarry Smith } 4742325fc9f4SBarry Smith #endif 4743b3603a34SBarry Smith 47440b039ecaSBarry Smith 47450b039ecaSBarry Smith 4746b3603a34SBarry Smith #undef __FUNCT__ 47474f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 4748b3603a34SBarry Smith /*@C 47494f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 4750b3603a34SBarry Smith in a time based line graph 4751b3603a34SBarry Smith 4752b3603a34SBarry Smith Collective on TS 4753b3603a34SBarry Smith 4754b3603a34SBarry Smith Input Parameters: 4755b3603a34SBarry Smith + ts - the TS context 4756b3603a34SBarry Smith . step - current time-step 4757b3603a34SBarry Smith . ptime - current time 4758b3603a34SBarry Smith - lg - a line graph object 4759b3603a34SBarry Smith 4760b3603a34SBarry Smith Level: intermediate 4761b3603a34SBarry Smith 47620b039ecaSBarry Smith Notes: each process in a parallel run displays its component solutions in a separate window 4763b3603a34SBarry Smith 4764b3603a34SBarry Smith .keywords: TS, vector, monitor, view 4765b3603a34SBarry Smith 4766b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4767b3603a34SBarry Smith @*/ 47680910c330SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4769b3603a34SBarry Smith { 4770b3603a34SBarry Smith PetscErrorCode ierr; 47710b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4772b3603a34SBarry Smith const PetscScalar *yy; 477358ff32f7SBarry Smith PetscInt dim; 4774b3603a34SBarry Smith 4775b3603a34SBarry Smith PetscFunctionBegin; 477658ff32f7SBarry Smith if (!step) { 4777a9f9c1f6SBarry Smith PetscDrawAxis axis; 4778a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4779a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 47800910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 47810b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 47820b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 478358ff32f7SBarry Smith } 47840910c330SBarry Smith ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr); 4785e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 4786e3efe391SJed Brown { 4787e3efe391SJed Brown PetscReal *yreal; 4788e3efe391SJed Brown PetscInt i,n; 47890910c330SBarry Smith ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr); 4790785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 4791e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 47920b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4793e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4794e3efe391SJed Brown } 4795e3efe391SJed Brown #else 47960b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4797e3efe391SJed Brown #endif 47980910c330SBarry Smith ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr); 4799b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 48000b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 48013923b477SBarry Smith } 4802b3603a34SBarry Smith PetscFunctionReturn(0); 4803b3603a34SBarry Smith } 4804b3603a34SBarry Smith 4805b3603a34SBarry Smith #undef __FUNCT__ 48064f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 4807ef20d060SBarry Smith /*@C 48084f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 4809ef20d060SBarry Smith in a time based line graph 4810ef20d060SBarry Smith 4811ef20d060SBarry Smith Collective on TS 4812ef20d060SBarry Smith 4813ef20d060SBarry Smith Input Parameters: 4814ef20d060SBarry Smith + ts - the TS context 4815ef20d060SBarry Smith . step - current time-step 4816ef20d060SBarry Smith . ptime - current time 4817ef20d060SBarry Smith - lg - a line graph object 4818ef20d060SBarry Smith 4819ef20d060SBarry Smith Level: intermediate 4820ef20d060SBarry Smith 4821abd5a294SJed Brown Notes: 4822abd5a294SJed Brown Only for sequential solves. 4823abd5a294SJed Brown 4824abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 4825abd5a294SJed Brown 4826abd5a294SJed Brown Options Database Keys: 48274f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 4828ef20d060SBarry Smith 4829ef20d060SBarry Smith .keywords: TS, vector, monitor, view 4830ef20d060SBarry Smith 4831abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 4832ef20d060SBarry Smith @*/ 48330910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4834ef20d060SBarry Smith { 4835ef20d060SBarry Smith PetscErrorCode ierr; 48360b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4837ef20d060SBarry Smith const PetscScalar *yy; 4838ef20d060SBarry Smith Vec y; 4839a9f9c1f6SBarry Smith PetscInt dim; 4840ef20d060SBarry Smith 4841ef20d060SBarry Smith PetscFunctionBegin; 4842a9f9c1f6SBarry Smith if (!step) { 4843a9f9c1f6SBarry Smith PetscDrawAxis axis; 4844a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 48451ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 48460910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 4847a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 4848a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4849a9f9c1f6SBarry Smith } 48500910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 4851ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 48520910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 4853ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 4854e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 4855e3efe391SJed Brown { 4856e3efe391SJed Brown PetscReal *yreal; 4857e3efe391SJed Brown PetscInt i,n; 4858e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 4859785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 4860e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 48610b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4862e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4863e3efe391SJed Brown } 4864e3efe391SJed Brown #else 48650b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4866e3efe391SJed Brown #endif 4867ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 4868ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 4869b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 48700b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 48713923b477SBarry Smith } 4872ef20d060SBarry Smith PetscFunctionReturn(0); 4873ef20d060SBarry Smith } 4874ef20d060SBarry Smith 4875201da799SBarry Smith #undef __FUNCT__ 4876201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 4877201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4878201da799SBarry Smith { 4879201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4880201da799SBarry Smith PetscReal x = ptime,y; 4881201da799SBarry Smith PetscErrorCode ierr; 4882201da799SBarry Smith PetscInt its; 4883201da799SBarry Smith 4884201da799SBarry Smith PetscFunctionBegin; 4885201da799SBarry Smith if (!n) { 4886201da799SBarry Smith PetscDrawAxis axis; 4887bbd56ea5SKarl Rupp 4888201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4889201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 4890201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4891bbd56ea5SKarl Rupp 4892201da799SBarry Smith ctx->snes_its = 0; 4893201da799SBarry Smith } 4894201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 4895201da799SBarry Smith y = its - ctx->snes_its; 4896201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 48973fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 4898201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4899201da799SBarry Smith } 4900201da799SBarry Smith ctx->snes_its = its; 4901201da799SBarry Smith PetscFunctionReturn(0); 4902201da799SBarry Smith } 4903201da799SBarry Smith 4904201da799SBarry Smith #undef __FUNCT__ 4905201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 4906201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4907201da799SBarry Smith { 4908201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4909201da799SBarry Smith PetscReal x = ptime,y; 4910201da799SBarry Smith PetscErrorCode ierr; 4911201da799SBarry Smith PetscInt its; 4912201da799SBarry Smith 4913201da799SBarry Smith PetscFunctionBegin; 4914201da799SBarry Smith if (!n) { 4915201da799SBarry Smith PetscDrawAxis axis; 4916bbd56ea5SKarl Rupp 4917201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4918201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 4919201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4920bbd56ea5SKarl Rupp 4921201da799SBarry Smith ctx->ksp_its = 0; 4922201da799SBarry Smith } 4923201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 4924201da799SBarry Smith y = its - ctx->ksp_its; 4925201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 492699fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 4927201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4928201da799SBarry Smith } 4929201da799SBarry Smith ctx->ksp_its = its; 4930201da799SBarry Smith PetscFunctionReturn(0); 4931201da799SBarry Smith } 4932f9c1d6abSBarry Smith 4933f9c1d6abSBarry Smith #undef __FUNCT__ 4934f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 4935f9c1d6abSBarry Smith /*@ 4936f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 4937f9c1d6abSBarry Smith 4938f9c1d6abSBarry Smith Collective on TS and Vec 4939f9c1d6abSBarry Smith 4940f9c1d6abSBarry Smith Input Parameters: 4941f9c1d6abSBarry Smith + ts - the TS context 4942f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 4943f9c1d6abSBarry Smith 4944f9c1d6abSBarry Smith Output Parameters: 4945f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 4946f9c1d6abSBarry Smith 4947f9c1d6abSBarry Smith Level: developer 4948f9c1d6abSBarry Smith 4949f9c1d6abSBarry Smith .keywords: TS, compute 4950f9c1d6abSBarry Smith 4951f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 4952f9c1d6abSBarry Smith @*/ 4953f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 4954f9c1d6abSBarry Smith { 4955f9c1d6abSBarry Smith PetscErrorCode ierr; 4956f9c1d6abSBarry Smith 4957f9c1d6abSBarry Smith PetscFunctionBegin; 4958f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4959ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 4960f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 4961f9c1d6abSBarry Smith PetscFunctionReturn(0); 4962f9c1d6abSBarry Smith } 4963