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 64a9f9c1f6SBarry Smith - -ts_monitor_draw - plot time-step information at each timestep 65bdad233fSMatthew Knepley 66bdad233fSMatthew Knepley Level: beginner 67bdad233fSMatthew Knepley 68bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 69bdad233fSMatthew Knepley 70a313700dSBarry Smith .seealso: TSGetType() 71bdad233fSMatthew Knepley @*/ 727087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 73bdad233fSMatthew Knepley { 74ace3abfcSBarry Smith PetscBool opt,flg; 75dfbe8321SBarry Smith PetscErrorCode ierr; 76649052a6SBarry Smith PetscViewer monviewer; 77eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 78089b2837SJed Brown SNES snes; 791c3436cfSJed Brown TSAdapt adapt; 8031748224SBarry Smith PetscReal time_step; 81bdad233fSMatthew Knepley 82bdad233fSMatthew Knepley PetscFunctionBegin; 830700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 843194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 85a43b19c4SJed Brown /* Handle TS type options */ 86a43b19c4SJed Brown ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr); 87bdad233fSMatthew Knepley 88bdad233fSMatthew Knepley /* Handle generic TS options */ 89bdad233fSMatthew Knepley ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr); 903bca7d26SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr); 91d8cd7023SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr); 9231748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 9331748224SBarry Smith if (flg) { 9431748224SBarry Smith ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 9531748224SBarry Smith } 96a43b19c4SJed Brown opt = ts->exact_final_time == PETSC_DECIDE ? PETSC_FALSE : (PetscBool)ts->exact_final_time; 97a43b19c4SJed Brown ierr = PetscOptionsBool("-ts_exact_final_time","Interpolate output to stop exactly at the final time","TSSetExactFinalTime",opt,&opt,&flg);CHKERRQ(ierr); 98461e00b3SSean Farley if (flg) {ierr = TSSetExactFinalTime(ts,opt);CHKERRQ(ierr);} 99cef5090cSJed 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); 100cef5090cSJed 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); 101cef5090cSJed Brown ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr); 1021c3436cfSJed Brown ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,PETSC_NULL);CHKERRQ(ierr); 1031c3436cfSJed Brown ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,PETSC_NULL);CHKERRQ(ierr); 104bdad233fSMatthew Knepley 105bdad233fSMatthew Knepley /* Monitor options */ 106a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 107eabae89aSBarry Smith if (flg) { 108649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr); 109649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 110bdad233fSMatthew Knepley } 1115180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1125180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1135180491cSLisandro Dalcin 114a9f9c1f6SBarry Smith ierr = PetscOptionsName("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorTimeStep",&opt);CHKERRQ(ierr); 115a7cc72afSBarry Smith if (opt) { 1160b039ecaSBarry Smith TSMonitorLGCtx ctx; 117*3923b477SBarry Smith PetscInt howoften = 1; 118a80ad3e0SBarry Smith 119*3923b477SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorTimeStep",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 120*3923b477SBarry Smith ierr = TSMonitorLGCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx); 121a9f9c1f6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 122b3603a34SBarry Smith } 123a9f9c1f6SBarry Smith ierr = PetscOptionsName("-ts_monitor_solutionode","Monitor solution graphically","TSMonitorSolutionODE",&opt);CHKERRQ(ierr); 124b3603a34SBarry Smith if (opt) { 1250b039ecaSBarry Smith TSMonitorLGCtx ctx; 126*3923b477SBarry Smith PetscInt howoften = 1; 127b3603a34SBarry Smith 128*3923b477SBarry Smith ierr = PetscOptionsInt("-ts_monitor_solutionode","Monitor solution graphically","TSMonitorSolutionODE",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 129*3923b477SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 130a9f9c1f6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionODE,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 131bdad233fSMatthew Knepley } 132a9f9c1f6SBarry Smith ierr = PetscOptionsName("-ts_monitor_errorode","Monitor error graphically","TSMonitorErrorODE",&opt);CHKERRQ(ierr); 133ef20d060SBarry Smith if (opt) { 1340b039ecaSBarry Smith TSMonitorLGCtx ctx; 135*3923b477SBarry Smith PetscInt howoften = 1; 136ef20d060SBarry Smith 137*3923b477SBarry Smith ierr = PetscOptionsInt("-ts_monitor_errorode","Monitor error graphically","TSMonitorErrorODE",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr); 138*3923b477SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx); 139a9f9c1f6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorErrorODE,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 140ef20d060SBarry Smith } 141ef20d060SBarry Smith opt = PETSC_FALSE; 142acfcf0e5SJed Brown ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr); 143a7cc72afSBarry Smith if (opt) { 1446083293cSBarry Smith void *ctx; 145a80ad3e0SBarry Smith PetscViewer viewer; 146a80ad3e0SBarry Smith 147ab352700SBarry Smith ierr = PetscViewerDrawOpen(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,&viewer); 148a80ad3e0SBarry Smith ierr = TSMonitorSolutionCreate(ts,viewer,&ctx);CHKERRQ(ierr); 149a80ad3e0SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 1506083293cSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolution,ctx,TSMonitorSolutionDestroy);CHKERRQ(ierr); 151bdad233fSMatthew Knepley } 152fb1732b5SBarry Smith opt = PETSC_FALSE; 153fb1732b5SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 154fb1732b5SBarry Smith if (flg) { 155fb1732b5SBarry Smith PetscViewer ctx; 156fb1732b5SBarry Smith if (monfilename[0]) { 157fb1732b5SBarry Smith ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 158fb1732b5SBarry Smith } else { 159fb1732b5SBarry Smith ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm); 160fb1732b5SBarry Smith } 161fb1732b5SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 162fb1732b5SBarry Smith } 163ed81e22dSJed Brown opt = PETSC_FALSE; 164ed81e22dSJed 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); 165ed81e22dSJed Brown if (flg) { 166ed81e22dSJed Brown const char *ptr,*ptr2; 167ed81e22dSJed Brown char *filetemplate; 168ed81e22dSJed Brown if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 169ed81e22dSJed Brown /* Do some cursory validation of the input. */ 170ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 171ed81e22dSJed Brown if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 172ed81e22dSJed Brown for (ptr++ ; ptr && *ptr; ptr++) { 173ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 174ed81e22dSJed 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"); 175ed81e22dSJed Brown if (ptr2) break; 176ed81e22dSJed Brown } 177ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 178ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 179ed81e22dSJed Brown } 180bdad233fSMatthew Knepley 1811c3436cfSJed Brown ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr); 1821c3436cfSJed Brown ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr); 1831c3436cfSJed Brown 184d52bd9f3SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 185d52bd9f3SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 186d52bd9f3SBarry Smith 187bdad233fSMatthew Knepley /* Handle specific TS options */ 188abc0a331SBarry Smith if (ts->ops->setfromoptions) { 189bdad233fSMatthew Knepley ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr); 190bdad233fSMatthew Knepley } 1915d973c19SBarry Smith 1925d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 1935d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 194bdad233fSMatthew Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 195bdad233fSMatthew Knepley PetscFunctionReturn(0); 196bdad233fSMatthew Knepley } 197bdad233fSMatthew Knepley 198bdad233fSMatthew Knepley #undef __FUNCT__ 199cdcf91faSSean Farley #undef __FUNCT__ 2004a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian" 201a7a1495cSBarry Smith /*@ 2028c385f81SBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 203a7a1495cSBarry Smith set with TSSetRHSJacobian(). 204a7a1495cSBarry Smith 205a7a1495cSBarry Smith Collective on TS and Vec 206a7a1495cSBarry Smith 207a7a1495cSBarry Smith Input Parameters: 208316643e7SJed Brown + ts - the TS context 209a7a1495cSBarry Smith . t - current timestep 210a7a1495cSBarry Smith - x - input vector 211a7a1495cSBarry Smith 212a7a1495cSBarry Smith Output Parameters: 213a7a1495cSBarry Smith + A - Jacobian matrix 214a7a1495cSBarry Smith . B - optional preconditioning matrix 215a7a1495cSBarry Smith - flag - flag indicating matrix structure 216a7a1495cSBarry Smith 217a7a1495cSBarry Smith Notes: 218a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 219a7a1495cSBarry Smith is used internally within the nonlinear solvers. 220a7a1495cSBarry Smith 22194b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 222a7a1495cSBarry Smith flag parameter. 223a7a1495cSBarry Smith 224a7a1495cSBarry Smith Level: developer 225a7a1495cSBarry Smith 226a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 227a7a1495cSBarry Smith 22894b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 229a7a1495cSBarry Smith @*/ 2307087cfbeSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg) 231a7a1495cSBarry Smith { 232dfbe8321SBarry Smith PetscErrorCode ierr; 2330e4ef248SJed Brown PetscInt Xstate; 23424989b8cSPeter Brune DM dm; 23524989b8cSPeter Brune TSDM tsdm; 23624989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 23724989b8cSPeter Brune void *ctx; 23824989b8cSPeter Brune TSIJacobian ijacobianfunc; 239a7a1495cSBarry Smith 240a7a1495cSBarry Smith PetscFunctionBegin; 2410700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2420700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 243c9780b6fSBarry Smith PetscCheckSameComm(ts,1,X,3); 24424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 24524989b8cSPeter Brune ierr = DMTSGetContext(dm,&tsdm);CHKERRQ(ierr); 24624989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 24724989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr); 2480e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr); 2490e4ef248SJed Brown if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) { 2500e4ef248SJed Brown *flg = ts->rhsjacobian.mstructure; 2510e4ef248SJed Brown PetscFunctionReturn(0); 252f8ede8e7SJed Brown } 253d90be118SSean Farley 25424989b8cSPeter Brune if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 255d90be118SSean Farley 25624989b8cSPeter Brune if (rhsjacobianfunc) { 257d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 258a7a1495cSBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 259a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 26024989b8cSPeter Brune ierr = (*rhsjacobianfunc)(ts,t,X,A,B,flg,ctx);CHKERRQ(ierr); 261a7a1495cSBarry Smith PetscStackPop; 262d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 263a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 2640700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 2650700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 266ef66eb69SBarry Smith } else { 267214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 268214bc6a2SJed Brown if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);} 269214bc6a2SJed Brown *flg = SAME_NONZERO_PATTERN; 270ef66eb69SBarry Smith } 2710e4ef248SJed Brown ts->rhsjacobian.time = t; 2720e4ef248SJed Brown ts->rhsjacobian.X = X; 2730e4ef248SJed Brown ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 2740e4ef248SJed Brown ts->rhsjacobian.mstructure = *flg; 275a7a1495cSBarry Smith PetscFunctionReturn(0); 276a7a1495cSBarry Smith } 277a7a1495cSBarry Smith 2784a2ae208SSatish Balay #undef __FUNCT__ 2794a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 280316643e7SJed Brown /*@ 281d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 282d763cef2SBarry Smith 283316643e7SJed Brown Collective on TS and Vec 284316643e7SJed Brown 285316643e7SJed Brown Input Parameters: 286316643e7SJed Brown + ts - the TS context 287316643e7SJed Brown . t - current time 288316643e7SJed Brown - x - state vector 289316643e7SJed Brown 290316643e7SJed Brown Output Parameter: 291316643e7SJed Brown . y - right hand side 292316643e7SJed Brown 293316643e7SJed Brown Note: 294316643e7SJed Brown Most users should not need to explicitly call this routine, as it 295316643e7SJed Brown is used internally within the nonlinear solvers. 296316643e7SJed Brown 297316643e7SJed Brown Level: developer 298316643e7SJed Brown 299316643e7SJed Brown .keywords: TS, compute 300316643e7SJed Brown 301316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 302316643e7SJed Brown @*/ 303dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y) 304d763cef2SBarry Smith { 305dfbe8321SBarry Smith PetscErrorCode ierr; 306d763cef2SBarry Smith 30724989b8cSPeter Brune TSRHSFunction rhsfunction; 30824989b8cSPeter Brune TSIFunction ifunction; 30924989b8cSPeter Brune void *ctx; 31024989b8cSPeter Brune DM dm; 31124989b8cSPeter Brune 312d763cef2SBarry Smith PetscFunctionBegin; 3130700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3140700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3150700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 31624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 31724989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 31824989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr); 319d763cef2SBarry Smith 32024989b8cSPeter Brune if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 321d763cef2SBarry Smith 322d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 32324989b8cSPeter Brune if (rhsfunction) { 324d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 32524989b8cSPeter Brune ierr = (*rhsfunction)(ts,t,x,y,ctx);CHKERRQ(ierr); 326d763cef2SBarry Smith PetscStackPop; 327214bc6a2SJed Brown } else { 328214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 329b2cd27e8SJed Brown } 33044a41b28SSean Farley 331d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 332d763cef2SBarry Smith PetscFunctionReturn(0); 333d763cef2SBarry Smith } 334d763cef2SBarry Smith 3354a2ae208SSatish Balay #undef __FUNCT__ 336ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 337ef20d060SBarry Smith /*@ 338ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 339ef20d060SBarry Smith 340ef20d060SBarry Smith Collective on TS and Vec 341ef20d060SBarry Smith 342ef20d060SBarry Smith Input Parameters: 343ef20d060SBarry Smith + ts - the TS context 344ef20d060SBarry Smith - t - current time 345ef20d060SBarry Smith 346ef20d060SBarry Smith Output Parameter: 347ef20d060SBarry Smith . y - right hand side 348ef20d060SBarry Smith 349ef20d060SBarry Smith Note: 350ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 351ef20d060SBarry Smith is used internally within the nonlinear solvers. 352ef20d060SBarry Smith 353ef20d060SBarry Smith Level: developer 354ef20d060SBarry Smith 355ef20d060SBarry Smith .keywords: TS, compute 356ef20d060SBarry Smith 357abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 358ef20d060SBarry Smith @*/ 359ef20d060SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec x) 360ef20d060SBarry Smith { 361ef20d060SBarry Smith PetscErrorCode ierr; 362ef20d060SBarry Smith TSSolutionFunction solutionfunction; 363ef20d060SBarry Smith void *ctx; 364ef20d060SBarry Smith DM dm; 365ef20d060SBarry Smith 366ef20d060SBarry Smith PetscFunctionBegin; 367ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 368ef20d060SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 369ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 370ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 371ef20d060SBarry Smith 372ef20d060SBarry Smith if (solutionfunction) { 373ef20d060SBarry Smith PetscStackPush("TS user right-hand-side function"); 374ef20d060SBarry Smith ierr = (*solutionfunction)(ts,t,x,ctx);CHKERRQ(ierr); 375ef20d060SBarry Smith PetscStackPop; 376ef20d060SBarry Smith } 377ef20d060SBarry Smith PetscFunctionReturn(0); 378ef20d060SBarry Smith } 379ef20d060SBarry Smith 380ef20d060SBarry Smith #undef __FUNCT__ 381214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 382214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 383214bc6a2SJed Brown { 3842dd45cf8SJed Brown Vec F; 385214bc6a2SJed Brown PetscErrorCode ierr; 386214bc6a2SJed Brown 387214bc6a2SJed Brown PetscFunctionBegin; 3880da9a4f0SJed Brown *Frhs = PETSC_NULL; 3892dd45cf8SJed Brown ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 390214bc6a2SJed Brown if (!ts->Frhs) { 3912dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 392214bc6a2SJed Brown } 393214bc6a2SJed Brown *Frhs = ts->Frhs; 394214bc6a2SJed Brown PetscFunctionReturn(0); 395214bc6a2SJed Brown } 396214bc6a2SJed Brown 397214bc6a2SJed Brown #undef __FUNCT__ 398214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 399214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 400214bc6a2SJed Brown { 401214bc6a2SJed Brown Mat A,B; 4022dd45cf8SJed Brown PetscErrorCode ierr; 403214bc6a2SJed Brown 404214bc6a2SJed Brown PetscFunctionBegin; 405214bc6a2SJed Brown ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 406214bc6a2SJed Brown if (Arhs) { 407214bc6a2SJed Brown if (!ts->Arhs) { 408214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 409214bc6a2SJed Brown } 410214bc6a2SJed Brown *Arhs = ts->Arhs; 411214bc6a2SJed Brown } 412214bc6a2SJed Brown if (Brhs) { 413214bc6a2SJed Brown if (!ts->Brhs) { 414214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 415214bc6a2SJed Brown } 416214bc6a2SJed Brown *Brhs = ts->Brhs; 417214bc6a2SJed Brown } 418214bc6a2SJed Brown PetscFunctionReturn(0); 419214bc6a2SJed Brown } 420214bc6a2SJed Brown 421214bc6a2SJed Brown #undef __FUNCT__ 422316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 423316643e7SJed Brown /*@ 424316643e7SJed Brown TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0 425316643e7SJed Brown 426316643e7SJed Brown Collective on TS and Vec 427316643e7SJed Brown 428316643e7SJed Brown Input Parameters: 429316643e7SJed Brown + ts - the TS context 430316643e7SJed Brown . t - current time 431316643e7SJed Brown . X - state vector 432214bc6a2SJed Brown . Xdot - time derivative of state vector 433214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 434316643e7SJed Brown 435316643e7SJed Brown Output Parameter: 436316643e7SJed Brown . Y - right hand side 437316643e7SJed Brown 438316643e7SJed Brown Note: 439316643e7SJed Brown Most users should not need to explicitly call this routine, as it 440316643e7SJed Brown is used internally within the nonlinear solvers. 441316643e7SJed Brown 442316643e7SJed Brown If the user did did not write their equations in implicit form, this 443316643e7SJed Brown function recasts them in implicit form. 444316643e7SJed Brown 445316643e7SJed Brown Level: developer 446316643e7SJed Brown 447316643e7SJed Brown .keywords: TS, compute 448316643e7SJed Brown 449316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 450316643e7SJed Brown @*/ 451214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex) 452316643e7SJed Brown { 453316643e7SJed Brown PetscErrorCode ierr; 45424989b8cSPeter Brune TSIFunction ifunction; 45524989b8cSPeter Brune TSRHSFunction rhsfunction; 45624989b8cSPeter Brune void *ctx; 45724989b8cSPeter Brune DM dm; 458316643e7SJed Brown 459316643e7SJed Brown PetscFunctionBegin; 4600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4610700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 4620700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 4630700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 464316643e7SJed Brown 46524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 46624989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 46724989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr); 46824989b8cSPeter Brune 46924989b8cSPeter Brune if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 470d90be118SSean Farley 471316643e7SJed Brown ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 47224989b8cSPeter Brune if (ifunction) { 473316643e7SJed Brown PetscStackPush("TS user implicit function"); 47424989b8cSPeter Brune ierr = (*ifunction)(ts,t,X,Xdot,Y,ctx);CHKERRQ(ierr); 475316643e7SJed Brown PetscStackPop; 476214bc6a2SJed Brown } 477214bc6a2SJed Brown if (imex) { 47824989b8cSPeter Brune if (!ifunction) { 4792dd45cf8SJed Brown ierr = VecCopy(Xdot,Y);CHKERRQ(ierr); 4802dd45cf8SJed Brown } 48124989b8cSPeter Brune } else if (rhsfunction) { 48224989b8cSPeter Brune if (ifunction) { 483214bc6a2SJed Brown Vec Frhs; 484214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 485214bc6a2SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr); 486214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 4872dd45cf8SJed Brown } else { 4882dd45cf8SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr); 4892dd45cf8SJed Brown ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr); 490316643e7SJed Brown } 4914a6899ffSJed Brown } 492316643e7SJed Brown ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 493316643e7SJed Brown PetscFunctionReturn(0); 494316643e7SJed Brown } 495316643e7SJed Brown 496316643e7SJed Brown #undef __FUNCT__ 497316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 498316643e7SJed Brown /*@ 499316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 500316643e7SJed Brown 501316643e7SJed Brown Collective on TS and Vec 502316643e7SJed Brown 503316643e7SJed Brown Input 504316643e7SJed Brown Input Parameters: 505316643e7SJed Brown + ts - the TS context 506316643e7SJed Brown . t - current timestep 507316643e7SJed Brown . X - state vector 508316643e7SJed Brown . Xdot - time derivative of state vector 509214bc6a2SJed Brown . shift - shift to apply, see note below 510214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 511316643e7SJed Brown 512316643e7SJed Brown Output Parameters: 513316643e7SJed Brown + A - Jacobian matrix 514316643e7SJed Brown . B - optional preconditioning matrix 515316643e7SJed Brown - flag - flag indicating matrix structure 516316643e7SJed Brown 517316643e7SJed Brown Notes: 518316643e7SJed Brown If F(t,X,Xdot)=0 is the DAE, the required Jacobian is 519316643e7SJed Brown 520316643e7SJed Brown dF/dX + shift*dF/dXdot 521316643e7SJed Brown 522316643e7SJed Brown Most users should not need to explicitly call this routine, as it 523316643e7SJed Brown is used internally within the nonlinear solvers. 524316643e7SJed Brown 525316643e7SJed Brown Level: developer 526316643e7SJed Brown 527316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 528316643e7SJed Brown 529316643e7SJed Brown .seealso: TSSetIJacobian() 53063495f91SJed Brown @*/ 531214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex) 532316643e7SJed Brown { 5330026cea9SSean Farley PetscInt Xstate, Xdotstate; 534316643e7SJed Brown PetscErrorCode ierr; 53524989b8cSPeter Brune TSIJacobian ijacobian; 53624989b8cSPeter Brune TSRHSJacobian rhsjacobian; 53724989b8cSPeter Brune DM dm; 53824989b8cSPeter Brune void *ctx; 539316643e7SJed Brown 540316643e7SJed Brown PetscFunctionBegin; 54124989b8cSPeter Brune 5420700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5430700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 5440700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 545316643e7SJed Brown PetscValidPointer(A,6); 5460700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,6); 547316643e7SJed Brown PetscValidPointer(B,7); 5480700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,7); 549316643e7SJed Brown PetscValidPointer(flg,8); 55024989b8cSPeter Brune 55124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 55224989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 55324989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr); 55424989b8cSPeter Brune 5550026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr); 5560026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr); 5570026cea9SSean 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))) { 5580026cea9SSean Farley *flg = ts->ijacobian.mstructure; 5590026cea9SSean Farley ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 5600026cea9SSean Farley PetscFunctionReturn(0); 5610026cea9SSean Farley } 562316643e7SJed Brown 56324989b8cSPeter Brune if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 564316643e7SJed Brown 5654e684422SJed Brown *flg = SAME_NONZERO_PATTERN; /* In case we're solving a linear problem in which case it wouldn't get initialized below. */ 566316643e7SJed Brown ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 56724989b8cSPeter Brune if (ijacobian) { 5682dd45cf8SJed Brown *flg = DIFFERENT_NONZERO_PATTERN; 569316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 57024989b8cSPeter Brune ierr = (*ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ctx);CHKERRQ(ierr); 571316643e7SJed Brown PetscStackPop; 572214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 573214bc6a2SJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 574214bc6a2SJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 5754a6899ffSJed Brown } 576214bc6a2SJed Brown if (imex) { 57724989b8cSPeter Brune if (!ijacobian) { /* system was written as Xdot = F(t,X) */ 578214bc6a2SJed Brown ierr = MatZeroEntries(*A);CHKERRQ(ierr); 5792dd45cf8SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 580214bc6a2SJed Brown if (*A != *B) { 581214bc6a2SJed Brown ierr = MatZeroEntries(*B);CHKERRQ(ierr); 582214bc6a2SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 583214bc6a2SJed Brown } 584214bc6a2SJed Brown *flg = SAME_PRECONDITIONER; 585214bc6a2SJed Brown } 586214bc6a2SJed Brown } else { 58724989b8cSPeter Brune if (!ijacobian) { 588214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr); 589214bc6a2SJed Brown ierr = MatScale(*A,-1);CHKERRQ(ierr); 590214bc6a2SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 591316643e7SJed Brown if (*A != *B) { 592316643e7SJed Brown ierr = MatScale(*B,-1);CHKERRQ(ierr); 593316643e7SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 594316643e7SJed Brown } 59524989b8cSPeter Brune } else if (rhsjacobian) { 596214bc6a2SJed Brown Mat Arhs,Brhs; 597214bc6a2SJed Brown MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN; 598214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 599214bc6a2SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 600214bc6a2SJed Brown axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN; 601214bc6a2SJed Brown ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr); 602214bc6a2SJed Brown if (*A != *B) { 603214bc6a2SJed Brown ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr); 604214bc6a2SJed Brown } 605214bc6a2SJed Brown *flg = PetscMin(*flg,flg2); 606214bc6a2SJed Brown } 607316643e7SJed Brown } 6080026cea9SSean Farley 6090026cea9SSean Farley ts->ijacobian.time = t; 6100026cea9SSean Farley ts->ijacobian.X = X; 6110026cea9SSean Farley ts->ijacobian.Xdot = Xdot; 6120026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr); 6130026cea9SSean Farley ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr); 6140026cea9SSean Farley ts->ijacobian.shift = shift; 6150026cea9SSean Farley ts->ijacobian.imex = imex; 6160026cea9SSean Farley ts->ijacobian.mstructure = *flg; 617316643e7SJed Brown ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 618316643e7SJed Brown PetscFunctionReturn(0); 619316643e7SJed Brown } 620316643e7SJed Brown 621316643e7SJed Brown #undef __FUNCT__ 6224a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 623d763cef2SBarry Smith /*@C 624d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 625d763cef2SBarry Smith F(t,u), where U_t = F(t,u). 626d763cef2SBarry Smith 6273f9fe445SBarry Smith Logically Collective on TS 628d763cef2SBarry Smith 629d763cef2SBarry Smith Input Parameters: 630d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 631ca94891dSJed Brown . r - vector to put the computed right hand side (or PETSC_NULL to have it created) 632d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 633d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 634d763cef2SBarry Smith function evaluation routine (may be PETSC_NULL) 635d763cef2SBarry Smith 636d763cef2SBarry Smith Calling sequence of func: 63787828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 638d763cef2SBarry Smith 639d763cef2SBarry Smith + t - current timestep 640d763cef2SBarry Smith . u - input vector 641d763cef2SBarry Smith . F - function vector 642d763cef2SBarry Smith - ctx - [optional] user-defined function context 643d763cef2SBarry Smith 644d763cef2SBarry Smith Level: beginner 645d763cef2SBarry Smith 646d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 647d763cef2SBarry Smith 648d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian() 649d763cef2SBarry Smith @*/ 650089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 651d763cef2SBarry Smith { 652089b2837SJed Brown PetscErrorCode ierr; 653089b2837SJed Brown SNES snes; 654e856ceecSJed Brown Vec ralloc = PETSC_NULL; 65524989b8cSPeter Brune DM dm; 656d763cef2SBarry Smith 657089b2837SJed Brown PetscFunctionBegin; 6580700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 659ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 66024989b8cSPeter Brune 66124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 66224989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 663089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 664e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 665e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 666e856ceecSJed Brown r = ralloc; 667e856ceecSJed Brown } 668089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 669e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 670d763cef2SBarry Smith PetscFunctionReturn(0); 671d763cef2SBarry Smith } 672d763cef2SBarry Smith 6734a2ae208SSatish Balay #undef __FUNCT__ 674ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 675ef20d060SBarry Smith /*@C 676abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 677ef20d060SBarry Smith 678ef20d060SBarry Smith Logically Collective on TS 679ef20d060SBarry Smith 680ef20d060SBarry Smith Input Parameters: 681ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 682ef20d060SBarry Smith . f - routine for evaluating the solution 683ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 684ef20d060SBarry Smith function evaluation routine (may be PETSC_NULL) 685ef20d060SBarry Smith 686ef20d060SBarry Smith Calling sequence of func: 687ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 688ef20d060SBarry Smith 689ef20d060SBarry Smith + t - current timestep 690ef20d060SBarry Smith . u - output vector 691ef20d060SBarry Smith - ctx - [optional] user-defined function context 692ef20d060SBarry Smith 693abd5a294SJed Brown Notes: 694abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 695abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 696abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 697abd5a294SJed Brown 698abd5a294SJed Brown For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorErrorODE() can be used to monitor the error history. 699abd5a294SJed Brown 700ef20d060SBarry Smith Level: beginner 701ef20d060SBarry Smith 702ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 703ef20d060SBarry Smith 704abd5a294SJed Brown .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction() 705ef20d060SBarry Smith @*/ 706ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 707ef20d060SBarry Smith { 708ef20d060SBarry Smith PetscErrorCode ierr; 709ef20d060SBarry Smith DM dm; 710ef20d060SBarry Smith 711ef20d060SBarry Smith PetscFunctionBegin; 712ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 713ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 714ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 715ef20d060SBarry Smith PetscFunctionReturn(0); 716ef20d060SBarry Smith } 717ef20d060SBarry Smith 718ef20d060SBarry Smith #undef __FUNCT__ 7194a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 720d763cef2SBarry Smith /*@C 721d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 722d763cef2SBarry Smith where U_t = F(U,t), as well as the location to store the matrix. 723d763cef2SBarry Smith 7243f9fe445SBarry Smith Logically Collective on TS 725d763cef2SBarry Smith 726d763cef2SBarry Smith Input Parameters: 727d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 728d763cef2SBarry Smith . A - Jacobian matrix 729d763cef2SBarry Smith . B - preconditioner matrix (usually same as A) 730d763cef2SBarry Smith . f - the Jacobian evaluation routine 731d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 732d763cef2SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) 733d763cef2SBarry Smith 734d763cef2SBarry Smith Calling sequence of func: 73587828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 736d763cef2SBarry Smith 737d763cef2SBarry Smith + t - current timestep 738d763cef2SBarry Smith . u - input vector 739d763cef2SBarry Smith . A - matrix A, where U_t = A(t)u 740d763cef2SBarry Smith . B - preconditioner matrix, usually the same as A 741d763cef2SBarry Smith . flag - flag indicating information about the preconditioner matrix 74294b7f48cSBarry Smith structure (same as flag in KSPSetOperators()) 743d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 744d763cef2SBarry Smith 745d763cef2SBarry Smith Notes: 74694b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 747d763cef2SBarry Smith output parameter in the routine func(). Be sure to read this information! 748d763cef2SBarry Smith 749d763cef2SBarry Smith The routine func() takes Mat * as the matrix arguments rather than Mat. 750d763cef2SBarry Smith This allows the matrix evaluation routine to replace A and/or B with a 75156335db2SHong Zhang completely new matrix structure (not just different matrix elements) 752d763cef2SBarry Smith when appropriate, for instance, if the nonzero structure is changing 753d763cef2SBarry Smith throughout the global iterations. 754d763cef2SBarry Smith 755d763cef2SBarry Smith Level: beginner 756d763cef2SBarry Smith 757d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 758d763cef2SBarry Smith 759ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction() 760d763cef2SBarry Smith 761d763cef2SBarry Smith @*/ 762089b2837SJed Brown PetscErrorCode TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx) 763d763cef2SBarry Smith { 764277b19d0SLisandro Dalcin PetscErrorCode ierr; 765089b2837SJed Brown SNES snes; 76624989b8cSPeter Brune DM dm; 76724989b8cSPeter Brune TSIJacobian ijacobian; 768277b19d0SLisandro Dalcin 769d763cef2SBarry Smith PetscFunctionBegin; 7700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 771277b19d0SLisandro Dalcin if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 772277b19d0SLisandro Dalcin if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 773277b19d0SLisandro Dalcin if (A) PetscCheckSameComm(ts,1,A,2); 774277b19d0SLisandro Dalcin if (B) PetscCheckSameComm(ts,1,B,3); 775d763cef2SBarry Smith 77624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 77724989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 77824989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr); 77924989b8cSPeter Brune 780089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 7815f659677SPeter Brune if (!ijacobian) { 782089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 7830e4ef248SJed Brown } 7840e4ef248SJed Brown if (A) { 7850e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 7860e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 7870e4ef248SJed Brown ts->Arhs = A; 7880e4ef248SJed Brown } 7890e4ef248SJed Brown if (B) { 7900e4ef248SJed Brown ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 7910e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 7920e4ef248SJed Brown ts->Brhs = B; 7930e4ef248SJed Brown } 794d763cef2SBarry Smith PetscFunctionReturn(0); 795d763cef2SBarry Smith } 796d763cef2SBarry Smith 797316643e7SJed Brown 798316643e7SJed Brown #undef __FUNCT__ 799316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 800316643e7SJed Brown /*@C 801316643e7SJed Brown TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved. 802316643e7SJed Brown 8033f9fe445SBarry Smith Logically Collective on TS 804316643e7SJed Brown 805316643e7SJed Brown Input Parameters: 806316643e7SJed Brown + ts - the TS context obtained from TSCreate() 807ca94891dSJed Brown . r - vector to hold the residual (or PETSC_NULL to have it created internally) 808316643e7SJed Brown . f - the function evaluation routine 809316643e7SJed Brown - ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL) 810316643e7SJed Brown 811316643e7SJed Brown Calling sequence of f: 812316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 813316643e7SJed Brown 814316643e7SJed Brown + t - time at step/stage being solved 815316643e7SJed Brown . u - state vector 816316643e7SJed Brown . u_t - time derivative of state vector 817316643e7SJed Brown . F - function vector 818316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 819316643e7SJed Brown 820316643e7SJed Brown Important: 821d6cbdb99SBarry Smith The user MUST call either this routine, TSSetRHSFunction(). This routine must be used when not solving an ODE, for example a DAE. 822316643e7SJed Brown 823316643e7SJed Brown Level: beginner 824316643e7SJed Brown 825316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 826316643e7SJed Brown 827d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 828316643e7SJed Brown @*/ 829089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 830316643e7SJed Brown { 831089b2837SJed Brown PetscErrorCode ierr; 832089b2837SJed Brown SNES snes; 833e856ceecSJed Brown Vec resalloc = PETSC_NULL; 83424989b8cSPeter Brune DM dm; 835316643e7SJed Brown 836316643e7SJed Brown PetscFunctionBegin; 8370700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 838ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 83924989b8cSPeter Brune 84024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 84124989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 84224989b8cSPeter Brune 843089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 844e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 845e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 846e856ceecSJed Brown res = resalloc; 847e856ceecSJed Brown } 848089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 849e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 85024989b8cSPeter Brune 851089b2837SJed Brown PetscFunctionReturn(0); 852089b2837SJed Brown } 853089b2837SJed Brown 854089b2837SJed Brown #undef __FUNCT__ 855089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 856089b2837SJed Brown /*@C 857089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 858089b2837SJed Brown 859089b2837SJed Brown Not Collective 860089b2837SJed Brown 861089b2837SJed Brown Input Parameter: 862089b2837SJed Brown . ts - the TS context 863089b2837SJed Brown 864089b2837SJed Brown Output Parameter: 865089b2837SJed Brown + r - vector to hold residual (or PETSC_NULL) 866089b2837SJed Brown . func - the function to compute residual (or PETSC_NULL) 867089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 868089b2837SJed Brown 869089b2837SJed Brown Level: advanced 870089b2837SJed Brown 871089b2837SJed Brown .keywords: TS, nonlinear, get, function 872089b2837SJed Brown 873089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 874089b2837SJed Brown @*/ 875089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 876089b2837SJed Brown { 877089b2837SJed Brown PetscErrorCode ierr; 878089b2837SJed Brown SNES snes; 87924989b8cSPeter Brune DM dm; 880089b2837SJed Brown 881089b2837SJed Brown PetscFunctionBegin; 882089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 883089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 884089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 88524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 88624989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 887089b2837SJed Brown PetscFunctionReturn(0); 888089b2837SJed Brown } 889089b2837SJed Brown 890089b2837SJed Brown #undef __FUNCT__ 891089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 892089b2837SJed Brown /*@C 893089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 894089b2837SJed Brown 895089b2837SJed Brown Not Collective 896089b2837SJed Brown 897089b2837SJed Brown Input Parameter: 898089b2837SJed Brown . ts - the TS context 899089b2837SJed Brown 900089b2837SJed Brown Output Parameter: 901089b2837SJed Brown + r - vector to hold computed right hand side (or PETSC_NULL) 902089b2837SJed Brown . func - the function to compute right hand side (or PETSC_NULL) 903089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 904089b2837SJed Brown 905089b2837SJed Brown Level: advanced 906089b2837SJed Brown 907089b2837SJed Brown .keywords: TS, nonlinear, get, function 908089b2837SJed Brown 909089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 910089b2837SJed Brown @*/ 911089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 912089b2837SJed Brown { 913089b2837SJed Brown PetscErrorCode ierr; 914089b2837SJed Brown SNES snes; 91524989b8cSPeter Brune DM dm; 916089b2837SJed Brown 917089b2837SJed Brown PetscFunctionBegin; 918089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 919089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 920089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 92124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 92224989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 923316643e7SJed Brown PetscFunctionReturn(0); 924316643e7SJed Brown } 925316643e7SJed Brown 926316643e7SJed Brown #undef __FUNCT__ 927316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 928316643e7SJed Brown /*@C 929a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 930a4f0a591SBarry Smith you provided with TSSetIFunction(). 931316643e7SJed Brown 9323f9fe445SBarry Smith Logically Collective on TS 933316643e7SJed Brown 934316643e7SJed Brown Input Parameters: 935316643e7SJed Brown + ts - the TS context obtained from TSCreate() 936316643e7SJed Brown . A - Jacobian matrix 937316643e7SJed Brown . B - preconditioning matrix for A (may be same as A) 938316643e7SJed Brown . f - the Jacobian evaluation routine 939316643e7SJed Brown - ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL) 940316643e7SJed Brown 941316643e7SJed Brown Calling sequence of f: 9421b4a444bSJed Brown $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx); 943316643e7SJed Brown 944316643e7SJed Brown + t - time at step/stage being solved 9451b4a444bSJed Brown . U - state vector 9461b4a444bSJed Brown . U_t - time derivative of state vector 947316643e7SJed Brown . a - shift 9481b4a444bSJed Brown . A - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 949316643e7SJed Brown . B - preconditioning matrix for A, may be same as A 950316643e7SJed Brown . flag - flag indicating information about the preconditioner matrix 951316643e7SJed Brown structure (same as flag in KSPSetOperators()) 952316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 953316643e7SJed Brown 954316643e7SJed Brown Notes: 955316643e7SJed Brown The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve. 956316643e7SJed Brown 957a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 958a4f0a591SBarry 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. 959a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 960a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 961a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 962a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 963a4f0a591SBarry Smith 964316643e7SJed Brown Level: beginner 965316643e7SJed Brown 966316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 967316643e7SJed Brown 968ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian() 969316643e7SJed Brown 970316643e7SJed Brown @*/ 9717087cfbeSBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx) 972316643e7SJed Brown { 973316643e7SJed Brown PetscErrorCode ierr; 974089b2837SJed Brown SNES snes; 97524989b8cSPeter Brune DM dm; 976316643e7SJed Brown 977316643e7SJed Brown PetscFunctionBegin; 9780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9790700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 9800700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 981316643e7SJed Brown if (A) PetscCheckSameComm(ts,1,A,2); 982316643e7SJed Brown if (B) PetscCheckSameComm(ts,1,B,3); 98324989b8cSPeter Brune 98424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 98524989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 98624989b8cSPeter Brune 987089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 988089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 989316643e7SJed Brown PetscFunctionReturn(0); 990316643e7SJed Brown } 991316643e7SJed Brown 9924a2ae208SSatish Balay #undef __FUNCT__ 9934a2ae208SSatish Balay #define __FUNCT__ "TSView" 9947e2c5f70SBarry Smith /*@C 995d763cef2SBarry Smith TSView - Prints the TS data structure. 996d763cef2SBarry Smith 9974c49b128SBarry Smith Collective on TS 998d763cef2SBarry Smith 999d763cef2SBarry Smith Input Parameters: 1000d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1001d763cef2SBarry Smith - viewer - visualization context 1002d763cef2SBarry Smith 1003d763cef2SBarry Smith Options Database Key: 1004d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1005d763cef2SBarry Smith 1006d763cef2SBarry Smith Notes: 1007d763cef2SBarry Smith The available visualization contexts include 1008b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1009b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1010d763cef2SBarry Smith output where only the first processor opens 1011d763cef2SBarry Smith the file. All other processors send their 1012d763cef2SBarry Smith data to the first processor to print. 1013d763cef2SBarry Smith 1014d763cef2SBarry Smith The user can open an alternative visualization context with 1015b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1016d763cef2SBarry Smith 1017d763cef2SBarry Smith Level: beginner 1018d763cef2SBarry Smith 1019d763cef2SBarry Smith .keywords: TS, timestep, view 1020d763cef2SBarry Smith 1021b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1022d763cef2SBarry Smith @*/ 10237087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1024d763cef2SBarry Smith { 1025dfbe8321SBarry Smith PetscErrorCode ierr; 102619fd82e9SBarry Smith TSType type; 1027089b2837SJed Brown PetscBool iascii,isstring,isundials; 1028d763cef2SBarry Smith 1029d763cef2SBarry Smith PetscFunctionBegin; 10300700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10313050cee2SBarry Smith if (!viewer) { 10327adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr); 10333050cee2SBarry Smith } 10340700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1035c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1036fd16b177SBarry Smith 1037251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1038251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 103932077d6dSBarry Smith if (iascii) { 1040317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr); 104177431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 1042a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%G\n",ts->max_time);CHKERRQ(ierr); 1043d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 10445ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1045c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1046d763cef2SBarry Smith } 10475ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1048193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 1049d52bd9f3SBarry Smith if (ts->ops->view) { 1050d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1051d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1052d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1053d52bd9f3SBarry Smith } 10540f5bd95cSBarry Smith } else if (isstring) { 1055a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1056b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 1057d763cef2SBarry Smith } 1058b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1059251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1060b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1061d763cef2SBarry Smith PetscFunctionReturn(0); 1062d763cef2SBarry Smith } 1063d763cef2SBarry Smith 1064d763cef2SBarry Smith 10654a2ae208SSatish Balay #undef __FUNCT__ 10664a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1067b07ff414SBarry Smith /*@ 1068d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1069d763cef2SBarry Smith the timesteppers. 1070d763cef2SBarry Smith 10713f9fe445SBarry Smith Logically Collective on TS 1072d763cef2SBarry Smith 1073d763cef2SBarry Smith Input Parameters: 1074d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1075d763cef2SBarry Smith - usrP - optional user context 1076d763cef2SBarry Smith 1077d763cef2SBarry Smith Level: intermediate 1078d763cef2SBarry Smith 1079d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1080d763cef2SBarry Smith 1081d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1082d763cef2SBarry Smith @*/ 10837087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1084d763cef2SBarry Smith { 1085d763cef2SBarry Smith PetscFunctionBegin; 10860700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1087d763cef2SBarry Smith ts->user = usrP; 1088d763cef2SBarry Smith PetscFunctionReturn(0); 1089d763cef2SBarry Smith } 1090d763cef2SBarry Smith 10914a2ae208SSatish Balay #undef __FUNCT__ 10924a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1093b07ff414SBarry Smith /*@ 1094d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1095d763cef2SBarry Smith timestepper. 1096d763cef2SBarry Smith 1097d763cef2SBarry Smith Not Collective 1098d763cef2SBarry Smith 1099d763cef2SBarry Smith Input Parameter: 1100d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1101d763cef2SBarry Smith 1102d763cef2SBarry Smith Output Parameter: 1103d763cef2SBarry Smith . usrP - user context 1104d763cef2SBarry Smith 1105d763cef2SBarry Smith Level: intermediate 1106d763cef2SBarry Smith 1107d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1108d763cef2SBarry Smith 1109d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1110d763cef2SBarry Smith @*/ 1111e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1112d763cef2SBarry Smith { 1113d763cef2SBarry Smith PetscFunctionBegin; 11140700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1115e71120c6SJed Brown *(void**)usrP = ts->user; 1116d763cef2SBarry Smith PetscFunctionReturn(0); 1117d763cef2SBarry Smith } 1118d763cef2SBarry Smith 11194a2ae208SSatish Balay #undef __FUNCT__ 11204a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1121d763cef2SBarry Smith /*@ 1122b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1123d763cef2SBarry Smith 1124d763cef2SBarry Smith Not Collective 1125d763cef2SBarry Smith 1126d763cef2SBarry Smith Input Parameter: 1127d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1128d763cef2SBarry Smith 1129d763cef2SBarry Smith Output Parameter: 1130b8123daeSJed Brown . iter - number of steps completed so far 1131d763cef2SBarry Smith 1132d763cef2SBarry Smith Level: intermediate 1133d763cef2SBarry Smith 1134d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 1135b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep() 1136d763cef2SBarry Smith @*/ 11377087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt* iter) 1138d763cef2SBarry Smith { 1139d763cef2SBarry Smith PetscFunctionBegin; 11400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11414482741eSBarry Smith PetscValidIntPointer(iter,2); 1142d763cef2SBarry Smith *iter = ts->steps; 1143d763cef2SBarry Smith PetscFunctionReturn(0); 1144d763cef2SBarry Smith } 1145d763cef2SBarry Smith 11464a2ae208SSatish Balay #undef __FUNCT__ 11474a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1148d763cef2SBarry Smith /*@ 1149d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1150d763cef2SBarry Smith as well as the initial time. 1151d763cef2SBarry Smith 11523f9fe445SBarry Smith Logically Collective on TS 1153d763cef2SBarry Smith 1154d763cef2SBarry Smith Input Parameters: 1155d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1156d763cef2SBarry Smith . initial_time - the initial time 1157d763cef2SBarry Smith - time_step - the size of the timestep 1158d763cef2SBarry Smith 1159d763cef2SBarry Smith Level: intermediate 1160d763cef2SBarry Smith 1161d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1162d763cef2SBarry Smith 1163d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1164d763cef2SBarry Smith @*/ 11657087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1166d763cef2SBarry Smith { 1167e144a568SJed Brown PetscErrorCode ierr; 1168e144a568SJed Brown 1169d763cef2SBarry Smith PetscFunctionBegin; 11700700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1171e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1172d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1173d763cef2SBarry Smith PetscFunctionReturn(0); 1174d763cef2SBarry Smith } 1175d763cef2SBarry Smith 11764a2ae208SSatish Balay #undef __FUNCT__ 11774a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1178d763cef2SBarry Smith /*@ 1179d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1180d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1181d763cef2SBarry Smith 11823f9fe445SBarry Smith Logically Collective on TS 1183d763cef2SBarry Smith 1184d763cef2SBarry Smith Input Parameters: 1185d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1186d763cef2SBarry Smith - time_step - the size of the timestep 1187d763cef2SBarry Smith 1188d763cef2SBarry Smith Level: intermediate 1189d763cef2SBarry Smith 1190d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1191d763cef2SBarry Smith 1192d763cef2SBarry Smith .keywords: TS, set, timestep 1193d763cef2SBarry Smith @*/ 11947087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1195d763cef2SBarry Smith { 1196d763cef2SBarry Smith PetscFunctionBegin; 11970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1198c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1199d763cef2SBarry Smith ts->time_step = time_step; 120031748224SBarry Smith ts->time_step_orig = time_step; 1201d763cef2SBarry Smith PetscFunctionReturn(0); 1202d763cef2SBarry Smith } 1203d763cef2SBarry Smith 12044a2ae208SSatish Balay #undef __FUNCT__ 1205a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1206a43b19c4SJed Brown /*@ 1207a43b19c4SJed Brown TSSetExactFinalTime - Determines whether to interpolate solution to the 1208a43b19c4SJed Brown exact final time requested by the user or just returns it at the final time 1209a43b19c4SJed Brown it computed. 1210a43b19c4SJed Brown 1211a43b19c4SJed Brown Logically Collective on TS 1212a43b19c4SJed Brown 1213a43b19c4SJed Brown Input Parameter: 1214a43b19c4SJed Brown + ts - the time-step context 1215a43b19c4SJed Brown - ft - PETSC_TRUE if interpolates, else PETSC_FALSE 1216a43b19c4SJed Brown 1217a43b19c4SJed Brown Level: beginner 1218a43b19c4SJed Brown 1219a43b19c4SJed Brown .seealso: TSSetDuration() 1220a43b19c4SJed Brown @*/ 1221a43b19c4SJed Brown PetscErrorCode TSSetExactFinalTime(TS ts,PetscBool flg) 1222a43b19c4SJed Brown { 1223a43b19c4SJed Brown 1224a43b19c4SJed Brown PetscFunctionBegin; 1225a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1226d8cd7023SBarry Smith PetscValidLogicalCollectiveBool(ts,flg,2); 1227a43b19c4SJed Brown ts->exact_final_time = flg; 1228a43b19c4SJed Brown PetscFunctionReturn(0); 1229a43b19c4SJed Brown } 1230a43b19c4SJed Brown 1231a43b19c4SJed Brown #undef __FUNCT__ 12324a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1233d763cef2SBarry Smith /*@ 1234d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1235d763cef2SBarry Smith 1236d763cef2SBarry Smith Not Collective 1237d763cef2SBarry Smith 1238d763cef2SBarry Smith Input Parameter: 1239d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1240d763cef2SBarry Smith 1241d763cef2SBarry Smith Output Parameter: 1242d763cef2SBarry Smith . dt - the current timestep size 1243d763cef2SBarry Smith 1244d763cef2SBarry Smith Level: intermediate 1245d763cef2SBarry Smith 1246d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1247d763cef2SBarry Smith 1248d763cef2SBarry Smith .keywords: TS, get, timestep 1249d763cef2SBarry Smith @*/ 12507087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal* dt) 1251d763cef2SBarry Smith { 1252d763cef2SBarry Smith PetscFunctionBegin; 12530700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1254f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1255d763cef2SBarry Smith *dt = ts->time_step; 1256d763cef2SBarry Smith PetscFunctionReturn(0); 1257d763cef2SBarry Smith } 1258d763cef2SBarry Smith 12594a2ae208SSatish Balay #undef __FUNCT__ 12604a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1261d8e5e3e6SSatish Balay /*@ 1262d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1263d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1264d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1265d763cef2SBarry Smith the solution at the next timestep has been calculated. 1266d763cef2SBarry Smith 1267d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1268d763cef2SBarry Smith 1269d763cef2SBarry Smith Input Parameter: 1270d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1271d763cef2SBarry Smith 1272d763cef2SBarry Smith Output Parameter: 1273d763cef2SBarry Smith . v - the vector containing the solution 1274d763cef2SBarry Smith 1275d763cef2SBarry Smith Level: intermediate 1276d763cef2SBarry Smith 1277d763cef2SBarry Smith .seealso: TSGetTimeStep() 1278d763cef2SBarry Smith 1279d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1280d763cef2SBarry Smith @*/ 12817087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1282d763cef2SBarry Smith { 1283d763cef2SBarry Smith PetscFunctionBegin; 12840700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 12854482741eSBarry Smith PetscValidPointer(v,2); 12868737fe31SLisandro Dalcin *v = ts->vec_sol; 1287d763cef2SBarry Smith PetscFunctionReturn(0); 1288d763cef2SBarry Smith } 1289d763cef2SBarry Smith 1290bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 12914a2ae208SSatish Balay #undef __FUNCT__ 1292bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1293d8e5e3e6SSatish Balay /*@ 1294bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1295d763cef2SBarry Smith 1296bdad233fSMatthew Knepley Not collective 1297d763cef2SBarry Smith 1298bdad233fSMatthew Knepley Input Parameters: 1299bdad233fSMatthew Knepley + ts - The TS 1300bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1301d763cef2SBarry Smith .vb 1302d763cef2SBarry Smith U_t = A U 1303d763cef2SBarry Smith U_t = A(t) U 1304d763cef2SBarry Smith U_t = F(t,U) 1305d763cef2SBarry Smith .ve 1306d763cef2SBarry Smith 1307d763cef2SBarry Smith Level: beginner 1308d763cef2SBarry Smith 1309bdad233fSMatthew Knepley .keywords: TS, problem type 1310bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1311d763cef2SBarry Smith @*/ 13127087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1313a7cc72afSBarry Smith { 13149e2a6581SJed Brown PetscErrorCode ierr; 13159e2a6581SJed Brown 1316d763cef2SBarry Smith PetscFunctionBegin; 13170700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1318bdad233fSMatthew Knepley ts->problem_type = type; 13199e2a6581SJed Brown if (type == TS_LINEAR) { 13209e2a6581SJed Brown SNES snes; 13219e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13229e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 13239e2a6581SJed Brown } 1324d763cef2SBarry Smith PetscFunctionReturn(0); 1325d763cef2SBarry Smith } 1326d763cef2SBarry Smith 1327bdad233fSMatthew Knepley #undef __FUNCT__ 1328bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1329bdad233fSMatthew Knepley /*@C 1330bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1331bdad233fSMatthew Knepley 1332bdad233fSMatthew Knepley Not collective 1333bdad233fSMatthew Knepley 1334bdad233fSMatthew Knepley Input Parameter: 1335bdad233fSMatthew Knepley . ts - The TS 1336bdad233fSMatthew Knepley 1337bdad233fSMatthew Knepley Output Parameter: 1338bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1339bdad233fSMatthew Knepley .vb 1340089b2837SJed Brown M U_t = A U 1341089b2837SJed Brown M(t) U_t = A(t) U 1342bdad233fSMatthew Knepley U_t = F(t,U) 1343bdad233fSMatthew Knepley .ve 1344bdad233fSMatthew Knepley 1345bdad233fSMatthew Knepley Level: beginner 1346bdad233fSMatthew Knepley 1347bdad233fSMatthew Knepley .keywords: TS, problem type 1348bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1349bdad233fSMatthew Knepley @*/ 13507087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1351a7cc72afSBarry Smith { 1352bdad233fSMatthew Knepley PetscFunctionBegin; 13530700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 13544482741eSBarry Smith PetscValidIntPointer(type,2); 1355bdad233fSMatthew Knepley *type = ts->problem_type; 1356bdad233fSMatthew Knepley PetscFunctionReturn(0); 1357bdad233fSMatthew Knepley } 1358d763cef2SBarry Smith 13594a2ae208SSatish Balay #undef __FUNCT__ 13604a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1361d763cef2SBarry Smith /*@ 1362d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1363d763cef2SBarry Smith of a timestepper. 1364d763cef2SBarry Smith 1365d763cef2SBarry Smith Collective on TS 1366d763cef2SBarry Smith 1367d763cef2SBarry Smith Input Parameter: 1368d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1369d763cef2SBarry Smith 1370d763cef2SBarry Smith Notes: 1371d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1372d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1373d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1374d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1375d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1376d763cef2SBarry Smith 1377d763cef2SBarry Smith Level: advanced 1378d763cef2SBarry Smith 1379d763cef2SBarry Smith .keywords: TS, timestep, setup 1380d763cef2SBarry Smith 1381d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1382d763cef2SBarry Smith @*/ 13837087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1384d763cef2SBarry Smith { 1385dfbe8321SBarry Smith PetscErrorCode ierr; 13866c6b9e74SPeter Brune DM dm; 13876c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 13886c6b9e74SPeter Brune PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*); 13896c6b9e74SPeter Brune TSIJacobian ijac; 13906c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1391d763cef2SBarry Smith 1392d763cef2SBarry Smith PetscFunctionBegin; 13930700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1394277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1395277b19d0SLisandro Dalcin 13967adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 13979596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1398d763cef2SBarry Smith } 1399a43b19c4SJed Brown if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE; 1400277b19d0SLisandro Dalcin 1401277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1402277b19d0SLisandro Dalcin 14031c3436cfSJed Brown ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 14041c3436cfSJed Brown 1405277b19d0SLisandro Dalcin if (ts->ops->setup) { 1406000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1407277b19d0SLisandro Dalcin } 1408277b19d0SLisandro Dalcin 14096c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 14106c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 14116c6b9e74SPeter Brune */ 14126c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 14136c6b9e74SPeter Brune ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr); 14146c6b9e74SPeter Brune if (!func) { 14156c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 14166c6b9e74SPeter Brune } 14176c6b9e74SPeter 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. 14186c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 14196c6b9e74SPeter Brune */ 14206c6b9e74SPeter Brune ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr); 14216c6b9e74SPeter Brune ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr); 14226c6b9e74SPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr); 14236c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 14246c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 14256c6b9e74SPeter Brune } 1426277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1427277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1428277b19d0SLisandro Dalcin } 1429277b19d0SLisandro Dalcin 1430277b19d0SLisandro Dalcin #undef __FUNCT__ 1431277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1432277b19d0SLisandro Dalcin /*@ 1433277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1434277b19d0SLisandro Dalcin 1435277b19d0SLisandro Dalcin Collective on TS 1436277b19d0SLisandro Dalcin 1437277b19d0SLisandro Dalcin Input Parameter: 1438277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1439277b19d0SLisandro Dalcin 1440277b19d0SLisandro Dalcin Level: beginner 1441277b19d0SLisandro Dalcin 1442277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1443277b19d0SLisandro Dalcin 1444277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1445277b19d0SLisandro Dalcin @*/ 1446277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1447277b19d0SLisandro Dalcin { 1448277b19d0SLisandro Dalcin PetscErrorCode ierr; 1449277b19d0SLisandro Dalcin 1450277b19d0SLisandro Dalcin PetscFunctionBegin; 1451277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1452277b19d0SLisandro Dalcin if (ts->ops->reset) { 1453277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1454277b19d0SLisandro Dalcin } 1455277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 14564e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 14574e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1458214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 14596bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1460e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 1461e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 146238637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 1463277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1464d763cef2SBarry Smith PetscFunctionReturn(0); 1465d763cef2SBarry Smith } 1466d763cef2SBarry Smith 14674a2ae208SSatish Balay #undef __FUNCT__ 14684a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1469d8e5e3e6SSatish Balay /*@ 1470d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1471d763cef2SBarry Smith with TSCreate(). 1472d763cef2SBarry Smith 1473d763cef2SBarry Smith Collective on TS 1474d763cef2SBarry Smith 1475d763cef2SBarry Smith Input Parameter: 1476d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1477d763cef2SBarry Smith 1478d763cef2SBarry Smith Level: beginner 1479d763cef2SBarry Smith 1480d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1481d763cef2SBarry Smith 1482d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1483d763cef2SBarry Smith @*/ 14846bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1485d763cef2SBarry Smith { 14866849ba73SBarry Smith PetscErrorCode ierr; 1487d763cef2SBarry Smith 1488d763cef2SBarry Smith PetscFunctionBegin; 14896bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 14906bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 14916bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1492d763cef2SBarry Smith 14936bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1494277b19d0SLisandro Dalcin 1495be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 14966bf464f9SBarry Smith ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 14976bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 14986d4c513bSLisandro Dalcin 149984df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 15006bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 15016bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 15026bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 15036d4c513bSLisandro Dalcin 1504a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1505d763cef2SBarry Smith PetscFunctionReturn(0); 1506d763cef2SBarry Smith } 1507d763cef2SBarry Smith 15084a2ae208SSatish Balay #undef __FUNCT__ 15094a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1510d8e5e3e6SSatish Balay /*@ 1511d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1512d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1513d763cef2SBarry Smith 1514d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1515d763cef2SBarry Smith 1516d763cef2SBarry Smith Input Parameter: 1517d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1518d763cef2SBarry Smith 1519d763cef2SBarry Smith Output Parameter: 1520d763cef2SBarry Smith . snes - the nonlinear solver context 1521d763cef2SBarry Smith 1522d763cef2SBarry Smith Notes: 1523d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1524d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 152594b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1526d763cef2SBarry Smith 1527d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 1528d763cef2SBarry Smith this case TSGetSNES() returns PETSC_NULL in snes. 1529d763cef2SBarry Smith 1530d763cef2SBarry Smith Level: beginner 1531d763cef2SBarry Smith 1532d763cef2SBarry Smith .keywords: timestep, get, SNES 1533d763cef2SBarry Smith @*/ 15347087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1535d763cef2SBarry Smith { 1536d372ba47SLisandro Dalcin PetscErrorCode ierr; 1537d372ba47SLisandro Dalcin 1538d763cef2SBarry Smith PetscFunctionBegin; 15390700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15404482741eSBarry Smith PetscValidPointer(snes,2); 1541d372ba47SLisandro Dalcin if (!ts->snes) { 1542d372ba47SLisandro Dalcin ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 15436c6b9e74SPeter Brune ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 1544d372ba47SLisandro Dalcin ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1545d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 1546496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 15479e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 15489e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 15499e2a6581SJed Brown } 1550d372ba47SLisandro Dalcin } 1551d763cef2SBarry Smith *snes = ts->snes; 1552d763cef2SBarry Smith PetscFunctionReturn(0); 1553d763cef2SBarry Smith } 1554d763cef2SBarry Smith 15554a2ae208SSatish Balay #undef __FUNCT__ 155694b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 1557d8e5e3e6SSatish Balay /*@ 155894b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 1559d763cef2SBarry Smith a TS (timestepper) context. 1560d763cef2SBarry Smith 156194b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 1562d763cef2SBarry Smith 1563d763cef2SBarry Smith Input Parameter: 1564d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1565d763cef2SBarry Smith 1566d763cef2SBarry Smith Output Parameter: 156794b7f48cSBarry Smith . ksp - the nonlinear solver context 1568d763cef2SBarry Smith 1569d763cef2SBarry Smith Notes: 157094b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 1571d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 1572d763cef2SBarry Smith KSP and PC contexts as well. 1573d763cef2SBarry Smith 157494b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 157594b7f48cSBarry Smith in this case TSGetKSP() returns PETSC_NULL in ksp. 1576d763cef2SBarry Smith 1577d763cef2SBarry Smith Level: beginner 1578d763cef2SBarry Smith 157994b7f48cSBarry Smith .keywords: timestep, get, KSP 1580d763cef2SBarry Smith @*/ 15817087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1582d763cef2SBarry Smith { 1583d372ba47SLisandro Dalcin PetscErrorCode ierr; 1584089b2837SJed Brown SNES snes; 1585d372ba47SLisandro Dalcin 1586d763cef2SBarry Smith PetscFunctionBegin; 15870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15884482741eSBarry Smith PetscValidPointer(ksp,2); 158917186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1590e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1591089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1592089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1593d763cef2SBarry Smith PetscFunctionReturn(0); 1594d763cef2SBarry Smith } 1595d763cef2SBarry Smith 1596d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 1597d763cef2SBarry Smith 15984a2ae208SSatish Balay #undef __FUNCT__ 1599adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 1600adb62b0dSMatthew Knepley /*@ 1601adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 1602adb62b0dSMatthew Knepley maximum time for iteration. 1603adb62b0dSMatthew Knepley 16043f9fe445SBarry Smith Not Collective 1605adb62b0dSMatthew Knepley 1606adb62b0dSMatthew Knepley Input Parameters: 1607adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 1608adb62b0dSMatthew Knepley . maxsteps - maximum number of iterations to use, or PETSC_NULL 1609adb62b0dSMatthew Knepley - maxtime - final time to iterate to, or PETSC_NULL 1610adb62b0dSMatthew Knepley 1611adb62b0dSMatthew Knepley Level: intermediate 1612adb62b0dSMatthew Knepley 1613adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 1614adb62b0dSMatthew Knepley @*/ 16157087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1616adb62b0dSMatthew Knepley { 1617adb62b0dSMatthew Knepley PetscFunctionBegin; 16180700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1619abc0a331SBarry Smith if (maxsteps) { 16204482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 1621adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 1622adb62b0dSMatthew Knepley } 1623abc0a331SBarry Smith if (maxtime) { 16244482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 1625adb62b0dSMatthew Knepley *maxtime = ts->max_time; 1626adb62b0dSMatthew Knepley } 1627adb62b0dSMatthew Knepley PetscFunctionReturn(0); 1628adb62b0dSMatthew Knepley } 1629adb62b0dSMatthew Knepley 1630adb62b0dSMatthew Knepley #undef __FUNCT__ 16314a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 1632d763cef2SBarry Smith /*@ 1633d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 1634d763cef2SBarry Smith maximum time for iteration. 1635d763cef2SBarry Smith 16363f9fe445SBarry Smith Logically Collective on TS 1637d763cef2SBarry Smith 1638d763cef2SBarry Smith Input Parameters: 1639d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1640d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 1641d763cef2SBarry Smith - maxtime - final time to iterate to 1642d763cef2SBarry Smith 1643d763cef2SBarry Smith Options Database Keys: 1644d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 16453bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 1646d763cef2SBarry Smith 1647d763cef2SBarry Smith Notes: 1648d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 1649d763cef2SBarry Smith 1650d763cef2SBarry Smith Level: intermediate 1651d763cef2SBarry Smith 1652d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 1653a43b19c4SJed Brown 1654a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 1655d763cef2SBarry Smith @*/ 16567087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1657d763cef2SBarry Smith { 1658d763cef2SBarry Smith PetscFunctionBegin; 16590700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1660c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1661c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 166239b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 166339b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 1664d763cef2SBarry Smith PetscFunctionReturn(0); 1665d763cef2SBarry Smith } 1666d763cef2SBarry Smith 16674a2ae208SSatish Balay #undef __FUNCT__ 16684a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 1669d763cef2SBarry Smith /*@ 1670d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 1671d763cef2SBarry Smith for use by the TS routines. 1672d763cef2SBarry Smith 16733f9fe445SBarry Smith Logically Collective on TS and Vec 1674d763cef2SBarry Smith 1675d763cef2SBarry Smith Input Parameters: 1676d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1677d763cef2SBarry Smith - x - the solution vector 1678d763cef2SBarry Smith 1679d763cef2SBarry Smith Level: beginner 1680d763cef2SBarry Smith 1681d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 1682d763cef2SBarry Smith @*/ 16837087cfbeSBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec x) 1684d763cef2SBarry Smith { 16858737fe31SLisandro Dalcin PetscErrorCode ierr; 1686496e6a7aSJed Brown DM dm; 16878737fe31SLisandro Dalcin 1688d763cef2SBarry Smith PetscFunctionBegin; 16890700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 16900700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 16918737fe31SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 16926bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 16938737fe31SLisandro Dalcin ts->vec_sol = x; 1694496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1695496e6a7aSJed Brown ierr = DMShellSetGlobalVector(dm,x);CHKERRQ(ierr); 1696d763cef2SBarry Smith PetscFunctionReturn(0); 1697d763cef2SBarry Smith } 1698d763cef2SBarry Smith 1699e74ef692SMatthew Knepley #undef __FUNCT__ 1700e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 1701ac226902SBarry Smith /*@C 1702000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 17033f2090d5SJed Brown called once at the beginning of each time step. 1704000e7ae3SMatthew Knepley 17053f9fe445SBarry Smith Logically Collective on TS 1706000e7ae3SMatthew Knepley 1707000e7ae3SMatthew Knepley Input Parameters: 1708000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1709000e7ae3SMatthew Knepley - func - The function 1710000e7ae3SMatthew Knepley 1711000e7ae3SMatthew Knepley Calling sequence of func: 1712000e7ae3SMatthew Knepley . func (TS ts); 1713000e7ae3SMatthew Knepley 1714000e7ae3SMatthew Knepley Level: intermediate 1715000e7ae3SMatthew Knepley 1716b8123daeSJed Brown Note: 1717b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 1718b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 1719b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 1720b8123daeSJed Brown 1721000e7ae3SMatthew Knepley .keywords: TS, timestep 1722b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep() 1723000e7ae3SMatthew Knepley @*/ 17247087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1725000e7ae3SMatthew Knepley { 1726000e7ae3SMatthew Knepley PetscFunctionBegin; 17270700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1728000e7ae3SMatthew Knepley ts->ops->prestep = func; 1729000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1730000e7ae3SMatthew Knepley } 1731000e7ae3SMatthew Knepley 1732e74ef692SMatthew Knepley #undef __FUNCT__ 17333f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 173409ee8438SJed Brown /*@ 17353f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 17363f2090d5SJed Brown 17373f2090d5SJed Brown Collective on TS 17383f2090d5SJed Brown 17393f2090d5SJed Brown Input Parameters: 17403f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 17413f2090d5SJed Brown 17423f2090d5SJed Brown Notes: 17433f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 17443f2090d5SJed Brown so most users would not generally call this routine themselves. 17453f2090d5SJed Brown 17463f2090d5SJed Brown Level: developer 17473f2090d5SJed Brown 17483f2090d5SJed Brown .keywords: TS, timestep 1749b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep() 17503f2090d5SJed Brown @*/ 17517087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 17523f2090d5SJed Brown { 17533f2090d5SJed Brown PetscErrorCode ierr; 17543f2090d5SJed Brown 17553f2090d5SJed Brown PetscFunctionBegin; 17560700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 175772ac3e02SJed Brown if (ts->ops->prestep) { 17583f2090d5SJed Brown PetscStackPush("TS PreStep function"); 17593f2090d5SJed Brown ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 17603f2090d5SJed Brown PetscStackPop; 1761312ce896SJed Brown } 17623f2090d5SJed Brown PetscFunctionReturn(0); 17633f2090d5SJed Brown } 17643f2090d5SJed Brown 17653f2090d5SJed Brown #undef __FUNCT__ 1766b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 1767b8123daeSJed Brown /*@C 1768b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 1769b8123daeSJed Brown called once at the beginning of each stage. 1770b8123daeSJed Brown 1771b8123daeSJed Brown Logically Collective on TS 1772b8123daeSJed Brown 1773b8123daeSJed Brown Input Parameters: 1774b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 1775b8123daeSJed Brown - func - The function 1776b8123daeSJed Brown 1777b8123daeSJed Brown Calling sequence of func: 1778b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 1779b8123daeSJed Brown 1780b8123daeSJed Brown Level: intermediate 1781b8123daeSJed Brown 1782b8123daeSJed Brown Note: 1783b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 1784b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 1785b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 1786b8123daeSJed Brown 1787b8123daeSJed Brown .keywords: TS, timestep 1788b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 1789b8123daeSJed Brown @*/ 1790b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 1791b8123daeSJed Brown { 1792b8123daeSJed Brown PetscFunctionBegin; 1793b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1794b8123daeSJed Brown ts->ops->prestage = func; 1795b8123daeSJed Brown PetscFunctionReturn(0); 1796b8123daeSJed Brown } 1797b8123daeSJed Brown 1798b8123daeSJed Brown #undef __FUNCT__ 1799b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 1800b8123daeSJed Brown /*@ 1801b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 1802b8123daeSJed Brown 1803b8123daeSJed Brown Collective on TS 1804b8123daeSJed Brown 1805b8123daeSJed Brown Input Parameters: 1806b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 1807b8123daeSJed Brown 1808b8123daeSJed Brown Notes: 1809b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 1810b8123daeSJed Brown most users would not generally call this routine themselves. 1811b8123daeSJed Brown 1812b8123daeSJed Brown Level: developer 1813b8123daeSJed Brown 1814b8123daeSJed Brown .keywords: TS, timestep 1815b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep() 1816b8123daeSJed Brown @*/ 1817b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 1818b8123daeSJed Brown { 1819b8123daeSJed Brown PetscErrorCode ierr; 1820b8123daeSJed Brown 1821b8123daeSJed Brown PetscFunctionBegin; 1822b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1823b8123daeSJed Brown if (ts->ops->prestage) { 1824b8123daeSJed Brown PetscStackPush("TS PreStage function"); 1825b8123daeSJed Brown ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr); 1826b8123daeSJed Brown PetscStackPop; 1827b8123daeSJed Brown } 1828b8123daeSJed Brown PetscFunctionReturn(0); 1829b8123daeSJed Brown } 1830b8123daeSJed Brown 1831b8123daeSJed Brown #undef __FUNCT__ 1832e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 1833ac226902SBarry Smith /*@C 1834000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 18353f2090d5SJed Brown called once at the end of each time step. 1836000e7ae3SMatthew Knepley 18373f9fe445SBarry Smith Logically Collective on TS 1838000e7ae3SMatthew Knepley 1839000e7ae3SMatthew Knepley Input Parameters: 1840000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1841000e7ae3SMatthew Knepley - func - The function 1842000e7ae3SMatthew Knepley 1843000e7ae3SMatthew Knepley Calling sequence of func: 1844b8123daeSJed Brown $ func (TS ts); 1845000e7ae3SMatthew Knepley 1846000e7ae3SMatthew Knepley Level: intermediate 1847000e7ae3SMatthew Knepley 1848000e7ae3SMatthew Knepley .keywords: TS, timestep 1849b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 1850000e7ae3SMatthew Knepley @*/ 18517087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 1852000e7ae3SMatthew Knepley { 1853000e7ae3SMatthew Knepley PetscFunctionBegin; 18540700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1855000e7ae3SMatthew Knepley ts->ops->poststep = func; 1856000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1857000e7ae3SMatthew Knepley } 1858000e7ae3SMatthew Knepley 1859e74ef692SMatthew Knepley #undef __FUNCT__ 18603f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 186109ee8438SJed Brown /*@ 18623f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 18633f2090d5SJed Brown 18643f2090d5SJed Brown Collective on TS 18653f2090d5SJed Brown 18663f2090d5SJed Brown Input Parameters: 18673f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 18683f2090d5SJed Brown 18693f2090d5SJed Brown Notes: 18703f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 18713f2090d5SJed Brown so most users would not generally call this routine themselves. 18723f2090d5SJed Brown 18733f2090d5SJed Brown Level: developer 18743f2090d5SJed Brown 18753f2090d5SJed Brown .keywords: TS, timestep 18763f2090d5SJed Brown @*/ 18777087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 18783f2090d5SJed Brown { 18793f2090d5SJed Brown PetscErrorCode ierr; 18803f2090d5SJed Brown 18813f2090d5SJed Brown PetscFunctionBegin; 18820700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 188372ac3e02SJed Brown if (ts->ops->poststep) { 18843f2090d5SJed Brown PetscStackPush("TS PostStep function"); 18853f2090d5SJed Brown ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 18863f2090d5SJed Brown PetscStackPop; 188772ac3e02SJed Brown } 18883f2090d5SJed Brown PetscFunctionReturn(0); 18893f2090d5SJed Brown } 18903f2090d5SJed Brown 1891d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 1892d763cef2SBarry Smith 18934a2ae208SSatish Balay #undef __FUNCT__ 1894a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 1895d763cef2SBarry Smith /*@C 1896a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 1897d763cef2SBarry Smith timestep to display the iteration's progress. 1898d763cef2SBarry Smith 18993f9fe445SBarry Smith Logically Collective on TS 1900d763cef2SBarry Smith 1901d763cef2SBarry Smith Input Parameters: 1902d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1903e213d8f1SJed Brown . monitor - monitoring routine 1904329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 1905b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desired) 1906b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1907b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 1908d763cef2SBarry Smith 1909e213d8f1SJed Brown Calling sequence of monitor: 1910e213d8f1SJed Brown $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx) 1911d763cef2SBarry Smith 1912d763cef2SBarry Smith + ts - the TS context 1913d763cef2SBarry Smith . steps - iteration number 19141f06c33eSBarry Smith . time - current time 1915d763cef2SBarry Smith . x - current iterate 1916d763cef2SBarry Smith - mctx - [optional] monitoring context 1917d763cef2SBarry Smith 1918d763cef2SBarry Smith Notes: 1919d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 1920d763cef2SBarry Smith already has been loaded. 1921d763cef2SBarry Smith 1922025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 1923025f1a04SBarry Smith 1924d763cef2SBarry Smith Level: intermediate 1925d763cef2SBarry Smith 1926d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1927d763cef2SBarry Smith 1928a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 1929d763cef2SBarry Smith @*/ 1930c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 1931d763cef2SBarry Smith { 1932d763cef2SBarry Smith PetscFunctionBegin; 19330700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 193417186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1935d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 19368704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 1937d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 1938d763cef2SBarry Smith PetscFunctionReturn(0); 1939d763cef2SBarry Smith } 1940d763cef2SBarry Smith 19414a2ae208SSatish Balay #undef __FUNCT__ 1942a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 1943d763cef2SBarry Smith /*@C 1944a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 1945d763cef2SBarry Smith 19463f9fe445SBarry Smith Logically Collective on TS 1947d763cef2SBarry Smith 1948d763cef2SBarry Smith Input Parameters: 1949d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1950d763cef2SBarry Smith 1951d763cef2SBarry Smith Notes: 1952d763cef2SBarry Smith There is no way to remove a single, specific monitor. 1953d763cef2SBarry Smith 1954d763cef2SBarry Smith Level: intermediate 1955d763cef2SBarry Smith 1956d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1957d763cef2SBarry Smith 1958a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 1959d763cef2SBarry Smith @*/ 19607087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 1961d763cef2SBarry Smith { 1962d952e501SBarry Smith PetscErrorCode ierr; 1963d952e501SBarry Smith PetscInt i; 1964d952e501SBarry Smith 1965d763cef2SBarry Smith PetscFunctionBegin; 19660700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1967d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 19688704b422SBarry Smith if (ts->monitordestroy[i]) { 19698704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 1970d952e501SBarry Smith } 1971d952e501SBarry Smith } 1972d763cef2SBarry Smith ts->numbermonitors = 0; 1973d763cef2SBarry Smith PetscFunctionReturn(0); 1974d763cef2SBarry Smith } 1975d763cef2SBarry Smith 19764a2ae208SSatish Balay #undef __FUNCT__ 1977a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 1978d8e5e3e6SSatish Balay /*@ 1979a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 19805516499fSSatish Balay 19815516499fSSatish Balay Level: intermediate 198241251cbbSSatish Balay 19835516499fSSatish Balay .keywords: TS, set, monitor 19845516499fSSatish Balay 198541251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 198641251cbbSSatish Balay @*/ 1987649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 1988d763cef2SBarry Smith { 1989dfbe8321SBarry Smith PetscErrorCode ierr; 1990649052a6SBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm); 1991d132466eSBarry Smith 1992d763cef2SBarry Smith PetscFunctionBegin; 1993649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1994971832b6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr); 1995649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1996d763cef2SBarry Smith PetscFunctionReturn(0); 1997d763cef2SBarry Smith } 1998d763cef2SBarry Smith 19994a2ae208SSatish Balay #undef __FUNCT__ 2000cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 2001cd652676SJed Brown /*@ 2002cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 2003cd652676SJed Brown 2004cd652676SJed Brown Logically Collective on TS 2005cd652676SJed Brown 2006cd652676SJed Brown Input Argument: 2007cd652676SJed Brown . ts - time stepping context 2008cd652676SJed Brown 2009cd652676SJed Brown Output Argument: 2010cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 2011cd652676SJed Brown 2012cd652676SJed Brown Level: intermediate 2013cd652676SJed Brown 2014cd652676SJed Brown .keywords: TS, set 2015cd652676SJed Brown 2016cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 2017cd652676SJed Brown @*/ 2018cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 2019cd652676SJed Brown { 2020cd652676SJed Brown 2021cd652676SJed Brown PetscFunctionBegin; 2022cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2023cd652676SJed Brown ts->retain_stages = flg; 2024cd652676SJed Brown PetscFunctionReturn(0); 2025cd652676SJed Brown } 2026cd652676SJed Brown 2027cd652676SJed Brown #undef __FUNCT__ 2028cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 2029cd652676SJed Brown /*@ 2030cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 2031cd652676SJed Brown 2032cd652676SJed Brown Collective on TS 2033cd652676SJed Brown 2034cd652676SJed Brown Input Argument: 2035cd652676SJed Brown + ts - time stepping context 2036cd652676SJed Brown - t - time to interpolate to 2037cd652676SJed Brown 2038cd652676SJed Brown Output Argument: 2039cd652676SJed Brown . X - state at given time 2040cd652676SJed Brown 2041cd652676SJed Brown Notes: 2042cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 2043cd652676SJed Brown 2044cd652676SJed Brown Level: intermediate 2045cd652676SJed Brown 2046cd652676SJed Brown Developer Notes: 2047cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 2048cd652676SJed Brown 2049cd652676SJed Brown .keywords: TS, set 2050cd652676SJed Brown 2051cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 2052cd652676SJed Brown @*/ 2053cd652676SJed Brown PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec X) 2054cd652676SJed Brown { 2055cd652676SJed Brown PetscErrorCode ierr; 2056cd652676SJed Brown 2057cd652676SJed Brown PetscFunctionBegin; 2058cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2059d8cd7023SBarry 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); 2060cd652676SJed Brown if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 2061cd652676SJed Brown ierr = (*ts->ops->interpolate)(ts,t,X);CHKERRQ(ierr); 2062cd652676SJed Brown PetscFunctionReturn(0); 2063cd652676SJed Brown } 2064cd652676SJed Brown 2065cd652676SJed Brown #undef __FUNCT__ 20664a2ae208SSatish Balay #define __FUNCT__ "TSStep" 2067d763cef2SBarry Smith /*@ 20686d9e5789SSean Farley TSStep - Steps one time step 2069d763cef2SBarry Smith 2070d763cef2SBarry Smith Collective on TS 2071d763cef2SBarry Smith 2072d763cef2SBarry Smith Input Parameter: 2073d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2074d763cef2SBarry Smith 20756d9e5789SSean Farley Level: intermediate 2076d763cef2SBarry Smith 2077b8123daeSJed Brown Notes: 2078b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 2079b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 2080b8123daeSJed Brown 2081d763cef2SBarry Smith .keywords: TS, timestep, solve 2082d763cef2SBarry Smith 2083b8123daeSJed Brown .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage() 2084d763cef2SBarry Smith @*/ 2085193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 2086d763cef2SBarry Smith { 2087362cd11cSLisandro Dalcin PetscReal ptime_prev; 2088dfbe8321SBarry Smith PetscErrorCode ierr; 2089d763cef2SBarry Smith 2090d763cef2SBarry Smith PetscFunctionBegin; 20910700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2092d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 2093d405a339SMatthew Knepley 2094362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 2095362cd11cSLisandro Dalcin 2096362cd11cSLisandro Dalcin ptime_prev = ts->ptime; 2097d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2098193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 2099d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 2100362cd11cSLisandro Dalcin ts->time_step_prev = ts->ptime - ptime_prev; 2101362cd11cSLisandro Dalcin 2102362cd11cSLisandro Dalcin if (ts->reason < 0) { 2103cef5090cSJed Brown if (ts->errorifstepfailed) { 2104cef5090cSJed Brown if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) { 2105cef5090cSJed 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]); 2106cef5090cSJed Brown } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 2107cef5090cSJed Brown } 2108362cd11cSLisandro Dalcin } else if (!ts->reason) { 2109362cd11cSLisandro Dalcin if (ts->steps >= ts->max_steps) 2110362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITS; 2111362cd11cSLisandro Dalcin else if (ts->ptime >= ts->max_time) 2112362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_TIME; 2113362cd11cSLisandro Dalcin } 2114362cd11cSLisandro Dalcin 2115d763cef2SBarry Smith PetscFunctionReturn(0); 2116d763cef2SBarry Smith } 2117d763cef2SBarry Smith 21184a2ae208SSatish Balay #undef __FUNCT__ 211905175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 212005175c85SJed Brown /*@ 212105175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 212205175c85SJed Brown 21231c3436cfSJed Brown Collective on TS 212405175c85SJed Brown 212505175c85SJed Brown Input Arguments: 21261c3436cfSJed Brown + ts - time stepping context 21271c3436cfSJed Brown . order - desired order of accuracy 21281c3436cfSJed Brown - done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available) 212905175c85SJed Brown 213005175c85SJed Brown Output Arguments: 21311c3436cfSJed Brown . X - state at the end of the current step 213205175c85SJed Brown 213305175c85SJed Brown Level: advanced 213405175c85SJed Brown 2135108c343cSJed Brown Notes: 2136108c343cSJed Brown This function cannot be called until all stages have been evaluated. 2137108c343cSJed 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. 2138108c343cSJed Brown 21391c3436cfSJed Brown .seealso: TSStep(), TSAdapt 214005175c85SJed Brown @*/ 21411c3436cfSJed Brown PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec X,PetscBool *done) 214205175c85SJed Brown { 214305175c85SJed Brown PetscErrorCode ierr; 214405175c85SJed Brown 214505175c85SJed Brown PetscFunctionBegin; 214605175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 214705175c85SJed Brown PetscValidType(ts,1); 214805175c85SJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,3); 21491c3436cfSJed Brown if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 21501c3436cfSJed Brown ierr = (*ts->ops->evaluatestep)(ts,order,X,done);CHKERRQ(ierr); 215105175c85SJed Brown PetscFunctionReturn(0); 215205175c85SJed Brown } 215305175c85SJed Brown 215405175c85SJed Brown #undef __FUNCT__ 21556a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 21566a4d4014SLisandro Dalcin /*@ 21576a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 21586a4d4014SLisandro Dalcin 21596a4d4014SLisandro Dalcin Collective on TS 21606a4d4014SLisandro Dalcin 21616a4d4014SLisandro Dalcin Input Parameter: 21626a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 2163362cd11cSLisandro Dalcin - x - the solution vector 21646a4d4014SLisandro Dalcin 21655a3a76d0SJed Brown Output Parameter: 21665a3a76d0SJed Brown . ftime - time of the state vector x upon completion 21675a3a76d0SJed Brown 21686a4d4014SLisandro Dalcin Level: beginner 21696a4d4014SLisandro Dalcin 21705a3a76d0SJed Brown Notes: 21715a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 21725a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 21735a3a76d0SJed Brown stepped over the final time. 21745a3a76d0SJed Brown 21756a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 21766a4d4014SLisandro Dalcin 21776a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 21786a4d4014SLisandro Dalcin @*/ 21795a3a76d0SJed Brown PetscErrorCode TSSolve(TS ts,Vec x,PetscReal *ftime) 21806a4d4014SLisandro Dalcin { 21814d7d938eSLisandro Dalcin PetscBool flg; 21824d7d938eSLisandro Dalcin char filename[PETSC_MAX_PATH_LEN]; 21834d7d938eSLisandro Dalcin PetscViewer viewer; 21846a4d4014SLisandro Dalcin PetscErrorCode ierr; 2185f22f69f0SBarry Smith 21866a4d4014SLisandro Dalcin PetscFunctionBegin; 21870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2188193ac0bcSJed Brown PetscValidHeaderSpecific(x,VEC_CLASSID,2); 21895a3a76d0SJed Brown if (ts->exact_final_time) { /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */ 21905a3a76d0SJed Brown if (!ts->vec_sol || x == ts->vec_sol) { 21915a3a76d0SJed Brown Vec y; 21925a3a76d0SJed Brown ierr = VecDuplicate(x,&y);CHKERRQ(ierr); 21935a3a76d0SJed Brown ierr = TSSetSolution(ts,y);CHKERRQ(ierr); 21945a3a76d0SJed Brown ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */ 21955a3a76d0SJed Brown } 2196362cd11cSLisandro Dalcin ierr = VecCopy(x,ts->vec_sol);CHKERRQ(ierr); 21975a3a76d0SJed Brown } else { 2198193ac0bcSJed Brown ierr = TSSetSolution(ts,x);CHKERRQ(ierr); 21995a3a76d0SJed Brown } 2200b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 22016a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 2202193ac0bcSJed Brown ts->steps = 0; 22035ef26d82SJed Brown ts->ksp_its = 0; 22045ef26d82SJed Brown ts->snes_its = 0; 2205c610991cSLisandro Dalcin ts->num_snes_failures = 0; 2206c610991cSLisandro Dalcin ts->reject = 0; 2207193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 2208193ac0bcSJed Brown 2209193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 2210193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 22115a3a76d0SJed Brown ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr); 2212a5b82c69SSean Farley if (ftime) *ftime = ts->ptime; 2213193ac0bcSJed Brown } else { 22146a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 2215362cd11cSLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2216362cd11cSLisandro Dalcin if (ts->steps >= ts->max_steps) 2217362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITS; 2218362cd11cSLisandro Dalcin else if (ts->ptime >= ts->max_time) 2219362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_TIME; 2220e1a7a14fSJed Brown while (!ts->reason) { 2221193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 2222193ac0bcSJed Brown ierr = TSPostStep(ts);CHKERRQ(ierr); 2223193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 2224193ac0bcSJed Brown } 2225362cd11cSLisandro Dalcin if (ts->exact_final_time && ts->ptime > ts->max_time) { 2226a43b19c4SJed Brown ierr = TSInterpolate(ts,ts->max_time,x);CHKERRQ(ierr); 22275a3a76d0SJed Brown if (ftime) *ftime = ts->max_time; 22280574a7fbSJed Brown } else { 22290574a7fbSJed Brown ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr); 22300574a7fbSJed Brown if (ftime) *ftime = ts->ptime; 22310574a7fbSJed Brown } 2232193ac0bcSJed Brown } 2233*3923b477SBarry Smith ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 22344d7d938eSLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 22354d7d938eSLisandro Dalcin if (flg && !PetscPreLoadingOn) { 22364d7d938eSLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr); 22374d7d938eSLisandro Dalcin ierr = TSView(ts,viewer);CHKERRQ(ierr); 22384d7d938eSLisandro Dalcin ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 2239193ac0bcSJed Brown } 22406a4d4014SLisandro Dalcin PetscFunctionReturn(0); 22416a4d4014SLisandro Dalcin } 22426a4d4014SLisandro Dalcin 22436a4d4014SLisandro Dalcin #undef __FUNCT__ 22444a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 2245228d79bcSJed Brown /*@ 2246228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 2247228d79bcSJed Brown 2248228d79bcSJed Brown Collective on TS 2249228d79bcSJed Brown 2250228d79bcSJed Brown Input Parameters: 2251228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 2252228d79bcSJed Brown . step - step number that has just completed 2253228d79bcSJed Brown . ptime - model time of the state 2254228d79bcSJed Brown - x - state at the current model time 2255228d79bcSJed Brown 2256228d79bcSJed Brown Notes: 2257228d79bcSJed Brown TSMonitor() is typically used within the time stepping implementations. 2258228d79bcSJed Brown Users might call this function when using the TSStep() interface instead of TSSolve(). 2259228d79bcSJed Brown 2260228d79bcSJed Brown Level: advanced 2261228d79bcSJed Brown 2262228d79bcSJed Brown .keywords: TS, timestep 2263228d79bcSJed Brown @*/ 2264a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x) 2265d763cef2SBarry Smith { 22666849ba73SBarry Smith PetscErrorCode ierr; 2267a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 2268d763cef2SBarry Smith 2269d763cef2SBarry Smith PetscFunctionBegin; 2270d763cef2SBarry Smith for (i=0; i<n; i++) { 2271142b95e3SSatish Balay ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr); 2272d763cef2SBarry Smith } 2273d763cef2SBarry Smith PetscFunctionReturn(0); 2274d763cef2SBarry Smith } 2275d763cef2SBarry Smith 2276d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 22770b039ecaSBarry Smith struct _n_TSMonitorLGCtx { 22780b039ecaSBarry Smith PetscDrawLG lg; 22790b039ecaSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 22800b039ecaSBarry Smith }; 22810b039ecaSBarry Smith 2282d763cef2SBarry Smith 22834a2ae208SSatish Balay #undef __FUNCT__ 2284a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 2285d763cef2SBarry Smith /*@C 2286a9f9c1f6SBarry Smith TSMonitorLGCtxCreate - Creates a line graph context for use with 2287a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 2288d763cef2SBarry Smith 2289d763cef2SBarry Smith Collective on TS 2290d763cef2SBarry Smith 2291d763cef2SBarry Smith Input Parameters: 2292d763cef2SBarry Smith + host - the X display to open, or null for the local machine 2293d763cef2SBarry Smith . label - the title to put in the title bar 22947c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 2295a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 2296a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 2297d763cef2SBarry Smith 2298d763cef2SBarry Smith Output Parameter: 22990b039ecaSBarry Smith . ctx - the context 2300d763cef2SBarry Smith 2301d763cef2SBarry Smith Options Database Key: 2302a9f9c1f6SBarry Smith + -ts_monitor_draw - automatically sets line graph monitor 2303a9f9c1f6SBarry Smith . -ts_monitor_solutionode - 2304a9f9c1f6SBarry Smith - -ts_monitor_errorode - 2305d763cef2SBarry Smith 2306d763cef2SBarry Smith Notes: 2307a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 2308d763cef2SBarry Smith 2309d763cef2SBarry Smith Level: intermediate 2310d763cef2SBarry Smith 23117c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 2312d763cef2SBarry Smith 2313a9f9c1f6SBarry Smith .seealso: TSMonitorTimeStep(), TSMonitorSet(), TSMonitorSolutionODE(), TSMonitorErrorODE() 23147c922b88SBarry Smith 2315d763cef2SBarry Smith @*/ 2316a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 2317d763cef2SBarry Smith { 2318b0a32e0cSBarry Smith PetscDraw win; 2319dfbe8321SBarry Smith PetscErrorCode ierr; 2320d763cef2SBarry Smith 2321d763cef2SBarry Smith PetscFunctionBegin; 23220b039ecaSBarry Smith ierr = PetscNew(sizeof(struct _n_TSMonitorLGCtx),ctx);CHKERRQ(ierr); 2323a80ad3e0SBarry Smith ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr); 2324b0a32e0cSBarry Smith ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr); 23250b039ecaSBarry Smith ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr); 23260b039ecaSBarry Smith ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr); 23270b039ecaSBarry Smith ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr); 2328a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 2329d763cef2SBarry Smith PetscFunctionReturn(0); 2330d763cef2SBarry Smith } 2331d763cef2SBarry Smith 23324a2ae208SSatish Balay #undef __FUNCT__ 23330b039ecaSBarry Smith #define __FUNCT__ "TSMonitorTimeStep" 23340b039ecaSBarry Smith PetscErrorCode TSMonitorTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 2335d763cef2SBarry Smith { 23360b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 2337c32365f1SBarry Smith PetscReal x = ptime,y; 2338dfbe8321SBarry Smith PetscErrorCode ierr; 2339d763cef2SBarry Smith 2340d763cef2SBarry Smith PetscFunctionBegin; 2341a9f9c1f6SBarry Smith if (!n) { 2342a9f9c1f6SBarry Smith PetscDrawAxis axis; 2343a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 2344a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 2345a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 2346a9f9c1f6SBarry Smith } 2347c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 23480b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 2349*3923b477SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))){ 23500b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 2351*3923b477SBarry Smith } 2352d763cef2SBarry Smith PetscFunctionReturn(0); 2353d763cef2SBarry Smith } 2354d763cef2SBarry Smith 23554a2ae208SSatish Balay #undef __FUNCT__ 2356a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 2357d763cef2SBarry Smith /*@C 2358a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 2359a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 2360d763cef2SBarry Smith 23610b039ecaSBarry Smith Collective on TSMonitorLGCtx 2362d763cef2SBarry Smith 2363d763cef2SBarry Smith Input Parameter: 23640b039ecaSBarry Smith . ctx - the monitor context 2365d763cef2SBarry Smith 2366d763cef2SBarry Smith Level: intermediate 2367d763cef2SBarry Smith 2368d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 2369d763cef2SBarry Smith 2370a9f9c1f6SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorTimeStep(); 2371d763cef2SBarry Smith @*/ 2372a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 2373d763cef2SBarry Smith { 2374b0a32e0cSBarry Smith PetscDraw draw; 2375dfbe8321SBarry Smith PetscErrorCode ierr; 2376d763cef2SBarry Smith 2377d763cef2SBarry Smith PetscFunctionBegin; 23780b039ecaSBarry Smith ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr); 23796bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 23800b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 23810b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 2382d763cef2SBarry Smith PetscFunctionReturn(0); 2383d763cef2SBarry Smith } 2384d763cef2SBarry Smith 23854a2ae208SSatish Balay #undef __FUNCT__ 23864a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 2387d763cef2SBarry Smith /*@ 2388b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 2389d763cef2SBarry Smith 2390d763cef2SBarry Smith Not Collective 2391d763cef2SBarry Smith 2392d763cef2SBarry Smith Input Parameter: 2393d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2394d763cef2SBarry Smith 2395d763cef2SBarry Smith Output Parameter: 2396d763cef2SBarry Smith . t - the current time 2397d763cef2SBarry Smith 2398d763cef2SBarry Smith Level: beginner 2399d763cef2SBarry Smith 2400b8123daeSJed Brown Note: 2401b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 2402b8123daeSJed Brown TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 2403b8123daeSJed Brown 2404d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 2405d763cef2SBarry Smith 2406d763cef2SBarry Smith .keywords: TS, get, time 2407d763cef2SBarry Smith @*/ 24087087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal* t) 2409d763cef2SBarry Smith { 2410d763cef2SBarry Smith PetscFunctionBegin; 24110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2412f7cf8827SBarry Smith PetscValidRealPointer(t,2); 2413d763cef2SBarry Smith *t = ts->ptime; 2414d763cef2SBarry Smith PetscFunctionReturn(0); 2415d763cef2SBarry Smith } 2416d763cef2SBarry Smith 24174a2ae208SSatish Balay #undef __FUNCT__ 24186a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 24196a4d4014SLisandro Dalcin /*@ 24206a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 24216a4d4014SLisandro Dalcin 24223f9fe445SBarry Smith Logically Collective on TS 24236a4d4014SLisandro Dalcin 24246a4d4014SLisandro Dalcin Input Parameters: 24256a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 24266a4d4014SLisandro Dalcin - time - the time 24276a4d4014SLisandro Dalcin 24286a4d4014SLisandro Dalcin Level: intermediate 24296a4d4014SLisandro Dalcin 24306a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 24316a4d4014SLisandro Dalcin 24326a4d4014SLisandro Dalcin .keywords: TS, set, time 24336a4d4014SLisandro Dalcin @*/ 24347087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 24356a4d4014SLisandro Dalcin { 24366a4d4014SLisandro Dalcin PetscFunctionBegin; 24370700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2438c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 24396a4d4014SLisandro Dalcin ts->ptime = t; 24406a4d4014SLisandro Dalcin PetscFunctionReturn(0); 24416a4d4014SLisandro Dalcin } 24426a4d4014SLisandro Dalcin 24436a4d4014SLisandro Dalcin #undef __FUNCT__ 24444a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 2445d763cef2SBarry Smith /*@C 2446d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 2447d763cef2SBarry Smith TS options in the database. 2448d763cef2SBarry Smith 24493f9fe445SBarry Smith Logically Collective on TS 2450d763cef2SBarry Smith 2451d763cef2SBarry Smith Input Parameter: 2452d763cef2SBarry Smith + ts - The TS context 2453d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2454d763cef2SBarry Smith 2455d763cef2SBarry Smith Notes: 2456d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2457d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2458d763cef2SBarry Smith hyphen. 2459d763cef2SBarry Smith 2460d763cef2SBarry Smith Level: advanced 2461d763cef2SBarry Smith 2462d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 2463d763cef2SBarry Smith 2464d763cef2SBarry Smith .seealso: TSSetFromOptions() 2465d763cef2SBarry Smith 2466d763cef2SBarry Smith @*/ 24677087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 2468d763cef2SBarry Smith { 2469dfbe8321SBarry Smith PetscErrorCode ierr; 2470089b2837SJed Brown SNES snes; 2471d763cef2SBarry Smith 2472d763cef2SBarry Smith PetscFunctionBegin; 24730700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2474d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2475089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2476089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2477d763cef2SBarry Smith PetscFunctionReturn(0); 2478d763cef2SBarry Smith } 2479d763cef2SBarry Smith 2480d763cef2SBarry Smith 24814a2ae208SSatish Balay #undef __FUNCT__ 24824a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 2483d763cef2SBarry Smith /*@C 2484d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 2485d763cef2SBarry Smith TS options in the database. 2486d763cef2SBarry Smith 24873f9fe445SBarry Smith Logically Collective on TS 2488d763cef2SBarry Smith 2489d763cef2SBarry Smith Input Parameter: 2490d763cef2SBarry Smith + ts - The TS context 2491d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2492d763cef2SBarry Smith 2493d763cef2SBarry Smith Notes: 2494d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2495d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2496d763cef2SBarry Smith hyphen. 2497d763cef2SBarry Smith 2498d763cef2SBarry Smith Level: advanced 2499d763cef2SBarry Smith 2500d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 2501d763cef2SBarry Smith 2502d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 2503d763cef2SBarry Smith 2504d763cef2SBarry Smith @*/ 25057087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 2506d763cef2SBarry Smith { 2507dfbe8321SBarry Smith PetscErrorCode ierr; 2508089b2837SJed Brown SNES snes; 2509d763cef2SBarry Smith 2510d763cef2SBarry Smith PetscFunctionBegin; 25110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2512d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2513089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2514089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2515d763cef2SBarry Smith PetscFunctionReturn(0); 2516d763cef2SBarry Smith } 2517d763cef2SBarry Smith 25184a2ae208SSatish Balay #undef __FUNCT__ 25194a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2520d763cef2SBarry Smith /*@C 2521d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2522d763cef2SBarry Smith TS options in the database. 2523d763cef2SBarry Smith 2524d763cef2SBarry Smith Not Collective 2525d763cef2SBarry Smith 2526d763cef2SBarry Smith Input Parameter: 2527d763cef2SBarry Smith . ts - The TS context 2528d763cef2SBarry Smith 2529d763cef2SBarry Smith Output Parameter: 2530d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2531d763cef2SBarry Smith 2532d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2533d763cef2SBarry Smith sufficient length to hold the prefix. 2534d763cef2SBarry Smith 2535d763cef2SBarry Smith Level: intermediate 2536d763cef2SBarry Smith 2537d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2538d763cef2SBarry Smith 2539d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2540d763cef2SBarry Smith @*/ 25417087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2542d763cef2SBarry Smith { 2543dfbe8321SBarry Smith PetscErrorCode ierr; 2544d763cef2SBarry Smith 2545d763cef2SBarry Smith PetscFunctionBegin; 25460700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25474482741eSBarry Smith PetscValidPointer(prefix,2); 2548d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2549d763cef2SBarry Smith PetscFunctionReturn(0); 2550d763cef2SBarry Smith } 2551d763cef2SBarry Smith 25524a2ae208SSatish Balay #undef __FUNCT__ 25534a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2554d763cef2SBarry Smith /*@C 2555d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2556d763cef2SBarry Smith 2557d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2558d763cef2SBarry Smith 2559d763cef2SBarry Smith Input Parameter: 2560d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2561d763cef2SBarry Smith 2562d763cef2SBarry Smith Output Parameters: 2563d763cef2SBarry Smith + J - The Jacobian J of F, where U_t = F(U,t) 2564d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2565089b2837SJed Brown . func - Function to compute the Jacobian of the RHS 2566d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2567d763cef2SBarry Smith 2568d763cef2SBarry Smith Notes: You can pass in PETSC_NULL for any return argument you do not need. 2569d763cef2SBarry Smith 2570d763cef2SBarry Smith Level: intermediate 2571d763cef2SBarry Smith 257226d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2573d763cef2SBarry Smith 2574d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2575d763cef2SBarry Smith @*/ 2576089b2837SJed Brown PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2577d763cef2SBarry Smith { 2578089b2837SJed Brown PetscErrorCode ierr; 2579089b2837SJed Brown SNES snes; 258024989b8cSPeter Brune DM dm; 2581089b2837SJed Brown 2582d763cef2SBarry Smith PetscFunctionBegin; 2583089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2584089b2837SJed Brown ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 258524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 258624989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 2587d763cef2SBarry Smith PetscFunctionReturn(0); 2588d763cef2SBarry Smith } 2589d763cef2SBarry Smith 25901713a123SBarry Smith #undef __FUNCT__ 25912eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 25922eca1d9cSJed Brown /*@C 25932eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 25942eca1d9cSJed Brown 25952eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 25962eca1d9cSJed Brown 25972eca1d9cSJed Brown Input Parameter: 25982eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 25992eca1d9cSJed Brown 26002eca1d9cSJed Brown Output Parameters: 26012eca1d9cSJed Brown + A - The Jacobian of F(t,U,U_t) 26022eca1d9cSJed Brown . B - The preconditioner matrix, often the same as A 26032eca1d9cSJed Brown . f - The function to compute the matrices 26042eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 26052eca1d9cSJed Brown 26062eca1d9cSJed Brown Notes: You can pass in PETSC_NULL for any return argument you do not need. 26072eca1d9cSJed Brown 26082eca1d9cSJed Brown Level: advanced 26092eca1d9cSJed Brown 26102eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 26112eca1d9cSJed Brown 26122eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 26132eca1d9cSJed Brown @*/ 26147087cfbeSBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 26152eca1d9cSJed Brown { 2616089b2837SJed Brown PetscErrorCode ierr; 2617089b2837SJed Brown SNES snes; 261824989b8cSPeter Brune DM dm; 26192eca1d9cSJed Brown PetscFunctionBegin; 2620089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2621089b2837SJed Brown ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 262224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 262324989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 26242eca1d9cSJed Brown PetscFunctionReturn(0); 26252eca1d9cSJed Brown } 26262eca1d9cSJed Brown 26276083293cSBarry Smith typedef struct { 26286083293cSBarry Smith PetscViewer viewer; 26296083293cSBarry Smith Vec initialsolution; 26306083293cSBarry Smith PetscBool showinitial; 26316083293cSBarry Smith } TSMonitorSolutionCtx; 26326083293cSBarry Smith 26332eca1d9cSJed Brown #undef __FUNCT__ 2634a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution" 26351713a123SBarry Smith /*@C 2636a6570f20SBarry Smith TSMonitorSolution - Monitors progress of the TS solvers by calling 26371713a123SBarry Smith VecView() for the solution at each timestep 26381713a123SBarry Smith 26391713a123SBarry Smith Collective on TS 26401713a123SBarry Smith 26411713a123SBarry Smith Input Parameters: 26421713a123SBarry Smith + ts - the TS context 26431713a123SBarry Smith . step - current time-step 2644142b95e3SSatish Balay . ptime - current time 26451713a123SBarry Smith - dummy - either a viewer or PETSC_NULL 26461713a123SBarry Smith 26471713a123SBarry Smith Level: intermediate 26481713a123SBarry Smith 26491713a123SBarry Smith .keywords: TS, vector, monitor, view 26501713a123SBarry Smith 2651a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 26521713a123SBarry Smith @*/ 26537087cfbeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 26541713a123SBarry Smith { 2655dfbe8321SBarry Smith PetscErrorCode ierr; 26566083293cSBarry Smith TSMonitorSolutionCtx *ictx = (TSMonitorSolutionCtx*)dummy; 26571713a123SBarry Smith 26581713a123SBarry Smith PetscFunctionBegin; 26596083293cSBarry Smith if (!step && ictx->showinitial) { 26606083293cSBarry Smith if (!ictx->initialsolution) { 26616083293cSBarry Smith ierr = VecDuplicate(x,&ictx->initialsolution);CHKERRQ(ierr); 26621713a123SBarry Smith } 26636083293cSBarry Smith ierr = VecCopy(x,ictx->initialsolution);CHKERRQ(ierr); 26646083293cSBarry Smith } 26656083293cSBarry Smith if (ictx->showinitial) { 26666083293cSBarry Smith PetscReal pause; 26676083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 26686083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 26696083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 26706083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 26716083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 26726083293cSBarry Smith } 26736083293cSBarry Smith ierr = VecView(x,ictx->viewer);CHKERRQ(ierr); 26746083293cSBarry Smith if (ictx->showinitial) { 26756083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 26766083293cSBarry Smith } 26771713a123SBarry Smith PetscFunctionReturn(0); 26781713a123SBarry Smith } 26791713a123SBarry Smith 26801713a123SBarry Smith 26816c699258SBarry Smith #undef __FUNCT__ 26826083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionDestroy" 26836083293cSBarry Smith /*@C 26846083293cSBarry Smith TSMonitorSolutionDestroy - Destroys the monitor context for TSMonitorSolution 26856083293cSBarry Smith 26866083293cSBarry Smith Collective on TS 26876083293cSBarry Smith 26886083293cSBarry Smith Input Parameters: 26896083293cSBarry Smith . ctx - the monitor context 26906083293cSBarry Smith 26916083293cSBarry Smith Level: intermediate 26926083293cSBarry Smith 26936083293cSBarry Smith .keywords: TS, vector, monitor, view 26946083293cSBarry Smith 26956083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution() 26966083293cSBarry Smith @*/ 26976083293cSBarry Smith PetscErrorCode TSMonitorSolutionDestroy(void **ctx) 26986083293cSBarry Smith { 26996083293cSBarry Smith PetscErrorCode ierr; 27006083293cSBarry Smith TSMonitorSolutionCtx *ictx = *(TSMonitorSolutionCtx**)ctx; 27016083293cSBarry Smith 27026083293cSBarry Smith PetscFunctionBegin; 27036083293cSBarry Smith ierr = PetscViewerDestroy(&ictx->viewer);CHKERRQ(ierr); 27046083293cSBarry Smith ierr = VecDestroy(&ictx->initialsolution);CHKERRQ(ierr); 27056083293cSBarry Smith ierr = PetscFree(ictx);CHKERRQ(ierr); 27066083293cSBarry Smith PetscFunctionReturn(0); 27076083293cSBarry Smith } 27086083293cSBarry Smith 27096083293cSBarry Smith #undef __FUNCT__ 27106083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionCreate" 27116083293cSBarry Smith /*@C 27126083293cSBarry Smith TSMonitorSolutionCreate - Creates the monitor context for TSMonitorSolution 27136083293cSBarry Smith 27146083293cSBarry Smith Collective on TS 27156083293cSBarry Smith 27166083293cSBarry Smith Input Parameter: 27176083293cSBarry Smith . ts - time-step context 27186083293cSBarry Smith 27196083293cSBarry Smith Output Patameter: 27206083293cSBarry Smith . ctx - the monitor context 27216083293cSBarry Smith 27226083293cSBarry Smith Level: intermediate 27236083293cSBarry Smith 27246083293cSBarry Smith .keywords: TS, vector, monitor, view 27256083293cSBarry Smith 27266083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution() 27276083293cSBarry Smith @*/ 27286083293cSBarry Smith PetscErrorCode TSMonitorSolutionCreate(TS ts,PetscViewer viewer,void **ctx) 27296083293cSBarry Smith { 27306083293cSBarry Smith PetscErrorCode ierr; 27316083293cSBarry Smith TSMonitorSolutionCtx *ictx; 27326083293cSBarry Smith 27336083293cSBarry Smith PetscFunctionBegin; 27346083293cSBarry Smith ierr = PetscNew(TSMonitorSolutionCtx,&ictx);CHKERRQ(ierr); 27356083293cSBarry Smith *ctx = (void*)ictx; 27366083293cSBarry Smith if (!viewer) { 27376083293cSBarry Smith viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm); 27386083293cSBarry Smith } 27396083293cSBarry Smith ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr); 27406083293cSBarry Smith ictx->viewer = viewer; 27416083293cSBarry Smith ictx->showinitial = PETSC_FALSE; 27426083293cSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix,"-ts_monitor_solution_initial",&ictx->showinitial,PETSC_NULL);CHKERRQ(ierr); 27436083293cSBarry Smith PetscFunctionReturn(0); 27446083293cSBarry Smith } 27456083293cSBarry Smith 27466083293cSBarry Smith #undef __FUNCT__ 27476c699258SBarry Smith #define __FUNCT__ "TSSetDM" 27486c699258SBarry Smith /*@ 27496c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 27506c699258SBarry Smith 27513f9fe445SBarry Smith Logically Collective on TS and DM 27526c699258SBarry Smith 27536c699258SBarry Smith Input Parameters: 27546c699258SBarry Smith + ts - the preconditioner context 27556c699258SBarry Smith - dm - the dm 27566c699258SBarry Smith 27576c699258SBarry Smith Level: intermediate 27586c699258SBarry Smith 27596c699258SBarry Smith 27606c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 27616c699258SBarry Smith @*/ 27627087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 27636c699258SBarry Smith { 27646c699258SBarry Smith PetscErrorCode ierr; 2765089b2837SJed Brown SNES snes; 276624989b8cSPeter Brune TSDM tsdm; 27676c699258SBarry Smith 27686c699258SBarry Smith PetscFunctionBegin; 27690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 277070663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 277124989b8cSPeter Brune if (ts->dm) { /* Move the TSDM context over to the new DM unless the new DM already has one */ 277224989b8cSPeter Brune PetscContainer oldcontainer,container; 277324989b8cSPeter Brune ierr = PetscObjectQuery((PetscObject)ts->dm,"TSDM",(PetscObject*)&oldcontainer);CHKERRQ(ierr); 277424989b8cSPeter Brune ierr = PetscObjectQuery((PetscObject)dm,"TSDM",(PetscObject*)&container);CHKERRQ(ierr); 277524989b8cSPeter Brune if (oldcontainer && !container) { 277624989b8cSPeter Brune ierr = DMTSCopyContext(ts->dm,dm);CHKERRQ(ierr); 277724989b8cSPeter Brune ierr = DMTSGetContext(ts->dm,&tsdm);CHKERRQ(ierr); 277824989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 277924989b8cSPeter Brune tsdm->originaldm = dm; 278024989b8cSPeter Brune } 278124989b8cSPeter Brune } 27826bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 278324989b8cSPeter Brune } 27846c699258SBarry Smith ts->dm = dm; 2785089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2786089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 27876c699258SBarry Smith PetscFunctionReturn(0); 27886c699258SBarry Smith } 27896c699258SBarry Smith 27906c699258SBarry Smith #undef __FUNCT__ 27916c699258SBarry Smith #define __FUNCT__ "TSGetDM" 27926c699258SBarry Smith /*@ 27936c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 27946c699258SBarry Smith 27953f9fe445SBarry Smith Not Collective 27966c699258SBarry Smith 27976c699258SBarry Smith Input Parameter: 27986c699258SBarry Smith . ts - the preconditioner context 27996c699258SBarry Smith 28006c699258SBarry Smith Output Parameter: 28016c699258SBarry Smith . dm - the dm 28026c699258SBarry Smith 28036c699258SBarry Smith Level: intermediate 28046c699258SBarry Smith 28056c699258SBarry Smith 28066c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 28076c699258SBarry Smith @*/ 28087087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 28096c699258SBarry Smith { 2810496e6a7aSJed Brown PetscErrorCode ierr; 2811496e6a7aSJed Brown 28126c699258SBarry Smith PetscFunctionBegin; 28130700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2814496e6a7aSJed Brown if (!ts->dm) { 2815496e6a7aSJed Brown ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr); 2816496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 2817496e6a7aSJed Brown } 28186c699258SBarry Smith *dm = ts->dm; 28196c699258SBarry Smith PetscFunctionReturn(0); 28206c699258SBarry Smith } 28211713a123SBarry Smith 28220f5c6efeSJed Brown #undef __FUNCT__ 28230f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 28240f5c6efeSJed Brown /*@ 28250f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 28260f5c6efeSJed Brown 28273f9fe445SBarry Smith Logically Collective on SNES 28280f5c6efeSJed Brown 28290f5c6efeSJed Brown Input Parameter: 2830d42a1c89SJed Brown + snes - nonlinear solver 28310f5c6efeSJed Brown . X - the current state at which to evaluate the residual 2832d42a1c89SJed Brown - ctx - user context, must be a TS 28330f5c6efeSJed Brown 28340f5c6efeSJed Brown Output Parameter: 28350f5c6efeSJed Brown . F - the nonlinear residual 28360f5c6efeSJed Brown 28370f5c6efeSJed Brown Notes: 28380f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 28390f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 28400f5c6efeSJed Brown 28410f5c6efeSJed Brown Level: advanced 28420f5c6efeSJed Brown 28430f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 28440f5c6efeSJed Brown @*/ 28457087cfbeSBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx) 28460f5c6efeSJed Brown { 28470f5c6efeSJed Brown TS ts = (TS)ctx; 28480f5c6efeSJed Brown PetscErrorCode ierr; 28490f5c6efeSJed Brown 28500f5c6efeSJed Brown PetscFunctionBegin; 28510f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 28520f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 28530f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 28540f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 28550f5c6efeSJed Brown ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr); 28560f5c6efeSJed Brown PetscFunctionReturn(0); 28570f5c6efeSJed Brown } 28580f5c6efeSJed Brown 28590f5c6efeSJed Brown #undef __FUNCT__ 28600f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 28610f5c6efeSJed Brown /*@ 28620f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 28630f5c6efeSJed Brown 28640f5c6efeSJed Brown Collective on SNES 28650f5c6efeSJed Brown 28660f5c6efeSJed Brown Input Parameter: 28670f5c6efeSJed Brown + snes - nonlinear solver 28680f5c6efeSJed Brown . X - the current state at which to evaluate the residual 28690f5c6efeSJed Brown - ctx - user context, must be a TS 28700f5c6efeSJed Brown 28710f5c6efeSJed Brown Output Parameter: 28720f5c6efeSJed Brown + A - the Jacobian 28730f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 28740f5c6efeSJed Brown - flag - indicates any structure change in the matrix 28750f5c6efeSJed Brown 28760f5c6efeSJed Brown Notes: 28770f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 28780f5c6efeSJed Brown 28790f5c6efeSJed Brown Level: developer 28800f5c6efeSJed Brown 28810f5c6efeSJed Brown .seealso: SNESSetJacobian() 28820f5c6efeSJed Brown @*/ 28837087cfbeSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx) 28840f5c6efeSJed Brown { 28850f5c6efeSJed Brown TS ts = (TS)ctx; 28860f5c6efeSJed Brown PetscErrorCode ierr; 28870f5c6efeSJed Brown 28880f5c6efeSJed Brown PetscFunctionBegin; 28890f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 28900f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 28910f5c6efeSJed Brown PetscValidPointer(A,3); 28920f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 28930f5c6efeSJed Brown PetscValidPointer(B,4); 28940f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 28950f5c6efeSJed Brown PetscValidPointer(flag,5); 28960f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 28970f5c6efeSJed Brown ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr); 28980f5c6efeSJed Brown PetscFunctionReturn(0); 28990f5c6efeSJed Brown } 2900325fc9f4SBarry Smith 29010e4ef248SJed Brown #undef __FUNCT__ 29020e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 29030e4ef248SJed Brown /*@C 29040e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 29050e4ef248SJed Brown 29060e4ef248SJed Brown Collective on TS 29070e4ef248SJed Brown 29080e4ef248SJed Brown Input Arguments: 29090e4ef248SJed Brown + ts - time stepping context 29100e4ef248SJed Brown . t - time at which to evaluate 29110e4ef248SJed Brown . X - state at which to evaluate 29120e4ef248SJed Brown - ctx - context 29130e4ef248SJed Brown 29140e4ef248SJed Brown Output Arguments: 29150e4ef248SJed Brown . F - right hand side 29160e4ef248SJed Brown 29170e4ef248SJed Brown Level: intermediate 29180e4ef248SJed Brown 29190e4ef248SJed Brown Notes: 29200e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 29210e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 29220e4ef248SJed Brown 29230e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 29240e4ef248SJed Brown @*/ 29250e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx) 29260e4ef248SJed Brown { 29270e4ef248SJed Brown PetscErrorCode ierr; 29280e4ef248SJed Brown Mat Arhs,Brhs; 29290e4ef248SJed Brown MatStructure flg2; 29300e4ef248SJed Brown 29310e4ef248SJed Brown PetscFunctionBegin; 29320e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 29330e4ef248SJed Brown ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr); 29340e4ef248SJed Brown ierr = MatMult(Arhs,X,F);CHKERRQ(ierr); 29350e4ef248SJed Brown PetscFunctionReturn(0); 29360e4ef248SJed Brown } 29370e4ef248SJed Brown 29380e4ef248SJed Brown #undef __FUNCT__ 29390e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 29400e4ef248SJed Brown /*@C 29410e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 29420e4ef248SJed Brown 29430e4ef248SJed Brown Collective on TS 29440e4ef248SJed Brown 29450e4ef248SJed Brown Input Arguments: 29460e4ef248SJed Brown + ts - time stepping context 29470e4ef248SJed Brown . t - time at which to evaluate 29480e4ef248SJed Brown . X - state at which to evaluate 29490e4ef248SJed Brown - ctx - context 29500e4ef248SJed Brown 29510e4ef248SJed Brown Output Arguments: 29520e4ef248SJed Brown + A - pointer to operator 29530e4ef248SJed Brown . B - pointer to preconditioning matrix 29540e4ef248SJed Brown - flg - matrix structure flag 29550e4ef248SJed Brown 29560e4ef248SJed Brown Level: intermediate 29570e4ef248SJed Brown 29580e4ef248SJed Brown Notes: 29590e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 29600e4ef248SJed Brown 29610e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 29620e4ef248SJed Brown @*/ 29630e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx) 29640e4ef248SJed Brown { 29650e4ef248SJed Brown 29660e4ef248SJed Brown PetscFunctionBegin; 29670e4ef248SJed Brown *flg = SAME_PRECONDITIONER; 29680e4ef248SJed Brown PetscFunctionReturn(0); 29690e4ef248SJed Brown } 29700e4ef248SJed Brown 29710026cea9SSean Farley #undef __FUNCT__ 29720026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 29730026cea9SSean Farley /*@C 29740026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 29750026cea9SSean Farley 29760026cea9SSean Farley Collective on TS 29770026cea9SSean Farley 29780026cea9SSean Farley Input Arguments: 29790026cea9SSean Farley + ts - time stepping context 29800026cea9SSean Farley . t - time at which to evaluate 29810026cea9SSean Farley . X - state at which to evaluate 29820026cea9SSean Farley . Xdot - time derivative of state vector 29830026cea9SSean Farley - ctx - context 29840026cea9SSean Farley 29850026cea9SSean Farley Output Arguments: 29860026cea9SSean Farley . F - left hand side 29870026cea9SSean Farley 29880026cea9SSean Farley Level: intermediate 29890026cea9SSean Farley 29900026cea9SSean Farley Notes: 29910026cea9SSean 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 29920026cea9SSean Farley user is required to write their own TSComputeIFunction. 29930026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 29940026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 29950026cea9SSean Farley 29960026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 29970026cea9SSean Farley @*/ 29980026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx) 29990026cea9SSean Farley { 30000026cea9SSean Farley PetscErrorCode ierr; 30010026cea9SSean Farley Mat A,B; 30020026cea9SSean Farley MatStructure flg2; 30030026cea9SSean Farley 30040026cea9SSean Farley PetscFunctionBegin; 30050026cea9SSean Farley ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 30060026cea9SSean Farley ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr); 30070026cea9SSean Farley ierr = MatMult(A,Xdot,F);CHKERRQ(ierr); 30080026cea9SSean Farley PetscFunctionReturn(0); 30090026cea9SSean Farley } 30100026cea9SSean Farley 30110026cea9SSean Farley #undef __FUNCT__ 30120026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 30130026cea9SSean Farley /*@C 301424e94211SJed Brown TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent. 30150026cea9SSean Farley 30160026cea9SSean Farley Collective on TS 30170026cea9SSean Farley 30180026cea9SSean Farley Input Arguments: 30190026cea9SSean Farley + ts - time stepping context 30200026cea9SSean Farley . t - time at which to evaluate 30210026cea9SSean Farley . X - state at which to evaluate 30220026cea9SSean Farley . Xdot - time derivative of state vector 30230026cea9SSean Farley . shift - shift to apply 30240026cea9SSean Farley - ctx - context 30250026cea9SSean Farley 30260026cea9SSean Farley Output Arguments: 30270026cea9SSean Farley + A - pointer to operator 30280026cea9SSean Farley . B - pointer to preconditioning matrix 30290026cea9SSean Farley - flg - matrix structure flag 30300026cea9SSean Farley 30310026cea9SSean Farley Level: intermediate 30320026cea9SSean Farley 30330026cea9SSean Farley Notes: 30340026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 30350026cea9SSean Farley 30360026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 30370026cea9SSean Farley @*/ 30380026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx) 30390026cea9SSean Farley { 30400026cea9SSean Farley 30410026cea9SSean Farley PetscFunctionBegin; 30420026cea9SSean Farley *flg = SAME_PRECONDITIONER; 30430026cea9SSean Farley PetscFunctionReturn(0); 30440026cea9SSean Farley } 30450026cea9SSean Farley 30464af1b03aSJed Brown 30474af1b03aSJed Brown #undef __FUNCT__ 30484af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 30494af1b03aSJed Brown /*@ 30504af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 30514af1b03aSJed Brown 30524af1b03aSJed Brown Not Collective 30534af1b03aSJed Brown 30544af1b03aSJed Brown Input Parameter: 30554af1b03aSJed Brown . ts - the TS context 30564af1b03aSJed Brown 30574af1b03aSJed Brown Output Parameter: 30584af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 30594af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 30604af1b03aSJed Brown 30614af1b03aSJed Brown Level: intermediate 30624af1b03aSJed Brown 3063cd652676SJed Brown Notes: 3064cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 30654af1b03aSJed Brown 30664af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 30674af1b03aSJed Brown 30684af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 30694af1b03aSJed Brown @*/ 30704af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 30714af1b03aSJed Brown { 30724af1b03aSJed Brown PetscFunctionBegin; 30734af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30744af1b03aSJed Brown PetscValidPointer(reason,2); 30754af1b03aSJed Brown *reason = ts->reason; 30764af1b03aSJed Brown PetscFunctionReturn(0); 30774af1b03aSJed Brown } 30784af1b03aSJed Brown 3079fb1732b5SBarry Smith #undef __FUNCT__ 30805ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 30819f67acb7SJed Brown /*@ 30825ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 30839f67acb7SJed Brown used by the time integrator. 30849f67acb7SJed Brown 30859f67acb7SJed Brown Not Collective 30869f67acb7SJed Brown 30879f67acb7SJed Brown Input Parameter: 30889f67acb7SJed Brown . ts - TS context 30899f67acb7SJed Brown 30909f67acb7SJed Brown Output Parameter: 30919f67acb7SJed Brown . nits - number of nonlinear iterations 30929f67acb7SJed Brown 30939f67acb7SJed Brown Notes: 30949f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 30959f67acb7SJed Brown 30969f67acb7SJed Brown Level: intermediate 30979f67acb7SJed Brown 30989f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 30999f67acb7SJed Brown 31005ef26d82SJed Brown .seealso: TSGetKSPIterations() 31019f67acb7SJed Brown @*/ 31025ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 31039f67acb7SJed Brown { 31049f67acb7SJed Brown PetscFunctionBegin; 31059f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31069f67acb7SJed Brown PetscValidIntPointer(nits,2); 31075ef26d82SJed Brown *nits = ts->snes_its; 31089f67acb7SJed Brown PetscFunctionReturn(0); 31099f67acb7SJed Brown } 31109f67acb7SJed Brown 31119f67acb7SJed Brown #undef __FUNCT__ 31125ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 31139f67acb7SJed Brown /*@ 31145ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 31159f67acb7SJed Brown used by the time integrator. 31169f67acb7SJed Brown 31179f67acb7SJed Brown Not Collective 31189f67acb7SJed Brown 31199f67acb7SJed Brown Input Parameter: 31209f67acb7SJed Brown . ts - TS context 31219f67acb7SJed Brown 31229f67acb7SJed Brown Output Parameter: 31239f67acb7SJed Brown . lits - number of linear iterations 31249f67acb7SJed Brown 31259f67acb7SJed Brown Notes: 31269f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 31279f67acb7SJed Brown 31289f67acb7SJed Brown Level: intermediate 31299f67acb7SJed Brown 31309f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 31319f67acb7SJed Brown 31325ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 31339f67acb7SJed Brown @*/ 31345ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 31359f67acb7SJed Brown { 31369f67acb7SJed Brown PetscFunctionBegin; 31379f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31389f67acb7SJed Brown PetscValidIntPointer(lits,2); 31395ef26d82SJed Brown *lits = ts->ksp_its; 31409f67acb7SJed Brown PetscFunctionReturn(0); 31419f67acb7SJed Brown } 31429f67acb7SJed Brown 31439f67acb7SJed Brown #undef __FUNCT__ 3144cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 3145cef5090cSJed Brown /*@ 3146cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 3147cef5090cSJed Brown 3148cef5090cSJed Brown Not Collective 3149cef5090cSJed Brown 3150cef5090cSJed Brown Input Parameter: 3151cef5090cSJed Brown . ts - TS context 3152cef5090cSJed Brown 3153cef5090cSJed Brown Output Parameter: 3154cef5090cSJed Brown . rejects - number of steps rejected 3155cef5090cSJed Brown 3156cef5090cSJed Brown Notes: 3157cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 3158cef5090cSJed Brown 3159cef5090cSJed Brown Level: intermediate 3160cef5090cSJed Brown 3161cef5090cSJed Brown .keywords: TS, get, number 3162cef5090cSJed Brown 31635ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 3164cef5090cSJed Brown @*/ 3165cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 3166cef5090cSJed Brown { 3167cef5090cSJed Brown PetscFunctionBegin; 3168cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3169cef5090cSJed Brown PetscValidIntPointer(rejects,2); 3170cef5090cSJed Brown *rejects = ts->reject; 3171cef5090cSJed Brown PetscFunctionReturn(0); 3172cef5090cSJed Brown } 3173cef5090cSJed Brown 3174cef5090cSJed Brown #undef __FUNCT__ 3175cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 3176cef5090cSJed Brown /*@ 3177cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 3178cef5090cSJed Brown 3179cef5090cSJed Brown Not Collective 3180cef5090cSJed Brown 3181cef5090cSJed Brown Input Parameter: 3182cef5090cSJed Brown . ts - TS context 3183cef5090cSJed Brown 3184cef5090cSJed Brown Output Parameter: 3185cef5090cSJed Brown . fails - number of failed nonlinear solves 3186cef5090cSJed Brown 3187cef5090cSJed Brown Notes: 3188cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 3189cef5090cSJed Brown 3190cef5090cSJed Brown Level: intermediate 3191cef5090cSJed Brown 3192cef5090cSJed Brown .keywords: TS, get, number 3193cef5090cSJed Brown 31945ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 3195cef5090cSJed Brown @*/ 3196cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 3197cef5090cSJed Brown { 3198cef5090cSJed Brown PetscFunctionBegin; 3199cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3200cef5090cSJed Brown PetscValidIntPointer(fails,2); 3201cef5090cSJed Brown *fails = ts->num_snes_failures; 3202cef5090cSJed Brown PetscFunctionReturn(0); 3203cef5090cSJed Brown } 3204cef5090cSJed Brown 3205cef5090cSJed Brown #undef __FUNCT__ 3206cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 3207cef5090cSJed Brown /*@ 3208cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 3209cef5090cSJed Brown 3210cef5090cSJed Brown Not Collective 3211cef5090cSJed Brown 3212cef5090cSJed Brown Input Parameter: 3213cef5090cSJed Brown + ts - TS context 3214cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 3215cef5090cSJed Brown 3216cef5090cSJed Brown Notes: 3217cef5090cSJed Brown The counter is reset to zero for each step 3218cef5090cSJed Brown 3219cef5090cSJed Brown Options Database Key: 3220cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 3221cef5090cSJed Brown 3222cef5090cSJed Brown Level: intermediate 3223cef5090cSJed Brown 3224cef5090cSJed Brown .keywords: TS, set, maximum, number 3225cef5090cSJed Brown 32265ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3227cef5090cSJed Brown @*/ 3228cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 3229cef5090cSJed Brown { 3230cef5090cSJed Brown PetscFunctionBegin; 3231cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3232cef5090cSJed Brown ts->max_reject = rejects; 3233cef5090cSJed Brown PetscFunctionReturn(0); 3234cef5090cSJed Brown } 3235cef5090cSJed Brown 3236cef5090cSJed Brown #undef __FUNCT__ 3237cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 3238cef5090cSJed Brown /*@ 3239cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 3240cef5090cSJed Brown 3241cef5090cSJed Brown Not Collective 3242cef5090cSJed Brown 3243cef5090cSJed Brown Input Parameter: 3244cef5090cSJed Brown + ts - TS context 3245cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 3246cef5090cSJed Brown 3247cef5090cSJed Brown Notes: 3248cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 3249cef5090cSJed Brown 3250cef5090cSJed Brown Options Database Key: 3251cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 3252cef5090cSJed Brown 3253cef5090cSJed Brown Level: intermediate 3254cef5090cSJed Brown 3255cef5090cSJed Brown .keywords: TS, set, maximum, number 3256cef5090cSJed Brown 32575ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 3258cef5090cSJed Brown @*/ 3259cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 3260cef5090cSJed Brown { 3261cef5090cSJed Brown PetscFunctionBegin; 3262cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3263cef5090cSJed Brown ts->max_snes_failures = fails; 3264cef5090cSJed Brown PetscFunctionReturn(0); 3265cef5090cSJed Brown } 3266cef5090cSJed Brown 3267cef5090cSJed Brown #undef __FUNCT__ 3268cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()" 3269cef5090cSJed Brown /*@ 3270cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 3271cef5090cSJed Brown 3272cef5090cSJed Brown Not Collective 3273cef5090cSJed Brown 3274cef5090cSJed Brown Input Parameter: 3275cef5090cSJed Brown + ts - TS context 3276cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 3277cef5090cSJed Brown 3278cef5090cSJed Brown Options Database Key: 3279cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 3280cef5090cSJed Brown 3281cef5090cSJed Brown Level: intermediate 3282cef5090cSJed Brown 3283cef5090cSJed Brown .keywords: TS, set, error 3284cef5090cSJed Brown 32855ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 3286cef5090cSJed Brown @*/ 3287cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 3288cef5090cSJed Brown { 3289cef5090cSJed Brown PetscFunctionBegin; 3290cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3291cef5090cSJed Brown ts->errorifstepfailed = err; 3292cef5090cSJed Brown PetscFunctionReturn(0); 3293cef5090cSJed Brown } 3294cef5090cSJed Brown 3295cef5090cSJed Brown #undef __FUNCT__ 3296fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 3297fb1732b5SBarry Smith /*@C 3298fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 3299fb1732b5SBarry Smith 3300fb1732b5SBarry Smith Collective on TS 3301fb1732b5SBarry Smith 3302fb1732b5SBarry Smith Input Parameters: 3303fb1732b5SBarry Smith + ts - the TS context 3304fb1732b5SBarry Smith . step - current time-step 3305fb1732b5SBarry Smith . ptime - current time 3306ed81e22dSJed Brown . x - current state 3307fb1732b5SBarry Smith - viewer - binary viewer 3308fb1732b5SBarry Smith 3309fb1732b5SBarry Smith Level: intermediate 3310fb1732b5SBarry Smith 3311fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 3312fb1732b5SBarry Smith 3313fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3314fb1732b5SBarry Smith @*/ 3315ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec x,void *viewer) 3316fb1732b5SBarry Smith { 3317fb1732b5SBarry Smith PetscErrorCode ierr; 3318ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 3319fb1732b5SBarry Smith 3320fb1732b5SBarry Smith PetscFunctionBegin; 3321ed81e22dSJed Brown ierr = VecView(x,v);CHKERRQ(ierr); 3322ed81e22dSJed Brown PetscFunctionReturn(0); 3323ed81e22dSJed Brown } 3324ed81e22dSJed Brown 3325ed81e22dSJed Brown #undef __FUNCT__ 3326ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 3327ed81e22dSJed Brown /*@C 3328ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 3329ed81e22dSJed Brown 3330ed81e22dSJed Brown Collective on TS 3331ed81e22dSJed Brown 3332ed81e22dSJed Brown Input Parameters: 3333ed81e22dSJed Brown + ts - the TS context 3334ed81e22dSJed Brown . step - current time-step 3335ed81e22dSJed Brown . ptime - current time 3336ed81e22dSJed Brown . x - current state 3337ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3338ed81e22dSJed Brown 3339ed81e22dSJed Brown Level: intermediate 3340ed81e22dSJed Brown 3341ed81e22dSJed Brown Notes: 3342ed81e22dSJed 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. 3343ed81e22dSJed Brown These are named according to the file name template. 3344ed81e22dSJed Brown 3345ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 3346ed81e22dSJed Brown 3347ed81e22dSJed Brown .keywords: TS, vector, monitor, view 3348ed81e22dSJed Brown 3349ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3350ed81e22dSJed Brown @*/ 3351ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec x,void *filenametemplate) 3352ed81e22dSJed Brown { 3353ed81e22dSJed Brown PetscErrorCode ierr; 3354ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 3355ed81e22dSJed Brown PetscViewer viewer; 3356ed81e22dSJed Brown 3357ed81e22dSJed Brown PetscFunctionBegin; 33588caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 3359ed81e22dSJed Brown ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 3360fb1732b5SBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 3361ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 3362ed81e22dSJed Brown PetscFunctionReturn(0); 3363ed81e22dSJed Brown } 3364ed81e22dSJed Brown 3365ed81e22dSJed Brown #undef __FUNCT__ 3366ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 3367ed81e22dSJed Brown /*@C 3368ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 3369ed81e22dSJed Brown 3370ed81e22dSJed Brown Collective on TS 3371ed81e22dSJed Brown 3372ed81e22dSJed Brown Input Parameters: 3373ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 3374ed81e22dSJed Brown 3375ed81e22dSJed Brown Level: intermediate 3376ed81e22dSJed Brown 3377ed81e22dSJed Brown Note: 3378ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 3379ed81e22dSJed Brown 3380ed81e22dSJed Brown .keywords: TS, vector, monitor, view 3381ed81e22dSJed Brown 3382ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 3383ed81e22dSJed Brown @*/ 3384ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 3385ed81e22dSJed Brown { 3386ed81e22dSJed Brown PetscErrorCode ierr; 3387ed81e22dSJed Brown 3388ed81e22dSJed Brown PetscFunctionBegin; 3389ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 3390fb1732b5SBarry Smith PetscFunctionReturn(0); 3391fb1732b5SBarry Smith } 3392fb1732b5SBarry Smith 339384df9cb4SJed Brown #undef __FUNCT__ 339484df9cb4SJed Brown #define __FUNCT__ "TSGetAdapt" 339584df9cb4SJed Brown /*@ 339684df9cb4SJed Brown TSGetAdapt - Get the adaptive controller context for the current method 339784df9cb4SJed Brown 3398ed81e22dSJed Brown Collective on TS if controller has not been created yet 339984df9cb4SJed Brown 340084df9cb4SJed Brown Input Arguments: 3401ed81e22dSJed Brown . ts - time stepping context 340284df9cb4SJed Brown 340384df9cb4SJed Brown Output Arguments: 3404ed81e22dSJed Brown . adapt - adaptive controller 340584df9cb4SJed Brown 340684df9cb4SJed Brown Level: intermediate 340784df9cb4SJed Brown 3408ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 340984df9cb4SJed Brown @*/ 341084df9cb4SJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 341184df9cb4SJed Brown { 341284df9cb4SJed Brown PetscErrorCode ierr; 341384df9cb4SJed Brown 341484df9cb4SJed Brown PetscFunctionBegin; 341584df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 341684df9cb4SJed Brown PetscValidPointer(adapt,2); 341784df9cb4SJed Brown if (!ts->adapt) { 341884df9cb4SJed Brown ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr); 34191c3436cfSJed Brown ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr); 34201c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 342184df9cb4SJed Brown } 342284df9cb4SJed Brown *adapt = ts->adapt; 342384df9cb4SJed Brown PetscFunctionReturn(0); 342484df9cb4SJed Brown } 3425d6ebe24aSShri Abhyankar 3426d6ebe24aSShri Abhyankar #undef __FUNCT__ 34271c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 34281c3436cfSJed Brown /*@ 34291c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 34301c3436cfSJed Brown 34311c3436cfSJed Brown Logically Collective 34321c3436cfSJed Brown 34331c3436cfSJed Brown Input Arguments: 34341c3436cfSJed Brown + ts - time integration context 34351c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 34361c3436cfSJed Brown . vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present 34371c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 34381c3436cfSJed Brown - vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present 34391c3436cfSJed Brown 34401c3436cfSJed Brown Level: beginner 34411c3436cfSJed Brown 3442c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 34431c3436cfSJed Brown @*/ 34441c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 34451c3436cfSJed Brown { 34461c3436cfSJed Brown PetscErrorCode ierr; 34471c3436cfSJed Brown 34481c3436cfSJed Brown PetscFunctionBegin; 3449c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 34501c3436cfSJed Brown if (vatol) { 34511c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 34521c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 34531c3436cfSJed Brown ts->vatol = vatol; 34541c3436cfSJed Brown } 3455c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 34561c3436cfSJed Brown if (vrtol) { 34571c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 34581c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 34591c3436cfSJed Brown ts->vrtol = vrtol; 34601c3436cfSJed Brown } 34611c3436cfSJed Brown PetscFunctionReturn(0); 34621c3436cfSJed Brown } 34631c3436cfSJed Brown 34641c3436cfSJed Brown #undef __FUNCT__ 3465c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 3466c5033834SJed Brown /*@ 3467c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 3468c5033834SJed Brown 3469c5033834SJed Brown Logically Collective 3470c5033834SJed Brown 3471c5033834SJed Brown Input Arguments: 3472c5033834SJed Brown . ts - time integration context 3473c5033834SJed Brown 3474c5033834SJed Brown Output Arguments: 3475c5033834SJed Brown + atol - scalar absolute tolerances, PETSC_NULL to ignore 3476c5033834SJed Brown . vatol - vector of absolute tolerances, PETSC_NULL to ignore 3477c5033834SJed Brown . rtol - scalar relative tolerances, PETSC_NULL to ignore 3478c5033834SJed Brown - vrtol - vector of relative tolerances, PETSC_NULL to ignore 3479c5033834SJed Brown 3480c5033834SJed Brown Level: beginner 3481c5033834SJed Brown 3482c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 3483c5033834SJed Brown @*/ 3484c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 3485c5033834SJed Brown { 3486c5033834SJed Brown 3487c5033834SJed Brown PetscFunctionBegin; 3488c5033834SJed Brown if (atol) *atol = ts->atol; 3489c5033834SJed Brown if (vatol) *vatol = ts->vatol; 3490c5033834SJed Brown if (rtol) *rtol = ts->rtol; 3491c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 3492c5033834SJed Brown PetscFunctionReturn(0); 3493c5033834SJed Brown } 3494c5033834SJed Brown 3495c5033834SJed Brown #undef __FUNCT__ 34961c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS" 34971c3436cfSJed Brown /*@ 34981c3436cfSJed Brown TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 34991c3436cfSJed Brown 35001c3436cfSJed Brown Collective on TS 35011c3436cfSJed Brown 35021c3436cfSJed Brown Input Arguments: 35031c3436cfSJed Brown + ts - time stepping context 35041c3436cfSJed Brown - Y - state vector to be compared to ts->vec_sol 35051c3436cfSJed Brown 35061c3436cfSJed Brown Output Arguments: 35071c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 35081c3436cfSJed Brown 35091c3436cfSJed Brown Level: developer 35101c3436cfSJed Brown 35111c3436cfSJed Brown .seealso: TSSetTolerances() 35121c3436cfSJed Brown @*/ 35131c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 35141c3436cfSJed Brown { 35151c3436cfSJed Brown PetscErrorCode ierr; 35161c3436cfSJed Brown PetscInt i,n,N; 35171c3436cfSJed Brown const PetscScalar *x,*y; 35181c3436cfSJed Brown Vec X; 35191c3436cfSJed Brown PetscReal sum,gsum; 35201c3436cfSJed Brown 35211c3436cfSJed Brown PetscFunctionBegin; 35221c3436cfSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 35231c3436cfSJed Brown PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 35241c3436cfSJed Brown PetscValidPointer(norm,3); 35251c3436cfSJed Brown X = ts->vec_sol; 35261c3436cfSJed Brown PetscCheckSameTypeAndComm(X,1,Y,2); 35271c3436cfSJed Brown if (X == Y) SETERRQ(((PetscObject)X)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 35281c3436cfSJed Brown 35291c3436cfSJed Brown ierr = VecGetSize(X,&N);CHKERRQ(ierr); 35301c3436cfSJed Brown ierr = VecGetLocalSize(X,&n);CHKERRQ(ierr); 35311c3436cfSJed Brown ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr); 35321c3436cfSJed Brown ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 35331c3436cfSJed Brown sum = 0.; 3534ceefc113SJed Brown if (ts->vatol && ts->vrtol) { 3535ceefc113SJed Brown const PetscScalar *atol,*rtol; 3536ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3537ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3538ceefc113SJed Brown for (i=0; i<n; i++) { 3539ceefc113SJed Brown PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i])); 3540ceefc113SJed Brown sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol); 3541ceefc113SJed Brown } 3542ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3543ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3544ceefc113SJed Brown } else if (ts->vatol) { /* vector atol, scalar rtol */ 3545ceefc113SJed Brown const PetscScalar *atol; 3546ceefc113SJed Brown ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3547ceefc113SJed Brown for (i=0; i<n; i++) { 3548ceefc113SJed Brown PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i])); 3549ceefc113SJed Brown sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol); 3550ceefc113SJed Brown } 3551ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 3552ceefc113SJed Brown } else if (ts->vrtol) { /* scalar atol, vector rtol */ 3553ceefc113SJed Brown const PetscScalar *rtol; 3554ceefc113SJed Brown ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3555ceefc113SJed Brown for (i=0; i<n; i++) { 3556ceefc113SJed Brown PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i])); 3557ceefc113SJed Brown sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol); 3558ceefc113SJed Brown } 3559ceefc113SJed Brown ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 3560ceefc113SJed Brown } else { /* scalar atol, scalar rtol */ 35611c3436cfSJed Brown for (i=0; i<n; i++) { 35621c3436cfSJed Brown PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i])); 35631c3436cfSJed Brown sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol); 35641c3436cfSJed Brown } 3565ceefc113SJed Brown } 35661c3436cfSJed Brown ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr); 35671c3436cfSJed Brown ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 35681c3436cfSJed Brown 35691c3436cfSJed Brown ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr); 35701c3436cfSJed Brown *norm = PetscSqrtReal(gsum / N); 35711c3436cfSJed Brown if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 35721c3436cfSJed Brown PetscFunctionReturn(0); 35731c3436cfSJed Brown } 35741c3436cfSJed Brown 35751c3436cfSJed Brown #undef __FUNCT__ 35768d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 35778d59e960SJed Brown /*@ 35788d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 35798d59e960SJed Brown 35808d59e960SJed Brown Logically Collective on TS 35818d59e960SJed Brown 35828d59e960SJed Brown Input Arguments: 35838d59e960SJed Brown + ts - time stepping context 35848d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 35858d59e960SJed Brown 35868d59e960SJed Brown Note: 35878d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 35888d59e960SJed Brown 35898d59e960SJed Brown Level: intermediate 35908d59e960SJed Brown 35918d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 35928d59e960SJed Brown @*/ 35938d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 35948d59e960SJed Brown { 35958d59e960SJed Brown 35968d59e960SJed Brown PetscFunctionBegin; 35978d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 35988d59e960SJed Brown ts->cfltime_local = cfltime; 35998d59e960SJed Brown ts->cfltime = -1.; 36008d59e960SJed Brown PetscFunctionReturn(0); 36018d59e960SJed Brown } 36028d59e960SJed Brown 36038d59e960SJed Brown #undef __FUNCT__ 36048d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 36058d59e960SJed Brown /*@ 36068d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 36078d59e960SJed Brown 36088d59e960SJed Brown Collective on TS 36098d59e960SJed Brown 36108d59e960SJed Brown Input Arguments: 36118d59e960SJed Brown . ts - time stepping context 36128d59e960SJed Brown 36138d59e960SJed Brown Output Arguments: 36148d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 36158d59e960SJed Brown 36168d59e960SJed Brown Level: advanced 36178d59e960SJed Brown 36188d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 36198d59e960SJed Brown @*/ 36208d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 36218d59e960SJed Brown { 36228d59e960SJed Brown PetscErrorCode ierr; 36238d59e960SJed Brown 36248d59e960SJed Brown PetscFunctionBegin; 36258d59e960SJed Brown if (ts->cfltime < 0) { 36268d59e960SJed Brown ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr); 36278d59e960SJed Brown } 36288d59e960SJed Brown *cfltime = ts->cfltime; 36298d59e960SJed Brown PetscFunctionReturn(0); 36308d59e960SJed Brown } 36318d59e960SJed Brown 36328d59e960SJed Brown #undef __FUNCT__ 3633d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 3634d6ebe24aSShri Abhyankar /*@ 3635d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 3636d6ebe24aSShri Abhyankar 3637d6ebe24aSShri Abhyankar Input Parameters: 3638d6ebe24aSShri Abhyankar . ts - the TS context. 3639d6ebe24aSShri Abhyankar . xl - lower bound. 3640d6ebe24aSShri Abhyankar . xu - upper bound. 3641d6ebe24aSShri Abhyankar 3642d6ebe24aSShri Abhyankar Notes: 3643d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 364433c0c2ddSJed Brown SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp(). 3645d6ebe24aSShri Abhyankar 36462bd2b0e6SSatish Balay Level: advanced 36472bd2b0e6SSatish Balay 3648d6ebe24aSShri Abhyankar @*/ 3649d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 3650d6ebe24aSShri Abhyankar { 3651d6ebe24aSShri Abhyankar PetscErrorCode ierr; 3652d6ebe24aSShri Abhyankar SNES snes; 3653d6ebe24aSShri Abhyankar 3654d6ebe24aSShri Abhyankar PetscFunctionBegin; 3655d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3656d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 3657d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 3658d6ebe24aSShri Abhyankar } 3659d6ebe24aSShri Abhyankar 3660325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 3661c6db04a5SJed Brown #include <mex.h> 3662325fc9f4SBarry Smith 3663325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 3664325fc9f4SBarry Smith 3665325fc9f4SBarry Smith #undef __FUNCT__ 3666325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 3667325fc9f4SBarry Smith /* 3668325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 3669325fc9f4SBarry Smith TSSetFunctionMatlab(). 3670325fc9f4SBarry Smith 3671325fc9f4SBarry Smith Collective on TS 3672325fc9f4SBarry Smith 3673325fc9f4SBarry Smith Input Parameters: 3674325fc9f4SBarry Smith + snes - the TS context 3675325fc9f4SBarry Smith - x - input vector 3676325fc9f4SBarry Smith 3677325fc9f4SBarry Smith Output Parameter: 3678325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 3679325fc9f4SBarry Smith 3680325fc9f4SBarry Smith Notes: 3681325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 3682325fc9f4SBarry Smith implementations, so most users would not generally call this routine 3683325fc9f4SBarry Smith themselves. 3684325fc9f4SBarry Smith 3685325fc9f4SBarry Smith Level: developer 3686325fc9f4SBarry Smith 3687325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 3688325fc9f4SBarry Smith 3689325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3690325fc9f4SBarry Smith */ 36917087cfbeSBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx) 3692325fc9f4SBarry Smith { 3693325fc9f4SBarry Smith PetscErrorCode ierr; 3694325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3695325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 3696325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 3697325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 3698325fc9f4SBarry Smith 3699325fc9f4SBarry Smith PetscFunctionBegin; 3700325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 3701325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3702325fc9f4SBarry Smith PetscValidHeaderSpecific(xdot,VEC_CLASSID,4); 3703325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 3704325fc9f4SBarry Smith PetscCheckSameComm(snes,1,x,3); 3705325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 3706325fc9f4SBarry Smith 3707325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3708325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3709880f3077SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr); 3710325fc9f4SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 3711325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3712325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 3713325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 3714325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 3715325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 3716325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 3717325fc9f4SBarry Smith prhs[6] = sctx->ctx; 3718325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 3719325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3720325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 3721325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 3722325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 3723325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 3724325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 3725325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 3726325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 3727325fc9f4SBarry Smith PetscFunctionReturn(0); 3728325fc9f4SBarry Smith } 3729325fc9f4SBarry Smith 3730325fc9f4SBarry Smith 3731325fc9f4SBarry Smith #undef __FUNCT__ 3732325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 3733325fc9f4SBarry Smith /* 3734325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 3735325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 3736e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 3737325fc9f4SBarry Smith 3738325fc9f4SBarry Smith Logically Collective on TS 3739325fc9f4SBarry Smith 3740325fc9f4SBarry Smith Input Parameters: 3741325fc9f4SBarry Smith + ts - the TS context 3742325fc9f4SBarry Smith - func - function evaluation routine 3743325fc9f4SBarry Smith 3744325fc9f4SBarry Smith Calling sequence of func: 3745325fc9f4SBarry Smith $ func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx); 3746325fc9f4SBarry Smith 3747325fc9f4SBarry Smith Level: beginner 3748325fc9f4SBarry Smith 3749325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 3750325fc9f4SBarry Smith 3751325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3752325fc9f4SBarry Smith */ 3753cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 3754325fc9f4SBarry Smith { 3755325fc9f4SBarry Smith PetscErrorCode ierr; 3756325fc9f4SBarry Smith TSMatlabContext *sctx; 3757325fc9f4SBarry Smith 3758325fc9f4SBarry Smith PetscFunctionBegin; 3759325fc9f4SBarry Smith /* currently sctx is memory bleed */ 3760325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3761325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3762325fc9f4SBarry Smith /* 3763325fc9f4SBarry Smith This should work, but it doesn't 3764325fc9f4SBarry Smith sctx->ctx = ctx; 3765325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 3766325fc9f4SBarry Smith */ 3767325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 3768cdcf91faSSean Farley ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 3769325fc9f4SBarry Smith PetscFunctionReturn(0); 3770325fc9f4SBarry Smith } 3771325fc9f4SBarry Smith 3772325fc9f4SBarry Smith #undef __FUNCT__ 3773325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 3774325fc9f4SBarry Smith /* 3775325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 3776325fc9f4SBarry Smith TSSetJacobianMatlab(). 3777325fc9f4SBarry Smith 3778325fc9f4SBarry Smith Collective on TS 3779325fc9f4SBarry Smith 3780325fc9f4SBarry Smith Input Parameters: 3781cdcf91faSSean Farley + ts - the TS context 3782325fc9f4SBarry Smith . x - input vector 3783325fc9f4SBarry Smith . A, B - the matrices 3784325fc9f4SBarry Smith - ctx - user context 3785325fc9f4SBarry Smith 3786325fc9f4SBarry Smith Output Parameter: 3787325fc9f4SBarry Smith . flag - structure of the matrix 3788325fc9f4SBarry Smith 3789325fc9f4SBarry Smith Level: developer 3790325fc9f4SBarry Smith 3791325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 3792325fc9f4SBarry Smith 3793325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3794325fc9f4SBarry Smith @*/ 3795cdcf91faSSean Farley PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 3796325fc9f4SBarry Smith { 3797325fc9f4SBarry Smith PetscErrorCode ierr; 3798325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3799325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 3800325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 3801325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 3802325fc9f4SBarry Smith 3803325fc9f4SBarry Smith PetscFunctionBegin; 3804cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3805325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3806325fc9f4SBarry Smith 3807325fc9f4SBarry Smith /* call Matlab function in ctx with arguments x and y */ 3808325fc9f4SBarry Smith 3809cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 3810325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3811325fc9f4SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr); 3812325fc9f4SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 3813325fc9f4SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 3814325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3815325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 3816325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 3817325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 3818325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 3819325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 3820325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 3821325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 3822325fc9f4SBarry Smith prhs[8] = sctx->ctx; 3823325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 3824325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3825325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 3826325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 3827325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 3828325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 3829325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 3830325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 3831325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 3832325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 3833325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 3834325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 3835325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 3836325fc9f4SBarry Smith PetscFunctionReturn(0); 3837325fc9f4SBarry Smith } 3838325fc9f4SBarry Smith 3839325fc9f4SBarry Smith 3840325fc9f4SBarry Smith #undef __FUNCT__ 3841325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 3842325fc9f4SBarry Smith /* 3843325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 3844e3c5b3baSBarry 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 3845325fc9f4SBarry Smith 3846325fc9f4SBarry Smith Logically Collective on TS 3847325fc9f4SBarry Smith 3848325fc9f4SBarry Smith Input Parameters: 3849cdcf91faSSean Farley + ts - the TS context 3850325fc9f4SBarry Smith . A,B - Jacobian matrices 3851325fc9f4SBarry Smith . func - function evaluation routine 3852325fc9f4SBarry Smith - ctx - user context 3853325fc9f4SBarry Smith 3854325fc9f4SBarry Smith Calling sequence of func: 3855cdcf91faSSean Farley $ flag = func (TS ts,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx); 3856325fc9f4SBarry Smith 3857325fc9f4SBarry Smith 3858325fc9f4SBarry Smith Level: developer 3859325fc9f4SBarry Smith 3860325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 3861325fc9f4SBarry Smith 3862325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3863325fc9f4SBarry Smith */ 3864cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 3865325fc9f4SBarry Smith { 3866325fc9f4SBarry Smith PetscErrorCode ierr; 3867325fc9f4SBarry Smith TSMatlabContext *sctx; 3868325fc9f4SBarry Smith 3869325fc9f4SBarry Smith PetscFunctionBegin; 3870325fc9f4SBarry Smith /* currently sctx is memory bleed */ 3871325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3872325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3873325fc9f4SBarry Smith /* 3874325fc9f4SBarry Smith This should work, but it doesn't 3875325fc9f4SBarry Smith sctx->ctx = ctx; 3876325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 3877325fc9f4SBarry Smith */ 3878325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 3879cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 3880325fc9f4SBarry Smith PetscFunctionReturn(0); 3881325fc9f4SBarry Smith } 3882325fc9f4SBarry Smith 3883b5b1a830SBarry Smith #undef __FUNCT__ 3884b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 3885b5b1a830SBarry Smith /* 3886b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 3887b5b1a830SBarry Smith 3888b5b1a830SBarry Smith Collective on TS 3889b5b1a830SBarry Smith 3890b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 3891b5b1a830SBarry Smith @*/ 3892cdcf91faSSean Farley PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec x, void *ctx) 3893b5b1a830SBarry Smith { 3894b5b1a830SBarry Smith PetscErrorCode ierr; 3895b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 3896a530c242SBarry Smith int nlhs = 1,nrhs = 6; 3897b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 3898b5b1a830SBarry Smith long long int lx = 0,ls = 0; 3899b5b1a830SBarry Smith 3900b5b1a830SBarry Smith PetscFunctionBegin; 3901cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3902b5b1a830SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,4); 3903b5b1a830SBarry Smith 3904cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 3905b5b1a830SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3906b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 3907b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 3908b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 3909b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 3910b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 3911b5b1a830SBarry Smith prhs[5] = sctx->ctx; 3912b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 3913b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3914b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 3915b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 3916b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 3917b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 3918b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 3919b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 3920b5b1a830SBarry Smith PetscFunctionReturn(0); 3921b5b1a830SBarry Smith } 3922b5b1a830SBarry Smith 3923b5b1a830SBarry Smith 3924b5b1a830SBarry Smith #undef __FUNCT__ 3925b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 3926b5b1a830SBarry Smith /* 3927b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 3928b5b1a830SBarry Smith 3929b5b1a830SBarry Smith Level: developer 3930b5b1a830SBarry Smith 3931b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 3932b5b1a830SBarry Smith 3933b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 3934b5b1a830SBarry Smith */ 3935cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 3936b5b1a830SBarry Smith { 3937b5b1a830SBarry Smith PetscErrorCode ierr; 3938b5b1a830SBarry Smith TSMatlabContext *sctx; 3939b5b1a830SBarry Smith 3940b5b1a830SBarry Smith PetscFunctionBegin; 3941b5b1a830SBarry Smith /* currently sctx is memory bleed */ 3942b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 3943b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3944b5b1a830SBarry Smith /* 3945b5b1a830SBarry Smith This should work, but it doesn't 3946b5b1a830SBarry Smith sctx->ctx = ctx; 3947b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 3948b5b1a830SBarry Smith */ 3949b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 3950cdcf91faSSean Farley ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 3951b5b1a830SBarry Smith PetscFunctionReturn(0); 3952b5b1a830SBarry Smith } 3953325fc9f4SBarry Smith #endif 3954b3603a34SBarry Smith 39550b039ecaSBarry Smith 39560b039ecaSBarry Smith 3957b3603a34SBarry Smith #undef __FUNCT__ 3958b3603a34SBarry Smith #define __FUNCT__ "TSMonitorSolutionODE" 3959b3603a34SBarry Smith /*@C 3960b3603a34SBarry Smith TSMonitorSolutionODE - Monitors progress of the TS solvers by plotting each component of the solution vector 3961b3603a34SBarry Smith in a time based line graph 3962b3603a34SBarry Smith 3963b3603a34SBarry Smith Collective on TS 3964b3603a34SBarry Smith 3965b3603a34SBarry Smith Input Parameters: 3966b3603a34SBarry Smith + ts - the TS context 3967b3603a34SBarry Smith . step - current time-step 3968b3603a34SBarry Smith . ptime - current time 3969b3603a34SBarry Smith - lg - a line graph object 3970b3603a34SBarry Smith 3971b3603a34SBarry Smith Level: intermediate 3972b3603a34SBarry Smith 39730b039ecaSBarry Smith Notes: each process in a parallel run displays its component solutions in a separate window 3974b3603a34SBarry Smith 3975b3603a34SBarry Smith .keywords: TS, vector, monitor, view 3976b3603a34SBarry Smith 3977b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3978b3603a34SBarry Smith @*/ 3979b3603a34SBarry Smith PetscErrorCode TSMonitorSolutionODE(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 3980b3603a34SBarry Smith { 3981b3603a34SBarry Smith PetscErrorCode ierr; 39820b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 3983b3603a34SBarry Smith const PetscScalar *yy; 398458ff32f7SBarry Smith PetscInt dim; 3985b3603a34SBarry Smith 3986b3603a34SBarry Smith PetscFunctionBegin; 398758ff32f7SBarry Smith if (!step) { 3988a9f9c1f6SBarry Smith PetscDrawAxis axis; 3989a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 3990a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 399158ff32f7SBarry Smith ierr = VecGetLocalSize(x,&dim);CHKERRQ(ierr); 39920b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 39930b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 399458ff32f7SBarry Smith } 3995b3603a34SBarry Smith ierr = VecGetArrayRead(x,&yy);CHKERRQ(ierr); 3996e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 3997e3efe391SJed Brown { 3998e3efe391SJed Brown PetscReal *yreal; 3999e3efe391SJed Brown PetscInt i,n; 4000e3efe391SJed Brown ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr); 4001e3efe391SJed Brown ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4002e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 40030b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4004e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4005e3efe391SJed Brown } 4006e3efe391SJed Brown #else 40070b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4008e3efe391SJed Brown #endif 4009b3603a34SBarry Smith ierr = VecRestoreArrayRead(x,&yy);CHKERRQ(ierr); 4010*3923b477SBarry Smith if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && (step == -1))){ 40110b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4012*3923b477SBarry Smith } 4013b3603a34SBarry Smith PetscFunctionReturn(0); 4014b3603a34SBarry Smith } 4015b3603a34SBarry Smith 4016b3603a34SBarry Smith #undef __FUNCT__ 4017ef20d060SBarry Smith #define __FUNCT__ "TSMonitorErrorODE" 4018ef20d060SBarry Smith /*@C 4019ef20d060SBarry Smith TSMonitorErrorODE - Monitors progress of the TS solvers by plotting each component of the solution vector 4020ef20d060SBarry Smith in a time based line graph 4021ef20d060SBarry Smith 4022ef20d060SBarry Smith Collective on TS 4023ef20d060SBarry Smith 4024ef20d060SBarry Smith Input Parameters: 4025ef20d060SBarry Smith + ts - the TS context 4026ef20d060SBarry Smith . step - current time-step 4027ef20d060SBarry Smith . ptime - current time 4028ef20d060SBarry Smith - lg - a line graph object 4029ef20d060SBarry Smith 4030ef20d060SBarry Smith Level: intermediate 4031ef20d060SBarry Smith 4032abd5a294SJed Brown Notes: 4033abd5a294SJed Brown Only for sequential solves. 4034abd5a294SJed Brown 4035abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 4036abd5a294SJed Brown 4037abd5a294SJed Brown Options Database Keys: 4038abd5a294SJed Brown . -ts_monitor_errorode - create a graphical monitor of error history 4039ef20d060SBarry Smith 4040ef20d060SBarry Smith .keywords: TS, vector, monitor, view 4041ef20d060SBarry Smith 4042abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 4043ef20d060SBarry Smith @*/ 4044ef20d060SBarry Smith PetscErrorCode TSMonitorErrorODE(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 4045ef20d060SBarry Smith { 4046ef20d060SBarry Smith PetscErrorCode ierr; 40470b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 4048ef20d060SBarry Smith const PetscScalar *yy; 4049ef20d060SBarry Smith Vec y; 4050a9f9c1f6SBarry Smith PetscInt dim; 4051ef20d060SBarry Smith 4052ef20d060SBarry Smith PetscFunctionBegin; 4053a9f9c1f6SBarry Smith if (!step) { 4054a9f9c1f6SBarry Smith PetscDrawAxis axis; 4055a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 4056a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 4057a9f9c1f6SBarry Smith ierr = VecGetLocalSize(x,&dim);CHKERRQ(ierr); 4058a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 4059a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 4060a9f9c1f6SBarry Smith } 4061ef20d060SBarry Smith ierr = VecDuplicate(x,&y);CHKERRQ(ierr); 4062ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 4063ef20d060SBarry Smith ierr = VecAXPY(y,-1.0,x);CHKERRQ(ierr); 4064ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 4065e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 4066e3efe391SJed Brown { 4067e3efe391SJed Brown PetscReal *yreal; 4068e3efe391SJed Brown PetscInt i,n; 4069e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 4070e3efe391SJed Brown ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr); 4071e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 40720b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 4073e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 4074e3efe391SJed Brown } 4075e3efe391SJed Brown #else 40760b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 4077e3efe391SJed Brown #endif 4078ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 4079ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 4080*3923b477SBarry Smith if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && (step == -1))){ 40810b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 4082*3923b477SBarry Smith } 4083ef20d060SBarry Smith PetscFunctionReturn(0); 4084ef20d060SBarry Smith } 4085ef20d060SBarry Smith 4086