163dd3a1aSKris Buschelman 2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h> /*I "petscts.h" I*/ 3496e6a7aSJed Brown #include <petscdmshell.h> 4d763cef2SBarry Smith 5d5ba7fb7SMatthew Knepley /* Logging support */ 6d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 7166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 8d405a339SMatthew Knepley 949354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1049354f04SShri Abhyankar 114a2ae208SSatish Balay #undef __FUNCT__ 12bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions" 13bdad233fSMatthew Knepley /* 14bdad233fSMatthew Knepley TSSetTypeFromOptions - Sets the type of ts from user options. 15bdad233fSMatthew Knepley 16bdad233fSMatthew Knepley Collective on TS 17bdad233fSMatthew Knepley 18bdad233fSMatthew Knepley Input Parameter: 19bdad233fSMatthew Knepley . ts - The ts 20bdad233fSMatthew Knepley 21bdad233fSMatthew Knepley Level: intermediate 22bdad233fSMatthew Knepley 23bdad233fSMatthew Knepley .keywords: TS, set, options, database, type 24bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType() 25bdad233fSMatthew Knepley */ 266849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts) 27bdad233fSMatthew Knepley { 28ace3abfcSBarry Smith PetscBool opt; 292fc52814SBarry Smith const char *defaultType; 30bdad233fSMatthew Knepley char typeName[256]; 31dfbe8321SBarry Smith PetscErrorCode ierr; 32bdad233fSMatthew Knepley 33bdad233fSMatthew Knepley PetscFunctionBegin; 347adad957SLisandro Dalcin if (((PetscObject)ts)->type_name) { 357adad957SLisandro Dalcin defaultType = ((PetscObject)ts)->type_name; 36bdad233fSMatthew Knepley } else { 379596e0b4SJed Brown defaultType = TSEULER; 38bdad233fSMatthew Knepley } 39bdad233fSMatthew Knepley 40cce0b1b2SLisandro Dalcin if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 41bdad233fSMatthew Knepley ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 42a7cc72afSBarry Smith if (opt) { 43bdad233fSMatthew Knepley ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 44bdad233fSMatthew Knepley } else { 45bdad233fSMatthew Knepley ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 46bdad233fSMatthew Knepley } 47bdad233fSMatthew Knepley PetscFunctionReturn(0); 48bdad233fSMatthew Knepley } 49bdad233fSMatthew Knepley 50bdad233fSMatthew Knepley #undef __FUNCT__ 51bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions" 52bdad233fSMatthew Knepley /*@ 53bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 54bdad233fSMatthew Knepley 55bdad233fSMatthew Knepley Collective on TS 56bdad233fSMatthew Knepley 57bdad233fSMatthew Knepley Input Parameter: 58bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 59bdad233fSMatthew Knepley 60bdad233fSMatthew Knepley Options Database Keys: 614d91e141SJed Brown + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP 62bdad233fSMatthew Knepley . -ts_max_steps maxsteps - maximum number of time-steps to take 633bca7d26SBarry Smith . -ts_final_time time - maximum time to compute to 64bdad233fSMatthew Knepley . -ts_dt dt - initial time step 65bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 66de06c3feSJed Brown . -ts_monitor_lg_timestep - Monitor timestep size graphically 67de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 68de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 69de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 70de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 71de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 72de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 73de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 74de06c3feSJed Brown . -ts_monitor_draw_error - Monitor error graphically 75de06c3feSJed Brown . -ts_monitor_draw_solution_binary <filename> - Save each solution to a binary file 76de06c3feSJed Brown - -ts_monitor_draw_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 77bdad233fSMatthew Knepley 78bdad233fSMatthew Knepley Level: beginner 79bdad233fSMatthew Knepley 80bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 81bdad233fSMatthew Knepley 82a313700dSBarry Smith .seealso: TSGetType() 83bdad233fSMatthew Knepley @*/ 847087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 85bdad233fSMatthew Knepley { 86ace3abfcSBarry Smith PetscBool opt,flg; 87dfbe8321SBarry Smith PetscErrorCode ierr; 88649052a6SBarry Smith PetscViewer monviewer; 89eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 90089b2837SJed Brown SNES snes; 911c3436cfSJed Brown TSAdapt adapt; 9231748224SBarry Smith PetscReal time_step; 9349354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 94bdad233fSMatthew Knepley 95bdad233fSMatthew Knepley PetscFunctionBegin; 960700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 973194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 98a43b19c4SJed Brown /* Handle TS type options */ 99a43b19c4SJed Brown ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr); 100bdad233fSMatthew Knepley 101bdad233fSMatthew Knepley /* Handle generic TS options */ 102bdad233fSMatthew Knepley ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr); 1033bca7d26SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr); 104d8cd7023SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr); 10531748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 10631748224SBarry Smith if (flg) { 10731748224SBarry Smith ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 10831748224SBarry Smith } 10949354f04SShri 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); 11049354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 111cef5090cSJed Brown ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,PETSC_NULL);CHKERRQ(ierr); 112cef5090cSJed Brown ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,PETSC_NULL);CHKERRQ(ierr); 113cef5090cSJed Brown ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr); 1141c3436cfSJed Brown ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,PETSC_NULL);CHKERRQ(ierr); 1151c3436cfSJed Brown ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,PETSC_NULL);CHKERRQ(ierr); 116bdad233fSMatthew Knepley 117bdad233fSMatthew Knepley /* Monitor options */ 118a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 119eabae89aSBarry Smith if (flg) { 120649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr); 121649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 122bdad233fSMatthew Knepley } 1235180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1245180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1255180491cSLisandro Dalcin 1264f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 127a7cc72afSBarry Smith if (opt) { 1280b039ecaSBarry Smith TSMonitorLGCtx ctx; 1293923b477SBarry Smith PetscInt howoften = 1; 130a80ad3e0SBarry Smith 1314f09c107SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 1321d1e9da1SBarry Smith ierr = TSMonitorLGCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 1334f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 134b3603a34SBarry Smith } 1354f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 136b3603a34SBarry Smith if (opt) { 1370b039ecaSBarry Smith TSMonitorLGCtx ctx; 1383923b477SBarry Smith PetscInt howoften = 1; 139b3603a34SBarry Smith 1404f09c107SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 1413923b477SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 1424f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 143bdad233fSMatthew Knepley } 1444f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 145ef20d060SBarry Smith if (opt) { 1460b039ecaSBarry Smith TSMonitorLGCtx ctx; 1473923b477SBarry Smith PetscInt howoften = 1; 148ef20d060SBarry Smith 1494f09c107SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 1503923b477SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 1514f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 152ef20d060SBarry Smith } 153201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 154201da799SBarry Smith if (opt) { 155201da799SBarry Smith TSMonitorLGCtx ctx; 156201da799SBarry Smith PetscInt howoften = 1; 157201da799SBarry Smith 158201da799SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 159201da799SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx); 160201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 161201da799SBarry Smith } 162201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 163201da799SBarry Smith if (opt) { 164201da799SBarry Smith TSMonitorLGCtx ctx; 165201da799SBarry Smith PetscInt howoften = 1; 166201da799SBarry Smith 167201da799SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 168201da799SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx); 169201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 170201da799SBarry Smith } 1718189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 1728189c53fSBarry Smith if (opt) { 1738189c53fSBarry Smith TSMonitorSPEigCtx ctx; 1748189c53fSBarry Smith PetscInt howoften = 1; 1758189c53fSBarry Smith 1768189c53fSBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 1778189c53fSBarry Smith ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 1788189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 1798189c53fSBarry Smith } 180ef20d060SBarry Smith opt = PETSC_FALSE; 1810dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 182a7cc72afSBarry Smith if (opt) { 18383a4ac43SBarry Smith TSMonitorDrawCtx ctx; 18483a4ac43SBarry Smith PetscInt howoften = 1; 185a80ad3e0SBarry Smith 18683a4ac43SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 18783a4ac43SBarry Smith ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 18883a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 189bdad233fSMatthew Knepley } 190fb1732b5SBarry Smith opt = PETSC_FALSE; 1910dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 1923a471f94SBarry Smith if (opt) { 19383a4ac43SBarry Smith TSMonitorDrawCtx ctx; 19483a4ac43SBarry Smith PetscInt howoften = 1; 1953a471f94SBarry Smith 19683a4ac43SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 19783a4ac43SBarry Smith ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 19883a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 1993a471f94SBarry Smith } 2003a471f94SBarry Smith opt = PETSC_FALSE; 201de06c3feSJed Brown ierr = PetscOptionsString("-ts_monitor_draw_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 202fb1732b5SBarry Smith if (flg) { 203fb1732b5SBarry Smith PetscViewer ctx; 204fb1732b5SBarry Smith if (monfilename[0]) { 205fb1732b5SBarry Smith ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 206fb1732b5SBarry Smith } else { 207fb1732b5SBarry Smith ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm); 208fb1732b5SBarry Smith } 209fb1732b5SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 210fb1732b5SBarry Smith } 211ed81e22dSJed Brown opt = PETSC_FALSE; 212de06c3feSJed Brown ierr = PetscOptionsString("-ts_monitor_draw_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 213ed81e22dSJed Brown if (flg) { 214ed81e22dSJed Brown const char *ptr,*ptr2; 215ed81e22dSJed Brown char *filetemplate; 216de06c3feSJed Brown if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_draw_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 217ed81e22dSJed Brown /* Do some cursory validation of the input. */ 218ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 219de06c3feSJed Brown if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_draw_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 220ed81e22dSJed Brown for (ptr++ ; ptr && *ptr; ptr++) { 221ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 222de06c3feSJed Brown if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_draw_solution_vtk, should look like filename-%%03D.vts"); 223ed81e22dSJed Brown if (ptr2) break; 224ed81e22dSJed Brown } 225ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 226ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 227ed81e22dSJed Brown } 228bdad233fSMatthew Knepley 229ad6bc421SBarry Smith ierr = TSGetTSAdapt(ts,&adapt);CHKERRQ(ierr); 2301c3436cfSJed Brown ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr); 2311c3436cfSJed Brown 232d52bd9f3SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 233d52bd9f3SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 234d52bd9f3SBarry Smith 235bdad233fSMatthew Knepley /* Handle specific TS options */ 236abc0a331SBarry Smith if (ts->ops->setfromoptions) { 237bdad233fSMatthew Knepley ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr); 238bdad233fSMatthew Knepley } 2395d973c19SBarry Smith 2405d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 2415d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 242bdad233fSMatthew Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 243bdad233fSMatthew Knepley PetscFunctionReturn(0); 244bdad233fSMatthew Knepley } 245bdad233fSMatthew Knepley 246bdad233fSMatthew Knepley #undef __FUNCT__ 247cdcf91faSSean Farley #undef __FUNCT__ 2484a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian" 249a7a1495cSBarry Smith /*@ 2508c385f81SBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 251a7a1495cSBarry Smith set with TSSetRHSJacobian(). 252a7a1495cSBarry Smith 253a7a1495cSBarry Smith Collective on TS and Vec 254a7a1495cSBarry Smith 255a7a1495cSBarry Smith Input Parameters: 256316643e7SJed Brown + ts - the TS context 257a7a1495cSBarry Smith . t - current timestep 2580910c330SBarry Smith - U - input vector 259a7a1495cSBarry Smith 260a7a1495cSBarry Smith Output Parameters: 261a7a1495cSBarry Smith + A - Jacobian matrix 262a7a1495cSBarry Smith . B - optional preconditioning matrix 263a7a1495cSBarry Smith - flag - flag indicating matrix structure 264a7a1495cSBarry Smith 265a7a1495cSBarry Smith Notes: 266a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 267a7a1495cSBarry Smith is used internally within the nonlinear solvers. 268a7a1495cSBarry Smith 26994b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 270a7a1495cSBarry Smith flag parameter. 271a7a1495cSBarry Smith 272a7a1495cSBarry Smith Level: developer 273a7a1495cSBarry Smith 274a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 275a7a1495cSBarry Smith 27694b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 277a7a1495cSBarry Smith @*/ 2780910c330SBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg) 279a7a1495cSBarry Smith { 280dfbe8321SBarry Smith PetscErrorCode ierr; 2810910c330SBarry Smith PetscInt Ustate; 28224989b8cSPeter Brune DM dm; 283942e3340SBarry Smith DMTS tsdm; 28424989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 28524989b8cSPeter Brune void *ctx; 28624989b8cSPeter Brune TSIJacobian ijacobianfunc; 287a7a1495cSBarry Smith 288a7a1495cSBarry Smith PetscFunctionBegin; 2890700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2900910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 2910910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 29224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 293942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 29424989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 29524989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr); 2960910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr); 2970910c330SBarry Smith if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) { 2980e4ef248SJed Brown *flg = ts->rhsjacobian.mstructure; 2990e4ef248SJed Brown PetscFunctionReturn(0); 300f8ede8e7SJed Brown } 301d90be118SSean Farley 30224989b8cSPeter Brune if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 303d90be118SSean Farley 30424989b8cSPeter Brune if (rhsjacobianfunc) { 3050910c330SBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 306a7a1495cSBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 307a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 3080910c330SBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr); 309a7a1495cSBarry Smith PetscStackPop; 3100910c330SBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 311a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 3120700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 3130700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 314ef66eb69SBarry Smith } else { 315214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 316214bc6a2SJed Brown if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);} 317214bc6a2SJed Brown *flg = SAME_NONZERO_PATTERN; 318ef66eb69SBarry Smith } 3190e4ef248SJed Brown ts->rhsjacobian.time = t; 3200910c330SBarry Smith ts->rhsjacobian.X = U; 3210910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 3220e4ef248SJed Brown ts->rhsjacobian.mstructure = *flg; 323a7a1495cSBarry Smith PetscFunctionReturn(0); 324a7a1495cSBarry Smith } 325a7a1495cSBarry Smith 3264a2ae208SSatish Balay #undef __FUNCT__ 3274a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 328316643e7SJed Brown /*@ 329d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 330d763cef2SBarry Smith 331316643e7SJed Brown Collective on TS and Vec 332316643e7SJed Brown 333316643e7SJed Brown Input Parameters: 334316643e7SJed Brown + ts - the TS context 335316643e7SJed Brown . t - current time 3360910c330SBarry Smith - U - state vector 337316643e7SJed Brown 338316643e7SJed Brown Output Parameter: 339316643e7SJed Brown . y - right hand side 340316643e7SJed Brown 341316643e7SJed Brown Note: 342316643e7SJed Brown Most users should not need to explicitly call this routine, as it 343316643e7SJed Brown is used internally within the nonlinear solvers. 344316643e7SJed Brown 345316643e7SJed Brown Level: developer 346316643e7SJed Brown 347316643e7SJed Brown .keywords: TS, compute 348316643e7SJed Brown 349316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 350316643e7SJed Brown @*/ 3510910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 352d763cef2SBarry Smith { 353dfbe8321SBarry Smith PetscErrorCode ierr; 35424989b8cSPeter Brune TSRHSFunction rhsfunction; 35524989b8cSPeter Brune TSIFunction ifunction; 35624989b8cSPeter Brune void *ctx; 35724989b8cSPeter Brune DM dm; 35824989b8cSPeter Brune 359d763cef2SBarry Smith PetscFunctionBegin; 3600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3610910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3620700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 36324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 36424989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 36524989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr); 366d763cef2SBarry Smith 36724989b8cSPeter Brune if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 368d763cef2SBarry Smith 3690910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 37024989b8cSPeter Brune if (rhsfunction) { 371d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 3720910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 373d763cef2SBarry Smith PetscStackPop; 374214bc6a2SJed Brown } else { 375214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 376b2cd27e8SJed Brown } 37744a41b28SSean Farley 3780910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 379d763cef2SBarry Smith PetscFunctionReturn(0); 380d763cef2SBarry Smith } 381d763cef2SBarry Smith 3824a2ae208SSatish Balay #undef __FUNCT__ 383ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 384ef20d060SBarry Smith /*@ 385ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 386ef20d060SBarry Smith 387ef20d060SBarry Smith Collective on TS and Vec 388ef20d060SBarry Smith 389ef20d060SBarry Smith Input Parameters: 390ef20d060SBarry Smith + ts - the TS context 391ef20d060SBarry Smith - t - current time 392ef20d060SBarry Smith 393ef20d060SBarry Smith Output Parameter: 3940910c330SBarry Smith . U - the solution 395ef20d060SBarry Smith 396ef20d060SBarry Smith Note: 397ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 398ef20d060SBarry Smith is used internally within the nonlinear solvers. 399ef20d060SBarry Smith 400ef20d060SBarry Smith Level: developer 401ef20d060SBarry Smith 402ef20d060SBarry Smith .keywords: TS, compute 403ef20d060SBarry Smith 404abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 405ef20d060SBarry Smith @*/ 4060910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 407ef20d060SBarry Smith { 408ef20d060SBarry Smith PetscErrorCode ierr; 409ef20d060SBarry Smith TSSolutionFunction solutionfunction; 410ef20d060SBarry Smith void *ctx; 411ef20d060SBarry Smith DM dm; 412ef20d060SBarry Smith 413ef20d060SBarry Smith PetscFunctionBegin; 414ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4150910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 416ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 417ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 418ef20d060SBarry Smith 419ef20d060SBarry Smith if (solutionfunction) { 420ef20d060SBarry Smith PetscStackPush("TS user right-hand-side function"); 4210910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 422ef20d060SBarry Smith PetscStackPop; 423ef20d060SBarry Smith } 424ef20d060SBarry Smith PetscFunctionReturn(0); 425ef20d060SBarry Smith } 426ef20d060SBarry Smith 427ef20d060SBarry Smith #undef __FUNCT__ 428214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 429214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 430214bc6a2SJed Brown { 4312dd45cf8SJed Brown Vec F; 432214bc6a2SJed Brown PetscErrorCode ierr; 433214bc6a2SJed Brown 434214bc6a2SJed Brown PetscFunctionBegin; 4350da9a4f0SJed Brown *Frhs = PETSC_NULL; 4362dd45cf8SJed Brown ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 437214bc6a2SJed Brown if (!ts->Frhs) { 4382dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 439214bc6a2SJed Brown } 440214bc6a2SJed Brown *Frhs = ts->Frhs; 441214bc6a2SJed Brown PetscFunctionReturn(0); 442214bc6a2SJed Brown } 443214bc6a2SJed Brown 444214bc6a2SJed Brown #undef __FUNCT__ 445214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 446214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 447214bc6a2SJed Brown { 448214bc6a2SJed Brown Mat A,B; 4492dd45cf8SJed Brown PetscErrorCode ierr; 450214bc6a2SJed Brown 451214bc6a2SJed Brown PetscFunctionBegin; 452214bc6a2SJed Brown ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 453214bc6a2SJed Brown if (Arhs) { 454214bc6a2SJed Brown if (!ts->Arhs) { 455214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 456214bc6a2SJed Brown } 457214bc6a2SJed Brown *Arhs = ts->Arhs; 458214bc6a2SJed Brown } 459214bc6a2SJed Brown if (Brhs) { 460214bc6a2SJed Brown if (!ts->Brhs) { 461214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 462214bc6a2SJed Brown } 463214bc6a2SJed Brown *Brhs = ts->Brhs; 464214bc6a2SJed Brown } 465214bc6a2SJed Brown PetscFunctionReturn(0); 466214bc6a2SJed Brown } 467214bc6a2SJed Brown 468214bc6a2SJed Brown #undef __FUNCT__ 469316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 470316643e7SJed Brown /*@ 4710910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 472316643e7SJed Brown 473316643e7SJed Brown Collective on TS and Vec 474316643e7SJed Brown 475316643e7SJed Brown Input Parameters: 476316643e7SJed Brown + ts - the TS context 477316643e7SJed Brown . t - current time 4780910c330SBarry Smith . U - state vector 4790910c330SBarry Smith . Udot - time derivative of state vector 480214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 481316643e7SJed Brown 482316643e7SJed Brown Output Parameter: 483316643e7SJed Brown . Y - right hand side 484316643e7SJed Brown 485316643e7SJed Brown Note: 486316643e7SJed Brown Most users should not need to explicitly call this routine, as it 487316643e7SJed Brown is used internally within the nonlinear solvers. 488316643e7SJed Brown 489316643e7SJed Brown If the user did did not write their equations in implicit form, this 490316643e7SJed Brown function recasts them in implicit form. 491316643e7SJed Brown 492316643e7SJed Brown Level: developer 493316643e7SJed Brown 494316643e7SJed Brown .keywords: TS, compute 495316643e7SJed Brown 496316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 497316643e7SJed Brown @*/ 4980910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 499316643e7SJed Brown { 500316643e7SJed Brown PetscErrorCode ierr; 50124989b8cSPeter Brune TSIFunction ifunction; 50224989b8cSPeter Brune TSRHSFunction rhsfunction; 50324989b8cSPeter Brune void *ctx; 50424989b8cSPeter Brune DM dm; 505316643e7SJed Brown 506316643e7SJed Brown PetscFunctionBegin; 5070700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5080910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5090910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 5100700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 511316643e7SJed Brown 51224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 51324989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 51424989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr); 51524989b8cSPeter Brune 51624989b8cSPeter Brune if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 517d90be118SSean Farley 5180910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 51924989b8cSPeter Brune if (ifunction) { 520316643e7SJed Brown PetscStackPush("TS user implicit function"); 5210910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 522316643e7SJed Brown PetscStackPop; 523214bc6a2SJed Brown } 524214bc6a2SJed Brown if (imex) { 52524989b8cSPeter Brune if (!ifunction) { 5260910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 5272dd45cf8SJed Brown } 52824989b8cSPeter Brune } else if (rhsfunction) { 52924989b8cSPeter Brune if (ifunction) { 530214bc6a2SJed Brown Vec Frhs; 531214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 5320910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 533214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 5342dd45cf8SJed Brown } else { 5350910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 5360910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 537316643e7SJed Brown } 5384a6899ffSJed Brown } 5390910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 540316643e7SJed Brown PetscFunctionReturn(0); 541316643e7SJed Brown } 542316643e7SJed Brown 543316643e7SJed Brown #undef __FUNCT__ 544316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 545316643e7SJed Brown /*@ 546316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 547316643e7SJed Brown 548316643e7SJed Brown Collective on TS and Vec 549316643e7SJed Brown 550316643e7SJed Brown Input 551316643e7SJed Brown Input Parameters: 552316643e7SJed Brown + ts - the TS context 553316643e7SJed Brown . t - current timestep 5540910c330SBarry Smith . U - state vector 5550910c330SBarry Smith . Udot - time derivative of state vector 556214bc6a2SJed Brown . shift - shift to apply, see note below 557214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 558316643e7SJed Brown 559316643e7SJed Brown Output Parameters: 560316643e7SJed Brown + A - Jacobian matrix 561316643e7SJed Brown . B - optional preconditioning matrix 562316643e7SJed Brown - flag - flag indicating matrix structure 563316643e7SJed Brown 564316643e7SJed Brown Notes: 5650910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 566316643e7SJed Brown 5670910c330SBarry Smith dF/dU + shift*dF/dUdot 568316643e7SJed Brown 569316643e7SJed Brown Most users should not need to explicitly call this routine, as it 570316643e7SJed Brown is used internally within the nonlinear solvers. 571316643e7SJed Brown 572316643e7SJed Brown Level: developer 573316643e7SJed Brown 574316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 575316643e7SJed Brown 576316643e7SJed Brown .seealso: TSSetIJacobian() 57763495f91SJed Brown @*/ 5780910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex) 579316643e7SJed Brown { 5800910c330SBarry Smith PetscInt Ustate, Udotstate; 581316643e7SJed Brown PetscErrorCode ierr; 58224989b8cSPeter Brune TSIJacobian ijacobian; 58324989b8cSPeter Brune TSRHSJacobian rhsjacobian; 58424989b8cSPeter Brune DM dm; 58524989b8cSPeter Brune void *ctx; 586316643e7SJed Brown 587316643e7SJed Brown PetscFunctionBegin; 58824989b8cSPeter Brune 5890700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5900910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5910910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 592316643e7SJed Brown PetscValidPointer(A,6); 5930700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,6); 594316643e7SJed Brown PetscValidPointer(B,7); 5950700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,7); 596316643e7SJed Brown PetscValidPointer(flg,8); 59724989b8cSPeter Brune 59824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 59924989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 60024989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr); 60124989b8cSPeter Brune 6020910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr); 6030910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr); 6040910c330SBarry Smith if (ts->ijacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->ijacobian.X == U && ts->ijacobian.Xstate == Ustate && ts->ijacobian.Xdot == Udot && ts->ijacobian.Xdotstate == Udotstate && ts->ijacobian.imex == imex))) { 6050026cea9SSean Farley *flg = ts->ijacobian.mstructure; 6060026cea9SSean Farley ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 6070026cea9SSean Farley PetscFunctionReturn(0); 6080026cea9SSean Farley } 609316643e7SJed Brown 61024989b8cSPeter Brune if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 611316643e7SJed Brown 6124e684422SJed Brown *flg = SAME_NONZERO_PATTERN; /* In case we're solving a linear problem in which case it wouldn't get initialized below. */ 6130910c330SBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 61424989b8cSPeter Brune if (ijacobian) { 6152dd45cf8SJed Brown *flg = DIFFERENT_NONZERO_PATTERN; 616316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 6170910c330SBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr); 618316643e7SJed Brown PetscStackPop; 619214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 620214bc6a2SJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 621214bc6a2SJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 6224a6899ffSJed Brown } 623214bc6a2SJed Brown if (imex) { 624b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 625214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 6262dd45cf8SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 627214bc6a2SJed Brown if (*A != *B) { 628214bc6a2SJed Brown ierr = MatZeroEntries(*B);CHKERRQ(ierr); 629214bc6a2SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 630214bc6a2SJed Brown } 631214bc6a2SJed Brown *flg = SAME_PRECONDITIONER; 632214bc6a2SJed Brown } 633214bc6a2SJed Brown } else { 63424989b8cSPeter Brune if (!ijacobian) { 6350910c330SBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,A,B,flg);CHKERRQ(ierr); 636214bc6a2SJed Brown ierr = MatScale(*A,-1);CHKERRQ(ierr); 637214bc6a2SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 638316643e7SJed Brown if (*A != *B) { 639316643e7SJed Brown ierr = MatScale(*B,-1);CHKERRQ(ierr); 640316643e7SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 641316643e7SJed Brown } 64224989b8cSPeter Brune } else if (rhsjacobian) { 643214bc6a2SJed Brown Mat Arhs,Brhs; 644214bc6a2SJed Brown MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN; 645214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 6460910c330SBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 647214bc6a2SJed Brown axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN; 648214bc6a2SJed Brown ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr); 649214bc6a2SJed Brown if (*A != *B) { 650214bc6a2SJed Brown ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr); 651214bc6a2SJed Brown } 652214bc6a2SJed Brown *flg = PetscMin(*flg,flg2); 653214bc6a2SJed Brown } 654316643e7SJed Brown } 6550026cea9SSean Farley 6560026cea9SSean Farley ts->ijacobian.time = t; 6570910c330SBarry Smith ts->ijacobian.X = U; 6580910c330SBarry Smith ts->ijacobian.Xdot = Udot; 6590910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr); 6600910c330SBarry Smith ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr); 6610026cea9SSean Farley ts->ijacobian.shift = shift; 6620026cea9SSean Farley ts->ijacobian.imex = imex; 6630026cea9SSean Farley ts->ijacobian.mstructure = *flg; 6640910c330SBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr); 665316643e7SJed Brown PetscFunctionReturn(0); 666316643e7SJed Brown } 667316643e7SJed Brown 668316643e7SJed Brown #undef __FUNCT__ 6694a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 670d763cef2SBarry Smith /*@C 671d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 672b5abc632SBarry Smith where U_t = G(t,u). 673d763cef2SBarry Smith 6743f9fe445SBarry Smith Logically Collective on TS 675d763cef2SBarry Smith 676d763cef2SBarry Smith Input Parameters: 677d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 678ca94891dSJed Brown . r - vector to put the computed right hand side (or PETSC_NULL to have it created) 679d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 680d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 681d763cef2SBarry Smith function evaluation routine (may be PETSC_NULL) 682d763cef2SBarry Smith 683d763cef2SBarry Smith Calling sequence of func: 68487828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 685d763cef2SBarry Smith 686d763cef2SBarry Smith + t - current timestep 687d763cef2SBarry Smith . u - input vector 688d763cef2SBarry Smith . F - function vector 689d763cef2SBarry Smith - ctx - [optional] user-defined function context 690d763cef2SBarry Smith 691d763cef2SBarry Smith Level: beginner 692d763cef2SBarry Smith 693d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 694d763cef2SBarry Smith 695d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian() 696d763cef2SBarry Smith @*/ 697089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 698d763cef2SBarry Smith { 699089b2837SJed Brown PetscErrorCode ierr; 700089b2837SJed Brown SNES snes; 701e856ceecSJed Brown Vec ralloc = PETSC_NULL; 70224989b8cSPeter Brune DM dm; 703d763cef2SBarry Smith 704089b2837SJed Brown PetscFunctionBegin; 7050700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 706ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 70724989b8cSPeter Brune 70824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 70924989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 710089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 711e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 712e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 713e856ceecSJed Brown r = ralloc; 714e856ceecSJed Brown } 715089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 716e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 717d763cef2SBarry Smith PetscFunctionReturn(0); 718d763cef2SBarry Smith } 719d763cef2SBarry Smith 7204a2ae208SSatish Balay #undef __FUNCT__ 721ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 722ef20d060SBarry Smith /*@C 723abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 724ef20d060SBarry Smith 725ef20d060SBarry Smith Logically Collective on TS 726ef20d060SBarry Smith 727ef20d060SBarry Smith Input Parameters: 728ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 729ef20d060SBarry Smith . f - routine for evaluating the solution 730ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 731ef20d060SBarry Smith function evaluation routine (may be PETSC_NULL) 732ef20d060SBarry Smith 733ef20d060SBarry Smith Calling sequence of func: 734ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 735ef20d060SBarry Smith 736ef20d060SBarry Smith + t - current timestep 737ef20d060SBarry Smith . u - output vector 738ef20d060SBarry Smith - ctx - [optional] user-defined function context 739ef20d060SBarry Smith 740abd5a294SJed Brown Notes: 741abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 742abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 743abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 744abd5a294SJed Brown 7454f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 746abd5a294SJed Brown 747ef20d060SBarry Smith Level: beginner 748ef20d060SBarry Smith 749ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 750ef20d060SBarry Smith 751abd5a294SJed Brown .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction() 752ef20d060SBarry Smith @*/ 753ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 754ef20d060SBarry Smith { 755ef20d060SBarry Smith PetscErrorCode ierr; 756ef20d060SBarry Smith DM dm; 757ef20d060SBarry Smith 758ef20d060SBarry Smith PetscFunctionBegin; 759ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 760ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 761ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 762ef20d060SBarry Smith PetscFunctionReturn(0); 763ef20d060SBarry Smith } 764ef20d060SBarry Smith 765ef20d060SBarry Smith #undef __FUNCT__ 7664a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 767d763cef2SBarry Smith /*@C 768d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 769b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 770d763cef2SBarry Smith 7713f9fe445SBarry Smith Logically Collective on TS 772d763cef2SBarry Smith 773d763cef2SBarry Smith Input Parameters: 774d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 775d763cef2SBarry Smith . A - Jacobian matrix 776d763cef2SBarry Smith . B - preconditioner matrix (usually same as A) 777d763cef2SBarry Smith . f - the Jacobian evaluation routine 778d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 779d763cef2SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) 780d763cef2SBarry Smith 781d763cef2SBarry Smith Calling sequence of func: 78287828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 783d763cef2SBarry Smith 784d763cef2SBarry Smith + t - current timestep 785d763cef2SBarry Smith . u - input vector 786d763cef2SBarry Smith . A - matrix A, where U_t = A(t)u 787d763cef2SBarry Smith . B - preconditioner matrix, usually the same as A 788d763cef2SBarry Smith . flag - flag indicating information about the preconditioner matrix 78994b7f48cSBarry Smith structure (same as flag in KSPSetOperators()) 790d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 791d763cef2SBarry Smith 792d763cef2SBarry Smith Notes: 79394b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 794d763cef2SBarry Smith output parameter in the routine func(). Be sure to read this information! 795d763cef2SBarry Smith 796d763cef2SBarry Smith The routine func() takes Mat * as the matrix arguments rather than Mat. 797d763cef2SBarry Smith This allows the matrix evaluation routine to replace A and/or B with a 79856335db2SHong Zhang completely new matrix structure (not just different matrix elements) 799d763cef2SBarry Smith when appropriate, for instance, if the nonzero structure is changing 800d763cef2SBarry Smith throughout the global iterations. 801d763cef2SBarry Smith 802d763cef2SBarry Smith Level: beginner 803d763cef2SBarry Smith 804d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 805d763cef2SBarry Smith 806ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction() 807d763cef2SBarry Smith 808d763cef2SBarry Smith @*/ 809089b2837SJed Brown PetscErrorCode TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx) 810d763cef2SBarry Smith { 811277b19d0SLisandro Dalcin PetscErrorCode ierr; 812089b2837SJed Brown SNES snes; 81324989b8cSPeter Brune DM dm; 81424989b8cSPeter Brune TSIJacobian ijacobian; 815277b19d0SLisandro Dalcin 816d763cef2SBarry Smith PetscFunctionBegin; 8170700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 818277b19d0SLisandro Dalcin if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 819277b19d0SLisandro Dalcin if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 820277b19d0SLisandro Dalcin if (A) PetscCheckSameComm(ts,1,A,2); 821277b19d0SLisandro Dalcin if (B) PetscCheckSameComm(ts,1,B,3); 822d763cef2SBarry Smith 82324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 82424989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 82524989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr); 82624989b8cSPeter Brune 827089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 8285f659677SPeter Brune if (!ijacobian) { 829089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 8300e4ef248SJed Brown } 8310e4ef248SJed Brown if (A) { 8320e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 8330e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 8340e4ef248SJed Brown ts->Arhs = A; 8350e4ef248SJed Brown } 8360e4ef248SJed Brown if (B) { 8370e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 8380e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 8390e4ef248SJed Brown ts->Brhs = B; 8400e4ef248SJed Brown } 841d763cef2SBarry Smith PetscFunctionReturn(0); 842d763cef2SBarry Smith } 843d763cef2SBarry Smith 844316643e7SJed Brown 845316643e7SJed Brown #undef __FUNCT__ 846316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 847316643e7SJed Brown /*@C 848b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 849316643e7SJed Brown 8503f9fe445SBarry Smith Logically Collective on TS 851316643e7SJed Brown 852316643e7SJed Brown Input Parameters: 853316643e7SJed Brown + ts - the TS context obtained from TSCreate() 854ca94891dSJed Brown . r - vector to hold the residual (or PETSC_NULL to have it created internally) 855316643e7SJed Brown . f - the function evaluation routine 856316643e7SJed Brown - ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL) 857316643e7SJed Brown 858316643e7SJed Brown Calling sequence of f: 859316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 860316643e7SJed Brown 861316643e7SJed Brown + t - time at step/stage being solved 862316643e7SJed Brown . u - state vector 863316643e7SJed Brown . u_t - time derivative of state vector 864316643e7SJed Brown . F - function vector 865316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 866316643e7SJed Brown 867316643e7SJed Brown Important: 868d6cbdb99SBarry Smith The user MUST call either this routine, TSSetRHSFunction(). This routine must be used when not solving an ODE, for example a DAE. 869316643e7SJed Brown 870316643e7SJed Brown Level: beginner 871316643e7SJed Brown 872316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 873316643e7SJed Brown 874d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 875316643e7SJed Brown @*/ 876089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 877316643e7SJed Brown { 878089b2837SJed Brown PetscErrorCode ierr; 879089b2837SJed Brown SNES snes; 880e856ceecSJed Brown Vec resalloc = PETSC_NULL; 88124989b8cSPeter Brune DM dm; 882316643e7SJed Brown 883316643e7SJed Brown PetscFunctionBegin; 8840700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 885ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 88624989b8cSPeter Brune 88724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 88824989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 88924989b8cSPeter Brune 890089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 891e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 892e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 893e856ceecSJed Brown res = resalloc; 894e856ceecSJed Brown } 895089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 896e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 89724989b8cSPeter Brune 898089b2837SJed Brown PetscFunctionReturn(0); 899089b2837SJed Brown } 900089b2837SJed Brown 901089b2837SJed Brown #undef __FUNCT__ 902089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 903089b2837SJed Brown /*@C 904089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 905089b2837SJed Brown 906089b2837SJed Brown Not Collective 907089b2837SJed Brown 908089b2837SJed Brown Input Parameter: 909089b2837SJed Brown . ts - the TS context 910089b2837SJed Brown 911089b2837SJed Brown Output Parameter: 912089b2837SJed Brown + r - vector to hold residual (or PETSC_NULL) 913089b2837SJed Brown . func - the function to compute residual (or PETSC_NULL) 914089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 915089b2837SJed Brown 916089b2837SJed Brown Level: advanced 917089b2837SJed Brown 918089b2837SJed Brown .keywords: TS, nonlinear, get, function 919089b2837SJed Brown 920089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 921089b2837SJed Brown @*/ 922089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 923089b2837SJed Brown { 924089b2837SJed Brown PetscErrorCode ierr; 925089b2837SJed Brown SNES snes; 92624989b8cSPeter Brune DM dm; 927089b2837SJed Brown 928089b2837SJed Brown PetscFunctionBegin; 929089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 930089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 931089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 93224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 93324989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 934089b2837SJed Brown PetscFunctionReturn(0); 935089b2837SJed Brown } 936089b2837SJed Brown 937089b2837SJed Brown #undef __FUNCT__ 938089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 939089b2837SJed Brown /*@C 940089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 941089b2837SJed Brown 942089b2837SJed Brown Not Collective 943089b2837SJed Brown 944089b2837SJed Brown Input Parameter: 945089b2837SJed Brown . ts - the TS context 946089b2837SJed Brown 947089b2837SJed Brown Output Parameter: 948089b2837SJed Brown + r - vector to hold computed right hand side (or PETSC_NULL) 949089b2837SJed Brown . func - the function to compute right hand side (or PETSC_NULL) 950089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 951089b2837SJed Brown 952089b2837SJed Brown Level: advanced 953089b2837SJed Brown 954089b2837SJed Brown .keywords: TS, nonlinear, get, function 955089b2837SJed Brown 956089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 957089b2837SJed Brown @*/ 958089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 959089b2837SJed Brown { 960089b2837SJed Brown PetscErrorCode ierr; 961089b2837SJed Brown SNES snes; 96224989b8cSPeter Brune DM dm; 963089b2837SJed Brown 964089b2837SJed Brown PetscFunctionBegin; 965089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 966089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 967089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 96824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 96924989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 970316643e7SJed Brown PetscFunctionReturn(0); 971316643e7SJed Brown } 972316643e7SJed Brown 973316643e7SJed Brown #undef __FUNCT__ 974316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 975316643e7SJed Brown /*@C 976a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 977a4f0a591SBarry Smith you provided with TSSetIFunction(). 978316643e7SJed Brown 9793f9fe445SBarry Smith Logically Collective on TS 980316643e7SJed Brown 981316643e7SJed Brown Input Parameters: 982316643e7SJed Brown + ts - the TS context obtained from TSCreate() 983316643e7SJed Brown . A - Jacobian matrix 984316643e7SJed Brown . B - preconditioning matrix for A (may be same as A) 985316643e7SJed Brown . f - the Jacobian evaluation routine 986316643e7SJed Brown - ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL) 987316643e7SJed Brown 988316643e7SJed Brown Calling sequence of f: 9891b4a444bSJed Brown $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx); 990316643e7SJed Brown 991316643e7SJed Brown + t - time at step/stage being solved 9921b4a444bSJed Brown . U - state vector 9931b4a444bSJed Brown . U_t - time derivative of state vector 994316643e7SJed Brown . a - shift 995b5abc632SBarry Smith . A - Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 996316643e7SJed Brown . B - preconditioning matrix for A, may be same as A 997316643e7SJed Brown . flag - flag indicating information about the preconditioner matrix 998316643e7SJed Brown structure (same as flag in KSPSetOperators()) 999316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1000316643e7SJed Brown 1001316643e7SJed Brown Notes: 1002316643e7SJed Brown The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve. 1003316643e7SJed Brown 1004a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1005b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1006a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1007a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1008a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1009a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1010a4f0a591SBarry Smith 1011316643e7SJed Brown Level: beginner 1012316643e7SJed Brown 1013316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1014316643e7SJed Brown 1015ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian() 1016316643e7SJed Brown 1017316643e7SJed Brown @*/ 10187087cfbeSBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx) 1019316643e7SJed Brown { 1020316643e7SJed Brown PetscErrorCode ierr; 1021089b2837SJed Brown SNES snes; 102224989b8cSPeter Brune DM dm; 1023316643e7SJed Brown 1024316643e7SJed Brown PetscFunctionBegin; 10250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10260700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 10270700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 1028316643e7SJed Brown if (A) PetscCheckSameComm(ts,1,A,2); 1029316643e7SJed Brown if (B) PetscCheckSameComm(ts,1,B,3); 103024989b8cSPeter Brune 103124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 103224989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 103324989b8cSPeter Brune 1034089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1035089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1036316643e7SJed Brown PetscFunctionReturn(0); 1037316643e7SJed Brown } 1038316643e7SJed Brown 10394a2ae208SSatish Balay #undef __FUNCT__ 104055849f57SBarry Smith #define __FUNCT__ "TSLoad" 104155849f57SBarry Smith /*@C 104255849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 104355849f57SBarry Smith 104455849f57SBarry Smith Collective on PetscViewer 104555849f57SBarry Smith 104655849f57SBarry Smith Input Parameters: 104755849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 104855849f57SBarry Smith some related function before a call to TSLoad(). 104955849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 105055849f57SBarry Smith 105155849f57SBarry Smith Level: intermediate 105255849f57SBarry Smith 105355849f57SBarry Smith Notes: 105455849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 105555849f57SBarry Smith 105655849f57SBarry Smith Notes for advanced users: 105755849f57SBarry Smith Most users should not need to know the details of the binary storage 105855849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 105955849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 106055849f57SBarry Smith format is 106155849f57SBarry Smith .vb 106255849f57SBarry Smith has not yet been determined 106355849f57SBarry Smith .ve 106455849f57SBarry Smith 106555849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 106655849f57SBarry Smith @*/ 1067f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 106855849f57SBarry Smith { 106955849f57SBarry Smith PetscErrorCode ierr; 107055849f57SBarry Smith PetscBool isbinary; 107155849f57SBarry Smith PetscInt classid; 107255849f57SBarry Smith char type[256]; 10732d53ad75SBarry Smith DMTS sdm; 1074ad6bc421SBarry Smith DM dm; 107555849f57SBarry Smith 107655849f57SBarry Smith PetscFunctionBegin; 1077f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 107855849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 107955849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 108055849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 108155849f57SBarry Smith 108255849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 1083f2c2a1b9SBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Not TS next in file"); 108455849f57SBarry Smith ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 1085f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1086f2c2a1b9SBarry Smith if (ts->ops->load) { 1087f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1088f2c2a1b9SBarry Smith } 1089ad6bc421SBarry Smith ierr = DMCreate(((PetscObject)ts)->comm,&dm);CHKERRQ(ierr); 1090ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1091ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1092f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1093f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 10942d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 10952d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 109655849f57SBarry Smith PetscFunctionReturn(0); 109755849f57SBarry Smith } 109855849f57SBarry Smith 109955849f57SBarry Smith #undef __FUNCT__ 11004a2ae208SSatish Balay #define __FUNCT__ "TSView" 11017e2c5f70SBarry Smith /*@C 1102d763cef2SBarry Smith TSView - Prints the TS data structure. 1103d763cef2SBarry Smith 11044c49b128SBarry Smith Collective on TS 1105d763cef2SBarry Smith 1106d763cef2SBarry Smith Input Parameters: 1107d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1108d763cef2SBarry Smith - viewer - visualization context 1109d763cef2SBarry Smith 1110d763cef2SBarry Smith Options Database Key: 1111d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1112d763cef2SBarry Smith 1113d763cef2SBarry Smith Notes: 1114d763cef2SBarry Smith The available visualization contexts include 1115b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1116b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1117d763cef2SBarry Smith output where only the first processor opens 1118d763cef2SBarry Smith the file. All other processors send their 1119d763cef2SBarry Smith data to the first processor to print. 1120d763cef2SBarry Smith 1121d763cef2SBarry Smith The user can open an alternative visualization context with 1122b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1123d763cef2SBarry Smith 1124d763cef2SBarry Smith Level: beginner 1125d763cef2SBarry Smith 1126d763cef2SBarry Smith .keywords: TS, timestep, view 1127d763cef2SBarry Smith 1128b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1129d763cef2SBarry Smith @*/ 11307087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1131d763cef2SBarry Smith { 1132dfbe8321SBarry Smith PetscErrorCode ierr; 113319fd82e9SBarry Smith TSType type; 11342b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 11352d53ad75SBarry Smith DMTS sdm; 1136d763cef2SBarry Smith 1137d763cef2SBarry Smith PetscFunctionBegin; 11380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11393050cee2SBarry Smith if (!viewer) { 11407adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr); 11413050cee2SBarry Smith } 11420700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1143c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1144fd16b177SBarry Smith 1145251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1146251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 114755849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 11482b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 114932077d6dSBarry Smith if (iascii) { 1150317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr); 115177431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 1152a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%G\n",ts->max_time);CHKERRQ(ierr); 1153d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 11545ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1155c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1156d763cef2SBarry Smith } 11575ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1158193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 11592d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 11602d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1161d52bd9f3SBarry Smith if (ts->ops->view) { 1162d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1163d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1164d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1165d52bd9f3SBarry Smith } 11660f5bd95cSBarry Smith } else if (isstring) { 1167a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1168b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 116955849f57SBarry Smith } else if (isbinary) { 117055849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 117155849f57SBarry Smith MPI_Comm comm; 117255849f57SBarry Smith PetscMPIInt rank; 117355849f57SBarry Smith char type[256]; 117455849f57SBarry Smith 117555849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 117655849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 117755849f57SBarry Smith if (!rank) { 117855849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 117955849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 118055849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 118155849f57SBarry Smith } 118255849f57SBarry Smith if (ts->ops->view) { 118355849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 118455849f57SBarry Smith } 1185f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1186f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 11872d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 11882d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 11892b0a91c0SBarry Smith } else if (isdraw) { 11902b0a91c0SBarry Smith PetscDraw draw; 11912b0a91c0SBarry Smith char str[36]; 119289fd9fafSBarry Smith PetscReal x,y,bottom,h; 11932b0a91c0SBarry Smith 11942b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 11952b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 11962b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 11972b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 119889fd9fafSBarry Smith ierr = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,PETSC_NULL,&h);CHKERRQ(ierr); 119989fd9fafSBarry Smith bottom = y - h; 12002b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 12012b0a91c0SBarry Smith if (ts->ops->view) { 12022b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 12032b0a91c0SBarry Smith } 12042b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1205d763cef2SBarry Smith } 1206b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1207251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1208b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1209d763cef2SBarry Smith PetscFunctionReturn(0); 1210d763cef2SBarry Smith } 1211d763cef2SBarry Smith 1212d763cef2SBarry Smith 12134a2ae208SSatish Balay #undef __FUNCT__ 12144a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1215b07ff414SBarry Smith /*@ 1216d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1217d763cef2SBarry Smith the timesteppers. 1218d763cef2SBarry Smith 12193f9fe445SBarry Smith Logically Collective on TS 1220d763cef2SBarry Smith 1221d763cef2SBarry Smith Input Parameters: 1222d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1223d763cef2SBarry Smith - usrP - optional user context 1224d763cef2SBarry Smith 1225d763cef2SBarry Smith Level: intermediate 1226d763cef2SBarry Smith 1227d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1228d763cef2SBarry Smith 1229d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1230d763cef2SBarry Smith @*/ 12317087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1232d763cef2SBarry Smith { 1233d763cef2SBarry Smith PetscFunctionBegin; 12340700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1235d763cef2SBarry Smith ts->user = usrP; 1236d763cef2SBarry Smith PetscFunctionReturn(0); 1237d763cef2SBarry Smith } 1238d763cef2SBarry Smith 12394a2ae208SSatish Balay #undef __FUNCT__ 12404a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1241b07ff414SBarry Smith /*@ 1242d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1243d763cef2SBarry Smith timestepper. 1244d763cef2SBarry Smith 1245d763cef2SBarry Smith Not Collective 1246d763cef2SBarry Smith 1247d763cef2SBarry Smith Input Parameter: 1248d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1249d763cef2SBarry Smith 1250d763cef2SBarry Smith Output Parameter: 1251d763cef2SBarry Smith . usrP - user context 1252d763cef2SBarry Smith 1253d763cef2SBarry Smith Level: intermediate 1254d763cef2SBarry Smith 1255d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1256d763cef2SBarry Smith 1257d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1258d763cef2SBarry Smith @*/ 1259e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1260d763cef2SBarry Smith { 1261d763cef2SBarry Smith PetscFunctionBegin; 12620700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1263e71120c6SJed Brown *(void**)usrP = ts->user; 1264d763cef2SBarry Smith PetscFunctionReturn(0); 1265d763cef2SBarry Smith } 1266d763cef2SBarry Smith 12674a2ae208SSatish Balay #undef __FUNCT__ 12684a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1269d763cef2SBarry Smith /*@ 1270b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1271d763cef2SBarry Smith 1272d763cef2SBarry Smith Not Collective 1273d763cef2SBarry Smith 1274d763cef2SBarry Smith Input Parameter: 1275d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1276d763cef2SBarry Smith 1277d763cef2SBarry Smith Output Parameter: 1278b8123daeSJed Brown . iter - number of steps completed so far 1279d763cef2SBarry Smith 1280d763cef2SBarry Smith Level: intermediate 1281d763cef2SBarry Smith 1282d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 1283b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep() 1284d763cef2SBarry Smith @*/ 12857087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt* iter) 1286d763cef2SBarry Smith { 1287d763cef2SBarry Smith PetscFunctionBegin; 12880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 12894482741eSBarry Smith PetscValidIntPointer(iter,2); 1290d763cef2SBarry Smith *iter = ts->steps; 1291d763cef2SBarry Smith PetscFunctionReturn(0); 1292d763cef2SBarry Smith } 1293d763cef2SBarry Smith 12944a2ae208SSatish Balay #undef __FUNCT__ 12954a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1296d763cef2SBarry Smith /*@ 1297d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1298d763cef2SBarry Smith as well as the initial time. 1299d763cef2SBarry Smith 13003f9fe445SBarry Smith Logically Collective on TS 1301d763cef2SBarry Smith 1302d763cef2SBarry Smith Input Parameters: 1303d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1304d763cef2SBarry Smith . initial_time - the initial time 1305d763cef2SBarry Smith - time_step - the size of the timestep 1306d763cef2SBarry Smith 1307d763cef2SBarry Smith Level: intermediate 1308d763cef2SBarry Smith 1309d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1310d763cef2SBarry Smith 1311d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1312d763cef2SBarry Smith @*/ 13137087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1314d763cef2SBarry Smith { 1315e144a568SJed Brown PetscErrorCode ierr; 1316e144a568SJed Brown 1317d763cef2SBarry Smith PetscFunctionBegin; 13180700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1319e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1320d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1321d763cef2SBarry Smith PetscFunctionReturn(0); 1322d763cef2SBarry Smith } 1323d763cef2SBarry Smith 13244a2ae208SSatish Balay #undef __FUNCT__ 13254a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1326d763cef2SBarry Smith /*@ 1327d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1328d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1329d763cef2SBarry Smith 13303f9fe445SBarry Smith Logically Collective on TS 1331d763cef2SBarry Smith 1332d763cef2SBarry Smith Input Parameters: 1333d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1334d763cef2SBarry Smith - time_step - the size of the timestep 1335d763cef2SBarry Smith 1336d763cef2SBarry Smith Level: intermediate 1337d763cef2SBarry Smith 1338d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1339d763cef2SBarry Smith 1340d763cef2SBarry Smith .keywords: TS, set, timestep 1341d763cef2SBarry Smith @*/ 13427087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1343d763cef2SBarry Smith { 1344d763cef2SBarry Smith PetscFunctionBegin; 13450700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1346c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1347d763cef2SBarry Smith ts->time_step = time_step; 134831748224SBarry Smith ts->time_step_orig = time_step; 1349d763cef2SBarry Smith PetscFunctionReturn(0); 1350d763cef2SBarry Smith } 1351d763cef2SBarry Smith 13524a2ae208SSatish Balay #undef __FUNCT__ 1353a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1354a43b19c4SJed Brown /*@ 135549354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 135649354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 135749354f04SShri Abhyankar or just return at the final time TS computed. 1358a43b19c4SJed Brown 1359a43b19c4SJed Brown Logically Collective on TS 1360a43b19c4SJed Brown 1361a43b19c4SJed Brown Input Parameter: 1362a43b19c4SJed Brown + ts - the time-step context 136349354f04SShri Abhyankar - eftopt - exact final time option 1364a43b19c4SJed Brown 1365a43b19c4SJed Brown Level: beginner 1366a43b19c4SJed Brown 136749354f04SShri Abhyankar .seealso: TSExactFinalTime 1368a43b19c4SJed Brown @*/ 136949354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 1370a43b19c4SJed Brown { 1371a43b19c4SJed Brown PetscFunctionBegin; 1372a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 137349354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 137449354f04SShri Abhyankar ts->exact_final_time = eftopt; 1375a43b19c4SJed Brown PetscFunctionReturn(0); 1376a43b19c4SJed Brown } 1377a43b19c4SJed Brown 1378a43b19c4SJed Brown #undef __FUNCT__ 13794a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1380d763cef2SBarry Smith /*@ 1381d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1382d763cef2SBarry Smith 1383d763cef2SBarry Smith Not Collective 1384d763cef2SBarry Smith 1385d763cef2SBarry Smith Input Parameter: 1386d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1387d763cef2SBarry Smith 1388d763cef2SBarry Smith Output Parameter: 1389d763cef2SBarry Smith . dt - the current timestep size 1390d763cef2SBarry Smith 1391d763cef2SBarry Smith Level: intermediate 1392d763cef2SBarry Smith 1393d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1394d763cef2SBarry Smith 1395d763cef2SBarry Smith .keywords: TS, get, timestep 1396d763cef2SBarry Smith @*/ 13977087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal* dt) 1398d763cef2SBarry Smith { 1399d763cef2SBarry Smith PetscFunctionBegin; 14000700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1401f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1402d763cef2SBarry Smith *dt = ts->time_step; 1403d763cef2SBarry Smith PetscFunctionReturn(0); 1404d763cef2SBarry Smith } 1405d763cef2SBarry Smith 14064a2ae208SSatish Balay #undef __FUNCT__ 14074a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1408d8e5e3e6SSatish Balay /*@ 1409d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1410d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1411d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1412d763cef2SBarry Smith the solution at the next timestep has been calculated. 1413d763cef2SBarry Smith 1414d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1415d763cef2SBarry Smith 1416d763cef2SBarry Smith Input Parameter: 1417d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1418d763cef2SBarry Smith 1419d763cef2SBarry Smith Output Parameter: 1420d763cef2SBarry Smith . v - the vector containing the solution 1421d763cef2SBarry Smith 1422d763cef2SBarry Smith Level: intermediate 1423d763cef2SBarry Smith 1424d763cef2SBarry Smith .seealso: TSGetTimeStep() 1425d763cef2SBarry Smith 1426d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1427d763cef2SBarry Smith @*/ 14287087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1429d763cef2SBarry Smith { 1430d763cef2SBarry Smith PetscFunctionBegin; 14310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14324482741eSBarry Smith PetscValidPointer(v,2); 14338737fe31SLisandro Dalcin *v = ts->vec_sol; 1434d763cef2SBarry Smith PetscFunctionReturn(0); 1435d763cef2SBarry Smith } 1436d763cef2SBarry Smith 1437bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 14384a2ae208SSatish Balay #undef __FUNCT__ 1439bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1440d8e5e3e6SSatish Balay /*@ 1441bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1442d763cef2SBarry Smith 1443bdad233fSMatthew Knepley Not collective 1444d763cef2SBarry Smith 1445bdad233fSMatthew Knepley Input Parameters: 1446bdad233fSMatthew Knepley + ts - The TS 1447bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1448d763cef2SBarry Smith .vb 14490910c330SBarry Smith U_t - A U = 0 (linear) 14500910c330SBarry Smith U_t - A(t) U = 0 (linear) 14510910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 1452d763cef2SBarry Smith .ve 1453d763cef2SBarry Smith 1454d763cef2SBarry Smith Level: beginner 1455d763cef2SBarry Smith 1456bdad233fSMatthew Knepley .keywords: TS, problem type 1457bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1458d763cef2SBarry Smith @*/ 14597087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1460a7cc72afSBarry Smith { 14619e2a6581SJed Brown PetscErrorCode ierr; 14629e2a6581SJed Brown 1463d763cef2SBarry Smith PetscFunctionBegin; 14640700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1465bdad233fSMatthew Knepley ts->problem_type = type; 14669e2a6581SJed Brown if (type == TS_LINEAR) { 14679e2a6581SJed Brown SNES snes; 14689e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 14699e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 14709e2a6581SJed Brown } 1471d763cef2SBarry Smith PetscFunctionReturn(0); 1472d763cef2SBarry Smith } 1473d763cef2SBarry Smith 1474bdad233fSMatthew Knepley #undef __FUNCT__ 1475bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1476bdad233fSMatthew Knepley /*@C 1477bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1478bdad233fSMatthew Knepley 1479bdad233fSMatthew Knepley Not collective 1480bdad233fSMatthew Knepley 1481bdad233fSMatthew Knepley Input Parameter: 1482bdad233fSMatthew Knepley . ts - The TS 1483bdad233fSMatthew Knepley 1484bdad233fSMatthew Knepley Output Parameter: 1485bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1486bdad233fSMatthew Knepley .vb 1487089b2837SJed Brown M U_t = A U 1488089b2837SJed Brown M(t) U_t = A(t) U 1489b5abc632SBarry Smith F(t,U,U_t) 1490bdad233fSMatthew Knepley .ve 1491bdad233fSMatthew Knepley 1492bdad233fSMatthew Knepley Level: beginner 1493bdad233fSMatthew Knepley 1494bdad233fSMatthew Knepley .keywords: TS, problem type 1495bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1496bdad233fSMatthew Knepley @*/ 14977087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1498a7cc72afSBarry Smith { 1499bdad233fSMatthew Knepley PetscFunctionBegin; 15000700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 15014482741eSBarry Smith PetscValidIntPointer(type,2); 1502bdad233fSMatthew Knepley *type = ts->problem_type; 1503bdad233fSMatthew Knepley PetscFunctionReturn(0); 1504bdad233fSMatthew Knepley } 1505d763cef2SBarry Smith 15064a2ae208SSatish Balay #undef __FUNCT__ 15074a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1508d763cef2SBarry Smith /*@ 1509d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1510d763cef2SBarry Smith of a timestepper. 1511d763cef2SBarry Smith 1512d763cef2SBarry Smith Collective on TS 1513d763cef2SBarry Smith 1514d763cef2SBarry Smith Input Parameter: 1515d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1516d763cef2SBarry Smith 1517d763cef2SBarry Smith Notes: 1518d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1519d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1520d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1521d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1522d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1523d763cef2SBarry Smith 1524d763cef2SBarry Smith Level: advanced 1525d763cef2SBarry Smith 1526d763cef2SBarry Smith .keywords: TS, timestep, setup 1527d763cef2SBarry Smith 1528d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1529d763cef2SBarry Smith @*/ 15307087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1531d763cef2SBarry Smith { 1532dfbe8321SBarry Smith PetscErrorCode ierr; 15336c6b9e74SPeter Brune DM dm; 15346c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 15356c6b9e74SPeter Brune PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*); 15366c6b9e74SPeter Brune TSIJacobian ijac; 15376c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1538d763cef2SBarry Smith 1539d763cef2SBarry Smith PetscFunctionBegin; 15400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1541277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1542277b19d0SLisandro Dalcin 15437adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 15449596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1545d763cef2SBarry Smith } 1546277b19d0SLisandro Dalcin 1547277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1548277b19d0SLisandro Dalcin 1549ad6bc421SBarry Smith ierr = TSGetTSAdapt(ts,&ts->adapt);CHKERRQ(ierr); 15501c3436cfSJed Brown 1551277b19d0SLisandro Dalcin if (ts->ops->setup) { 1552000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1553277b19d0SLisandro Dalcin } 1554277b19d0SLisandro Dalcin 15556c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 15566c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 15576c6b9e74SPeter Brune */ 15586c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 15596c6b9e74SPeter Brune ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr); 15606c6b9e74SPeter Brune if (!func) { 15616c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 15626c6b9e74SPeter Brune } 15636c6b9e74SPeter 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. 15646c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 15656c6b9e74SPeter Brune */ 15666c6b9e74SPeter Brune ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr); 15676c6b9e74SPeter Brune ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr); 15686c6b9e74SPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr); 15696c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 15706c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 15716c6b9e74SPeter Brune } 1572277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1573277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1574277b19d0SLisandro Dalcin } 1575277b19d0SLisandro Dalcin 1576277b19d0SLisandro Dalcin #undef __FUNCT__ 1577277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1578277b19d0SLisandro Dalcin /*@ 1579277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1580277b19d0SLisandro Dalcin 1581277b19d0SLisandro Dalcin Collective on TS 1582277b19d0SLisandro Dalcin 1583277b19d0SLisandro Dalcin Input Parameter: 1584277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1585277b19d0SLisandro Dalcin 1586277b19d0SLisandro Dalcin Level: beginner 1587277b19d0SLisandro Dalcin 1588277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1589277b19d0SLisandro Dalcin 1590277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1591277b19d0SLisandro Dalcin @*/ 1592277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1593277b19d0SLisandro Dalcin { 1594277b19d0SLisandro Dalcin PetscErrorCode ierr; 1595277b19d0SLisandro Dalcin 1596277b19d0SLisandro Dalcin PetscFunctionBegin; 1597277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1598277b19d0SLisandro Dalcin if (ts->ops->reset) { 1599277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1600277b19d0SLisandro Dalcin } 1601277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 16024e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 16034e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1604214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 16056bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1606e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 1607e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 160838637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 1609277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1610d763cef2SBarry Smith PetscFunctionReturn(0); 1611d763cef2SBarry Smith } 1612d763cef2SBarry Smith 16134a2ae208SSatish Balay #undef __FUNCT__ 16144a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1615d8e5e3e6SSatish Balay /*@ 1616d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1617d763cef2SBarry Smith with TSCreate(). 1618d763cef2SBarry Smith 1619d763cef2SBarry Smith Collective on TS 1620d763cef2SBarry Smith 1621d763cef2SBarry Smith Input Parameter: 1622d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1623d763cef2SBarry Smith 1624d763cef2SBarry Smith Level: beginner 1625d763cef2SBarry Smith 1626d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1627d763cef2SBarry Smith 1628d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1629d763cef2SBarry Smith @*/ 16306bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1631d763cef2SBarry Smith { 16326849ba73SBarry Smith PetscErrorCode ierr; 1633d763cef2SBarry Smith 1634d763cef2SBarry Smith PetscFunctionBegin; 16356bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 16366bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 16376bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1638d763cef2SBarry Smith 16396bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1640277b19d0SLisandro Dalcin 1641be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 16426bf464f9SBarry Smith ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 16436bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 16446d4c513bSLisandro Dalcin 164584df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 16466bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 16476bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 16486bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 16496d4c513bSLisandro Dalcin 1650a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1651d763cef2SBarry Smith PetscFunctionReturn(0); 1652d763cef2SBarry Smith } 1653d763cef2SBarry Smith 16544a2ae208SSatish Balay #undef __FUNCT__ 16554a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1656d8e5e3e6SSatish Balay /*@ 1657d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1658d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1659d763cef2SBarry Smith 1660d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1661d763cef2SBarry Smith 1662d763cef2SBarry Smith Input Parameter: 1663d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1664d763cef2SBarry Smith 1665d763cef2SBarry Smith Output Parameter: 1666d763cef2SBarry Smith . snes - the nonlinear solver context 1667d763cef2SBarry Smith 1668d763cef2SBarry Smith Notes: 1669d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1670d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 167194b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1672d763cef2SBarry Smith 1673d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 1674d763cef2SBarry Smith this case TSGetSNES() returns PETSC_NULL in snes. 1675d763cef2SBarry Smith 1676d763cef2SBarry Smith Level: beginner 1677d763cef2SBarry Smith 1678d763cef2SBarry Smith .keywords: timestep, get, SNES 1679d763cef2SBarry Smith @*/ 16807087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1681d763cef2SBarry Smith { 1682d372ba47SLisandro Dalcin PetscErrorCode ierr; 1683d372ba47SLisandro Dalcin 1684d763cef2SBarry Smith PetscFunctionBegin; 16850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 16864482741eSBarry Smith PetscValidPointer(snes,2); 1687d372ba47SLisandro Dalcin if (!ts->snes) { 1688d372ba47SLisandro Dalcin ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 16896c6b9e74SPeter Brune ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 1690d372ba47SLisandro Dalcin ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1691d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 1692496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 16939e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 16949e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 16959e2a6581SJed Brown } 1696d372ba47SLisandro Dalcin } 1697d763cef2SBarry Smith *snes = ts->snes; 1698d763cef2SBarry Smith PetscFunctionReturn(0); 1699d763cef2SBarry Smith } 1700d763cef2SBarry Smith 17014a2ae208SSatish Balay #undef __FUNCT__ 170294b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 1703d8e5e3e6SSatish Balay /*@ 170494b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 1705d763cef2SBarry Smith a TS (timestepper) context. 1706d763cef2SBarry Smith 170794b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 1708d763cef2SBarry Smith 1709d763cef2SBarry Smith Input Parameter: 1710d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1711d763cef2SBarry Smith 1712d763cef2SBarry Smith Output Parameter: 171394b7f48cSBarry Smith . ksp - the nonlinear solver context 1714d763cef2SBarry Smith 1715d763cef2SBarry Smith Notes: 171694b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 1717d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 1718d763cef2SBarry Smith KSP and PC contexts as well. 1719d763cef2SBarry Smith 172094b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 172194b7f48cSBarry Smith in this case TSGetKSP() returns PETSC_NULL in ksp. 1722d763cef2SBarry Smith 1723d763cef2SBarry Smith Level: beginner 1724d763cef2SBarry Smith 172594b7f48cSBarry Smith .keywords: timestep, get, KSP 1726d763cef2SBarry Smith @*/ 17277087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1728d763cef2SBarry Smith { 1729d372ba47SLisandro Dalcin PetscErrorCode ierr; 1730089b2837SJed Brown SNES snes; 1731d372ba47SLisandro Dalcin 1732d763cef2SBarry Smith PetscFunctionBegin; 17330700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17344482741eSBarry Smith PetscValidPointer(ksp,2); 173517186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1736e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1737089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1738089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1739d763cef2SBarry Smith PetscFunctionReturn(0); 1740d763cef2SBarry Smith } 1741d763cef2SBarry Smith 1742d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 1743d763cef2SBarry Smith 17444a2ae208SSatish Balay #undef __FUNCT__ 1745adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 1746adb62b0dSMatthew Knepley /*@ 1747adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 1748adb62b0dSMatthew Knepley maximum time for iteration. 1749adb62b0dSMatthew Knepley 17503f9fe445SBarry Smith Not Collective 1751adb62b0dSMatthew Knepley 1752adb62b0dSMatthew Knepley Input Parameters: 1753adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 1754adb62b0dSMatthew Knepley . maxsteps - maximum number of iterations to use, or PETSC_NULL 1755adb62b0dSMatthew Knepley - maxtime - final time to iterate to, or PETSC_NULL 1756adb62b0dSMatthew Knepley 1757adb62b0dSMatthew Knepley Level: intermediate 1758adb62b0dSMatthew Knepley 1759adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 1760adb62b0dSMatthew Knepley @*/ 17617087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1762adb62b0dSMatthew Knepley { 1763adb62b0dSMatthew Knepley PetscFunctionBegin; 17640700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1765abc0a331SBarry Smith if (maxsteps) { 17664482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 1767adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 1768adb62b0dSMatthew Knepley } 1769abc0a331SBarry Smith if (maxtime) { 17704482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 1771adb62b0dSMatthew Knepley *maxtime = ts->max_time; 1772adb62b0dSMatthew Knepley } 1773adb62b0dSMatthew Knepley PetscFunctionReturn(0); 1774adb62b0dSMatthew Knepley } 1775adb62b0dSMatthew Knepley 1776adb62b0dSMatthew Knepley #undef __FUNCT__ 17774a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 1778d763cef2SBarry Smith /*@ 1779d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 1780d763cef2SBarry Smith maximum time for iteration. 1781d763cef2SBarry Smith 17823f9fe445SBarry Smith Logically Collective on TS 1783d763cef2SBarry Smith 1784d763cef2SBarry Smith Input Parameters: 1785d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1786d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 1787d763cef2SBarry Smith - maxtime - final time to iterate to 1788d763cef2SBarry Smith 1789d763cef2SBarry Smith Options Database Keys: 1790d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 17913bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 1792d763cef2SBarry Smith 1793d763cef2SBarry Smith Notes: 1794d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 1795d763cef2SBarry Smith 1796d763cef2SBarry Smith Level: intermediate 1797d763cef2SBarry Smith 1798d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 1799a43b19c4SJed Brown 1800a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 1801d763cef2SBarry Smith @*/ 18027087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1803d763cef2SBarry Smith { 1804d763cef2SBarry Smith PetscFunctionBegin; 18050700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1806c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1807c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 180839b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 180939b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 1810d763cef2SBarry Smith PetscFunctionReturn(0); 1811d763cef2SBarry Smith } 1812d763cef2SBarry Smith 18134a2ae208SSatish Balay #undef __FUNCT__ 18144a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 1815d763cef2SBarry Smith /*@ 1816d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 1817d763cef2SBarry Smith for use by the TS routines. 1818d763cef2SBarry Smith 18193f9fe445SBarry Smith Logically Collective on TS and Vec 1820d763cef2SBarry Smith 1821d763cef2SBarry Smith Input Parameters: 1822d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 18230910c330SBarry Smith - u - the solution vector 1824d763cef2SBarry Smith 1825d763cef2SBarry Smith Level: beginner 1826d763cef2SBarry Smith 1827d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 1828d763cef2SBarry Smith @*/ 18290910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 1830d763cef2SBarry Smith { 18318737fe31SLisandro Dalcin PetscErrorCode ierr; 1832496e6a7aSJed Brown DM dm; 18338737fe31SLisandro Dalcin 1834d763cef2SBarry Smith PetscFunctionBegin; 18350700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18360910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 18370910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 18386bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 18390910c330SBarry Smith ts->vec_sol = u; 1840496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 18410910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 1842d763cef2SBarry Smith PetscFunctionReturn(0); 1843d763cef2SBarry Smith } 1844d763cef2SBarry Smith 1845e74ef692SMatthew Knepley #undef __FUNCT__ 1846e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 1847ac226902SBarry Smith /*@C 1848000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 18493f2090d5SJed Brown called once at the beginning of each time step. 1850000e7ae3SMatthew Knepley 18513f9fe445SBarry Smith Logically Collective on TS 1852000e7ae3SMatthew Knepley 1853000e7ae3SMatthew Knepley Input Parameters: 1854000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1855000e7ae3SMatthew Knepley - func - The function 1856000e7ae3SMatthew Knepley 1857000e7ae3SMatthew Knepley Calling sequence of func: 1858000e7ae3SMatthew Knepley . func (TS ts); 1859000e7ae3SMatthew Knepley 1860000e7ae3SMatthew Knepley Level: intermediate 1861000e7ae3SMatthew Knepley 1862b8123daeSJed Brown Note: 1863b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 1864b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 1865b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 1866b8123daeSJed Brown 1867000e7ae3SMatthew Knepley .keywords: TS, timestep 1868b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep() 1869000e7ae3SMatthew Knepley @*/ 18707087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1871000e7ae3SMatthew Knepley { 1872000e7ae3SMatthew Knepley PetscFunctionBegin; 18730700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1874000e7ae3SMatthew Knepley ts->ops->prestep = func; 1875000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1876000e7ae3SMatthew Knepley } 1877000e7ae3SMatthew Knepley 1878e74ef692SMatthew Knepley #undef __FUNCT__ 18793f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 188009ee8438SJed Brown /*@ 18813f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 18823f2090d5SJed Brown 18833f2090d5SJed Brown Collective on TS 18843f2090d5SJed Brown 18853f2090d5SJed Brown Input Parameters: 18863f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 18873f2090d5SJed Brown 18883f2090d5SJed Brown Notes: 18893f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 18903f2090d5SJed Brown so most users would not generally call this routine themselves. 18913f2090d5SJed Brown 18923f2090d5SJed Brown Level: developer 18933f2090d5SJed Brown 18943f2090d5SJed Brown .keywords: TS, timestep 1895b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep() 18963f2090d5SJed Brown @*/ 18977087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 18983f2090d5SJed Brown { 18993f2090d5SJed Brown PetscErrorCode ierr; 19003f2090d5SJed Brown 19013f2090d5SJed Brown PetscFunctionBegin; 19020700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 190372ac3e02SJed Brown if (ts->ops->prestep) { 19043f2090d5SJed Brown PetscStackPush("TS PreStep function"); 19053f2090d5SJed Brown ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 19063f2090d5SJed Brown PetscStackPop; 1907312ce896SJed Brown } 19083f2090d5SJed Brown PetscFunctionReturn(0); 19093f2090d5SJed Brown } 19103f2090d5SJed Brown 19113f2090d5SJed Brown #undef __FUNCT__ 1912b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 1913b8123daeSJed Brown /*@C 1914b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 1915b8123daeSJed Brown called once at the beginning of each stage. 1916b8123daeSJed Brown 1917b8123daeSJed Brown Logically Collective on TS 1918b8123daeSJed Brown 1919b8123daeSJed Brown Input Parameters: 1920b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 1921b8123daeSJed Brown - func - The function 1922b8123daeSJed Brown 1923b8123daeSJed Brown Calling sequence of func: 1924b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 1925b8123daeSJed Brown 1926b8123daeSJed Brown Level: intermediate 1927b8123daeSJed Brown 1928b8123daeSJed Brown Note: 1929b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 1930b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 1931b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 1932b8123daeSJed Brown 1933b8123daeSJed Brown .keywords: TS, timestep 1934b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 1935b8123daeSJed Brown @*/ 1936b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 1937b8123daeSJed Brown { 1938b8123daeSJed Brown PetscFunctionBegin; 1939b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1940b8123daeSJed Brown ts->ops->prestage = func; 1941b8123daeSJed Brown PetscFunctionReturn(0); 1942b8123daeSJed Brown } 1943b8123daeSJed Brown 1944b8123daeSJed Brown #undef __FUNCT__ 1945b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 1946b8123daeSJed Brown /*@ 1947b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 1948b8123daeSJed Brown 1949b8123daeSJed Brown Collective on TS 1950b8123daeSJed Brown 1951b8123daeSJed Brown Input Parameters: 1952b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 1953b8123daeSJed Brown 1954b8123daeSJed Brown Notes: 1955b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 1956b8123daeSJed Brown most users would not generally call this routine themselves. 1957b8123daeSJed Brown 1958b8123daeSJed Brown Level: developer 1959b8123daeSJed Brown 1960b8123daeSJed Brown .keywords: TS, timestep 1961b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep() 1962b8123daeSJed Brown @*/ 1963b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 1964b8123daeSJed Brown { 1965b8123daeSJed Brown PetscErrorCode ierr; 1966b8123daeSJed Brown 1967b8123daeSJed Brown PetscFunctionBegin; 1968b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1969b8123daeSJed Brown if (ts->ops->prestage) { 1970b8123daeSJed Brown PetscStackPush("TS PreStage function"); 1971b8123daeSJed Brown ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr); 1972b8123daeSJed Brown PetscStackPop; 1973b8123daeSJed Brown } 1974b8123daeSJed Brown PetscFunctionReturn(0); 1975b8123daeSJed Brown } 1976b8123daeSJed Brown 1977b8123daeSJed Brown #undef __FUNCT__ 1978e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 1979ac226902SBarry Smith /*@C 1980000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 19813f2090d5SJed Brown called once at the end of each time step. 1982000e7ae3SMatthew Knepley 19833f9fe445SBarry Smith Logically Collective on TS 1984000e7ae3SMatthew Knepley 1985000e7ae3SMatthew Knepley Input Parameters: 1986000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1987000e7ae3SMatthew Knepley - func - The function 1988000e7ae3SMatthew Knepley 1989000e7ae3SMatthew Knepley Calling sequence of func: 1990b8123daeSJed Brown $ func (TS ts); 1991000e7ae3SMatthew Knepley 1992000e7ae3SMatthew Knepley Level: intermediate 1993000e7ae3SMatthew Knepley 1994000e7ae3SMatthew Knepley .keywords: TS, timestep 1995b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 1996000e7ae3SMatthew Knepley @*/ 19977087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 1998000e7ae3SMatthew Knepley { 1999000e7ae3SMatthew Knepley PetscFunctionBegin; 20000700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2001000e7ae3SMatthew Knepley ts->ops->poststep = func; 2002000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2003000e7ae3SMatthew Knepley } 2004000e7ae3SMatthew Knepley 2005e74ef692SMatthew Knepley #undef __FUNCT__ 20063f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 200709ee8438SJed Brown /*@ 20083f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 20093f2090d5SJed Brown 20103f2090d5SJed Brown Collective on TS 20113f2090d5SJed Brown 20123f2090d5SJed Brown Input Parameters: 20133f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 20143f2090d5SJed Brown 20153f2090d5SJed Brown Notes: 20163f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 20173f2090d5SJed Brown so most users would not generally call this routine themselves. 20183f2090d5SJed Brown 20193f2090d5SJed Brown Level: developer 20203f2090d5SJed Brown 20213f2090d5SJed Brown .keywords: TS, timestep 20223f2090d5SJed Brown @*/ 20237087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 20243f2090d5SJed Brown { 20253f2090d5SJed Brown PetscErrorCode ierr; 20263f2090d5SJed Brown 20273f2090d5SJed Brown PetscFunctionBegin; 20280700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 202972ac3e02SJed Brown if (ts->ops->poststep) { 20303f2090d5SJed Brown PetscStackPush("TS PostStep function"); 20313f2090d5SJed Brown ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 20323f2090d5SJed Brown PetscStackPop; 203372ac3e02SJed Brown } 20343f2090d5SJed Brown PetscFunctionReturn(0); 20353f2090d5SJed Brown } 20363f2090d5SJed Brown 2037d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 2038d763cef2SBarry Smith 20394a2ae208SSatish Balay #undef __FUNCT__ 2040a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 2041d763cef2SBarry Smith /*@C 2042a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 2043d763cef2SBarry Smith timestep to display the iteration's progress. 2044d763cef2SBarry Smith 20453f9fe445SBarry Smith Logically Collective on TS 2046d763cef2SBarry Smith 2047d763cef2SBarry Smith Input Parameters: 2048d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2049e213d8f1SJed Brown . monitor - monitoring routine 2050329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 2051b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desired) 2052b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 2053b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 2054d763cef2SBarry Smith 2055e213d8f1SJed Brown Calling sequence of monitor: 20560910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 2057d763cef2SBarry Smith 2058d763cef2SBarry Smith + ts - the TS context 205988c05cc5SBarry 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 206088c05cc5SBarry Smith been interpolated to) 20611f06c33eSBarry Smith . time - current time 20620910c330SBarry Smith . u - current iterate 2063d763cef2SBarry Smith - mctx - [optional] monitoring context 2064d763cef2SBarry Smith 2065d763cef2SBarry Smith Notes: 2066d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 2067d763cef2SBarry Smith already has been loaded. 2068d763cef2SBarry Smith 2069025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 2070025f1a04SBarry Smith 2071d763cef2SBarry Smith Level: intermediate 2072d763cef2SBarry Smith 2073d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2074d763cef2SBarry Smith 2075a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 2076d763cef2SBarry Smith @*/ 2077c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 2078d763cef2SBarry Smith { 2079d763cef2SBarry Smith PetscFunctionBegin; 20800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 208117186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2082d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 20838704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 2084d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 2085d763cef2SBarry Smith PetscFunctionReturn(0); 2086d763cef2SBarry Smith } 2087d763cef2SBarry Smith 20884a2ae208SSatish Balay #undef __FUNCT__ 2089a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 2090d763cef2SBarry Smith /*@C 2091a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 2092d763cef2SBarry Smith 20933f9fe445SBarry Smith Logically Collective on TS 2094d763cef2SBarry Smith 2095d763cef2SBarry Smith Input Parameters: 2096d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2097d763cef2SBarry Smith 2098d763cef2SBarry Smith Notes: 2099d763cef2SBarry Smith There is no way to remove a single, specific monitor. 2100d763cef2SBarry Smith 2101d763cef2SBarry Smith Level: intermediate 2102d763cef2SBarry Smith 2103d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2104d763cef2SBarry Smith 2105a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 2106d763cef2SBarry Smith @*/ 21077087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 2108d763cef2SBarry Smith { 2109d952e501SBarry Smith PetscErrorCode ierr; 2110d952e501SBarry Smith PetscInt i; 2111d952e501SBarry Smith 2112d763cef2SBarry Smith PetscFunctionBegin; 21130700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2114d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 21158704b422SBarry Smith if (ts->monitordestroy[i]) { 21168704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 2117d952e501SBarry Smith } 2118d952e501SBarry Smith } 2119d763cef2SBarry Smith ts->numbermonitors = 0; 2120d763cef2SBarry Smith PetscFunctionReturn(0); 2121d763cef2SBarry Smith } 2122d763cef2SBarry Smith 21234a2ae208SSatish Balay #undef __FUNCT__ 2124a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 2125d8e5e3e6SSatish Balay /*@ 2126a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 21275516499fSSatish Balay 21285516499fSSatish Balay Level: intermediate 212941251cbbSSatish Balay 21305516499fSSatish Balay .keywords: TS, set, monitor 21315516499fSSatish Balay 213241251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 213341251cbbSSatish Balay @*/ 2134649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 2135d763cef2SBarry Smith { 2136dfbe8321SBarry Smith PetscErrorCode ierr; 2137649052a6SBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm); 2138d132466eSBarry Smith 2139d763cef2SBarry Smith PetscFunctionBegin; 2140649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2141971832b6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr); 2142649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2143d763cef2SBarry Smith PetscFunctionReturn(0); 2144d763cef2SBarry Smith } 2145d763cef2SBarry Smith 21464a2ae208SSatish Balay #undef __FUNCT__ 2147cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 2148cd652676SJed Brown /*@ 2149cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 2150cd652676SJed Brown 2151cd652676SJed Brown Logically Collective on TS 2152cd652676SJed Brown 2153cd652676SJed Brown Input Argument: 2154cd652676SJed Brown . ts - time stepping context 2155cd652676SJed Brown 2156cd652676SJed Brown Output Argument: 2157cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 2158cd652676SJed Brown 2159cd652676SJed Brown Level: intermediate 2160cd652676SJed Brown 2161cd652676SJed Brown .keywords: TS, set 2162cd652676SJed Brown 2163cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 2164cd652676SJed Brown @*/ 2165cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 2166cd652676SJed Brown { 2167cd652676SJed Brown PetscFunctionBegin; 2168cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2169cd652676SJed Brown ts->retain_stages = flg; 2170cd652676SJed Brown PetscFunctionReturn(0); 2171cd652676SJed Brown } 2172cd652676SJed Brown 2173cd652676SJed Brown #undef __FUNCT__ 2174cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 2175cd652676SJed Brown /*@ 2176cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 2177cd652676SJed Brown 2178cd652676SJed Brown Collective on TS 2179cd652676SJed Brown 2180cd652676SJed Brown Input Argument: 2181cd652676SJed Brown + ts - time stepping context 2182cd652676SJed Brown - t - time to interpolate to 2183cd652676SJed Brown 2184cd652676SJed Brown Output Argument: 21850910c330SBarry Smith . U - state at given time 2186cd652676SJed Brown 2187cd652676SJed Brown Notes: 2188cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 2189cd652676SJed Brown 2190cd652676SJed Brown Level: intermediate 2191cd652676SJed Brown 2192cd652676SJed Brown Developer Notes: 2193cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 2194cd652676SJed Brown 2195cd652676SJed Brown .keywords: TS, set 2196cd652676SJed Brown 2197cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 2198cd652676SJed Brown @*/ 21990910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 2200cd652676SJed Brown { 2201cd652676SJed Brown PetscErrorCode ierr; 2202cd652676SJed Brown 2203cd652676SJed Brown PetscFunctionBegin; 2204cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2205d8cd7023SBarry Smith if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Requested time %G not in last time steps [%G,%G]",t,ts->ptime-ts->time_step_prev,ts->ptime); 2206cd652676SJed Brown if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 22070910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 2208cd652676SJed Brown PetscFunctionReturn(0); 2209cd652676SJed Brown } 2210cd652676SJed Brown 2211cd652676SJed Brown #undef __FUNCT__ 22124a2ae208SSatish Balay #define __FUNCT__ "TSStep" 2213d763cef2SBarry Smith /*@ 22146d9e5789SSean Farley TSStep - Steps one time step 2215d763cef2SBarry Smith 2216d763cef2SBarry Smith Collective on TS 2217d763cef2SBarry Smith 2218d763cef2SBarry Smith Input Parameter: 2219d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2220d763cef2SBarry Smith 22216d9e5789SSean Farley Level: intermediate 2222d763cef2SBarry Smith 2223b8123daeSJed Brown Notes: 2224b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 2225b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 2226b8123daeSJed Brown 222725cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 222825cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 222925cb2221SBarry Smith 2230d763cef2SBarry Smith .keywords: TS, timestep, solve 2231d763cef2SBarry Smith 223225cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 2233d763cef2SBarry Smith @*/ 2234193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 2235d763cef2SBarry Smith { 2236362cd11cSLisandro Dalcin PetscReal ptime_prev; 2237dfbe8321SBarry Smith PetscErrorCode ierr; 2238d763cef2SBarry Smith 2239d763cef2SBarry Smith PetscFunctionBegin; 22400700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2241d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 2242d405a339SMatthew Knepley 2243362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 2244362cd11cSLisandro Dalcin 2245362cd11cSLisandro Dalcin ptime_prev = ts->ptime; 2246d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2247193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 2248d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2249362cd11cSLisandro Dalcin ts->time_step_prev = ts->ptime - ptime_prev; 2250362cd11cSLisandro Dalcin 2251362cd11cSLisandro Dalcin if (ts->reason < 0) { 2252cef5090cSJed Brown if (ts->errorifstepfailed) { 2253cef5090cSJed Brown if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) { 2254cef5090cSJed Brown SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]); 2255cef5090cSJed Brown } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 2256cef5090cSJed Brown } 2257362cd11cSLisandro Dalcin } else if (!ts->reason) { 2258362cd11cSLisandro Dalcin if (ts->steps >= ts->max_steps) 2259362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITS; 2260362cd11cSLisandro Dalcin else if (ts->ptime >= ts->max_time) 2261362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_TIME; 2262362cd11cSLisandro Dalcin } 2263362cd11cSLisandro Dalcin 2264d763cef2SBarry Smith PetscFunctionReturn(0); 2265d763cef2SBarry Smith } 2266d763cef2SBarry Smith 22674a2ae208SSatish Balay #undef __FUNCT__ 226805175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 226905175c85SJed Brown /*@ 227005175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 227105175c85SJed Brown 22721c3436cfSJed Brown Collective on TS 227305175c85SJed Brown 227405175c85SJed Brown Input Arguments: 22751c3436cfSJed Brown + ts - time stepping context 22761c3436cfSJed Brown . order - desired order of accuracy 22771c3436cfSJed Brown - done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available) 227805175c85SJed Brown 227905175c85SJed Brown Output Arguments: 22800910c330SBarry Smith . U - state at the end of the current step 228105175c85SJed Brown 228205175c85SJed Brown Level: advanced 228305175c85SJed Brown 2284108c343cSJed Brown Notes: 2285108c343cSJed Brown This function cannot be called until all stages have been evaluated. 2286108c343cSJed 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. 2287108c343cSJed Brown 22881c3436cfSJed Brown .seealso: TSStep(), TSAdapt 228905175c85SJed Brown @*/ 22900910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 229105175c85SJed Brown { 229205175c85SJed Brown PetscErrorCode ierr; 229305175c85SJed Brown 229405175c85SJed Brown PetscFunctionBegin; 229505175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 229605175c85SJed Brown PetscValidType(ts,1); 22970910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 22981c3436cfSJed Brown if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 22990910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 230005175c85SJed Brown PetscFunctionReturn(0); 230105175c85SJed Brown } 230205175c85SJed Brown 230305175c85SJed Brown #undef __FUNCT__ 23046a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 23056a4d4014SLisandro Dalcin /*@ 23066a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 23076a4d4014SLisandro Dalcin 23086a4d4014SLisandro Dalcin Collective on TS 23096a4d4014SLisandro Dalcin 23106a4d4014SLisandro Dalcin Input Parameter: 23116a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 2312cc708dedSBarry Smith - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 23135a3a76d0SJed Brown 23146a4d4014SLisandro Dalcin Level: beginner 23156a4d4014SLisandro Dalcin 23165a3a76d0SJed Brown Notes: 23175a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 23185a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 23195a3a76d0SJed Brown stepped over the final time. 23205a3a76d0SJed Brown 23216a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 23226a4d4014SLisandro Dalcin 23236a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 23246a4d4014SLisandro Dalcin @*/ 2325cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 23266a4d4014SLisandro Dalcin { 23274d7d938eSLisandro Dalcin PetscBool flg; 23284d7d938eSLisandro Dalcin PetscViewer viewer; 23296a4d4014SLisandro Dalcin PetscErrorCode ierr; 2330*cffb1e40SBarry Smith PetscViewerFormat format; 2331f22f69f0SBarry Smith 23326a4d4014SLisandro Dalcin PetscFunctionBegin; 23330700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2334f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 233549354f04SShri 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 */ 23360910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 23375a3a76d0SJed Brown Vec y; 23380910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 23395a3a76d0SJed Brown ierr = TSSetSolution(ts,y);CHKERRQ(ierr); 23405a3a76d0SJed Brown ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */ 23415a3a76d0SJed Brown } 2342f2c2a1b9SBarry Smith if (u) { 23430910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 2344f2c2a1b9SBarry Smith } 23455a3a76d0SJed Brown } else { 2346f2c2a1b9SBarry Smith if (u) { 23470910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 23485a3a76d0SJed Brown } 2349f2c2a1b9SBarry Smith } 2350b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 23516a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 2352193ac0bcSJed Brown ts->steps = 0; 23535ef26d82SJed Brown ts->ksp_its = 0; 23545ef26d82SJed Brown ts->snes_its = 0; 2355c610991cSLisandro Dalcin ts->num_snes_failures = 0; 2356c610991cSLisandro Dalcin ts->reject = 0; 2357193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 2358193ac0bcSJed Brown 2359193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 2360193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 23610910c330SBarry Smith ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 2362cc708dedSBarry Smith ts->solvetime = ts->ptime; 2363193ac0bcSJed Brown } else { 23646a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 2365362cd11cSLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2366362cd11cSLisandro Dalcin if (ts->steps >= ts->max_steps) 2367362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITS; 2368362cd11cSLisandro Dalcin else if (ts->ptime >= ts->max_time) 2369362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_TIME; 2370e1a7a14fSJed Brown while (!ts->reason) { 2371193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 2372193ac0bcSJed Brown ierr = TSPostStep(ts);CHKERRQ(ierr); 2373193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2374193ac0bcSJed Brown } 237549354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 23760910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 2377cc708dedSBarry Smith ts->solvetime = ts->max_time; 23780574a7fbSJed Brown } else { 2379ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 2380cc708dedSBarry Smith ts->solvetime = ts->ptime; 23810574a7fbSJed Brown } 2382193ac0bcSJed Brown } 23833923b477SBarry Smith ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2384*cffb1e40SBarry Smith ierr = PetscOptionsGetViewer(((PetscObject)ts)->comm,((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr); 23854d7d938eSLisandro Dalcin if (flg && !PetscPreLoadingOn) { 2386*cffb1e40SBarry Smith ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); 23874d7d938eSLisandro Dalcin ierr = TSView(ts,viewer);CHKERRQ(ierr); 2388*cffb1e40SBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 2389*cffb1e40SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 23902b0a91c0SBarry Smith } 23916a4d4014SLisandro Dalcin PetscFunctionReturn(0); 23926a4d4014SLisandro Dalcin } 23936a4d4014SLisandro Dalcin 23946a4d4014SLisandro Dalcin #undef __FUNCT__ 23954a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 2396228d79bcSJed Brown /*@ 2397228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 2398228d79bcSJed Brown 2399228d79bcSJed Brown Collective on TS 2400228d79bcSJed Brown 2401228d79bcSJed Brown Input Parameters: 2402228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 2403228d79bcSJed Brown . step - step number that has just completed 2404228d79bcSJed Brown . ptime - model time of the state 24050910c330SBarry Smith - u - state at the current model time 2406228d79bcSJed Brown 2407228d79bcSJed Brown Notes: 2408228d79bcSJed Brown TSMonitor() is typically used within the time stepping implementations. 2409228d79bcSJed Brown Users might call this function when using the TSStep() interface instead of TSSolve(). 2410228d79bcSJed Brown 2411228d79bcSJed Brown Level: advanced 2412228d79bcSJed Brown 2413228d79bcSJed Brown .keywords: TS, timestep 2414228d79bcSJed Brown @*/ 24150910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 2416d763cef2SBarry Smith { 24176849ba73SBarry Smith PetscErrorCode ierr; 2418a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 2419d763cef2SBarry Smith 2420d763cef2SBarry Smith PetscFunctionBegin; 2421d763cef2SBarry Smith for (i=0; i<n; i++) { 24220910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 2423d763cef2SBarry Smith } 2424d763cef2SBarry Smith PetscFunctionReturn(0); 2425d763cef2SBarry Smith } 2426d763cef2SBarry Smith 2427d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 24280b039ecaSBarry Smith struct _n_TSMonitorLGCtx { 24290b039ecaSBarry Smith PetscDrawLG lg; 24300b039ecaSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 2431201da799SBarry Smith PetscInt ksp_its,snes_its; 24320b039ecaSBarry Smith }; 24330b039ecaSBarry Smith 2434d763cef2SBarry Smith 24354a2ae208SSatish Balay #undef __FUNCT__ 2436a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 2437d763cef2SBarry Smith /*@C 2438a9f9c1f6SBarry Smith TSMonitorLGCtxCreate - Creates a line graph context for use with 2439a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 2440d763cef2SBarry Smith 2441d763cef2SBarry Smith Collective on TS 2442d763cef2SBarry Smith 2443d763cef2SBarry Smith Input Parameters: 2444d763cef2SBarry Smith + host - the X display to open, or null for the local machine 2445d763cef2SBarry Smith . label - the title to put in the title bar 24467c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 2447a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 2448a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 2449d763cef2SBarry Smith 2450d763cef2SBarry Smith Output Parameter: 24510b039ecaSBarry Smith . ctx - the context 2452d763cef2SBarry Smith 2453d763cef2SBarry Smith Options Database Key: 24544f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 24554f09c107SBarry Smith . -ts_monitor_lg_solution - 245699fdda47SBarry Smith . -ts_monitor_lg_error - 245799fdda47SBarry Smith . -ts_monitor_lg_ksp_iterations - 245899fdda47SBarry Smith . -ts_monitor_lg_snes_iterations - 245999fdda47SBarry Smith - -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true 2460d763cef2SBarry Smith 2461d763cef2SBarry Smith Notes: 2462a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 2463d763cef2SBarry Smith 2464d763cef2SBarry Smith Level: intermediate 2465d763cef2SBarry Smith 24667c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 2467d763cef2SBarry Smith 24684f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 24697c922b88SBarry Smith 2470d763cef2SBarry Smith @*/ 2471a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 2472d763cef2SBarry Smith { 2473b0a32e0cSBarry Smith PetscDraw win; 2474dfbe8321SBarry Smith PetscErrorCode ierr; 247599fdda47SBarry Smith PetscBool flg = PETSC_TRUE; 2476d763cef2SBarry Smith 2477d763cef2SBarry Smith PetscFunctionBegin; 2478201da799SBarry Smith ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr); 2479a80ad3e0SBarry Smith ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr); 24803fbbecb0SBarry Smith ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr); 24810b039ecaSBarry Smith ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr); 248299fdda47SBarry Smith ierr = PetscOptionsGetBool(PETSC_NULL,"-lg_indicate_data_points",&flg,PETSC_NULL);CHKERRQ(ierr); 248399fdda47SBarry Smith if (flg) { 24840b039ecaSBarry Smith ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr); 248599fdda47SBarry Smith } 24860b039ecaSBarry Smith ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr); 2487a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 2488d763cef2SBarry Smith PetscFunctionReturn(0); 2489d763cef2SBarry Smith } 2490d763cef2SBarry Smith 24914a2ae208SSatish Balay #undef __FUNCT__ 24924f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 24934f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 2494d763cef2SBarry Smith { 24950b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 2496c32365f1SBarry Smith PetscReal x = ptime,y; 2497dfbe8321SBarry Smith PetscErrorCode ierr; 2498d763cef2SBarry Smith 2499d763cef2SBarry Smith PetscFunctionBegin; 2500a9f9c1f6SBarry Smith if (!n) { 2501a9f9c1f6SBarry Smith PetscDrawAxis axis; 2502a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 2503a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 2504a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 2505a9f9c1f6SBarry Smith } 2506c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 25070b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 250899fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))){ 25090b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 25103923b477SBarry Smith } 2511d763cef2SBarry Smith PetscFunctionReturn(0); 2512d763cef2SBarry Smith } 2513d763cef2SBarry Smith 25144a2ae208SSatish Balay #undef __FUNCT__ 2515a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 2516d763cef2SBarry Smith /*@C 2517a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 2518a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 2519d763cef2SBarry Smith 25200b039ecaSBarry Smith Collective on TSMonitorLGCtx 2521d763cef2SBarry Smith 2522d763cef2SBarry Smith Input Parameter: 25230b039ecaSBarry Smith . ctx - the monitor context 2524d763cef2SBarry Smith 2525d763cef2SBarry Smith Level: intermediate 2526d763cef2SBarry Smith 2527d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 2528d763cef2SBarry Smith 25294f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 2530d763cef2SBarry Smith @*/ 2531a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 2532d763cef2SBarry Smith { 2533b0a32e0cSBarry Smith PetscDraw draw; 2534dfbe8321SBarry Smith PetscErrorCode ierr; 2535d763cef2SBarry Smith 2536d763cef2SBarry Smith PetscFunctionBegin; 25370b039ecaSBarry Smith ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr); 25386bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 25390b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 25400b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 2541d763cef2SBarry Smith PetscFunctionReturn(0); 2542d763cef2SBarry Smith } 2543d763cef2SBarry Smith 25444a2ae208SSatish Balay #undef __FUNCT__ 25454a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 2546d763cef2SBarry Smith /*@ 2547b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 2548d763cef2SBarry Smith 2549d763cef2SBarry Smith Not Collective 2550d763cef2SBarry Smith 2551d763cef2SBarry Smith Input Parameter: 2552d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2553d763cef2SBarry Smith 2554d763cef2SBarry Smith Output Parameter: 2555d763cef2SBarry Smith . t - the current time 2556d763cef2SBarry Smith 2557d763cef2SBarry Smith Level: beginner 2558d763cef2SBarry Smith 2559b8123daeSJed Brown Note: 2560b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 2561b8123daeSJed Brown TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 2562b8123daeSJed Brown 2563d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2564d763cef2SBarry Smith 2565d763cef2SBarry Smith .keywords: TS, get, time 2566d763cef2SBarry Smith @*/ 25677087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal* t) 2568d763cef2SBarry Smith { 2569d763cef2SBarry Smith PetscFunctionBegin; 25700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2571f7cf8827SBarry Smith PetscValidRealPointer(t,2); 2572d763cef2SBarry Smith *t = ts->ptime; 2573d763cef2SBarry Smith PetscFunctionReturn(0); 2574d763cef2SBarry Smith } 2575d763cef2SBarry Smith 25764a2ae208SSatish Balay #undef __FUNCT__ 25776a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 25786a4d4014SLisandro Dalcin /*@ 25796a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 25806a4d4014SLisandro Dalcin 25813f9fe445SBarry Smith Logically Collective on TS 25826a4d4014SLisandro Dalcin 25836a4d4014SLisandro Dalcin Input Parameters: 25846a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 25856a4d4014SLisandro Dalcin - time - the time 25866a4d4014SLisandro Dalcin 25876a4d4014SLisandro Dalcin Level: intermediate 25886a4d4014SLisandro Dalcin 25896a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 25906a4d4014SLisandro Dalcin 25916a4d4014SLisandro Dalcin .keywords: TS, set, time 25926a4d4014SLisandro Dalcin @*/ 25937087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 25946a4d4014SLisandro Dalcin { 25956a4d4014SLisandro Dalcin PetscFunctionBegin; 25960700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2597c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 25986a4d4014SLisandro Dalcin ts->ptime = t; 25996a4d4014SLisandro Dalcin PetscFunctionReturn(0); 26006a4d4014SLisandro Dalcin } 26016a4d4014SLisandro Dalcin 26026a4d4014SLisandro Dalcin #undef __FUNCT__ 26034a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 2604d763cef2SBarry Smith /*@C 2605d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 2606d763cef2SBarry Smith TS options in the database. 2607d763cef2SBarry Smith 26083f9fe445SBarry Smith Logically Collective on TS 2609d763cef2SBarry Smith 2610d763cef2SBarry Smith Input Parameter: 2611d763cef2SBarry Smith + ts - The TS context 2612d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2613d763cef2SBarry Smith 2614d763cef2SBarry Smith Notes: 2615d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2616d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2617d763cef2SBarry Smith hyphen. 2618d763cef2SBarry Smith 2619d763cef2SBarry Smith Level: advanced 2620d763cef2SBarry Smith 2621d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 2622d763cef2SBarry Smith 2623d763cef2SBarry Smith .seealso: TSSetFromOptions() 2624d763cef2SBarry Smith 2625d763cef2SBarry Smith @*/ 26267087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 2627d763cef2SBarry Smith { 2628dfbe8321SBarry Smith PetscErrorCode ierr; 2629089b2837SJed Brown SNES snes; 2630d763cef2SBarry Smith 2631d763cef2SBarry Smith PetscFunctionBegin; 26320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2633d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2634089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2635089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2636d763cef2SBarry Smith PetscFunctionReturn(0); 2637d763cef2SBarry Smith } 2638d763cef2SBarry Smith 2639d763cef2SBarry Smith 26404a2ae208SSatish Balay #undef __FUNCT__ 26414a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 2642d763cef2SBarry Smith /*@C 2643d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 2644d763cef2SBarry Smith TS options in the database. 2645d763cef2SBarry Smith 26463f9fe445SBarry Smith Logically Collective on TS 2647d763cef2SBarry Smith 2648d763cef2SBarry Smith Input Parameter: 2649d763cef2SBarry Smith + ts - The TS context 2650d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2651d763cef2SBarry Smith 2652d763cef2SBarry Smith Notes: 2653d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2654d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2655d763cef2SBarry Smith hyphen. 2656d763cef2SBarry Smith 2657d763cef2SBarry Smith Level: advanced 2658d763cef2SBarry Smith 2659d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 2660d763cef2SBarry Smith 2661d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 2662d763cef2SBarry Smith 2663d763cef2SBarry Smith @*/ 26647087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 2665d763cef2SBarry Smith { 2666dfbe8321SBarry Smith PetscErrorCode ierr; 2667089b2837SJed Brown SNES snes; 2668d763cef2SBarry Smith 2669d763cef2SBarry Smith PetscFunctionBegin; 26700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2671d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2672089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2673089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2674d763cef2SBarry Smith PetscFunctionReturn(0); 2675d763cef2SBarry Smith } 2676d763cef2SBarry Smith 26774a2ae208SSatish Balay #undef __FUNCT__ 26784a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2679d763cef2SBarry Smith /*@C 2680d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2681d763cef2SBarry Smith TS options in the database. 2682d763cef2SBarry Smith 2683d763cef2SBarry Smith Not Collective 2684d763cef2SBarry Smith 2685d763cef2SBarry Smith Input Parameter: 2686d763cef2SBarry Smith . ts - The TS context 2687d763cef2SBarry Smith 2688d763cef2SBarry Smith Output Parameter: 2689d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2690d763cef2SBarry Smith 2691d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2692d763cef2SBarry Smith sufficient length to hold the prefix. 2693d763cef2SBarry Smith 2694d763cef2SBarry Smith Level: intermediate 2695d763cef2SBarry Smith 2696d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2697d763cef2SBarry Smith 2698d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2699d763cef2SBarry Smith @*/ 27007087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2701d763cef2SBarry Smith { 2702dfbe8321SBarry Smith PetscErrorCode ierr; 2703d763cef2SBarry Smith 2704d763cef2SBarry Smith PetscFunctionBegin; 27050700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27064482741eSBarry Smith PetscValidPointer(prefix,2); 2707d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2708d763cef2SBarry Smith PetscFunctionReturn(0); 2709d763cef2SBarry Smith } 2710d763cef2SBarry Smith 27114a2ae208SSatish Balay #undef __FUNCT__ 27124a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2713d763cef2SBarry Smith /*@C 2714d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2715d763cef2SBarry Smith 2716d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2717d763cef2SBarry Smith 2718d763cef2SBarry Smith Input Parameter: 2719d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2720d763cef2SBarry Smith 2721d763cef2SBarry Smith Output Parameters: 2722b5abc632SBarry Smith + J - The Jacobian J of F, where U_t = G(U,t) 2723d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2724089b2837SJed Brown . func - Function to compute the Jacobian of the RHS 2725d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2726d763cef2SBarry Smith 2727d763cef2SBarry Smith Notes: You can pass in PETSC_NULL for any return argument you do not need. 2728d763cef2SBarry Smith 2729d763cef2SBarry Smith Level: intermediate 2730d763cef2SBarry Smith 273126d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2732d763cef2SBarry Smith 2733d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2734d763cef2SBarry Smith @*/ 2735089b2837SJed Brown PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2736d763cef2SBarry Smith { 2737089b2837SJed Brown PetscErrorCode ierr; 2738089b2837SJed Brown SNES snes; 273924989b8cSPeter Brune DM dm; 2740089b2837SJed Brown 2741d763cef2SBarry Smith PetscFunctionBegin; 2742089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2743089b2837SJed Brown ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 274424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 274524989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 2746d763cef2SBarry Smith PetscFunctionReturn(0); 2747d763cef2SBarry Smith } 2748d763cef2SBarry Smith 27491713a123SBarry Smith #undef __FUNCT__ 27502eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 27512eca1d9cSJed Brown /*@C 27522eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 27532eca1d9cSJed Brown 27542eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 27552eca1d9cSJed Brown 27562eca1d9cSJed Brown Input Parameter: 27572eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 27582eca1d9cSJed Brown 27592eca1d9cSJed Brown Output Parameters: 27602eca1d9cSJed Brown + A - The Jacobian of F(t,U,U_t) 27612eca1d9cSJed Brown . B - The preconditioner matrix, often the same as A 27622eca1d9cSJed Brown . f - The function to compute the matrices 27632eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 27642eca1d9cSJed Brown 27652eca1d9cSJed Brown Notes: You can pass in PETSC_NULL for any return argument you do not need. 27662eca1d9cSJed Brown 27672eca1d9cSJed Brown Level: advanced 27682eca1d9cSJed Brown 27692eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 27702eca1d9cSJed Brown 27712eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 27722eca1d9cSJed Brown @*/ 27737087cfbeSBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 27742eca1d9cSJed Brown { 2775089b2837SJed Brown PetscErrorCode ierr; 2776089b2837SJed Brown SNES snes; 277724989b8cSPeter Brune DM dm; 27780910c330SBarry Smith 27792eca1d9cSJed Brown PetscFunctionBegin; 2780089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2781f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 2782089b2837SJed Brown ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 278324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 278424989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 27852eca1d9cSJed Brown PetscFunctionReturn(0); 27862eca1d9cSJed Brown } 27872eca1d9cSJed Brown 278883a4ac43SBarry Smith struct _n_TSMonitorDrawCtx { 27896083293cSBarry Smith PetscViewer viewer; 27906083293cSBarry Smith Vec initialsolution; 27916083293cSBarry Smith PetscBool showinitial; 279283a4ac43SBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 279383a4ac43SBarry Smith }; 27946083293cSBarry Smith 27952eca1d9cSJed Brown #undef __FUNCT__ 279683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 27971713a123SBarry Smith /*@C 279883a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 27991713a123SBarry Smith VecView() for the solution at each timestep 28001713a123SBarry Smith 28011713a123SBarry Smith Collective on TS 28021713a123SBarry Smith 28031713a123SBarry Smith Input Parameters: 28041713a123SBarry Smith + ts - the TS context 28051713a123SBarry Smith . step - current time-step 2806142b95e3SSatish Balay . ptime - current time 28071713a123SBarry Smith - dummy - either a viewer or PETSC_NULL 28081713a123SBarry Smith 280999fdda47SBarry Smith Options Database: 281099fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 281199fdda47SBarry Smith 281299fdda47SBarry 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 281399fdda47SBarry Smith will look bad 281499fdda47SBarry Smith 28151713a123SBarry Smith Level: intermediate 28161713a123SBarry Smith 28171713a123SBarry Smith .keywords: TS, vector, monitor, view 28181713a123SBarry Smith 2819a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 28201713a123SBarry Smith @*/ 28210910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 28221713a123SBarry Smith { 2823dfbe8321SBarry Smith PetscErrorCode ierr; 282483a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 28251713a123SBarry Smith 28261713a123SBarry Smith PetscFunctionBegin; 28276083293cSBarry Smith if (!step && ictx->showinitial) { 28286083293cSBarry Smith if (!ictx->initialsolution) { 28290910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 28301713a123SBarry Smith } 28310910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 28326083293cSBarry Smith } 28333fbbecb0SBarry Smith if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0); 28340dcf80beSBarry Smith 28356083293cSBarry Smith if (ictx->showinitial) { 28366083293cSBarry Smith PetscReal pause; 28376083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 28386083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 28396083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 28406083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 28416083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 28426083293cSBarry Smith } 28430910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 28446083293cSBarry Smith if (ictx->showinitial) { 28456083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 28466083293cSBarry Smith } 28471713a123SBarry Smith PetscFunctionReturn(0); 28481713a123SBarry Smith } 28491713a123SBarry Smith 28501713a123SBarry Smith 28516c699258SBarry Smith #undef __FUNCT__ 285283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 28536083293cSBarry Smith /*@C 285483a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 28556083293cSBarry Smith 28566083293cSBarry Smith Collective on TS 28576083293cSBarry Smith 28586083293cSBarry Smith Input Parameters: 28596083293cSBarry Smith . ctx - the monitor context 28606083293cSBarry Smith 28616083293cSBarry Smith Level: intermediate 28626083293cSBarry Smith 28636083293cSBarry Smith .keywords: TS, vector, monitor, view 28646083293cSBarry Smith 286583a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 28666083293cSBarry Smith @*/ 286783a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 28686083293cSBarry Smith { 28696083293cSBarry Smith PetscErrorCode ierr; 28706083293cSBarry Smith 28716083293cSBarry Smith PetscFunctionBegin; 287283a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 287383a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 287483a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 28756083293cSBarry Smith PetscFunctionReturn(0); 28766083293cSBarry Smith } 28776083293cSBarry Smith 28786083293cSBarry Smith #undef __FUNCT__ 287983a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 28806083293cSBarry Smith /*@C 288183a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 28826083293cSBarry Smith 28836083293cSBarry Smith Collective on TS 28846083293cSBarry Smith 28856083293cSBarry Smith Input Parameter: 28866083293cSBarry Smith . ts - time-step context 28876083293cSBarry Smith 28886083293cSBarry Smith Output Patameter: 28896083293cSBarry Smith . ctx - the monitor context 28906083293cSBarry Smith 289199fdda47SBarry Smith Options Database: 289299fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 289399fdda47SBarry Smith 28946083293cSBarry Smith Level: intermediate 28956083293cSBarry Smith 28966083293cSBarry Smith .keywords: TS, vector, monitor, view 28976083293cSBarry Smith 289883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 28996083293cSBarry Smith @*/ 290083a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 29016083293cSBarry Smith { 29026083293cSBarry Smith PetscErrorCode ierr; 29036083293cSBarry Smith 29046083293cSBarry Smith PetscFunctionBegin; 290583a4ac43SBarry Smith ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr); 290683a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 290783a4ac43SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 290883a4ac43SBarry Smith (*ctx)->howoften = howoften; 290999fdda47SBarry Smith ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,PETSC_NULL);CHKERRQ(ierr); 29106083293cSBarry Smith PetscFunctionReturn(0); 29116083293cSBarry Smith } 29126083293cSBarry Smith 29136083293cSBarry Smith #undef __FUNCT__ 291483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 29153a471f94SBarry Smith /*@C 291683a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 29173a471f94SBarry Smith VecView() for the error at each timestep 29183a471f94SBarry Smith 29193a471f94SBarry Smith Collective on TS 29203a471f94SBarry Smith 29213a471f94SBarry Smith Input Parameters: 29223a471f94SBarry Smith + ts - the TS context 29233a471f94SBarry Smith . step - current time-step 29243a471f94SBarry Smith . ptime - current time 29253a471f94SBarry Smith - dummy - either a viewer or PETSC_NULL 29263a471f94SBarry Smith 29273a471f94SBarry Smith Level: intermediate 29283a471f94SBarry Smith 29293a471f94SBarry Smith .keywords: TS, vector, monitor, view 29303a471f94SBarry Smith 29313a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 29323a471f94SBarry Smith @*/ 29330910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 29343a471f94SBarry Smith { 29353a471f94SBarry Smith PetscErrorCode ierr; 293683a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 293783a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 29383a471f94SBarry Smith Vec work; 29393a471f94SBarry Smith 29403a471f94SBarry Smith PetscFunctionBegin; 29413fbbecb0SBarry Smith if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0); 29420910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 29433a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 29440910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 29453a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 29463a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 29473a471f94SBarry Smith PetscFunctionReturn(0); 29483a471f94SBarry Smith } 29493a471f94SBarry Smith 29502a34c10cSBarry Smith #include <petsc-private/dmimpl.h> 29513a471f94SBarry Smith #undef __FUNCT__ 29526c699258SBarry Smith #define __FUNCT__ "TSSetDM" 29536c699258SBarry Smith /*@ 29546c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 29556c699258SBarry Smith 29563f9fe445SBarry Smith Logically Collective on TS and DM 29576c699258SBarry Smith 29586c699258SBarry Smith Input Parameters: 29596c699258SBarry Smith + ts - the preconditioner context 29606c699258SBarry Smith - dm - the dm 29616c699258SBarry Smith 29626c699258SBarry Smith Level: intermediate 29636c699258SBarry Smith 29646c699258SBarry Smith 29656c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 29666c699258SBarry Smith @*/ 29677087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 29686c699258SBarry Smith { 29696c699258SBarry Smith PetscErrorCode ierr; 2970089b2837SJed Brown SNES snes; 2971942e3340SBarry Smith DMTS tsdm; 29726c699258SBarry Smith 29736c699258SBarry Smith PetscFunctionBegin; 29740700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 297570663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 2976942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 29772a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 2978942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 2979942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 298024989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 298124989b8cSPeter Brune tsdm->originaldm = dm; 298224989b8cSPeter Brune } 298324989b8cSPeter Brune } 29846bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 298524989b8cSPeter Brune } 29866c699258SBarry Smith ts->dm = dm; 2987089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2988089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 29896c699258SBarry Smith PetscFunctionReturn(0); 29906c699258SBarry Smith } 29916c699258SBarry Smith 29926c699258SBarry Smith #undef __FUNCT__ 29936c699258SBarry Smith #define __FUNCT__ "TSGetDM" 29946c699258SBarry Smith /*@ 29956c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 29966c699258SBarry Smith 29973f9fe445SBarry Smith Not Collective 29986c699258SBarry Smith 29996c699258SBarry Smith Input Parameter: 30006c699258SBarry Smith . ts - the preconditioner context 30016c699258SBarry Smith 30026c699258SBarry Smith Output Parameter: 30036c699258SBarry Smith . dm - the dm 30046c699258SBarry Smith 30056c699258SBarry Smith Level: intermediate 30066c699258SBarry Smith 30076c699258SBarry Smith 30086c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 30096c699258SBarry Smith @*/ 30107087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 30116c699258SBarry Smith { 3012496e6a7aSJed Brown PetscErrorCode ierr; 3013496e6a7aSJed Brown 30146c699258SBarry Smith PetscFunctionBegin; 30150700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3016496e6a7aSJed Brown if (!ts->dm) { 3017496e6a7aSJed Brown ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr); 3018496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 3019496e6a7aSJed Brown } 30206c699258SBarry Smith *dm = ts->dm; 30216c699258SBarry Smith PetscFunctionReturn(0); 30226c699258SBarry Smith } 30231713a123SBarry Smith 30240f5c6efeSJed Brown #undef __FUNCT__ 30250f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 30260f5c6efeSJed Brown /*@ 30270f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 30280f5c6efeSJed Brown 30293f9fe445SBarry Smith Logically Collective on SNES 30300f5c6efeSJed Brown 30310f5c6efeSJed Brown Input Parameter: 3032d42a1c89SJed Brown + snes - nonlinear solver 30330910c330SBarry Smith . U - the current state at which to evaluate the residual 3034d42a1c89SJed Brown - ctx - user context, must be a TS 30350f5c6efeSJed Brown 30360f5c6efeSJed Brown Output Parameter: 30370f5c6efeSJed Brown . F - the nonlinear residual 30380f5c6efeSJed Brown 30390f5c6efeSJed Brown Notes: 30400f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 30410f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 30420f5c6efeSJed Brown 30430f5c6efeSJed Brown Level: advanced 30440f5c6efeSJed Brown 30450f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 30460f5c6efeSJed Brown @*/ 30470910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 30480f5c6efeSJed Brown { 30490f5c6efeSJed Brown TS ts = (TS)ctx; 30500f5c6efeSJed Brown PetscErrorCode ierr; 30510f5c6efeSJed Brown 30520f5c6efeSJed Brown PetscFunctionBegin; 30530f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 30540910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 30550f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 30560f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 30570910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 30580f5c6efeSJed Brown PetscFunctionReturn(0); 30590f5c6efeSJed Brown } 30600f5c6efeSJed Brown 30610f5c6efeSJed Brown #undef __FUNCT__ 30620f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 30630f5c6efeSJed Brown /*@ 30640f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 30650f5c6efeSJed Brown 30660f5c6efeSJed Brown Collective on SNES 30670f5c6efeSJed Brown 30680f5c6efeSJed Brown Input Parameter: 30690f5c6efeSJed Brown + snes - nonlinear solver 30700910c330SBarry Smith . U - the current state at which to evaluate the residual 30710f5c6efeSJed Brown - ctx - user context, must be a TS 30720f5c6efeSJed Brown 30730f5c6efeSJed Brown Output Parameter: 30740f5c6efeSJed Brown + A - the Jacobian 30750f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 30760f5c6efeSJed Brown - flag - indicates any structure change in the matrix 30770f5c6efeSJed Brown 30780f5c6efeSJed Brown Notes: 30790f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 30800f5c6efeSJed Brown 30810f5c6efeSJed Brown Level: developer 30820f5c6efeSJed Brown 30830f5c6efeSJed Brown .seealso: SNESSetJacobian() 30840f5c6efeSJed Brown @*/ 30850910c330SBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx) 30860f5c6efeSJed Brown { 30870f5c6efeSJed Brown TS ts = (TS)ctx; 30880f5c6efeSJed Brown PetscErrorCode ierr; 30890f5c6efeSJed Brown 30900f5c6efeSJed Brown PetscFunctionBegin; 30910f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 30920910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 30930f5c6efeSJed Brown PetscValidPointer(A,3); 30940f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 30950f5c6efeSJed Brown PetscValidPointer(B,4); 30960f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 30970f5c6efeSJed Brown PetscValidPointer(flag,5); 30980f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 30990910c330SBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr); 31000f5c6efeSJed Brown PetscFunctionReturn(0); 31010f5c6efeSJed Brown } 3102325fc9f4SBarry Smith 31030e4ef248SJed Brown #undef __FUNCT__ 31040e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 31050e4ef248SJed Brown /*@C 31060e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 31070e4ef248SJed Brown 31080e4ef248SJed Brown Collective on TS 31090e4ef248SJed Brown 31100e4ef248SJed Brown Input Arguments: 31110e4ef248SJed Brown + ts - time stepping context 31120e4ef248SJed Brown . t - time at which to evaluate 31130910c330SBarry Smith . U - state at which to evaluate 31140e4ef248SJed Brown - ctx - context 31150e4ef248SJed Brown 31160e4ef248SJed Brown Output Arguments: 31170e4ef248SJed Brown . F - right hand side 31180e4ef248SJed Brown 31190e4ef248SJed Brown Level: intermediate 31200e4ef248SJed Brown 31210e4ef248SJed Brown Notes: 31220e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 31230e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 31240e4ef248SJed Brown 31250e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 31260e4ef248SJed Brown @*/ 31270910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 31280e4ef248SJed Brown { 31290e4ef248SJed Brown PetscErrorCode ierr; 31300e4ef248SJed Brown Mat Arhs,Brhs; 31310e4ef248SJed Brown MatStructure flg2; 31320e4ef248SJed Brown 31330e4ef248SJed Brown PetscFunctionBegin; 31340e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 31350910c330SBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 31360910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 31370e4ef248SJed Brown PetscFunctionReturn(0); 31380e4ef248SJed Brown } 31390e4ef248SJed Brown 31400e4ef248SJed Brown #undef __FUNCT__ 31410e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 31420e4ef248SJed Brown /*@C 31430e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 31440e4ef248SJed Brown 31450e4ef248SJed Brown Collective on TS 31460e4ef248SJed Brown 31470e4ef248SJed Brown Input Arguments: 31480e4ef248SJed Brown + ts - time stepping context 31490e4ef248SJed Brown . t - time at which to evaluate 31500910c330SBarry Smith . U - state at which to evaluate 31510e4ef248SJed Brown - ctx - context 31520e4ef248SJed Brown 31530e4ef248SJed Brown Output Arguments: 31540e4ef248SJed Brown + A - pointer to operator 31550e4ef248SJed Brown . B - pointer to preconditioning matrix 31560e4ef248SJed Brown - flg - matrix structure flag 31570e4ef248SJed Brown 31580e4ef248SJed Brown Level: intermediate 31590e4ef248SJed Brown 31600e4ef248SJed Brown Notes: 31610e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 31620e4ef248SJed Brown 31630e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 31640e4ef248SJed Brown @*/ 31650910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx) 31660e4ef248SJed Brown { 31670e4ef248SJed Brown PetscFunctionBegin; 31680e4ef248SJed Brown *flg = SAME_PRECONDITIONER; 31690e4ef248SJed Brown PetscFunctionReturn(0); 31700e4ef248SJed Brown } 31710e4ef248SJed Brown 31720026cea9SSean Farley #undef __FUNCT__ 31730026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 31740026cea9SSean Farley /*@C 31750026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 31760026cea9SSean Farley 31770026cea9SSean Farley Collective on TS 31780026cea9SSean Farley 31790026cea9SSean Farley Input Arguments: 31800026cea9SSean Farley + ts - time stepping context 31810026cea9SSean Farley . t - time at which to evaluate 31820910c330SBarry Smith . U - state at which to evaluate 31830910c330SBarry Smith . Udot - time derivative of state vector 31840026cea9SSean Farley - ctx - context 31850026cea9SSean Farley 31860026cea9SSean Farley Output Arguments: 31870026cea9SSean Farley . F - left hand side 31880026cea9SSean Farley 31890026cea9SSean Farley Level: intermediate 31900026cea9SSean Farley 31910026cea9SSean Farley Notes: 31920910c330SBarry 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 31930026cea9SSean Farley user is required to write their own TSComputeIFunction. 31940026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 31950026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 31960026cea9SSean Farley 31970026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 31980026cea9SSean Farley @*/ 31990910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 32000026cea9SSean Farley { 32010026cea9SSean Farley PetscErrorCode ierr; 32020026cea9SSean Farley Mat A,B; 32030026cea9SSean Farley MatStructure flg2; 32040026cea9SSean Farley 32050026cea9SSean Farley PetscFunctionBegin; 32060026cea9SSean Farley ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 32070910c330SBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr); 32080910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 32090026cea9SSean Farley PetscFunctionReturn(0); 32100026cea9SSean Farley } 32110026cea9SSean Farley 32120026cea9SSean Farley #undef __FUNCT__ 32130026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 32140026cea9SSean Farley /*@C 321524e94211SJed Brown TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent. 32160026cea9SSean Farley 32170026cea9SSean Farley Collective on TS 32180026cea9SSean Farley 32190026cea9SSean Farley Input Arguments: 32200026cea9SSean Farley + ts - time stepping context 32210026cea9SSean Farley . t - time at which to evaluate 32220910c330SBarry Smith . U - state at which to evaluate 32230910c330SBarry Smith . Udot - time derivative of state vector 32240026cea9SSean Farley . shift - shift to apply 32250026cea9SSean Farley - ctx - context 32260026cea9SSean Farley 32270026cea9SSean Farley Output Arguments: 32280026cea9SSean Farley + A - pointer to operator 32290026cea9SSean Farley . B - pointer to preconditioning matrix 32300026cea9SSean Farley - flg - matrix structure flag 32310026cea9SSean Farley 32320026cea9SSean Farley Level: intermediate 32330026cea9SSean Farley 32340026cea9SSean Farley Notes: 32350026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 32360026cea9SSean Farley 32370026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 32380026cea9SSean Farley @*/ 32390910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx) 32400026cea9SSean Farley { 32410026cea9SSean Farley PetscFunctionBegin; 32420026cea9SSean Farley *flg = SAME_PRECONDITIONER; 32430026cea9SSean Farley PetscFunctionReturn(0); 32440026cea9SSean Farley } 32450026cea9SSean Farley 32464af1b03aSJed Brown #undef __FUNCT__ 32474af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 32484af1b03aSJed Brown /*@ 32494af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 32504af1b03aSJed Brown 32514af1b03aSJed Brown Not Collective 32524af1b03aSJed Brown 32534af1b03aSJed Brown Input Parameter: 32544af1b03aSJed Brown . ts - the TS context 32554af1b03aSJed Brown 32564af1b03aSJed Brown Output Parameter: 32574af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 32584af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 32594af1b03aSJed Brown 3260487e0bb9SJed Brown Level: beginner 32614af1b03aSJed Brown 3262cd652676SJed Brown Notes: 3263cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 32644af1b03aSJed Brown 32654af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 32664af1b03aSJed Brown 32674af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 32684af1b03aSJed Brown @*/ 32694af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 32704af1b03aSJed Brown { 32714af1b03aSJed Brown PetscFunctionBegin; 32724af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 32734af1b03aSJed Brown PetscValidPointer(reason,2); 32744af1b03aSJed Brown *reason = ts->reason; 32754af1b03aSJed Brown PetscFunctionReturn(0); 32764af1b03aSJed Brown } 32774af1b03aSJed Brown 3278fb1732b5SBarry Smith #undef __FUNCT__ 3279d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 3280d6ad946cSShri Abhyankar /*@ 3281d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 3282d6ad946cSShri Abhyankar 3283d6ad946cSShri Abhyankar Not Collective 3284d6ad946cSShri Abhyankar 3285d6ad946cSShri Abhyankar Input Parameter: 3286d6ad946cSShri Abhyankar + ts - the TS context 3287d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 3288d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 3289d6ad946cSShri Abhyankar 3290f5abba47SShri Abhyankar Level: advanced 3291d6ad946cSShri Abhyankar 3292d6ad946cSShri Abhyankar Notes: 3293d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 3294d6ad946cSShri Abhyankar 3295d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 3296d6ad946cSShri Abhyankar 3297d6ad946cSShri Abhyankar .seealso: TSConvergedReason 3298d6ad946cSShri Abhyankar @*/ 3299d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 3300d6ad946cSShri Abhyankar { 3301d6ad946cSShri Abhyankar PetscFunctionBegin; 3302d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3303d6ad946cSShri Abhyankar ts->reason = reason; 3304d6ad946cSShri Abhyankar PetscFunctionReturn(0); 3305d6ad946cSShri Abhyankar } 3306d6ad946cSShri Abhyankar 3307d6ad946cSShri Abhyankar #undef __FUNCT__ 3308cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 3309cc708dedSBarry Smith /*@ 3310cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 3311cc708dedSBarry Smith 3312cc708dedSBarry Smith Not Collective 3313cc708dedSBarry Smith 3314cc708dedSBarry Smith Input Parameter: 3315cc708dedSBarry Smith . ts - the TS context 3316cc708dedSBarry Smith 3317cc708dedSBarry Smith Output Parameter: 3318cc708dedSBarry Smith . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 3319cc708dedSBarry Smith 3320487e0bb9SJed Brown Level: beginner 3321cc708dedSBarry Smith 3322cc708dedSBarry Smith Notes: 3323cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 3324cc708dedSBarry Smith 3325cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 3326cc708dedSBarry Smith 3327cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 3328cc708dedSBarry Smith @*/ 3329cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 3330cc708dedSBarry Smith { 3331cc708dedSBarry Smith PetscFunctionBegin; 3332cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3333cc708dedSBarry Smith PetscValidPointer(ftime,2); 3334cc708dedSBarry Smith *ftime = ts->solvetime; 3335cc708dedSBarry Smith PetscFunctionReturn(0); 3336cc708dedSBarry Smith } 3337cc708dedSBarry Smith 3338cc708dedSBarry Smith #undef __FUNCT__ 33395ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 33409f67acb7SJed Brown /*@ 33415ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 33429f67acb7SJed Brown used by the time integrator. 33439f67acb7SJed Brown 33449f67acb7SJed Brown Not Collective 33459f67acb7SJed Brown 33469f67acb7SJed Brown Input Parameter: 33479f67acb7SJed Brown . ts - TS context 33489f67acb7SJed Brown 33499f67acb7SJed Brown Output Parameter: 33509f67acb7SJed Brown . nits - number of nonlinear iterations 33519f67acb7SJed Brown 33529f67acb7SJed Brown Notes: 33539f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 33549f67acb7SJed Brown 33559f67acb7SJed Brown Level: intermediate 33569f67acb7SJed Brown 33579f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 33589f67acb7SJed Brown 33595ef26d82SJed Brown .seealso: TSGetKSPIterations() 33609f67acb7SJed Brown @*/ 33615ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 33629f67acb7SJed Brown { 33639f67acb7SJed Brown PetscFunctionBegin; 33649f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 33659f67acb7SJed Brown PetscValidIntPointer(nits,2); 33665ef26d82SJed Brown *nits = ts->snes_its; 33679f67acb7SJed Brown PetscFunctionReturn(0); 33689f67acb7SJed Brown } 33699f67acb7SJed Brown 33709f67acb7SJed Brown #undef __FUNCT__ 33715ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 33729f67acb7SJed Brown /*@ 33735ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 33749f67acb7SJed Brown used by the time integrator. 33759f67acb7SJed Brown 33769f67acb7SJed Brown Not Collective 33779f67acb7SJed Brown 33789f67acb7SJed Brown Input Parameter: 33799f67acb7SJed Brown . ts - TS context 33809f67acb7SJed Brown 33819f67acb7SJed Brown Output Parameter: 33829f67acb7SJed Brown . lits - number of linear iterations 33839f67acb7SJed Brown 33849f67acb7SJed Brown Notes: 33859f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 33869f67acb7SJed Brown 33879f67acb7SJed Brown Level: intermediate 33889f67acb7SJed Brown 33899f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 33909f67acb7SJed Brown 33915ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 33929f67acb7SJed Brown @*/ 33935ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 33949f67acb7SJed Brown { 33959f67acb7SJed Brown PetscFunctionBegin; 33969f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 33979f67acb7SJed Brown PetscValidIntPointer(lits,2); 33985ef26d82SJed Brown *lits = ts->ksp_its; 33999f67acb7SJed Brown PetscFunctionReturn(0); 34009f67acb7SJed Brown } 34019f67acb7SJed Brown 34029f67acb7SJed Brown #undef __FUNCT__ 3403cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 3404cef5090cSJed Brown /*@ 3405cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 3406cef5090cSJed Brown 3407cef5090cSJed Brown Not Collective 3408cef5090cSJed Brown 3409cef5090cSJed Brown Input Parameter: 3410cef5090cSJed Brown . ts - TS context 3411cef5090cSJed Brown 3412cef5090cSJed Brown Output Parameter: 3413cef5090cSJed Brown . rejects - number of steps rejected 3414cef5090cSJed Brown 3415cef5090cSJed Brown Notes: 3416cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 3417cef5090cSJed Brown 3418cef5090cSJed Brown Level: intermediate 3419cef5090cSJed Brown 3420cef5090cSJed Brown .keywords: TS, get, number 3421cef5090cSJed Brown 34225ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 3423cef5090cSJed Brown @*/ 3424cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 3425cef5090cSJed Brown { 3426cef5090cSJed Brown PetscFunctionBegin; 3427cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3428cef5090cSJed Brown PetscValidIntPointer(rejects,2); 3429cef5090cSJed Brown *rejects = ts->reject; 3430cef5090cSJed Brown PetscFunctionReturn(0); 3431cef5090cSJed Brown } 3432cef5090cSJed Brown 3433cef5090cSJed Brown #undef __FUNCT__ 3434cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 3435cef5090cSJed Brown /*@ 3436cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 3437cef5090cSJed Brown 3438cef5090cSJed Brown Not Collective 3439cef5090cSJed Brown 3440cef5090cSJed Brown Input Parameter: 3441cef5090cSJed Brown . ts - TS context 3442cef5090cSJed Brown 3443cef5090cSJed Brown Output Parameter: 3444cef5090cSJed Brown . fails - number of failed nonlinear solves 3445cef5090cSJed Brown 3446cef5090cSJed Brown Notes: 3447cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 3448cef5090cSJed Brown 3449cef5090cSJed Brown Level: intermediate 3450cef5090cSJed Brown 3451cef5090cSJed Brown .keywords: TS, get, number 3452cef5090cSJed Brown 34535ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 3454cef5090cSJed Brown @*/ 3455cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 3456cef5090cSJed Brown { 3457cef5090cSJed Brown PetscFunctionBegin; 3458cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3459cef5090cSJed Brown PetscValidIntPointer(fails,2); 3460cef5090cSJed Brown *fails = ts->num_snes_failures; 3461cef5090cSJed Brown PetscFunctionReturn(0); 3462cef5090cSJed Brown } 3463cef5090cSJed Brown 3464cef5090cSJed Brown #undef __FUNCT__ 3465cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 3466cef5090cSJed Brown /*@ 3467cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 3468cef5090cSJed Brown 3469cef5090cSJed Brown Not Collective 3470cef5090cSJed Brown 3471cef5090cSJed Brown Input Parameter: 3472cef5090cSJed Brown + ts - TS context 3473cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 3474cef5090cSJed Brown 3475cef5090cSJed Brown Notes: 3476cef5090cSJed Brown The counter is reset to zero for each step 3477cef5090cSJed Brown 3478cef5090cSJed Brown Options Database Key: 3479cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 3480cef5090cSJed Brown 3481cef5090cSJed Brown Level: intermediate 3482cef5090cSJed Brown 3483cef5090cSJed Brown .keywords: TS, set, maximum, number 3484cef5090cSJed Brown 34855ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3486cef5090cSJed Brown @*/ 3487cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 3488cef5090cSJed Brown { 3489cef5090cSJed Brown PetscFunctionBegin; 3490cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3491cef5090cSJed Brown ts->max_reject = rejects; 3492cef5090cSJed Brown PetscFunctionReturn(0); 3493cef5090cSJed Brown } 3494cef5090cSJed Brown 3495cef5090cSJed Brown #undef __FUNCT__ 3496cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 3497cef5090cSJed Brown /*@ 3498cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 3499cef5090cSJed Brown 3500cef5090cSJed Brown Not Collective 3501cef5090cSJed Brown 3502cef5090cSJed Brown Input Parameter: 3503cef5090cSJed Brown + ts - TS context 3504cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 3505cef5090cSJed Brown 3506cef5090cSJed Brown Notes: 3507cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 3508cef5090cSJed Brown 3509cef5090cSJed Brown Options Database Key: 3510cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 3511cef5090cSJed Brown 3512cef5090cSJed Brown Level: intermediate 3513cef5090cSJed Brown 3514cef5090cSJed Brown .keywords: TS, set, maximum, number 3515cef5090cSJed Brown 35165ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 3517cef5090cSJed Brown @*/ 3518cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 3519cef5090cSJed Brown { 3520cef5090cSJed Brown PetscFunctionBegin; 3521cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3522cef5090cSJed Brown ts->max_snes_failures = fails; 3523cef5090cSJed Brown PetscFunctionReturn(0); 3524cef5090cSJed Brown } 3525cef5090cSJed Brown 3526cef5090cSJed Brown #undef __FUNCT__ 3527cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()" 3528cef5090cSJed Brown /*@ 3529cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 3530cef5090cSJed Brown 3531cef5090cSJed Brown Not Collective 3532cef5090cSJed Brown 3533cef5090cSJed Brown Input Parameter: 3534cef5090cSJed Brown + ts - TS context 3535cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 3536cef5090cSJed Brown 3537cef5090cSJed Brown Options Database Key: 3538cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 3539cef5090cSJed Brown 3540cef5090cSJed Brown Level: intermediate 3541cef5090cSJed Brown 3542cef5090cSJed Brown .keywords: TS, set, error 3543cef5090cSJed Brown 35445ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3545cef5090cSJed Brown @*/ 3546cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 3547cef5090cSJed Brown { 3548cef5090cSJed Brown PetscFunctionBegin; 3549cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3550cef5090cSJed Brown ts->errorifstepfailed = err; 3551cef5090cSJed Brown PetscFunctionReturn(0); 3552cef5090cSJed Brown } 3553cef5090cSJed Brown 3554cef5090cSJed Brown #undef __FUNCT__ 3555fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 3556fb1732b5SBarry Smith /*@C 3557fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 3558fb1732b5SBarry Smith 3559fb1732b5SBarry Smith Collective on TS 3560fb1732b5SBarry Smith 3561fb1732b5SBarry Smith Input Parameters: 3562fb1732b5SBarry Smith + ts - the TS context 3563fb1732b5SBarry Smith . step - current time-step 3564fb1732b5SBarry Smith . ptime - current time 35650910c330SBarry Smith . u - current state 3566fb1732b5SBarry Smith - viewer - binary viewer 3567fb1732b5SBarry Smith 3568fb1732b5SBarry Smith Level: intermediate 3569fb1732b5SBarry Smith 3570fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 3571fb1732b5SBarry Smith 3572fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3573fb1732b5SBarry Smith @*/ 35740910c330SBarry Smith PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 3575fb1732b5SBarry Smith { 3576fb1732b5SBarry Smith PetscErrorCode ierr; 3577ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 3578fb1732b5SBarry Smith 3579fb1732b5SBarry Smith PetscFunctionBegin; 35800910c330SBarry Smith ierr = VecView(u,v);CHKERRQ(ierr); 3581ed81e22dSJed Brown PetscFunctionReturn(0); 3582ed81e22dSJed Brown } 3583ed81e22dSJed Brown 3584ed81e22dSJed Brown #undef __FUNCT__ 3585ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 3586ed81e22dSJed Brown /*@C 3587ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 3588ed81e22dSJed Brown 3589ed81e22dSJed Brown Collective on TS 3590ed81e22dSJed Brown 3591ed81e22dSJed Brown Input Parameters: 3592ed81e22dSJed Brown + ts - the TS context 3593ed81e22dSJed Brown . step - current time-step 3594ed81e22dSJed Brown . ptime - current time 35950910c330SBarry Smith . u - current state 3596ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3597ed81e22dSJed Brown 3598ed81e22dSJed Brown Level: intermediate 3599ed81e22dSJed Brown 3600ed81e22dSJed Brown Notes: 3601ed81e22dSJed 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. 3602ed81e22dSJed Brown These are named according to the file name template. 3603ed81e22dSJed Brown 3604ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 3605ed81e22dSJed Brown 3606ed81e22dSJed Brown .keywords: TS, vector, monitor, view 3607ed81e22dSJed Brown 3608ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3609ed81e22dSJed Brown @*/ 36100910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 3611ed81e22dSJed Brown { 3612ed81e22dSJed Brown PetscErrorCode ierr; 3613ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 3614ed81e22dSJed Brown PetscViewer viewer; 3615ed81e22dSJed Brown 3616ed81e22dSJed Brown PetscFunctionBegin; 36178caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 3618ed81e22dSJed Brown ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 36190910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 3620ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 3621ed81e22dSJed Brown PetscFunctionReturn(0); 3622ed81e22dSJed Brown } 3623ed81e22dSJed Brown 3624ed81e22dSJed Brown #undef __FUNCT__ 3625ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 3626ed81e22dSJed Brown /*@C 3627ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 3628ed81e22dSJed Brown 3629ed81e22dSJed Brown Collective on TS 3630ed81e22dSJed Brown 3631ed81e22dSJed Brown Input Parameters: 3632ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3633ed81e22dSJed Brown 3634ed81e22dSJed Brown Level: intermediate 3635ed81e22dSJed Brown 3636ed81e22dSJed Brown Note: 3637ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 3638ed81e22dSJed Brown 3639ed81e22dSJed Brown .keywords: TS, vector, monitor, view 3640ed81e22dSJed Brown 3641ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 3642ed81e22dSJed Brown @*/ 3643ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 3644ed81e22dSJed Brown { 3645ed81e22dSJed Brown PetscErrorCode ierr; 3646ed81e22dSJed Brown 3647ed81e22dSJed Brown PetscFunctionBegin; 3648ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 3649fb1732b5SBarry Smith PetscFunctionReturn(0); 3650fb1732b5SBarry Smith } 3651fb1732b5SBarry Smith 365284df9cb4SJed Brown #undef __FUNCT__ 3653ad6bc421SBarry Smith #define __FUNCT__ "TSGetTSAdapt" 365484df9cb4SJed Brown /*@ 3655ad6bc421SBarry Smith TSGetTSAdapt - Get the adaptive controller context for the current method 365684df9cb4SJed Brown 3657ed81e22dSJed Brown Collective on TS if controller has not been created yet 365884df9cb4SJed Brown 365984df9cb4SJed Brown Input Arguments: 3660ed81e22dSJed Brown . ts - time stepping context 366184df9cb4SJed Brown 366284df9cb4SJed Brown Output Arguments: 3663ed81e22dSJed Brown . adapt - adaptive controller 366484df9cb4SJed Brown 366584df9cb4SJed Brown Level: intermediate 366684df9cb4SJed Brown 3667ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 366884df9cb4SJed Brown @*/ 3669ad6bc421SBarry Smith PetscErrorCode TSGetTSAdapt(TS ts,TSAdapt *adapt) 367084df9cb4SJed Brown { 367184df9cb4SJed Brown PetscErrorCode ierr; 367284df9cb4SJed Brown 367384df9cb4SJed Brown PetscFunctionBegin; 367484df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 367584df9cb4SJed Brown PetscValidPointer(adapt,2); 367684df9cb4SJed Brown if (!ts->adapt) { 367784df9cb4SJed Brown ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr); 36781c3436cfSJed Brown ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr); 36791c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 368084df9cb4SJed Brown } 368184df9cb4SJed Brown *adapt = ts->adapt; 368284df9cb4SJed Brown PetscFunctionReturn(0); 368384df9cb4SJed Brown } 3684d6ebe24aSShri Abhyankar 3685d6ebe24aSShri Abhyankar #undef __FUNCT__ 36861c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 36871c3436cfSJed Brown /*@ 36881c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 36891c3436cfSJed Brown 36901c3436cfSJed Brown Logically Collective 36911c3436cfSJed Brown 36921c3436cfSJed Brown Input Arguments: 36931c3436cfSJed Brown + ts - time integration context 36941c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 36951c3436cfSJed Brown . vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present 36961c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 36971c3436cfSJed Brown - vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present 36981c3436cfSJed Brown 36991c3436cfSJed Brown Level: beginner 37001c3436cfSJed Brown 3701c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 37021c3436cfSJed Brown @*/ 37031c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 37041c3436cfSJed Brown { 37051c3436cfSJed Brown PetscErrorCode ierr; 37061c3436cfSJed Brown 37071c3436cfSJed Brown PetscFunctionBegin; 3708c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 37091c3436cfSJed Brown if (vatol) { 37101c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 37111c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 37121c3436cfSJed Brown ts->vatol = vatol; 37131c3436cfSJed Brown } 3714c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 37151c3436cfSJed Brown if (vrtol) { 37161c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 37171c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 37181c3436cfSJed Brown ts->vrtol = vrtol; 37191c3436cfSJed Brown } 37201c3436cfSJed Brown PetscFunctionReturn(0); 37211c3436cfSJed Brown } 37221c3436cfSJed Brown 37231c3436cfSJed Brown #undef __FUNCT__ 3724c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 3725c5033834SJed Brown /*@ 3726c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 3727c5033834SJed Brown 3728c5033834SJed Brown Logically Collective 3729c5033834SJed Brown 3730c5033834SJed Brown Input Arguments: 3731c5033834SJed Brown . ts - time integration context 3732c5033834SJed Brown 3733c5033834SJed Brown Output Arguments: 3734c5033834SJed Brown + atol - scalar absolute tolerances, PETSC_NULL to ignore 3735c5033834SJed Brown . vatol - vector of absolute tolerances, PETSC_NULL to ignore 3736c5033834SJed Brown . rtol - scalar relative tolerances, PETSC_NULL to ignore 3737c5033834SJed Brown - vrtol - vector of relative tolerances, PETSC_NULL to ignore 3738c5033834SJed Brown 3739c5033834SJed Brown Level: beginner 3740c5033834SJed Brown 3741c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 3742c5033834SJed Brown @*/ 3743c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 3744c5033834SJed Brown { 3745c5033834SJed Brown PetscFunctionBegin; 3746c5033834SJed Brown if (atol) *atol = ts->atol; 3747c5033834SJed Brown if (vatol) *vatol = ts->vatol; 3748c5033834SJed Brown if (rtol) *rtol = ts->rtol; 3749c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 3750c5033834SJed Brown PetscFunctionReturn(0); 3751c5033834SJed Brown } 3752c5033834SJed Brown 3753c5033834SJed Brown #undef __FUNCT__ 37541c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS" 37551c3436cfSJed Brown /*@ 37561c3436cfSJed Brown TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 37571c3436cfSJed Brown 37581c3436cfSJed Brown Collective on TS 37591c3436cfSJed Brown 37601c3436cfSJed Brown Input Arguments: 37611c3436cfSJed Brown + ts - time stepping context 37621c3436cfSJed Brown - Y - state vector to be compared to ts->vec_sol 37631c3436cfSJed Brown 37641c3436cfSJed Brown Output Arguments: 37651c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 37661c3436cfSJed Brown 37671c3436cfSJed Brown Level: developer 37681c3436cfSJed Brown 37691c3436cfSJed Brown .seealso: TSSetTolerances() 37701c3436cfSJed Brown @*/ 37711c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 37721c3436cfSJed Brown { 37731c3436cfSJed Brown PetscErrorCode ierr; 37741c3436cfSJed Brown PetscInt i,n,N; 37750910c330SBarry Smith const PetscScalar *u,*y; 37760910c330SBarry Smith Vec U; 37771c3436cfSJed Brown PetscReal sum,gsum; 37781c3436cfSJed Brown 37791c3436cfSJed Brown PetscFunctionBegin; 37801c3436cfSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 37811c3436cfSJed Brown PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 37821c3436cfSJed Brown PetscValidPointer(norm,3); 37830910c330SBarry Smith U = ts->vec_sol; 37840910c330SBarry Smith PetscCheckSameTypeAndComm(U,1,Y,2); 37850910c330SBarry Smith if (U == Y) SETERRQ(((PetscObject)U)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 37861c3436cfSJed Brown 37870910c330SBarry Smith ierr = VecGetSize(U,&N);CHKERRQ(ierr); 37880910c330SBarry Smith ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 37890910c330SBarry Smith ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 37901c3436cfSJed Brown ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 37911c3436cfSJed Brown sum = 0.; 3792ceefc113SJed Brown if (ts->vatol && ts->vrtol) { 3793ceefc113SJed Brown const PetscScalar *atol,*rtol; 3794ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3795ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3796ceefc113SJed Brown for (i=0; i<n; i++) { 37970910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 37980910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3799ceefc113SJed Brown } 3800ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3801ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3802ceefc113SJed Brown } else if (ts->vatol) { /* vector atol, scalar rtol */ 3803ceefc113SJed Brown const PetscScalar *atol; 3804ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3805ceefc113SJed Brown for (i=0; i<n; i++) { 38060910c330SBarry Smith PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 38070910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3808ceefc113SJed Brown } 3809ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3810ceefc113SJed Brown } else if (ts->vrtol) { /* scalar atol, vector rtol */ 3811ceefc113SJed Brown const PetscScalar *rtol; 3812ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3813ceefc113SJed Brown for (i=0; i<n; i++) { 38140910c330SBarry Smith PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 38150910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 3816ceefc113SJed Brown } 3817ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3818ceefc113SJed Brown } else { /* scalar atol, scalar rtol */ 38191c3436cfSJed Brown for (i=0; i<n; i++) { 38200910c330SBarry Smith PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 38210910c330SBarry Smith sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 38221c3436cfSJed Brown } 3823ceefc113SJed Brown } 38240910c330SBarry Smith ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 38251c3436cfSJed Brown ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 38261c3436cfSJed Brown 38271c3436cfSJed Brown ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr); 38281c3436cfSJed Brown *norm = PetscSqrtReal(gsum / N); 38291c3436cfSJed Brown if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 38301c3436cfSJed Brown PetscFunctionReturn(0); 38311c3436cfSJed Brown } 38321c3436cfSJed Brown 38331c3436cfSJed Brown #undef __FUNCT__ 38348d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 38358d59e960SJed Brown /*@ 38368d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 38378d59e960SJed Brown 38388d59e960SJed Brown Logically Collective on TS 38398d59e960SJed Brown 38408d59e960SJed Brown Input Arguments: 38418d59e960SJed Brown + ts - time stepping context 38428d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 38438d59e960SJed Brown 38448d59e960SJed Brown Note: 38458d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 38468d59e960SJed Brown 38478d59e960SJed Brown Level: intermediate 38488d59e960SJed Brown 38498d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 38508d59e960SJed Brown @*/ 38518d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 38528d59e960SJed Brown { 38538d59e960SJed Brown PetscFunctionBegin; 38548d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 38558d59e960SJed Brown ts->cfltime_local = cfltime; 38568d59e960SJed Brown ts->cfltime = -1.; 38578d59e960SJed Brown PetscFunctionReturn(0); 38588d59e960SJed Brown } 38598d59e960SJed Brown 38608d59e960SJed Brown #undef __FUNCT__ 38618d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 38628d59e960SJed Brown /*@ 38638d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 38648d59e960SJed Brown 38658d59e960SJed Brown Collective on TS 38668d59e960SJed Brown 38678d59e960SJed Brown Input Arguments: 38688d59e960SJed Brown . ts - time stepping context 38698d59e960SJed Brown 38708d59e960SJed Brown Output Arguments: 38718d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 38728d59e960SJed Brown 38738d59e960SJed Brown Level: advanced 38748d59e960SJed Brown 38758d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 38768d59e960SJed Brown @*/ 38778d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 38788d59e960SJed Brown { 38798d59e960SJed Brown PetscErrorCode ierr; 38808d59e960SJed Brown 38818d59e960SJed Brown PetscFunctionBegin; 38828d59e960SJed Brown if (ts->cfltime < 0) { 38838d59e960SJed Brown ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr); 38848d59e960SJed Brown } 38858d59e960SJed Brown *cfltime = ts->cfltime; 38868d59e960SJed Brown PetscFunctionReturn(0); 38878d59e960SJed Brown } 38888d59e960SJed Brown 38898d59e960SJed Brown #undef __FUNCT__ 3890d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 3891d6ebe24aSShri Abhyankar /*@ 3892d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 3893d6ebe24aSShri Abhyankar 3894d6ebe24aSShri Abhyankar Input Parameters: 3895d6ebe24aSShri Abhyankar . ts - the TS context. 3896d6ebe24aSShri Abhyankar . xl - lower bound. 3897d6ebe24aSShri Abhyankar . xu - upper bound. 3898d6ebe24aSShri Abhyankar 3899d6ebe24aSShri Abhyankar Notes: 3900d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 390133c0c2ddSJed Brown SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp(). 3902d6ebe24aSShri Abhyankar 39032bd2b0e6SSatish Balay Level: advanced 39042bd2b0e6SSatish Balay 3905d6ebe24aSShri Abhyankar @*/ 3906d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 3907d6ebe24aSShri Abhyankar { 3908d6ebe24aSShri Abhyankar PetscErrorCode ierr; 3909d6ebe24aSShri Abhyankar SNES snes; 3910d6ebe24aSShri Abhyankar 3911d6ebe24aSShri Abhyankar PetscFunctionBegin; 3912d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3913d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 3914d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 3915d6ebe24aSShri Abhyankar } 3916d6ebe24aSShri Abhyankar 3917325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 3918c6db04a5SJed Brown #include <mex.h> 3919325fc9f4SBarry Smith 3920325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 3921325fc9f4SBarry Smith 3922325fc9f4SBarry Smith #undef __FUNCT__ 3923325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 3924325fc9f4SBarry Smith /* 3925325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 3926325fc9f4SBarry Smith TSSetFunctionMatlab(). 3927325fc9f4SBarry Smith 3928325fc9f4SBarry Smith Collective on TS 3929325fc9f4SBarry Smith 3930325fc9f4SBarry Smith Input Parameters: 3931325fc9f4SBarry Smith + snes - the TS context 39320910c330SBarry Smith - u - input vector 3933325fc9f4SBarry Smith 3934325fc9f4SBarry Smith Output Parameter: 3935325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 3936325fc9f4SBarry Smith 3937325fc9f4SBarry Smith Notes: 3938325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 3939325fc9f4SBarry Smith implementations, so most users would not generally call this routine 3940325fc9f4SBarry Smith themselves. 3941325fc9f4SBarry Smith 3942325fc9f4SBarry Smith Level: developer 3943325fc9f4SBarry Smith 3944325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 3945325fc9f4SBarry Smith 3946325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3947325fc9f4SBarry Smith */ 39480910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 3949325fc9f4SBarry Smith { 3950325fc9f4SBarry Smith PetscErrorCode ierr; 3951325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3952325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 3953325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 3954325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 3955325fc9f4SBarry Smith 3956325fc9f4SBarry Smith PetscFunctionBegin; 3957325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 39580910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 39590910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 3960325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 39610910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 3962325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 3963325fc9f4SBarry Smith 3964325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 39650910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 39660910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 39670910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 3968325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3969325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 3970325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 3971325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 3972325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 3973325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 3974325fc9f4SBarry Smith prhs[6] = sctx->ctx; 3975325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 3976325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3977325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 3978325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 3979325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 3980325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 3981325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 3982325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 3983325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 3984325fc9f4SBarry Smith PetscFunctionReturn(0); 3985325fc9f4SBarry Smith } 3986325fc9f4SBarry Smith 3987325fc9f4SBarry Smith 3988325fc9f4SBarry Smith #undef __FUNCT__ 3989325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 3990325fc9f4SBarry Smith /* 3991325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 3992325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 3993e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 3994325fc9f4SBarry Smith 3995325fc9f4SBarry Smith Logically Collective on TS 3996325fc9f4SBarry Smith 3997325fc9f4SBarry Smith Input Parameters: 3998325fc9f4SBarry Smith + ts - the TS context 3999325fc9f4SBarry Smith - func - function evaluation routine 4000325fc9f4SBarry Smith 4001325fc9f4SBarry Smith Calling sequence of func: 40020910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 4003325fc9f4SBarry Smith 4004325fc9f4SBarry Smith Level: beginner 4005325fc9f4SBarry Smith 4006325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 4007325fc9f4SBarry Smith 4008325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4009325fc9f4SBarry Smith */ 4010cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 4011325fc9f4SBarry Smith { 4012325fc9f4SBarry Smith PetscErrorCode ierr; 4013325fc9f4SBarry Smith TSMatlabContext *sctx; 4014325fc9f4SBarry Smith 4015325fc9f4SBarry Smith PetscFunctionBegin; 4016325fc9f4SBarry Smith /* currently sctx is memory bleed */ 4017325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4018325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4019325fc9f4SBarry Smith /* 4020325fc9f4SBarry Smith This should work, but it doesn't 4021325fc9f4SBarry Smith sctx->ctx = ctx; 4022325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4023325fc9f4SBarry Smith */ 4024325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4025cdcf91faSSean Farley ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 4026325fc9f4SBarry Smith PetscFunctionReturn(0); 4027325fc9f4SBarry Smith } 4028325fc9f4SBarry Smith 4029325fc9f4SBarry Smith #undef __FUNCT__ 4030325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 4031325fc9f4SBarry Smith /* 4032325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 4033325fc9f4SBarry Smith TSSetJacobianMatlab(). 4034325fc9f4SBarry Smith 4035325fc9f4SBarry Smith Collective on TS 4036325fc9f4SBarry Smith 4037325fc9f4SBarry Smith Input Parameters: 4038cdcf91faSSean Farley + ts - the TS context 40390910c330SBarry Smith . u - input vector 4040325fc9f4SBarry Smith . A, B - the matrices 4041325fc9f4SBarry Smith - ctx - user context 4042325fc9f4SBarry Smith 4043325fc9f4SBarry Smith Output Parameter: 4044325fc9f4SBarry Smith . flag - structure of the matrix 4045325fc9f4SBarry Smith 4046325fc9f4SBarry Smith Level: developer 4047325fc9f4SBarry Smith 4048325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 4049325fc9f4SBarry Smith 4050325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 4051325fc9f4SBarry Smith @*/ 40520910c330SBarry Smith PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 4053325fc9f4SBarry Smith { 4054325fc9f4SBarry Smith PetscErrorCode ierr; 4055325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 4056325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 4057325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 4058325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 4059325fc9f4SBarry Smith 4060325fc9f4SBarry Smith PetscFunctionBegin; 4061cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 40620910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 4063325fc9f4SBarry Smith 40640910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 4065325fc9f4SBarry Smith 4066cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 40670910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 40680910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 40690910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 40700910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 4071325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 4072325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 4073325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 4074325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 4075325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 4076325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 4077325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 4078325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 4079325fc9f4SBarry Smith prhs[8] = sctx->ctx; 4080325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 4081325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4082325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 4083325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 4084325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 4085325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 4086325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 4087325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 4088325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 4089325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 4090325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 4091325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 4092325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 4093325fc9f4SBarry Smith PetscFunctionReturn(0); 4094325fc9f4SBarry Smith } 4095325fc9f4SBarry Smith 4096325fc9f4SBarry Smith 4097325fc9f4SBarry Smith #undef __FUNCT__ 4098325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 4099325fc9f4SBarry Smith /* 4100325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 4101e3c5b3baSBarry 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 4102325fc9f4SBarry Smith 4103325fc9f4SBarry Smith Logically Collective on TS 4104325fc9f4SBarry Smith 4105325fc9f4SBarry Smith Input Parameters: 4106cdcf91faSSean Farley + ts - the TS context 4107325fc9f4SBarry Smith . A,B - Jacobian matrices 4108325fc9f4SBarry Smith . func - function evaluation routine 4109325fc9f4SBarry Smith - ctx - user context 4110325fc9f4SBarry Smith 4111325fc9f4SBarry Smith Calling sequence of func: 41120910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 4113325fc9f4SBarry Smith 4114325fc9f4SBarry Smith 4115325fc9f4SBarry Smith Level: developer 4116325fc9f4SBarry Smith 4117325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 4118325fc9f4SBarry Smith 4119325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4120325fc9f4SBarry Smith */ 4121cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 4122325fc9f4SBarry Smith { 4123325fc9f4SBarry Smith PetscErrorCode ierr; 4124325fc9f4SBarry Smith TSMatlabContext *sctx; 4125325fc9f4SBarry Smith 4126325fc9f4SBarry Smith PetscFunctionBegin; 4127325fc9f4SBarry Smith /* currently sctx is memory bleed */ 4128325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4129325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4130325fc9f4SBarry Smith /* 4131325fc9f4SBarry Smith This should work, but it doesn't 4132325fc9f4SBarry Smith sctx->ctx = ctx; 4133325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4134325fc9f4SBarry Smith */ 4135325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4136cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 4137325fc9f4SBarry Smith PetscFunctionReturn(0); 4138325fc9f4SBarry Smith } 4139325fc9f4SBarry Smith 4140b5b1a830SBarry Smith #undef __FUNCT__ 4141b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 4142b5b1a830SBarry Smith /* 4143b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 4144b5b1a830SBarry Smith 4145b5b1a830SBarry Smith Collective on TS 4146b5b1a830SBarry Smith 4147b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 4148b5b1a830SBarry Smith @*/ 41490910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 4150b5b1a830SBarry Smith { 4151b5b1a830SBarry Smith PetscErrorCode ierr; 4152b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 4153a530c242SBarry Smith int nlhs = 1,nrhs = 6; 4154b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 4155b5b1a830SBarry Smith long long int lx = 0,ls = 0; 4156b5b1a830SBarry Smith 4157b5b1a830SBarry Smith PetscFunctionBegin; 4158cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 41590910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 4160b5b1a830SBarry Smith 4161cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 41620910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 4163b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 4164b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 4165b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 4166b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 4167b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 4168b5b1a830SBarry Smith prhs[5] = sctx->ctx; 4169b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 4170b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4171b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 4172b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 4173b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 4174b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 4175b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 4176b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 4177b5b1a830SBarry Smith PetscFunctionReturn(0); 4178b5b1a830SBarry Smith } 4179b5b1a830SBarry Smith 4180b5b1a830SBarry Smith 4181b5b1a830SBarry Smith #undef __FUNCT__ 4182b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 4183b5b1a830SBarry Smith /* 4184b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 4185b5b1a830SBarry Smith 4186b5b1a830SBarry Smith Level: developer 4187b5b1a830SBarry Smith 4188b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 4189b5b1a830SBarry Smith 4190b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4191b5b1a830SBarry Smith */ 4192cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 4193b5b1a830SBarry Smith { 4194b5b1a830SBarry Smith PetscErrorCode ierr; 4195b5b1a830SBarry Smith TSMatlabContext *sctx; 4196b5b1a830SBarry Smith 4197b5b1a830SBarry Smith PetscFunctionBegin; 4198b5b1a830SBarry Smith /* currently sctx is memory bleed */ 4199b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4200b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4201b5b1a830SBarry Smith /* 4202b5b1a830SBarry Smith This should work, but it doesn't 4203b5b1a830SBarry Smith sctx->ctx = ctx; 4204b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4205b5b1a830SBarry Smith */ 4206b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4207cdcf91faSSean Farley ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 4208b5b1a830SBarry Smith PetscFunctionReturn(0); 4209b5b1a830SBarry Smith } 4210325fc9f4SBarry Smith #endif 4211b3603a34SBarry Smith 42120b039ecaSBarry Smith 42130b039ecaSBarry Smith 4214b3603a34SBarry Smith #undef __FUNCT__ 42154f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 4216b3603a34SBarry Smith /*@C 42174f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 4218b3603a34SBarry Smith in a time based line graph 4219b3603a34SBarry Smith 4220b3603a34SBarry Smith Collective on TS 4221b3603a34SBarry Smith 4222b3603a34SBarry Smith Input Parameters: 4223b3603a34SBarry Smith + ts - the TS context 4224b3603a34SBarry Smith . step - current time-step 4225b3603a34SBarry Smith . ptime - current time 4226b3603a34SBarry Smith - lg - a line graph object 4227b3603a34SBarry Smith 4228b3603a34SBarry Smith Level: intermediate 4229b3603a34SBarry Smith 42300b039ecaSBarry Smith Notes: each process in a parallel run displays its component solutions in a separate window 4231b3603a34SBarry Smith 4232b3603a34SBarry Smith .keywords: TS, vector, monitor, view 4233b3603a34SBarry Smith 4234b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4235b3603a34SBarry Smith @*/ 42360910c330SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4237b3603a34SBarry Smith { 4238b3603a34SBarry Smith PetscErrorCode ierr; 42390b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4240b3603a34SBarry Smith const PetscScalar *yy; 424158ff32f7SBarry Smith PetscInt dim; 4242b3603a34SBarry Smith 4243b3603a34SBarry Smith PetscFunctionBegin; 424458ff32f7SBarry Smith if (!step) { 4245a9f9c1f6SBarry Smith PetscDrawAxis axis; 4246a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4247a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 42480910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 42490b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 42500b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 425158ff32f7SBarry Smith } 42520910c330SBarry Smith ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr); 4253e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 4254e3efe391SJed Brown { 4255e3efe391SJed Brown PetscReal *yreal; 4256e3efe391SJed Brown PetscInt i,n; 42570910c330SBarry Smith ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr); 4258e3efe391SJed Brown ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4259e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 42600b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4261e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4262e3efe391SJed Brown } 4263e3efe391SJed Brown #else 42640b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4265e3efe391SJed Brown #endif 42660910c330SBarry Smith ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr); 42673fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){ 42680b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 42693923b477SBarry Smith } 4270b3603a34SBarry Smith PetscFunctionReturn(0); 4271b3603a34SBarry Smith } 4272b3603a34SBarry Smith 4273b3603a34SBarry Smith #undef __FUNCT__ 42744f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 4275ef20d060SBarry Smith /*@C 42764f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 4277ef20d060SBarry Smith in a time based line graph 4278ef20d060SBarry Smith 4279ef20d060SBarry Smith Collective on TS 4280ef20d060SBarry Smith 4281ef20d060SBarry Smith Input Parameters: 4282ef20d060SBarry Smith + ts - the TS context 4283ef20d060SBarry Smith . step - current time-step 4284ef20d060SBarry Smith . ptime - current time 4285ef20d060SBarry Smith - lg - a line graph object 4286ef20d060SBarry Smith 4287ef20d060SBarry Smith Level: intermediate 4288ef20d060SBarry Smith 4289abd5a294SJed Brown Notes: 4290abd5a294SJed Brown Only for sequential solves. 4291abd5a294SJed Brown 4292abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 4293abd5a294SJed Brown 4294abd5a294SJed Brown Options Database Keys: 42954f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 4296ef20d060SBarry Smith 4297ef20d060SBarry Smith .keywords: TS, vector, monitor, view 4298ef20d060SBarry Smith 4299abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 4300ef20d060SBarry Smith @*/ 43010910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4302ef20d060SBarry Smith { 4303ef20d060SBarry Smith PetscErrorCode ierr; 43040b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4305ef20d060SBarry Smith const PetscScalar *yy; 4306ef20d060SBarry Smith Vec y; 4307a9f9c1f6SBarry Smith PetscInt dim; 4308ef20d060SBarry Smith 4309ef20d060SBarry Smith PetscFunctionBegin; 4310a9f9c1f6SBarry Smith if (!step) { 4311a9f9c1f6SBarry Smith PetscDrawAxis axis; 4312a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 43131ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 43140910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 4315a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 4316a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4317a9f9c1f6SBarry Smith } 43180910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 4319ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 43200910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 4321ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 4322e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 4323e3efe391SJed Brown { 4324e3efe391SJed Brown PetscReal *yreal; 4325e3efe391SJed Brown PetscInt i,n; 4326e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 4327e3efe391SJed Brown ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4328e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 43290b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4330e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4331e3efe391SJed Brown } 4332e3efe391SJed Brown #else 43330b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4334e3efe391SJed Brown #endif 4335ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 4336ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 43373fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){ 43380b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 43393923b477SBarry Smith } 4340ef20d060SBarry Smith PetscFunctionReturn(0); 4341ef20d060SBarry Smith } 4342ef20d060SBarry Smith 4343201da799SBarry Smith #undef __FUNCT__ 4344201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 4345201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4346201da799SBarry Smith { 4347201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4348201da799SBarry Smith PetscReal x = ptime,y; 4349201da799SBarry Smith PetscErrorCode ierr; 4350201da799SBarry Smith PetscInt its; 4351201da799SBarry Smith 4352201da799SBarry Smith PetscFunctionBegin; 4353201da799SBarry Smith if (!n) { 4354201da799SBarry Smith PetscDrawAxis axis; 4355201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4356201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 4357201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4358201da799SBarry Smith ctx->snes_its = 0; 4359201da799SBarry Smith } 4360201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 4361201da799SBarry Smith y = its - ctx->snes_its; 4362201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 43633fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){ 4364201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4365201da799SBarry Smith } 4366201da799SBarry Smith ctx->snes_its = its; 4367201da799SBarry Smith PetscFunctionReturn(0); 4368201da799SBarry Smith } 4369201da799SBarry Smith 4370201da799SBarry Smith #undef __FUNCT__ 4371201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 4372201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4373201da799SBarry Smith { 4374201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4375201da799SBarry Smith PetscReal x = ptime,y; 4376201da799SBarry Smith PetscErrorCode ierr; 4377201da799SBarry Smith PetscInt its; 4378201da799SBarry Smith 4379201da799SBarry Smith PetscFunctionBegin; 4380201da799SBarry Smith if (!n) { 4381201da799SBarry Smith PetscDrawAxis axis; 4382201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4383201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 4384201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4385201da799SBarry Smith ctx->ksp_its = 0; 4386201da799SBarry Smith } 4387201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 4388201da799SBarry Smith y = its - ctx->ksp_its; 4389201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 439099fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){ 4391201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4392201da799SBarry Smith } 4393201da799SBarry Smith ctx->ksp_its = its; 4394201da799SBarry Smith PetscFunctionReturn(0); 4395201da799SBarry Smith } 4396f9c1d6abSBarry Smith 4397f9c1d6abSBarry Smith #undef __FUNCT__ 4398f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 4399f9c1d6abSBarry Smith /*@ 4400f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 4401f9c1d6abSBarry Smith 4402f9c1d6abSBarry Smith Collective on TS and Vec 4403f9c1d6abSBarry Smith 4404f9c1d6abSBarry Smith Input Parameters: 4405f9c1d6abSBarry Smith + ts - the TS context 4406f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 4407f9c1d6abSBarry Smith 4408f9c1d6abSBarry Smith Output Parameters: 4409f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 4410f9c1d6abSBarry Smith 4411f9c1d6abSBarry Smith Level: developer 4412f9c1d6abSBarry Smith 4413f9c1d6abSBarry Smith .keywords: TS, compute 4414f9c1d6abSBarry Smith 4415f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 4416f9c1d6abSBarry Smith @*/ 4417f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 4418f9c1d6abSBarry Smith { 4419f9c1d6abSBarry Smith PetscErrorCode ierr; 4420f9c1d6abSBarry Smith 4421f9c1d6abSBarry Smith PetscFunctionBegin; 4422f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4423f9c1d6abSBarry Smith if (!ts->ops->linearstability) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 4424f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 4425f9c1d6abSBarry Smith PetscFunctionReturn(0); 4426f9c1d6abSBarry Smith } 4427