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 64*201da799SBarry Smith . -ts_monitor_lg_timestep - plot time-step information at each timestep 65*201da799SBarry Smith . -ts_monitor_lg_ksp_iterations - plot the number of linear iterations used at each time-step 66*201da799SBarry 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 } 143*201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 144*201da799SBarry Smith if (opt) { 145*201da799SBarry Smith TSMonitorLGCtx ctx; 146*201da799SBarry Smith PetscInt howoften = 1; 147*201da799SBarry Smith 148*201da799SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 149*201da799SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx); 150*201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 151*201da799SBarry Smith } 152*201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 153*201da799SBarry Smith if (opt) { 154*201da799SBarry Smith TSMonitorLGCtx ctx; 155*201da799SBarry Smith PetscInt howoften = 1; 156*201da799SBarry Smith 157*201da799SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 158*201da799SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx); 159*201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 160*201da799SBarry Smith } 161ef20d060SBarry Smith opt = PETSC_FALSE; 162acfcf0e5SJed Brown ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr); 163a7cc72afSBarry Smith if (opt) { 1646083293cSBarry Smith void *ctx; 165a80ad3e0SBarry Smith PetscViewer viewer; 166a80ad3e0SBarry Smith 167ab352700SBarry Smith ierr = PetscViewerDrawOpen(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,&viewer); 168a80ad3e0SBarry Smith ierr = TSMonitorSolutionCreate(ts,viewer,&ctx);CHKERRQ(ierr); 169a80ad3e0SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 1706083293cSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolution,ctx,TSMonitorSolutionDestroy);CHKERRQ(ierr); 171bdad233fSMatthew Knepley } 172fb1732b5SBarry Smith opt = PETSC_FALSE; 173fb1732b5SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 174fb1732b5SBarry Smith if (flg) { 175fb1732b5SBarry Smith PetscViewer ctx; 176fb1732b5SBarry Smith if (monfilename[0]) { 177fb1732b5SBarry Smith ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 178fb1732b5SBarry Smith } else { 179fb1732b5SBarry Smith ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm); 180fb1732b5SBarry Smith } 181fb1732b5SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 182fb1732b5SBarry Smith } 183ed81e22dSJed Brown opt = PETSC_FALSE; 184ed81e22dSJed 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); 185ed81e22dSJed Brown if (flg) { 186ed81e22dSJed Brown const char *ptr,*ptr2; 187ed81e22dSJed Brown char *filetemplate; 188ed81e22dSJed Brown if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 189ed81e22dSJed Brown /* Do some cursory validation of the input. */ 190ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 191ed81e22dSJed Brown if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 192ed81e22dSJed Brown for (ptr++ ; ptr && *ptr; ptr++) { 193ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 194ed81e22dSJed 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"); 195ed81e22dSJed Brown if (ptr2) break; 196ed81e22dSJed Brown } 197ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 198ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 199ed81e22dSJed Brown } 200bdad233fSMatthew Knepley 2011c3436cfSJed Brown ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr); 2021c3436cfSJed Brown ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr); 2031c3436cfSJed Brown 204d52bd9f3SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 205d52bd9f3SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 206d52bd9f3SBarry Smith 207bdad233fSMatthew Knepley /* Handle specific TS options */ 208abc0a331SBarry Smith if (ts->ops->setfromoptions) { 209bdad233fSMatthew Knepley ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr); 210bdad233fSMatthew Knepley } 2115d973c19SBarry Smith 2125d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 2135d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 214bdad233fSMatthew Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 215bdad233fSMatthew Knepley PetscFunctionReturn(0); 216bdad233fSMatthew Knepley } 217bdad233fSMatthew Knepley 218bdad233fSMatthew Knepley #undef __FUNCT__ 219cdcf91faSSean Farley #undef __FUNCT__ 2204a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian" 221a7a1495cSBarry Smith /*@ 2228c385f81SBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 223a7a1495cSBarry Smith set with TSSetRHSJacobian(). 224a7a1495cSBarry Smith 225a7a1495cSBarry Smith Collective on TS and Vec 226a7a1495cSBarry Smith 227a7a1495cSBarry Smith Input Parameters: 228316643e7SJed Brown + ts - the TS context 229a7a1495cSBarry Smith . t - current timestep 230a7a1495cSBarry Smith - x - input vector 231a7a1495cSBarry Smith 232a7a1495cSBarry Smith Output Parameters: 233a7a1495cSBarry Smith + A - Jacobian matrix 234a7a1495cSBarry Smith . B - optional preconditioning matrix 235a7a1495cSBarry Smith - flag - flag indicating matrix structure 236a7a1495cSBarry Smith 237a7a1495cSBarry Smith Notes: 238a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 239a7a1495cSBarry Smith is used internally within the nonlinear solvers. 240a7a1495cSBarry Smith 24194b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 242a7a1495cSBarry Smith flag parameter. 243a7a1495cSBarry Smith 244a7a1495cSBarry Smith Level: developer 245a7a1495cSBarry Smith 246a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 247a7a1495cSBarry Smith 24894b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 249a7a1495cSBarry Smith @*/ 2507087cfbeSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg) 251a7a1495cSBarry Smith { 252dfbe8321SBarry Smith PetscErrorCode ierr; 2530e4ef248SJed Brown PetscInt Xstate; 25424989b8cSPeter Brune DM dm; 25524989b8cSPeter Brune TSDM tsdm; 25624989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 25724989b8cSPeter Brune void *ctx; 25824989b8cSPeter Brune TSIJacobian ijacobianfunc; 259a7a1495cSBarry Smith 260a7a1495cSBarry Smith PetscFunctionBegin; 2610700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2620700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 263c9780b6fSBarry Smith PetscCheckSameComm(ts,1,X,3); 26424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 26524989b8cSPeter Brune ierr = DMTSGetContext(dm,&tsdm);CHKERRQ(ierr); 26624989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 26724989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr); 2680e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr); 2690e4ef248SJed Brown if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) { 2700e4ef248SJed Brown *flg = ts->rhsjacobian.mstructure; 2710e4ef248SJed Brown PetscFunctionReturn(0); 272f8ede8e7SJed Brown } 273d90be118SSean Farley 27424989b8cSPeter Brune if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 275d90be118SSean Farley 27624989b8cSPeter Brune if (rhsjacobianfunc) { 277d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 278a7a1495cSBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 279a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 28024989b8cSPeter Brune ierr = (*rhsjacobianfunc)(ts,t,X,A,B,flg,ctx);CHKERRQ(ierr); 281a7a1495cSBarry Smith PetscStackPop; 282d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 283a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 2840700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 2850700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 286ef66eb69SBarry Smith } else { 287214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 288214bc6a2SJed Brown if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);} 289214bc6a2SJed Brown *flg = SAME_NONZERO_PATTERN; 290ef66eb69SBarry Smith } 2910e4ef248SJed Brown ts->rhsjacobian.time = t; 2920e4ef248SJed Brown ts->rhsjacobian.X = X; 2930e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 2940e4ef248SJed Brown ts->rhsjacobian.mstructure = *flg; 295a7a1495cSBarry Smith PetscFunctionReturn(0); 296a7a1495cSBarry Smith } 297a7a1495cSBarry Smith 2984a2ae208SSatish Balay #undef __FUNCT__ 2994a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 300316643e7SJed Brown /*@ 301d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 302d763cef2SBarry Smith 303316643e7SJed Brown Collective on TS and Vec 304316643e7SJed Brown 305316643e7SJed Brown Input Parameters: 306316643e7SJed Brown + ts - the TS context 307316643e7SJed Brown . t - current time 308316643e7SJed Brown - x - state vector 309316643e7SJed Brown 310316643e7SJed Brown Output Parameter: 311316643e7SJed Brown . y - right hand side 312316643e7SJed Brown 313316643e7SJed Brown Note: 314316643e7SJed Brown Most users should not need to explicitly call this routine, as it 315316643e7SJed Brown is used internally within the nonlinear solvers. 316316643e7SJed Brown 317316643e7SJed Brown Level: developer 318316643e7SJed Brown 319316643e7SJed Brown .keywords: TS, compute 320316643e7SJed Brown 321316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 322316643e7SJed Brown @*/ 323dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y) 324d763cef2SBarry Smith { 325dfbe8321SBarry Smith PetscErrorCode ierr; 326d763cef2SBarry Smith 32724989b8cSPeter Brune TSRHSFunction rhsfunction; 32824989b8cSPeter Brune TSIFunction ifunction; 32924989b8cSPeter Brune void *ctx; 33024989b8cSPeter Brune DM dm; 33124989b8cSPeter Brune 332d763cef2SBarry Smith PetscFunctionBegin; 3330700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3340700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3350700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 33624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 33724989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 33824989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr); 339d763cef2SBarry Smith 34024989b8cSPeter Brune if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 341d763cef2SBarry Smith 342d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 34324989b8cSPeter Brune if (rhsfunction) { 344d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 34524989b8cSPeter Brune ierr = (*rhsfunction)(ts,t,x,y,ctx);CHKERRQ(ierr); 346d763cef2SBarry Smith PetscStackPop; 347214bc6a2SJed Brown } else { 348214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 349b2cd27e8SJed Brown } 35044a41b28SSean Farley 351d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 352d763cef2SBarry Smith PetscFunctionReturn(0); 353d763cef2SBarry Smith } 354d763cef2SBarry Smith 3554a2ae208SSatish Balay #undef __FUNCT__ 356ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 357ef20d060SBarry Smith /*@ 358ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 359ef20d060SBarry Smith 360ef20d060SBarry Smith Collective on TS and Vec 361ef20d060SBarry Smith 362ef20d060SBarry Smith Input Parameters: 363ef20d060SBarry Smith + ts - the TS context 364ef20d060SBarry Smith - t - current time 365ef20d060SBarry Smith 366ef20d060SBarry Smith Output Parameter: 367ef20d060SBarry Smith . y - right hand side 368ef20d060SBarry Smith 369ef20d060SBarry Smith Note: 370ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 371ef20d060SBarry Smith is used internally within the nonlinear solvers. 372ef20d060SBarry Smith 373ef20d060SBarry Smith Level: developer 374ef20d060SBarry Smith 375ef20d060SBarry Smith .keywords: TS, compute 376ef20d060SBarry Smith 377abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 378ef20d060SBarry Smith @*/ 379ef20d060SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec x) 380ef20d060SBarry Smith { 381ef20d060SBarry Smith PetscErrorCode ierr; 382ef20d060SBarry Smith TSSolutionFunction solutionfunction; 383ef20d060SBarry Smith void *ctx; 384ef20d060SBarry Smith DM dm; 385ef20d060SBarry Smith 386ef20d060SBarry Smith PetscFunctionBegin; 387ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 388ef20d060SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 389ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 390ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 391ef20d060SBarry Smith 392ef20d060SBarry Smith if (solutionfunction) { 393ef20d060SBarry Smith PetscStackPush("TS user right-hand-side function"); 394ef20d060SBarry Smith ierr = (*solutionfunction)(ts,t,x,ctx);CHKERRQ(ierr); 395ef20d060SBarry Smith PetscStackPop; 396ef20d060SBarry Smith } 397ef20d060SBarry Smith PetscFunctionReturn(0); 398ef20d060SBarry Smith } 399ef20d060SBarry Smith 400ef20d060SBarry Smith #undef __FUNCT__ 401214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 402214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 403214bc6a2SJed Brown { 4042dd45cf8SJed Brown Vec F; 405214bc6a2SJed Brown PetscErrorCode ierr; 406214bc6a2SJed Brown 407214bc6a2SJed Brown PetscFunctionBegin; 4080da9a4f0SJed Brown *Frhs = PETSC_NULL; 4092dd45cf8SJed Brown ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 410214bc6a2SJed Brown if (!ts->Frhs) { 4112dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 412214bc6a2SJed Brown } 413214bc6a2SJed Brown *Frhs = ts->Frhs; 414214bc6a2SJed Brown PetscFunctionReturn(0); 415214bc6a2SJed Brown } 416214bc6a2SJed Brown 417214bc6a2SJed Brown #undef __FUNCT__ 418214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 419214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 420214bc6a2SJed Brown { 421214bc6a2SJed Brown Mat A,B; 4222dd45cf8SJed Brown PetscErrorCode ierr; 423214bc6a2SJed Brown 424214bc6a2SJed Brown PetscFunctionBegin; 425214bc6a2SJed Brown ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 426214bc6a2SJed Brown if (Arhs) { 427214bc6a2SJed Brown if (!ts->Arhs) { 428214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 429214bc6a2SJed Brown } 430214bc6a2SJed Brown *Arhs = ts->Arhs; 431214bc6a2SJed Brown } 432214bc6a2SJed Brown if (Brhs) { 433214bc6a2SJed Brown if (!ts->Brhs) { 434214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 435214bc6a2SJed Brown } 436214bc6a2SJed Brown *Brhs = ts->Brhs; 437214bc6a2SJed Brown } 438214bc6a2SJed Brown PetscFunctionReturn(0); 439214bc6a2SJed Brown } 440214bc6a2SJed Brown 441214bc6a2SJed Brown #undef __FUNCT__ 442316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 443316643e7SJed Brown /*@ 444316643e7SJed Brown TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0 445316643e7SJed Brown 446316643e7SJed Brown Collective on TS and Vec 447316643e7SJed Brown 448316643e7SJed Brown Input Parameters: 449316643e7SJed Brown + ts - the TS context 450316643e7SJed Brown . t - current time 451316643e7SJed Brown . X - state vector 452214bc6a2SJed Brown . Xdot - time derivative of state vector 453214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 454316643e7SJed Brown 455316643e7SJed Brown Output Parameter: 456316643e7SJed Brown . Y - right hand side 457316643e7SJed Brown 458316643e7SJed Brown Note: 459316643e7SJed Brown Most users should not need to explicitly call this routine, as it 460316643e7SJed Brown is used internally within the nonlinear solvers. 461316643e7SJed Brown 462316643e7SJed Brown If the user did did not write their equations in implicit form, this 463316643e7SJed Brown function recasts them in implicit form. 464316643e7SJed Brown 465316643e7SJed Brown Level: developer 466316643e7SJed Brown 467316643e7SJed Brown .keywords: TS, compute 468316643e7SJed Brown 469316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 470316643e7SJed Brown @*/ 471214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex) 472316643e7SJed Brown { 473316643e7SJed Brown PetscErrorCode ierr; 47424989b8cSPeter Brune TSIFunction ifunction; 47524989b8cSPeter Brune TSRHSFunction rhsfunction; 47624989b8cSPeter Brune void *ctx; 47724989b8cSPeter Brune DM dm; 478316643e7SJed Brown 479316643e7SJed Brown PetscFunctionBegin; 4800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4810700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 4820700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 4830700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 484316643e7SJed Brown 48524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 48624989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 48724989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr); 48824989b8cSPeter Brune 48924989b8cSPeter Brune if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 490d90be118SSean Farley 491316643e7SJed Brown ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 49224989b8cSPeter Brune if (ifunction) { 493316643e7SJed Brown PetscStackPush("TS user implicit function"); 49424989b8cSPeter Brune ierr = (*ifunction)(ts,t,X,Xdot,Y,ctx);CHKERRQ(ierr); 495316643e7SJed Brown PetscStackPop; 496214bc6a2SJed Brown } 497214bc6a2SJed Brown if (imex) { 49824989b8cSPeter Brune if (!ifunction) { 4992dd45cf8SJed Brown ierr = VecCopy(Xdot,Y);CHKERRQ(ierr); 5002dd45cf8SJed Brown } 50124989b8cSPeter Brune } else if (rhsfunction) { 50224989b8cSPeter Brune if (ifunction) { 503214bc6a2SJed Brown Vec Frhs; 504214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 505214bc6a2SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr); 506214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 5072dd45cf8SJed Brown } else { 5082dd45cf8SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr); 5092dd45cf8SJed Brown ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr); 510316643e7SJed Brown } 5114a6899ffSJed Brown } 512316643e7SJed Brown ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 513316643e7SJed Brown PetscFunctionReturn(0); 514316643e7SJed Brown } 515316643e7SJed Brown 516316643e7SJed Brown #undef __FUNCT__ 517316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 518316643e7SJed Brown /*@ 519316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 520316643e7SJed Brown 521316643e7SJed Brown Collective on TS and Vec 522316643e7SJed Brown 523316643e7SJed Brown Input 524316643e7SJed Brown Input Parameters: 525316643e7SJed Brown + ts - the TS context 526316643e7SJed Brown . t - current timestep 527316643e7SJed Brown . X - state vector 528316643e7SJed Brown . Xdot - time derivative of state vector 529214bc6a2SJed Brown . shift - shift to apply, see note below 530214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 531316643e7SJed Brown 532316643e7SJed Brown Output Parameters: 533316643e7SJed Brown + A - Jacobian matrix 534316643e7SJed Brown . B - optional preconditioning matrix 535316643e7SJed Brown - flag - flag indicating matrix structure 536316643e7SJed Brown 537316643e7SJed Brown Notes: 538316643e7SJed Brown If F(t,X,Xdot)=0 is the DAE, the required Jacobian is 539316643e7SJed Brown 540316643e7SJed Brown dF/dX + shift*dF/dXdot 541316643e7SJed Brown 542316643e7SJed Brown Most users should not need to explicitly call this routine, as it 543316643e7SJed Brown is used internally within the nonlinear solvers. 544316643e7SJed Brown 545316643e7SJed Brown Level: developer 546316643e7SJed Brown 547316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 548316643e7SJed Brown 549316643e7SJed Brown .seealso: TSSetIJacobian() 55063495f91SJed Brown @*/ 551214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex) 552316643e7SJed Brown { 5530026cea9SSean Farley PetscInt Xstate, Xdotstate; 554316643e7SJed Brown PetscErrorCode ierr; 55524989b8cSPeter Brune TSIJacobian ijacobian; 55624989b8cSPeter Brune TSRHSJacobian rhsjacobian; 55724989b8cSPeter Brune DM dm; 55824989b8cSPeter Brune void *ctx; 559316643e7SJed Brown 560316643e7SJed Brown PetscFunctionBegin; 56124989b8cSPeter Brune 5620700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5630700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 5640700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 565316643e7SJed Brown PetscValidPointer(A,6); 5660700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,6); 567316643e7SJed Brown PetscValidPointer(B,7); 5680700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,7); 569316643e7SJed Brown PetscValidPointer(flg,8); 57024989b8cSPeter Brune 57124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 57224989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 57324989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr); 57424989b8cSPeter Brune 5750026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr); 5760026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr); 5770026cea9SSean 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))) { 5780026cea9SSean Farley *flg = ts->ijacobian.mstructure; 5790026cea9SSean Farley ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 5800026cea9SSean Farley PetscFunctionReturn(0); 5810026cea9SSean Farley } 582316643e7SJed Brown 58324989b8cSPeter Brune if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 584316643e7SJed Brown 5854e684422SJed Brown *flg = SAME_NONZERO_PATTERN; /* In case we're solving a linear problem in which case it wouldn't get initialized below. */ 586316643e7SJed Brown ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 58724989b8cSPeter Brune if (ijacobian) { 5882dd45cf8SJed Brown *flg = DIFFERENT_NONZERO_PATTERN; 589316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 59024989b8cSPeter Brune ierr = (*ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ctx);CHKERRQ(ierr); 591316643e7SJed Brown PetscStackPop; 592214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 593214bc6a2SJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 594214bc6a2SJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 5954a6899ffSJed Brown } 596214bc6a2SJed Brown if (imex) { 59724989b8cSPeter Brune if (!ijacobian) { /* system was written as Xdot = F(t,X) */ 598214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 5992dd45cf8SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 600214bc6a2SJed Brown if (*A != *B) { 601214bc6a2SJed Brown ierr = MatZeroEntries(*B);CHKERRQ(ierr); 602214bc6a2SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 603214bc6a2SJed Brown } 604214bc6a2SJed Brown *flg = SAME_PRECONDITIONER; 605214bc6a2SJed Brown } 606214bc6a2SJed Brown } else { 60724989b8cSPeter Brune if (!ijacobian) { 608214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr); 609214bc6a2SJed Brown ierr = MatScale(*A,-1);CHKERRQ(ierr); 610214bc6a2SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 611316643e7SJed Brown if (*A != *B) { 612316643e7SJed Brown ierr = MatScale(*B,-1);CHKERRQ(ierr); 613316643e7SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 614316643e7SJed Brown } 61524989b8cSPeter Brune } else if (rhsjacobian) { 616214bc6a2SJed Brown Mat Arhs,Brhs; 617214bc6a2SJed Brown MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN; 618214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 619214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 620214bc6a2SJed Brown axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN; 621214bc6a2SJed Brown ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr); 622214bc6a2SJed Brown if (*A != *B) { 623214bc6a2SJed Brown ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr); 624214bc6a2SJed Brown } 625214bc6a2SJed Brown *flg = PetscMin(*flg,flg2); 626214bc6a2SJed Brown } 627316643e7SJed Brown } 6280026cea9SSean Farley 6290026cea9SSean Farley ts->ijacobian.time = t; 6300026cea9SSean Farley ts->ijacobian.X = X; 6310026cea9SSean Farley ts->ijacobian.Xdot = Xdot; 6320026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr); 6330026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr); 6340026cea9SSean Farley ts->ijacobian.shift = shift; 6350026cea9SSean Farley ts->ijacobian.imex = imex; 6360026cea9SSean Farley ts->ijacobian.mstructure = *flg; 637316643e7SJed Brown ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 638316643e7SJed Brown PetscFunctionReturn(0); 639316643e7SJed Brown } 640316643e7SJed Brown 641316643e7SJed Brown #undef __FUNCT__ 6424a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 643d763cef2SBarry Smith /*@C 644d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 645d763cef2SBarry Smith F(t,u), where U_t = F(t,u). 646d763cef2SBarry Smith 6473f9fe445SBarry Smith Logically Collective on TS 648d763cef2SBarry Smith 649d763cef2SBarry Smith Input Parameters: 650d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 651ca94891dSJed Brown . r - vector to put the computed right hand side (or PETSC_NULL to have it created) 652d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 653d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 654d763cef2SBarry Smith function evaluation routine (may be PETSC_NULL) 655d763cef2SBarry Smith 656d763cef2SBarry Smith Calling sequence of func: 65787828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 658d763cef2SBarry Smith 659d763cef2SBarry Smith + t - current timestep 660d763cef2SBarry Smith . u - input vector 661d763cef2SBarry Smith . F - function vector 662d763cef2SBarry Smith - ctx - [optional] user-defined function context 663d763cef2SBarry Smith 664d763cef2SBarry Smith Level: beginner 665d763cef2SBarry Smith 666d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 667d763cef2SBarry Smith 668d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian() 669d763cef2SBarry Smith @*/ 670089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 671d763cef2SBarry Smith { 672089b2837SJed Brown PetscErrorCode ierr; 673089b2837SJed Brown SNES snes; 674e856ceecSJed Brown Vec ralloc = PETSC_NULL; 67524989b8cSPeter Brune DM dm; 676d763cef2SBarry Smith 677089b2837SJed Brown PetscFunctionBegin; 6780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 679ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 68024989b8cSPeter Brune 68124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 68224989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 683089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 684e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 685e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 686e856ceecSJed Brown r = ralloc; 687e856ceecSJed Brown } 688089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 689e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 690d763cef2SBarry Smith PetscFunctionReturn(0); 691d763cef2SBarry Smith } 692d763cef2SBarry Smith 6934a2ae208SSatish Balay #undef __FUNCT__ 694ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 695ef20d060SBarry Smith /*@C 696abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 697ef20d060SBarry Smith 698ef20d060SBarry Smith Logically Collective on TS 699ef20d060SBarry Smith 700ef20d060SBarry Smith Input Parameters: 701ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 702ef20d060SBarry Smith . f - routine for evaluating the solution 703ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 704ef20d060SBarry Smith function evaluation routine (may be PETSC_NULL) 705ef20d060SBarry Smith 706ef20d060SBarry Smith Calling sequence of func: 707ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 708ef20d060SBarry Smith 709ef20d060SBarry Smith + t - current timestep 710ef20d060SBarry Smith . u - output vector 711ef20d060SBarry Smith - ctx - [optional] user-defined function context 712ef20d060SBarry Smith 713abd5a294SJed Brown Notes: 714abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 715abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 716abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 717abd5a294SJed Brown 7184f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 719abd5a294SJed Brown 720ef20d060SBarry Smith Level: beginner 721ef20d060SBarry Smith 722ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 723ef20d060SBarry Smith 724abd5a294SJed Brown .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction() 725ef20d060SBarry Smith @*/ 726ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 727ef20d060SBarry Smith { 728ef20d060SBarry Smith PetscErrorCode ierr; 729ef20d060SBarry Smith DM dm; 730ef20d060SBarry Smith 731ef20d060SBarry Smith PetscFunctionBegin; 732ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 733ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 734ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 735ef20d060SBarry Smith PetscFunctionReturn(0); 736ef20d060SBarry Smith } 737ef20d060SBarry Smith 738ef20d060SBarry Smith #undef __FUNCT__ 7394a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 740d763cef2SBarry Smith /*@C 741d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 742d763cef2SBarry Smith where U_t = F(U,t), as well as the location to store the matrix. 743d763cef2SBarry Smith 7443f9fe445SBarry Smith Logically Collective on TS 745d763cef2SBarry Smith 746d763cef2SBarry Smith Input Parameters: 747d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 748d763cef2SBarry Smith . A - Jacobian matrix 749d763cef2SBarry Smith . B - preconditioner matrix (usually same as A) 750d763cef2SBarry Smith . f - the Jacobian evaluation routine 751d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 752d763cef2SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) 753d763cef2SBarry Smith 754d763cef2SBarry Smith Calling sequence of func: 75587828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 756d763cef2SBarry Smith 757d763cef2SBarry Smith + t - current timestep 758d763cef2SBarry Smith . u - input vector 759d763cef2SBarry Smith . A - matrix A, where U_t = A(t)u 760d763cef2SBarry Smith . B - preconditioner matrix, usually the same as A 761d763cef2SBarry Smith . flag - flag indicating information about the preconditioner matrix 76294b7f48cSBarry Smith structure (same as flag in KSPSetOperators()) 763d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 764d763cef2SBarry Smith 765d763cef2SBarry Smith Notes: 76694b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 767d763cef2SBarry Smith output parameter in the routine func(). Be sure to read this information! 768d763cef2SBarry Smith 769d763cef2SBarry Smith The routine func() takes Mat * as the matrix arguments rather than Mat. 770d763cef2SBarry Smith This allows the matrix evaluation routine to replace A and/or B with a 77156335db2SHong Zhang completely new matrix structure (not just different matrix elements) 772d763cef2SBarry Smith when appropriate, for instance, if the nonzero structure is changing 773d763cef2SBarry Smith throughout the global iterations. 774d763cef2SBarry Smith 775d763cef2SBarry Smith Level: beginner 776d763cef2SBarry Smith 777d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 778d763cef2SBarry Smith 779ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction() 780d763cef2SBarry Smith 781d763cef2SBarry Smith @*/ 782089b2837SJed Brown PetscErrorCode TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx) 783d763cef2SBarry Smith { 784277b19d0SLisandro Dalcin PetscErrorCode ierr; 785089b2837SJed Brown SNES snes; 78624989b8cSPeter Brune DM dm; 78724989b8cSPeter Brune TSIJacobian ijacobian; 788277b19d0SLisandro Dalcin 789d763cef2SBarry Smith PetscFunctionBegin; 7900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 791277b19d0SLisandro Dalcin if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 792277b19d0SLisandro Dalcin if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 793277b19d0SLisandro Dalcin if (A) PetscCheckSameComm(ts,1,A,2); 794277b19d0SLisandro Dalcin if (B) PetscCheckSameComm(ts,1,B,3); 795d763cef2SBarry Smith 79624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 79724989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 79824989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr); 79924989b8cSPeter Brune 800089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 8015f659677SPeter Brune if (!ijacobian) { 802089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 8030e4ef248SJed Brown } 8040e4ef248SJed Brown if (A) { 8050e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 8060e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 8070e4ef248SJed Brown ts->Arhs = A; 8080e4ef248SJed Brown } 8090e4ef248SJed Brown if (B) { 8100e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 8110e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 8120e4ef248SJed Brown ts->Brhs = B; 8130e4ef248SJed Brown } 814d763cef2SBarry Smith PetscFunctionReturn(0); 815d763cef2SBarry Smith } 816d763cef2SBarry Smith 817316643e7SJed Brown 818316643e7SJed Brown #undef __FUNCT__ 819316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 820316643e7SJed Brown /*@C 821316643e7SJed Brown TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved. 822316643e7SJed Brown 8233f9fe445SBarry Smith Logically Collective on TS 824316643e7SJed Brown 825316643e7SJed Brown Input Parameters: 826316643e7SJed Brown + ts - the TS context obtained from TSCreate() 827ca94891dSJed Brown . r - vector to hold the residual (or PETSC_NULL to have it created internally) 828316643e7SJed Brown . f - the function evaluation routine 829316643e7SJed Brown - ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL) 830316643e7SJed Brown 831316643e7SJed Brown Calling sequence of f: 832316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 833316643e7SJed Brown 834316643e7SJed Brown + t - time at step/stage being solved 835316643e7SJed Brown . u - state vector 836316643e7SJed Brown . u_t - time derivative of state vector 837316643e7SJed Brown . F - function vector 838316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 839316643e7SJed Brown 840316643e7SJed Brown Important: 841d6cbdb99SBarry Smith The user MUST call either this routine, TSSetRHSFunction(). This routine must be used when not solving an ODE, for example a DAE. 842316643e7SJed Brown 843316643e7SJed Brown Level: beginner 844316643e7SJed Brown 845316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 846316643e7SJed Brown 847d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 848316643e7SJed Brown @*/ 849089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 850316643e7SJed Brown { 851089b2837SJed Brown PetscErrorCode ierr; 852089b2837SJed Brown SNES snes; 853e856ceecSJed Brown Vec resalloc = PETSC_NULL; 85424989b8cSPeter Brune DM dm; 855316643e7SJed Brown 856316643e7SJed Brown PetscFunctionBegin; 8570700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 858ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 85924989b8cSPeter Brune 86024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 86124989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 86224989b8cSPeter Brune 863089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 864e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 865e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 866e856ceecSJed Brown res = resalloc; 867e856ceecSJed Brown } 868089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 869e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 87024989b8cSPeter Brune 871089b2837SJed Brown PetscFunctionReturn(0); 872089b2837SJed Brown } 873089b2837SJed Brown 874089b2837SJed Brown #undef __FUNCT__ 875089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 876089b2837SJed Brown /*@C 877089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 878089b2837SJed Brown 879089b2837SJed Brown Not Collective 880089b2837SJed Brown 881089b2837SJed Brown Input Parameter: 882089b2837SJed Brown . ts - the TS context 883089b2837SJed Brown 884089b2837SJed Brown Output Parameter: 885089b2837SJed Brown + r - vector to hold residual (or PETSC_NULL) 886089b2837SJed Brown . func - the function to compute residual (or PETSC_NULL) 887089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 888089b2837SJed Brown 889089b2837SJed Brown Level: advanced 890089b2837SJed Brown 891089b2837SJed Brown .keywords: TS, nonlinear, get, function 892089b2837SJed Brown 893089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 894089b2837SJed Brown @*/ 895089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 896089b2837SJed Brown { 897089b2837SJed Brown PetscErrorCode ierr; 898089b2837SJed Brown SNES snes; 89924989b8cSPeter Brune DM dm; 900089b2837SJed Brown 901089b2837SJed Brown PetscFunctionBegin; 902089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 903089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 904089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 90524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 90624989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 907089b2837SJed Brown PetscFunctionReturn(0); 908089b2837SJed Brown } 909089b2837SJed Brown 910089b2837SJed Brown #undef __FUNCT__ 911089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 912089b2837SJed Brown /*@C 913089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 914089b2837SJed Brown 915089b2837SJed Brown Not Collective 916089b2837SJed Brown 917089b2837SJed Brown Input Parameter: 918089b2837SJed Brown . ts - the TS context 919089b2837SJed Brown 920089b2837SJed Brown Output Parameter: 921089b2837SJed Brown + r - vector to hold computed right hand side (or PETSC_NULL) 922089b2837SJed Brown . func - the function to compute right hand side (or PETSC_NULL) 923089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 924089b2837SJed Brown 925089b2837SJed Brown Level: advanced 926089b2837SJed Brown 927089b2837SJed Brown .keywords: TS, nonlinear, get, function 928089b2837SJed Brown 929089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 930089b2837SJed Brown @*/ 931089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 932089b2837SJed Brown { 933089b2837SJed Brown PetscErrorCode ierr; 934089b2837SJed Brown SNES snes; 93524989b8cSPeter Brune DM dm; 936089b2837SJed Brown 937089b2837SJed Brown PetscFunctionBegin; 938089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 939089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 940089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 94124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 94224989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 943316643e7SJed Brown PetscFunctionReturn(0); 944316643e7SJed Brown } 945316643e7SJed Brown 946316643e7SJed Brown #undef __FUNCT__ 947316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 948316643e7SJed Brown /*@C 949a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 950a4f0a591SBarry Smith you provided with TSSetIFunction(). 951316643e7SJed Brown 9523f9fe445SBarry Smith Logically Collective on TS 953316643e7SJed Brown 954316643e7SJed Brown Input Parameters: 955316643e7SJed Brown + ts - the TS context obtained from TSCreate() 956316643e7SJed Brown . A - Jacobian matrix 957316643e7SJed Brown . B - preconditioning matrix for A (may be same as A) 958316643e7SJed Brown . f - the Jacobian evaluation routine 959316643e7SJed Brown - ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL) 960316643e7SJed Brown 961316643e7SJed Brown Calling sequence of f: 9621b4a444bSJed Brown $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx); 963316643e7SJed Brown 964316643e7SJed Brown + t - time at step/stage being solved 9651b4a444bSJed Brown . U - state vector 9661b4a444bSJed Brown . U_t - time derivative of state vector 967316643e7SJed Brown . a - shift 9681b4a444bSJed Brown . A - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 969316643e7SJed Brown . B - preconditioning matrix for A, may be same as A 970316643e7SJed Brown . flag - flag indicating information about the preconditioner matrix 971316643e7SJed Brown structure (same as flag in KSPSetOperators()) 972316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 973316643e7SJed Brown 974316643e7SJed Brown Notes: 975316643e7SJed Brown The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve. 976316643e7SJed Brown 977a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 978a4f0a591SBarry 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. 979a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 980a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 981a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 982a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 983a4f0a591SBarry Smith 984316643e7SJed Brown Level: beginner 985316643e7SJed Brown 986316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 987316643e7SJed Brown 988ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian() 989316643e7SJed Brown 990316643e7SJed Brown @*/ 9917087cfbeSBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx) 992316643e7SJed Brown { 993316643e7SJed Brown PetscErrorCode ierr; 994089b2837SJed Brown SNES snes; 99524989b8cSPeter Brune DM dm; 996316643e7SJed Brown 997316643e7SJed Brown PetscFunctionBegin; 9980700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9990700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 10000700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 1001316643e7SJed Brown if (A) PetscCheckSameComm(ts,1,A,2); 1002316643e7SJed Brown if (B) PetscCheckSameComm(ts,1,B,3); 100324989b8cSPeter Brune 100424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 100524989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 100624989b8cSPeter Brune 1007089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1008089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1009316643e7SJed Brown PetscFunctionReturn(0); 1010316643e7SJed Brown } 1011316643e7SJed Brown 10124a2ae208SSatish Balay #undef __FUNCT__ 10134a2ae208SSatish Balay #define __FUNCT__ "TSView" 10147e2c5f70SBarry Smith /*@C 1015d763cef2SBarry Smith TSView - Prints the TS data structure. 1016d763cef2SBarry Smith 10174c49b128SBarry Smith Collective on TS 1018d763cef2SBarry Smith 1019d763cef2SBarry Smith Input Parameters: 1020d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1021d763cef2SBarry Smith - viewer - visualization context 1022d763cef2SBarry Smith 1023d763cef2SBarry Smith Options Database Key: 1024d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1025d763cef2SBarry Smith 1026d763cef2SBarry Smith Notes: 1027d763cef2SBarry Smith The available visualization contexts include 1028b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1029b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1030d763cef2SBarry Smith output where only the first processor opens 1031d763cef2SBarry Smith the file. All other processors send their 1032d763cef2SBarry Smith data to the first processor to print. 1033d763cef2SBarry Smith 1034d763cef2SBarry Smith The user can open an alternative visualization context with 1035b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1036d763cef2SBarry Smith 1037d763cef2SBarry Smith Level: beginner 1038d763cef2SBarry Smith 1039d763cef2SBarry Smith .keywords: TS, timestep, view 1040d763cef2SBarry Smith 1041b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1042d763cef2SBarry Smith @*/ 10437087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1044d763cef2SBarry Smith { 1045dfbe8321SBarry Smith PetscErrorCode ierr; 104619fd82e9SBarry Smith TSType type; 1047089b2837SJed Brown PetscBool iascii,isstring,isundials; 1048d763cef2SBarry Smith 1049d763cef2SBarry Smith PetscFunctionBegin; 10500700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10513050cee2SBarry Smith if (!viewer) { 10527adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr); 10533050cee2SBarry Smith } 10540700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1055c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1056fd16b177SBarry Smith 1057251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1058251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 105932077d6dSBarry Smith if (iascii) { 1060317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr); 106177431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 1062a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%G\n",ts->max_time);CHKERRQ(ierr); 1063d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 10645ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1065c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1066d763cef2SBarry Smith } 10675ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1068193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 1069d52bd9f3SBarry Smith if (ts->ops->view) { 1070d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1071d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1072d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1073d52bd9f3SBarry Smith } 10740f5bd95cSBarry Smith } else if (isstring) { 1075a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1076b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 1077d763cef2SBarry Smith } 1078b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1079251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1080b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1081d763cef2SBarry Smith PetscFunctionReturn(0); 1082d763cef2SBarry Smith } 1083d763cef2SBarry Smith 1084d763cef2SBarry Smith 10854a2ae208SSatish Balay #undef __FUNCT__ 10864a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1087b07ff414SBarry Smith /*@ 1088d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1089d763cef2SBarry Smith the timesteppers. 1090d763cef2SBarry Smith 10913f9fe445SBarry Smith Logically Collective on TS 1092d763cef2SBarry Smith 1093d763cef2SBarry Smith Input Parameters: 1094d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1095d763cef2SBarry Smith - usrP - optional user context 1096d763cef2SBarry Smith 1097d763cef2SBarry Smith Level: intermediate 1098d763cef2SBarry Smith 1099d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1100d763cef2SBarry Smith 1101d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1102d763cef2SBarry Smith @*/ 11037087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1104d763cef2SBarry Smith { 1105d763cef2SBarry Smith PetscFunctionBegin; 11060700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1107d763cef2SBarry Smith ts->user = usrP; 1108d763cef2SBarry Smith PetscFunctionReturn(0); 1109d763cef2SBarry Smith } 1110d763cef2SBarry Smith 11114a2ae208SSatish Balay #undef __FUNCT__ 11124a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1113b07ff414SBarry Smith /*@ 1114d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1115d763cef2SBarry Smith timestepper. 1116d763cef2SBarry Smith 1117d763cef2SBarry Smith Not Collective 1118d763cef2SBarry Smith 1119d763cef2SBarry Smith Input Parameter: 1120d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1121d763cef2SBarry Smith 1122d763cef2SBarry Smith Output Parameter: 1123d763cef2SBarry Smith . usrP - user context 1124d763cef2SBarry Smith 1125d763cef2SBarry Smith Level: intermediate 1126d763cef2SBarry Smith 1127d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1128d763cef2SBarry Smith 1129d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1130d763cef2SBarry Smith @*/ 1131e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1132d763cef2SBarry Smith { 1133d763cef2SBarry Smith PetscFunctionBegin; 11340700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1135e71120c6SJed Brown *(void**)usrP = ts->user; 1136d763cef2SBarry Smith PetscFunctionReturn(0); 1137d763cef2SBarry Smith } 1138d763cef2SBarry Smith 11394a2ae208SSatish Balay #undef __FUNCT__ 11404a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1141d763cef2SBarry Smith /*@ 1142b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1143d763cef2SBarry Smith 1144d763cef2SBarry Smith Not Collective 1145d763cef2SBarry Smith 1146d763cef2SBarry Smith Input Parameter: 1147d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1148d763cef2SBarry Smith 1149d763cef2SBarry Smith Output Parameter: 1150b8123daeSJed Brown . iter - number of steps completed so far 1151d763cef2SBarry Smith 1152d763cef2SBarry Smith Level: intermediate 1153d763cef2SBarry Smith 1154d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 1155b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep() 1156d763cef2SBarry Smith @*/ 11577087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt* iter) 1158d763cef2SBarry Smith { 1159d763cef2SBarry Smith PetscFunctionBegin; 11600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11614482741eSBarry Smith PetscValidIntPointer(iter,2); 1162d763cef2SBarry Smith *iter = ts->steps; 1163d763cef2SBarry Smith PetscFunctionReturn(0); 1164d763cef2SBarry Smith } 1165d763cef2SBarry Smith 11664a2ae208SSatish Balay #undef __FUNCT__ 11674a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1168d763cef2SBarry Smith /*@ 1169d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1170d763cef2SBarry Smith as well as the initial time. 1171d763cef2SBarry Smith 11723f9fe445SBarry Smith Logically Collective on TS 1173d763cef2SBarry Smith 1174d763cef2SBarry Smith Input Parameters: 1175d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1176d763cef2SBarry Smith . initial_time - the initial time 1177d763cef2SBarry Smith - time_step - the size of the timestep 1178d763cef2SBarry Smith 1179d763cef2SBarry Smith Level: intermediate 1180d763cef2SBarry Smith 1181d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1182d763cef2SBarry Smith 1183d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1184d763cef2SBarry Smith @*/ 11857087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1186d763cef2SBarry Smith { 1187e144a568SJed Brown PetscErrorCode ierr; 1188e144a568SJed Brown 1189d763cef2SBarry Smith PetscFunctionBegin; 11900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1191e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1192d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1193d763cef2SBarry Smith PetscFunctionReturn(0); 1194d763cef2SBarry Smith } 1195d763cef2SBarry Smith 11964a2ae208SSatish Balay #undef __FUNCT__ 11974a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1198d763cef2SBarry Smith /*@ 1199d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1200d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1201d763cef2SBarry Smith 12023f9fe445SBarry Smith Logically Collective on TS 1203d763cef2SBarry Smith 1204d763cef2SBarry Smith Input Parameters: 1205d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1206d763cef2SBarry Smith - time_step - the size of the timestep 1207d763cef2SBarry Smith 1208d763cef2SBarry Smith Level: intermediate 1209d763cef2SBarry Smith 1210d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1211d763cef2SBarry Smith 1212d763cef2SBarry Smith .keywords: TS, set, timestep 1213d763cef2SBarry Smith @*/ 12147087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1215d763cef2SBarry Smith { 1216d763cef2SBarry Smith PetscFunctionBegin; 12170700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1218c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1219d763cef2SBarry Smith ts->time_step = time_step; 122031748224SBarry Smith ts->time_step_orig = time_step; 1221d763cef2SBarry Smith PetscFunctionReturn(0); 1222d763cef2SBarry Smith } 1223d763cef2SBarry Smith 12244a2ae208SSatish Balay #undef __FUNCT__ 1225a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1226a43b19c4SJed Brown /*@ 1227a43b19c4SJed Brown TSSetExactFinalTime - Determines whether to interpolate solution to the 1228a43b19c4SJed Brown exact final time requested by the user or just returns it at the final time 1229a43b19c4SJed Brown it computed. 1230a43b19c4SJed Brown 1231a43b19c4SJed Brown Logically Collective on TS 1232a43b19c4SJed Brown 1233a43b19c4SJed Brown Input Parameter: 1234a43b19c4SJed Brown + ts - the time-step context 1235a43b19c4SJed Brown - ft - PETSC_TRUE if interpolates, else PETSC_FALSE 1236a43b19c4SJed Brown 1237a43b19c4SJed Brown Level: beginner 1238a43b19c4SJed Brown 1239a43b19c4SJed Brown .seealso: TSSetDuration() 1240a43b19c4SJed Brown @*/ 1241a43b19c4SJed Brown PetscErrorCode TSSetExactFinalTime(TS ts,PetscBool flg) 1242a43b19c4SJed Brown { 1243a43b19c4SJed Brown 1244a43b19c4SJed Brown PetscFunctionBegin; 1245a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1246d8cd7023SBarry Smith PetscValidLogicalCollectiveBool(ts,flg,2); 1247a43b19c4SJed Brown ts->exact_final_time = flg; 1248a43b19c4SJed Brown PetscFunctionReturn(0); 1249a43b19c4SJed Brown } 1250a43b19c4SJed Brown 1251a43b19c4SJed Brown #undef __FUNCT__ 12524a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1253d763cef2SBarry Smith /*@ 1254d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1255d763cef2SBarry Smith 1256d763cef2SBarry Smith Not Collective 1257d763cef2SBarry Smith 1258d763cef2SBarry Smith Input Parameter: 1259d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1260d763cef2SBarry Smith 1261d763cef2SBarry Smith Output Parameter: 1262d763cef2SBarry Smith . dt - the current timestep size 1263d763cef2SBarry Smith 1264d763cef2SBarry Smith Level: intermediate 1265d763cef2SBarry Smith 1266d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1267d763cef2SBarry Smith 1268d763cef2SBarry Smith .keywords: TS, get, timestep 1269d763cef2SBarry Smith @*/ 12707087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal* dt) 1271d763cef2SBarry Smith { 1272d763cef2SBarry Smith PetscFunctionBegin; 12730700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1274f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1275d763cef2SBarry Smith *dt = ts->time_step; 1276d763cef2SBarry Smith PetscFunctionReturn(0); 1277d763cef2SBarry Smith } 1278d763cef2SBarry Smith 12794a2ae208SSatish Balay #undef __FUNCT__ 12804a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1281d8e5e3e6SSatish Balay /*@ 1282d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1283d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1284d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1285d763cef2SBarry Smith the solution at the next timestep has been calculated. 1286d763cef2SBarry Smith 1287d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1288d763cef2SBarry Smith 1289d763cef2SBarry Smith Input Parameter: 1290d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1291d763cef2SBarry Smith 1292d763cef2SBarry Smith Output Parameter: 1293d763cef2SBarry Smith . v - the vector containing the solution 1294d763cef2SBarry Smith 1295d763cef2SBarry Smith Level: intermediate 1296d763cef2SBarry Smith 1297d763cef2SBarry Smith .seealso: TSGetTimeStep() 1298d763cef2SBarry Smith 1299d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1300d763cef2SBarry Smith @*/ 13017087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1302d763cef2SBarry Smith { 1303d763cef2SBarry Smith PetscFunctionBegin; 13040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13054482741eSBarry Smith PetscValidPointer(v,2); 13068737fe31SLisandro Dalcin *v = ts->vec_sol; 1307d763cef2SBarry Smith PetscFunctionReturn(0); 1308d763cef2SBarry Smith } 1309d763cef2SBarry Smith 1310bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 13114a2ae208SSatish Balay #undef __FUNCT__ 1312bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1313d8e5e3e6SSatish Balay /*@ 1314bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1315d763cef2SBarry Smith 1316bdad233fSMatthew Knepley Not collective 1317d763cef2SBarry Smith 1318bdad233fSMatthew Knepley Input Parameters: 1319bdad233fSMatthew Knepley + ts - The TS 1320bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1321d763cef2SBarry Smith .vb 1322d763cef2SBarry Smith U_t = A U 1323d763cef2SBarry Smith U_t = A(t) U 1324d763cef2SBarry Smith U_t = F(t,U) 1325d763cef2SBarry Smith .ve 1326d763cef2SBarry Smith 1327d763cef2SBarry Smith Level: beginner 1328d763cef2SBarry Smith 1329bdad233fSMatthew Knepley .keywords: TS, problem type 1330bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1331d763cef2SBarry Smith @*/ 13327087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1333a7cc72afSBarry Smith { 13349e2a6581SJed Brown PetscErrorCode ierr; 13359e2a6581SJed Brown 1336d763cef2SBarry Smith PetscFunctionBegin; 13370700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1338bdad233fSMatthew Knepley ts->problem_type = type; 13399e2a6581SJed Brown if (type == TS_LINEAR) { 13409e2a6581SJed Brown SNES snes; 13419e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13429e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 13439e2a6581SJed Brown } 1344d763cef2SBarry Smith PetscFunctionReturn(0); 1345d763cef2SBarry Smith } 1346d763cef2SBarry Smith 1347bdad233fSMatthew Knepley #undef __FUNCT__ 1348bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1349bdad233fSMatthew Knepley /*@C 1350bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1351bdad233fSMatthew Knepley 1352bdad233fSMatthew Knepley Not collective 1353bdad233fSMatthew Knepley 1354bdad233fSMatthew Knepley Input Parameter: 1355bdad233fSMatthew Knepley . ts - The TS 1356bdad233fSMatthew Knepley 1357bdad233fSMatthew Knepley Output Parameter: 1358bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1359bdad233fSMatthew Knepley .vb 1360089b2837SJed Brown M U_t = A U 1361089b2837SJed Brown M(t) U_t = A(t) U 1362bdad233fSMatthew Knepley U_t = F(t,U) 1363bdad233fSMatthew Knepley .ve 1364bdad233fSMatthew Knepley 1365bdad233fSMatthew Knepley Level: beginner 1366bdad233fSMatthew Knepley 1367bdad233fSMatthew Knepley .keywords: TS, problem type 1368bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1369bdad233fSMatthew Knepley @*/ 13707087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1371a7cc72afSBarry Smith { 1372bdad233fSMatthew Knepley PetscFunctionBegin; 13730700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 13744482741eSBarry Smith PetscValidIntPointer(type,2); 1375bdad233fSMatthew Knepley *type = ts->problem_type; 1376bdad233fSMatthew Knepley PetscFunctionReturn(0); 1377bdad233fSMatthew Knepley } 1378d763cef2SBarry Smith 13794a2ae208SSatish Balay #undef __FUNCT__ 13804a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1381d763cef2SBarry Smith /*@ 1382d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1383d763cef2SBarry Smith of a timestepper. 1384d763cef2SBarry Smith 1385d763cef2SBarry Smith Collective on TS 1386d763cef2SBarry Smith 1387d763cef2SBarry Smith Input Parameter: 1388d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1389d763cef2SBarry Smith 1390d763cef2SBarry Smith Notes: 1391d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1392d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1393d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1394d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1395d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1396d763cef2SBarry Smith 1397d763cef2SBarry Smith Level: advanced 1398d763cef2SBarry Smith 1399d763cef2SBarry Smith .keywords: TS, timestep, setup 1400d763cef2SBarry Smith 1401d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1402d763cef2SBarry Smith @*/ 14037087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1404d763cef2SBarry Smith { 1405dfbe8321SBarry Smith PetscErrorCode ierr; 14066c6b9e74SPeter Brune DM dm; 14076c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 14086c6b9e74SPeter Brune PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*); 14096c6b9e74SPeter Brune TSIJacobian ijac; 14106c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1411d763cef2SBarry Smith 1412d763cef2SBarry Smith PetscFunctionBegin; 14130700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1414277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1415277b19d0SLisandro Dalcin 14167adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 14179596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1418d763cef2SBarry Smith } 1419a43b19c4SJed Brown if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE; 1420277b19d0SLisandro Dalcin 1421277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1422277b19d0SLisandro Dalcin 14231c3436cfSJed Brown ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 14241c3436cfSJed Brown 1425277b19d0SLisandro Dalcin if (ts->ops->setup) { 1426000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1427277b19d0SLisandro Dalcin } 1428277b19d0SLisandro Dalcin 14296c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 14306c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 14316c6b9e74SPeter Brune */ 14326c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 14336c6b9e74SPeter Brune ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr); 14346c6b9e74SPeter Brune if (!func) { 14356c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 14366c6b9e74SPeter Brune } 14376c6b9e74SPeter 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. 14386c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 14396c6b9e74SPeter Brune */ 14406c6b9e74SPeter Brune ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr); 14416c6b9e74SPeter Brune ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr); 14426c6b9e74SPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr); 14436c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 14446c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 14456c6b9e74SPeter Brune } 1446277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1447277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1448277b19d0SLisandro Dalcin } 1449277b19d0SLisandro Dalcin 1450277b19d0SLisandro Dalcin #undef __FUNCT__ 1451277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1452277b19d0SLisandro Dalcin /*@ 1453277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1454277b19d0SLisandro Dalcin 1455277b19d0SLisandro Dalcin Collective on TS 1456277b19d0SLisandro Dalcin 1457277b19d0SLisandro Dalcin Input Parameter: 1458277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1459277b19d0SLisandro Dalcin 1460277b19d0SLisandro Dalcin Level: beginner 1461277b19d0SLisandro Dalcin 1462277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1463277b19d0SLisandro Dalcin 1464277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1465277b19d0SLisandro Dalcin @*/ 1466277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1467277b19d0SLisandro Dalcin { 1468277b19d0SLisandro Dalcin PetscErrorCode ierr; 1469277b19d0SLisandro Dalcin 1470277b19d0SLisandro Dalcin PetscFunctionBegin; 1471277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1472277b19d0SLisandro Dalcin if (ts->ops->reset) { 1473277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1474277b19d0SLisandro Dalcin } 1475277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 14764e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 14774e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1478214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 14796bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1480e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 1481e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 148238637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 1483277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1484d763cef2SBarry Smith PetscFunctionReturn(0); 1485d763cef2SBarry Smith } 1486d763cef2SBarry Smith 14874a2ae208SSatish Balay #undef __FUNCT__ 14884a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1489d8e5e3e6SSatish Balay /*@ 1490d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1491d763cef2SBarry Smith with TSCreate(). 1492d763cef2SBarry Smith 1493d763cef2SBarry Smith Collective on TS 1494d763cef2SBarry Smith 1495d763cef2SBarry Smith Input Parameter: 1496d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1497d763cef2SBarry Smith 1498d763cef2SBarry Smith Level: beginner 1499d763cef2SBarry Smith 1500d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1501d763cef2SBarry Smith 1502d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1503d763cef2SBarry Smith @*/ 15046bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1505d763cef2SBarry Smith { 15066849ba73SBarry Smith PetscErrorCode ierr; 1507d763cef2SBarry Smith 1508d763cef2SBarry Smith PetscFunctionBegin; 15096bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 15106bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 15116bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1512d763cef2SBarry Smith 15136bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1514277b19d0SLisandro Dalcin 1515be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 15166bf464f9SBarry Smith ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 15176bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 15186d4c513bSLisandro Dalcin 151984df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 15206bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 15216bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 15226bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 15236d4c513bSLisandro Dalcin 1524a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1525d763cef2SBarry Smith PetscFunctionReturn(0); 1526d763cef2SBarry Smith } 1527d763cef2SBarry Smith 15284a2ae208SSatish Balay #undef __FUNCT__ 15294a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1530d8e5e3e6SSatish Balay /*@ 1531d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1532d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1533d763cef2SBarry Smith 1534d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1535d763cef2SBarry Smith 1536d763cef2SBarry Smith Input Parameter: 1537d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1538d763cef2SBarry Smith 1539d763cef2SBarry Smith Output Parameter: 1540d763cef2SBarry Smith . snes - the nonlinear solver context 1541d763cef2SBarry Smith 1542d763cef2SBarry Smith Notes: 1543d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1544d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 154594b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1546d763cef2SBarry Smith 1547d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 1548d763cef2SBarry Smith this case TSGetSNES() returns PETSC_NULL in snes. 1549d763cef2SBarry Smith 1550d763cef2SBarry Smith Level: beginner 1551d763cef2SBarry Smith 1552d763cef2SBarry Smith .keywords: timestep, get, SNES 1553d763cef2SBarry Smith @*/ 15547087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1555d763cef2SBarry Smith { 1556d372ba47SLisandro Dalcin PetscErrorCode ierr; 1557d372ba47SLisandro Dalcin 1558d763cef2SBarry Smith PetscFunctionBegin; 15590700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15604482741eSBarry Smith PetscValidPointer(snes,2); 1561d372ba47SLisandro Dalcin if (!ts->snes) { 1562d372ba47SLisandro Dalcin ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 15636c6b9e74SPeter Brune ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 1564d372ba47SLisandro Dalcin ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1565d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 1566496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 15679e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 15689e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 15699e2a6581SJed Brown } 1570d372ba47SLisandro Dalcin } 1571d763cef2SBarry Smith *snes = ts->snes; 1572d763cef2SBarry Smith PetscFunctionReturn(0); 1573d763cef2SBarry Smith } 1574d763cef2SBarry Smith 15754a2ae208SSatish Balay #undef __FUNCT__ 157694b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 1577d8e5e3e6SSatish Balay /*@ 157894b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 1579d763cef2SBarry Smith a TS (timestepper) context. 1580d763cef2SBarry Smith 158194b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 1582d763cef2SBarry Smith 1583d763cef2SBarry Smith Input Parameter: 1584d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1585d763cef2SBarry Smith 1586d763cef2SBarry Smith Output Parameter: 158794b7f48cSBarry Smith . ksp - the nonlinear solver context 1588d763cef2SBarry Smith 1589d763cef2SBarry Smith Notes: 159094b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 1591d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 1592d763cef2SBarry Smith KSP and PC contexts as well. 1593d763cef2SBarry Smith 159494b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 159594b7f48cSBarry Smith in this case TSGetKSP() returns PETSC_NULL in ksp. 1596d763cef2SBarry Smith 1597d763cef2SBarry Smith Level: beginner 1598d763cef2SBarry Smith 159994b7f48cSBarry Smith .keywords: timestep, get, KSP 1600d763cef2SBarry Smith @*/ 16017087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1602d763cef2SBarry Smith { 1603d372ba47SLisandro Dalcin PetscErrorCode ierr; 1604089b2837SJed Brown SNES snes; 1605d372ba47SLisandro Dalcin 1606d763cef2SBarry Smith PetscFunctionBegin; 16070700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 16084482741eSBarry Smith PetscValidPointer(ksp,2); 160917186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1610e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1611089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1612089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1613d763cef2SBarry Smith PetscFunctionReturn(0); 1614d763cef2SBarry Smith } 1615d763cef2SBarry Smith 1616d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 1617d763cef2SBarry Smith 16184a2ae208SSatish Balay #undef __FUNCT__ 1619adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 1620adb62b0dSMatthew Knepley /*@ 1621adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 1622adb62b0dSMatthew Knepley maximum time for iteration. 1623adb62b0dSMatthew Knepley 16243f9fe445SBarry Smith Not Collective 1625adb62b0dSMatthew Knepley 1626adb62b0dSMatthew Knepley Input Parameters: 1627adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 1628adb62b0dSMatthew Knepley . maxsteps - maximum number of iterations to use, or PETSC_NULL 1629adb62b0dSMatthew Knepley - maxtime - final time to iterate to, or PETSC_NULL 1630adb62b0dSMatthew Knepley 1631adb62b0dSMatthew Knepley Level: intermediate 1632adb62b0dSMatthew Knepley 1633adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 1634adb62b0dSMatthew Knepley @*/ 16357087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1636adb62b0dSMatthew Knepley { 1637adb62b0dSMatthew Knepley PetscFunctionBegin; 16380700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1639abc0a331SBarry Smith if (maxsteps) { 16404482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 1641adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 1642adb62b0dSMatthew Knepley } 1643abc0a331SBarry Smith if (maxtime) { 16444482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 1645adb62b0dSMatthew Knepley *maxtime = ts->max_time; 1646adb62b0dSMatthew Knepley } 1647adb62b0dSMatthew Knepley PetscFunctionReturn(0); 1648adb62b0dSMatthew Knepley } 1649adb62b0dSMatthew Knepley 1650adb62b0dSMatthew Knepley #undef __FUNCT__ 16514a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 1652d763cef2SBarry Smith /*@ 1653d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 1654d763cef2SBarry Smith maximum time for iteration. 1655d763cef2SBarry Smith 16563f9fe445SBarry Smith Logically Collective on TS 1657d763cef2SBarry Smith 1658d763cef2SBarry Smith Input Parameters: 1659d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1660d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 1661d763cef2SBarry Smith - maxtime - final time to iterate to 1662d763cef2SBarry Smith 1663d763cef2SBarry Smith Options Database Keys: 1664d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 16653bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 1666d763cef2SBarry Smith 1667d763cef2SBarry Smith Notes: 1668d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 1669d763cef2SBarry Smith 1670d763cef2SBarry Smith Level: intermediate 1671d763cef2SBarry Smith 1672d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 1673a43b19c4SJed Brown 1674a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 1675d763cef2SBarry Smith @*/ 16767087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1677d763cef2SBarry Smith { 1678d763cef2SBarry Smith PetscFunctionBegin; 16790700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1680c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1681c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 168239b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 168339b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 1684d763cef2SBarry Smith PetscFunctionReturn(0); 1685d763cef2SBarry Smith } 1686d763cef2SBarry Smith 16874a2ae208SSatish Balay #undef __FUNCT__ 16884a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 1689d763cef2SBarry Smith /*@ 1690d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 1691d763cef2SBarry Smith for use by the TS routines. 1692d763cef2SBarry Smith 16933f9fe445SBarry Smith Logically Collective on TS and Vec 1694d763cef2SBarry Smith 1695d763cef2SBarry Smith Input Parameters: 1696d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1697d763cef2SBarry Smith - x - the solution vector 1698d763cef2SBarry Smith 1699d763cef2SBarry Smith Level: beginner 1700d763cef2SBarry Smith 1701d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 1702d763cef2SBarry Smith @*/ 17037087cfbeSBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec x) 1704d763cef2SBarry Smith { 17058737fe31SLisandro Dalcin PetscErrorCode ierr; 1706496e6a7aSJed Brown DM dm; 17078737fe31SLisandro Dalcin 1708d763cef2SBarry Smith PetscFunctionBegin; 17090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17100700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 17118737fe31SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 17126bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 17138737fe31SLisandro Dalcin ts->vec_sol = x; 1714496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1715496e6a7aSJed Brown ierr = DMShellSetGlobalVector(dm,x);CHKERRQ(ierr); 1716d763cef2SBarry Smith PetscFunctionReturn(0); 1717d763cef2SBarry Smith } 1718d763cef2SBarry Smith 1719e74ef692SMatthew Knepley #undef __FUNCT__ 1720e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 1721ac226902SBarry Smith /*@C 1722000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 17233f2090d5SJed Brown called once at the beginning of each time step. 1724000e7ae3SMatthew Knepley 17253f9fe445SBarry Smith Logically Collective on TS 1726000e7ae3SMatthew Knepley 1727000e7ae3SMatthew Knepley Input Parameters: 1728000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1729000e7ae3SMatthew Knepley - func - The function 1730000e7ae3SMatthew Knepley 1731000e7ae3SMatthew Knepley Calling sequence of func: 1732000e7ae3SMatthew Knepley . func (TS ts); 1733000e7ae3SMatthew Knepley 1734000e7ae3SMatthew Knepley Level: intermediate 1735000e7ae3SMatthew Knepley 1736b8123daeSJed Brown Note: 1737b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 1738b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 1739b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 1740b8123daeSJed Brown 1741000e7ae3SMatthew Knepley .keywords: TS, timestep 1742b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep() 1743000e7ae3SMatthew Knepley @*/ 17447087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1745000e7ae3SMatthew Knepley { 1746000e7ae3SMatthew Knepley PetscFunctionBegin; 17470700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1748000e7ae3SMatthew Knepley ts->ops->prestep = func; 1749000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1750000e7ae3SMatthew Knepley } 1751000e7ae3SMatthew Knepley 1752e74ef692SMatthew Knepley #undef __FUNCT__ 17533f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 175409ee8438SJed Brown /*@ 17553f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 17563f2090d5SJed Brown 17573f2090d5SJed Brown Collective on TS 17583f2090d5SJed Brown 17593f2090d5SJed Brown Input Parameters: 17603f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 17613f2090d5SJed Brown 17623f2090d5SJed Brown Notes: 17633f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 17643f2090d5SJed Brown so most users would not generally call this routine themselves. 17653f2090d5SJed Brown 17663f2090d5SJed Brown Level: developer 17673f2090d5SJed Brown 17683f2090d5SJed Brown .keywords: TS, timestep 1769b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep() 17703f2090d5SJed Brown @*/ 17717087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 17723f2090d5SJed Brown { 17733f2090d5SJed Brown PetscErrorCode ierr; 17743f2090d5SJed Brown 17753f2090d5SJed Brown PetscFunctionBegin; 17760700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 177772ac3e02SJed Brown if (ts->ops->prestep) { 17783f2090d5SJed Brown PetscStackPush("TS PreStep function"); 17793f2090d5SJed Brown ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 17803f2090d5SJed Brown PetscStackPop; 1781312ce896SJed Brown } 17823f2090d5SJed Brown PetscFunctionReturn(0); 17833f2090d5SJed Brown } 17843f2090d5SJed Brown 17853f2090d5SJed Brown #undef __FUNCT__ 1786b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 1787b8123daeSJed Brown /*@C 1788b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 1789b8123daeSJed Brown called once at the beginning of each stage. 1790b8123daeSJed Brown 1791b8123daeSJed Brown Logically Collective on TS 1792b8123daeSJed Brown 1793b8123daeSJed Brown Input Parameters: 1794b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 1795b8123daeSJed Brown - func - The function 1796b8123daeSJed Brown 1797b8123daeSJed Brown Calling sequence of func: 1798b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 1799b8123daeSJed Brown 1800b8123daeSJed Brown Level: intermediate 1801b8123daeSJed Brown 1802b8123daeSJed Brown Note: 1803b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 1804b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 1805b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 1806b8123daeSJed Brown 1807b8123daeSJed Brown .keywords: TS, timestep 1808b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 1809b8123daeSJed Brown @*/ 1810b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 1811b8123daeSJed Brown { 1812b8123daeSJed Brown PetscFunctionBegin; 1813b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1814b8123daeSJed Brown ts->ops->prestage = func; 1815b8123daeSJed Brown PetscFunctionReturn(0); 1816b8123daeSJed Brown } 1817b8123daeSJed Brown 1818b8123daeSJed Brown #undef __FUNCT__ 1819b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 1820b8123daeSJed Brown /*@ 1821b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 1822b8123daeSJed Brown 1823b8123daeSJed Brown Collective on TS 1824b8123daeSJed Brown 1825b8123daeSJed Brown Input Parameters: 1826b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 1827b8123daeSJed Brown 1828b8123daeSJed Brown Notes: 1829b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 1830b8123daeSJed Brown most users would not generally call this routine themselves. 1831b8123daeSJed Brown 1832b8123daeSJed Brown Level: developer 1833b8123daeSJed Brown 1834b8123daeSJed Brown .keywords: TS, timestep 1835b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep() 1836b8123daeSJed Brown @*/ 1837b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 1838b8123daeSJed Brown { 1839b8123daeSJed Brown PetscErrorCode ierr; 1840b8123daeSJed Brown 1841b8123daeSJed Brown PetscFunctionBegin; 1842b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1843b8123daeSJed Brown if (ts->ops->prestage) { 1844b8123daeSJed Brown PetscStackPush("TS PreStage function"); 1845b8123daeSJed Brown ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr); 1846b8123daeSJed Brown PetscStackPop; 1847b8123daeSJed Brown } 1848b8123daeSJed Brown PetscFunctionReturn(0); 1849b8123daeSJed Brown } 1850b8123daeSJed Brown 1851b8123daeSJed Brown #undef __FUNCT__ 1852e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 1853ac226902SBarry Smith /*@C 1854000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 18553f2090d5SJed Brown called once at the end of each time step. 1856000e7ae3SMatthew Knepley 18573f9fe445SBarry Smith Logically Collective on TS 1858000e7ae3SMatthew Knepley 1859000e7ae3SMatthew Knepley Input Parameters: 1860000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1861000e7ae3SMatthew Knepley - func - The function 1862000e7ae3SMatthew Knepley 1863000e7ae3SMatthew Knepley Calling sequence of func: 1864b8123daeSJed Brown $ func (TS ts); 1865000e7ae3SMatthew Knepley 1866000e7ae3SMatthew Knepley Level: intermediate 1867000e7ae3SMatthew Knepley 1868000e7ae3SMatthew Knepley .keywords: TS, timestep 1869b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 1870000e7ae3SMatthew Knepley @*/ 18717087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 1872000e7ae3SMatthew Knepley { 1873000e7ae3SMatthew Knepley PetscFunctionBegin; 18740700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1875000e7ae3SMatthew Knepley ts->ops->poststep = func; 1876000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1877000e7ae3SMatthew Knepley } 1878000e7ae3SMatthew Knepley 1879e74ef692SMatthew Knepley #undef __FUNCT__ 18803f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 188109ee8438SJed Brown /*@ 18823f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 18833f2090d5SJed Brown 18843f2090d5SJed Brown Collective on TS 18853f2090d5SJed Brown 18863f2090d5SJed Brown Input Parameters: 18873f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 18883f2090d5SJed Brown 18893f2090d5SJed Brown Notes: 18903f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 18913f2090d5SJed Brown so most users would not generally call this routine themselves. 18923f2090d5SJed Brown 18933f2090d5SJed Brown Level: developer 18943f2090d5SJed Brown 18953f2090d5SJed Brown .keywords: TS, timestep 18963f2090d5SJed Brown @*/ 18977087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 18983f2090d5SJed Brown { 18993f2090d5SJed Brown PetscErrorCode ierr; 19003f2090d5SJed Brown 19013f2090d5SJed Brown PetscFunctionBegin; 19020700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 190372ac3e02SJed Brown if (ts->ops->poststep) { 19043f2090d5SJed Brown PetscStackPush("TS PostStep function"); 19053f2090d5SJed Brown ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 19063f2090d5SJed Brown PetscStackPop; 190772ac3e02SJed Brown } 19083f2090d5SJed Brown PetscFunctionReturn(0); 19093f2090d5SJed Brown } 19103f2090d5SJed Brown 1911d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 1912d763cef2SBarry Smith 19134a2ae208SSatish Balay #undef __FUNCT__ 1914a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 1915d763cef2SBarry Smith /*@C 1916a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 1917d763cef2SBarry Smith timestep to display the iteration's progress. 1918d763cef2SBarry Smith 19193f9fe445SBarry Smith Logically Collective on TS 1920d763cef2SBarry Smith 1921d763cef2SBarry Smith Input Parameters: 1922d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1923e213d8f1SJed Brown . monitor - monitoring routine 1924329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 1925b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desired) 1926b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1927b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 1928d763cef2SBarry Smith 1929e213d8f1SJed Brown Calling sequence of monitor: 1930e213d8f1SJed Brown $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx) 1931d763cef2SBarry Smith 1932d763cef2SBarry Smith + ts - the TS context 1933d763cef2SBarry Smith . steps - iteration number 19341f06c33eSBarry Smith . time - current time 1935d763cef2SBarry Smith . x - current iterate 1936d763cef2SBarry Smith - mctx - [optional] monitoring context 1937d763cef2SBarry Smith 1938d763cef2SBarry Smith Notes: 1939d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 1940d763cef2SBarry Smith already has been loaded. 1941d763cef2SBarry Smith 1942025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 1943025f1a04SBarry Smith 1944d763cef2SBarry Smith Level: intermediate 1945d763cef2SBarry Smith 1946d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1947d763cef2SBarry Smith 1948a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 1949d763cef2SBarry Smith @*/ 1950c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 1951d763cef2SBarry Smith { 1952d763cef2SBarry Smith PetscFunctionBegin; 19530700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 195417186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1955d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 19568704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 1957d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 1958d763cef2SBarry Smith PetscFunctionReturn(0); 1959d763cef2SBarry Smith } 1960d763cef2SBarry Smith 19614a2ae208SSatish Balay #undef __FUNCT__ 1962a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 1963d763cef2SBarry Smith /*@C 1964a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 1965d763cef2SBarry Smith 19663f9fe445SBarry Smith Logically Collective on TS 1967d763cef2SBarry Smith 1968d763cef2SBarry Smith Input Parameters: 1969d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1970d763cef2SBarry Smith 1971d763cef2SBarry Smith Notes: 1972d763cef2SBarry Smith There is no way to remove a single, specific monitor. 1973d763cef2SBarry Smith 1974d763cef2SBarry Smith Level: intermediate 1975d763cef2SBarry Smith 1976d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1977d763cef2SBarry Smith 1978a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 1979d763cef2SBarry Smith @*/ 19807087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 1981d763cef2SBarry Smith { 1982d952e501SBarry Smith PetscErrorCode ierr; 1983d952e501SBarry Smith PetscInt i; 1984d952e501SBarry Smith 1985d763cef2SBarry Smith PetscFunctionBegin; 19860700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1987d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 19888704b422SBarry Smith if (ts->monitordestroy[i]) { 19898704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 1990d952e501SBarry Smith } 1991d952e501SBarry Smith } 1992d763cef2SBarry Smith ts->numbermonitors = 0; 1993d763cef2SBarry Smith PetscFunctionReturn(0); 1994d763cef2SBarry Smith } 1995d763cef2SBarry Smith 19964a2ae208SSatish Balay #undef __FUNCT__ 1997a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 1998d8e5e3e6SSatish Balay /*@ 1999a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 20005516499fSSatish Balay 20015516499fSSatish Balay Level: intermediate 200241251cbbSSatish Balay 20035516499fSSatish Balay .keywords: TS, set, monitor 20045516499fSSatish Balay 200541251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 200641251cbbSSatish Balay @*/ 2007649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 2008d763cef2SBarry Smith { 2009dfbe8321SBarry Smith PetscErrorCode ierr; 2010649052a6SBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm); 2011d132466eSBarry Smith 2012d763cef2SBarry Smith PetscFunctionBegin; 2013649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2014971832b6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr); 2015649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2016d763cef2SBarry Smith PetscFunctionReturn(0); 2017d763cef2SBarry Smith } 2018d763cef2SBarry Smith 20194a2ae208SSatish Balay #undef __FUNCT__ 2020cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 2021cd652676SJed Brown /*@ 2022cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 2023cd652676SJed Brown 2024cd652676SJed Brown Logically Collective on TS 2025cd652676SJed Brown 2026cd652676SJed Brown Input Argument: 2027cd652676SJed Brown . ts - time stepping context 2028cd652676SJed Brown 2029cd652676SJed Brown Output Argument: 2030cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 2031cd652676SJed Brown 2032cd652676SJed Brown Level: intermediate 2033cd652676SJed Brown 2034cd652676SJed Brown .keywords: TS, set 2035cd652676SJed Brown 2036cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 2037cd652676SJed Brown @*/ 2038cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 2039cd652676SJed Brown { 2040cd652676SJed Brown 2041cd652676SJed Brown PetscFunctionBegin; 2042cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2043cd652676SJed Brown ts->retain_stages = flg; 2044cd652676SJed Brown PetscFunctionReturn(0); 2045cd652676SJed Brown } 2046cd652676SJed Brown 2047cd652676SJed Brown #undef __FUNCT__ 2048cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 2049cd652676SJed Brown /*@ 2050cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 2051cd652676SJed Brown 2052cd652676SJed Brown Collective on TS 2053cd652676SJed Brown 2054cd652676SJed Brown Input Argument: 2055cd652676SJed Brown + ts - time stepping context 2056cd652676SJed Brown - t - time to interpolate to 2057cd652676SJed Brown 2058cd652676SJed Brown Output Argument: 2059cd652676SJed Brown . X - state at given time 2060cd652676SJed Brown 2061cd652676SJed Brown Notes: 2062cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 2063cd652676SJed Brown 2064cd652676SJed Brown Level: intermediate 2065cd652676SJed Brown 2066cd652676SJed Brown Developer Notes: 2067cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 2068cd652676SJed Brown 2069cd652676SJed Brown .keywords: TS, set 2070cd652676SJed Brown 2071cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 2072cd652676SJed Brown @*/ 2073cd652676SJed Brown PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec X) 2074cd652676SJed Brown { 2075cd652676SJed Brown PetscErrorCode ierr; 2076cd652676SJed Brown 2077cd652676SJed Brown PetscFunctionBegin; 2078cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2079d8cd7023SBarry 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); 2080cd652676SJed Brown if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 2081cd652676SJed Brown ierr = (*ts->ops->interpolate)(ts,t,X);CHKERRQ(ierr); 2082cd652676SJed Brown PetscFunctionReturn(0); 2083cd652676SJed Brown } 2084cd652676SJed Brown 2085cd652676SJed Brown #undef __FUNCT__ 20864a2ae208SSatish Balay #define __FUNCT__ "TSStep" 2087d763cef2SBarry Smith /*@ 20886d9e5789SSean Farley TSStep - Steps one time step 2089d763cef2SBarry Smith 2090d763cef2SBarry Smith Collective on TS 2091d763cef2SBarry Smith 2092d763cef2SBarry Smith Input Parameter: 2093d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2094d763cef2SBarry Smith 20956d9e5789SSean Farley Level: intermediate 2096d763cef2SBarry Smith 2097b8123daeSJed Brown Notes: 2098b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 2099b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 2100b8123daeSJed Brown 2101d763cef2SBarry Smith .keywords: TS, timestep, solve 2102d763cef2SBarry Smith 2103b8123daeSJed Brown .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage() 2104d763cef2SBarry Smith @*/ 2105193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 2106d763cef2SBarry Smith { 2107362cd11cSLisandro Dalcin PetscReal ptime_prev; 2108dfbe8321SBarry Smith PetscErrorCode ierr; 2109d763cef2SBarry Smith 2110d763cef2SBarry Smith PetscFunctionBegin; 21110700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2112d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 2113d405a339SMatthew Knepley 2114362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 2115362cd11cSLisandro Dalcin 2116362cd11cSLisandro Dalcin ptime_prev = ts->ptime; 2117d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2118193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 2119d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2120362cd11cSLisandro Dalcin ts->time_step_prev = ts->ptime - ptime_prev; 2121362cd11cSLisandro Dalcin 2122362cd11cSLisandro Dalcin if (ts->reason < 0) { 2123cef5090cSJed Brown if (ts->errorifstepfailed) { 2124cef5090cSJed Brown if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) { 2125cef5090cSJed 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]); 2126cef5090cSJed Brown } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 2127cef5090cSJed Brown } 2128362cd11cSLisandro Dalcin } else if (!ts->reason) { 2129362cd11cSLisandro Dalcin if (ts->steps >= ts->max_steps) 2130362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITS; 2131362cd11cSLisandro Dalcin else if (ts->ptime >= ts->max_time) 2132362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_TIME; 2133362cd11cSLisandro Dalcin } 2134362cd11cSLisandro Dalcin 2135d763cef2SBarry Smith PetscFunctionReturn(0); 2136d763cef2SBarry Smith } 2137d763cef2SBarry Smith 21384a2ae208SSatish Balay #undef __FUNCT__ 213905175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 214005175c85SJed Brown /*@ 214105175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 214205175c85SJed Brown 21431c3436cfSJed Brown Collective on TS 214405175c85SJed Brown 214505175c85SJed Brown Input Arguments: 21461c3436cfSJed Brown + ts - time stepping context 21471c3436cfSJed Brown . order - desired order of accuracy 21481c3436cfSJed Brown - done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available) 214905175c85SJed Brown 215005175c85SJed Brown Output Arguments: 21511c3436cfSJed Brown . X - state at the end of the current step 215205175c85SJed Brown 215305175c85SJed Brown Level: advanced 215405175c85SJed Brown 2155108c343cSJed Brown Notes: 2156108c343cSJed Brown This function cannot be called until all stages have been evaluated. 2157108c343cSJed 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. 2158108c343cSJed Brown 21591c3436cfSJed Brown .seealso: TSStep(), TSAdapt 216005175c85SJed Brown @*/ 21611c3436cfSJed Brown PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec X,PetscBool *done) 216205175c85SJed Brown { 216305175c85SJed Brown PetscErrorCode ierr; 216405175c85SJed Brown 216505175c85SJed Brown PetscFunctionBegin; 216605175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 216705175c85SJed Brown PetscValidType(ts,1); 216805175c85SJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,3); 21691c3436cfSJed Brown if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 21701c3436cfSJed Brown ierr = (*ts->ops->evaluatestep)(ts,order,X,done);CHKERRQ(ierr); 217105175c85SJed Brown PetscFunctionReturn(0); 217205175c85SJed Brown } 217305175c85SJed Brown 217405175c85SJed Brown #undef __FUNCT__ 21756a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 21766a4d4014SLisandro Dalcin /*@ 21776a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 21786a4d4014SLisandro Dalcin 21796a4d4014SLisandro Dalcin Collective on TS 21806a4d4014SLisandro Dalcin 21816a4d4014SLisandro Dalcin Input Parameter: 21826a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 2183362cd11cSLisandro Dalcin - x - the solution vector 21846a4d4014SLisandro Dalcin 21855a3a76d0SJed Brown Output Parameter: 21865a3a76d0SJed Brown . ftime - time of the state vector x upon completion 21875a3a76d0SJed Brown 21886a4d4014SLisandro Dalcin Level: beginner 21896a4d4014SLisandro Dalcin 21905a3a76d0SJed Brown Notes: 21915a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 21925a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 21935a3a76d0SJed Brown stepped over the final time. 21945a3a76d0SJed Brown 21956a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 21966a4d4014SLisandro Dalcin 21976a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 21986a4d4014SLisandro Dalcin @*/ 21995a3a76d0SJed Brown PetscErrorCode TSSolve(TS ts,Vec x,PetscReal *ftime) 22006a4d4014SLisandro Dalcin { 22014d7d938eSLisandro Dalcin PetscBool flg; 22024d7d938eSLisandro Dalcin char filename[PETSC_MAX_PATH_LEN]; 22034d7d938eSLisandro Dalcin PetscViewer viewer; 22046a4d4014SLisandro Dalcin PetscErrorCode ierr; 2205f22f69f0SBarry Smith 22066a4d4014SLisandro Dalcin PetscFunctionBegin; 22070700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2208193ac0bcSJed Brown PetscValidHeaderSpecific(x,VEC_CLASSID,2); 22095a3a76d0SJed Brown if (ts->exact_final_time) { /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */ 22105a3a76d0SJed Brown if (!ts->vec_sol || x == ts->vec_sol) { 22115a3a76d0SJed Brown Vec y; 22125a3a76d0SJed Brown ierr = VecDuplicate(x,&y);CHKERRQ(ierr); 22135a3a76d0SJed Brown ierr = TSSetSolution(ts,y);CHKERRQ(ierr); 22145a3a76d0SJed Brown ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */ 22155a3a76d0SJed Brown } 2216362cd11cSLisandro Dalcin ierr = VecCopy(x,ts->vec_sol);CHKERRQ(ierr); 22175a3a76d0SJed Brown } else { 2218193ac0bcSJed Brown ierr = TSSetSolution(ts,x);CHKERRQ(ierr); 22195a3a76d0SJed Brown } 2220b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 22216a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 2222193ac0bcSJed Brown ts->steps = 0; 22235ef26d82SJed Brown ts->ksp_its = 0; 22245ef26d82SJed Brown ts->snes_its = 0; 2225c610991cSLisandro Dalcin ts->num_snes_failures = 0; 2226c610991cSLisandro Dalcin ts->reject = 0; 2227193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 2228193ac0bcSJed Brown 2229193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 2230193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 22315a3a76d0SJed Brown ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr); 2232a5b82c69SSean Farley if (ftime) *ftime = ts->ptime; 2233193ac0bcSJed Brown } else { 22346a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 2235362cd11cSLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2236362cd11cSLisandro Dalcin if (ts->steps >= ts->max_steps) 2237362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITS; 2238362cd11cSLisandro Dalcin else if (ts->ptime >= ts->max_time) 2239362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_TIME; 2240e1a7a14fSJed Brown while (!ts->reason) { 2241193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 2242193ac0bcSJed Brown ierr = TSPostStep(ts);CHKERRQ(ierr); 2243193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2244193ac0bcSJed Brown } 2245362cd11cSLisandro Dalcin if (ts->exact_final_time && ts->ptime > ts->max_time) { 2246a43b19c4SJed Brown ierr = TSInterpolate(ts,ts->max_time,x);CHKERRQ(ierr); 22475a3a76d0SJed Brown if (ftime) *ftime = ts->max_time; 22480574a7fbSJed Brown } else { 22490574a7fbSJed Brown ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr); 22500574a7fbSJed Brown if (ftime) *ftime = ts->ptime; 22510574a7fbSJed Brown } 2252193ac0bcSJed Brown } 22533923b477SBarry Smith ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 22544d7d938eSLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 22554d7d938eSLisandro Dalcin if (flg && !PetscPreLoadingOn) { 22564d7d938eSLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr); 22574d7d938eSLisandro Dalcin ierr = TSView(ts,viewer);CHKERRQ(ierr); 22584d7d938eSLisandro Dalcin ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 2259193ac0bcSJed Brown } 22606a4d4014SLisandro Dalcin PetscFunctionReturn(0); 22616a4d4014SLisandro Dalcin } 22626a4d4014SLisandro Dalcin 22636a4d4014SLisandro Dalcin #undef __FUNCT__ 22644a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 2265228d79bcSJed Brown /*@ 2266228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 2267228d79bcSJed Brown 2268228d79bcSJed Brown Collective on TS 2269228d79bcSJed Brown 2270228d79bcSJed Brown Input Parameters: 2271228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 2272228d79bcSJed Brown . step - step number that has just completed 2273228d79bcSJed Brown . ptime - model time of the state 2274228d79bcSJed Brown - x - state at the current model time 2275228d79bcSJed Brown 2276228d79bcSJed Brown Notes: 2277228d79bcSJed Brown TSMonitor() is typically used within the time stepping implementations. 2278228d79bcSJed Brown Users might call this function when using the TSStep() interface instead of TSSolve(). 2279228d79bcSJed Brown 2280228d79bcSJed Brown Level: advanced 2281228d79bcSJed Brown 2282228d79bcSJed Brown .keywords: TS, timestep 2283228d79bcSJed Brown @*/ 2284a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x) 2285d763cef2SBarry Smith { 22866849ba73SBarry Smith PetscErrorCode ierr; 2287a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 2288d763cef2SBarry Smith 2289d763cef2SBarry Smith PetscFunctionBegin; 2290d763cef2SBarry Smith for (i=0; i<n; i++) { 2291142b95e3SSatish Balay ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr); 2292d763cef2SBarry Smith } 2293d763cef2SBarry Smith PetscFunctionReturn(0); 2294d763cef2SBarry Smith } 2295d763cef2SBarry Smith 2296d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 22970b039ecaSBarry Smith struct _n_TSMonitorLGCtx { 22980b039ecaSBarry Smith PetscDrawLG lg; 22990b039ecaSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 2300*201da799SBarry Smith PetscInt ksp_its,snes_its; 23010b039ecaSBarry Smith }; 23020b039ecaSBarry Smith 2303d763cef2SBarry Smith 23044a2ae208SSatish Balay #undef __FUNCT__ 2305a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 2306d763cef2SBarry Smith /*@C 2307a9f9c1f6SBarry Smith TSMonitorLGCtxCreate - Creates a line graph context for use with 2308a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 2309d763cef2SBarry Smith 2310d763cef2SBarry Smith Collective on TS 2311d763cef2SBarry Smith 2312d763cef2SBarry Smith Input Parameters: 2313d763cef2SBarry Smith + host - the X display to open, or null for the local machine 2314d763cef2SBarry Smith . label - the title to put in the title bar 23157c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 2316a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 2317a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 2318d763cef2SBarry Smith 2319d763cef2SBarry Smith Output Parameter: 23200b039ecaSBarry Smith . ctx - the context 2321d763cef2SBarry Smith 2322d763cef2SBarry Smith Options Database Key: 23234f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 23244f09c107SBarry Smith . -ts_monitor_lg_solution - 23254f09c107SBarry Smith - -ts_monitor_lg_error - 2326d763cef2SBarry Smith 2327d763cef2SBarry Smith Notes: 2328a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 2329d763cef2SBarry Smith 2330d763cef2SBarry Smith Level: intermediate 2331d763cef2SBarry Smith 23327c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 2333d763cef2SBarry Smith 23344f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 23357c922b88SBarry Smith 2336d763cef2SBarry Smith @*/ 2337a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 2338d763cef2SBarry Smith { 2339b0a32e0cSBarry Smith PetscDraw win; 2340dfbe8321SBarry Smith PetscErrorCode ierr; 2341d763cef2SBarry Smith 2342d763cef2SBarry Smith PetscFunctionBegin; 2343*201da799SBarry Smith ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr); 2344a80ad3e0SBarry Smith ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr); 2345b0a32e0cSBarry Smith ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr); 23460b039ecaSBarry Smith ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr); 23470b039ecaSBarry Smith ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr); 23480b039ecaSBarry Smith ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr); 2349a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 2350d763cef2SBarry Smith PetscFunctionReturn(0); 2351d763cef2SBarry Smith } 2352d763cef2SBarry Smith 23534a2ae208SSatish Balay #undef __FUNCT__ 23544f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 23554f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 2356d763cef2SBarry Smith { 23570b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 2358c32365f1SBarry Smith PetscReal x = ptime,y; 2359dfbe8321SBarry Smith PetscErrorCode ierr; 2360d763cef2SBarry Smith 2361d763cef2SBarry Smith PetscFunctionBegin; 2362a9f9c1f6SBarry Smith if (!n) { 2363a9f9c1f6SBarry Smith PetscDrawAxis axis; 2364a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 2365a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 2366a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 2367a9f9c1f6SBarry Smith } 2368c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 23690b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 23703923b477SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))){ 23710b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 23723923b477SBarry Smith } 2373d763cef2SBarry Smith PetscFunctionReturn(0); 2374d763cef2SBarry Smith } 2375d763cef2SBarry Smith 23764a2ae208SSatish Balay #undef __FUNCT__ 2377a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 2378d763cef2SBarry Smith /*@C 2379a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 2380a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 2381d763cef2SBarry Smith 23820b039ecaSBarry Smith Collective on TSMonitorLGCtx 2383d763cef2SBarry Smith 2384d763cef2SBarry Smith Input Parameter: 23850b039ecaSBarry Smith . ctx - the monitor context 2386d763cef2SBarry Smith 2387d763cef2SBarry Smith Level: intermediate 2388d763cef2SBarry Smith 2389d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 2390d763cef2SBarry Smith 23914f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 2392d763cef2SBarry Smith @*/ 2393a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 2394d763cef2SBarry Smith { 2395b0a32e0cSBarry Smith PetscDraw draw; 2396dfbe8321SBarry Smith PetscErrorCode ierr; 2397d763cef2SBarry Smith 2398d763cef2SBarry Smith PetscFunctionBegin; 23990b039ecaSBarry Smith ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr); 24006bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 24010b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 24020b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 2403d763cef2SBarry Smith PetscFunctionReturn(0); 2404d763cef2SBarry Smith } 2405d763cef2SBarry Smith 24064a2ae208SSatish Balay #undef __FUNCT__ 24074a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 2408d763cef2SBarry Smith /*@ 2409b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 2410d763cef2SBarry Smith 2411d763cef2SBarry Smith Not Collective 2412d763cef2SBarry Smith 2413d763cef2SBarry Smith Input Parameter: 2414d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2415d763cef2SBarry Smith 2416d763cef2SBarry Smith Output Parameter: 2417d763cef2SBarry Smith . t - the current time 2418d763cef2SBarry Smith 2419d763cef2SBarry Smith Level: beginner 2420d763cef2SBarry Smith 2421b8123daeSJed Brown Note: 2422b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 2423b8123daeSJed Brown TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 2424b8123daeSJed Brown 2425d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2426d763cef2SBarry Smith 2427d763cef2SBarry Smith .keywords: TS, get, time 2428d763cef2SBarry Smith @*/ 24297087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal* t) 2430d763cef2SBarry Smith { 2431d763cef2SBarry Smith PetscFunctionBegin; 24320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2433f7cf8827SBarry Smith PetscValidRealPointer(t,2); 2434d763cef2SBarry Smith *t = ts->ptime; 2435d763cef2SBarry Smith PetscFunctionReturn(0); 2436d763cef2SBarry Smith } 2437d763cef2SBarry Smith 24384a2ae208SSatish Balay #undef __FUNCT__ 24396a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 24406a4d4014SLisandro Dalcin /*@ 24416a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 24426a4d4014SLisandro Dalcin 24433f9fe445SBarry Smith Logically Collective on TS 24446a4d4014SLisandro Dalcin 24456a4d4014SLisandro Dalcin Input Parameters: 24466a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 24476a4d4014SLisandro Dalcin - time - the time 24486a4d4014SLisandro Dalcin 24496a4d4014SLisandro Dalcin Level: intermediate 24506a4d4014SLisandro Dalcin 24516a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 24526a4d4014SLisandro Dalcin 24536a4d4014SLisandro Dalcin .keywords: TS, set, time 24546a4d4014SLisandro Dalcin @*/ 24557087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 24566a4d4014SLisandro Dalcin { 24576a4d4014SLisandro Dalcin PetscFunctionBegin; 24580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2459c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 24606a4d4014SLisandro Dalcin ts->ptime = t; 24616a4d4014SLisandro Dalcin PetscFunctionReturn(0); 24626a4d4014SLisandro Dalcin } 24636a4d4014SLisandro Dalcin 24646a4d4014SLisandro Dalcin #undef __FUNCT__ 24654a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 2466d763cef2SBarry Smith /*@C 2467d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 2468d763cef2SBarry Smith TS options in the database. 2469d763cef2SBarry Smith 24703f9fe445SBarry Smith Logically Collective on TS 2471d763cef2SBarry Smith 2472d763cef2SBarry Smith Input Parameter: 2473d763cef2SBarry Smith + ts - The TS context 2474d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2475d763cef2SBarry Smith 2476d763cef2SBarry Smith Notes: 2477d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2478d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2479d763cef2SBarry Smith hyphen. 2480d763cef2SBarry Smith 2481d763cef2SBarry Smith Level: advanced 2482d763cef2SBarry Smith 2483d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 2484d763cef2SBarry Smith 2485d763cef2SBarry Smith .seealso: TSSetFromOptions() 2486d763cef2SBarry Smith 2487d763cef2SBarry Smith @*/ 24887087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 2489d763cef2SBarry Smith { 2490dfbe8321SBarry Smith PetscErrorCode ierr; 2491089b2837SJed Brown SNES snes; 2492d763cef2SBarry Smith 2493d763cef2SBarry Smith PetscFunctionBegin; 24940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2495d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2496089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2497089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2498d763cef2SBarry Smith PetscFunctionReturn(0); 2499d763cef2SBarry Smith } 2500d763cef2SBarry Smith 2501d763cef2SBarry Smith 25024a2ae208SSatish Balay #undef __FUNCT__ 25034a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 2504d763cef2SBarry Smith /*@C 2505d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 2506d763cef2SBarry Smith TS options in the database. 2507d763cef2SBarry Smith 25083f9fe445SBarry Smith Logically Collective on TS 2509d763cef2SBarry Smith 2510d763cef2SBarry Smith Input Parameter: 2511d763cef2SBarry Smith + ts - The TS context 2512d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2513d763cef2SBarry Smith 2514d763cef2SBarry Smith Notes: 2515d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2516d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2517d763cef2SBarry Smith hyphen. 2518d763cef2SBarry Smith 2519d763cef2SBarry Smith Level: advanced 2520d763cef2SBarry Smith 2521d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 2522d763cef2SBarry Smith 2523d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 2524d763cef2SBarry Smith 2525d763cef2SBarry Smith @*/ 25267087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 2527d763cef2SBarry Smith { 2528dfbe8321SBarry Smith PetscErrorCode ierr; 2529089b2837SJed Brown SNES snes; 2530d763cef2SBarry Smith 2531d763cef2SBarry Smith PetscFunctionBegin; 25320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2533d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2534089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2535089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2536d763cef2SBarry Smith PetscFunctionReturn(0); 2537d763cef2SBarry Smith } 2538d763cef2SBarry Smith 25394a2ae208SSatish Balay #undef __FUNCT__ 25404a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2541d763cef2SBarry Smith /*@C 2542d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2543d763cef2SBarry Smith TS options in the database. 2544d763cef2SBarry Smith 2545d763cef2SBarry Smith Not Collective 2546d763cef2SBarry Smith 2547d763cef2SBarry Smith Input Parameter: 2548d763cef2SBarry Smith . ts - The TS context 2549d763cef2SBarry Smith 2550d763cef2SBarry Smith Output Parameter: 2551d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2552d763cef2SBarry Smith 2553d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2554d763cef2SBarry Smith sufficient length to hold the prefix. 2555d763cef2SBarry Smith 2556d763cef2SBarry Smith Level: intermediate 2557d763cef2SBarry Smith 2558d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2559d763cef2SBarry Smith 2560d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2561d763cef2SBarry Smith @*/ 25627087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2563d763cef2SBarry Smith { 2564dfbe8321SBarry Smith PetscErrorCode ierr; 2565d763cef2SBarry Smith 2566d763cef2SBarry Smith PetscFunctionBegin; 25670700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25684482741eSBarry Smith PetscValidPointer(prefix,2); 2569d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2570d763cef2SBarry Smith PetscFunctionReturn(0); 2571d763cef2SBarry Smith } 2572d763cef2SBarry Smith 25734a2ae208SSatish Balay #undef __FUNCT__ 25744a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2575d763cef2SBarry Smith /*@C 2576d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2577d763cef2SBarry Smith 2578d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2579d763cef2SBarry Smith 2580d763cef2SBarry Smith Input Parameter: 2581d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2582d763cef2SBarry Smith 2583d763cef2SBarry Smith Output Parameters: 2584d763cef2SBarry Smith + J - The Jacobian J of F, where U_t = F(U,t) 2585d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2586089b2837SJed Brown . func - Function to compute the Jacobian of the RHS 2587d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2588d763cef2SBarry Smith 2589d763cef2SBarry Smith Notes: You can pass in PETSC_NULL for any return argument you do not need. 2590d763cef2SBarry Smith 2591d763cef2SBarry Smith Level: intermediate 2592d763cef2SBarry Smith 259326d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2594d763cef2SBarry Smith 2595d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2596d763cef2SBarry Smith @*/ 2597089b2837SJed Brown PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2598d763cef2SBarry Smith { 2599089b2837SJed Brown PetscErrorCode ierr; 2600089b2837SJed Brown SNES snes; 260124989b8cSPeter Brune DM dm; 2602089b2837SJed Brown 2603d763cef2SBarry Smith PetscFunctionBegin; 2604089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2605089b2837SJed Brown ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 260624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 260724989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 2608d763cef2SBarry Smith PetscFunctionReturn(0); 2609d763cef2SBarry Smith } 2610d763cef2SBarry Smith 26111713a123SBarry Smith #undef __FUNCT__ 26122eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 26132eca1d9cSJed Brown /*@C 26142eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 26152eca1d9cSJed Brown 26162eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 26172eca1d9cSJed Brown 26182eca1d9cSJed Brown Input Parameter: 26192eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 26202eca1d9cSJed Brown 26212eca1d9cSJed Brown Output Parameters: 26222eca1d9cSJed Brown + A - The Jacobian of F(t,U,U_t) 26232eca1d9cSJed Brown . B - The preconditioner matrix, often the same as A 26242eca1d9cSJed Brown . f - The function to compute the matrices 26252eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 26262eca1d9cSJed Brown 26272eca1d9cSJed Brown Notes: You can pass in PETSC_NULL for any return argument you do not need. 26282eca1d9cSJed Brown 26292eca1d9cSJed Brown Level: advanced 26302eca1d9cSJed Brown 26312eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 26322eca1d9cSJed Brown 26332eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 26342eca1d9cSJed Brown @*/ 26357087cfbeSBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 26362eca1d9cSJed Brown { 2637089b2837SJed Brown PetscErrorCode ierr; 2638089b2837SJed Brown SNES snes; 263924989b8cSPeter Brune DM dm; 26402eca1d9cSJed Brown PetscFunctionBegin; 2641089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2642089b2837SJed Brown ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 264324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 264424989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 26452eca1d9cSJed Brown PetscFunctionReturn(0); 26462eca1d9cSJed Brown } 26472eca1d9cSJed Brown 26486083293cSBarry Smith typedef struct { 26496083293cSBarry Smith PetscViewer viewer; 26506083293cSBarry Smith Vec initialsolution; 26516083293cSBarry Smith PetscBool showinitial; 26526083293cSBarry Smith } TSMonitorSolutionCtx; 26536083293cSBarry Smith 26542eca1d9cSJed Brown #undef __FUNCT__ 2655a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution" 26561713a123SBarry Smith /*@C 2657a6570f20SBarry Smith TSMonitorSolution - Monitors progress of the TS solvers by calling 26581713a123SBarry Smith VecView() for the solution at each timestep 26591713a123SBarry Smith 26601713a123SBarry Smith Collective on TS 26611713a123SBarry Smith 26621713a123SBarry Smith Input Parameters: 26631713a123SBarry Smith + ts - the TS context 26641713a123SBarry Smith . step - current time-step 2665142b95e3SSatish Balay . ptime - current time 26661713a123SBarry Smith - dummy - either a viewer or PETSC_NULL 26671713a123SBarry Smith 26681713a123SBarry Smith Level: intermediate 26691713a123SBarry Smith 26701713a123SBarry Smith .keywords: TS, vector, monitor, view 26711713a123SBarry Smith 2672a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 26731713a123SBarry Smith @*/ 26747087cfbeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 26751713a123SBarry Smith { 2676dfbe8321SBarry Smith PetscErrorCode ierr; 26776083293cSBarry Smith TSMonitorSolutionCtx *ictx = (TSMonitorSolutionCtx*)dummy; 26781713a123SBarry Smith 26791713a123SBarry Smith PetscFunctionBegin; 26806083293cSBarry Smith if (!step && ictx->showinitial) { 26816083293cSBarry Smith if (!ictx->initialsolution) { 26826083293cSBarry Smith ierr = VecDuplicate(x,&ictx->initialsolution);CHKERRQ(ierr); 26831713a123SBarry Smith } 26846083293cSBarry Smith ierr = VecCopy(x,ictx->initialsolution);CHKERRQ(ierr); 26856083293cSBarry Smith } 26866083293cSBarry Smith if (ictx->showinitial) { 26876083293cSBarry Smith PetscReal pause; 26886083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 26896083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 26906083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 26916083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 26926083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 26936083293cSBarry Smith } 26946083293cSBarry Smith ierr = VecView(x,ictx->viewer);CHKERRQ(ierr); 26956083293cSBarry Smith if (ictx->showinitial) { 26966083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 26976083293cSBarry Smith } 26981713a123SBarry Smith PetscFunctionReturn(0); 26991713a123SBarry Smith } 27001713a123SBarry Smith 27011713a123SBarry Smith 27026c699258SBarry Smith #undef __FUNCT__ 27036083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionDestroy" 27046083293cSBarry Smith /*@C 27056083293cSBarry Smith TSMonitorSolutionDestroy - Destroys the monitor context for TSMonitorSolution 27066083293cSBarry Smith 27076083293cSBarry Smith Collective on TS 27086083293cSBarry Smith 27096083293cSBarry Smith Input Parameters: 27106083293cSBarry Smith . ctx - the monitor context 27116083293cSBarry Smith 27126083293cSBarry Smith Level: intermediate 27136083293cSBarry Smith 27146083293cSBarry Smith .keywords: TS, vector, monitor, view 27156083293cSBarry Smith 27166083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution() 27176083293cSBarry Smith @*/ 27186083293cSBarry Smith PetscErrorCode TSMonitorSolutionDestroy(void **ctx) 27196083293cSBarry Smith { 27206083293cSBarry Smith PetscErrorCode ierr; 27216083293cSBarry Smith TSMonitorSolutionCtx *ictx = *(TSMonitorSolutionCtx**)ctx; 27226083293cSBarry Smith 27236083293cSBarry Smith PetscFunctionBegin; 27246083293cSBarry Smith ierr = PetscViewerDestroy(&ictx->viewer);CHKERRQ(ierr); 27256083293cSBarry Smith ierr = VecDestroy(&ictx->initialsolution);CHKERRQ(ierr); 27266083293cSBarry Smith ierr = PetscFree(ictx);CHKERRQ(ierr); 27276083293cSBarry Smith PetscFunctionReturn(0); 27286083293cSBarry Smith } 27296083293cSBarry Smith 27306083293cSBarry Smith #undef __FUNCT__ 27316083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionCreate" 27326083293cSBarry Smith /*@C 27336083293cSBarry Smith TSMonitorSolutionCreate - Creates the monitor context for TSMonitorSolution 27346083293cSBarry Smith 27356083293cSBarry Smith Collective on TS 27366083293cSBarry Smith 27376083293cSBarry Smith Input Parameter: 27386083293cSBarry Smith . ts - time-step context 27396083293cSBarry Smith 27406083293cSBarry Smith Output Patameter: 27416083293cSBarry Smith . ctx - the monitor context 27426083293cSBarry Smith 27436083293cSBarry Smith Level: intermediate 27446083293cSBarry Smith 27456083293cSBarry Smith .keywords: TS, vector, monitor, view 27466083293cSBarry Smith 27476083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution() 27486083293cSBarry Smith @*/ 27496083293cSBarry Smith PetscErrorCode TSMonitorSolutionCreate(TS ts,PetscViewer viewer,void **ctx) 27506083293cSBarry Smith { 27516083293cSBarry Smith PetscErrorCode ierr; 27526083293cSBarry Smith TSMonitorSolutionCtx *ictx; 27536083293cSBarry Smith 27546083293cSBarry Smith PetscFunctionBegin; 27556083293cSBarry Smith ierr = PetscNew(TSMonitorSolutionCtx,&ictx);CHKERRQ(ierr); 27566083293cSBarry Smith *ctx = (void*)ictx; 27576083293cSBarry Smith if (!viewer) { 27586083293cSBarry Smith viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm); 27596083293cSBarry Smith } 27606083293cSBarry Smith ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr); 27616083293cSBarry Smith ictx->viewer = viewer; 27626083293cSBarry Smith ictx->showinitial = PETSC_FALSE; 27636083293cSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix,"-ts_monitor_solution_initial",&ictx->showinitial,PETSC_NULL);CHKERRQ(ierr); 27646083293cSBarry Smith PetscFunctionReturn(0); 27656083293cSBarry Smith } 27666083293cSBarry Smith 27676083293cSBarry Smith #undef __FUNCT__ 27686c699258SBarry Smith #define __FUNCT__ "TSSetDM" 27696c699258SBarry Smith /*@ 27706c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 27716c699258SBarry Smith 27723f9fe445SBarry Smith Logically Collective on TS and DM 27736c699258SBarry Smith 27746c699258SBarry Smith Input Parameters: 27756c699258SBarry Smith + ts - the preconditioner context 27766c699258SBarry Smith - dm - the dm 27776c699258SBarry Smith 27786c699258SBarry Smith Level: intermediate 27796c699258SBarry Smith 27806c699258SBarry Smith 27816c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 27826c699258SBarry Smith @*/ 27837087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 27846c699258SBarry Smith { 27856c699258SBarry Smith PetscErrorCode ierr; 2786089b2837SJed Brown SNES snes; 278724989b8cSPeter Brune TSDM tsdm; 27886c699258SBarry Smith 27896c699258SBarry Smith PetscFunctionBegin; 27900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 279170663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 279224989b8cSPeter Brune if (ts->dm) { /* Move the TSDM context over to the new DM unless the new DM already has one */ 279324989b8cSPeter Brune PetscContainer oldcontainer,container; 279424989b8cSPeter Brune ierr = PetscObjectQuery((PetscObject)ts->dm,"TSDM",(PetscObject*)&oldcontainer);CHKERRQ(ierr); 279524989b8cSPeter Brune ierr = PetscObjectQuery((PetscObject)dm,"TSDM",(PetscObject*)&container);CHKERRQ(ierr); 279624989b8cSPeter Brune if (oldcontainer && !container) { 279724989b8cSPeter Brune ierr = DMTSCopyContext(ts->dm,dm);CHKERRQ(ierr); 279824989b8cSPeter Brune ierr = DMTSGetContext(ts->dm,&tsdm);CHKERRQ(ierr); 279924989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 280024989b8cSPeter Brune tsdm->originaldm = dm; 280124989b8cSPeter Brune } 280224989b8cSPeter Brune } 28036bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 280424989b8cSPeter Brune } 28056c699258SBarry Smith ts->dm = dm; 2806089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2807089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 28086c699258SBarry Smith PetscFunctionReturn(0); 28096c699258SBarry Smith } 28106c699258SBarry Smith 28116c699258SBarry Smith #undef __FUNCT__ 28126c699258SBarry Smith #define __FUNCT__ "TSGetDM" 28136c699258SBarry Smith /*@ 28146c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 28156c699258SBarry Smith 28163f9fe445SBarry Smith Not Collective 28176c699258SBarry Smith 28186c699258SBarry Smith Input Parameter: 28196c699258SBarry Smith . ts - the preconditioner context 28206c699258SBarry Smith 28216c699258SBarry Smith Output Parameter: 28226c699258SBarry Smith . dm - the dm 28236c699258SBarry Smith 28246c699258SBarry Smith Level: intermediate 28256c699258SBarry Smith 28266c699258SBarry Smith 28276c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 28286c699258SBarry Smith @*/ 28297087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 28306c699258SBarry Smith { 2831496e6a7aSJed Brown PetscErrorCode ierr; 2832496e6a7aSJed Brown 28336c699258SBarry Smith PetscFunctionBegin; 28340700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2835496e6a7aSJed Brown if (!ts->dm) { 2836496e6a7aSJed Brown ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr); 2837496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 2838496e6a7aSJed Brown } 28396c699258SBarry Smith *dm = ts->dm; 28406c699258SBarry Smith PetscFunctionReturn(0); 28416c699258SBarry Smith } 28421713a123SBarry Smith 28430f5c6efeSJed Brown #undef __FUNCT__ 28440f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 28450f5c6efeSJed Brown /*@ 28460f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 28470f5c6efeSJed Brown 28483f9fe445SBarry Smith Logically Collective on SNES 28490f5c6efeSJed Brown 28500f5c6efeSJed Brown Input Parameter: 2851d42a1c89SJed Brown + snes - nonlinear solver 28520f5c6efeSJed Brown . X - the current state at which to evaluate the residual 2853d42a1c89SJed Brown - ctx - user context, must be a TS 28540f5c6efeSJed Brown 28550f5c6efeSJed Brown Output Parameter: 28560f5c6efeSJed Brown . F - the nonlinear residual 28570f5c6efeSJed Brown 28580f5c6efeSJed Brown Notes: 28590f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 28600f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 28610f5c6efeSJed Brown 28620f5c6efeSJed Brown Level: advanced 28630f5c6efeSJed Brown 28640f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 28650f5c6efeSJed Brown @*/ 28667087cfbeSBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx) 28670f5c6efeSJed Brown { 28680f5c6efeSJed Brown TS ts = (TS)ctx; 28690f5c6efeSJed Brown PetscErrorCode ierr; 28700f5c6efeSJed Brown 28710f5c6efeSJed Brown PetscFunctionBegin; 28720f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 28730f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 28740f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 28750f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 28760f5c6efeSJed Brown ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr); 28770f5c6efeSJed Brown PetscFunctionReturn(0); 28780f5c6efeSJed Brown } 28790f5c6efeSJed Brown 28800f5c6efeSJed Brown #undef __FUNCT__ 28810f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 28820f5c6efeSJed Brown /*@ 28830f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 28840f5c6efeSJed Brown 28850f5c6efeSJed Brown Collective on SNES 28860f5c6efeSJed Brown 28870f5c6efeSJed Brown Input Parameter: 28880f5c6efeSJed Brown + snes - nonlinear solver 28890f5c6efeSJed Brown . X - the current state at which to evaluate the residual 28900f5c6efeSJed Brown - ctx - user context, must be a TS 28910f5c6efeSJed Brown 28920f5c6efeSJed Brown Output Parameter: 28930f5c6efeSJed Brown + A - the Jacobian 28940f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 28950f5c6efeSJed Brown - flag - indicates any structure change in the matrix 28960f5c6efeSJed Brown 28970f5c6efeSJed Brown Notes: 28980f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 28990f5c6efeSJed Brown 29000f5c6efeSJed Brown Level: developer 29010f5c6efeSJed Brown 29020f5c6efeSJed Brown .seealso: SNESSetJacobian() 29030f5c6efeSJed Brown @*/ 29047087cfbeSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx) 29050f5c6efeSJed Brown { 29060f5c6efeSJed Brown TS ts = (TS)ctx; 29070f5c6efeSJed Brown PetscErrorCode ierr; 29080f5c6efeSJed Brown 29090f5c6efeSJed Brown PetscFunctionBegin; 29100f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 29110f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 29120f5c6efeSJed Brown PetscValidPointer(A,3); 29130f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 29140f5c6efeSJed Brown PetscValidPointer(B,4); 29150f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 29160f5c6efeSJed Brown PetscValidPointer(flag,5); 29170f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 29180f5c6efeSJed Brown ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr); 29190f5c6efeSJed Brown PetscFunctionReturn(0); 29200f5c6efeSJed Brown } 2921325fc9f4SBarry Smith 29220e4ef248SJed Brown #undef __FUNCT__ 29230e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 29240e4ef248SJed Brown /*@C 29250e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 29260e4ef248SJed Brown 29270e4ef248SJed Brown Collective on TS 29280e4ef248SJed Brown 29290e4ef248SJed Brown Input Arguments: 29300e4ef248SJed Brown + ts - time stepping context 29310e4ef248SJed Brown . t - time at which to evaluate 29320e4ef248SJed Brown . X - state at which to evaluate 29330e4ef248SJed Brown - ctx - context 29340e4ef248SJed Brown 29350e4ef248SJed Brown Output Arguments: 29360e4ef248SJed Brown . F - right hand side 29370e4ef248SJed Brown 29380e4ef248SJed Brown Level: intermediate 29390e4ef248SJed Brown 29400e4ef248SJed Brown Notes: 29410e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 29420e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 29430e4ef248SJed Brown 29440e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 29450e4ef248SJed Brown @*/ 29460e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx) 29470e4ef248SJed Brown { 29480e4ef248SJed Brown PetscErrorCode ierr; 29490e4ef248SJed Brown Mat Arhs,Brhs; 29500e4ef248SJed Brown MatStructure flg2; 29510e4ef248SJed Brown 29520e4ef248SJed Brown PetscFunctionBegin; 29530e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 29540e4ef248SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 29550e4ef248SJed Brown ierr = MatMult(Arhs,X,F);CHKERRQ(ierr); 29560e4ef248SJed Brown PetscFunctionReturn(0); 29570e4ef248SJed Brown } 29580e4ef248SJed Brown 29590e4ef248SJed Brown #undef __FUNCT__ 29600e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 29610e4ef248SJed Brown /*@C 29620e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 29630e4ef248SJed Brown 29640e4ef248SJed Brown Collective on TS 29650e4ef248SJed Brown 29660e4ef248SJed Brown Input Arguments: 29670e4ef248SJed Brown + ts - time stepping context 29680e4ef248SJed Brown . t - time at which to evaluate 29690e4ef248SJed Brown . X - state at which to evaluate 29700e4ef248SJed Brown - ctx - context 29710e4ef248SJed Brown 29720e4ef248SJed Brown Output Arguments: 29730e4ef248SJed Brown + A - pointer to operator 29740e4ef248SJed Brown . B - pointer to preconditioning matrix 29750e4ef248SJed Brown - flg - matrix structure flag 29760e4ef248SJed Brown 29770e4ef248SJed Brown Level: intermediate 29780e4ef248SJed Brown 29790e4ef248SJed Brown Notes: 29800e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 29810e4ef248SJed Brown 29820e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 29830e4ef248SJed Brown @*/ 29840e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx) 29850e4ef248SJed Brown { 29860e4ef248SJed Brown 29870e4ef248SJed Brown PetscFunctionBegin; 29880e4ef248SJed Brown *flg = SAME_PRECONDITIONER; 29890e4ef248SJed Brown PetscFunctionReturn(0); 29900e4ef248SJed Brown } 29910e4ef248SJed Brown 29920026cea9SSean Farley #undef __FUNCT__ 29930026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 29940026cea9SSean Farley /*@C 29950026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 29960026cea9SSean Farley 29970026cea9SSean Farley Collective on TS 29980026cea9SSean Farley 29990026cea9SSean Farley Input Arguments: 30000026cea9SSean Farley + ts - time stepping context 30010026cea9SSean Farley . t - time at which to evaluate 30020026cea9SSean Farley . X - state at which to evaluate 30030026cea9SSean Farley . Xdot - time derivative of state vector 30040026cea9SSean Farley - ctx - context 30050026cea9SSean Farley 30060026cea9SSean Farley Output Arguments: 30070026cea9SSean Farley . F - left hand side 30080026cea9SSean Farley 30090026cea9SSean Farley Level: intermediate 30100026cea9SSean Farley 30110026cea9SSean Farley Notes: 30120026cea9SSean 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 30130026cea9SSean Farley user is required to write their own TSComputeIFunction. 30140026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 30150026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 30160026cea9SSean Farley 30170026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 30180026cea9SSean Farley @*/ 30190026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx) 30200026cea9SSean Farley { 30210026cea9SSean Farley PetscErrorCode ierr; 30220026cea9SSean Farley Mat A,B; 30230026cea9SSean Farley MatStructure flg2; 30240026cea9SSean Farley 30250026cea9SSean Farley PetscFunctionBegin; 30260026cea9SSean Farley ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 30270026cea9SSean Farley ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr); 30280026cea9SSean Farley ierr = MatMult(A,Xdot,F);CHKERRQ(ierr); 30290026cea9SSean Farley PetscFunctionReturn(0); 30300026cea9SSean Farley } 30310026cea9SSean Farley 30320026cea9SSean Farley #undef __FUNCT__ 30330026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 30340026cea9SSean Farley /*@C 303524e94211SJed Brown TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent. 30360026cea9SSean Farley 30370026cea9SSean Farley Collective on TS 30380026cea9SSean Farley 30390026cea9SSean Farley Input Arguments: 30400026cea9SSean Farley + ts - time stepping context 30410026cea9SSean Farley . t - time at which to evaluate 30420026cea9SSean Farley . X - state at which to evaluate 30430026cea9SSean Farley . Xdot - time derivative of state vector 30440026cea9SSean Farley . shift - shift to apply 30450026cea9SSean Farley - ctx - context 30460026cea9SSean Farley 30470026cea9SSean Farley Output Arguments: 30480026cea9SSean Farley + A - pointer to operator 30490026cea9SSean Farley . B - pointer to preconditioning matrix 30500026cea9SSean Farley - flg - matrix structure flag 30510026cea9SSean Farley 30520026cea9SSean Farley Level: intermediate 30530026cea9SSean Farley 30540026cea9SSean Farley Notes: 30550026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 30560026cea9SSean Farley 30570026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 30580026cea9SSean Farley @*/ 30590026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx) 30600026cea9SSean Farley { 30610026cea9SSean Farley 30620026cea9SSean Farley PetscFunctionBegin; 30630026cea9SSean Farley *flg = SAME_PRECONDITIONER; 30640026cea9SSean Farley PetscFunctionReturn(0); 30650026cea9SSean Farley } 30660026cea9SSean Farley 30674af1b03aSJed Brown 30684af1b03aSJed Brown #undef __FUNCT__ 30694af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 30704af1b03aSJed Brown /*@ 30714af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 30724af1b03aSJed Brown 30734af1b03aSJed Brown Not Collective 30744af1b03aSJed Brown 30754af1b03aSJed Brown Input Parameter: 30764af1b03aSJed Brown . ts - the TS context 30774af1b03aSJed Brown 30784af1b03aSJed Brown Output Parameter: 30794af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 30804af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 30814af1b03aSJed Brown 30824af1b03aSJed Brown Level: intermediate 30834af1b03aSJed Brown 3084cd652676SJed Brown Notes: 3085cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 30864af1b03aSJed Brown 30874af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 30884af1b03aSJed Brown 30894af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 30904af1b03aSJed Brown @*/ 30914af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 30924af1b03aSJed Brown { 30934af1b03aSJed Brown PetscFunctionBegin; 30944af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30954af1b03aSJed Brown PetscValidPointer(reason,2); 30964af1b03aSJed Brown *reason = ts->reason; 30974af1b03aSJed Brown PetscFunctionReturn(0); 30984af1b03aSJed Brown } 30994af1b03aSJed Brown 3100fb1732b5SBarry Smith #undef __FUNCT__ 31015ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 31029f67acb7SJed Brown /*@ 31035ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 31049f67acb7SJed Brown used by the time integrator. 31059f67acb7SJed Brown 31069f67acb7SJed Brown Not Collective 31079f67acb7SJed Brown 31089f67acb7SJed Brown Input Parameter: 31099f67acb7SJed Brown . ts - TS context 31109f67acb7SJed Brown 31119f67acb7SJed Brown Output Parameter: 31129f67acb7SJed Brown . nits - number of nonlinear iterations 31139f67acb7SJed Brown 31149f67acb7SJed Brown Notes: 31159f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 31169f67acb7SJed Brown 31179f67acb7SJed Brown Level: intermediate 31189f67acb7SJed Brown 31199f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 31209f67acb7SJed Brown 31215ef26d82SJed Brown .seealso: TSGetKSPIterations() 31229f67acb7SJed Brown @*/ 31235ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 31249f67acb7SJed Brown { 31259f67acb7SJed Brown PetscFunctionBegin; 31269f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31279f67acb7SJed Brown PetscValidIntPointer(nits,2); 31285ef26d82SJed Brown *nits = ts->snes_its; 31299f67acb7SJed Brown PetscFunctionReturn(0); 31309f67acb7SJed Brown } 31319f67acb7SJed Brown 31329f67acb7SJed Brown #undef __FUNCT__ 31335ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 31349f67acb7SJed Brown /*@ 31355ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 31369f67acb7SJed Brown used by the time integrator. 31379f67acb7SJed Brown 31389f67acb7SJed Brown Not Collective 31399f67acb7SJed Brown 31409f67acb7SJed Brown Input Parameter: 31419f67acb7SJed Brown . ts - TS context 31429f67acb7SJed Brown 31439f67acb7SJed Brown Output Parameter: 31449f67acb7SJed Brown . lits - number of linear iterations 31459f67acb7SJed Brown 31469f67acb7SJed Brown Notes: 31479f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 31489f67acb7SJed Brown 31499f67acb7SJed Brown Level: intermediate 31509f67acb7SJed Brown 31519f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 31529f67acb7SJed Brown 31535ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 31549f67acb7SJed Brown @*/ 31555ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 31569f67acb7SJed Brown { 31579f67acb7SJed Brown PetscFunctionBegin; 31589f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31599f67acb7SJed Brown PetscValidIntPointer(lits,2); 31605ef26d82SJed Brown *lits = ts->ksp_its; 31619f67acb7SJed Brown PetscFunctionReturn(0); 31629f67acb7SJed Brown } 31639f67acb7SJed Brown 31649f67acb7SJed Brown #undef __FUNCT__ 3165cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 3166cef5090cSJed Brown /*@ 3167cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 3168cef5090cSJed Brown 3169cef5090cSJed Brown Not Collective 3170cef5090cSJed Brown 3171cef5090cSJed Brown Input Parameter: 3172cef5090cSJed Brown . ts - TS context 3173cef5090cSJed Brown 3174cef5090cSJed Brown Output Parameter: 3175cef5090cSJed Brown . rejects - number of steps rejected 3176cef5090cSJed Brown 3177cef5090cSJed Brown Notes: 3178cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 3179cef5090cSJed Brown 3180cef5090cSJed Brown Level: intermediate 3181cef5090cSJed Brown 3182cef5090cSJed Brown .keywords: TS, get, number 3183cef5090cSJed Brown 31845ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 3185cef5090cSJed Brown @*/ 3186cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 3187cef5090cSJed Brown { 3188cef5090cSJed Brown PetscFunctionBegin; 3189cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3190cef5090cSJed Brown PetscValidIntPointer(rejects,2); 3191cef5090cSJed Brown *rejects = ts->reject; 3192cef5090cSJed Brown PetscFunctionReturn(0); 3193cef5090cSJed Brown } 3194cef5090cSJed Brown 3195cef5090cSJed Brown #undef __FUNCT__ 3196cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 3197cef5090cSJed Brown /*@ 3198cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 3199cef5090cSJed Brown 3200cef5090cSJed Brown Not Collective 3201cef5090cSJed Brown 3202cef5090cSJed Brown Input Parameter: 3203cef5090cSJed Brown . ts - TS context 3204cef5090cSJed Brown 3205cef5090cSJed Brown Output Parameter: 3206cef5090cSJed Brown . fails - number of failed nonlinear solves 3207cef5090cSJed Brown 3208cef5090cSJed Brown Notes: 3209cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 3210cef5090cSJed Brown 3211cef5090cSJed Brown Level: intermediate 3212cef5090cSJed Brown 3213cef5090cSJed Brown .keywords: TS, get, number 3214cef5090cSJed Brown 32155ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 3216cef5090cSJed Brown @*/ 3217cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 3218cef5090cSJed Brown { 3219cef5090cSJed Brown PetscFunctionBegin; 3220cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3221cef5090cSJed Brown PetscValidIntPointer(fails,2); 3222cef5090cSJed Brown *fails = ts->num_snes_failures; 3223cef5090cSJed Brown PetscFunctionReturn(0); 3224cef5090cSJed Brown } 3225cef5090cSJed Brown 3226cef5090cSJed Brown #undef __FUNCT__ 3227cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 3228cef5090cSJed Brown /*@ 3229cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 3230cef5090cSJed Brown 3231cef5090cSJed Brown Not Collective 3232cef5090cSJed Brown 3233cef5090cSJed Brown Input Parameter: 3234cef5090cSJed Brown + ts - TS context 3235cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 3236cef5090cSJed Brown 3237cef5090cSJed Brown Notes: 3238cef5090cSJed Brown The counter is reset to zero for each step 3239cef5090cSJed Brown 3240cef5090cSJed Brown Options Database Key: 3241cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 3242cef5090cSJed Brown 3243cef5090cSJed Brown Level: intermediate 3244cef5090cSJed Brown 3245cef5090cSJed Brown .keywords: TS, set, maximum, number 3246cef5090cSJed Brown 32475ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3248cef5090cSJed Brown @*/ 3249cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 3250cef5090cSJed Brown { 3251cef5090cSJed Brown PetscFunctionBegin; 3252cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3253cef5090cSJed Brown ts->max_reject = rejects; 3254cef5090cSJed Brown PetscFunctionReturn(0); 3255cef5090cSJed Brown } 3256cef5090cSJed Brown 3257cef5090cSJed Brown #undef __FUNCT__ 3258cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 3259cef5090cSJed Brown /*@ 3260cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 3261cef5090cSJed Brown 3262cef5090cSJed Brown Not Collective 3263cef5090cSJed Brown 3264cef5090cSJed Brown Input Parameter: 3265cef5090cSJed Brown + ts - TS context 3266cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 3267cef5090cSJed Brown 3268cef5090cSJed Brown Notes: 3269cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 3270cef5090cSJed Brown 3271cef5090cSJed Brown Options Database Key: 3272cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 3273cef5090cSJed Brown 3274cef5090cSJed Brown Level: intermediate 3275cef5090cSJed Brown 3276cef5090cSJed Brown .keywords: TS, set, maximum, number 3277cef5090cSJed Brown 32785ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 3279cef5090cSJed Brown @*/ 3280cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 3281cef5090cSJed Brown { 3282cef5090cSJed Brown PetscFunctionBegin; 3283cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3284cef5090cSJed Brown ts->max_snes_failures = fails; 3285cef5090cSJed Brown PetscFunctionReturn(0); 3286cef5090cSJed Brown } 3287cef5090cSJed Brown 3288cef5090cSJed Brown #undef __FUNCT__ 3289cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()" 3290cef5090cSJed Brown /*@ 3291cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 3292cef5090cSJed Brown 3293cef5090cSJed Brown Not Collective 3294cef5090cSJed Brown 3295cef5090cSJed Brown Input Parameter: 3296cef5090cSJed Brown + ts - TS context 3297cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 3298cef5090cSJed Brown 3299cef5090cSJed Brown Options Database Key: 3300cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 3301cef5090cSJed Brown 3302cef5090cSJed Brown Level: intermediate 3303cef5090cSJed Brown 3304cef5090cSJed Brown .keywords: TS, set, error 3305cef5090cSJed Brown 33065ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3307cef5090cSJed Brown @*/ 3308cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 3309cef5090cSJed Brown { 3310cef5090cSJed Brown PetscFunctionBegin; 3311cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3312cef5090cSJed Brown ts->errorifstepfailed = err; 3313cef5090cSJed Brown PetscFunctionReturn(0); 3314cef5090cSJed Brown } 3315cef5090cSJed Brown 3316cef5090cSJed Brown #undef __FUNCT__ 3317fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 3318fb1732b5SBarry Smith /*@C 3319fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 3320fb1732b5SBarry Smith 3321fb1732b5SBarry Smith Collective on TS 3322fb1732b5SBarry Smith 3323fb1732b5SBarry Smith Input Parameters: 3324fb1732b5SBarry Smith + ts - the TS context 3325fb1732b5SBarry Smith . step - current time-step 3326fb1732b5SBarry Smith . ptime - current time 3327ed81e22dSJed Brown . x - current state 3328fb1732b5SBarry Smith - viewer - binary viewer 3329fb1732b5SBarry Smith 3330fb1732b5SBarry Smith Level: intermediate 3331fb1732b5SBarry Smith 3332fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 3333fb1732b5SBarry Smith 3334fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3335fb1732b5SBarry Smith @*/ 3336ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec x,void *viewer) 3337fb1732b5SBarry Smith { 3338fb1732b5SBarry Smith PetscErrorCode ierr; 3339ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 3340fb1732b5SBarry Smith 3341fb1732b5SBarry Smith PetscFunctionBegin; 3342ed81e22dSJed Brown ierr = VecView(x,v);CHKERRQ(ierr); 3343ed81e22dSJed Brown PetscFunctionReturn(0); 3344ed81e22dSJed Brown } 3345ed81e22dSJed Brown 3346ed81e22dSJed Brown #undef __FUNCT__ 3347ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 3348ed81e22dSJed Brown /*@C 3349ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 3350ed81e22dSJed Brown 3351ed81e22dSJed Brown Collective on TS 3352ed81e22dSJed Brown 3353ed81e22dSJed Brown Input Parameters: 3354ed81e22dSJed Brown + ts - the TS context 3355ed81e22dSJed Brown . step - current time-step 3356ed81e22dSJed Brown . ptime - current time 3357ed81e22dSJed Brown . x - current state 3358ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3359ed81e22dSJed Brown 3360ed81e22dSJed Brown Level: intermediate 3361ed81e22dSJed Brown 3362ed81e22dSJed Brown Notes: 3363ed81e22dSJed 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. 3364ed81e22dSJed Brown These are named according to the file name template. 3365ed81e22dSJed Brown 3366ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 3367ed81e22dSJed Brown 3368ed81e22dSJed Brown .keywords: TS, vector, monitor, view 3369ed81e22dSJed Brown 3370ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3371ed81e22dSJed Brown @*/ 3372ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec x,void *filenametemplate) 3373ed81e22dSJed Brown { 3374ed81e22dSJed Brown PetscErrorCode ierr; 3375ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 3376ed81e22dSJed Brown PetscViewer viewer; 3377ed81e22dSJed Brown 3378ed81e22dSJed Brown PetscFunctionBegin; 33798caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 3380ed81e22dSJed Brown ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 3381fb1732b5SBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 3382ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 3383ed81e22dSJed Brown PetscFunctionReturn(0); 3384ed81e22dSJed Brown } 3385ed81e22dSJed Brown 3386ed81e22dSJed Brown #undef __FUNCT__ 3387ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 3388ed81e22dSJed Brown /*@C 3389ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 3390ed81e22dSJed Brown 3391ed81e22dSJed Brown Collective on TS 3392ed81e22dSJed Brown 3393ed81e22dSJed Brown Input Parameters: 3394ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3395ed81e22dSJed Brown 3396ed81e22dSJed Brown Level: intermediate 3397ed81e22dSJed Brown 3398ed81e22dSJed Brown Note: 3399ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 3400ed81e22dSJed Brown 3401ed81e22dSJed Brown .keywords: TS, vector, monitor, view 3402ed81e22dSJed Brown 3403ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 3404ed81e22dSJed Brown @*/ 3405ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 3406ed81e22dSJed Brown { 3407ed81e22dSJed Brown PetscErrorCode ierr; 3408ed81e22dSJed Brown 3409ed81e22dSJed Brown PetscFunctionBegin; 3410ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 3411fb1732b5SBarry Smith PetscFunctionReturn(0); 3412fb1732b5SBarry Smith } 3413fb1732b5SBarry Smith 341484df9cb4SJed Brown #undef __FUNCT__ 341584df9cb4SJed Brown #define __FUNCT__ "TSGetAdapt" 341684df9cb4SJed Brown /*@ 341784df9cb4SJed Brown TSGetAdapt - Get the adaptive controller context for the current method 341884df9cb4SJed Brown 3419ed81e22dSJed Brown Collective on TS if controller has not been created yet 342084df9cb4SJed Brown 342184df9cb4SJed Brown Input Arguments: 3422ed81e22dSJed Brown . ts - time stepping context 342384df9cb4SJed Brown 342484df9cb4SJed Brown Output Arguments: 3425ed81e22dSJed Brown . adapt - adaptive controller 342684df9cb4SJed Brown 342784df9cb4SJed Brown Level: intermediate 342884df9cb4SJed Brown 3429ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 343084df9cb4SJed Brown @*/ 343184df9cb4SJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 343284df9cb4SJed Brown { 343384df9cb4SJed Brown PetscErrorCode ierr; 343484df9cb4SJed Brown 343584df9cb4SJed Brown PetscFunctionBegin; 343684df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 343784df9cb4SJed Brown PetscValidPointer(adapt,2); 343884df9cb4SJed Brown if (!ts->adapt) { 343984df9cb4SJed Brown ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr); 34401c3436cfSJed Brown ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr); 34411c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 344284df9cb4SJed Brown } 344384df9cb4SJed Brown *adapt = ts->adapt; 344484df9cb4SJed Brown PetscFunctionReturn(0); 344584df9cb4SJed Brown } 3446d6ebe24aSShri Abhyankar 3447d6ebe24aSShri Abhyankar #undef __FUNCT__ 34481c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 34491c3436cfSJed Brown /*@ 34501c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 34511c3436cfSJed Brown 34521c3436cfSJed Brown Logically Collective 34531c3436cfSJed Brown 34541c3436cfSJed Brown Input Arguments: 34551c3436cfSJed Brown + ts - time integration context 34561c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 34571c3436cfSJed Brown . vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present 34581c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 34591c3436cfSJed Brown - vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present 34601c3436cfSJed Brown 34611c3436cfSJed Brown Level: beginner 34621c3436cfSJed Brown 3463c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 34641c3436cfSJed Brown @*/ 34651c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 34661c3436cfSJed Brown { 34671c3436cfSJed Brown PetscErrorCode ierr; 34681c3436cfSJed Brown 34691c3436cfSJed Brown PetscFunctionBegin; 3470c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 34711c3436cfSJed Brown if (vatol) { 34721c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 34731c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 34741c3436cfSJed Brown ts->vatol = vatol; 34751c3436cfSJed Brown } 3476c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 34771c3436cfSJed Brown if (vrtol) { 34781c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 34791c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 34801c3436cfSJed Brown ts->vrtol = vrtol; 34811c3436cfSJed Brown } 34821c3436cfSJed Brown PetscFunctionReturn(0); 34831c3436cfSJed Brown } 34841c3436cfSJed Brown 34851c3436cfSJed Brown #undef __FUNCT__ 3486c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 3487c5033834SJed Brown /*@ 3488c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 3489c5033834SJed Brown 3490c5033834SJed Brown Logically Collective 3491c5033834SJed Brown 3492c5033834SJed Brown Input Arguments: 3493c5033834SJed Brown . ts - time integration context 3494c5033834SJed Brown 3495c5033834SJed Brown Output Arguments: 3496c5033834SJed Brown + atol - scalar absolute tolerances, PETSC_NULL to ignore 3497c5033834SJed Brown . vatol - vector of absolute tolerances, PETSC_NULL to ignore 3498c5033834SJed Brown . rtol - scalar relative tolerances, PETSC_NULL to ignore 3499c5033834SJed Brown - vrtol - vector of relative tolerances, PETSC_NULL to ignore 3500c5033834SJed Brown 3501c5033834SJed Brown Level: beginner 3502c5033834SJed Brown 3503c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 3504c5033834SJed Brown @*/ 3505c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 3506c5033834SJed Brown { 3507c5033834SJed Brown 3508c5033834SJed Brown PetscFunctionBegin; 3509c5033834SJed Brown if (atol) *atol = ts->atol; 3510c5033834SJed Brown if (vatol) *vatol = ts->vatol; 3511c5033834SJed Brown if (rtol) *rtol = ts->rtol; 3512c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 3513c5033834SJed Brown PetscFunctionReturn(0); 3514c5033834SJed Brown } 3515c5033834SJed Brown 3516c5033834SJed Brown #undef __FUNCT__ 35171c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS" 35181c3436cfSJed Brown /*@ 35191c3436cfSJed Brown TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 35201c3436cfSJed Brown 35211c3436cfSJed Brown Collective on TS 35221c3436cfSJed Brown 35231c3436cfSJed Brown Input Arguments: 35241c3436cfSJed Brown + ts - time stepping context 35251c3436cfSJed Brown - Y - state vector to be compared to ts->vec_sol 35261c3436cfSJed Brown 35271c3436cfSJed Brown Output Arguments: 35281c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 35291c3436cfSJed Brown 35301c3436cfSJed Brown Level: developer 35311c3436cfSJed Brown 35321c3436cfSJed Brown .seealso: TSSetTolerances() 35331c3436cfSJed Brown @*/ 35341c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 35351c3436cfSJed Brown { 35361c3436cfSJed Brown PetscErrorCode ierr; 35371c3436cfSJed Brown PetscInt i,n,N; 35381c3436cfSJed Brown const PetscScalar *x,*y; 35391c3436cfSJed Brown Vec X; 35401c3436cfSJed Brown PetscReal sum,gsum; 35411c3436cfSJed Brown 35421c3436cfSJed Brown PetscFunctionBegin; 35431c3436cfSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 35441c3436cfSJed Brown PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 35451c3436cfSJed Brown PetscValidPointer(norm,3); 35461c3436cfSJed Brown X = ts->vec_sol; 35471c3436cfSJed Brown PetscCheckSameTypeAndComm(X,1,Y,2); 35481c3436cfSJed Brown if (X == Y) SETERRQ(((PetscObject)X)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 35491c3436cfSJed Brown 35501c3436cfSJed Brown ierr = VecGetSize(X,&N);CHKERRQ(ierr); 35511c3436cfSJed Brown ierr = VecGetLocalSize(X,&n);CHKERRQ(ierr); 35521c3436cfSJed Brown ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr); 35531c3436cfSJed Brown ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 35541c3436cfSJed Brown sum = 0.; 3555ceefc113SJed Brown if (ts->vatol && ts->vrtol) { 3556ceefc113SJed Brown const PetscScalar *atol,*rtol; 3557ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3558ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3559ceefc113SJed Brown for (i=0; i<n; i++) { 3560ceefc113SJed Brown PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i])); 3561ceefc113SJed Brown sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol); 3562ceefc113SJed Brown } 3563ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3564ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3565ceefc113SJed Brown } else if (ts->vatol) { /* vector atol, scalar rtol */ 3566ceefc113SJed Brown const PetscScalar *atol; 3567ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3568ceefc113SJed Brown for (i=0; i<n; i++) { 3569ceefc113SJed Brown PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i])); 3570ceefc113SJed Brown sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol); 3571ceefc113SJed Brown } 3572ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3573ceefc113SJed Brown } else if (ts->vrtol) { /* scalar atol, vector rtol */ 3574ceefc113SJed Brown const PetscScalar *rtol; 3575ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3576ceefc113SJed Brown for (i=0; i<n; i++) { 3577ceefc113SJed Brown PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i])); 3578ceefc113SJed Brown sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol); 3579ceefc113SJed Brown } 3580ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3581ceefc113SJed Brown } else { /* scalar atol, scalar rtol */ 35821c3436cfSJed Brown for (i=0; i<n; i++) { 35831c3436cfSJed Brown PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i])); 35841c3436cfSJed Brown sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol); 35851c3436cfSJed Brown } 3586ceefc113SJed Brown } 35871c3436cfSJed Brown ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr); 35881c3436cfSJed Brown ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 35891c3436cfSJed Brown 35901c3436cfSJed Brown ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr); 35911c3436cfSJed Brown *norm = PetscSqrtReal(gsum / N); 35921c3436cfSJed Brown if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 35931c3436cfSJed Brown PetscFunctionReturn(0); 35941c3436cfSJed Brown } 35951c3436cfSJed Brown 35961c3436cfSJed Brown #undef __FUNCT__ 35978d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 35988d59e960SJed Brown /*@ 35998d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 36008d59e960SJed Brown 36018d59e960SJed Brown Logically Collective on TS 36028d59e960SJed Brown 36038d59e960SJed Brown Input Arguments: 36048d59e960SJed Brown + ts - time stepping context 36058d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 36068d59e960SJed Brown 36078d59e960SJed Brown Note: 36088d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 36098d59e960SJed Brown 36108d59e960SJed Brown Level: intermediate 36118d59e960SJed Brown 36128d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 36138d59e960SJed Brown @*/ 36148d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 36158d59e960SJed Brown { 36168d59e960SJed Brown 36178d59e960SJed Brown PetscFunctionBegin; 36188d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36198d59e960SJed Brown ts->cfltime_local = cfltime; 36208d59e960SJed Brown ts->cfltime = -1.; 36218d59e960SJed Brown PetscFunctionReturn(0); 36228d59e960SJed Brown } 36238d59e960SJed Brown 36248d59e960SJed Brown #undef __FUNCT__ 36258d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 36268d59e960SJed Brown /*@ 36278d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 36288d59e960SJed Brown 36298d59e960SJed Brown Collective on TS 36308d59e960SJed Brown 36318d59e960SJed Brown Input Arguments: 36328d59e960SJed Brown . ts - time stepping context 36338d59e960SJed Brown 36348d59e960SJed Brown Output Arguments: 36358d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 36368d59e960SJed Brown 36378d59e960SJed Brown Level: advanced 36388d59e960SJed Brown 36398d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 36408d59e960SJed Brown @*/ 36418d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 36428d59e960SJed Brown { 36438d59e960SJed Brown PetscErrorCode ierr; 36448d59e960SJed Brown 36458d59e960SJed Brown PetscFunctionBegin; 36468d59e960SJed Brown if (ts->cfltime < 0) { 36478d59e960SJed Brown ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr); 36488d59e960SJed Brown } 36498d59e960SJed Brown *cfltime = ts->cfltime; 36508d59e960SJed Brown PetscFunctionReturn(0); 36518d59e960SJed Brown } 36528d59e960SJed Brown 36538d59e960SJed Brown #undef __FUNCT__ 3654d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 3655d6ebe24aSShri Abhyankar /*@ 3656d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 3657d6ebe24aSShri Abhyankar 3658d6ebe24aSShri Abhyankar Input Parameters: 3659d6ebe24aSShri Abhyankar . ts - the TS context. 3660d6ebe24aSShri Abhyankar . xl - lower bound. 3661d6ebe24aSShri Abhyankar . xu - upper bound. 3662d6ebe24aSShri Abhyankar 3663d6ebe24aSShri Abhyankar Notes: 3664d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 366533c0c2ddSJed Brown SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp(). 3666d6ebe24aSShri Abhyankar 36672bd2b0e6SSatish Balay Level: advanced 36682bd2b0e6SSatish Balay 3669d6ebe24aSShri Abhyankar @*/ 3670d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 3671d6ebe24aSShri Abhyankar { 3672d6ebe24aSShri Abhyankar PetscErrorCode ierr; 3673d6ebe24aSShri Abhyankar SNES snes; 3674d6ebe24aSShri Abhyankar 3675d6ebe24aSShri Abhyankar PetscFunctionBegin; 3676d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3677d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 3678d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 3679d6ebe24aSShri Abhyankar } 3680d6ebe24aSShri Abhyankar 3681325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 3682c6db04a5SJed Brown #include <mex.h> 3683325fc9f4SBarry Smith 3684325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 3685325fc9f4SBarry Smith 3686325fc9f4SBarry Smith #undef __FUNCT__ 3687325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 3688325fc9f4SBarry Smith /* 3689325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 3690325fc9f4SBarry Smith TSSetFunctionMatlab(). 3691325fc9f4SBarry Smith 3692325fc9f4SBarry Smith Collective on TS 3693325fc9f4SBarry Smith 3694325fc9f4SBarry Smith Input Parameters: 3695325fc9f4SBarry Smith + snes - the TS context 3696325fc9f4SBarry Smith - x - input vector 3697325fc9f4SBarry Smith 3698325fc9f4SBarry Smith Output Parameter: 3699325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 3700325fc9f4SBarry Smith 3701325fc9f4SBarry Smith Notes: 3702325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 3703325fc9f4SBarry Smith implementations, so most users would not generally call this routine 3704325fc9f4SBarry Smith themselves. 3705325fc9f4SBarry Smith 3706325fc9f4SBarry Smith Level: developer 3707325fc9f4SBarry Smith 3708325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 3709325fc9f4SBarry Smith 3710325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3711325fc9f4SBarry Smith */ 37127087cfbeSBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx) 3713325fc9f4SBarry Smith { 3714325fc9f4SBarry Smith PetscErrorCode ierr; 3715325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3716325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 3717325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 3718325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 3719325fc9f4SBarry Smith 3720325fc9f4SBarry Smith PetscFunctionBegin; 3721325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 3722325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3723325fc9f4SBarry Smith PetscValidHeaderSpecific(xdot,VEC_CLASSID,4); 3724325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 3725325fc9f4SBarry Smith PetscCheckSameComm(snes,1,x,3); 3726325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 3727325fc9f4SBarry Smith 3728325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3729325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3730880f3077SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr); 3731325fc9f4SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 3732325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3733325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 3734325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 3735325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 3736325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 3737325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 3738325fc9f4SBarry Smith prhs[6] = sctx->ctx; 3739325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 3740325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3741325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 3742325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 3743325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 3744325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 3745325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 3746325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 3747325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 3748325fc9f4SBarry Smith PetscFunctionReturn(0); 3749325fc9f4SBarry Smith } 3750325fc9f4SBarry Smith 3751325fc9f4SBarry Smith 3752325fc9f4SBarry Smith #undef __FUNCT__ 3753325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 3754325fc9f4SBarry Smith /* 3755325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 3756325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 3757e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 3758325fc9f4SBarry Smith 3759325fc9f4SBarry Smith Logically Collective on TS 3760325fc9f4SBarry Smith 3761325fc9f4SBarry Smith Input Parameters: 3762325fc9f4SBarry Smith + ts - the TS context 3763325fc9f4SBarry Smith - func - function evaluation routine 3764325fc9f4SBarry Smith 3765325fc9f4SBarry Smith Calling sequence of func: 3766325fc9f4SBarry Smith $ func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx); 3767325fc9f4SBarry Smith 3768325fc9f4SBarry Smith Level: beginner 3769325fc9f4SBarry Smith 3770325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 3771325fc9f4SBarry Smith 3772325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3773325fc9f4SBarry Smith */ 3774cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 3775325fc9f4SBarry Smith { 3776325fc9f4SBarry Smith PetscErrorCode ierr; 3777325fc9f4SBarry Smith TSMatlabContext *sctx; 3778325fc9f4SBarry Smith 3779325fc9f4SBarry Smith PetscFunctionBegin; 3780325fc9f4SBarry Smith /* currently sctx is memory bleed */ 3781325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3782325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3783325fc9f4SBarry Smith /* 3784325fc9f4SBarry Smith This should work, but it doesn't 3785325fc9f4SBarry Smith sctx->ctx = ctx; 3786325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 3787325fc9f4SBarry Smith */ 3788325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 3789cdcf91faSSean Farley ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 3790325fc9f4SBarry Smith PetscFunctionReturn(0); 3791325fc9f4SBarry Smith } 3792325fc9f4SBarry Smith 3793325fc9f4SBarry Smith #undef __FUNCT__ 3794325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 3795325fc9f4SBarry Smith /* 3796325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 3797325fc9f4SBarry Smith TSSetJacobianMatlab(). 3798325fc9f4SBarry Smith 3799325fc9f4SBarry Smith Collective on TS 3800325fc9f4SBarry Smith 3801325fc9f4SBarry Smith Input Parameters: 3802cdcf91faSSean Farley + ts - the TS context 3803325fc9f4SBarry Smith . x - input vector 3804325fc9f4SBarry Smith . A, B - the matrices 3805325fc9f4SBarry Smith - ctx - user context 3806325fc9f4SBarry Smith 3807325fc9f4SBarry Smith Output Parameter: 3808325fc9f4SBarry Smith . flag - structure of the matrix 3809325fc9f4SBarry Smith 3810325fc9f4SBarry Smith Level: developer 3811325fc9f4SBarry Smith 3812325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 3813325fc9f4SBarry Smith 3814325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3815325fc9f4SBarry Smith @*/ 3816cdcf91faSSean Farley PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 3817325fc9f4SBarry Smith { 3818325fc9f4SBarry Smith PetscErrorCode ierr; 3819325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3820325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 3821325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 3822325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 3823325fc9f4SBarry Smith 3824325fc9f4SBarry Smith PetscFunctionBegin; 3825cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3826325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3827325fc9f4SBarry Smith 3828325fc9f4SBarry Smith /* call Matlab function in ctx with arguments x and y */ 3829325fc9f4SBarry Smith 3830cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 3831325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3832325fc9f4SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr); 3833325fc9f4SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 3834325fc9f4SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 3835325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3836325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 3837325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 3838325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 3839325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 3840325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 3841325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 3842325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 3843325fc9f4SBarry Smith prhs[8] = sctx->ctx; 3844325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 3845325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3846325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 3847325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 3848325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 3849325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 3850325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 3851325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 3852325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 3853325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 3854325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 3855325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 3856325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 3857325fc9f4SBarry Smith PetscFunctionReturn(0); 3858325fc9f4SBarry Smith } 3859325fc9f4SBarry Smith 3860325fc9f4SBarry Smith 3861325fc9f4SBarry Smith #undef __FUNCT__ 3862325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 3863325fc9f4SBarry Smith /* 3864325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 3865e3c5b3baSBarry 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 3866325fc9f4SBarry Smith 3867325fc9f4SBarry Smith Logically Collective on TS 3868325fc9f4SBarry Smith 3869325fc9f4SBarry Smith Input Parameters: 3870cdcf91faSSean Farley + ts - the TS context 3871325fc9f4SBarry Smith . A,B - Jacobian matrices 3872325fc9f4SBarry Smith . func - function evaluation routine 3873325fc9f4SBarry Smith - ctx - user context 3874325fc9f4SBarry Smith 3875325fc9f4SBarry Smith Calling sequence of func: 3876cdcf91faSSean Farley $ flag = func (TS ts,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx); 3877325fc9f4SBarry Smith 3878325fc9f4SBarry Smith 3879325fc9f4SBarry Smith Level: developer 3880325fc9f4SBarry Smith 3881325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 3882325fc9f4SBarry Smith 3883325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3884325fc9f4SBarry Smith */ 3885cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 3886325fc9f4SBarry Smith { 3887325fc9f4SBarry Smith PetscErrorCode ierr; 3888325fc9f4SBarry Smith TSMatlabContext *sctx; 3889325fc9f4SBarry Smith 3890325fc9f4SBarry Smith PetscFunctionBegin; 3891325fc9f4SBarry Smith /* currently sctx is memory bleed */ 3892325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3893325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3894325fc9f4SBarry Smith /* 3895325fc9f4SBarry Smith This should work, but it doesn't 3896325fc9f4SBarry Smith sctx->ctx = ctx; 3897325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 3898325fc9f4SBarry Smith */ 3899325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 3900cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 3901325fc9f4SBarry Smith PetscFunctionReturn(0); 3902325fc9f4SBarry Smith } 3903325fc9f4SBarry Smith 3904b5b1a830SBarry Smith #undef __FUNCT__ 3905b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 3906b5b1a830SBarry Smith /* 3907b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 3908b5b1a830SBarry Smith 3909b5b1a830SBarry Smith Collective on TS 3910b5b1a830SBarry Smith 3911b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3912b5b1a830SBarry Smith @*/ 3913cdcf91faSSean Farley PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec x, void *ctx) 3914b5b1a830SBarry Smith { 3915b5b1a830SBarry Smith PetscErrorCode ierr; 3916b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3917a530c242SBarry Smith int nlhs = 1,nrhs = 6; 3918b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 3919b5b1a830SBarry Smith long long int lx = 0,ls = 0; 3920b5b1a830SBarry Smith 3921b5b1a830SBarry Smith PetscFunctionBegin; 3922cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3923b5b1a830SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,4); 3924b5b1a830SBarry Smith 3925cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 3926b5b1a830SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3927b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3928b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 3929b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 3930b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 3931b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 3932b5b1a830SBarry Smith prhs[5] = sctx->ctx; 3933b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 3934b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3935b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 3936b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 3937b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 3938b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 3939b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 3940b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 3941b5b1a830SBarry Smith PetscFunctionReturn(0); 3942b5b1a830SBarry Smith } 3943b5b1a830SBarry Smith 3944b5b1a830SBarry Smith 3945b5b1a830SBarry Smith #undef __FUNCT__ 3946b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 3947b5b1a830SBarry Smith /* 3948b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 3949b5b1a830SBarry Smith 3950b5b1a830SBarry Smith Level: developer 3951b5b1a830SBarry Smith 3952b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 3953b5b1a830SBarry Smith 3954b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3955b5b1a830SBarry Smith */ 3956cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 3957b5b1a830SBarry Smith { 3958b5b1a830SBarry Smith PetscErrorCode ierr; 3959b5b1a830SBarry Smith TSMatlabContext *sctx; 3960b5b1a830SBarry Smith 3961b5b1a830SBarry Smith PetscFunctionBegin; 3962b5b1a830SBarry Smith /* currently sctx is memory bleed */ 3963b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3964b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3965b5b1a830SBarry Smith /* 3966b5b1a830SBarry Smith This should work, but it doesn't 3967b5b1a830SBarry Smith sctx->ctx = ctx; 3968b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 3969b5b1a830SBarry Smith */ 3970b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 3971cdcf91faSSean Farley ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 3972b5b1a830SBarry Smith PetscFunctionReturn(0); 3973b5b1a830SBarry Smith } 3974325fc9f4SBarry Smith #endif 3975b3603a34SBarry Smith 39760b039ecaSBarry Smith 39770b039ecaSBarry Smith 3978b3603a34SBarry Smith #undef __FUNCT__ 39794f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 3980b3603a34SBarry Smith /*@C 39814f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 3982b3603a34SBarry Smith in a time based line graph 3983b3603a34SBarry Smith 3984b3603a34SBarry Smith Collective on TS 3985b3603a34SBarry Smith 3986b3603a34SBarry Smith Input Parameters: 3987b3603a34SBarry Smith + ts - the TS context 3988b3603a34SBarry Smith . step - current time-step 3989b3603a34SBarry Smith . ptime - current time 3990b3603a34SBarry Smith - lg - a line graph object 3991b3603a34SBarry Smith 3992b3603a34SBarry Smith Level: intermediate 3993b3603a34SBarry Smith 39940b039ecaSBarry Smith Notes: each process in a parallel run displays its component solutions in a separate window 3995b3603a34SBarry Smith 3996b3603a34SBarry Smith .keywords: TS, vector, monitor, view 3997b3603a34SBarry Smith 3998b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3999b3603a34SBarry Smith @*/ 40004f09c107SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 4001b3603a34SBarry Smith { 4002b3603a34SBarry Smith PetscErrorCode ierr; 40030b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4004b3603a34SBarry Smith const PetscScalar *yy; 400558ff32f7SBarry Smith PetscInt dim; 4006b3603a34SBarry Smith 4007b3603a34SBarry Smith PetscFunctionBegin; 400858ff32f7SBarry Smith if (!step) { 4009a9f9c1f6SBarry Smith PetscDrawAxis axis; 4010a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4011a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 401258ff32f7SBarry Smith ierr = VecGetLocalSize(x,&dim);CHKERRQ(ierr); 40130b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 40140b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 401558ff32f7SBarry Smith } 4016b3603a34SBarry Smith ierr = VecGetArrayRead(x,&yy);CHKERRQ(ierr); 4017e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 4018e3efe391SJed Brown { 4019e3efe391SJed Brown PetscReal *yreal; 4020e3efe391SJed Brown PetscInt i,n; 4021e3efe391SJed Brown ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr); 4022e3efe391SJed Brown ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4023e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 40240b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4025e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4026e3efe391SJed Brown } 4027e3efe391SJed Brown #else 40280b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4029e3efe391SJed Brown #endif 4030b3603a34SBarry Smith ierr = VecRestoreArrayRead(x,&yy);CHKERRQ(ierr); 40313923b477SBarry Smith if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && (step == -1))){ 40320b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 40333923b477SBarry Smith } 4034b3603a34SBarry Smith PetscFunctionReturn(0); 4035b3603a34SBarry Smith } 4036b3603a34SBarry Smith 4037b3603a34SBarry Smith #undef __FUNCT__ 40384f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 4039ef20d060SBarry Smith /*@C 40404f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 4041ef20d060SBarry Smith in a time based line graph 4042ef20d060SBarry Smith 4043ef20d060SBarry Smith Collective on TS 4044ef20d060SBarry Smith 4045ef20d060SBarry Smith Input Parameters: 4046ef20d060SBarry Smith + ts - the TS context 4047ef20d060SBarry Smith . step - current time-step 4048ef20d060SBarry Smith . ptime - current time 4049ef20d060SBarry Smith - lg - a line graph object 4050ef20d060SBarry Smith 4051ef20d060SBarry Smith Level: intermediate 4052ef20d060SBarry Smith 4053abd5a294SJed Brown Notes: 4054abd5a294SJed Brown Only for sequential solves. 4055abd5a294SJed Brown 4056abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 4057abd5a294SJed Brown 4058abd5a294SJed Brown Options Database Keys: 40594f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 4060ef20d060SBarry Smith 4061ef20d060SBarry Smith .keywords: TS, vector, monitor, view 4062ef20d060SBarry Smith 4063abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 4064ef20d060SBarry Smith @*/ 40654f09c107SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 4066ef20d060SBarry Smith { 4067ef20d060SBarry Smith PetscErrorCode ierr; 40680b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4069ef20d060SBarry Smith const PetscScalar *yy; 4070ef20d060SBarry Smith Vec y; 4071a9f9c1f6SBarry Smith PetscInt dim; 4072ef20d060SBarry Smith 4073ef20d060SBarry Smith PetscFunctionBegin; 4074a9f9c1f6SBarry Smith if (!step) { 4075a9f9c1f6SBarry Smith PetscDrawAxis axis; 4076a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4077a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 4078a9f9c1f6SBarry Smith ierr = VecGetLocalSize(x,&dim);CHKERRQ(ierr); 4079a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 4080a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4081a9f9c1f6SBarry Smith } 4082ef20d060SBarry Smith ierr = VecDuplicate(x,&y);CHKERRQ(ierr); 4083ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 4084ef20d060SBarry Smith ierr = VecAXPY(y,-1.0,x);CHKERRQ(ierr); 4085ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 4086e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 4087e3efe391SJed Brown { 4088e3efe391SJed Brown PetscReal *yreal; 4089e3efe391SJed Brown PetscInt i,n; 4090e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 4091e3efe391SJed Brown ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4092e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 40930b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4094e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4095e3efe391SJed Brown } 4096e3efe391SJed Brown #else 40970b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4098e3efe391SJed Brown #endif 4099ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 4100ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 41013923b477SBarry Smith if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && (step == -1))){ 41020b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 41033923b477SBarry Smith } 4104ef20d060SBarry Smith PetscFunctionReturn(0); 4105ef20d060SBarry Smith } 4106ef20d060SBarry Smith 4107*201da799SBarry Smith #undef __FUNCT__ 4108*201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 4109*201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4110*201da799SBarry Smith { 4111*201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4112*201da799SBarry Smith PetscReal x = ptime,y; 4113*201da799SBarry Smith PetscErrorCode ierr; 4114*201da799SBarry Smith PetscInt its; 4115*201da799SBarry Smith 4116*201da799SBarry Smith PetscFunctionBegin; 4117*201da799SBarry Smith if (!n) { 4118*201da799SBarry Smith PetscDrawAxis axis; 4119*201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4120*201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 4121*201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4122*201da799SBarry Smith ctx->snes_its = 0; 4123*201da799SBarry Smith } 4124*201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 4125*201da799SBarry Smith y = its - ctx->snes_its; 4126*201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4127*201da799SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))){ 4128*201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4129*201da799SBarry Smith } 4130*201da799SBarry Smith ctx->snes_its = its; 4131*201da799SBarry Smith PetscFunctionReturn(0); 4132*201da799SBarry Smith } 4133*201da799SBarry Smith 4134*201da799SBarry Smith #undef __FUNCT__ 4135*201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 4136*201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 4137*201da799SBarry Smith { 4138*201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 4139*201da799SBarry Smith PetscReal x = ptime,y; 4140*201da799SBarry Smith PetscErrorCode ierr; 4141*201da799SBarry Smith PetscInt its; 4142*201da799SBarry Smith 4143*201da799SBarry Smith PetscFunctionBegin; 4144*201da799SBarry Smith if (!n) { 4145*201da799SBarry Smith PetscDrawAxis axis; 4146*201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4147*201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 4148*201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4149*201da799SBarry Smith ctx->ksp_its = 0; 4150*201da799SBarry Smith } 4151*201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 4152*201da799SBarry Smith y = its - ctx->ksp_its; 4153*201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 4154*201da799SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))){ 4155*201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4156*201da799SBarry Smith } 4157*201da799SBarry Smith ctx->ksp_its = its; 4158*201da799SBarry Smith PetscFunctionReturn(0); 4159*201da799SBarry Smith } 4160