163dd3a1aSKris Buschelman 2c6db04a5SJed Brown #include <private/tsimpl.h> /*I "petscts.h" I*/ 3d763cef2SBarry Smith 4d5ba7fb7SMatthew Knepley /* Logging support */ 57087cfbeSBarry Smith PetscClassId TS_CLASSID; 6166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 7d405a339SMatthew Knepley 84a2ae208SSatish Balay #undef __FUNCT__ 9bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions" 10bdad233fSMatthew Knepley /* 11bdad233fSMatthew Knepley TSSetTypeFromOptions - Sets the type of ts from user options. 12bdad233fSMatthew Knepley 13bdad233fSMatthew Knepley Collective on TS 14bdad233fSMatthew Knepley 15bdad233fSMatthew Knepley Input Parameter: 16bdad233fSMatthew Knepley . ts - The ts 17bdad233fSMatthew Knepley 18bdad233fSMatthew Knepley Level: intermediate 19bdad233fSMatthew Knepley 20bdad233fSMatthew Knepley .keywords: TS, set, options, database, type 21bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType() 22bdad233fSMatthew Knepley */ 236849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts) 24bdad233fSMatthew Knepley { 25ace3abfcSBarry Smith PetscBool opt; 262fc52814SBarry Smith const char *defaultType; 27bdad233fSMatthew Knepley char typeName[256]; 28dfbe8321SBarry Smith PetscErrorCode ierr; 29bdad233fSMatthew Knepley 30bdad233fSMatthew Knepley PetscFunctionBegin; 317adad957SLisandro Dalcin if (((PetscObject)ts)->type_name) { 327adad957SLisandro Dalcin defaultType = ((PetscObject)ts)->type_name; 33bdad233fSMatthew Knepley } else { 349596e0b4SJed Brown defaultType = TSEULER; 35bdad233fSMatthew Knepley } 36bdad233fSMatthew Knepley 37cce0b1b2SLisandro Dalcin if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 38bdad233fSMatthew Knepley ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 39a7cc72afSBarry Smith if (opt) { 40bdad233fSMatthew Knepley ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 41bdad233fSMatthew Knepley } else { 42bdad233fSMatthew Knepley ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 43bdad233fSMatthew Knepley } 44bdad233fSMatthew Knepley PetscFunctionReturn(0); 45bdad233fSMatthew Knepley } 46bdad233fSMatthew Knepley 47bdad233fSMatthew Knepley #undef __FUNCT__ 48bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions" 49bdad233fSMatthew Knepley /*@ 50bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 51bdad233fSMatthew Knepley 52bdad233fSMatthew Knepley Collective on TS 53bdad233fSMatthew Knepley 54bdad233fSMatthew Knepley Input Parameter: 55bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 56bdad233fSMatthew Knepley 57bdad233fSMatthew Knepley Options Database Keys: 584d91e141SJed Brown + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP 59bdad233fSMatthew Knepley . -ts_max_steps maxsteps - maximum number of time-steps to take 60bdad233fSMatthew Knepley . -ts_max_time time - maximum time to compute to 61bdad233fSMatthew Knepley . -ts_dt dt - initial time step 62bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 63a6570f20SBarry Smith - -ts_monitor_draw - plot information at each timestep 64bdad233fSMatthew Knepley 65bdad233fSMatthew Knepley Level: beginner 66bdad233fSMatthew Knepley 67bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 68bdad233fSMatthew Knepley 69a313700dSBarry Smith .seealso: TSGetType() 70bdad233fSMatthew Knepley @*/ 717087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 72bdad233fSMatthew Knepley { 73bdad233fSMatthew Knepley PetscReal dt; 74ace3abfcSBarry Smith PetscBool opt,flg; 75dfbe8321SBarry Smith PetscErrorCode ierr; 76649052a6SBarry Smith PetscViewer monviewer; 77eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 78*089b2837SJed Brown SNES snes; 79bdad233fSMatthew Knepley 80bdad233fSMatthew Knepley PetscFunctionBegin; 810700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 827adad957SLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)ts)->comm, ((PetscObject)ts)->prefix, "Time step options", "TS");CHKERRQ(ierr); 83bdad233fSMatthew Knepley 84bdad233fSMatthew Knepley /* Handle generic TS options */ 85bdad233fSMatthew Knepley ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr); 86bdad233fSMatthew Knepley ierr = PetscOptionsReal("-ts_max_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr); 87bdad233fSMatthew Knepley ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetInitialTime", ts->ptime, &ts->ptime, PETSC_NULL);CHKERRQ(ierr); 88bdad233fSMatthew Knepley ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetInitialTimeStep",ts->initial_time_step,&dt,&opt);CHKERRQ(ierr); 89a7cc72afSBarry Smith if (opt) { 90bdad233fSMatthew Knepley ts->initial_time_step = ts->time_step = dt; 91bdad233fSMatthew Knepley } 92bdad233fSMatthew Knepley 93bdad233fSMatthew Knepley /* Monitor options */ 94a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 95eabae89aSBarry Smith if (flg) { 96649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr); 97649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 98bdad233fSMatthew Knepley } 9990d69ab7SBarry Smith opt = PETSC_FALSE; 100acfcf0e5SJed Brown ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr); 101a7cc72afSBarry Smith if (opt) { 102a6570f20SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 103bdad233fSMatthew Knepley } 10490d69ab7SBarry Smith opt = PETSC_FALSE; 105acfcf0e5SJed Brown ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr); 106a7cc72afSBarry Smith if (opt) { 107a6570f20SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolution,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 108bdad233fSMatthew Knepley } 109bdad233fSMatthew Knepley 110bdad233fSMatthew Knepley /* Handle TS type options */ 111bdad233fSMatthew Knepley ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr); 112bdad233fSMatthew Knepley 113bdad233fSMatthew Knepley /* Handle specific TS options */ 114abc0a331SBarry Smith if (ts->ops->setfromoptions) { 115bdad233fSMatthew Knepley ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr); 116bdad233fSMatthew Knepley } 1175d973c19SBarry Smith 1185d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 1195d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 120bdad233fSMatthew Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 121bdad233fSMatthew Knepley 122*089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 123bdad233fSMatthew Knepley /* Handle subobject options */ 124*089b2837SJed Brown if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 125*089b2837SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 126bdad233fSMatthew Knepley PetscFunctionReturn(0); 127bdad233fSMatthew Knepley } 128bdad233fSMatthew Knepley 129bdad233fSMatthew Knepley #undef __FUNCT__ 130bdad233fSMatthew Knepley #define __FUNCT__ "TSViewFromOptions" 131bdad233fSMatthew Knepley /*@ 132bdad233fSMatthew Knepley TSViewFromOptions - This function visualizes the ts based upon user options. 133bdad233fSMatthew Knepley 134bdad233fSMatthew Knepley Collective on TS 135bdad233fSMatthew Knepley 136bdad233fSMatthew Knepley Input Parameter: 137bdad233fSMatthew Knepley . ts - The ts 138bdad233fSMatthew Knepley 139bdad233fSMatthew Knepley Level: intermediate 140bdad233fSMatthew Knepley 141bdad233fSMatthew Knepley .keywords: TS, view, options, database 142bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSView() 143bdad233fSMatthew Knepley @*/ 1447087cfbeSBarry Smith PetscErrorCode TSViewFromOptions(TS ts,const char title[]) 145bdad233fSMatthew Knepley { 146bdad233fSMatthew Knepley PetscViewer viewer; 147bdad233fSMatthew Knepley PetscDraw draw; 148ace3abfcSBarry Smith PetscBool opt = PETSC_FALSE; 149e10c49a3SBarry Smith char fileName[PETSC_MAX_PATH_LEN]; 150dfbe8321SBarry Smith PetscErrorCode ierr; 151bdad233fSMatthew Knepley 152bdad233fSMatthew Knepley PetscFunctionBegin; 1537adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)ts)->prefix, "-ts_view", fileName, PETSC_MAX_PATH_LEN, &opt);CHKERRQ(ierr); 154eabae89aSBarry Smith if (opt && !PetscPreLoadingOn) { 1557adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,fileName,&viewer);CHKERRQ(ierr); 156bdad233fSMatthew Knepley ierr = TSView(ts, viewer);CHKERRQ(ierr); 1576bf464f9SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 158bdad233fSMatthew Knepley } 1598e83347fSKai Germaschewski opt = PETSC_FALSE; 160acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix, "-ts_view_draw", &opt,PETSC_NULL);CHKERRQ(ierr); 161a7cc72afSBarry Smith if (opt) { 1627adad957SLisandro Dalcin ierr = PetscViewerDrawOpen(((PetscObject)ts)->comm, 0, 0, 0, 0, 300, 300, &viewer);CHKERRQ(ierr); 163bdad233fSMatthew Knepley ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr); 164a7cc72afSBarry Smith if (title) { 1651836bdbcSSatish Balay ierr = PetscDrawSetTitle(draw, (char *)title);CHKERRQ(ierr); 166bdad233fSMatthew Knepley } else { 167bdad233fSMatthew Knepley ierr = PetscObjectName((PetscObject)ts);CHKERRQ(ierr); 1687adad957SLisandro Dalcin ierr = PetscDrawSetTitle(draw, ((PetscObject)ts)->name);CHKERRQ(ierr); 169bdad233fSMatthew Knepley } 170bdad233fSMatthew Knepley ierr = TSView(ts, viewer);CHKERRQ(ierr); 171bdad233fSMatthew Knepley ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 172bdad233fSMatthew Knepley ierr = PetscDrawPause(draw);CHKERRQ(ierr); 1736bf464f9SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 174bdad233fSMatthew Knepley } 175bdad233fSMatthew Knepley PetscFunctionReturn(0); 176bdad233fSMatthew Knepley } 177bdad233fSMatthew Knepley 178bdad233fSMatthew Knepley #undef __FUNCT__ 1794a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian" 180a7a1495cSBarry Smith /*@ 1818c385f81SBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 182a7a1495cSBarry Smith set with TSSetRHSJacobian(). 183a7a1495cSBarry Smith 184a7a1495cSBarry Smith Collective on TS and Vec 185a7a1495cSBarry Smith 186a7a1495cSBarry Smith Input Parameters: 187316643e7SJed Brown + ts - the TS context 188a7a1495cSBarry Smith . t - current timestep 189a7a1495cSBarry Smith - x - input vector 190a7a1495cSBarry Smith 191a7a1495cSBarry Smith Output Parameters: 192a7a1495cSBarry Smith + A - Jacobian matrix 193a7a1495cSBarry Smith . B - optional preconditioning matrix 194a7a1495cSBarry Smith - flag - flag indicating matrix structure 195a7a1495cSBarry Smith 196a7a1495cSBarry Smith Notes: 197a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 198a7a1495cSBarry Smith is used internally within the nonlinear solvers. 199a7a1495cSBarry Smith 20094b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 201a7a1495cSBarry Smith flag parameter. 202a7a1495cSBarry Smith 203a7a1495cSBarry Smith TSComputeJacobian() is valid only for TS_NONLINEAR 204a7a1495cSBarry Smith 205a7a1495cSBarry Smith Level: developer 206a7a1495cSBarry Smith 207a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 208a7a1495cSBarry Smith 20994b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 210a7a1495cSBarry Smith @*/ 2117087cfbeSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg) 212a7a1495cSBarry Smith { 213dfbe8321SBarry Smith PetscErrorCode ierr; 214a7a1495cSBarry Smith 215a7a1495cSBarry Smith PetscFunctionBegin; 2160700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2170700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 218c9780b6fSBarry Smith PetscCheckSameComm(ts,1,X,3); 21917186662SBarry Smith if (ts->problem_type != TS_NONLINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"For TS_NONLINEAR only"); 220000e7ae3SMatthew Knepley if (ts->ops->rhsjacobian) { 221d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 222a7a1495cSBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 223a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 224000e7ae3SMatthew Knepley ierr = (*ts->ops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr); 225a7a1495cSBarry Smith PetscStackPop; 226d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 227a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 2280700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,4); 2290700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,5); 230ef66eb69SBarry Smith } else { 231ef66eb69SBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 232ef66eb69SBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 233ef66eb69SBarry Smith if (*A != *B) { 234ef66eb69SBarry Smith ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 235ef66eb69SBarry Smith ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 236ef66eb69SBarry Smith } 237*089b2837SJed Brown *flg = ts->rhsmatstructure; 238ef66eb69SBarry Smith } 239a7a1495cSBarry Smith PetscFunctionReturn(0); 240a7a1495cSBarry Smith } 241a7a1495cSBarry Smith 2424a2ae208SSatish Balay #undef __FUNCT__ 2434a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 244316643e7SJed Brown /*@ 245d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 246d763cef2SBarry Smith 247316643e7SJed Brown Collective on TS and Vec 248316643e7SJed Brown 249316643e7SJed Brown Input Parameters: 250316643e7SJed Brown + ts - the TS context 251316643e7SJed Brown . t - current time 252316643e7SJed Brown - x - state vector 253316643e7SJed Brown 254316643e7SJed Brown Output Parameter: 255316643e7SJed Brown . y - right hand side 256316643e7SJed Brown 257316643e7SJed Brown Note: 258316643e7SJed Brown Most users should not need to explicitly call this routine, as it 259316643e7SJed Brown is used internally within the nonlinear solvers. 260316643e7SJed Brown 261316643e7SJed Brown If the user did not provide a function but merely a matrix, 262d763cef2SBarry Smith this routine applies the matrix. 263316643e7SJed Brown 264316643e7SJed Brown Level: developer 265316643e7SJed Brown 266316643e7SJed Brown .keywords: TS, compute 267316643e7SJed Brown 268316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 269316643e7SJed Brown @*/ 270dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y) 271d763cef2SBarry Smith { 272dfbe8321SBarry Smith PetscErrorCode ierr; 273d763cef2SBarry Smith 274d763cef2SBarry Smith PetscFunctionBegin; 2750700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2760700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2770700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 278d763cef2SBarry Smith 279d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 280000e7ae3SMatthew Knepley if (ts->ops->rhsfunction) { 281d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 282000e7ae3SMatthew Knepley ierr = (*ts->ops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr); 283d763cef2SBarry Smith PetscStackPop; 284*089b2837SJed Brown } else if (ts->ops->rhsmatrix) { 285*089b2837SJed Brown Mat A,B; 286*089b2837SJed Brown ierr = TSGetMatrices(ts,&A,&B,PETSC_NULL, PETSC_NULL,PETSC_NULL,PETSC_NULL, PETSC_NULL);CHKERRQ(ierr); 287*089b2837SJed Brown ts->rhsmatstructure = DIFFERENT_NONZERO_PATTERN; 288d763cef2SBarry Smith PetscStackPush("TS user right-hand-side matrix function"); 289*089b2837SJed Brown ierr = (*ts->ops->rhsmatrix)(ts,t,&A,&B,&ts->rhsmatstructure,ts->jacP);CHKERRQ(ierr); 290d763cef2SBarry Smith PetscStackPop; 291*089b2837SJed Brown /* call TSSetMatrices() in case the user changed the pointers */ 292*089b2837SJed Brown ierr = TSSetMatrices(ts,A,B,PETSC_NULL, PETSC_NULL,PETSC_NULL,PETSC_NULL, PETSC_NULL,ts->jacP);CHKERRQ(ierr); 293*089b2837SJed Brown ierr = MatMult(A,x,y);CHKERRQ(ierr); 294d16dab79SJed Brown } else SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"No RHS provided, must call TSSetRHSFunction() or TSSetMatrices()"); 295d763cef2SBarry Smith 296d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 297d763cef2SBarry Smith 298d763cef2SBarry Smith PetscFunctionReturn(0); 299d763cef2SBarry Smith } 300d763cef2SBarry Smith 3014a2ae208SSatish Balay #undef __FUNCT__ 302316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 303316643e7SJed Brown /*@ 304316643e7SJed Brown TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0 305316643e7SJed Brown 306316643e7SJed Brown Collective on TS and Vec 307316643e7SJed Brown 308316643e7SJed Brown Input Parameters: 309316643e7SJed Brown + ts - the TS context 310316643e7SJed Brown . t - current time 311316643e7SJed Brown . X - state vector 312316643e7SJed Brown - Xdot - time derivative of state vector 313316643e7SJed Brown 314316643e7SJed Brown Output Parameter: 315316643e7SJed Brown . Y - right hand side 316316643e7SJed Brown 317316643e7SJed Brown Note: 318316643e7SJed Brown Most users should not need to explicitly call this routine, as it 319316643e7SJed Brown is used internally within the nonlinear solvers. 320316643e7SJed Brown 321316643e7SJed Brown If the user did did not write their equations in implicit form, this 322316643e7SJed Brown function recasts them in implicit form. 323316643e7SJed Brown 324316643e7SJed Brown Level: developer 325316643e7SJed Brown 326316643e7SJed Brown .keywords: TS, compute 327316643e7SJed Brown 328316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 329316643e7SJed Brown @*/ 330316643e7SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y) 331316643e7SJed Brown { 332316643e7SJed Brown PetscErrorCode ierr; 333316643e7SJed Brown 334316643e7SJed Brown PetscFunctionBegin; 3350700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3360700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 3370700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 3380700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 339316643e7SJed Brown 340316643e7SJed Brown ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 341316643e7SJed Brown if (ts->ops->ifunction) { 342316643e7SJed Brown PetscStackPush("TS user implicit function"); 343316643e7SJed Brown ierr = (*ts->ops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr); 344316643e7SJed Brown PetscStackPop; 345316643e7SJed Brown } else { 346*089b2837SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr); 3474a6899ffSJed Brown /* Convert to implicit form: F(X,Xdot) = Alhs * Xdot - Frhs(X) */ 3484a6899ffSJed Brown if (ts->ops->lhsmatrix) { 349*089b2837SJed Brown ts->lhsmatstructure = DIFFERENT_NONZERO_PATTERN; 3504a6899ffSJed Brown PetscStackPush("TS user left-hand-side matrix function"); 351*089b2837SJed Brown ierr = (*ts->ops->lhsmatrix)(ts,t,&ts->Alhs,&ts->Blhs,&ts->lhsmatstructure,ts->jacP);CHKERRQ(ierr); 3524a6899ffSJed Brown PetscStackPop; 3534a6899ffSJed Brown ierr = VecScale(Y,-1.);CHKERRQ(ierr); 3544a6899ffSJed Brown ierr = MatMultAdd(ts->Alhs,Xdot,Y,Y);CHKERRQ(ierr); 3554a6899ffSJed Brown } else { 356ace68cafSJed Brown ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr); 357316643e7SJed Brown } 3584a6899ffSJed Brown } 359316643e7SJed Brown ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 360316643e7SJed Brown PetscFunctionReturn(0); 361316643e7SJed Brown } 362316643e7SJed Brown 363316643e7SJed Brown #undef __FUNCT__ 364316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 365316643e7SJed Brown /*@ 366316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 367316643e7SJed Brown 368316643e7SJed Brown Collective on TS and Vec 369316643e7SJed Brown 370316643e7SJed Brown Input 371316643e7SJed Brown Input Parameters: 372316643e7SJed Brown + ts - the TS context 373316643e7SJed Brown . t - current timestep 374316643e7SJed Brown . X - state vector 375316643e7SJed Brown . Xdot - time derivative of state vector 376316643e7SJed Brown - shift - shift to apply, see note below 377316643e7SJed Brown 378316643e7SJed Brown Output Parameters: 379316643e7SJed Brown + A - Jacobian matrix 380316643e7SJed Brown . B - optional preconditioning matrix 381316643e7SJed Brown - flag - flag indicating matrix structure 382316643e7SJed Brown 383316643e7SJed Brown Notes: 384316643e7SJed Brown If F(t,X,Xdot)=0 is the DAE, the required Jacobian is 385316643e7SJed Brown 386316643e7SJed Brown dF/dX + shift*dF/dXdot 387316643e7SJed Brown 388316643e7SJed Brown Most users should not need to explicitly call this routine, as it 389316643e7SJed Brown is used internally within the nonlinear solvers. 390316643e7SJed Brown 391316643e7SJed Brown TSComputeIJacobian() is valid only for TS_NONLINEAR 392316643e7SJed Brown 393316643e7SJed Brown Level: developer 394316643e7SJed Brown 395316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 396316643e7SJed Brown 397316643e7SJed Brown .seealso: TSSetIJacobian() 39863495f91SJed Brown @*/ 3997087cfbeSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg) 400316643e7SJed Brown { 401316643e7SJed Brown PetscErrorCode ierr; 402316643e7SJed Brown 403316643e7SJed Brown PetscFunctionBegin; 4040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4050700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 4060700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 407316643e7SJed Brown PetscValidPointer(A,6); 4080700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,6); 409316643e7SJed Brown PetscValidPointer(B,7); 4100700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,7); 411316643e7SJed Brown PetscValidPointer(flg,8); 412316643e7SJed Brown 4134a6899ffSJed Brown *flg = SAME_NONZERO_PATTERN; /* In case it we're solving a linear problem in which case it wouldn't get initialized below. */ 414316643e7SJed Brown ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 415316643e7SJed Brown if (ts->ops->ijacobian) { 416316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 417316643e7SJed Brown ierr = (*ts->ops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr); 418316643e7SJed Brown PetscStackPop; 419316643e7SJed Brown } else { 420316643e7SJed Brown if (ts->ops->rhsjacobian) { 421316643e7SJed Brown PetscStackPush("TS user right-hand-side Jacobian"); 422316643e7SJed Brown ierr = (*ts->ops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr); 423316643e7SJed Brown PetscStackPop; 424316643e7SJed Brown } else { 425316643e7SJed Brown ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 426316643e7SJed Brown ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 427316643e7SJed Brown if (*A != *B) { 428316643e7SJed Brown ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 429316643e7SJed Brown ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 430316643e7SJed Brown } 431316643e7SJed Brown } 432316643e7SJed Brown 433316643e7SJed Brown /* Convert to implicit form */ 434316643e7SJed Brown /* inefficient because these operations will normally traverse all matrix elements twice */ 435316643e7SJed Brown ierr = MatScale(*A,-1);CHKERRQ(ierr); 4364a6899ffSJed Brown if (ts->Alhs) { 4374a6899ffSJed Brown ierr = MatAXPY(*A,shift,ts->Alhs,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); 4384a6899ffSJed Brown } else { 439316643e7SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 4404a6899ffSJed Brown } 441316643e7SJed Brown if (*A != *B) { 442316643e7SJed Brown ierr = MatScale(*B,-1);CHKERRQ(ierr); 443316643e7SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 444316643e7SJed Brown } 445316643e7SJed Brown } 446316643e7SJed Brown ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 447316643e7SJed Brown PetscFunctionReturn(0); 448316643e7SJed Brown } 449316643e7SJed Brown 450316643e7SJed Brown #undef __FUNCT__ 4514a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 452d763cef2SBarry Smith /*@C 453d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 454d763cef2SBarry Smith F(t,u), where U_t = F(t,u). 455d763cef2SBarry Smith 4563f9fe445SBarry Smith Logically Collective on TS 457d763cef2SBarry Smith 458d763cef2SBarry Smith Input Parameters: 459d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 460*089b2837SJed Brown . r - vector to put the computed right hand side 461d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 462d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 463d763cef2SBarry Smith function evaluation routine (may be PETSC_NULL) 464d763cef2SBarry Smith 465d763cef2SBarry Smith Calling sequence of func: 46687828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 467d763cef2SBarry Smith 468d763cef2SBarry Smith + t - current timestep 469d763cef2SBarry Smith . u - input vector 470d763cef2SBarry Smith . F - function vector 471d763cef2SBarry Smith - ctx - [optional] user-defined function context 472d763cef2SBarry Smith 473d763cef2SBarry Smith Important: 47476f2fa84SHong Zhang The user MUST call either this routine or TSSetMatrices(). 475d763cef2SBarry Smith 476d763cef2SBarry Smith Level: beginner 477d763cef2SBarry Smith 478d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 479d763cef2SBarry Smith 48076f2fa84SHong Zhang .seealso: TSSetMatrices() 481d763cef2SBarry Smith @*/ 482*089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 483d763cef2SBarry Smith { 484*089b2837SJed Brown PetscErrorCode ierr; 485*089b2837SJed Brown SNES snes; 486d763cef2SBarry Smith 487*089b2837SJed Brown PetscFunctionBegin; 4880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 48917186662SBarry Smith if (ts->problem_type == TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot set function for linear problem"); 490000e7ae3SMatthew Knepley ts->ops->rhsfunction = f; 491d763cef2SBarry Smith ts->funP = ctx; 492*089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 493*089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 494d763cef2SBarry Smith PetscFunctionReturn(0); 495d763cef2SBarry Smith } 496d763cef2SBarry Smith 4974a2ae208SSatish Balay #undef __FUNCT__ 49895f0b562SHong Zhang #define __FUNCT__ "TSSetMatrices" 49995f0b562SHong Zhang /*@C 50095f0b562SHong Zhang TSSetMatrices - Sets the functions to compute the matrices Alhs and Arhs, 50195f0b562SHong Zhang where Alhs(t) U_t = Arhs(t) U. 50295f0b562SHong Zhang 5033f9fe445SBarry Smith Logically Collective on TS 50495f0b562SHong Zhang 50595f0b562SHong Zhang Input Parameters: 50695f0b562SHong Zhang + ts - the TS context obtained from TSCreate() 50795f0b562SHong Zhang . Arhs - matrix 508*089b2837SJed Brown . Brhs - preconditioning matrix 509*089b2837SJed Brown . frhs - the matrix evaluation routine for Arhs and Brhs; use PETSC_NULL (PETSC_NULL_FUNCTION in fortran) 51095f0b562SHong Zhang if Arhs is not a function of t. 51195f0b562SHong Zhang . Alhs - matrix or PETSC_NULL if Alhs is an indentity matrix. 512*089b2837SJed Brown . Blhs - preconditioning matrix or PETSC_NULL if Blhs is an identity matrix 51395f0b562SHong Zhang . flhs - the matrix evaluation routine for Alhs; use PETSC_NULL (PETSC_NULL_FUNCTION in fortran) 51495f0b562SHong Zhang if Alhs is not a function of t. 51595f0b562SHong Zhang . flag - flag indicating information about the matrix structure of Arhs and Alhs. 51695f0b562SHong Zhang The available options are 51795f0b562SHong Zhang SAME_NONZERO_PATTERN - Alhs has the same nonzero structure as Arhs 51895f0b562SHong Zhang DIFFERENT_NONZERO_PATTERN - Alhs has different nonzero structure as Arhs 51995f0b562SHong Zhang - ctx - [optional] user-defined context for private data for the 52095f0b562SHong Zhang matrix evaluation routine (may be PETSC_NULL) 52195f0b562SHong Zhang 52295f0b562SHong Zhang Calling sequence of func: 52395f0b562SHong Zhang $ func(TS ts,PetscReal t,Mat *A,Mat *B,PetscInt *flag,void *ctx); 52495f0b562SHong Zhang 52595f0b562SHong Zhang + t - current timestep 52695f0b562SHong Zhang . A - matrix A, where U_t = A(t) U 52795f0b562SHong Zhang . B - preconditioner matrix, usually the same as A 52895f0b562SHong Zhang . flag - flag indicating information about the preconditioner matrix 52995f0b562SHong Zhang structure (same as flag in KSPSetOperators()) 53095f0b562SHong Zhang - ctx - [optional] user-defined context for matrix evaluation routine 53195f0b562SHong Zhang 53295f0b562SHong Zhang Notes: 53395f0b562SHong Zhang The routine func() takes Mat* as the matrix arguments rather than Mat. 53495f0b562SHong Zhang This allows the matrix evaluation routine to replace Arhs or Alhs with a 53595f0b562SHong Zhang completely new new matrix structure (not just different matrix elements) 53695f0b562SHong Zhang when appropriate, for instance, if the nonzero structure is changing 53795f0b562SHong Zhang throughout the global iterations. 53895f0b562SHong Zhang 53995f0b562SHong Zhang Important: 54095f0b562SHong Zhang The user MUST call either this routine or TSSetRHSFunction(). 54195f0b562SHong Zhang 54295f0b562SHong Zhang Level: beginner 54395f0b562SHong Zhang 54495f0b562SHong Zhang .keywords: TS, timestep, set, matrix 54595f0b562SHong Zhang 54695f0b562SHong Zhang .seealso: TSSetRHSFunction() 54795f0b562SHong Zhang @*/ 548*089b2837SJed Brown PetscErrorCode TSSetMatrices(TS ts,Mat Arhs,Mat Brhs,PetscErrorCode (*frhs)(TS,PetscReal,Mat*,Mat*,MatStructure*,void*),Mat Alhs,Mat Blhs,PetscErrorCode (*flhs)(TS,PetscReal,Mat*,Mat*,MatStructure*,void*),MatStructure flag,void *ctx) 54995f0b562SHong Zhang { 550277b19d0SLisandro Dalcin PetscErrorCode ierr; 551*089b2837SJed Brown SNES snes; 552277b19d0SLisandro Dalcin 55395f0b562SHong Zhang PetscFunctionBegin; 5540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 555277b19d0SLisandro Dalcin if (frhs) ts->ops->rhsmatrix = frhs; 556277b19d0SLisandro Dalcin if (flhs) ts->ops->lhsmatrix = flhs; 557277b19d0SLisandro Dalcin if (ctx) ts->jacP = ctx; 558*089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 559*089b2837SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 560*089b2837SJed Brown ierr = SNESSetJacobian(snes,Arhs,Brhs,SNESTSFormJacobian,ts);CHKERRQ(ierr); 56192af4f6aSHong Zhang if (Alhs) { 5620700a824SBarry Smith PetscValidHeaderSpecific(Alhs,MAT_CLASSID,4); 56392af4f6aSHong Zhang PetscCheckSameComm(ts,1,Alhs,4); 564277b19d0SLisandro Dalcin ierr = PetscObjectReference((PetscObject)Alhs);CHKERRQ(ierr); 5656bf464f9SBarry Smith ierr = MatDestroy(&ts->Alhs);CHKERRQ(ierr); 56695f0b562SHong Zhang ts->Alhs = Alhs; 56792af4f6aSHong Zhang } 568*089b2837SJed Brown if (Blhs) { 569*089b2837SJed Brown PetscValidHeaderSpecific(Blhs,MAT_CLASSID,4); 570*089b2837SJed Brown PetscCheckSameComm(ts,1,Blhs,4); 571*089b2837SJed Brown ierr = PetscObjectReference((PetscObject)Blhs);CHKERRQ(ierr); 572*089b2837SJed Brown ierr = MatDestroy(&ts->Blhs);CHKERRQ(ierr); 573*089b2837SJed Brown ts->Blhs = Blhs; 574*089b2837SJed Brown } 575*089b2837SJed Brown ts->rhsmatstructure = flag; 576*089b2837SJed Brown ts->lhsmatstructure = flag; 57795f0b562SHong Zhang PetscFunctionReturn(0); 57895f0b562SHong Zhang } 579d763cef2SBarry Smith 580aa644b49SHong Zhang #undef __FUNCT__ 581cda39b92SHong Zhang #define __FUNCT__ "TSGetMatrices" 582cda39b92SHong Zhang /*@C 583cda39b92SHong Zhang TSGetMatrices - Returns the matrices Arhs and Alhs at the present timestep, 584cda39b92SHong Zhang where Alhs(t) U_t = Arhs(t) U. 585cda39b92SHong Zhang 586cda39b92SHong Zhang Not Collective, but parallel objects are returned if TS is parallel 587cda39b92SHong Zhang 588cda39b92SHong Zhang Input Parameter: 589cda39b92SHong Zhang . ts - The TS context obtained from TSCreate() 590cda39b92SHong Zhang 591cda39b92SHong Zhang Output Parameters: 592cda39b92SHong Zhang + Arhs - The right-hand side matrix 593cda39b92SHong Zhang . Alhs - The left-hand side matrix 594cda39b92SHong Zhang - ctx - User-defined context for matrix evaluation routine 595cda39b92SHong Zhang 596cda39b92SHong Zhang Notes: You can pass in PETSC_NULL for any return argument you do not need. 597cda39b92SHong Zhang 598cda39b92SHong Zhang Level: intermediate 599cda39b92SHong Zhang 600cda39b92SHong Zhang .seealso: TSSetMatrices(), TSGetTimeStep(), TSGetTime(), TSGetTimeStepNumber(), TSGetRHSJacobian() 601cda39b92SHong Zhang 602cda39b92SHong Zhang .keywords: TS, timestep, get, matrix 603cda39b92SHong Zhang 604cda39b92SHong Zhang @*/ 605*089b2837SJed Brown PetscErrorCode TSGetMatrices(TS ts,Mat *Arhs,Mat *Brhs,TSMatrix *frhs,Mat *Alhs,Mat *Blhs,TSMatrix *flhs,void **ctx) 606cda39b92SHong Zhang { 607*089b2837SJed Brown PetscErrorCode ierr; 608*089b2837SJed Brown SNES snes; 609*089b2837SJed Brown 610cda39b92SHong Zhang PetscFunctionBegin; 6110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 612*089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 613*089b2837SJed Brown ierr = SNESGetJacobian(snes,Arhs,Brhs,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 614*089b2837SJed Brown if (frhs) *frhs = ts->ops->rhsmatrix; 615cda39b92SHong Zhang if (Alhs) *Alhs = ts->Alhs; 616*089b2837SJed Brown if (Blhs) *Blhs = ts->Blhs; 617*089b2837SJed Brown if (flhs) *flhs = ts->ops->lhsmatrix; 618cda39b92SHong Zhang if (ctx) *ctx = ts->jacP; 619cda39b92SHong Zhang PetscFunctionReturn(0); 620cda39b92SHong Zhang } 621cda39b92SHong Zhang 622cda39b92SHong Zhang #undef __FUNCT__ 6234a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 624d763cef2SBarry Smith /*@C 625d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 626d763cef2SBarry Smith where U_t = F(U,t), as well as the location to store the matrix. 62776f2fa84SHong Zhang Use TSSetMatrices() for linear problems. 628d763cef2SBarry Smith 6293f9fe445SBarry Smith Logically Collective on TS 630d763cef2SBarry Smith 631d763cef2SBarry Smith Input Parameters: 632d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 633d763cef2SBarry Smith . A - Jacobian matrix 634d763cef2SBarry Smith . B - preconditioner matrix (usually same as A) 635d763cef2SBarry Smith . f - the Jacobian evaluation routine 636d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 637d763cef2SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) 638d763cef2SBarry Smith 639d763cef2SBarry Smith Calling sequence of func: 64087828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 641d763cef2SBarry Smith 642d763cef2SBarry Smith + t - current timestep 643d763cef2SBarry Smith . u - input vector 644d763cef2SBarry Smith . A - matrix A, where U_t = A(t)u 645d763cef2SBarry Smith . B - preconditioner matrix, usually the same as A 646d763cef2SBarry Smith . flag - flag indicating information about the preconditioner matrix 64794b7f48cSBarry Smith structure (same as flag in KSPSetOperators()) 648d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 649d763cef2SBarry Smith 650d763cef2SBarry Smith Notes: 65194b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 652d763cef2SBarry Smith output parameter in the routine func(). Be sure to read this information! 653d763cef2SBarry Smith 654d763cef2SBarry Smith The routine func() takes Mat * as the matrix arguments rather than Mat. 655d763cef2SBarry Smith This allows the matrix evaluation routine to replace A and/or B with a 65656335db2SHong Zhang completely new matrix structure (not just different matrix elements) 657d763cef2SBarry Smith when appropriate, for instance, if the nonzero structure is changing 658d763cef2SBarry Smith throughout the global iterations. 659d763cef2SBarry Smith 660d763cef2SBarry Smith Level: beginner 661d763cef2SBarry Smith 662d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 663d763cef2SBarry Smith 664d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(), 66576f2fa84SHong Zhang SNESDefaultComputeJacobianColor(), TSSetRHSFunction(), TSSetMatrices() 666d763cef2SBarry Smith 667d763cef2SBarry Smith @*/ 668*089b2837SJed Brown PetscErrorCode TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx) 669d763cef2SBarry Smith { 670277b19d0SLisandro Dalcin PetscErrorCode ierr; 671*089b2837SJed Brown SNES snes; 672277b19d0SLisandro Dalcin 673d763cef2SBarry Smith PetscFunctionBegin; 6740700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 675277b19d0SLisandro Dalcin if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 676277b19d0SLisandro Dalcin if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 677277b19d0SLisandro Dalcin if (A) PetscCheckSameComm(ts,1,A,2); 678277b19d0SLisandro Dalcin if (B) PetscCheckSameComm(ts,1,B,3); 67917186662SBarry Smith if (ts->problem_type != TS_NONLINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not for linear problems; use TSSetMatrices()"); 680d763cef2SBarry Smith 681277b19d0SLisandro Dalcin if (f) ts->ops->rhsjacobian = f; 682277b19d0SLisandro Dalcin if (ctx) ts->jacP = ctx; 683*089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 684*089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 685d763cef2SBarry Smith PetscFunctionReturn(0); 686d763cef2SBarry Smith } 687d763cef2SBarry Smith 688316643e7SJed Brown 689316643e7SJed Brown #undef __FUNCT__ 690316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 691316643e7SJed Brown /*@C 692316643e7SJed Brown TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved. 693316643e7SJed Brown 6943f9fe445SBarry Smith Logically Collective on TS 695316643e7SJed Brown 696316643e7SJed Brown Input Parameters: 697316643e7SJed Brown + ts - the TS context obtained from TSCreate() 698316643e7SJed Brown . f - the function evaluation routine 699316643e7SJed Brown - ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL) 700316643e7SJed Brown 701316643e7SJed Brown Calling sequence of f: 702316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 703316643e7SJed Brown 704316643e7SJed Brown + t - time at step/stage being solved 705316643e7SJed Brown . u - state vector 706316643e7SJed Brown . u_t - time derivative of state vector 707316643e7SJed Brown . F - function vector 708316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 709316643e7SJed Brown 710316643e7SJed Brown Important: 711316643e7SJed Brown The user MUST call either this routine, TSSetRHSFunction(), or TSSetMatrices(). This routine must be used when not solving an ODE. 712316643e7SJed Brown 713316643e7SJed Brown Level: beginner 714316643e7SJed Brown 715316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 716316643e7SJed Brown 717316643e7SJed Brown .seealso: TSSetMatrices(), TSSetRHSFunction(), TSSetIJacobian() 718316643e7SJed Brown @*/ 719*089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 720316643e7SJed Brown { 721*089b2837SJed Brown PetscErrorCode ierr; 722*089b2837SJed Brown SNES snes; 723316643e7SJed Brown 724316643e7SJed Brown PetscFunctionBegin; 7250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 726*089b2837SJed Brown if (f) ts->ops->ifunction = f; 727*089b2837SJed Brown if (ctx) ts->funP = ctx; 728*089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 729*089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 730*089b2837SJed Brown PetscFunctionReturn(0); 731*089b2837SJed Brown } 732*089b2837SJed Brown 733*089b2837SJed Brown #undef __FUNCT__ 734*089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 735*089b2837SJed Brown /*@C 736*089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 737*089b2837SJed Brown 738*089b2837SJed Brown Not Collective 739*089b2837SJed Brown 740*089b2837SJed Brown Input Parameter: 741*089b2837SJed Brown . ts - the TS context 742*089b2837SJed Brown 743*089b2837SJed Brown Output Parameter: 744*089b2837SJed Brown + r - vector to hold residual (or PETSC_NULL) 745*089b2837SJed Brown . func - the function to compute residual (or PETSC_NULL) 746*089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 747*089b2837SJed Brown 748*089b2837SJed Brown Level: advanced 749*089b2837SJed Brown 750*089b2837SJed Brown .keywords: TS, nonlinear, get, function 751*089b2837SJed Brown 752*089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 753*089b2837SJed Brown @*/ 754*089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 755*089b2837SJed Brown { 756*089b2837SJed Brown PetscErrorCode ierr; 757*089b2837SJed Brown SNES snes; 758*089b2837SJed Brown 759*089b2837SJed Brown PetscFunctionBegin; 760*089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 761*089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 762*089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 763*089b2837SJed Brown if (func) *func = ts->ops->ifunction; 764*089b2837SJed Brown if (ctx) *ctx = ts->funP; 765*089b2837SJed Brown PetscFunctionReturn(0); 766*089b2837SJed Brown } 767*089b2837SJed Brown 768*089b2837SJed Brown #undef __FUNCT__ 769*089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 770*089b2837SJed Brown /*@C 771*089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 772*089b2837SJed Brown 773*089b2837SJed Brown Not Collective 774*089b2837SJed Brown 775*089b2837SJed Brown Input Parameter: 776*089b2837SJed Brown . ts - the TS context 777*089b2837SJed Brown 778*089b2837SJed Brown Output Parameter: 779*089b2837SJed Brown + r - vector to hold computed right hand side (or PETSC_NULL) 780*089b2837SJed Brown . func - the function to compute right hand side (or PETSC_NULL) 781*089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 782*089b2837SJed Brown 783*089b2837SJed Brown Level: advanced 784*089b2837SJed Brown 785*089b2837SJed Brown .keywords: TS, nonlinear, get, function 786*089b2837SJed Brown 787*089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 788*089b2837SJed Brown @*/ 789*089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 790*089b2837SJed Brown { 791*089b2837SJed Brown PetscErrorCode ierr; 792*089b2837SJed Brown SNES snes; 793*089b2837SJed Brown 794*089b2837SJed Brown PetscFunctionBegin; 795*089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 796*089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 797*089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 798*089b2837SJed Brown if (func) *func = ts->ops->rhsfunction; 799*089b2837SJed Brown if (ctx) *ctx = ts->funP; 800316643e7SJed Brown PetscFunctionReturn(0); 801316643e7SJed Brown } 802316643e7SJed Brown 803316643e7SJed Brown #undef __FUNCT__ 804316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 805316643e7SJed Brown /*@C 806a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 807a4f0a591SBarry Smith you provided with TSSetIFunction(). 808316643e7SJed Brown 8093f9fe445SBarry Smith Logically Collective on TS 810316643e7SJed Brown 811316643e7SJed Brown Input Parameters: 812316643e7SJed Brown + ts - the TS context obtained from TSCreate() 813316643e7SJed Brown . A - Jacobian matrix 814316643e7SJed Brown . B - preconditioning matrix for A (may be same as A) 815316643e7SJed Brown . f - the Jacobian evaluation routine 816316643e7SJed Brown - ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL) 817316643e7SJed Brown 818316643e7SJed Brown Calling sequence of f: 8191b4a444bSJed Brown $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx); 820316643e7SJed Brown 821316643e7SJed Brown + t - time at step/stage being solved 8221b4a444bSJed Brown . U - state vector 8231b4a444bSJed Brown . U_t - time derivative of state vector 824316643e7SJed Brown . a - shift 8251b4a444bSJed Brown . A - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 826316643e7SJed Brown . B - preconditioning matrix for A, may be same as A 827316643e7SJed Brown . flag - flag indicating information about the preconditioner matrix 828316643e7SJed Brown structure (same as flag in KSPSetOperators()) 829316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 830316643e7SJed Brown 831316643e7SJed Brown Notes: 832316643e7SJed Brown The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve. 833316643e7SJed Brown 834a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 835a4f0a591SBarry 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. 836a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 837a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 838a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 839a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 840a4f0a591SBarry Smith 841316643e7SJed Brown Level: beginner 842316643e7SJed Brown 843316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 844316643e7SJed Brown 845316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian() 846316643e7SJed Brown 847316643e7SJed Brown @*/ 8487087cfbeSBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx) 849316643e7SJed Brown { 850316643e7SJed Brown PetscErrorCode ierr; 851*089b2837SJed Brown SNES snes; 852316643e7SJed Brown 853316643e7SJed Brown PetscFunctionBegin; 8540700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8550700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 8560700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 857316643e7SJed Brown if (A) PetscCheckSameComm(ts,1,A,2); 858316643e7SJed Brown if (B) PetscCheckSameComm(ts,1,B,3); 859316643e7SJed Brown if (f) ts->ops->ijacobian = f; 860316643e7SJed Brown if (ctx) ts->jacP = ctx; 861*089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 862*089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 863316643e7SJed Brown PetscFunctionReturn(0); 864316643e7SJed Brown } 865316643e7SJed Brown 8664a2ae208SSatish Balay #undef __FUNCT__ 8674a2ae208SSatish Balay #define __FUNCT__ "TSView" 8687e2c5f70SBarry Smith /*@C 869d763cef2SBarry Smith TSView - Prints the TS data structure. 870d763cef2SBarry Smith 8714c49b128SBarry Smith Collective on TS 872d763cef2SBarry Smith 873d763cef2SBarry Smith Input Parameters: 874d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 875d763cef2SBarry Smith - viewer - visualization context 876d763cef2SBarry Smith 877d763cef2SBarry Smith Options Database Key: 878d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 879d763cef2SBarry Smith 880d763cef2SBarry Smith Notes: 881d763cef2SBarry Smith The available visualization contexts include 882b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 883b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 884d763cef2SBarry Smith output where only the first processor opens 885d763cef2SBarry Smith the file. All other processors send their 886d763cef2SBarry Smith data to the first processor to print. 887d763cef2SBarry Smith 888d763cef2SBarry Smith The user can open an alternative visualization context with 889b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 890d763cef2SBarry Smith 891d763cef2SBarry Smith Level: beginner 892d763cef2SBarry Smith 893d763cef2SBarry Smith .keywords: TS, timestep, view 894d763cef2SBarry Smith 895b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 896d763cef2SBarry Smith @*/ 8977087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 898d763cef2SBarry Smith { 899dfbe8321SBarry Smith PetscErrorCode ierr; 900a313700dSBarry Smith const TSType type; 901*089b2837SJed Brown PetscBool iascii,isstring,isundials; 902d763cef2SBarry Smith 903d763cef2SBarry Smith PetscFunctionBegin; 9040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9053050cee2SBarry Smith if (!viewer) { 9067adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr); 9073050cee2SBarry Smith } 9080700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 909c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 910fd16b177SBarry Smith 9112692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 9122692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 91332077d6dSBarry Smith if (iascii) { 914317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr); 915000e7ae3SMatthew Knepley if (ts->ops->view) { 916b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 917000e7ae3SMatthew Knepley ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 918b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 919d763cef2SBarry Smith } 92077431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 921a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%G\n",ts->max_time);CHKERRQ(ierr); 922d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 92377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr); 924d763cef2SBarry Smith } 92577431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr); 9260f5bd95cSBarry Smith } else if (isstring) { 927a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 928b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 929d763cef2SBarry Smith } 930b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 931*089b2837SJed Brown ierr = PetscTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 932*089b2837SJed Brown if (!isundials && ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 933b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 934d763cef2SBarry Smith PetscFunctionReturn(0); 935d763cef2SBarry Smith } 936d763cef2SBarry Smith 937d763cef2SBarry Smith 9384a2ae208SSatish Balay #undef __FUNCT__ 9394a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 940b07ff414SBarry Smith /*@ 941d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 942d763cef2SBarry Smith the timesteppers. 943d763cef2SBarry Smith 9443f9fe445SBarry Smith Logically Collective on TS 945d763cef2SBarry Smith 946d763cef2SBarry Smith Input Parameters: 947d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 948d763cef2SBarry Smith - usrP - optional user context 949d763cef2SBarry Smith 950d763cef2SBarry Smith Level: intermediate 951d763cef2SBarry Smith 952d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 953d763cef2SBarry Smith 954d763cef2SBarry Smith .seealso: TSGetApplicationContext() 955d763cef2SBarry Smith @*/ 9567087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 957d763cef2SBarry Smith { 958d763cef2SBarry Smith PetscFunctionBegin; 9590700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 960d763cef2SBarry Smith ts->user = usrP; 961d763cef2SBarry Smith PetscFunctionReturn(0); 962d763cef2SBarry Smith } 963d763cef2SBarry Smith 9644a2ae208SSatish Balay #undef __FUNCT__ 9654a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 966b07ff414SBarry Smith /*@ 967d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 968d763cef2SBarry Smith timestepper. 969d763cef2SBarry Smith 970d763cef2SBarry Smith Not Collective 971d763cef2SBarry Smith 972d763cef2SBarry Smith Input Parameter: 973d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 974d763cef2SBarry Smith 975d763cef2SBarry Smith Output Parameter: 976d763cef2SBarry Smith . usrP - user context 977d763cef2SBarry Smith 978d763cef2SBarry Smith Level: intermediate 979d763cef2SBarry Smith 980d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 981d763cef2SBarry Smith 982d763cef2SBarry Smith .seealso: TSSetApplicationContext() 983d763cef2SBarry Smith @*/ 984e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 985d763cef2SBarry Smith { 986d763cef2SBarry Smith PetscFunctionBegin; 9870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 988e71120c6SJed Brown *(void**)usrP = ts->user; 989d763cef2SBarry Smith PetscFunctionReturn(0); 990d763cef2SBarry Smith } 991d763cef2SBarry Smith 9924a2ae208SSatish Balay #undef __FUNCT__ 9934a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 994d763cef2SBarry Smith /*@ 995d763cef2SBarry Smith TSGetTimeStepNumber - Gets the current number of timesteps. 996d763cef2SBarry Smith 997d763cef2SBarry Smith Not Collective 998d763cef2SBarry Smith 999d763cef2SBarry Smith Input Parameter: 1000d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1001d763cef2SBarry Smith 1002d763cef2SBarry Smith Output Parameter: 1003d763cef2SBarry Smith . iter - number steps so far 1004d763cef2SBarry Smith 1005d763cef2SBarry Smith Level: intermediate 1006d763cef2SBarry Smith 1007d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 1008d763cef2SBarry Smith @*/ 10097087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt* iter) 1010d763cef2SBarry Smith { 1011d763cef2SBarry Smith PetscFunctionBegin; 10120700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10134482741eSBarry Smith PetscValidIntPointer(iter,2); 1014d763cef2SBarry Smith *iter = ts->steps; 1015d763cef2SBarry Smith PetscFunctionReturn(0); 1016d763cef2SBarry Smith } 1017d763cef2SBarry Smith 10184a2ae208SSatish Balay #undef __FUNCT__ 10194a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1020d763cef2SBarry Smith /*@ 1021d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1022d763cef2SBarry Smith as well as the initial time. 1023d763cef2SBarry Smith 10243f9fe445SBarry Smith Logically Collective on TS 1025d763cef2SBarry Smith 1026d763cef2SBarry Smith Input Parameters: 1027d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1028d763cef2SBarry Smith . initial_time - the initial time 1029d763cef2SBarry Smith - time_step - the size of the timestep 1030d763cef2SBarry Smith 1031d763cef2SBarry Smith Level: intermediate 1032d763cef2SBarry Smith 1033d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1034d763cef2SBarry Smith 1035d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1036d763cef2SBarry Smith @*/ 10377087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1038d763cef2SBarry Smith { 1039d763cef2SBarry Smith PetscFunctionBegin; 10400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1041d763cef2SBarry Smith ts->time_step = time_step; 1042d763cef2SBarry Smith ts->initial_time_step = time_step; 1043d763cef2SBarry Smith ts->ptime = initial_time; 1044d763cef2SBarry Smith PetscFunctionReturn(0); 1045d763cef2SBarry Smith } 1046d763cef2SBarry Smith 10474a2ae208SSatish Balay #undef __FUNCT__ 10484a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1049d763cef2SBarry Smith /*@ 1050d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1051d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1052d763cef2SBarry Smith 10533f9fe445SBarry Smith Logically Collective on TS 1054d763cef2SBarry Smith 1055d763cef2SBarry Smith Input Parameters: 1056d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1057d763cef2SBarry Smith - time_step - the size of the timestep 1058d763cef2SBarry Smith 1059d763cef2SBarry Smith Level: intermediate 1060d763cef2SBarry Smith 1061d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1062d763cef2SBarry Smith 1063d763cef2SBarry Smith .keywords: TS, set, timestep 1064d763cef2SBarry Smith @*/ 10657087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1066d763cef2SBarry Smith { 1067d763cef2SBarry Smith PetscFunctionBegin; 10680700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1069c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1070d763cef2SBarry Smith ts->time_step = time_step; 1071d763cef2SBarry Smith PetscFunctionReturn(0); 1072d763cef2SBarry Smith } 1073d763cef2SBarry Smith 10744a2ae208SSatish Balay #undef __FUNCT__ 10754a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1076d763cef2SBarry Smith /*@ 1077d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1078d763cef2SBarry Smith 1079d763cef2SBarry Smith Not Collective 1080d763cef2SBarry Smith 1081d763cef2SBarry Smith Input Parameter: 1082d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1083d763cef2SBarry Smith 1084d763cef2SBarry Smith Output Parameter: 1085d763cef2SBarry Smith . dt - the current timestep size 1086d763cef2SBarry Smith 1087d763cef2SBarry Smith Level: intermediate 1088d763cef2SBarry Smith 1089d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1090d763cef2SBarry Smith 1091d763cef2SBarry Smith .keywords: TS, get, timestep 1092d763cef2SBarry Smith @*/ 10937087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal* dt) 1094d763cef2SBarry Smith { 1095d763cef2SBarry Smith PetscFunctionBegin; 10960700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10974482741eSBarry Smith PetscValidDoublePointer(dt,2); 1098d763cef2SBarry Smith *dt = ts->time_step; 1099d763cef2SBarry Smith PetscFunctionReturn(0); 1100d763cef2SBarry Smith } 1101d763cef2SBarry Smith 11024a2ae208SSatish Balay #undef __FUNCT__ 11034a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1104d8e5e3e6SSatish Balay /*@ 1105d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1106d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1107d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1108d763cef2SBarry Smith the solution at the next timestep has been calculated. 1109d763cef2SBarry Smith 1110d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1111d763cef2SBarry Smith 1112d763cef2SBarry Smith Input Parameter: 1113d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1114d763cef2SBarry Smith 1115d763cef2SBarry Smith Output Parameter: 1116d763cef2SBarry Smith . v - the vector containing the solution 1117d763cef2SBarry Smith 1118d763cef2SBarry Smith Level: intermediate 1119d763cef2SBarry Smith 1120d763cef2SBarry Smith .seealso: TSGetTimeStep() 1121d763cef2SBarry Smith 1122d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1123d763cef2SBarry Smith @*/ 11247087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1125d763cef2SBarry Smith { 1126d763cef2SBarry Smith PetscFunctionBegin; 11270700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11284482741eSBarry Smith PetscValidPointer(v,2); 11298737fe31SLisandro Dalcin *v = ts->vec_sol; 1130d763cef2SBarry Smith PetscFunctionReturn(0); 1131d763cef2SBarry Smith } 1132d763cef2SBarry Smith 1133bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 11344a2ae208SSatish Balay #undef __FUNCT__ 1135bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1136d8e5e3e6SSatish Balay /*@ 1137bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1138d763cef2SBarry Smith 1139bdad233fSMatthew Knepley Not collective 1140d763cef2SBarry Smith 1141bdad233fSMatthew Knepley Input Parameters: 1142bdad233fSMatthew Knepley + ts - The TS 1143bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1144d763cef2SBarry Smith .vb 1145d763cef2SBarry Smith U_t = A U 1146d763cef2SBarry Smith U_t = A(t) U 1147d763cef2SBarry Smith U_t = F(t,U) 1148d763cef2SBarry Smith .ve 1149d763cef2SBarry Smith 1150d763cef2SBarry Smith Level: beginner 1151d763cef2SBarry Smith 1152bdad233fSMatthew Knepley .keywords: TS, problem type 1153bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1154d763cef2SBarry Smith @*/ 11557087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1156a7cc72afSBarry Smith { 1157d763cef2SBarry Smith PetscFunctionBegin; 11580700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1159bdad233fSMatthew Knepley ts->problem_type = type; 1160d763cef2SBarry Smith PetscFunctionReturn(0); 1161d763cef2SBarry Smith } 1162d763cef2SBarry Smith 1163bdad233fSMatthew Knepley #undef __FUNCT__ 1164bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1165bdad233fSMatthew Knepley /*@C 1166bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1167bdad233fSMatthew Knepley 1168bdad233fSMatthew Knepley Not collective 1169bdad233fSMatthew Knepley 1170bdad233fSMatthew Knepley Input Parameter: 1171bdad233fSMatthew Knepley . ts - The TS 1172bdad233fSMatthew Knepley 1173bdad233fSMatthew Knepley Output Parameter: 1174bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1175bdad233fSMatthew Knepley .vb 1176*089b2837SJed Brown M U_t = A U 1177*089b2837SJed Brown M(t) U_t = A(t) U 1178bdad233fSMatthew Knepley U_t = F(t,U) 1179bdad233fSMatthew Knepley .ve 1180bdad233fSMatthew Knepley 1181bdad233fSMatthew Knepley Level: beginner 1182bdad233fSMatthew Knepley 1183bdad233fSMatthew Knepley .keywords: TS, problem type 1184bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1185bdad233fSMatthew Knepley @*/ 11867087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1187a7cc72afSBarry Smith { 1188bdad233fSMatthew Knepley PetscFunctionBegin; 11890700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 11904482741eSBarry Smith PetscValidIntPointer(type,2); 1191bdad233fSMatthew Knepley *type = ts->problem_type; 1192bdad233fSMatthew Knepley PetscFunctionReturn(0); 1193bdad233fSMatthew Knepley } 1194d763cef2SBarry Smith 11954a2ae208SSatish Balay #undef __FUNCT__ 11964a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1197d763cef2SBarry Smith /*@ 1198d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1199d763cef2SBarry Smith of a timestepper. 1200d763cef2SBarry Smith 1201d763cef2SBarry Smith Collective on TS 1202d763cef2SBarry Smith 1203d763cef2SBarry Smith Input Parameter: 1204d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1205d763cef2SBarry Smith 1206d763cef2SBarry Smith Notes: 1207d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1208d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1209d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1210d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1211d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1212d763cef2SBarry Smith 1213d763cef2SBarry Smith Level: advanced 1214d763cef2SBarry Smith 1215d763cef2SBarry Smith .keywords: TS, timestep, setup 1216d763cef2SBarry Smith 1217d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1218d763cef2SBarry Smith @*/ 12197087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1220d763cef2SBarry Smith { 1221dfbe8321SBarry Smith PetscErrorCode ierr; 1222d763cef2SBarry Smith 1223d763cef2SBarry Smith PetscFunctionBegin; 12240700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1225277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1226277b19d0SLisandro Dalcin 12277adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 12289596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1229d763cef2SBarry Smith } 1230277b19d0SLisandro Dalcin 1231277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1232277b19d0SLisandro Dalcin 1233277b19d0SLisandro Dalcin if (ts->ops->setup) { 1234000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1235277b19d0SLisandro Dalcin } 1236277b19d0SLisandro Dalcin 1237277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1238277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1239277b19d0SLisandro Dalcin } 1240277b19d0SLisandro Dalcin 1241277b19d0SLisandro Dalcin #undef __FUNCT__ 1242277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1243277b19d0SLisandro Dalcin /*@ 1244277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1245277b19d0SLisandro Dalcin 1246277b19d0SLisandro Dalcin Collective on TS 1247277b19d0SLisandro Dalcin 1248277b19d0SLisandro Dalcin Input Parameter: 1249277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1250277b19d0SLisandro Dalcin 1251277b19d0SLisandro Dalcin Level: beginner 1252277b19d0SLisandro Dalcin 1253277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1254277b19d0SLisandro Dalcin 1255277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1256277b19d0SLisandro Dalcin @*/ 1257277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1258277b19d0SLisandro Dalcin { 1259277b19d0SLisandro Dalcin PetscErrorCode ierr; 1260277b19d0SLisandro Dalcin 1261277b19d0SLisandro Dalcin PetscFunctionBegin; 1262277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1263277b19d0SLisandro Dalcin if (ts->ops->reset) { 1264277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1265277b19d0SLisandro Dalcin } 1266277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 12676bf464f9SBarry Smith ierr = MatDestroy(&ts->Alhs);CHKERRQ(ierr); 1268*089b2837SJed Brown ierr = MatDestroy(&ts->Blhs);CHKERRQ(ierr); 12696bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1270277b19d0SLisandro Dalcin if (ts->work) {ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);} 1271277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1272d763cef2SBarry Smith PetscFunctionReturn(0); 1273d763cef2SBarry Smith } 1274d763cef2SBarry Smith 12754a2ae208SSatish Balay #undef __FUNCT__ 12764a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1277d8e5e3e6SSatish Balay /*@ 1278d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1279d763cef2SBarry Smith with TSCreate(). 1280d763cef2SBarry Smith 1281d763cef2SBarry Smith Collective on TS 1282d763cef2SBarry Smith 1283d763cef2SBarry Smith Input Parameter: 1284d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1285d763cef2SBarry Smith 1286d763cef2SBarry Smith Level: beginner 1287d763cef2SBarry Smith 1288d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1289d763cef2SBarry Smith 1290d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1291d763cef2SBarry Smith @*/ 12926bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1293d763cef2SBarry Smith { 12946849ba73SBarry Smith PetscErrorCode ierr; 1295d763cef2SBarry Smith 1296d763cef2SBarry Smith PetscFunctionBegin; 12976bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 12986bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 12996bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1300d763cef2SBarry Smith 13016bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1302277b19d0SLisandro Dalcin 1303be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 13046bf464f9SBarry Smith ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 13056bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 13066d4c513bSLisandro Dalcin 13076bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 13086bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 13096bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 13106d4c513bSLisandro Dalcin 1311a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1312d763cef2SBarry Smith PetscFunctionReturn(0); 1313d763cef2SBarry Smith } 1314d763cef2SBarry Smith 13154a2ae208SSatish Balay #undef __FUNCT__ 13164a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1317d8e5e3e6SSatish Balay /*@ 1318d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1319d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1320d763cef2SBarry Smith 1321d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1322d763cef2SBarry Smith 1323d763cef2SBarry Smith Input Parameter: 1324d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1325d763cef2SBarry Smith 1326d763cef2SBarry Smith Output Parameter: 1327d763cef2SBarry Smith . snes - the nonlinear solver context 1328d763cef2SBarry Smith 1329d763cef2SBarry Smith Notes: 1330d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1331d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 133294b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1333d763cef2SBarry Smith 1334d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 1335d763cef2SBarry Smith this case TSGetSNES() returns PETSC_NULL in snes. 1336d763cef2SBarry Smith 1337d763cef2SBarry Smith Level: beginner 1338d763cef2SBarry Smith 1339d763cef2SBarry Smith .keywords: timestep, get, SNES 1340d763cef2SBarry Smith @*/ 13417087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1342d763cef2SBarry Smith { 1343d372ba47SLisandro Dalcin PetscErrorCode ierr; 1344d372ba47SLisandro Dalcin 1345d763cef2SBarry Smith PetscFunctionBegin; 13460700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13474482741eSBarry Smith PetscValidPointer(snes,2); 1348d372ba47SLisandro Dalcin if (ts->problem_type != TS_NONLINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Nonlinear only; use TSGetKSP()"); 1349d372ba47SLisandro Dalcin if (!ts->snes) { 1350d372ba47SLisandro Dalcin ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 1351d372ba47SLisandro Dalcin ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1352d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 1353d372ba47SLisandro Dalcin } 1354d763cef2SBarry Smith *snes = ts->snes; 1355d763cef2SBarry Smith PetscFunctionReturn(0); 1356d763cef2SBarry Smith } 1357d763cef2SBarry Smith 13584a2ae208SSatish Balay #undef __FUNCT__ 135994b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 1360d8e5e3e6SSatish Balay /*@ 136194b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 1362d763cef2SBarry Smith a TS (timestepper) context. 1363d763cef2SBarry Smith 136494b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 1365d763cef2SBarry Smith 1366d763cef2SBarry Smith Input Parameter: 1367d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1368d763cef2SBarry Smith 1369d763cef2SBarry Smith Output Parameter: 137094b7f48cSBarry Smith . ksp - the nonlinear solver context 1371d763cef2SBarry Smith 1372d763cef2SBarry Smith Notes: 137394b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 1374d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 1375d763cef2SBarry Smith KSP and PC contexts as well. 1376d763cef2SBarry Smith 137794b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 137894b7f48cSBarry Smith in this case TSGetKSP() returns PETSC_NULL in ksp. 1379d763cef2SBarry Smith 1380d763cef2SBarry Smith Level: beginner 1381d763cef2SBarry Smith 138294b7f48cSBarry Smith .keywords: timestep, get, KSP 1383d763cef2SBarry Smith @*/ 13847087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1385d763cef2SBarry Smith { 1386d372ba47SLisandro Dalcin PetscErrorCode ierr; 1387*089b2837SJed Brown SNES snes; 1388d372ba47SLisandro Dalcin 1389d763cef2SBarry Smith PetscFunctionBegin; 13900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13914482741eSBarry Smith PetscValidPointer(ksp,2); 139217186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1393e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1394*089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1395*089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1396d763cef2SBarry Smith PetscFunctionReturn(0); 1397d763cef2SBarry Smith } 1398d763cef2SBarry Smith 1399d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 1400d763cef2SBarry Smith 14014a2ae208SSatish Balay #undef __FUNCT__ 1402adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 1403adb62b0dSMatthew Knepley /*@ 1404adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 1405adb62b0dSMatthew Knepley maximum time for iteration. 1406adb62b0dSMatthew Knepley 14073f9fe445SBarry Smith Not Collective 1408adb62b0dSMatthew Knepley 1409adb62b0dSMatthew Knepley Input Parameters: 1410adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 1411adb62b0dSMatthew Knepley . maxsteps - maximum number of iterations to use, or PETSC_NULL 1412adb62b0dSMatthew Knepley - maxtime - final time to iterate to, or PETSC_NULL 1413adb62b0dSMatthew Knepley 1414adb62b0dSMatthew Knepley Level: intermediate 1415adb62b0dSMatthew Knepley 1416adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 1417adb62b0dSMatthew Knepley @*/ 14187087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1419adb62b0dSMatthew Knepley { 1420adb62b0dSMatthew Knepley PetscFunctionBegin; 14210700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1422abc0a331SBarry Smith if (maxsteps) { 14234482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 1424adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 1425adb62b0dSMatthew Knepley } 1426abc0a331SBarry Smith if (maxtime ) { 14274482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 1428adb62b0dSMatthew Knepley *maxtime = ts->max_time; 1429adb62b0dSMatthew Knepley } 1430adb62b0dSMatthew Knepley PetscFunctionReturn(0); 1431adb62b0dSMatthew Knepley } 1432adb62b0dSMatthew Knepley 1433adb62b0dSMatthew Knepley #undef __FUNCT__ 14344a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 1435d763cef2SBarry Smith /*@ 1436d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 1437d763cef2SBarry Smith maximum time for iteration. 1438d763cef2SBarry Smith 14393f9fe445SBarry Smith Logically Collective on TS 1440d763cef2SBarry Smith 1441d763cef2SBarry Smith Input Parameters: 1442d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1443d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 1444d763cef2SBarry Smith - maxtime - final time to iterate to 1445d763cef2SBarry Smith 1446d763cef2SBarry Smith Options Database Keys: 1447d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 1448d763cef2SBarry Smith . -ts_max_time <maxtime> - Sets maxtime 1449d763cef2SBarry Smith 1450d763cef2SBarry Smith Notes: 1451d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 1452d763cef2SBarry Smith 1453d763cef2SBarry Smith Level: intermediate 1454d763cef2SBarry Smith 1455d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 1456d763cef2SBarry Smith @*/ 14577087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1458d763cef2SBarry Smith { 1459d763cef2SBarry Smith PetscFunctionBegin; 14600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1461c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1462c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 1463d763cef2SBarry Smith ts->max_steps = maxsteps; 1464d763cef2SBarry Smith ts->max_time = maxtime; 1465d763cef2SBarry Smith PetscFunctionReturn(0); 1466d763cef2SBarry Smith } 1467d763cef2SBarry Smith 14684a2ae208SSatish Balay #undef __FUNCT__ 14694a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 1470d763cef2SBarry Smith /*@ 1471d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 1472d763cef2SBarry Smith for use by the TS routines. 1473d763cef2SBarry Smith 14743f9fe445SBarry Smith Logically Collective on TS and Vec 1475d763cef2SBarry Smith 1476d763cef2SBarry Smith Input Parameters: 1477d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1478d763cef2SBarry Smith - x - the solution vector 1479d763cef2SBarry Smith 1480d763cef2SBarry Smith Level: beginner 1481d763cef2SBarry Smith 1482d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 1483d763cef2SBarry Smith @*/ 14847087cfbeSBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec x) 1485d763cef2SBarry Smith { 14868737fe31SLisandro Dalcin PetscErrorCode ierr; 14878737fe31SLisandro Dalcin 1488d763cef2SBarry Smith PetscFunctionBegin; 14890700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14900700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 14918737fe31SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 14926bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 14938737fe31SLisandro Dalcin ts->vec_sol = x; 1494d763cef2SBarry Smith PetscFunctionReturn(0); 1495d763cef2SBarry Smith } 1496d763cef2SBarry Smith 1497e74ef692SMatthew Knepley #undef __FUNCT__ 1498e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 1499ac226902SBarry Smith /*@C 1500000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 15013f2090d5SJed Brown called once at the beginning of each time step. 1502000e7ae3SMatthew Knepley 15033f9fe445SBarry Smith Logically Collective on TS 1504000e7ae3SMatthew Knepley 1505000e7ae3SMatthew Knepley Input Parameters: 1506000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1507000e7ae3SMatthew Knepley - func - The function 1508000e7ae3SMatthew Knepley 1509000e7ae3SMatthew Knepley Calling sequence of func: 1510000e7ae3SMatthew Knepley . func (TS ts); 1511000e7ae3SMatthew Knepley 1512000e7ae3SMatthew Knepley Level: intermediate 1513000e7ae3SMatthew Knepley 1514000e7ae3SMatthew Knepley .keywords: TS, timestep 1515000e7ae3SMatthew Knepley @*/ 15167087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1517000e7ae3SMatthew Knepley { 1518000e7ae3SMatthew Knepley PetscFunctionBegin; 15190700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1520000e7ae3SMatthew Knepley ts->ops->prestep = func; 1521000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1522000e7ae3SMatthew Knepley } 1523000e7ae3SMatthew Knepley 1524e74ef692SMatthew Knepley #undef __FUNCT__ 15253f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 15263f2090d5SJed Brown /*@C 15273f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 15283f2090d5SJed Brown 15293f2090d5SJed Brown Collective on TS 15303f2090d5SJed Brown 15313f2090d5SJed Brown Input Parameters: 15323f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 15333f2090d5SJed Brown 15343f2090d5SJed Brown Notes: 15353f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 15363f2090d5SJed Brown so most users would not generally call this routine themselves. 15373f2090d5SJed Brown 15383f2090d5SJed Brown Level: developer 15393f2090d5SJed Brown 15403f2090d5SJed Brown .keywords: TS, timestep 15413f2090d5SJed Brown @*/ 15427087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 15433f2090d5SJed Brown { 15443f2090d5SJed Brown PetscErrorCode ierr; 15453f2090d5SJed Brown 15463f2090d5SJed Brown PetscFunctionBegin; 15470700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 154872ac3e02SJed Brown if (ts->ops->prestep) { 15493f2090d5SJed Brown PetscStackPush("TS PreStep function"); 15503f2090d5SJed Brown ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 15513f2090d5SJed Brown PetscStackPop; 1552312ce896SJed Brown } 15533f2090d5SJed Brown PetscFunctionReturn(0); 15543f2090d5SJed Brown } 15553f2090d5SJed Brown 15563f2090d5SJed Brown #undef __FUNCT__ 1557e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 1558ac226902SBarry Smith /*@C 1559000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 15603f2090d5SJed Brown called once at the end of each time step. 1561000e7ae3SMatthew Knepley 15623f9fe445SBarry Smith Logically Collective on TS 1563000e7ae3SMatthew Knepley 1564000e7ae3SMatthew Knepley Input Parameters: 1565000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1566000e7ae3SMatthew Knepley - func - The function 1567000e7ae3SMatthew Knepley 1568000e7ae3SMatthew Knepley Calling sequence of func: 1569000e7ae3SMatthew Knepley . func (TS ts); 1570000e7ae3SMatthew Knepley 1571000e7ae3SMatthew Knepley Level: intermediate 1572000e7ae3SMatthew Knepley 1573000e7ae3SMatthew Knepley .keywords: TS, timestep 1574000e7ae3SMatthew Knepley @*/ 15757087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 1576000e7ae3SMatthew Knepley { 1577000e7ae3SMatthew Knepley PetscFunctionBegin; 15780700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1579000e7ae3SMatthew Knepley ts->ops->poststep = func; 1580000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1581000e7ae3SMatthew Knepley } 1582000e7ae3SMatthew Knepley 1583e74ef692SMatthew Knepley #undef __FUNCT__ 15843f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 15853f2090d5SJed Brown /*@C 15863f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 15873f2090d5SJed Brown 15883f2090d5SJed Brown Collective on TS 15893f2090d5SJed Brown 15903f2090d5SJed Brown Input Parameters: 15913f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 15923f2090d5SJed Brown 15933f2090d5SJed Brown Notes: 15943f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 15953f2090d5SJed Brown so most users would not generally call this routine themselves. 15963f2090d5SJed Brown 15973f2090d5SJed Brown Level: developer 15983f2090d5SJed Brown 15993f2090d5SJed Brown .keywords: TS, timestep 16003f2090d5SJed Brown @*/ 16017087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 16023f2090d5SJed Brown { 16033f2090d5SJed Brown PetscErrorCode ierr; 16043f2090d5SJed Brown 16053f2090d5SJed Brown PetscFunctionBegin; 16060700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 160772ac3e02SJed Brown if (ts->ops->poststep) { 16083f2090d5SJed Brown PetscStackPush("TS PostStep function"); 16093f2090d5SJed Brown ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 16103f2090d5SJed Brown PetscStackPop; 161172ac3e02SJed Brown } 16123f2090d5SJed Brown PetscFunctionReturn(0); 16133f2090d5SJed Brown } 16143f2090d5SJed Brown 1615d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 1616d763cef2SBarry Smith 16174a2ae208SSatish Balay #undef __FUNCT__ 1618a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 1619d763cef2SBarry Smith /*@C 1620a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 1621d763cef2SBarry Smith timestep to display the iteration's progress. 1622d763cef2SBarry Smith 16233f9fe445SBarry Smith Logically Collective on TS 1624d763cef2SBarry Smith 1625d763cef2SBarry Smith Input Parameters: 1626d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1627d763cef2SBarry Smith . func - monitoring routine 1628329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 1629b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desired) 1630b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1631b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 1632d763cef2SBarry Smith 1633d763cef2SBarry Smith Calling sequence of func: 1634a7cc72afSBarry Smith $ int func(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx) 1635d763cef2SBarry Smith 1636d763cef2SBarry Smith + ts - the TS context 1637d763cef2SBarry Smith . steps - iteration number 16381f06c33eSBarry Smith . time - current time 1639d763cef2SBarry Smith . x - current iterate 1640d763cef2SBarry Smith - mctx - [optional] monitoring context 1641d763cef2SBarry Smith 1642d763cef2SBarry Smith Notes: 1643d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 1644d763cef2SBarry Smith already has been loaded. 1645d763cef2SBarry Smith 1646025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 1647025f1a04SBarry Smith 1648d763cef2SBarry Smith Level: intermediate 1649d763cef2SBarry Smith 1650d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1651d763cef2SBarry Smith 1652a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 1653d763cef2SBarry Smith @*/ 1654c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 1655d763cef2SBarry Smith { 1656d763cef2SBarry Smith PetscFunctionBegin; 16570700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 165817186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1659d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 1660329f5518SBarry Smith ts->mdestroy[ts->numbermonitors] = mdestroy; 1661d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 1662d763cef2SBarry Smith PetscFunctionReturn(0); 1663d763cef2SBarry Smith } 1664d763cef2SBarry Smith 16654a2ae208SSatish Balay #undef __FUNCT__ 1666a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 1667d763cef2SBarry Smith /*@C 1668a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 1669d763cef2SBarry Smith 16703f9fe445SBarry Smith Logically Collective on TS 1671d763cef2SBarry Smith 1672d763cef2SBarry Smith Input Parameters: 1673d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1674d763cef2SBarry Smith 1675d763cef2SBarry Smith Notes: 1676d763cef2SBarry Smith There is no way to remove a single, specific monitor. 1677d763cef2SBarry Smith 1678d763cef2SBarry Smith Level: intermediate 1679d763cef2SBarry Smith 1680d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1681d763cef2SBarry Smith 1682a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 1683d763cef2SBarry Smith @*/ 16847087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 1685d763cef2SBarry Smith { 1686d952e501SBarry Smith PetscErrorCode ierr; 1687d952e501SBarry Smith PetscInt i; 1688d952e501SBarry Smith 1689d763cef2SBarry Smith PetscFunctionBegin; 16900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1691d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 1692d952e501SBarry Smith if (ts->mdestroy[i]) { 16933c4aec1bSBarry Smith ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 1694d952e501SBarry Smith } 1695d952e501SBarry Smith } 1696d763cef2SBarry Smith ts->numbermonitors = 0; 1697d763cef2SBarry Smith PetscFunctionReturn(0); 1698d763cef2SBarry Smith } 1699d763cef2SBarry Smith 17004a2ae208SSatish Balay #undef __FUNCT__ 1701a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 1702d8e5e3e6SSatish Balay /*@ 1703a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 17045516499fSSatish Balay 17055516499fSSatish Balay Level: intermediate 170641251cbbSSatish Balay 17075516499fSSatish Balay .keywords: TS, set, monitor 17085516499fSSatish Balay 170941251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 171041251cbbSSatish Balay @*/ 1711649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 1712d763cef2SBarry Smith { 1713dfbe8321SBarry Smith PetscErrorCode ierr; 1714649052a6SBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm); 1715d132466eSBarry Smith 1716d763cef2SBarry Smith PetscFunctionBegin; 1717649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1718649052a6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %G time %G\n",step,ts->time_step,ptime);CHKERRQ(ierr); 1719649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1720d763cef2SBarry Smith PetscFunctionReturn(0); 1721d763cef2SBarry Smith } 1722d763cef2SBarry Smith 17234a2ae208SSatish Balay #undef __FUNCT__ 17244a2ae208SSatish Balay #define __FUNCT__ "TSStep" 1725d763cef2SBarry Smith /*@ 1726d763cef2SBarry Smith TSStep - Steps the requested number of timesteps. 1727d763cef2SBarry Smith 1728d763cef2SBarry Smith Collective on TS 1729d763cef2SBarry Smith 1730d763cef2SBarry Smith Input Parameter: 1731d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1732d763cef2SBarry Smith 1733d763cef2SBarry Smith Output Parameters: 1734d763cef2SBarry Smith + steps - number of iterations until termination 1735142b95e3SSatish Balay - ptime - time until termination 1736d763cef2SBarry Smith 1737d763cef2SBarry Smith Level: beginner 1738d763cef2SBarry Smith 1739d763cef2SBarry Smith .keywords: TS, timestep, solve 1740d763cef2SBarry Smith 1741d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy() 1742d763cef2SBarry Smith @*/ 17437087cfbeSBarry Smith PetscErrorCode TSStep(TS ts,PetscInt *steps,PetscReal *ptime) 1744d763cef2SBarry Smith { 1745dfbe8321SBarry Smith PetscErrorCode ierr; 1746d763cef2SBarry Smith 1747d763cef2SBarry Smith PetscFunctionBegin; 17480700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1749277b19d0SLisandro Dalcin 1750d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 1751d405a339SMatthew Knepley 1752d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1753000e7ae3SMatthew Knepley ierr = (*ts->ops->step)(ts, steps, ptime);CHKERRQ(ierr); 1754d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1755d405a339SMatthew Knepley 17564bb05414SBarry Smith if (!PetscPreLoadingOn) { 17577adad957SLisandro Dalcin ierr = TSViewFromOptions(ts,((PetscObject)ts)->name);CHKERRQ(ierr); 1758d405a339SMatthew Knepley } 1759d763cef2SBarry Smith PetscFunctionReturn(0); 1760d763cef2SBarry Smith } 1761d763cef2SBarry Smith 17624a2ae208SSatish Balay #undef __FUNCT__ 17636a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 17646a4d4014SLisandro Dalcin /*@ 17656a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 17666a4d4014SLisandro Dalcin 17676a4d4014SLisandro Dalcin Collective on TS 17686a4d4014SLisandro Dalcin 17696a4d4014SLisandro Dalcin Input Parameter: 17706a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 17716a4d4014SLisandro Dalcin - x - the solution vector, or PETSC_NULL if it was set with TSSetSolution() 17726a4d4014SLisandro Dalcin 17736a4d4014SLisandro Dalcin Level: beginner 17746a4d4014SLisandro Dalcin 17756a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 17766a4d4014SLisandro Dalcin 17776a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 17786a4d4014SLisandro Dalcin @*/ 17797087cfbeSBarry Smith PetscErrorCode TSSolve(TS ts, Vec x) 17806a4d4014SLisandro Dalcin { 17816a4d4014SLisandro Dalcin PetscInt steps; 17826a4d4014SLisandro Dalcin PetscReal ptime; 17836a4d4014SLisandro Dalcin PetscErrorCode ierr; 1784f22f69f0SBarry Smith 17856a4d4014SLisandro Dalcin PetscFunctionBegin; 17860700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17876a4d4014SLisandro Dalcin /* set solution vector if provided */ 17886a4d4014SLisandro Dalcin if (x) { ierr = TSSetSolution(ts, x); CHKERRQ(ierr); } 17896a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 17906a4d4014SLisandro Dalcin ts->steps = 0; ts->linear_its = 0; ts->nonlinear_its = 0; 17916a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 17926a4d4014SLisandro Dalcin ierr = TSStep(ts, &steps, &ptime);CHKERRQ(ierr); 17936a4d4014SLisandro Dalcin PetscFunctionReturn(0); 17946a4d4014SLisandro Dalcin } 17956a4d4014SLisandro Dalcin 17966a4d4014SLisandro Dalcin #undef __FUNCT__ 17974a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 1798d763cef2SBarry Smith /* 1799d763cef2SBarry Smith Runs the user provided monitor routines, if they exists. 1800d763cef2SBarry Smith */ 1801a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x) 1802d763cef2SBarry Smith { 18036849ba73SBarry Smith PetscErrorCode ierr; 1804a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 1805d763cef2SBarry Smith 1806d763cef2SBarry Smith PetscFunctionBegin; 1807d763cef2SBarry Smith for (i=0; i<n; i++) { 1808142b95e3SSatish Balay ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr); 1809d763cef2SBarry Smith } 1810d763cef2SBarry Smith PetscFunctionReturn(0); 1811d763cef2SBarry Smith } 1812d763cef2SBarry Smith 1813d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 1814d763cef2SBarry Smith 18154a2ae208SSatish Balay #undef __FUNCT__ 1816a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate" 1817d763cef2SBarry Smith /*@C 1818a6570f20SBarry Smith TSMonitorLGCreate - Creates a line graph context for use with 1819d763cef2SBarry Smith TS to monitor convergence of preconditioned residual norms. 1820d763cef2SBarry Smith 1821d763cef2SBarry Smith Collective on TS 1822d763cef2SBarry Smith 1823d763cef2SBarry Smith Input Parameters: 1824d763cef2SBarry Smith + host - the X display to open, or null for the local machine 1825d763cef2SBarry Smith . label - the title to put in the title bar 18267c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 1827d763cef2SBarry Smith - m, n - the screen width and height in pixels 1828d763cef2SBarry Smith 1829d763cef2SBarry Smith Output Parameter: 1830d763cef2SBarry Smith . draw - the drawing context 1831d763cef2SBarry Smith 1832d763cef2SBarry Smith Options Database Key: 1833a6570f20SBarry Smith . -ts_monitor_draw - automatically sets line graph monitor 1834d763cef2SBarry Smith 1835d763cef2SBarry Smith Notes: 1836a6570f20SBarry Smith Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy(). 1837d763cef2SBarry Smith 1838d763cef2SBarry Smith Level: intermediate 1839d763cef2SBarry Smith 18407c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 1841d763cef2SBarry Smith 1842a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet() 18437c922b88SBarry Smith 1844d763cef2SBarry Smith @*/ 18457087cfbeSBarry Smith PetscErrorCode TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1846d763cef2SBarry Smith { 1847b0a32e0cSBarry Smith PetscDraw win; 1848dfbe8321SBarry Smith PetscErrorCode ierr; 1849d763cef2SBarry Smith 1850d763cef2SBarry Smith PetscFunctionBegin; 1851b0a32e0cSBarry Smith ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr); 1852b0a32e0cSBarry Smith ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr); 1853b0a32e0cSBarry Smith ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr); 1854b0a32e0cSBarry Smith ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr); 1855d763cef2SBarry Smith 185652e6d16bSBarry Smith ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr); 1857d763cef2SBarry Smith PetscFunctionReturn(0); 1858d763cef2SBarry Smith } 1859d763cef2SBarry Smith 18604a2ae208SSatish Balay #undef __FUNCT__ 1861a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG" 1862a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 1863d763cef2SBarry Smith { 1864b0a32e0cSBarry Smith PetscDrawLG lg = (PetscDrawLG) monctx; 186587828ca2SBarry Smith PetscReal x,y = ptime; 1866dfbe8321SBarry Smith PetscErrorCode ierr; 1867d763cef2SBarry Smith 1868d763cef2SBarry Smith PetscFunctionBegin; 18697c922b88SBarry Smith if (!monctx) { 18707c922b88SBarry Smith MPI_Comm comm; 1871b0a32e0cSBarry Smith PetscViewer viewer; 18727c922b88SBarry Smith 18737c922b88SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 1874b0a32e0cSBarry Smith viewer = PETSC_VIEWER_DRAW_(comm); 1875b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr); 18767c922b88SBarry Smith } 18777c922b88SBarry Smith 1878b0a32e0cSBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 187987828ca2SBarry Smith x = (PetscReal)n; 1880b0a32e0cSBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1881d763cef2SBarry Smith if (n < 20 || (n % 5)) { 1882b0a32e0cSBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1883d763cef2SBarry Smith } 1884d763cef2SBarry Smith PetscFunctionReturn(0); 1885d763cef2SBarry Smith } 1886d763cef2SBarry Smith 18874a2ae208SSatish Balay #undef __FUNCT__ 1888a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy" 1889d763cef2SBarry Smith /*@C 1890a6570f20SBarry Smith TSMonitorLGDestroy - Destroys a line graph context that was created 1891a6570f20SBarry Smith with TSMonitorLGCreate(). 1892d763cef2SBarry Smith 1893b0a32e0cSBarry Smith Collective on PetscDrawLG 1894d763cef2SBarry Smith 1895d763cef2SBarry Smith Input Parameter: 1896d763cef2SBarry Smith . draw - the drawing context 1897d763cef2SBarry Smith 1898d763cef2SBarry Smith Level: intermediate 1899d763cef2SBarry Smith 1900d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 1901d763cef2SBarry Smith 1902a6570f20SBarry Smith .seealso: TSMonitorLGCreate(), TSMonitorSet(), TSMonitorLG(); 1903d763cef2SBarry Smith @*/ 19046bf464f9SBarry Smith PetscErrorCode TSMonitorLGDestroy(PetscDrawLG *drawlg) 1905d763cef2SBarry Smith { 1906b0a32e0cSBarry Smith PetscDraw draw; 1907dfbe8321SBarry Smith PetscErrorCode ierr; 1908d763cef2SBarry Smith 1909d763cef2SBarry Smith PetscFunctionBegin; 19106bf464f9SBarry Smith ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr); 19116bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 1912b0a32e0cSBarry Smith ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr); 1913d763cef2SBarry Smith PetscFunctionReturn(0); 1914d763cef2SBarry Smith } 1915d763cef2SBarry Smith 19164a2ae208SSatish Balay #undef __FUNCT__ 19174a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 1918d763cef2SBarry Smith /*@ 1919d763cef2SBarry Smith TSGetTime - Gets the current time. 1920d763cef2SBarry Smith 1921d763cef2SBarry Smith Not Collective 1922d763cef2SBarry Smith 1923d763cef2SBarry Smith Input Parameter: 1924d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1925d763cef2SBarry Smith 1926d763cef2SBarry Smith Output Parameter: 1927d763cef2SBarry Smith . t - the current time 1928d763cef2SBarry Smith 1929d763cef2SBarry Smith Level: beginner 1930d763cef2SBarry Smith 1931d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1932d763cef2SBarry Smith 1933d763cef2SBarry Smith .keywords: TS, get, time 1934d763cef2SBarry Smith @*/ 19357087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal* t) 1936d763cef2SBarry Smith { 1937d763cef2SBarry Smith PetscFunctionBegin; 19380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 19394482741eSBarry Smith PetscValidDoublePointer(t,2); 1940d763cef2SBarry Smith *t = ts->ptime; 1941d763cef2SBarry Smith PetscFunctionReturn(0); 1942d763cef2SBarry Smith } 1943d763cef2SBarry Smith 19444a2ae208SSatish Balay #undef __FUNCT__ 19456a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 19466a4d4014SLisandro Dalcin /*@ 19476a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 19486a4d4014SLisandro Dalcin 19493f9fe445SBarry Smith Logically Collective on TS 19506a4d4014SLisandro Dalcin 19516a4d4014SLisandro Dalcin Input Parameters: 19526a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 19536a4d4014SLisandro Dalcin - time - the time 19546a4d4014SLisandro Dalcin 19556a4d4014SLisandro Dalcin Level: intermediate 19566a4d4014SLisandro Dalcin 19576a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 19586a4d4014SLisandro Dalcin 19596a4d4014SLisandro Dalcin .keywords: TS, set, time 19606a4d4014SLisandro Dalcin @*/ 19617087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 19626a4d4014SLisandro Dalcin { 19636a4d4014SLisandro Dalcin PetscFunctionBegin; 19640700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1965c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 19666a4d4014SLisandro Dalcin ts->ptime = t; 19676a4d4014SLisandro Dalcin PetscFunctionReturn(0); 19686a4d4014SLisandro Dalcin } 19696a4d4014SLisandro Dalcin 19706a4d4014SLisandro Dalcin #undef __FUNCT__ 19714a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 1972d763cef2SBarry Smith /*@C 1973d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 1974d763cef2SBarry Smith TS options in the database. 1975d763cef2SBarry Smith 19763f9fe445SBarry Smith Logically Collective on TS 1977d763cef2SBarry Smith 1978d763cef2SBarry Smith Input Parameter: 1979d763cef2SBarry Smith + ts - The TS context 1980d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 1981d763cef2SBarry Smith 1982d763cef2SBarry Smith Notes: 1983d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 1984d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 1985d763cef2SBarry Smith hyphen. 1986d763cef2SBarry Smith 1987d763cef2SBarry Smith Level: advanced 1988d763cef2SBarry Smith 1989d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 1990d763cef2SBarry Smith 1991d763cef2SBarry Smith .seealso: TSSetFromOptions() 1992d763cef2SBarry Smith 1993d763cef2SBarry Smith @*/ 19947087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 1995d763cef2SBarry Smith { 1996dfbe8321SBarry Smith PetscErrorCode ierr; 1997*089b2837SJed Brown SNES snes; 1998d763cef2SBarry Smith 1999d763cef2SBarry Smith PetscFunctionBegin; 20000700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2001d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2002*089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2003*089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2004d763cef2SBarry Smith PetscFunctionReturn(0); 2005d763cef2SBarry Smith } 2006d763cef2SBarry Smith 2007d763cef2SBarry Smith 20084a2ae208SSatish Balay #undef __FUNCT__ 20094a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 2010d763cef2SBarry Smith /*@C 2011d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 2012d763cef2SBarry Smith TS options in the database. 2013d763cef2SBarry Smith 20143f9fe445SBarry Smith Logically Collective on TS 2015d763cef2SBarry Smith 2016d763cef2SBarry Smith Input Parameter: 2017d763cef2SBarry Smith + ts - The TS context 2018d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2019d763cef2SBarry Smith 2020d763cef2SBarry Smith Notes: 2021d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2022d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2023d763cef2SBarry Smith hyphen. 2024d763cef2SBarry Smith 2025d763cef2SBarry Smith Level: advanced 2026d763cef2SBarry Smith 2027d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 2028d763cef2SBarry Smith 2029d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 2030d763cef2SBarry Smith 2031d763cef2SBarry Smith @*/ 20327087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 2033d763cef2SBarry Smith { 2034dfbe8321SBarry Smith PetscErrorCode ierr; 2035*089b2837SJed Brown SNES snes; 2036d763cef2SBarry Smith 2037d763cef2SBarry Smith PetscFunctionBegin; 20380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2039d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2040*089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2041*089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2042d763cef2SBarry Smith PetscFunctionReturn(0); 2043d763cef2SBarry Smith } 2044d763cef2SBarry Smith 20454a2ae208SSatish Balay #undef __FUNCT__ 20464a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2047d763cef2SBarry Smith /*@C 2048d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2049d763cef2SBarry Smith TS options in the database. 2050d763cef2SBarry Smith 2051d763cef2SBarry Smith Not Collective 2052d763cef2SBarry Smith 2053d763cef2SBarry Smith Input Parameter: 2054d763cef2SBarry Smith . ts - The TS context 2055d763cef2SBarry Smith 2056d763cef2SBarry Smith Output Parameter: 2057d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2058d763cef2SBarry Smith 2059d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2060d763cef2SBarry Smith sufficient length to hold the prefix. 2061d763cef2SBarry Smith 2062d763cef2SBarry Smith Level: intermediate 2063d763cef2SBarry Smith 2064d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2065d763cef2SBarry Smith 2066d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2067d763cef2SBarry Smith @*/ 20687087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2069d763cef2SBarry Smith { 2070dfbe8321SBarry Smith PetscErrorCode ierr; 2071d763cef2SBarry Smith 2072d763cef2SBarry Smith PetscFunctionBegin; 20730700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20744482741eSBarry Smith PetscValidPointer(prefix,2); 2075d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2076d763cef2SBarry Smith PetscFunctionReturn(0); 2077d763cef2SBarry Smith } 2078d763cef2SBarry Smith 20794a2ae208SSatish Balay #undef __FUNCT__ 20804a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2081d763cef2SBarry Smith /*@C 2082d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2083d763cef2SBarry Smith 2084d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2085d763cef2SBarry Smith 2086d763cef2SBarry Smith Input Parameter: 2087d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2088d763cef2SBarry Smith 2089d763cef2SBarry Smith Output Parameters: 2090d763cef2SBarry Smith + J - The Jacobian J of F, where U_t = F(U,t) 2091d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2092*089b2837SJed Brown . func - Function to compute the Jacobian of the RHS 2093d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2094d763cef2SBarry Smith 2095d763cef2SBarry Smith Notes: You can pass in PETSC_NULL for any return argument you do not need. 2096d763cef2SBarry Smith 2097d763cef2SBarry Smith Level: intermediate 2098d763cef2SBarry Smith 209926d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2100d763cef2SBarry Smith 2101d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2102d763cef2SBarry Smith @*/ 2103*089b2837SJed Brown PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2104d763cef2SBarry Smith { 2105*089b2837SJed Brown PetscErrorCode ierr; 2106*089b2837SJed Brown SNES snes; 2107*089b2837SJed Brown 2108d763cef2SBarry Smith PetscFunctionBegin; 2109*089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2110*089b2837SJed Brown ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 2111*089b2837SJed Brown if (func) *func = ts->ops->rhsjacobian; 211226d46c62SHong Zhang if (ctx) *ctx = ts->jacP; 2113d763cef2SBarry Smith PetscFunctionReturn(0); 2114d763cef2SBarry Smith } 2115d763cef2SBarry Smith 21161713a123SBarry Smith #undef __FUNCT__ 21172eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 21182eca1d9cSJed Brown /*@C 21192eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 21202eca1d9cSJed Brown 21212eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 21222eca1d9cSJed Brown 21232eca1d9cSJed Brown Input Parameter: 21242eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 21252eca1d9cSJed Brown 21262eca1d9cSJed Brown Output Parameters: 21272eca1d9cSJed Brown + A - The Jacobian of F(t,U,U_t) 21282eca1d9cSJed Brown . B - The preconditioner matrix, often the same as A 21292eca1d9cSJed Brown . f - The function to compute the matrices 21302eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 21312eca1d9cSJed Brown 21322eca1d9cSJed Brown Notes: You can pass in PETSC_NULL for any return argument you do not need. 21332eca1d9cSJed Brown 21342eca1d9cSJed Brown Level: advanced 21352eca1d9cSJed Brown 21362eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 21372eca1d9cSJed Brown 21382eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 21392eca1d9cSJed Brown @*/ 21407087cfbeSBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 21412eca1d9cSJed Brown { 2142*089b2837SJed Brown PetscErrorCode ierr; 2143*089b2837SJed Brown SNES snes; 2144*089b2837SJed Brown 21452eca1d9cSJed Brown PetscFunctionBegin; 2146*089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2147*089b2837SJed Brown ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 21482eca1d9cSJed Brown if (f) *f = ts->ops->ijacobian; 21492eca1d9cSJed Brown if (ctx) *ctx = ts->jacP; 21502eca1d9cSJed Brown PetscFunctionReturn(0); 21512eca1d9cSJed Brown } 21522eca1d9cSJed Brown 21532eca1d9cSJed Brown #undef __FUNCT__ 2154a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution" 21551713a123SBarry Smith /*@C 2156a6570f20SBarry Smith TSMonitorSolution - Monitors progress of the TS solvers by calling 21571713a123SBarry Smith VecView() for the solution at each timestep 21581713a123SBarry Smith 21591713a123SBarry Smith Collective on TS 21601713a123SBarry Smith 21611713a123SBarry Smith Input Parameters: 21621713a123SBarry Smith + ts - the TS context 21631713a123SBarry Smith . step - current time-step 2164142b95e3SSatish Balay . ptime - current time 21651713a123SBarry Smith - dummy - either a viewer or PETSC_NULL 21661713a123SBarry Smith 21671713a123SBarry Smith Level: intermediate 21681713a123SBarry Smith 21691713a123SBarry Smith .keywords: TS, vector, monitor, view 21701713a123SBarry Smith 2171a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 21721713a123SBarry Smith @*/ 21737087cfbeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 21741713a123SBarry Smith { 2175dfbe8321SBarry Smith PetscErrorCode ierr; 21761713a123SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 21771713a123SBarry Smith 21781713a123SBarry Smith PetscFunctionBegin; 2179a34d58ebSBarry Smith if (!dummy) { 21807adad957SLisandro Dalcin viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm); 21811713a123SBarry Smith } 21821713a123SBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 21831713a123SBarry Smith PetscFunctionReturn(0); 21841713a123SBarry Smith } 21851713a123SBarry Smith 21861713a123SBarry Smith 21876c699258SBarry Smith #undef __FUNCT__ 21886c699258SBarry Smith #define __FUNCT__ "TSSetDM" 21896c699258SBarry Smith /*@ 21906c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 21916c699258SBarry Smith 21923f9fe445SBarry Smith Logically Collective on TS and DM 21936c699258SBarry Smith 21946c699258SBarry Smith Input Parameters: 21956c699258SBarry Smith + ts - the preconditioner context 21966c699258SBarry Smith - dm - the dm 21976c699258SBarry Smith 21986c699258SBarry Smith Level: intermediate 21996c699258SBarry Smith 22006c699258SBarry Smith 22016c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 22026c699258SBarry Smith @*/ 22037087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 22046c699258SBarry Smith { 22056c699258SBarry Smith PetscErrorCode ierr; 2206*089b2837SJed Brown SNES snes; 22076c699258SBarry Smith 22086c699258SBarry Smith PetscFunctionBegin; 22090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 221070663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 22116bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 22126c699258SBarry Smith ts->dm = dm; 2213*089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2214*089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 22156c699258SBarry Smith PetscFunctionReturn(0); 22166c699258SBarry Smith } 22176c699258SBarry Smith 22186c699258SBarry Smith #undef __FUNCT__ 22196c699258SBarry Smith #define __FUNCT__ "TSGetDM" 22206c699258SBarry Smith /*@ 22216c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 22226c699258SBarry Smith 22233f9fe445SBarry Smith Not Collective 22246c699258SBarry Smith 22256c699258SBarry Smith Input Parameter: 22266c699258SBarry Smith . ts - the preconditioner context 22276c699258SBarry Smith 22286c699258SBarry Smith Output Parameter: 22296c699258SBarry Smith . dm - the dm 22306c699258SBarry Smith 22316c699258SBarry Smith Level: intermediate 22326c699258SBarry Smith 22336c699258SBarry Smith 22346c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 22356c699258SBarry Smith @*/ 22367087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 22376c699258SBarry Smith { 22386c699258SBarry Smith PetscFunctionBegin; 22390700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22406c699258SBarry Smith *dm = ts->dm; 22416c699258SBarry Smith PetscFunctionReturn(0); 22426c699258SBarry Smith } 22431713a123SBarry Smith 22440f5c6efeSJed Brown #undef __FUNCT__ 22450f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 22460f5c6efeSJed Brown /*@ 22470f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 22480f5c6efeSJed Brown 22493f9fe445SBarry Smith Logically Collective on SNES 22500f5c6efeSJed Brown 22510f5c6efeSJed Brown Input Parameter: 2252d42a1c89SJed Brown + snes - nonlinear solver 22530f5c6efeSJed Brown . X - the current state at which to evaluate the residual 2254d42a1c89SJed Brown - ctx - user context, must be a TS 22550f5c6efeSJed Brown 22560f5c6efeSJed Brown Output Parameter: 22570f5c6efeSJed Brown . F - the nonlinear residual 22580f5c6efeSJed Brown 22590f5c6efeSJed Brown Notes: 22600f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 22610f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 22620f5c6efeSJed Brown 22630f5c6efeSJed Brown Level: advanced 22640f5c6efeSJed Brown 22650f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 22660f5c6efeSJed Brown @*/ 22677087cfbeSBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx) 22680f5c6efeSJed Brown { 22690f5c6efeSJed Brown TS ts = (TS)ctx; 22700f5c6efeSJed Brown PetscErrorCode ierr; 22710f5c6efeSJed Brown 22720f5c6efeSJed Brown PetscFunctionBegin; 22730f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 22740f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 22750f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 22760f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 22770f5c6efeSJed Brown ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr); 22780f5c6efeSJed Brown PetscFunctionReturn(0); 22790f5c6efeSJed Brown } 22800f5c6efeSJed Brown 22810f5c6efeSJed Brown #undef __FUNCT__ 22820f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 22830f5c6efeSJed Brown /*@ 22840f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 22850f5c6efeSJed Brown 22860f5c6efeSJed Brown Collective on SNES 22870f5c6efeSJed Brown 22880f5c6efeSJed Brown Input Parameter: 22890f5c6efeSJed Brown + snes - nonlinear solver 22900f5c6efeSJed Brown . X - the current state at which to evaluate the residual 22910f5c6efeSJed Brown - ctx - user context, must be a TS 22920f5c6efeSJed Brown 22930f5c6efeSJed Brown Output Parameter: 22940f5c6efeSJed Brown + A - the Jacobian 22950f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 22960f5c6efeSJed Brown - flag - indicates any structure change in the matrix 22970f5c6efeSJed Brown 22980f5c6efeSJed Brown Notes: 22990f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 23000f5c6efeSJed Brown 23010f5c6efeSJed Brown Level: developer 23020f5c6efeSJed Brown 23030f5c6efeSJed Brown .seealso: SNESSetJacobian() 23040f5c6efeSJed Brown @*/ 23057087cfbeSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx) 23060f5c6efeSJed Brown { 23070f5c6efeSJed Brown TS ts = (TS)ctx; 23080f5c6efeSJed Brown PetscErrorCode ierr; 23090f5c6efeSJed Brown 23100f5c6efeSJed Brown PetscFunctionBegin; 23110f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 23120f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 23130f5c6efeSJed Brown PetscValidPointer(A,3); 23140f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 23150f5c6efeSJed Brown PetscValidPointer(B,4); 23160f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 23170f5c6efeSJed Brown PetscValidPointer(flag,5); 23180f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 23190f5c6efeSJed Brown ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr); 23200f5c6efeSJed Brown PetscFunctionReturn(0); 23210f5c6efeSJed Brown } 2322325fc9f4SBarry Smith 2323325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 2324c6db04a5SJed Brown #include <mex.h> 2325325fc9f4SBarry Smith 2326325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 2327325fc9f4SBarry Smith 2328325fc9f4SBarry Smith #undef __FUNCT__ 2329325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 2330325fc9f4SBarry Smith /* 2331325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 2332325fc9f4SBarry Smith TSSetFunctionMatlab(). 2333325fc9f4SBarry Smith 2334325fc9f4SBarry Smith Collective on TS 2335325fc9f4SBarry Smith 2336325fc9f4SBarry Smith Input Parameters: 2337325fc9f4SBarry Smith + snes - the TS context 2338325fc9f4SBarry Smith - x - input vector 2339325fc9f4SBarry Smith 2340325fc9f4SBarry Smith Output Parameter: 2341325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 2342325fc9f4SBarry Smith 2343325fc9f4SBarry Smith Notes: 2344325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 2345325fc9f4SBarry Smith implementations, so most users would not generally call this routine 2346325fc9f4SBarry Smith themselves. 2347325fc9f4SBarry Smith 2348325fc9f4SBarry Smith Level: developer 2349325fc9f4SBarry Smith 2350325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2351325fc9f4SBarry Smith 2352325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2353325fc9f4SBarry Smith */ 23547087cfbeSBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx) 2355325fc9f4SBarry Smith { 2356325fc9f4SBarry Smith PetscErrorCode ierr; 2357325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2358325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 2359325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 2360325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 2361325fc9f4SBarry Smith 2362325fc9f4SBarry Smith PetscFunctionBegin; 2363325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2364325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2365325fc9f4SBarry Smith PetscValidHeaderSpecific(xdot,VEC_CLASSID,4); 2366325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 2367325fc9f4SBarry Smith PetscCheckSameComm(snes,1,x,3); 2368325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 2369325fc9f4SBarry Smith 2370325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2371325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2372880f3077SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr); 2373325fc9f4SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 2374325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2375325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 2376325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2377325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2378325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 2379325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 2380325fc9f4SBarry Smith prhs[6] = sctx->ctx; 2381325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 2382325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2383325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2384325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2385325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2386325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2387325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2388325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2389325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2390325fc9f4SBarry Smith PetscFunctionReturn(0); 2391325fc9f4SBarry Smith } 2392325fc9f4SBarry Smith 2393325fc9f4SBarry Smith 2394325fc9f4SBarry Smith #undef __FUNCT__ 2395325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 2396325fc9f4SBarry Smith /* 2397325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 2398325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 2399e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 2400325fc9f4SBarry Smith 2401325fc9f4SBarry Smith Logically Collective on TS 2402325fc9f4SBarry Smith 2403325fc9f4SBarry Smith Input Parameters: 2404325fc9f4SBarry Smith + ts - the TS context 2405325fc9f4SBarry Smith - func - function evaluation routine 2406325fc9f4SBarry Smith 2407325fc9f4SBarry Smith Calling sequence of func: 2408325fc9f4SBarry Smith $ func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx); 2409325fc9f4SBarry Smith 2410325fc9f4SBarry Smith Level: beginner 2411325fc9f4SBarry Smith 2412325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2413325fc9f4SBarry Smith 2414325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2415325fc9f4SBarry Smith */ 24167087cfbeSBarry Smith PetscErrorCode TSSetFunctionMatlab(TS snes,const char *func,mxArray *ctx) 2417325fc9f4SBarry Smith { 2418325fc9f4SBarry Smith PetscErrorCode ierr; 2419325fc9f4SBarry Smith TSMatlabContext *sctx; 2420325fc9f4SBarry Smith 2421325fc9f4SBarry Smith PetscFunctionBegin; 2422325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2423325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2424325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2425325fc9f4SBarry Smith /* 2426325fc9f4SBarry Smith This should work, but it doesn't 2427325fc9f4SBarry Smith sctx->ctx = ctx; 2428325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2429325fc9f4SBarry Smith */ 2430325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2431325fc9f4SBarry Smith ierr = TSSetIFunction(snes,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 2432325fc9f4SBarry Smith PetscFunctionReturn(0); 2433325fc9f4SBarry Smith } 2434325fc9f4SBarry Smith 2435325fc9f4SBarry Smith #undef __FUNCT__ 2436325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 2437325fc9f4SBarry Smith /* 2438325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 2439325fc9f4SBarry Smith TSSetJacobianMatlab(). 2440325fc9f4SBarry Smith 2441325fc9f4SBarry Smith Collective on TS 2442325fc9f4SBarry Smith 2443325fc9f4SBarry Smith Input Parameters: 2444325fc9f4SBarry Smith + snes - the TS context 2445325fc9f4SBarry Smith . x - input vector 2446325fc9f4SBarry Smith . A, B - the matrices 2447325fc9f4SBarry Smith - ctx - user context 2448325fc9f4SBarry Smith 2449325fc9f4SBarry Smith Output Parameter: 2450325fc9f4SBarry Smith . flag - structure of the matrix 2451325fc9f4SBarry Smith 2452325fc9f4SBarry Smith Level: developer 2453325fc9f4SBarry Smith 2454325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2455325fc9f4SBarry Smith 2456325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2457325fc9f4SBarry Smith @*/ 24587087cfbeSBarry Smith PetscErrorCode TSComputeJacobian_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 2459325fc9f4SBarry Smith { 2460325fc9f4SBarry Smith PetscErrorCode ierr; 2461325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2462325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 2463325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 2464325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 2465325fc9f4SBarry Smith 2466325fc9f4SBarry Smith PetscFunctionBegin; 2467325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2468325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2469325fc9f4SBarry Smith 2470325fc9f4SBarry Smith /* call Matlab function in ctx with arguments x and y */ 2471325fc9f4SBarry Smith 2472325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2473325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2474325fc9f4SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr); 2475325fc9f4SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 2476325fc9f4SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 2477325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2478325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 2479325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2480325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2481325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 2482325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 2483325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 2484325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 2485325fc9f4SBarry Smith prhs[8] = sctx->ctx; 2486325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 2487325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2488325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 2489325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2490325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2491325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2492325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2493325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2494325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2495325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 2496325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 2497325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2498325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 2499325fc9f4SBarry Smith PetscFunctionReturn(0); 2500325fc9f4SBarry Smith } 2501325fc9f4SBarry Smith 2502325fc9f4SBarry Smith 2503325fc9f4SBarry Smith #undef __FUNCT__ 2504325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 2505325fc9f4SBarry Smith /* 2506325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 2507e3c5b3baSBarry 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 2508325fc9f4SBarry Smith 2509325fc9f4SBarry Smith Logically Collective on TS 2510325fc9f4SBarry Smith 2511325fc9f4SBarry Smith Input Parameters: 2512325fc9f4SBarry Smith + snes - the TS context 2513325fc9f4SBarry Smith . A,B - Jacobian matrices 2514325fc9f4SBarry Smith . func - function evaluation routine 2515325fc9f4SBarry Smith - ctx - user context 2516325fc9f4SBarry Smith 2517325fc9f4SBarry Smith Calling sequence of func: 2518325fc9f4SBarry Smith $ flag = func (TS snes,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx); 2519325fc9f4SBarry Smith 2520325fc9f4SBarry Smith 2521325fc9f4SBarry Smith Level: developer 2522325fc9f4SBarry Smith 2523325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2524325fc9f4SBarry Smith 2525325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2526325fc9f4SBarry Smith */ 25277087cfbeSBarry Smith PetscErrorCode TSSetJacobianMatlab(TS snes,Mat A,Mat B,const char *func,mxArray *ctx) 2528325fc9f4SBarry Smith { 2529325fc9f4SBarry Smith PetscErrorCode ierr; 2530325fc9f4SBarry Smith TSMatlabContext *sctx; 2531325fc9f4SBarry Smith 2532325fc9f4SBarry Smith PetscFunctionBegin; 2533325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2534325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2535325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2536325fc9f4SBarry Smith /* 2537325fc9f4SBarry Smith This should work, but it doesn't 2538325fc9f4SBarry Smith sctx->ctx = ctx; 2539325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2540325fc9f4SBarry Smith */ 2541325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2542325fc9f4SBarry Smith ierr = TSSetIJacobian(snes,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 2543325fc9f4SBarry Smith PetscFunctionReturn(0); 2544325fc9f4SBarry Smith } 2545325fc9f4SBarry Smith 2546b5b1a830SBarry Smith #undef __FUNCT__ 2547b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 2548b5b1a830SBarry Smith /* 2549b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 2550b5b1a830SBarry Smith 2551b5b1a830SBarry Smith Collective on TS 2552b5b1a830SBarry Smith 2553b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2554b5b1a830SBarry Smith @*/ 25557087cfbeSBarry Smith PetscErrorCode TSMonitor_Matlab(TS snes,PetscInt it, PetscReal time,Vec x, void *ctx) 2556b5b1a830SBarry Smith { 2557b5b1a830SBarry Smith PetscErrorCode ierr; 2558b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2559a530c242SBarry Smith int nlhs = 1,nrhs = 6; 2560b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 2561b5b1a830SBarry Smith long long int lx = 0,ls = 0; 2562b5b1a830SBarry Smith 2563b5b1a830SBarry Smith PetscFunctionBegin; 2564b5b1a830SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2565b5b1a830SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,4); 2566b5b1a830SBarry Smith 2567b5b1a830SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2568b5b1a830SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2569b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2570b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 2571b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 2572b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 2573b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 2574b5b1a830SBarry Smith prhs[5] = sctx->ctx; 2575b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 2576b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2577b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 2578b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 2579b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 2580b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 2581b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 2582b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 2583b5b1a830SBarry Smith PetscFunctionReturn(0); 2584b5b1a830SBarry Smith } 2585b5b1a830SBarry Smith 2586b5b1a830SBarry Smith 2587b5b1a830SBarry Smith #undef __FUNCT__ 2588b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 2589b5b1a830SBarry Smith /* 2590b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 2591b5b1a830SBarry Smith 2592b5b1a830SBarry Smith Level: developer 2593b5b1a830SBarry Smith 2594b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 2595b5b1a830SBarry Smith 2596b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2597b5b1a830SBarry Smith */ 25987087cfbeSBarry Smith PetscErrorCode TSMonitorSetMatlab(TS snes,const char *func,mxArray *ctx) 2599b5b1a830SBarry Smith { 2600b5b1a830SBarry Smith PetscErrorCode ierr; 2601b5b1a830SBarry Smith TSMatlabContext *sctx; 2602b5b1a830SBarry Smith 2603b5b1a830SBarry Smith PetscFunctionBegin; 2604b5b1a830SBarry Smith /* currently sctx is memory bleed */ 2605b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2606b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2607b5b1a830SBarry Smith /* 2608b5b1a830SBarry Smith This should work, but it doesn't 2609b5b1a830SBarry Smith sctx->ctx = ctx; 2610b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2611b5b1a830SBarry Smith */ 2612b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2613b5b1a830SBarry Smith ierr = TSMonitorSet(snes,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 2614b5b1a830SBarry Smith PetscFunctionReturn(0); 2615b5b1a830SBarry Smith } 2616b5b1a830SBarry Smith 2617325fc9f4SBarry Smith #endif 2618