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]; 78089b2837SJed 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 122089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 123bdad233fSMatthew Knepley /* Handle subobject options */ 124089b2837SJed Brown if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 125089b2837SJed 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 } 237089b2837SJed 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*b2cd27e8SJed Brown } else if (ts->problem_type == TS_LINEAR) { 285089b2837SJed Brown Mat A,B; 286089b2837SJed Brown ierr = TSGetMatrices(ts,&A,&B,PETSC_NULL, PETSC_NULL,PETSC_NULL,PETSC_NULL, PETSC_NULL);CHKERRQ(ierr); 287*b2cd27e8SJed Brown if (ts->ops->rhsmatrix) { 288089b2837SJed Brown ts->rhsmatstructure = DIFFERENT_NONZERO_PATTERN; 289d763cef2SBarry Smith PetscStackPush("TS user right-hand-side matrix function"); 290089b2837SJed Brown ierr = (*ts->ops->rhsmatrix)(ts,t,&A,&B,&ts->rhsmatstructure,ts->jacP);CHKERRQ(ierr); 291d763cef2SBarry Smith PetscStackPop; 292089b2837SJed Brown /* call TSSetMatrices() in case the user changed the pointers */ 293089b2837SJed Brown ierr = TSSetMatrices(ts,A,B,PETSC_NULL, PETSC_NULL,PETSC_NULL,PETSC_NULL, PETSC_NULL,ts->jacP);CHKERRQ(ierr); 294*b2cd27e8SJed Brown } 295089b2837SJed Brown ierr = MatMult(A,x,y);CHKERRQ(ierr); 296d16dab79SJed Brown } else SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"No RHS provided, must call TSSetRHSFunction() or TSSetMatrices()"); 297d763cef2SBarry Smith 298d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr); 299d763cef2SBarry Smith 300d763cef2SBarry Smith PetscFunctionReturn(0); 301d763cef2SBarry Smith } 302d763cef2SBarry Smith 3034a2ae208SSatish Balay #undef __FUNCT__ 304316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 305316643e7SJed Brown /*@ 306316643e7SJed Brown TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0 307316643e7SJed Brown 308316643e7SJed Brown Collective on TS and Vec 309316643e7SJed Brown 310316643e7SJed Brown Input Parameters: 311316643e7SJed Brown + ts - the TS context 312316643e7SJed Brown . t - current time 313316643e7SJed Brown . X - state vector 314316643e7SJed Brown - Xdot - time derivative of state vector 315316643e7SJed Brown 316316643e7SJed Brown Output Parameter: 317316643e7SJed Brown . Y - right hand side 318316643e7SJed Brown 319316643e7SJed Brown Note: 320316643e7SJed Brown Most users should not need to explicitly call this routine, as it 321316643e7SJed Brown is used internally within the nonlinear solvers. 322316643e7SJed Brown 323316643e7SJed Brown If the user did did not write their equations in implicit form, this 324316643e7SJed Brown function recasts them in implicit form. 325316643e7SJed Brown 326316643e7SJed Brown Level: developer 327316643e7SJed Brown 328316643e7SJed Brown .keywords: TS, compute 329316643e7SJed Brown 330316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 331316643e7SJed Brown @*/ 332316643e7SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y) 333316643e7SJed Brown { 334316643e7SJed Brown PetscErrorCode ierr; 335316643e7SJed Brown 336316643e7SJed Brown PetscFunctionBegin; 3370700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3380700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 3390700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 3400700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 341316643e7SJed Brown 342316643e7SJed Brown ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 343316643e7SJed Brown if (ts->ops->ifunction) { 344316643e7SJed Brown PetscStackPush("TS user implicit function"); 345316643e7SJed Brown ierr = (*ts->ops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr); 346316643e7SJed Brown PetscStackPop; 347316643e7SJed Brown } else { 348089b2837SJed Brown ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr); 3494a6899ffSJed Brown /* Convert to implicit form: F(X,Xdot) = Alhs * Xdot - Frhs(X) */ 3504a6899ffSJed Brown if (ts->ops->lhsmatrix) { 351089b2837SJed Brown ts->lhsmatstructure = DIFFERENT_NONZERO_PATTERN; 3524a6899ffSJed Brown PetscStackPush("TS user left-hand-side matrix function"); 353089b2837SJed Brown ierr = (*ts->ops->lhsmatrix)(ts,t,&ts->Alhs,&ts->Blhs,&ts->lhsmatstructure,ts->jacP);CHKERRQ(ierr); 3544a6899ffSJed Brown PetscStackPop; 3554a6899ffSJed Brown ierr = VecScale(Y,-1.);CHKERRQ(ierr); 3564a6899ffSJed Brown ierr = MatMultAdd(ts->Alhs,Xdot,Y,Y);CHKERRQ(ierr); 3574a6899ffSJed Brown } else { 358ace68cafSJed Brown ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr); 359316643e7SJed Brown } 3604a6899ffSJed Brown } 361316643e7SJed Brown ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr); 362316643e7SJed Brown PetscFunctionReturn(0); 363316643e7SJed Brown } 364316643e7SJed Brown 365316643e7SJed Brown #undef __FUNCT__ 366316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 367316643e7SJed Brown /*@ 368316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 369316643e7SJed Brown 370316643e7SJed Brown Collective on TS and Vec 371316643e7SJed Brown 372316643e7SJed Brown Input 373316643e7SJed Brown Input Parameters: 374316643e7SJed Brown + ts - the TS context 375316643e7SJed Brown . t - current timestep 376316643e7SJed Brown . X - state vector 377316643e7SJed Brown . Xdot - time derivative of state vector 378316643e7SJed Brown - shift - shift to apply, see note below 379316643e7SJed Brown 380316643e7SJed Brown Output Parameters: 381316643e7SJed Brown + A - Jacobian matrix 382316643e7SJed Brown . B - optional preconditioning matrix 383316643e7SJed Brown - flag - flag indicating matrix structure 384316643e7SJed Brown 385316643e7SJed Brown Notes: 386316643e7SJed Brown If F(t,X,Xdot)=0 is the DAE, the required Jacobian is 387316643e7SJed Brown 388316643e7SJed Brown dF/dX + shift*dF/dXdot 389316643e7SJed Brown 390316643e7SJed Brown Most users should not need to explicitly call this routine, as it 391316643e7SJed Brown is used internally within the nonlinear solvers. 392316643e7SJed Brown 393316643e7SJed Brown TSComputeIJacobian() is valid only for TS_NONLINEAR 394316643e7SJed Brown 395316643e7SJed Brown Level: developer 396316643e7SJed Brown 397316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 398316643e7SJed Brown 399316643e7SJed Brown .seealso: TSSetIJacobian() 40063495f91SJed Brown @*/ 4017087cfbeSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg) 402316643e7SJed Brown { 403316643e7SJed Brown PetscErrorCode ierr; 404316643e7SJed Brown 405316643e7SJed Brown PetscFunctionBegin; 4060700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4070700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,3); 4080700a824SBarry Smith PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4); 409316643e7SJed Brown PetscValidPointer(A,6); 4100700a824SBarry Smith PetscValidHeaderSpecific(*A,MAT_CLASSID,6); 411316643e7SJed Brown PetscValidPointer(B,7); 4120700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,7); 413316643e7SJed Brown PetscValidPointer(flg,8); 414316643e7SJed Brown 4154a6899ffSJed Brown *flg = SAME_NONZERO_PATTERN; /* In case it we're solving a linear problem in which case it wouldn't get initialized below. */ 416316643e7SJed Brown ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 417316643e7SJed Brown if (ts->ops->ijacobian) { 418316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 419316643e7SJed Brown ierr = (*ts->ops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr); 420316643e7SJed Brown PetscStackPop; 421316643e7SJed Brown } else { 422316643e7SJed Brown if (ts->ops->rhsjacobian) { 423316643e7SJed Brown PetscStackPush("TS user right-hand-side Jacobian"); 424316643e7SJed Brown ierr = (*ts->ops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr); 425316643e7SJed Brown PetscStackPop; 426316643e7SJed Brown } else { 427316643e7SJed Brown ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 428316643e7SJed Brown ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 429316643e7SJed Brown if (*A != *B) { 430316643e7SJed Brown ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 431316643e7SJed Brown ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 432316643e7SJed Brown } 433316643e7SJed Brown } 434316643e7SJed Brown 435316643e7SJed Brown /* Convert to implicit form */ 436316643e7SJed Brown /* inefficient because these operations will normally traverse all matrix elements twice */ 437316643e7SJed Brown ierr = MatScale(*A,-1);CHKERRQ(ierr); 4384a6899ffSJed Brown if (ts->Alhs) { 4394a6899ffSJed Brown ierr = MatAXPY(*A,shift,ts->Alhs,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); 4404a6899ffSJed Brown } else { 441316643e7SJed Brown ierr = MatShift(*A,shift);CHKERRQ(ierr); 4424a6899ffSJed Brown } 443316643e7SJed Brown if (*A != *B) { 444316643e7SJed Brown ierr = MatScale(*B,-1);CHKERRQ(ierr); 445316643e7SJed Brown ierr = MatShift(*B,shift);CHKERRQ(ierr); 446316643e7SJed Brown } 447316643e7SJed Brown } 448316643e7SJed Brown ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr); 449316643e7SJed Brown PetscFunctionReturn(0); 450316643e7SJed Brown } 451316643e7SJed Brown 452316643e7SJed Brown #undef __FUNCT__ 4534a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 454d763cef2SBarry Smith /*@C 455d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 456d763cef2SBarry Smith F(t,u), where U_t = F(t,u). 457d763cef2SBarry Smith 4583f9fe445SBarry Smith Logically Collective on TS 459d763cef2SBarry Smith 460d763cef2SBarry Smith Input Parameters: 461d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 462ca94891dSJed Brown . r - vector to put the computed right hand side (or PETSC_NULL to have it created) 463d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 464d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 465d763cef2SBarry Smith function evaluation routine (may be PETSC_NULL) 466d763cef2SBarry Smith 467d763cef2SBarry Smith Calling sequence of func: 46887828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 469d763cef2SBarry Smith 470d763cef2SBarry Smith + t - current timestep 471d763cef2SBarry Smith . u - input vector 472d763cef2SBarry Smith . F - function vector 473d763cef2SBarry Smith - ctx - [optional] user-defined function context 474d763cef2SBarry Smith 475d763cef2SBarry Smith Important: 47676f2fa84SHong Zhang The user MUST call either this routine or TSSetMatrices(). 477d763cef2SBarry Smith 478d763cef2SBarry Smith Level: beginner 479d763cef2SBarry Smith 480d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 481d763cef2SBarry Smith 48276f2fa84SHong Zhang .seealso: TSSetMatrices() 483d763cef2SBarry Smith @*/ 484089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 485d763cef2SBarry Smith { 486089b2837SJed Brown PetscErrorCode ierr; 487089b2837SJed Brown SNES snes; 488d763cef2SBarry Smith 489089b2837SJed Brown PetscFunctionBegin; 4900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 491ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 49217186662SBarry Smith if (ts->problem_type == TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot set function for linear problem"); 493000e7ae3SMatthew Knepley ts->ops->rhsfunction = f; 494d763cef2SBarry Smith ts->funP = ctx; 495089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 496089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 497d763cef2SBarry Smith PetscFunctionReturn(0); 498d763cef2SBarry Smith } 499d763cef2SBarry Smith 5004a2ae208SSatish Balay #undef __FUNCT__ 50195f0b562SHong Zhang #define __FUNCT__ "TSSetMatrices" 50295f0b562SHong Zhang /*@C 50395f0b562SHong Zhang TSSetMatrices - Sets the functions to compute the matrices Alhs and Arhs, 50495f0b562SHong Zhang where Alhs(t) U_t = Arhs(t) U. 50595f0b562SHong Zhang 5063f9fe445SBarry Smith Logically Collective on TS 50795f0b562SHong Zhang 50895f0b562SHong Zhang Input Parameters: 50995f0b562SHong Zhang + ts - the TS context obtained from TSCreate() 51095f0b562SHong Zhang . Arhs - matrix 511089b2837SJed Brown . Brhs - preconditioning matrix 512089b2837SJed Brown . frhs - the matrix evaluation routine for Arhs and Brhs; use PETSC_NULL (PETSC_NULL_FUNCTION in fortran) 51395f0b562SHong Zhang if Arhs is not a function of t. 51495f0b562SHong Zhang . Alhs - matrix or PETSC_NULL if Alhs is an indentity matrix. 515089b2837SJed Brown . Blhs - preconditioning matrix or PETSC_NULL if Blhs is an identity matrix 51695f0b562SHong Zhang . flhs - the matrix evaluation routine for Alhs; use PETSC_NULL (PETSC_NULL_FUNCTION in fortran) 51795f0b562SHong Zhang if Alhs is not a function of t. 51895f0b562SHong Zhang . flag - flag indicating information about the matrix structure of Arhs and Alhs. 51995f0b562SHong Zhang The available options are 52095f0b562SHong Zhang SAME_NONZERO_PATTERN - Alhs has the same nonzero structure as Arhs 52195f0b562SHong Zhang DIFFERENT_NONZERO_PATTERN - Alhs has different nonzero structure as Arhs 52295f0b562SHong Zhang - ctx - [optional] user-defined context for private data for the 52395f0b562SHong Zhang matrix evaluation routine (may be PETSC_NULL) 52495f0b562SHong Zhang 52595f0b562SHong Zhang Calling sequence of func: 52695f0b562SHong Zhang $ func(TS ts,PetscReal t,Mat *A,Mat *B,PetscInt *flag,void *ctx); 52795f0b562SHong Zhang 52895f0b562SHong Zhang + t - current timestep 52995f0b562SHong Zhang . A - matrix A, where U_t = A(t) U 53095f0b562SHong Zhang . B - preconditioner matrix, usually the same as A 53195f0b562SHong Zhang . flag - flag indicating information about the preconditioner matrix 53295f0b562SHong Zhang structure (same as flag in KSPSetOperators()) 53395f0b562SHong Zhang - ctx - [optional] user-defined context for matrix evaluation routine 53495f0b562SHong Zhang 53595f0b562SHong Zhang Notes: 53695f0b562SHong Zhang The routine func() takes Mat* as the matrix arguments rather than Mat. 53795f0b562SHong Zhang This allows the matrix evaluation routine to replace Arhs or Alhs with a 53895f0b562SHong Zhang completely new new matrix structure (not just different matrix elements) 53995f0b562SHong Zhang when appropriate, for instance, if the nonzero structure is changing 54095f0b562SHong Zhang throughout the global iterations. 54195f0b562SHong Zhang 54295f0b562SHong Zhang Important: 54395f0b562SHong Zhang The user MUST call either this routine or TSSetRHSFunction(). 54495f0b562SHong Zhang 54595f0b562SHong Zhang Level: beginner 54695f0b562SHong Zhang 54795f0b562SHong Zhang .keywords: TS, timestep, set, matrix 54895f0b562SHong Zhang 54995f0b562SHong Zhang .seealso: TSSetRHSFunction() 55095f0b562SHong Zhang @*/ 551089b2837SJed 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) 55295f0b562SHong Zhang { 553277b19d0SLisandro Dalcin PetscErrorCode ierr; 554089b2837SJed Brown SNES snes; 555277b19d0SLisandro Dalcin 55695f0b562SHong Zhang PetscFunctionBegin; 5570700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 558277b19d0SLisandro Dalcin if (frhs) ts->ops->rhsmatrix = frhs; 559277b19d0SLisandro Dalcin if (flhs) ts->ops->lhsmatrix = flhs; 560277b19d0SLisandro Dalcin if (ctx) ts->jacP = ctx; 561089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 562089b2837SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 563089b2837SJed Brown ierr = SNESSetJacobian(snes,Arhs,Brhs,SNESTSFormJacobian,ts);CHKERRQ(ierr); 56492af4f6aSHong Zhang if (Alhs) { 5650700a824SBarry Smith PetscValidHeaderSpecific(Alhs,MAT_CLASSID,4); 56692af4f6aSHong Zhang PetscCheckSameComm(ts,1,Alhs,4); 567277b19d0SLisandro Dalcin ierr = PetscObjectReference((PetscObject)Alhs);CHKERRQ(ierr); 5686bf464f9SBarry Smith ierr = MatDestroy(&ts->Alhs);CHKERRQ(ierr); 56995f0b562SHong Zhang ts->Alhs = Alhs; 57092af4f6aSHong Zhang } 571089b2837SJed Brown if (Blhs) { 572089b2837SJed Brown PetscValidHeaderSpecific(Blhs,MAT_CLASSID,4); 573089b2837SJed Brown PetscCheckSameComm(ts,1,Blhs,4); 574089b2837SJed Brown ierr = PetscObjectReference((PetscObject)Blhs);CHKERRQ(ierr); 575089b2837SJed Brown ierr = MatDestroy(&ts->Blhs);CHKERRQ(ierr); 576089b2837SJed Brown ts->Blhs = Blhs; 577089b2837SJed Brown } 578089b2837SJed Brown ts->rhsmatstructure = flag; 579089b2837SJed Brown ts->lhsmatstructure = flag; 58095f0b562SHong Zhang PetscFunctionReturn(0); 58195f0b562SHong Zhang } 582d763cef2SBarry Smith 583aa644b49SHong Zhang #undef __FUNCT__ 584cda39b92SHong Zhang #define __FUNCT__ "TSGetMatrices" 585cda39b92SHong Zhang /*@C 586cda39b92SHong Zhang TSGetMatrices - Returns the matrices Arhs and Alhs at the present timestep, 587cda39b92SHong Zhang where Alhs(t) U_t = Arhs(t) U. 588cda39b92SHong Zhang 589cda39b92SHong Zhang Not Collective, but parallel objects are returned if TS is parallel 590cda39b92SHong Zhang 591cda39b92SHong Zhang Input Parameter: 592cda39b92SHong Zhang . ts - The TS context obtained from TSCreate() 593cda39b92SHong Zhang 594cda39b92SHong Zhang Output Parameters: 595cda39b92SHong Zhang + Arhs - The right-hand side matrix 596cda39b92SHong Zhang . Alhs - The left-hand side matrix 597cda39b92SHong Zhang - ctx - User-defined context for matrix evaluation routine 598cda39b92SHong Zhang 599cda39b92SHong Zhang Notes: You can pass in PETSC_NULL for any return argument you do not need. 600cda39b92SHong Zhang 601cda39b92SHong Zhang Level: intermediate 602cda39b92SHong Zhang 603cda39b92SHong Zhang .seealso: TSSetMatrices(), TSGetTimeStep(), TSGetTime(), TSGetTimeStepNumber(), TSGetRHSJacobian() 604cda39b92SHong Zhang 605cda39b92SHong Zhang .keywords: TS, timestep, get, matrix 606cda39b92SHong Zhang 607cda39b92SHong Zhang @*/ 608089b2837SJed Brown PetscErrorCode TSGetMatrices(TS ts,Mat *Arhs,Mat *Brhs,TSMatrix *frhs,Mat *Alhs,Mat *Blhs,TSMatrix *flhs,void **ctx) 609cda39b92SHong Zhang { 610089b2837SJed Brown PetscErrorCode ierr; 611089b2837SJed Brown SNES snes; 612089b2837SJed Brown 613cda39b92SHong Zhang PetscFunctionBegin; 6140700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 615089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 616089b2837SJed Brown ierr = SNESGetJacobian(snes,Arhs,Brhs,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 617089b2837SJed Brown if (frhs) *frhs = ts->ops->rhsmatrix; 618cda39b92SHong Zhang if (Alhs) *Alhs = ts->Alhs; 619089b2837SJed Brown if (Blhs) *Blhs = ts->Blhs; 620089b2837SJed Brown if (flhs) *flhs = ts->ops->lhsmatrix; 621cda39b92SHong Zhang if (ctx) *ctx = ts->jacP; 622cda39b92SHong Zhang PetscFunctionReturn(0); 623cda39b92SHong Zhang } 624cda39b92SHong Zhang 625cda39b92SHong Zhang #undef __FUNCT__ 6264a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 627d763cef2SBarry Smith /*@C 628d763cef2SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of F, 629d763cef2SBarry Smith where U_t = F(U,t), as well as the location to store the matrix. 63076f2fa84SHong Zhang Use TSSetMatrices() for linear problems. 631d763cef2SBarry Smith 6323f9fe445SBarry Smith Logically Collective on TS 633d763cef2SBarry Smith 634d763cef2SBarry Smith Input Parameters: 635d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 636d763cef2SBarry Smith . A - Jacobian matrix 637d763cef2SBarry Smith . B - preconditioner matrix (usually same as A) 638d763cef2SBarry Smith . f - the Jacobian evaluation routine 639d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 640d763cef2SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) 641d763cef2SBarry Smith 642d763cef2SBarry Smith Calling sequence of func: 64387828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx); 644d763cef2SBarry Smith 645d763cef2SBarry Smith + t - current timestep 646d763cef2SBarry Smith . u - input vector 647d763cef2SBarry Smith . A - matrix A, where U_t = A(t)u 648d763cef2SBarry Smith . B - preconditioner matrix, usually the same as A 649d763cef2SBarry Smith . flag - flag indicating information about the preconditioner matrix 65094b7f48cSBarry Smith structure (same as flag in KSPSetOperators()) 651d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 652d763cef2SBarry Smith 653d763cef2SBarry Smith Notes: 65494b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 655d763cef2SBarry Smith output parameter in the routine func(). Be sure to read this information! 656d763cef2SBarry Smith 657d763cef2SBarry Smith The routine func() takes Mat * as the matrix arguments rather than Mat. 658d763cef2SBarry Smith This allows the matrix evaluation routine to replace A and/or B with a 65956335db2SHong Zhang completely new matrix structure (not just different matrix elements) 660d763cef2SBarry Smith when appropriate, for instance, if the nonzero structure is changing 661d763cef2SBarry Smith throughout the global iterations. 662d763cef2SBarry Smith 663d763cef2SBarry Smith Level: beginner 664d763cef2SBarry Smith 665d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 666d763cef2SBarry Smith 667d763cef2SBarry Smith .seealso: TSDefaultComputeJacobianColor(), 66876f2fa84SHong Zhang SNESDefaultComputeJacobianColor(), TSSetRHSFunction(), TSSetMatrices() 669d763cef2SBarry Smith 670d763cef2SBarry Smith @*/ 671089b2837SJed Brown PetscErrorCode TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx) 672d763cef2SBarry Smith { 673277b19d0SLisandro Dalcin PetscErrorCode ierr; 674089b2837SJed Brown SNES snes; 675277b19d0SLisandro Dalcin 676d763cef2SBarry Smith PetscFunctionBegin; 6770700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 678277b19d0SLisandro Dalcin if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 679277b19d0SLisandro Dalcin if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 680277b19d0SLisandro Dalcin if (A) PetscCheckSameComm(ts,1,A,2); 681277b19d0SLisandro Dalcin if (B) PetscCheckSameComm(ts,1,B,3); 68217186662SBarry Smith if (ts->problem_type != TS_NONLINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not for linear problems; use TSSetMatrices()"); 683d763cef2SBarry Smith 684277b19d0SLisandro Dalcin if (f) ts->ops->rhsjacobian = f; 685277b19d0SLisandro Dalcin if (ctx) ts->jacP = ctx; 686089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 687089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 688d763cef2SBarry Smith PetscFunctionReturn(0); 689d763cef2SBarry Smith } 690d763cef2SBarry Smith 691316643e7SJed Brown 692316643e7SJed Brown #undef __FUNCT__ 693316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 694316643e7SJed Brown /*@C 695316643e7SJed Brown TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved. 696316643e7SJed Brown 6973f9fe445SBarry Smith Logically Collective on TS 698316643e7SJed Brown 699316643e7SJed Brown Input Parameters: 700316643e7SJed Brown + ts - the TS context obtained from TSCreate() 701ca94891dSJed Brown . r - vector to hold the residual (or PETSC_NULL to have it created internally) 702316643e7SJed Brown . f - the function evaluation routine 703316643e7SJed Brown - ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL) 704316643e7SJed Brown 705316643e7SJed Brown Calling sequence of f: 706316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 707316643e7SJed Brown 708316643e7SJed Brown + t - time at step/stage being solved 709316643e7SJed Brown . u - state vector 710316643e7SJed Brown . u_t - time derivative of state vector 711316643e7SJed Brown . F - function vector 712316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 713316643e7SJed Brown 714316643e7SJed Brown Important: 715316643e7SJed Brown The user MUST call either this routine, TSSetRHSFunction(), or TSSetMatrices(). This routine must be used when not solving an ODE. 716316643e7SJed Brown 717316643e7SJed Brown Level: beginner 718316643e7SJed Brown 719316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 720316643e7SJed Brown 721316643e7SJed Brown .seealso: TSSetMatrices(), TSSetRHSFunction(), TSSetIJacobian() 722316643e7SJed Brown @*/ 723089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 724316643e7SJed Brown { 725089b2837SJed Brown PetscErrorCode ierr; 726089b2837SJed Brown SNES snes; 727316643e7SJed Brown 728316643e7SJed Brown PetscFunctionBegin; 7290700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 730ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 731089b2837SJed Brown if (f) ts->ops->ifunction = f; 732089b2837SJed Brown if (ctx) ts->funP = ctx; 733089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 734089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 735089b2837SJed Brown PetscFunctionReturn(0); 736089b2837SJed Brown } 737089b2837SJed Brown 738089b2837SJed Brown #undef __FUNCT__ 739089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 740089b2837SJed Brown /*@C 741089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 742089b2837SJed Brown 743089b2837SJed Brown Not Collective 744089b2837SJed Brown 745089b2837SJed Brown Input Parameter: 746089b2837SJed Brown . ts - the TS context 747089b2837SJed Brown 748089b2837SJed Brown Output Parameter: 749089b2837SJed Brown + r - vector to hold residual (or PETSC_NULL) 750089b2837SJed Brown . func - the function to compute residual (or PETSC_NULL) 751089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 752089b2837SJed Brown 753089b2837SJed Brown Level: advanced 754089b2837SJed Brown 755089b2837SJed Brown .keywords: TS, nonlinear, get, function 756089b2837SJed Brown 757089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 758089b2837SJed Brown @*/ 759089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 760089b2837SJed Brown { 761089b2837SJed Brown PetscErrorCode ierr; 762089b2837SJed Brown SNES snes; 763089b2837SJed Brown 764089b2837SJed Brown PetscFunctionBegin; 765089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 766089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 767089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 768089b2837SJed Brown if (func) *func = ts->ops->ifunction; 769089b2837SJed Brown if (ctx) *ctx = ts->funP; 770089b2837SJed Brown PetscFunctionReturn(0); 771089b2837SJed Brown } 772089b2837SJed Brown 773089b2837SJed Brown #undef __FUNCT__ 774089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 775089b2837SJed Brown /*@C 776089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 777089b2837SJed Brown 778089b2837SJed Brown Not Collective 779089b2837SJed Brown 780089b2837SJed Brown Input Parameter: 781089b2837SJed Brown . ts - the TS context 782089b2837SJed Brown 783089b2837SJed Brown Output Parameter: 784089b2837SJed Brown + r - vector to hold computed right hand side (or PETSC_NULL) 785089b2837SJed Brown . func - the function to compute right hand side (or PETSC_NULL) 786089b2837SJed Brown - ctx - the function context (or PETSC_NULL) 787089b2837SJed Brown 788089b2837SJed Brown Level: advanced 789089b2837SJed Brown 790089b2837SJed Brown .keywords: TS, nonlinear, get, function 791089b2837SJed Brown 792089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction() 793089b2837SJed Brown @*/ 794089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 795089b2837SJed Brown { 796089b2837SJed Brown PetscErrorCode ierr; 797089b2837SJed Brown SNES snes; 798089b2837SJed Brown 799089b2837SJed Brown PetscFunctionBegin; 800089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 801089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 802089b2837SJed Brown ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 803089b2837SJed Brown if (func) *func = ts->ops->rhsfunction; 804089b2837SJed Brown if (ctx) *ctx = ts->funP; 805316643e7SJed Brown PetscFunctionReturn(0); 806316643e7SJed Brown } 807316643e7SJed Brown 808316643e7SJed Brown #undef __FUNCT__ 809316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 810316643e7SJed Brown /*@C 811a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 812a4f0a591SBarry Smith you provided with TSSetIFunction(). 813316643e7SJed Brown 8143f9fe445SBarry Smith Logically Collective on TS 815316643e7SJed Brown 816316643e7SJed Brown Input Parameters: 817316643e7SJed Brown + ts - the TS context obtained from TSCreate() 818316643e7SJed Brown . A - Jacobian matrix 819316643e7SJed Brown . B - preconditioning matrix for A (may be same as A) 820316643e7SJed Brown . f - the Jacobian evaluation routine 821316643e7SJed Brown - ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL) 822316643e7SJed Brown 823316643e7SJed Brown Calling sequence of f: 8241b4a444bSJed Brown $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx); 825316643e7SJed Brown 826316643e7SJed Brown + t - time at step/stage being solved 8271b4a444bSJed Brown . U - state vector 8281b4a444bSJed Brown . U_t - time derivative of state vector 829316643e7SJed Brown . a - shift 8301b4a444bSJed Brown . A - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 831316643e7SJed Brown . B - preconditioning matrix for A, may be same as A 832316643e7SJed Brown . flag - flag indicating information about the preconditioner matrix 833316643e7SJed Brown structure (same as flag in KSPSetOperators()) 834316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 835316643e7SJed Brown 836316643e7SJed Brown Notes: 837316643e7SJed Brown The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve. 838316643e7SJed Brown 839a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 840a4f0a591SBarry 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. 841a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 842a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 843a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 844a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 845a4f0a591SBarry Smith 846316643e7SJed Brown Level: beginner 847316643e7SJed Brown 848316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 849316643e7SJed Brown 850316643e7SJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian() 851316643e7SJed Brown 852316643e7SJed Brown @*/ 8537087cfbeSBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx) 854316643e7SJed Brown { 855316643e7SJed Brown PetscErrorCode ierr; 856089b2837SJed Brown SNES snes; 857316643e7SJed Brown 858316643e7SJed Brown PetscFunctionBegin; 8590700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8600700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 8610700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 862316643e7SJed Brown if (A) PetscCheckSameComm(ts,1,A,2); 863316643e7SJed Brown if (B) PetscCheckSameComm(ts,1,B,3); 864316643e7SJed Brown if (f) ts->ops->ijacobian = f; 865316643e7SJed Brown if (ctx) ts->jacP = ctx; 866089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 867089b2837SJed Brown ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr); 868316643e7SJed Brown PetscFunctionReturn(0); 869316643e7SJed Brown } 870316643e7SJed Brown 8714a2ae208SSatish Balay #undef __FUNCT__ 8724a2ae208SSatish Balay #define __FUNCT__ "TSView" 8737e2c5f70SBarry Smith /*@C 874d763cef2SBarry Smith TSView - Prints the TS data structure. 875d763cef2SBarry Smith 8764c49b128SBarry Smith Collective on TS 877d763cef2SBarry Smith 878d763cef2SBarry Smith Input Parameters: 879d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 880d763cef2SBarry Smith - viewer - visualization context 881d763cef2SBarry Smith 882d763cef2SBarry Smith Options Database Key: 883d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 884d763cef2SBarry Smith 885d763cef2SBarry Smith Notes: 886d763cef2SBarry Smith The available visualization contexts include 887b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 888b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 889d763cef2SBarry Smith output where only the first processor opens 890d763cef2SBarry Smith the file. All other processors send their 891d763cef2SBarry Smith data to the first processor to print. 892d763cef2SBarry Smith 893d763cef2SBarry Smith The user can open an alternative visualization context with 894b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 895d763cef2SBarry Smith 896d763cef2SBarry Smith Level: beginner 897d763cef2SBarry Smith 898d763cef2SBarry Smith .keywords: TS, timestep, view 899d763cef2SBarry Smith 900b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 901d763cef2SBarry Smith @*/ 9027087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 903d763cef2SBarry Smith { 904dfbe8321SBarry Smith PetscErrorCode ierr; 905a313700dSBarry Smith const TSType type; 906089b2837SJed Brown PetscBool iascii,isstring,isundials; 907d763cef2SBarry Smith 908d763cef2SBarry Smith PetscFunctionBegin; 9090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9103050cee2SBarry Smith if (!viewer) { 9117adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr); 9123050cee2SBarry Smith } 9130700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 914c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 915fd16b177SBarry Smith 9162692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 9172692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 91832077d6dSBarry Smith if (iascii) { 919317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr); 920000e7ae3SMatthew Knepley if (ts->ops->view) { 921b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 922000e7ae3SMatthew Knepley ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 923b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 924d763cef2SBarry Smith } 92577431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 926a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%G\n",ts->max_time);CHKERRQ(ierr); 927d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 92877431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->nonlinear_its);CHKERRQ(ierr); 929d763cef2SBarry Smith } 93077431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->linear_its);CHKERRQ(ierr); 9310f5bd95cSBarry Smith } else if (isstring) { 932a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 933b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 934d763cef2SBarry Smith } 935b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 936089b2837SJed Brown ierr = PetscTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 937089b2837SJed Brown if (!isundials && ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 938b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 939d763cef2SBarry Smith PetscFunctionReturn(0); 940d763cef2SBarry Smith } 941d763cef2SBarry Smith 942d763cef2SBarry Smith 9434a2ae208SSatish Balay #undef __FUNCT__ 9444a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 945b07ff414SBarry Smith /*@ 946d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 947d763cef2SBarry Smith the timesteppers. 948d763cef2SBarry Smith 9493f9fe445SBarry Smith Logically Collective on TS 950d763cef2SBarry Smith 951d763cef2SBarry Smith Input Parameters: 952d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 953d763cef2SBarry Smith - usrP - optional user context 954d763cef2SBarry Smith 955d763cef2SBarry Smith Level: intermediate 956d763cef2SBarry Smith 957d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 958d763cef2SBarry Smith 959d763cef2SBarry Smith .seealso: TSGetApplicationContext() 960d763cef2SBarry Smith @*/ 9617087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 962d763cef2SBarry Smith { 963d763cef2SBarry Smith PetscFunctionBegin; 9640700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 965d763cef2SBarry Smith ts->user = usrP; 966d763cef2SBarry Smith PetscFunctionReturn(0); 967d763cef2SBarry Smith } 968d763cef2SBarry Smith 9694a2ae208SSatish Balay #undef __FUNCT__ 9704a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 971b07ff414SBarry Smith /*@ 972d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 973d763cef2SBarry Smith timestepper. 974d763cef2SBarry Smith 975d763cef2SBarry Smith Not Collective 976d763cef2SBarry Smith 977d763cef2SBarry Smith Input Parameter: 978d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 979d763cef2SBarry Smith 980d763cef2SBarry Smith Output Parameter: 981d763cef2SBarry Smith . usrP - user context 982d763cef2SBarry Smith 983d763cef2SBarry Smith Level: intermediate 984d763cef2SBarry Smith 985d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 986d763cef2SBarry Smith 987d763cef2SBarry Smith .seealso: TSSetApplicationContext() 988d763cef2SBarry Smith @*/ 989e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 990d763cef2SBarry Smith { 991d763cef2SBarry Smith PetscFunctionBegin; 9920700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 993e71120c6SJed Brown *(void**)usrP = ts->user; 994d763cef2SBarry Smith PetscFunctionReturn(0); 995d763cef2SBarry Smith } 996d763cef2SBarry Smith 9974a2ae208SSatish Balay #undef __FUNCT__ 9984a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 999d763cef2SBarry Smith /*@ 1000d763cef2SBarry Smith TSGetTimeStepNumber - Gets the current number of timesteps. 1001d763cef2SBarry Smith 1002d763cef2SBarry Smith Not Collective 1003d763cef2SBarry Smith 1004d763cef2SBarry Smith Input Parameter: 1005d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1006d763cef2SBarry Smith 1007d763cef2SBarry Smith Output Parameter: 1008d763cef2SBarry Smith . iter - number steps so far 1009d763cef2SBarry Smith 1010d763cef2SBarry Smith Level: intermediate 1011d763cef2SBarry Smith 1012d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 1013d763cef2SBarry Smith @*/ 10147087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt* iter) 1015d763cef2SBarry Smith { 1016d763cef2SBarry Smith PetscFunctionBegin; 10170700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10184482741eSBarry Smith PetscValidIntPointer(iter,2); 1019d763cef2SBarry Smith *iter = ts->steps; 1020d763cef2SBarry Smith PetscFunctionReturn(0); 1021d763cef2SBarry Smith } 1022d763cef2SBarry Smith 10234a2ae208SSatish Balay #undef __FUNCT__ 10244a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1025d763cef2SBarry Smith /*@ 1026d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1027d763cef2SBarry Smith as well as the initial time. 1028d763cef2SBarry Smith 10293f9fe445SBarry Smith Logically Collective on TS 1030d763cef2SBarry Smith 1031d763cef2SBarry Smith Input Parameters: 1032d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1033d763cef2SBarry Smith . initial_time - the initial time 1034d763cef2SBarry Smith - time_step - the size of the timestep 1035d763cef2SBarry Smith 1036d763cef2SBarry Smith Level: intermediate 1037d763cef2SBarry Smith 1038d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1039d763cef2SBarry Smith 1040d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1041d763cef2SBarry Smith @*/ 10427087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1043d763cef2SBarry Smith { 1044d763cef2SBarry Smith PetscFunctionBegin; 10450700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1046d763cef2SBarry Smith ts->time_step = time_step; 1047d763cef2SBarry Smith ts->initial_time_step = time_step; 1048d763cef2SBarry Smith ts->ptime = initial_time; 1049d763cef2SBarry Smith PetscFunctionReturn(0); 1050d763cef2SBarry Smith } 1051d763cef2SBarry Smith 10524a2ae208SSatish Balay #undef __FUNCT__ 10534a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1054d763cef2SBarry Smith /*@ 1055d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1056d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1057d763cef2SBarry Smith 10583f9fe445SBarry Smith Logically Collective on TS 1059d763cef2SBarry Smith 1060d763cef2SBarry Smith Input Parameters: 1061d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1062d763cef2SBarry Smith - time_step - the size of the timestep 1063d763cef2SBarry Smith 1064d763cef2SBarry Smith Level: intermediate 1065d763cef2SBarry Smith 1066d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1067d763cef2SBarry Smith 1068d763cef2SBarry Smith .keywords: TS, set, timestep 1069d763cef2SBarry Smith @*/ 10707087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1071d763cef2SBarry Smith { 1072d763cef2SBarry Smith PetscFunctionBegin; 10730700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1074c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1075d763cef2SBarry Smith ts->time_step = time_step; 1076d763cef2SBarry Smith PetscFunctionReturn(0); 1077d763cef2SBarry Smith } 1078d763cef2SBarry Smith 10794a2ae208SSatish Balay #undef __FUNCT__ 10804a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1081d763cef2SBarry Smith /*@ 1082d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1083d763cef2SBarry Smith 1084d763cef2SBarry Smith Not Collective 1085d763cef2SBarry Smith 1086d763cef2SBarry Smith Input Parameter: 1087d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1088d763cef2SBarry Smith 1089d763cef2SBarry Smith Output Parameter: 1090d763cef2SBarry Smith . dt - the current timestep size 1091d763cef2SBarry Smith 1092d763cef2SBarry Smith Level: intermediate 1093d763cef2SBarry Smith 1094d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1095d763cef2SBarry Smith 1096d763cef2SBarry Smith .keywords: TS, get, timestep 1097d763cef2SBarry Smith @*/ 10987087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal* dt) 1099d763cef2SBarry Smith { 1100d763cef2SBarry Smith PetscFunctionBegin; 11010700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11024482741eSBarry Smith PetscValidDoublePointer(dt,2); 1103d763cef2SBarry Smith *dt = ts->time_step; 1104d763cef2SBarry Smith PetscFunctionReturn(0); 1105d763cef2SBarry Smith } 1106d763cef2SBarry Smith 11074a2ae208SSatish Balay #undef __FUNCT__ 11084a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1109d8e5e3e6SSatish Balay /*@ 1110d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1111d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1112d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1113d763cef2SBarry Smith the solution at the next timestep has been calculated. 1114d763cef2SBarry Smith 1115d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1116d763cef2SBarry Smith 1117d763cef2SBarry Smith Input Parameter: 1118d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1119d763cef2SBarry Smith 1120d763cef2SBarry Smith Output Parameter: 1121d763cef2SBarry Smith . v - the vector containing the solution 1122d763cef2SBarry Smith 1123d763cef2SBarry Smith Level: intermediate 1124d763cef2SBarry Smith 1125d763cef2SBarry Smith .seealso: TSGetTimeStep() 1126d763cef2SBarry Smith 1127d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1128d763cef2SBarry Smith @*/ 11297087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1130d763cef2SBarry Smith { 1131d763cef2SBarry Smith PetscFunctionBegin; 11320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11334482741eSBarry Smith PetscValidPointer(v,2); 11348737fe31SLisandro Dalcin *v = ts->vec_sol; 1135d763cef2SBarry Smith PetscFunctionReturn(0); 1136d763cef2SBarry Smith } 1137d763cef2SBarry Smith 1138bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 11394a2ae208SSatish Balay #undef __FUNCT__ 1140bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1141d8e5e3e6SSatish Balay /*@ 1142bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1143d763cef2SBarry Smith 1144bdad233fSMatthew Knepley Not collective 1145d763cef2SBarry Smith 1146bdad233fSMatthew Knepley Input Parameters: 1147bdad233fSMatthew Knepley + ts - The TS 1148bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1149d763cef2SBarry Smith .vb 1150d763cef2SBarry Smith U_t = A U 1151d763cef2SBarry Smith U_t = A(t) U 1152d763cef2SBarry Smith U_t = F(t,U) 1153d763cef2SBarry Smith .ve 1154d763cef2SBarry Smith 1155d763cef2SBarry Smith Level: beginner 1156d763cef2SBarry Smith 1157bdad233fSMatthew Knepley .keywords: TS, problem type 1158bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1159d763cef2SBarry Smith @*/ 11607087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1161a7cc72afSBarry Smith { 1162d763cef2SBarry Smith PetscFunctionBegin; 11630700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1164bdad233fSMatthew Knepley ts->problem_type = type; 1165d763cef2SBarry Smith PetscFunctionReturn(0); 1166d763cef2SBarry Smith } 1167d763cef2SBarry Smith 1168bdad233fSMatthew Knepley #undef __FUNCT__ 1169bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1170bdad233fSMatthew Knepley /*@C 1171bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1172bdad233fSMatthew Knepley 1173bdad233fSMatthew Knepley Not collective 1174bdad233fSMatthew Knepley 1175bdad233fSMatthew Knepley Input Parameter: 1176bdad233fSMatthew Knepley . ts - The TS 1177bdad233fSMatthew Knepley 1178bdad233fSMatthew Knepley Output Parameter: 1179bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1180bdad233fSMatthew Knepley .vb 1181089b2837SJed Brown M U_t = A U 1182089b2837SJed Brown M(t) U_t = A(t) U 1183bdad233fSMatthew Knepley U_t = F(t,U) 1184bdad233fSMatthew Knepley .ve 1185bdad233fSMatthew Knepley 1186bdad233fSMatthew Knepley Level: beginner 1187bdad233fSMatthew Knepley 1188bdad233fSMatthew Knepley .keywords: TS, problem type 1189bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1190bdad233fSMatthew Knepley @*/ 11917087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1192a7cc72afSBarry Smith { 1193bdad233fSMatthew Knepley PetscFunctionBegin; 11940700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 11954482741eSBarry Smith PetscValidIntPointer(type,2); 1196bdad233fSMatthew Knepley *type = ts->problem_type; 1197bdad233fSMatthew Knepley PetscFunctionReturn(0); 1198bdad233fSMatthew Knepley } 1199d763cef2SBarry Smith 12004a2ae208SSatish Balay #undef __FUNCT__ 12014a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1202d763cef2SBarry Smith /*@ 1203d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1204d763cef2SBarry Smith of a timestepper. 1205d763cef2SBarry Smith 1206d763cef2SBarry Smith Collective on TS 1207d763cef2SBarry Smith 1208d763cef2SBarry Smith Input Parameter: 1209d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1210d763cef2SBarry Smith 1211d763cef2SBarry Smith Notes: 1212d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1213d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1214d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1215d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1216d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1217d763cef2SBarry Smith 1218d763cef2SBarry Smith Level: advanced 1219d763cef2SBarry Smith 1220d763cef2SBarry Smith .keywords: TS, timestep, setup 1221d763cef2SBarry Smith 1222d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1223d763cef2SBarry Smith @*/ 12247087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1225d763cef2SBarry Smith { 1226dfbe8321SBarry Smith PetscErrorCode ierr; 1227d763cef2SBarry Smith 1228d763cef2SBarry Smith PetscFunctionBegin; 12290700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1230277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1231277b19d0SLisandro Dalcin 12327adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 12339596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1234d763cef2SBarry Smith } 1235277b19d0SLisandro Dalcin 1236277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1237277b19d0SLisandro Dalcin 1238277b19d0SLisandro Dalcin if (ts->ops->setup) { 1239000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1240277b19d0SLisandro Dalcin } 1241277b19d0SLisandro Dalcin 1242277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1243277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1244277b19d0SLisandro Dalcin } 1245277b19d0SLisandro Dalcin 1246277b19d0SLisandro Dalcin #undef __FUNCT__ 1247277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1248277b19d0SLisandro Dalcin /*@ 1249277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1250277b19d0SLisandro Dalcin 1251277b19d0SLisandro Dalcin Collective on TS 1252277b19d0SLisandro Dalcin 1253277b19d0SLisandro Dalcin Input Parameter: 1254277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1255277b19d0SLisandro Dalcin 1256277b19d0SLisandro Dalcin Level: beginner 1257277b19d0SLisandro Dalcin 1258277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1259277b19d0SLisandro Dalcin 1260277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1261277b19d0SLisandro Dalcin @*/ 1262277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1263277b19d0SLisandro Dalcin { 1264277b19d0SLisandro Dalcin PetscErrorCode ierr; 1265277b19d0SLisandro Dalcin 1266277b19d0SLisandro Dalcin PetscFunctionBegin; 1267277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1268277b19d0SLisandro Dalcin if (ts->ops->reset) { 1269277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1270277b19d0SLisandro Dalcin } 1271277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 12726bf464f9SBarry Smith ierr = MatDestroy(&ts->Alhs);CHKERRQ(ierr); 1273089b2837SJed Brown ierr = MatDestroy(&ts->Blhs);CHKERRQ(ierr); 12746bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 1275277b19d0SLisandro Dalcin if (ts->work) {ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);} 1276277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 1277d763cef2SBarry Smith PetscFunctionReturn(0); 1278d763cef2SBarry Smith } 1279d763cef2SBarry Smith 12804a2ae208SSatish Balay #undef __FUNCT__ 12814a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 1282d8e5e3e6SSatish Balay /*@ 1283d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 1284d763cef2SBarry Smith with TSCreate(). 1285d763cef2SBarry Smith 1286d763cef2SBarry Smith Collective on TS 1287d763cef2SBarry Smith 1288d763cef2SBarry Smith Input Parameter: 1289d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1290d763cef2SBarry Smith 1291d763cef2SBarry Smith Level: beginner 1292d763cef2SBarry Smith 1293d763cef2SBarry Smith .keywords: TS, timestepper, destroy 1294d763cef2SBarry Smith 1295d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 1296d763cef2SBarry Smith @*/ 12976bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 1298d763cef2SBarry Smith { 12996849ba73SBarry Smith PetscErrorCode ierr; 1300d763cef2SBarry Smith 1301d763cef2SBarry Smith PetscFunctionBegin; 13026bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 13036bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 13046bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 1305d763cef2SBarry Smith 13066bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 1307277b19d0SLisandro Dalcin 1308be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 13096bf464f9SBarry Smith ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr); 13106bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 13116d4c513bSLisandro Dalcin 13126bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 13136bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 13146bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 13156d4c513bSLisandro Dalcin 1316a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 1317d763cef2SBarry Smith PetscFunctionReturn(0); 1318d763cef2SBarry Smith } 1319d763cef2SBarry Smith 13204a2ae208SSatish Balay #undef __FUNCT__ 13214a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 1322d8e5e3e6SSatish Balay /*@ 1323d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 1324d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 1325d763cef2SBarry Smith 1326d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 1327d763cef2SBarry Smith 1328d763cef2SBarry Smith Input Parameter: 1329d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1330d763cef2SBarry Smith 1331d763cef2SBarry Smith Output Parameter: 1332d763cef2SBarry Smith . snes - the nonlinear solver context 1333d763cef2SBarry Smith 1334d763cef2SBarry Smith Notes: 1335d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 1336d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 133794b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 1338d763cef2SBarry Smith 1339d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 1340d763cef2SBarry Smith this case TSGetSNES() returns PETSC_NULL in snes. 1341d763cef2SBarry Smith 1342d763cef2SBarry Smith Level: beginner 1343d763cef2SBarry Smith 1344d763cef2SBarry Smith .keywords: timestep, get, SNES 1345d763cef2SBarry Smith @*/ 13467087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 1347d763cef2SBarry Smith { 1348d372ba47SLisandro Dalcin PetscErrorCode ierr; 1349d372ba47SLisandro Dalcin 1350d763cef2SBarry Smith PetscFunctionBegin; 13510700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13524482741eSBarry Smith PetscValidPointer(snes,2); 1353d372ba47SLisandro Dalcin if (!ts->snes) { 1354d372ba47SLisandro Dalcin ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 1355d372ba47SLisandro Dalcin ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr); 1356d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 1357d372ba47SLisandro Dalcin } 1358d763cef2SBarry Smith *snes = ts->snes; 1359d763cef2SBarry Smith PetscFunctionReturn(0); 1360d763cef2SBarry Smith } 1361d763cef2SBarry Smith 13624a2ae208SSatish Balay #undef __FUNCT__ 136394b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 1364d8e5e3e6SSatish Balay /*@ 136594b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 1366d763cef2SBarry Smith a TS (timestepper) context. 1367d763cef2SBarry Smith 136894b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 1369d763cef2SBarry Smith 1370d763cef2SBarry Smith Input Parameter: 1371d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1372d763cef2SBarry Smith 1373d763cef2SBarry Smith Output Parameter: 137494b7f48cSBarry Smith . ksp - the nonlinear solver context 1375d763cef2SBarry Smith 1376d763cef2SBarry Smith Notes: 137794b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 1378d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 1379d763cef2SBarry Smith KSP and PC contexts as well. 1380d763cef2SBarry Smith 138194b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 138294b7f48cSBarry Smith in this case TSGetKSP() returns PETSC_NULL in ksp. 1383d763cef2SBarry Smith 1384d763cef2SBarry Smith Level: beginner 1385d763cef2SBarry Smith 138694b7f48cSBarry Smith .keywords: timestep, get, KSP 1387d763cef2SBarry Smith @*/ 13887087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 1389d763cef2SBarry Smith { 1390d372ba47SLisandro Dalcin PetscErrorCode ierr; 1391089b2837SJed Brown SNES snes; 1392d372ba47SLisandro Dalcin 1393d763cef2SBarry Smith PetscFunctionBegin; 13940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13954482741eSBarry Smith PetscValidPointer(ksp,2); 139617186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 1397e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 1398089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1399089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 1400d763cef2SBarry Smith PetscFunctionReturn(0); 1401d763cef2SBarry Smith } 1402d763cef2SBarry Smith 1403d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 1404d763cef2SBarry Smith 14054a2ae208SSatish Balay #undef __FUNCT__ 1406adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 1407adb62b0dSMatthew Knepley /*@ 1408adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 1409adb62b0dSMatthew Knepley maximum time for iteration. 1410adb62b0dSMatthew Knepley 14113f9fe445SBarry Smith Not Collective 1412adb62b0dSMatthew Knepley 1413adb62b0dSMatthew Knepley Input Parameters: 1414adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 1415adb62b0dSMatthew Knepley . maxsteps - maximum number of iterations to use, or PETSC_NULL 1416adb62b0dSMatthew Knepley - maxtime - final time to iterate to, or PETSC_NULL 1417adb62b0dSMatthew Knepley 1418adb62b0dSMatthew Knepley Level: intermediate 1419adb62b0dSMatthew Knepley 1420adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 1421adb62b0dSMatthew Knepley @*/ 14227087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 1423adb62b0dSMatthew Knepley { 1424adb62b0dSMatthew Knepley PetscFunctionBegin; 14250700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1426abc0a331SBarry Smith if (maxsteps) { 14274482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 1428adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 1429adb62b0dSMatthew Knepley } 1430abc0a331SBarry Smith if (maxtime ) { 14314482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 1432adb62b0dSMatthew Knepley *maxtime = ts->max_time; 1433adb62b0dSMatthew Knepley } 1434adb62b0dSMatthew Knepley PetscFunctionReturn(0); 1435adb62b0dSMatthew Knepley } 1436adb62b0dSMatthew Knepley 1437adb62b0dSMatthew Knepley #undef __FUNCT__ 14384a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 1439d763cef2SBarry Smith /*@ 1440d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 1441d763cef2SBarry Smith maximum time for iteration. 1442d763cef2SBarry Smith 14433f9fe445SBarry Smith Logically Collective on TS 1444d763cef2SBarry Smith 1445d763cef2SBarry Smith Input Parameters: 1446d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1447d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 1448d763cef2SBarry Smith - maxtime - final time to iterate to 1449d763cef2SBarry Smith 1450d763cef2SBarry Smith Options Database Keys: 1451d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 1452d763cef2SBarry Smith . -ts_max_time <maxtime> - Sets maxtime 1453d763cef2SBarry Smith 1454d763cef2SBarry Smith Notes: 1455d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 1456d763cef2SBarry Smith 1457d763cef2SBarry Smith Level: intermediate 1458d763cef2SBarry Smith 1459d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 1460d763cef2SBarry Smith @*/ 14617087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 1462d763cef2SBarry Smith { 1463d763cef2SBarry Smith PetscFunctionBegin; 14640700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1465c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 1466c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 1467d763cef2SBarry Smith ts->max_steps = maxsteps; 1468d763cef2SBarry Smith ts->max_time = maxtime; 1469d763cef2SBarry Smith PetscFunctionReturn(0); 1470d763cef2SBarry Smith } 1471d763cef2SBarry Smith 14724a2ae208SSatish Balay #undef __FUNCT__ 14734a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 1474d763cef2SBarry Smith /*@ 1475d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 1476d763cef2SBarry Smith for use by the TS routines. 1477d763cef2SBarry Smith 14783f9fe445SBarry Smith Logically Collective on TS and Vec 1479d763cef2SBarry Smith 1480d763cef2SBarry Smith Input Parameters: 1481d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1482d763cef2SBarry Smith - x - the solution vector 1483d763cef2SBarry Smith 1484d763cef2SBarry Smith Level: beginner 1485d763cef2SBarry Smith 1486d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 1487d763cef2SBarry Smith @*/ 14887087cfbeSBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec x) 1489d763cef2SBarry Smith { 14908737fe31SLisandro Dalcin PetscErrorCode ierr; 14918737fe31SLisandro Dalcin 1492d763cef2SBarry Smith PetscFunctionBegin; 14930700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14940700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 14958737fe31SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 14966bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 14978737fe31SLisandro Dalcin ts->vec_sol = x; 1498d763cef2SBarry Smith PetscFunctionReturn(0); 1499d763cef2SBarry Smith } 1500d763cef2SBarry Smith 1501e74ef692SMatthew Knepley #undef __FUNCT__ 1502e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 1503ac226902SBarry Smith /*@C 1504000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 15053f2090d5SJed Brown called once at the beginning of each time step. 1506000e7ae3SMatthew Knepley 15073f9fe445SBarry Smith Logically Collective on TS 1508000e7ae3SMatthew Knepley 1509000e7ae3SMatthew Knepley Input Parameters: 1510000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1511000e7ae3SMatthew Knepley - func - The function 1512000e7ae3SMatthew Knepley 1513000e7ae3SMatthew Knepley Calling sequence of func: 1514000e7ae3SMatthew Knepley . func (TS ts); 1515000e7ae3SMatthew Knepley 1516000e7ae3SMatthew Knepley Level: intermediate 1517000e7ae3SMatthew Knepley 1518000e7ae3SMatthew Knepley .keywords: TS, timestep 1519000e7ae3SMatthew Knepley @*/ 15207087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 1521000e7ae3SMatthew Knepley { 1522000e7ae3SMatthew Knepley PetscFunctionBegin; 15230700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1524000e7ae3SMatthew Knepley ts->ops->prestep = func; 1525000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1526000e7ae3SMatthew Knepley } 1527000e7ae3SMatthew Knepley 1528e74ef692SMatthew Knepley #undef __FUNCT__ 15293f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 15303f2090d5SJed Brown /*@C 15313f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 15323f2090d5SJed Brown 15333f2090d5SJed Brown Collective on TS 15343f2090d5SJed Brown 15353f2090d5SJed Brown Input Parameters: 15363f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 15373f2090d5SJed Brown 15383f2090d5SJed Brown Notes: 15393f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 15403f2090d5SJed Brown so most users would not generally call this routine themselves. 15413f2090d5SJed Brown 15423f2090d5SJed Brown Level: developer 15433f2090d5SJed Brown 15443f2090d5SJed Brown .keywords: TS, timestep 15453f2090d5SJed Brown @*/ 15467087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 15473f2090d5SJed Brown { 15483f2090d5SJed Brown PetscErrorCode ierr; 15493f2090d5SJed Brown 15503f2090d5SJed Brown PetscFunctionBegin; 15510700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 155272ac3e02SJed Brown if (ts->ops->prestep) { 15533f2090d5SJed Brown PetscStackPush("TS PreStep function"); 15543f2090d5SJed Brown ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr); 15553f2090d5SJed Brown PetscStackPop; 1556312ce896SJed Brown } 15573f2090d5SJed Brown PetscFunctionReturn(0); 15583f2090d5SJed Brown } 15593f2090d5SJed Brown 15603f2090d5SJed Brown #undef __FUNCT__ 1561e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 1562ac226902SBarry Smith /*@C 1563000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 15643f2090d5SJed Brown called once at the end of each time step. 1565000e7ae3SMatthew Knepley 15663f9fe445SBarry Smith Logically Collective on TS 1567000e7ae3SMatthew Knepley 1568000e7ae3SMatthew Knepley Input Parameters: 1569000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 1570000e7ae3SMatthew Knepley - func - The function 1571000e7ae3SMatthew Knepley 1572000e7ae3SMatthew Knepley Calling sequence of func: 1573000e7ae3SMatthew Knepley . func (TS ts); 1574000e7ae3SMatthew Knepley 1575000e7ae3SMatthew Knepley Level: intermediate 1576000e7ae3SMatthew Knepley 1577000e7ae3SMatthew Knepley .keywords: TS, timestep 1578000e7ae3SMatthew Knepley @*/ 15797087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 1580000e7ae3SMatthew Knepley { 1581000e7ae3SMatthew Knepley PetscFunctionBegin; 15820700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1583000e7ae3SMatthew Knepley ts->ops->poststep = func; 1584000e7ae3SMatthew Knepley PetscFunctionReturn(0); 1585000e7ae3SMatthew Knepley } 1586000e7ae3SMatthew Knepley 1587e74ef692SMatthew Knepley #undef __FUNCT__ 15883f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 15893f2090d5SJed Brown /*@C 15903f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 15913f2090d5SJed Brown 15923f2090d5SJed Brown Collective on TS 15933f2090d5SJed Brown 15943f2090d5SJed Brown Input Parameters: 15953f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 15963f2090d5SJed Brown 15973f2090d5SJed Brown Notes: 15983f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 15993f2090d5SJed Brown so most users would not generally call this routine themselves. 16003f2090d5SJed Brown 16013f2090d5SJed Brown Level: developer 16023f2090d5SJed Brown 16033f2090d5SJed Brown .keywords: TS, timestep 16043f2090d5SJed Brown @*/ 16057087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 16063f2090d5SJed Brown { 16073f2090d5SJed Brown PetscErrorCode ierr; 16083f2090d5SJed Brown 16093f2090d5SJed Brown PetscFunctionBegin; 16100700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 161172ac3e02SJed Brown if (ts->ops->poststep) { 16123f2090d5SJed Brown PetscStackPush("TS PostStep function"); 16133f2090d5SJed Brown ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr); 16143f2090d5SJed Brown PetscStackPop; 161572ac3e02SJed Brown } 16163f2090d5SJed Brown PetscFunctionReturn(0); 16173f2090d5SJed Brown } 16183f2090d5SJed Brown 1619d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 1620d763cef2SBarry Smith 16214a2ae208SSatish Balay #undef __FUNCT__ 1622a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 1623d763cef2SBarry Smith /*@C 1624a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 1625d763cef2SBarry Smith timestep to display the iteration's progress. 1626d763cef2SBarry Smith 16273f9fe445SBarry Smith Logically Collective on TS 1628d763cef2SBarry Smith 1629d763cef2SBarry Smith Input Parameters: 1630d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1631d763cef2SBarry Smith . func - monitoring routine 1632329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 1633b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desired) 1634b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1635b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 1636d763cef2SBarry Smith 1637d763cef2SBarry Smith Calling sequence of func: 1638a7cc72afSBarry Smith $ int func(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx) 1639d763cef2SBarry Smith 1640d763cef2SBarry Smith + ts - the TS context 1641d763cef2SBarry Smith . steps - iteration number 16421f06c33eSBarry Smith . time - current time 1643d763cef2SBarry Smith . x - current iterate 1644d763cef2SBarry Smith - mctx - [optional] monitoring context 1645d763cef2SBarry Smith 1646d763cef2SBarry Smith Notes: 1647d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 1648d763cef2SBarry Smith already has been loaded. 1649d763cef2SBarry Smith 1650025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 1651025f1a04SBarry Smith 1652d763cef2SBarry Smith Level: intermediate 1653d763cef2SBarry Smith 1654d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1655d763cef2SBarry Smith 1656a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 1657d763cef2SBarry Smith @*/ 1658c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 1659d763cef2SBarry Smith { 1660d763cef2SBarry Smith PetscFunctionBegin; 16610700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 166217186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1663d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 1664329f5518SBarry Smith ts->mdestroy[ts->numbermonitors] = mdestroy; 1665d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 1666d763cef2SBarry Smith PetscFunctionReturn(0); 1667d763cef2SBarry Smith } 1668d763cef2SBarry Smith 16694a2ae208SSatish Balay #undef __FUNCT__ 1670a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 1671d763cef2SBarry Smith /*@C 1672a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 1673d763cef2SBarry Smith 16743f9fe445SBarry Smith Logically Collective on TS 1675d763cef2SBarry Smith 1676d763cef2SBarry Smith Input Parameters: 1677d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1678d763cef2SBarry Smith 1679d763cef2SBarry Smith Notes: 1680d763cef2SBarry Smith There is no way to remove a single, specific monitor. 1681d763cef2SBarry Smith 1682d763cef2SBarry Smith Level: intermediate 1683d763cef2SBarry Smith 1684d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 1685d763cef2SBarry Smith 1686a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 1687d763cef2SBarry Smith @*/ 16887087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 1689d763cef2SBarry Smith { 1690d952e501SBarry Smith PetscErrorCode ierr; 1691d952e501SBarry Smith PetscInt i; 1692d952e501SBarry Smith 1693d763cef2SBarry Smith PetscFunctionBegin; 16940700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1695d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 1696d952e501SBarry Smith if (ts->mdestroy[i]) { 16973c4aec1bSBarry Smith ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 1698d952e501SBarry Smith } 1699d952e501SBarry Smith } 1700d763cef2SBarry Smith ts->numbermonitors = 0; 1701d763cef2SBarry Smith PetscFunctionReturn(0); 1702d763cef2SBarry Smith } 1703d763cef2SBarry Smith 17044a2ae208SSatish Balay #undef __FUNCT__ 1705a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 1706d8e5e3e6SSatish Balay /*@ 1707a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 17085516499fSSatish Balay 17095516499fSSatish Balay Level: intermediate 171041251cbbSSatish Balay 17115516499fSSatish Balay .keywords: TS, set, monitor 17125516499fSSatish Balay 171341251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 171441251cbbSSatish Balay @*/ 1715649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 1716d763cef2SBarry Smith { 1717dfbe8321SBarry Smith PetscErrorCode ierr; 1718649052a6SBarry Smith PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm); 1719d132466eSBarry Smith 1720d763cef2SBarry Smith PetscFunctionBegin; 1721649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1722649052a6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %G time %G\n",step,ts->time_step,ptime);CHKERRQ(ierr); 1723649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 1724d763cef2SBarry Smith PetscFunctionReturn(0); 1725d763cef2SBarry Smith } 1726d763cef2SBarry Smith 17274a2ae208SSatish Balay #undef __FUNCT__ 17284a2ae208SSatish Balay #define __FUNCT__ "TSStep" 1729d763cef2SBarry Smith /*@ 1730d763cef2SBarry Smith TSStep - Steps the requested number of timesteps. 1731d763cef2SBarry Smith 1732d763cef2SBarry Smith Collective on TS 1733d763cef2SBarry Smith 1734d763cef2SBarry Smith Input Parameter: 1735d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1736d763cef2SBarry Smith 1737d763cef2SBarry Smith Output Parameters: 1738d763cef2SBarry Smith + steps - number of iterations until termination 1739142b95e3SSatish Balay - ptime - time until termination 1740d763cef2SBarry Smith 1741d763cef2SBarry Smith Level: beginner 1742d763cef2SBarry Smith 1743d763cef2SBarry Smith .keywords: TS, timestep, solve 1744d763cef2SBarry Smith 1745d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy() 1746d763cef2SBarry Smith @*/ 17477087cfbeSBarry Smith PetscErrorCode TSStep(TS ts,PetscInt *steps,PetscReal *ptime) 1748d763cef2SBarry Smith { 1749dfbe8321SBarry Smith PetscErrorCode ierr; 1750d763cef2SBarry Smith 1751d763cef2SBarry Smith PetscFunctionBegin; 17520700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1753277b19d0SLisandro Dalcin 1754d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 1755d405a339SMatthew Knepley 1756d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1757000e7ae3SMatthew Knepley ierr = (*ts->ops->step)(ts, steps, ptime);CHKERRQ(ierr); 1758d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step, ts, 0, 0, 0);CHKERRQ(ierr); 1759d405a339SMatthew Knepley 17604bb05414SBarry Smith if (!PetscPreLoadingOn) { 17617adad957SLisandro Dalcin ierr = TSViewFromOptions(ts,((PetscObject)ts)->name);CHKERRQ(ierr); 1762d405a339SMatthew Knepley } 1763d763cef2SBarry Smith PetscFunctionReturn(0); 1764d763cef2SBarry Smith } 1765d763cef2SBarry Smith 17664a2ae208SSatish Balay #undef __FUNCT__ 17676a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve" 17686a4d4014SLisandro Dalcin /*@ 17696a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 17706a4d4014SLisandro Dalcin 17716a4d4014SLisandro Dalcin Collective on TS 17726a4d4014SLisandro Dalcin 17736a4d4014SLisandro Dalcin Input Parameter: 17746a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 17756a4d4014SLisandro Dalcin - x - the solution vector, or PETSC_NULL if it was set with TSSetSolution() 17766a4d4014SLisandro Dalcin 17776a4d4014SLisandro Dalcin Level: beginner 17786a4d4014SLisandro Dalcin 17796a4d4014SLisandro Dalcin .keywords: TS, timestep, solve 17806a4d4014SLisandro Dalcin 17816a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep() 17826a4d4014SLisandro Dalcin @*/ 17837087cfbeSBarry Smith PetscErrorCode TSSolve(TS ts, Vec x) 17846a4d4014SLisandro Dalcin { 17856a4d4014SLisandro Dalcin PetscInt steps; 17866a4d4014SLisandro Dalcin PetscReal ptime; 17876a4d4014SLisandro Dalcin PetscErrorCode ierr; 1788f22f69f0SBarry Smith 17896a4d4014SLisandro Dalcin PetscFunctionBegin; 17900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17916a4d4014SLisandro Dalcin /* set solution vector if provided */ 17926a4d4014SLisandro Dalcin if (x) { ierr = TSSetSolution(ts, x); CHKERRQ(ierr); } 17936a4d4014SLisandro Dalcin /* reset time step and iteration counters */ 17946a4d4014SLisandro Dalcin ts->steps = 0; ts->linear_its = 0; ts->nonlinear_its = 0; 17956a4d4014SLisandro Dalcin /* steps the requested number of timesteps. */ 17966a4d4014SLisandro Dalcin ierr = TSStep(ts, &steps, &ptime);CHKERRQ(ierr); 17976a4d4014SLisandro Dalcin PetscFunctionReturn(0); 17986a4d4014SLisandro Dalcin } 17996a4d4014SLisandro Dalcin 18006a4d4014SLisandro Dalcin #undef __FUNCT__ 18014a2ae208SSatish Balay #define __FUNCT__ "TSMonitor" 1802d763cef2SBarry Smith /* 1803d763cef2SBarry Smith Runs the user provided monitor routines, if they exists. 1804d763cef2SBarry Smith */ 1805a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x) 1806d763cef2SBarry Smith { 18076849ba73SBarry Smith PetscErrorCode ierr; 1808a7cc72afSBarry Smith PetscInt i,n = ts->numbermonitors; 1809d763cef2SBarry Smith 1810d763cef2SBarry Smith PetscFunctionBegin; 1811d763cef2SBarry Smith for (i=0; i<n; i++) { 1812142b95e3SSatish Balay ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr); 1813d763cef2SBarry Smith } 1814d763cef2SBarry Smith PetscFunctionReturn(0); 1815d763cef2SBarry Smith } 1816d763cef2SBarry Smith 1817d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 1818d763cef2SBarry Smith 18194a2ae208SSatish Balay #undef __FUNCT__ 1820a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate" 1821d763cef2SBarry Smith /*@C 1822a6570f20SBarry Smith TSMonitorLGCreate - Creates a line graph context for use with 1823d763cef2SBarry Smith TS to monitor convergence of preconditioned residual norms. 1824d763cef2SBarry Smith 1825d763cef2SBarry Smith Collective on TS 1826d763cef2SBarry Smith 1827d763cef2SBarry Smith Input Parameters: 1828d763cef2SBarry Smith + host - the X display to open, or null for the local machine 1829d763cef2SBarry Smith . label - the title to put in the title bar 18307c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 1831d763cef2SBarry Smith - m, n - the screen width and height in pixels 1832d763cef2SBarry Smith 1833d763cef2SBarry Smith Output Parameter: 1834d763cef2SBarry Smith . draw - the drawing context 1835d763cef2SBarry Smith 1836d763cef2SBarry Smith Options Database Key: 1837a6570f20SBarry Smith . -ts_monitor_draw - automatically sets line graph monitor 1838d763cef2SBarry Smith 1839d763cef2SBarry Smith Notes: 1840a6570f20SBarry Smith Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy(). 1841d763cef2SBarry Smith 1842d763cef2SBarry Smith Level: intermediate 1843d763cef2SBarry Smith 18447c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 1845d763cef2SBarry Smith 1846a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet() 18477c922b88SBarry Smith 1848d763cef2SBarry Smith @*/ 18497087cfbeSBarry Smith PetscErrorCode TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1850d763cef2SBarry Smith { 1851b0a32e0cSBarry Smith PetscDraw win; 1852dfbe8321SBarry Smith PetscErrorCode ierr; 1853d763cef2SBarry Smith 1854d763cef2SBarry Smith PetscFunctionBegin; 1855b0a32e0cSBarry Smith ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr); 1856b0a32e0cSBarry Smith ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr); 1857b0a32e0cSBarry Smith ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr); 1858b0a32e0cSBarry Smith ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr); 1859d763cef2SBarry Smith 186052e6d16bSBarry Smith ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr); 1861d763cef2SBarry Smith PetscFunctionReturn(0); 1862d763cef2SBarry Smith } 1863d763cef2SBarry Smith 18644a2ae208SSatish Balay #undef __FUNCT__ 1865a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG" 1866a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 1867d763cef2SBarry Smith { 1868b0a32e0cSBarry Smith PetscDrawLG lg = (PetscDrawLG) monctx; 186987828ca2SBarry Smith PetscReal x,y = ptime; 1870dfbe8321SBarry Smith PetscErrorCode ierr; 1871d763cef2SBarry Smith 1872d763cef2SBarry Smith PetscFunctionBegin; 18737c922b88SBarry Smith if (!monctx) { 18747c922b88SBarry Smith MPI_Comm comm; 1875b0a32e0cSBarry Smith PetscViewer viewer; 18767c922b88SBarry Smith 18777c922b88SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 1878b0a32e0cSBarry Smith viewer = PETSC_VIEWER_DRAW_(comm); 1879b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr); 18807c922b88SBarry Smith } 18817c922b88SBarry Smith 1882b0a32e0cSBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 188387828ca2SBarry Smith x = (PetscReal)n; 1884b0a32e0cSBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1885d763cef2SBarry Smith if (n < 20 || (n % 5)) { 1886b0a32e0cSBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1887d763cef2SBarry Smith } 1888d763cef2SBarry Smith PetscFunctionReturn(0); 1889d763cef2SBarry Smith } 1890d763cef2SBarry Smith 18914a2ae208SSatish Balay #undef __FUNCT__ 1892a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy" 1893d763cef2SBarry Smith /*@C 1894a6570f20SBarry Smith TSMonitorLGDestroy - Destroys a line graph context that was created 1895a6570f20SBarry Smith with TSMonitorLGCreate(). 1896d763cef2SBarry Smith 1897b0a32e0cSBarry Smith Collective on PetscDrawLG 1898d763cef2SBarry Smith 1899d763cef2SBarry Smith Input Parameter: 1900d763cef2SBarry Smith . draw - the drawing context 1901d763cef2SBarry Smith 1902d763cef2SBarry Smith Level: intermediate 1903d763cef2SBarry Smith 1904d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 1905d763cef2SBarry Smith 1906a6570f20SBarry Smith .seealso: TSMonitorLGCreate(), TSMonitorSet(), TSMonitorLG(); 1907d763cef2SBarry Smith @*/ 19086bf464f9SBarry Smith PetscErrorCode TSMonitorLGDestroy(PetscDrawLG *drawlg) 1909d763cef2SBarry Smith { 1910b0a32e0cSBarry Smith PetscDraw draw; 1911dfbe8321SBarry Smith PetscErrorCode ierr; 1912d763cef2SBarry Smith 1913d763cef2SBarry Smith PetscFunctionBegin; 19146bf464f9SBarry Smith ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr); 19156bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 1916b0a32e0cSBarry Smith ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr); 1917d763cef2SBarry Smith PetscFunctionReturn(0); 1918d763cef2SBarry Smith } 1919d763cef2SBarry Smith 19204a2ae208SSatish Balay #undef __FUNCT__ 19214a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 1922d763cef2SBarry Smith /*@ 1923d763cef2SBarry Smith TSGetTime - Gets the current time. 1924d763cef2SBarry Smith 1925d763cef2SBarry Smith Not Collective 1926d763cef2SBarry Smith 1927d763cef2SBarry Smith Input Parameter: 1928d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1929d763cef2SBarry Smith 1930d763cef2SBarry Smith Output Parameter: 1931d763cef2SBarry Smith . t - the current time 1932d763cef2SBarry Smith 1933d763cef2SBarry Smith Level: beginner 1934d763cef2SBarry Smith 1935d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1936d763cef2SBarry Smith 1937d763cef2SBarry Smith .keywords: TS, get, time 1938d763cef2SBarry Smith @*/ 19397087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal* t) 1940d763cef2SBarry Smith { 1941d763cef2SBarry Smith PetscFunctionBegin; 19420700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 19434482741eSBarry Smith PetscValidDoublePointer(t,2); 1944d763cef2SBarry Smith *t = ts->ptime; 1945d763cef2SBarry Smith PetscFunctionReturn(0); 1946d763cef2SBarry Smith } 1947d763cef2SBarry Smith 19484a2ae208SSatish Balay #undef __FUNCT__ 19496a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 19506a4d4014SLisandro Dalcin /*@ 19516a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 19526a4d4014SLisandro Dalcin 19533f9fe445SBarry Smith Logically Collective on TS 19546a4d4014SLisandro Dalcin 19556a4d4014SLisandro Dalcin Input Parameters: 19566a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 19576a4d4014SLisandro Dalcin - time - the time 19586a4d4014SLisandro Dalcin 19596a4d4014SLisandro Dalcin Level: intermediate 19606a4d4014SLisandro Dalcin 19616a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 19626a4d4014SLisandro Dalcin 19636a4d4014SLisandro Dalcin .keywords: TS, set, time 19646a4d4014SLisandro Dalcin @*/ 19657087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 19666a4d4014SLisandro Dalcin { 19676a4d4014SLisandro Dalcin PetscFunctionBegin; 19680700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1969c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 19706a4d4014SLisandro Dalcin ts->ptime = t; 19716a4d4014SLisandro Dalcin PetscFunctionReturn(0); 19726a4d4014SLisandro Dalcin } 19736a4d4014SLisandro Dalcin 19746a4d4014SLisandro Dalcin #undef __FUNCT__ 19754a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 1976d763cef2SBarry Smith /*@C 1977d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 1978d763cef2SBarry Smith TS options in the database. 1979d763cef2SBarry Smith 19803f9fe445SBarry Smith Logically Collective on TS 1981d763cef2SBarry Smith 1982d763cef2SBarry Smith Input Parameter: 1983d763cef2SBarry Smith + ts - The TS context 1984d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 1985d763cef2SBarry Smith 1986d763cef2SBarry Smith Notes: 1987d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 1988d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 1989d763cef2SBarry Smith hyphen. 1990d763cef2SBarry Smith 1991d763cef2SBarry Smith Level: advanced 1992d763cef2SBarry Smith 1993d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 1994d763cef2SBarry Smith 1995d763cef2SBarry Smith .seealso: TSSetFromOptions() 1996d763cef2SBarry Smith 1997d763cef2SBarry Smith @*/ 19987087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 1999d763cef2SBarry Smith { 2000dfbe8321SBarry Smith PetscErrorCode ierr; 2001089b2837SJed Brown SNES snes; 2002d763cef2SBarry Smith 2003d763cef2SBarry Smith PetscFunctionBegin; 20040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2005d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2006089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2007089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2008d763cef2SBarry Smith PetscFunctionReturn(0); 2009d763cef2SBarry Smith } 2010d763cef2SBarry Smith 2011d763cef2SBarry Smith 20124a2ae208SSatish Balay #undef __FUNCT__ 20134a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 2014d763cef2SBarry Smith /*@C 2015d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 2016d763cef2SBarry Smith TS options in the database. 2017d763cef2SBarry Smith 20183f9fe445SBarry Smith Logically Collective on TS 2019d763cef2SBarry Smith 2020d763cef2SBarry Smith Input Parameter: 2021d763cef2SBarry Smith + ts - The TS context 2022d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 2023d763cef2SBarry Smith 2024d763cef2SBarry Smith Notes: 2025d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2026d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 2027d763cef2SBarry Smith hyphen. 2028d763cef2SBarry Smith 2029d763cef2SBarry Smith Level: advanced 2030d763cef2SBarry Smith 2031d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 2032d763cef2SBarry Smith 2033d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 2034d763cef2SBarry Smith 2035d763cef2SBarry Smith @*/ 20367087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 2037d763cef2SBarry Smith { 2038dfbe8321SBarry Smith PetscErrorCode ierr; 2039089b2837SJed Brown SNES snes; 2040d763cef2SBarry Smith 2041d763cef2SBarry Smith PetscFunctionBegin; 20420700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2043d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2044089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2045089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 2046d763cef2SBarry Smith PetscFunctionReturn(0); 2047d763cef2SBarry Smith } 2048d763cef2SBarry Smith 20494a2ae208SSatish Balay #undef __FUNCT__ 20504a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 2051d763cef2SBarry Smith /*@C 2052d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 2053d763cef2SBarry Smith TS options in the database. 2054d763cef2SBarry Smith 2055d763cef2SBarry Smith Not Collective 2056d763cef2SBarry Smith 2057d763cef2SBarry Smith Input Parameter: 2058d763cef2SBarry Smith . ts - The TS context 2059d763cef2SBarry Smith 2060d763cef2SBarry Smith Output Parameter: 2061d763cef2SBarry Smith . prefix - A pointer to the prefix string used 2062d763cef2SBarry Smith 2063d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 2064d763cef2SBarry Smith sufficient length to hold the prefix. 2065d763cef2SBarry Smith 2066d763cef2SBarry Smith Level: intermediate 2067d763cef2SBarry Smith 2068d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 2069d763cef2SBarry Smith 2070d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 2071d763cef2SBarry Smith @*/ 20727087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 2073d763cef2SBarry Smith { 2074dfbe8321SBarry Smith PetscErrorCode ierr; 2075d763cef2SBarry Smith 2076d763cef2SBarry Smith PetscFunctionBegin; 20770700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20784482741eSBarry Smith PetscValidPointer(prefix,2); 2079d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 2080d763cef2SBarry Smith PetscFunctionReturn(0); 2081d763cef2SBarry Smith } 2082d763cef2SBarry Smith 20834a2ae208SSatish Balay #undef __FUNCT__ 20844a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 2085d763cef2SBarry Smith /*@C 2086d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 2087d763cef2SBarry Smith 2088d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 2089d763cef2SBarry Smith 2090d763cef2SBarry Smith Input Parameter: 2091d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 2092d763cef2SBarry Smith 2093d763cef2SBarry Smith Output Parameters: 2094d763cef2SBarry Smith + J - The Jacobian J of F, where U_t = F(U,t) 2095d763cef2SBarry Smith . M - The preconditioner matrix, usually the same as J 2096089b2837SJed Brown . func - Function to compute the Jacobian of the RHS 2097d763cef2SBarry Smith - ctx - User-defined context for Jacobian evaluation routine 2098d763cef2SBarry Smith 2099d763cef2SBarry Smith Notes: You can pass in PETSC_NULL for any return argument you do not need. 2100d763cef2SBarry Smith 2101d763cef2SBarry Smith Level: intermediate 2102d763cef2SBarry Smith 210326d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 2104d763cef2SBarry Smith 2105d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 2106d763cef2SBarry Smith @*/ 2107089b2837SJed Brown PetscErrorCode TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx) 2108d763cef2SBarry Smith { 2109089b2837SJed Brown PetscErrorCode ierr; 2110089b2837SJed Brown SNES snes; 2111089b2837SJed Brown 2112d763cef2SBarry Smith PetscFunctionBegin; 2113089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2114089b2837SJed Brown ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 2115089b2837SJed Brown if (func) *func = ts->ops->rhsjacobian; 211626d46c62SHong Zhang if (ctx) *ctx = ts->jacP; 2117d763cef2SBarry Smith PetscFunctionReturn(0); 2118d763cef2SBarry Smith } 2119d763cef2SBarry Smith 21201713a123SBarry Smith #undef __FUNCT__ 21212eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 21222eca1d9cSJed Brown /*@C 21232eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 21242eca1d9cSJed Brown 21252eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 21262eca1d9cSJed Brown 21272eca1d9cSJed Brown Input Parameter: 21282eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 21292eca1d9cSJed Brown 21302eca1d9cSJed Brown Output Parameters: 21312eca1d9cSJed Brown + A - The Jacobian of F(t,U,U_t) 21322eca1d9cSJed Brown . B - The preconditioner matrix, often the same as A 21332eca1d9cSJed Brown . f - The function to compute the matrices 21342eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 21352eca1d9cSJed Brown 21362eca1d9cSJed Brown Notes: You can pass in PETSC_NULL for any return argument you do not need. 21372eca1d9cSJed Brown 21382eca1d9cSJed Brown Level: advanced 21392eca1d9cSJed Brown 21402eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 21412eca1d9cSJed Brown 21422eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 21432eca1d9cSJed Brown @*/ 21447087cfbeSBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx) 21452eca1d9cSJed Brown { 2146089b2837SJed Brown PetscErrorCode ierr; 2147089b2837SJed Brown SNES snes; 2148089b2837SJed Brown 21492eca1d9cSJed Brown PetscFunctionBegin; 2150089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2151089b2837SJed Brown ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 21522eca1d9cSJed Brown if (f) *f = ts->ops->ijacobian; 21532eca1d9cSJed Brown if (ctx) *ctx = ts->jacP; 21542eca1d9cSJed Brown PetscFunctionReturn(0); 21552eca1d9cSJed Brown } 21562eca1d9cSJed Brown 21572eca1d9cSJed Brown #undef __FUNCT__ 2158a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution" 21591713a123SBarry Smith /*@C 2160a6570f20SBarry Smith TSMonitorSolution - Monitors progress of the TS solvers by calling 21611713a123SBarry Smith VecView() for the solution at each timestep 21621713a123SBarry Smith 21631713a123SBarry Smith Collective on TS 21641713a123SBarry Smith 21651713a123SBarry Smith Input Parameters: 21661713a123SBarry Smith + ts - the TS context 21671713a123SBarry Smith . step - current time-step 2168142b95e3SSatish Balay . ptime - current time 21691713a123SBarry Smith - dummy - either a viewer or PETSC_NULL 21701713a123SBarry Smith 21711713a123SBarry Smith Level: intermediate 21721713a123SBarry Smith 21731713a123SBarry Smith .keywords: TS, vector, monitor, view 21741713a123SBarry Smith 2175a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 21761713a123SBarry Smith @*/ 21777087cfbeSBarry Smith PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy) 21781713a123SBarry Smith { 2179dfbe8321SBarry Smith PetscErrorCode ierr; 21801713a123SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 21811713a123SBarry Smith 21821713a123SBarry Smith PetscFunctionBegin; 2183a34d58ebSBarry Smith if (!dummy) { 21847adad957SLisandro Dalcin viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm); 21851713a123SBarry Smith } 21861713a123SBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 21871713a123SBarry Smith PetscFunctionReturn(0); 21881713a123SBarry Smith } 21891713a123SBarry Smith 21901713a123SBarry Smith 21916c699258SBarry Smith #undef __FUNCT__ 21926c699258SBarry Smith #define __FUNCT__ "TSSetDM" 21936c699258SBarry Smith /*@ 21946c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 21956c699258SBarry Smith 21963f9fe445SBarry Smith Logically Collective on TS and DM 21976c699258SBarry Smith 21986c699258SBarry Smith Input Parameters: 21996c699258SBarry Smith + ts - the preconditioner context 22006c699258SBarry Smith - dm - the dm 22016c699258SBarry Smith 22026c699258SBarry Smith Level: intermediate 22036c699258SBarry Smith 22046c699258SBarry Smith 22056c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 22066c699258SBarry Smith @*/ 22077087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 22086c699258SBarry Smith { 22096c699258SBarry Smith PetscErrorCode ierr; 2210089b2837SJed Brown SNES snes; 22116c699258SBarry Smith 22126c699258SBarry Smith PetscFunctionBegin; 22130700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 221470663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 22156bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 22166c699258SBarry Smith ts->dm = dm; 2217089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2218089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 22196c699258SBarry Smith PetscFunctionReturn(0); 22206c699258SBarry Smith } 22216c699258SBarry Smith 22226c699258SBarry Smith #undef __FUNCT__ 22236c699258SBarry Smith #define __FUNCT__ "TSGetDM" 22246c699258SBarry Smith /*@ 22256c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 22266c699258SBarry Smith 22273f9fe445SBarry Smith Not Collective 22286c699258SBarry Smith 22296c699258SBarry Smith Input Parameter: 22306c699258SBarry Smith . ts - the preconditioner context 22316c699258SBarry Smith 22326c699258SBarry Smith Output Parameter: 22336c699258SBarry Smith . dm - the dm 22346c699258SBarry Smith 22356c699258SBarry Smith Level: intermediate 22366c699258SBarry Smith 22376c699258SBarry Smith 22386c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 22396c699258SBarry Smith @*/ 22407087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 22416c699258SBarry Smith { 22426c699258SBarry Smith PetscFunctionBegin; 22430700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22446c699258SBarry Smith *dm = ts->dm; 22456c699258SBarry Smith PetscFunctionReturn(0); 22466c699258SBarry Smith } 22471713a123SBarry Smith 22480f5c6efeSJed Brown #undef __FUNCT__ 22490f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 22500f5c6efeSJed Brown /*@ 22510f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 22520f5c6efeSJed Brown 22533f9fe445SBarry Smith Logically Collective on SNES 22540f5c6efeSJed Brown 22550f5c6efeSJed Brown Input Parameter: 2256d42a1c89SJed Brown + snes - nonlinear solver 22570f5c6efeSJed Brown . X - the current state at which to evaluate the residual 2258d42a1c89SJed Brown - ctx - user context, must be a TS 22590f5c6efeSJed Brown 22600f5c6efeSJed Brown Output Parameter: 22610f5c6efeSJed Brown . F - the nonlinear residual 22620f5c6efeSJed Brown 22630f5c6efeSJed Brown Notes: 22640f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 22650f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 22660f5c6efeSJed Brown 22670f5c6efeSJed Brown Level: advanced 22680f5c6efeSJed Brown 22690f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 22700f5c6efeSJed Brown @*/ 22717087cfbeSBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx) 22720f5c6efeSJed Brown { 22730f5c6efeSJed Brown TS ts = (TS)ctx; 22740f5c6efeSJed Brown PetscErrorCode ierr; 22750f5c6efeSJed Brown 22760f5c6efeSJed Brown PetscFunctionBegin; 22770f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 22780f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 22790f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 22800f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 22810f5c6efeSJed Brown ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr); 22820f5c6efeSJed Brown PetscFunctionReturn(0); 22830f5c6efeSJed Brown } 22840f5c6efeSJed Brown 22850f5c6efeSJed Brown #undef __FUNCT__ 22860f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 22870f5c6efeSJed Brown /*@ 22880f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 22890f5c6efeSJed Brown 22900f5c6efeSJed Brown Collective on SNES 22910f5c6efeSJed Brown 22920f5c6efeSJed Brown Input Parameter: 22930f5c6efeSJed Brown + snes - nonlinear solver 22940f5c6efeSJed Brown . X - the current state at which to evaluate the residual 22950f5c6efeSJed Brown - ctx - user context, must be a TS 22960f5c6efeSJed Brown 22970f5c6efeSJed Brown Output Parameter: 22980f5c6efeSJed Brown + A - the Jacobian 22990f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 23000f5c6efeSJed Brown - flag - indicates any structure change in the matrix 23010f5c6efeSJed Brown 23020f5c6efeSJed Brown Notes: 23030f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 23040f5c6efeSJed Brown 23050f5c6efeSJed Brown Level: developer 23060f5c6efeSJed Brown 23070f5c6efeSJed Brown .seealso: SNESSetJacobian() 23080f5c6efeSJed Brown @*/ 23097087cfbeSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx) 23100f5c6efeSJed Brown { 23110f5c6efeSJed Brown TS ts = (TS)ctx; 23120f5c6efeSJed Brown PetscErrorCode ierr; 23130f5c6efeSJed Brown 23140f5c6efeSJed Brown PetscFunctionBegin; 23150f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 23160f5c6efeSJed Brown PetscValidHeaderSpecific(X,VEC_CLASSID,2); 23170f5c6efeSJed Brown PetscValidPointer(A,3); 23180f5c6efeSJed Brown PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 23190f5c6efeSJed Brown PetscValidPointer(B,4); 23200f5c6efeSJed Brown PetscValidHeaderSpecific(*B,MAT_CLASSID,4); 23210f5c6efeSJed Brown PetscValidPointer(flag,5); 23220f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 23230f5c6efeSJed Brown ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr); 23240f5c6efeSJed Brown PetscFunctionReturn(0); 23250f5c6efeSJed Brown } 2326325fc9f4SBarry Smith 2327325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 2328c6db04a5SJed Brown #include <mex.h> 2329325fc9f4SBarry Smith 2330325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 2331325fc9f4SBarry Smith 2332325fc9f4SBarry Smith #undef __FUNCT__ 2333325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 2334325fc9f4SBarry Smith /* 2335325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 2336325fc9f4SBarry Smith TSSetFunctionMatlab(). 2337325fc9f4SBarry Smith 2338325fc9f4SBarry Smith Collective on TS 2339325fc9f4SBarry Smith 2340325fc9f4SBarry Smith Input Parameters: 2341325fc9f4SBarry Smith + snes - the TS context 2342325fc9f4SBarry Smith - x - input vector 2343325fc9f4SBarry Smith 2344325fc9f4SBarry Smith Output Parameter: 2345325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 2346325fc9f4SBarry Smith 2347325fc9f4SBarry Smith Notes: 2348325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 2349325fc9f4SBarry Smith implementations, so most users would not generally call this routine 2350325fc9f4SBarry Smith themselves. 2351325fc9f4SBarry Smith 2352325fc9f4SBarry Smith Level: developer 2353325fc9f4SBarry Smith 2354325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2355325fc9f4SBarry Smith 2356325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2357325fc9f4SBarry Smith */ 23587087cfbeSBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx) 2359325fc9f4SBarry Smith { 2360325fc9f4SBarry Smith PetscErrorCode ierr; 2361325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2362325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 2363325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 2364325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 2365325fc9f4SBarry Smith 2366325fc9f4SBarry Smith PetscFunctionBegin; 2367325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2368325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2369325fc9f4SBarry Smith PetscValidHeaderSpecific(xdot,VEC_CLASSID,4); 2370325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 2371325fc9f4SBarry Smith PetscCheckSameComm(snes,1,x,3); 2372325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 2373325fc9f4SBarry Smith 2374325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2375325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2376880f3077SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr); 2377325fc9f4SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 2378325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2379325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 2380325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2381325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2382325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 2383325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 2384325fc9f4SBarry Smith prhs[6] = sctx->ctx; 2385325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 2386325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2387325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2388325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2389325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2390325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2391325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2392325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2393325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2394325fc9f4SBarry Smith PetscFunctionReturn(0); 2395325fc9f4SBarry Smith } 2396325fc9f4SBarry Smith 2397325fc9f4SBarry Smith 2398325fc9f4SBarry Smith #undef __FUNCT__ 2399325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 2400325fc9f4SBarry Smith /* 2401325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 2402325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 2403e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 2404325fc9f4SBarry Smith 2405325fc9f4SBarry Smith Logically Collective on TS 2406325fc9f4SBarry Smith 2407325fc9f4SBarry Smith Input Parameters: 2408325fc9f4SBarry Smith + ts - the TS context 2409325fc9f4SBarry Smith - func - function evaluation routine 2410325fc9f4SBarry Smith 2411325fc9f4SBarry Smith Calling sequence of func: 2412325fc9f4SBarry Smith $ func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx); 2413325fc9f4SBarry Smith 2414325fc9f4SBarry Smith Level: beginner 2415325fc9f4SBarry Smith 2416325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2417325fc9f4SBarry Smith 2418325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2419325fc9f4SBarry Smith */ 24207087cfbeSBarry Smith PetscErrorCode TSSetFunctionMatlab(TS snes,const char *func,mxArray *ctx) 2421325fc9f4SBarry Smith { 2422325fc9f4SBarry Smith PetscErrorCode ierr; 2423325fc9f4SBarry Smith TSMatlabContext *sctx; 2424325fc9f4SBarry Smith 2425325fc9f4SBarry Smith PetscFunctionBegin; 2426325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2427325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2428325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2429325fc9f4SBarry Smith /* 2430325fc9f4SBarry Smith This should work, but it doesn't 2431325fc9f4SBarry Smith sctx->ctx = ctx; 2432325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2433325fc9f4SBarry Smith */ 2434325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2435325fc9f4SBarry Smith ierr = TSSetIFunction(snes,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 2436325fc9f4SBarry Smith PetscFunctionReturn(0); 2437325fc9f4SBarry Smith } 2438325fc9f4SBarry Smith 2439325fc9f4SBarry Smith #undef __FUNCT__ 2440325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 2441325fc9f4SBarry Smith /* 2442325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 2443325fc9f4SBarry Smith TSSetJacobianMatlab(). 2444325fc9f4SBarry Smith 2445325fc9f4SBarry Smith Collective on TS 2446325fc9f4SBarry Smith 2447325fc9f4SBarry Smith Input Parameters: 2448325fc9f4SBarry Smith + snes - the TS context 2449325fc9f4SBarry Smith . x - input vector 2450325fc9f4SBarry Smith . A, B - the matrices 2451325fc9f4SBarry Smith - ctx - user context 2452325fc9f4SBarry Smith 2453325fc9f4SBarry Smith Output Parameter: 2454325fc9f4SBarry Smith . flag - structure of the matrix 2455325fc9f4SBarry Smith 2456325fc9f4SBarry Smith Level: developer 2457325fc9f4SBarry Smith 2458325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 2459325fc9f4SBarry Smith 2460325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2461325fc9f4SBarry Smith @*/ 24627087cfbeSBarry Smith PetscErrorCode TSComputeJacobian_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx) 2463325fc9f4SBarry Smith { 2464325fc9f4SBarry Smith PetscErrorCode ierr; 2465325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2466325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 2467325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 2468325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 2469325fc9f4SBarry Smith 2470325fc9f4SBarry Smith PetscFunctionBegin; 2471325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2472325fc9f4SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2473325fc9f4SBarry Smith 2474325fc9f4SBarry Smith /* call Matlab function in ctx with arguments x and y */ 2475325fc9f4SBarry Smith 2476325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2477325fc9f4SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2478325fc9f4SBarry Smith ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr); 2479325fc9f4SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 2480325fc9f4SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 2481325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2482325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 2483325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 2484325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 2485325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 2486325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 2487325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 2488325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 2489325fc9f4SBarry Smith prhs[8] = sctx->ctx; 2490325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 2491325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2492325fc9f4SBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 2493325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 2494325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 2495325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 2496325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 2497325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 2498325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 2499325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 2500325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 2501325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 2502325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 2503325fc9f4SBarry Smith PetscFunctionReturn(0); 2504325fc9f4SBarry Smith } 2505325fc9f4SBarry Smith 2506325fc9f4SBarry Smith 2507325fc9f4SBarry Smith #undef __FUNCT__ 2508325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 2509325fc9f4SBarry Smith /* 2510325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 2511e3c5b3baSBarry 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 2512325fc9f4SBarry Smith 2513325fc9f4SBarry Smith Logically Collective on TS 2514325fc9f4SBarry Smith 2515325fc9f4SBarry Smith Input Parameters: 2516325fc9f4SBarry Smith + snes - the TS context 2517325fc9f4SBarry Smith . A,B - Jacobian matrices 2518325fc9f4SBarry Smith . func - function evaluation routine 2519325fc9f4SBarry Smith - ctx - user context 2520325fc9f4SBarry Smith 2521325fc9f4SBarry Smith Calling sequence of func: 2522325fc9f4SBarry Smith $ flag = func (TS snes,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx); 2523325fc9f4SBarry Smith 2524325fc9f4SBarry Smith 2525325fc9f4SBarry Smith Level: developer 2526325fc9f4SBarry Smith 2527325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 2528325fc9f4SBarry Smith 2529325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2530325fc9f4SBarry Smith */ 25317087cfbeSBarry Smith PetscErrorCode TSSetJacobianMatlab(TS snes,Mat A,Mat B,const char *func,mxArray *ctx) 2532325fc9f4SBarry Smith { 2533325fc9f4SBarry Smith PetscErrorCode ierr; 2534325fc9f4SBarry Smith TSMatlabContext *sctx; 2535325fc9f4SBarry Smith 2536325fc9f4SBarry Smith PetscFunctionBegin; 2537325fc9f4SBarry Smith /* currently sctx is memory bleed */ 2538325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2539325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2540325fc9f4SBarry Smith /* 2541325fc9f4SBarry Smith This should work, but it doesn't 2542325fc9f4SBarry Smith sctx->ctx = ctx; 2543325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2544325fc9f4SBarry Smith */ 2545325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2546325fc9f4SBarry Smith ierr = TSSetIJacobian(snes,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 2547325fc9f4SBarry Smith PetscFunctionReturn(0); 2548325fc9f4SBarry Smith } 2549325fc9f4SBarry Smith 2550b5b1a830SBarry Smith #undef __FUNCT__ 2551b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 2552b5b1a830SBarry Smith /* 2553b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 2554b5b1a830SBarry Smith 2555b5b1a830SBarry Smith Collective on TS 2556b5b1a830SBarry Smith 2557b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 2558b5b1a830SBarry Smith @*/ 25597087cfbeSBarry Smith PetscErrorCode TSMonitor_Matlab(TS snes,PetscInt it, PetscReal time,Vec x, void *ctx) 2560b5b1a830SBarry Smith { 2561b5b1a830SBarry Smith PetscErrorCode ierr; 2562b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext *)ctx; 2563a530c242SBarry Smith int nlhs = 1,nrhs = 6; 2564b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 2565b5b1a830SBarry Smith long long int lx = 0,ls = 0; 2566b5b1a830SBarry Smith 2567b5b1a830SBarry Smith PetscFunctionBegin; 2568b5b1a830SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 2569b5b1a830SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,4); 2570b5b1a830SBarry Smith 2571b5b1a830SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 2572b5b1a830SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 2573b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 2574b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 2575b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 2576b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 2577b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 2578b5b1a830SBarry Smith prhs[5] = sctx->ctx; 2579b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 2580b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 2581b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 2582b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 2583b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 2584b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 2585b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 2586b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 2587b5b1a830SBarry Smith PetscFunctionReturn(0); 2588b5b1a830SBarry Smith } 2589b5b1a830SBarry Smith 2590b5b1a830SBarry Smith 2591b5b1a830SBarry Smith #undef __FUNCT__ 2592b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 2593b5b1a830SBarry Smith /* 2594b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 2595b5b1a830SBarry Smith 2596b5b1a830SBarry Smith Level: developer 2597b5b1a830SBarry Smith 2598b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 2599b5b1a830SBarry Smith 2600b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 2601b5b1a830SBarry Smith */ 26027087cfbeSBarry Smith PetscErrorCode TSMonitorSetMatlab(TS snes,const char *func,mxArray *ctx) 2603b5b1a830SBarry Smith { 2604b5b1a830SBarry Smith PetscErrorCode ierr; 2605b5b1a830SBarry Smith TSMatlabContext *sctx; 2606b5b1a830SBarry Smith 2607b5b1a830SBarry Smith PetscFunctionBegin; 2608b5b1a830SBarry Smith /* currently sctx is memory bleed */ 2609b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 2610b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 2611b5b1a830SBarry Smith /* 2612b5b1a830SBarry Smith This should work, but it doesn't 2613b5b1a830SBarry Smith sctx->ctx = ctx; 2614b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 2615b5b1a830SBarry Smith */ 2616b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 2617b5b1a830SBarry Smith ierr = TSMonitorSet(snes,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 2618b5b1a830SBarry Smith PetscFunctionReturn(0); 2619b5b1a830SBarry Smith } 2620b5b1a830SBarry Smith 2621325fc9f4SBarry Smith #endif 2622