163dd3a1aSKris Buschelman 2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h> /*I "petscts.h" I*/ 3496e6a7aSJed Brown #include <petscdmshell.h> 4d763cef2SBarry Smith 5d5ba7fb7SMatthew Knepley /* Logging support */ 67087cfbeSBarry Smith PetscClassId TS_CLASSID; 7166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 8d405a339SMatthew Knepley 94a2ae208SSatish Balay #undef __FUNCT__ 10bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions" 11bdad233fSMatthew Knepley /* 12bdad233fSMatthew Knepley TSSetTypeFromOptions - Sets the type of ts from user options. 13bdad233fSMatthew Knepley 14bdad233fSMatthew Knepley Collective on TS 15bdad233fSMatthew Knepley 16bdad233fSMatthew Knepley Input Parameter: 17bdad233fSMatthew Knepley . ts - The ts 18bdad233fSMatthew Knepley 19bdad233fSMatthew Knepley Level: intermediate 20bdad233fSMatthew Knepley 21bdad233fSMatthew Knepley .keywords: TS, set, options, database, type 22bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType() 23bdad233fSMatthew Knepley */ 246849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts) 25bdad233fSMatthew Knepley { 26ace3abfcSBarry Smith PetscBool opt; 272fc52814SBarry Smith const char *defaultType; 28bdad233fSMatthew Knepley char typeName[256]; 29dfbe8321SBarry Smith PetscErrorCode ierr; 30bdad233fSMatthew Knepley 31bdad233fSMatthew Knepley PetscFunctionBegin; 327adad957SLisandro Dalcin if (((PetscObject)ts)->type_name) { 337adad957SLisandro Dalcin defaultType = ((PetscObject)ts)->type_name; 34bdad233fSMatthew Knepley } else { 359596e0b4SJed Brown defaultType = TSEULER; 36bdad233fSMatthew Knepley } 37bdad233fSMatthew Knepley 38cce0b1b2SLisandro Dalcin if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 39bdad233fSMatthew Knepley ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 40a7cc72afSBarry Smith if (opt) { 41bdad233fSMatthew Knepley ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 42bdad233fSMatthew Knepley } else { 43bdad233fSMatthew Knepley ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 44bdad233fSMatthew Knepley } 45bdad233fSMatthew Knepley PetscFunctionReturn(0); 46bdad233fSMatthew Knepley } 47bdad233fSMatthew Knepley 48bdad233fSMatthew Knepley #undef __FUNCT__ 49bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions" 50bdad233fSMatthew Knepley /*@ 51bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 52bdad233fSMatthew Knepley 53bdad233fSMatthew Knepley Collective on TS 54bdad233fSMatthew Knepley 55bdad233fSMatthew Knepley Input Parameter: 56bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 57bdad233fSMatthew Knepley 58bdad233fSMatthew Knepley Options Database Keys: 594d91e141SJed Brown + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP 60bdad233fSMatthew Knepley . -ts_max_steps maxsteps - maximum number of time-steps to take 613bca7d26SBarry Smith . -ts_final_time time - maximum time to compute to 62bdad233fSMatthew Knepley . -ts_dt dt - initial time step 63bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 64201da799SBarry Smith . -ts_monitor_lg_timestep - plot time-step information at each timestep 65201da799SBarry Smith . -ts_monitor_lg_ksp_iterations - plot the number of linear iterations used at each time-step 66201da799SBarry Smith - -ts_monitor_lg_snes_iterations - plot the number of nonlinear iterations used at each time-step 67bdad233fSMatthew Knepley 68bdad233fSMatthew Knepley Level: beginner 69bdad233fSMatthew Knepley 70bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 71bdad233fSMatthew Knepley 72a313700dSBarry Smith .seealso: TSGetType() 73bdad233fSMatthew Knepley @*/ 747087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 75bdad233fSMatthew Knepley { 76ace3abfcSBarry Smith PetscBool opt,flg; 77dfbe8321SBarry Smith PetscErrorCode ierr; 78649052a6SBarry Smith PetscViewer monviewer; 79eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 80089b2837SJed Brown SNES snes; 811c3436cfSJed Brown TSAdapt adapt; 8231748224SBarry Smith PetscReal time_step; 83bdad233fSMatthew Knepley 84bdad233fSMatthew Knepley PetscFunctionBegin; 850700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 863194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 87a43b19c4SJed Brown /* Handle TS type options */ 88a43b19c4SJed Brown ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr); 89bdad233fSMatthew Knepley 90bdad233fSMatthew Knepley /* Handle generic TS options */ 91bdad233fSMatthew Knepley ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr); 923bca7d26SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr); 93d8cd7023SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr); 9431748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 9531748224SBarry Smith if (flg) { 9631748224SBarry Smith ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 9731748224SBarry Smith } 98a43b19c4SJed Brown opt = ts->exact_final_time == PETSC_DECIDE ? PETSC_FALSE : (PetscBool)ts->exact_final_time; 99a43b19c4SJed Brown ierr = PetscOptionsBool("-ts_exact_final_time","Interpolate output to stop exactly at the final time","TSSetExactFinalTime",opt,&opt,&flg);CHKERRQ(ierr); 100461e00b3SSean Farley if (flg) {ierr = TSSetExactFinalTime(ts,opt);CHKERRQ(ierr);} 101cef5090cSJed 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); 102cef5090cSJed 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); 103cef5090cSJed Brown ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr); 1041c3436cfSJed Brown ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,PETSC_NULL);CHKERRQ(ierr); 1051c3436cfSJed Brown ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,PETSC_NULL);CHKERRQ(ierr); 106bdad233fSMatthew Knepley 107bdad233fSMatthew Knepley /* Monitor options */ 108a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 109eabae89aSBarry Smith if (flg) { 110649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr); 111649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 112bdad233fSMatthew Knepley } 1135180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1145180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1155180491cSLisandro Dalcin 1164f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 117a7cc72afSBarry Smith if (opt) { 1180b039ecaSBarry Smith TSMonitorLGCtx ctx; 1193923b477SBarry Smith PetscInt howoften = 1; 120a80ad3e0SBarry Smith 1214f09c107SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 1221d1e9da1SBarry Smith ierr = TSMonitorLGCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 1234f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 124b3603a34SBarry Smith } 1254f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 126b3603a34SBarry Smith if (opt) { 1270b039ecaSBarry Smith TSMonitorLGCtx ctx; 1283923b477SBarry Smith PetscInt howoften = 1; 129b3603a34SBarry Smith 1304f09c107SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 1313923b477SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 1324f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 133bdad233fSMatthew Knepley } 1344f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 135ef20d060SBarry Smith if (opt) { 1360b039ecaSBarry Smith TSMonitorLGCtx ctx; 1373923b477SBarry Smith PetscInt howoften = 1; 138ef20d060SBarry Smith 1394f09c107SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 1403923b477SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 1414f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 142ef20d060SBarry Smith } 143201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 144201da799SBarry Smith if (opt) { 145201da799SBarry Smith TSMonitorLGCtx ctx; 146201da799SBarry Smith PetscInt howoften = 1; 147201da799SBarry Smith 148201da799SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 149201da799SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx); 150201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 151201da799SBarry Smith } 152201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 153201da799SBarry Smith if (opt) { 154201da799SBarry Smith TSMonitorLGCtx ctx; 155201da799SBarry Smith PetscInt howoften = 1; 156201da799SBarry Smith 157201da799SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 158201da799SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx); 159201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 160201da799SBarry Smith } 161*8189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 162*8189c53fSBarry Smith if (opt) { 163*8189c53fSBarry Smith TSMonitorSPEigCtx ctx; 164*8189c53fSBarry Smith PetscInt howoften = 1; 165*8189c53fSBarry Smith 166*8189c53fSBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 167*8189c53fSBarry Smith ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 168*8189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 169*8189c53fSBarry Smith } 170ef20d060SBarry Smith opt = PETSC_FALSE; 1710dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 172a7cc72afSBarry Smith if (opt) { 17383a4ac43SBarry Smith TSMonitorDrawCtx ctx; 17483a4ac43SBarry Smith PetscInt howoften = 1; 175a80ad3e0SBarry Smith 17683a4ac43SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 17783a4ac43SBarry Smith ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 17883a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 179bdad233fSMatthew Knepley } 180fb1732b5SBarry Smith opt = PETSC_FALSE; 1810dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 1823a471f94SBarry Smith if (opt) { 18383a4ac43SBarry Smith TSMonitorDrawCtx ctx; 18483a4ac43SBarry Smith PetscInt howoften = 1; 1853a471f94SBarry Smith 18683a4ac43SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",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,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 1893a471f94SBarry Smith } 1903a471f94SBarry Smith opt = PETSC_FALSE; 191fb1732b5SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 192fb1732b5SBarry Smith if (flg) { 193fb1732b5SBarry Smith PetscViewer ctx; 194fb1732b5SBarry Smith if (monfilename[0]) { 195fb1732b5SBarry Smith ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 196fb1732b5SBarry Smith } else { 197fb1732b5SBarry Smith ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm); 198fb1732b5SBarry Smith } 199fb1732b5SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 200fb1732b5SBarry Smith } 201ed81e22dSJed Brown opt = PETSC_FALSE; 202ed81e22dSJed Brown ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 203ed81e22dSJed Brown if (flg) { 204ed81e22dSJed Brown const char *ptr,*ptr2; 205ed81e22dSJed Brown char *filetemplate; 206ed81e22dSJed Brown if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 207ed81e22dSJed Brown /* Do some cursory validation of the input. */ 208ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 209ed81e22dSJed Brown if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 210ed81e22dSJed Brown for (ptr++ ; ptr && *ptr; ptr++) { 211ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 212ed81e22dSJed Brown if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts"); 213ed81e22dSJed Brown if (ptr2) break; 214ed81e22dSJed Brown } 215ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 216ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 217ed81e22dSJed Brown } 218bdad233fSMatthew Knepley 2191c3436cfSJed Brown ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr); 2201c3436cfSJed Brown ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr); 2211c3436cfSJed Brown 222d52bd9f3SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 223d52bd9f3SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 224d52bd9f3SBarry Smith 225bdad233fSMatthew Knepley /* Handle specific TS options */ 226abc0a331SBarry Smith if (ts->ops->setfromoptions) { 227bdad233fSMatthew Knepley ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr); 228bdad233fSMatthew Knepley } 2295d973c19SBarry Smith 2305d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 2315d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 232bdad233fSMatthew Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 233bdad233fSMatthew Knepley PetscFunctionReturn(0); 234bdad233fSMatthew Knepley } 235bdad233fSMatthew Knepley 236bdad233fSMatthew Knepley #undef __FUNCT__ 237cdcf91faSSean Farley #undef __FUNCT__ 2384a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian" 239a7a1495cSBarry Smith /*@ 2408c385f81SBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 241a7a1495cSBarry Smith set with TSSetRHSJacobian(). 242a7a1495cSBarry Smith 243a7a1495cSBarry Smith Collective on TS and Vec 244a7a1495cSBarry Smith 245a7a1495cSBarry Smith Input Parameters: 246316643e7SJed Brown + ts - the TS context 247a7a1495cSBarry Smith . t - current timestep 248a7a1495cSBarry Smith - x - input vector 249a7a1495cSBarry Smith 250a7a1495cSBarry Smith Output Parameters: 251a7a1495cSBarry Smith + A - Jacobian matrix 252a7a1495cSBarry Smith . B - optional preconditioning matrix 253a7a1495cSBarry Smith - flag - flag indicating matrix structure 254a7a1495cSBarry Smith 255a7a1495cSBarry Smith Notes: 256a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 257a7a1495cSBarry Smith is used internally within the nonlinear solvers. 258a7a1495cSBarry Smith 25994b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 260a7a1495cSBarry Smith flag parameter. 261a7a1495cSBarry Smith 262a7a1495cSBarry Smith Level: developer 263a7a1495cSBarry Smith 264a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 265a7a1495cSBarry Smith 26694b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 267a7a1495cSBarry Smith @*/ 2687087cfbeSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg) 269a7a1495cSBarry Smith { 270dfbe8321SBarry Smith PetscErrorCode ierr; 2710e4ef248SJed Brown PetscInt Xstate; 27224989b8cSPeter Brune DM dm; 27324989b8cSPeter Brune TSDM tsdm; 27424989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 27524989b8cSPeter Brune void *ctx; 27624989b8cSPeter Brune TSIJacobian ijacobianfunc; 277a7a1495cSBarry Smith 278a7a1495cSBarry Smith PetscFunctionBegin; 2790700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2800700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 281c9780b6fSBarry Smith PetscCheckSameComm(ts,1,X,3); 28224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 28324989b8cSPeter Brune ierr = DMTSGetContext(dm,&tsdm);CHKERRQ(ierr); 28424989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 28524989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr); 2860e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr); 2870e4ef248SJed Brown if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) { 2880e4ef248SJed Brown *flg = ts->rhsjacobian.mstructure; 2890e4ef248SJed Brown PetscFunctionReturn(0); 290f8ede8e7SJed Brown } 291d90be118SSean Farley 29224989b8cSPeter Brune if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 293d90be118SSean Farley 29424989b8cSPeter Brune if (rhsjacobianfunc) { 295d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 296a7a1495cSBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 297a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 29824989b8cSPeter Brune ierr = (*rhsjacobianfunc)(ts,t,X,A,B,flg,ctx);CHKERRQ(ierr); 299a7a1495cSBarry Smith PetscStackPop; 300d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 301a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 3020700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 3030700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 304ef66eb69SBarry Smith } else { 305214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 306214bc6a2SJed Brown if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);} 307214bc6a2SJed Brown *flg = SAME_NONZERO_PATTERN; 308ef66eb69SBarry Smith } 3090e4ef248SJed Brown ts->rhsjacobian.time = t; 3100e4ef248SJed Brown ts->rhsjacobian.X = X; 3110e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 3120e4ef248SJed Brown ts->rhsjacobian.mstructure = *flg; 313a7a1495cSBarry Smith PetscFunctionReturn(0); 314a7a1495cSBarry Smith } 315a7a1495cSBarry Smith 3164a2ae208SSatish Balay #undef __FUNCT__ 3174a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 318316643e7SJed Brown /*@ 319d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 320d763cef2SBarry Smith 321316643e7SJed Brown Collective on TS and Vec 322316643e7SJed Brown 323316643e7SJed Brown Input Parameters: 324316643e7SJed Brown + ts - the TS context 325316643e7SJed Brown . t - current time 326316643e7SJed Brown - x - state vector 327316643e7SJed Brown 328316643e7SJed Brown Output Parameter: 329316643e7SJed Brown . y - right hand side 330316643e7SJed Brown 331316643e7SJed Brown Note: 332316643e7SJed Brown Most users should not need to explicitly call this routine, as it 333316643e7SJed Brown is used internally within the nonlinear solvers. 334316643e7SJed Brown 335316643e7SJed Brown Level: developer 336316643e7SJed Brown 337316643e7SJed Brown .keywords: TS, compute 338316643e7SJed Brown 339316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 340316643e7SJed Brown @*/ 341dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y) 342d763cef2SBarry Smith { 343dfbe8321SBarry Smith PetscErrorCode ierr; 344d763cef2SBarry Smith 34524989b8cSPeter Brune TSRHSFunction rhsfunction; 34624989b8cSPeter Brune TSIFunction ifunction; 34724989b8cSPeter Brune void *ctx; 34824989b8cSPeter Brune DM dm; 34924989b8cSPeter Brune 350d763cef2SBarry Smith PetscFunctionBegin; 3510700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3520700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3530700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 35424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 35524989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 35624989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr); 357d763cef2SBarry Smith 35824989b8cSPeter Brune if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 359d763cef2SBarry Smith 360d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 36124989b8cSPeter Brune if (rhsfunction) { 362d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 36324989b8cSPeter Brune ierr = (*rhsfunction)(ts,t,x,y,ctx);CHKERRQ(ierr); 364d763cef2SBarry Smith PetscStackPop; 365214bc6a2SJed Brown } else { 366214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 367b2cd27e8SJed Brown } 36844a41b28SSean Farley 369d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 370d763cef2SBarry Smith PetscFunctionReturn(0); 371d763cef2SBarry Smith } 372d763cef2SBarry Smith 3734a2ae208SSatish Balay #undef __FUNCT__ 374ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 375ef20d060SBarry Smith /*@ 376ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 377ef20d060SBarry Smith 378ef20d060SBarry Smith Collective on TS and Vec 379ef20d060SBarry Smith 380ef20d060SBarry Smith Input Parameters: 381ef20d060SBarry Smith + ts - the TS context 382ef20d060SBarry Smith - t - current time 383ef20d060SBarry Smith 384ef20d060SBarry Smith Output Parameter: 385ef20d060SBarry Smith . y - right hand side 386ef20d060SBarry Smith 387ef20d060SBarry Smith Note: 388ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 389ef20d060SBarry Smith is used internally within the nonlinear solvers. 390ef20d060SBarry Smith 391ef20d060SBarry Smith Level: developer 392ef20d060SBarry Smith 393ef20d060SBarry Smith .keywords: TS, compute 394ef20d060SBarry Smith 395abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 396ef20d060SBarry Smith @*/ 397ef20d060SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec x) 398ef20d060SBarry Smith { 399ef20d060SBarry Smith PetscErrorCode ierr; 400ef20d060SBarry Smith TSSolutionFunction solutionfunction; 401ef20d060SBarry Smith void *ctx; 402ef20d060SBarry Smith DM dm; 403ef20d060SBarry Smith 404ef20d060SBarry Smith PetscFunctionBegin; 405ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 406ef20d060SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 407ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 408ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 409ef20d060SBarry Smith 410ef20d060SBarry Smith if (solutionfunction) { 411ef20d060SBarry Smith PetscStackPush("TS user right-hand-side function"); 412ef20d060SBarry Smith ierr = (*solutionfunction)(ts,t,x,ctx);CHKERRQ(ierr); 413ef20d060SBarry Smith PetscStackPop; 414ef20d060SBarry Smith } 415ef20d060SBarry Smith PetscFunctionReturn(0); 416ef20d060SBarry Smith } 417ef20d060SBarry Smith 418ef20d060SBarry Smith #undef __FUNCT__ 419214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 420214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 421214bc6a2SJed Brown { 4222dd45cf8SJed Brown Vec F; 423214bc6a2SJed Brown PetscErrorCode ierr; 424214bc6a2SJed Brown 425214bc6a2SJed Brown PetscFunctionBegin; 4260da9a4f0SJed Brown *Frhs = PETSC_NULL; 4272dd45cf8SJed Brown ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 428214bc6a2SJed Brown if (!ts->Frhs) { 4292dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 430214bc6a2SJed Brown } 431214bc6a2SJed Brown *Frhs = ts->Frhs; 432214bc6a2SJed Brown PetscFunctionReturn(0); 433214bc6a2SJed Brown } 434214bc6a2SJed Brown 435214bc6a2SJed Brown #undef __FUNCT__ 436214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 437214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 438214bc6a2SJed Brown { 439214bc6a2SJed Brown Mat A,B; 4402dd45cf8SJed Brown PetscErrorCode ierr; 441214bc6a2SJed Brown 442214bc6a2SJed Brown PetscFunctionBegin; 443214bc6a2SJed Brown ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 444214bc6a2SJed Brown if (Arhs) { 445214bc6a2SJed Brown if (!ts->Arhs) { 446214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 447214bc6a2SJed Brown } 448214bc6a2SJed Brown *Arhs = ts->Arhs; 449214bc6a2SJed Brown } 450214bc6a2SJed Brown if (Brhs) { 451214bc6a2SJed Brown if (!ts->Brhs) { 452214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 453214bc6a2SJed Brown } 454214bc6a2SJed Brown *Brhs = ts->Brhs; 455214bc6a2SJed Brown } 456214bc6a2SJed Brown PetscFunctionReturn(0); 457214bc6a2SJed Brown } 458214bc6a2SJed Brown 459214bc6a2SJed Brown #undef __FUNCT__ 460316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 461316643e7SJed Brown /*@ 462316643e7SJed Brown TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0 463316643e7SJed Brown 464316643e7SJed Brown Collective on TS and Vec 465316643e7SJed Brown 466316643e7SJed Brown Input Parameters: 467316643e7SJed Brown + ts - the TS context 468316643e7SJed Brown . t - current time 469316643e7SJed Brown . X - state vector 470214bc6a2SJed Brown . Xdot - time derivative of state vector 471214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 472316643e7SJed Brown 473316643e7SJed Brown Output Parameter: 474316643e7SJed Brown . Y - right hand side 475316643e7SJed Brown 476316643e7SJed Brown Note: 477316643e7SJed Brown Most users should not need to explicitly call this routine, as it 478316643e7SJed Brown is used internally within the nonlinear solvers. 479316643e7SJed Brown 480316643e7SJed Brown If the user did did not write their equations in implicit form, this 481316643e7SJed Brown function recasts them in implicit form. 482316643e7SJed Brown 483316643e7SJed Brown Level: developer 484316643e7SJed Brown 485316643e7SJed Brown .keywords: TS, compute 486316643e7SJed Brown 487316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 488316643e7SJed Brown @*/ 489214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex) 490316643e7SJed Brown { 491316643e7SJed Brown PetscErrorCode ierr; 49224989b8cSPeter Brune TSIFunction ifunction; 49324989b8cSPeter Brune TSRHSFunction rhsfunction; 49424989b8cSPeter Brune void *ctx; 49524989b8cSPeter Brune DM dm; 496316643e7SJed Brown 497316643e7SJed Brown PetscFunctionBegin; 4980700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4990700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 5000700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 5010700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 502316643e7SJed Brown 50324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 50424989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 50524989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr); 50624989b8cSPeter Brune 50724989b8cSPeter Brune if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 508d90be118SSean Farley 509316643e7SJed Brown ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 51024989b8cSPeter Brune if (ifunction) { 511316643e7SJed Brown PetscStackPush("TS user implicit function"); 51224989b8cSPeter Brune ierr = (*ifunction)(ts,t,X,Xdot,Y,ctx);CHKERRQ(ierr); 513316643e7SJed Brown PetscStackPop; 514214bc6a2SJed Brown } 515214bc6a2SJed Brown if (imex) { 51624989b8cSPeter Brune if (!ifunction) { 5172dd45cf8SJed Brown ierr = VecCopy(Xdot,Y);CHKERRQ(ierr); 5182dd45cf8SJed Brown } 51924989b8cSPeter Brune } else if (rhsfunction) { 52024989b8cSPeter Brune if (ifunction) { 521214bc6a2SJed Brown Vec Frhs; 522214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 523214bc6a2SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr); 524214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 5252dd45cf8SJed Brown } else { 5262dd45cf8SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr); 5272dd45cf8SJed Brown ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr); 528316643e7SJed Brown } 5294a6899ffSJed Brown } 530316643e7SJed Brown ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 531316643e7SJed Brown PetscFunctionReturn(0); 532316643e7SJed Brown } 533316643e7SJed Brown 534316643e7SJed Brown #undef __FUNCT__ 535316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 536316643e7SJed Brown /*@ 537316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 538316643e7SJed Brown 539316643e7SJed Brown Collective on TS and Vec 540316643e7SJed Brown 541316643e7SJed Brown Input 542316643e7SJed Brown Input Parameters: 543316643e7SJed Brown + ts - the TS context 544316643e7SJed Brown . t - current timestep 545316643e7SJed Brown . X - state vector 546316643e7SJed Brown . Xdot - time derivative of state vector 547214bc6a2SJed Brown . shift - shift to apply, see note below 548214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 549316643e7SJed Brown 550316643e7SJed Brown Output Parameters: 551316643e7SJed Brown + A - Jacobian matrix 552316643e7SJed Brown . B - optional preconditioning matrix 553316643e7SJed Brown - flag - flag indicating matrix structure 554316643e7SJed Brown 555316643e7SJed Brown Notes: 556316643e7SJed Brown If F(t,X,Xdot)=0 is the DAE, the required Jacobian is 557316643e7SJed Brown 558316643e7SJed Brown dF/dX + shift*dF/dXdot 559316643e7SJed Brown 560316643e7SJed Brown Most users should not need to explicitly call this routine, as it 561316643e7SJed Brown is used internally within the nonlinear solvers. 562316643e7SJed Brown 563316643e7SJed Brown Level: developer 564316643e7SJed Brown 565316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 566316643e7SJed Brown 567316643e7SJed Brown .seealso: TSSetIJacobian() 56863495f91SJed Brown @*/ 569214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex) 570316643e7SJed Brown { 5710026cea9SSean Farley PetscInt Xstate, Xdotstate; 572316643e7SJed Brown PetscErrorCode ierr; 57324989b8cSPeter Brune TSIJacobian ijacobian; 57424989b8cSPeter Brune TSRHSJacobian rhsjacobian; 57524989b8cSPeter Brune DM dm; 57624989b8cSPeter Brune void *ctx; 577316643e7SJed Brown 578316643e7SJed Brown PetscFunctionBegin; 57924989b8cSPeter Brune 5800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5810700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 5820700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 583316643e7SJed Brown PetscValidPointer(A,6); 5840700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,6); 585316643e7SJed Brown PetscValidPointer(B,7); 5860700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,7); 587316643e7SJed Brown PetscValidPointer(flg,8); 58824989b8cSPeter Brune 58924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 59024989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 59124989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr); 59224989b8cSPeter Brune 5930026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr); 5940026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr); 5950026cea9SSean Farley if (ts->ijacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->ijacobian.X == X && ts->ijacobian.Xstate == Xstate && ts->ijacobian.Xdot == Xdot && ts->ijacobian.Xdotstate == Xdotstate && ts->ijacobian.imex == imex))) { 5960026cea9SSean Farley *flg = ts->ijacobian.mstructure; 5970026cea9SSean Farley ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 5980026cea9SSean Farley PetscFunctionReturn(0); 5990026cea9SSean Farley } 600316643e7SJed Brown 60124989b8cSPeter Brune if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 602316643e7SJed Brown 6034e684422SJed Brown *flg = SAME_NONZERO_PATTERN; /* In case we're solving a linear problem in which case it wouldn't get initialized below. */ 604316643e7SJed Brown ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 60524989b8cSPeter Brune if (ijacobian) { 6062dd45cf8SJed Brown *flg = DIFFERENT_NONZERO_PATTERN; 607316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 60824989b8cSPeter Brune ierr = (*ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ctx);CHKERRQ(ierr); 609316643e7SJed Brown PetscStackPop; 610214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 611214bc6a2SJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 612214bc6a2SJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 6134a6899ffSJed Brown } 614214bc6a2SJed Brown if (imex) { 61524989b8cSPeter Brune if (!ijacobian) { /* system was written as Xdot = F(t,X) */ 616214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 6172dd45cf8SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 618214bc6a2SJed Brown if (*A != *B) { 619214bc6a2SJed Brown ierr = MatZeroEntries(*B);CHKERRQ(ierr); 620214bc6a2SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 621214bc6a2SJed Brown } 622214bc6a2SJed Brown *flg = SAME_PRECONDITIONER; 623214bc6a2SJed Brown } 624214bc6a2SJed Brown } else { 62524989b8cSPeter Brune if (!ijacobian) { 626214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr); 627214bc6a2SJed Brown ierr = MatScale(*A,-1);CHKERRQ(ierr); 628214bc6a2SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 629316643e7SJed Brown if (*A != *B) { 630316643e7SJed Brown ierr = MatScale(*B,-1);CHKERRQ(ierr); 631316643e7SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 632316643e7SJed Brown } 63324989b8cSPeter Brune } else if (rhsjacobian) { 634214bc6a2SJed Brown Mat Arhs,Brhs; 635214bc6a2SJed Brown MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN; 636214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 637214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 638214bc6a2SJed Brown axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN; 639214bc6a2SJed Brown ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr); 640214bc6a2SJed Brown if (*A != *B) { 641214bc6a2SJed Brown ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr); 642214bc6a2SJed Brown } 643214bc6a2SJed Brown *flg = PetscMin(*flg,flg2); 644214bc6a2SJed Brown } 645316643e7SJed Brown } 6460026cea9SSean Farley 6470026cea9SSean Farley ts->ijacobian.time = t; 6480026cea9SSean Farley ts->ijacobian.X = X; 6490026cea9SSean Farley ts->ijacobian.Xdot = Xdot; 6500026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr); 6510026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr); 6520026cea9SSean Farley ts->ijacobian.shift = shift; 6530026cea9SSean Farley ts->ijacobian.imex = imex; 6540026cea9SSean Farley ts->ijacobian.mstructure = *flg; 655316643e7SJed Brown ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 656316643e7SJed Brown PetscFunctionReturn(0); 657316643e7SJed Brown } 658316643e7SJed Brown 659316643e7SJed Brown #undef __FUNCT__ 6604a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 661d763cef2SBarry Smith /*@C 662d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 663d763cef2SBarry Smith F(t,u), where U_t = F(t,u). 664d763cef2SBarry Smith 6653f9fe445SBarry Smith Logically Collective on TS 666d763cef2SBarry Smith 667d763cef2SBarry Smith Input Parameters: 668d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 669ca94891dSJed Brown . r - vector to put the computed right hand side (or PETSC_NULL to have it created) 670d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 671d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 672d763cef2SBarry Smith function evaluation routine (may be PETSC_NULL) 673d763cef2SBarry Smith 674d763cef2SBarry Smith Calling sequence of func: 67587828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 676d763cef2SBarry Smith 677d763cef2SBarry Smith + t - current timestep 678d763cef2SBarry Smith . u - input vector 679d763cef2SBarry Smith . F - function vector 680d763cef2SBarry Smith - ctx - [optional] user-defined function context 681d763cef2SBarry Smith 682d763cef2SBarry Smith Level: beginner 683d763cef2SBarry Smith 684d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 685d763cef2SBarry Smith 686d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian() 687d763cef2SBarry Smith @*/ 688089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 689d763cef2SBarry Smith { 690089b2837SJed Brown PetscErrorCode ierr; 691089b2837SJed Brown SNES snes; 692e856ceecSJed Brown Vec ralloc = PETSC_NULL; 69324989b8cSPeter Brune DM dm; 694d763cef2SBarry Smith 695089b2837SJed Brown PetscFunctionBegin; 6960700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 697ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 69824989b8cSPeter Brune 69924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 70024989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 701089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 702e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 703e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 704e856ceecSJed Brown r = ralloc; 705e856ceecSJed Brown } 706089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 707e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 708d763cef2SBarry Smith PetscFunctionReturn(0); 709d763cef2SBarry Smith } 710d763cef2SBarry Smith 7114a2ae208SSatish Balay #undef __FUNCT__ 712ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 713ef20d060SBarry Smith /*@C 714abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 715ef20d060SBarry Smith 716ef20d060SBarry Smith Logically Collective on TS 717ef20d060SBarry Smith 718ef20d060SBarry Smith Input Parameters: 719ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 720ef20d060SBarry Smith . f - routine for evaluating the solution 721ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 722ef20d060SBarry Smith function evaluation routine (may be PETSC_NULL) 723ef20d060SBarry Smith 724ef20d060SBarry Smith Calling sequence of func: 725ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 726ef20d060SBarry Smith 727ef20d060SBarry Smith + t - current timestep 728ef20d060SBarry Smith . u - output vector 729ef20d060SBarry Smith - ctx - [optional] user-defined function context 730ef20d060SBarry Smith 731abd5a294SJed Brown Notes: 732abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 733abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 734abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 735abd5a294SJed Brown 7364f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 737abd5a294SJed Brown 738ef20d060SBarry Smith Level: beginner 739ef20d060SBarry Smith 740ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 741ef20d060SBarry Smith 742abd5a294SJed Brown .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction() 743ef20d060SBarry Smith @*/ 744ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 745ef20d060SBarry Smith { 746ef20d060SBarry Smith PetscErrorCode ierr; 747ef20d060SBarry Smith DM dm; 748ef20d060SBarry Smith 749ef20d060SBarry Smith PetscFunctionBegin; 750ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 751ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 752ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 753ef20d060SBarry Smith PetscFunctionReturn(0); 754ef20d060SBarry Smith } 755ef20d060SBarry Smith 756ef20d060SBarry Smith #undef __FUNCT__ 7574a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 758d763cef2SBarry Smith /*@C 759d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 760d763cef2SBarry Smith where U_t = F(U,t), as well as the location to store the matrix. 761d763cef2SBarry Smith 7623f9fe445SBarry Smith Logically Collective on TS 763d763cef2SBarry Smith 764d763cef2SBarry Smith Input Parameters: 765d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 766d763cef2SBarry Smith . A - Jacobian matrix 767d763cef2SBarry Smith . B - preconditioner matrix (usually same as A) 768d763cef2SBarry Smith . f - the Jacobian evaluation routine 769d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 770d763cef2SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) 771d763cef2SBarry Smith 772d763cef2SBarry Smith Calling sequence of func: 77387828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 774d763cef2SBarry Smith 775d763cef2SBarry Smith + t - current timestep 776d763cef2SBarry Smith . u - input vector 777d763cef2SBarry Smith . A - matrix A, where U_t = A(t)u 778d763cef2SBarry Smith . B - preconditioner matrix, usually the same as A 779d763cef2SBarry Smith . flag - flag indicating information about the preconditioner matrix 78094b7f48cSBarry Smith structure (same as flag in KSPSetOperators()) 781d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 782d763cef2SBarry Smith 783d763cef2SBarry Smith Notes: 78494b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 785d763cef2SBarry Smith output parameter in the routine func(). Be sure to read this information! 786d763cef2SBarry Smith 787d763cef2SBarry Smith The routine func() takes Mat * as the matrix arguments rather than Mat. 788d763cef2SBarry Smith This allows the matrix evaluation routine to replace A and/or B with a 78956335db2SHong Zhang completely new matrix structure (not just different matrix elements) 790d763cef2SBarry Smith when appropriate, for instance, if the nonzero structure is changing 791d763cef2SBarry Smith throughout the global iterations. 792d763cef2SBarry Smith 793d763cef2SBarry Smith Level: beginner 794d763cef2SBarry Smith 795d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 796d763cef2SBarry Smith 797ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction() 798d763cef2SBarry Smith 799d763cef2SBarry Smith @*/ 800089b2837SJed Brown PetscErrorCode TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx) 801d763cef2SBarry Smith { 802277b19d0SLisandro Dalcin PetscErrorCode ierr; 803089b2837SJed Brown SNES snes; 80424989b8cSPeter Brune DM dm; 80524989b8cSPeter Brune TSIJacobian ijacobian; 806277b19d0SLisandro Dalcin 807d763cef2SBarry Smith PetscFunctionBegin; 8080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 809277b19d0SLisandro Dalcin if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 810277b19d0SLisandro Dalcin if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 811277b19d0SLisandro Dalcin if (A) PetscCheckSameComm(ts,1,A,2); 812277b19d0SLisandro Dalcin if (B) PetscCheckSameComm(ts,1,B,3); 813d763cef2SBarry Smith 81424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 81524989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 81624989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr); 81724989b8cSPeter Brune 818089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 8195f659677SPeter Brune if (!ijacobian) { 820089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 8210e4ef248SJed Brown } 8220e4ef248SJed Brown if (A) { 8230e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 8240e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 8250e4ef248SJed Brown ts->Arhs = A; 8260e4ef248SJed Brown } 8270e4ef248SJed Brown if (B) { 8280e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 8290e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 8300e4ef248SJed Brown ts->Brhs = B; 8310e4ef248SJed Brown } 832d763cef2SBarry Smith PetscFunctionReturn(0); 833d763cef2SBarry Smith } 834d763cef2SBarry Smith 835316643e7SJed Brown 836316643e7SJed Brown #undef __FUNCT__ 837316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 838316643e7SJed Brown /*@C 839316643e7SJed Brown TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved. 840316643e7SJed Brown 8413f9fe445SBarry Smith Logically Collective on TS 842316643e7SJed Brown 843316643e7SJed Brown Input Parameters: 844316643e7SJed Brown + ts - the TS context obtained from TSCreate() 845ca94891dSJed Brown . r - vector to hold the residual (or PETSC_NULL to have it created internally) 846316643e7SJed Brown . f - the function evaluation routine 847316643e7SJed Brown - ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL) 848316643e7SJed Brown 849316643e7SJed Brown Calling sequence of f: 850316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 851316643e7SJed Brown 852316643e7SJed Brown + t - time at step/stage being solved 853316643e7SJed Brown . u - state vector 854316643e7SJed Brown . u_t - time derivative of state vector 855316643e7SJed Brown . F - function vector 856316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 857316643e7SJed Brown 858316643e7SJed Brown Important: 859d6cbdb99SBarry Smith The user MUST call either this routine, TSSetRHSFunction(). This routine must be used when not solving an ODE, for example a DAE. 860316643e7SJed Brown 861316643e7SJed Brown Level: beginner 862316643e7SJed Brown 863316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 864316643e7SJed Brown 865d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 866316643e7SJed Brown @*/ 867089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 868316643e7SJed Brown { 869089b2837SJed Brown PetscErrorCode ierr; 870089b2837SJed Brown SNES snes; 871e856ceecSJed Brown Vec resalloc = PETSC_NULL; 87224989b8cSPeter Brune DM dm; 873316643e7SJed Brown 874316643e7SJed Brown PetscFunctionBegin; 8750700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 876ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 87724989b8cSPeter Brune 87824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 87924989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 88024989b8cSPeter Brune 881089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 882e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 883e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 884e856ceecSJed Brown res = resalloc; 885e856ceecSJed Brown } 886089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 887e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 88824989b8cSPeter Brune 889089b2837SJed Brown PetscFunctionReturn(0); 890089b2837SJed Brown } 891089b2837SJed Brown 892089b2837SJed Brown #undef __FUNCT__ 893089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 894089b2837SJed Brown /*@C 895089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 896089b2837SJed Brown 897089b2837SJed Brown Not Collective 898089b2837SJed Brown 899089b2837SJed Brown Input Parameter: 900089b2837SJed Brown . ts - the TS context 901089b2837SJed Brown 902089b2837SJed Brown Output Parameter: 903089b2837SJed Brown + r - vector to hold residual (or PETSC_NULL) 904089b2837SJed Brown . func - the function to compute residual (or PETSC_NULL) 905089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 906089b2837SJed Brown 907089b2837SJed Brown Level: advanced 908089b2837SJed Brown 909089b2837SJed Brown .keywords: TS, nonlinear, get, function 910089b2837SJed Brown 911089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 912089b2837SJed Brown @*/ 913089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 914089b2837SJed Brown { 915089b2837SJed Brown PetscErrorCode ierr; 916089b2837SJed Brown SNES snes; 91724989b8cSPeter Brune DM dm; 918089b2837SJed Brown 919089b2837SJed Brown PetscFunctionBegin; 920089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 921089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 922089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 92324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 92424989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 925089b2837SJed Brown PetscFunctionReturn(0); 926089b2837SJed Brown } 927089b2837SJed Brown 928089b2837SJed Brown #undef __FUNCT__ 929089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 930089b2837SJed Brown /*@C 931089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 932089b2837SJed Brown 933089b2837SJed Brown Not Collective 934089b2837SJed Brown 935089b2837SJed Brown Input Parameter: 936089b2837SJed Brown . ts - the TS context 937089b2837SJed Brown 938089b2837SJed Brown Output Parameter: 939089b2837SJed Brown + r - vector to hold computed right hand side (or PETSC_NULL) 940089b2837SJed Brown . func - the function to compute right hand side (or PETSC_NULL) 941089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 942089b2837SJed Brown 943089b2837SJed Brown Level: advanced 944089b2837SJed Brown 945089b2837SJed Brown .keywords: TS, nonlinear, get, function 946089b2837SJed Brown 947089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 948089b2837SJed Brown @*/ 949089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 950089b2837SJed Brown { 951089b2837SJed Brown PetscErrorCode ierr; 952089b2837SJed Brown SNES snes; 95324989b8cSPeter Brune DM dm; 954089b2837SJed Brown 955089b2837SJed Brown PetscFunctionBegin; 956089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 957089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 958089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 95924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 96024989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 961316643e7SJed Brown PetscFunctionReturn(0); 962316643e7SJed Brown } 963316643e7SJed Brown 964316643e7SJed Brown #undef __FUNCT__ 965316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 966316643e7SJed Brown /*@C 967a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 968a4f0a591SBarry Smith you provided with TSSetIFunction(). 969316643e7SJed Brown 9703f9fe445SBarry Smith Logically Collective on TS 971316643e7SJed Brown 972316643e7SJed Brown Input Parameters: 973316643e7SJed Brown + ts - the TS context obtained from TSCreate() 974316643e7SJed Brown . A - Jacobian matrix 975316643e7SJed Brown . B - preconditioning matrix for A (may be same as A) 976316643e7SJed Brown . f - the Jacobian evaluation routine 977316643e7SJed Brown - ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL) 978316643e7SJed Brown 979316643e7SJed Brown Calling sequence of f: 9801b4a444bSJed Brown $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx); 981316643e7SJed Brown 982316643e7SJed Brown + t - time at step/stage being solved 9831b4a444bSJed Brown . U - state vector 9841b4a444bSJed Brown . U_t - time derivative of state vector 985316643e7SJed Brown . a - shift 9861b4a444bSJed Brown . A - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 987316643e7SJed Brown . B - preconditioning matrix for A, may be same as A 988316643e7SJed Brown . flag - flag indicating information about the preconditioner matrix 989316643e7SJed Brown structure (same as flag in KSPSetOperators()) 990316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 991316643e7SJed Brown 992316643e7SJed Brown Notes: 993316643e7SJed Brown The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve. 994316643e7SJed Brown 995a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 996a4f0a591SBarry Smith the Jacobian of G(U) = F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 997a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 998a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 999a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1000a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1001a4f0a591SBarry Smith 1002316643e7SJed Brown Level: beginner 1003316643e7SJed Brown 1004316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1005316643e7SJed Brown 1006ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian() 1007316643e7SJed Brown 1008316643e7SJed Brown @*/ 10097087cfbeSBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx) 1010316643e7SJed Brown { 1011316643e7SJed Brown PetscErrorCode ierr; 1012089b2837SJed Brown SNES snes; 101324989b8cSPeter Brune DM dm; 1014316643e7SJed Brown 1015316643e7SJed Brown PetscFunctionBegin; 10160700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10170700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 10180700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 1019316643e7SJed Brown if (A) PetscCheckSameComm(ts,1,A,2); 1020316643e7SJed Brown if (B) PetscCheckSameComm(ts,1,B,3); 102124989b8cSPeter Brune 102224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 102324989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 102424989b8cSPeter Brune 1025089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1026089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1027316643e7SJed Brown PetscFunctionReturn(0); 1028316643e7SJed Brown } 1029316643e7SJed Brown 10304a2ae208SSatish Balay #undef __FUNCT__ 10314a2ae208SSatish Balay #define __FUNCT__ "TSView" 10327e2c5f70SBarry Smith /*@C 1033d763cef2SBarry Smith TSView - Prints the TS data structure. 1034d763cef2SBarry Smith 10354c49b128SBarry Smith Collective on TS 1036d763cef2SBarry Smith 1037d763cef2SBarry Smith Input Parameters: 1038d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1039d763cef2SBarry Smith - viewer - visualization context 1040d763cef2SBarry Smith 1041d763cef2SBarry Smith Options Database Key: 1042d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1043d763cef2SBarry Smith 1044d763cef2SBarry Smith Notes: 1045d763cef2SBarry Smith The available visualization contexts include 1046b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1047b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1048d763cef2SBarry Smith output where only the first processor opens 1049d763cef2SBarry Smith the file. All other processors send their 1050d763cef2SBarry Smith data to the first processor to print. 1051d763cef2SBarry Smith 1052d763cef2SBarry Smith The user can open an alternative visualization context with 1053b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1054d763cef2SBarry Smith 1055d763cef2SBarry Smith Level: beginner 1056d763cef2SBarry Smith 1057d763cef2SBarry Smith .keywords: TS, timestep, view 1058d763cef2SBarry Smith 1059b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1060d763cef2SBarry Smith @*/ 10617087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1062d763cef2SBarry Smith { 1063dfbe8321SBarry Smith PetscErrorCode ierr; 106419fd82e9SBarry Smith TSType type; 1065089b2837SJed Brown PetscBool iascii,isstring,isundials; 1066d763cef2SBarry Smith 1067d763cef2SBarry Smith PetscFunctionBegin; 10680700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10693050cee2SBarry Smith if (!viewer) { 10707adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr); 10713050cee2SBarry Smith } 10720700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1073c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1074fd16b177SBarry Smith 1075251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1076251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 107732077d6dSBarry Smith if (iascii) { 1078317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr); 107977431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 1080a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%G\n",ts->max_time);CHKERRQ(ierr); 1081d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 10825ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1083c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1084d763cef2SBarry Smith } 10855ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1086193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 1087d52bd9f3SBarry Smith if (ts->ops->view) { 1088d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1089d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1090d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1091d52bd9f3SBarry Smith } 10920f5bd95cSBarry Smith } else if (isstring) { 1093a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1094b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 1095d763cef2SBarry Smith } 1096b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1097251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1098b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1099d763cef2SBarry Smith PetscFunctionReturn(0); 1100d763cef2SBarry Smith } 1101d763cef2SBarry Smith 1102d763cef2SBarry Smith 11034a2ae208SSatish Balay #undef __FUNCT__ 11044a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1105b07ff414SBarry Smith /*@ 1106d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1107d763cef2SBarry Smith the timesteppers. 1108d763cef2SBarry Smith 11093f9fe445SBarry Smith Logically Collective on TS 1110d763cef2SBarry Smith 1111d763cef2SBarry Smith Input Parameters: 1112d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1113d763cef2SBarry Smith - usrP - optional user context 1114d763cef2SBarry Smith 1115d763cef2SBarry Smith Level: intermediate 1116d763cef2SBarry Smith 1117d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1118d763cef2SBarry Smith 1119d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1120d763cef2SBarry Smith @*/ 11217087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1122d763cef2SBarry Smith { 1123d763cef2SBarry Smith PetscFunctionBegin; 11240700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1125d763cef2SBarry Smith ts->user = usrP; 1126d763cef2SBarry Smith PetscFunctionReturn(0); 1127d763cef2SBarry Smith } 1128d763cef2SBarry Smith 11294a2ae208SSatish Balay #undef __FUNCT__ 11304a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1131b07ff414SBarry Smith /*@ 1132d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1133d763cef2SBarry Smith timestepper. 1134d763cef2SBarry Smith 1135d763cef2SBarry Smith Not Collective 1136d763cef2SBarry Smith 1137d763cef2SBarry Smith Input Parameter: 1138d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1139d763cef2SBarry Smith 1140d763cef2SBarry Smith Output Parameter: 1141d763cef2SBarry Smith . usrP - user context 1142d763cef2SBarry Smith 1143d763cef2SBarry Smith Level: intermediate 1144d763cef2SBarry Smith 1145d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1146d763cef2SBarry Smith 1147d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1148d763cef2SBarry Smith @*/ 1149e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1150d763cef2SBarry Smith { 1151d763cef2SBarry Smith PetscFunctionBegin; 11520700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1153e71120c6SJed Brown *(void**)usrP = ts->user; 1154d763cef2SBarry Smith PetscFunctionReturn(0); 1155d763cef2SBarry Smith } 1156d763cef2SBarry Smith 11574a2ae208SSatish Balay #undef __FUNCT__ 11584a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1159d763cef2SBarry Smith /*@ 1160b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1161d763cef2SBarry Smith 1162d763cef2SBarry Smith Not Collective 1163d763cef2SBarry Smith 1164d763cef2SBarry Smith Input Parameter: 1165d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1166d763cef2SBarry Smith 1167d763cef2SBarry Smith Output Parameter: 1168b8123daeSJed Brown . iter - number of steps completed so far 1169d763cef2SBarry Smith 1170d763cef2SBarry Smith Level: intermediate 1171d763cef2SBarry Smith 1172d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 1173b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep() 1174d763cef2SBarry Smith @*/ 11757087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt* iter) 1176d763cef2SBarry Smith { 1177d763cef2SBarry Smith PetscFunctionBegin; 11780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11794482741eSBarry Smith PetscValidIntPointer(iter,2); 1180d763cef2SBarry Smith *iter = ts->steps; 1181d763cef2SBarry Smith PetscFunctionReturn(0); 1182d763cef2SBarry Smith } 1183d763cef2SBarry Smith 11844a2ae208SSatish Balay #undef __FUNCT__ 11854a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1186d763cef2SBarry Smith /*@ 1187d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1188d763cef2SBarry Smith as well as the initial time. 1189d763cef2SBarry Smith 11903f9fe445SBarry Smith Logically Collective on TS 1191d763cef2SBarry Smith 1192d763cef2SBarry Smith Input Parameters: 1193d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1194d763cef2SBarry Smith . initial_time - the initial time 1195d763cef2SBarry Smith - time_step - the size of the timestep 1196d763cef2SBarry Smith 1197d763cef2SBarry Smith Level: intermediate 1198d763cef2SBarry Smith 1199d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1200d763cef2SBarry Smith 1201d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1202d763cef2SBarry Smith @*/ 12037087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1204d763cef2SBarry Smith { 1205e144a568SJed Brown PetscErrorCode ierr; 1206e144a568SJed Brown 1207d763cef2SBarry Smith PetscFunctionBegin; 12080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1209e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1210d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1211d763cef2SBarry Smith PetscFunctionReturn(0); 1212d763cef2SBarry Smith } 1213d763cef2SBarry Smith 12144a2ae208SSatish Balay #undef __FUNCT__ 12154a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1216d763cef2SBarry Smith /*@ 1217d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1218d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1219d763cef2SBarry Smith 12203f9fe445SBarry Smith Logically Collective on TS 1221d763cef2SBarry Smith 1222d763cef2SBarry Smith Input Parameters: 1223d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1224d763cef2SBarry Smith - time_step - the size of the timestep 1225d763cef2SBarry Smith 1226d763cef2SBarry Smith Level: intermediate 1227d763cef2SBarry Smith 1228d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1229d763cef2SBarry Smith 1230d763cef2SBarry Smith .keywords: TS, set, timestep 1231d763cef2SBarry Smith @*/ 12327087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1233d763cef2SBarry Smith { 1234d763cef2SBarry Smith PetscFunctionBegin; 12350700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1236c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1237d763cef2SBarry Smith ts->time_step = time_step; 123831748224SBarry Smith ts->time_step_orig = time_step; 1239d763cef2SBarry Smith PetscFunctionReturn(0); 1240d763cef2SBarry Smith } 1241d763cef2SBarry Smith 12424a2ae208SSatish Balay #undef __FUNCT__ 1243a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1244a43b19c4SJed Brown /*@ 1245a43b19c4SJed Brown TSSetExactFinalTime - Determines whether to interpolate solution to the 1246a43b19c4SJed Brown exact final time requested by the user or just returns it at the final time 1247a43b19c4SJed Brown it computed. 1248a43b19c4SJed Brown 1249a43b19c4SJed Brown Logically Collective on TS 1250a43b19c4SJed Brown 1251a43b19c4SJed Brown Input Parameter: 1252a43b19c4SJed Brown + ts - the time-step context 1253a43b19c4SJed Brown - ft - PETSC_TRUE if interpolates, else PETSC_FALSE 1254a43b19c4SJed Brown 1255a43b19c4SJed Brown Level: beginner 1256a43b19c4SJed Brown 1257a43b19c4SJed Brown .seealso: TSSetDuration() 1258a43b19c4SJed Brown @*/ 1259a43b19c4SJed Brown PetscErrorCode TSSetExactFinalTime(TS ts,PetscBool flg) 1260a43b19c4SJed Brown { 1261a43b19c4SJed Brown 1262a43b19c4SJed Brown PetscFunctionBegin; 1263a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1264d8cd7023SBarry Smith PetscValidLogicalCollectiveBool(ts,flg,2); 1265a43b19c4SJed Brown ts->exact_final_time = flg; 1266a43b19c4SJed Brown PetscFunctionReturn(0); 1267a43b19c4SJed Brown } 1268a43b19c4SJed Brown 1269a43b19c4SJed Brown #undef __FUNCT__ 12704a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1271d763cef2SBarry Smith /*@ 1272d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1273d763cef2SBarry Smith 1274d763cef2SBarry Smith Not Collective 1275d763cef2SBarry Smith 1276d763cef2SBarry Smith Input Parameter: 1277d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1278d763cef2SBarry Smith 1279d763cef2SBarry Smith Output Parameter: 1280d763cef2SBarry Smith . dt - the current timestep size 1281d763cef2SBarry Smith 1282d763cef2SBarry Smith Level: intermediate 1283d763cef2SBarry Smith 1284d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1285d763cef2SBarry Smith 1286d763cef2SBarry Smith .keywords: TS, get, timestep 1287d763cef2SBarry Smith @*/ 12887087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal* dt) 1289d763cef2SBarry Smith { 1290d763cef2SBarry Smith PetscFunctionBegin; 12910700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1292f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1293d763cef2SBarry Smith *dt = ts->time_step; 1294d763cef2SBarry Smith PetscFunctionReturn(0); 1295d763cef2SBarry Smith } 1296d763cef2SBarry Smith 12974a2ae208SSatish Balay #undef __FUNCT__ 12984a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1299d8e5e3e6SSatish Balay /*@ 1300d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1301d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1302d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1303d763cef2SBarry Smith the solution at the next timestep has been calculated. 1304d763cef2SBarry Smith 1305d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1306d763cef2SBarry Smith 1307d763cef2SBarry Smith Input Parameter: 1308d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1309d763cef2SBarry Smith 1310d763cef2SBarry Smith Output Parameter: 1311d763cef2SBarry Smith . v - the vector containing the solution 1312d763cef2SBarry Smith 1313d763cef2SBarry Smith Level: intermediate 1314d763cef2SBarry Smith 1315d763cef2SBarry Smith .seealso: TSGetTimeStep() 1316d763cef2SBarry Smith 1317d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1318d763cef2SBarry Smith @*/ 13197087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1320d763cef2SBarry Smith { 1321d763cef2SBarry Smith PetscFunctionBegin; 13220700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13234482741eSBarry Smith PetscValidPointer(v,2); 13248737fe31SLisandro Dalcin *v = ts->vec_sol; 1325d763cef2SBarry Smith PetscFunctionReturn(0); 1326d763cef2SBarry Smith } 1327d763cef2SBarry Smith 1328bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 13294a2ae208SSatish Balay #undef __FUNCT__ 1330bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1331d8e5e3e6SSatish Balay /*@ 1332bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1333d763cef2SBarry Smith 1334bdad233fSMatthew Knepley Not collective 1335d763cef2SBarry Smith 1336bdad233fSMatthew Knepley Input Parameters: 1337bdad233fSMatthew Knepley + ts - The TS 1338bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1339d763cef2SBarry Smith .vb 1340d763cef2SBarry Smith U_t = A U 1341d763cef2SBarry Smith U_t = A(t) U 1342d763cef2SBarry Smith U_t = F(t,U) 1343d763cef2SBarry Smith .ve 1344d763cef2SBarry Smith 1345d763cef2SBarry Smith Level: beginner 1346d763cef2SBarry Smith 1347bdad233fSMatthew Knepley .keywords: TS, problem type 1348bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1349d763cef2SBarry Smith @*/ 13507087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1351a7cc72afSBarry Smith { 13529e2a6581SJed Brown PetscErrorCode ierr; 13539e2a6581SJed Brown 1354d763cef2SBarry Smith PetscFunctionBegin; 13550700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1356bdad233fSMatthew Knepley ts->problem_type = type; 13579e2a6581SJed Brown if (type == TS_LINEAR) { 13589e2a6581SJed Brown SNES snes; 13599e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13609e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 13619e2a6581SJed Brown } 1362d763cef2SBarry Smith PetscFunctionReturn(0); 1363d763cef2SBarry Smith } 1364d763cef2SBarry Smith 1365bdad233fSMatthew Knepley #undef __FUNCT__ 1366bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1367bdad233fSMatthew Knepley /*@C 1368bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1369bdad233fSMatthew Knepley 1370bdad233fSMatthew Knepley Not collective 1371bdad233fSMatthew Knepley 1372bdad233fSMatthew Knepley Input Parameter: 1373bdad233fSMatthew Knepley . ts - The TS 1374bdad233fSMatthew Knepley 1375bdad233fSMatthew Knepley Output Parameter: 1376bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1377bdad233fSMatthew Knepley .vb 1378089b2837SJed Brown M U_t = A U 1379089b2837SJed Brown M(t) U_t = A(t) U 1380bdad233fSMatthew Knepley U_t = F(t,U) 1381bdad233fSMatthew Knepley .ve 1382bdad233fSMatthew Knepley 1383bdad233fSMatthew Knepley Level: beginner 1384bdad233fSMatthew Knepley 1385bdad233fSMatthew Knepley .keywords: TS, problem type 1386bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1387bdad233fSMatthew Knepley @*/ 13887087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1389a7cc72afSBarry Smith { 1390bdad233fSMatthew Knepley PetscFunctionBegin; 13910700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 13924482741eSBarry Smith PetscValidIntPointer(type,2); 1393bdad233fSMatthew Knepley *type = ts->problem_type; 1394bdad233fSMatthew Knepley PetscFunctionReturn(0); 1395bdad233fSMatthew Knepley } 1396d763cef2SBarry Smith 13974a2ae208SSatish Balay #undef __FUNCT__ 13984a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1399d763cef2SBarry Smith /*@ 1400d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1401d763cef2SBarry Smith of a timestepper. 1402d763cef2SBarry Smith 1403d763cef2SBarry Smith Collective on TS 1404d763cef2SBarry Smith 1405d763cef2SBarry Smith Input Parameter: 1406d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1407d763cef2SBarry Smith 1408d763cef2SBarry Smith Notes: 1409d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1410d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1411d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1412d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1413d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1414d763cef2SBarry Smith 1415d763cef2SBarry Smith Level: advanced 1416d763cef2SBarry Smith 1417d763cef2SBarry Smith .keywords: TS, timestep, setup 1418d763cef2SBarry Smith 1419d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1420d763cef2SBarry Smith @*/ 14217087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1422d763cef2SBarry Smith { 1423dfbe8321SBarry Smith PetscErrorCode ierr; 14246c6b9e74SPeter Brune DM dm; 14256c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 14266c6b9e74SPeter Brune PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*); 14276c6b9e74SPeter Brune TSIJacobian ijac; 14286c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1429d763cef2SBarry Smith 1430d763cef2SBarry Smith PetscFunctionBegin; 14310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1432277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1433277b19d0SLisandro Dalcin 14347adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 14359596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1436d763cef2SBarry Smith } 1437a43b19c4SJed Brown if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE; 1438277b19d0SLisandro Dalcin 1439277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1440277b19d0SLisandro Dalcin 14411c3436cfSJed Brown ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 14421c3436cfSJed Brown 1443277b19d0SLisandro Dalcin if (ts->ops->setup) { 1444000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1445277b19d0SLisandro Dalcin } 1446277b19d0SLisandro Dalcin 14476c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 14486c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 14496c6b9e74SPeter Brune */ 14506c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 14516c6b9e74SPeter Brune ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr); 14526c6b9e74SPeter Brune if (!func) { 14536c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 14546c6b9e74SPeter Brune } 14556c6b9e74SPeter 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. 14566c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 14576c6b9e74SPeter Brune */ 14586c6b9e74SPeter Brune ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr); 14596c6b9e74SPeter Brune ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr); 14606c6b9e74SPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr); 14616c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 14626c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 14636c6b9e74SPeter Brune } 1464277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1465277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1466277b19d0SLisandro Dalcin } 1467277b19d0SLisandro Dalcin 1468277b19d0SLisandro Dalcin #undef __FUNCT__ 1469277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1470277b19d0SLisandro Dalcin /*@ 1471277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1472277b19d0SLisandro Dalcin 1473277b19d0SLisandro Dalcin Collective on TS 1474277b19d0SLisandro Dalcin 1475277b19d0SLisandro Dalcin Input Parameter: 1476277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1477277b19d0SLisandro Dalcin 1478277b19d0SLisandro Dalcin Level: beginner 1479277b19d0SLisandro Dalcin 1480277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1481277b19d0SLisandro Dalcin 1482277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1483277b19d0SLisandro Dalcin @*/ 1484277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1485277b19d0SLisandro Dalcin { 1486277b19d0SLisandro Dalcin PetscErrorCode ierr; 1487277b19d0SLisandro Dalcin 1488277b19d0SLisandro Dalcin PetscFunctionBegin; 1489277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1490277b19d0SLisandro Dalcin if (ts->ops->reset) { 1491277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1492277b19d0SLisandro Dalcin } 1493277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 14944e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 14954e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1496214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 14976bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1498e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 1499e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 150038637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 1501277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1502d763cef2SBarry Smith PetscFunctionReturn(0); 1503d763cef2SBarry Smith } 1504d763cef2SBarry Smith 15054a2ae208SSatish Balay #undef __FUNCT__ 15064a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1507d8e5e3e6SSatish Balay /*@ 1508d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1509d763cef2SBarry Smith with TSCreate(). 1510d763cef2SBarry Smith 1511d763cef2SBarry Smith Collective on TS 1512d763cef2SBarry Smith 1513d763cef2SBarry Smith Input Parameter: 1514d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1515d763cef2SBarry Smith 1516d763cef2SBarry Smith Level: beginner 1517d763cef2SBarry Smith 1518d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1519d763cef2SBarry Smith 1520d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1521d763cef2SBarry Smith @*/ 15226bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1523d763cef2SBarry Smith { 15246849ba73SBarry Smith PetscErrorCode ierr; 1525d763cef2SBarry Smith 1526d763cef2SBarry Smith PetscFunctionBegin; 15276bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 15286bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 15296bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1530d763cef2SBarry Smith 15316bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1532277b19d0SLisandro Dalcin 1533be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 15346bf464f9SBarry Smith ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 15356bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 15366d4c513bSLisandro Dalcin 153784df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 15386bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 15396bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 15406bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 15416d4c513bSLisandro Dalcin 1542a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1543d763cef2SBarry Smith PetscFunctionReturn(0); 1544d763cef2SBarry Smith } 1545d763cef2SBarry Smith 15464a2ae208SSatish Balay #undef __FUNCT__ 15474a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1548d8e5e3e6SSatish Balay /*@ 1549d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1550d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1551d763cef2SBarry Smith 1552d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1553d763cef2SBarry Smith 1554d763cef2SBarry Smith Input Parameter: 1555d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1556d763cef2SBarry Smith 1557d763cef2SBarry Smith Output Parameter: 1558d763cef2SBarry Smith . snes - the nonlinear solver context 1559d763cef2SBarry Smith 1560d763cef2SBarry Smith Notes: 1561d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1562d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 156394b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1564d763cef2SBarry Smith 1565d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 1566d763cef2SBarry Smith this case TSGetSNES() returns PETSC_NULL in snes. 1567d763cef2SBarry Smith 1568d763cef2SBarry Smith Level: beginner 1569d763cef2SBarry Smith 1570d763cef2SBarry Smith .keywords: timestep, get, SNES 1571d763cef2SBarry Smith @*/ 15727087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1573d763cef2SBarry Smith { 1574d372ba47SLisandro Dalcin PetscErrorCode ierr; 1575d372ba47SLisandro Dalcin 1576d763cef2SBarry Smith PetscFunctionBegin; 15770700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15784482741eSBarry Smith PetscValidPointer(snes,2); 1579d372ba47SLisandro Dalcin if (!ts->snes) { 1580d372ba47SLisandro Dalcin ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 15816c6b9e74SPeter Brune ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 1582d372ba47SLisandro Dalcin ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1583d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 1584496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 15859e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 15869e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 15879e2a6581SJed Brown } 1588d372ba47SLisandro Dalcin } 1589d763cef2SBarry Smith *snes = ts->snes; 1590d763cef2SBarry Smith PetscFunctionReturn(0); 1591d763cef2SBarry Smith } 1592d763cef2SBarry Smith 15934a2ae208SSatish Balay #undef __FUNCT__ 159494b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 1595d8e5e3e6SSatish Balay /*@ 159694b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 1597d763cef2SBarry Smith a TS (timestepper) context. 1598d763cef2SBarry Smith 159994b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 1600d763cef2SBarry Smith 1601d763cef2SBarry Smith Input Parameter: 1602d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1603d763cef2SBarry Smith 1604d763cef2SBarry Smith Output Parameter: 160594b7f48cSBarry Smith . ksp - the nonlinear solver context 1606d763cef2SBarry Smith 1607d763cef2SBarry Smith Notes: 160894b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 1609d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 1610d763cef2SBarry Smith KSP and PC contexts as well. 1611d763cef2SBarry Smith 161294b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 161394b7f48cSBarry Smith in this case TSGetKSP() returns PETSC_NULL in ksp. 1614d763cef2SBarry Smith 1615d763cef2SBarry Smith Level: beginner 1616d763cef2SBarry Smith 161794b7f48cSBarry Smith .keywords: timestep, get, KSP 1618d763cef2SBarry Smith @*/ 16197087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1620d763cef2SBarry Smith { 1621d372ba47SLisandro Dalcin PetscErrorCode ierr; 1622089b2837SJed Brown SNES snes; 1623d372ba47SLisandro Dalcin 1624d763cef2SBarry Smith PetscFunctionBegin; 16250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 16264482741eSBarry Smith PetscValidPointer(ksp,2); 162717186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1628e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1629089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1630089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1631d763cef2SBarry Smith PetscFunctionReturn(0); 1632d763cef2SBarry Smith } 1633d763cef2SBarry Smith 1634d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 1635d763cef2SBarry Smith 16364a2ae208SSatish Balay #undef __FUNCT__ 1637adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 1638adb62b0dSMatthew Knepley /*@ 1639adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 1640adb62b0dSMatthew Knepley maximum time for iteration. 1641adb62b0dSMatthew Knepley 16423f9fe445SBarry Smith Not Collective 1643adb62b0dSMatthew Knepley 1644adb62b0dSMatthew Knepley Input Parameters: 1645adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 1646adb62b0dSMatthew Knepley . maxsteps - maximum number of iterations to use, or PETSC_NULL 1647adb62b0dSMatthew Knepley - maxtime - final time to iterate to, or PETSC_NULL 1648adb62b0dSMatthew Knepley 1649adb62b0dSMatthew Knepley Level: intermediate 1650adb62b0dSMatthew Knepley 1651adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 1652adb62b0dSMatthew Knepley @*/ 16537087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1654adb62b0dSMatthew Knepley { 1655adb62b0dSMatthew Knepley PetscFunctionBegin; 16560700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1657abc0a331SBarry Smith if (maxsteps) { 16584482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 1659adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 1660adb62b0dSMatthew Knepley } 1661abc0a331SBarry Smith if (maxtime) { 16624482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 1663adb62b0dSMatthew Knepley *maxtime = ts->max_time; 1664adb62b0dSMatthew Knepley } 1665adb62b0dSMatthew Knepley PetscFunctionReturn(0); 1666adb62b0dSMatthew Knepley } 1667adb62b0dSMatthew Knepley 1668adb62b0dSMatthew Knepley #undef __FUNCT__ 16694a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 1670d763cef2SBarry Smith /*@ 1671d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 1672d763cef2SBarry Smith maximum time for iteration. 1673d763cef2SBarry Smith 16743f9fe445SBarry Smith Logically Collective on TS 1675d763cef2SBarry Smith 1676d763cef2SBarry Smith Input Parameters: 1677d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1678d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 1679d763cef2SBarry Smith - maxtime - final time to iterate to 1680d763cef2SBarry Smith 1681d763cef2SBarry Smith Options Database Keys: 1682d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 16833bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 1684d763cef2SBarry Smith 1685d763cef2SBarry Smith Notes: 1686d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 1687d763cef2SBarry Smith 1688d763cef2SBarry Smith Level: intermediate 1689d763cef2SBarry Smith 1690d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 1691a43b19c4SJed Brown 1692a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 1693d763cef2SBarry Smith @*/ 16947087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1695d763cef2SBarry Smith { 1696d763cef2SBarry Smith PetscFunctionBegin; 16970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1698c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1699c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 170039b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 170139b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 1702d763cef2SBarry Smith PetscFunctionReturn(0); 1703d763cef2SBarry Smith } 1704d763cef2SBarry Smith 17054a2ae208SSatish Balay #undef __FUNCT__ 17064a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 1707d763cef2SBarry Smith /*@ 1708d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 1709d763cef2SBarry Smith for use by the TS routines. 1710d763cef2SBarry Smith 17113f9fe445SBarry Smith Logically Collective on TS and Vec 1712d763cef2SBarry Smith 1713d763cef2SBarry Smith Input Parameters: 1714d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1715d763cef2SBarry Smith - x - the solution vector 1716d763cef2SBarry Smith 1717d763cef2SBarry Smith Level: beginner 1718d763cef2SBarry Smith 1719d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 1720d763cef2SBarry Smith @*/ 17217087cfbeSBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec x) 1722d763cef2SBarry Smith { 17238737fe31SLisandro Dalcin PetscErrorCode ierr; 1724496e6a7aSJed Brown DM dm; 17258737fe31SLisandro Dalcin 1726d763cef2SBarry Smith PetscFunctionBegin; 17270700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17280700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 17298737fe31SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 17306bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 17318737fe31SLisandro Dalcin ts->vec_sol = x; 1732496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1733496e6a7aSJed Brown ierr = DMShellSetGlobalVector(dm,x);CHKERRQ(ierr); 1734d763cef2SBarry Smith PetscFunctionReturn(0); 1735d763cef2SBarry Smith } 1736d763cef2SBarry Smith 1737e74ef692SMatthew Knepley #undef __FUNCT__ 1738e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 1739ac226902SBarry Smith /*@C 1740000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 17413f2090d5SJed Brown called once at the beginning of each time step. 1742000e7ae3SMatthew Knepley 17433f9fe445SBarry Smith Logically Collective on TS 1744000e7ae3SMatthew Knepley 1745000e7ae3SMatthew Knepley Input Parameters: 1746000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1747000e7ae3SMatthew Knepley - func - The function 1748000e7ae3SMatthew Knepley 1749000e7ae3SMatthew Knepley Calling sequence of func: 1750000e7ae3SMatthew Knepley . func (TS ts); 1751000e7ae3SMatthew Knepley 1752000e7ae3SMatthew Knepley Level: intermediate 1753000e7ae3SMatthew Knepley 1754b8123daeSJed Brown Note: 1755b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 1756b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 1757b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 1758b8123daeSJed Brown 1759000e7ae3SMatthew Knepley .keywords: TS, timestep 1760b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep() 1761000e7ae3SMatthew Knepley @*/ 17627087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1763000e7ae3SMatthew Knepley { 1764000e7ae3SMatthew Knepley PetscFunctionBegin; 17650700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1766000e7ae3SMatthew Knepley ts->ops->prestep = func; 1767000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1768000e7ae3SMatthew Knepley } 1769000e7ae3SMatthew Knepley 1770e74ef692SMatthew Knepley #undef __FUNCT__ 17713f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 177209ee8438SJed Brown /*@ 17733f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 17743f2090d5SJed Brown 17753f2090d5SJed Brown Collective on TS 17763f2090d5SJed Brown 17773f2090d5SJed Brown Input Parameters: 17783f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 17793f2090d5SJed Brown 17803f2090d5SJed Brown Notes: 17813f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 17823f2090d5SJed Brown so most users would not generally call this routine themselves. 17833f2090d5SJed Brown 17843f2090d5SJed Brown Level: developer 17853f2090d5SJed Brown 17863f2090d5SJed Brown .keywords: TS, timestep 1787b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep() 17883f2090d5SJed Brown @*/ 17897087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 17903f2090d5SJed Brown { 17913f2090d5SJed Brown PetscErrorCode ierr; 17923f2090d5SJed Brown 17933f2090d5SJed Brown PetscFunctionBegin; 17940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 179572ac3e02SJed Brown if (ts->ops->prestep) { 17963f2090d5SJed Brown PetscStackPush("TS PreStep function"); 17973f2090d5SJed Brown ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 17983f2090d5SJed Brown PetscStackPop; 1799312ce896SJed Brown } 18003f2090d5SJed Brown PetscFunctionReturn(0); 18013f2090d5SJed Brown } 18023f2090d5SJed Brown 18033f2090d5SJed Brown #undef __FUNCT__ 1804b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 1805b8123daeSJed Brown /*@C 1806b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 1807b8123daeSJed Brown called once at the beginning of each stage. 1808b8123daeSJed Brown 1809b8123daeSJed Brown Logically Collective on TS 1810b8123daeSJed Brown 1811b8123daeSJed Brown Input Parameters: 1812b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 1813b8123daeSJed Brown - func - The function 1814b8123daeSJed Brown 1815b8123daeSJed Brown Calling sequence of func: 1816b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 1817b8123daeSJed Brown 1818b8123daeSJed Brown Level: intermediate 1819b8123daeSJed Brown 1820b8123daeSJed Brown Note: 1821b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 1822b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 1823b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 1824b8123daeSJed Brown 1825b8123daeSJed Brown .keywords: TS, timestep 1826b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 1827b8123daeSJed Brown @*/ 1828b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 1829b8123daeSJed Brown { 1830b8123daeSJed Brown PetscFunctionBegin; 1831b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1832b8123daeSJed Brown ts->ops->prestage = func; 1833b8123daeSJed Brown PetscFunctionReturn(0); 1834b8123daeSJed Brown } 1835b8123daeSJed Brown 1836b8123daeSJed Brown #undef __FUNCT__ 1837b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 1838b8123daeSJed Brown /*@ 1839b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 1840b8123daeSJed Brown 1841b8123daeSJed Brown Collective on TS 1842b8123daeSJed Brown 1843b8123daeSJed Brown Input Parameters: 1844b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 1845b8123daeSJed Brown 1846b8123daeSJed Brown Notes: 1847b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 1848b8123daeSJed Brown most users would not generally call this routine themselves. 1849b8123daeSJed Brown 1850b8123daeSJed Brown Level: developer 1851b8123daeSJed Brown 1852b8123daeSJed Brown .keywords: TS, timestep 1853b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep() 1854b8123daeSJed Brown @*/ 1855b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 1856b8123daeSJed Brown { 1857b8123daeSJed Brown PetscErrorCode ierr; 1858b8123daeSJed Brown 1859b8123daeSJed Brown PetscFunctionBegin; 1860b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1861b8123daeSJed Brown if (ts->ops->prestage) { 1862b8123daeSJed Brown PetscStackPush("TS PreStage function"); 1863b8123daeSJed Brown ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr); 1864b8123daeSJed Brown PetscStackPop; 1865b8123daeSJed Brown } 1866b8123daeSJed Brown PetscFunctionReturn(0); 1867b8123daeSJed Brown } 1868b8123daeSJed Brown 1869b8123daeSJed Brown #undef __FUNCT__ 1870e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 1871ac226902SBarry Smith /*@C 1872000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 18733f2090d5SJed Brown called once at the end of each time step. 1874000e7ae3SMatthew Knepley 18753f9fe445SBarry Smith Logically Collective on TS 1876000e7ae3SMatthew Knepley 1877000e7ae3SMatthew Knepley Input Parameters: 1878000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1879000e7ae3SMatthew Knepley - func - The function 1880000e7ae3SMatthew Knepley 1881000e7ae3SMatthew Knepley Calling sequence of func: 1882b8123daeSJed Brown $ func (TS ts); 1883000e7ae3SMatthew Knepley 1884000e7ae3SMatthew Knepley Level: intermediate 1885000e7ae3SMatthew Knepley 1886000e7ae3SMatthew Knepley .keywords: TS, timestep 1887b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 1888000e7ae3SMatthew Knepley @*/ 18897087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 1890000e7ae3SMatthew Knepley { 1891000e7ae3SMatthew Knepley PetscFunctionBegin; 18920700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1893000e7ae3SMatthew Knepley ts->ops->poststep = func; 1894000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1895000e7ae3SMatthew Knepley } 1896000e7ae3SMatthew Knepley 1897e74ef692SMatthew Knepley #undef __FUNCT__ 18983f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 189909ee8438SJed Brown /*@ 19003f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 19013f2090d5SJed Brown 19023f2090d5SJed Brown Collective on TS 19033f2090d5SJed Brown 19043f2090d5SJed Brown Input Parameters: 19053f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 19063f2090d5SJed Brown 19073f2090d5SJed Brown Notes: 19083f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 19093f2090d5SJed Brown so most users would not generally call this routine themselves. 19103f2090d5SJed Brown 19113f2090d5SJed Brown Level: developer 19123f2090d5SJed Brown 19133f2090d5SJed Brown .keywords: TS, timestep 19143f2090d5SJed Brown @*/ 19157087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 19163f2090d5SJed Brown { 19173f2090d5SJed Brown PetscErrorCode ierr; 19183f2090d5SJed Brown 19193f2090d5SJed Brown PetscFunctionBegin; 19200700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 192172ac3e02SJed Brown if (ts->ops->poststep) { 19223f2090d5SJed Brown PetscStackPush("TS PostStep function"); 19233f2090d5SJed Brown ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 19243f2090d5SJed Brown PetscStackPop; 192572ac3e02SJed Brown } 19263f2090d5SJed Brown PetscFunctionReturn(0); 19273f2090d5SJed Brown } 19283f2090d5SJed Brown 1929d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 1930d763cef2SBarry Smith 19314a2ae208SSatish Balay #undef __FUNCT__ 1932a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 1933d763cef2SBarry Smith /*@C 1934a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 1935d763cef2SBarry Smith timestep to display the iteration's progress. 1936d763cef2SBarry Smith 19373f9fe445SBarry Smith Logically Collective on TS 1938d763cef2SBarry Smith 1939d763cef2SBarry Smith Input Parameters: 1940d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1941e213d8f1SJed Brown . monitor - monitoring routine 1942329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 1943b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desired) 1944b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1945b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 1946d763cef2SBarry Smith 1947e213d8f1SJed Brown Calling sequence of monitor: 1948e213d8f1SJed Brown $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx) 1949d763cef2SBarry Smith 1950d763cef2SBarry Smith + ts - the TS context 1951d763cef2SBarry Smith . steps - iteration number 19521f06c33eSBarry Smith . time - current time 1953d763cef2SBarry Smith . x - current iterate 1954d763cef2SBarry Smith - mctx - [optional] monitoring context 1955d763cef2SBarry Smith 1956d763cef2SBarry Smith Notes: 1957d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 1958d763cef2SBarry Smith already has been loaded. 1959d763cef2SBarry Smith 1960025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 1961025f1a04SBarry Smith 1962d763cef2SBarry Smith Level: intermediate 1963d763cef2SBarry Smith 1964d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1965d763cef2SBarry Smith 1966a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 1967d763cef2SBarry Smith @*/ 1968c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 1969d763cef2SBarry Smith { 1970d763cef2SBarry Smith PetscFunctionBegin; 19710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 197217186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1973d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 19748704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 1975d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 1976d763cef2SBarry Smith PetscFunctionReturn(0); 1977d763cef2SBarry Smith } 1978d763cef2SBarry Smith 19794a2ae208SSatish Balay #undef __FUNCT__ 1980a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 1981d763cef2SBarry Smith /*@C 1982a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 1983d763cef2SBarry Smith 19843f9fe445SBarry Smith Logically Collective on TS 1985d763cef2SBarry Smith 1986d763cef2SBarry Smith Input Parameters: 1987d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1988d763cef2SBarry Smith 1989d763cef2SBarry Smith Notes: 1990d763cef2SBarry Smith There is no way to remove a single, specific monitor. 1991d763cef2SBarry Smith 1992d763cef2SBarry Smith Level: intermediate 1993d763cef2SBarry Smith 1994d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1995d763cef2SBarry Smith 1996a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 1997d763cef2SBarry Smith @*/ 19987087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 1999d763cef2SBarry Smith { 2000d952e501SBarry Smith PetscErrorCode ierr; 2001d952e501SBarry Smith PetscInt i; 2002d952e501SBarry Smith 2003d763cef2SBarry Smith PetscFunctionBegin; 20040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2005d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 20068704b422SBarry Smith if (ts->monitordestroy[i]) { 20078704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 2008d952e501SBarry Smith } 2009d952e501SBarry Smith } 2010d763cef2SBarry Smith ts->numbermonitors = 0; 2011d763cef2SBarry Smith PetscFunctionReturn(0); 2012d763cef2SBarry Smith } 2013d763cef2SBarry Smith 20144a2ae208SSatish Balay #undef __FUNCT__ 2015a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 2016d8e5e3e6SSatish Balay /*@ 2017a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 20185516499fSSatish Balay 20195516499fSSatish Balay Level: intermediate 202041251cbbSSatish Balay 20215516499fSSatish Balay .keywords: TS, set, monitor 20225516499fSSatish Balay 202341251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 202441251cbbSSatish Balay @*/ 2025649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 2026d763cef2SBarry Smith { 2027dfbe8321SBarry Smith PetscErrorCode ierr; 2028649052a6SBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm); 2029d132466eSBarry Smith 2030d763cef2SBarry Smith PetscFunctionBegin; 2031649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2032971832b6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr); 2033649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2034d763cef2SBarry Smith PetscFunctionReturn(0); 2035d763cef2SBarry Smith } 2036d763cef2SBarry Smith 20374a2ae208SSatish Balay #undef __FUNCT__ 2038cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 2039cd652676SJed Brown /*@ 2040cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 2041cd652676SJed Brown 2042cd652676SJed Brown Logically Collective on TS 2043cd652676SJed Brown 2044cd652676SJed Brown Input Argument: 2045cd652676SJed Brown . ts - time stepping context 2046cd652676SJed Brown 2047cd652676SJed Brown Output Argument: 2048cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 2049cd652676SJed Brown 2050cd652676SJed Brown Level: intermediate 2051cd652676SJed Brown 2052cd652676SJed Brown .keywords: TS, set 2053cd652676SJed Brown 2054cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 2055cd652676SJed Brown @*/ 2056cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 2057cd652676SJed Brown { 2058cd652676SJed Brown 2059cd652676SJed Brown PetscFunctionBegin; 2060cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2061cd652676SJed Brown ts->retain_stages = flg; 2062cd652676SJed Brown PetscFunctionReturn(0); 2063cd652676SJed Brown } 2064cd652676SJed Brown 2065cd652676SJed Brown #undef __FUNCT__ 2066cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 2067cd652676SJed Brown /*@ 2068cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 2069cd652676SJed Brown 2070cd652676SJed Brown Collective on TS 2071cd652676SJed Brown 2072cd652676SJed Brown Input Argument: 2073cd652676SJed Brown + ts - time stepping context 2074cd652676SJed Brown - t - time to interpolate to 2075cd652676SJed Brown 2076cd652676SJed Brown Output Argument: 2077cd652676SJed Brown . X - state at given time 2078cd652676SJed Brown 2079cd652676SJed Brown Notes: 2080cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 2081cd652676SJed Brown 2082cd652676SJed Brown Level: intermediate 2083cd652676SJed Brown 2084cd652676SJed Brown Developer Notes: 2085cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 2086cd652676SJed Brown 2087cd652676SJed Brown .keywords: TS, set 2088cd652676SJed Brown 2089cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 2090cd652676SJed Brown @*/ 2091cd652676SJed Brown PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec X) 2092cd652676SJed Brown { 2093cd652676SJed Brown PetscErrorCode ierr; 2094cd652676SJed Brown 2095cd652676SJed Brown PetscFunctionBegin; 2096cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2097d8cd7023SBarry 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); 2098cd652676SJed Brown if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 2099cd652676SJed Brown ierr = (*ts->ops->interpolate)(ts,t,X);CHKERRQ(ierr); 2100cd652676SJed Brown PetscFunctionReturn(0); 2101cd652676SJed Brown } 2102cd652676SJed Brown 2103cd652676SJed Brown #undef __FUNCT__ 21044a2ae208SSatish Balay #define __FUNCT__ "TSStep" 2105d763cef2SBarry Smith /*@ 21066d9e5789SSean Farley TSStep - Steps one time step 2107d763cef2SBarry Smith 2108d763cef2SBarry Smith Collective on TS 2109d763cef2SBarry Smith 2110d763cef2SBarry Smith Input Parameter: 2111d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2112d763cef2SBarry Smith 21136d9e5789SSean Farley Level: intermediate 2114d763cef2SBarry Smith 2115b8123daeSJed Brown Notes: 2116b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 2117b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 2118b8123daeSJed Brown 2119d763cef2SBarry Smith .keywords: TS, timestep, solve 2120d763cef2SBarry Smith 2121b8123daeSJed Brown .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage() 2122d763cef2SBarry Smith @*/ 2123193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 2124d763cef2SBarry Smith { 2125362cd11cSLisandro Dalcin PetscReal ptime_prev; 2126dfbe8321SBarry Smith PetscErrorCode ierr; 2127d763cef2SBarry Smith 2128d763cef2SBarry Smith PetscFunctionBegin; 21290700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2130d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 2131d405a339SMatthew Knepley 2132362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 2133362cd11cSLisandro Dalcin 2134362cd11cSLisandro Dalcin ptime_prev = ts->ptime; 2135d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2136193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 2137d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2138362cd11cSLisandro Dalcin ts->time_step_prev = ts->ptime - ptime_prev; 2139362cd11cSLisandro Dalcin 2140362cd11cSLisandro Dalcin if (ts->reason < 0) { 2141cef5090cSJed Brown if (ts->errorifstepfailed) { 2142cef5090cSJed Brown if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) { 2143cef5090cSJed 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]); 2144cef5090cSJed Brown } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 2145cef5090cSJed Brown } 2146362cd11cSLisandro Dalcin } else if (!ts->reason) { 2147362cd11cSLisandro Dalcin if (ts->steps >= ts->max_steps) 2148362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITS; 2149362cd11cSLisandro Dalcin else if (ts->ptime >= ts->max_time) 2150362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_TIME; 2151362cd11cSLisandro Dalcin } 2152362cd11cSLisandro Dalcin 2153d763cef2SBarry Smith PetscFunctionReturn(0); 2154d763cef2SBarry Smith } 2155d763cef2SBarry Smith 21564a2ae208SSatish Balay #undef __FUNCT__ 215705175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 215805175c85SJed Brown /*@ 215905175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 216005175c85SJed Brown 21611c3436cfSJed Brown Collective on TS 216205175c85SJed Brown 216305175c85SJed Brown Input Arguments: 21641c3436cfSJed Brown + ts - time stepping context 21651c3436cfSJed Brown . order - desired order of accuracy 21661c3436cfSJed Brown - done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available) 216705175c85SJed Brown 216805175c85SJed Brown Output Arguments: 21691c3436cfSJed Brown . X - state at the end of the current step 217005175c85SJed Brown 217105175c85SJed Brown Level: advanced 217205175c85SJed Brown 2173108c343cSJed Brown Notes: 2174108c343cSJed Brown This function cannot be called until all stages have been evaluated. 2175108c343cSJed 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. 2176108c343cSJed Brown 21771c3436cfSJed Brown .seealso: TSStep(), TSAdapt 217805175c85SJed Brown @*/ 21791c3436cfSJed Brown PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec X,PetscBool *done) 218005175c85SJed Brown { 218105175c85SJed Brown PetscErrorCode ierr; 218205175c85SJed Brown 218305175c85SJed Brown PetscFunctionBegin; 218405175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 218505175c85SJed Brown PetscValidType(ts,1); 218605175c85SJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,3); 21871c3436cfSJed Brown if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 21881c3436cfSJed Brown ierr = (*ts->ops->evaluatestep)(ts,order,X,done);CHKERRQ(ierr); 218905175c85SJed Brown PetscFunctionReturn(0); 219005175c85SJed Brown } 219105175c85SJed Brown 219205175c85SJed Brown #undef __FUNCT__ 21936a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 21946a4d4014SLisandro Dalcin /*@ 21956a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 21966a4d4014SLisandro Dalcin 21976a4d4014SLisandro Dalcin Collective on TS 21986a4d4014SLisandro Dalcin 21996a4d4014SLisandro Dalcin Input Parameter: 22006a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 2201362cd11cSLisandro Dalcin - x - the solution vector 22026a4d4014SLisandro Dalcin 22035a3a76d0SJed Brown Output Parameter: 22045a3a76d0SJed Brown . ftime - time of the state vector x upon completion 22055a3a76d0SJed Brown 22066a4d4014SLisandro Dalcin Level: beginner 22076a4d4014SLisandro Dalcin 22085a3a76d0SJed Brown Notes: 22095a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 22105a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 22115a3a76d0SJed Brown stepped over the final time. 22125a3a76d0SJed Brown 22136a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 22146a4d4014SLisandro Dalcin 22156a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 22166a4d4014SLisandro Dalcin @*/ 22175a3a76d0SJed Brown PetscErrorCode TSSolve(TS ts,Vec x,PetscReal *ftime) 22186a4d4014SLisandro Dalcin { 22194d7d938eSLisandro Dalcin PetscBool flg; 22204d7d938eSLisandro Dalcin char filename[PETSC_MAX_PATH_LEN]; 22214d7d938eSLisandro Dalcin PetscViewer viewer; 22226a4d4014SLisandro Dalcin PetscErrorCode ierr; 2223f22f69f0SBarry Smith 22246a4d4014SLisandro Dalcin PetscFunctionBegin; 22250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2226193ac0bcSJed Brown PetscValidHeaderSpecific(x,VEC_CLASSID,2); 22275a3a76d0SJed Brown if (ts->exact_final_time) { /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */ 22285a3a76d0SJed Brown if (!ts->vec_sol || x == ts->vec_sol) { 22295a3a76d0SJed Brown Vec y; 22305a3a76d0SJed Brown ierr = VecDuplicate(x,&y);CHKERRQ(ierr); 22315a3a76d0SJed Brown ierr = TSSetSolution(ts,y);CHKERRQ(ierr); 22325a3a76d0SJed Brown ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */ 22335a3a76d0SJed Brown } 2234362cd11cSLisandro Dalcin ierr = VecCopy(x,ts->vec_sol);CHKERRQ(ierr); 22355a3a76d0SJed Brown } else { 2236193ac0bcSJed Brown ierr = TSSetSolution(ts,x);CHKERRQ(ierr); 22375a3a76d0SJed Brown } 2238b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 22396a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 2240193ac0bcSJed Brown ts->steps = 0; 22415ef26d82SJed Brown ts->ksp_its = 0; 22425ef26d82SJed Brown ts->snes_its = 0; 2243c610991cSLisandro Dalcin ts->num_snes_failures = 0; 2244c610991cSLisandro Dalcin ts->reject = 0; 2245193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 2246193ac0bcSJed Brown 2247193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 2248193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 22495a3a76d0SJed Brown ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr); 2250a5b82c69SSean Farley if (ftime) *ftime = ts->ptime; 2251193ac0bcSJed Brown } else { 22526a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 2253362cd11cSLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2254362cd11cSLisandro Dalcin if (ts->steps >= ts->max_steps) 2255362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITS; 2256362cd11cSLisandro Dalcin else if (ts->ptime >= ts->max_time) 2257362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_TIME; 2258e1a7a14fSJed Brown while (!ts->reason) { 2259193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 2260193ac0bcSJed Brown ierr = TSPostStep(ts);CHKERRQ(ierr); 2261193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2262193ac0bcSJed Brown } 2263362cd11cSLisandro Dalcin if (ts->exact_final_time && ts->ptime > ts->max_time) { 2264a43b19c4SJed Brown ierr = TSInterpolate(ts,ts->max_time,x);CHKERRQ(ierr); 22655a3a76d0SJed Brown if (ftime) *ftime = ts->max_time; 22660574a7fbSJed Brown } else { 22670574a7fbSJed Brown ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr); 22680574a7fbSJed Brown if (ftime) *ftime = ts->ptime; 22690574a7fbSJed Brown } 2270193ac0bcSJed Brown } 22713923b477SBarry Smith ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 22724d7d938eSLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 22734d7d938eSLisandro Dalcin if (flg && !PetscPreLoadingOn) { 22744d7d938eSLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr); 22754d7d938eSLisandro Dalcin ierr = TSView(ts,viewer);CHKERRQ(ierr); 22764d7d938eSLisandro Dalcin ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 2277193ac0bcSJed Brown } 22786a4d4014SLisandro Dalcin PetscFunctionReturn(0); 22796a4d4014SLisandro Dalcin } 22806a4d4014SLisandro Dalcin 22816a4d4014SLisandro Dalcin #undef __FUNCT__ 22824a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 2283228d79bcSJed Brown /*@ 2284228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 2285228d79bcSJed Brown 2286228d79bcSJed Brown Collective on TS 2287228d79bcSJed Brown 2288228d79bcSJed Brown Input Parameters: 2289228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 2290228d79bcSJed Brown . step - step number that has just completed 2291228d79bcSJed Brown . ptime - model time of the state 2292228d79bcSJed Brown - x - state at the current model time 2293228d79bcSJed Brown 2294228d79bcSJed Brown Notes: 2295228d79bcSJed Brown TSMonitor() is typically used within the time stepping implementations. 2296228d79bcSJed Brown Users might call this function when using the TSStep() interface instead of TSSolve(). 2297228d79bcSJed Brown 2298228d79bcSJed Brown Level: advanced 2299228d79bcSJed Brown 2300228d79bcSJed Brown .keywords: TS, timestep 2301228d79bcSJed Brown @*/ 2302a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x) 2303d763cef2SBarry Smith { 23046849ba73SBarry Smith PetscErrorCode ierr; 2305a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 2306d763cef2SBarry Smith 2307d763cef2SBarry Smith PetscFunctionBegin; 2308d763cef2SBarry Smith for (i=0; i<n; i++) { 2309142b95e3SSatish Balay ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr); 2310d763cef2SBarry Smith } 2311d763cef2SBarry Smith PetscFunctionReturn(0); 2312d763cef2SBarry Smith } 2313d763cef2SBarry Smith 2314d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 23150b039ecaSBarry Smith struct _n_TSMonitorLGCtx { 23160b039ecaSBarry Smith PetscDrawLG lg; 23170b039ecaSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 2318201da799SBarry Smith PetscInt ksp_its,snes_its; 23190b039ecaSBarry Smith }; 23200b039ecaSBarry Smith 2321d763cef2SBarry Smith 23224a2ae208SSatish Balay #undef __FUNCT__ 2323a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 2324d763cef2SBarry Smith /*@C 2325a9f9c1f6SBarry Smith TSMonitorLGCtxCreate - Creates a line graph context for use with 2326a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 2327d763cef2SBarry Smith 2328d763cef2SBarry Smith Collective on TS 2329d763cef2SBarry Smith 2330d763cef2SBarry Smith Input Parameters: 2331d763cef2SBarry Smith + host - the X display to open, or null for the local machine 2332d763cef2SBarry Smith . label - the title to put in the title bar 23337c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 2334a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 2335a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 2336d763cef2SBarry Smith 2337d763cef2SBarry Smith Output Parameter: 23380b039ecaSBarry Smith . ctx - the context 2339d763cef2SBarry Smith 2340d763cef2SBarry Smith Options Database Key: 23414f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 23424f09c107SBarry Smith . -ts_monitor_lg_solution - 234399fdda47SBarry Smith . -ts_monitor_lg_error - 234499fdda47SBarry Smith . -ts_monitor_lg_ksp_iterations - 234599fdda47SBarry Smith . -ts_monitor_lg_snes_iterations - 234699fdda47SBarry Smith - -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true 2347d763cef2SBarry Smith 2348d763cef2SBarry Smith Notes: 2349a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 2350d763cef2SBarry Smith 2351d763cef2SBarry Smith Level: intermediate 2352d763cef2SBarry Smith 23537c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 2354d763cef2SBarry Smith 23554f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 23567c922b88SBarry Smith 2357d763cef2SBarry Smith @*/ 2358a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 2359d763cef2SBarry Smith { 2360b0a32e0cSBarry Smith PetscDraw win; 2361dfbe8321SBarry Smith PetscErrorCode ierr; 236299fdda47SBarry Smith PetscBool flg = PETSC_TRUE; 2363d763cef2SBarry Smith 2364d763cef2SBarry Smith PetscFunctionBegin; 2365201da799SBarry Smith ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr); 2366a80ad3e0SBarry Smith ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr); 23673fbbecb0SBarry Smith ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr); 23680b039ecaSBarry Smith ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr); 236999fdda47SBarry Smith ierr = PetscOptionsGetBool(PETSC_NULL,"-lg_indicate_data_points",&flg,PETSC_NULL);CHKERRQ(ierr); 237099fdda47SBarry Smith if (flg) { 23710b039ecaSBarry Smith ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr); 237299fdda47SBarry Smith } 23730b039ecaSBarry Smith ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr); 2374a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 2375d763cef2SBarry Smith PetscFunctionReturn(0); 2376d763cef2SBarry Smith } 2377d763cef2SBarry Smith 23784a2ae208SSatish Balay #undef __FUNCT__ 23794f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 23804f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 2381d763cef2SBarry Smith { 23820b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 2383c32365f1SBarry Smith PetscReal x = ptime,y; 2384dfbe8321SBarry Smith PetscErrorCode ierr; 2385d763cef2SBarry Smith 2386d763cef2SBarry Smith PetscFunctionBegin; 2387a9f9c1f6SBarry Smith if (!n) { 2388a9f9c1f6SBarry Smith PetscDrawAxis axis; 2389a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 2390a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 2391a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 2392a9f9c1f6SBarry Smith } 2393c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 23940b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 239599fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))){ 23960b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 23973923b477SBarry Smith } 2398d763cef2SBarry Smith PetscFunctionReturn(0); 2399d763cef2SBarry Smith } 2400d763cef2SBarry Smith 24014a2ae208SSatish Balay #undef __FUNCT__ 2402a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 2403d763cef2SBarry Smith /*@C 2404a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 2405a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 2406d763cef2SBarry Smith 24070b039ecaSBarry Smith Collective on TSMonitorLGCtx 2408d763cef2SBarry Smith 2409d763cef2SBarry Smith Input Parameter: 24100b039ecaSBarry Smith . ctx - the monitor context 2411d763cef2SBarry Smith 2412d763cef2SBarry Smith Level: intermediate 2413d763cef2SBarry Smith 2414d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 2415d763cef2SBarry Smith 24164f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 2417d763cef2SBarry Smith @*/ 2418a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 2419d763cef2SBarry Smith { 2420b0a32e0cSBarry Smith PetscDraw draw; 2421dfbe8321SBarry Smith PetscErrorCode ierr; 2422d763cef2SBarry Smith 2423d763cef2SBarry Smith PetscFunctionBegin; 24240b039ecaSBarry Smith ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr); 24256bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 24260b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 24270b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 2428d763cef2SBarry Smith PetscFunctionReturn(0); 2429d763cef2SBarry Smith } 2430d763cef2SBarry Smith 24314a2ae208SSatish Balay #undef __FUNCT__ 24324a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 2433d763cef2SBarry Smith /*@ 2434b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 2435d763cef2SBarry Smith 2436d763cef2SBarry Smith Not Collective 2437d763cef2SBarry Smith 2438d763cef2SBarry Smith Input Parameter: 2439d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2440d763cef2SBarry Smith 2441d763cef2SBarry Smith Output Parameter: 2442d763cef2SBarry Smith . t - the current time 2443d763cef2SBarry Smith 2444d763cef2SBarry Smith Level: beginner 2445d763cef2SBarry Smith 2446b8123daeSJed Brown Note: 2447b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 2448b8123daeSJed Brown TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 2449b8123daeSJed Brown 2450d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2451d763cef2SBarry Smith 2452d763cef2SBarry Smith .keywords: TS, get, time 2453d763cef2SBarry Smith @*/ 24547087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal* t) 2455d763cef2SBarry Smith { 2456d763cef2SBarry Smith PetscFunctionBegin; 24570700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2458f7cf8827SBarry Smith PetscValidRealPointer(t,2); 2459d763cef2SBarry Smith *t = ts->ptime; 2460d763cef2SBarry Smith PetscFunctionReturn(0); 2461d763cef2SBarry Smith } 2462d763cef2SBarry Smith 24634a2ae208SSatish Balay #undef __FUNCT__ 24646a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 24656a4d4014SLisandro Dalcin /*@ 24666a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 24676a4d4014SLisandro Dalcin 24683f9fe445SBarry Smith Logically Collective on TS 24696a4d4014SLisandro Dalcin 24706a4d4014SLisandro Dalcin Input Parameters: 24716a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 24726a4d4014SLisandro Dalcin - time - the time 24736a4d4014SLisandro Dalcin 24746a4d4014SLisandro Dalcin Level: intermediate 24756a4d4014SLisandro Dalcin 24766a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 24776a4d4014SLisandro Dalcin 24786a4d4014SLisandro Dalcin .keywords: TS, set, time 24796a4d4014SLisandro Dalcin @*/ 24807087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 24816a4d4014SLisandro Dalcin { 24826a4d4014SLisandro Dalcin PetscFunctionBegin; 24830700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2484c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 24856a4d4014SLisandro Dalcin ts->ptime = t; 24866a4d4014SLisandro Dalcin PetscFunctionReturn(0); 24876a4d4014SLisandro Dalcin } 24886a4d4014SLisandro Dalcin 24896a4d4014SLisandro Dalcin #undef __FUNCT__ 24904a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 2491d763cef2SBarry Smith /*@C 2492d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 2493d763cef2SBarry Smith TS options in the database. 2494d763cef2SBarry Smith 24953f9fe445SBarry Smith Logically Collective on TS 2496d763cef2SBarry Smith 2497d763cef2SBarry Smith Input Parameter: 2498d763cef2SBarry Smith + ts - The TS context 2499d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2500d763cef2SBarry Smith 2501d763cef2SBarry Smith Notes: 2502d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2503d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2504d763cef2SBarry Smith hyphen. 2505d763cef2SBarry Smith 2506d763cef2SBarry Smith Level: advanced 2507d763cef2SBarry Smith 2508d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 2509d763cef2SBarry Smith 2510d763cef2SBarry Smith .seealso: TSSetFromOptions() 2511d763cef2SBarry Smith 2512d763cef2SBarry Smith @*/ 25137087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 2514d763cef2SBarry Smith { 2515dfbe8321SBarry Smith PetscErrorCode ierr; 2516089b2837SJed Brown SNES snes; 2517d763cef2SBarry Smith 2518d763cef2SBarry Smith PetscFunctionBegin; 25190700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2520d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2521089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2522089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2523d763cef2SBarry Smith PetscFunctionReturn(0); 2524d763cef2SBarry Smith } 2525d763cef2SBarry Smith 2526d763cef2SBarry Smith 25274a2ae208SSatish Balay #undef __FUNCT__ 25284a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 2529d763cef2SBarry Smith /*@C 2530d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 2531d763cef2SBarry Smith TS options in the database. 2532d763cef2SBarry Smith 25333f9fe445SBarry Smith Logically Collective on TS 2534d763cef2SBarry Smith 2535d763cef2SBarry Smith Input Parameter: 2536d763cef2SBarry Smith + ts - The TS context 2537d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2538d763cef2SBarry Smith 2539d763cef2SBarry Smith Notes: 2540d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2541d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2542d763cef2SBarry Smith hyphen. 2543d763cef2SBarry Smith 2544d763cef2SBarry Smith Level: advanced 2545d763cef2SBarry Smith 2546d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 2547d763cef2SBarry Smith 2548d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 2549d763cef2SBarry Smith 2550d763cef2SBarry Smith @*/ 25517087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 2552d763cef2SBarry Smith { 2553dfbe8321SBarry Smith PetscErrorCode ierr; 2554089b2837SJed Brown SNES snes; 2555d763cef2SBarry Smith 2556d763cef2SBarry Smith PetscFunctionBegin; 25570700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2558d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2559089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2560089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2561d763cef2SBarry Smith PetscFunctionReturn(0); 2562d763cef2SBarry Smith } 2563d763cef2SBarry Smith 25644a2ae208SSatish Balay #undef __FUNCT__ 25654a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2566d763cef2SBarry Smith /*@C 2567d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2568d763cef2SBarry Smith TS options in the database. 2569d763cef2SBarry Smith 2570d763cef2SBarry Smith Not Collective 2571d763cef2SBarry Smith 2572d763cef2SBarry Smith Input Parameter: 2573d763cef2SBarry Smith . ts - The TS context 2574d763cef2SBarry Smith 2575d763cef2SBarry Smith Output Parameter: 2576d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2577d763cef2SBarry Smith 2578d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2579d763cef2SBarry Smith sufficient length to hold the prefix. 2580d763cef2SBarry Smith 2581d763cef2SBarry Smith Level: intermediate 2582d763cef2SBarry Smith 2583d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2584d763cef2SBarry Smith 2585d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2586d763cef2SBarry Smith @*/ 25877087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2588d763cef2SBarry Smith { 2589dfbe8321SBarry Smith PetscErrorCode ierr; 2590d763cef2SBarry Smith 2591d763cef2SBarry Smith PetscFunctionBegin; 25920700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25934482741eSBarry Smith PetscValidPointer(prefix,2); 2594d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2595d763cef2SBarry Smith PetscFunctionReturn(0); 2596d763cef2SBarry Smith } 2597d763cef2SBarry Smith 25984a2ae208SSatish Balay #undef __FUNCT__ 25994a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2600d763cef2SBarry Smith /*@C 2601d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2602d763cef2SBarry Smith 2603d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2604d763cef2SBarry Smith 2605d763cef2SBarry Smith Input Parameter: 2606d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2607d763cef2SBarry Smith 2608d763cef2SBarry Smith Output Parameters: 2609d763cef2SBarry Smith + J - The Jacobian J of F, where U_t = F(U,t) 2610d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2611089b2837SJed Brown . func - Function to compute the Jacobian of the RHS 2612d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2613d763cef2SBarry Smith 2614d763cef2SBarry Smith Notes: You can pass in PETSC_NULL for any return argument you do not need. 2615d763cef2SBarry Smith 2616d763cef2SBarry Smith Level: intermediate 2617d763cef2SBarry Smith 261826d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2619d763cef2SBarry Smith 2620d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2621d763cef2SBarry Smith @*/ 2622089b2837SJed Brown PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2623d763cef2SBarry Smith { 2624089b2837SJed Brown PetscErrorCode ierr; 2625089b2837SJed Brown SNES snes; 262624989b8cSPeter Brune DM dm; 2627089b2837SJed Brown 2628d763cef2SBarry Smith PetscFunctionBegin; 2629089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2630089b2837SJed Brown ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 263124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 263224989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 2633d763cef2SBarry Smith PetscFunctionReturn(0); 2634d763cef2SBarry Smith } 2635d763cef2SBarry Smith 26361713a123SBarry Smith #undef __FUNCT__ 26372eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 26382eca1d9cSJed Brown /*@C 26392eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 26402eca1d9cSJed Brown 26412eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 26422eca1d9cSJed Brown 26432eca1d9cSJed Brown Input Parameter: 26442eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 26452eca1d9cSJed Brown 26462eca1d9cSJed Brown Output Parameters: 26472eca1d9cSJed Brown + A - The Jacobian of F(t,U,U_t) 26482eca1d9cSJed Brown . B - The preconditioner matrix, often the same as A 26492eca1d9cSJed Brown . f - The function to compute the matrices 26502eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 26512eca1d9cSJed Brown 26522eca1d9cSJed Brown Notes: You can pass in PETSC_NULL for any return argument you do not need. 26532eca1d9cSJed Brown 26542eca1d9cSJed Brown Level: advanced 26552eca1d9cSJed Brown 26562eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 26572eca1d9cSJed Brown 26582eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 26592eca1d9cSJed Brown @*/ 26607087cfbeSBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 26612eca1d9cSJed Brown { 2662089b2837SJed Brown PetscErrorCode ierr; 2663089b2837SJed Brown SNES snes; 266424989b8cSPeter Brune DM dm; 26652eca1d9cSJed Brown PetscFunctionBegin; 2666089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2667089b2837SJed Brown ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 266824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 266924989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 26702eca1d9cSJed Brown PetscFunctionReturn(0); 26712eca1d9cSJed Brown } 26722eca1d9cSJed Brown 267383a4ac43SBarry Smith struct _n_TSMonitorDrawCtx { 26746083293cSBarry Smith PetscViewer viewer; 26756083293cSBarry Smith Vec initialsolution; 26766083293cSBarry Smith PetscBool showinitial; 267783a4ac43SBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 267883a4ac43SBarry Smith }; 26796083293cSBarry Smith 26802eca1d9cSJed Brown #undef __FUNCT__ 268183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 26821713a123SBarry Smith /*@C 268383a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 26841713a123SBarry Smith VecView() for the solution at each timestep 26851713a123SBarry Smith 26861713a123SBarry Smith Collective on TS 26871713a123SBarry Smith 26881713a123SBarry Smith Input Parameters: 26891713a123SBarry Smith + ts - the TS context 26901713a123SBarry Smith . step - current time-step 2691142b95e3SSatish Balay . ptime - current time 26921713a123SBarry Smith - dummy - either a viewer or PETSC_NULL 26931713a123SBarry Smith 269499fdda47SBarry Smith Options Database: 269599fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 269699fdda47SBarry Smith 269799fdda47SBarry 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 269899fdda47SBarry Smith will look bad 269999fdda47SBarry Smith 27001713a123SBarry Smith Level: intermediate 27011713a123SBarry Smith 27021713a123SBarry Smith .keywords: TS, vector, monitor, view 27031713a123SBarry Smith 2704a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 27051713a123SBarry Smith @*/ 270683a4ac43SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 27071713a123SBarry Smith { 2708dfbe8321SBarry Smith PetscErrorCode ierr; 270983a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 27101713a123SBarry Smith 27111713a123SBarry Smith PetscFunctionBegin; 27126083293cSBarry Smith if (!step && ictx->showinitial) { 27136083293cSBarry Smith if (!ictx->initialsolution) { 27146083293cSBarry Smith ierr = VecDuplicate(x,&ictx->initialsolution);CHKERRQ(ierr); 27151713a123SBarry Smith } 27166083293cSBarry Smith ierr = VecCopy(x,ictx->initialsolution);CHKERRQ(ierr); 27176083293cSBarry Smith } 27183fbbecb0SBarry Smith if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0); 27190dcf80beSBarry Smith 27206083293cSBarry Smith if (ictx->showinitial) { 27216083293cSBarry Smith PetscReal pause; 27226083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 27236083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 27246083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 27256083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 27266083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 27276083293cSBarry Smith } 27286083293cSBarry Smith ierr = VecView(x,ictx->viewer);CHKERRQ(ierr); 27296083293cSBarry Smith if (ictx->showinitial) { 27306083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 27316083293cSBarry Smith } 27321713a123SBarry Smith PetscFunctionReturn(0); 27331713a123SBarry Smith } 27341713a123SBarry Smith 27351713a123SBarry Smith 27366c699258SBarry Smith #undef __FUNCT__ 273783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 27386083293cSBarry Smith /*@C 273983a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 27406083293cSBarry Smith 27416083293cSBarry Smith Collective on TS 27426083293cSBarry Smith 27436083293cSBarry Smith Input Parameters: 27446083293cSBarry Smith . ctx - the monitor context 27456083293cSBarry Smith 27466083293cSBarry Smith Level: intermediate 27476083293cSBarry Smith 27486083293cSBarry Smith .keywords: TS, vector, monitor, view 27496083293cSBarry Smith 275083a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 27516083293cSBarry Smith @*/ 275283a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 27536083293cSBarry Smith { 27546083293cSBarry Smith PetscErrorCode ierr; 27556083293cSBarry Smith 27566083293cSBarry Smith PetscFunctionBegin; 275783a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 275883a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 275983a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 27606083293cSBarry Smith PetscFunctionReturn(0); 27616083293cSBarry Smith } 27626083293cSBarry Smith 27636083293cSBarry Smith #undef __FUNCT__ 276483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 27656083293cSBarry Smith /*@C 276683a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 27676083293cSBarry Smith 27686083293cSBarry Smith Collective on TS 27696083293cSBarry Smith 27706083293cSBarry Smith Input Parameter: 27716083293cSBarry Smith . ts - time-step context 27726083293cSBarry Smith 27736083293cSBarry Smith Output Patameter: 27746083293cSBarry Smith . ctx - the monitor context 27756083293cSBarry Smith 277699fdda47SBarry Smith Options Database: 277799fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 277899fdda47SBarry Smith 27796083293cSBarry Smith Level: intermediate 27806083293cSBarry Smith 27816083293cSBarry Smith .keywords: TS, vector, monitor, view 27826083293cSBarry Smith 278383a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 27846083293cSBarry Smith @*/ 278583a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 27866083293cSBarry Smith { 27876083293cSBarry Smith PetscErrorCode ierr; 27886083293cSBarry Smith 27896083293cSBarry Smith PetscFunctionBegin; 279083a4ac43SBarry Smith ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr); 279183a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 279283a4ac43SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 279383a4ac43SBarry Smith (*ctx)->howoften = howoften; 279499fdda47SBarry Smith ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,PETSC_NULL);CHKERRQ(ierr); 27956083293cSBarry Smith PetscFunctionReturn(0); 27966083293cSBarry Smith } 27976083293cSBarry Smith 27986083293cSBarry Smith #undef __FUNCT__ 279983a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 28003a471f94SBarry Smith /*@C 280183a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 28023a471f94SBarry Smith VecView() for the error at each timestep 28033a471f94SBarry Smith 28043a471f94SBarry Smith Collective on TS 28053a471f94SBarry Smith 28063a471f94SBarry Smith Input Parameters: 28073a471f94SBarry Smith + ts - the TS context 28083a471f94SBarry Smith . step - current time-step 28093a471f94SBarry Smith . ptime - current time 28103a471f94SBarry Smith - dummy - either a viewer or PETSC_NULL 28113a471f94SBarry Smith 28123a471f94SBarry Smith Level: intermediate 28133a471f94SBarry Smith 28143a471f94SBarry Smith .keywords: TS, vector, monitor, view 28153a471f94SBarry Smith 28163a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 28173a471f94SBarry Smith @*/ 281883a4ac43SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 28193a471f94SBarry Smith { 28203a471f94SBarry Smith PetscErrorCode ierr; 282183a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 282283a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 28233a471f94SBarry Smith Vec work; 28243a471f94SBarry Smith 28253a471f94SBarry Smith PetscFunctionBegin; 28263fbbecb0SBarry Smith if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0); 28273a471f94SBarry Smith ierr = VecDuplicate(x,&work);CHKERRQ(ierr); 28283a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 28293a471f94SBarry Smith ierr = VecAXPY(work,-1.0,x);CHKERRQ(ierr); 28303a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 28313a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 28323a471f94SBarry Smith PetscFunctionReturn(0); 28333a471f94SBarry Smith } 28343a471f94SBarry Smith 28353a471f94SBarry Smith #undef __FUNCT__ 28366c699258SBarry Smith #define __FUNCT__ "TSSetDM" 28376c699258SBarry Smith /*@ 28386c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 28396c699258SBarry Smith 28403f9fe445SBarry Smith Logically Collective on TS and DM 28416c699258SBarry Smith 28426c699258SBarry Smith Input Parameters: 28436c699258SBarry Smith + ts - the preconditioner context 28446c699258SBarry Smith - dm - the dm 28456c699258SBarry Smith 28466c699258SBarry Smith Level: intermediate 28476c699258SBarry Smith 28486c699258SBarry Smith 28496c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 28506c699258SBarry Smith @*/ 28517087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 28526c699258SBarry Smith { 28536c699258SBarry Smith PetscErrorCode ierr; 2854089b2837SJed Brown SNES snes; 285524989b8cSPeter Brune TSDM tsdm; 28566c699258SBarry Smith 28576c699258SBarry Smith PetscFunctionBegin; 28580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 285970663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 286024989b8cSPeter Brune if (ts->dm) { /* Move the TSDM context over to the new DM unless the new DM already has one */ 286124989b8cSPeter Brune PetscContainer oldcontainer,container; 286224989b8cSPeter Brune ierr = PetscObjectQuery((PetscObject)ts->dm,"TSDM",(PetscObject*)&oldcontainer);CHKERRQ(ierr); 286324989b8cSPeter Brune ierr = PetscObjectQuery((PetscObject)dm,"TSDM",(PetscObject*)&container);CHKERRQ(ierr); 286424989b8cSPeter Brune if (oldcontainer && !container) { 286524989b8cSPeter Brune ierr = DMTSCopyContext(ts->dm,dm);CHKERRQ(ierr); 286624989b8cSPeter Brune ierr = DMTSGetContext(ts->dm,&tsdm);CHKERRQ(ierr); 286724989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 286824989b8cSPeter Brune tsdm->originaldm = dm; 286924989b8cSPeter Brune } 287024989b8cSPeter Brune } 28716bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 287224989b8cSPeter Brune } 28736c699258SBarry Smith ts->dm = dm; 2874089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2875089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 28766c699258SBarry Smith PetscFunctionReturn(0); 28776c699258SBarry Smith } 28786c699258SBarry Smith 28796c699258SBarry Smith #undef __FUNCT__ 28806c699258SBarry Smith #define __FUNCT__ "TSGetDM" 28816c699258SBarry Smith /*@ 28826c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 28836c699258SBarry Smith 28843f9fe445SBarry Smith Not Collective 28856c699258SBarry Smith 28866c699258SBarry Smith Input Parameter: 28876c699258SBarry Smith . ts - the preconditioner context 28886c699258SBarry Smith 28896c699258SBarry Smith Output Parameter: 28906c699258SBarry Smith . dm - the dm 28916c699258SBarry Smith 28926c699258SBarry Smith Level: intermediate 28936c699258SBarry Smith 28946c699258SBarry Smith 28956c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 28966c699258SBarry Smith @*/ 28977087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 28986c699258SBarry Smith { 2899496e6a7aSJed Brown PetscErrorCode ierr; 2900496e6a7aSJed Brown 29016c699258SBarry Smith PetscFunctionBegin; 29020700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2903496e6a7aSJed Brown if (!ts->dm) { 2904496e6a7aSJed Brown ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr); 2905496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 2906496e6a7aSJed Brown } 29076c699258SBarry Smith *dm = ts->dm; 29086c699258SBarry Smith PetscFunctionReturn(0); 29096c699258SBarry Smith } 29101713a123SBarry Smith 29110f5c6efeSJed Brown #undef __FUNCT__ 29120f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 29130f5c6efeSJed Brown /*@ 29140f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 29150f5c6efeSJed Brown 29163f9fe445SBarry Smith Logically Collective on SNES 29170f5c6efeSJed Brown 29180f5c6efeSJed Brown Input Parameter: 2919d42a1c89SJed Brown + snes - nonlinear solver 29200f5c6efeSJed Brown . X - the current state at which to evaluate the residual 2921d42a1c89SJed Brown - ctx - user context, must be a TS 29220f5c6efeSJed Brown 29230f5c6efeSJed Brown Output Parameter: 29240f5c6efeSJed Brown . F - the nonlinear residual 29250f5c6efeSJed Brown 29260f5c6efeSJed Brown Notes: 29270f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 29280f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 29290f5c6efeSJed Brown 29300f5c6efeSJed Brown Level: advanced 29310f5c6efeSJed Brown 29320f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 29330f5c6efeSJed Brown @*/ 29347087cfbeSBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx) 29350f5c6efeSJed Brown { 29360f5c6efeSJed Brown TS ts = (TS)ctx; 29370f5c6efeSJed Brown PetscErrorCode ierr; 29380f5c6efeSJed Brown 29390f5c6efeSJed Brown PetscFunctionBegin; 29400f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 29410f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 29420f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 29430f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 29440f5c6efeSJed Brown ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr); 29450f5c6efeSJed Brown PetscFunctionReturn(0); 29460f5c6efeSJed Brown } 29470f5c6efeSJed Brown 29480f5c6efeSJed Brown #undef __FUNCT__ 29490f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 29500f5c6efeSJed Brown /*@ 29510f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 29520f5c6efeSJed Brown 29530f5c6efeSJed Brown Collective on SNES 29540f5c6efeSJed Brown 29550f5c6efeSJed Brown Input Parameter: 29560f5c6efeSJed Brown + snes - nonlinear solver 29570f5c6efeSJed Brown . X - the current state at which to evaluate the residual 29580f5c6efeSJed Brown - ctx - user context, must be a TS 29590f5c6efeSJed Brown 29600f5c6efeSJed Brown Output Parameter: 29610f5c6efeSJed Brown + A - the Jacobian 29620f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 29630f5c6efeSJed Brown - flag - indicates any structure change in the matrix 29640f5c6efeSJed Brown 29650f5c6efeSJed Brown Notes: 29660f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 29670f5c6efeSJed Brown 29680f5c6efeSJed Brown Level: developer 29690f5c6efeSJed Brown 29700f5c6efeSJed Brown .seealso: SNESSetJacobian() 29710f5c6efeSJed Brown @*/ 29727087cfbeSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx) 29730f5c6efeSJed Brown { 29740f5c6efeSJed Brown TS ts = (TS)ctx; 29750f5c6efeSJed Brown PetscErrorCode ierr; 29760f5c6efeSJed Brown 29770f5c6efeSJed Brown PetscFunctionBegin; 29780f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 29790f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 29800f5c6efeSJed Brown PetscValidPointer(A,3); 29810f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 29820f5c6efeSJed Brown PetscValidPointer(B,4); 29830f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 29840f5c6efeSJed Brown PetscValidPointer(flag,5); 29850f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 29860f5c6efeSJed Brown ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr); 29870f5c6efeSJed Brown PetscFunctionReturn(0); 29880f5c6efeSJed Brown } 2989325fc9f4SBarry Smith 29900e4ef248SJed Brown #undef __FUNCT__ 29910e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 29920e4ef248SJed Brown /*@C 29930e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 29940e4ef248SJed Brown 29950e4ef248SJed Brown Collective on TS 29960e4ef248SJed Brown 29970e4ef248SJed Brown Input Arguments: 29980e4ef248SJed Brown + ts - time stepping context 29990e4ef248SJed Brown . t - time at which to evaluate 30000e4ef248SJed Brown . X - state at which to evaluate 30010e4ef248SJed Brown - ctx - context 30020e4ef248SJed Brown 30030e4ef248SJed Brown Output Arguments: 30040e4ef248SJed Brown . F - right hand side 30050e4ef248SJed Brown 30060e4ef248SJed Brown Level: intermediate 30070e4ef248SJed Brown 30080e4ef248SJed Brown Notes: 30090e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 30100e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 30110e4ef248SJed Brown 30120e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 30130e4ef248SJed Brown @*/ 30140e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx) 30150e4ef248SJed Brown { 30160e4ef248SJed Brown PetscErrorCode ierr; 30170e4ef248SJed Brown Mat Arhs,Brhs; 30180e4ef248SJed Brown MatStructure flg2; 30190e4ef248SJed Brown 30200e4ef248SJed Brown PetscFunctionBegin; 30210e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 30220e4ef248SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 30230e4ef248SJed Brown ierr = MatMult(Arhs,X,F);CHKERRQ(ierr); 30240e4ef248SJed Brown PetscFunctionReturn(0); 30250e4ef248SJed Brown } 30260e4ef248SJed Brown 30270e4ef248SJed Brown #undef __FUNCT__ 30280e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 30290e4ef248SJed Brown /*@C 30300e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 30310e4ef248SJed Brown 30320e4ef248SJed Brown Collective on TS 30330e4ef248SJed Brown 30340e4ef248SJed Brown Input Arguments: 30350e4ef248SJed Brown + ts - time stepping context 30360e4ef248SJed Brown . t - time at which to evaluate 30370e4ef248SJed Brown . X - state at which to evaluate 30380e4ef248SJed Brown - ctx - context 30390e4ef248SJed Brown 30400e4ef248SJed Brown Output Arguments: 30410e4ef248SJed Brown + A - pointer to operator 30420e4ef248SJed Brown . B - pointer to preconditioning matrix 30430e4ef248SJed Brown - flg - matrix structure flag 30440e4ef248SJed Brown 30450e4ef248SJed Brown Level: intermediate 30460e4ef248SJed Brown 30470e4ef248SJed Brown Notes: 30480e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 30490e4ef248SJed Brown 30500e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 30510e4ef248SJed Brown @*/ 30520e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx) 30530e4ef248SJed Brown { 30540e4ef248SJed Brown 30550e4ef248SJed Brown PetscFunctionBegin; 30560e4ef248SJed Brown *flg = SAME_PRECONDITIONER; 30570e4ef248SJed Brown PetscFunctionReturn(0); 30580e4ef248SJed Brown } 30590e4ef248SJed Brown 30600026cea9SSean Farley #undef __FUNCT__ 30610026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 30620026cea9SSean Farley /*@C 30630026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 30640026cea9SSean Farley 30650026cea9SSean Farley Collective on TS 30660026cea9SSean Farley 30670026cea9SSean Farley Input Arguments: 30680026cea9SSean Farley + ts - time stepping context 30690026cea9SSean Farley . t - time at which to evaluate 30700026cea9SSean Farley . X - state at which to evaluate 30710026cea9SSean Farley . Xdot - time derivative of state vector 30720026cea9SSean Farley - ctx - context 30730026cea9SSean Farley 30740026cea9SSean Farley Output Arguments: 30750026cea9SSean Farley . F - left hand side 30760026cea9SSean Farley 30770026cea9SSean Farley Level: intermediate 30780026cea9SSean Farley 30790026cea9SSean Farley Notes: 30800026cea9SSean Farley The assumption here is that the left hand side is of the form A*Xdot (and not A*Xdot + B*X). For other cases, the 30810026cea9SSean Farley user is required to write their own TSComputeIFunction. 30820026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 30830026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 30840026cea9SSean Farley 30850026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 30860026cea9SSean Farley @*/ 30870026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx) 30880026cea9SSean Farley { 30890026cea9SSean Farley PetscErrorCode ierr; 30900026cea9SSean Farley Mat A,B; 30910026cea9SSean Farley MatStructure flg2; 30920026cea9SSean Farley 30930026cea9SSean Farley PetscFunctionBegin; 30940026cea9SSean Farley ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 30950026cea9SSean Farley ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr); 30960026cea9SSean Farley ierr = MatMult(A,Xdot,F);CHKERRQ(ierr); 30970026cea9SSean Farley PetscFunctionReturn(0); 30980026cea9SSean Farley } 30990026cea9SSean Farley 31000026cea9SSean Farley #undef __FUNCT__ 31010026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 31020026cea9SSean Farley /*@C 310324e94211SJed Brown TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent. 31040026cea9SSean Farley 31050026cea9SSean Farley Collective on TS 31060026cea9SSean Farley 31070026cea9SSean Farley Input Arguments: 31080026cea9SSean Farley + ts - time stepping context 31090026cea9SSean Farley . t - time at which to evaluate 31100026cea9SSean Farley . X - state at which to evaluate 31110026cea9SSean Farley . Xdot - time derivative of state vector 31120026cea9SSean Farley . shift - shift to apply 31130026cea9SSean Farley - ctx - context 31140026cea9SSean Farley 31150026cea9SSean Farley Output Arguments: 31160026cea9SSean Farley + A - pointer to operator 31170026cea9SSean Farley . B - pointer to preconditioning matrix 31180026cea9SSean Farley - flg - matrix structure flag 31190026cea9SSean Farley 31200026cea9SSean Farley Level: intermediate 31210026cea9SSean Farley 31220026cea9SSean Farley Notes: 31230026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 31240026cea9SSean Farley 31250026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 31260026cea9SSean Farley @*/ 31270026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx) 31280026cea9SSean Farley { 31290026cea9SSean Farley 31300026cea9SSean Farley PetscFunctionBegin; 31310026cea9SSean Farley *flg = SAME_PRECONDITIONER; 31320026cea9SSean Farley PetscFunctionReturn(0); 31330026cea9SSean Farley } 31340026cea9SSean Farley 31354af1b03aSJed Brown 31364af1b03aSJed Brown #undef __FUNCT__ 31374af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 31384af1b03aSJed Brown /*@ 31394af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 31404af1b03aSJed Brown 31414af1b03aSJed Brown Not Collective 31424af1b03aSJed Brown 31434af1b03aSJed Brown Input Parameter: 31444af1b03aSJed Brown . ts - the TS context 31454af1b03aSJed Brown 31464af1b03aSJed Brown Output Parameter: 31474af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 31484af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 31494af1b03aSJed Brown 31504af1b03aSJed Brown Level: intermediate 31514af1b03aSJed Brown 3152cd652676SJed Brown Notes: 3153cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 31544af1b03aSJed Brown 31554af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 31564af1b03aSJed Brown 31574af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 31584af1b03aSJed Brown @*/ 31594af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 31604af1b03aSJed Brown { 31614af1b03aSJed Brown PetscFunctionBegin; 31624af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31634af1b03aSJed Brown PetscValidPointer(reason,2); 31644af1b03aSJed Brown *reason = ts->reason; 31654af1b03aSJed Brown PetscFunctionReturn(0); 31664af1b03aSJed Brown } 31674af1b03aSJed Brown 3168fb1732b5SBarry Smith #undef __FUNCT__ 31695ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 31709f67acb7SJed Brown /*@ 31715ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 31729f67acb7SJed Brown used by the time integrator. 31739f67acb7SJed Brown 31749f67acb7SJed Brown Not Collective 31759f67acb7SJed Brown 31769f67acb7SJed Brown Input Parameter: 31779f67acb7SJed Brown . ts - TS context 31789f67acb7SJed Brown 31799f67acb7SJed Brown Output Parameter: 31809f67acb7SJed Brown . nits - number of nonlinear iterations 31819f67acb7SJed Brown 31829f67acb7SJed Brown Notes: 31839f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 31849f67acb7SJed Brown 31859f67acb7SJed Brown Level: intermediate 31869f67acb7SJed Brown 31879f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 31889f67acb7SJed Brown 31895ef26d82SJed Brown .seealso: TSGetKSPIterations() 31909f67acb7SJed Brown @*/ 31915ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 31929f67acb7SJed Brown { 31939f67acb7SJed Brown PetscFunctionBegin; 31949f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31959f67acb7SJed Brown PetscValidIntPointer(nits,2); 31965ef26d82SJed Brown *nits = ts->snes_its; 31979f67acb7SJed Brown PetscFunctionReturn(0); 31989f67acb7SJed Brown } 31999f67acb7SJed Brown 32009f67acb7SJed Brown #undef __FUNCT__ 32015ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 32029f67acb7SJed Brown /*@ 32035ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 32049f67acb7SJed Brown used by the time integrator. 32059f67acb7SJed Brown 32069f67acb7SJed Brown Not Collective 32079f67acb7SJed Brown 32089f67acb7SJed Brown Input Parameter: 32099f67acb7SJed Brown . ts - TS context 32109f67acb7SJed Brown 32119f67acb7SJed Brown Output Parameter: 32129f67acb7SJed Brown . lits - number of linear iterations 32139f67acb7SJed Brown 32149f67acb7SJed Brown Notes: 32159f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 32169f67acb7SJed Brown 32179f67acb7SJed Brown Level: intermediate 32189f67acb7SJed Brown 32199f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 32209f67acb7SJed Brown 32215ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 32229f67acb7SJed Brown @*/ 32235ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 32249f67acb7SJed Brown { 32259f67acb7SJed Brown PetscFunctionBegin; 32269f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 32279f67acb7SJed Brown PetscValidIntPointer(lits,2); 32285ef26d82SJed Brown *lits = ts->ksp_its; 32299f67acb7SJed Brown PetscFunctionReturn(0); 32309f67acb7SJed Brown } 32319f67acb7SJed Brown 32329f67acb7SJed Brown #undef __FUNCT__ 3233cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 3234cef5090cSJed Brown /*@ 3235cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 3236cef5090cSJed Brown 3237cef5090cSJed Brown Not Collective 3238cef5090cSJed Brown 3239cef5090cSJed Brown Input Parameter: 3240cef5090cSJed Brown . ts - TS context 3241cef5090cSJed Brown 3242cef5090cSJed Brown Output Parameter: 3243cef5090cSJed Brown . rejects - number of steps rejected 3244cef5090cSJed Brown 3245cef5090cSJed Brown Notes: 3246cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 3247cef5090cSJed Brown 3248cef5090cSJed Brown Level: intermediate 3249cef5090cSJed Brown 3250cef5090cSJed Brown .keywords: TS, get, number 3251cef5090cSJed Brown 32525ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 3253cef5090cSJed Brown @*/ 3254cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 3255cef5090cSJed Brown { 3256cef5090cSJed Brown PetscFunctionBegin; 3257cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3258cef5090cSJed Brown PetscValidIntPointer(rejects,2); 3259cef5090cSJed Brown *rejects = ts->reject; 3260cef5090cSJed Brown PetscFunctionReturn(0); 3261cef5090cSJed Brown } 3262cef5090cSJed Brown 3263cef5090cSJed Brown #undef __FUNCT__ 3264cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 3265cef5090cSJed Brown /*@ 3266cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 3267cef5090cSJed Brown 3268cef5090cSJed Brown Not Collective 3269cef5090cSJed Brown 3270cef5090cSJed Brown Input Parameter: 3271cef5090cSJed Brown . ts - TS context 3272cef5090cSJed Brown 3273cef5090cSJed Brown Output Parameter: 3274cef5090cSJed Brown . fails - number of failed nonlinear solves 3275cef5090cSJed Brown 3276cef5090cSJed Brown Notes: 3277cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 3278cef5090cSJed Brown 3279cef5090cSJed Brown Level: intermediate 3280cef5090cSJed Brown 3281cef5090cSJed Brown .keywords: TS, get, number 3282cef5090cSJed Brown 32835ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 3284cef5090cSJed Brown @*/ 3285cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 3286cef5090cSJed Brown { 3287cef5090cSJed Brown PetscFunctionBegin; 3288cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3289cef5090cSJed Brown PetscValidIntPointer(fails,2); 3290cef5090cSJed Brown *fails = ts->num_snes_failures; 3291cef5090cSJed Brown PetscFunctionReturn(0); 3292cef5090cSJed Brown } 3293cef5090cSJed Brown 3294cef5090cSJed Brown #undef __FUNCT__ 3295cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 3296cef5090cSJed Brown /*@ 3297cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 3298cef5090cSJed Brown 3299cef5090cSJed Brown Not Collective 3300cef5090cSJed Brown 3301cef5090cSJed Brown Input Parameter: 3302cef5090cSJed Brown + ts - TS context 3303cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 3304cef5090cSJed Brown 3305cef5090cSJed Brown Notes: 3306cef5090cSJed Brown The counter is reset to zero for each step 3307cef5090cSJed Brown 3308cef5090cSJed Brown Options Database Key: 3309cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 3310cef5090cSJed Brown 3311cef5090cSJed Brown Level: intermediate 3312cef5090cSJed Brown 3313cef5090cSJed Brown .keywords: TS, set, maximum, number 3314cef5090cSJed Brown 33155ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3316cef5090cSJed Brown @*/ 3317cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 3318cef5090cSJed Brown { 3319cef5090cSJed Brown PetscFunctionBegin; 3320cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3321cef5090cSJed Brown ts->max_reject = rejects; 3322cef5090cSJed Brown PetscFunctionReturn(0); 3323cef5090cSJed Brown } 3324cef5090cSJed Brown 3325cef5090cSJed Brown #undef __FUNCT__ 3326cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 3327cef5090cSJed Brown /*@ 3328cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 3329cef5090cSJed Brown 3330cef5090cSJed Brown Not Collective 3331cef5090cSJed Brown 3332cef5090cSJed Brown Input Parameter: 3333cef5090cSJed Brown + ts - TS context 3334cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 3335cef5090cSJed Brown 3336cef5090cSJed Brown Notes: 3337cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 3338cef5090cSJed Brown 3339cef5090cSJed Brown Options Database Key: 3340cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 3341cef5090cSJed Brown 3342cef5090cSJed Brown Level: intermediate 3343cef5090cSJed Brown 3344cef5090cSJed Brown .keywords: TS, set, maximum, number 3345cef5090cSJed Brown 33465ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 3347cef5090cSJed Brown @*/ 3348cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 3349cef5090cSJed Brown { 3350cef5090cSJed Brown PetscFunctionBegin; 3351cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3352cef5090cSJed Brown ts->max_snes_failures = fails; 3353cef5090cSJed Brown PetscFunctionReturn(0); 3354cef5090cSJed Brown } 3355cef5090cSJed Brown 3356cef5090cSJed Brown #undef __FUNCT__ 3357cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()" 3358cef5090cSJed Brown /*@ 3359cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 3360cef5090cSJed Brown 3361cef5090cSJed Brown Not Collective 3362cef5090cSJed Brown 3363cef5090cSJed Brown Input Parameter: 3364cef5090cSJed Brown + ts - TS context 3365cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 3366cef5090cSJed Brown 3367cef5090cSJed Brown Options Database Key: 3368cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 3369cef5090cSJed Brown 3370cef5090cSJed Brown Level: intermediate 3371cef5090cSJed Brown 3372cef5090cSJed Brown .keywords: TS, set, error 3373cef5090cSJed Brown 33745ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3375cef5090cSJed Brown @*/ 3376cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 3377cef5090cSJed Brown { 3378cef5090cSJed Brown PetscFunctionBegin; 3379cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3380cef5090cSJed Brown ts->errorifstepfailed = err; 3381cef5090cSJed Brown PetscFunctionReturn(0); 3382cef5090cSJed Brown } 3383cef5090cSJed Brown 3384cef5090cSJed Brown #undef __FUNCT__ 3385fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 3386fb1732b5SBarry Smith /*@C 3387fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 3388fb1732b5SBarry Smith 3389fb1732b5SBarry Smith Collective on TS 3390fb1732b5SBarry Smith 3391fb1732b5SBarry Smith Input Parameters: 3392fb1732b5SBarry Smith + ts - the TS context 3393fb1732b5SBarry Smith . step - current time-step 3394fb1732b5SBarry Smith . ptime - current time 3395ed81e22dSJed Brown . x - current state 3396fb1732b5SBarry Smith - viewer - binary viewer 3397fb1732b5SBarry Smith 3398fb1732b5SBarry Smith Level: intermediate 3399fb1732b5SBarry Smith 3400fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 3401fb1732b5SBarry Smith 3402fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3403fb1732b5SBarry Smith @*/ 3404ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec x,void *viewer) 3405fb1732b5SBarry Smith { 3406fb1732b5SBarry Smith PetscErrorCode ierr; 3407ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 3408fb1732b5SBarry Smith 3409fb1732b5SBarry Smith PetscFunctionBegin; 3410ed81e22dSJed Brown ierr = VecView(x,v);CHKERRQ(ierr); 3411ed81e22dSJed Brown PetscFunctionReturn(0); 3412ed81e22dSJed Brown } 3413ed81e22dSJed Brown 3414ed81e22dSJed Brown #undef __FUNCT__ 3415ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 3416ed81e22dSJed Brown /*@C 3417ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 3418ed81e22dSJed Brown 3419ed81e22dSJed Brown Collective on TS 3420ed81e22dSJed Brown 3421ed81e22dSJed Brown Input Parameters: 3422ed81e22dSJed Brown + ts - the TS context 3423ed81e22dSJed Brown . step - current time-step 3424ed81e22dSJed Brown . ptime - current time 3425ed81e22dSJed Brown . x - current state 3426ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3427ed81e22dSJed Brown 3428ed81e22dSJed Brown Level: intermediate 3429ed81e22dSJed Brown 3430ed81e22dSJed Brown Notes: 3431ed81e22dSJed 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. 3432ed81e22dSJed Brown These are named according to the file name template. 3433ed81e22dSJed Brown 3434ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 3435ed81e22dSJed Brown 3436ed81e22dSJed Brown .keywords: TS, vector, monitor, view 3437ed81e22dSJed Brown 3438ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3439ed81e22dSJed Brown @*/ 3440ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec x,void *filenametemplate) 3441ed81e22dSJed Brown { 3442ed81e22dSJed Brown PetscErrorCode ierr; 3443ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 3444ed81e22dSJed Brown PetscViewer viewer; 3445ed81e22dSJed Brown 3446ed81e22dSJed Brown PetscFunctionBegin; 34478caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 3448ed81e22dSJed Brown ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 3449fb1732b5SBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 3450ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 3451ed81e22dSJed Brown PetscFunctionReturn(0); 3452ed81e22dSJed Brown } 3453ed81e22dSJed Brown 3454ed81e22dSJed Brown #undef __FUNCT__ 3455ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 3456ed81e22dSJed Brown /*@C 3457ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 3458ed81e22dSJed Brown 3459ed81e22dSJed Brown Collective on TS 3460ed81e22dSJed Brown 3461ed81e22dSJed Brown Input Parameters: 3462ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3463ed81e22dSJed Brown 3464ed81e22dSJed Brown Level: intermediate 3465ed81e22dSJed Brown 3466ed81e22dSJed Brown Note: 3467ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 3468ed81e22dSJed Brown 3469ed81e22dSJed Brown .keywords: TS, vector, monitor, view 3470ed81e22dSJed Brown 3471ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 3472ed81e22dSJed Brown @*/ 3473ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 3474ed81e22dSJed Brown { 3475ed81e22dSJed Brown PetscErrorCode ierr; 3476ed81e22dSJed Brown 3477ed81e22dSJed Brown PetscFunctionBegin; 3478ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 3479fb1732b5SBarry Smith PetscFunctionReturn(0); 3480fb1732b5SBarry Smith } 3481fb1732b5SBarry Smith 348284df9cb4SJed Brown #undef __FUNCT__ 348384df9cb4SJed Brown #define __FUNCT__ "TSGetAdapt" 348484df9cb4SJed Brown /*@ 348584df9cb4SJed Brown TSGetAdapt - Get the adaptive controller context for the current method 348684df9cb4SJed Brown 3487ed81e22dSJed Brown Collective on TS if controller has not been created yet 348884df9cb4SJed Brown 348984df9cb4SJed Brown Input Arguments: 3490ed81e22dSJed Brown . ts - time stepping context 349184df9cb4SJed Brown 349284df9cb4SJed Brown Output Arguments: 3493ed81e22dSJed Brown . adapt - adaptive controller 349484df9cb4SJed Brown 349584df9cb4SJed Brown Level: intermediate 349684df9cb4SJed Brown 3497ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 349884df9cb4SJed Brown @*/ 349984df9cb4SJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 350084df9cb4SJed Brown { 350184df9cb4SJed Brown PetscErrorCode ierr; 350284df9cb4SJed Brown 350384df9cb4SJed Brown PetscFunctionBegin; 350484df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 350584df9cb4SJed Brown PetscValidPointer(adapt,2); 350684df9cb4SJed Brown if (!ts->adapt) { 350784df9cb4SJed Brown ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr); 35081c3436cfSJed Brown ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr); 35091c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 351084df9cb4SJed Brown } 351184df9cb4SJed Brown *adapt = ts->adapt; 351284df9cb4SJed Brown PetscFunctionReturn(0); 351384df9cb4SJed Brown } 3514d6ebe24aSShri Abhyankar 3515d6ebe24aSShri Abhyankar #undef __FUNCT__ 35161c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 35171c3436cfSJed Brown /*@ 35181c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 35191c3436cfSJed Brown 35201c3436cfSJed Brown Logically Collective 35211c3436cfSJed Brown 35221c3436cfSJed Brown Input Arguments: 35231c3436cfSJed Brown + ts - time integration context 35241c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 35251c3436cfSJed Brown . vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present 35261c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 35271c3436cfSJed Brown - vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present 35281c3436cfSJed Brown 35291c3436cfSJed Brown Level: beginner 35301c3436cfSJed Brown 3531c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 35321c3436cfSJed Brown @*/ 35331c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 35341c3436cfSJed Brown { 35351c3436cfSJed Brown PetscErrorCode ierr; 35361c3436cfSJed Brown 35371c3436cfSJed Brown PetscFunctionBegin; 3538c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 35391c3436cfSJed Brown if (vatol) { 35401c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 35411c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 35421c3436cfSJed Brown ts->vatol = vatol; 35431c3436cfSJed Brown } 3544c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 35451c3436cfSJed Brown if (vrtol) { 35461c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 35471c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 35481c3436cfSJed Brown ts->vrtol = vrtol; 35491c3436cfSJed Brown } 35501c3436cfSJed Brown PetscFunctionReturn(0); 35511c3436cfSJed Brown } 35521c3436cfSJed Brown 35531c3436cfSJed Brown #undef __FUNCT__ 3554c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 3555c5033834SJed Brown /*@ 3556c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 3557c5033834SJed Brown 3558c5033834SJed Brown Logically Collective 3559c5033834SJed Brown 3560c5033834SJed Brown Input Arguments: 3561c5033834SJed Brown . ts - time integration context 3562c5033834SJed Brown 3563c5033834SJed Brown Output Arguments: 3564c5033834SJed Brown + atol - scalar absolute tolerances, PETSC_NULL to ignore 3565c5033834SJed Brown . vatol - vector of absolute tolerances, PETSC_NULL to ignore 3566c5033834SJed Brown . rtol - scalar relative tolerances, PETSC_NULL to ignore 3567c5033834SJed Brown - vrtol - vector of relative tolerances, PETSC_NULL to ignore 3568c5033834SJed Brown 3569c5033834SJed Brown Level: beginner 3570c5033834SJed Brown 3571c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 3572c5033834SJed Brown @*/ 3573c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 3574c5033834SJed Brown { 3575c5033834SJed Brown 3576c5033834SJed Brown PetscFunctionBegin; 3577c5033834SJed Brown if (atol) *atol = ts->atol; 3578c5033834SJed Brown if (vatol) *vatol = ts->vatol; 3579c5033834SJed Brown if (rtol) *rtol = ts->rtol; 3580c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 3581c5033834SJed Brown PetscFunctionReturn(0); 3582c5033834SJed Brown } 3583c5033834SJed Brown 3584c5033834SJed Brown #undef __FUNCT__ 35851c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS" 35861c3436cfSJed Brown /*@ 35871c3436cfSJed Brown TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 35881c3436cfSJed Brown 35891c3436cfSJed Brown Collective on TS 35901c3436cfSJed Brown 35911c3436cfSJed Brown Input Arguments: 35921c3436cfSJed Brown + ts - time stepping context 35931c3436cfSJed Brown - Y - state vector to be compared to ts->vec_sol 35941c3436cfSJed Brown 35951c3436cfSJed Brown Output Arguments: 35961c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 35971c3436cfSJed Brown 35981c3436cfSJed Brown Level: developer 35991c3436cfSJed Brown 36001c3436cfSJed Brown .seealso: TSSetTolerances() 36011c3436cfSJed Brown @*/ 36021c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 36031c3436cfSJed Brown { 36041c3436cfSJed Brown PetscErrorCode ierr; 36051c3436cfSJed Brown PetscInt i,n,N; 36061c3436cfSJed Brown const PetscScalar *x,*y; 36071c3436cfSJed Brown Vec X; 36081c3436cfSJed Brown PetscReal sum,gsum; 36091c3436cfSJed Brown 36101c3436cfSJed Brown PetscFunctionBegin; 36111c3436cfSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36121c3436cfSJed Brown PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 36131c3436cfSJed Brown PetscValidPointer(norm,3); 36141c3436cfSJed Brown X = ts->vec_sol; 36151c3436cfSJed Brown PetscCheckSameTypeAndComm(X,1,Y,2); 36161c3436cfSJed Brown if (X == Y) SETERRQ(((PetscObject)X)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 36171c3436cfSJed Brown 36181c3436cfSJed Brown ierr = VecGetSize(X,&N);CHKERRQ(ierr); 36191c3436cfSJed Brown ierr = VecGetLocalSize(X,&n);CHKERRQ(ierr); 36201c3436cfSJed Brown ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr); 36211c3436cfSJed Brown ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 36221c3436cfSJed Brown sum = 0.; 3623ceefc113SJed Brown if (ts->vatol && ts->vrtol) { 3624ceefc113SJed Brown const PetscScalar *atol,*rtol; 3625ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3626ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3627ceefc113SJed Brown for (i=0; i<n; i++) { 3628ceefc113SJed Brown PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i])); 3629ceefc113SJed Brown sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol); 3630ceefc113SJed Brown } 3631ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3632ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3633ceefc113SJed Brown } else if (ts->vatol) { /* vector atol, scalar rtol */ 3634ceefc113SJed Brown const PetscScalar *atol; 3635ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3636ceefc113SJed Brown for (i=0; i<n; i++) { 3637ceefc113SJed Brown PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i])); 3638ceefc113SJed Brown sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol); 3639ceefc113SJed Brown } 3640ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3641ceefc113SJed Brown } else if (ts->vrtol) { /* scalar atol, vector rtol */ 3642ceefc113SJed Brown const PetscScalar *rtol; 3643ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3644ceefc113SJed Brown for (i=0; i<n; i++) { 3645ceefc113SJed Brown PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i])); 3646ceefc113SJed Brown sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol); 3647ceefc113SJed Brown } 3648ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3649ceefc113SJed Brown } else { /* scalar atol, scalar rtol */ 36501c3436cfSJed Brown for (i=0; i<n; i++) { 36511c3436cfSJed Brown PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i])); 36521c3436cfSJed Brown sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol); 36531c3436cfSJed Brown } 3654ceefc113SJed Brown } 36551c3436cfSJed Brown ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr); 36561c3436cfSJed Brown ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 36571c3436cfSJed Brown 36581c3436cfSJed Brown ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr); 36591c3436cfSJed Brown *norm = PetscSqrtReal(gsum / N); 36601c3436cfSJed Brown if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 36611c3436cfSJed Brown PetscFunctionReturn(0); 36621c3436cfSJed Brown } 36631c3436cfSJed Brown 36641c3436cfSJed Brown #undef __FUNCT__ 36658d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 36668d59e960SJed Brown /*@ 36678d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 36688d59e960SJed Brown 36698d59e960SJed Brown Logically Collective on TS 36708d59e960SJed Brown 36718d59e960SJed Brown Input Arguments: 36728d59e960SJed Brown + ts - time stepping context 36738d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 36748d59e960SJed Brown 36758d59e960SJed Brown Note: 36768d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 36778d59e960SJed Brown 36788d59e960SJed Brown Level: intermediate 36798d59e960SJed Brown 36808d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 36818d59e960SJed Brown @*/ 36828d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 36838d59e960SJed Brown { 36848d59e960SJed Brown 36858d59e960SJed Brown PetscFunctionBegin; 36868d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36878d59e960SJed Brown ts->cfltime_local = cfltime; 36888d59e960SJed Brown ts->cfltime = -1.; 36898d59e960SJed Brown PetscFunctionReturn(0); 36908d59e960SJed Brown } 36918d59e960SJed Brown 36928d59e960SJed Brown #undef __FUNCT__ 36938d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 36948d59e960SJed Brown /*@ 36958d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 36968d59e960SJed Brown 36978d59e960SJed Brown Collective on TS 36988d59e960SJed Brown 36998d59e960SJed Brown Input Arguments: 37008d59e960SJed Brown . ts - time stepping context 37018d59e960SJed Brown 37028d59e960SJed Brown Output Arguments: 37038d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 37048d59e960SJed Brown 37058d59e960SJed Brown Level: advanced 37068d59e960SJed Brown 37078d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 37088d59e960SJed Brown @*/ 37098d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 37108d59e960SJed Brown { 37118d59e960SJed Brown PetscErrorCode ierr; 37128d59e960SJed Brown 37138d59e960SJed Brown PetscFunctionBegin; 37148d59e960SJed Brown if (ts->cfltime < 0) { 37158d59e960SJed Brown ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr); 37168d59e960SJed Brown } 37178d59e960SJed Brown *cfltime = ts->cfltime; 37188d59e960SJed Brown PetscFunctionReturn(0); 37198d59e960SJed Brown } 37208d59e960SJed Brown 37218d59e960SJed Brown #undef __FUNCT__ 3722d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 3723d6ebe24aSShri Abhyankar /*@ 3724d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 3725d6ebe24aSShri Abhyankar 3726d6ebe24aSShri Abhyankar Input Parameters: 3727d6ebe24aSShri Abhyankar . ts - the TS context. 3728d6ebe24aSShri Abhyankar . xl - lower bound. 3729d6ebe24aSShri Abhyankar . xu - upper bound. 3730d6ebe24aSShri Abhyankar 3731d6ebe24aSShri Abhyankar Notes: 3732d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 373333c0c2ddSJed Brown SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp(). 3734d6ebe24aSShri Abhyankar 37352bd2b0e6SSatish Balay Level: advanced 37362bd2b0e6SSatish Balay 3737d6ebe24aSShri Abhyankar @*/ 3738d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 3739d6ebe24aSShri Abhyankar { 3740d6ebe24aSShri Abhyankar PetscErrorCode ierr; 3741d6ebe24aSShri Abhyankar SNES snes; 3742d6ebe24aSShri Abhyankar 3743d6ebe24aSShri Abhyankar PetscFunctionBegin; 3744d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3745d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 3746d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 3747d6ebe24aSShri Abhyankar } 3748d6ebe24aSShri Abhyankar 3749325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 3750c6db04a5SJed Brown #include <mex.h> 3751325fc9f4SBarry Smith 3752325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 3753325fc9f4SBarry Smith 3754325fc9f4SBarry Smith #undef __FUNCT__ 3755325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 3756325fc9f4SBarry Smith /* 3757325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 3758325fc9f4SBarry Smith TSSetFunctionMatlab(). 3759325fc9f4SBarry Smith 3760325fc9f4SBarry Smith Collective on TS 3761325fc9f4SBarry Smith 3762325fc9f4SBarry Smith Input Parameters: 3763325fc9f4SBarry Smith + snes - the TS context 3764325fc9f4SBarry Smith - x - input vector 3765325fc9f4SBarry Smith 3766325fc9f4SBarry Smith Output Parameter: 3767325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 3768325fc9f4SBarry Smith 3769325fc9f4SBarry Smith Notes: 3770325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 3771325fc9f4SBarry Smith implementations, so most users would not generally call this routine 3772325fc9f4SBarry Smith themselves. 3773325fc9f4SBarry Smith 3774325fc9f4SBarry Smith Level: developer 3775325fc9f4SBarry Smith 3776325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 3777325fc9f4SBarry Smith 3778325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3779325fc9f4SBarry Smith */ 37807087cfbeSBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx) 3781325fc9f4SBarry Smith { 3782325fc9f4SBarry Smith PetscErrorCode ierr; 3783325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3784325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 3785325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 3786325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 3787325fc9f4SBarry Smith 3788325fc9f4SBarry Smith PetscFunctionBegin; 3789325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 3790325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3791325fc9f4SBarry Smith PetscValidHeaderSpecific(xdot,VEC_CLASSID,4); 3792325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 3793325fc9f4SBarry Smith PetscCheckSameComm(snes,1,x,3); 3794325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 3795325fc9f4SBarry Smith 3796325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3797325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3798880f3077SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr); 3799325fc9f4SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 3800325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3801325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 3802325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 3803325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 3804325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 3805325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 3806325fc9f4SBarry Smith prhs[6] = sctx->ctx; 3807325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 3808325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3809325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 3810325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 3811325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 3812325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 3813325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 3814325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 3815325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 3816325fc9f4SBarry Smith PetscFunctionReturn(0); 3817325fc9f4SBarry Smith } 3818325fc9f4SBarry Smith 3819325fc9f4SBarry Smith 3820325fc9f4SBarry Smith #undef __FUNCT__ 3821325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 3822325fc9f4SBarry Smith /* 3823325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 3824325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 3825e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 3826325fc9f4SBarry Smith 3827325fc9f4SBarry Smith Logically Collective on TS 3828325fc9f4SBarry Smith 3829325fc9f4SBarry Smith Input Parameters: 3830325fc9f4SBarry Smith + ts - the TS context 3831325fc9f4SBarry Smith - func - function evaluation routine 3832325fc9f4SBarry Smith 3833325fc9f4SBarry Smith Calling sequence of func: 3834325fc9f4SBarry Smith $ func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx); 3835325fc9f4SBarry Smith 3836325fc9f4SBarry Smith Level: beginner 3837325fc9f4SBarry Smith 3838325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 3839325fc9f4SBarry Smith 3840325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3841325fc9f4SBarry Smith */ 3842cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 3843325fc9f4SBarry Smith { 3844325fc9f4SBarry Smith PetscErrorCode ierr; 3845325fc9f4SBarry Smith TSMatlabContext *sctx; 3846325fc9f4SBarry Smith 3847325fc9f4SBarry Smith PetscFunctionBegin; 3848325fc9f4SBarry Smith /* currently sctx is memory bleed */ 3849325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3850325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3851325fc9f4SBarry Smith /* 3852325fc9f4SBarry Smith This should work, but it doesn't 3853325fc9f4SBarry Smith sctx->ctx = ctx; 3854325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 3855325fc9f4SBarry Smith */ 3856325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 3857cdcf91faSSean Farley ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 3858325fc9f4SBarry Smith PetscFunctionReturn(0); 3859325fc9f4SBarry Smith } 3860325fc9f4SBarry Smith 3861325fc9f4SBarry Smith #undef __FUNCT__ 3862325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 3863325fc9f4SBarry Smith /* 3864325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 3865325fc9f4SBarry Smith TSSetJacobianMatlab(). 3866325fc9f4SBarry Smith 3867325fc9f4SBarry Smith Collective on TS 3868325fc9f4SBarry Smith 3869325fc9f4SBarry Smith Input Parameters: 3870cdcf91faSSean Farley + ts - the TS context 3871325fc9f4SBarry Smith . x - input vector 3872325fc9f4SBarry Smith . A, B - the matrices 3873325fc9f4SBarry Smith - ctx - user context 3874325fc9f4SBarry Smith 3875325fc9f4SBarry Smith Output Parameter: 3876325fc9f4SBarry Smith . flag - structure of the matrix 3877325fc9f4SBarry Smith 3878325fc9f4SBarry Smith Level: developer 3879325fc9f4SBarry Smith 3880325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 3881325fc9f4SBarry Smith 3882325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3883325fc9f4SBarry Smith @*/ 3884cdcf91faSSean Farley PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 3885325fc9f4SBarry Smith { 3886325fc9f4SBarry Smith PetscErrorCode ierr; 3887325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3888325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 3889325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 3890325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 3891325fc9f4SBarry Smith 3892325fc9f4SBarry Smith PetscFunctionBegin; 3893cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3894325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3895325fc9f4SBarry Smith 3896325fc9f4SBarry Smith /* call Matlab function in ctx with arguments x and y */ 3897325fc9f4SBarry Smith 3898cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 3899325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3900325fc9f4SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr); 3901325fc9f4SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 3902325fc9f4SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 3903325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3904325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 3905325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 3906325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 3907325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 3908325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 3909325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 3910325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 3911325fc9f4SBarry Smith prhs[8] = sctx->ctx; 3912325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 3913325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3914325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 3915325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 3916325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 3917325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 3918325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 3919325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 3920325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 3921325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 3922325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 3923325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 3924325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 3925325fc9f4SBarry Smith PetscFunctionReturn(0); 3926325fc9f4SBarry Smith } 3927325fc9f4SBarry Smith 3928325fc9f4SBarry Smith 3929325fc9f4SBarry Smith #undef __FUNCT__ 3930325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 3931325fc9f4SBarry Smith /* 3932325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 3933e3c5b3baSBarry 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 3934325fc9f4SBarry Smith 3935325fc9f4SBarry Smith Logically Collective on TS 3936325fc9f4SBarry Smith 3937325fc9f4SBarry Smith Input Parameters: 3938cdcf91faSSean Farley + ts - the TS context 3939325fc9f4SBarry Smith . A,B - Jacobian matrices 3940325fc9f4SBarry Smith . func - function evaluation routine 3941325fc9f4SBarry Smith - ctx - user context 3942325fc9f4SBarry Smith 3943325fc9f4SBarry Smith Calling sequence of func: 3944cdcf91faSSean Farley $ flag = func (TS ts,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx); 3945325fc9f4SBarry Smith 3946325fc9f4SBarry Smith 3947325fc9f4SBarry Smith Level: developer 3948325fc9f4SBarry Smith 3949325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 3950325fc9f4SBarry Smith 3951325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3952325fc9f4SBarry Smith */ 3953cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 3954325fc9f4SBarry Smith { 3955325fc9f4SBarry Smith PetscErrorCode ierr; 3956325fc9f4SBarry Smith TSMatlabContext *sctx; 3957325fc9f4SBarry Smith 3958325fc9f4SBarry Smith PetscFunctionBegin; 3959325fc9f4SBarry Smith /* currently sctx is memory bleed */ 3960325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3961325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3962325fc9f4SBarry Smith /* 3963325fc9f4SBarry Smith This should work, but it doesn't 3964325fc9f4SBarry Smith sctx->ctx = ctx; 3965325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 3966325fc9f4SBarry Smith */ 3967325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 3968cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 3969325fc9f4SBarry Smith PetscFunctionReturn(0); 3970325fc9f4SBarry Smith } 3971325fc9f4SBarry Smith 3972b5b1a830SBarry Smith #undef __FUNCT__ 3973b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 3974b5b1a830SBarry Smith /* 3975b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 3976b5b1a830SBarry Smith 3977b5b1a830SBarry Smith Collective on TS 3978b5b1a830SBarry Smith 3979b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3980b5b1a830SBarry Smith @*/ 3981cdcf91faSSean Farley PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec x, void *ctx) 3982b5b1a830SBarry Smith { 3983b5b1a830SBarry Smith PetscErrorCode ierr; 3984b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3985a530c242SBarry Smith int nlhs = 1,nrhs = 6; 3986b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 3987b5b1a830SBarry Smith long long int lx = 0,ls = 0; 3988b5b1a830SBarry Smith 3989b5b1a830SBarry Smith PetscFunctionBegin; 3990cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3991b5b1a830SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,4); 3992b5b1a830SBarry Smith 3993cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 3994b5b1a830SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3995b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3996b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 3997b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 3998b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 3999b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 4000b5b1a830SBarry Smith prhs[5] = sctx->ctx; 4001b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 4002b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4003b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 4004b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 4005b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 4006b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 4007b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 4008b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 4009b5b1a830SBarry Smith PetscFunctionReturn(0); 4010b5b1a830SBarry Smith } 4011b5b1a830SBarry Smith 4012b5b1a830SBarry Smith 4013b5b1a830SBarry Smith #undef __FUNCT__ 4014b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 4015b5b1a830SBarry Smith /* 4016b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 4017b5b1a830SBarry Smith 4018b5b1a830SBarry Smith Level: developer 4019b5b1a830SBarry Smith 4020b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 4021b5b1a830SBarry Smith 4022b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 4023b5b1a830SBarry Smith */ 4024cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 4025b5b1a830SBarry Smith { 4026b5b1a830SBarry Smith PetscErrorCode ierr; 4027b5b1a830SBarry Smith TSMatlabContext *sctx; 4028b5b1a830SBarry Smith 4029b5b1a830SBarry Smith PetscFunctionBegin; 4030b5b1a830SBarry Smith /* currently sctx is memory bleed */ 4031b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 4032b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4033b5b1a830SBarry Smith /* 4034b5b1a830SBarry Smith This should work, but it doesn't 4035b5b1a830SBarry Smith sctx->ctx = ctx; 4036b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 4037b5b1a830SBarry Smith */ 4038b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 4039cdcf91faSSean Farley ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 4040b5b1a830SBarry Smith PetscFunctionReturn(0); 4041b5b1a830SBarry Smith } 4042325fc9f4SBarry Smith #endif 4043b3603a34SBarry Smith 40440b039ecaSBarry Smith 40450b039ecaSBarry Smith 4046b3603a34SBarry Smith #undef __FUNCT__ 40474f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 4048b3603a34SBarry Smith /*@C 40494f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 4050b3603a34SBarry Smith in a time based line graph 4051b3603a34SBarry Smith 4052b3603a34SBarry Smith Collective on TS 4053b3603a34SBarry Smith 4054b3603a34SBarry Smith Input Parameters: 4055b3603a34SBarry Smith + ts - the TS context 4056b3603a34SBarry Smith . step - current time-step 4057b3603a34SBarry Smith . ptime - current time 4058b3603a34SBarry Smith - lg - a line graph object 4059b3603a34SBarry Smith 4060b3603a34SBarry Smith Level: intermediate 4061b3603a34SBarry Smith 40620b039ecaSBarry Smith Notes: each process in a parallel run displays its component solutions in a separate window 4063b3603a34SBarry Smith 4064b3603a34SBarry Smith .keywords: TS, vector, monitor, view 4065b3603a34SBarry Smith 4066b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4067b3603a34SBarry Smith @*/ 40684f09c107SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 4069b3603a34SBarry Smith { 4070b3603a34SBarry Smith PetscErrorCode ierr; 40710b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4072b3603a34SBarry Smith const PetscScalar *yy; 407358ff32f7SBarry Smith PetscInt dim; 4074b3603a34SBarry Smith 4075b3603a34SBarry Smith PetscFunctionBegin; 407658ff32f7SBarry Smith if (!step) { 4077a9f9c1f6SBarry Smith PetscDrawAxis axis; 4078a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4079a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 408058ff32f7SBarry Smith ierr = VecGetLocalSize(x,&dim);CHKERRQ(ierr); 40810b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 40820b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 408358ff32f7SBarry Smith } 4084b3603a34SBarry Smith ierr = VecGetArrayRead(x,&yy);CHKERRQ(ierr); 4085e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 4086e3efe391SJed Brown { 4087e3efe391SJed Brown PetscReal *yreal; 4088e3efe391SJed Brown PetscInt i,n; 4089e3efe391SJed Brown ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr); 4090e3efe391SJed Brown ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4091e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 40920b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4093e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4094e3efe391SJed Brown } 4095e3efe391SJed Brown #else 40960b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4097e3efe391SJed Brown #endif 4098b3603a34SBarry Smith ierr = VecRestoreArrayRead(x,&yy);CHKERRQ(ierr); 40993fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){ 41000b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 41013923b477SBarry Smith } 4102b3603a34SBarry Smith PetscFunctionReturn(0); 4103b3603a34SBarry Smith } 4104b3603a34SBarry Smith 4105b3603a34SBarry Smith #undef __FUNCT__ 41064f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 4107ef20d060SBarry Smith /*@C 41084f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 4109ef20d060SBarry Smith in a time based line graph 4110ef20d060SBarry Smith 4111ef20d060SBarry Smith Collective on TS 4112ef20d060SBarry Smith 4113ef20d060SBarry Smith Input Parameters: 4114ef20d060SBarry Smith + ts - the TS context 4115ef20d060SBarry Smith . step - current time-step 4116ef20d060SBarry Smith . ptime - current time 4117ef20d060SBarry Smith - lg - a line graph object 4118ef20d060SBarry Smith 4119ef20d060SBarry Smith Level: intermediate 4120ef20d060SBarry Smith 4121abd5a294SJed Brown Notes: 4122abd5a294SJed Brown Only for sequential solves. 4123abd5a294SJed Brown 4124abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 4125abd5a294SJed Brown 4126abd5a294SJed Brown Options Database Keys: 41274f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 4128ef20d060SBarry Smith 4129ef20d060SBarry Smith .keywords: TS, vector, monitor, view 4130ef20d060SBarry Smith 4131abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 4132ef20d060SBarry Smith @*/ 41334f09c107SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 4134ef20d060SBarry Smith { 4135ef20d060SBarry Smith PetscErrorCode ierr; 41360b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4137ef20d060SBarry Smith const PetscScalar *yy; 4138ef20d060SBarry Smith Vec y; 4139a9f9c1f6SBarry Smith PetscInt dim; 4140ef20d060SBarry Smith 4141ef20d060SBarry Smith PetscFunctionBegin; 4142a9f9c1f6SBarry Smith if (!step) { 4143a9f9c1f6SBarry Smith PetscDrawAxis axis; 4144a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 41451ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 4146a9f9c1f6SBarry Smith ierr = VecGetLocalSize(x,&dim);CHKERRQ(ierr); 4147a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 4148a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4149a9f9c1f6SBarry Smith } 4150ef20d060SBarry Smith ierr = VecDuplicate(x,&y);CHKERRQ(ierr); 4151ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 4152ef20d060SBarry Smith ierr = VecAXPY(y,-1.0,x);CHKERRQ(ierr); 4153ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 4154e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 4155e3efe391SJed Brown { 4156e3efe391SJed Brown PetscReal *yreal; 4157e3efe391SJed Brown PetscInt i,n; 4158e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 4159e3efe391SJed Brown ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4160e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 41610b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4162e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4163e3efe391SJed Brown } 4164e3efe391SJed Brown #else 41650b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4166e3efe391SJed Brown #endif 4167ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 4168ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 41693fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){ 41700b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 41713923b477SBarry Smith } 4172ef20d060SBarry Smith PetscFunctionReturn(0); 4173ef20d060SBarry Smith } 4174ef20d060SBarry Smith 4175201da799SBarry Smith #undef __FUNCT__ 4176201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 4177201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4178201da799SBarry Smith { 4179201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4180201da799SBarry Smith PetscReal x = ptime,y; 4181201da799SBarry Smith PetscErrorCode ierr; 4182201da799SBarry Smith PetscInt its; 4183201da799SBarry Smith 4184201da799SBarry Smith PetscFunctionBegin; 4185201da799SBarry Smith if (!n) { 4186201da799SBarry Smith PetscDrawAxis axis; 4187201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4188201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 4189201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4190201da799SBarry Smith ctx->snes_its = 0; 4191201da799SBarry Smith } 4192201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 4193201da799SBarry Smith y = its - ctx->snes_its; 4194201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 41953fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){ 4196201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4197201da799SBarry Smith } 4198201da799SBarry Smith ctx->snes_its = its; 4199201da799SBarry Smith PetscFunctionReturn(0); 4200201da799SBarry Smith } 4201201da799SBarry Smith 4202201da799SBarry Smith #undef __FUNCT__ 4203201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 4204201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4205201da799SBarry Smith { 4206201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4207201da799SBarry Smith PetscReal x = ptime,y; 4208201da799SBarry Smith PetscErrorCode ierr; 4209201da799SBarry Smith PetscInt its; 4210201da799SBarry Smith 4211201da799SBarry Smith PetscFunctionBegin; 4212201da799SBarry Smith if (!n) { 4213201da799SBarry Smith PetscDrawAxis axis; 4214201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4215201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 4216201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4217201da799SBarry Smith ctx->ksp_its = 0; 4218201da799SBarry Smith } 4219201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 4220201da799SBarry Smith y = its - ctx->ksp_its; 4221201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 422299fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){ 4223201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4224201da799SBarry Smith } 4225201da799SBarry Smith ctx->ksp_its = its; 4226201da799SBarry Smith PetscFunctionReturn(0); 4227201da799SBarry Smith } 4228